blob: ebb5958f1c5c37e41ce8572678b60c6eefc317a2 [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{
249 struct xfs_log_item_desc *lidp;
250 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 */
263 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
264 struct xfs_buf_log_item *bip;
265 bip = (struct xfs_buf_log_item *)lidp->lid_item;
266 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));
315 panic("%s: ptrs are equal in node\n",
316 __func__);
317 }
318 }
319 }
320}
321
322/*
323 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100324 * btree leaves. THis becomes prohibitively expensive for large extent count
325 * files, so don't bother with inodes that have more than 10,000 extents in
326 * them. The btree record ordering checks will still be done, so for such large
327 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100328 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100329STATIC void
330xfs_bmap_check_leaf_extents(
331 xfs_btree_cur_t *cur, /* btree cursor or null */
332 xfs_inode_t *ip, /* incore inode pointer */
333 int whichfork) /* data or attr fork */
334{
335 struct xfs_btree_block *block; /* current btree block */
336 xfs_fsblock_t bno; /* block # of "block" */
337 xfs_buf_t *bp; /* buffer for "block" */
338 int error; /* error return value */
339 xfs_extnum_t i=0, j; /* index into the extents list */
340 xfs_ifork_t *ifp; /* fork structure */
341 int level; /* btree level, for checking */
342 xfs_mount_t *mp; /* file system mount structure */
343 __be64 *pp; /* pointer to block address */
344 xfs_bmbt_rec_t *ep; /* pointer to current extent */
345 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
346 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
347 int bp_release = 0;
348
349 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
350 return;
351 }
352
Dave Chinnere3543812016-01-08 11:28:49 +1100353 /* skip large extent count inodes */
354 if (ip->i_d.di_nextents > 10000)
355 return;
356
Dave Chinner9e5987a2013-02-25 12:31:26 +1100357 bno = NULLFSBLOCK;
358 mp = ip->i_mount;
359 ifp = XFS_IFORK_PTR(ip, whichfork);
360 block = ifp->if_broot;
361 /*
362 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
363 */
364 level = be16_to_cpu(block->bb_level);
365 ASSERT(level > 0);
366 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
367 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
368 bno = be64_to_cpu(*pp);
369
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000370 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100371 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
372 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
373
374 /*
375 * Go down the tree until leaf level is reached, following the first
376 * pointer (leftmost) at each level.
377 */
378 while (level-- > 0) {
379 /* See if buf is in cur first */
380 bp_release = 0;
381 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
382 if (!bp) {
383 bp_release = 1;
384 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
385 XFS_BMAP_BTREE_REF,
386 &xfs_bmbt_buf_ops);
387 if (error)
388 goto error_norelse;
389 }
390 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100391 if (level == 0)
392 break;
393
394 /*
395 * Check this block for basic sanity (increasing keys and
396 * no duplicate blocks).
397 */
398
399 xfs_check_block(block, mp, 0, 0);
400 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
401 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100402 XFS_WANT_CORRUPTED_GOTO(mp,
403 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100404 if (bp_release) {
405 bp_release = 0;
406 xfs_trans_brelse(NULL, bp);
407 }
408 }
409
410 /*
411 * Here with bp and block set to the leftmost leaf node in the tree.
412 */
413 i = 0;
414
415 /*
416 * Loop over all leaf nodes checking that all extents are in the right order.
417 */
418 for (;;) {
419 xfs_fsblock_t nextbno;
420 xfs_extnum_t num_recs;
421
422
423 num_recs = xfs_btree_get_numrecs(block);
424
425 /*
426 * Read-ahead the next leaf block, if any.
427 */
428
429 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
430
431 /*
432 * Check all the extents to make sure they are OK.
433 * If we had a previous block, the last entry should
434 * conform with the first entry in this one.
435 */
436
437 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
438 if (i) {
439 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
440 xfs_bmbt_disk_get_blockcount(&last) <=
441 xfs_bmbt_disk_get_startoff(ep));
442 }
443 for (j = 1; j < num_recs; j++) {
444 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
445 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
446 xfs_bmbt_disk_get_blockcount(ep) <=
447 xfs_bmbt_disk_get_startoff(nextp));
448 ep = nextp;
449 }
450
451 last = *ep;
452 i += num_recs;
453 if (bp_release) {
454 bp_release = 0;
455 xfs_trans_brelse(NULL, bp);
456 }
457 bno = nextbno;
458 /*
459 * If we've reached the end, stop.
460 */
461 if (bno == NULLFSBLOCK)
462 break;
463
464 bp_release = 0;
465 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
466 if (!bp) {
467 bp_release = 1;
468 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
469 XFS_BMAP_BTREE_REF,
470 &xfs_bmbt_buf_ops);
471 if (error)
472 goto error_norelse;
473 }
474 block = XFS_BUF_TO_BLOCK(bp);
475 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100476
Dave Chinner9e5987a2013-02-25 12:31:26 +1100477 return;
478
479error0:
480 xfs_warn(mp, "%s: at error0", __func__);
481 if (bp_release)
482 xfs_trans_brelse(NULL, bp);
483error_norelse:
484 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
485 __func__, i);
486 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
487 return;
488}
489
490/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100491 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000492 * given the caller's original parameters. Specifically check the
493 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100494 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
495 */
496STATIC void
497xfs_bmap_validate_ret(
498 xfs_fileoff_t bno,
499 xfs_filblks_t len,
500 int flags,
501 xfs_bmbt_irec_t *mval,
502 int nmap,
503 int ret_nmap)
504{
505 int i; /* index to map values */
506
507 ASSERT(ret_nmap <= nmap);
508
509 for (i = 0; i < ret_nmap; i++) {
510 ASSERT(mval[i].br_blockcount > 0);
511 if (!(flags & XFS_BMAPI_ENTIRE)) {
512 ASSERT(mval[i].br_startoff >= bno);
513 ASSERT(mval[i].br_blockcount <= len);
514 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
515 bno + len);
516 } else {
517 ASSERT(mval[i].br_startoff < bno + len);
518 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
519 bno);
520 }
521 ASSERT(i == 0 ||
522 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
523 mval[i].br_startoff);
524 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
525 mval[i].br_startblock != HOLESTARTBLOCK);
526 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
527 mval[i].br_state == XFS_EXT_UNWRITTEN);
528 }
529}
530
531#else
532#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700533#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100534#endif /* DEBUG */
535
536/*
537 * bmap free list manipulation functions
538 */
539
540/*
541 * Add the extent to the list of extents to be free at transaction end.
542 * The list is maintained sorted (by block number).
543 */
544void
545xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000546 struct xfs_mount *mp,
547 struct xfs_defer_ops *dfops,
548 xfs_fsblock_t bno,
549 xfs_filblks_t len,
550 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100551{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000552 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100553#ifdef DEBUG
554 xfs_agnumber_t agno;
555 xfs_agblock_t agbno;
556
557 ASSERT(bno != NULLFSBLOCK);
558 ASSERT(len > 0);
559 ASSERT(len <= MAXEXTLEN);
560 ASSERT(!isnullstartblock(bno));
561 agno = XFS_FSB_TO_AGNO(mp, bno);
562 agbno = XFS_FSB_TO_AGBNO(mp, bno);
563 ASSERT(agno < mp->m_sb.sb_agcount);
564 ASSERT(agbno < mp->m_sb.sb_agblocks);
565 ASSERT(len < mp->m_sb.sb_agblocks);
566 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
567#endif
568 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000569
Dave Chinner9e5987a2013-02-25 12:31:26 +1100570 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000571 new->xefi_startblock = bno;
572 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000573 if (oinfo)
574 new->xefi_oinfo = *oinfo;
575 else
576 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000577 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
578 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000579 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100580}
581
582/*
583 * Inode fork format manipulation functions
584 */
585
586/*
587 * Transform a btree format file with only one leaf node, where the
588 * extents list will fit in the inode, into an extents format file.
589 * Since the file extents are already in-core, all we have to do is
590 * give up the space for the btree root and pitch the leaf block.
591 */
592STATIC int /* error */
593xfs_bmap_btree_to_extents(
594 xfs_trans_t *tp, /* transaction pointer */
595 xfs_inode_t *ip, /* incore inode pointer */
596 xfs_btree_cur_t *cur, /* btree cursor */
597 int *logflagsp, /* inode logging flags */
598 int whichfork) /* data or attr fork */
599{
600 /* REFERENCED */
601 struct xfs_btree_block *cblock;/* child btree block */
602 xfs_fsblock_t cbno; /* child block number */
603 xfs_buf_t *cbp; /* child block's buffer */
604 int error; /* error return value */
605 xfs_ifork_t *ifp; /* inode fork data */
606 xfs_mount_t *mp; /* mount point structure */
607 __be64 *pp; /* ptr to block address */
608 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000609 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100610
611 mp = ip->i_mount;
612 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700613 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100614 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
615 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
616 rblock = ifp->if_broot;
617 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
618 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
619 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
620 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
621 cbno = be64_to_cpu(*pp);
622 *logflagsp = 0;
623#ifdef DEBUG
Darrick J. Wongf1357612017-10-17 21:37:33 -0700624 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
625 xfs_btree_check_lptr(cur, cbno, 1));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100626#endif
627 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
628 &xfs_bmbt_buf_ops);
629 if (error)
630 return error;
631 cblock = XFS_BUF_TO_BLOCK(cbp);
632 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
633 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000634 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
635 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100636 ip->i_d.di_nblocks--;
637 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
638 xfs_trans_binval(tp, cbp);
639 if (cur->bc_bufs[0] == cbp)
640 cur->bc_bufs[0] = NULL;
641 xfs_iroot_realloc(ip, -1, whichfork);
642 ASSERT(ifp->if_broot == NULL);
643 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
644 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
645 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
646 return 0;
647}
648
649/*
650 * Convert an extents-format file into a btree-format file.
651 * The new file will have a root block (in the inode) and a single child block.
652 */
653STATIC int /* error */
654xfs_bmap_extents_to_btree(
655 xfs_trans_t *tp, /* transaction pointer */
656 xfs_inode_t *ip, /* incore inode pointer */
657 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000658 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100659 xfs_btree_cur_t **curp, /* cursor returned to caller */
660 int wasdel, /* converting a delayed alloc */
661 int *logflagsp, /* inode logging flags */
662 int whichfork) /* data or attr fork */
663{
664 struct xfs_btree_block *ablock; /* allocated (child) bt block */
665 xfs_buf_t *abp; /* buffer for ablock */
666 xfs_alloc_arg_t args; /* allocation arguments */
667 xfs_bmbt_rec_t *arp; /* child record pointer */
668 struct xfs_btree_block *block; /* btree root block */
669 xfs_btree_cur_t *cur; /* bmap btree cursor */
670 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
671 int error; /* error return value */
672 xfs_extnum_t i, cnt; /* extent record index */
673 xfs_ifork_t *ifp; /* inode fork pointer */
674 xfs_bmbt_key_t *kp; /* root block key pointer */
675 xfs_mount_t *mp; /* mount structure */
676 xfs_extnum_t nextents; /* number of file extents */
677 xfs_bmbt_ptr_t *pp; /* root block address pointer */
678
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500679 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700680 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100681 ifp = XFS_IFORK_PTR(ip, whichfork);
682 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
683
684 /*
685 * Make space in the inode incore.
686 */
687 xfs_iroot_realloc(ip, 1, whichfork);
688 ifp->if_flags |= XFS_IFBROOT;
689
690 /*
691 * Fill in the root.
692 */
693 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800694 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
695 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800696 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100697 /*
698 * Need a cursor. Can't allocate until bb_level is filled in.
699 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100700 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
701 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000702 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100703 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
704 /*
705 * Convert to a btree with two levels, one record in root.
706 */
707 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
708 memset(&args, 0, sizeof(args));
709 args.tp = tp;
710 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000711 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100712 args.firstblock = *firstblock;
713 if (*firstblock == NULLFSBLOCK) {
714 args.type = XFS_ALLOCTYPE_START_BNO;
715 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000716 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100717 args.type = XFS_ALLOCTYPE_START_BNO;
718 args.fsbno = *firstblock;
719 } else {
720 args.type = XFS_ALLOCTYPE_NEAR_BNO;
721 args.fsbno = *firstblock;
722 }
723 args.minlen = args.maxlen = args.prod = 1;
724 args.wasdel = wasdel;
725 *logflagsp = 0;
726 if ((error = xfs_alloc_vextent(&args))) {
727 xfs_iroot_realloc(ip, -1, whichfork);
728 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
729 return error;
730 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700731
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800732 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
733 xfs_iroot_realloc(ip, -1, whichfork);
734 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
735 return -ENOSPC;
736 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100737 /*
738 * Allocation can't fail, the space was reserved.
739 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100740 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800741 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100742 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
743 cur->bc_private.b.allocated++;
744 ip->i_d.di_nblocks++;
745 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
746 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
747 /*
748 * Fill in the child block.
749 */
750 abp->b_ops = &xfs_bmbt_buf_ops;
751 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800752 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
753 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500754 XFS_BTREE_LONG_PTRS);
755
Dave Chinner9e5987a2013-02-25 12:31:26 +1100756 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100757 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100758 for (cnt = i = 0; i < nextents; i++) {
759 ep = xfs_iext_get_ext(ifp, i);
760 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
761 arp->l0 = cpu_to_be64(ep->l0);
762 arp->l1 = cpu_to_be64(ep->l1);
763 arp++; cnt++;
764 }
765 }
766 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
767 xfs_btree_set_numrecs(ablock, cnt);
768
769 /*
770 * Fill in the root key and pointer.
771 */
772 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
773 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
774 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
775 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
776 be16_to_cpu(block->bb_level)));
777 *pp = cpu_to_be64(args.fsbno);
778
779 /*
780 * Do all this logging at the end so that
781 * the root is at the right level.
782 */
783 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
784 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
785 ASSERT(*curp == NULL);
786 *curp = cur;
787 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
788 return 0;
789}
790
791/*
792 * Convert a local file to an extents file.
793 * This code is out of bounds for data forks of regular files,
794 * since the file data needs to get logged so things will stay consistent.
795 * (The bmap-level manipulations are ok, though).
796 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000797void
798xfs_bmap_local_to_extents_empty(
799 struct xfs_inode *ip,
800 int whichfork)
801{
802 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
803
Darrick J. Wong60b49842016-10-03 09:11:34 -0700804 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000805 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
806 ASSERT(ifp->if_bytes == 0);
807 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
808
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000809 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000810 ifp->if_flags &= ~XFS_IFINLINE;
811 ifp->if_flags |= XFS_IFEXTENTS;
812 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
813}
814
815
Dave Chinner9e5987a2013-02-25 12:31:26 +1100816STATIC int /* error */
817xfs_bmap_local_to_extents(
818 xfs_trans_t *tp, /* transaction pointer */
819 xfs_inode_t *ip, /* incore inode pointer */
820 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
821 xfs_extlen_t total, /* total blocks needed by transaction */
822 int *logflagsp, /* inode logging flags */
823 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500824 void (*init_fn)(struct xfs_trans *tp,
825 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100826 struct xfs_inode *ip,
827 struct xfs_ifork *ifp))
828{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000829 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100830 int flags; /* logging flags returned */
831 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000832 xfs_alloc_arg_t args; /* allocation arguments */
833 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700834 struct xfs_bmbt_irec rec;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100835
836 /*
837 * We don't want to deal with the case of keeping inode data inline yet.
838 * So sending the data fork of a regular inode is invalid.
839 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100840 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100841 ifp = XFS_IFORK_PTR(ip, whichfork);
842 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000843
844 if (!ifp->if_bytes) {
845 xfs_bmap_local_to_extents_empty(ip, whichfork);
846 flags = XFS_ILOG_CORE;
847 goto done;
848 }
849
Dave Chinner9e5987a2013-02-25 12:31:26 +1100850 flags = 0;
851 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000852 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
853 XFS_IFINLINE);
854 memset(&args, 0, sizeof(args));
855 args.tp = tp;
856 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000857 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000858 args.firstblock = *firstblock;
859 /*
860 * Allocate a block. We know we need only one, since the
861 * file currently fits in an inode.
862 */
863 if (*firstblock == NULLFSBLOCK) {
864 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
865 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100866 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000867 args.fsbno = *firstblock;
868 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100869 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000870 args.total = total;
871 args.minlen = args.maxlen = args.prod = 1;
872 error = xfs_alloc_vextent(&args);
873 if (error)
874 goto done;
875
876 /* Can't fail, the space was reserved. */
877 ASSERT(args.fsbno != NULLFSBLOCK);
878 ASSERT(args.len == 1);
879 *firstblock = args.fsbno;
880 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
881
Dave Chinnerfe22d552015-01-22 09:30:06 +1100882 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100883 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100884 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100885 * The callout is responsible for logging because the remote format
886 * might differ from the local format and thus we don't know how much to
887 * log here. Note that init_fn must also set the buffer log item type
888 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100889 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000890 init_fn(tp, bp, ip, ifp);
891
Brian Fosterb7cdc662015-10-12 15:40:24 +1100892 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000893 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
894 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100895 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000896
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700897 rec.br_startoff = 0;
898 rec.br_startblock = args.fsbno;
899 rec.br_blockcount = 1;
900 rec.br_state = XFS_EXT_NORM;
901 xfs_iext_insert(ip, 0, 1, &rec, 0);
902
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000903 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
904 ip->i_d.di_nblocks = 1;
905 xfs_trans_mod_dquot_byino(tp, ip,
906 XFS_TRANS_DQ_BCOUNT, 1L);
907 flags |= xfs_ilog_fext(whichfork);
908
Dave Chinner9e5987a2013-02-25 12:31:26 +1100909done:
910 *logflagsp = flags;
911 return error;
912}
913
914/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 * Called from xfs_bmap_add_attrfork to handle btree format files.
916 */
917STATIC int /* error */
918xfs_bmap_add_attrfork_btree(
919 xfs_trans_t *tp, /* transaction pointer */
920 xfs_inode_t *ip, /* incore inode pointer */
921 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000922 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 int *flags) /* inode logging flags */
924{
925 xfs_btree_cur_t *cur; /* btree cursor */
926 int error; /* error return value */
927 xfs_mount_t *mp; /* file system mount struct */
928 int stat; /* newroot status */
929
930 mp = ip->i_mount;
931 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
932 *flags |= XFS_ILOG_DBROOT;
933 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100934 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000935 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 cur->bc_private.b.firstblock = *firstblock;
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700937 error = xfs_bmbt_lookup_first(cur, &stat);
938 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000940 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100941 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100942 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 goto error0;
944 if (stat == 0) {
945 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000946 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
948 *firstblock = cur->bc_private.b.firstblock;
949 cur->bc_private.b.allocated = 0;
950 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
951 }
952 return 0;
953error0:
954 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
955 return error;
956}
957
958/*
959 * Called from xfs_bmap_add_attrfork to handle extents format files.
960 */
961STATIC int /* error */
962xfs_bmap_add_attrfork_extents(
963 xfs_trans_t *tp, /* transaction pointer */
964 xfs_inode_t *ip, /* incore inode pointer */
965 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000966 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 int *flags) /* inode logging flags */
968{
969 xfs_btree_cur_t *cur; /* bmap btree cursor */
970 int error; /* error return value */
971
972 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
973 return 0;
974 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000975 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 flags, XFS_DATA_FORK);
977 if (cur) {
978 cur->bc_private.b.allocated = 0;
979 xfs_btree_del_cursor(cur,
980 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
981 }
982 return error;
983}
984
985/*
Dave Chinner1e823792013-02-11 15:58:13 +1100986 * Called from xfs_bmap_add_attrfork to handle local format files. Each
987 * different data fork content type needs a different callout to do the
988 * conversion. Some are basic and only require special block initialisation
989 * callouts for the data formating, others (directories) are so specialised they
990 * handle everything themselves.
991 *
992 * XXX (dgc): investigate whether directory conversion can use the generic
993 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500994 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 */
996STATIC int /* error */
997xfs_bmap_add_attrfork_local(
998 xfs_trans_t *tp, /* transaction pointer */
999 xfs_inode_t *ip, /* incore inode pointer */
1000 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001001 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int *flags) /* inode logging flags */
1003{
1004 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1007 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001008
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001009 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001011 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 dargs.dp = ip;
1013 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001014 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001015 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 dargs.whichfork = XFS_DATA_FORK;
1017 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001018 return xfs_dir2_sf_to_block(&dargs);
1019 }
1020
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001021 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001022 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1023 flags, XFS_DATA_FORK,
1024 xfs_symlink_local_to_remote);
1025
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001026 /* should only be called for types that support local format data */
1027 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001028 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
1031/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001032 * Convert inode from non-attributed to attributed.
1033 * Must not be in a transaction, ip must not be locked.
1034 */
1035int /* error code */
1036xfs_bmap_add_attrfork(
1037 xfs_inode_t *ip, /* incore inode pointer */
1038 int size, /* space new attribute needs */
1039 int rsvd) /* xact may use reserved blks */
1040{
1041 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001042 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001043 xfs_mount_t *mp; /* mount structure */
1044 xfs_trans_t *tp; /* transaction pointer */
1045 int blks; /* space reservation */
1046 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001047 int logflags; /* logging flags */
1048 int error; /* error return value */
1049
1050 ASSERT(XFS_IFORK_Q(ip) == 0);
1051
1052 mp = ip->i_mount;
1053 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001054
Dave Chinner9e5987a2013-02-25 12:31:26 +11001055 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001056
1057 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1058 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1059 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001060 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001061
Dave Chinner9e5987a2013-02-25 12:31:26 +11001062 xfs_ilock(ip, XFS_ILOCK_EXCL);
1063 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1064 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1065 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001066 if (error)
1067 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001068 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001069 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001070 if (ip->i_d.di_anextents != 0) {
1071 error = -EFSCORRUPTED;
1072 goto trans_cancel;
1073 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001074 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1075 /*
1076 * For inodes coming from pre-6.2 filesystems.
1077 */
1078 ASSERT(ip->i_d.di_aformat == 0);
1079 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1080 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001081
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001082 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001083 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1084
1085 switch (ip->i_d.di_format) {
1086 case XFS_DINODE_FMT_DEV:
1087 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1088 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001089 case XFS_DINODE_FMT_LOCAL:
1090 case XFS_DINODE_FMT_EXTENTS:
1091 case XFS_DINODE_FMT_BTREE:
1092 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1093 if (!ip->i_d.di_forkoff)
1094 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1095 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1096 version = 2;
1097 break;
1098 default:
1099 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001100 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001101 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001102 }
1103
1104 ASSERT(ip->i_afp == NULL);
1105 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1106 ip->i_afp->if_flags = XFS_IFEXTENTS;
1107 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001108 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001109 switch (ip->i_d.di_format) {
1110 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001111 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001112 &logflags);
1113 break;
1114 case XFS_DINODE_FMT_EXTENTS:
1115 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001116 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001117 break;
1118 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001119 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001120 &logflags);
1121 break;
1122 default:
1123 error = 0;
1124 break;
1125 }
1126 if (logflags)
1127 xfs_trans_log_inode(tp, ip, logflags);
1128 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001129 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001130 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1131 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001132 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001133
1134 spin_lock(&mp->m_sb_lock);
1135 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1136 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001137 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001138 }
1139 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1140 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001141 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001142 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001143 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001144 if (log_sb)
1145 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001146 }
1147
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001148 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001149 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001150 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001151 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001152 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001153 return error;
1154
1155bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001156 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001157trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001158 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001159 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001160 return error;
1161}
1162
1163/*
1164 * Internal and external extent tree search functions.
1165 */
1166
1167/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001168 * Read in extents from a btree-format inode.
Dave Chinner9e5987a2013-02-25 12:31:26 +11001169 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001170int
1171xfs_iread_extents(
1172 struct xfs_trans *tp,
1173 struct xfs_inode *ip,
1174 int whichfork)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001175{
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001176 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001177 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001178 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1179 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1180 struct xfs_btree_block *block = ifp->if_broot;
1181 xfs_fsblock_t bno;
1182 struct xfs_buf *bp;
1183 xfs_extnum_t i, j;
1184 int level;
1185 __be64 *pp;
1186 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001187
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001188 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1189
1190 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1191 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1192 return -EFSCORRUPTED;
1193 }
1194
1195 ifp->if_bytes = 0;
1196 ifp->if_real_bytes = 0;
1197 xfs_iext_add(ifp, 0, nextents);
1198
Dave Chinner9e5987a2013-02-25 12:31:26 +11001199 /*
1200 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1201 */
1202 level = be16_to_cpu(block->bb_level);
1203 ASSERT(level > 0);
1204 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1205 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001206
Dave Chinner9e5987a2013-02-25 12:31:26 +11001207 /*
1208 * Go down the tree until leaf level is reached, following the first
1209 * pointer (leftmost) at each level.
1210 */
1211 while (level-- > 0) {
1212 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1213 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1214 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001215 goto out;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001216 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001217 if (level == 0)
1218 break;
1219 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1220 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001221 XFS_WANT_CORRUPTED_GOTO(mp,
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001222 XFS_FSB_SANITY_CHECK(mp, bno), out_brelse);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001223 xfs_trans_brelse(tp, bp);
1224 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001225
Dave Chinner9e5987a2013-02-25 12:31:26 +11001226 /*
1227 * Here with bp and block set to the leftmost leaf node in the tree.
1228 */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001229 i = 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001230
Dave Chinner9e5987a2013-02-25 12:31:26 +11001231 /*
1232 * Loop over all leaf nodes. Copy information to the extent records.
1233 */
1234 for (;;) {
1235 xfs_bmbt_rec_t *frp;
1236 xfs_fsblock_t nextbno;
1237 xfs_extnum_t num_recs;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001238
1239 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001240 if (unlikely(i + num_recs > nextents)) {
1241 ASSERT(i + num_recs <= nextents);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001242 xfs_warn(ip->i_mount,
1243 "corrupt dinode %Lu, (btree extents).",
1244 (unsigned long long) ip->i_ino);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001245 XFS_CORRUPTION_ERROR(__func__,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001246 XFS_ERRLEVEL_LOW, ip->i_mount, block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001247 error = -EFSCORRUPTED;
1248 goto out_brelse;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001249 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001250 /*
1251 * Read-ahead the next leaf block, if any.
1252 */
1253 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1254 if (nextbno != NULLFSBLOCK)
1255 xfs_btree_reada_bufl(mp, nextbno, 1,
1256 &xfs_bmbt_buf_ops);
1257 /*
1258 * Copy records into the extent records.
1259 */
1260 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001261 for (j = 0; j < num_recs; j++, i++, frp++) {
1262 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1263 trp->l0 = be64_to_cpu(frp->l0);
1264 trp->l1 = be64_to_cpu(frp->l1);
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001265 if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001266 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001267 XFS_ERRLEVEL_LOW, mp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001268 error = -EFSCORRUPTED;
1269 goto out_brelse;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001270 }
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001271 trace_xfs_read_extent(ip, i, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001272 }
1273 xfs_trans_brelse(tp, bp);
1274 bno = nextbno;
1275 /*
1276 * If we've reached the end, stop.
1277 */
1278 if (bno == NULLFSBLOCK)
1279 break;
1280 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1281 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1282 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001283 goto out;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001284 block = XFS_BUF_TO_BLOCK(bp);
1285 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001286
1287 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1288 error = -EFSCORRUPTED;
1289 goto out;
1290 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001291 ASSERT(i == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001292
1293 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001294 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001295
1296out_brelse:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001297 xfs_trans_brelse(tp, bp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001298out:
1299 xfs_iext_destroy(ifp);
1300 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001301}
1302
Dave Chinner9e5987a2013-02-25 12:31:26 +11001303/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001304 * Returns the relative block number of the first unused block(s) in the given
1305 * fork with at least "len" logically contiguous blocks free. This is the
1306 * lowest-address hole if the fork has holes, else the first block past the end
1307 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a2013-02-25 12:31:26 +11001308 */
1309int /* error */
1310xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001311 struct xfs_trans *tp, /* transaction pointer */
1312 struct xfs_inode *ip, /* incore inode */
1313 xfs_extlen_t len, /* size of hole to find */
1314 xfs_fileoff_t *first_unused, /* unused block */
1315 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001316{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001317 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1318 struct xfs_bmbt_irec got;
1319 xfs_extnum_t idx = 0;
1320 xfs_fileoff_t lastaddr = 0;
1321 xfs_fileoff_t lowest, max;
1322 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001323
1324 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1325 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1326 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001327
Dave Chinner9e5987a2013-02-25 12:31:26 +11001328 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1329 *first_unused = 0;
1330 return 0;
1331 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001332
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001333 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1334 error = xfs_iread_extents(tp, ip, whichfork);
1335 if (error)
1336 return error;
1337 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001338
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001339 lowest = max = *first_unused;
1340 while (xfs_iext_get_extent(ifp, idx++, &got)) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001341 /*
1342 * See if the hole before this extent will work.
1343 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001344 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001345 got.br_startoff - max >= len)
1346 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001347 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001348 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1349 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001350
Dave Chinner9e5987a2013-02-25 12:31:26 +11001351 *first_unused = max;
1352 return 0;
1353}
1354
1355/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001356 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001357 * last_block (input value) in the file.
1358 * This is not based on i_size, it is based on the extent records.
1359 * Returns 0 for local files, as they do not have extent records.
1360 */
1361int /* error */
1362xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001363 struct xfs_trans *tp, /* transaction pointer */
1364 struct xfs_inode *ip, /* incore inode */
1365 xfs_fileoff_t *last_block, /* last block */
1366 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001367{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001368 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1369 struct xfs_bmbt_irec got;
1370 xfs_extnum_t idx;
1371 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001372
Christoph Hellwig86685f72016-11-24 11:39:38 +11001373 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1374 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001375 *last_block = 0;
1376 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001377 case XFS_DINODE_FMT_BTREE:
1378 case XFS_DINODE_FMT_EXTENTS:
1379 break;
1380 default:
1381 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001382 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001383
1384 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1385 error = xfs_iread_extents(tp, ip, whichfork);
1386 if (error)
1387 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001388 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001389
Christoph Hellwigdc560152017-10-23 16:32:39 -07001390 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &idx, &got))
1391 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001392 return 0;
1393}
1394
Dave Chinner68988112013-08-12 20:49:42 +10001395int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001396xfs_bmap_last_extent(
1397 struct xfs_trans *tp,
1398 struct xfs_inode *ip,
1399 int whichfork,
1400 struct xfs_bmbt_irec *rec,
1401 int *is_empty)
1402{
1403 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1404 int error;
1405 int nextents;
1406
1407 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1408 error = xfs_iread_extents(tp, ip, whichfork);
1409 if (error)
1410 return error;
1411 }
1412
Eric Sandeen5d829302016-11-08 12:59:42 +11001413 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001414 if (nextents == 0) {
1415 *is_empty = 1;
1416 return 0;
1417 }
1418
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001419 xfs_iext_get_extent(ifp, nextents - 1, rec);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001420 *is_empty = 0;
1421 return 0;
1422}
1423
1424/*
1425 * Check the last inode extent to determine whether this allocation will result
1426 * in blocks being allocated at the end of the file. When we allocate new data
1427 * blocks at the end of the file which do not start at the previous data block,
1428 * we will try to align the new blocks at stripe unit boundaries.
1429 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001430 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001431 * at, or past the EOF.
1432 */
1433STATIC int
1434xfs_bmap_isaeof(
1435 struct xfs_bmalloca *bma,
1436 int whichfork)
1437{
1438 struct xfs_bmbt_irec rec;
1439 int is_empty;
1440 int error;
1441
Thomas Meyer749f24f2017-10-09 11:38:54 -07001442 bma->aeof = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001443 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1444 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001445 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001446 return error;
1447
Dave Chinner6e708bc2013-11-22 10:41:16 +11001448 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001449 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001450 return 0;
1451 }
1452
Dave Chinner9e5987a2013-02-25 12:31:26 +11001453 /*
1454 * Check if we are allocation or past the last extent, or at least into
1455 * the last delayed allocated extent.
1456 */
1457 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1458 (bma->offset >= rec.br_startoff &&
1459 isnullstartblock(rec.br_startblock));
1460 return 0;
1461}
1462
1463/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001464 * Returns the file-relative block number of the first block past eof in
1465 * the file. This is not based on i_size, it is based on the extent records.
1466 * Returns 0 for local files, as they do not have extent records.
1467 */
1468int
1469xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001470 struct xfs_inode *ip,
1471 xfs_fileoff_t *last_block,
1472 int whichfork)
1473{
1474 struct xfs_bmbt_irec rec;
1475 int is_empty;
1476 int error;
1477
1478 *last_block = 0;
1479
1480 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1481 return 0;
1482
1483 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1484 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001485 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001486
1487 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1488 if (error || is_empty)
1489 return error;
1490
1491 *last_block = rec.br_startoff + rec.br_blockcount;
1492 return 0;
1493}
1494
1495/*
1496 * Returns whether the selected fork of the inode has exactly one
1497 * block or not. For the data fork we check this matches di_size,
1498 * implying the file's range is 0..bsize-1.
1499 */
1500int /* 1=>1 block, 0=>otherwise */
1501xfs_bmap_one_block(
1502 xfs_inode_t *ip, /* incore inode */
1503 int whichfork) /* data or attr fork */
1504{
Dave Chinner9e5987a2013-02-25 12:31:26 +11001505 xfs_ifork_t *ifp; /* inode fork pointer */
1506 int rval; /* return value */
1507 xfs_bmbt_irec_t s; /* internal version of extent */
1508
1509#ifndef DEBUG
1510 if (whichfork == XFS_DATA_FORK)
1511 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1512#endif /* !DEBUG */
1513 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1514 return 0;
1515 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1516 return 0;
1517 ifp = XFS_IFORK_PTR(ip, whichfork);
1518 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001519 xfs_iext_get_extent(ifp, 0, &s);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001520 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1521 if (rval && whichfork == XFS_DATA_FORK)
1522 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1523 return rval;
1524}
1525
1526/*
1527 * Extent tree manipulation functions used during allocation.
1528 */
1529
1530/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001531 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 */
1533STATIC int /* error */
1534xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001535 struct xfs_bmalloca *bma,
1536 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001538 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001541 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 xfs_fileoff_t new_endoff; /* end offset of new entry */
1543 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1544 /* left is 0, right is 1, prev is 2 */
1545 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001546 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001547 xfs_filblks_t da_new; /* new count del alloc blocks used */
1548 xfs_filblks_t da_old; /* old count del alloc blocks used */
1549 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001551 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001552 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001553 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001555 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001556 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001557 ASSERT(whichfork != XFS_ATTR_FORK);
1558 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1559 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001560
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001561 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001562 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001563 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001564 ASSERT(!bma->cur ||
1565 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001566
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001567 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569#define LEFT r[0]
1570#define RIGHT r[1]
1571#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 /*
1574 * Set up a bunch of variables to make the tests simpler.
1575 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001576 xfs_iext_get_extent(ifp, bma->idx, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001578 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 ASSERT(PREV.br_startoff <= new->br_startoff);
1580 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001581
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001582 da_old = startblockval(PREV.br_startblock);
1583 da_new = 0;
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 /*
1586 * Set flags determining what part of the previous delayed allocation
1587 * extent is being replaced by a real allocation.
1588 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001589 if (PREV.br_startoff == new->br_startoff)
1590 state |= BMAP_LEFT_FILLING;
1591 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1592 state |= BMAP_RIGHT_FILLING;
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 /*
1595 * Check and set flags if this segment has a left neighbor.
1596 * Don't set contiguous if the combined extent would be too large.
1597 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001598 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001599 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001600 xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001601
1602 if (isnullstartblock(LEFT.br_startblock))
1603 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001605
1606 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1607 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1608 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1609 LEFT.br_state == new->br_state &&
1610 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1611 state |= BMAP_LEFT_CONTIG;
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 /*
1614 * Check and set flags if this segment has a right neighbor.
1615 * Don't set contiguous if the combined extent would be too large.
1616 * Also check for all-three-contiguous being too large.
1617 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001618 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001619 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001620 xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001621
1622 if (isnullstartblock(RIGHT.br_startblock))
1623 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001625
1626 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1627 new_endoff == RIGHT.br_startoff &&
1628 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1629 new->br_state == RIGHT.br_state &&
1630 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1631 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1632 BMAP_RIGHT_FILLING)) !=
1633 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1634 BMAP_RIGHT_FILLING) ||
1635 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1636 <= MAXEXTLEN))
1637 state |= BMAP_RIGHT_CONTIG;
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 error = 0;
1640 /*
1641 * Switch out based on the FILLING and CONTIG state bits.
1642 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001643 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1644 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1645 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1646 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 /*
1648 * Filling in all of a previously delayed allocation extent.
1649 * The left and right neighbors are both contiguous with new.
1650 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001651 bma->idx--;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001652 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001653 xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001654
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001655 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001656 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001657 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1659 else {
1660 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001661 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001662 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001664 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001665 error = xfs_btree_delete(bma->cur, &i);
1666 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001668 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001669 error = xfs_btree_decrement(bma->cur, 0, &i);
1670 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001672 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001673 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001674 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 goto done;
1676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 break;
1678
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001679 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 /*
1681 * Filling in all of a previously delayed allocation extent.
1682 * The left neighbor is contiguous, the right is not.
1683 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001684 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001685
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001686 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001687 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001688 xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001689
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001690 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1691 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 rval = XFS_ILOG_DEXT;
1693 else {
1694 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001695 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001696 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001698 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001699 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001700 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 goto done;
1702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 break;
1704
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001705 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /*
1707 * Filling in all of a previously delayed allocation extent.
1708 * The right neighbor is contiguous, the left is not.
1709 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001710 PREV.br_startblock = new->br_startblock;
1711 PREV.br_blockcount += RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001712 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001713
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001714 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1715 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 rval = XFS_ILOG_DEXT;
1717 else {
1718 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001719 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001720 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001722 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001723 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001724 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 goto done;
1726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 break;
1728
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001729 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /*
1731 * Filling in all of a previously delayed allocation extent.
1732 * Neither the left nor right neighbors are contiguous with
1733 * the new one.
1734 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001735 PREV.br_startblock = new->br_startblock;
1736 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001737 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001738
Darrick J. Wong60b49842016-10-03 09:11:34 -07001739 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001740 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1742 else {
1743 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001744 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001745 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001747 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001748 error = xfs_btree_insert(bma->cur, &i);
1749 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001751 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 break;
1754
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001755 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /*
1757 * Filling in the first part of a previous delayed allocation.
1758 * The left neighbor is contiguous.
1759 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001760 old = LEFT;
1761 temp = PREV.br_blockcount - new->br_blockcount;
1762 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1763 startblockval(PREV.br_startblock));
1764
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001765 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001766 xfs_iext_update_extent(bma->ip, state, bma->idx - 1, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001767
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001768 PREV.br_blockcount = temp = PREV.br_blockcount - new->br_blockcount;
1769 PREV.br_startoff += new->br_blockcount;
1770 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001771 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001772
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001773 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 rval = XFS_ILOG_DEXT;
1775 else {
1776 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001777 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001778 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001780 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001781 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001782 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 goto done;
1784 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001785
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001786 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 break;
1788
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001789 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /*
1791 * Filling in the first part of a previous delayed allocation.
1792 * The left neighbor is not contiguous.
1793 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001794 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001795 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001796 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1798 else {
1799 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001800 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001801 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001803 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001804 error = xfs_btree_insert(bma->cur, &i);
1805 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001807 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001809
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001810 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001811 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001812 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001813 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 rval |= tmp_rval;
1815 if (error)
1816 goto done;
1817 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001818
1819 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001820 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001821 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001822 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001823
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001824 PREV.br_startoff = new_endoff;
1825 PREV.br_blockcount = temp;
1826 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001827 xfs_iext_update_extent(bma->ip, state, bma->idx + 1, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 break;
1829
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001830 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 /*
1832 * Filling in the last part of a previous delayed allocation.
1833 * The right neighbor is contiguous with the new allocation.
1834 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001835 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001836 RIGHT.br_startoff = new->br_startoff;
1837 RIGHT.br_startblock = new->br_startblock;
1838 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001839 xfs_iext_update_extent(bma->ip, state, bma->idx + 1, &RIGHT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001840
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001841 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 rval = XFS_ILOG_DEXT;
1843 else {
1844 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001845 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001846 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001848 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001849 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001850 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 goto done;
1852 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001853
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001854 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001855 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001856 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001857
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001858 PREV.br_blockcount = temp;
1859 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001860 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001861
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001862 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 break;
1864
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001865 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 /*
1867 * Filling in the last part of a previous delayed allocation.
1868 * The right neighbor is not contiguous.
1869 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001870 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001871 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001872 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1874 else {
1875 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001876 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001877 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001879 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001880 error = xfs_btree_insert(bma->cur, &i);
1881 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001883 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001885
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001886 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001887 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001888 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001889 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 rval |= tmp_rval;
1891 if (error)
1892 goto done;
1893 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001894
1895 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001896 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001897 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001898 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001899
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001900 PREV.br_startblock = nullstartblock(da_new);
1901 PREV.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001902 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001903
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001904 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 break;
1906
1907 case 0:
1908 /*
1909 * Filling in the middle part of a previous delayed allocation.
1910 * Contiguity is impossible here.
1911 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001912 *
1913 * We start with a delayed allocation:
1914 *
1915 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1916 * PREV @ idx
1917 *
1918 * and we are allocating:
1919 * +rrrrrrrrrrrrrrrrr+
1920 * new
1921 *
1922 * and we set it up for insertion as:
1923 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1924 * new
1925 * PREV @ idx LEFT RIGHT
1926 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001928 old = PREV;
1929
1930 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001931 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001932
1933 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001934 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001935 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001936 RIGHT.br_blockcount =
1937 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1938 RIGHT.br_startblock =
1939 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1940 RIGHT.br_blockcount));
1941
1942 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001943 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1944 PREV.br_startblock =
1945 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1946 PREV.br_blockcount));
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001947 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001948
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001949 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001950 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001951 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001952
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001953 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1955 else {
1956 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001957 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001960 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001961 error = xfs_btree_insert(bma->cur, &i);
1962 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001964 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001966
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001967 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001968 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001969 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001970 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 rval |= tmp_rval;
1972 if (error)
1973 goto done;
1974 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001975
1976 da_new = startblockval(PREV.br_startblock) +
1977 startblockval(RIGHT.br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001978 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 break;
1980
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001981 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1982 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1983 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1984 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1985 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1986 case BMAP_LEFT_CONTIG:
1987 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /*
1989 * These cases are all impossible.
1990 */
1991 ASSERT(0);
1992 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001993
Darrick J. Wong9c194642016-08-03 12:16:05 +10001994 /* add reverse mapping */
1995 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
1996 if (error)
1997 goto done;
1998
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001999 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002000 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002001 int tmp_logflags; /* partial log flag return val */
2002
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002003 ASSERT(bma->cur == NULL);
2004 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002005 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002006 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002007 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002008 if (error)
2009 goto done;
2010 }
2011
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002012 if (bma->cur) {
2013 da_new += bma->cur->bc_private.b.allocated;
2014 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002015 }
2016
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002017 /* adjust for changes in reserved delayed indirect blocks */
2018 if (da_new != da_old) {
2019 ASSERT(state == 0 || da_new < da_old);
2020 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2021 false);
2022 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002023
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002024 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002026 if (whichfork != XFS_COW_FORK)
2027 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 return error;
2029#undef LEFT
2030#undef RIGHT
2031#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032}
2033
2034/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002035 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 */
2037STATIC int /* error */
2038xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002039 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002041 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002042 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002044 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002045 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002046 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002047 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002052 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2055 /* left is 0, right is 1, prev is 2 */
2056 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002057 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002058 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002059 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002061 *logflagsp = 0;
2062
2063 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002064 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002065
2066 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002067 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002068 ASSERT(!isnullstartblock(new->br_startblock));
2069
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002070 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072#define LEFT r[0]
2073#define RIGHT r[1]
2074#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 /*
2077 * Set up a bunch of variables to make the tests simpler.
2078 */
2079 error = 0;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002080 xfs_iext_get_extent(ifp, *idx, &PREV);
2081 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 new_endoff = new->br_startoff + new->br_blockcount;
2083 ASSERT(PREV.br_startoff <= new->br_startoff);
2084 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 /*
2087 * Set flags determining what part of the previous oldext allocation
2088 * extent is being replaced by a newext allocation.
2089 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002090 if (PREV.br_startoff == new->br_startoff)
2091 state |= BMAP_LEFT_FILLING;
2092 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2093 state |= BMAP_RIGHT_FILLING;
2094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 /*
2096 * Check and set flags if this segment has a left neighbor.
2097 * Don't set contiguous if the combined extent would be too large.
2098 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002099 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002100 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002101 xfs_iext_get_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002102
2103 if (isnullstartblock(LEFT.br_startblock))
2104 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002106
2107 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2108 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2109 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002110 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002111 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2112 state |= BMAP_LEFT_CONTIG;
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 /*
2115 * Check and set flags if this segment has a right neighbor.
2116 * Don't set contiguous if the combined extent would be too large.
2117 * Also check for all-three-contiguous being too large.
2118 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002119 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002120 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002121 xfs_iext_get_extent(ifp, *idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002122 if (isnullstartblock(RIGHT.br_startblock))
2123 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002125
2126 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2127 new_endoff == RIGHT.br_startoff &&
2128 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002129 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002130 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2131 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2132 BMAP_RIGHT_FILLING)) !=
2133 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2134 BMAP_RIGHT_FILLING) ||
2135 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2136 <= MAXEXTLEN))
2137 state |= BMAP_RIGHT_CONTIG;
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 /*
2140 * Switch out based on the FILLING and CONTIG state bits.
2141 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002142 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2143 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2144 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2145 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 /*
2147 * Setting all of a previous oldext extent to newext.
2148 * The left and right neighbors are both contiguous with new.
2149 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002150 --*idx;
2151
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002152 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002153 xfs_iext_update_extent(ip, state, *idx, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002154
Christoph Hellwigec90c552011-05-23 08:52:53 +00002155 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002156 XFS_IFORK_NEXT_SET(ip, whichfork,
2157 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 if (cur == NULL)
2159 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2160 else {
2161 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002162 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2163 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002165 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002166 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002168 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002169 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002171 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002172 if ((error = xfs_btree_delete(cur, &i)))
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 Hellwig8df4da42008-10-30 16:55:58 +11002175 if ((error = xfs_btree_decrement(cur, 0, &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 Hellwiga67d00a2017-10-17 14:16:26 -07002178 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002179 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 goto done;
2181 }
2182 break;
2183
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002184 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 /*
2186 * Setting all of a previous oldext extent to newext.
2187 * The left neighbor is contiguous, the right is not.
2188 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002189 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002190
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002191 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002192 xfs_iext_update_extent(ip, state, *idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002193
2194 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002195 XFS_IFORK_NEXT_SET(ip, whichfork,
2196 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 if (cur == NULL)
2198 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2199 else {
2200 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002201 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2202 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002204 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002205 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002207 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002208 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002210 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002211 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002212 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 goto done;
2214 }
2215 break;
2216
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002217 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /*
2219 * Setting all of a previous oldext extent to newext.
2220 * The right neighbor is contiguous, the left is not.
2221 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002222 PREV.br_blockcount += RIGHT.br_blockcount;
2223 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002224 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002225
Christoph Hellwigec90c552011-05-23 08:52:53 +00002226 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002227 XFS_IFORK_NEXT_SET(ip, whichfork,
2228 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 if (cur == NULL)
2230 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2231 else {
2232 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002233 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2234 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002236 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002237 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002239 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002240 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002242 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002243 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002244 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 goto done;
2246 }
2247 break;
2248
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002249 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 /*
2251 * Setting all of a previous oldext extent to newext.
2252 * Neither the left nor right neighbors are contiguous with
2253 * the new one.
2254 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002255 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002256 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 if (cur == NULL)
2259 rval = XFS_ILOG_DEXT;
2260 else {
2261 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002262 error = xfs_bmbt_lookup_eq(cur, new, &i);
2263 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002265 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002266 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002267 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 goto done;
2269 }
2270 break;
2271
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002272 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 /*
2274 * Setting the first part of a previous oldext extent to newext.
2275 * The left neighbor is contiguous.
2276 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002277 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002278 xfs_iext_update_extent(ip, state, *idx - 1, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002279
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002280 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002281 PREV.br_startoff += new->br_blockcount;
2282 PREV.br_startblock += new->br_blockcount;
2283 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002284 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002285
Christoph Hellwigec90c552011-05-23 08:52:53 +00002286 --*idx;
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 if (cur == NULL)
2289 rval = XFS_ILOG_DEXT;
2290 else {
2291 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002292 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002293 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002295 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002296 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002297 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002299 error = xfs_btree_decrement(cur, 0, &i);
2300 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002302 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002303 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 goto done;
2305 }
2306 break;
2307
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002308 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 /*
2310 * Setting the first part of a previous oldext extent to newext.
2311 * The left neighbor is not contiguous.
2312 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002313 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002314 PREV.br_startoff += new->br_blockcount;
2315 PREV.br_startblock += new->br_blockcount;
2316 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002317 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002318
Christoph Hellwigec90c552011-05-23 08:52:53 +00002319 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002320 XFS_IFORK_NEXT_SET(ip, whichfork,
2321 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 if (cur == NULL)
2323 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2324 else {
2325 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002326 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002327 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002329 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002330 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002331 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 goto done;
2333 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002334 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002336 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338 break;
2339
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002340 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 /*
2342 * Setting the last part of a previous oldext extent to newext.
2343 * The right neighbor is contiguous with the new allocation.
2344 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002345 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002346 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002347 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002348
2349 ++*idx;
2350
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002351 RIGHT.br_startoff = new->br_startoff;
2352 RIGHT.br_startblock = new->br_startblock;
2353 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002354 xfs_iext_update_extent(ip, state, *idx, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002355
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if (cur == NULL)
2357 rval = XFS_ILOG_DEXT;
2358 else {
2359 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002360 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002361 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002363 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002364 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002365 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002367 error = xfs_btree_increment(cur, 0, &i);
2368 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002370 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002371 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 goto done;
2373 }
2374 break;
2375
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002376 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 /*
2378 * Setting the last part of a previous oldext extent to newext.
2379 * The right neighbor is not contiguous.
2380 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002381 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002382 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002383 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002384
Christoph Hellwigec90c552011-05-23 08:52:53 +00002385 ++*idx;
2386 xfs_iext_insert(ip, *idx, 1, new, state);
2387
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002388 XFS_IFORK_NEXT_SET(ip, whichfork,
2389 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 if (cur == NULL)
2391 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2392 else {
2393 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002394 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002395 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002397 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002398 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002399 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002401 error = xfs_bmbt_lookup_eq(cur, new, &i);
2402 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002404 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002405 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002407 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
2409 break;
2410
2411 case 0:
2412 /*
2413 * Setting the middle part of a previous oldext extent to
2414 * newext. Contiguity is impossible here.
2415 * One extent becomes three extents.
2416 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002417 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002418 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002419 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 r[0] = *new;
2422 r[1].br_startoff = new_endoff;
2423 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002424 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002426 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002427
2428 ++*idx;
2429 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2430
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002431 XFS_IFORK_NEXT_SET(ip, whichfork,
2432 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (cur == NULL)
2434 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2435 else {
2436 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002437 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002438 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002440 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002442 error = xfs_bmbt_update(cur, &r[1]);
2443 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 goto done;
2445 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002447 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002449 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002450 /*
2451 * Reset the cursor to the position of the new extent
2452 * we are about to insert as we can't trust it after
2453 * the previous insert.
2454 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002455 error = xfs_bmbt_lookup_eq(cur, new, &i);
2456 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002458 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002460 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002462 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 }
2464 break;
2465
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002466 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2467 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2468 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2469 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2470 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2471 case BMAP_LEFT_CONTIG:
2472 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 /*
2474 * These cases are all impossible.
2475 */
2476 ASSERT(0);
2477 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002478
Darrick J. Wong9c194642016-08-03 12:16:05 +10002479 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002480 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002481 if (error)
2482 goto done;
2483
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002484 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002485 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002486 int tmp_logflags; /* partial log flag return val */
2487
2488 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002489 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002490 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002491 *logflagsp |= tmp_logflags;
2492 if (error)
2493 goto done;
2494 }
2495
2496 /* clear out the allocated field, done with it now in any case. */
2497 if (cur) {
2498 cur->bc_private.b.allocated = 0;
2499 *curp = cur;
2500 }
2501
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002502 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002504 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 return error;
2506#undef LEFT
2507#undef RIGHT
2508#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510
2511/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002512 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002514STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515xfs_bmap_add_extent_hole_delay(
2516 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002517 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002518 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002519 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002521 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2523 xfs_filblks_t newlen=0; /* new indirect size */
2524 xfs_filblks_t oldlen=0; /* old indirect size */
2525 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002526 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002527 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002529 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002530 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 /*
2533 * Check and set flags if this segment has a left neighbor
2534 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002535 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002536 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002537 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002538
2539 if (isnullstartblock(left.br_startblock))
2540 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 /*
2544 * Check and set flags if the current (right) segment exists.
2545 * If it doesn't exist, we're converting the hole at end-of-file.
2546 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002547 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002548 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002549 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002550
2551 if (isnullstartblock(right.br_startblock))
2552 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 /*
2556 * Set contiguity flags on the left and right neighbors.
2557 * Don't let extents get too large, even if the pieces are contiguous.
2558 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002559 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2560 left.br_startoff + left.br_blockcount == new->br_startoff &&
2561 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2562 state |= BMAP_LEFT_CONTIG;
2563
2564 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2565 new->br_startoff + new->br_blockcount == right.br_startoff &&
2566 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2567 (!(state & BMAP_LEFT_CONTIG) ||
2568 (left.br_blockcount + new->br_blockcount +
2569 right.br_blockcount <= MAXEXTLEN)))
2570 state |= BMAP_RIGHT_CONTIG;
2571
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 /*
2573 * Switch out based on the contiguity flags.
2574 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002575 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2576 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 /*
2578 * New allocation is contiguous with delayed allocations
2579 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002580 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002582 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 temp = left.br_blockcount + new->br_blockcount +
2584 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002585
Eric Sandeen9d87c312009-01-14 23:22:07 -06002586 oldlen = startblockval(left.br_startblock) +
2587 startblockval(new->br_startblock) +
2588 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002589 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2590 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002591 left.br_startblock = nullstartblock(newlen);
2592 left.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002593 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002594
Christoph Hellwigec90c552011-05-23 08:52:53 +00002595 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 break;
2597
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002598 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 /*
2600 * New allocation is contiguous with a delayed allocation
2601 * on the left.
2602 * Merge the new allocation with the left neighbor.
2603 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002604 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002606
Eric Sandeen9d87c312009-01-14 23:22:07 -06002607 oldlen = startblockval(left.br_startblock) +
2608 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002609 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2610 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002611 left.br_blockcount = temp;
2612 left.br_startblock = nullstartblock(newlen);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002613 xfs_iext_update_extent(ip, state, *idx, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 break;
2615
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002616 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 /*
2618 * New allocation is contiguous with a delayed allocation
2619 * on the right.
2620 * Merge the new allocation with the right neighbor.
2621 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002623 oldlen = startblockval(new->br_startblock) +
2624 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002625 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2626 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002627 right.br_startoff = new->br_startoff;
2628 right.br_startblock = nullstartblock(newlen);
2629 right.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002630 xfs_iext_update_extent(ip, state, *idx, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 break;
2632
2633 case 0:
2634 /*
2635 * New allocation is not contiguous with another
2636 * delayed allocation.
2637 * Insert a new entry.
2638 */
2639 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002640 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 break;
2642 }
2643 if (oldlen != newlen) {
2644 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002645 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2646 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 /*
2648 * Nothing to do for disk quota accounting here.
2649 */
2650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651}
2652
2653/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002654 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 */
2656STATIC int /* error */
2657xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002658 struct xfs_trans *tp,
2659 struct xfs_inode *ip,
2660 int whichfork,
2661 xfs_extnum_t *idx,
2662 struct xfs_btree_cur **curp,
2663 struct xfs_bmbt_irec *new,
2664 xfs_fsblock_t *first,
2665 struct xfs_defer_ops *dfops,
2666 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002668 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2669 struct xfs_mount *mp = ip->i_mount;
2670 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2674 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002675 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002676 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002677 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Christoph Hellwig6d045582017-04-11 16:45:54 -07002679 ASSERT(*idx >= 0);
2680 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002681 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002682 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002683
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002684 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002685
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 /*
2687 * Check and set flags if this segment has a left neighbor.
2688 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002689 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002690 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002691 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002692 if (isnullstartblock(left.br_startblock))
2693 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 /*
2697 * Check and set flags if this segment has a current value.
2698 * Not true if we're inserting into the "hole" at eof.
2699 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002700 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002701 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002702 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002703 if (isnullstartblock(right.br_startblock))
2704 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 /*
2708 * We're inserting a real allocation between "left" and "right".
2709 * Set the contiguity flags. Don't let extents get too large.
2710 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002711 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2712 left.br_startoff + left.br_blockcount == new->br_startoff &&
2713 left.br_startblock + left.br_blockcount == new->br_startblock &&
2714 left.br_state == new->br_state &&
2715 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2716 state |= BMAP_LEFT_CONTIG;
2717
2718 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2719 new->br_startoff + new->br_blockcount == right.br_startoff &&
2720 new->br_startblock + new->br_blockcount == right.br_startblock &&
2721 new->br_state == right.br_state &&
2722 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2723 (!(state & BMAP_LEFT_CONTIG) ||
2724 left.br_blockcount + new->br_blockcount +
2725 right.br_blockcount <= MAXEXTLEN))
2726 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002728 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 /*
2730 * Select which case we're in here, and implement it.
2731 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002732 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2733 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /*
2735 * New allocation is contiguous with real allocations on the
2736 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002737 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002739 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002740 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002741 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002742
Christoph Hellwig6d045582017-04-11 16:45:54 -07002743 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002744
Christoph Hellwig6d045582017-04-11 16:45:54 -07002745 XFS_IFORK_NEXT_SET(ip, whichfork,
2746 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2747 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002748 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002749 } else {
2750 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002751 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002752 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002753 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002754 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002755 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002756 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002757 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002758 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002759 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002760 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002761 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002762 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002763 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002764 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002765 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002767 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002769 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /*
2771 * New allocation is contiguous with a real allocation
2772 * on the left.
2773 * Merge the new allocation with the left neighbor.
2774 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002775 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002776 old = left;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002777
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002778 left.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002779 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002780
Christoph Hellwig6d045582017-04-11 16:45:54 -07002781 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002782 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002783 } else {
2784 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002785 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002786 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002787 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002788 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002789 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002790 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002791 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002793 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002795 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 /*
2797 * New allocation is contiguous with a real allocation
2798 * on the right.
2799 * Merge the new allocation with the right neighbor.
2800 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002801 old = right;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002802
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002803 right.br_startoff = new->br_startoff;
2804 right.br_startblock = new->br_startblock;
2805 right.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002806 xfs_iext_update_extent(ip, state, *idx, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002807
Christoph Hellwig6d045582017-04-11 16:45:54 -07002808 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002809 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002810 } else {
2811 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002812 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002813 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002814 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002815 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002816 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002817 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002818 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002820 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821
2822 case 0:
2823 /*
2824 * New allocation is not contiguous with another
2825 * real allocation.
2826 * Insert a new entry.
2827 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002828 xfs_iext_insert(ip, *idx, 1, new, state);
2829 XFS_IFORK_NEXT_SET(ip, whichfork,
2830 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2831 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002832 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002833 } else {
2834 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002835 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002836 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002837 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002838 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002839 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002840 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002841 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002842 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002844 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002846
Darrick J. Wong9c194642016-08-03 12:16:05 +10002847 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002848 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002849 if (error)
2850 goto done;
2851
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002852 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002853 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002854 int tmp_logflags; /* partial log flag return val */
2855
Christoph Hellwig6d045582017-04-11 16:45:54 -07002856 ASSERT(cur == NULL);
2857 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002858 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002859 *logflagsp |= tmp_logflags;
2860 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002861 if (error)
2862 goto done;
2863 }
2864
2865 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002866 if (cur)
2867 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002868
Christoph Hellwig6d045582017-04-11 16:45:54 -07002869 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002870done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002871 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002872 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873}
2874
Nathan Scottdd9f4382006-01-11 15:28:28 +11002875/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11002876 * Functions used in the extent read, allocate and remove paths
2877 */
2878
2879/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002880 * Adjust the size of the new extent based on di_extsize and rt extsize.
2881 */
Dave Chinner68988112013-08-12 20:49:42 +10002882int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002883xfs_bmap_extsize_align(
2884 xfs_mount_t *mp,
2885 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2886 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2887 xfs_extlen_t extsz, /* align to this extent size */
2888 int rt, /* is this a realtime inode? */
2889 int eof, /* is extent at end-of-file? */
2890 int delay, /* creating delalloc extent? */
2891 int convert, /* overwriting unwritten extent? */
2892 xfs_fileoff_t *offp, /* in/out: aligned offset */
2893 xfs_extlen_t *lenp) /* in/out: aligned length */
2894{
2895 xfs_fileoff_t orig_off; /* original offset */
2896 xfs_extlen_t orig_alen; /* original length */
2897 xfs_fileoff_t orig_end; /* original off+len */
2898 xfs_fileoff_t nexto; /* next file offset */
2899 xfs_fileoff_t prevo; /* previous file offset */
2900 xfs_fileoff_t align_off; /* temp for offset */
2901 xfs_extlen_t align_alen; /* temp for length */
2902 xfs_extlen_t temp; /* temp for calculations */
2903
2904 if (convert)
2905 return 0;
2906
2907 orig_off = align_off = *offp;
2908 orig_alen = align_alen = *lenp;
2909 orig_end = orig_off + orig_alen;
2910
2911 /*
2912 * If this request overlaps an existing extent, then don't
2913 * attempt to perform any additional alignment.
2914 */
2915 if (!delay && !eof &&
2916 (orig_off >= gotp->br_startoff) &&
2917 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2918 return 0;
2919 }
2920
2921 /*
2922 * If the file offset is unaligned vs. the extent size
2923 * we need to align it. This will be possible unless
2924 * the file was previously written with a kernel that didn't
2925 * perform this alignment, or if a truncate shot us in the
2926 * foot.
2927 */
2928 temp = do_mod(orig_off, extsz);
2929 if (temp) {
2930 align_alen += temp;
2931 align_off -= temp;
2932 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002933
2934 /* Same adjustment for the end of the requested area. */
2935 temp = (align_alen % extsz);
2936 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002937 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002938
2939 /*
2940 * For large extent hint sizes, the aligned extent might be larger than
2941 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2942 * the length back under MAXEXTLEN. The outer allocation loops handle
2943 * short allocation just fine, so it is safe to do this. We only want to
2944 * do it when we are forced to, though, because it means more allocation
2945 * operations are required.
2946 */
2947 while (align_alen > MAXEXTLEN)
2948 align_alen -= extsz;
2949 ASSERT(align_alen <= MAXEXTLEN);
2950
Nathan Scottdd9f4382006-01-11 15:28:28 +11002951 /*
2952 * If the previous block overlaps with this proposed allocation
2953 * then move the start forward without adjusting the length.
2954 */
2955 if (prevp->br_startoff != NULLFILEOFF) {
2956 if (prevp->br_startblock == HOLESTARTBLOCK)
2957 prevo = prevp->br_startoff;
2958 else
2959 prevo = prevp->br_startoff + prevp->br_blockcount;
2960 } else
2961 prevo = 0;
2962 if (align_off != orig_off && align_off < prevo)
2963 align_off = prevo;
2964 /*
2965 * If the next block overlaps with this proposed allocation
2966 * then move the start back without adjusting the length,
2967 * but not before offset 0.
2968 * This may of course make the start overlap previous block,
2969 * and if we hit the offset 0 limit then the next block
2970 * can still overlap too.
2971 */
2972 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2973 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2974 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2975 nexto = gotp->br_startoff + gotp->br_blockcount;
2976 else
2977 nexto = gotp->br_startoff;
2978 } else
2979 nexto = NULLFILEOFF;
2980 if (!eof &&
2981 align_off + align_alen != orig_end &&
2982 align_off + align_alen > nexto)
2983 align_off = nexto > align_alen ? nexto - align_alen : 0;
2984 /*
2985 * If we're now overlapping the next or previous extent that
2986 * means we can't fit an extsz piece in this hole. Just move
2987 * the start forward to the first valid spot and set
2988 * the length so we hit the end.
2989 */
2990 if (align_off != orig_off && align_off < prevo)
2991 align_off = prevo;
2992 if (align_off + align_alen != orig_end &&
2993 align_off + align_alen > nexto &&
2994 nexto != NULLFILEOFF) {
2995 ASSERT(nexto > prevo);
2996 align_alen = nexto - align_off;
2997 }
2998
2999 /*
3000 * If realtime, and the result isn't a multiple of the realtime
3001 * extent size we need to remove blocks until it is.
3002 */
3003 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3004 /*
3005 * We're not covering the original request, or
3006 * we won't be able to once we fix the length.
3007 */
3008 if (orig_off < align_off ||
3009 orig_end > align_off + align_alen ||
3010 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003011 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003012 /*
3013 * Try to fix it by moving the start up.
3014 */
3015 if (align_off + temp <= orig_off) {
3016 align_alen -= temp;
3017 align_off += temp;
3018 }
3019 /*
3020 * Try to fix it by moving the end in.
3021 */
3022 else if (align_off + align_alen - temp >= orig_end)
3023 align_alen -= temp;
3024 /*
3025 * Set the start to the minimum then trim the length.
3026 */
3027 else {
3028 align_alen -= orig_off - align_off;
3029 align_off = orig_off;
3030 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3031 }
3032 /*
3033 * Result doesn't cover the request, fail it.
3034 */
3035 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003036 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003037 } else {
3038 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003039 /* see MAXEXTLEN handling above */
3040 ASSERT(orig_end <= align_off + align_alen ||
3041 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003042 }
3043
3044#ifdef DEBUG
3045 if (!eof && gotp->br_startoff != NULLFILEOFF)
3046 ASSERT(align_off + align_alen <= gotp->br_startoff);
3047 if (prevp->br_startoff != NULLFILEOFF)
3048 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3049#endif
3050
3051 *lenp = align_alen;
3052 *offp = align_off;
3053 return 0;
3054}
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056#define XFS_ALLOC_GAP_UNITS 4
3057
Dave Chinner68988112013-08-12 20:49:42 +10003058void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003059xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003060 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3064 xfs_mount_t *mp; /* mount point structure */
3065 int nullfb; /* true if ap->firstblock isn't set */
3066 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068#define ISVALID(x,y) \
3069 (rt ? \
3070 (x) < mp->m_sb.sb_rblocks : \
3071 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3072 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3073 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003076 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003077 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3078 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003079 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 /*
3081 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003082 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003084 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3085 !isnullstartblock(ap->prev.br_startblock) &&
3086 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3087 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003088 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 /*
3090 * Adjust for the gap between prevp and us.
3091 */
Dave Chinner3a756672011-09-18 20:40:58 +00003092 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003093 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003095 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3096 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 }
3098 /*
3099 * If not at eof, then compare the two neighbor blocks.
3100 * Figure out whether either one gives us a good starting point,
3101 * and pick the better one.
3102 */
3103 else if (!ap->eof) {
3104 xfs_fsblock_t gotbno; /* right side block number */
3105 xfs_fsblock_t gotdiff=0; /* right side difference */
3106 xfs_fsblock_t prevbno; /* left side block number */
3107 xfs_fsblock_t prevdiff=0; /* left side difference */
3108
3109 /*
3110 * If there's a previous (left) block, select a requested
3111 * start block based on it.
3112 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003113 if (ap->prev.br_startoff != NULLFILEOFF &&
3114 !isnullstartblock(ap->prev.br_startblock) &&
3115 (prevbno = ap->prev.br_startblock +
3116 ap->prev.br_blockcount) &&
3117 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 /*
3119 * Calculate gap to end of previous block.
3120 */
Dave Chinner3a756672011-09-18 20:40:58 +00003121 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003122 (ap->prev.br_startoff +
3123 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 /*
3125 * Figure the startblock based on the previous block's
3126 * end and the gap size.
3127 * Heuristic!
3128 * If the gap is large relative to the piece we're
3129 * allocating, or using it gives us an invalid block
3130 * number, then just use the end of the previous block.
3131 */
Dave Chinner3a756672011-09-18 20:40:58 +00003132 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003134 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 prevbno += adjust;
3136 else
3137 prevdiff += adjust;
3138 /*
3139 * If the firstblock forbids it, can't use it,
3140 * must use default.
3141 */
3142 if (!rt && !nullfb &&
3143 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3144 prevbno = NULLFSBLOCK;
3145 }
3146 /*
3147 * No previous block or can't follow it, just default.
3148 */
3149 else
3150 prevbno = NULLFSBLOCK;
3151 /*
3152 * If there's a following (right) block, select a requested
3153 * start block based on it.
3154 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003155 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 /*
3157 * Calculate gap to start of next block.
3158 */
Dave Chinner3a756672011-09-18 20:40:58 +00003159 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 /*
3161 * Figure the startblock based on the next block's
3162 * start and the gap size.
3163 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003164 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 /*
3166 * Heuristic!
3167 * If the gap is large relative to the piece we're
3168 * allocating, or using it gives us an invalid block
3169 * number, then just use the start of the next block
3170 * offset by our length.
3171 */
Dave Chinner3a756672011-09-18 20:40:58 +00003172 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 ISVALID(gotbno - gotdiff, gotbno))
3174 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003175 else if (ISVALID(gotbno - ap->length, gotbno)) {
3176 gotbno -= ap->length;
3177 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 } else
3179 gotdiff += adjust;
3180 /*
3181 * If the firstblock forbids it, can't use it,
3182 * must use default.
3183 */
3184 if (!rt && !nullfb &&
3185 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3186 gotbno = NULLFSBLOCK;
3187 }
3188 /*
3189 * No next block, just default.
3190 */
3191 else
3192 gotbno = NULLFSBLOCK;
3193 /*
3194 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003195 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 */
3197 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003198 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003200 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003202 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003204#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003205}
3206
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003207static int
3208xfs_bmap_longest_free_extent(
3209 struct xfs_trans *tp,
3210 xfs_agnumber_t ag,
3211 xfs_extlen_t *blen,
3212 int *notinit)
3213{
3214 struct xfs_mount *mp = tp->t_mountp;
3215 struct xfs_perag *pag;
3216 xfs_extlen_t longest;
3217 int error = 0;
3218
3219 pag = xfs_perag_get(mp, ag);
3220 if (!pag->pagf_init) {
3221 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3222 if (error)
3223 goto out;
3224
3225 if (!pag->pagf_init) {
3226 *notinit = 1;
3227 goto out;
3228 }
3229 }
3230
Dave Chinner50adbcb2015-06-22 10:04:31 +10003231 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003232 xfs_alloc_min_freelist(mp, pag),
3233 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003234 if (*blen < longest)
3235 *blen = longest;
3236
3237out:
3238 xfs_perag_put(pag);
3239 return error;
3240}
3241
3242static void
3243xfs_bmap_select_minlen(
3244 struct xfs_bmalloca *ap,
3245 struct xfs_alloc_arg *args,
3246 xfs_extlen_t *blen,
3247 int notinit)
3248{
3249 if (notinit || *blen < ap->minlen) {
3250 /*
3251 * Since we did a BUF_TRYLOCK above, it is possible that
3252 * there is space for this request.
3253 */
3254 args->minlen = ap->minlen;
3255 } else if (*blen < args->maxlen) {
3256 /*
3257 * If the best seen length is less than the request length,
3258 * use the best as the minimum.
3259 */
3260 args->minlen = *blen;
3261 } else {
3262 /*
3263 * Otherwise we've seen an extent as big as maxlen, use that
3264 * as the minimum.
3265 */
3266 args->minlen = args->maxlen;
3267 }
3268}
3269
Nathan Scotta365bdd2006-03-14 13:34:16 +11003270STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003271xfs_bmap_btalloc_nullfb(
3272 struct xfs_bmalloca *ap,
3273 struct xfs_alloc_arg *args,
3274 xfs_extlen_t *blen)
3275{
3276 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003277 xfs_agnumber_t ag, startag;
3278 int notinit = 0;
3279 int error;
3280
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003281 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003282 args->total = ap->total;
3283
Christoph Hellwigc467c042010-02-15 23:34:42 +00003284 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3285 if (startag == NULLAGNUMBER)
3286 startag = ag = 0;
3287
Dave Chinner14b064c2011-01-27 12:16:28 +11003288 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003289 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3290 &notinit);
3291 if (error)
3292 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003293
Christoph Hellwigc467c042010-02-15 23:34:42 +00003294 if (++ag == mp->m_sb.sb_agcount)
3295 ag = 0;
3296 if (ag == startag)
3297 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003298 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003299
3300 xfs_bmap_select_minlen(ap, args, blen, notinit);
3301 return 0;
3302}
3303
3304STATIC int
3305xfs_bmap_btalloc_filestreams(
3306 struct xfs_bmalloca *ap,
3307 struct xfs_alloc_arg *args,
3308 xfs_extlen_t *blen)
3309{
3310 struct xfs_mount *mp = ap->ip->i_mount;
3311 xfs_agnumber_t ag;
3312 int notinit = 0;
3313 int error;
3314
3315 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3316 args->total = ap->total;
3317
3318 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3319 if (ag == NULLAGNUMBER)
3320 ag = 0;
3321
3322 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3323 if (error)
3324 return error;
3325
3326 if (*blen < args->maxlen) {
3327 error = xfs_filestream_new_ag(ap, &ag);
3328 if (error)
3329 return error;
3330
3331 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3332 &notinit);
3333 if (error)
3334 return error;
3335
3336 }
3337
3338 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003339
3340 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003341 * Set the failure fallback case to look in the selected AG as stream
3342 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003343 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003344 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003345 return 0;
3346}
3347
3348STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003349xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003350 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003351{
3352 xfs_mount_t *mp; /* mount point structure */
3353 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003354 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003355 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003356 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003357 xfs_alloc_arg_t args;
3358 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003359 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003360 int nullfb; /* true if ap->firstblock isn't set */
3361 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003362 int tryagain;
3363 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003364 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003365
Dave Chinnera99ebf42011-12-01 11:24:20 +00003366 ASSERT(ap->length);
3367
Nathan Scotta365bdd2006-03-14 13:34:16 +11003368 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003369
3370 /* stripe alignment for allocation is determined by mount parameters */
3371 stripe_align = 0;
3372 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3373 stripe_align = mp->m_swidth;
3374 else if (mp->m_dalign)
3375 stripe_align = mp->m_dalign;
3376
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003377 if (ap->flags & XFS_BMAPI_COWFORK)
3378 align = xfs_get_cowextsz_hint(ap->ip);
3379 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003380 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003381 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003382 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003383 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003384 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003385 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003386 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003387 }
Dave Chinner33177f052013-12-12 16:34:36 +11003388
3389
Dave Chinner0937e0f2011-09-18 20:40:57 +00003390 nullfb = *ap->firstblock == NULLFSBLOCK;
3391 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003392 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003393 if (xfs_alloc_is_userdata(ap->datatype) &&
3394 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003395 ag = xfs_filestream_lookup_ag(ap->ip);
3396 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003397 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003398 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003399 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003400 }
3401 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003402 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003403
3404 xfs_bmap_adjacent(ap);
3405
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003407 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 * it's in the right allocation group.
3409 */
Dave Chinner3a756672011-09-18 20:40:58 +00003410 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 ;
3412 else
Dave Chinner3a756672011-09-18 20:40:58 +00003413 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 * Normal allocation, done through xfs_alloc_vextent.
3416 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003417 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003418 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003419 args.tp = ap->tp;
3420 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003421 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003422 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003423
3424 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003425 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003426 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003427 blen = 0;
3428 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003429 /*
3430 * Search for an allocation group with a single extent large
3431 * enough for the request. If one isn't found, then adjust
3432 * the minimum allocation size to the largest space found.
3433 */
Dave Chinner292378e2016-09-26 08:21:28 +10003434 if (xfs_alloc_is_userdata(ap->datatype) &&
3435 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003436 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3437 else
3438 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003439 if (error)
3440 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003441 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003442 if (xfs_inode_is_filestream(ap->ip))
3443 args.type = XFS_ALLOCTYPE_FIRST_AG;
3444 else
3445 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003446 args.total = args.minlen = ap->minlen;
3447 } else {
3448 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3449 args.total = ap->total;
3450 args.minlen = ap->minlen;
3451 }
David Chinner957d0eb2007-06-18 16:50:37 +10003452 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003453 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003454 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003455 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003456 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003457 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003458 args.prod = 1;
3459 args.mod = 0;
3460 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003461 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003462 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003463 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3464 }
3465 /*
3466 * If we are not low on available data blocks, and the
3467 * underlying logical volume manager is a stripe, and
3468 * the file offset is zero then try to allocate data
3469 * blocks on stripe unit boundary.
3470 * NOTE: ap->aeof is only set if the allocation length
3471 * is >= the stripe unit and the allocation offset is
3472 * at the end of file.
3473 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003474 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003475 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003476 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003477 atype = args.type;
3478 isaligned = 1;
3479 /*
3480 * Adjust for alignment
3481 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003482 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003483 args.minlen = blen - args.alignment;
3484 args.minalignslop = 0;
3485 } else {
3486 /*
3487 * First try an exact bno allocation.
3488 * If it fails then do a near or start bno
3489 * allocation with alignment turned on.
3490 */
3491 atype = args.type;
3492 tryagain = 1;
3493 args.type = XFS_ALLOCTYPE_THIS_BNO;
3494 args.alignment = 1;
3495 /*
3496 * Compute the minlen+alignment for the
3497 * next case. Set slop so that the value
3498 * of minlen+alignment+slop doesn't go up
3499 * between the calls.
3500 */
Dave Chinner33177f052013-12-12 16:34:36 +11003501 if (blen > stripe_align && blen <= args.maxlen)
3502 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003503 else
3504 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003505 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003506 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003507 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003508 args.minlen - 1;
3509 else
3510 args.minalignslop = 0;
3511 }
3512 } else {
3513 args.alignment = 1;
3514 args.minalignslop = 0;
3515 }
3516 args.minleft = ap->minleft;
3517 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003518 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003519 args.datatype = ap->datatype;
3520 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003521 args.ip = ap->ip;
3522
3523 error = xfs_alloc_vextent(&args);
3524 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003525 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003526
Nathan Scotta365bdd2006-03-14 13:34:16 +11003527 if (tryagain && args.fsbno == NULLFSBLOCK) {
3528 /*
3529 * Exact allocation failed. Now try with alignment
3530 * turned on.
3531 */
3532 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003533 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003534 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003535 args.minlen = nextminlen;
3536 args.minalignslop = 0;
3537 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 if ((error = xfs_alloc_vextent(&args)))
3539 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003540 }
3541 if (isaligned && args.fsbno == NULLFSBLOCK) {
3542 /*
3543 * allocation failed, so turn off alignment and
3544 * try again.
3545 */
3546 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003547 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003548 args.alignment = 0;
3549 if ((error = xfs_alloc_vextent(&args)))
3550 return error;
3551 }
3552 if (args.fsbno == NULLFSBLOCK && nullfb &&
3553 args.minlen > ap->minlen) {
3554 args.minlen = ap->minlen;
3555 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003556 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003557 if ((error = xfs_alloc_vextent(&args)))
3558 return error;
3559 }
3560 if (args.fsbno == NULLFSBLOCK && nullfb) {
3561 args.fsbno = 0;
3562 args.type = XFS_ALLOCTYPE_FIRST_AG;
3563 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003564 if ((error = xfs_alloc_vextent(&args)))
3565 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003566 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003567 }
3568 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003569 /*
3570 * check the allocation happened at the same or higher AG than
3571 * the first block that was allocated.
3572 */
3573 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003574 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3575 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003576
Dave Chinner3a756672011-09-18 20:40:58 +00003577 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003578 if (*ap->firstblock == NULLFSBLOCK)
3579 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003580 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003581 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003582 if (!(ap->flags & XFS_BMAPI_COWFORK))
3583 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003584 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3585 if (ap->wasdel)
3586 ap->ip->i_delayed_blks -= args.len;
3587 /*
3588 * Adjust the disk quota also. This was reserved
3589 * earlier.
3590 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003591 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003592 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3593 XFS_TRANS_DQ_BCOUNT,
3594 (long) args.len);
3595 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003596 ap->blkno = NULLFSBLOCK;
3597 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 }
3599 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003600}
3601
3602/*
3603 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3604 * It figures out where to ask the underlying allocator to put the new extent.
3605 */
3606STATIC int
3607xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003608 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003609{
Dave Chinner292378e2016-09-26 08:21:28 +10003610 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3611 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003612 return xfs_bmap_rtalloc(ap);
3613 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614}
3615
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003616/* Trim extent to fit a logical block range. */
3617void
3618xfs_trim_extent(
3619 struct xfs_bmbt_irec *irec,
3620 xfs_fileoff_t bno,
3621 xfs_filblks_t len)
3622{
3623 xfs_fileoff_t distance;
3624 xfs_fileoff_t end = bno + len;
3625
3626 if (irec->br_startoff + irec->br_blockcount <= bno ||
3627 irec->br_startoff >= end) {
3628 irec->br_blockcount = 0;
3629 return;
3630 }
3631
3632 if (irec->br_startoff < bno) {
3633 distance = bno - irec->br_startoff;
3634 if (isnullstartblock(irec->br_startblock))
3635 irec->br_startblock = DELAYSTARTBLOCK;
3636 if (irec->br_startblock != DELAYSTARTBLOCK &&
3637 irec->br_startblock != HOLESTARTBLOCK)
3638 irec->br_startblock += distance;
3639 irec->br_startoff += distance;
3640 irec->br_blockcount -= distance;
3641 }
3642
3643 if (end < irec->br_startoff + irec->br_blockcount) {
3644 distance = irec->br_startoff + irec->br_blockcount - end;
3645 irec->br_blockcount -= distance;
3646 }
3647}
3648
Brian Foster40214d12017-10-13 09:47:46 -07003649/* trim extent to within eof */
3650void
3651xfs_trim_extent_eof(
3652 struct xfs_bmbt_irec *irec,
3653 struct xfs_inode *ip)
3654
3655{
3656 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3657 i_size_read(VFS_I(ip))));
3658}
3659
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003661 * Trim the returned map to the required bounds
3662 */
3663STATIC void
3664xfs_bmapi_trim_map(
3665 struct xfs_bmbt_irec *mval,
3666 struct xfs_bmbt_irec *got,
3667 xfs_fileoff_t *bno,
3668 xfs_filblks_t len,
3669 xfs_fileoff_t obno,
3670 xfs_fileoff_t end,
3671 int n,
3672 int flags)
3673{
3674 if ((flags & XFS_BMAPI_ENTIRE) ||
3675 got->br_startoff + got->br_blockcount <= obno) {
3676 *mval = *got;
3677 if (isnullstartblock(got->br_startblock))
3678 mval->br_startblock = DELAYSTARTBLOCK;
3679 return;
3680 }
3681
3682 if (obno > *bno)
3683 *bno = obno;
3684 ASSERT((*bno >= obno) || (n == 0));
3685 ASSERT(*bno < end);
3686 mval->br_startoff = *bno;
3687 if (isnullstartblock(got->br_startblock))
3688 mval->br_startblock = DELAYSTARTBLOCK;
3689 else
3690 mval->br_startblock = got->br_startblock +
3691 (*bno - got->br_startoff);
3692 /*
3693 * Return the minimum of what we got and what we asked for for
3694 * the length. We can use the len variable here because it is
3695 * modified below and we could have been there before coming
3696 * here if the first part of the allocation didn't overlap what
3697 * was asked for.
3698 */
3699 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3700 got->br_blockcount - (*bno - got->br_startoff));
3701 mval->br_state = got->br_state;
3702 ASSERT(mval->br_blockcount <= len);
3703 return;
3704}
3705
3706/*
3707 * Update and validate the extent map to return
3708 */
3709STATIC void
3710xfs_bmapi_update_map(
3711 struct xfs_bmbt_irec **map,
3712 xfs_fileoff_t *bno,
3713 xfs_filblks_t *len,
3714 xfs_fileoff_t obno,
3715 xfs_fileoff_t end,
3716 int *n,
3717 int flags)
3718{
3719 xfs_bmbt_irec_t *mval = *map;
3720
3721 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3722 ((mval->br_startoff + mval->br_blockcount) <= end));
3723 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3724 (mval->br_startoff < obno));
3725
3726 *bno = mval->br_startoff + mval->br_blockcount;
3727 *len = end - *bno;
3728 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3729 /* update previous map with new information */
3730 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3731 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3732 ASSERT(mval->br_state == mval[-1].br_state);
3733 mval[-1].br_blockcount = mval->br_blockcount;
3734 mval[-1].br_state = mval->br_state;
3735 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3736 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3737 mval[-1].br_startblock != HOLESTARTBLOCK &&
3738 mval->br_startblock == mval[-1].br_startblock +
3739 mval[-1].br_blockcount &&
3740 ((flags & XFS_BMAPI_IGSTATE) ||
3741 mval[-1].br_state == mval->br_state)) {
3742 ASSERT(mval->br_startoff ==
3743 mval[-1].br_startoff + mval[-1].br_blockcount);
3744 mval[-1].br_blockcount += mval->br_blockcount;
3745 } else if (*n > 0 &&
3746 mval->br_startblock == DELAYSTARTBLOCK &&
3747 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3748 mval->br_startoff ==
3749 mval[-1].br_startoff + mval[-1].br_blockcount) {
3750 mval[-1].br_blockcount += mval->br_blockcount;
3751 mval[-1].br_state = mval->br_state;
3752 } else if (!((*n == 0) &&
3753 ((mval->br_startoff + mval->br_blockcount) <=
3754 obno))) {
3755 mval++;
3756 (*n)++;
3757 }
3758 *map = mval;
3759}
3760
3761/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003762 * Map file blocks to filesystem blocks without allocation.
3763 */
3764int
3765xfs_bmapi_read(
3766 struct xfs_inode *ip,
3767 xfs_fileoff_t bno,
3768 xfs_filblks_t len,
3769 struct xfs_bmbt_irec *mval,
3770 int *nmap,
3771 int flags)
3772{
3773 struct xfs_mount *mp = ip->i_mount;
3774 struct xfs_ifork *ifp;
3775 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003776 xfs_fileoff_t obno;
3777 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003778 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003779 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003780 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003781 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003782 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003783
3784 ASSERT(*nmap >= 1);
3785 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003786 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003787 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003788
3789 if (unlikely(XFS_TEST_ERROR(
3790 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3791 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003792 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003793 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003794 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003795 }
3796
3797 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003798 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003799
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003800 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003801
3802 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003803
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003804 /* No CoW fork? Return a hole. */
3805 if (whichfork == XFS_COW_FORK && !ifp) {
3806 mval->br_startoff = bno;
3807 mval->br_startblock = HOLESTARTBLOCK;
3808 mval->br_blockcount = len;
3809 mval->br_state = XFS_EXT_NORM;
3810 *nmap = 1;
3811 return 0;
3812 }
3813
Dave Chinner5c8ed202011-09-18 20:40:45 +00003814 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3815 error = xfs_iread_extents(NULL, ip, whichfork);
3816 if (error)
3817 return error;
3818 }
3819
Christoph Hellwig334f3422016-11-24 11:39:43 +11003820 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
3821 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003822 end = bno + len;
3823 obno = bno;
3824
3825 while (bno < end && n < *nmap) {
3826 /* Reading past eof, act as though there's a hole up to end. */
3827 if (eof)
3828 got.br_startoff = end;
3829 if (got.br_startoff > bno) {
3830 /* Reading in a hole. */
3831 mval->br_startoff = bno;
3832 mval->br_startblock = HOLESTARTBLOCK;
3833 mval->br_blockcount =
3834 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3835 mval->br_state = XFS_EXT_NORM;
3836 bno += mval->br_blockcount;
3837 len -= mval->br_blockcount;
3838 mval++;
3839 n++;
3840 continue;
3841 }
3842
3843 /* set up the extent map to return. */
3844 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3845 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3846
3847 /* If we're done, stop now. */
3848 if (bno >= end || n >= *nmap)
3849 break;
3850
3851 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11003852 if (!xfs_iext_get_extent(ifp, ++idx, &got))
3853 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003854 }
3855 *nmap = n;
3856 return 0;
3857}
3858
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003859/*
3860 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3861 * global pool and the extent inserted into the inode in-core extent tree.
3862 *
3863 * On entry, got refers to the first extent beyond the offset of the extent to
3864 * allocate or eof is specified if no such extent exists. On return, got refers
3865 * to the extent record that was inserted to the inode fork.
3866 *
3867 * Note that the allocated extent may have been merged with contiguous extents
3868 * during insertion into the inode fork. Thus, got does not reflect the current
3869 * state of the inode fork on return. If necessary, the caller can use lastx to
3870 * look up the updated record in the inode fork.
3871 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003872int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003873xfs_bmapi_reserve_delalloc(
3874 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003875 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003876 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003877 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003878 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003879 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003880 xfs_extnum_t *lastx,
3881 int eof)
3882{
3883 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003884 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003885 xfs_extlen_t alen;
3886 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003887 char rt = XFS_IS_REALTIME_INODE(ip);
3888 xfs_extlen_t extsz;
3889 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003890 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003891
Brian Foster974ae922016-11-28 14:57:42 +11003892 /*
3893 * Cap the alloc length. Keep track of prealloc so we know whether to
3894 * tag the inode before we return.
3895 */
3896 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003897 if (!eof)
3898 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003899 if (prealloc && alen >= len)
3900 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003901
3902 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003903 if (whichfork == XFS_COW_FORK)
3904 extsz = xfs_get_cowextsz_hint(ip);
3905 else
3906 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003907 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003908 struct xfs_bmbt_irec prev;
3909
3910 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
3911 prev.br_startoff = NULLFILEOFF;
3912
3913 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003914 1, 0, &aoff, &alen);
3915 ASSERT(!error);
3916 }
3917
3918 if (rt)
3919 extsz = alen / mp->m_sb.sb_rextsize;
3920
3921 /*
3922 * Make a transaction-less quota reservation for delayed allocation
3923 * blocks. This number gets adjusted later. We return if we haven't
3924 * allocated blocks already inside this loop.
3925 */
3926 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
3927 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
3928 if (error)
3929 return error;
3930
3931 /*
3932 * Split changing sb for alen and indlen since they could be coming
3933 * from different places.
3934 */
3935 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3936 ASSERT(indlen > 0);
3937
3938 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11003939 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003940 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11003941 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003942 }
3943
3944 if (error)
3945 goto out_unreserve_quota;
3946
Dave Chinner0d485ad2015-02-23 21:22:03 +11003947 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003948 if (error)
3949 goto out_unreserve_blocks;
3950
3951
3952 ip->i_delayed_blks += alen;
3953
3954 got->br_startoff = aoff;
3955 got->br_startblock = nullstartblock(indlen);
3956 got->br_blockcount = alen;
3957 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003958
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003959 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003960
Brian Foster974ae922016-11-28 14:57:42 +11003961 /*
3962 * Tag the inode if blocks were preallocated. Note that COW fork
3963 * preallocation can occur at the start or end of the extent, even when
3964 * prealloc == 0, so we must also check the aligned offset and length.
3965 */
3966 if (whichfork == XFS_DATA_FORK && prealloc)
3967 xfs_inode_set_eofblocks_tag(ip);
3968 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
3969 xfs_inode_set_cowblocks_tag(ip);
3970
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003971 return 0;
3972
3973out_unreserve_blocks:
3974 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11003975 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003976 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11003977 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003978out_unreserve_quota:
3979 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10003980 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003981 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
3982 return error;
3983}
3984
Dave Chinnercf11da92014-07-15 07:08:24 +10003985static int
3986xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10003987 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003988{
3989 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003990 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003991 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00003992 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003993 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003994
Dave Chinnera99ebf42011-12-01 11:24:20 +00003995 ASSERT(bma->length > 0);
3996
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003997 /*
3998 * For the wasdelay case, we could also just allocate the stuff asked
3999 * for in this bmap call but that wouldn't be as good.
4000 */
4001 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004002 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4003 bma->offset = bma->got.br_startoff;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004004 if (bma->idx)
4005 xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004006 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004007 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004008 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004009 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004010 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004011 }
4012
4013 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004014 * Set the data type being allocated. For the data fork, the first data
4015 * in the file is treated differently to all other allocations. For the
4016 * attribute fork, we only need to ensure the allocated range is not on
4017 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004018 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004019 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004020 bma->datatype = XFS_ALLOC_NOBUSY;
4021 if (whichfork == XFS_DATA_FORK) {
4022 if (bma->offset == 0)
4023 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4024 else
4025 bma->datatype |= XFS_ALLOC_USERDATA;
4026 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004027 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004028 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004029 }
4030
Dave Chinnere04426b2012-10-05 11:06:59 +10004031 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004032
4033 /*
4034 * Only want to do the alignment at the eof if it is userdata and
4035 * allocation length is larger than a stripe unit.
4036 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004037 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004038 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004039 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004040 if (error)
4041 return error;
4042 }
4043
4044 error = xfs_bmap_alloc(bma);
4045 if (error)
4046 return error;
4047
Dave Chinner29c8d172011-09-18 20:41:00 +00004048 if (bma->cur)
4049 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004050 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004051 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004052 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4053 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4054 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004055 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004056 }
4057 /*
4058 * Bump the number of extents we've allocated
4059 * in this call.
4060 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004061 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004062
Dave Chinner29c8d172011-09-18 20:41:00 +00004063 if (bma->cur)
4064 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004065 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4066
Dave Chinner963c30c2011-09-18 20:40:59 +00004067 bma->got.br_startoff = bma->offset;
4068 bma->got.br_startblock = bma->blkno;
4069 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004070 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004071
4072 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004073 * In the data fork, a wasdelay extent has been initialized, so
4074 * shouldn't be flagged as unwritten.
4075 *
4076 * For the cow fork, however, we convert delalloc reservations
4077 * (extents allocated for speculative preallocation) to
4078 * allocated unwritten extents, and only convert the unwritten
4079 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004080 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004081 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4082 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004083 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004084 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004085
Christoph Hellwigc6534242011-09-18 20:41:05 +00004086 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004087 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004088 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004089 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4090 whichfork, &bma->idx, &bma->cur, &bma->got,
4091 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004092
Christoph Hellwigc315c902011-09-18 20:41:02 +00004093 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004094 if (error)
4095 return error;
4096
4097 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004098 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4099 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4100 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004101 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004102 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004103
Dave Chinner963c30c2011-09-18 20:40:59 +00004104 ASSERT(bma->got.br_startoff <= bma->offset);
4105 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4106 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004107 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4108 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004109 return 0;
4110}
4111
Dave Chinnerb447fe52011-09-18 20:40:51 +00004112STATIC int
4113xfs_bmapi_convert_unwritten(
4114 struct xfs_bmalloca *bma,
4115 struct xfs_bmbt_irec *mval,
4116 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004117 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004118{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004119 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004120 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004121 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004122 int error;
4123
Dave Chinnerb447fe52011-09-18 20:40:51 +00004124 /* check if we need to do unwritten->real conversion */
4125 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4126 (flags & XFS_BMAPI_PREALLOC))
4127 return 0;
4128
4129 /* check if we need to do real->unwritten conversion */
4130 if (mval->br_state == XFS_EXT_NORM &&
4131 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4132 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4133 return 0;
4134
4135 /*
4136 * Modify (by adding) the state flag, if writing.
4137 */
4138 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004139 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4140 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004141 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004142 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004143 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004144 }
4145 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4146 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4147
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004148 /*
4149 * Before insertion into the bmbt, zero the range being converted
4150 * if required.
4151 */
4152 if (flags & XFS_BMAPI_ZERO) {
4153 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4154 mval->br_blockcount);
4155 if (error)
4156 return error;
4157 }
4158
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004159 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4160 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004161 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004162 /*
4163 * Log the inode core unconditionally in the unwritten extent conversion
4164 * path because the conversion might not have done so (e.g., if the
4165 * extent count hasn't changed). We need to make sure the inode is dirty
4166 * in the transaction for the sake of fsync(), even if nothing has
4167 * changed, because fsync() will not force the log for this transaction
4168 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004169 *
4170 * Note: If we're only converting cow fork extents, there aren't
4171 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004172 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004173 if (whichfork != XFS_COW_FORK)
4174 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004175 if (error)
4176 return error;
4177
4178 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004179 * Update our extent pointer, given that
4180 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4181 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004182 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004183 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004184
4185 /*
4186 * We may have combined previously unwritten space with written space,
4187 * so generate another request.
4188 */
4189 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004190 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004191 return 0;
4192}
4193
Christoph Hellwig44032802011-09-18 20:40:48 +00004194/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004195 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4196 * extent state if necessary. Details behaviour is controlled by the flags
4197 * parameter. Only allocates blocks from a single allocation group, to avoid
4198 * locking problems.
4199 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 * The returned value in "firstblock" from the first call in a transaction
4201 * must be remembered and presented to subsequent calls in "firstblock".
4202 * An upper bound for the number of blocks to be allocated is supplied to
4203 * the first call in "total"; if no allocation group has that many free
4204 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4205 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004206int
4207xfs_bmapi_write(
4208 struct xfs_trans *tp, /* transaction pointer */
4209 struct xfs_inode *ip, /* incore inode */
4210 xfs_fileoff_t bno, /* starting file offs. mapped */
4211 xfs_filblks_t len, /* length to map in file */
4212 int flags, /* XFS_BMAPI_... */
4213 xfs_fsblock_t *firstblock, /* first allocated block
4214 controls a.g. for allocs */
4215 xfs_extlen_t total, /* total blocks needed */
4216 struct xfs_bmbt_irec *mval, /* output: map values */
4217 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004218 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004220 struct xfs_mount *mp = ip->i_mount;
4221 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004222 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004223 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004224 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004225 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004226 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004227 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004228 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004229
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004231 xfs_fileoff_t orig_bno; /* original block number value */
4232 int orig_flags; /* original flags arg value */
4233 xfs_filblks_t orig_len; /* original value of len arg */
4234 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4235 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236
4237 orig_bno = bno;
4238 orig_len = len;
4239 orig_flags = flags;
4240 orig_mval = mval;
4241 orig_nmap = *nmap;
4242#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004243 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004244
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004246 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4247 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004248 ASSERT(tp != NULL ||
4249 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4250 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004251 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004252 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004253 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004254 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004255
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004256 /* zeroing is for currently only for data extents, not metadata */
4257 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4258 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4259 /*
4260 * we can allocate unwritten extents or pre-zero allocated blocks,
4261 * but it makes no sense to do both at once. This would result in
4262 * zeroing the unwritten extent twice, but it still being an
4263 * unwritten extent....
4264 */
4265 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4266 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4267
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 if (unlikely(XFS_TEST_ERROR(
4269 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004270 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004271 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004272 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004273 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004277 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004280
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004281 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004282
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004283 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004285 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004287 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004288 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004289 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004290 }
4291
4292 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4293 error = xfs_iread_extents(tp, ip, whichfork);
4294 if (error)
4295 goto error0;
4296 }
4297
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 n = 0;
4299 end = bno + len;
4300 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004301
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004302 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4303 eof = true;
4304 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4305 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004306 bma.tp = tp;
4307 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004308 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004309 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004310 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004311 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004312
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004314 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004315
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004316 /* in hole or beyoned EOF? */
4317 if (eof || bma.got.br_startoff > bno) {
4318 if (flags & XFS_BMAPI_DELALLOC) {
4319 /*
4320 * For the COW fork we can reasonably get a
4321 * request for converting an extent that races
4322 * with other threads already having converted
4323 * part of it, as there converting COW to
4324 * regular blocks is not protected using the
4325 * IOLOCK.
4326 */
4327 ASSERT(flags & XFS_BMAPI_COWFORK);
4328 if (!(flags & XFS_BMAPI_COWFORK)) {
4329 error = -EIO;
4330 goto error0;
4331 }
4332
4333 if (eof || bno >= end)
4334 break;
4335 } else {
4336 need_alloc = true;
4337 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004338 } else if (isnullstartblock(bma.got.br_startblock)) {
4339 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004340 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004341
4342 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 * First, deal with the hole before the allocated space
4344 * that we found, if any.
4345 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004346 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004347 bma.eof = eof;
4348 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4349 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004350 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004351 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004352
Dave Chinnera99ebf42011-12-01 11:24:20 +00004353 /*
4354 * There's a 32/64 bit type mismatch between the
4355 * allocation length request (which can be 64 bits in
4356 * length) and the bma length request, which is
4357 * xfs_extlen_t and therefore 32 bits. Hence we have to
4358 * check for 32-bit overflows and handle them here.
4359 */
4360 if (len > (xfs_filblks_t)MAXEXTLEN)
4361 bma.length = MAXEXTLEN;
4362 else
4363 bma.length = len;
4364
4365 ASSERT(len > 0);
4366 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004367 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 if (error)
4369 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004370 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004371 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004372
4373 /*
4374 * If this is a CoW allocation, record the data in
4375 * the refcount btree for orphan recovery.
4376 */
4377 if (whichfork == XFS_COW_FORK) {
4378 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4379 bma.blkno, bma.length);
4380 if (error)
4381 goto error0;
4382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004384
Dave Chinneraef9a892011-09-18 20:40:44 +00004385 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004386 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4387 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Dave Chinnerb447fe52011-09-18 20:40:51 +00004389 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004390 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004391 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004392 continue;
4393 if (error)
4394 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395
Dave Chinneraef9a892011-09-18 20:40:44 +00004396 /* update the extent map to return */
4397 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 /*
4400 * If we're done, stop now. Stop when we've allocated
4401 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4402 * the transaction may get too big.
4403 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004404 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004406
4407 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004408 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004409 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4410 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004413
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 /*
4415 * Transform from btree to extents, give it cur.
4416 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004417 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004418 int tmp_logflags = 0;
4419
Dave Chinner29c8d172011-09-18 20:41:00 +00004420 ASSERT(bma.cur);
4421 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004423 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 if (error)
4425 goto error0;
4426 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004427
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004429 XFS_IFORK_NEXTENTS(ip, whichfork) >
4430 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432error0:
4433 /*
4434 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004435 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004437 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004439 bma.logflags &= ~xfs_ilog_fext(whichfork);
4440 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004442 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 /*
4444 * Log whatever the flags say, even if error. Otherwise we might miss
4445 * detecting a case where the data is changed, there's an error,
4446 * and it's not logged so we don't shutdown when we should.
4447 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004448 if (bma.logflags)
4449 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004450
Dave Chinner29c8d172011-09-18 20:41:00 +00004451 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 if (!error) {
4453 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004454 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004456 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004457 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004458 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004459 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4461 }
4462 if (!error)
4463 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4464 orig_nmap, *nmap);
4465 return error;
4466}
4467
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004468static int
4469xfs_bmapi_remap(
4470 struct xfs_trans *tp,
4471 struct xfs_inode *ip,
4472 xfs_fileoff_t bno,
4473 xfs_filblks_t len,
4474 xfs_fsblock_t startblock,
4475 struct xfs_defer_ops *dfops)
4476{
4477 struct xfs_mount *mp = ip->i_mount;
4478 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4479 struct xfs_btree_cur *cur = NULL;
4480 xfs_fsblock_t firstblock = NULLFSBLOCK;
4481 struct xfs_bmbt_irec got;
4482 xfs_extnum_t idx;
4483 int logflags = 0, error;
4484
4485 ASSERT(len > 0);
4486 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4487 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4488
4489 if (unlikely(XFS_TEST_ERROR(
4490 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4491 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004492 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004493 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4494 return -EFSCORRUPTED;
4495 }
4496
4497 if (XFS_FORCED_SHUTDOWN(mp))
4498 return -EIO;
4499
4500 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4501 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4502 if (error)
4503 return error;
4504 }
4505
4506 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4507 /* make sure we only reflink into a hole. */
4508 ASSERT(got.br_startoff > bno);
4509 ASSERT(got.br_startoff - bno >= len);
4510 }
4511
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004512 ip->i_d.di_nblocks += len;
4513 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004514
4515 if (ifp->if_flags & XFS_IFBROOT) {
4516 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4517 cur->bc_private.b.firstblock = firstblock;
4518 cur->bc_private.b.dfops = dfops;
4519 cur->bc_private.b.flags = 0;
4520 }
4521
4522 got.br_startoff = bno;
4523 got.br_startblock = startblock;
4524 got.br_blockcount = len;
4525 got.br_state = XFS_EXT_NORM;
4526
4527 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4528 &got, &firstblock, dfops, &logflags);
4529 if (error)
4530 goto error0;
4531
4532 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4533 int tmp_logflags = 0;
4534
4535 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4536 &tmp_logflags, XFS_DATA_FORK);
4537 logflags |= tmp_logflags;
4538 }
4539
4540error0:
4541 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4542 logflags &= ~XFS_ILOG_DEXT;
4543 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4544 logflags &= ~XFS_ILOG_DBROOT;
4545
4546 if (logflags)
4547 xfs_trans_log_inode(tp, ip, logflags);
4548 if (cur) {
4549 xfs_btree_del_cursor(cur,
4550 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4551 }
4552 return error;
4553}
4554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004556 * When a delalloc extent is split (e.g., due to a hole punch), the original
4557 * indlen reservation must be shared across the two new extents that are left
4558 * behind.
4559 *
4560 * Given the original reservation and the worst case indlen for the two new
4561 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004562 * reservation fairly across the two new extents. If necessary, steal available
4563 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4564 * ores == 1). The number of stolen blocks is returned. The availability and
4565 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004566 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004567static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004568xfs_bmap_split_indlen(
4569 xfs_filblks_t ores, /* original res. */
4570 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004571 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4572 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004573{
4574 xfs_filblks_t len1 = *indlen1;
4575 xfs_filblks_t len2 = *indlen2;
4576 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004577 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004578 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004579
4580 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004581 * Steal as many blocks as we can to try and satisfy the worst case
4582 * indlen for both new extents.
4583 */
Brian Foster75d65362017-02-13 22:48:30 -08004584 if (ores < nres && avail)
4585 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4586 ores += stolen;
4587
4588 /* nothing else to do if we've satisfied the new reservation */
4589 if (ores >= nres)
4590 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004591
4592 /*
Brian Foster75d65362017-02-13 22:48:30 -08004593 * We can't meet the total required reservation for the two extents.
4594 * Calculate the percent of the overall shortage between both extents
4595 * and apply this percentage to each of the requested indlen values.
4596 * This distributes the shortage fairly and reduces the chances that one
4597 * of the two extents is left with nothing when extents are repeatedly
4598 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004599 */
Brian Foster75d65362017-02-13 22:48:30 -08004600 resfactor = (ores * 100);
4601 do_div(resfactor, nres);
4602 len1 *= resfactor;
4603 do_div(len1, 100);
4604 len2 *= resfactor;
4605 do_div(len2, 100);
4606 ASSERT(len1 + len2 <= ores);
4607 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4608
4609 /*
4610 * Hand out the remainder to each extent. If one of the two reservations
4611 * is zero, we want to make sure that one gets a block first. The loop
4612 * below starts with len1, so hand len2 a block right off the bat if it
4613 * is zero.
4614 */
4615 ores -= (len1 + len2);
4616 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4617 if (ores && !len2 && *indlen2) {
4618 len2++;
4619 ores--;
4620 }
4621 while (ores) {
4622 if (len1 < *indlen1) {
4623 len1++;
4624 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004625 }
Brian Foster75d65362017-02-13 22:48:30 -08004626 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004627 break;
Brian Foster75d65362017-02-13 22:48:30 -08004628 if (len2 < *indlen2) {
4629 len2++;
4630 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004631 }
4632 }
4633
4634 *indlen1 = len1;
4635 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004636
4637 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004638}
4639
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004640int
4641xfs_bmap_del_extent_delay(
4642 struct xfs_inode *ip,
4643 int whichfork,
4644 xfs_extnum_t *idx,
4645 struct xfs_bmbt_irec *got,
4646 struct xfs_bmbt_irec *del)
4647{
4648 struct xfs_mount *mp = ip->i_mount;
4649 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4650 struct xfs_bmbt_irec new;
4651 int64_t da_old, da_new, da_diff = 0;
4652 xfs_fileoff_t del_endoff, got_endoff;
4653 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004654 int state = xfs_bmap_fork_to_state(whichfork);
4655 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004656 bool isrt;
4657
4658 XFS_STATS_INC(mp, xs_del_exlist);
4659
4660 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4661 del_endoff = del->br_startoff + del->br_blockcount;
4662 got_endoff = got->br_startoff + got->br_blockcount;
4663 da_old = startblockval(got->br_startblock);
4664 da_new = 0;
4665
4666 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004667 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004668 ASSERT(del->br_blockcount > 0);
4669 ASSERT(got->br_startoff <= del->br_startoff);
4670 ASSERT(got_endoff >= del_endoff);
4671
4672 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004673 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004674
4675 do_div(rtexts, mp->m_sb.sb_rextsize);
4676 xfs_mod_frextents(mp, rtexts);
4677 }
4678
4679 /*
4680 * Update the inode delalloc counter now and wait to update the
4681 * sb counters as we might have to borrow some blocks for the
4682 * indirect block accounting.
4683 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004684 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4685 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004686 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004687 if (error)
4688 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004689 ip->i_delayed_blks -= del->br_blockcount;
4690
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004691 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004692 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004693 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004694 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004695
Christoph Hellwig0173c682017-10-17 14:16:22 -07004696 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4697 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004698 /*
4699 * Matches the whole extent. Delete the entry.
4700 */
4701 xfs_iext_remove(ip, *idx, 1, state);
4702 --*idx;
4703 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004704 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004705 /*
4706 * Deleting the first part of the extent.
4707 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004708 got->br_startoff = del_endoff;
4709 got->br_blockcount -= del->br_blockcount;
4710 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4711 got->br_blockcount), da_old);
4712 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004713 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004714 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004715 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004716 /*
4717 * Deleting the last part of the extent.
4718 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004719 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4720 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4721 got->br_blockcount), da_old);
4722 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004723 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004724 break;
4725 case 0:
4726 /*
4727 * Deleting the middle of the extent.
4728 *
4729 * Distribute the original indlen reservation across the two new
4730 * extents. Steal blocks from the deleted extent if necessary.
4731 * Stealing blocks simply fudges the fdblocks accounting below.
4732 * Warn if either of the new indlen reservations is zero as this
4733 * can lead to delalloc problems.
4734 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004735 got->br_blockcount = del->br_startoff - got->br_startoff;
4736 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4737
4738 new.br_blockcount = got_endoff - del_endoff;
4739 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4740
4741 WARN_ON_ONCE(!got_indlen || !new_indlen);
4742 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4743 del->br_blockcount);
4744
4745 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004746 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004747
4748 new.br_startoff = del_endoff;
4749 new.br_state = got->br_state;
4750 new.br_startblock = nullstartblock((int)new_indlen);
4751
4752 ++*idx;
4753 xfs_iext_insert(ip, *idx, 1, &new, state);
4754
4755 da_new = got_indlen + new_indlen - stolen;
4756 del->br_blockcount -= stolen;
4757 break;
4758 }
4759
4760 ASSERT(da_old >= da_new);
4761 da_diff = da_old - da_new;
4762 if (!isrt)
4763 da_diff += del->br_blockcount;
4764 if (da_diff)
4765 xfs_mod_fdblocks(mp, da_diff, false);
4766 return error;
4767}
4768
4769void
4770xfs_bmap_del_extent_cow(
4771 struct xfs_inode *ip,
4772 xfs_extnum_t *idx,
4773 struct xfs_bmbt_irec *got,
4774 struct xfs_bmbt_irec *del)
4775{
4776 struct xfs_mount *mp = ip->i_mount;
4777 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4778 struct xfs_bmbt_irec new;
4779 xfs_fileoff_t del_endoff, got_endoff;
4780 int state = BMAP_COWFORK;
4781
4782 XFS_STATS_INC(mp, xs_del_exlist);
4783
4784 del_endoff = del->br_startoff + del->br_blockcount;
4785 got_endoff = got->br_startoff + got->br_blockcount;
4786
4787 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004788 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004789 ASSERT(del->br_blockcount > 0);
4790 ASSERT(got->br_startoff <= del->br_startoff);
4791 ASSERT(got_endoff >= del_endoff);
4792 ASSERT(!isnullstartblock(got->br_startblock));
4793
4794 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004795 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004796 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004797 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004798
Christoph Hellwig0173c682017-10-17 14:16:22 -07004799 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4800 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004801 /*
4802 * Matches the whole extent. Delete the entry.
4803 */
4804 xfs_iext_remove(ip, *idx, 1, state);
4805 --*idx;
4806 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004807 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004808 /*
4809 * Deleting the first part of the extent.
4810 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004811 got->br_startoff = del_endoff;
4812 got->br_blockcount -= del->br_blockcount;
4813 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004814 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004815 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004816 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004817 /*
4818 * Deleting the last part of the extent.
4819 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004820 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004821 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004822 break;
4823 case 0:
4824 /*
4825 * Deleting the middle of the extent.
4826 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004827 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004828 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004829
4830 new.br_startoff = del_endoff;
4831 new.br_blockcount = got_endoff - del_endoff;
4832 new.br_state = got->br_state;
4833 new.br_startblock = del->br_startblock + del->br_blockcount;
4834
4835 ++*idx;
4836 xfs_iext_insert(ip, *idx, 1, &new, state);
4837 break;
4838 }
4839}
4840
Brian Fostera9bd24a2016-03-15 11:42:46 +11004841/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004842 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004843 * after removing space.
Dave Chinner9e5987a2013-02-25 12:31:26 +11004844 */
4845STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004846xfs_bmap_del_extent_real(
Dave Chinner9e5987a2013-02-25 12:31:26 +11004847 xfs_inode_t *ip, /* incore inode pointer */
4848 xfs_trans_t *tp, /* current transaction pointer */
4849 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004850 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004851 xfs_btree_cur_t *cur, /* if null, not a btree */
4852 xfs_bmbt_irec_t *del, /* data to remove from extents */
4853 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004854 int whichfork, /* data or attr fork */
4855 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004856{
Dave Chinner9e5987a2013-02-25 12:31:26 +11004857 xfs_fsblock_t del_endblock=0; /* first block past del */
4858 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004859 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004860 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004861 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004862 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004863 xfs_fileoff_t got_endoff; /* first offset past got */
4864 int i; /* temp state */
4865 xfs_ifork_t *ifp; /* inode fork pointer */
4866 xfs_mount_t *mp; /* mount structure */
4867 xfs_filblks_t nblks; /* quota/sb block count */
4868 xfs_bmbt_irec_t new; /* new record to be inserted */
4869 /* REFERENCED */
4870 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004871 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004872 struct xfs_bmbt_irec old;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004873
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004874 mp = ip->i_mount;
4875 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004876
Dave Chinner9e5987a2013-02-25 12:31:26 +11004877 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11004878 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004879 ASSERT(del->br_blockcount > 0);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004880 xfs_iext_get_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004881 ASSERT(got.br_startoff <= del->br_startoff);
4882 del_endoff = del->br_startoff + del->br_blockcount;
4883 got_endoff = got.br_startoff + got.br_blockcount;
4884 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004885 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004886 qfield = 0;
4887 error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004888
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004889 /*
4890 * If it's the case where the directory code is running with no block
4891 * reservation, and the deleted block is in the middle of its extent,
4892 * and the resulting insert of an extent would cause transformation to
4893 * btree format, then reject it. The calling code will then swap blocks
4894 * around instead. We have to do this now, rather than waiting for the
4895 * conversion to btree format, since the transaction will be dirty then.
4896 */
4897 if (tp->t_blk_res == 0 &&
4898 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4899 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4900 XFS_IFORK_MAXEXT(ip, whichfork) &&
4901 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4902 return -ENOSPC;
4903
4904 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004905 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4906 xfs_fsblock_t bno;
4907 xfs_filblks_t len;
4908
4909 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
4910 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
4911 bno = del->br_startblock;
4912 len = del->br_blockcount;
4913 do_div(bno, mp->m_sb.sb_rextsize);
4914 do_div(len, mp->m_sb.sb_rextsize);
4915 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4916 if (error)
4917 goto done;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004918 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004919 nblks = len * mp->m_sb.sb_rextsize;
4920 qfield = XFS_TRANS_DQ_RTBCOUNT;
4921 } else {
4922 do_fx = 1;
4923 nblks = del->br_blockcount;
4924 qfield = XFS_TRANS_DQ_BCOUNT;
4925 }
4926
4927 del_endblock = del->br_startblock + del->br_blockcount;
4928 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07004929 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004930 if (error)
4931 goto done;
4932 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004933 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004934
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004935 if (got.br_startoff == del->br_startoff)
4936 state |= BMAP_LEFT_FILLING;
4937 if (got_endoff == del_endoff)
4938 state |= BMAP_RIGHT_FILLING;
4939
4940 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4941 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004942 /*
4943 * Matches the whole extent. Delete the entry.
4944 */
Christoph Hellwig35e62da2017-10-19 11:04:43 -07004945 xfs_iext_remove(ip, *idx, 1, state);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004946 --*idx;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004947
4948 XFS_IFORK_NEXT_SET(ip, whichfork,
4949 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4950 flags |= XFS_ILOG_CORE;
4951 if (!cur) {
4952 flags |= xfs_ilog_fext(whichfork);
4953 break;
4954 }
4955 if ((error = xfs_btree_delete(cur, &i)))
4956 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004957 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004958 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004959 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004960 /*
4961 * Deleting the first part of the extent.
4962 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004963 got.br_startoff = del_endoff;
4964 got.br_startblock = del_endblock;
4965 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004966 xfs_iext_update_extent(ip, state, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004967 if (!cur) {
4968 flags |= xfs_ilog_fext(whichfork);
4969 break;
4970 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004971 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004972 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004973 goto done;
4974 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004975 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004976 /*
4977 * Deleting the last part of the extent.
4978 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004979 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004980 xfs_iext_update_extent(ip, state, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004981 if (!cur) {
4982 flags |= xfs_ilog_fext(whichfork);
4983 break;
4984 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004985 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004986 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004987 goto done;
4988 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004989 case 0:
4990 /*
4991 * Deleting the middle of the extent.
4992 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004993 old = got;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004994
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004995 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004996 xfs_iext_update_extent(ip, state, *idx, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004997
Dave Chinner9e5987a2013-02-25 12:31:26 +11004998 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004999 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005000 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005001 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005002
Christoph Hellwige1d75532017-10-17 14:16:21 -07005003 flags |= XFS_ILOG_CORE;
5004 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005005 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005006 if (error)
5007 goto done;
5008 error = xfs_btree_increment(cur, 0, &i);
5009 if (error)
5010 goto done;
5011 cur->bc_rec.b = new;
5012 error = xfs_btree_insert(cur, &i);
5013 if (error && error != -ENOSPC)
5014 goto done;
5015 /*
5016 * If get no-space back from btree insert, it tried a
5017 * split, and we have a zero block reservation. Fix up
5018 * our state and return the error.
5019 */
5020 if (error == -ENOSPC) {
5021 /*
5022 * Reset the cursor, don't trust it after any
5023 * insert operation.
5024 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005025 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005026 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005027 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005028 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5029 /*
5030 * Update the btree record back
5031 * to the original value.
5032 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005033 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005034 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005035 goto done;
5036 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005037 * Reset the extent record back
5038 * to the original value.
Dave Chinner9e5987a2013-02-25 12:31:26 +11005039 */
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005040 xfs_iext_update_extent(ip, state, *idx, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005041 flags = 0;
5042 error = -ENOSPC;
5043 goto done;
5044 }
5045 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5046 } else
5047 flags |= xfs_ilog_fext(whichfork);
5048 XFS_IFORK_NEXT_SET(ip, whichfork,
5049 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005050 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5051 ++*idx;
5052 break;
5053 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005054
5055 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005056 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5057 if (error)
5058 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005059
Dave Chinner9e5987a2013-02-25 12:31:26 +11005060 /*
5061 * If we need to, add to list of extents to delete.
5062 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005063 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005064 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5065 error = xfs_refcount_decrease_extent(mp, dfops, del);
5066 if (error)
5067 goto done;
5068 } else
5069 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5070 del->br_blockcount, NULL);
5071 }
5072
Dave Chinner9e5987a2013-02-25 12:31:26 +11005073 /*
5074 * Adjust inode # blocks in the file.
5075 */
5076 if (nblks)
5077 ip->i_d.di_nblocks -= nblks;
5078 /*
5079 * Adjust quota data.
5080 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005081 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005082 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5083
Dave Chinner9e5987a2013-02-25 12:31:26 +11005084done:
5085 *logflagsp = flags;
5086 return error;
5087}
5088
5089/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 * Unmap (remove) blocks from a file.
5091 * If nexts is nonzero then the number of extents to remove is limited to
5092 * that value. If not all extents in the block range can be removed then
5093 * *done is set.
5094 */
5095int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005096__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 xfs_trans_t *tp, /* transaction pointer */
5098 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005099 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005100 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 int flags, /* misc flags */
5102 xfs_extnum_t nexts, /* number of extents max */
5103 xfs_fsblock_t *firstblock, /* first allocated block
5104 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005105 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106{
5107 xfs_btree_cur_t *cur; /* bmap btree cursor */
5108 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 int error; /* error return value */
5110 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005111 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 xfs_ifork_t *ifp; /* inode fork pointer */
5113 int isrt; /* freeing in rt area */
5114 xfs_extnum_t lastx; /* last extent index used */
5115 int logflags; /* transaction logging flags */
5116 xfs_extlen_t mod; /* rt extent offset */
5117 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 int tmp_logflags; /* partial logging flags */
5119 int wasdel; /* was a delayed alloc extent */
5120 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005122 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005123 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005124 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005125 xfs_fileoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005127 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005128
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005129 whichfork = xfs_bmapi_whichfork(flags);
5130 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 ifp = XFS_IFORK_PTR(ip, whichfork);
5132 if (unlikely(
5133 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5134 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5135 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5136 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005137 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005138 }
5139 mp = ip->i_mount;
5140 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005141 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005142
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005143 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 ASSERT(len > 0);
5145 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005146
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005147 /*
5148 * Guesstimate how many blocks we can unmap without running the risk of
5149 * blowing out the transaction with a mix of EFIs and reflink
5150 * adjustments.
5151 */
5152 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5153 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5154 else
5155 max_len = len;
5156
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5158 (error = xfs_iread_extents(tp, ip, whichfork)))
5159 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005160 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005161 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 return 0;
5163 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005164 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005165 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005166 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005167
Christoph Hellwigdc560152017-10-23 16:32:39 -07005168 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &lastx, &got)) {
5169 *rlen = 0;
5170 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005172 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005173
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 logflags = 0;
5175 if (ifp->if_flags & XFS_IFBROOT) {
5176 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005177 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005179 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 cur->bc_private.b.flags = 0;
5181 } else
5182 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005183
5184 if (isrt) {
5185 /*
5186 * Synchronize by locking the bitmap inode.
5187 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005188 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005189 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005190 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5191 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005192 }
5193
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 extno = 0;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005195 while (end != (xfs_fileoff_t)-1 && end >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005196 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005198 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 * Just back up to the previous extent, if so.
5200 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005201 if (got.br_startoff > end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 if (--lastx < 0)
5203 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005204 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 }
5206 /*
5207 * Is the last block of this extent before the range
5208 * we're supposed to delete? If so, we're done.
5209 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005210 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005212 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 break;
5214 /*
5215 * Then deal with the (possibly delayed) allocated space
5216 * we found.
5217 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005219 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005220
5221 /*
5222 * Make sure we don't touch multiple AGF headers out of order
5223 * in a single transaction, as that could cause AB-BA deadlocks.
5224 */
5225 if (!wasdel) {
5226 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5227 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5228 break;
5229 prev_agno = agno;
5230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 if (got.br_startoff < start) {
5232 del.br_startoff = start;
5233 del.br_blockcount -= start - got.br_startoff;
5234 if (!wasdel)
5235 del.br_startblock += start - got.br_startoff;
5236 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005237 if (del.br_startoff + del.br_blockcount > end + 1)
5238 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005239
5240 /* How much can we safely unmap? */
5241 if (max_len < del.br_blockcount) {
5242 del.br_startoff += del.br_blockcount - max_len;
5243 if (!wasdel)
5244 del.br_startblock += del.br_blockcount - max_len;
5245 del.br_blockcount = max_len;
5246 }
5247
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 sum = del.br_startblock + del.br_blockcount;
5249 if (isrt &&
5250 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5251 /*
5252 * Realtime extent not lined up at the end.
5253 * The extent could have been split into written
5254 * and unwritten pieces, or we could just be
5255 * unmapping part of it. But we can't really
5256 * get rid of part of a realtime extent.
5257 */
5258 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005259 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 /*
5261 * This piece is unwritten, or we're not
5262 * using unwritten extents. Skip over it.
5263 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005264 ASSERT(end >= mod);
5265 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266 del.br_blockcount : mod;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005267 if (end < got.br_startoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 if (--lastx >= 0)
Christoph Hellwige3f0f752017-10-17 14:16:20 -07005269 xfs_iext_get_extent(ifp, lastx,
5270 &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 }
5272 continue;
5273 }
5274 /*
5275 * It's written, turn it unwritten.
5276 * This is better than zeroing it.
5277 */
5278 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005279 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 /*
5281 * If this spans a realtime extent boundary,
5282 * chop it back to the start of the one we end at.
5283 */
5284 if (del.br_blockcount > mod) {
5285 del.br_startoff += del.br_blockcount - mod;
5286 del.br_startblock += del.br_blockcount - mod;
5287 del.br_blockcount = mod;
5288 }
5289 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005290 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005291 whichfork, &lastx, &cur, &del,
5292 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 if (error)
5294 goto error0;
5295 goto nodelete;
5296 }
5297 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5298 /*
5299 * Realtime extent is lined up at the end but not
5300 * at the front. We'll get rid of full extents if
5301 * we can.
5302 */
5303 mod = mp->m_sb.sb_rextsize - mod;
5304 if (del.br_blockcount > mod) {
5305 del.br_blockcount -= mod;
5306 del.br_startoff += mod;
5307 del.br_startblock += mod;
5308 } else if ((del.br_startoff == start &&
5309 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005310 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005311 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 /*
5313 * Can't make it unwritten. There isn't
5314 * a full extent here so just skip it.
5315 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005316 ASSERT(end >= del.br_blockcount);
5317 end -= del.br_blockcount;
5318 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005319 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 continue;
5321 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005322 struct xfs_bmbt_irec prev;
5323
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 /*
5325 * This one is already unwritten.
5326 * It must have a written left neighbor.
5327 * Unwrite the killed part of that one and
5328 * try again.
5329 */
5330 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005331 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005333 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 ASSERT(del.br_startblock ==
5335 prev.br_startblock + prev.br_blockcount);
5336 if (prev.br_startoff < start) {
5337 mod = start - prev.br_startoff;
5338 prev.br_blockcount -= mod;
5339 prev.br_startblock += mod;
5340 prev.br_startoff = start;
5341 }
5342 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005343 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005344 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005345 ip, whichfork, &lastx, &cur,
5346 &prev, firstblock, dfops,
5347 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 if (error)
5349 goto error0;
5350 goto nodelete;
5351 } else {
5352 ASSERT(del.br_state == XFS_EXT_NORM);
5353 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005354 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005355 ip, whichfork, &lastx, &cur,
5356 &del, firstblock, dfops,
5357 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 if (error)
5359 goto error0;
5360 goto nodelete;
5361 }
5362 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005363
Brian Fosterb2706a02016-03-15 11:42:46 +11005364 if (wasdel) {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005365 error = xfs_bmap_del_extent_delay(ip, whichfork, &lastx,
5366 &got, &del);
5367 } else {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005368 error = xfs_bmap_del_extent_real(ip, tp, &lastx, dfops,
5369 cur, &del, &tmp_logflags, whichfork,
5370 flags);
5371 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005372 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005373
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 if (error)
5375 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005376
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005377 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005378 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380 /*
5381 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005383 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005384 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005385 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005386 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005387 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 extno++;
5390 }
5391 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005392 if (end == (xfs_fileoff_t)-1 || end < start || lastx < 0)
Darrick J. Wong44535932016-10-03 09:11:29 -07005393 *rlen = 0;
5394 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005395 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005396
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397 /*
5398 * Convert to a btree if necessary.
5399 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005400 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005402 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 &cur, 0, &tmp_logflags, whichfork);
5404 logflags |= tmp_logflags;
5405 if (error)
5406 goto error0;
5407 }
5408 /*
5409 * transform from btree to extents, give it cur
5410 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005411 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 ASSERT(cur != NULL);
5413 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5414 whichfork);
5415 logflags |= tmp_logflags;
5416 if (error)
5417 goto error0;
5418 }
5419 /*
5420 * transform from extents to local?
5421 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 error = 0;
5423error0:
5424 /*
5425 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005426 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005428 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005430 logflags &= ~xfs_ilog_fext(whichfork);
5431 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005433 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 /*
5435 * Log inode even in the error case, if the transaction
5436 * is dirty we'll need to shut down the filesystem.
5437 */
5438 if (logflags)
5439 xfs_trans_log_inode(tp, ip, logflags);
5440 if (cur) {
5441 if (!error) {
5442 *firstblock = cur->bc_private.b.firstblock;
5443 cur->bc_private.b.allocated = 0;
5444 }
5445 xfs_btree_del_cursor(cur,
5446 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5447 }
5448 return error;
5449}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005450
Darrick J. Wong44535932016-10-03 09:11:29 -07005451/* Unmap a range of a file. */
5452int
5453xfs_bunmapi(
5454 xfs_trans_t *tp,
5455 struct xfs_inode *ip,
5456 xfs_fileoff_t bno,
5457 xfs_filblks_t len,
5458 int flags,
5459 xfs_extnum_t nexts,
5460 xfs_fsblock_t *firstblock,
5461 struct xfs_defer_ops *dfops,
5462 int *done)
5463{
5464 int error;
5465
5466 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5467 dfops);
5468 *done = (len == 0);
5469 return error;
5470}
5471
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005472/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005473 * Determine whether an extent shift can be accomplished by a merge with the
5474 * extent that precedes the target hole of the shift.
5475 */
5476STATIC bool
5477xfs_bmse_can_merge(
5478 struct xfs_bmbt_irec *left, /* preceding extent */
5479 struct xfs_bmbt_irec *got, /* current extent to shift */
5480 xfs_fileoff_t shift) /* shift fsb */
5481{
5482 xfs_fileoff_t startoff;
5483
5484 startoff = got->br_startoff - shift;
5485
5486 /*
5487 * The extent, once shifted, must be adjacent in-file and on-disk with
5488 * the preceding extent.
5489 */
5490 if ((left->br_startoff + left->br_blockcount != startoff) ||
5491 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5492 (left->br_state != got->br_state) ||
5493 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5494 return false;
5495
5496 return true;
5497}
5498
5499/*
5500 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5501 * hole in the file. If an extent shift would result in the extent being fully
5502 * adjacent to the extent that currently precedes the hole, we can merge with
5503 * the preceding extent rather than do the shift.
5504 *
5505 * This function assumes the caller has verified a shift-by-merge is possible
5506 * with the provided extents via xfs_bmse_can_merge().
5507 */
5508STATIC int
5509xfs_bmse_merge(
5510 struct xfs_inode *ip,
5511 int whichfork,
5512 xfs_fileoff_t shift, /* shift fsb */
5513 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005514 struct xfs_bmbt_irec *got, /* extent to shift */
5515 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005516 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005517 int *logflags, /* output */
5518 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005519{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005520 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005521 xfs_filblks_t blockcount;
5522 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005523 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005524
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005525 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005526
5527 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5528 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005529 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005530
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005531 new = *left;
5532 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005533
5534 /*
5535 * Update the on-disk extent count, the btree if necessary and log the
5536 * inode.
5537 */
5538 XFS_IFORK_NEXT_SET(ip, whichfork,
5539 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5540 *logflags |= XFS_ILOG_CORE;
5541 if (!cur) {
5542 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005543 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005544 }
5545
5546 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005547 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005548 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005549 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005550 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005551
5552 error = xfs_btree_delete(cur, &i);
5553 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005554 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005555 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005556
5557 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005558 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005559 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005560 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005561 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005562
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005563 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005564 if (error)
5565 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005566
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005567done:
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005568 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
5569 current_ext - 1, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005570 xfs_iext_remove(ip, current_ext, 1, 0);
5571
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005572 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005573 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5574 if (error)
5575 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005576 memcpy(&new, got, sizeof(new));
5577 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005578 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005579}
5580
Christoph Hellwigbf806282017-10-19 11:07:34 -07005581static int
5582xfs_bmap_shift_update_extent(
5583 struct xfs_inode *ip,
5584 int whichfork,
5585 xfs_extnum_t idx,
5586 struct xfs_bmbt_irec *got,
5587 struct xfs_btree_cur *cur,
5588 int *logflags,
5589 struct xfs_defer_ops *dfops,
5590 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005591{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005592 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005593 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005594 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005595
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005596 *logflags |= XFS_ILOG_CORE;
5597
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005598 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005599
5600 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005601 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005602 if (error)
5603 return error;
5604 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5605
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005606 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005607 if (error)
5608 return error;
5609 } else {
5610 *logflags |= XFS_ILOG_DEXT;
5611 }
5612
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005613 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), idx, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005614
Darrick J. Wong9c194642016-08-03 12:16:05 +10005615 /* update reverse mapping */
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005616 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005617 if (error)
5618 return error;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005619 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005620}
5621
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005622int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005623xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005624 struct xfs_trans *tp,
5625 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005626 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005627 xfs_fileoff_t offset_shift_fsb,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005628 bool *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005629 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005630 xfs_fsblock_t *firstblock,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005631 struct xfs_defer_ops *dfops)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005632{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005633 int whichfork = XFS_DATA_FORK;
5634 struct xfs_mount *mp = ip->i_mount;
5635 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5636 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005637 struct xfs_bmbt_irec got, prev;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005638 xfs_extnum_t current_ext;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005639 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005640 int error = 0;
5641 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005642
5643 if (unlikely(XFS_TEST_ERROR(
5644 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5645 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005646 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005647 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005648 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005649 }
5650
5651 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005652 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005653
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005654 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005655
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005656 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005657 error = xfs_iread_extents(tp, ip, whichfork);
5658 if (error)
5659 return error;
5660 }
5661
Brian Fosterddb19e32014-09-23 15:38:09 +10005662 if (ifp->if_flags & XFS_IFBROOT) {
5663 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5664 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005665 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005666 cur->bc_private.b.flags = 0;
5667 }
5668
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005669 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext, &got)) {
5670 *done = true;
5671 goto del_cursor;
5672 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005673 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005674
Christoph Hellwigbf806282017-10-19 11:07:34 -07005675 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005676 if (xfs_iext_get_extent(ifp, current_ext - 1, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005677 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5678 error = -EINVAL;
5679 goto del_cursor;
5680 }
5681
Christoph Hellwigbf806282017-10-19 11:07:34 -07005682 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5683 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5684 current_ext, &got, &prev, cur,
5685 &logflags, dfops);
5686 if (error)
5687 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005688
5689 /* update got after merge */
5690 if (!xfs_iext_get_extent(ifp, current_ext, &got)) {
5691 *done = true;
5692 goto del_cursor;
5693 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005694 goto done;
5695 }
5696 } else {
5697 if (got.br_startoff < offset_shift_fsb) {
5698 error = -EINVAL;
5699 goto del_cursor;
5700 }
5701 }
5702
5703 error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got,
5704 cur, &logflags, dfops, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005705 if (error)
5706 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005707
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005708 if (!xfs_iext_get_extent(ifp, ++current_ext, &got)) {
5709 *done = true;
5710 goto del_cursor;
5711 }
5712
5713done:
Christoph Hellwigbf806282017-10-19 11:07:34 -07005714 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005715del_cursor:
5716 if (cur)
5717 xfs_btree_del_cursor(cur,
5718 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005719 if (logflags)
5720 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005721 return error;
5722}
5723
5724int
5725xfs_bmap_insert_extents(
5726 struct xfs_trans *tp,
5727 struct xfs_inode *ip,
5728 xfs_fileoff_t *next_fsb,
5729 xfs_fileoff_t offset_shift_fsb,
5730 bool *done,
5731 xfs_fileoff_t stop_fsb,
5732 xfs_fsblock_t *firstblock,
5733 struct xfs_defer_ops *dfops)
5734{
5735 int whichfork = XFS_DATA_FORK;
5736 struct xfs_mount *mp = ip->i_mount;
5737 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5738 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005739 struct xfs_bmbt_irec got, next;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005740 xfs_extnum_t current_ext;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005741 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005742 int error = 0;
5743 int logflags = 0;
5744
5745 if (unlikely(XFS_TEST_ERROR(
5746 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5747 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5748 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5749 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5750 return -EFSCORRUPTED;
5751 }
5752
5753 if (XFS_FORCED_SHUTDOWN(mp))
5754 return -EIO;
5755
5756 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5757
5758 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5759 error = xfs_iread_extents(tp, ip, whichfork);
5760 if (error)
5761 return error;
5762 }
5763
5764 if (ifp->if_flags & XFS_IFBROOT) {
5765 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5766 cur->bc_private.b.firstblock = *firstblock;
5767 cur->bc_private.b.dfops = dfops;
5768 cur->bc_private.b.flags = 0;
5769 }
5770
Namjae Jeona904b1c2015-03-25 15:08:56 +11005771 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005772 current_ext = xfs_iext_count(ifp) - 1;
5773 if (!xfs_iext_get_extent(ifp, current_ext, &got) ||
5774 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005775 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005776 goto del_cursor;
5777 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005778 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005779 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
5780 &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005781 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005782 goto del_cursor;
5783 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005784 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005785 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005786
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005787 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005788 error = -EIO;
5789 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005790 }
5791
Christoph Hellwigbf806282017-10-19 11:07:34 -07005792 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005793 if (xfs_iext_get_extent(ifp, current_ext + 1, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005794 if (new_startoff + got.br_blockcount > next.br_startoff) {
5795 error = -EINVAL;
5796 goto del_cursor;
5797 }
5798
5799 /*
5800 * Unlike a left shift (which involves a hole punch), a right
5801 * shift does not modify extent neighbors in any way. We should
5802 * never find mergeable extents in this scenario. Check anyways
5803 * and warn if we encounter two extents that could be one.
5804 */
5805 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5806 WARN_ON_ONCE(1);
5807 }
5808
5809 error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got,
5810 cur, &logflags, dfops, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005811 if (error)
5812 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005813
5814 if (!xfs_iext_get_extent(ifp, --current_ext, &got) ||
5815 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005816 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005817 goto del_cursor;
5818 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005819
5820 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005821del_cursor:
5822 if (cur)
5823 xfs_btree_del_cursor(cur,
5824 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Brian Fosterca446d82014-09-02 12:12:53 +10005825 if (logflags)
5826 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005827 return error;
5828}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005829
5830/*
5831 * Splits an extent into two extents at split_fsb block such that it is
5832 * the first block of the current_ext. @current_ext is a target extent
5833 * to be split. @split_fsb is a block where the extents is split.
5834 * If split_fsb lies in a hole or the first block of extents, just return 0.
5835 */
5836STATIC int
5837xfs_bmap_split_extent_at(
5838 struct xfs_trans *tp,
5839 struct xfs_inode *ip,
5840 xfs_fileoff_t split_fsb,
5841 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005842 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005843{
5844 int whichfork = XFS_DATA_FORK;
5845 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005846 struct xfs_bmbt_irec got;
5847 struct xfs_bmbt_irec new; /* split extent */
5848 struct xfs_mount *mp = ip->i_mount;
5849 struct xfs_ifork *ifp;
5850 xfs_fsblock_t gotblkcnt; /* new block count for got */
5851 xfs_extnum_t current_ext;
5852 int error = 0;
5853 int logflags = 0;
5854 int i = 0;
5855
5856 if (unlikely(XFS_TEST_ERROR(
5857 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5858 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005859 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005860 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5861 XFS_ERRLEVEL_LOW, mp);
5862 return -EFSCORRUPTED;
5863 }
5864
5865 if (XFS_FORCED_SHUTDOWN(mp))
5866 return -EIO;
5867
5868 ifp = XFS_IFORK_PTR(ip, whichfork);
5869 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5870 /* Read in all the extents */
5871 error = xfs_iread_extents(tp, ip, whichfork);
5872 if (error)
5873 return error;
5874 }
5875
5876 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005877 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005878 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005879 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
5880 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005881 return 0;
5882
5883 gotblkcnt = split_fsb - got.br_startoff;
5884 new.br_startoff = split_fsb;
5885 new.br_startblock = got.br_startblock + gotblkcnt;
5886 new.br_blockcount = got.br_blockcount - gotblkcnt;
5887 new.br_state = got.br_state;
5888
5889 if (ifp->if_flags & XFS_IFBROOT) {
5890 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5891 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005892 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005893 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005894 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005895 if (error)
5896 goto del_cursor;
5897 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5898 }
5899
Namjae Jeona904b1c2015-03-25 15:08:56 +11005900 got.br_blockcount = gotblkcnt;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005901 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
5902 current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005903
5904 logflags = XFS_ILOG_CORE;
5905 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005906 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005907 if (error)
5908 goto del_cursor;
5909 } else
5910 logflags |= XFS_ILOG_DEXT;
5911
5912 /* Add new extent */
5913 current_ext++;
5914 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5915 XFS_IFORK_NEXT_SET(ip, whichfork,
5916 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5917
5918 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005919 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005920 if (error)
5921 goto del_cursor;
5922 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005923 error = xfs_btree_insert(cur, &i);
5924 if (error)
5925 goto del_cursor;
5926 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5927 }
5928
5929 /*
5930 * Convert to a btree if necessary.
5931 */
5932 if (xfs_bmap_needs_btree(ip, whichfork)) {
5933 int tmp_logflags; /* partial log flag return val */
5934
5935 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005936 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005937 &cur, 0, &tmp_logflags, whichfork);
5938 logflags |= tmp_logflags;
5939 }
5940
5941del_cursor:
5942 if (cur) {
5943 cur->bc_private.b.allocated = 0;
5944 xfs_btree_del_cursor(cur,
5945 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5946 }
5947
5948 if (logflags)
5949 xfs_trans_log_inode(tp, ip, logflags);
5950 return error;
5951}
5952
5953int
5954xfs_bmap_split_extent(
5955 struct xfs_inode *ip,
5956 xfs_fileoff_t split_fsb)
5957{
5958 struct xfs_mount *mp = ip->i_mount;
5959 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005960 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005961 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005962 int error;
5963
Christoph Hellwig253f4912016-04-06 09:19:55 +10005964 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
5965 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
5966 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005967 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005968
5969 xfs_ilock(ip, XFS_ILOCK_EXCL);
5970 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
5971
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005972 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005973
5974 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005975 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005976 if (error)
5977 goto out;
5978
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07005979 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005980 if (error)
5981 goto out;
5982
Christoph Hellwig70393312015-06-04 13:48:08 +10005983 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005984
5985out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005986 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10005987 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005988 return error;
5989}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07005990
5991/* Deferred mapping is only for real extents in the data fork. */
5992static bool
5993xfs_bmap_is_update_needed(
5994 struct xfs_bmbt_irec *bmap)
5995{
5996 return bmap->br_startblock != HOLESTARTBLOCK &&
5997 bmap->br_startblock != DELAYSTARTBLOCK;
5998}
5999
6000/* Record a bmap intent. */
6001static int
6002__xfs_bmap_add(
6003 struct xfs_mount *mp,
6004 struct xfs_defer_ops *dfops,
6005 enum xfs_bmap_intent_type type,
6006 struct xfs_inode *ip,
6007 int whichfork,
6008 struct xfs_bmbt_irec *bmap)
6009{
6010 int error;
6011 struct xfs_bmap_intent *bi;
6012
6013 trace_xfs_bmap_defer(mp,
6014 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6015 type,
6016 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6017 ip->i_ino, whichfork,
6018 bmap->br_startoff,
6019 bmap->br_blockcount,
6020 bmap->br_state);
6021
6022 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6023 INIT_LIST_HEAD(&bi->bi_list);
6024 bi->bi_type = type;
6025 bi->bi_owner = ip;
6026 bi->bi_whichfork = whichfork;
6027 bi->bi_bmap = *bmap;
6028
Christoph Hellwig882d8782017-08-28 10:21:03 -07006029 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006030 if (error) {
6031 kmem_free(bi);
6032 return error;
6033 }
6034
6035 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6036 return 0;
6037}
6038
6039/* Map an extent into a file. */
6040int
6041xfs_bmap_map_extent(
6042 struct xfs_mount *mp,
6043 struct xfs_defer_ops *dfops,
6044 struct xfs_inode *ip,
6045 struct xfs_bmbt_irec *PREV)
6046{
6047 if (!xfs_bmap_is_update_needed(PREV))
6048 return 0;
6049
6050 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6051 XFS_DATA_FORK, PREV);
6052}
6053
6054/* Unmap an extent out of a file. */
6055int
6056xfs_bmap_unmap_extent(
6057 struct xfs_mount *mp,
6058 struct xfs_defer_ops *dfops,
6059 struct xfs_inode *ip,
6060 struct xfs_bmbt_irec *PREV)
6061{
6062 if (!xfs_bmap_is_update_needed(PREV))
6063 return 0;
6064
6065 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6066 XFS_DATA_FORK, PREV);
6067}
6068
6069/*
6070 * Process one of the deferred bmap operations. We pass back the
6071 * btree cursor to maintain our lock on the bmapbt between calls.
6072 */
6073int
6074xfs_bmap_finish_one(
6075 struct xfs_trans *tp,
6076 struct xfs_defer_ops *dfops,
6077 struct xfs_inode *ip,
6078 enum xfs_bmap_intent_type type,
6079 int whichfork,
6080 xfs_fileoff_t startoff,
6081 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006082 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006083 xfs_exntst_t state)
6084{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006085 xfs_fsblock_t firstfsb;
6086 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006087
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006088 /*
6089 * firstfsb is tied to the transaction lifetime and is used to
6090 * ensure correct AG locking order and schedule work item
6091 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6092 * to only making one bmap call per transaction, so it should
6093 * be safe to have it as a local variable here.
6094 */
6095 firstfsb = NULLFSBLOCK;
6096
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006097 trace_xfs_bmap_deferred(tp->t_mountp,
6098 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6099 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006100 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006101
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006102 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006103 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006104
6105 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006106 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006107 return -EIO;
6108
6109 switch (type) {
6110 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006111 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006112 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006113 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006114 break;
6115 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006116 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6117 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006118 break;
6119 default:
6120 ASSERT(0);
6121 error = -EFSCORRUPTED;
6122 }
6123
6124 return error;
6125}