blob: f45f05c45e1580dd74321f10b375ce31c969548f [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
Christoph Hellwigdc560152017-10-23 16:32:39 -07001389 if (!xfs_iext_lookup_extent_before(ip, ifp, last_block, &idx, &got))
1390 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001391 return 0;
1392}
1393
Dave Chinner68988112013-08-12 20:49:42 +10001394int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001395xfs_bmap_last_extent(
1396 struct xfs_trans *tp,
1397 struct xfs_inode *ip,
1398 int whichfork,
1399 struct xfs_bmbt_irec *rec,
1400 int *is_empty)
1401{
1402 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1403 int error;
1404 int nextents;
1405
1406 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1407 error = xfs_iread_extents(tp, ip, whichfork);
1408 if (error)
1409 return error;
1410 }
1411
Eric Sandeen5d829302016-11-08 12:59:42 +11001412 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001413 if (nextents == 0) {
1414 *is_empty = 1;
1415 return 0;
1416 }
1417
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001418 xfs_iext_get_extent(ifp, nextents - 1, rec);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001419 *is_empty = 0;
1420 return 0;
1421}
1422
1423/*
1424 * Check the last inode extent to determine whether this allocation will result
1425 * in blocks being allocated at the end of the file. When we allocate new data
1426 * blocks at the end of the file which do not start at the previous data block,
1427 * we will try to align the new blocks at stripe unit boundaries.
1428 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001429 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001430 * at, or past the EOF.
1431 */
1432STATIC int
1433xfs_bmap_isaeof(
1434 struct xfs_bmalloca *bma,
1435 int whichfork)
1436{
1437 struct xfs_bmbt_irec rec;
1438 int is_empty;
1439 int error;
1440
Thomas Meyer749f24f2017-10-09 11:38:54 -07001441 bma->aeof = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001442 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1443 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001444 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001445 return error;
1446
Dave Chinner6e708bc2013-11-22 10:41:16 +11001447 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001448 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001449 return 0;
1450 }
1451
Dave Chinner9e5987a2013-02-25 12:31:26 +11001452 /*
1453 * Check if we are allocation or past the last extent, or at least into
1454 * the last delayed allocated extent.
1455 */
1456 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1457 (bma->offset >= rec.br_startoff &&
1458 isnullstartblock(rec.br_startblock));
1459 return 0;
1460}
1461
1462/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001463 * Returns the file-relative block number of the first block past eof in
1464 * the file. This is not based on i_size, it is based on the extent records.
1465 * Returns 0 for local files, as they do not have extent records.
1466 */
1467int
1468xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001469 struct xfs_inode *ip,
1470 xfs_fileoff_t *last_block,
1471 int whichfork)
1472{
1473 struct xfs_bmbt_irec rec;
1474 int is_empty;
1475 int error;
1476
1477 *last_block = 0;
1478
1479 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1480 return 0;
1481
1482 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1483 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001484 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001485
1486 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1487 if (error || is_empty)
1488 return error;
1489
1490 *last_block = rec.br_startoff + rec.br_blockcount;
1491 return 0;
1492}
1493
1494/*
1495 * Returns whether the selected fork of the inode has exactly one
1496 * block or not. For the data fork we check this matches di_size,
1497 * implying the file's range is 0..bsize-1.
1498 */
1499int /* 1=>1 block, 0=>otherwise */
1500xfs_bmap_one_block(
1501 xfs_inode_t *ip, /* incore inode */
1502 int whichfork) /* data or attr fork */
1503{
Dave Chinner9e5987a2013-02-25 12:31:26 +11001504 xfs_ifork_t *ifp; /* inode fork pointer */
1505 int rval; /* return value */
1506 xfs_bmbt_irec_t s; /* internal version of extent */
1507
1508#ifndef DEBUG
1509 if (whichfork == XFS_DATA_FORK)
1510 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1511#endif /* !DEBUG */
1512 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1513 return 0;
1514 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1515 return 0;
1516 ifp = XFS_IFORK_PTR(ip, whichfork);
1517 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001518 xfs_iext_get_extent(ifp, 0, &s);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001519 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1520 if (rval && whichfork == XFS_DATA_FORK)
1521 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1522 return rval;
1523}
1524
1525/*
1526 * Extent tree manipulation functions used during allocation.
1527 */
1528
1529/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001530 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 */
1532STATIC int /* error */
1533xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001534 struct xfs_bmalloca *bma,
1535 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001537 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001540 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 xfs_fileoff_t new_endoff; /* end offset of new entry */
1542 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1543 /* left is 0, right is 1, prev is 2 */
1544 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001545 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001546 xfs_filblks_t da_new; /* new count del alloc blocks used */
1547 xfs_filblks_t da_old; /* old count del alloc blocks used */
1548 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001550 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001551 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001552 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001554 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001555 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001556 ASSERT(whichfork != XFS_ATTR_FORK);
1557 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1558 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001559
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001560 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001561 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001562 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001563 ASSERT(!bma->cur ||
1564 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001565
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001566 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568#define LEFT r[0]
1569#define RIGHT r[1]
1570#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 /*
1573 * Set up a bunch of variables to make the tests simpler.
1574 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001575 xfs_iext_get_extent(ifp, bma->idx, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001577 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 ASSERT(PREV.br_startoff <= new->br_startoff);
1579 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001580
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001581 da_old = startblockval(PREV.br_startblock);
1582 da_new = 0;
1583
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 /*
1585 * Set flags determining what part of the previous delayed allocation
1586 * extent is being replaced by a real allocation.
1587 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001588 if (PREV.br_startoff == new->br_startoff)
1589 state |= BMAP_LEFT_FILLING;
1590 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1591 state |= BMAP_RIGHT_FILLING;
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 /*
1594 * Check and set flags if this segment has a left neighbor.
1595 * Don't set contiguous if the combined extent would be too large.
1596 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001597 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001598 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001599 xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001600
1601 if (isnullstartblock(LEFT.br_startblock))
1602 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001604
1605 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1606 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1607 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1608 LEFT.br_state == new->br_state &&
1609 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1610 state |= BMAP_LEFT_CONTIG;
1611
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 /*
1613 * Check and set flags if this segment has a right neighbor.
1614 * Don't set contiguous if the combined extent would be too large.
1615 * Also check for all-three-contiguous being too large.
1616 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001617 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001618 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001619 xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001620
1621 if (isnullstartblock(RIGHT.br_startblock))
1622 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001624
1625 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1626 new_endoff == RIGHT.br_startoff &&
1627 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1628 new->br_state == RIGHT.br_state &&
1629 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1630 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1631 BMAP_RIGHT_FILLING)) !=
1632 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1633 BMAP_RIGHT_FILLING) ||
1634 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1635 <= MAXEXTLEN))
1636 state |= BMAP_RIGHT_CONTIG;
1637
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 error = 0;
1639 /*
1640 * Switch out based on the FILLING and CONTIG state bits.
1641 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001642 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1643 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1644 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1645 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 /*
1647 * Filling in all of a previously delayed allocation extent.
1648 * The left and right neighbors are both contiguous with new.
1649 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001650 bma->idx--;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001651 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001652 xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001653
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001654 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001655 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001656 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1658 else {
1659 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001660 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001661 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001663 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001664 error = xfs_btree_delete(bma->cur, &i);
1665 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001667 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001668 error = xfs_btree_decrement(bma->cur, 0, &i);
1669 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001671 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001672 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001673 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 goto done;
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 break;
1677
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001678 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /*
1680 * Filling in all of a previously delayed allocation extent.
1681 * The left neighbor is contiguous, the right is not.
1682 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001683 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001684
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001685 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001686 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001687 xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001688
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001689 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1690 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 rval = XFS_ILOG_DEXT;
1692 else {
1693 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001694 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001695 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001697 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001698 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001699 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 goto done;
1701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 break;
1703
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001704 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 /*
1706 * Filling in all of a previously delayed allocation extent.
1707 * The right neighbor is contiguous, the left is not.
1708 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001709 PREV.br_startblock = new->br_startblock;
1710 PREV.br_blockcount += RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001711 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001712
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001713 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1714 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 rval = XFS_ILOG_DEXT;
1716 else {
1717 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001718 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001719 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001721 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001722 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001723 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 goto done;
1725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 break;
1727
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001728 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 /*
1730 * Filling in all of a previously delayed allocation extent.
1731 * Neither the left nor right neighbors are contiguous with
1732 * the new one.
1733 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001734 PREV.br_startblock = new->br_startblock;
1735 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001736 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001737
Darrick J. Wong60b49842016-10-03 09:11:34 -07001738 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001739 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1741 else {
1742 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001743 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001744 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001746 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001747 error = xfs_btree_insert(bma->cur, &i);
1748 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001750 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 break;
1753
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001754 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 /*
1756 * Filling in the first part of a previous delayed allocation.
1757 * The left neighbor is contiguous.
1758 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001759 old = LEFT;
1760 temp = PREV.br_blockcount - new->br_blockcount;
1761 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1762 startblockval(PREV.br_startblock));
1763
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001764 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001765 xfs_iext_update_extent(bma->ip, state, bma->idx - 1, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001766
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001767 PREV.br_blockcount = temp = PREV.br_blockcount - new->br_blockcount;
1768 PREV.br_startoff += new->br_blockcount;
1769 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001770 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001771
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001772 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 rval = XFS_ILOG_DEXT;
1774 else {
1775 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001776 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001777 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001779 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001780 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001781 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 goto done;
1783 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001784
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001785 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 break;
1787
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001788 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 /*
1790 * Filling in the first part of a previous delayed allocation.
1791 * The left neighbor is not contiguous.
1792 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001793 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001794 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001795 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1797 else {
1798 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001799 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001800 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001802 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001803 error = xfs_btree_insert(bma->cur, &i);
1804 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001806 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001808
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001809 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001810 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001811 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001812 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 rval |= tmp_rval;
1814 if (error)
1815 goto done;
1816 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001817
1818 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001820 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001821 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001822
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001823 PREV.br_startoff = new_endoff;
1824 PREV.br_blockcount = temp;
1825 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001826 xfs_iext_update_extent(bma->ip, state, bma->idx + 1, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 break;
1828
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001829 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /*
1831 * Filling in the last part of a previous delayed allocation.
1832 * The right neighbor is contiguous with the new allocation.
1833 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001834 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001835 RIGHT.br_startoff = new->br_startoff;
1836 RIGHT.br_startblock = new->br_startblock;
1837 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001838 xfs_iext_update_extent(bma->ip, state, bma->idx + 1, &RIGHT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001839
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001840 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 rval = XFS_ILOG_DEXT;
1842 else {
1843 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001844 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001845 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001847 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001848 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001849 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 goto done;
1851 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001852
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001853 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001854 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001855 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001856
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001857 PREV.br_blockcount = temp;
1858 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001859 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001860
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001861 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 break;
1863
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001864 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 /*
1866 * Filling in the last part of a previous delayed allocation.
1867 * The right neighbor is not contiguous.
1868 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001869 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001870 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001871 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1873 else {
1874 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001875 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001876 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001878 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001879 error = xfs_btree_insert(bma->cur, &i);
1880 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001882 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001884
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001885 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001886 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001887 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001888 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 rval |= tmp_rval;
1890 if (error)
1891 goto done;
1892 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001893
1894 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001895 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001896 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001897 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001898
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001899 PREV.br_startblock = nullstartblock(da_new);
1900 PREV.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001901 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001902
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001903 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 break;
1905
1906 case 0:
1907 /*
1908 * Filling in the middle part of a previous delayed allocation.
1909 * Contiguity is impossible here.
1910 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001911 *
1912 * We start with a delayed allocation:
1913 *
1914 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1915 * PREV @ idx
1916 *
1917 * and we are allocating:
1918 * +rrrrrrrrrrrrrrrrr+
1919 * new
1920 *
1921 * and we set it up for insertion as:
1922 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1923 * new
1924 * PREV @ idx LEFT RIGHT
1925 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001927 old = PREV;
1928
1929 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001930 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001931
1932 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001933 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001934 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001935 RIGHT.br_blockcount =
1936 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1937 RIGHT.br_startblock =
1938 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1939 RIGHT.br_blockcount));
1940
1941 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001942 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1943 PREV.br_startblock =
1944 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1945 PREV.br_blockcount));
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001946 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001947
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001948 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001949 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001950 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001951
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001952 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1954 else {
1955 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001956 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001957 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001959 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001960 error = xfs_btree_insert(bma->cur, &i);
1961 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001963 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001965
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001966 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001967 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001968 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001969 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 rval |= tmp_rval;
1971 if (error)
1972 goto done;
1973 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001974
1975 da_new = startblockval(PREV.br_startblock) +
1976 startblockval(RIGHT.br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001977 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 break;
1979
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001980 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1981 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1982 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1983 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1984 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1985 case BMAP_LEFT_CONTIG:
1986 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 /*
1988 * These cases are all impossible.
1989 */
1990 ASSERT(0);
1991 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001992
Darrick J. Wong9c194642016-08-03 12:16:05 +10001993 /* add reverse mapping */
1994 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
1995 if (error)
1996 goto done;
1997
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001998 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001999 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002000 int tmp_logflags; /* partial log flag return val */
2001
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002002 ASSERT(bma->cur == NULL);
2003 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002004 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002005 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002006 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002007 if (error)
2008 goto done;
2009 }
2010
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002011 if (bma->cur) {
2012 da_new += bma->cur->bc_private.b.allocated;
2013 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002014 }
2015
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002016 /* adjust for changes in reserved delayed indirect blocks */
2017 if (da_new != da_old) {
2018 ASSERT(state == 0 || da_new < da_old);
2019 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2020 false);
2021 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002022
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002023 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002025 if (whichfork != XFS_COW_FORK)
2026 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 return error;
2028#undef LEFT
2029#undef RIGHT
2030#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031}
2032
2033/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002034 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 */
2036STATIC int /* error */
2037xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002038 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002040 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002041 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002043 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002044 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002045 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002046 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002051 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2054 /* left is 0, right is 1, prev is 2 */
2055 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002056 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002057 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002058 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002060 *logflagsp = 0;
2061
2062 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002063 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002064
2065 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002066 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002067 ASSERT(!isnullstartblock(new->br_startblock));
2068
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002069 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071#define LEFT r[0]
2072#define RIGHT r[1]
2073#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /*
2076 * Set up a bunch of variables to make the tests simpler.
2077 */
2078 error = 0;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002079 xfs_iext_get_extent(ifp, *idx, &PREV);
2080 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 new_endoff = new->br_startoff + new->br_blockcount;
2082 ASSERT(PREV.br_startoff <= new->br_startoff);
2083 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /*
2086 * Set flags determining what part of the previous oldext allocation
2087 * extent is being replaced by a newext allocation.
2088 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002089 if (PREV.br_startoff == new->br_startoff)
2090 state |= BMAP_LEFT_FILLING;
2091 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2092 state |= BMAP_RIGHT_FILLING;
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /*
2095 * Check and set flags if this segment has a left neighbor.
2096 * Don't set contiguous if the combined extent would be too large.
2097 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002098 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002099 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002100 xfs_iext_get_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002101
2102 if (isnullstartblock(LEFT.br_startblock))
2103 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002105
2106 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2107 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2108 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002109 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002110 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2111 state |= BMAP_LEFT_CONTIG;
2112
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 /*
2114 * Check and set flags if this segment has a right neighbor.
2115 * Don't set contiguous if the combined extent would be too large.
2116 * Also check for all-three-contiguous being too large.
2117 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002118 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002119 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002120 xfs_iext_get_extent(ifp, *idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002121 if (isnullstartblock(RIGHT.br_startblock))
2122 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002124
2125 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2126 new_endoff == RIGHT.br_startoff &&
2127 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002128 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002129 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2130 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2131 BMAP_RIGHT_FILLING)) !=
2132 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2133 BMAP_RIGHT_FILLING) ||
2134 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2135 <= MAXEXTLEN))
2136 state |= BMAP_RIGHT_CONTIG;
2137
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /*
2139 * Switch out based on the FILLING and CONTIG state bits.
2140 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002141 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2142 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2143 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2144 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 /*
2146 * Setting all of a previous oldext extent to newext.
2147 * The left and right neighbors are both contiguous with new.
2148 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002149 --*idx;
2150
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002151 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002152 xfs_iext_update_extent(ip, state, *idx, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002153
Christoph Hellwigec90c552011-05-23 08:52:53 +00002154 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002155 XFS_IFORK_NEXT_SET(ip, whichfork,
2156 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (cur == NULL)
2158 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2159 else {
2160 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002161 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2162 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002164 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002165 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002167 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002168 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002170 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002171 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002173 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002174 if ((error = xfs_btree_decrement(cur, 0, &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 Hellwiga67d00a2017-10-17 14:16:26 -07002177 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002178 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 goto done;
2180 }
2181 break;
2182
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002183 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 /*
2185 * Setting all of a previous oldext extent to newext.
2186 * The left neighbor is contiguous, the right is not.
2187 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002188 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002189
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002190 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002191 xfs_iext_update_extent(ip, state, *idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002192
2193 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002194 XFS_IFORK_NEXT_SET(ip, whichfork,
2195 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 if (cur == NULL)
2197 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2198 else {
2199 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002200 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2201 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002203 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002204 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002206 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002207 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002209 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002210 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002211 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 goto done;
2213 }
2214 break;
2215
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002216 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 /*
2218 * Setting all of a previous oldext extent to newext.
2219 * The right neighbor is contiguous, the left is not.
2220 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002221 PREV.br_blockcount += RIGHT.br_blockcount;
2222 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002223 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002224
Christoph Hellwigec90c552011-05-23 08:52:53 +00002225 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002226 XFS_IFORK_NEXT_SET(ip, whichfork,
2227 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 if (cur == NULL)
2229 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2230 else {
2231 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002232 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2233 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002235 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002236 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002238 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002239 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002241 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002242 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002243 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 goto done;
2245 }
2246 break;
2247
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002248 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 /*
2250 * Setting all of a previous oldext extent to newext.
2251 * Neither the left nor right neighbors are contiguous with
2252 * the new one.
2253 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002254 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002255 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (cur == NULL)
2258 rval = XFS_ILOG_DEXT;
2259 else {
2260 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002261 error = xfs_bmbt_lookup_eq(cur, new, &i);
2262 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002264 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002265 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002266 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 goto done;
2268 }
2269 break;
2270
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002271 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 /*
2273 * Setting the first part of a previous oldext extent to newext.
2274 * The left neighbor is contiguous.
2275 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002276 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002277 xfs_iext_update_extent(ip, state, *idx - 1, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002278
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002279 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002280 PREV.br_startoff += new->br_blockcount;
2281 PREV.br_startblock += new->br_blockcount;
2282 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002283 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002284
Christoph Hellwigec90c552011-05-23 08:52:53 +00002285 --*idx;
2286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (cur == NULL)
2288 rval = XFS_ILOG_DEXT;
2289 else {
2290 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002291 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002292 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002294 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002295 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002296 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002298 error = xfs_btree_decrement(cur, 0, &i);
2299 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002301 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002302 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 goto done;
2304 }
2305 break;
2306
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002307 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 /*
2309 * Setting the first part of a previous oldext extent to newext.
2310 * The left neighbor is not contiguous.
2311 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002312 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002313 PREV.br_startoff += new->br_blockcount;
2314 PREV.br_startblock += new->br_blockcount;
2315 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002316 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002317
Christoph Hellwigec90c552011-05-23 08:52:53 +00002318 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002319 XFS_IFORK_NEXT_SET(ip, whichfork,
2320 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (cur == NULL)
2322 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2323 else {
2324 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002325 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002326 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002328 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002329 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002330 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 goto done;
2332 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002333 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002335 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337 break;
2338
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002339 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 /*
2341 * Setting the last part of a previous oldext extent to newext.
2342 * The right neighbor is contiguous with the new allocation.
2343 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002344 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002345 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002346 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002347
2348 ++*idx;
2349
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002350 RIGHT.br_startoff = new->br_startoff;
2351 RIGHT.br_startblock = new->br_startblock;
2352 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002353 xfs_iext_update_extent(ip, state, *idx, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (cur == NULL)
2356 rval = XFS_ILOG_DEXT;
2357 else {
2358 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002359 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002360 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002362 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002363 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002364 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002366 error = xfs_btree_increment(cur, 0, &i);
2367 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002369 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002370 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 goto done;
2372 }
2373 break;
2374
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002375 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 /*
2377 * Setting the last part of a previous oldext extent to newext.
2378 * The right neighbor is not contiguous.
2379 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002380 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002381 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002382 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002383
Christoph Hellwigec90c552011-05-23 08:52:53 +00002384 ++*idx;
2385 xfs_iext_insert(ip, *idx, 1, new, state);
2386
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002387 XFS_IFORK_NEXT_SET(ip, whichfork,
2388 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 if (cur == NULL)
2390 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2391 else {
2392 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002393 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002394 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002396 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002397 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002398 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002400 error = xfs_bmbt_lookup_eq(cur, new, &i);
2401 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002403 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002404 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002406 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
2408 break;
2409
2410 case 0:
2411 /*
2412 * Setting the middle part of a previous oldext extent to
2413 * newext. Contiguity is impossible here.
2414 * One extent becomes three extents.
2415 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002416 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002417 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002418 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 r[0] = *new;
2421 r[1].br_startoff = new_endoff;
2422 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002423 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002425 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002426
2427 ++*idx;
2428 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2429
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002430 XFS_IFORK_NEXT_SET(ip, whichfork,
2431 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (cur == NULL)
2433 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2434 else {
2435 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002436 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002437 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002439 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002441 error = xfs_bmbt_update(cur, &r[1]);
2442 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 goto done;
2444 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002446 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002448 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002449 /*
2450 * Reset the cursor to the position of the new extent
2451 * we are about to insert as we can't trust it after
2452 * the previous insert.
2453 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002454 error = xfs_bmbt_lookup_eq(cur, new, &i);
2455 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002457 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002459 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002461 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 }
2463 break;
2464
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002465 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2466 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2467 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2468 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2469 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2470 case BMAP_LEFT_CONTIG:
2471 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /*
2473 * These cases are all impossible.
2474 */
2475 ASSERT(0);
2476 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002477
Darrick J. Wong9c194642016-08-03 12:16:05 +10002478 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002479 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002480 if (error)
2481 goto done;
2482
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002483 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002484 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002485 int tmp_logflags; /* partial log flag return val */
2486
2487 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002488 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002489 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002490 *logflagsp |= tmp_logflags;
2491 if (error)
2492 goto done;
2493 }
2494
2495 /* clear out the allocated field, done with it now in any case. */
2496 if (cur) {
2497 cur->bc_private.b.allocated = 0;
2498 *curp = cur;
2499 }
2500
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002501 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002503 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 return error;
2505#undef LEFT
2506#undef RIGHT
2507#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
2510/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002511 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002513STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514xfs_bmap_add_extent_hole_delay(
2515 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002516 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002517 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002518 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002520 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2522 xfs_filblks_t newlen=0; /* new indirect size */
2523 xfs_filblks_t oldlen=0; /* old indirect size */
2524 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002525 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002526 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002528 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002529 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /*
2532 * Check and set flags if this segment has a left neighbor
2533 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002534 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002535 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002536 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002537
2538 if (isnullstartblock(left.br_startblock))
2539 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002541
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 /*
2543 * Check and set flags if the current (right) segment exists.
2544 * If it doesn't exist, we're converting the hole at end-of-file.
2545 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002546 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002547 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002548 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002549
2550 if (isnullstartblock(right.br_startblock))
2551 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /*
2555 * Set contiguity flags on the left and right neighbors.
2556 * Don't let extents get too large, even if the pieces are contiguous.
2557 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002558 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2559 left.br_startoff + left.br_blockcount == new->br_startoff &&
2560 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2561 state |= BMAP_LEFT_CONTIG;
2562
2563 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2564 new->br_startoff + new->br_blockcount == right.br_startoff &&
2565 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2566 (!(state & BMAP_LEFT_CONTIG) ||
2567 (left.br_blockcount + new->br_blockcount +
2568 right.br_blockcount <= MAXEXTLEN)))
2569 state |= BMAP_RIGHT_CONTIG;
2570
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 /*
2572 * Switch out based on the contiguity flags.
2573 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002574 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2575 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 /*
2577 * New allocation is contiguous with delayed allocations
2578 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002579 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002581 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 temp = left.br_blockcount + new->br_blockcount +
2583 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002584
Eric Sandeen9d87c312009-01-14 23:22:07 -06002585 oldlen = startblockval(left.br_startblock) +
2586 startblockval(new->br_startblock) +
2587 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002588 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2589 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002590 left.br_startblock = nullstartblock(newlen);
2591 left.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002592 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002593
Christoph Hellwigec90c552011-05-23 08:52:53 +00002594 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 break;
2596
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002597 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 /*
2599 * New allocation is contiguous with a delayed allocation
2600 * on the left.
2601 * Merge the new allocation with the left neighbor.
2602 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002603 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002605
Eric Sandeen9d87c312009-01-14 23:22:07 -06002606 oldlen = startblockval(left.br_startblock) +
2607 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002608 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2609 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002610 left.br_blockcount = temp;
2611 left.br_startblock = nullstartblock(newlen);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002612 xfs_iext_update_extent(ip, state, *idx, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 break;
2614
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002615 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 /*
2617 * New allocation is contiguous with a delayed allocation
2618 * on the right.
2619 * Merge the new allocation with the right neighbor.
2620 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002622 oldlen = startblockval(new->br_startblock) +
2623 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002624 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2625 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002626 right.br_startoff = new->br_startoff;
2627 right.br_startblock = nullstartblock(newlen);
2628 right.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002629 xfs_iext_update_extent(ip, state, *idx, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 break;
2631
2632 case 0:
2633 /*
2634 * New allocation is not contiguous with another
2635 * delayed allocation.
2636 * Insert a new entry.
2637 */
2638 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002639 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 break;
2641 }
2642 if (oldlen != newlen) {
2643 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002644 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2645 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 /*
2647 * Nothing to do for disk quota accounting here.
2648 */
2649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650}
2651
2652/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002653 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 */
2655STATIC int /* error */
2656xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002657 struct xfs_trans *tp,
2658 struct xfs_inode *ip,
2659 int whichfork,
2660 xfs_extnum_t *idx,
2661 struct xfs_btree_cur **curp,
2662 struct xfs_bmbt_irec *new,
2663 xfs_fsblock_t *first,
2664 struct xfs_defer_ops *dfops,
2665 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002667 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2668 struct xfs_mount *mp = ip->i_mount;
2669 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2673 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002674 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002675 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002676 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Christoph Hellwig6d045582017-04-11 16:45:54 -07002678 ASSERT(*idx >= 0);
2679 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002680 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002681 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002682
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002683 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 /*
2686 * Check and set flags if this segment has a left neighbor.
2687 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002688 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002689 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002690 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002691 if (isnullstartblock(left.br_startblock))
2692 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 /*
2696 * Check and set flags if this segment has a current value.
2697 * Not true if we're inserting into the "hole" at eof.
2698 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002699 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002700 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002701 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002702 if (isnullstartblock(right.br_startblock))
2703 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 /*
2707 * We're inserting a real allocation between "left" and "right".
2708 * Set the contiguity flags. Don't let extents get too large.
2709 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002710 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2711 left.br_startoff + left.br_blockcount == new->br_startoff &&
2712 left.br_startblock + left.br_blockcount == new->br_startblock &&
2713 left.br_state == new->br_state &&
2714 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2715 state |= BMAP_LEFT_CONTIG;
2716
2717 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2718 new->br_startoff + new->br_blockcount == right.br_startoff &&
2719 new->br_startblock + new->br_blockcount == right.br_startblock &&
2720 new->br_state == right.br_state &&
2721 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2722 (!(state & BMAP_LEFT_CONTIG) ||
2723 left.br_blockcount + new->br_blockcount +
2724 right.br_blockcount <= MAXEXTLEN))
2725 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002727 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 /*
2729 * Select which case we're in here, and implement it.
2730 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002731 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2732 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 /*
2734 * New allocation is contiguous with real allocations on the
2735 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002736 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002738 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002739 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002740 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002741
Christoph Hellwig6d045582017-04-11 16:45:54 -07002742 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002743
Christoph Hellwig6d045582017-04-11 16:45:54 -07002744 XFS_IFORK_NEXT_SET(ip, whichfork,
2745 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2746 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002747 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002748 } else {
2749 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002750 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002751 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002752 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002753 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002754 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002755 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002756 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002757 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002758 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002759 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002760 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002761 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002762 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002763 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002764 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002766 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002768 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 /*
2770 * New allocation is contiguous with a real allocation
2771 * on the left.
2772 * Merge the new allocation with the left neighbor.
2773 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002774 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002775 old = left;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002776
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002777 left.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002778 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002779
Christoph Hellwig6d045582017-04-11 16:45:54 -07002780 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002781 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002782 } else {
2783 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002784 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002785 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002786 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002787 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002788 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002789 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002790 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002792 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002794 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 /*
2796 * New allocation is contiguous with a real allocation
2797 * on the right.
2798 * Merge the new allocation with the right neighbor.
2799 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002800 old = right;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002801
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002802 right.br_startoff = new->br_startoff;
2803 right.br_startblock = new->br_startblock;
2804 right.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002805 xfs_iext_update_extent(ip, state, *idx, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002806
Christoph Hellwig6d045582017-04-11 16:45:54 -07002807 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002808 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002809 } else {
2810 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002811 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002812 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002813 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002814 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002815 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002816 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002817 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002819 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 case 0:
2822 /*
2823 * New allocation is not contiguous with another
2824 * real allocation.
2825 * Insert a new entry.
2826 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002827 xfs_iext_insert(ip, *idx, 1, new, state);
2828 XFS_IFORK_NEXT_SET(ip, whichfork,
2829 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2830 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002831 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002832 } else {
2833 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002834 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002835 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002836 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002837 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002838 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002839 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002840 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002841 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002843 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002845
Darrick J. Wong9c194642016-08-03 12:16:05 +10002846 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002847 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002848 if (error)
2849 goto done;
2850
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002851 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002852 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002853 int tmp_logflags; /* partial log flag return val */
2854
Christoph Hellwig6d045582017-04-11 16:45:54 -07002855 ASSERT(cur == NULL);
2856 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002857 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002858 *logflagsp |= tmp_logflags;
2859 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002860 if (error)
2861 goto done;
2862 }
2863
2864 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002865 if (cur)
2866 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002867
Christoph Hellwig6d045582017-04-11 16:45:54 -07002868 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002869done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002870 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002871 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872}
2873
Nathan Scottdd9f4382006-01-11 15:28:28 +11002874/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11002875 * Functions used in the extent read, allocate and remove paths
2876 */
2877
2878/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002879 * Adjust the size of the new extent based on di_extsize and rt extsize.
2880 */
Dave Chinner68988112013-08-12 20:49:42 +10002881int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002882xfs_bmap_extsize_align(
2883 xfs_mount_t *mp,
2884 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2885 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2886 xfs_extlen_t extsz, /* align to this extent size */
2887 int rt, /* is this a realtime inode? */
2888 int eof, /* is extent at end-of-file? */
2889 int delay, /* creating delalloc extent? */
2890 int convert, /* overwriting unwritten extent? */
2891 xfs_fileoff_t *offp, /* in/out: aligned offset */
2892 xfs_extlen_t *lenp) /* in/out: aligned length */
2893{
2894 xfs_fileoff_t orig_off; /* original offset */
2895 xfs_extlen_t orig_alen; /* original length */
2896 xfs_fileoff_t orig_end; /* original off+len */
2897 xfs_fileoff_t nexto; /* next file offset */
2898 xfs_fileoff_t prevo; /* previous file offset */
2899 xfs_fileoff_t align_off; /* temp for offset */
2900 xfs_extlen_t align_alen; /* temp for length */
2901 xfs_extlen_t temp; /* temp for calculations */
2902
2903 if (convert)
2904 return 0;
2905
2906 orig_off = align_off = *offp;
2907 orig_alen = align_alen = *lenp;
2908 orig_end = orig_off + orig_alen;
2909
2910 /*
2911 * If this request overlaps an existing extent, then don't
2912 * attempt to perform any additional alignment.
2913 */
2914 if (!delay && !eof &&
2915 (orig_off >= gotp->br_startoff) &&
2916 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2917 return 0;
2918 }
2919
2920 /*
2921 * If the file offset is unaligned vs. the extent size
2922 * we need to align it. This will be possible unless
2923 * the file was previously written with a kernel that didn't
2924 * perform this alignment, or if a truncate shot us in the
2925 * foot.
2926 */
2927 temp = do_mod(orig_off, extsz);
2928 if (temp) {
2929 align_alen += temp;
2930 align_off -= temp;
2931 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002932
2933 /* Same adjustment for the end of the requested area. */
2934 temp = (align_alen % extsz);
2935 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002936 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002937
2938 /*
2939 * For large extent hint sizes, the aligned extent might be larger than
2940 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2941 * the length back under MAXEXTLEN. The outer allocation loops handle
2942 * short allocation just fine, so it is safe to do this. We only want to
2943 * do it when we are forced to, though, because it means more allocation
2944 * operations are required.
2945 */
2946 while (align_alen > MAXEXTLEN)
2947 align_alen -= extsz;
2948 ASSERT(align_alen <= MAXEXTLEN);
2949
Nathan Scottdd9f4382006-01-11 15:28:28 +11002950 /*
2951 * If the previous block overlaps with this proposed allocation
2952 * then move the start forward without adjusting the length.
2953 */
2954 if (prevp->br_startoff != NULLFILEOFF) {
2955 if (prevp->br_startblock == HOLESTARTBLOCK)
2956 prevo = prevp->br_startoff;
2957 else
2958 prevo = prevp->br_startoff + prevp->br_blockcount;
2959 } else
2960 prevo = 0;
2961 if (align_off != orig_off && align_off < prevo)
2962 align_off = prevo;
2963 /*
2964 * If the next block overlaps with this proposed allocation
2965 * then move the start back without adjusting the length,
2966 * but not before offset 0.
2967 * This may of course make the start overlap previous block,
2968 * and if we hit the offset 0 limit then the next block
2969 * can still overlap too.
2970 */
2971 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2972 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2973 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2974 nexto = gotp->br_startoff + gotp->br_blockcount;
2975 else
2976 nexto = gotp->br_startoff;
2977 } else
2978 nexto = NULLFILEOFF;
2979 if (!eof &&
2980 align_off + align_alen != orig_end &&
2981 align_off + align_alen > nexto)
2982 align_off = nexto > align_alen ? nexto - align_alen : 0;
2983 /*
2984 * If we're now overlapping the next or previous extent that
2985 * means we can't fit an extsz piece in this hole. Just move
2986 * the start forward to the first valid spot and set
2987 * the length so we hit the end.
2988 */
2989 if (align_off != orig_off && align_off < prevo)
2990 align_off = prevo;
2991 if (align_off + align_alen != orig_end &&
2992 align_off + align_alen > nexto &&
2993 nexto != NULLFILEOFF) {
2994 ASSERT(nexto > prevo);
2995 align_alen = nexto - align_off;
2996 }
2997
2998 /*
2999 * If realtime, and the result isn't a multiple of the realtime
3000 * extent size we need to remove blocks until it is.
3001 */
3002 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3003 /*
3004 * We're not covering the original request, or
3005 * we won't be able to once we fix the length.
3006 */
3007 if (orig_off < align_off ||
3008 orig_end > align_off + align_alen ||
3009 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003010 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003011 /*
3012 * Try to fix it by moving the start up.
3013 */
3014 if (align_off + temp <= orig_off) {
3015 align_alen -= temp;
3016 align_off += temp;
3017 }
3018 /*
3019 * Try to fix it by moving the end in.
3020 */
3021 else if (align_off + align_alen - temp >= orig_end)
3022 align_alen -= temp;
3023 /*
3024 * Set the start to the minimum then trim the length.
3025 */
3026 else {
3027 align_alen -= orig_off - align_off;
3028 align_off = orig_off;
3029 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3030 }
3031 /*
3032 * Result doesn't cover the request, fail it.
3033 */
3034 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003035 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003036 } else {
3037 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003038 /* see MAXEXTLEN handling above */
3039 ASSERT(orig_end <= align_off + align_alen ||
3040 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003041 }
3042
3043#ifdef DEBUG
3044 if (!eof && gotp->br_startoff != NULLFILEOFF)
3045 ASSERT(align_off + align_alen <= gotp->br_startoff);
3046 if (prevp->br_startoff != NULLFILEOFF)
3047 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3048#endif
3049
3050 *lenp = align_alen;
3051 *offp = align_off;
3052 return 0;
3053}
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055#define XFS_ALLOC_GAP_UNITS 4
3056
Dave Chinner68988112013-08-12 20:49:42 +10003057void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003058xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003059 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
3061 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3063 xfs_mount_t *mp; /* mount point structure */
3064 int nullfb; /* true if ap->firstblock isn't set */
3065 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
3067#define ISVALID(x,y) \
3068 (rt ? \
3069 (x) < mp->m_sb.sb_rblocks : \
3070 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3071 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3072 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3073
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003075 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003076 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3077 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003078 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 /*
3080 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003081 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003083 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3084 !isnullstartblock(ap->prev.br_startblock) &&
3085 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3086 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003087 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 /*
3089 * Adjust for the gap between prevp and us.
3090 */
Dave Chinner3a756672011-09-18 20:40:58 +00003091 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003092 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003094 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3095 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 }
3097 /*
3098 * If not at eof, then compare the two neighbor blocks.
3099 * Figure out whether either one gives us a good starting point,
3100 * and pick the better one.
3101 */
3102 else if (!ap->eof) {
3103 xfs_fsblock_t gotbno; /* right side block number */
3104 xfs_fsblock_t gotdiff=0; /* right side difference */
3105 xfs_fsblock_t prevbno; /* left side block number */
3106 xfs_fsblock_t prevdiff=0; /* left side difference */
3107
3108 /*
3109 * If there's a previous (left) block, select a requested
3110 * start block based on it.
3111 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003112 if (ap->prev.br_startoff != NULLFILEOFF &&
3113 !isnullstartblock(ap->prev.br_startblock) &&
3114 (prevbno = ap->prev.br_startblock +
3115 ap->prev.br_blockcount) &&
3116 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 /*
3118 * Calculate gap to end of previous block.
3119 */
Dave Chinner3a756672011-09-18 20:40:58 +00003120 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003121 (ap->prev.br_startoff +
3122 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 /*
3124 * Figure the startblock based on the previous block's
3125 * end and the gap size.
3126 * Heuristic!
3127 * If the gap is large relative to the piece we're
3128 * allocating, or using it gives us an invalid block
3129 * number, then just use the end of the previous block.
3130 */
Dave Chinner3a756672011-09-18 20:40:58 +00003131 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003133 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 prevbno += adjust;
3135 else
3136 prevdiff += adjust;
3137 /*
3138 * If the firstblock forbids it, can't use it,
3139 * must use default.
3140 */
3141 if (!rt && !nullfb &&
3142 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3143 prevbno = NULLFSBLOCK;
3144 }
3145 /*
3146 * No previous block or can't follow it, just default.
3147 */
3148 else
3149 prevbno = NULLFSBLOCK;
3150 /*
3151 * If there's a following (right) block, select a requested
3152 * start block based on it.
3153 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003154 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 /*
3156 * Calculate gap to start of next block.
3157 */
Dave Chinner3a756672011-09-18 20:40:58 +00003158 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 /*
3160 * Figure the startblock based on the next block's
3161 * start and the gap size.
3162 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003163 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 /*
3165 * Heuristic!
3166 * If the gap is large relative to the piece we're
3167 * allocating, or using it gives us an invalid block
3168 * number, then just use the start of the next block
3169 * offset by our length.
3170 */
Dave Chinner3a756672011-09-18 20:40:58 +00003171 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 ISVALID(gotbno - gotdiff, gotbno))
3173 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003174 else if (ISVALID(gotbno - ap->length, gotbno)) {
3175 gotbno -= ap->length;
3176 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 } else
3178 gotdiff += adjust;
3179 /*
3180 * If the firstblock forbids it, can't use it,
3181 * must use default.
3182 */
3183 if (!rt && !nullfb &&
3184 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3185 gotbno = NULLFSBLOCK;
3186 }
3187 /*
3188 * No next block, just default.
3189 */
3190 else
3191 gotbno = NULLFSBLOCK;
3192 /*
3193 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003194 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 */
3196 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003197 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003199 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003201 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003203#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003204}
3205
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003206static int
3207xfs_bmap_longest_free_extent(
3208 struct xfs_trans *tp,
3209 xfs_agnumber_t ag,
3210 xfs_extlen_t *blen,
3211 int *notinit)
3212{
3213 struct xfs_mount *mp = tp->t_mountp;
3214 struct xfs_perag *pag;
3215 xfs_extlen_t longest;
3216 int error = 0;
3217
3218 pag = xfs_perag_get(mp, ag);
3219 if (!pag->pagf_init) {
3220 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3221 if (error)
3222 goto out;
3223
3224 if (!pag->pagf_init) {
3225 *notinit = 1;
3226 goto out;
3227 }
3228 }
3229
Dave Chinner50adbcb2015-06-22 10:04:31 +10003230 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003231 xfs_alloc_min_freelist(mp, pag),
3232 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003233 if (*blen < longest)
3234 *blen = longest;
3235
3236out:
3237 xfs_perag_put(pag);
3238 return error;
3239}
3240
3241static void
3242xfs_bmap_select_minlen(
3243 struct xfs_bmalloca *ap,
3244 struct xfs_alloc_arg *args,
3245 xfs_extlen_t *blen,
3246 int notinit)
3247{
3248 if (notinit || *blen < ap->minlen) {
3249 /*
3250 * Since we did a BUF_TRYLOCK above, it is possible that
3251 * there is space for this request.
3252 */
3253 args->minlen = ap->minlen;
3254 } else if (*blen < args->maxlen) {
3255 /*
3256 * If the best seen length is less than the request length,
3257 * use the best as the minimum.
3258 */
3259 args->minlen = *blen;
3260 } else {
3261 /*
3262 * Otherwise we've seen an extent as big as maxlen, use that
3263 * as the minimum.
3264 */
3265 args->minlen = args->maxlen;
3266 }
3267}
3268
Nathan Scotta365bdd2006-03-14 13:34:16 +11003269STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003270xfs_bmap_btalloc_nullfb(
3271 struct xfs_bmalloca *ap,
3272 struct xfs_alloc_arg *args,
3273 xfs_extlen_t *blen)
3274{
3275 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003276 xfs_agnumber_t ag, startag;
3277 int notinit = 0;
3278 int error;
3279
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003280 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003281 args->total = ap->total;
3282
Christoph Hellwigc467c042010-02-15 23:34:42 +00003283 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3284 if (startag == NULLAGNUMBER)
3285 startag = ag = 0;
3286
Dave Chinner14b064c2011-01-27 12:16:28 +11003287 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003288 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3289 &notinit);
3290 if (error)
3291 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003292
Christoph Hellwigc467c042010-02-15 23:34:42 +00003293 if (++ag == mp->m_sb.sb_agcount)
3294 ag = 0;
3295 if (ag == startag)
3296 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003297 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003298
3299 xfs_bmap_select_minlen(ap, args, blen, notinit);
3300 return 0;
3301}
3302
3303STATIC int
3304xfs_bmap_btalloc_filestreams(
3305 struct xfs_bmalloca *ap,
3306 struct xfs_alloc_arg *args,
3307 xfs_extlen_t *blen)
3308{
3309 struct xfs_mount *mp = ap->ip->i_mount;
3310 xfs_agnumber_t ag;
3311 int notinit = 0;
3312 int error;
3313
3314 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3315 args->total = ap->total;
3316
3317 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3318 if (ag == NULLAGNUMBER)
3319 ag = 0;
3320
3321 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3322 if (error)
3323 return error;
3324
3325 if (*blen < args->maxlen) {
3326 error = xfs_filestream_new_ag(ap, &ag);
3327 if (error)
3328 return error;
3329
3330 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3331 &notinit);
3332 if (error)
3333 return error;
3334
3335 }
3336
3337 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003338
3339 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003340 * Set the failure fallback case to look in the selected AG as stream
3341 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003342 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003343 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003344 return 0;
3345}
3346
3347STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003348xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003349 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003350{
3351 xfs_mount_t *mp; /* mount point structure */
3352 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003353 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003354 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003355 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003356 xfs_alloc_arg_t args;
3357 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003358 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003359 int nullfb; /* true if ap->firstblock isn't set */
3360 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003361 int tryagain;
3362 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003363 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003364
Dave Chinnera99ebf42011-12-01 11:24:20 +00003365 ASSERT(ap->length);
3366
Nathan Scotta365bdd2006-03-14 13:34:16 +11003367 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003368
3369 /* stripe alignment for allocation is determined by mount parameters */
3370 stripe_align = 0;
3371 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3372 stripe_align = mp->m_swidth;
3373 else if (mp->m_dalign)
3374 stripe_align = mp->m_dalign;
3375
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003376 if (ap->flags & XFS_BMAPI_COWFORK)
3377 align = xfs_get_cowextsz_hint(ap->ip);
3378 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003379 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003380 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003381 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003382 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003383 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003384 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003385 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003386 }
Dave Chinner33177f052013-12-12 16:34:36 +11003387
3388
Dave Chinner0937e0f2011-09-18 20:40:57 +00003389 nullfb = *ap->firstblock == NULLFSBLOCK;
3390 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003391 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003392 if (xfs_alloc_is_userdata(ap->datatype) &&
3393 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003394 ag = xfs_filestream_lookup_ag(ap->ip);
3395 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003396 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003397 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003398 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003399 }
3400 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003401 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003402
3403 xfs_bmap_adjacent(ap);
3404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003406 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 * it's in the right allocation group.
3408 */
Dave Chinner3a756672011-09-18 20:40:58 +00003409 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 ;
3411 else
Dave Chinner3a756672011-09-18 20:40:58 +00003412 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 * Normal allocation, done through xfs_alloc_vextent.
3415 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003416 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003417 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003418 args.tp = ap->tp;
3419 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003420 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003421 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003422
3423 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003424 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003425 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003426 blen = 0;
3427 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003428 /*
3429 * Search for an allocation group with a single extent large
3430 * enough for the request. If one isn't found, then adjust
3431 * the minimum allocation size to the largest space found.
3432 */
Dave Chinner292378e2016-09-26 08:21:28 +10003433 if (xfs_alloc_is_userdata(ap->datatype) &&
3434 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003435 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3436 else
3437 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003438 if (error)
3439 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003440 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003441 if (xfs_inode_is_filestream(ap->ip))
3442 args.type = XFS_ALLOCTYPE_FIRST_AG;
3443 else
3444 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003445 args.total = args.minlen = ap->minlen;
3446 } else {
3447 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3448 args.total = ap->total;
3449 args.minlen = ap->minlen;
3450 }
David Chinner957d0eb2007-06-18 16:50:37 +10003451 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003452 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003453 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003454 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003455 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003456 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003457 args.prod = 1;
3458 args.mod = 0;
3459 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003460 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003461 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003462 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3463 }
3464 /*
3465 * If we are not low on available data blocks, and the
3466 * underlying logical volume manager is a stripe, and
3467 * the file offset is zero then try to allocate data
3468 * blocks on stripe unit boundary.
3469 * NOTE: ap->aeof is only set if the allocation length
3470 * is >= the stripe unit and the allocation offset is
3471 * at the end of file.
3472 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003473 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003474 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003475 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003476 atype = args.type;
3477 isaligned = 1;
3478 /*
3479 * Adjust for alignment
3480 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003481 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003482 args.minlen = blen - args.alignment;
3483 args.minalignslop = 0;
3484 } else {
3485 /*
3486 * First try an exact bno allocation.
3487 * If it fails then do a near or start bno
3488 * allocation with alignment turned on.
3489 */
3490 atype = args.type;
3491 tryagain = 1;
3492 args.type = XFS_ALLOCTYPE_THIS_BNO;
3493 args.alignment = 1;
3494 /*
3495 * Compute the minlen+alignment for the
3496 * next case. Set slop so that the value
3497 * of minlen+alignment+slop doesn't go up
3498 * between the calls.
3499 */
Dave Chinner33177f052013-12-12 16:34:36 +11003500 if (blen > stripe_align && blen <= args.maxlen)
3501 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003502 else
3503 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003504 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003505 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003506 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003507 args.minlen - 1;
3508 else
3509 args.minalignslop = 0;
3510 }
3511 } else {
3512 args.alignment = 1;
3513 args.minalignslop = 0;
3514 }
3515 args.minleft = ap->minleft;
3516 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003517 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003518 args.datatype = ap->datatype;
3519 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003520 args.ip = ap->ip;
3521
3522 error = xfs_alloc_vextent(&args);
3523 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003524 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003525
Nathan Scotta365bdd2006-03-14 13:34:16 +11003526 if (tryagain && args.fsbno == NULLFSBLOCK) {
3527 /*
3528 * Exact allocation failed. Now try with alignment
3529 * turned on.
3530 */
3531 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003532 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003533 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003534 args.minlen = nextminlen;
3535 args.minalignslop = 0;
3536 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 if ((error = xfs_alloc_vextent(&args)))
3538 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003539 }
3540 if (isaligned && args.fsbno == NULLFSBLOCK) {
3541 /*
3542 * allocation failed, so turn off alignment and
3543 * try again.
3544 */
3545 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003546 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003547 args.alignment = 0;
3548 if ((error = xfs_alloc_vextent(&args)))
3549 return error;
3550 }
3551 if (args.fsbno == NULLFSBLOCK && nullfb &&
3552 args.minlen > ap->minlen) {
3553 args.minlen = ap->minlen;
3554 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003555 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003556 if ((error = xfs_alloc_vextent(&args)))
3557 return error;
3558 }
3559 if (args.fsbno == NULLFSBLOCK && nullfb) {
3560 args.fsbno = 0;
3561 args.type = XFS_ALLOCTYPE_FIRST_AG;
3562 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003563 if ((error = xfs_alloc_vextent(&args)))
3564 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003565 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003566 }
3567 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003568 /*
3569 * check the allocation happened at the same or higher AG than
3570 * the first block that was allocated.
3571 */
3572 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003573 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3574 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003575
Dave Chinner3a756672011-09-18 20:40:58 +00003576 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003577 if (*ap->firstblock == NULLFSBLOCK)
3578 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003579 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003580 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003581 if (!(ap->flags & XFS_BMAPI_COWFORK))
3582 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003583 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3584 if (ap->wasdel)
3585 ap->ip->i_delayed_blks -= args.len;
3586 /*
3587 * Adjust the disk quota also. This was reserved
3588 * earlier.
3589 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003590 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003591 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3592 XFS_TRANS_DQ_BCOUNT,
3593 (long) args.len);
3594 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003595 ap->blkno = NULLFSBLOCK;
3596 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 }
3598 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003599}
3600
3601/*
3602 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3603 * It figures out where to ask the underlying allocator to put the new extent.
3604 */
3605STATIC int
3606xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003607 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003608{
Dave Chinner292378e2016-09-26 08:21:28 +10003609 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3610 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003611 return xfs_bmap_rtalloc(ap);
3612 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613}
3614
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003615/* Trim extent to fit a logical block range. */
3616void
3617xfs_trim_extent(
3618 struct xfs_bmbt_irec *irec,
3619 xfs_fileoff_t bno,
3620 xfs_filblks_t len)
3621{
3622 xfs_fileoff_t distance;
3623 xfs_fileoff_t end = bno + len;
3624
3625 if (irec->br_startoff + irec->br_blockcount <= bno ||
3626 irec->br_startoff >= end) {
3627 irec->br_blockcount = 0;
3628 return;
3629 }
3630
3631 if (irec->br_startoff < bno) {
3632 distance = bno - irec->br_startoff;
3633 if (isnullstartblock(irec->br_startblock))
3634 irec->br_startblock = DELAYSTARTBLOCK;
3635 if (irec->br_startblock != DELAYSTARTBLOCK &&
3636 irec->br_startblock != HOLESTARTBLOCK)
3637 irec->br_startblock += distance;
3638 irec->br_startoff += distance;
3639 irec->br_blockcount -= distance;
3640 }
3641
3642 if (end < irec->br_startoff + irec->br_blockcount) {
3643 distance = irec->br_startoff + irec->br_blockcount - end;
3644 irec->br_blockcount -= distance;
3645 }
3646}
3647
Brian Foster40214d12017-10-13 09:47:46 -07003648/* trim extent to within eof */
3649void
3650xfs_trim_extent_eof(
3651 struct xfs_bmbt_irec *irec,
3652 struct xfs_inode *ip)
3653
3654{
3655 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3656 i_size_read(VFS_I(ip))));
3657}
3658
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003660 * Trim the returned map to the required bounds
3661 */
3662STATIC void
3663xfs_bmapi_trim_map(
3664 struct xfs_bmbt_irec *mval,
3665 struct xfs_bmbt_irec *got,
3666 xfs_fileoff_t *bno,
3667 xfs_filblks_t len,
3668 xfs_fileoff_t obno,
3669 xfs_fileoff_t end,
3670 int n,
3671 int flags)
3672{
3673 if ((flags & XFS_BMAPI_ENTIRE) ||
3674 got->br_startoff + got->br_blockcount <= obno) {
3675 *mval = *got;
3676 if (isnullstartblock(got->br_startblock))
3677 mval->br_startblock = DELAYSTARTBLOCK;
3678 return;
3679 }
3680
3681 if (obno > *bno)
3682 *bno = obno;
3683 ASSERT((*bno >= obno) || (n == 0));
3684 ASSERT(*bno < end);
3685 mval->br_startoff = *bno;
3686 if (isnullstartblock(got->br_startblock))
3687 mval->br_startblock = DELAYSTARTBLOCK;
3688 else
3689 mval->br_startblock = got->br_startblock +
3690 (*bno - got->br_startoff);
3691 /*
3692 * Return the minimum of what we got and what we asked for for
3693 * the length. We can use the len variable here because it is
3694 * modified below and we could have been there before coming
3695 * here if the first part of the allocation didn't overlap what
3696 * was asked for.
3697 */
3698 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3699 got->br_blockcount - (*bno - got->br_startoff));
3700 mval->br_state = got->br_state;
3701 ASSERT(mval->br_blockcount <= len);
3702 return;
3703}
3704
3705/*
3706 * Update and validate the extent map to return
3707 */
3708STATIC void
3709xfs_bmapi_update_map(
3710 struct xfs_bmbt_irec **map,
3711 xfs_fileoff_t *bno,
3712 xfs_filblks_t *len,
3713 xfs_fileoff_t obno,
3714 xfs_fileoff_t end,
3715 int *n,
3716 int flags)
3717{
3718 xfs_bmbt_irec_t *mval = *map;
3719
3720 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3721 ((mval->br_startoff + mval->br_blockcount) <= end));
3722 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3723 (mval->br_startoff < obno));
3724
3725 *bno = mval->br_startoff + mval->br_blockcount;
3726 *len = end - *bno;
3727 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3728 /* update previous map with new information */
3729 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3730 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3731 ASSERT(mval->br_state == mval[-1].br_state);
3732 mval[-1].br_blockcount = mval->br_blockcount;
3733 mval[-1].br_state = mval->br_state;
3734 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3735 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3736 mval[-1].br_startblock != HOLESTARTBLOCK &&
3737 mval->br_startblock == mval[-1].br_startblock +
3738 mval[-1].br_blockcount &&
3739 ((flags & XFS_BMAPI_IGSTATE) ||
3740 mval[-1].br_state == mval->br_state)) {
3741 ASSERT(mval->br_startoff ==
3742 mval[-1].br_startoff + mval[-1].br_blockcount);
3743 mval[-1].br_blockcount += mval->br_blockcount;
3744 } else if (*n > 0 &&
3745 mval->br_startblock == DELAYSTARTBLOCK &&
3746 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3747 mval->br_startoff ==
3748 mval[-1].br_startoff + mval[-1].br_blockcount) {
3749 mval[-1].br_blockcount += mval->br_blockcount;
3750 mval[-1].br_state = mval->br_state;
3751 } else if (!((*n == 0) &&
3752 ((mval->br_startoff + mval->br_blockcount) <=
3753 obno))) {
3754 mval++;
3755 (*n)++;
3756 }
3757 *map = mval;
3758}
3759
3760/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003761 * Map file blocks to filesystem blocks without allocation.
3762 */
3763int
3764xfs_bmapi_read(
3765 struct xfs_inode *ip,
3766 xfs_fileoff_t bno,
3767 xfs_filblks_t len,
3768 struct xfs_bmbt_irec *mval,
3769 int *nmap,
3770 int flags)
3771{
3772 struct xfs_mount *mp = ip->i_mount;
3773 struct xfs_ifork *ifp;
3774 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003775 xfs_fileoff_t obno;
3776 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003777 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003778 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003779 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003780 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003781 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003782
3783 ASSERT(*nmap >= 1);
3784 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003785 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003786 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003787
3788 if (unlikely(XFS_TEST_ERROR(
3789 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3790 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003791 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003792 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003793 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003794 }
3795
3796 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003797 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003798
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003799 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003800
3801 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003802
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003803 /* No CoW fork? Return a hole. */
3804 if (whichfork == XFS_COW_FORK && !ifp) {
3805 mval->br_startoff = bno;
3806 mval->br_startblock = HOLESTARTBLOCK;
3807 mval->br_blockcount = len;
3808 mval->br_state = XFS_EXT_NORM;
3809 *nmap = 1;
3810 return 0;
3811 }
3812
Dave Chinner5c8ed202011-09-18 20:40:45 +00003813 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3814 error = xfs_iread_extents(NULL, ip, whichfork);
3815 if (error)
3816 return error;
3817 }
3818
Christoph Hellwig334f3422016-11-24 11:39:43 +11003819 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
3820 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003821 end = bno + len;
3822 obno = bno;
3823
3824 while (bno < end && n < *nmap) {
3825 /* Reading past eof, act as though there's a hole up to end. */
3826 if (eof)
3827 got.br_startoff = end;
3828 if (got.br_startoff > bno) {
3829 /* Reading in a hole. */
3830 mval->br_startoff = bno;
3831 mval->br_startblock = HOLESTARTBLOCK;
3832 mval->br_blockcount =
3833 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3834 mval->br_state = XFS_EXT_NORM;
3835 bno += mval->br_blockcount;
3836 len -= mval->br_blockcount;
3837 mval++;
3838 n++;
3839 continue;
3840 }
3841
3842 /* set up the extent map to return. */
3843 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3844 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3845
3846 /* If we're done, stop now. */
3847 if (bno >= end || n >= *nmap)
3848 break;
3849
3850 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11003851 if (!xfs_iext_get_extent(ifp, ++idx, &got))
3852 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003853 }
3854 *nmap = n;
3855 return 0;
3856}
3857
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003858/*
3859 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3860 * global pool and the extent inserted into the inode in-core extent tree.
3861 *
3862 * On entry, got refers to the first extent beyond the offset of the extent to
3863 * allocate or eof is specified if no such extent exists. On return, got refers
3864 * to the extent record that was inserted to the inode fork.
3865 *
3866 * Note that the allocated extent may have been merged with contiguous extents
3867 * during insertion into the inode fork. Thus, got does not reflect the current
3868 * state of the inode fork on return. If necessary, the caller can use lastx to
3869 * look up the updated record in the inode fork.
3870 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003871int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003872xfs_bmapi_reserve_delalloc(
3873 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003874 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003875 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003876 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003877 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003878 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003879 xfs_extnum_t *lastx,
3880 int eof)
3881{
3882 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003883 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003884 xfs_extlen_t alen;
3885 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003886 char rt = XFS_IS_REALTIME_INODE(ip);
3887 xfs_extlen_t extsz;
3888 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003889 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003890
Brian Foster974ae922016-11-28 14:57:42 +11003891 /*
3892 * Cap the alloc length. Keep track of prealloc so we know whether to
3893 * tag the inode before we return.
3894 */
3895 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003896 if (!eof)
3897 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003898 if (prealloc && alen >= len)
3899 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003900
3901 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003902 if (whichfork == XFS_COW_FORK)
3903 extsz = xfs_get_cowextsz_hint(ip);
3904 else
3905 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003906 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003907 struct xfs_bmbt_irec prev;
3908
3909 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
3910 prev.br_startoff = NULLFILEOFF;
3911
3912 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003913 1, 0, &aoff, &alen);
3914 ASSERT(!error);
3915 }
3916
3917 if (rt)
3918 extsz = alen / mp->m_sb.sb_rextsize;
3919
3920 /*
3921 * Make a transaction-less quota reservation for delayed allocation
3922 * blocks. This number gets adjusted later. We return if we haven't
3923 * allocated blocks already inside this loop.
3924 */
3925 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
3926 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
3927 if (error)
3928 return error;
3929
3930 /*
3931 * Split changing sb for alen and indlen since they could be coming
3932 * from different places.
3933 */
3934 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3935 ASSERT(indlen > 0);
3936
3937 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11003938 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003939 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11003940 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003941 }
3942
3943 if (error)
3944 goto out_unreserve_quota;
3945
Dave Chinner0d485ad2015-02-23 21:22:03 +11003946 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003947 if (error)
3948 goto out_unreserve_blocks;
3949
3950
3951 ip->i_delayed_blks += alen;
3952
3953 got->br_startoff = aoff;
3954 got->br_startblock = nullstartblock(indlen);
3955 got->br_blockcount = alen;
3956 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003957
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003958 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003959
Brian Foster974ae922016-11-28 14:57:42 +11003960 /*
3961 * Tag the inode if blocks were preallocated. Note that COW fork
3962 * preallocation can occur at the start or end of the extent, even when
3963 * prealloc == 0, so we must also check the aligned offset and length.
3964 */
3965 if (whichfork == XFS_DATA_FORK && prealloc)
3966 xfs_inode_set_eofblocks_tag(ip);
3967 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
3968 xfs_inode_set_cowblocks_tag(ip);
3969
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003970 return 0;
3971
3972out_unreserve_blocks:
3973 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11003974 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003975 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11003976 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003977out_unreserve_quota:
3978 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10003979 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003980 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
3981 return error;
3982}
3983
Dave Chinnercf11da92014-07-15 07:08:24 +10003984static int
3985xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10003986 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003987{
3988 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003989 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003990 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00003991 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003992 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003993
Dave Chinnera99ebf42011-12-01 11:24:20 +00003994 ASSERT(bma->length > 0);
3995
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003996 /*
3997 * For the wasdelay case, we could also just allocate the stuff asked
3998 * for in this bmap call but that wouldn't be as good.
3999 */
4000 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004001 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4002 bma->offset = bma->got.br_startoff;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004003 if (bma->idx)
4004 xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004005 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004006 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004007 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004008 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004009 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004010 }
4011
4012 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004013 * Set the data type being allocated. For the data fork, the first data
4014 * in the file is treated differently to all other allocations. For the
4015 * attribute fork, we only need to ensure the allocated range is not on
4016 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004017 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004018 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004019 bma->datatype = XFS_ALLOC_NOBUSY;
4020 if (whichfork == XFS_DATA_FORK) {
4021 if (bma->offset == 0)
4022 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4023 else
4024 bma->datatype |= XFS_ALLOC_USERDATA;
4025 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004026 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004027 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004028 }
4029
Dave Chinnere04426b2012-10-05 11:06:59 +10004030 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004031
4032 /*
4033 * Only want to do the alignment at the eof if it is userdata and
4034 * allocation length is larger than a stripe unit.
4035 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004036 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004037 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004038 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004039 if (error)
4040 return error;
4041 }
4042
4043 error = xfs_bmap_alloc(bma);
4044 if (error)
4045 return error;
4046
Dave Chinner29c8d172011-09-18 20:41:00 +00004047 if (bma->cur)
4048 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004049 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004050 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004051 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4052 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4053 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004054 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004055 }
4056 /*
4057 * Bump the number of extents we've allocated
4058 * in this call.
4059 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004060 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004061
Dave Chinner29c8d172011-09-18 20:41:00 +00004062 if (bma->cur)
4063 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004064 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4065
Dave Chinner963c30c2011-09-18 20:40:59 +00004066 bma->got.br_startoff = bma->offset;
4067 bma->got.br_startblock = bma->blkno;
4068 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004069 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004070
4071 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004072 * In the data fork, a wasdelay extent has been initialized, so
4073 * shouldn't be flagged as unwritten.
4074 *
4075 * For the cow fork, however, we convert delalloc reservations
4076 * (extents allocated for speculative preallocation) to
4077 * allocated unwritten extents, and only convert the unwritten
4078 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004079 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004080 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4081 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004082 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004083 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004084
Christoph Hellwigc6534242011-09-18 20:41:05 +00004085 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004086 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004087 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004088 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4089 whichfork, &bma->idx, &bma->cur, &bma->got,
4090 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004091
Christoph Hellwigc315c902011-09-18 20:41:02 +00004092 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004093 if (error)
4094 return error;
4095
4096 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004097 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4098 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4099 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004100 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004101 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004102
Dave Chinner963c30c2011-09-18 20:40:59 +00004103 ASSERT(bma->got.br_startoff <= bma->offset);
4104 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4105 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004106 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4107 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004108 return 0;
4109}
4110
Dave Chinnerb447fe52011-09-18 20:40:51 +00004111STATIC int
4112xfs_bmapi_convert_unwritten(
4113 struct xfs_bmalloca *bma,
4114 struct xfs_bmbt_irec *mval,
4115 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004116 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004117{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004118 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004119 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004120 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004121 int error;
4122
Dave Chinnerb447fe52011-09-18 20:40:51 +00004123 /* check if we need to do unwritten->real conversion */
4124 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4125 (flags & XFS_BMAPI_PREALLOC))
4126 return 0;
4127
4128 /* check if we need to do real->unwritten conversion */
4129 if (mval->br_state == XFS_EXT_NORM &&
4130 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4131 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4132 return 0;
4133
4134 /*
4135 * Modify (by adding) the state flag, if writing.
4136 */
4137 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004138 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4139 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004140 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004141 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004142 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004143 }
4144 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4145 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4146
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004147 /*
4148 * Before insertion into the bmbt, zero the range being converted
4149 * if required.
4150 */
4151 if (flags & XFS_BMAPI_ZERO) {
4152 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4153 mval->br_blockcount);
4154 if (error)
4155 return error;
4156 }
4157
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004158 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4159 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004160 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004161 /*
4162 * Log the inode core unconditionally in the unwritten extent conversion
4163 * path because the conversion might not have done so (e.g., if the
4164 * extent count hasn't changed). We need to make sure the inode is dirty
4165 * in the transaction for the sake of fsync(), even if nothing has
4166 * changed, because fsync() will not force the log for this transaction
4167 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004168 *
4169 * Note: If we're only converting cow fork extents, there aren't
4170 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004171 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004172 if (whichfork != XFS_COW_FORK)
4173 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004174 if (error)
4175 return error;
4176
4177 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004178 * Update our extent pointer, given that
4179 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4180 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004181 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004182 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004183
4184 /*
4185 * We may have combined previously unwritten space with written space,
4186 * so generate another request.
4187 */
4188 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004189 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004190 return 0;
4191}
4192
Christoph Hellwig44032802011-09-18 20:40:48 +00004193/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004194 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4195 * extent state if necessary. Details behaviour is controlled by the flags
4196 * parameter. Only allocates blocks from a single allocation group, to avoid
4197 * locking problems.
4198 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 * The returned value in "firstblock" from the first call in a transaction
4200 * must be remembered and presented to subsequent calls in "firstblock".
4201 * An upper bound for the number of blocks to be allocated is supplied to
4202 * the first call in "total"; if no allocation group has that many free
4203 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4204 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004205int
4206xfs_bmapi_write(
4207 struct xfs_trans *tp, /* transaction pointer */
4208 struct xfs_inode *ip, /* incore inode */
4209 xfs_fileoff_t bno, /* starting file offs. mapped */
4210 xfs_filblks_t len, /* length to map in file */
4211 int flags, /* XFS_BMAPI_... */
4212 xfs_fsblock_t *firstblock, /* first allocated block
4213 controls a.g. for allocs */
4214 xfs_extlen_t total, /* total blocks needed */
4215 struct xfs_bmbt_irec *mval, /* output: map values */
4216 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004217 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004219 struct xfs_mount *mp = ip->i_mount;
4220 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004221 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004222 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004223 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004224 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004225 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004226 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004227 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004228
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004230 xfs_fileoff_t orig_bno; /* original block number value */
4231 int orig_flags; /* original flags arg value */
4232 xfs_filblks_t orig_len; /* original value of len arg */
4233 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4234 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
4236 orig_bno = bno;
4237 orig_len = len;
4238 orig_flags = flags;
4239 orig_mval = mval;
4240 orig_nmap = *nmap;
4241#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004242 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004245 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4246 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004247 ASSERT(tp != NULL ||
4248 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4249 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004250 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004251 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004252 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004253 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004254
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004255 /* zeroing is for currently only for data extents, not metadata */
4256 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4257 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4258 /*
4259 * we can allocate unwritten extents or pre-zero allocated blocks,
4260 * but it makes no sense to do both at once. This would result in
4261 * zeroing the unwritten extent twice, but it still being an
4262 * unwritten extent....
4263 */
4264 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4265 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4266
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 if (unlikely(XFS_TEST_ERROR(
4268 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004269 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004270 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004271 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004272 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004276 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004277
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004279
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004280 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004281
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004282 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004284 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004286 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004287 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004288 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004289 }
4290
4291 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4292 error = xfs_iread_extents(tp, ip, whichfork);
4293 if (error)
4294 goto error0;
4295 }
4296
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 n = 0;
4298 end = bno + len;
4299 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004300
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004301 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4302 eof = true;
4303 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4304 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004305 bma.tp = tp;
4306 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004307 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004308 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004309 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004310 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004311
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004313 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004314
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004315 /* in hole or beyoned EOF? */
4316 if (eof || bma.got.br_startoff > bno) {
4317 if (flags & XFS_BMAPI_DELALLOC) {
4318 /*
4319 * For the COW fork we can reasonably get a
4320 * request for converting an extent that races
4321 * with other threads already having converted
4322 * part of it, as there converting COW to
4323 * regular blocks is not protected using the
4324 * IOLOCK.
4325 */
4326 ASSERT(flags & XFS_BMAPI_COWFORK);
4327 if (!(flags & XFS_BMAPI_COWFORK)) {
4328 error = -EIO;
4329 goto error0;
4330 }
4331
4332 if (eof || bno >= end)
4333 break;
4334 } else {
4335 need_alloc = true;
4336 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004337 } else if (isnullstartblock(bma.got.br_startblock)) {
4338 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004339 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004340
4341 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 * First, deal with the hole before the allocated space
4343 * that we found, if any.
4344 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004345 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004346 bma.eof = eof;
4347 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4348 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004349 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004350 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004351
Dave Chinnera99ebf42011-12-01 11:24:20 +00004352 /*
4353 * There's a 32/64 bit type mismatch between the
4354 * allocation length request (which can be 64 bits in
4355 * length) and the bma length request, which is
4356 * xfs_extlen_t and therefore 32 bits. Hence we have to
4357 * check for 32-bit overflows and handle them here.
4358 */
4359 if (len > (xfs_filblks_t)MAXEXTLEN)
4360 bma.length = MAXEXTLEN;
4361 else
4362 bma.length = len;
4363
4364 ASSERT(len > 0);
4365 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004366 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 if (error)
4368 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004369 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004370 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004371
4372 /*
4373 * If this is a CoW allocation, record the data in
4374 * the refcount btree for orphan recovery.
4375 */
4376 if (whichfork == XFS_COW_FORK) {
4377 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4378 bma.blkno, bma.length);
4379 if (error)
4380 goto error0;
4381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004383
Dave Chinneraef9a892011-09-18 20:40:44 +00004384 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004385 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4386 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387
Dave Chinnerb447fe52011-09-18 20:40:51 +00004388 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004389 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004390 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004391 continue;
4392 if (error)
4393 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Dave Chinneraef9a892011-09-18 20:40:44 +00004395 /* update the extent map to return */
4396 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 /*
4399 * If we're done, stop now. Stop when we've allocated
4400 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4401 * the transaction may get too big.
4402 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004403 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004405
4406 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004407 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004408 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4409 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004412
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 /*
4414 * Transform from btree to extents, give it cur.
4415 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004416 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004417 int tmp_logflags = 0;
4418
Dave Chinner29c8d172011-09-18 20:41:00 +00004419 ASSERT(bma.cur);
4420 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004422 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 if (error)
4424 goto error0;
4425 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004428 XFS_IFORK_NEXTENTS(ip, whichfork) >
4429 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431error0:
4432 /*
4433 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004434 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004436 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004438 bma.logflags &= ~xfs_ilog_fext(whichfork);
4439 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004441 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 /*
4443 * Log whatever the flags say, even if error. Otherwise we might miss
4444 * detecting a case where the data is changed, there's an error,
4445 * and it's not logged so we don't shutdown when we should.
4446 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004447 if (bma.logflags)
4448 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004449
Dave Chinner29c8d172011-09-18 20:41:00 +00004450 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 if (!error) {
4452 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004453 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004455 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004456 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004458 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4460 }
4461 if (!error)
4462 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4463 orig_nmap, *nmap);
4464 return error;
4465}
4466
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004467static int
4468xfs_bmapi_remap(
4469 struct xfs_trans *tp,
4470 struct xfs_inode *ip,
4471 xfs_fileoff_t bno,
4472 xfs_filblks_t len,
4473 xfs_fsblock_t startblock,
4474 struct xfs_defer_ops *dfops)
4475{
4476 struct xfs_mount *mp = ip->i_mount;
4477 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4478 struct xfs_btree_cur *cur = NULL;
4479 xfs_fsblock_t firstblock = NULLFSBLOCK;
4480 struct xfs_bmbt_irec got;
4481 xfs_extnum_t idx;
4482 int logflags = 0, error;
4483
4484 ASSERT(len > 0);
4485 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4486 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4487
4488 if (unlikely(XFS_TEST_ERROR(
4489 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4490 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004491 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004492 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4493 return -EFSCORRUPTED;
4494 }
4495
4496 if (XFS_FORCED_SHUTDOWN(mp))
4497 return -EIO;
4498
4499 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4500 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4501 if (error)
4502 return error;
4503 }
4504
4505 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4506 /* make sure we only reflink into a hole. */
4507 ASSERT(got.br_startoff > bno);
4508 ASSERT(got.br_startoff - bno >= len);
4509 }
4510
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004511 ip->i_d.di_nblocks += len;
4512 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004513
4514 if (ifp->if_flags & XFS_IFBROOT) {
4515 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4516 cur->bc_private.b.firstblock = firstblock;
4517 cur->bc_private.b.dfops = dfops;
4518 cur->bc_private.b.flags = 0;
4519 }
4520
4521 got.br_startoff = bno;
4522 got.br_startblock = startblock;
4523 got.br_blockcount = len;
4524 got.br_state = XFS_EXT_NORM;
4525
4526 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4527 &got, &firstblock, dfops, &logflags);
4528 if (error)
4529 goto error0;
4530
4531 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4532 int tmp_logflags = 0;
4533
4534 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4535 &tmp_logflags, XFS_DATA_FORK);
4536 logflags |= tmp_logflags;
4537 }
4538
4539error0:
4540 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4541 logflags &= ~XFS_ILOG_DEXT;
4542 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4543 logflags &= ~XFS_ILOG_DBROOT;
4544
4545 if (logflags)
4546 xfs_trans_log_inode(tp, ip, logflags);
4547 if (cur) {
4548 xfs_btree_del_cursor(cur,
4549 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4550 }
4551 return error;
4552}
4553
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004555 * When a delalloc extent is split (e.g., due to a hole punch), the original
4556 * indlen reservation must be shared across the two new extents that are left
4557 * behind.
4558 *
4559 * Given the original reservation and the worst case indlen for the two new
4560 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004561 * reservation fairly across the two new extents. If necessary, steal available
4562 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4563 * ores == 1). The number of stolen blocks is returned. The availability and
4564 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004565 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004566static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004567xfs_bmap_split_indlen(
4568 xfs_filblks_t ores, /* original res. */
4569 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004570 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4571 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004572{
4573 xfs_filblks_t len1 = *indlen1;
4574 xfs_filblks_t len2 = *indlen2;
4575 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004576 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004577 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004578
4579 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004580 * Steal as many blocks as we can to try and satisfy the worst case
4581 * indlen for both new extents.
4582 */
Brian Foster75d65362017-02-13 22:48:30 -08004583 if (ores < nres && avail)
4584 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4585 ores += stolen;
4586
4587 /* nothing else to do if we've satisfied the new reservation */
4588 if (ores >= nres)
4589 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004590
4591 /*
Brian Foster75d65362017-02-13 22:48:30 -08004592 * We can't meet the total required reservation for the two extents.
4593 * Calculate the percent of the overall shortage between both extents
4594 * and apply this percentage to each of the requested indlen values.
4595 * This distributes the shortage fairly and reduces the chances that one
4596 * of the two extents is left with nothing when extents are repeatedly
4597 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004598 */
Brian Foster75d65362017-02-13 22:48:30 -08004599 resfactor = (ores * 100);
4600 do_div(resfactor, nres);
4601 len1 *= resfactor;
4602 do_div(len1, 100);
4603 len2 *= resfactor;
4604 do_div(len2, 100);
4605 ASSERT(len1 + len2 <= ores);
4606 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4607
4608 /*
4609 * Hand out the remainder to each extent. If one of the two reservations
4610 * is zero, we want to make sure that one gets a block first. The loop
4611 * below starts with len1, so hand len2 a block right off the bat if it
4612 * is zero.
4613 */
4614 ores -= (len1 + len2);
4615 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4616 if (ores && !len2 && *indlen2) {
4617 len2++;
4618 ores--;
4619 }
4620 while (ores) {
4621 if (len1 < *indlen1) {
4622 len1++;
4623 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004624 }
Brian Foster75d65362017-02-13 22:48:30 -08004625 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004626 break;
Brian Foster75d65362017-02-13 22:48:30 -08004627 if (len2 < *indlen2) {
4628 len2++;
4629 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004630 }
4631 }
4632
4633 *indlen1 = len1;
4634 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004635
4636 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004637}
4638
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004639int
4640xfs_bmap_del_extent_delay(
4641 struct xfs_inode *ip,
4642 int whichfork,
4643 xfs_extnum_t *idx,
4644 struct xfs_bmbt_irec *got,
4645 struct xfs_bmbt_irec *del)
4646{
4647 struct xfs_mount *mp = ip->i_mount;
4648 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4649 struct xfs_bmbt_irec new;
4650 int64_t da_old, da_new, da_diff = 0;
4651 xfs_fileoff_t del_endoff, got_endoff;
4652 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004653 int state = xfs_bmap_fork_to_state(whichfork);
4654 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004655 bool isrt;
4656
4657 XFS_STATS_INC(mp, xs_del_exlist);
4658
4659 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4660 del_endoff = del->br_startoff + del->br_blockcount;
4661 got_endoff = got->br_startoff + got->br_blockcount;
4662 da_old = startblockval(got->br_startblock);
4663 da_new = 0;
4664
4665 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004666 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004667 ASSERT(del->br_blockcount > 0);
4668 ASSERT(got->br_startoff <= del->br_startoff);
4669 ASSERT(got_endoff >= del_endoff);
4670
4671 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004672 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004673
4674 do_div(rtexts, mp->m_sb.sb_rextsize);
4675 xfs_mod_frextents(mp, rtexts);
4676 }
4677
4678 /*
4679 * Update the inode delalloc counter now and wait to update the
4680 * sb counters as we might have to borrow some blocks for the
4681 * indirect block accounting.
4682 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004683 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4684 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004685 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004686 if (error)
4687 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004688 ip->i_delayed_blks -= del->br_blockcount;
4689
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004690 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004691 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004692 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004693 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004694
Christoph Hellwig0173c682017-10-17 14:16:22 -07004695 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4696 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004697 /*
4698 * Matches the whole extent. Delete the entry.
4699 */
4700 xfs_iext_remove(ip, *idx, 1, state);
4701 --*idx;
4702 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004703 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004704 /*
4705 * Deleting the first part of the extent.
4706 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004707 got->br_startoff = del_endoff;
4708 got->br_blockcount -= del->br_blockcount;
4709 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4710 got->br_blockcount), da_old);
4711 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004712 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004713 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004714 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004715 /*
4716 * Deleting the last part of the extent.
4717 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004718 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4719 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4720 got->br_blockcount), da_old);
4721 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004722 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004723 break;
4724 case 0:
4725 /*
4726 * Deleting the middle of the extent.
4727 *
4728 * Distribute the original indlen reservation across the two new
4729 * extents. Steal blocks from the deleted extent if necessary.
4730 * Stealing blocks simply fudges the fdblocks accounting below.
4731 * Warn if either of the new indlen reservations is zero as this
4732 * can lead to delalloc problems.
4733 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004734 got->br_blockcount = del->br_startoff - got->br_startoff;
4735 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4736
4737 new.br_blockcount = got_endoff - del_endoff;
4738 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4739
4740 WARN_ON_ONCE(!got_indlen || !new_indlen);
4741 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4742 del->br_blockcount);
4743
4744 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004745 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004746
4747 new.br_startoff = del_endoff;
4748 new.br_state = got->br_state;
4749 new.br_startblock = nullstartblock((int)new_indlen);
4750
4751 ++*idx;
4752 xfs_iext_insert(ip, *idx, 1, &new, state);
4753
4754 da_new = got_indlen + new_indlen - stolen;
4755 del->br_blockcount -= stolen;
4756 break;
4757 }
4758
4759 ASSERT(da_old >= da_new);
4760 da_diff = da_old - da_new;
4761 if (!isrt)
4762 da_diff += del->br_blockcount;
4763 if (da_diff)
4764 xfs_mod_fdblocks(mp, da_diff, false);
4765 return error;
4766}
4767
4768void
4769xfs_bmap_del_extent_cow(
4770 struct xfs_inode *ip,
4771 xfs_extnum_t *idx,
4772 struct xfs_bmbt_irec *got,
4773 struct xfs_bmbt_irec *del)
4774{
4775 struct xfs_mount *mp = ip->i_mount;
4776 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4777 struct xfs_bmbt_irec new;
4778 xfs_fileoff_t del_endoff, got_endoff;
4779 int state = BMAP_COWFORK;
4780
4781 XFS_STATS_INC(mp, xs_del_exlist);
4782
4783 del_endoff = del->br_startoff + del->br_blockcount;
4784 got_endoff = got->br_startoff + got->br_blockcount;
4785
4786 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004787 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004788 ASSERT(del->br_blockcount > 0);
4789 ASSERT(got->br_startoff <= del->br_startoff);
4790 ASSERT(got_endoff >= del_endoff);
4791 ASSERT(!isnullstartblock(got->br_startblock));
4792
4793 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004794 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004795 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004796 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004797
Christoph Hellwig0173c682017-10-17 14:16:22 -07004798 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4799 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004800 /*
4801 * Matches the whole extent. Delete the entry.
4802 */
4803 xfs_iext_remove(ip, *idx, 1, state);
4804 --*idx;
4805 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004806 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004807 /*
4808 * Deleting the first part of the extent.
4809 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004810 got->br_startoff = del_endoff;
4811 got->br_blockcount -= del->br_blockcount;
4812 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004813 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004814 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004815 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004816 /*
4817 * Deleting the last part of the extent.
4818 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004819 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004820 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004821 break;
4822 case 0:
4823 /*
4824 * Deleting the middle of the extent.
4825 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004826 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004827 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004828
4829 new.br_startoff = del_endoff;
4830 new.br_blockcount = got_endoff - del_endoff;
4831 new.br_state = got->br_state;
4832 new.br_startblock = del->br_startblock + del->br_blockcount;
4833
4834 ++*idx;
4835 xfs_iext_insert(ip, *idx, 1, &new, state);
4836 break;
4837 }
4838}
4839
Brian Fostera9bd24a2016-03-15 11:42:46 +11004840/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004841 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004842 * after removing space.
Dave Chinner9e5987a2013-02-25 12:31:26 +11004843 */
4844STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004845xfs_bmap_del_extent_real(
Dave Chinner9e5987a2013-02-25 12:31:26 +11004846 xfs_inode_t *ip, /* incore inode pointer */
4847 xfs_trans_t *tp, /* current transaction pointer */
4848 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004849 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004850 xfs_btree_cur_t *cur, /* if null, not a btree */
4851 xfs_bmbt_irec_t *del, /* data to remove from extents */
4852 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004853 int whichfork, /* data or attr fork */
4854 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004855{
Dave Chinner9e5987a2013-02-25 12:31:26 +11004856 xfs_fsblock_t del_endblock=0; /* first block past del */
4857 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004858 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004859 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004860 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004861 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004862 xfs_fileoff_t got_endoff; /* first offset past got */
4863 int i; /* temp state */
4864 xfs_ifork_t *ifp; /* inode fork pointer */
4865 xfs_mount_t *mp; /* mount structure */
4866 xfs_filblks_t nblks; /* quota/sb block count */
4867 xfs_bmbt_irec_t new; /* new record to be inserted */
4868 /* REFERENCED */
4869 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004870 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004871 struct xfs_bmbt_irec old;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004872
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004873 mp = ip->i_mount;
4874 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004875
Dave Chinner9e5987a2013-02-25 12:31:26 +11004876 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11004877 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004878 ASSERT(del->br_blockcount > 0);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004879 xfs_iext_get_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004880 ASSERT(got.br_startoff <= del->br_startoff);
4881 del_endoff = del->br_startoff + del->br_blockcount;
4882 got_endoff = got.br_startoff + got.br_blockcount;
4883 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004884 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004885 qfield = 0;
4886 error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004887
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004888 /*
4889 * If it's the case where the directory code is running with no block
4890 * reservation, and the deleted block is in the middle of its extent,
4891 * and the resulting insert of an extent would cause transformation to
4892 * btree format, then reject it. The calling code will then swap blocks
4893 * around instead. We have to do this now, rather than waiting for the
4894 * conversion to btree format, since the transaction will be dirty then.
4895 */
4896 if (tp->t_blk_res == 0 &&
4897 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4898 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4899 XFS_IFORK_MAXEXT(ip, whichfork) &&
4900 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4901 return -ENOSPC;
4902
4903 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004904 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4905 xfs_fsblock_t bno;
4906 xfs_filblks_t len;
4907
4908 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
4909 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
4910 bno = del->br_startblock;
4911 len = del->br_blockcount;
4912 do_div(bno, mp->m_sb.sb_rextsize);
4913 do_div(len, mp->m_sb.sb_rextsize);
4914 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4915 if (error)
4916 goto done;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004917 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004918 nblks = len * mp->m_sb.sb_rextsize;
4919 qfield = XFS_TRANS_DQ_RTBCOUNT;
4920 } else {
4921 do_fx = 1;
4922 nblks = del->br_blockcount;
4923 qfield = XFS_TRANS_DQ_BCOUNT;
4924 }
4925
4926 del_endblock = del->br_startblock + del->br_blockcount;
4927 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07004928 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004929 if (error)
4930 goto done;
4931 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004932 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004933
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004934 if (got.br_startoff == del->br_startoff)
4935 state |= BMAP_LEFT_FILLING;
4936 if (got_endoff == del_endoff)
4937 state |= BMAP_RIGHT_FILLING;
4938
4939 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4940 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004941 /*
4942 * Matches the whole extent. Delete the entry.
4943 */
Christoph Hellwig35e62da2017-10-19 11:04:43 -07004944 xfs_iext_remove(ip, *idx, 1, state);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004945 --*idx;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004946
4947 XFS_IFORK_NEXT_SET(ip, whichfork,
4948 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4949 flags |= XFS_ILOG_CORE;
4950 if (!cur) {
4951 flags |= xfs_ilog_fext(whichfork);
4952 break;
4953 }
4954 if ((error = xfs_btree_delete(cur, &i)))
4955 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004956 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004957 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004958 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004959 /*
4960 * Deleting the first part of the extent.
4961 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004962 got.br_startoff = del_endoff;
4963 got.br_startblock = del_endblock;
4964 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004965 xfs_iext_update_extent(ip, state, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004966 if (!cur) {
4967 flags |= xfs_ilog_fext(whichfork);
4968 break;
4969 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004970 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004971 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004972 goto done;
4973 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004974 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004975 /*
4976 * Deleting the last part of the extent.
4977 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004978 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004979 xfs_iext_update_extent(ip, state, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004980 if (!cur) {
4981 flags |= xfs_ilog_fext(whichfork);
4982 break;
4983 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004984 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004985 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004986 goto done;
4987 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004988 case 0:
4989 /*
4990 * Deleting the middle of the extent.
4991 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004992 old = got;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004993
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004994 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004995 xfs_iext_update_extent(ip, state, *idx, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004996
Dave Chinner9e5987a2013-02-25 12:31:26 +11004997 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004998 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004999 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005000 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005001
Christoph Hellwige1d75532017-10-17 14:16:21 -07005002 flags |= XFS_ILOG_CORE;
5003 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005004 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005005 if (error)
5006 goto done;
5007 error = xfs_btree_increment(cur, 0, &i);
5008 if (error)
5009 goto done;
5010 cur->bc_rec.b = new;
5011 error = xfs_btree_insert(cur, &i);
5012 if (error && error != -ENOSPC)
5013 goto done;
5014 /*
5015 * If get no-space back from btree insert, it tried a
5016 * split, and we have a zero block reservation. Fix up
5017 * our state and return the error.
5018 */
5019 if (error == -ENOSPC) {
5020 /*
5021 * Reset the cursor, don't trust it after any
5022 * insert operation.
5023 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005024 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005025 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005026 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005027 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5028 /*
5029 * Update the btree record back
5030 * to the original value.
5031 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005032 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005033 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005034 goto done;
5035 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005036 * Reset the extent record back
5037 * to the original value.
Dave Chinner9e5987a2013-02-25 12:31:26 +11005038 */
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005039 xfs_iext_update_extent(ip, state, *idx, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005040 flags = 0;
5041 error = -ENOSPC;
5042 goto done;
5043 }
5044 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5045 } else
5046 flags |= xfs_ilog_fext(whichfork);
5047 XFS_IFORK_NEXT_SET(ip, whichfork,
5048 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005049 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5050 ++*idx;
5051 break;
5052 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005053
5054 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005055 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5056 if (error)
5057 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005058
Dave Chinner9e5987a2013-02-25 12:31:26 +11005059 /*
5060 * If we need to, add to list of extents to delete.
5061 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005062 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005063 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5064 error = xfs_refcount_decrease_extent(mp, dfops, del);
5065 if (error)
5066 goto done;
5067 } else
5068 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5069 del->br_blockcount, NULL);
5070 }
5071
Dave Chinner9e5987a2013-02-25 12:31:26 +11005072 /*
5073 * Adjust inode # blocks in the file.
5074 */
5075 if (nblks)
5076 ip->i_d.di_nblocks -= nblks;
5077 /*
5078 * Adjust quota data.
5079 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005080 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005081 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5082
Dave Chinner9e5987a2013-02-25 12:31:26 +11005083done:
5084 *logflagsp = flags;
5085 return error;
5086}
5087
5088/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 * Unmap (remove) blocks from a file.
5090 * If nexts is nonzero then the number of extents to remove is limited to
5091 * that value. If not all extents in the block range can be removed then
5092 * *done is set.
5093 */
5094int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005095__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 xfs_trans_t *tp, /* transaction pointer */
5097 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005098 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005099 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 int flags, /* misc flags */
5101 xfs_extnum_t nexts, /* number of extents max */
5102 xfs_fsblock_t *firstblock, /* first allocated block
5103 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005104 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105{
5106 xfs_btree_cur_t *cur; /* bmap btree cursor */
5107 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 int error; /* error return value */
5109 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005110 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 xfs_ifork_t *ifp; /* inode fork pointer */
5112 int isrt; /* freeing in rt area */
5113 xfs_extnum_t lastx; /* last extent index used */
5114 int logflags; /* transaction logging flags */
5115 xfs_extlen_t mod; /* rt extent offset */
5116 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 int tmp_logflags; /* partial logging flags */
5118 int wasdel; /* was a delayed alloc extent */
5119 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005121 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005122 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005123 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005124 xfs_fileoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005126 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005127
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005128 whichfork = xfs_bmapi_whichfork(flags);
5129 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130 ifp = XFS_IFORK_PTR(ip, whichfork);
5131 if (unlikely(
5132 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5133 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5134 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5135 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005136 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 }
5138 mp = ip->i_mount;
5139 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005140 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005141
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005142 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005143 ASSERT(len > 0);
5144 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005145
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005146 /*
5147 * Guesstimate how many blocks we can unmap without running the risk of
5148 * blowing out the transaction with a mix of EFIs and reflink
5149 * adjustments.
5150 */
5151 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5152 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5153 else
5154 max_len = len;
5155
Linus Torvalds1da177e2005-04-16 15:20:36 -07005156 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5157 (error = xfs_iread_extents(tp, ip, whichfork)))
5158 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005159 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005160 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 return 0;
5162 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005163 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005164 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwigdc560152017-10-23 16:32:39 -07005165 end = start + len;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005166
Christoph Hellwigdc560152017-10-23 16:32:39 -07005167 if (!xfs_iext_lookup_extent_before(ip, ifp, &end, &lastx, &got)) {
5168 *rlen = 0;
5169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 }
Christoph Hellwigdc560152017-10-23 16:32:39 -07005171 end--;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005172
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 logflags = 0;
5174 if (ifp->if_flags & XFS_IFBROOT) {
5175 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005176 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005178 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 cur->bc_private.b.flags = 0;
5180 } else
5181 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005182
5183 if (isrt) {
5184 /*
5185 * Synchronize by locking the bitmap inode.
5186 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005187 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005188 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005189 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5190 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005191 }
5192
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 extno = 0;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005194 while (end != (xfs_fileoff_t)-1 && end >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005195 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005197 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 * Just back up to the previous extent, if so.
5199 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005200 if (got.br_startoff > end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 if (--lastx < 0)
5202 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005203 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 }
5205 /*
5206 * Is the last block of this extent before the range
5207 * we're supposed to delete? If so, we're done.
5208 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005209 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005211 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 break;
5213 /*
5214 * Then deal with the (possibly delayed) allocated space
5215 * we found.
5216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005218 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005219
5220 /*
5221 * Make sure we don't touch multiple AGF headers out of order
5222 * in a single transaction, as that could cause AB-BA deadlocks.
5223 */
5224 if (!wasdel) {
5225 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5226 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5227 break;
5228 prev_agno = agno;
5229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 if (got.br_startoff < start) {
5231 del.br_startoff = start;
5232 del.br_blockcount -= start - got.br_startoff;
5233 if (!wasdel)
5234 del.br_startblock += start - got.br_startoff;
5235 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005236 if (del.br_startoff + del.br_blockcount > end + 1)
5237 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005238
5239 /* How much can we safely unmap? */
5240 if (max_len < del.br_blockcount) {
5241 del.br_startoff += del.br_blockcount - max_len;
5242 if (!wasdel)
5243 del.br_startblock += del.br_blockcount - max_len;
5244 del.br_blockcount = max_len;
5245 }
5246
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 sum = del.br_startblock + del.br_blockcount;
5248 if (isrt &&
5249 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5250 /*
5251 * Realtime extent not lined up at the end.
5252 * The extent could have been split into written
5253 * and unwritten pieces, or we could just be
5254 * unmapping part of it. But we can't really
5255 * get rid of part of a realtime extent.
5256 */
5257 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005258 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 /*
5260 * This piece is unwritten, or we're not
5261 * using unwritten extents. Skip over it.
5262 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005263 ASSERT(end >= mod);
5264 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 del.br_blockcount : mod;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005266 if (end < got.br_startoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 if (--lastx >= 0)
Christoph Hellwige3f0f752017-10-17 14:16:20 -07005268 xfs_iext_get_extent(ifp, lastx,
5269 &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 }
5271 continue;
5272 }
5273 /*
5274 * It's written, turn it unwritten.
5275 * This is better than zeroing it.
5276 */
5277 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005278 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 /*
5280 * If this spans a realtime extent boundary,
5281 * chop it back to the start of the one we end at.
5282 */
5283 if (del.br_blockcount > mod) {
5284 del.br_startoff += del.br_blockcount - mod;
5285 del.br_startblock += del.br_blockcount - mod;
5286 del.br_blockcount = mod;
5287 }
5288 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005289 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005290 whichfork, &lastx, &cur, &del,
5291 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005292 if (error)
5293 goto error0;
5294 goto nodelete;
5295 }
5296 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5297 /*
5298 * Realtime extent is lined up at the end but not
5299 * at the front. We'll get rid of full extents if
5300 * we can.
5301 */
5302 mod = mp->m_sb.sb_rextsize - mod;
5303 if (del.br_blockcount > mod) {
5304 del.br_blockcount -= mod;
5305 del.br_startoff += mod;
5306 del.br_startblock += mod;
5307 } else if ((del.br_startoff == start &&
5308 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005309 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005310 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005311 /*
5312 * Can't make it unwritten. There isn't
5313 * a full extent here so just skip it.
5314 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005315 ASSERT(end >= del.br_blockcount);
5316 end -= del.br_blockcount;
5317 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005318 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 continue;
5320 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005321 struct xfs_bmbt_irec prev;
5322
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 /*
5324 * This one is already unwritten.
5325 * It must have a written left neighbor.
5326 * Unwrite the killed part of that one and
5327 * try again.
5328 */
5329 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005330 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005332 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 ASSERT(del.br_startblock ==
5334 prev.br_startblock + prev.br_blockcount);
5335 if (prev.br_startoff < start) {
5336 mod = start - prev.br_startoff;
5337 prev.br_blockcount -= mod;
5338 prev.br_startblock += mod;
5339 prev.br_startoff = start;
5340 }
5341 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005342 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005343 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005344 ip, whichfork, &lastx, &cur,
5345 &prev, firstblock, dfops,
5346 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 if (error)
5348 goto error0;
5349 goto nodelete;
5350 } else {
5351 ASSERT(del.br_state == XFS_EXT_NORM);
5352 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005353 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005354 ip, whichfork, &lastx, &cur,
5355 &del, firstblock, dfops,
5356 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 if (error)
5358 goto error0;
5359 goto nodelete;
5360 }
5361 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005362
Brian Fosterb2706a02016-03-15 11:42:46 +11005363 if (wasdel) {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005364 error = xfs_bmap_del_extent_delay(ip, whichfork, &lastx,
5365 &got, &del);
5366 } else {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005367 error = xfs_bmap_del_extent_real(ip, tp, &lastx, dfops,
5368 cur, &del, &tmp_logflags, whichfork,
5369 flags);
5370 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005371 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005372
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 if (error)
5374 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005375
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005376 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005377 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 /*
5380 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005382 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005383 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005384 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005385 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005386 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 extno++;
5389 }
5390 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005391 if (end == (xfs_fileoff_t)-1 || end < start || lastx < 0)
Darrick J. Wong44535932016-10-03 09:11:29 -07005392 *rlen = 0;
5393 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005394 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005395
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 /*
5397 * Convert to a btree if necessary.
5398 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005399 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005401 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 &cur, 0, &tmp_logflags, whichfork);
5403 logflags |= tmp_logflags;
5404 if (error)
5405 goto error0;
5406 }
5407 /*
5408 * transform from btree to extents, give it cur
5409 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005410 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 ASSERT(cur != NULL);
5412 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5413 whichfork);
5414 logflags |= tmp_logflags;
5415 if (error)
5416 goto error0;
5417 }
5418 /*
5419 * transform from extents to local?
5420 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 error = 0;
5422error0:
5423 /*
5424 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005425 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005427 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005429 logflags &= ~xfs_ilog_fext(whichfork);
5430 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005432 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 /*
5434 * Log inode even in the error case, if the transaction
5435 * is dirty we'll need to shut down the filesystem.
5436 */
5437 if (logflags)
5438 xfs_trans_log_inode(tp, ip, logflags);
5439 if (cur) {
5440 if (!error) {
5441 *firstblock = cur->bc_private.b.firstblock;
5442 cur->bc_private.b.allocated = 0;
5443 }
5444 xfs_btree_del_cursor(cur,
5445 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5446 }
5447 return error;
5448}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005449
Darrick J. Wong44535932016-10-03 09:11:29 -07005450/* Unmap a range of a file. */
5451int
5452xfs_bunmapi(
5453 xfs_trans_t *tp,
5454 struct xfs_inode *ip,
5455 xfs_fileoff_t bno,
5456 xfs_filblks_t len,
5457 int flags,
5458 xfs_extnum_t nexts,
5459 xfs_fsblock_t *firstblock,
5460 struct xfs_defer_ops *dfops,
5461 int *done)
5462{
5463 int error;
5464
5465 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5466 dfops);
5467 *done = (len == 0);
5468 return error;
5469}
5470
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005471/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005472 * Determine whether an extent shift can be accomplished by a merge with the
5473 * extent that precedes the target hole of the shift.
5474 */
5475STATIC bool
5476xfs_bmse_can_merge(
5477 struct xfs_bmbt_irec *left, /* preceding extent */
5478 struct xfs_bmbt_irec *got, /* current extent to shift */
5479 xfs_fileoff_t shift) /* shift fsb */
5480{
5481 xfs_fileoff_t startoff;
5482
5483 startoff = got->br_startoff - shift;
5484
5485 /*
5486 * The extent, once shifted, must be adjacent in-file and on-disk with
5487 * the preceding extent.
5488 */
5489 if ((left->br_startoff + left->br_blockcount != startoff) ||
5490 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5491 (left->br_state != got->br_state) ||
5492 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5493 return false;
5494
5495 return true;
5496}
5497
5498/*
5499 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5500 * hole in the file. If an extent shift would result in the extent being fully
5501 * adjacent to the extent that currently precedes the hole, we can merge with
5502 * the preceding extent rather than do the shift.
5503 *
5504 * This function assumes the caller has verified a shift-by-merge is possible
5505 * with the provided extents via xfs_bmse_can_merge().
5506 */
5507STATIC int
5508xfs_bmse_merge(
5509 struct xfs_inode *ip,
5510 int whichfork,
5511 xfs_fileoff_t shift, /* shift fsb */
5512 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005513 struct xfs_bmbt_irec *got, /* extent to shift */
5514 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005515 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005516 int *logflags, /* output */
5517 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005518{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005519 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005520 xfs_filblks_t blockcount;
5521 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005522 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005523
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005524 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005525
5526 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5527 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005528 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005529
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005530 new = *left;
5531 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005532
5533 /*
5534 * Update the on-disk extent count, the btree if necessary and log the
5535 * inode.
5536 */
5537 XFS_IFORK_NEXT_SET(ip, whichfork,
5538 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5539 *logflags |= XFS_ILOG_CORE;
5540 if (!cur) {
5541 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005542 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005543 }
5544
5545 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005546 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005547 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005548 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005549 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005550
5551 error = xfs_btree_delete(cur, &i);
5552 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005553 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005554 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005555
5556 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005557 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005558 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005559 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005560 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005561
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005562 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005563 if (error)
5564 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005565
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005566done:
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005567 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
5568 current_ext - 1, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005569 xfs_iext_remove(ip, current_ext, 1, 0);
5570
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005571 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005572 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5573 if (error)
5574 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005575 memcpy(&new, got, sizeof(new));
5576 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005577 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005578}
5579
Christoph Hellwigbf806282017-10-19 11:07:34 -07005580static int
5581xfs_bmap_shift_update_extent(
5582 struct xfs_inode *ip,
5583 int whichfork,
5584 xfs_extnum_t idx,
5585 struct xfs_bmbt_irec *got,
5586 struct xfs_btree_cur *cur,
5587 int *logflags,
5588 struct xfs_defer_ops *dfops,
5589 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005590{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005591 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005592 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005593 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005594
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005595 *logflags |= XFS_ILOG_CORE;
5596
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005597 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005598
5599 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005600 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005601 if (error)
5602 return error;
5603 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5604
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005605 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005606 if (error)
5607 return error;
5608 } else {
5609 *logflags |= XFS_ILOG_DEXT;
5610 }
5611
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005612 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), idx, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005613
Darrick J. Wong9c194642016-08-03 12:16:05 +10005614 /* update reverse mapping */
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005615 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005616 if (error)
5617 return error;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005618 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005619}
5620
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005621int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005622xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005623 struct xfs_trans *tp,
5624 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005625 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005626 xfs_fileoff_t offset_shift_fsb,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005627 bool *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005628 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005629 xfs_fsblock_t *firstblock,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005630 struct xfs_defer_ops *dfops)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005631{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005632 int whichfork = XFS_DATA_FORK;
5633 struct xfs_mount *mp = ip->i_mount;
5634 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5635 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005636 struct xfs_bmbt_irec got, prev;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005637 xfs_extnum_t current_ext;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005638 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005639 int error = 0;
5640 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005641
5642 if (unlikely(XFS_TEST_ERROR(
5643 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5644 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005645 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005646 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005647 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005648 }
5649
5650 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005651 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005652
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005653 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005654
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005655 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005656 error = xfs_iread_extents(tp, ip, whichfork);
5657 if (error)
5658 return error;
5659 }
5660
Brian Fosterddb19e32014-09-23 15:38:09 +10005661 if (ifp->if_flags & XFS_IFBROOT) {
5662 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5663 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005664 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005665 cur->bc_private.b.flags = 0;
5666 }
5667
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005668 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext, &got)) {
5669 *done = true;
5670 goto del_cursor;
5671 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005672 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005673
Christoph Hellwigbf806282017-10-19 11:07:34 -07005674 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005675 if (xfs_iext_get_extent(ifp, current_ext - 1, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005676 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5677 error = -EINVAL;
5678 goto del_cursor;
5679 }
5680
Christoph Hellwigbf806282017-10-19 11:07:34 -07005681 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5682 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5683 current_ext, &got, &prev, cur,
5684 &logflags, dfops);
5685 if (error)
5686 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005687
5688 /* update got after merge */
5689 if (!xfs_iext_get_extent(ifp, current_ext, &got)) {
5690 *done = true;
5691 goto del_cursor;
5692 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005693 goto done;
5694 }
5695 } else {
5696 if (got.br_startoff < offset_shift_fsb) {
5697 error = -EINVAL;
5698 goto del_cursor;
5699 }
5700 }
5701
5702 error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got,
5703 cur, &logflags, dfops, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005704 if (error)
5705 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005706
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005707 if (!xfs_iext_get_extent(ifp, ++current_ext, &got)) {
5708 *done = true;
5709 goto del_cursor;
5710 }
5711
5712done:
Christoph Hellwigbf806282017-10-19 11:07:34 -07005713 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005714del_cursor:
5715 if (cur)
5716 xfs_btree_del_cursor(cur,
5717 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005718 if (logflags)
5719 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005720 return error;
5721}
5722
5723int
5724xfs_bmap_insert_extents(
5725 struct xfs_trans *tp,
5726 struct xfs_inode *ip,
5727 xfs_fileoff_t *next_fsb,
5728 xfs_fileoff_t offset_shift_fsb,
5729 bool *done,
5730 xfs_fileoff_t stop_fsb,
5731 xfs_fsblock_t *firstblock,
5732 struct xfs_defer_ops *dfops)
5733{
5734 int whichfork = XFS_DATA_FORK;
5735 struct xfs_mount *mp = ip->i_mount;
5736 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5737 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005738 struct xfs_bmbt_irec got, next;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005739 xfs_extnum_t current_ext;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005740 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005741 int error = 0;
5742 int logflags = 0;
5743
5744 if (unlikely(XFS_TEST_ERROR(
5745 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5746 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5747 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5748 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5749 return -EFSCORRUPTED;
5750 }
5751
5752 if (XFS_FORCED_SHUTDOWN(mp))
5753 return -EIO;
5754
5755 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5756
5757 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5758 error = xfs_iread_extents(tp, ip, whichfork);
5759 if (error)
5760 return error;
5761 }
5762
5763 if (ifp->if_flags & XFS_IFBROOT) {
5764 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5765 cur->bc_private.b.firstblock = *firstblock;
5766 cur->bc_private.b.dfops = dfops;
5767 cur->bc_private.b.flags = 0;
5768 }
5769
Namjae Jeona904b1c2015-03-25 15:08:56 +11005770 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005771 current_ext = xfs_iext_count(ifp) - 1;
5772 if (!xfs_iext_get_extent(ifp, current_ext, &got) ||
5773 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005774 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005775 goto del_cursor;
5776 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005777 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005778 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
5779 &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005780 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005781 goto del_cursor;
5782 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005783 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005784 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005785
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005786 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005787 error = -EIO;
5788 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005789 }
5790
Christoph Hellwigbf806282017-10-19 11:07:34 -07005791 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005792 if (xfs_iext_get_extent(ifp, current_ext + 1, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005793 if (new_startoff + got.br_blockcount > next.br_startoff) {
5794 error = -EINVAL;
5795 goto del_cursor;
5796 }
5797
5798 /*
5799 * Unlike a left shift (which involves a hole punch), a right
5800 * shift does not modify extent neighbors in any way. We should
5801 * never find mergeable extents in this scenario. Check anyways
5802 * and warn if we encounter two extents that could be one.
5803 */
5804 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5805 WARN_ON_ONCE(1);
5806 }
5807
5808 error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got,
5809 cur, &logflags, dfops, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005810 if (error)
5811 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005812
5813 if (!xfs_iext_get_extent(ifp, --current_ext, &got) ||
5814 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005815 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005816 goto del_cursor;
5817 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005818
5819 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005820del_cursor:
5821 if (cur)
5822 xfs_btree_del_cursor(cur,
5823 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Brian Fosterca446d82014-09-02 12:12:53 +10005824 if (logflags)
5825 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005826 return error;
5827}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005828
5829/*
5830 * Splits an extent into two extents at split_fsb block such that it is
5831 * the first block of the current_ext. @current_ext is a target extent
5832 * to be split. @split_fsb is a block where the extents is split.
5833 * If split_fsb lies in a hole or the first block of extents, just return 0.
5834 */
5835STATIC int
5836xfs_bmap_split_extent_at(
5837 struct xfs_trans *tp,
5838 struct xfs_inode *ip,
5839 xfs_fileoff_t split_fsb,
5840 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005841 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005842{
5843 int whichfork = XFS_DATA_FORK;
5844 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005845 struct xfs_bmbt_irec got;
5846 struct xfs_bmbt_irec new; /* split extent */
5847 struct xfs_mount *mp = ip->i_mount;
5848 struct xfs_ifork *ifp;
5849 xfs_fsblock_t gotblkcnt; /* new block count for got */
5850 xfs_extnum_t current_ext;
5851 int error = 0;
5852 int logflags = 0;
5853 int i = 0;
5854
5855 if (unlikely(XFS_TEST_ERROR(
5856 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5857 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005858 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005859 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5860 XFS_ERRLEVEL_LOW, mp);
5861 return -EFSCORRUPTED;
5862 }
5863
5864 if (XFS_FORCED_SHUTDOWN(mp))
5865 return -EIO;
5866
5867 ifp = XFS_IFORK_PTR(ip, whichfork);
5868 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5869 /* Read in all the extents */
5870 error = xfs_iread_extents(tp, ip, whichfork);
5871 if (error)
5872 return error;
5873 }
5874
5875 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005876 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005877 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005878 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
5879 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005880 return 0;
5881
5882 gotblkcnt = split_fsb - got.br_startoff;
5883 new.br_startoff = split_fsb;
5884 new.br_startblock = got.br_startblock + gotblkcnt;
5885 new.br_blockcount = got.br_blockcount - gotblkcnt;
5886 new.br_state = got.br_state;
5887
5888 if (ifp->if_flags & XFS_IFBROOT) {
5889 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5890 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005891 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005892 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005893 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005894 if (error)
5895 goto del_cursor;
5896 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5897 }
5898
Namjae Jeona904b1c2015-03-25 15:08:56 +11005899 got.br_blockcount = gotblkcnt;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005900 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
5901 current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005902
5903 logflags = XFS_ILOG_CORE;
5904 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005905 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005906 if (error)
5907 goto del_cursor;
5908 } else
5909 logflags |= XFS_ILOG_DEXT;
5910
5911 /* Add new extent */
5912 current_ext++;
5913 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5914 XFS_IFORK_NEXT_SET(ip, whichfork,
5915 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5916
5917 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005918 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005919 if (error)
5920 goto del_cursor;
5921 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005922 error = xfs_btree_insert(cur, &i);
5923 if (error)
5924 goto del_cursor;
5925 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5926 }
5927
5928 /*
5929 * Convert to a btree if necessary.
5930 */
5931 if (xfs_bmap_needs_btree(ip, whichfork)) {
5932 int tmp_logflags; /* partial log flag return val */
5933
5934 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005935 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005936 &cur, 0, &tmp_logflags, whichfork);
5937 logflags |= tmp_logflags;
5938 }
5939
5940del_cursor:
5941 if (cur) {
5942 cur->bc_private.b.allocated = 0;
5943 xfs_btree_del_cursor(cur,
5944 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5945 }
5946
5947 if (logflags)
5948 xfs_trans_log_inode(tp, ip, logflags);
5949 return error;
5950}
5951
5952int
5953xfs_bmap_split_extent(
5954 struct xfs_inode *ip,
5955 xfs_fileoff_t split_fsb)
5956{
5957 struct xfs_mount *mp = ip->i_mount;
5958 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005959 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005960 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005961 int error;
5962
Christoph Hellwig253f4912016-04-06 09:19:55 +10005963 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
5964 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
5965 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005966 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005967
5968 xfs_ilock(ip, XFS_ILOCK_EXCL);
5969 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
5970
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005971 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005972
5973 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005974 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005975 if (error)
5976 goto out;
5977
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07005978 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005979 if (error)
5980 goto out;
5981
Christoph Hellwig70393312015-06-04 13:48:08 +10005982 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005983
5984out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005985 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10005986 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005987 return error;
5988}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07005989
5990/* Deferred mapping is only for real extents in the data fork. */
5991static bool
5992xfs_bmap_is_update_needed(
5993 struct xfs_bmbt_irec *bmap)
5994{
5995 return bmap->br_startblock != HOLESTARTBLOCK &&
5996 bmap->br_startblock != DELAYSTARTBLOCK;
5997}
5998
5999/* Record a bmap intent. */
6000static int
6001__xfs_bmap_add(
6002 struct xfs_mount *mp,
6003 struct xfs_defer_ops *dfops,
6004 enum xfs_bmap_intent_type type,
6005 struct xfs_inode *ip,
6006 int whichfork,
6007 struct xfs_bmbt_irec *bmap)
6008{
6009 int error;
6010 struct xfs_bmap_intent *bi;
6011
6012 trace_xfs_bmap_defer(mp,
6013 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6014 type,
6015 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6016 ip->i_ino, whichfork,
6017 bmap->br_startoff,
6018 bmap->br_blockcount,
6019 bmap->br_state);
6020
6021 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6022 INIT_LIST_HEAD(&bi->bi_list);
6023 bi->bi_type = type;
6024 bi->bi_owner = ip;
6025 bi->bi_whichfork = whichfork;
6026 bi->bi_bmap = *bmap;
6027
Christoph Hellwig882d8782017-08-28 10:21:03 -07006028 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006029 if (error) {
6030 kmem_free(bi);
6031 return error;
6032 }
6033
6034 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6035 return 0;
6036}
6037
6038/* Map an extent into a file. */
6039int
6040xfs_bmap_map_extent(
6041 struct xfs_mount *mp,
6042 struct xfs_defer_ops *dfops,
6043 struct xfs_inode *ip,
6044 struct xfs_bmbt_irec *PREV)
6045{
6046 if (!xfs_bmap_is_update_needed(PREV))
6047 return 0;
6048
6049 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6050 XFS_DATA_FORK, PREV);
6051}
6052
6053/* Unmap an extent out of a file. */
6054int
6055xfs_bmap_unmap_extent(
6056 struct xfs_mount *mp,
6057 struct xfs_defer_ops *dfops,
6058 struct xfs_inode *ip,
6059 struct xfs_bmbt_irec *PREV)
6060{
6061 if (!xfs_bmap_is_update_needed(PREV))
6062 return 0;
6063
6064 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6065 XFS_DATA_FORK, PREV);
6066}
6067
6068/*
6069 * Process one of the deferred bmap operations. We pass back the
6070 * btree cursor to maintain our lock on the bmapbt between calls.
6071 */
6072int
6073xfs_bmap_finish_one(
6074 struct xfs_trans *tp,
6075 struct xfs_defer_ops *dfops,
6076 struct xfs_inode *ip,
6077 enum xfs_bmap_intent_type type,
6078 int whichfork,
6079 xfs_fileoff_t startoff,
6080 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006081 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006082 xfs_exntst_t state)
6083{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006084 xfs_fsblock_t firstfsb;
6085 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006086
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006087 /*
6088 * firstfsb is tied to the transaction lifetime and is used to
6089 * ensure correct AG locking order and schedule work item
6090 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6091 * to only making one bmap call per transaction, so it should
6092 * be safe to have it as a local variable here.
6093 */
6094 firstfsb = NULLFSBLOCK;
6095
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006096 trace_xfs_bmap_deferred(tp->t_mountp,
6097 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6098 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006099 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006100
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006101 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006102 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006103
6104 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006105 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006106 return -EIO;
6107
6108 switch (type) {
6109 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006110 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006111 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006112 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006113 break;
6114 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006115 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6116 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006117 break;
6118 default:
6119 ASSERT(0);
6120 error = -EFSCORRUPTED;
6121 }
6122
6123 return error;
6124}