blob: 26518aa7b9aec31d6f7f34b54f9f6d8830d80706 [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"
41#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_quota.h"
43#include "xfs_trans_space.h"
44#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110046#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110047#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110048#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100049#include "xfs_rmap.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100050#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070051#include "xfs_refcount.h"
Brian Foster974ae922016-11-28 14:57:42 +110052#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055kmem_zone_t *xfs_bmap_free_item_zone;
56
57/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110058 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110062 * Compute and fill in the value of the maximum depth of a bmap btree
63 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110065void
66xfs_bmap_compute_maxlevels(
67 xfs_mount_t *mp, /* file system mount structure */
68 int whichfork) /* data or attr fork */
69{
70 int level; /* btree level */
71 uint maxblocks; /* max blocks at this level */
72 uint maxleafents; /* max leaf entries possible */
73 int maxrootrecs; /* max records in root block */
74 int minleafrecs; /* min records in leaf block */
75 int minnoderecs; /* min records in node block */
76 int sz; /* root block size */
77
78 /*
79 * The maximum number of extents in a file, hence the maximum
80 * number of leaf entries, is controlled by the type of di_nextents
81 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
82 * (a signed 16-bit number, xfs_aextnum_t).
83 *
84 * Note that we can no longer assume that if we are in ATTR1 that
85 * the fork offset of all the inodes will be
86 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
87 * with ATTR2 and then mounted back with ATTR1, keeping the
88 * di_forkoff's fixed but probably at various positions. Therefore,
89 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
90 * of a minimum size available.
91 */
92 if (whichfork == XFS_DATA_FORK) {
93 maxleafents = MAXEXTNUM;
94 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
95 } else {
96 maxleafents = MAXAEXTNUM;
97 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
98 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100099 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100100 minleafrecs = mp->m_bmap_dmnr[0];
101 minnoderecs = mp->m_bmap_dmnr[1];
102 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
103 for (level = 1; maxblocks > 1; level++) {
104 if (maxblocks <= maxrootrecs)
105 maxblocks = 1;
106 else
107 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
108 }
109 mp->m_bm_maxlevels[whichfork] = level;
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100112STATIC int /* error */
113xfs_bmbt_lookup_eq(
114 struct xfs_btree_cur *cur,
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700115 struct xfs_bmbt_irec *irec,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100116 int *stat) /* success/failure */
117{
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700118 cur->bc_rec.b = *irec;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100119 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
120}
121
122STATIC int /* error */
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700123xfs_bmbt_lookup_first(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100124 struct xfs_btree_cur *cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100125 int *stat) /* success/failure */
126{
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700127 cur->bc_rec.b.br_startoff = 0;
128 cur->bc_rec.b.br_startblock = 0;
129 cur->bc_rec.b.br_blockcount = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100130 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
131}
132
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100133/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000134 * Check if the inode needs to be converted to btree format.
135 */
136static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
137{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700138 return whichfork != XFS_COW_FORK &&
139 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000140 XFS_IFORK_NEXTENTS(ip, whichfork) >
141 XFS_IFORK_MAXEXT(ip, whichfork);
142}
143
144/*
145 * Check if the inode should be converted to extent format.
146 */
147static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
148{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700149 return whichfork != XFS_COW_FORK &&
150 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000151 XFS_IFORK_NEXTENTS(ip, whichfork) <=
152 XFS_IFORK_MAXEXT(ip, whichfork);
153}
154
155/*
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700156 * Update the record referred to by cur to the value given by irec
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100157 * This either works (return 0) or gets an EFSCORRUPTED error.
158 */
159STATIC int
160xfs_bmbt_update(
161 struct xfs_btree_cur *cur,
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700162 struct xfs_bmbt_irec *irec)
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100163{
164 union xfs_btree_rec rec;
165
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700166 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100167 return xfs_btree_update(cur, &rec);
168}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100171 * Compute the worst-case number of indirect blocks that will be used
172 * for ip's delayed extent of length "len".
173 */
174STATIC xfs_filblks_t
175xfs_bmap_worst_indlen(
176 xfs_inode_t *ip, /* incore inode pointer */
177 xfs_filblks_t len) /* delayed extent length */
178{
179 int level; /* btree level number */
180 int maxrecs; /* maximum record count at this level */
181 xfs_mount_t *mp; /* mount structure */
182 xfs_filblks_t rval; /* return value */
183
184 mp = ip->i_mount;
185 maxrecs = mp->m_bmap_dmxr[0];
186 for (level = 0, rval = 0;
187 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
188 level++) {
189 len += maxrecs - 1;
190 do_div(len, maxrecs);
191 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700192 if (len == 1)
193 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100194 level - 1;
195 if (level == 0)
196 maxrecs = mp->m_bmap_dmxr[1];
197 }
198 return rval;
199}
200
201/*
202 * Calculate the default attribute fork offset for newly created inodes.
203 */
204uint
205xfs_default_attroffset(
206 struct xfs_inode *ip)
207{
208 struct xfs_mount *mp = ip->i_mount;
209 uint offset;
210
211 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100212 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100213 XFS_BMDR_SPACE_CALC(MINABTPTRS);
214 } else {
215 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
216 }
217
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100218 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100219 return offset;
220}
221
222/*
223 * Helper routine to reset inode di_forkoff field when switching
224 * attribute fork from local to extent format - we reset it where
225 * possible to make space available for inline data fork extents.
226 */
227STATIC void
228xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100229 xfs_inode_t *ip,
230 int whichfork)
231{
232 if (whichfork == XFS_ATTR_FORK &&
233 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
Dave Chinner9e5987a2013-02-25 12:31:26 +1100234 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
235 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
236
237 if (dfl_forkoff > ip->i_d.di_forkoff)
238 ip->i_d.di_forkoff = dfl_forkoff;
239 }
240}
241
Dave Chinner9e5987a2013-02-25 12:31:26 +1100242#ifdef DEBUG
243STATIC struct xfs_buf *
244xfs_bmap_get_bp(
245 struct xfs_btree_cur *cur,
246 xfs_fsblock_t bno)
247{
248 struct xfs_log_item_desc *lidp;
249 int i;
250
251 if (!cur)
252 return NULL;
253
254 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
255 if (!cur->bc_bufs[i])
256 break;
257 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
258 return cur->bc_bufs[i];
259 }
260
261 /* Chase down all the log items to see if the bp is there */
262 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
263 struct xfs_buf_log_item *bip;
264 bip = (struct xfs_buf_log_item *)lidp->lid_item;
265 if (bip->bli_item.li_type == XFS_LI_BUF &&
266 XFS_BUF_ADDR(bip->bli_buf) == bno)
267 return bip->bli_buf;
268 }
269
270 return NULL;
271}
272
273STATIC void
274xfs_check_block(
275 struct xfs_btree_block *block,
276 xfs_mount_t *mp,
277 int root,
278 short sz)
279{
280 int i, j, dmxr;
281 __be64 *pp, *thispa; /* pointer to block address */
282 xfs_bmbt_key_t *prevp, *keyp;
283
284 ASSERT(be16_to_cpu(block->bb_level) > 0);
285
286 prevp = NULL;
287 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
288 dmxr = mp->m_bmap_dmxr[0];
289 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
290
291 if (prevp) {
292 ASSERT(be64_to_cpu(prevp->br_startoff) <
293 be64_to_cpu(keyp->br_startoff));
294 }
295 prevp = keyp;
296
297 /*
298 * Compare the block numbers to see if there are dups.
299 */
300 if (root)
301 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
302 else
303 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
304
305 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
306 if (root)
307 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
308 else
309 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
310 if (*thispa == *pp) {
311 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
312 __func__, j, i,
313 (unsigned long long)be64_to_cpu(*thispa));
314 panic("%s: ptrs are equal in node\n",
315 __func__);
316 }
317 }
318 }
319}
320
321/*
322 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100323 * btree leaves. THis becomes prohibitively expensive for large extent count
324 * files, so don't bother with inodes that have more than 10,000 extents in
325 * them. The btree record ordering checks will still be done, so for such large
326 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100327 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100328STATIC void
329xfs_bmap_check_leaf_extents(
330 xfs_btree_cur_t *cur, /* btree cursor or null */
331 xfs_inode_t *ip, /* incore inode pointer */
332 int whichfork) /* data or attr fork */
333{
334 struct xfs_btree_block *block; /* current btree block */
335 xfs_fsblock_t bno; /* block # of "block" */
336 xfs_buf_t *bp; /* buffer for "block" */
337 int error; /* error return value */
338 xfs_extnum_t i=0, j; /* index into the extents list */
339 xfs_ifork_t *ifp; /* fork structure */
340 int level; /* btree level, for checking */
341 xfs_mount_t *mp; /* file system mount structure */
342 __be64 *pp; /* pointer to block address */
343 xfs_bmbt_rec_t *ep; /* pointer to current extent */
344 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
345 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
346 int bp_release = 0;
347
348 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
349 return;
350 }
351
Dave Chinnere3543812016-01-08 11:28:49 +1100352 /* skip large extent count inodes */
353 if (ip->i_d.di_nextents > 10000)
354 return;
355
Dave Chinner9e5987a2013-02-25 12:31:26 +1100356 bno = NULLFSBLOCK;
357 mp = ip->i_mount;
358 ifp = XFS_IFORK_PTR(ip, whichfork);
359 block = ifp->if_broot;
360 /*
361 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
362 */
363 level = be16_to_cpu(block->bb_level);
364 ASSERT(level > 0);
365 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
366 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
367 bno = be64_to_cpu(*pp);
368
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000369 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100370 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
371 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
372
373 /*
374 * Go down the tree until leaf level is reached, following the first
375 * pointer (leftmost) at each level.
376 */
377 while (level-- > 0) {
378 /* See if buf is in cur first */
379 bp_release = 0;
380 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
381 if (!bp) {
382 bp_release = 1;
383 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
384 XFS_BMAP_BTREE_REF,
385 &xfs_bmbt_buf_ops);
386 if (error)
387 goto error_norelse;
388 }
389 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100390 if (level == 0)
391 break;
392
393 /*
394 * Check this block for basic sanity (increasing keys and
395 * no duplicate blocks).
396 */
397
398 xfs_check_block(block, mp, 0, 0);
399 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
400 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100401 XFS_WANT_CORRUPTED_GOTO(mp,
402 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100403 if (bp_release) {
404 bp_release = 0;
405 xfs_trans_brelse(NULL, bp);
406 }
407 }
408
409 /*
410 * Here with bp and block set to the leftmost leaf node in the tree.
411 */
412 i = 0;
413
414 /*
415 * Loop over all leaf nodes checking that all extents are in the right order.
416 */
417 for (;;) {
418 xfs_fsblock_t nextbno;
419 xfs_extnum_t num_recs;
420
421
422 num_recs = xfs_btree_get_numrecs(block);
423
424 /*
425 * Read-ahead the next leaf block, if any.
426 */
427
428 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
429
430 /*
431 * Check all the extents to make sure they are OK.
432 * If we had a previous block, the last entry should
433 * conform with the first entry in this one.
434 */
435
436 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
437 if (i) {
438 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
439 xfs_bmbt_disk_get_blockcount(&last) <=
440 xfs_bmbt_disk_get_startoff(ep));
441 }
442 for (j = 1; j < num_recs; j++) {
443 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
444 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
445 xfs_bmbt_disk_get_blockcount(ep) <=
446 xfs_bmbt_disk_get_startoff(nextp));
447 ep = nextp;
448 }
449
450 last = *ep;
451 i += num_recs;
452 if (bp_release) {
453 bp_release = 0;
454 xfs_trans_brelse(NULL, bp);
455 }
456 bno = nextbno;
457 /*
458 * If we've reached the end, stop.
459 */
460 if (bno == NULLFSBLOCK)
461 break;
462
463 bp_release = 0;
464 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
465 if (!bp) {
466 bp_release = 1;
467 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
468 XFS_BMAP_BTREE_REF,
469 &xfs_bmbt_buf_ops);
470 if (error)
471 goto error_norelse;
472 }
473 block = XFS_BUF_TO_BLOCK(bp);
474 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100475
Dave Chinner9e5987a2013-02-25 12:31:26 +1100476 return;
477
478error0:
479 xfs_warn(mp, "%s: at error0", __func__);
480 if (bp_release)
481 xfs_trans_brelse(NULL, bp);
482error_norelse:
483 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
484 __func__, i);
485 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
486 return;
487}
488
489/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100490 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000491 * given the caller's original parameters. Specifically check the
492 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100493 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
494 */
495STATIC void
496xfs_bmap_validate_ret(
497 xfs_fileoff_t bno,
498 xfs_filblks_t len,
499 int flags,
500 xfs_bmbt_irec_t *mval,
501 int nmap,
502 int ret_nmap)
503{
504 int i; /* index to map values */
505
506 ASSERT(ret_nmap <= nmap);
507
508 for (i = 0; i < ret_nmap; i++) {
509 ASSERT(mval[i].br_blockcount > 0);
510 if (!(flags & XFS_BMAPI_ENTIRE)) {
511 ASSERT(mval[i].br_startoff >= bno);
512 ASSERT(mval[i].br_blockcount <= len);
513 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
514 bno + len);
515 } else {
516 ASSERT(mval[i].br_startoff < bno + len);
517 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
518 bno);
519 }
520 ASSERT(i == 0 ||
521 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
522 mval[i].br_startoff);
523 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
524 mval[i].br_startblock != HOLESTARTBLOCK);
525 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
526 mval[i].br_state == XFS_EXT_UNWRITTEN);
527 }
528}
529
530#else
531#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700532#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100533#endif /* DEBUG */
534
535/*
536 * bmap free list manipulation functions
537 */
538
539/*
540 * Add the extent to the list of extents to be free at transaction end.
541 * The list is maintained sorted (by block number).
542 */
543void
544xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000545 struct xfs_mount *mp,
546 struct xfs_defer_ops *dfops,
547 xfs_fsblock_t bno,
548 xfs_filblks_t len,
549 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100550{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000551 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100552#ifdef DEBUG
553 xfs_agnumber_t agno;
554 xfs_agblock_t agbno;
555
556 ASSERT(bno != NULLFSBLOCK);
557 ASSERT(len > 0);
558 ASSERT(len <= MAXEXTLEN);
559 ASSERT(!isnullstartblock(bno));
560 agno = XFS_FSB_TO_AGNO(mp, bno);
561 agbno = XFS_FSB_TO_AGBNO(mp, bno);
562 ASSERT(agno < mp->m_sb.sb_agcount);
563 ASSERT(agbno < mp->m_sb.sb_agblocks);
564 ASSERT(len < mp->m_sb.sb_agblocks);
565 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
566#endif
567 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000568
Dave Chinner9e5987a2013-02-25 12:31:26 +1100569 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000570 new->xefi_startblock = bno;
571 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000572 if (oinfo)
573 new->xefi_oinfo = *oinfo;
574 else
575 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000576 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
577 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000578 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100579}
580
581/*
582 * Inode fork format manipulation functions
583 */
584
585/*
586 * Transform a btree format file with only one leaf node, where the
587 * extents list will fit in the inode, into an extents format file.
588 * Since the file extents are already in-core, all we have to do is
589 * give up the space for the btree root and pitch the leaf block.
590 */
591STATIC int /* error */
592xfs_bmap_btree_to_extents(
593 xfs_trans_t *tp, /* transaction pointer */
594 xfs_inode_t *ip, /* incore inode pointer */
595 xfs_btree_cur_t *cur, /* btree cursor */
596 int *logflagsp, /* inode logging flags */
597 int whichfork) /* data or attr fork */
598{
599 /* REFERENCED */
600 struct xfs_btree_block *cblock;/* child btree block */
601 xfs_fsblock_t cbno; /* child block number */
602 xfs_buf_t *cbp; /* child block's buffer */
603 int error; /* error return value */
604 xfs_ifork_t *ifp; /* inode fork data */
605 xfs_mount_t *mp; /* mount point structure */
606 __be64 *pp; /* ptr to block address */
607 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000608 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100609
610 mp = ip->i_mount;
611 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700612 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100613 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
614 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
615 rblock = ifp->if_broot;
616 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
617 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
618 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
619 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
620 cbno = be64_to_cpu(*pp);
621 *logflagsp = 0;
622#ifdef DEBUG
Darrick J. Wongf1357612017-10-17 21:37:33 -0700623 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
624 xfs_btree_check_lptr(cur, cbno, 1));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100625#endif
626 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
627 &xfs_bmbt_buf_ops);
628 if (error)
629 return error;
630 cblock = XFS_BUF_TO_BLOCK(cbp);
631 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
632 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000633 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
634 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100635 ip->i_d.di_nblocks--;
636 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
637 xfs_trans_binval(tp, cbp);
638 if (cur->bc_bufs[0] == cbp)
639 cur->bc_bufs[0] = NULL;
640 xfs_iroot_realloc(ip, -1, whichfork);
641 ASSERT(ifp->if_broot == NULL);
642 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
643 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
644 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
645 return 0;
646}
647
648/*
649 * Convert an extents-format file into a btree-format file.
650 * The new file will have a root block (in the inode) and a single child block.
651 */
652STATIC int /* error */
653xfs_bmap_extents_to_btree(
654 xfs_trans_t *tp, /* transaction pointer */
655 xfs_inode_t *ip, /* incore inode pointer */
656 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000657 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100658 xfs_btree_cur_t **curp, /* cursor returned to caller */
659 int wasdel, /* converting a delayed alloc */
660 int *logflagsp, /* inode logging flags */
661 int whichfork) /* data or attr fork */
662{
663 struct xfs_btree_block *ablock; /* allocated (child) bt block */
664 xfs_buf_t *abp; /* buffer for ablock */
665 xfs_alloc_arg_t args; /* allocation arguments */
666 xfs_bmbt_rec_t *arp; /* child record pointer */
667 struct xfs_btree_block *block; /* btree root block */
668 xfs_btree_cur_t *cur; /* bmap btree cursor */
669 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
670 int error; /* error return value */
671 xfs_extnum_t i, cnt; /* extent record index */
672 xfs_ifork_t *ifp; /* inode fork pointer */
673 xfs_bmbt_key_t *kp; /* root block key pointer */
674 xfs_mount_t *mp; /* mount structure */
675 xfs_extnum_t nextents; /* number of file extents */
676 xfs_bmbt_ptr_t *pp; /* root block address pointer */
677
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500678 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700679 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100680 ifp = XFS_IFORK_PTR(ip, whichfork);
681 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
682
683 /*
684 * Make space in the inode incore.
685 */
686 xfs_iroot_realloc(ip, 1, whichfork);
687 ifp->if_flags |= XFS_IFBROOT;
688
689 /*
690 * Fill in the root.
691 */
692 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800693 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
694 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800695 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100696 /*
697 * Need a cursor. Can't allocate until bb_level is filled in.
698 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100699 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
700 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000701 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100702 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
703 /*
704 * Convert to a btree with two levels, one record in root.
705 */
706 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
707 memset(&args, 0, sizeof(args));
708 args.tp = tp;
709 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000710 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100711 args.firstblock = *firstblock;
712 if (*firstblock == NULLFSBLOCK) {
713 args.type = XFS_ALLOCTYPE_START_BNO;
714 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000715 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100716 args.type = XFS_ALLOCTYPE_START_BNO;
717 args.fsbno = *firstblock;
718 } else {
719 args.type = XFS_ALLOCTYPE_NEAR_BNO;
720 args.fsbno = *firstblock;
721 }
722 args.minlen = args.maxlen = args.prod = 1;
723 args.wasdel = wasdel;
724 *logflagsp = 0;
725 if ((error = xfs_alloc_vextent(&args))) {
726 xfs_iroot_realloc(ip, -1, whichfork);
727 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
728 return error;
729 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700730
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800731 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
732 xfs_iroot_realloc(ip, -1, whichfork);
733 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
734 return -ENOSPC;
735 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100736 /*
737 * Allocation can't fail, the space was reserved.
738 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100739 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800740 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100741 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
742 cur->bc_private.b.allocated++;
743 ip->i_d.di_nblocks++;
744 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
745 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
746 /*
747 * Fill in the child block.
748 */
749 abp->b_ops = &xfs_bmbt_buf_ops;
750 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800751 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
752 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500753 XFS_BTREE_LONG_PTRS);
754
Dave Chinner9e5987a2013-02-25 12:31:26 +1100755 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100756 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100757 for (cnt = i = 0; i < nextents; i++) {
758 ep = xfs_iext_get_ext(ifp, i);
759 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
760 arp->l0 = cpu_to_be64(ep->l0);
761 arp->l1 = cpu_to_be64(ep->l1);
762 arp++; cnt++;
763 }
764 }
765 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
766 xfs_btree_set_numrecs(ablock, cnt);
767
768 /*
769 * Fill in the root key and pointer.
770 */
771 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
772 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
773 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
774 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
775 be16_to_cpu(block->bb_level)));
776 *pp = cpu_to_be64(args.fsbno);
777
778 /*
779 * Do all this logging at the end so that
780 * the root is at the right level.
781 */
782 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
783 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
784 ASSERT(*curp == NULL);
785 *curp = cur;
786 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
787 return 0;
788}
789
790/*
791 * Convert a local file to an extents file.
792 * This code is out of bounds for data forks of regular files,
793 * since the file data needs to get logged so things will stay consistent.
794 * (The bmap-level manipulations are ok, though).
795 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000796void
797xfs_bmap_local_to_extents_empty(
798 struct xfs_inode *ip,
799 int whichfork)
800{
801 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
802
Darrick J. Wong60b49842016-10-03 09:11:34 -0700803 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000804 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
805 ASSERT(ifp->if_bytes == 0);
806 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
807
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000808 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000809 ifp->if_flags &= ~XFS_IFINLINE;
810 ifp->if_flags |= XFS_IFEXTENTS;
811 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
812}
813
814
Dave Chinner9e5987a2013-02-25 12:31:26 +1100815STATIC int /* error */
816xfs_bmap_local_to_extents(
817 xfs_trans_t *tp, /* transaction pointer */
818 xfs_inode_t *ip, /* incore inode pointer */
819 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
820 xfs_extlen_t total, /* total blocks needed by transaction */
821 int *logflagsp, /* inode logging flags */
822 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500823 void (*init_fn)(struct xfs_trans *tp,
824 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100825 struct xfs_inode *ip,
826 struct xfs_ifork *ifp))
827{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000828 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100829 int flags; /* logging flags returned */
830 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000831 xfs_alloc_arg_t args; /* allocation arguments */
832 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700833 struct xfs_bmbt_irec rec;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100834
835 /*
836 * We don't want to deal with the case of keeping inode data inline yet.
837 * So sending the data fork of a regular inode is invalid.
838 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100839 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100840 ifp = XFS_IFORK_PTR(ip, whichfork);
841 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000842
843 if (!ifp->if_bytes) {
844 xfs_bmap_local_to_extents_empty(ip, whichfork);
845 flags = XFS_ILOG_CORE;
846 goto done;
847 }
848
Dave Chinner9e5987a2013-02-25 12:31:26 +1100849 flags = 0;
850 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000851 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
852 XFS_IFINLINE);
853 memset(&args, 0, sizeof(args));
854 args.tp = tp;
855 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000856 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000857 args.firstblock = *firstblock;
858 /*
859 * Allocate a block. We know we need only one, since the
860 * file currently fits in an inode.
861 */
862 if (*firstblock == NULLFSBLOCK) {
863 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
864 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100865 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000866 args.fsbno = *firstblock;
867 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100868 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000869 args.total = total;
870 args.minlen = args.maxlen = args.prod = 1;
871 error = xfs_alloc_vextent(&args);
872 if (error)
873 goto done;
874
875 /* Can't fail, the space was reserved. */
876 ASSERT(args.fsbno != NULLFSBLOCK);
877 ASSERT(args.len == 1);
878 *firstblock = args.fsbno;
879 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
880
Dave Chinnerfe22d552015-01-22 09:30:06 +1100881 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100882 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100883 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100884 * The callout is responsible for logging because the remote format
885 * might differ from the local format and thus we don't know how much to
886 * log here. Note that init_fn must also set the buffer log item type
887 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100888 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000889 init_fn(tp, bp, ip, ifp);
890
Brian Fosterb7cdc662015-10-12 15:40:24 +1100891 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000892 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
893 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100894 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000895
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700896 rec.br_startoff = 0;
897 rec.br_startblock = args.fsbno;
898 rec.br_blockcount = 1;
899 rec.br_state = XFS_EXT_NORM;
900 xfs_iext_insert(ip, 0, 1, &rec, 0);
901
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000902 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
903 ip->i_d.di_nblocks = 1;
904 xfs_trans_mod_dquot_byino(tp, ip,
905 XFS_TRANS_DQ_BCOUNT, 1L);
906 flags |= xfs_ilog_fext(whichfork);
907
Dave Chinner9e5987a2013-02-25 12:31:26 +1100908done:
909 *logflagsp = flags;
910 return error;
911}
912
913/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 * Called from xfs_bmap_add_attrfork to handle btree format files.
915 */
916STATIC int /* error */
917xfs_bmap_add_attrfork_btree(
918 xfs_trans_t *tp, /* transaction pointer */
919 xfs_inode_t *ip, /* incore inode pointer */
920 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000921 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 int *flags) /* inode logging flags */
923{
924 xfs_btree_cur_t *cur; /* btree cursor */
925 int error; /* error return value */
926 xfs_mount_t *mp; /* file system mount struct */
927 int stat; /* newroot status */
928
929 mp = ip->i_mount;
930 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
931 *flags |= XFS_ILOG_DBROOT;
932 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100933 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000934 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 cur->bc_private.b.firstblock = *firstblock;
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700936 error = xfs_bmbt_lookup_first(cur, &stat);
937 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000939 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100940 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100941 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 goto error0;
943 if (stat == 0) {
944 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000945 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 *firstblock = cur->bc_private.b.firstblock;
948 cur->bc_private.b.allocated = 0;
949 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
950 }
951 return 0;
952error0:
953 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
954 return error;
955}
956
957/*
958 * Called from xfs_bmap_add_attrfork to handle extents format files.
959 */
960STATIC int /* error */
961xfs_bmap_add_attrfork_extents(
962 xfs_trans_t *tp, /* transaction pointer */
963 xfs_inode_t *ip, /* incore inode pointer */
964 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000965 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int *flags) /* inode logging flags */
967{
968 xfs_btree_cur_t *cur; /* bmap btree cursor */
969 int error; /* error return value */
970
971 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
972 return 0;
973 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000974 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 flags, XFS_DATA_FORK);
976 if (cur) {
977 cur->bc_private.b.allocated = 0;
978 xfs_btree_del_cursor(cur,
979 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
980 }
981 return error;
982}
983
984/*
Dave Chinner1e823792013-02-11 15:58:13 +1100985 * Called from xfs_bmap_add_attrfork to handle local format files. Each
986 * different data fork content type needs a different callout to do the
987 * conversion. Some are basic and only require special block initialisation
988 * callouts for the data formating, others (directories) are so specialised they
989 * handle everything themselves.
990 *
991 * XXX (dgc): investigate whether directory conversion can use the generic
992 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500993 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 */
995STATIC int /* error */
996xfs_bmap_add_attrfork_local(
997 xfs_trans_t *tp, /* transaction pointer */
998 xfs_inode_t *ip, /* incore inode pointer */
999 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001000 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 int *flags) /* inode logging flags */
1002{
1003 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1006 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001007
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001008 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001010 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 dargs.dp = ip;
1012 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001013 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001014 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 dargs.whichfork = XFS_DATA_FORK;
1016 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001017 return xfs_dir2_sf_to_block(&dargs);
1018 }
1019
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001020 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001021 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1022 flags, XFS_DATA_FORK,
1023 xfs_symlink_local_to_remote);
1024
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001025 /* should only be called for types that support local format data */
1026 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001027 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
1030/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001031 * Convert inode from non-attributed to attributed.
1032 * Must not be in a transaction, ip must not be locked.
1033 */
1034int /* error code */
1035xfs_bmap_add_attrfork(
1036 xfs_inode_t *ip, /* incore inode pointer */
1037 int size, /* space new attribute needs */
1038 int rsvd) /* xact may use reserved blks */
1039{
1040 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001041 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001042 xfs_mount_t *mp; /* mount structure */
1043 xfs_trans_t *tp; /* transaction pointer */
1044 int blks; /* space reservation */
1045 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001046 int logflags; /* logging flags */
1047 int error; /* error return value */
1048
1049 ASSERT(XFS_IFORK_Q(ip) == 0);
1050
1051 mp = ip->i_mount;
1052 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001053
Dave Chinner9e5987a2013-02-25 12:31:26 +11001054 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001055
1056 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1057 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1058 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001059 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001060
Dave Chinner9e5987a2013-02-25 12:31:26 +11001061 xfs_ilock(ip, XFS_ILOCK_EXCL);
1062 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1063 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1064 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001065 if (error)
1066 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001067 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001068 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001069 if (ip->i_d.di_anextents != 0) {
1070 error = -EFSCORRUPTED;
1071 goto trans_cancel;
1072 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001073 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1074 /*
1075 * For inodes coming from pre-6.2 filesystems.
1076 */
1077 ASSERT(ip->i_d.di_aformat == 0);
1078 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1079 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001080
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001081 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001082 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1083
1084 switch (ip->i_d.di_format) {
1085 case XFS_DINODE_FMT_DEV:
1086 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1087 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001088 case XFS_DINODE_FMT_LOCAL:
1089 case XFS_DINODE_FMT_EXTENTS:
1090 case XFS_DINODE_FMT_BTREE:
1091 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1092 if (!ip->i_d.di_forkoff)
1093 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1094 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1095 version = 2;
1096 break;
1097 default:
1098 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001099 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001100 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001101 }
1102
1103 ASSERT(ip->i_afp == NULL);
1104 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1105 ip->i_afp->if_flags = XFS_IFEXTENTS;
1106 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001107 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001108 switch (ip->i_d.di_format) {
1109 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001110 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001111 &logflags);
1112 break;
1113 case XFS_DINODE_FMT_EXTENTS:
1114 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001115 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001116 break;
1117 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001118 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001119 &logflags);
1120 break;
1121 default:
1122 error = 0;
1123 break;
1124 }
1125 if (logflags)
1126 xfs_trans_log_inode(tp, ip, logflags);
1127 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001128 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001129 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1130 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001131 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001132
1133 spin_lock(&mp->m_sb_lock);
1134 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1135 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001136 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001137 }
1138 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1139 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001140 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001141 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001142 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001143 if (log_sb)
1144 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001145 }
1146
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001147 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001148 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001149 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001150 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001151 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001152 return error;
1153
1154bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001155 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001156trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001157 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001158 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001159 return error;
1160}
1161
1162/*
1163 * Internal and external extent tree search functions.
1164 */
1165
1166/*
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001167 * Read in extents from a btree-format inode.
Dave Chinner9e5987a2013-02-25 12:31:26 +11001168 */
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001169int
1170xfs_iread_extents(
1171 struct xfs_trans *tp,
1172 struct xfs_inode *ip,
1173 int whichfork)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001174{
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001175 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001176 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001177 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1178 xfs_extnum_t nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
1179 struct xfs_btree_block *block = ifp->if_broot;
1180 xfs_fsblock_t bno;
1181 struct xfs_buf *bp;
1182 xfs_extnum_t i, j;
1183 int level;
1184 __be64 *pp;
1185 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001186
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001187 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
1188
1189 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
1190 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
1191 return -EFSCORRUPTED;
1192 }
1193
1194 ifp->if_bytes = 0;
1195 ifp->if_real_bytes = 0;
1196 xfs_iext_add(ifp, 0, nextents);
1197
Dave Chinner9e5987a2013-02-25 12:31:26 +11001198 /*
1199 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1200 */
1201 level = be16_to_cpu(block->bb_level);
1202 ASSERT(level > 0);
1203 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1204 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001205
Dave Chinner9e5987a2013-02-25 12:31:26 +11001206 /*
1207 * Go down the tree until leaf level is reached, following the first
1208 * pointer (leftmost) at each level.
1209 */
1210 while (level-- > 0) {
1211 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1212 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1213 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001214 goto out;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001215 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001216 if (level == 0)
1217 break;
1218 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1219 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001220 XFS_WANT_CORRUPTED_GOTO(mp,
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001221 XFS_FSB_SANITY_CHECK(mp, bno), out_brelse);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001222 xfs_trans_brelse(tp, bp);
1223 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001224
Dave Chinner9e5987a2013-02-25 12:31:26 +11001225 /*
1226 * Here with bp and block set to the leftmost leaf node in the tree.
1227 */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001228 i = 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001229
Dave Chinner9e5987a2013-02-25 12:31:26 +11001230 /*
1231 * Loop over all leaf nodes. Copy information to the extent records.
1232 */
1233 for (;;) {
1234 xfs_bmbt_rec_t *frp;
1235 xfs_fsblock_t nextbno;
1236 xfs_extnum_t num_recs;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001237
1238 num_recs = xfs_btree_get_numrecs(block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001239 if (unlikely(i + num_recs > nextents)) {
1240 ASSERT(i + num_recs <= nextents);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001241 xfs_warn(ip->i_mount,
1242 "corrupt dinode %Lu, (btree extents).",
1243 (unsigned long long) ip->i_ino);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001244 XFS_CORRUPTION_ERROR(__func__,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001245 XFS_ERRLEVEL_LOW, ip->i_mount, block);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001246 error = -EFSCORRUPTED;
1247 goto out_brelse;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001248 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001249 /*
1250 * Read-ahead the next leaf block, if any.
1251 */
1252 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1253 if (nextbno != NULLFSBLOCK)
1254 xfs_btree_reada_bufl(mp, nextbno, 1,
1255 &xfs_bmbt_buf_ops);
1256 /*
1257 * Copy records into the extent records.
1258 */
1259 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001260 for (j = 0; j < num_recs; j++, i++, frp++) {
1261 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1262 trp->l0 = be64_to_cpu(frp->l0);
1263 trp->l1 = be64_to_cpu(frp->l1);
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001264 if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001265 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001266 XFS_ERRLEVEL_LOW, mp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001267 error = -EFSCORRUPTED;
1268 goto out_brelse;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001269 }
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001270 trace_xfs_read_extent(ip, i, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001271 }
1272 xfs_trans_brelse(tp, bp);
1273 bno = nextbno;
1274 /*
1275 * If we've reached the end, stop.
1276 */
1277 if (bno == NULLFSBLOCK)
1278 break;
1279 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1280 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1281 if (error)
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001282 goto out;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001283 block = XFS_BUF_TO_BLOCK(bp);
1284 }
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001285
1286 if (i != XFS_IFORK_NEXTENTS(ip, whichfork)) {
1287 error = -EFSCORRUPTED;
1288 goto out;
1289 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001290 ASSERT(i == xfs_iext_count(ifp));
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001291
1292 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001293 return 0;
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001294
1295out_brelse:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001296 xfs_trans_brelse(tp, bp);
Christoph Hellwig211e95b2017-10-23 16:32:39 -07001297out:
1298 xfs_iext_destroy(ifp);
1299 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001300}
1301
Dave Chinner9e5987a2013-02-25 12:31:26 +11001302/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001303 * Returns the relative block number of the first unused block(s) in the given
1304 * fork with at least "len" logically contiguous blocks free. This is the
1305 * lowest-address hole if the fork has holes, else the first block past the end
1306 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a2013-02-25 12:31:26 +11001307 */
1308int /* error */
1309xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001310 struct xfs_trans *tp, /* transaction pointer */
1311 struct xfs_inode *ip, /* incore inode */
1312 xfs_extlen_t len, /* size of hole to find */
1313 xfs_fileoff_t *first_unused, /* unused block */
1314 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001315{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001316 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1317 struct xfs_bmbt_irec got;
1318 xfs_extnum_t idx = 0;
1319 xfs_fileoff_t lastaddr = 0;
1320 xfs_fileoff_t lowest, max;
1321 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001322
1323 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1324 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1325 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001326
Dave Chinner9e5987a2013-02-25 12:31:26 +11001327 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1328 *first_unused = 0;
1329 return 0;
1330 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001331
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001332 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1333 error = xfs_iread_extents(tp, ip, whichfork);
1334 if (error)
1335 return error;
1336 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001337
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001338 lowest = max = *first_unused;
1339 while (xfs_iext_get_extent(ifp, idx++, &got)) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001340 /*
1341 * See if the hole before this extent will work.
1342 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001343 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001344 got.br_startoff - max >= len)
1345 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001346 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001347 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1348 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001349
Dave Chinner9e5987a2013-02-25 12:31:26 +11001350 *first_unused = max;
1351 return 0;
1352}
1353
1354/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001355 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001356 * last_block (input value) in the file.
1357 * This is not based on i_size, it is based on the extent records.
1358 * Returns 0 for local files, as they do not have extent records.
1359 */
1360int /* error */
1361xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001362 struct xfs_trans *tp, /* transaction pointer */
1363 struct xfs_inode *ip, /* incore inode */
1364 xfs_fileoff_t *last_block, /* last block */
1365 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001366{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001367 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1368 struct xfs_bmbt_irec got;
1369 xfs_extnum_t idx;
1370 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001371
Christoph Hellwig86685f72016-11-24 11:39:38 +11001372 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1373 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001374 *last_block = 0;
1375 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001376 case XFS_DINODE_FMT_BTREE:
1377 case XFS_DINODE_FMT_EXTENTS:
1378 break;
1379 default:
1380 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001381 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001382
1383 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1384 error = xfs_iread_extents(tp, ip, whichfork);
1385 if (error)
1386 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001387 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001388
1389 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1390 if (got.br_startoff <= *last_block - 1)
1391 return 0;
1392 }
1393
1394 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1395 *last_block = got.br_startoff + got.br_blockcount;
1396 return 0;
1397 }
1398
1399 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001400 return 0;
1401}
1402
Dave Chinner68988112013-08-12 20:49:42 +10001403int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001404xfs_bmap_last_extent(
1405 struct xfs_trans *tp,
1406 struct xfs_inode *ip,
1407 int whichfork,
1408 struct xfs_bmbt_irec *rec,
1409 int *is_empty)
1410{
1411 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1412 int error;
1413 int nextents;
1414
1415 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1416 error = xfs_iread_extents(tp, ip, whichfork);
1417 if (error)
1418 return error;
1419 }
1420
Eric Sandeen5d829302016-11-08 12:59:42 +11001421 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001422 if (nextents == 0) {
1423 *is_empty = 1;
1424 return 0;
1425 }
1426
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001427 xfs_iext_get_extent(ifp, nextents - 1, rec);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001428 *is_empty = 0;
1429 return 0;
1430}
1431
1432/*
1433 * Check the last inode extent to determine whether this allocation will result
1434 * in blocks being allocated at the end of the file. When we allocate new data
1435 * blocks at the end of the file which do not start at the previous data block,
1436 * we will try to align the new blocks at stripe unit boundaries.
1437 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001438 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001439 * at, or past the EOF.
1440 */
1441STATIC int
1442xfs_bmap_isaeof(
1443 struct xfs_bmalloca *bma,
1444 int whichfork)
1445{
1446 struct xfs_bmbt_irec rec;
1447 int is_empty;
1448 int error;
1449
Thomas Meyer749f24f2017-10-09 11:38:54 -07001450 bma->aeof = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001451 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1452 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001453 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001454 return error;
1455
Dave Chinner6e708bc2013-11-22 10:41:16 +11001456 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001457 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001458 return 0;
1459 }
1460
Dave Chinner9e5987a2013-02-25 12:31:26 +11001461 /*
1462 * Check if we are allocation or past the last extent, or at least into
1463 * the last delayed allocated extent.
1464 */
1465 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1466 (bma->offset >= rec.br_startoff &&
1467 isnullstartblock(rec.br_startblock));
1468 return 0;
1469}
1470
1471/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001472 * Returns the file-relative block number of the first block past eof in
1473 * the file. This is not based on i_size, it is based on the extent records.
1474 * Returns 0 for local files, as they do not have extent records.
1475 */
1476int
1477xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001478 struct xfs_inode *ip,
1479 xfs_fileoff_t *last_block,
1480 int whichfork)
1481{
1482 struct xfs_bmbt_irec rec;
1483 int is_empty;
1484 int error;
1485
1486 *last_block = 0;
1487
1488 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1489 return 0;
1490
1491 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1492 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001493 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001494
1495 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1496 if (error || is_empty)
1497 return error;
1498
1499 *last_block = rec.br_startoff + rec.br_blockcount;
1500 return 0;
1501}
1502
1503/*
1504 * Returns whether the selected fork of the inode has exactly one
1505 * block or not. For the data fork we check this matches di_size,
1506 * implying the file's range is 0..bsize-1.
1507 */
1508int /* 1=>1 block, 0=>otherwise */
1509xfs_bmap_one_block(
1510 xfs_inode_t *ip, /* incore inode */
1511 int whichfork) /* data or attr fork */
1512{
Dave Chinner9e5987a2013-02-25 12:31:26 +11001513 xfs_ifork_t *ifp; /* inode fork pointer */
1514 int rval; /* return value */
1515 xfs_bmbt_irec_t s; /* internal version of extent */
1516
1517#ifndef DEBUG
1518 if (whichfork == XFS_DATA_FORK)
1519 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1520#endif /* !DEBUG */
1521 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1522 return 0;
1523 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1524 return 0;
1525 ifp = XFS_IFORK_PTR(ip, whichfork);
1526 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001527 xfs_iext_get_extent(ifp, 0, &s);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001528 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1529 if (rval && whichfork == XFS_DATA_FORK)
1530 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1531 return rval;
1532}
1533
1534/*
1535 * Extent tree manipulation functions used during allocation.
1536 */
1537
1538/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001539 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 */
1541STATIC int /* error */
1542xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001543 struct xfs_bmalloca *bma,
1544 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001546 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001549 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 xfs_fileoff_t new_endoff; /* end offset of new entry */
1551 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1552 /* left is 0, right is 1, prev is 2 */
1553 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001554 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001555 xfs_filblks_t da_new; /* new count del alloc blocks used */
1556 xfs_filblks_t da_old; /* old count del alloc blocks used */
1557 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001559 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001560 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001561 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001563 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001564 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001565 ASSERT(whichfork != XFS_ATTR_FORK);
1566 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1567 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001568
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001569 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001570 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001571 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001572 ASSERT(!bma->cur ||
1573 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001574
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001575 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577#define LEFT r[0]
1578#define RIGHT r[1]
1579#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
1581 /*
1582 * Set up a bunch of variables to make the tests simpler.
1583 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001584 xfs_iext_get_extent(ifp, bma->idx, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001586 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 ASSERT(PREV.br_startoff <= new->br_startoff);
1588 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001589
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001590 da_old = startblockval(PREV.br_startblock);
1591 da_new = 0;
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 /*
1594 * Set flags determining what part of the previous delayed allocation
1595 * extent is being replaced by a real allocation.
1596 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001597 if (PREV.br_startoff == new->br_startoff)
1598 state |= BMAP_LEFT_FILLING;
1599 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1600 state |= BMAP_RIGHT_FILLING;
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /*
1603 * Check and set flags if this segment has a left neighbor.
1604 * Don't set contiguous if the combined extent would be too large.
1605 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001606 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001607 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001608 xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001609
1610 if (isnullstartblock(LEFT.br_startblock))
1611 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001613
1614 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1615 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1616 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1617 LEFT.br_state == new->br_state &&
1618 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1619 state |= BMAP_LEFT_CONTIG;
1620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 /*
1622 * Check and set flags if this segment has a right neighbor.
1623 * Don't set contiguous if the combined extent would be too large.
1624 * Also check for all-three-contiguous being too large.
1625 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001626 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001627 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001628 xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001629
1630 if (isnullstartblock(RIGHT.br_startblock))
1631 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001633
1634 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1635 new_endoff == RIGHT.br_startoff &&
1636 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1637 new->br_state == RIGHT.br_state &&
1638 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1639 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1640 BMAP_RIGHT_FILLING)) !=
1641 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1642 BMAP_RIGHT_FILLING) ||
1643 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1644 <= MAXEXTLEN))
1645 state |= BMAP_RIGHT_CONTIG;
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 error = 0;
1648 /*
1649 * Switch out based on the FILLING and CONTIG state bits.
1650 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001651 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1652 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1653 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1654 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 /*
1656 * Filling in all of a previously delayed allocation extent.
1657 * The left and right neighbors are both contiguous with new.
1658 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001659 bma->idx--;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001660 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001661 xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001662
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001663 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001664 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001665 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1667 else {
1668 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001669 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001670 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001672 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001673 error = xfs_btree_delete(bma->cur, &i);
1674 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001676 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001677 error = xfs_btree_decrement(bma->cur, 0, &i);
1678 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001680 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001681 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001682 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 goto done;
1684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 break;
1686
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001687 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 /*
1689 * Filling in all of a previously delayed allocation extent.
1690 * The left neighbor is contiguous, the right is not.
1691 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001692 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001693
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001694 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001695 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001696 xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001697
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001698 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1699 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 rval = XFS_ILOG_DEXT;
1701 else {
1702 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001703 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001704 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001706 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001707 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001708 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 goto done;
1710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 break;
1712
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001713 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /*
1715 * Filling in all of a previously delayed allocation extent.
1716 * The right neighbor is contiguous, the left is not.
1717 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001718 PREV.br_startblock = new->br_startblock;
1719 PREV.br_blockcount += RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001720 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001721
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001722 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1723 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 rval = XFS_ILOG_DEXT;
1725 else {
1726 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001727 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001728 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001730 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001731 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001732 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 goto done;
1734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 break;
1736
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001737 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 /*
1739 * Filling in all of a previously delayed allocation extent.
1740 * Neither the left nor right neighbors are contiguous with
1741 * the new one.
1742 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001743 PREV.br_startblock = new->br_startblock;
1744 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001745 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001746
Darrick J. Wong60b49842016-10-03 09:11:34 -07001747 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001748 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1750 else {
1751 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001752 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001753 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001755 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001756 error = xfs_btree_insert(bma->cur, &i);
1757 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001759 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 break;
1762
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001763 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /*
1765 * Filling in the first part of a previous delayed allocation.
1766 * The left neighbor is contiguous.
1767 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001768 old = LEFT;
1769 temp = PREV.br_blockcount - new->br_blockcount;
1770 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1771 startblockval(PREV.br_startblock));
1772
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001773 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001774 xfs_iext_update_extent(bma->ip, state, bma->idx - 1, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001775
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001776 PREV.br_blockcount = temp = PREV.br_blockcount - new->br_blockcount;
1777 PREV.br_startoff += new->br_blockcount;
1778 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001779 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001780
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001781 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 rval = XFS_ILOG_DEXT;
1783 else {
1784 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001785 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001786 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001788 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001789 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001790 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 goto done;
1792 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001793
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001794 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 break;
1796
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001797 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 /*
1799 * Filling in the first part of a previous delayed allocation.
1800 * The left neighbor is not contiguous.
1801 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001802 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001803 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001804 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1806 else {
1807 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001808 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001809 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001811 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001812 error = xfs_btree_insert(bma->cur, &i);
1813 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001815 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001817
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001818 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001820 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001821 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 rval |= tmp_rval;
1823 if (error)
1824 goto done;
1825 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001826
1827 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001828 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001829 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001830 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001831
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001832 PREV.br_startoff = new_endoff;
1833 PREV.br_blockcount = temp;
1834 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001835 xfs_iext_update_extent(bma->ip, state, bma->idx + 1, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 break;
1837
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001838 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /*
1840 * Filling in the last part of a previous delayed allocation.
1841 * The right neighbor is contiguous with the new allocation.
1842 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001843 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001844 RIGHT.br_startoff = new->br_startoff;
1845 RIGHT.br_startblock = new->br_startblock;
1846 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001847 xfs_iext_update_extent(bma->ip, state, bma->idx + 1, &RIGHT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001848
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001849 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 rval = XFS_ILOG_DEXT;
1851 else {
1852 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001853 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001854 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001856 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001857 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001858 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 goto done;
1860 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001861
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001862 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001863 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001864 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001865
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001866 PREV.br_blockcount = temp;
1867 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001868 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001869
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001870 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 break;
1872
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001873 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 /*
1875 * Filling in the last part of a previous delayed allocation.
1876 * The right neighbor is not contiguous.
1877 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001878 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001879 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001880 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1882 else {
1883 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001884 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001885 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001887 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001888 error = xfs_btree_insert(bma->cur, &i);
1889 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001891 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001893
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001894 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001895 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001896 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001897 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 rval |= tmp_rval;
1899 if (error)
1900 goto done;
1901 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001902
1903 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001904 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001905 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001906 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001907
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001908 PREV.br_startblock = nullstartblock(da_new);
1909 PREV.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001910 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001911
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001912 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 break;
1914
1915 case 0:
1916 /*
1917 * Filling in the middle part of a previous delayed allocation.
1918 * Contiguity is impossible here.
1919 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001920 *
1921 * We start with a delayed allocation:
1922 *
1923 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1924 * PREV @ idx
1925 *
1926 * and we are allocating:
1927 * +rrrrrrrrrrrrrrrrr+
1928 * new
1929 *
1930 * and we set it up for insertion as:
1931 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1932 * new
1933 * PREV @ idx LEFT RIGHT
1934 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001936 old = PREV;
1937
1938 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001939 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001940
1941 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001942 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001943 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001944 RIGHT.br_blockcount =
1945 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1946 RIGHT.br_startblock =
1947 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1948 RIGHT.br_blockcount));
1949
1950 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001951 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1952 PREV.br_startblock =
1953 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1954 PREV.br_blockcount));
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001955 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001956
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001957 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001959 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001960
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001961 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1963 else {
1964 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001965 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001966 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001968 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001969 error = xfs_btree_insert(bma->cur, &i);
1970 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001972 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001974
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001975 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001976 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001977 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001978 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 rval |= tmp_rval;
1980 if (error)
1981 goto done;
1982 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001983
1984 da_new = startblockval(PREV.br_startblock) +
1985 startblockval(RIGHT.br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001986 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 break;
1988
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001989 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1990 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1991 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1992 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1993 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1994 case BMAP_LEFT_CONTIG:
1995 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 /*
1997 * These cases are all impossible.
1998 */
1999 ASSERT(0);
2000 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002001
Darrick J. Wong9c194642016-08-03 12:16:05 +10002002 /* add reverse mapping */
2003 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2004 if (error)
2005 goto done;
2006
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002007 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002008 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002009 int tmp_logflags; /* partial log flag return val */
2010
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002011 ASSERT(bma->cur == NULL);
2012 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002013 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002014 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002015 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002016 if (error)
2017 goto done;
2018 }
2019
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002020 if (bma->cur) {
2021 da_new += bma->cur->bc_private.b.allocated;
2022 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002023 }
2024
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002025 /* adjust for changes in reserved delayed indirect blocks */
2026 if (da_new != da_old) {
2027 ASSERT(state == 0 || da_new < da_old);
2028 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2029 false);
2030 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002031
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002032 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002034 if (whichfork != XFS_COW_FORK)
2035 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 return error;
2037#undef LEFT
2038#undef RIGHT
2039#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
2042/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002043 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 */
2045STATIC int /* error */
2046xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002047 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002049 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002050 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002052 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002053 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002054 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002055 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002060 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2063 /* left is 0, right is 1, prev is 2 */
2064 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002065 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002066 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002067 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002069 *logflagsp = 0;
2070
2071 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002072 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002073
2074 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002075 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002076 ASSERT(!isnullstartblock(new->br_startblock));
2077
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002078 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080#define LEFT r[0]
2081#define RIGHT r[1]
2082#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /*
2085 * Set up a bunch of variables to make the tests simpler.
2086 */
2087 error = 0;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002088 xfs_iext_get_extent(ifp, *idx, &PREV);
2089 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 new_endoff = new->br_startoff + new->br_blockcount;
2091 ASSERT(PREV.br_startoff <= new->br_startoff);
2092 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /*
2095 * Set flags determining what part of the previous oldext allocation
2096 * extent is being replaced by a newext allocation.
2097 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002098 if (PREV.br_startoff == new->br_startoff)
2099 state |= BMAP_LEFT_FILLING;
2100 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2101 state |= BMAP_RIGHT_FILLING;
2102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /*
2104 * Check and set flags if this segment has a left neighbor.
2105 * Don't set contiguous if the combined extent would be too large.
2106 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002107 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002108 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002109 xfs_iext_get_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002110
2111 if (isnullstartblock(LEFT.br_startblock))
2112 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002114
2115 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2116 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2117 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002118 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002119 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2120 state |= BMAP_LEFT_CONTIG;
2121
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 /*
2123 * Check and set flags if this segment has a right neighbor.
2124 * Don't set contiguous if the combined extent would be too large.
2125 * Also check for all-three-contiguous being too large.
2126 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002127 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002128 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002129 xfs_iext_get_extent(ifp, *idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002130 if (isnullstartblock(RIGHT.br_startblock))
2131 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002133
2134 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2135 new_endoff == RIGHT.br_startoff &&
2136 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002137 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002138 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2139 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2140 BMAP_RIGHT_FILLING)) !=
2141 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2142 BMAP_RIGHT_FILLING) ||
2143 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2144 <= MAXEXTLEN))
2145 state |= BMAP_RIGHT_CONTIG;
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 /*
2148 * Switch out based on the FILLING and CONTIG state bits.
2149 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002150 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2151 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2152 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2153 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 /*
2155 * Setting all of a previous oldext extent to newext.
2156 * The left and right neighbors are both contiguous with new.
2157 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002158 --*idx;
2159
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002160 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002161 xfs_iext_update_extent(ip, state, *idx, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002162
Christoph Hellwigec90c552011-05-23 08:52:53 +00002163 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002164 XFS_IFORK_NEXT_SET(ip, whichfork,
2165 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 if (cur == NULL)
2167 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2168 else {
2169 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002170 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2171 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002173 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002174 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002176 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002177 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002179 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002180 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002182 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002183 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002185 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002186 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002187 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 goto done;
2189 }
2190 break;
2191
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002192 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /*
2194 * Setting all of a previous oldext extent to newext.
2195 * The left neighbor is contiguous, the right is not.
2196 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002197 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002198
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002199 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002200 xfs_iext_update_extent(ip, state, *idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002201
2202 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002203 XFS_IFORK_NEXT_SET(ip, whichfork,
2204 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 if (cur == NULL)
2206 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2207 else {
2208 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002209 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2210 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002212 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002213 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002215 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002216 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002218 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002219 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002220 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 goto done;
2222 }
2223 break;
2224
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002225 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 /*
2227 * Setting all of a previous oldext extent to newext.
2228 * The right neighbor is contiguous, the left is not.
2229 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002230 PREV.br_blockcount += RIGHT.br_blockcount;
2231 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002232 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002233
Christoph Hellwigec90c552011-05-23 08:52:53 +00002234 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002235 XFS_IFORK_NEXT_SET(ip, whichfork,
2236 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 if (cur == NULL)
2238 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2239 else {
2240 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002241 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2242 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002244 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002245 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002247 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002248 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002250 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002251 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002252 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 goto done;
2254 }
2255 break;
2256
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002257 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 /*
2259 * Setting all of a previous oldext extent to newext.
2260 * Neither the left nor right neighbors are contiguous with
2261 * the new one.
2262 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002263 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002264 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002265
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (cur == NULL)
2267 rval = XFS_ILOG_DEXT;
2268 else {
2269 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002270 error = xfs_bmbt_lookup_eq(cur, new, &i);
2271 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002273 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002274 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002275 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto done;
2277 }
2278 break;
2279
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002280 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 /*
2282 * Setting the first part of a previous oldext extent to newext.
2283 * The left neighbor is contiguous.
2284 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002285 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002286 xfs_iext_update_extent(ip, state, *idx - 1, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002287
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002288 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002289 PREV.br_startoff += new->br_blockcount;
2290 PREV.br_startblock += new->br_blockcount;
2291 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002292 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002293
Christoph Hellwigec90c552011-05-23 08:52:53 +00002294 --*idx;
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 if (cur == NULL)
2297 rval = XFS_ILOG_DEXT;
2298 else {
2299 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002300 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002301 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002303 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002304 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002305 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002307 error = xfs_btree_decrement(cur, 0, &i);
2308 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002310 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002311 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 goto done;
2313 }
2314 break;
2315
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002316 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 /*
2318 * Setting the first part of a previous oldext extent to newext.
2319 * The left neighbor is not contiguous.
2320 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002321 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002322 PREV.br_startoff += new->br_blockcount;
2323 PREV.br_startblock += new->br_blockcount;
2324 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002325 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002326
Christoph Hellwigec90c552011-05-23 08:52:53 +00002327 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002328 XFS_IFORK_NEXT_SET(ip, whichfork,
2329 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 if (cur == NULL)
2331 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2332 else {
2333 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002334 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002335 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002337 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002338 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002339 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 goto done;
2341 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002342 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002344 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
2346 break;
2347
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002348 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 /*
2350 * Setting the last part of a previous oldext extent to newext.
2351 * The right neighbor is contiguous with the new allocation.
2352 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002353 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002354 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002355 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002356
2357 ++*idx;
2358
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002359 RIGHT.br_startoff = new->br_startoff;
2360 RIGHT.br_startblock = new->br_startblock;
2361 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002362 xfs_iext_update_extent(ip, state, *idx, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (cur == NULL)
2365 rval = XFS_ILOG_DEXT;
2366 else {
2367 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002368 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002369 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002371 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002372 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002373 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002375 error = xfs_btree_increment(cur, 0, &i);
2376 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002378 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002379 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 goto done;
2381 }
2382 break;
2383
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002384 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 /*
2386 * Setting the last part of a previous oldext extent to newext.
2387 * The right neighbor is not contiguous.
2388 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002389 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002390 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002391 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002392
Christoph Hellwigec90c552011-05-23 08:52:53 +00002393 ++*idx;
2394 xfs_iext_insert(ip, *idx, 1, new, state);
2395
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002396 XFS_IFORK_NEXT_SET(ip, whichfork,
2397 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (cur == NULL)
2399 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2400 else {
2401 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002402 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002403 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002405 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002406 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002407 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002409 error = xfs_bmbt_lookup_eq(cur, new, &i);
2410 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002412 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002413 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002415 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 }
2417 break;
2418
2419 case 0:
2420 /*
2421 * Setting the middle part of a previous oldext extent to
2422 * newext. Contiguity is impossible here.
2423 * One extent becomes three extents.
2424 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002425 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002426 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002427 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 r[0] = *new;
2430 r[1].br_startoff = new_endoff;
2431 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002432 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002434 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002435
2436 ++*idx;
2437 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2438
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002439 XFS_IFORK_NEXT_SET(ip, whichfork,
2440 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (cur == NULL)
2442 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2443 else {
2444 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002445 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002446 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002448 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002450 error = xfs_bmbt_update(cur, &r[1]);
2451 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 goto done;
2453 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002455 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002457 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002458 /*
2459 * Reset the cursor to the position of the new extent
2460 * we are about to insert as we can't trust it after
2461 * the previous insert.
2462 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002463 error = xfs_bmbt_lookup_eq(cur, new, &i);
2464 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002466 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002468 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002470 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
2472 break;
2473
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002474 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2475 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2476 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2477 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2478 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2479 case BMAP_LEFT_CONTIG:
2480 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 /*
2482 * These cases are all impossible.
2483 */
2484 ASSERT(0);
2485 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002486
Darrick J. Wong9c194642016-08-03 12:16:05 +10002487 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002488 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002489 if (error)
2490 goto done;
2491
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002492 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002493 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002494 int tmp_logflags; /* partial log flag return val */
2495
2496 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002497 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002498 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002499 *logflagsp |= tmp_logflags;
2500 if (error)
2501 goto done;
2502 }
2503
2504 /* clear out the allocated field, done with it now in any case. */
2505 if (cur) {
2506 cur->bc_private.b.allocated = 0;
2507 *curp = cur;
2508 }
2509
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002510 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002512 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 return error;
2514#undef LEFT
2515#undef RIGHT
2516#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
2519/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002520 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002522STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523xfs_bmap_add_extent_hole_delay(
2524 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002525 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002526 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002527 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002529 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2531 xfs_filblks_t newlen=0; /* new indirect size */
2532 xfs_filblks_t oldlen=0; /* old indirect size */
2533 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002534 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002535 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002537 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002538 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /*
2541 * Check and set flags if this segment has a left neighbor
2542 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002543 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002544 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002545 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002546
2547 if (isnullstartblock(left.br_startblock))
2548 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 /*
2552 * Check and set flags if the current (right) segment exists.
2553 * If it doesn't exist, we're converting the hole at end-of-file.
2554 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002555 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002556 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002557 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002558
2559 if (isnullstartblock(right.br_startblock))
2560 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 /*
2564 * Set contiguity flags on the left and right neighbors.
2565 * Don't let extents get too large, even if the pieces are contiguous.
2566 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002567 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2568 left.br_startoff + left.br_blockcount == new->br_startoff &&
2569 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2570 state |= BMAP_LEFT_CONTIG;
2571
2572 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2573 new->br_startoff + new->br_blockcount == right.br_startoff &&
2574 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2575 (!(state & BMAP_LEFT_CONTIG) ||
2576 (left.br_blockcount + new->br_blockcount +
2577 right.br_blockcount <= MAXEXTLEN)))
2578 state |= BMAP_RIGHT_CONTIG;
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 /*
2581 * Switch out based on the contiguity flags.
2582 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002583 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2584 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 /*
2586 * New allocation is contiguous with delayed allocations
2587 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002588 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002590 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 temp = left.br_blockcount + new->br_blockcount +
2592 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002593
Eric Sandeen9d87c312009-01-14 23:22:07 -06002594 oldlen = startblockval(left.br_startblock) +
2595 startblockval(new->br_startblock) +
2596 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002597 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2598 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002599 left.br_startblock = nullstartblock(newlen);
2600 left.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002601 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002602
Christoph Hellwigec90c552011-05-23 08:52:53 +00002603 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 break;
2605
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002606 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 /*
2608 * New allocation is contiguous with a delayed allocation
2609 * on the left.
2610 * Merge the new allocation with the left neighbor.
2611 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002612 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002614
Eric Sandeen9d87c312009-01-14 23:22:07 -06002615 oldlen = startblockval(left.br_startblock) +
2616 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002617 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2618 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002619 left.br_blockcount = temp;
2620 left.br_startblock = nullstartblock(newlen);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002621 xfs_iext_update_extent(ip, state, *idx, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 break;
2623
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002624 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 /*
2626 * New allocation is contiguous with a delayed allocation
2627 * on the right.
2628 * Merge the new allocation with the right neighbor.
2629 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002631 oldlen = startblockval(new->br_startblock) +
2632 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002633 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2634 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002635 right.br_startoff = new->br_startoff;
2636 right.br_startblock = nullstartblock(newlen);
2637 right.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002638 xfs_iext_update_extent(ip, state, *idx, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 break;
2640
2641 case 0:
2642 /*
2643 * New allocation is not contiguous with another
2644 * delayed allocation.
2645 * Insert a new entry.
2646 */
2647 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002648 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 break;
2650 }
2651 if (oldlen != newlen) {
2652 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002653 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2654 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 /*
2656 * Nothing to do for disk quota accounting here.
2657 */
2658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
2660
2661/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002662 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 */
2664STATIC int /* error */
2665xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002666 struct xfs_trans *tp,
2667 struct xfs_inode *ip,
2668 int whichfork,
2669 xfs_extnum_t *idx,
2670 struct xfs_btree_cur **curp,
2671 struct xfs_bmbt_irec *new,
2672 xfs_fsblock_t *first,
2673 struct xfs_defer_ops *dfops,
2674 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002676 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2677 struct xfs_mount *mp = ip->i_mount;
2678 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2682 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002683 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002684 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002685 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Christoph Hellwig6d045582017-04-11 16:45:54 -07002687 ASSERT(*idx >= 0);
2688 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002689 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002690 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002691
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002692 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 /*
2695 * Check and set flags if this segment has a left neighbor.
2696 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002697 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002698 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002699 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002700 if (isnullstartblock(left.br_startblock))
2701 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /*
2705 * Check and set flags if this segment has a current value.
2706 * Not true if we're inserting into the "hole" at eof.
2707 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002708 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002709 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002710 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002711 if (isnullstartblock(right.br_startblock))
2712 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 /*
2716 * We're inserting a real allocation between "left" and "right".
2717 * Set the contiguity flags. Don't let extents get too large.
2718 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002719 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2720 left.br_startoff + left.br_blockcount == new->br_startoff &&
2721 left.br_startblock + left.br_blockcount == new->br_startblock &&
2722 left.br_state == new->br_state &&
2723 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2724 state |= BMAP_LEFT_CONTIG;
2725
2726 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2727 new->br_startoff + new->br_blockcount == right.br_startoff &&
2728 new->br_startblock + new->br_blockcount == right.br_startblock &&
2729 new->br_state == right.br_state &&
2730 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2731 (!(state & BMAP_LEFT_CONTIG) ||
2732 left.br_blockcount + new->br_blockcount +
2733 right.br_blockcount <= MAXEXTLEN))
2734 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002736 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 /*
2738 * Select which case we're in here, and implement it.
2739 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002740 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2741 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 /*
2743 * New allocation is contiguous with real allocations on the
2744 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002745 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002747 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002748 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002749 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002750
Christoph Hellwig6d045582017-04-11 16:45:54 -07002751 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002752
Christoph Hellwig6d045582017-04-11 16:45:54 -07002753 XFS_IFORK_NEXT_SET(ip, whichfork,
2754 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2755 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002756 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002757 } else {
2758 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002759 error = xfs_bmbt_lookup_eq(cur, &right, &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 Hellwig6d045582017-04-11 16:45:54 -07002763 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002764 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002765 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002766 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002767 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002768 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002769 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002770 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002771 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002772 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002773 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002775 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002777 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 /*
2779 * New allocation is contiguous with a real allocation
2780 * on the left.
2781 * Merge the new allocation with the left neighbor.
2782 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002783 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002784 old = left;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002785
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002786 left.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002787 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002788
Christoph Hellwig6d045582017-04-11 16:45:54 -07002789 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002790 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002791 } else {
2792 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002793 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002794 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002795 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002796 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002797 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002798 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002799 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002803 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 /*
2805 * New allocation is contiguous with a real allocation
2806 * on the right.
2807 * Merge the new allocation with the right neighbor.
2808 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002809 old = right;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002810
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002811 right.br_startoff = new->br_startoff;
2812 right.br_startblock = new->br_startblock;
2813 right.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002814 xfs_iext_update_extent(ip, state, *idx, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002815
Christoph Hellwig6d045582017-04-11 16:45:54 -07002816 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002817 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002818 } else {
2819 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002820 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002821 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002822 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002823 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002824 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002825 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002826 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002828 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
2830 case 0:
2831 /*
2832 * New allocation is not contiguous with another
2833 * real allocation.
2834 * Insert a new entry.
2835 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002836 xfs_iext_insert(ip, *idx, 1, new, state);
2837 XFS_IFORK_NEXT_SET(ip, whichfork,
2838 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2839 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002840 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002841 } else {
2842 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002843 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002844 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002845 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002846 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002847 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002848 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002849 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002850 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002852 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002854
Darrick J. Wong9c194642016-08-03 12:16:05 +10002855 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002856 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002857 if (error)
2858 goto done;
2859
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002860 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002861 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002862 int tmp_logflags; /* partial log flag return val */
2863
Christoph Hellwig6d045582017-04-11 16:45:54 -07002864 ASSERT(cur == NULL);
2865 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002866 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002867 *logflagsp |= tmp_logflags;
2868 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002869 if (error)
2870 goto done;
2871 }
2872
2873 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002874 if (cur)
2875 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002876
Christoph Hellwig6d045582017-04-11 16:45:54 -07002877 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002878done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002879 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002880 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881}
2882
Nathan Scottdd9f4382006-01-11 15:28:28 +11002883/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11002884 * Functions used in the extent read, allocate and remove paths
2885 */
2886
2887/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002888 * Adjust the size of the new extent based on di_extsize and rt extsize.
2889 */
Dave Chinner68988112013-08-12 20:49:42 +10002890int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002891xfs_bmap_extsize_align(
2892 xfs_mount_t *mp,
2893 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2894 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2895 xfs_extlen_t extsz, /* align to this extent size */
2896 int rt, /* is this a realtime inode? */
2897 int eof, /* is extent at end-of-file? */
2898 int delay, /* creating delalloc extent? */
2899 int convert, /* overwriting unwritten extent? */
2900 xfs_fileoff_t *offp, /* in/out: aligned offset */
2901 xfs_extlen_t *lenp) /* in/out: aligned length */
2902{
2903 xfs_fileoff_t orig_off; /* original offset */
2904 xfs_extlen_t orig_alen; /* original length */
2905 xfs_fileoff_t orig_end; /* original off+len */
2906 xfs_fileoff_t nexto; /* next file offset */
2907 xfs_fileoff_t prevo; /* previous file offset */
2908 xfs_fileoff_t align_off; /* temp for offset */
2909 xfs_extlen_t align_alen; /* temp for length */
2910 xfs_extlen_t temp; /* temp for calculations */
2911
2912 if (convert)
2913 return 0;
2914
2915 orig_off = align_off = *offp;
2916 orig_alen = align_alen = *lenp;
2917 orig_end = orig_off + orig_alen;
2918
2919 /*
2920 * If this request overlaps an existing extent, then don't
2921 * attempt to perform any additional alignment.
2922 */
2923 if (!delay && !eof &&
2924 (orig_off >= gotp->br_startoff) &&
2925 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2926 return 0;
2927 }
2928
2929 /*
2930 * If the file offset is unaligned vs. the extent size
2931 * we need to align it. This will be possible unless
2932 * the file was previously written with a kernel that didn't
2933 * perform this alignment, or if a truncate shot us in the
2934 * foot.
2935 */
2936 temp = do_mod(orig_off, extsz);
2937 if (temp) {
2938 align_alen += temp;
2939 align_off -= temp;
2940 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002941
2942 /* Same adjustment for the end of the requested area. */
2943 temp = (align_alen % extsz);
2944 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002945 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002946
2947 /*
2948 * For large extent hint sizes, the aligned extent might be larger than
2949 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2950 * the length back under MAXEXTLEN. The outer allocation loops handle
2951 * short allocation just fine, so it is safe to do this. We only want to
2952 * do it when we are forced to, though, because it means more allocation
2953 * operations are required.
2954 */
2955 while (align_alen > MAXEXTLEN)
2956 align_alen -= extsz;
2957 ASSERT(align_alen <= MAXEXTLEN);
2958
Nathan Scottdd9f4382006-01-11 15:28:28 +11002959 /*
2960 * If the previous block overlaps with this proposed allocation
2961 * then move the start forward without adjusting the length.
2962 */
2963 if (prevp->br_startoff != NULLFILEOFF) {
2964 if (prevp->br_startblock == HOLESTARTBLOCK)
2965 prevo = prevp->br_startoff;
2966 else
2967 prevo = prevp->br_startoff + prevp->br_blockcount;
2968 } else
2969 prevo = 0;
2970 if (align_off != orig_off && align_off < prevo)
2971 align_off = prevo;
2972 /*
2973 * If the next block overlaps with this proposed allocation
2974 * then move the start back without adjusting the length,
2975 * but not before offset 0.
2976 * This may of course make the start overlap previous block,
2977 * and if we hit the offset 0 limit then the next block
2978 * can still overlap too.
2979 */
2980 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2981 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2982 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2983 nexto = gotp->br_startoff + gotp->br_blockcount;
2984 else
2985 nexto = gotp->br_startoff;
2986 } else
2987 nexto = NULLFILEOFF;
2988 if (!eof &&
2989 align_off + align_alen != orig_end &&
2990 align_off + align_alen > nexto)
2991 align_off = nexto > align_alen ? nexto - align_alen : 0;
2992 /*
2993 * If we're now overlapping the next or previous extent that
2994 * means we can't fit an extsz piece in this hole. Just move
2995 * the start forward to the first valid spot and set
2996 * the length so we hit the end.
2997 */
2998 if (align_off != orig_off && align_off < prevo)
2999 align_off = prevo;
3000 if (align_off + align_alen != orig_end &&
3001 align_off + align_alen > nexto &&
3002 nexto != NULLFILEOFF) {
3003 ASSERT(nexto > prevo);
3004 align_alen = nexto - align_off;
3005 }
3006
3007 /*
3008 * If realtime, and the result isn't a multiple of the realtime
3009 * extent size we need to remove blocks until it is.
3010 */
3011 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3012 /*
3013 * We're not covering the original request, or
3014 * we won't be able to once we fix the length.
3015 */
3016 if (orig_off < align_off ||
3017 orig_end > align_off + align_alen ||
3018 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003019 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003020 /*
3021 * Try to fix it by moving the start up.
3022 */
3023 if (align_off + temp <= orig_off) {
3024 align_alen -= temp;
3025 align_off += temp;
3026 }
3027 /*
3028 * Try to fix it by moving the end in.
3029 */
3030 else if (align_off + align_alen - temp >= orig_end)
3031 align_alen -= temp;
3032 /*
3033 * Set the start to the minimum then trim the length.
3034 */
3035 else {
3036 align_alen -= orig_off - align_off;
3037 align_off = orig_off;
3038 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3039 }
3040 /*
3041 * Result doesn't cover the request, fail it.
3042 */
3043 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003044 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003045 } else {
3046 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003047 /* see MAXEXTLEN handling above */
3048 ASSERT(orig_end <= align_off + align_alen ||
3049 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003050 }
3051
3052#ifdef DEBUG
3053 if (!eof && gotp->br_startoff != NULLFILEOFF)
3054 ASSERT(align_off + align_alen <= gotp->br_startoff);
3055 if (prevp->br_startoff != NULLFILEOFF)
3056 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3057#endif
3058
3059 *lenp = align_alen;
3060 *offp = align_off;
3061 return 0;
3062}
3063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064#define XFS_ALLOC_GAP_UNITS 4
3065
Dave Chinner68988112013-08-12 20:49:42 +10003066void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003067xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003068 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
3070 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3072 xfs_mount_t *mp; /* mount point structure */
3073 int nullfb; /* true if ap->firstblock isn't set */
3074 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
3076#define ISVALID(x,y) \
3077 (rt ? \
3078 (x) < mp->m_sb.sb_rblocks : \
3079 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3080 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3081 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003084 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003085 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3086 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003087 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 /*
3089 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003090 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003092 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3093 !isnullstartblock(ap->prev.br_startblock) &&
3094 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3095 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003096 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 /*
3098 * Adjust for the gap between prevp and us.
3099 */
Dave Chinner3a756672011-09-18 20:40:58 +00003100 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003101 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003103 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3104 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 }
3106 /*
3107 * If not at eof, then compare the two neighbor blocks.
3108 * Figure out whether either one gives us a good starting point,
3109 * and pick the better one.
3110 */
3111 else if (!ap->eof) {
3112 xfs_fsblock_t gotbno; /* right side block number */
3113 xfs_fsblock_t gotdiff=0; /* right side difference */
3114 xfs_fsblock_t prevbno; /* left side block number */
3115 xfs_fsblock_t prevdiff=0; /* left side difference */
3116
3117 /*
3118 * If there's a previous (left) block, select a requested
3119 * start block based on it.
3120 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003121 if (ap->prev.br_startoff != NULLFILEOFF &&
3122 !isnullstartblock(ap->prev.br_startblock) &&
3123 (prevbno = ap->prev.br_startblock +
3124 ap->prev.br_blockcount) &&
3125 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 /*
3127 * Calculate gap to end of previous block.
3128 */
Dave Chinner3a756672011-09-18 20:40:58 +00003129 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003130 (ap->prev.br_startoff +
3131 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 /*
3133 * Figure the startblock based on the previous block's
3134 * end and the gap size.
3135 * Heuristic!
3136 * If the gap is large relative to the piece we're
3137 * allocating, or using it gives us an invalid block
3138 * number, then just use the end of the previous block.
3139 */
Dave Chinner3a756672011-09-18 20:40:58 +00003140 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003142 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 prevbno += adjust;
3144 else
3145 prevdiff += adjust;
3146 /*
3147 * If the firstblock forbids it, can't use it,
3148 * must use default.
3149 */
3150 if (!rt && !nullfb &&
3151 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3152 prevbno = NULLFSBLOCK;
3153 }
3154 /*
3155 * No previous block or can't follow it, just default.
3156 */
3157 else
3158 prevbno = NULLFSBLOCK;
3159 /*
3160 * If there's a following (right) block, select a requested
3161 * start block based on it.
3162 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003163 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 /*
3165 * Calculate gap to start of next block.
3166 */
Dave Chinner3a756672011-09-18 20:40:58 +00003167 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 /*
3169 * Figure the startblock based on the next block's
3170 * start and the gap size.
3171 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003172 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 /*
3174 * Heuristic!
3175 * If the gap is large relative to the piece we're
3176 * allocating, or using it gives us an invalid block
3177 * number, then just use the start of the next block
3178 * offset by our length.
3179 */
Dave Chinner3a756672011-09-18 20:40:58 +00003180 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 ISVALID(gotbno - gotdiff, gotbno))
3182 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003183 else if (ISVALID(gotbno - ap->length, gotbno)) {
3184 gotbno -= ap->length;
3185 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 } else
3187 gotdiff += adjust;
3188 /*
3189 * If the firstblock forbids it, can't use it,
3190 * must use default.
3191 */
3192 if (!rt && !nullfb &&
3193 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3194 gotbno = NULLFSBLOCK;
3195 }
3196 /*
3197 * No next block, just default.
3198 */
3199 else
3200 gotbno = NULLFSBLOCK;
3201 /*
3202 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003203 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 */
3205 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003206 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003208 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003210 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003212#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003213}
3214
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003215static int
3216xfs_bmap_longest_free_extent(
3217 struct xfs_trans *tp,
3218 xfs_agnumber_t ag,
3219 xfs_extlen_t *blen,
3220 int *notinit)
3221{
3222 struct xfs_mount *mp = tp->t_mountp;
3223 struct xfs_perag *pag;
3224 xfs_extlen_t longest;
3225 int error = 0;
3226
3227 pag = xfs_perag_get(mp, ag);
3228 if (!pag->pagf_init) {
3229 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3230 if (error)
3231 goto out;
3232
3233 if (!pag->pagf_init) {
3234 *notinit = 1;
3235 goto out;
3236 }
3237 }
3238
Dave Chinner50adbcb2015-06-22 10:04:31 +10003239 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003240 xfs_alloc_min_freelist(mp, pag),
3241 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003242 if (*blen < longest)
3243 *blen = longest;
3244
3245out:
3246 xfs_perag_put(pag);
3247 return error;
3248}
3249
3250static void
3251xfs_bmap_select_minlen(
3252 struct xfs_bmalloca *ap,
3253 struct xfs_alloc_arg *args,
3254 xfs_extlen_t *blen,
3255 int notinit)
3256{
3257 if (notinit || *blen < ap->minlen) {
3258 /*
3259 * Since we did a BUF_TRYLOCK above, it is possible that
3260 * there is space for this request.
3261 */
3262 args->minlen = ap->minlen;
3263 } else if (*blen < args->maxlen) {
3264 /*
3265 * If the best seen length is less than the request length,
3266 * use the best as the minimum.
3267 */
3268 args->minlen = *blen;
3269 } else {
3270 /*
3271 * Otherwise we've seen an extent as big as maxlen, use that
3272 * as the minimum.
3273 */
3274 args->minlen = args->maxlen;
3275 }
3276}
3277
Nathan Scotta365bdd2006-03-14 13:34:16 +11003278STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003279xfs_bmap_btalloc_nullfb(
3280 struct xfs_bmalloca *ap,
3281 struct xfs_alloc_arg *args,
3282 xfs_extlen_t *blen)
3283{
3284 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003285 xfs_agnumber_t ag, startag;
3286 int notinit = 0;
3287 int error;
3288
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003289 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003290 args->total = ap->total;
3291
Christoph Hellwigc467c042010-02-15 23:34:42 +00003292 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3293 if (startag == NULLAGNUMBER)
3294 startag = ag = 0;
3295
Dave Chinner14b064c2011-01-27 12:16:28 +11003296 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003297 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3298 &notinit);
3299 if (error)
3300 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003301
Christoph Hellwigc467c042010-02-15 23:34:42 +00003302 if (++ag == mp->m_sb.sb_agcount)
3303 ag = 0;
3304 if (ag == startag)
3305 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003306 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003307
3308 xfs_bmap_select_minlen(ap, args, blen, notinit);
3309 return 0;
3310}
3311
3312STATIC int
3313xfs_bmap_btalloc_filestreams(
3314 struct xfs_bmalloca *ap,
3315 struct xfs_alloc_arg *args,
3316 xfs_extlen_t *blen)
3317{
3318 struct xfs_mount *mp = ap->ip->i_mount;
3319 xfs_agnumber_t ag;
3320 int notinit = 0;
3321 int error;
3322
3323 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3324 args->total = ap->total;
3325
3326 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3327 if (ag == NULLAGNUMBER)
3328 ag = 0;
3329
3330 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3331 if (error)
3332 return error;
3333
3334 if (*blen < args->maxlen) {
3335 error = xfs_filestream_new_ag(ap, &ag);
3336 if (error)
3337 return error;
3338
3339 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3340 &notinit);
3341 if (error)
3342 return error;
3343
3344 }
3345
3346 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003347
3348 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003349 * Set the failure fallback case to look in the selected AG as stream
3350 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003351 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003352 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003353 return 0;
3354}
3355
3356STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003357xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003358 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003359{
3360 xfs_mount_t *mp; /* mount point structure */
3361 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003362 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003363 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003364 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003365 xfs_alloc_arg_t args;
3366 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003367 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003368 int nullfb; /* true if ap->firstblock isn't set */
3369 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003370 int tryagain;
3371 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003372 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003373
Dave Chinnera99ebf42011-12-01 11:24:20 +00003374 ASSERT(ap->length);
3375
Nathan Scotta365bdd2006-03-14 13:34:16 +11003376 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003377
3378 /* stripe alignment for allocation is determined by mount parameters */
3379 stripe_align = 0;
3380 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3381 stripe_align = mp->m_swidth;
3382 else if (mp->m_dalign)
3383 stripe_align = mp->m_dalign;
3384
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003385 if (ap->flags & XFS_BMAPI_COWFORK)
3386 align = xfs_get_cowextsz_hint(ap->ip);
3387 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003388 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003389 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003390 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003391 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003392 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003393 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003394 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003395 }
Dave Chinner33177f052013-12-12 16:34:36 +11003396
3397
Dave Chinner0937e0f2011-09-18 20:40:57 +00003398 nullfb = *ap->firstblock == NULLFSBLOCK;
3399 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003400 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003401 if (xfs_alloc_is_userdata(ap->datatype) &&
3402 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003403 ag = xfs_filestream_lookup_ag(ap->ip);
3404 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003405 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003406 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003407 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003408 }
3409 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003410 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003411
3412 xfs_bmap_adjacent(ap);
3413
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003415 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 * it's in the right allocation group.
3417 */
Dave Chinner3a756672011-09-18 20:40:58 +00003418 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 ;
3420 else
Dave Chinner3a756672011-09-18 20:40:58 +00003421 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 * Normal allocation, done through xfs_alloc_vextent.
3424 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003425 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003426 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003427 args.tp = ap->tp;
3428 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003429 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003430 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003431
3432 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003433 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003434 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003435 blen = 0;
3436 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003437 /*
3438 * Search for an allocation group with a single extent large
3439 * enough for the request. If one isn't found, then adjust
3440 * the minimum allocation size to the largest space found.
3441 */
Dave Chinner292378e2016-09-26 08:21:28 +10003442 if (xfs_alloc_is_userdata(ap->datatype) &&
3443 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003444 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3445 else
3446 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003447 if (error)
3448 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003449 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003450 if (xfs_inode_is_filestream(ap->ip))
3451 args.type = XFS_ALLOCTYPE_FIRST_AG;
3452 else
3453 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003454 args.total = args.minlen = ap->minlen;
3455 } else {
3456 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3457 args.total = ap->total;
3458 args.minlen = ap->minlen;
3459 }
David Chinner957d0eb2007-06-18 16:50:37 +10003460 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003461 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003462 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003463 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003464 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003465 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003466 args.prod = 1;
3467 args.mod = 0;
3468 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003469 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003470 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003471 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3472 }
3473 /*
3474 * If we are not low on available data blocks, and the
3475 * underlying logical volume manager is a stripe, and
3476 * the file offset is zero then try to allocate data
3477 * blocks on stripe unit boundary.
3478 * NOTE: ap->aeof is only set if the allocation length
3479 * is >= the stripe unit and the allocation offset is
3480 * at the end of file.
3481 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003482 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003483 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003484 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003485 atype = args.type;
3486 isaligned = 1;
3487 /*
3488 * Adjust for alignment
3489 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003490 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003491 args.minlen = blen - args.alignment;
3492 args.minalignslop = 0;
3493 } else {
3494 /*
3495 * First try an exact bno allocation.
3496 * If it fails then do a near or start bno
3497 * allocation with alignment turned on.
3498 */
3499 atype = args.type;
3500 tryagain = 1;
3501 args.type = XFS_ALLOCTYPE_THIS_BNO;
3502 args.alignment = 1;
3503 /*
3504 * Compute the minlen+alignment for the
3505 * next case. Set slop so that the value
3506 * of minlen+alignment+slop doesn't go up
3507 * between the calls.
3508 */
Dave Chinner33177f052013-12-12 16:34:36 +11003509 if (blen > stripe_align && blen <= args.maxlen)
3510 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003511 else
3512 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003513 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003514 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003515 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003516 args.minlen - 1;
3517 else
3518 args.minalignslop = 0;
3519 }
3520 } else {
3521 args.alignment = 1;
3522 args.minalignslop = 0;
3523 }
3524 args.minleft = ap->minleft;
3525 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003526 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003527 args.datatype = ap->datatype;
3528 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003529 args.ip = ap->ip;
3530
3531 error = xfs_alloc_vextent(&args);
3532 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003533 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003534
Nathan Scotta365bdd2006-03-14 13:34:16 +11003535 if (tryagain && args.fsbno == NULLFSBLOCK) {
3536 /*
3537 * Exact allocation failed. Now try with alignment
3538 * turned on.
3539 */
3540 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003541 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003542 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003543 args.minlen = nextminlen;
3544 args.minalignslop = 0;
3545 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 if ((error = xfs_alloc_vextent(&args)))
3547 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003548 }
3549 if (isaligned && args.fsbno == NULLFSBLOCK) {
3550 /*
3551 * allocation failed, so turn off alignment and
3552 * try again.
3553 */
3554 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003555 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003556 args.alignment = 0;
3557 if ((error = xfs_alloc_vextent(&args)))
3558 return error;
3559 }
3560 if (args.fsbno == NULLFSBLOCK && nullfb &&
3561 args.minlen > ap->minlen) {
3562 args.minlen = ap->minlen;
3563 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003564 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003565 if ((error = xfs_alloc_vextent(&args)))
3566 return error;
3567 }
3568 if (args.fsbno == NULLFSBLOCK && nullfb) {
3569 args.fsbno = 0;
3570 args.type = XFS_ALLOCTYPE_FIRST_AG;
3571 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003572 if ((error = xfs_alloc_vextent(&args)))
3573 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003574 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003575 }
3576 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003577 /*
3578 * check the allocation happened at the same or higher AG than
3579 * the first block that was allocated.
3580 */
3581 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003582 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3583 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003584
Dave Chinner3a756672011-09-18 20:40:58 +00003585 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003586 if (*ap->firstblock == NULLFSBLOCK)
3587 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003588 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003589 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003590 if (!(ap->flags & XFS_BMAPI_COWFORK))
3591 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003592 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3593 if (ap->wasdel)
3594 ap->ip->i_delayed_blks -= args.len;
3595 /*
3596 * Adjust the disk quota also. This was reserved
3597 * earlier.
3598 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003599 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003600 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3601 XFS_TRANS_DQ_BCOUNT,
3602 (long) args.len);
3603 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003604 ap->blkno = NULLFSBLOCK;
3605 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 }
3607 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003608}
3609
3610/*
3611 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3612 * It figures out where to ask the underlying allocator to put the new extent.
3613 */
3614STATIC int
3615xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003616 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003617{
Dave Chinner292378e2016-09-26 08:21:28 +10003618 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3619 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003620 return xfs_bmap_rtalloc(ap);
3621 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622}
3623
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003624/* Trim extent to fit a logical block range. */
3625void
3626xfs_trim_extent(
3627 struct xfs_bmbt_irec *irec,
3628 xfs_fileoff_t bno,
3629 xfs_filblks_t len)
3630{
3631 xfs_fileoff_t distance;
3632 xfs_fileoff_t end = bno + len;
3633
3634 if (irec->br_startoff + irec->br_blockcount <= bno ||
3635 irec->br_startoff >= end) {
3636 irec->br_blockcount = 0;
3637 return;
3638 }
3639
3640 if (irec->br_startoff < bno) {
3641 distance = bno - irec->br_startoff;
3642 if (isnullstartblock(irec->br_startblock))
3643 irec->br_startblock = DELAYSTARTBLOCK;
3644 if (irec->br_startblock != DELAYSTARTBLOCK &&
3645 irec->br_startblock != HOLESTARTBLOCK)
3646 irec->br_startblock += distance;
3647 irec->br_startoff += distance;
3648 irec->br_blockcount -= distance;
3649 }
3650
3651 if (end < irec->br_startoff + irec->br_blockcount) {
3652 distance = irec->br_startoff + irec->br_blockcount - end;
3653 irec->br_blockcount -= distance;
3654 }
3655}
3656
Brian Foster40214d12017-10-13 09:47:46 -07003657/* trim extent to within eof */
3658void
3659xfs_trim_extent_eof(
3660 struct xfs_bmbt_irec *irec,
3661 struct xfs_inode *ip)
3662
3663{
3664 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3665 i_size_read(VFS_I(ip))));
3666}
3667
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003669 * Trim the returned map to the required bounds
3670 */
3671STATIC void
3672xfs_bmapi_trim_map(
3673 struct xfs_bmbt_irec *mval,
3674 struct xfs_bmbt_irec *got,
3675 xfs_fileoff_t *bno,
3676 xfs_filblks_t len,
3677 xfs_fileoff_t obno,
3678 xfs_fileoff_t end,
3679 int n,
3680 int flags)
3681{
3682 if ((flags & XFS_BMAPI_ENTIRE) ||
3683 got->br_startoff + got->br_blockcount <= obno) {
3684 *mval = *got;
3685 if (isnullstartblock(got->br_startblock))
3686 mval->br_startblock = DELAYSTARTBLOCK;
3687 return;
3688 }
3689
3690 if (obno > *bno)
3691 *bno = obno;
3692 ASSERT((*bno >= obno) || (n == 0));
3693 ASSERT(*bno < end);
3694 mval->br_startoff = *bno;
3695 if (isnullstartblock(got->br_startblock))
3696 mval->br_startblock = DELAYSTARTBLOCK;
3697 else
3698 mval->br_startblock = got->br_startblock +
3699 (*bno - got->br_startoff);
3700 /*
3701 * Return the minimum of what we got and what we asked for for
3702 * the length. We can use the len variable here because it is
3703 * modified below and we could have been there before coming
3704 * here if the first part of the allocation didn't overlap what
3705 * was asked for.
3706 */
3707 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3708 got->br_blockcount - (*bno - got->br_startoff));
3709 mval->br_state = got->br_state;
3710 ASSERT(mval->br_blockcount <= len);
3711 return;
3712}
3713
3714/*
3715 * Update and validate the extent map to return
3716 */
3717STATIC void
3718xfs_bmapi_update_map(
3719 struct xfs_bmbt_irec **map,
3720 xfs_fileoff_t *bno,
3721 xfs_filblks_t *len,
3722 xfs_fileoff_t obno,
3723 xfs_fileoff_t end,
3724 int *n,
3725 int flags)
3726{
3727 xfs_bmbt_irec_t *mval = *map;
3728
3729 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3730 ((mval->br_startoff + mval->br_blockcount) <= end));
3731 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3732 (mval->br_startoff < obno));
3733
3734 *bno = mval->br_startoff + mval->br_blockcount;
3735 *len = end - *bno;
3736 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3737 /* update previous map with new information */
3738 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3739 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3740 ASSERT(mval->br_state == mval[-1].br_state);
3741 mval[-1].br_blockcount = mval->br_blockcount;
3742 mval[-1].br_state = mval->br_state;
3743 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3744 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3745 mval[-1].br_startblock != HOLESTARTBLOCK &&
3746 mval->br_startblock == mval[-1].br_startblock +
3747 mval[-1].br_blockcount &&
3748 ((flags & XFS_BMAPI_IGSTATE) ||
3749 mval[-1].br_state == mval->br_state)) {
3750 ASSERT(mval->br_startoff ==
3751 mval[-1].br_startoff + mval[-1].br_blockcount);
3752 mval[-1].br_blockcount += mval->br_blockcount;
3753 } else if (*n > 0 &&
3754 mval->br_startblock == DELAYSTARTBLOCK &&
3755 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3756 mval->br_startoff ==
3757 mval[-1].br_startoff + mval[-1].br_blockcount) {
3758 mval[-1].br_blockcount += mval->br_blockcount;
3759 mval[-1].br_state = mval->br_state;
3760 } else if (!((*n == 0) &&
3761 ((mval->br_startoff + mval->br_blockcount) <=
3762 obno))) {
3763 mval++;
3764 (*n)++;
3765 }
3766 *map = mval;
3767}
3768
3769/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003770 * Map file blocks to filesystem blocks without allocation.
3771 */
3772int
3773xfs_bmapi_read(
3774 struct xfs_inode *ip,
3775 xfs_fileoff_t bno,
3776 xfs_filblks_t len,
3777 struct xfs_bmbt_irec *mval,
3778 int *nmap,
3779 int flags)
3780{
3781 struct xfs_mount *mp = ip->i_mount;
3782 struct xfs_ifork *ifp;
3783 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003784 xfs_fileoff_t obno;
3785 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003786 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003787 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003788 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003789 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003790 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003791
3792 ASSERT(*nmap >= 1);
3793 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003794 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003795 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003796
3797 if (unlikely(XFS_TEST_ERROR(
3798 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3799 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003800 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003801 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003802 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003803 }
3804
3805 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003806 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003807
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003808 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003809
3810 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003811
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003812 /* No CoW fork? Return a hole. */
3813 if (whichfork == XFS_COW_FORK && !ifp) {
3814 mval->br_startoff = bno;
3815 mval->br_startblock = HOLESTARTBLOCK;
3816 mval->br_blockcount = len;
3817 mval->br_state = XFS_EXT_NORM;
3818 *nmap = 1;
3819 return 0;
3820 }
3821
Dave Chinner5c8ed202011-09-18 20:40:45 +00003822 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3823 error = xfs_iread_extents(NULL, ip, whichfork);
3824 if (error)
3825 return error;
3826 }
3827
Christoph Hellwig334f3422016-11-24 11:39:43 +11003828 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
3829 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003830 end = bno + len;
3831 obno = bno;
3832
3833 while (bno < end && n < *nmap) {
3834 /* Reading past eof, act as though there's a hole up to end. */
3835 if (eof)
3836 got.br_startoff = end;
3837 if (got.br_startoff > bno) {
3838 /* Reading in a hole. */
3839 mval->br_startoff = bno;
3840 mval->br_startblock = HOLESTARTBLOCK;
3841 mval->br_blockcount =
3842 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3843 mval->br_state = XFS_EXT_NORM;
3844 bno += mval->br_blockcount;
3845 len -= mval->br_blockcount;
3846 mval++;
3847 n++;
3848 continue;
3849 }
3850
3851 /* set up the extent map to return. */
3852 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3853 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3854
3855 /* If we're done, stop now. */
3856 if (bno >= end || n >= *nmap)
3857 break;
3858
3859 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11003860 if (!xfs_iext_get_extent(ifp, ++idx, &got))
3861 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003862 }
3863 *nmap = n;
3864 return 0;
3865}
3866
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003867/*
3868 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3869 * global pool and the extent inserted into the inode in-core extent tree.
3870 *
3871 * On entry, got refers to the first extent beyond the offset of the extent to
3872 * allocate or eof is specified if no such extent exists. On return, got refers
3873 * to the extent record that was inserted to the inode fork.
3874 *
3875 * Note that the allocated extent may have been merged with contiguous extents
3876 * during insertion into the inode fork. Thus, got does not reflect the current
3877 * state of the inode fork on return. If necessary, the caller can use lastx to
3878 * look up the updated record in the inode fork.
3879 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003880int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003881xfs_bmapi_reserve_delalloc(
3882 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003883 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003884 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003885 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003886 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003887 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003888 xfs_extnum_t *lastx,
3889 int eof)
3890{
3891 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003892 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003893 xfs_extlen_t alen;
3894 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003895 char rt = XFS_IS_REALTIME_INODE(ip);
3896 xfs_extlen_t extsz;
3897 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003898 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003899
Brian Foster974ae922016-11-28 14:57:42 +11003900 /*
3901 * Cap the alloc length. Keep track of prealloc so we know whether to
3902 * tag the inode before we return.
3903 */
3904 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003905 if (!eof)
3906 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003907 if (prealloc && alen >= len)
3908 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003909
3910 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003911 if (whichfork == XFS_COW_FORK)
3912 extsz = xfs_get_cowextsz_hint(ip);
3913 else
3914 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003915 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003916 struct xfs_bmbt_irec prev;
3917
3918 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
3919 prev.br_startoff = NULLFILEOFF;
3920
3921 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003922 1, 0, &aoff, &alen);
3923 ASSERT(!error);
3924 }
3925
3926 if (rt)
3927 extsz = alen / mp->m_sb.sb_rextsize;
3928
3929 /*
3930 * Make a transaction-less quota reservation for delayed allocation
3931 * blocks. This number gets adjusted later. We return if we haven't
3932 * allocated blocks already inside this loop.
3933 */
3934 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
3935 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
3936 if (error)
3937 return error;
3938
3939 /*
3940 * Split changing sb for alen and indlen since they could be coming
3941 * from different places.
3942 */
3943 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3944 ASSERT(indlen > 0);
3945
3946 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11003947 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003948 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11003949 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003950 }
3951
3952 if (error)
3953 goto out_unreserve_quota;
3954
Dave Chinner0d485ad2015-02-23 21:22:03 +11003955 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003956 if (error)
3957 goto out_unreserve_blocks;
3958
3959
3960 ip->i_delayed_blks += alen;
3961
3962 got->br_startoff = aoff;
3963 got->br_startblock = nullstartblock(indlen);
3964 got->br_blockcount = alen;
3965 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003966
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003967 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003968
Brian Foster974ae922016-11-28 14:57:42 +11003969 /*
3970 * Tag the inode if blocks were preallocated. Note that COW fork
3971 * preallocation can occur at the start or end of the extent, even when
3972 * prealloc == 0, so we must also check the aligned offset and length.
3973 */
3974 if (whichfork == XFS_DATA_FORK && prealloc)
3975 xfs_inode_set_eofblocks_tag(ip);
3976 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
3977 xfs_inode_set_cowblocks_tag(ip);
3978
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003979 return 0;
3980
3981out_unreserve_blocks:
3982 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11003983 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003984 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11003985 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003986out_unreserve_quota:
3987 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10003988 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003989 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
3990 return error;
3991}
3992
Dave Chinnercf11da92014-07-15 07:08:24 +10003993static int
3994xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10003995 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003996{
3997 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003998 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003999 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004000 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004001 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004002
Dave Chinnera99ebf42011-12-01 11:24:20 +00004003 ASSERT(bma->length > 0);
4004
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004005 /*
4006 * For the wasdelay case, we could also just allocate the stuff asked
4007 * for in this bmap call but that wouldn't be as good.
4008 */
4009 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004010 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4011 bma->offset = bma->got.br_startoff;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004012 if (bma->idx)
4013 xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004014 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004015 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004016 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004017 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004018 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004019 }
4020
4021 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004022 * Set the data type being allocated. For the data fork, the first data
4023 * in the file is treated differently to all other allocations. For the
4024 * attribute fork, we only need to ensure the allocated range is not on
4025 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004026 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004027 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004028 bma->datatype = XFS_ALLOC_NOBUSY;
4029 if (whichfork == XFS_DATA_FORK) {
4030 if (bma->offset == 0)
4031 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4032 else
4033 bma->datatype |= XFS_ALLOC_USERDATA;
4034 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004035 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004036 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004037 }
4038
Dave Chinnere04426b2012-10-05 11:06:59 +10004039 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004040
4041 /*
4042 * Only want to do the alignment at the eof if it is userdata and
4043 * allocation length is larger than a stripe unit.
4044 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004045 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004046 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004047 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004048 if (error)
4049 return error;
4050 }
4051
4052 error = xfs_bmap_alloc(bma);
4053 if (error)
4054 return error;
4055
Dave Chinner29c8d172011-09-18 20:41:00 +00004056 if (bma->cur)
4057 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004058 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004059 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004060 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4061 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4062 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004063 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004064 }
4065 /*
4066 * Bump the number of extents we've allocated
4067 * in this call.
4068 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004069 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004070
Dave Chinner29c8d172011-09-18 20:41:00 +00004071 if (bma->cur)
4072 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004073 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4074
Dave Chinner963c30c2011-09-18 20:40:59 +00004075 bma->got.br_startoff = bma->offset;
4076 bma->got.br_startblock = bma->blkno;
4077 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004078 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004079
4080 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004081 * In the data fork, a wasdelay extent has been initialized, so
4082 * shouldn't be flagged as unwritten.
4083 *
4084 * For the cow fork, however, we convert delalloc reservations
4085 * (extents allocated for speculative preallocation) to
4086 * allocated unwritten extents, and only convert the unwritten
4087 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004088 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004089 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4090 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004091 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004092 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004093
Christoph Hellwigc6534242011-09-18 20:41:05 +00004094 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004095 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004096 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004097 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4098 whichfork, &bma->idx, &bma->cur, &bma->got,
4099 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004100
Christoph Hellwigc315c902011-09-18 20:41:02 +00004101 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004102 if (error)
4103 return error;
4104
4105 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004106 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4107 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4108 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004109 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004110 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004111
Dave Chinner963c30c2011-09-18 20:40:59 +00004112 ASSERT(bma->got.br_startoff <= bma->offset);
4113 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4114 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004115 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4116 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004117 return 0;
4118}
4119
Dave Chinnerb447fe52011-09-18 20:40:51 +00004120STATIC int
4121xfs_bmapi_convert_unwritten(
4122 struct xfs_bmalloca *bma,
4123 struct xfs_bmbt_irec *mval,
4124 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004125 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004126{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004127 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004128 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004129 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004130 int error;
4131
Dave Chinnerb447fe52011-09-18 20:40:51 +00004132 /* check if we need to do unwritten->real conversion */
4133 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4134 (flags & XFS_BMAPI_PREALLOC))
4135 return 0;
4136
4137 /* check if we need to do real->unwritten conversion */
4138 if (mval->br_state == XFS_EXT_NORM &&
4139 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4140 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4141 return 0;
4142
4143 /*
4144 * Modify (by adding) the state flag, if writing.
4145 */
4146 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004147 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4148 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004149 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004150 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004151 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004152 }
4153 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4154 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4155
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004156 /*
4157 * Before insertion into the bmbt, zero the range being converted
4158 * if required.
4159 */
4160 if (flags & XFS_BMAPI_ZERO) {
4161 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4162 mval->br_blockcount);
4163 if (error)
4164 return error;
4165 }
4166
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004167 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4168 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004169 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004170 /*
4171 * Log the inode core unconditionally in the unwritten extent conversion
4172 * path because the conversion might not have done so (e.g., if the
4173 * extent count hasn't changed). We need to make sure the inode is dirty
4174 * in the transaction for the sake of fsync(), even if nothing has
4175 * changed, because fsync() will not force the log for this transaction
4176 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004177 *
4178 * Note: If we're only converting cow fork extents, there aren't
4179 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004180 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004181 if (whichfork != XFS_COW_FORK)
4182 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004183 if (error)
4184 return error;
4185
4186 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004187 * Update our extent pointer, given that
4188 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4189 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004190 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004191 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004192
4193 /*
4194 * We may have combined previously unwritten space with written space,
4195 * so generate another request.
4196 */
4197 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004198 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004199 return 0;
4200}
4201
Christoph Hellwig44032802011-09-18 20:40:48 +00004202/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004203 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4204 * extent state if necessary. Details behaviour is controlled by the flags
4205 * parameter. Only allocates blocks from a single allocation group, to avoid
4206 * locking problems.
4207 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 * The returned value in "firstblock" from the first call in a transaction
4209 * must be remembered and presented to subsequent calls in "firstblock".
4210 * An upper bound for the number of blocks to be allocated is supplied to
4211 * the first call in "total"; if no allocation group has that many free
4212 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4213 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004214int
4215xfs_bmapi_write(
4216 struct xfs_trans *tp, /* transaction pointer */
4217 struct xfs_inode *ip, /* incore inode */
4218 xfs_fileoff_t bno, /* starting file offs. mapped */
4219 xfs_filblks_t len, /* length to map in file */
4220 int flags, /* XFS_BMAPI_... */
4221 xfs_fsblock_t *firstblock, /* first allocated block
4222 controls a.g. for allocs */
4223 xfs_extlen_t total, /* total blocks needed */
4224 struct xfs_bmbt_irec *mval, /* output: map values */
4225 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004226 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004228 struct xfs_mount *mp = ip->i_mount;
4229 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004230 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004231 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004232 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004233 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004234 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004235 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004236 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004239 xfs_fileoff_t orig_bno; /* original block number value */
4240 int orig_flags; /* original flags arg value */
4241 xfs_filblks_t orig_len; /* original value of len arg */
4242 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4243 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244
4245 orig_bno = bno;
4246 orig_len = len;
4247 orig_flags = flags;
4248 orig_mval = mval;
4249 orig_nmap = *nmap;
4250#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004251 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004254 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4255 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004256 ASSERT(tp != NULL ||
4257 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4258 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004259 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004260 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004261 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004262 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004263
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004264 /* zeroing is for currently only for data extents, not metadata */
4265 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4266 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4267 /*
4268 * we can allocate unwritten extents or pre-zero allocated blocks,
4269 * but it makes no sense to do both at once. This would result in
4270 * zeroing the unwritten extent twice, but it still being an
4271 * unwritten extent....
4272 */
4273 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4274 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 if (unlikely(XFS_TEST_ERROR(
4277 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004278 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004279 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004280 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004281 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004283
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004285 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004286
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004288
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004289 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004290
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004291 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004293 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004295 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004296 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004297 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004298 }
4299
4300 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4301 error = xfs_iread_extents(tp, ip, whichfork);
4302 if (error)
4303 goto error0;
4304 }
4305
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 n = 0;
4307 end = bno + len;
4308 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004309
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004310 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4311 eof = true;
4312 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4313 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004314 bma.tp = tp;
4315 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004316 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004317 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004318 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004319 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004320
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004322 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004323
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004324 /* in hole or beyoned EOF? */
4325 if (eof || bma.got.br_startoff > bno) {
4326 if (flags & XFS_BMAPI_DELALLOC) {
4327 /*
4328 * For the COW fork we can reasonably get a
4329 * request for converting an extent that races
4330 * with other threads already having converted
4331 * part of it, as there converting COW to
4332 * regular blocks is not protected using the
4333 * IOLOCK.
4334 */
4335 ASSERT(flags & XFS_BMAPI_COWFORK);
4336 if (!(flags & XFS_BMAPI_COWFORK)) {
4337 error = -EIO;
4338 goto error0;
4339 }
4340
4341 if (eof || bno >= end)
4342 break;
4343 } else {
4344 need_alloc = true;
4345 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004346 } else if (isnullstartblock(bma.got.br_startblock)) {
4347 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004348 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004349
4350 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 * First, deal with the hole before the allocated space
4352 * that we found, if any.
4353 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004354 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004355 bma.eof = eof;
4356 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4357 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004358 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004359 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004360
Dave Chinnera99ebf42011-12-01 11:24:20 +00004361 /*
4362 * There's a 32/64 bit type mismatch between the
4363 * allocation length request (which can be 64 bits in
4364 * length) and the bma length request, which is
4365 * xfs_extlen_t and therefore 32 bits. Hence we have to
4366 * check for 32-bit overflows and handle them here.
4367 */
4368 if (len > (xfs_filblks_t)MAXEXTLEN)
4369 bma.length = MAXEXTLEN;
4370 else
4371 bma.length = len;
4372
4373 ASSERT(len > 0);
4374 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004375 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 if (error)
4377 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004378 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004379 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004380
4381 /*
4382 * If this is a CoW allocation, record the data in
4383 * the refcount btree for orphan recovery.
4384 */
4385 if (whichfork == XFS_COW_FORK) {
4386 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4387 bma.blkno, bma.length);
4388 if (error)
4389 goto error0;
4390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004392
Dave Chinneraef9a892011-09-18 20:40:44 +00004393 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004394 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4395 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
Dave Chinnerb447fe52011-09-18 20:40:51 +00004397 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004398 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004399 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004400 continue;
4401 if (error)
4402 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403
Dave Chinneraef9a892011-09-18 20:40:44 +00004404 /* update the extent map to return */
4405 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4406
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 /*
4408 * If we're done, stop now. Stop when we've allocated
4409 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4410 * the transaction may get too big.
4411 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004412 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004414
4415 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004416 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004417 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4418 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004421
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 /*
4423 * Transform from btree to extents, give it cur.
4424 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004425 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004426 int tmp_logflags = 0;
4427
Dave Chinner29c8d172011-09-18 20:41:00 +00004428 ASSERT(bma.cur);
4429 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004431 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 if (error)
4433 goto error0;
4434 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004435
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004437 XFS_IFORK_NEXTENTS(ip, whichfork) >
4438 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440error0:
4441 /*
4442 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004443 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004445 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004447 bma.logflags &= ~xfs_ilog_fext(whichfork);
4448 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004450 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 /*
4452 * Log whatever the flags say, even if error. Otherwise we might miss
4453 * detecting a case where the data is changed, there's an error,
4454 * and it's not logged so we don't shutdown when we should.
4455 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004456 if (bma.logflags)
4457 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004458
Dave Chinner29c8d172011-09-18 20:41:00 +00004459 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 if (!error) {
4461 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004462 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004464 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004465 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004467 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4469 }
4470 if (!error)
4471 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4472 orig_nmap, *nmap);
4473 return error;
4474}
4475
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004476static int
4477xfs_bmapi_remap(
4478 struct xfs_trans *tp,
4479 struct xfs_inode *ip,
4480 xfs_fileoff_t bno,
4481 xfs_filblks_t len,
4482 xfs_fsblock_t startblock,
4483 struct xfs_defer_ops *dfops)
4484{
4485 struct xfs_mount *mp = ip->i_mount;
4486 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4487 struct xfs_btree_cur *cur = NULL;
4488 xfs_fsblock_t firstblock = NULLFSBLOCK;
4489 struct xfs_bmbt_irec got;
4490 xfs_extnum_t idx;
4491 int logflags = 0, error;
4492
4493 ASSERT(len > 0);
4494 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4495 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4496
4497 if (unlikely(XFS_TEST_ERROR(
4498 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4499 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004500 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004501 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4502 return -EFSCORRUPTED;
4503 }
4504
4505 if (XFS_FORCED_SHUTDOWN(mp))
4506 return -EIO;
4507
4508 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4509 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4510 if (error)
4511 return error;
4512 }
4513
4514 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4515 /* make sure we only reflink into a hole. */
4516 ASSERT(got.br_startoff > bno);
4517 ASSERT(got.br_startoff - bno >= len);
4518 }
4519
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004520 ip->i_d.di_nblocks += len;
4521 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004522
4523 if (ifp->if_flags & XFS_IFBROOT) {
4524 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4525 cur->bc_private.b.firstblock = firstblock;
4526 cur->bc_private.b.dfops = dfops;
4527 cur->bc_private.b.flags = 0;
4528 }
4529
4530 got.br_startoff = bno;
4531 got.br_startblock = startblock;
4532 got.br_blockcount = len;
4533 got.br_state = XFS_EXT_NORM;
4534
4535 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4536 &got, &firstblock, dfops, &logflags);
4537 if (error)
4538 goto error0;
4539
4540 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4541 int tmp_logflags = 0;
4542
4543 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4544 &tmp_logflags, XFS_DATA_FORK);
4545 logflags |= tmp_logflags;
4546 }
4547
4548error0:
4549 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4550 logflags &= ~XFS_ILOG_DEXT;
4551 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4552 logflags &= ~XFS_ILOG_DBROOT;
4553
4554 if (logflags)
4555 xfs_trans_log_inode(tp, ip, logflags);
4556 if (cur) {
4557 xfs_btree_del_cursor(cur,
4558 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4559 }
4560 return error;
4561}
4562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004564 * When a delalloc extent is split (e.g., due to a hole punch), the original
4565 * indlen reservation must be shared across the two new extents that are left
4566 * behind.
4567 *
4568 * Given the original reservation and the worst case indlen for the two new
4569 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004570 * reservation fairly across the two new extents. If necessary, steal available
4571 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4572 * ores == 1). The number of stolen blocks is returned. The availability and
4573 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004574 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004575static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004576xfs_bmap_split_indlen(
4577 xfs_filblks_t ores, /* original res. */
4578 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004579 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4580 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004581{
4582 xfs_filblks_t len1 = *indlen1;
4583 xfs_filblks_t len2 = *indlen2;
4584 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004585 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004586 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004587
4588 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004589 * Steal as many blocks as we can to try and satisfy the worst case
4590 * indlen for both new extents.
4591 */
Brian Foster75d65362017-02-13 22:48:30 -08004592 if (ores < nres && avail)
4593 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4594 ores += stolen;
4595
4596 /* nothing else to do if we've satisfied the new reservation */
4597 if (ores >= nres)
4598 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004599
4600 /*
Brian Foster75d65362017-02-13 22:48:30 -08004601 * We can't meet the total required reservation for the two extents.
4602 * Calculate the percent of the overall shortage between both extents
4603 * and apply this percentage to each of the requested indlen values.
4604 * This distributes the shortage fairly and reduces the chances that one
4605 * of the two extents is left with nothing when extents are repeatedly
4606 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004607 */
Brian Foster75d65362017-02-13 22:48:30 -08004608 resfactor = (ores * 100);
4609 do_div(resfactor, nres);
4610 len1 *= resfactor;
4611 do_div(len1, 100);
4612 len2 *= resfactor;
4613 do_div(len2, 100);
4614 ASSERT(len1 + len2 <= ores);
4615 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4616
4617 /*
4618 * Hand out the remainder to each extent. If one of the two reservations
4619 * is zero, we want to make sure that one gets a block first. The loop
4620 * below starts with len1, so hand len2 a block right off the bat if it
4621 * is zero.
4622 */
4623 ores -= (len1 + len2);
4624 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4625 if (ores && !len2 && *indlen2) {
4626 len2++;
4627 ores--;
4628 }
4629 while (ores) {
4630 if (len1 < *indlen1) {
4631 len1++;
4632 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004633 }
Brian Foster75d65362017-02-13 22:48:30 -08004634 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004635 break;
Brian Foster75d65362017-02-13 22:48:30 -08004636 if (len2 < *indlen2) {
4637 len2++;
4638 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004639 }
4640 }
4641
4642 *indlen1 = len1;
4643 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004644
4645 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004646}
4647
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004648int
4649xfs_bmap_del_extent_delay(
4650 struct xfs_inode *ip,
4651 int whichfork,
4652 xfs_extnum_t *idx,
4653 struct xfs_bmbt_irec *got,
4654 struct xfs_bmbt_irec *del)
4655{
4656 struct xfs_mount *mp = ip->i_mount;
4657 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4658 struct xfs_bmbt_irec new;
4659 int64_t da_old, da_new, da_diff = 0;
4660 xfs_fileoff_t del_endoff, got_endoff;
4661 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004662 int state = xfs_bmap_fork_to_state(whichfork);
4663 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004664 bool isrt;
4665
4666 XFS_STATS_INC(mp, xs_del_exlist);
4667
4668 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4669 del_endoff = del->br_startoff + del->br_blockcount;
4670 got_endoff = got->br_startoff + got->br_blockcount;
4671 da_old = startblockval(got->br_startblock);
4672 da_new = 0;
4673
4674 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004675 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004676 ASSERT(del->br_blockcount > 0);
4677 ASSERT(got->br_startoff <= del->br_startoff);
4678 ASSERT(got_endoff >= del_endoff);
4679
4680 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004681 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004682
4683 do_div(rtexts, mp->m_sb.sb_rextsize);
4684 xfs_mod_frextents(mp, rtexts);
4685 }
4686
4687 /*
4688 * Update the inode delalloc counter now and wait to update the
4689 * sb counters as we might have to borrow some blocks for the
4690 * indirect block accounting.
4691 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004692 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4693 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004694 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004695 if (error)
4696 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004697 ip->i_delayed_blks -= del->br_blockcount;
4698
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004699 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004700 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004701 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004702 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004703
Christoph Hellwig0173c682017-10-17 14:16:22 -07004704 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4705 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004706 /*
4707 * Matches the whole extent. Delete the entry.
4708 */
4709 xfs_iext_remove(ip, *idx, 1, state);
4710 --*idx;
4711 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004712 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004713 /*
4714 * Deleting the first part of the extent.
4715 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004716 got->br_startoff = del_endoff;
4717 got->br_blockcount -= del->br_blockcount;
4718 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4719 got->br_blockcount), da_old);
4720 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004721 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004722 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004723 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004724 /*
4725 * Deleting the last part of the extent.
4726 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004727 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4728 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4729 got->br_blockcount), da_old);
4730 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004731 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004732 break;
4733 case 0:
4734 /*
4735 * Deleting the middle of the extent.
4736 *
4737 * Distribute the original indlen reservation across the two new
4738 * extents. Steal blocks from the deleted extent if necessary.
4739 * Stealing blocks simply fudges the fdblocks accounting below.
4740 * Warn if either of the new indlen reservations is zero as this
4741 * can lead to delalloc problems.
4742 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004743 got->br_blockcount = del->br_startoff - got->br_startoff;
4744 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4745
4746 new.br_blockcount = got_endoff - del_endoff;
4747 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4748
4749 WARN_ON_ONCE(!got_indlen || !new_indlen);
4750 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4751 del->br_blockcount);
4752
4753 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004754 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004755
4756 new.br_startoff = del_endoff;
4757 new.br_state = got->br_state;
4758 new.br_startblock = nullstartblock((int)new_indlen);
4759
4760 ++*idx;
4761 xfs_iext_insert(ip, *idx, 1, &new, state);
4762
4763 da_new = got_indlen + new_indlen - stolen;
4764 del->br_blockcount -= stolen;
4765 break;
4766 }
4767
4768 ASSERT(da_old >= da_new);
4769 da_diff = da_old - da_new;
4770 if (!isrt)
4771 da_diff += del->br_blockcount;
4772 if (da_diff)
4773 xfs_mod_fdblocks(mp, da_diff, false);
4774 return error;
4775}
4776
4777void
4778xfs_bmap_del_extent_cow(
4779 struct xfs_inode *ip,
4780 xfs_extnum_t *idx,
4781 struct xfs_bmbt_irec *got,
4782 struct xfs_bmbt_irec *del)
4783{
4784 struct xfs_mount *mp = ip->i_mount;
4785 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4786 struct xfs_bmbt_irec new;
4787 xfs_fileoff_t del_endoff, got_endoff;
4788 int state = BMAP_COWFORK;
4789
4790 XFS_STATS_INC(mp, xs_del_exlist);
4791
4792 del_endoff = del->br_startoff + del->br_blockcount;
4793 got_endoff = got->br_startoff + got->br_blockcount;
4794
4795 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004796 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004797 ASSERT(del->br_blockcount > 0);
4798 ASSERT(got->br_startoff <= del->br_startoff);
4799 ASSERT(got_endoff >= del_endoff);
4800 ASSERT(!isnullstartblock(got->br_startblock));
4801
4802 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004803 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004804 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004805 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004806
Christoph Hellwig0173c682017-10-17 14:16:22 -07004807 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4808 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004809 /*
4810 * Matches the whole extent. Delete the entry.
4811 */
4812 xfs_iext_remove(ip, *idx, 1, state);
4813 --*idx;
4814 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004815 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004816 /*
4817 * Deleting the first part of the extent.
4818 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004819 got->br_startoff = del_endoff;
4820 got->br_blockcount -= del->br_blockcount;
4821 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004822 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004823 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004824 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004825 /*
4826 * Deleting the last part of the extent.
4827 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004828 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004829 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004830 break;
4831 case 0:
4832 /*
4833 * Deleting the middle of the extent.
4834 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004835 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004836 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004837
4838 new.br_startoff = del_endoff;
4839 new.br_blockcount = got_endoff - del_endoff;
4840 new.br_state = got->br_state;
4841 new.br_startblock = del->br_startblock + del->br_blockcount;
4842
4843 ++*idx;
4844 xfs_iext_insert(ip, *idx, 1, &new, state);
4845 break;
4846 }
4847}
4848
Brian Fostera9bd24a2016-03-15 11:42:46 +11004849/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004850 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004851 * after removing space.
Dave Chinner9e5987a2013-02-25 12:31:26 +11004852 */
4853STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004854xfs_bmap_del_extent_real(
Dave Chinner9e5987a2013-02-25 12:31:26 +11004855 xfs_inode_t *ip, /* incore inode pointer */
4856 xfs_trans_t *tp, /* current transaction pointer */
4857 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004858 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004859 xfs_btree_cur_t *cur, /* if null, not a btree */
4860 xfs_bmbt_irec_t *del, /* data to remove from extents */
4861 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004862 int whichfork, /* data or attr fork */
4863 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004864{
Dave Chinner9e5987a2013-02-25 12:31:26 +11004865 xfs_fsblock_t del_endblock=0; /* first block past del */
4866 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004867 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004868 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004869 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004870 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004871 xfs_fileoff_t got_endoff; /* first offset past got */
4872 int i; /* temp state */
4873 xfs_ifork_t *ifp; /* inode fork pointer */
4874 xfs_mount_t *mp; /* mount structure */
4875 xfs_filblks_t nblks; /* quota/sb block count */
4876 xfs_bmbt_irec_t new; /* new record to be inserted */
4877 /* REFERENCED */
4878 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004879 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004880 struct xfs_bmbt_irec old;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004881
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004882 mp = ip->i_mount;
4883 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004884
Dave Chinner9e5987a2013-02-25 12:31:26 +11004885 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11004886 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004887 ASSERT(del->br_blockcount > 0);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004888 xfs_iext_get_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004889 ASSERT(got.br_startoff <= del->br_startoff);
4890 del_endoff = del->br_startoff + del->br_blockcount;
4891 got_endoff = got.br_startoff + got.br_blockcount;
4892 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004893 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004894 qfield = 0;
4895 error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004896
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004897 /*
4898 * If it's the case where the directory code is running with no block
4899 * reservation, and the deleted block is in the middle of its extent,
4900 * and the resulting insert of an extent would cause transformation to
4901 * btree format, then reject it. The calling code will then swap blocks
4902 * around instead. We have to do this now, rather than waiting for the
4903 * conversion to btree format, since the transaction will be dirty then.
4904 */
4905 if (tp->t_blk_res == 0 &&
4906 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4907 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4908 XFS_IFORK_MAXEXT(ip, whichfork) &&
4909 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4910 return -ENOSPC;
4911
4912 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004913 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4914 xfs_fsblock_t bno;
4915 xfs_filblks_t len;
4916
4917 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
4918 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
4919 bno = del->br_startblock;
4920 len = del->br_blockcount;
4921 do_div(bno, mp->m_sb.sb_rextsize);
4922 do_div(len, mp->m_sb.sb_rextsize);
4923 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4924 if (error)
4925 goto done;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004926 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004927 nblks = len * mp->m_sb.sb_rextsize;
4928 qfield = XFS_TRANS_DQ_RTBCOUNT;
4929 } else {
4930 do_fx = 1;
4931 nblks = del->br_blockcount;
4932 qfield = XFS_TRANS_DQ_BCOUNT;
4933 }
4934
4935 del_endblock = del->br_startblock + del->br_blockcount;
4936 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07004937 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004938 if (error)
4939 goto done;
4940 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004941 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004942
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004943 if (got.br_startoff == del->br_startoff)
4944 state |= BMAP_LEFT_FILLING;
4945 if (got_endoff == del_endoff)
4946 state |= BMAP_RIGHT_FILLING;
4947
4948 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4949 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004950 /*
4951 * Matches the whole extent. Delete the entry.
4952 */
Christoph Hellwig35e62da2017-10-19 11:04:43 -07004953 xfs_iext_remove(ip, *idx, 1, state);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004954 --*idx;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004955
4956 XFS_IFORK_NEXT_SET(ip, whichfork,
4957 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4958 flags |= XFS_ILOG_CORE;
4959 if (!cur) {
4960 flags |= xfs_ilog_fext(whichfork);
4961 break;
4962 }
4963 if ((error = xfs_btree_delete(cur, &i)))
4964 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004965 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004966 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004967 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004968 /*
4969 * Deleting the first part of the extent.
4970 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004971 got.br_startoff = del_endoff;
4972 got.br_startblock = del_endblock;
4973 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004974 xfs_iext_update_extent(ip, state, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004975 if (!cur) {
4976 flags |= xfs_ilog_fext(whichfork);
4977 break;
4978 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004979 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004980 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004981 goto done;
4982 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004983 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004984 /*
4985 * Deleting the last part of the extent.
4986 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004987 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004988 xfs_iext_update_extent(ip, state, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004989 if (!cur) {
4990 flags |= xfs_ilog_fext(whichfork);
4991 break;
4992 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004993 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004994 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004995 goto done;
4996 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004997 case 0:
4998 /*
4999 * Deleting the middle of the extent.
5000 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005001 old = got;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005002
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005003 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005004 xfs_iext_update_extent(ip, state, *idx, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005005
Dave Chinner9e5987a2013-02-25 12:31:26 +11005006 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005007 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005008 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005009 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005010
Christoph Hellwige1d75532017-10-17 14:16:21 -07005011 flags |= XFS_ILOG_CORE;
5012 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005013 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005014 if (error)
5015 goto done;
5016 error = xfs_btree_increment(cur, 0, &i);
5017 if (error)
5018 goto done;
5019 cur->bc_rec.b = new;
5020 error = xfs_btree_insert(cur, &i);
5021 if (error && error != -ENOSPC)
5022 goto done;
5023 /*
5024 * If get no-space back from btree insert, it tried a
5025 * split, and we have a zero block reservation. Fix up
5026 * our state and return the error.
5027 */
5028 if (error == -ENOSPC) {
5029 /*
5030 * Reset the cursor, don't trust it after any
5031 * insert operation.
5032 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005033 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005034 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005035 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005036 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5037 /*
5038 * Update the btree record back
5039 * to the original value.
5040 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005041 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005042 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005043 goto done;
5044 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005045 * Reset the extent record back
5046 * to the original value.
Dave Chinner9e5987a2013-02-25 12:31:26 +11005047 */
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005048 xfs_iext_update_extent(ip, state, *idx, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005049 flags = 0;
5050 error = -ENOSPC;
5051 goto done;
5052 }
5053 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5054 } else
5055 flags |= xfs_ilog_fext(whichfork);
5056 XFS_IFORK_NEXT_SET(ip, whichfork,
5057 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005058 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5059 ++*idx;
5060 break;
5061 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005062
5063 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005064 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5065 if (error)
5066 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005067
Dave Chinner9e5987a2013-02-25 12:31:26 +11005068 /*
5069 * If we need to, add to list of extents to delete.
5070 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005071 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005072 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5073 error = xfs_refcount_decrease_extent(mp, dfops, del);
5074 if (error)
5075 goto done;
5076 } else
5077 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5078 del->br_blockcount, NULL);
5079 }
5080
Dave Chinner9e5987a2013-02-25 12:31:26 +11005081 /*
5082 * Adjust inode # blocks in the file.
5083 */
5084 if (nblks)
5085 ip->i_d.di_nblocks -= nblks;
5086 /*
5087 * Adjust quota data.
5088 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005089 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005090 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5091
Dave Chinner9e5987a2013-02-25 12:31:26 +11005092done:
5093 *logflagsp = flags;
5094 return error;
5095}
5096
5097/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 * Unmap (remove) blocks from a file.
5099 * If nexts is nonzero then the number of extents to remove is limited to
5100 * that value. If not all extents in the block range can be removed then
5101 * *done is set.
5102 */
5103int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005104__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 xfs_trans_t *tp, /* transaction pointer */
5106 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005107 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005108 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 int flags, /* misc flags */
5110 xfs_extnum_t nexts, /* number of extents max */
5111 xfs_fsblock_t *firstblock, /* first allocated block
5112 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005113 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114{
5115 xfs_btree_cur_t *cur; /* bmap btree cursor */
5116 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 int error; /* error return value */
5118 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005119 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 xfs_ifork_t *ifp; /* inode fork pointer */
5121 int isrt; /* freeing in rt area */
5122 xfs_extnum_t lastx; /* last extent index used */
5123 int logflags; /* transaction logging flags */
5124 xfs_extlen_t mod; /* rt extent offset */
5125 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 int tmp_logflags; /* partial logging flags */
5127 int wasdel; /* was a delayed alloc extent */
5128 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005130 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005131 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005132 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005133 xfs_fileoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005135 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005136
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005137 whichfork = xfs_bmapi_whichfork(flags);
5138 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 ifp = XFS_IFORK_PTR(ip, whichfork);
5140 if (unlikely(
5141 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5142 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5143 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5144 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005145 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 }
5147 mp = ip->i_mount;
5148 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005149 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005150
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005151 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005152 ASSERT(len > 0);
5153 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005154
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005155 /*
5156 * Guesstimate how many blocks we can unmap without running the risk of
5157 * blowing out the transaction with a mix of EFIs and reflink
5158 * adjustments.
5159 */
5160 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5161 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5162 else
5163 max_len = len;
5164
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5166 (error = xfs_iread_extents(tp, ip, whichfork)))
5167 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005168 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005169 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 return 0;
5171 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005172 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005173 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005174 end = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005175
Linus Torvalds1da177e2005-04-16 15:20:36 -07005176 /*
5177 * Check to see if the given block number is past the end of the
5178 * file, back up to the last block if so...
5179 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005180 if (!xfs_iext_lookup_extent(ip, ifp, end, &lastx, &got)) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005181 ASSERT(lastx > 0);
5182 xfs_iext_get_extent(ifp, --lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005183 end = got.br_startoff + got.br_blockcount - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005185
Linus Torvalds1da177e2005-04-16 15:20:36 -07005186 logflags = 0;
5187 if (ifp->if_flags & XFS_IFBROOT) {
5188 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005189 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005191 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005192 cur->bc_private.b.flags = 0;
5193 } else
5194 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005195
5196 if (isrt) {
5197 /*
5198 * Synchronize by locking the bitmap inode.
5199 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005200 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005201 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005202 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5203 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005204 }
5205
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 extno = 0;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005207 while (end != (xfs_fileoff_t)-1 && end >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005208 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005209 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005210 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005211 * Just back up to the previous extent, if so.
5212 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005213 if (got.br_startoff > end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 if (--lastx < 0)
5215 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005216 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 }
5218 /*
5219 * Is the last block of this extent before the range
5220 * we're supposed to delete? If so, we're done.
5221 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005222 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005224 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 break;
5226 /*
5227 * Then deal with the (possibly delayed) allocated space
5228 * we found.
5229 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005231 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005232
5233 /*
5234 * Make sure we don't touch multiple AGF headers out of order
5235 * in a single transaction, as that could cause AB-BA deadlocks.
5236 */
5237 if (!wasdel) {
5238 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5239 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5240 break;
5241 prev_agno = agno;
5242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 if (got.br_startoff < start) {
5244 del.br_startoff = start;
5245 del.br_blockcount -= start - got.br_startoff;
5246 if (!wasdel)
5247 del.br_startblock += start - got.br_startoff;
5248 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005249 if (del.br_startoff + del.br_blockcount > end + 1)
5250 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005251
5252 /* How much can we safely unmap? */
5253 if (max_len < del.br_blockcount) {
5254 del.br_startoff += del.br_blockcount - max_len;
5255 if (!wasdel)
5256 del.br_startblock += del.br_blockcount - max_len;
5257 del.br_blockcount = max_len;
5258 }
5259
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 sum = del.br_startblock + del.br_blockcount;
5261 if (isrt &&
5262 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5263 /*
5264 * Realtime extent not lined up at the end.
5265 * The extent could have been split into written
5266 * and unwritten pieces, or we could just be
5267 * unmapping part of it. But we can't really
5268 * get rid of part of a realtime extent.
5269 */
5270 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005271 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 /*
5273 * This piece is unwritten, or we're not
5274 * using unwritten extents. Skip over it.
5275 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005276 ASSERT(end >= mod);
5277 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 del.br_blockcount : mod;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005279 if (end < got.br_startoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005280 if (--lastx >= 0)
Christoph Hellwige3f0f752017-10-17 14:16:20 -07005281 xfs_iext_get_extent(ifp, lastx,
5282 &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 }
5284 continue;
5285 }
5286 /*
5287 * It's written, turn it unwritten.
5288 * This is better than zeroing it.
5289 */
5290 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005291 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 /*
5293 * If this spans a realtime extent boundary,
5294 * chop it back to the start of the one we end at.
5295 */
5296 if (del.br_blockcount > mod) {
5297 del.br_startoff += del.br_blockcount - mod;
5298 del.br_startblock += del.br_blockcount - mod;
5299 del.br_blockcount = mod;
5300 }
5301 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005302 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005303 whichfork, &lastx, &cur, &del,
5304 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 if (error)
5306 goto error0;
5307 goto nodelete;
5308 }
5309 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5310 /*
5311 * Realtime extent is lined up at the end but not
5312 * at the front. We'll get rid of full extents if
5313 * we can.
5314 */
5315 mod = mp->m_sb.sb_rextsize - mod;
5316 if (del.br_blockcount > mod) {
5317 del.br_blockcount -= mod;
5318 del.br_startoff += mod;
5319 del.br_startblock += mod;
5320 } else if ((del.br_startoff == start &&
5321 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005322 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005323 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 /*
5325 * Can't make it unwritten. There isn't
5326 * a full extent here so just skip it.
5327 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005328 ASSERT(end >= del.br_blockcount);
5329 end -= del.br_blockcount;
5330 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005331 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 continue;
5333 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005334 struct xfs_bmbt_irec prev;
5335
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 /*
5337 * This one is already unwritten.
5338 * It must have a written left neighbor.
5339 * Unwrite the killed part of that one and
5340 * try again.
5341 */
5342 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005343 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005345 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 ASSERT(del.br_startblock ==
5347 prev.br_startblock + prev.br_blockcount);
5348 if (prev.br_startoff < start) {
5349 mod = start - prev.br_startoff;
5350 prev.br_blockcount -= mod;
5351 prev.br_startblock += mod;
5352 prev.br_startoff = start;
5353 }
5354 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005355 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005356 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005357 ip, whichfork, &lastx, &cur,
5358 &prev, firstblock, dfops,
5359 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 if (error)
5361 goto error0;
5362 goto nodelete;
5363 } else {
5364 ASSERT(del.br_state == XFS_EXT_NORM);
5365 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005366 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005367 ip, whichfork, &lastx, &cur,
5368 &del, firstblock, dfops,
5369 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 if (error)
5371 goto error0;
5372 goto nodelete;
5373 }
5374 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005375
Brian Fosterb2706a02016-03-15 11:42:46 +11005376 if (wasdel) {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005377 error = xfs_bmap_del_extent_delay(ip, whichfork, &lastx,
5378 &got, &del);
5379 } else {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005380 error = xfs_bmap_del_extent_real(ip, tp, &lastx, dfops,
5381 cur, &del, &tmp_logflags, whichfork,
5382 flags);
5383 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005384 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005385
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 if (error)
5387 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005388
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005389 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005390 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 /*
5393 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005395 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005396 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005397 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005398 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005399 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005400 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 extno++;
5402 }
5403 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005404 if (end == (xfs_fileoff_t)-1 || end < start || lastx < 0)
Darrick J. Wong44535932016-10-03 09:11:29 -07005405 *rlen = 0;
5406 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005407 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005408
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 /*
5410 * Convert to a btree if necessary.
5411 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005412 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005414 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 &cur, 0, &tmp_logflags, whichfork);
5416 logflags |= tmp_logflags;
5417 if (error)
5418 goto error0;
5419 }
5420 /*
5421 * transform from btree to extents, give it cur
5422 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005423 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 ASSERT(cur != NULL);
5425 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5426 whichfork);
5427 logflags |= tmp_logflags;
5428 if (error)
5429 goto error0;
5430 }
5431 /*
5432 * transform from extents to local?
5433 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 error = 0;
5435error0:
5436 /*
5437 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005438 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005440 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005442 logflags &= ~xfs_ilog_fext(whichfork);
5443 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005445 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 /*
5447 * Log inode even in the error case, if the transaction
5448 * is dirty we'll need to shut down the filesystem.
5449 */
5450 if (logflags)
5451 xfs_trans_log_inode(tp, ip, logflags);
5452 if (cur) {
5453 if (!error) {
5454 *firstblock = cur->bc_private.b.firstblock;
5455 cur->bc_private.b.allocated = 0;
5456 }
5457 xfs_btree_del_cursor(cur,
5458 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5459 }
5460 return error;
5461}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005462
Darrick J. Wong44535932016-10-03 09:11:29 -07005463/* Unmap a range of a file. */
5464int
5465xfs_bunmapi(
5466 xfs_trans_t *tp,
5467 struct xfs_inode *ip,
5468 xfs_fileoff_t bno,
5469 xfs_filblks_t len,
5470 int flags,
5471 xfs_extnum_t nexts,
5472 xfs_fsblock_t *firstblock,
5473 struct xfs_defer_ops *dfops,
5474 int *done)
5475{
5476 int error;
5477
5478 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5479 dfops);
5480 *done = (len == 0);
5481 return error;
5482}
5483
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005484/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005485 * Determine whether an extent shift can be accomplished by a merge with the
5486 * extent that precedes the target hole of the shift.
5487 */
5488STATIC bool
5489xfs_bmse_can_merge(
5490 struct xfs_bmbt_irec *left, /* preceding extent */
5491 struct xfs_bmbt_irec *got, /* current extent to shift */
5492 xfs_fileoff_t shift) /* shift fsb */
5493{
5494 xfs_fileoff_t startoff;
5495
5496 startoff = got->br_startoff - shift;
5497
5498 /*
5499 * The extent, once shifted, must be adjacent in-file and on-disk with
5500 * the preceding extent.
5501 */
5502 if ((left->br_startoff + left->br_blockcount != startoff) ||
5503 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5504 (left->br_state != got->br_state) ||
5505 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5506 return false;
5507
5508 return true;
5509}
5510
5511/*
5512 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5513 * hole in the file. If an extent shift would result in the extent being fully
5514 * adjacent to the extent that currently precedes the hole, we can merge with
5515 * the preceding extent rather than do the shift.
5516 *
5517 * This function assumes the caller has verified a shift-by-merge is possible
5518 * with the provided extents via xfs_bmse_can_merge().
5519 */
5520STATIC int
5521xfs_bmse_merge(
5522 struct xfs_inode *ip,
5523 int whichfork,
5524 xfs_fileoff_t shift, /* shift fsb */
5525 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005526 struct xfs_bmbt_irec *got, /* extent to shift */
5527 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005528 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005529 int *logflags, /* output */
5530 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005531{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005532 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005533 xfs_filblks_t blockcount;
5534 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005535 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005536
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005537 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005538
5539 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5540 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005541 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005542
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005543 new = *left;
5544 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005545
5546 /*
5547 * Update the on-disk extent count, the btree if necessary and log the
5548 * inode.
5549 */
5550 XFS_IFORK_NEXT_SET(ip, whichfork,
5551 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5552 *logflags |= XFS_ILOG_CORE;
5553 if (!cur) {
5554 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005555 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005556 }
5557
5558 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005559 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005560 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005561 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005562 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005563
5564 error = xfs_btree_delete(cur, &i);
5565 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005566 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005567 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005568
5569 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005570 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005571 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005572 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005573 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005574
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005575 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005576 if (error)
5577 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005578
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005579done:
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005580 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
5581 current_ext - 1, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005582 xfs_iext_remove(ip, current_ext, 1, 0);
5583
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005584 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005585 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5586 if (error)
5587 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005588 memcpy(&new, got, sizeof(new));
5589 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005590 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005591}
5592
Christoph Hellwigbf806282017-10-19 11:07:34 -07005593static int
5594xfs_bmap_shift_update_extent(
5595 struct xfs_inode *ip,
5596 int whichfork,
5597 xfs_extnum_t idx,
5598 struct xfs_bmbt_irec *got,
5599 struct xfs_btree_cur *cur,
5600 int *logflags,
5601 struct xfs_defer_ops *dfops,
5602 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005603{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005604 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005605 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005606 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005607
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005608 *logflags |= XFS_ILOG_CORE;
5609
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005610 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005611
5612 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005613 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005614 if (error)
5615 return error;
5616 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5617
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005618 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005619 if (error)
5620 return error;
5621 } else {
5622 *logflags |= XFS_ILOG_DEXT;
5623 }
5624
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005625 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), idx, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005626
Darrick J. Wong9c194642016-08-03 12:16:05 +10005627 /* update reverse mapping */
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005628 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005629 if (error)
5630 return error;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005631 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005632}
5633
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005634int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005635xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005636 struct xfs_trans *tp,
5637 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005638 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005639 xfs_fileoff_t offset_shift_fsb,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005640 bool *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005641 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005642 xfs_fsblock_t *firstblock,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005643 struct xfs_defer_ops *dfops)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005644{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005645 int whichfork = XFS_DATA_FORK;
5646 struct xfs_mount *mp = ip->i_mount;
5647 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5648 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005649 struct xfs_bmbt_irec got, prev;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005650 xfs_extnum_t current_ext;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005651 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005652 int error = 0;
5653 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005654
5655 if (unlikely(XFS_TEST_ERROR(
5656 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5657 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005658 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005659 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005660 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005661 }
5662
5663 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005664 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005665
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005666 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005667
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005668 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005669 error = xfs_iread_extents(tp, ip, whichfork);
5670 if (error)
5671 return error;
5672 }
5673
Brian Fosterddb19e32014-09-23 15:38:09 +10005674 if (ifp->if_flags & XFS_IFBROOT) {
5675 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5676 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005677 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005678 cur->bc_private.b.flags = 0;
5679 }
5680
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005681 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext, &got)) {
5682 *done = true;
5683 goto del_cursor;
5684 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005685 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005686
Christoph Hellwigbf806282017-10-19 11:07:34 -07005687 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005688 if (xfs_iext_get_extent(ifp, current_ext - 1, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005689 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5690 error = -EINVAL;
5691 goto del_cursor;
5692 }
5693
Christoph Hellwigbf806282017-10-19 11:07:34 -07005694 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5695 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5696 current_ext, &got, &prev, cur,
5697 &logflags, dfops);
5698 if (error)
5699 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005700
5701 /* update got after merge */
5702 if (!xfs_iext_get_extent(ifp, current_ext, &got)) {
5703 *done = true;
5704 goto del_cursor;
5705 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005706 goto done;
5707 }
5708 } else {
5709 if (got.br_startoff < offset_shift_fsb) {
5710 error = -EINVAL;
5711 goto del_cursor;
5712 }
5713 }
5714
5715 error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got,
5716 cur, &logflags, dfops, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005717 if (error)
5718 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005719
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005720 if (!xfs_iext_get_extent(ifp, ++current_ext, &got)) {
5721 *done = true;
5722 goto del_cursor;
5723 }
5724
5725done:
Christoph Hellwigbf806282017-10-19 11:07:34 -07005726 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005727del_cursor:
5728 if (cur)
5729 xfs_btree_del_cursor(cur,
5730 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005731 if (logflags)
5732 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005733 return error;
5734}
5735
5736int
5737xfs_bmap_insert_extents(
5738 struct xfs_trans *tp,
5739 struct xfs_inode *ip,
5740 xfs_fileoff_t *next_fsb,
5741 xfs_fileoff_t offset_shift_fsb,
5742 bool *done,
5743 xfs_fileoff_t stop_fsb,
5744 xfs_fsblock_t *firstblock,
5745 struct xfs_defer_ops *dfops)
5746{
5747 int whichfork = XFS_DATA_FORK;
5748 struct xfs_mount *mp = ip->i_mount;
5749 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5750 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005751 struct xfs_bmbt_irec got, next;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005752 xfs_extnum_t current_ext;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005753 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005754 int error = 0;
5755 int logflags = 0;
5756
5757 if (unlikely(XFS_TEST_ERROR(
5758 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5759 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5760 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5761 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5762 return -EFSCORRUPTED;
5763 }
5764
5765 if (XFS_FORCED_SHUTDOWN(mp))
5766 return -EIO;
5767
5768 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5769
5770 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5771 error = xfs_iread_extents(tp, ip, whichfork);
5772 if (error)
5773 return error;
5774 }
5775
5776 if (ifp->if_flags & XFS_IFBROOT) {
5777 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5778 cur->bc_private.b.firstblock = *firstblock;
5779 cur->bc_private.b.dfops = dfops;
5780 cur->bc_private.b.flags = 0;
5781 }
5782
Namjae Jeona904b1c2015-03-25 15:08:56 +11005783 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005784 current_ext = xfs_iext_count(ifp) - 1;
5785 if (!xfs_iext_get_extent(ifp, current_ext, &got) ||
5786 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005787 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005788 goto del_cursor;
5789 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005790 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005791 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
5792 &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005793 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005794 goto del_cursor;
5795 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005796 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005797 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005798
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005799 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005800 error = -EIO;
5801 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005802 }
5803
Christoph Hellwigbf806282017-10-19 11:07:34 -07005804 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005805 if (xfs_iext_get_extent(ifp, current_ext + 1, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005806 if (new_startoff + got.br_blockcount > next.br_startoff) {
5807 error = -EINVAL;
5808 goto del_cursor;
5809 }
5810
5811 /*
5812 * Unlike a left shift (which involves a hole punch), a right
5813 * shift does not modify extent neighbors in any way. We should
5814 * never find mergeable extents in this scenario. Check anyways
5815 * and warn if we encounter two extents that could be one.
5816 */
5817 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5818 WARN_ON_ONCE(1);
5819 }
5820
5821 error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got,
5822 cur, &logflags, dfops, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005823 if (error)
5824 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005825
5826 if (!xfs_iext_get_extent(ifp, --current_ext, &got) ||
5827 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005828 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005829 goto del_cursor;
5830 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005831
5832 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005833del_cursor:
5834 if (cur)
5835 xfs_btree_del_cursor(cur,
5836 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Brian Fosterca446d82014-09-02 12:12:53 +10005837 if (logflags)
5838 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005839 return error;
5840}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005841
5842/*
5843 * Splits an extent into two extents at split_fsb block such that it is
5844 * the first block of the current_ext. @current_ext is a target extent
5845 * to be split. @split_fsb is a block where the extents is split.
5846 * If split_fsb lies in a hole or the first block of extents, just return 0.
5847 */
5848STATIC int
5849xfs_bmap_split_extent_at(
5850 struct xfs_trans *tp,
5851 struct xfs_inode *ip,
5852 xfs_fileoff_t split_fsb,
5853 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005854 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005855{
5856 int whichfork = XFS_DATA_FORK;
5857 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005858 struct xfs_bmbt_irec got;
5859 struct xfs_bmbt_irec new; /* split extent */
5860 struct xfs_mount *mp = ip->i_mount;
5861 struct xfs_ifork *ifp;
5862 xfs_fsblock_t gotblkcnt; /* new block count for got */
5863 xfs_extnum_t current_ext;
5864 int error = 0;
5865 int logflags = 0;
5866 int i = 0;
5867
5868 if (unlikely(XFS_TEST_ERROR(
5869 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5870 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005871 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005872 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5873 XFS_ERRLEVEL_LOW, mp);
5874 return -EFSCORRUPTED;
5875 }
5876
5877 if (XFS_FORCED_SHUTDOWN(mp))
5878 return -EIO;
5879
5880 ifp = XFS_IFORK_PTR(ip, whichfork);
5881 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5882 /* Read in all the extents */
5883 error = xfs_iread_extents(tp, ip, whichfork);
5884 if (error)
5885 return error;
5886 }
5887
5888 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005889 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005890 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005891 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
5892 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005893 return 0;
5894
5895 gotblkcnt = split_fsb - got.br_startoff;
5896 new.br_startoff = split_fsb;
5897 new.br_startblock = got.br_startblock + gotblkcnt;
5898 new.br_blockcount = got.br_blockcount - gotblkcnt;
5899 new.br_state = got.br_state;
5900
5901 if (ifp->if_flags & XFS_IFBROOT) {
5902 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5903 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005904 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005905 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005906 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005907 if (error)
5908 goto del_cursor;
5909 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5910 }
5911
Namjae Jeona904b1c2015-03-25 15:08:56 +11005912 got.br_blockcount = gotblkcnt;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005913 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
5914 current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005915
5916 logflags = XFS_ILOG_CORE;
5917 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005918 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005919 if (error)
5920 goto del_cursor;
5921 } else
5922 logflags |= XFS_ILOG_DEXT;
5923
5924 /* Add new extent */
5925 current_ext++;
5926 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5927 XFS_IFORK_NEXT_SET(ip, whichfork,
5928 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5929
5930 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005931 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005932 if (error)
5933 goto del_cursor;
5934 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005935 error = xfs_btree_insert(cur, &i);
5936 if (error)
5937 goto del_cursor;
5938 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5939 }
5940
5941 /*
5942 * Convert to a btree if necessary.
5943 */
5944 if (xfs_bmap_needs_btree(ip, whichfork)) {
5945 int tmp_logflags; /* partial log flag return val */
5946
5947 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005948 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005949 &cur, 0, &tmp_logflags, whichfork);
5950 logflags |= tmp_logflags;
5951 }
5952
5953del_cursor:
5954 if (cur) {
5955 cur->bc_private.b.allocated = 0;
5956 xfs_btree_del_cursor(cur,
5957 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5958 }
5959
5960 if (logflags)
5961 xfs_trans_log_inode(tp, ip, logflags);
5962 return error;
5963}
5964
5965int
5966xfs_bmap_split_extent(
5967 struct xfs_inode *ip,
5968 xfs_fileoff_t split_fsb)
5969{
5970 struct xfs_mount *mp = ip->i_mount;
5971 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005972 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005973 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005974 int error;
5975
Christoph Hellwig253f4912016-04-06 09:19:55 +10005976 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
5977 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
5978 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005979 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005980
5981 xfs_ilock(ip, XFS_ILOCK_EXCL);
5982 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
5983
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005984 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005985
5986 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005987 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005988 if (error)
5989 goto out;
5990
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07005991 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005992 if (error)
5993 goto out;
5994
Christoph Hellwig70393312015-06-04 13:48:08 +10005995 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005996
5997out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005998 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10005999 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006000 return error;
6001}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006002
6003/* Deferred mapping is only for real extents in the data fork. */
6004static bool
6005xfs_bmap_is_update_needed(
6006 struct xfs_bmbt_irec *bmap)
6007{
6008 return bmap->br_startblock != HOLESTARTBLOCK &&
6009 bmap->br_startblock != DELAYSTARTBLOCK;
6010}
6011
6012/* Record a bmap intent. */
6013static int
6014__xfs_bmap_add(
6015 struct xfs_mount *mp,
6016 struct xfs_defer_ops *dfops,
6017 enum xfs_bmap_intent_type type,
6018 struct xfs_inode *ip,
6019 int whichfork,
6020 struct xfs_bmbt_irec *bmap)
6021{
6022 int error;
6023 struct xfs_bmap_intent *bi;
6024
6025 trace_xfs_bmap_defer(mp,
6026 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6027 type,
6028 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6029 ip->i_ino, whichfork,
6030 bmap->br_startoff,
6031 bmap->br_blockcount,
6032 bmap->br_state);
6033
6034 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6035 INIT_LIST_HEAD(&bi->bi_list);
6036 bi->bi_type = type;
6037 bi->bi_owner = ip;
6038 bi->bi_whichfork = whichfork;
6039 bi->bi_bmap = *bmap;
6040
Christoph Hellwig882d8782017-08-28 10:21:03 -07006041 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006042 if (error) {
6043 kmem_free(bi);
6044 return error;
6045 }
6046
6047 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6048 return 0;
6049}
6050
6051/* Map an extent into a file. */
6052int
6053xfs_bmap_map_extent(
6054 struct xfs_mount *mp,
6055 struct xfs_defer_ops *dfops,
6056 struct xfs_inode *ip,
6057 struct xfs_bmbt_irec *PREV)
6058{
6059 if (!xfs_bmap_is_update_needed(PREV))
6060 return 0;
6061
6062 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6063 XFS_DATA_FORK, PREV);
6064}
6065
6066/* Unmap an extent out of a file. */
6067int
6068xfs_bmap_unmap_extent(
6069 struct xfs_mount *mp,
6070 struct xfs_defer_ops *dfops,
6071 struct xfs_inode *ip,
6072 struct xfs_bmbt_irec *PREV)
6073{
6074 if (!xfs_bmap_is_update_needed(PREV))
6075 return 0;
6076
6077 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6078 XFS_DATA_FORK, PREV);
6079}
6080
6081/*
6082 * Process one of the deferred bmap operations. We pass back the
6083 * btree cursor to maintain our lock on the bmapbt between calls.
6084 */
6085int
6086xfs_bmap_finish_one(
6087 struct xfs_trans *tp,
6088 struct xfs_defer_ops *dfops,
6089 struct xfs_inode *ip,
6090 enum xfs_bmap_intent_type type,
6091 int whichfork,
6092 xfs_fileoff_t startoff,
6093 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006094 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006095 xfs_exntst_t state)
6096{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006097 xfs_fsblock_t firstfsb;
6098 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006099
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006100 /*
6101 * firstfsb is tied to the transaction lifetime and is used to
6102 * ensure correct AG locking order and schedule work item
6103 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6104 * to only making one bmap call per transaction, so it should
6105 * be safe to have it as a local variable here.
6106 */
6107 firstfsb = NULLFSBLOCK;
6108
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006109 trace_xfs_bmap_deferred(tp->t_mountp,
6110 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6111 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006112 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006113
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006114 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006115 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006116
6117 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006118 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006119 return -EIO;
6120
6121 switch (type) {
6122 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006123 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006124 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006125 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006126 break;
6127 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006128 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6129 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006130 break;
6131 default:
6132 ASSERT(0);
6133 error = -EFSCORRUPTED;
6134 }
6135
6136 return error;
6137}