blob: f02eb767339219e8e59418429c666b3934d795f4 [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"
Darrick J. Wongfd26a882016-11-28 14:57:42 +110052#include "xfs_rmap_btree.h"
Brian Foster974ae922016-11-28 14:57:42 +110053#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056kmem_zone_t *xfs_bmap_free_item_zone;
57
58/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110059 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110063 * Compute and fill in the value of the maximum depth of a bmap btree
64 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110066void
67xfs_bmap_compute_maxlevels(
68 xfs_mount_t *mp, /* file system mount structure */
69 int whichfork) /* data or attr fork */
70{
71 int level; /* btree level */
72 uint maxblocks; /* max blocks at this level */
73 uint maxleafents; /* max leaf entries possible */
74 int maxrootrecs; /* max records in root block */
75 int minleafrecs; /* min records in leaf block */
76 int minnoderecs; /* min records in node block */
77 int sz; /* root block size */
78
79 /*
80 * The maximum number of extents in a file, hence the maximum
81 * number of leaf entries, is controlled by the type of di_nextents
82 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
83 * (a signed 16-bit number, xfs_aextnum_t).
84 *
85 * Note that we can no longer assume that if we are in ATTR1 that
86 * the fork offset of all the inodes will be
87 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
88 * with ATTR2 and then mounted back with ATTR1, keeping the
89 * di_forkoff's fixed but probably at various positions. Therefore,
90 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
91 * of a minimum size available.
92 */
93 if (whichfork == XFS_DATA_FORK) {
94 maxleafents = MAXEXTNUM;
95 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
96 } else {
97 maxleafents = MAXAEXTNUM;
98 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
99 }
Eric Sandeen152d93b2014-04-14 18:58:51 +1000100 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100101 minleafrecs = mp->m_bmap_dmnr[0];
102 minnoderecs = mp->m_bmap_dmnr[1];
103 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
104 for (level = 1; maxblocks > 1; level++) {
105 if (maxblocks <= maxrootrecs)
106 maxblocks = 1;
107 else
108 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
109 }
110 mp->m_bm_maxlevels[whichfork] = level;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100113STATIC int /* error */
114xfs_bmbt_lookup_eq(
115 struct xfs_btree_cur *cur,
116 xfs_fileoff_t off,
117 xfs_fsblock_t bno,
118 xfs_filblks_t len,
119 int *stat) /* success/failure */
120{
121 cur->bc_rec.b.br_startoff = off;
122 cur->bc_rec.b.br_startblock = bno;
123 cur->bc_rec.b.br_blockcount = len;
124 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
125}
126
127STATIC int /* error */
128xfs_bmbt_lookup_ge(
129 struct xfs_btree_cur *cur,
130 xfs_fileoff_t off,
131 xfs_fsblock_t bno,
132 xfs_filblks_t len,
133 int *stat) /* success/failure */
134{
135 cur->bc_rec.b.br_startoff = off;
136 cur->bc_rec.b.br_startblock = bno;
137 cur->bc_rec.b.br_blockcount = len;
138 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
139}
140
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100141/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000142 * Check if the inode needs to be converted to btree format.
143 */
144static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
145{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700146 return whichfork != XFS_COW_FORK &&
147 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000148 XFS_IFORK_NEXTENTS(ip, whichfork) >
149 XFS_IFORK_MAXEXT(ip, whichfork);
150}
151
152/*
153 * Check if the inode should be converted to extent format.
154 */
155static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
156{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700157 return whichfork != XFS_COW_FORK &&
158 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000159 XFS_IFORK_NEXTENTS(ip, whichfork) <=
160 XFS_IFORK_MAXEXT(ip, whichfork);
161}
162
163/*
164 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100165 * by [off, bno, len, state].
166 * This either works (return 0) or gets an EFSCORRUPTED error.
167 */
168STATIC int
169xfs_bmbt_update(
170 struct xfs_btree_cur *cur,
171 xfs_fileoff_t off,
172 xfs_fsblock_t bno,
173 xfs_filblks_t len,
174 xfs_exntst_t state)
175{
176 union xfs_btree_rec rec;
177
178 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
179 return xfs_btree_update(cur, &rec);
180}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100183 * Compute the worst-case number of indirect blocks that will be used
184 * for ip's delayed extent of length "len".
185 */
186STATIC xfs_filblks_t
187xfs_bmap_worst_indlen(
188 xfs_inode_t *ip, /* incore inode pointer */
189 xfs_filblks_t len) /* delayed extent length */
190{
191 int level; /* btree level number */
192 int maxrecs; /* maximum record count at this level */
193 xfs_mount_t *mp; /* mount structure */
194 xfs_filblks_t rval; /* return value */
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100195 xfs_filblks_t orig_len;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100196
197 mp = ip->i_mount;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100198
199 /* Calculate the worst-case size of the bmbt. */
200 orig_len = len;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100201 maxrecs = mp->m_bmap_dmxr[0];
202 for (level = 0, rval = 0;
203 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
204 level++) {
205 len += maxrecs - 1;
206 do_div(len, maxrecs);
207 rval += len;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100208 if (len == 1) {
209 rval += XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100210 level - 1;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100211 break;
212 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100213 if (level == 0)
214 maxrecs = mp->m_bmap_dmxr[1];
215 }
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100216
217 /* Calculate the worst-case size of the rmapbt. */
218 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
219 rval += 1 + xfs_rmapbt_calc_size(mp, orig_len) +
220 mp->m_rmap_maxlevels;
221
Dave Chinner9e5987a2013-02-25 12:31:26 +1100222 return rval;
223}
224
225/*
226 * Calculate the default attribute fork offset for newly created inodes.
227 */
228uint
229xfs_default_attroffset(
230 struct xfs_inode *ip)
231{
232 struct xfs_mount *mp = ip->i_mount;
233 uint offset;
234
235 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100236 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100237 XFS_BMDR_SPACE_CALC(MINABTPTRS);
238 } else {
239 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
240 }
241
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100242 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100243 return offset;
244}
245
246/*
247 * Helper routine to reset inode di_forkoff field when switching
248 * attribute fork from local to extent format - we reset it where
249 * possible to make space available for inline data fork extents.
250 */
251STATIC void
252xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100253 xfs_inode_t *ip,
254 int whichfork)
255{
256 if (whichfork == XFS_ATTR_FORK &&
257 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
258 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
259 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
260 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
261
262 if (dfl_forkoff > ip->i_d.di_forkoff)
263 ip->i_d.di_forkoff = dfl_forkoff;
264 }
265}
266
Dave Chinner9e5987a2013-02-25 12:31:26 +1100267#ifdef DEBUG
268STATIC struct xfs_buf *
269xfs_bmap_get_bp(
270 struct xfs_btree_cur *cur,
271 xfs_fsblock_t bno)
272{
273 struct xfs_log_item_desc *lidp;
274 int i;
275
276 if (!cur)
277 return NULL;
278
279 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
280 if (!cur->bc_bufs[i])
281 break;
282 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
283 return cur->bc_bufs[i];
284 }
285
286 /* Chase down all the log items to see if the bp is there */
287 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
288 struct xfs_buf_log_item *bip;
289 bip = (struct xfs_buf_log_item *)lidp->lid_item;
290 if (bip->bli_item.li_type == XFS_LI_BUF &&
291 XFS_BUF_ADDR(bip->bli_buf) == bno)
292 return bip->bli_buf;
293 }
294
295 return NULL;
296}
297
298STATIC void
299xfs_check_block(
300 struct xfs_btree_block *block,
301 xfs_mount_t *mp,
302 int root,
303 short sz)
304{
305 int i, j, dmxr;
306 __be64 *pp, *thispa; /* pointer to block address */
307 xfs_bmbt_key_t *prevp, *keyp;
308
309 ASSERT(be16_to_cpu(block->bb_level) > 0);
310
311 prevp = NULL;
312 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
313 dmxr = mp->m_bmap_dmxr[0];
314 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
315
316 if (prevp) {
317 ASSERT(be64_to_cpu(prevp->br_startoff) <
318 be64_to_cpu(keyp->br_startoff));
319 }
320 prevp = keyp;
321
322 /*
323 * Compare the block numbers to see if there are dups.
324 */
325 if (root)
326 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
327 else
328 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
329
330 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
331 if (root)
332 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
333 else
334 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
335 if (*thispa == *pp) {
336 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
337 __func__, j, i,
338 (unsigned long long)be64_to_cpu(*thispa));
339 panic("%s: ptrs are equal in node\n",
340 __func__);
341 }
342 }
343 }
344}
345
346/*
347 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100348 * btree leaves. THis becomes prohibitively expensive for large extent count
349 * files, so don't bother with inodes that have more than 10,000 extents in
350 * them. The btree record ordering checks will still be done, so for such large
351 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100352 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100353STATIC void
354xfs_bmap_check_leaf_extents(
355 xfs_btree_cur_t *cur, /* btree cursor or null */
356 xfs_inode_t *ip, /* incore inode pointer */
357 int whichfork) /* data or attr fork */
358{
359 struct xfs_btree_block *block; /* current btree block */
360 xfs_fsblock_t bno; /* block # of "block" */
361 xfs_buf_t *bp; /* buffer for "block" */
362 int error; /* error return value */
363 xfs_extnum_t i=0, j; /* index into the extents list */
364 xfs_ifork_t *ifp; /* fork structure */
365 int level; /* btree level, for checking */
366 xfs_mount_t *mp; /* file system mount structure */
367 __be64 *pp; /* pointer to block address */
368 xfs_bmbt_rec_t *ep; /* pointer to current extent */
369 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
370 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
371 int bp_release = 0;
372
373 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
374 return;
375 }
376
Dave Chinnere3543812016-01-08 11:28:49 +1100377 /* skip large extent count inodes */
378 if (ip->i_d.di_nextents > 10000)
379 return;
380
Dave Chinner9e5987a2013-02-25 12:31:26 +1100381 bno = NULLFSBLOCK;
382 mp = ip->i_mount;
383 ifp = XFS_IFORK_PTR(ip, whichfork);
384 block = ifp->if_broot;
385 /*
386 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
387 */
388 level = be16_to_cpu(block->bb_level);
389 ASSERT(level > 0);
390 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
391 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
392 bno = be64_to_cpu(*pp);
393
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000394 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100395 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
396 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
397
398 /*
399 * Go down the tree until leaf level is reached, following the first
400 * pointer (leftmost) at each level.
401 */
402 while (level-- > 0) {
403 /* See if buf is in cur first */
404 bp_release = 0;
405 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
406 if (!bp) {
407 bp_release = 1;
408 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
409 XFS_BMAP_BTREE_REF,
410 &xfs_bmbt_buf_ops);
411 if (error)
412 goto error_norelse;
413 }
414 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100415 if (level == 0)
416 break;
417
418 /*
419 * Check this block for basic sanity (increasing keys and
420 * no duplicate blocks).
421 */
422
423 xfs_check_block(block, mp, 0, 0);
424 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
425 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100426 XFS_WANT_CORRUPTED_GOTO(mp,
427 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100428 if (bp_release) {
429 bp_release = 0;
430 xfs_trans_brelse(NULL, bp);
431 }
432 }
433
434 /*
435 * Here with bp and block set to the leftmost leaf node in the tree.
436 */
437 i = 0;
438
439 /*
440 * Loop over all leaf nodes checking that all extents are in the right order.
441 */
442 for (;;) {
443 xfs_fsblock_t nextbno;
444 xfs_extnum_t num_recs;
445
446
447 num_recs = xfs_btree_get_numrecs(block);
448
449 /*
450 * Read-ahead the next leaf block, if any.
451 */
452
453 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
454
455 /*
456 * Check all the extents to make sure they are OK.
457 * If we had a previous block, the last entry should
458 * conform with the first entry in this one.
459 */
460
461 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
462 if (i) {
463 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
464 xfs_bmbt_disk_get_blockcount(&last) <=
465 xfs_bmbt_disk_get_startoff(ep));
466 }
467 for (j = 1; j < num_recs; j++) {
468 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
469 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
470 xfs_bmbt_disk_get_blockcount(ep) <=
471 xfs_bmbt_disk_get_startoff(nextp));
472 ep = nextp;
473 }
474
475 last = *ep;
476 i += num_recs;
477 if (bp_release) {
478 bp_release = 0;
479 xfs_trans_brelse(NULL, bp);
480 }
481 bno = nextbno;
482 /*
483 * If we've reached the end, stop.
484 */
485 if (bno == NULLFSBLOCK)
486 break;
487
488 bp_release = 0;
489 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
490 if (!bp) {
491 bp_release = 1;
492 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
493 XFS_BMAP_BTREE_REF,
494 &xfs_bmbt_buf_ops);
495 if (error)
496 goto error_norelse;
497 }
498 block = XFS_BUF_TO_BLOCK(bp);
499 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100500
Dave Chinner9e5987a2013-02-25 12:31:26 +1100501 return;
502
503error0:
504 xfs_warn(mp, "%s: at error0", __func__);
505 if (bp_release)
506 xfs_trans_brelse(NULL, bp);
507error_norelse:
508 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
509 __func__, i);
510 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
511 return;
512}
513
514/*
515 * Add bmap trace insert entries for all the contents of the extent records.
516 */
517void
518xfs_bmap_trace_exlist(
519 xfs_inode_t *ip, /* incore inode pointer */
520 xfs_extnum_t cnt, /* count of entries in the list */
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100521 int whichfork, /* data or attr or cow fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100522 unsigned long caller_ip)
523{
524 xfs_extnum_t idx; /* extent record index */
525 xfs_ifork_t *ifp; /* inode fork pointer */
526 int state = 0;
527
528 if (whichfork == XFS_ATTR_FORK)
529 state |= BMAP_ATTRFORK;
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100530 else if (whichfork == XFS_COW_FORK)
531 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100532
533 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +1100534 ASSERT(cnt == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100535 for (idx = 0; idx < cnt; idx++)
Eric Sandeen77105172016-12-05 12:31:50 +1100536 trace_xfs_extlist(ip, idx, state, caller_ip);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100537}
538
539/*
540 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000541 * given the caller's original parameters. Specifically check the
542 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100543 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
544 */
545STATIC void
546xfs_bmap_validate_ret(
547 xfs_fileoff_t bno,
548 xfs_filblks_t len,
549 int flags,
550 xfs_bmbt_irec_t *mval,
551 int nmap,
552 int ret_nmap)
553{
554 int i; /* index to map values */
555
556 ASSERT(ret_nmap <= nmap);
557
558 for (i = 0; i < ret_nmap; i++) {
559 ASSERT(mval[i].br_blockcount > 0);
560 if (!(flags & XFS_BMAPI_ENTIRE)) {
561 ASSERT(mval[i].br_startoff >= bno);
562 ASSERT(mval[i].br_blockcount <= len);
563 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
564 bno + len);
565 } else {
566 ASSERT(mval[i].br_startoff < bno + len);
567 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
568 bno);
569 }
570 ASSERT(i == 0 ||
571 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
572 mval[i].br_startoff);
573 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
574 mval[i].br_startblock != HOLESTARTBLOCK);
575 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
576 mval[i].br_state == XFS_EXT_UNWRITTEN);
577 }
578}
579
580#else
581#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
582#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
583#endif /* DEBUG */
584
585/*
586 * bmap free list manipulation functions
587 */
588
589/*
590 * Add the extent to the list of extents to be free at transaction end.
591 * The list is maintained sorted (by block number).
592 */
593void
594xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000595 struct xfs_mount *mp,
596 struct xfs_defer_ops *dfops,
597 xfs_fsblock_t bno,
598 xfs_filblks_t len,
599 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100600{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000601 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100602#ifdef DEBUG
603 xfs_agnumber_t agno;
604 xfs_agblock_t agbno;
605
606 ASSERT(bno != NULLFSBLOCK);
607 ASSERT(len > 0);
608 ASSERT(len <= MAXEXTLEN);
609 ASSERT(!isnullstartblock(bno));
610 agno = XFS_FSB_TO_AGNO(mp, bno);
611 agbno = XFS_FSB_TO_AGBNO(mp, bno);
612 ASSERT(agno < mp->m_sb.sb_agcount);
613 ASSERT(agbno < mp->m_sb.sb_agblocks);
614 ASSERT(len < mp->m_sb.sb_agblocks);
615 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
616#endif
617 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000618
Dave Chinner9e5987a2013-02-25 12:31:26 +1100619 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000620 new->xefi_startblock = bno;
621 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000622 if (oinfo)
623 new->xefi_oinfo = *oinfo;
624 else
625 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000626 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
627 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000628 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100629}
630
631/*
632 * Inode fork format manipulation functions
633 */
634
635/*
636 * Transform a btree format file with only one leaf node, where the
637 * extents list will fit in the inode, into an extents format file.
638 * Since the file extents are already in-core, all we have to do is
639 * give up the space for the btree root and pitch the leaf block.
640 */
641STATIC int /* error */
642xfs_bmap_btree_to_extents(
643 xfs_trans_t *tp, /* transaction pointer */
644 xfs_inode_t *ip, /* incore inode pointer */
645 xfs_btree_cur_t *cur, /* btree cursor */
646 int *logflagsp, /* inode logging flags */
647 int whichfork) /* data or attr fork */
648{
649 /* REFERENCED */
650 struct xfs_btree_block *cblock;/* child btree block */
651 xfs_fsblock_t cbno; /* child block number */
652 xfs_buf_t *cbp; /* child block's buffer */
653 int error; /* error return value */
654 xfs_ifork_t *ifp; /* inode fork data */
655 xfs_mount_t *mp; /* mount point structure */
656 __be64 *pp; /* ptr to block address */
657 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000658 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100659
660 mp = ip->i_mount;
661 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700662 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100663 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
664 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
665 rblock = ifp->if_broot;
666 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
667 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
668 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
669 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
670 cbno = be64_to_cpu(*pp);
671 *logflagsp = 0;
672#ifdef DEBUG
673 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
674 return error;
675#endif
676 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
677 &xfs_bmbt_buf_ops);
678 if (error)
679 return error;
680 cblock = XFS_BUF_TO_BLOCK(cbp);
681 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
682 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000683 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
684 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100685 ip->i_d.di_nblocks--;
686 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
687 xfs_trans_binval(tp, cbp);
688 if (cur->bc_bufs[0] == cbp)
689 cur->bc_bufs[0] = NULL;
690 xfs_iroot_realloc(ip, -1, whichfork);
691 ASSERT(ifp->if_broot == NULL);
692 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
693 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
694 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
695 return 0;
696}
697
698/*
699 * Convert an extents-format file into a btree-format file.
700 * The new file will have a root block (in the inode) and a single child block.
701 */
702STATIC int /* error */
703xfs_bmap_extents_to_btree(
704 xfs_trans_t *tp, /* transaction pointer */
705 xfs_inode_t *ip, /* incore inode pointer */
706 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000707 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100708 xfs_btree_cur_t **curp, /* cursor returned to caller */
709 int wasdel, /* converting a delayed alloc */
710 int *logflagsp, /* inode logging flags */
711 int whichfork) /* data or attr fork */
712{
713 struct xfs_btree_block *ablock; /* allocated (child) bt block */
714 xfs_buf_t *abp; /* buffer for ablock */
715 xfs_alloc_arg_t args; /* allocation arguments */
716 xfs_bmbt_rec_t *arp; /* child record pointer */
717 struct xfs_btree_block *block; /* btree root block */
718 xfs_btree_cur_t *cur; /* bmap btree cursor */
719 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
720 int error; /* error return value */
721 xfs_extnum_t i, cnt; /* extent record index */
722 xfs_ifork_t *ifp; /* inode fork pointer */
723 xfs_bmbt_key_t *kp; /* root block key pointer */
724 xfs_mount_t *mp; /* mount structure */
725 xfs_extnum_t nextents; /* number of file extents */
726 xfs_bmbt_ptr_t *pp; /* root block address pointer */
727
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500728 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700729 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100730 ifp = XFS_IFORK_PTR(ip, whichfork);
731 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
732
733 /*
734 * Make space in the inode incore.
735 */
736 xfs_iroot_realloc(ip, 1, whichfork);
737 ifp->if_flags |= XFS_IFBROOT;
738
739 /*
740 * Fill in the root.
741 */
742 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800743 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
744 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800745 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100746 /*
747 * Need a cursor. Can't allocate until bb_level is filled in.
748 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100749 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
750 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000751 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100752 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
753 /*
754 * Convert to a btree with two levels, one record in root.
755 */
756 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
757 memset(&args, 0, sizeof(args));
758 args.tp = tp;
759 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000760 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100761 args.firstblock = *firstblock;
762 if (*firstblock == NULLFSBLOCK) {
763 args.type = XFS_ALLOCTYPE_START_BNO;
764 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000765 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100766 args.type = XFS_ALLOCTYPE_START_BNO;
767 args.fsbno = *firstblock;
768 } else {
769 args.type = XFS_ALLOCTYPE_NEAR_BNO;
770 args.fsbno = *firstblock;
771 }
772 args.minlen = args.maxlen = args.prod = 1;
773 args.wasdel = wasdel;
774 *logflagsp = 0;
775 if ((error = xfs_alloc_vextent(&args))) {
776 xfs_iroot_realloc(ip, -1, whichfork);
777 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
778 return error;
779 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700780
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800781 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
782 xfs_iroot_realloc(ip, -1, whichfork);
783 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
784 return -ENOSPC;
785 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100786 /*
787 * Allocation can't fail, the space was reserved.
788 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100789 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800790 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100791 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
792 cur->bc_private.b.allocated++;
793 ip->i_d.di_nblocks++;
794 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
795 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
796 /*
797 * Fill in the child block.
798 */
799 abp->b_ops = &xfs_bmbt_buf_ops;
800 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800801 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
802 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500803 XFS_BTREE_LONG_PTRS);
804
Dave Chinner9e5987a2013-02-25 12:31:26 +1100805 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100806 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100807 for (cnt = i = 0; i < nextents; i++) {
808 ep = xfs_iext_get_ext(ifp, i);
809 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
810 arp->l0 = cpu_to_be64(ep->l0);
811 arp->l1 = cpu_to_be64(ep->l1);
812 arp++; cnt++;
813 }
814 }
815 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
816 xfs_btree_set_numrecs(ablock, cnt);
817
818 /*
819 * Fill in the root key and pointer.
820 */
821 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
822 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
823 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
824 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
825 be16_to_cpu(block->bb_level)));
826 *pp = cpu_to_be64(args.fsbno);
827
828 /*
829 * Do all this logging at the end so that
830 * the root is at the right level.
831 */
832 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
833 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
834 ASSERT(*curp == NULL);
835 *curp = cur;
836 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
837 return 0;
838}
839
840/*
841 * Convert a local file to an extents file.
842 * This code is out of bounds for data forks of regular files,
843 * since the file data needs to get logged so things will stay consistent.
844 * (The bmap-level manipulations are ok, though).
845 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000846void
847xfs_bmap_local_to_extents_empty(
848 struct xfs_inode *ip,
849 int whichfork)
850{
851 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
852
Darrick J. Wong60b49842016-10-03 09:11:34 -0700853 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000854 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
855 ASSERT(ifp->if_bytes == 0);
856 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
857
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000858 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000859 ifp->if_flags &= ~XFS_IFINLINE;
860 ifp->if_flags |= XFS_IFEXTENTS;
861 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
862}
863
864
Dave Chinner9e5987a2013-02-25 12:31:26 +1100865STATIC int /* error */
866xfs_bmap_local_to_extents(
867 xfs_trans_t *tp, /* transaction pointer */
868 xfs_inode_t *ip, /* incore inode pointer */
869 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
870 xfs_extlen_t total, /* total blocks needed by transaction */
871 int *logflagsp, /* inode logging flags */
872 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500873 void (*init_fn)(struct xfs_trans *tp,
874 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100875 struct xfs_inode *ip,
876 struct xfs_ifork *ifp))
877{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000878 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100879 int flags; /* logging flags returned */
880 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000881 xfs_alloc_arg_t args; /* allocation arguments */
882 xfs_buf_t *bp; /* buffer for extent block */
883 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100884
885 /*
886 * We don't want to deal with the case of keeping inode data inline yet.
887 * So sending the data fork of a regular inode is invalid.
888 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100889 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100890 ifp = XFS_IFORK_PTR(ip, whichfork);
891 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000892
893 if (!ifp->if_bytes) {
894 xfs_bmap_local_to_extents_empty(ip, whichfork);
895 flags = XFS_ILOG_CORE;
896 goto done;
897 }
898
Dave Chinner9e5987a2013-02-25 12:31:26 +1100899 flags = 0;
900 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000901 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
902 XFS_IFINLINE);
903 memset(&args, 0, sizeof(args));
904 args.tp = tp;
905 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000906 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000907 args.firstblock = *firstblock;
908 /*
909 * Allocate a block. We know we need only one, since the
910 * file currently fits in an inode.
911 */
912 if (*firstblock == NULLFSBLOCK) {
913 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
914 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100915 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000916 args.fsbno = *firstblock;
917 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100918 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000919 args.total = total;
920 args.minlen = args.maxlen = args.prod = 1;
921 error = xfs_alloc_vextent(&args);
922 if (error)
923 goto done;
924
925 /* Can't fail, the space was reserved. */
926 ASSERT(args.fsbno != NULLFSBLOCK);
927 ASSERT(args.len == 1);
928 *firstblock = args.fsbno;
929 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
930
Dave Chinnerfe22d552015-01-22 09:30:06 +1100931 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100932 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100933 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100934 * The callout is responsible for logging because the remote format
935 * might differ from the local format and thus we don't know how much to
936 * log here. Note that init_fn must also set the buffer log item type
937 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100938 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000939 init_fn(tp, bp, ip, ifp);
940
Brian Fosterb7cdc662015-10-12 15:40:24 +1100941 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000942 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
943 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100944 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000945
946 xfs_iext_add(ifp, 0, 1);
947 ep = xfs_iext_get_ext(ifp, 0);
948 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
949 trace_xfs_bmap_post_update(ip, 0,
950 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
951 _THIS_IP_);
952 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
953 ip->i_d.di_nblocks = 1;
954 xfs_trans_mod_dquot_byino(tp, ip,
955 XFS_TRANS_DQ_BCOUNT, 1L);
956 flags |= xfs_ilog_fext(whichfork);
957
Dave Chinner9e5987a2013-02-25 12:31:26 +1100958done:
959 *logflagsp = flags;
960 return error;
961}
962
963/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 * Called from xfs_bmap_add_attrfork to handle btree format files.
965 */
966STATIC int /* error */
967xfs_bmap_add_attrfork_btree(
968 xfs_trans_t *tp, /* transaction pointer */
969 xfs_inode_t *ip, /* incore inode pointer */
970 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000971 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 int *flags) /* inode logging flags */
973{
974 xfs_btree_cur_t *cur; /* btree cursor */
975 int error; /* error return value */
976 xfs_mount_t *mp; /* file system mount struct */
977 int stat; /* newroot status */
978
979 mp = ip->i_mount;
980 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
981 *flags |= XFS_ILOG_DBROOT;
982 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100983 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000984 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 cur->bc_private.b.firstblock = *firstblock;
986 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
987 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000988 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100989 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100990 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 goto error0;
992 if (stat == 0) {
993 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000994 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996 *firstblock = cur->bc_private.b.firstblock;
997 cur->bc_private.b.allocated = 0;
998 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
999 }
1000 return 0;
1001error0:
1002 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1003 return error;
1004}
1005
1006/*
1007 * Called from xfs_bmap_add_attrfork to handle extents format files.
1008 */
1009STATIC int /* error */
1010xfs_bmap_add_attrfork_extents(
1011 xfs_trans_t *tp, /* transaction pointer */
1012 xfs_inode_t *ip, /* incore inode pointer */
1013 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001014 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int *flags) /* inode logging flags */
1016{
1017 xfs_btree_cur_t *cur; /* bmap btree cursor */
1018 int error; /* error return value */
1019
1020 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1021 return 0;
1022 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001023 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 flags, XFS_DATA_FORK);
1025 if (cur) {
1026 cur->bc_private.b.allocated = 0;
1027 xfs_btree_del_cursor(cur,
1028 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1029 }
1030 return error;
1031}
1032
1033/*
Dave Chinner1e823792013-02-11 15:58:13 +11001034 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1035 * different data fork content type needs a different callout to do the
1036 * conversion. Some are basic and only require special block initialisation
1037 * callouts for the data formating, others (directories) are so specialised they
1038 * handle everything themselves.
1039 *
1040 * XXX (dgc): investigate whether directory conversion can use the generic
1041 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001042 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
1044STATIC int /* error */
1045xfs_bmap_add_attrfork_local(
1046 xfs_trans_t *tp, /* transaction pointer */
1047 xfs_inode_t *ip, /* incore inode pointer */
1048 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001049 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 int *flags) /* inode logging flags */
1051{
1052 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1055 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001056
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001057 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001059 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 dargs.dp = ip;
1061 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001062 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001063 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 dargs.whichfork = XFS_DATA_FORK;
1065 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001066 return xfs_dir2_sf_to_block(&dargs);
1067 }
1068
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001069 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001070 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1071 flags, XFS_DATA_FORK,
1072 xfs_symlink_local_to_remote);
1073
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001074 /* should only be called for types that support local format data */
1075 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001076 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
1079/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001080 * Convert inode from non-attributed to attributed.
1081 * Must not be in a transaction, ip must not be locked.
1082 */
1083int /* error code */
1084xfs_bmap_add_attrfork(
1085 xfs_inode_t *ip, /* incore inode pointer */
1086 int size, /* space new attribute needs */
1087 int rsvd) /* xact may use reserved blks */
1088{
1089 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001090 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001091 xfs_mount_t *mp; /* mount structure */
1092 xfs_trans_t *tp; /* transaction pointer */
1093 int blks; /* space reservation */
1094 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001095 int logflags; /* logging flags */
1096 int error; /* error return value */
1097
1098 ASSERT(XFS_IFORK_Q(ip) == 0);
1099
1100 mp = ip->i_mount;
1101 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001102
Dave Chinner9e5987a2013-02-25 12:31:26 +11001103 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001104
1105 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1106 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1107 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001108 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001109
Dave Chinner9e5987a2013-02-25 12:31:26 +11001110 xfs_ilock(ip, XFS_ILOCK_EXCL);
1111 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1112 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1113 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001114 if (error)
1115 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001116 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001117 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001118 if (ip->i_d.di_anextents != 0) {
1119 error = -EFSCORRUPTED;
1120 goto trans_cancel;
1121 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001122 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1123 /*
1124 * For inodes coming from pre-6.2 filesystems.
1125 */
1126 ASSERT(ip->i_d.di_aformat == 0);
1127 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1128 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001129
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001130 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001131 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1132
1133 switch (ip->i_d.di_format) {
1134 case XFS_DINODE_FMT_DEV:
1135 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1136 break;
1137 case XFS_DINODE_FMT_UUID:
1138 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1139 break;
1140 case XFS_DINODE_FMT_LOCAL:
1141 case XFS_DINODE_FMT_EXTENTS:
1142 case XFS_DINODE_FMT_BTREE:
1143 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1144 if (!ip->i_d.di_forkoff)
1145 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1146 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1147 version = 2;
1148 break;
1149 default:
1150 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001151 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001152 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001153 }
1154
1155 ASSERT(ip->i_afp == NULL);
1156 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1157 ip->i_afp->if_flags = XFS_IFEXTENTS;
1158 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001159 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001160 switch (ip->i_d.di_format) {
1161 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001162 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001163 &logflags);
1164 break;
1165 case XFS_DINODE_FMT_EXTENTS:
1166 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001167 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001168 break;
1169 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001170 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001171 &logflags);
1172 break;
1173 default:
1174 error = 0;
1175 break;
1176 }
1177 if (logflags)
1178 xfs_trans_log_inode(tp, ip, logflags);
1179 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001180 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001181 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1182 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001183 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001184
1185 spin_lock(&mp->m_sb_lock);
1186 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1187 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001188 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001189 }
1190 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1191 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001192 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001193 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001194 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001195 if (log_sb)
1196 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001197 }
1198
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001199 error = xfs_defer_finish(&tp, &dfops, NULL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001200 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001201 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001202 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001203 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001204 return error;
1205
1206bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001207 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001208trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001209 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001210 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001211 return error;
1212}
1213
1214/*
1215 * Internal and external extent tree search functions.
1216 */
1217
1218/*
1219 * Read in the extents to if_extents.
1220 * All inode fields are set up by caller, we just traverse the btree
1221 * and copy the records in. If the file system cannot contain unwritten
1222 * extents, the records are checked for no "state" flags.
1223 */
1224int /* error */
1225xfs_bmap_read_extents(
1226 xfs_trans_t *tp, /* transaction pointer */
1227 xfs_inode_t *ip, /* incore inode */
1228 int whichfork) /* data or attr fork */
1229{
1230 struct xfs_btree_block *block; /* current btree block */
1231 xfs_fsblock_t bno; /* block # of "block" */
1232 xfs_buf_t *bp; /* buffer for "block" */
1233 int error; /* error return value */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001234 xfs_extnum_t i, j; /* index into the extents list */
1235 xfs_ifork_t *ifp; /* fork structure */
1236 int level; /* btree level, for checking */
1237 xfs_mount_t *mp; /* file system mount structure */
1238 __be64 *pp; /* pointer to block address */
1239 /* REFERENCED */
1240 xfs_extnum_t room; /* number of entries there's room for */
1241
Dave Chinner9e5987a2013-02-25 12:31:26 +11001242 mp = ip->i_mount;
1243 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001244 block = ifp->if_broot;
1245 /*
1246 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1247 */
1248 level = be16_to_cpu(block->bb_level);
1249 ASSERT(level > 0);
1250 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1251 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001252
Dave Chinner9e5987a2013-02-25 12:31:26 +11001253 /*
1254 * Go down the tree until leaf level is reached, following the first
1255 * pointer (leftmost) at each level.
1256 */
1257 while (level-- > 0) {
1258 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1259 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1260 if (error)
1261 return error;
1262 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001263 if (level == 0)
1264 break;
1265 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1266 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001267 XFS_WANT_CORRUPTED_GOTO(mp,
1268 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001269 xfs_trans_brelse(tp, bp);
1270 }
1271 /*
1272 * Here with bp and block set to the leftmost leaf node in the tree.
1273 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001274 room = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001275 i = 0;
1276 /*
1277 * Loop over all leaf nodes. Copy information to the extent records.
1278 */
1279 for (;;) {
1280 xfs_bmbt_rec_t *frp;
1281 xfs_fsblock_t nextbno;
1282 xfs_extnum_t num_recs;
1283 xfs_extnum_t start;
1284
1285 num_recs = xfs_btree_get_numrecs(block);
1286 if (unlikely(i + num_recs > room)) {
1287 ASSERT(i + num_recs <= room);
1288 xfs_warn(ip->i_mount,
1289 "corrupt dinode %Lu, (btree extents).",
1290 (unsigned long long) ip->i_ino);
1291 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1292 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1293 goto error0;
1294 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001295 /*
1296 * Read-ahead the next leaf block, if any.
1297 */
1298 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1299 if (nextbno != NULLFSBLOCK)
1300 xfs_btree_reada_bufl(mp, nextbno, 1,
1301 &xfs_bmbt_buf_ops);
1302 /*
1303 * Copy records into the extent records.
1304 */
1305 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1306 start = i;
1307 for (j = 0; j < num_recs; j++, i++, frp++) {
1308 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1309 trp->l0 = be64_to_cpu(frp->l0);
1310 trp->l1 = be64_to_cpu(frp->l1);
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001311 if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001312 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001313 XFS_ERRLEVEL_LOW, mp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001314 goto error0;
1315 }
1316 }
1317 xfs_trans_brelse(tp, bp);
1318 bno = nextbno;
1319 /*
1320 * If we've reached the end, stop.
1321 */
1322 if (bno == NULLFSBLOCK)
1323 break;
1324 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1325 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1326 if (error)
1327 return error;
1328 block = XFS_BUF_TO_BLOCK(bp);
1329 }
Darrick J. Wong356a3222016-12-05 12:36:56 +11001330 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1331 return -EFSCORRUPTED;
Eric Sandeen5d829302016-11-08 12:59:42 +11001332 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11001333 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1334 return 0;
1335error0:
1336 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001337 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001338}
1339
Dave Chinner9e5987a2013-02-25 12:31:26 +11001340/*
1341 * Returns the file-relative block number of the first unused block(s)
1342 * in the file with at least "len" logically contiguous blocks free.
1343 * This is the lowest-address hole if the file has holes, else the first block
1344 * past the end of file.
1345 * Return 0 if the file is currently local (in-inode).
1346 */
1347int /* error */
1348xfs_bmap_first_unused(
1349 xfs_trans_t *tp, /* transaction pointer */
1350 xfs_inode_t *ip, /* incore inode */
1351 xfs_extlen_t len, /* size of hole to find */
1352 xfs_fileoff_t *first_unused, /* unused block */
1353 int whichfork) /* data or attr fork */
1354{
1355 int error; /* error return value */
1356 int idx; /* extent record index */
1357 xfs_ifork_t *ifp; /* inode fork pointer */
1358 xfs_fileoff_t lastaddr; /* last block number seen */
1359 xfs_fileoff_t lowest; /* lowest useful block */
1360 xfs_fileoff_t max; /* starting useful block */
1361 xfs_fileoff_t off; /* offset for this block */
1362 xfs_extnum_t nextents; /* number of extent entries */
1363
1364 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1365 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1366 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1367 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1368 *first_unused = 0;
1369 return 0;
1370 }
1371 ifp = XFS_IFORK_PTR(ip, whichfork);
1372 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1373 (error = xfs_iread_extents(tp, ip, whichfork)))
1374 return error;
1375 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001376 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001377 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1378 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1379 off = xfs_bmbt_get_startoff(ep);
1380 /*
1381 * See if the hole before this extent will work.
1382 */
1383 if (off >= lowest + len && off - max >= len) {
1384 *first_unused = max;
1385 return 0;
1386 }
1387 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1388 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1389 }
1390 *first_unused = max;
1391 return 0;
1392}
1393
1394/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001395 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001396 * last_block (input value) in the file.
1397 * This is not based on i_size, it is based on the extent records.
1398 * Returns 0 for local files, as they do not have extent records.
1399 */
1400int /* error */
1401xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001402 struct xfs_trans *tp, /* transaction pointer */
1403 struct xfs_inode *ip, /* incore inode */
1404 xfs_fileoff_t *last_block, /* last block */
1405 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001406{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001407 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1408 struct xfs_bmbt_irec got;
1409 xfs_extnum_t idx;
1410 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001411
Christoph Hellwig86685f72016-11-24 11:39:38 +11001412 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1413 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001414 *last_block = 0;
1415 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001416 case XFS_DINODE_FMT_BTREE:
1417 case XFS_DINODE_FMT_EXTENTS:
1418 break;
1419 default:
1420 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001421 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001422
1423 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1424 error = xfs_iread_extents(tp, ip, whichfork);
1425 if (error)
1426 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001427 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001428
1429 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1430 if (got.br_startoff <= *last_block - 1)
1431 return 0;
1432 }
1433
1434 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1435 *last_block = got.br_startoff + got.br_blockcount;
1436 return 0;
1437 }
1438
1439 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001440 return 0;
1441}
1442
Dave Chinner68988112013-08-12 20:49:42 +10001443int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001444xfs_bmap_last_extent(
1445 struct xfs_trans *tp,
1446 struct xfs_inode *ip,
1447 int whichfork,
1448 struct xfs_bmbt_irec *rec,
1449 int *is_empty)
1450{
1451 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1452 int error;
1453 int nextents;
1454
1455 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1456 error = xfs_iread_extents(tp, ip, whichfork);
1457 if (error)
1458 return error;
1459 }
1460
Eric Sandeen5d829302016-11-08 12:59:42 +11001461 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001462 if (nextents == 0) {
1463 *is_empty = 1;
1464 return 0;
1465 }
1466
1467 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1468 *is_empty = 0;
1469 return 0;
1470}
1471
1472/*
1473 * Check the last inode extent to determine whether this allocation will result
1474 * in blocks being allocated at the end of the file. When we allocate new data
1475 * blocks at the end of the file which do not start at the previous data block,
1476 * we will try to align the new blocks at stripe unit boundaries.
1477 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001478 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001479 * at, or past the EOF.
1480 */
1481STATIC int
1482xfs_bmap_isaeof(
1483 struct xfs_bmalloca *bma,
1484 int whichfork)
1485{
1486 struct xfs_bmbt_irec rec;
1487 int is_empty;
1488 int error;
1489
1490 bma->aeof = 0;
1491 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1492 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001493 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001494 return error;
1495
Dave Chinner6e708bc2013-11-22 10:41:16 +11001496 if (is_empty) {
1497 bma->aeof = 1;
1498 return 0;
1499 }
1500
Dave Chinner9e5987a2013-02-25 12:31:26 +11001501 /*
1502 * Check if we are allocation or past the last extent, or at least into
1503 * the last delayed allocated extent.
1504 */
1505 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1506 (bma->offset >= rec.br_startoff &&
1507 isnullstartblock(rec.br_startblock));
1508 return 0;
1509}
1510
1511/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001512 * Returns the file-relative block number of the first block past eof in
1513 * the file. This is not based on i_size, it is based on the extent records.
1514 * Returns 0 for local files, as they do not have extent records.
1515 */
1516int
1517xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001518 struct xfs_inode *ip,
1519 xfs_fileoff_t *last_block,
1520 int whichfork)
1521{
1522 struct xfs_bmbt_irec rec;
1523 int is_empty;
1524 int error;
1525
1526 *last_block = 0;
1527
1528 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1529 return 0;
1530
1531 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1532 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001533 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001534
1535 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1536 if (error || is_empty)
1537 return error;
1538
1539 *last_block = rec.br_startoff + rec.br_blockcount;
1540 return 0;
1541}
1542
1543/*
1544 * Returns whether the selected fork of the inode has exactly one
1545 * block or not. For the data fork we check this matches di_size,
1546 * implying the file's range is 0..bsize-1.
1547 */
1548int /* 1=>1 block, 0=>otherwise */
1549xfs_bmap_one_block(
1550 xfs_inode_t *ip, /* incore inode */
1551 int whichfork) /* data or attr fork */
1552{
1553 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1554 xfs_ifork_t *ifp; /* inode fork pointer */
1555 int rval; /* return value */
1556 xfs_bmbt_irec_t s; /* internal version of extent */
1557
1558#ifndef DEBUG
1559 if (whichfork == XFS_DATA_FORK)
1560 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1561#endif /* !DEBUG */
1562 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1563 return 0;
1564 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1565 return 0;
1566 ifp = XFS_IFORK_PTR(ip, whichfork);
1567 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1568 ep = xfs_iext_get_ext(ifp, 0);
1569 xfs_bmbt_get_all(ep, &s);
1570 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1571 if (rval && whichfork == XFS_DATA_FORK)
1572 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1573 return rval;
1574}
1575
1576/*
1577 * Extent tree manipulation functions used during allocation.
1578 */
1579
1580/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001581 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 */
1583STATIC int /* error */
1584xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001585 struct xfs_bmalloca *bma,
1586 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001588 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001590 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001593 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 xfs_fileoff_t new_endoff; /* end offset of new entry */
1595 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1596 /* left is 0, right is 1, prev is 2 */
1597 int rval=0; /* return value (logging flags) */
1598 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001599 xfs_filblks_t da_new; /* new count del alloc blocks used */
1600 xfs_filblks_t da_old; /* old count del alloc blocks used */
1601 xfs_filblks_t temp=0; /* value for da_new calculations */
1602 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001604 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001605 xfs_extnum_t *nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001607 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001608 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001609 ASSERT(whichfork != XFS_ATTR_FORK);
1610 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1611 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001612
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001613 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001614 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001615 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001616 ASSERT(!bma->cur ||
1617 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001618
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001619 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001620
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621#define LEFT r[0]
1622#define RIGHT r[1]
1623#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Darrick J. Wong60b49842016-10-03 09:11:34 -07001625 if (whichfork == XFS_COW_FORK)
1626 state |= BMAP_COWFORK;
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /*
1629 * Set up a bunch of variables to make the tests simpler.
1630 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001631 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 xfs_bmbt_get_all(ep, &PREV);
1633 new_endoff = new->br_startoff + new->br_blockcount;
1634 ASSERT(PREV.br_startoff <= new->br_startoff);
1635 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001636
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001637 da_old = startblockval(PREV.br_startblock);
1638 da_new = 0;
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /*
1641 * Set flags determining what part of the previous delayed allocation
1642 * extent is being replaced by a real allocation.
1643 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001644 if (PREV.br_startoff == new->br_startoff)
1645 state |= BMAP_LEFT_FILLING;
1646 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1647 state |= BMAP_RIGHT_FILLING;
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 /*
1650 * Check and set flags if this segment has a left neighbor.
1651 * Don't set contiguous if the combined extent would be too large.
1652 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001653 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001654 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001655 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001656
1657 if (isnullstartblock(LEFT.br_startblock))
1658 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001660
1661 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1662 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1663 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1664 LEFT.br_state == new->br_state &&
1665 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1666 state |= BMAP_LEFT_CONTIG;
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 /*
1669 * Check and set flags if this segment has a right neighbor.
1670 * Don't set contiguous if the combined extent would be too large.
1671 * Also check for all-three-contiguous being too large.
1672 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001673 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001674 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001675 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001676
1677 if (isnullstartblock(RIGHT.br_startblock))
1678 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001680
1681 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1682 new_endoff == RIGHT.br_startoff &&
1683 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1684 new->br_state == RIGHT.br_state &&
1685 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1686 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1687 BMAP_RIGHT_FILLING)) !=
1688 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1689 BMAP_RIGHT_FILLING) ||
1690 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1691 <= MAXEXTLEN))
1692 state |= BMAP_RIGHT_CONTIG;
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 error = 0;
1695 /*
1696 * Switch out based on the FILLING and CONTIG state bits.
1697 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001698 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1699 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1700 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1701 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 /*
1703 * Filling in all of a previously delayed allocation extent.
1704 * The left and right neighbors are both contiguous with new.
1705 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001706 bma->idx--;
1707 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1708 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 LEFT.br_blockcount + PREV.br_blockcount +
1710 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001711 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001712
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001713 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001714 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001715 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1717 else {
1718 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001719 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001721 RIGHT.br_blockcount, &i);
1722 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001724 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001725 error = xfs_btree_delete(bma->cur, &i);
1726 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001728 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001729 error = xfs_btree_decrement(bma->cur, 0, &i);
1730 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001732 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001733 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 LEFT.br_startblock,
1735 LEFT.br_blockcount +
1736 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001737 RIGHT.br_blockcount, LEFT.br_state);
1738 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 goto done;
1740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 break;
1742
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001743 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 /*
1745 * Filling in all of a previously delayed allocation extent.
1746 * The left neighbor is contiguous, the right is not.
1747 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001748 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001749
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001750 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1751 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001752 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001753 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001754
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001755 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1756 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 rval = XFS_ILOG_DEXT;
1758 else {
1759 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001760 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001762 &i);
1763 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001765 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001766 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 LEFT.br_startblock,
1768 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001769 PREV.br_blockcount, LEFT.br_state);
1770 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 goto done;
1772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 break;
1774
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001775 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 /*
1777 * Filling in all of a previously delayed allocation extent.
1778 * The right neighbor is contiguous, the left is not.
1779 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001780 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 xfs_bmbt_set_startblock(ep, new->br_startblock);
1782 xfs_bmbt_set_blockcount(ep,
1783 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001784 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001785
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001786 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1787 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 rval = XFS_ILOG_DEXT;
1789 else {
1790 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001791 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001793 RIGHT.br_blockcount, &i);
1794 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001796 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001797 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 new->br_startblock,
1799 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001800 RIGHT.br_blockcount, PREV.br_state);
1801 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 goto done;
1803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 break;
1805
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001806 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 /*
1808 * Filling in all of a previously delayed allocation extent.
1809 * Neither the left nor right neighbors are contiguous with
1810 * the new one.
1811 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001812 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 xfs_bmbt_set_startblock(ep, new->br_startblock);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08001814 xfs_bmbt_set_state(ep, new->br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001815 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001816
Darrick J. Wong60b49842016-10-03 09:11:34 -07001817 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001818 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1820 else {
1821 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001822 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001824 &i);
1825 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001827 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001828 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1829 error = xfs_btree_insert(bma->cur, &i);
1830 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001832 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 break;
1835
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001836 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 /*
1838 * Filling in the first part of a previous delayed allocation.
1839 * The left neighbor is contiguous.
1840 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001841 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1842 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 LEFT.br_blockcount + new->br_blockcount);
1844 xfs_bmbt_set_startoff(ep,
1845 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001846 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001847
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001849 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001851 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 rval = XFS_ILOG_DEXT;
1853 else {
1854 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001855 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001857 &i);
1858 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001860 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001861 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 LEFT.br_startblock,
1863 LEFT.br_blockcount +
1864 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001865 LEFT.br_state);
1866 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 goto done;
1868 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001869 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001870 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001871 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001872 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001873
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001874 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 break;
1876
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001877 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 /*
1879 * Filling in the first part of a previous delayed allocation.
1880 * The left neighbor is not contiguous.
1881 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001882 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 xfs_bmbt_set_startoff(ep, new_endoff);
1884 temp = PREV.br_blockcount - new->br_blockcount;
1885 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001886 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001887 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001888 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1890 else {
1891 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001892 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001894 &i);
1895 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001897 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001898 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1899 error = xfs_btree_insert(bma->cur, &i);
1900 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001902 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001904
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001905 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001906 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001907 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001908 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 rval |= tmp_rval;
1910 if (error)
1911 goto done;
1912 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001913 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001914 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001915 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1916 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001917 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001918 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 break;
1920
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001921 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 /*
1923 * Filling in the last part of a previous delayed allocation.
1924 * The right neighbor is contiguous with the new allocation.
1925 */
1926 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001927 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001929 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001930 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 new->br_blockcount + RIGHT.br_blockcount,
1932 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001933 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1934 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 rval = XFS_ILOG_DEXT;
1936 else {
1937 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001938 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001940 RIGHT.br_blockcount, &i);
1941 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001943 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001944 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 new->br_startblock,
1946 new->br_blockcount +
1947 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001948 RIGHT.br_state);
1949 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 goto done;
1951 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001952
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001953 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001954 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001955 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001956 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001957 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001958
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001959 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 break;
1961
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001962 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /*
1964 * Filling in the last part of a previous delayed allocation.
1965 * The right neighbor is not contiguous.
1966 */
1967 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001968 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001970 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001971 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001972 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1974 else {
1975 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001976 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001978 &i);
1979 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001981 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001982 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1983 error = xfs_btree_insert(bma->cur, &i);
1984 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001986 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001988
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001989 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001990 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001991 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001992 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 rval |= tmp_rval;
1994 if (error)
1995 goto done;
1996 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001997 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001998 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001999 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2000 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002001 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002002 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002003
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002004 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 break;
2006
2007 case 0:
2008 /*
2009 * Filling in the middle part of a previous delayed allocation.
2010 * Contiguity is impossible here.
2011 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002012 *
2013 * We start with a delayed allocation:
2014 *
2015 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2016 * PREV @ idx
2017 *
2018 * and we are allocating:
2019 * +rrrrrrrrrrrrrrrrr+
2020 * new
2021 *
2022 * and we set it up for insertion as:
2023 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2024 * new
2025 * PREV @ idx LEFT RIGHT
2026 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 */
2028 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002030 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002031 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2032 LEFT = *new;
2033 RIGHT.br_state = PREV.br_state;
2034 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002035 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002036 RIGHT.br_startoff = new_endoff;
2037 RIGHT.br_blockcount = temp2;
2038 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002039 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002040 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002041 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2043 else {
2044 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002045 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002047 &i);
2048 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002050 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002051 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2052 error = xfs_btree_insert(bma->cur, &i);
2053 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002055 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002057
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002058 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002059 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002060 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002061 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 rval |= tmp_rval;
2063 if (error)
2064 goto done;
2065 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002066 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2067 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002068 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002069 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002070 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002071 error = xfs_mod_fdblocks(bma->ip->i_mount,
2072 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002073 ASSERT(!error);
2074 if (error)
2075 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002077
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002078 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002079 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002080 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2081 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2082 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002083 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002084 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002085
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002086 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002087 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 break;
2089
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002090 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2091 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2092 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2093 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2094 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2095 case BMAP_LEFT_CONTIG:
2096 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 /*
2098 * These cases are all impossible.
2099 */
2100 ASSERT(0);
2101 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002102
Darrick J. Wong9c194642016-08-03 12:16:05 +10002103 /* add reverse mapping */
2104 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2105 if (error)
2106 goto done;
2107
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002108 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002109 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002110 int tmp_logflags; /* partial log flag return val */
2111
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002112 ASSERT(bma->cur == NULL);
2113 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002114 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002115 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002116 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002117 if (error)
2118 goto done;
2119 }
2120
2121 /* adjust for changes in reserved delayed indirect blocks */
2122 if (da_old || da_new) {
2123 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002124 if (bma->cur)
2125 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002126 ASSERT(temp <= da_old);
2127 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002128 xfs_mod_fdblocks(bma->ip->i_mount,
2129 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002130 }
2131
2132 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002133 if (bma->cur)
2134 bma->cur->bc_private.b.allocated = 0;
2135
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002136 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002138 if (whichfork != XFS_COW_FORK)
2139 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return error;
2141#undef LEFT
2142#undef RIGHT
2143#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144}
2145
2146/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002147 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 */
2149STATIC int /* error */
2150xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002151 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002153 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002154 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002156 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002157 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002158 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002159 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002162 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002165 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 xfs_fileoff_t new_endoff; /* end offset of new entry */
2167 xfs_exntst_t newext; /* new extent state */
2168 xfs_exntst_t oldext; /* old extent state */
2169 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2170 /* left is 0, right is 1, prev is 2 */
2171 int rval=0; /* return value (logging flags) */
2172 int state = 0;/* state bits, accessed thru macros */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002173 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002175 *logflagsp = 0;
2176
2177 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002178 ifp = XFS_IFORK_PTR(ip, whichfork);
2179 if (whichfork == XFS_COW_FORK)
2180 state |= BMAP_COWFORK;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002181
2182 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002183 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002184 ASSERT(!isnullstartblock(new->br_startblock));
2185
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002186 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002187
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188#define LEFT r[0]
2189#define RIGHT r[1]
2190#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 /*
2193 * Set up a bunch of variables to make the tests simpler.
2194 */
2195 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002196 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 xfs_bmbt_get_all(ep, &PREV);
2198 newext = new->br_state;
2199 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2200 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2201 ASSERT(PREV.br_state == oldext);
2202 new_endoff = new->br_startoff + new->br_blockcount;
2203 ASSERT(PREV.br_startoff <= new->br_startoff);
2204 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 /*
2207 * Set flags determining what part of the previous oldext allocation
2208 * extent is being replaced by a newext allocation.
2209 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002210 if (PREV.br_startoff == new->br_startoff)
2211 state |= BMAP_LEFT_FILLING;
2212 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2213 state |= BMAP_RIGHT_FILLING;
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /*
2216 * Check and set flags if this segment has a left neighbor.
2217 * Don't set contiguous if the combined extent would be too large.
2218 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002219 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002220 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002221 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002222
2223 if (isnullstartblock(LEFT.br_startblock))
2224 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002226
2227 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2228 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2229 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2230 LEFT.br_state == newext &&
2231 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2232 state |= BMAP_LEFT_CONTIG;
2233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 /*
2235 * Check and set flags if this segment has a right neighbor.
2236 * Don't set contiguous if the combined extent would be too large.
2237 * Also check for all-three-contiguous being too large.
2238 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002239 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002240 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002241 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002242 if (isnullstartblock(RIGHT.br_startblock))
2243 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002245
2246 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2247 new_endoff == RIGHT.br_startoff &&
2248 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2249 newext == RIGHT.br_state &&
2250 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2251 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2252 BMAP_RIGHT_FILLING)) !=
2253 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2254 BMAP_RIGHT_FILLING) ||
2255 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2256 <= MAXEXTLEN))
2257 state |= BMAP_RIGHT_CONTIG;
2258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 /*
2260 * Switch out based on the FILLING and CONTIG state bits.
2261 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002262 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2263 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2264 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2265 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 /*
2267 * Setting all of a previous oldext extent to newext.
2268 * The left and right neighbors are both contiguous with new.
2269 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002270 --*idx;
2271
2272 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2273 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 LEFT.br_blockcount + PREV.br_blockcount +
2275 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002276 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002277
Christoph Hellwigec90c552011-05-23 08:52:53 +00002278 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002279 XFS_IFORK_NEXT_SET(ip, whichfork,
2280 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (cur == NULL)
2282 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2283 else {
2284 rval = XFS_ILOG_CORE;
2285 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2286 RIGHT.br_startblock,
2287 RIGHT.br_blockcount, &i)))
2288 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002289 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002290 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002292 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002293 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002295 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002296 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002298 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002299 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002301 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2303 LEFT.br_startblock,
2304 LEFT.br_blockcount + PREV.br_blockcount +
2305 RIGHT.br_blockcount, LEFT.br_state)))
2306 goto done;
2307 }
2308 break;
2309
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002310 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 /*
2312 * Setting all of a previous oldext extent to newext.
2313 * The left neighbor is contiguous, the right is not.
2314 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002315 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002316
Christoph Hellwigec90c552011-05-23 08:52:53 +00002317 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2318 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2319 LEFT.br_blockcount + PREV.br_blockcount);
2320 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2321
2322 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002323 XFS_IFORK_NEXT_SET(ip, whichfork,
2324 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 if (cur == NULL)
2326 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2327 else {
2328 rval = XFS_ILOG_CORE;
2329 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2330 PREV.br_startblock, PREV.br_blockcount,
2331 &i)))
2332 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002333 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002334 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002336 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002337 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002339 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2341 LEFT.br_startblock,
2342 LEFT.br_blockcount + PREV.br_blockcount,
2343 LEFT.br_state)))
2344 goto done;
2345 }
2346 break;
2347
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002348 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 /*
2350 * Setting all of a previous oldext extent to newext.
2351 * The right neighbor is contiguous, the left is not.
2352 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002353 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 xfs_bmbt_set_blockcount(ep,
2355 PREV.br_blockcount + RIGHT.br_blockcount);
2356 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002357 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2358 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002359 XFS_IFORK_NEXT_SET(ip, whichfork,
2360 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 if (cur == NULL)
2362 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2363 else {
2364 rval = XFS_ILOG_CORE;
2365 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2366 RIGHT.br_startblock,
2367 RIGHT.br_blockcount, &i)))
2368 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002369 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002370 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002372 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002373 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002375 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2377 new->br_startblock,
2378 new->br_blockcount + RIGHT.br_blockcount,
2379 newext)))
2380 goto done;
2381 }
2382 break;
2383
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002384 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 /*
2386 * Setting all of a previous oldext extent to newext.
2387 * Neither the left nor right neighbors are contiguous with
2388 * the new one.
2389 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002390 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002392 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if (cur == NULL)
2395 rval = XFS_ILOG_DEXT;
2396 else {
2397 rval = 0;
2398 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2399 new->br_startblock, new->br_blockcount,
2400 &i)))
2401 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002402 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2404 new->br_startblock, new->br_blockcount,
2405 newext)))
2406 goto done;
2407 }
2408 break;
2409
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002410 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 /*
2412 * Setting the first part of a previous oldext extent to newext.
2413 * The left neighbor is contiguous.
2414 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002415 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2416 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 LEFT.br_blockcount + new->br_blockcount);
2418 xfs_bmbt_set_startoff(ep,
2419 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002420 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002421
Christoph Hellwigec90c552011-05-23 08:52:53 +00002422 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 xfs_bmbt_set_startblock(ep,
2424 new->br_startblock + new->br_blockcount);
2425 xfs_bmbt_set_blockcount(ep,
2426 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002427 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002428
Christoph Hellwigec90c552011-05-23 08:52:53 +00002429 --*idx;
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (cur == NULL)
2432 rval = XFS_ILOG_DEXT;
2433 else {
2434 rval = 0;
2435 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2436 PREV.br_startblock, PREV.br_blockcount,
2437 &i)))
2438 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 if ((error = xfs_bmbt_update(cur,
2441 PREV.br_startoff + new->br_blockcount,
2442 PREV.br_startblock + new->br_blockcount,
2443 PREV.br_blockcount - new->br_blockcount,
2444 oldext)))
2445 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002446 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002448 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 LEFT.br_startblock,
2450 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002451 LEFT.br_state);
2452 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 goto done;
2454 }
2455 break;
2456
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002457 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 /*
2459 * Setting the first part of a previous oldext extent to newext.
2460 * The left neighbor is not contiguous.
2461 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002462 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2464 xfs_bmbt_set_startoff(ep, new_endoff);
2465 xfs_bmbt_set_blockcount(ep,
2466 PREV.br_blockcount - new->br_blockcount);
2467 xfs_bmbt_set_startblock(ep,
2468 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002469 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002470
Christoph Hellwigec90c552011-05-23 08:52:53 +00002471 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002472 XFS_IFORK_NEXT_SET(ip, whichfork,
2473 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (cur == NULL)
2475 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2476 else {
2477 rval = XFS_ILOG_CORE;
2478 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2479 PREV.br_startblock, PREV.br_blockcount,
2480 &i)))
2481 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002482 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if ((error = xfs_bmbt_update(cur,
2484 PREV.br_startoff + new->br_blockcount,
2485 PREV.br_startblock + new->br_blockcount,
2486 PREV.br_blockcount - new->br_blockcount,
2487 oldext)))
2488 goto done;
2489 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002490 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002492 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
2494 break;
2495
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002496 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 /*
2498 * Setting the last part of a previous oldext extent to newext.
2499 * The right neighbor is contiguous with the new allocation.
2500 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002501 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 xfs_bmbt_set_blockcount(ep,
2503 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002504 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2505
2506 ++*idx;
2507
2508 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2509 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002510 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002512 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002513
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if (cur == NULL)
2515 rval = XFS_ILOG_DEXT;
2516 else {
2517 rval = 0;
2518 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2519 PREV.br_startblock,
2520 PREV.br_blockcount, &i)))
2521 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002522 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2524 PREV.br_startblock,
2525 PREV.br_blockcount - new->br_blockcount,
2526 oldext)))
2527 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002528 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 goto done;
2530 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2531 new->br_startblock,
2532 new->br_blockcount + RIGHT.br_blockcount,
2533 newext)))
2534 goto done;
2535 }
2536 break;
2537
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002538 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 /*
2540 * Setting the last part of a previous oldext extent to newext.
2541 * The right neighbor is not contiguous.
2542 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002543 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 xfs_bmbt_set_blockcount(ep,
2545 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002546 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002547
Christoph Hellwigec90c552011-05-23 08:52:53 +00002548 ++*idx;
2549 xfs_iext_insert(ip, *idx, 1, new, state);
2550
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002551 XFS_IFORK_NEXT_SET(ip, whichfork,
2552 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (cur == NULL)
2554 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2555 else {
2556 rval = XFS_ILOG_CORE;
2557 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2558 PREV.br_startblock, PREV.br_blockcount,
2559 &i)))
2560 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002561 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2563 PREV.br_startblock,
2564 PREV.br_blockcount - new->br_blockcount,
2565 oldext)))
2566 goto done;
2567 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2568 new->br_startblock, new->br_blockcount,
2569 &i)))
2570 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002571 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002573 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002575 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577 break;
2578
2579 case 0:
2580 /*
2581 * Setting the middle part of a previous oldext extent to
2582 * newext. Contiguity is impossible here.
2583 * One extent becomes three extents.
2584 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002585 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 xfs_bmbt_set_blockcount(ep,
2587 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002588 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 r[0] = *new;
2591 r[1].br_startoff = new_endoff;
2592 r[1].br_blockcount =
2593 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2594 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2595 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002596
2597 ++*idx;
2598 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2599
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002600 XFS_IFORK_NEXT_SET(ip, whichfork,
2601 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 if (cur == NULL)
2603 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2604 else {
2605 rval = XFS_ILOG_CORE;
2606 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2607 PREV.br_startblock, PREV.br_blockcount,
2608 &i)))
2609 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002610 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 /* new right extent - oldext */
2612 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2613 r[1].br_startblock, r[1].br_blockcount,
2614 r[1].br_state)))
2615 goto done;
2616 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002618 cur->bc_rec.b.br_blockcount =
2619 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002620 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002622 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002623 /*
2624 * Reset the cursor to the position of the new extent
2625 * we are about to insert as we can't trust it after
2626 * the previous insert.
2627 */
2628 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2629 new->br_startblock, new->br_blockcount,
2630 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002632 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002634 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002635 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002637 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 }
2639 break;
2640
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002641 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2642 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2643 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2644 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2645 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2646 case BMAP_LEFT_CONTIG:
2647 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 /*
2649 * These cases are all impossible.
2650 */
2651 ASSERT(0);
2652 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002653
Darrick J. Wong9c194642016-08-03 12:16:05 +10002654 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002655 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002656 if (error)
2657 goto done;
2658
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002659 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002660 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002661 int tmp_logflags; /* partial log flag return val */
2662
2663 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002664 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002665 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002666 *logflagsp |= tmp_logflags;
2667 if (error)
2668 goto done;
2669 }
2670
2671 /* clear out the allocated field, done with it now in any case. */
2672 if (cur) {
2673 cur->bc_private.b.allocated = 0;
2674 *curp = cur;
2675 }
2676
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002677 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002679 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 return error;
2681#undef LEFT
2682#undef RIGHT
2683#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
2686/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002687 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002689STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690xfs_bmap_add_extent_hole_delay(
2691 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002692 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002693 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002694 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002696 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2698 xfs_filblks_t newlen=0; /* new indirect size */
2699 xfs_filblks_t oldlen=0; /* old indirect size */
2700 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2701 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002702 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002704 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002706 if (whichfork == XFS_COW_FORK)
2707 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002708 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 /*
2711 * Check and set flags if this segment has a left neighbor
2712 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002713 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002714 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002715 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002716
2717 if (isnullstartblock(left.br_startblock))
2718 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002720
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 /*
2722 * Check and set flags if the current (right) segment exists.
2723 * If it doesn't exist, we're converting the hole at end-of-file.
2724 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002725 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002726 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002727 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002728
2729 if (isnullstartblock(right.br_startblock))
2730 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 /*
2734 * Set contiguity flags on the left and right neighbors.
2735 * Don't let extents get too large, even if the pieces are contiguous.
2736 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002737 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2738 left.br_startoff + left.br_blockcount == new->br_startoff &&
2739 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2740 state |= BMAP_LEFT_CONTIG;
2741
2742 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2743 new->br_startoff + new->br_blockcount == right.br_startoff &&
2744 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2745 (!(state & BMAP_LEFT_CONTIG) ||
2746 (left.br_blockcount + new->br_blockcount +
2747 right.br_blockcount <= MAXEXTLEN)))
2748 state |= BMAP_RIGHT_CONTIG;
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 /*
2751 * Switch out based on the contiguity flags.
2752 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002753 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2754 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 /*
2756 * New allocation is contiguous with delayed allocations
2757 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002758 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002760 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 temp = left.br_blockcount + new->br_blockcount +
2762 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002763
Christoph Hellwigec90c552011-05-23 08:52:53 +00002764 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2765 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002766 oldlen = startblockval(left.br_startblock) +
2767 startblockval(new->br_startblock) +
2768 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002769 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2770 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002771 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002772 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002773 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002774
Christoph Hellwigec90c552011-05-23 08:52:53 +00002775 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 break;
2777
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002778 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 /*
2780 * New allocation is contiguous with a delayed allocation
2781 * on the left.
2782 * Merge the new allocation with the left neighbor.
2783 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002784 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002786
2787 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2788 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002789 oldlen = startblockval(left.br_startblock) +
2790 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002791 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2792 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002793 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002794 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002795 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 break;
2797
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002798 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 /*
2800 * New allocation is contiguous with a delayed allocation
2801 * on the right.
2802 * Merge the new allocation with the right neighbor.
2803 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002804 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002806 oldlen = startblockval(new->br_startblock) +
2807 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002808 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2809 oldlen);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002810 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2811 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002812 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002813 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 break;
2815
2816 case 0:
2817 /*
2818 * New allocation is not contiguous with another
2819 * delayed allocation.
2820 * Insert a new entry.
2821 */
2822 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002823 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 break;
2825 }
2826 if (oldlen != newlen) {
2827 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002828 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2829 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 /*
2831 * Nothing to do for disk quota accounting here.
2832 */
2833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
2835
2836/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002837 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 */
2839STATIC int /* error */
2840xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002841 struct xfs_trans *tp,
2842 struct xfs_inode *ip,
2843 int whichfork,
2844 xfs_extnum_t *idx,
2845 struct xfs_btree_cur **curp,
2846 struct xfs_bmbt_irec *new,
2847 xfs_fsblock_t *first,
2848 struct xfs_defer_ops *dfops,
2849 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002851 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2852 struct xfs_mount *mp = ip->i_mount;
2853 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2857 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002858 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Christoph Hellwig6d045582017-04-11 16:45:54 -07002861 ASSERT(*idx >= 0);
2862 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002863 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002864 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002865
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002866 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002867
2868 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002869 if (whichfork == XFS_ATTR_FORK)
2870 state |= BMAP_ATTRFORK;
Christoph Hellwiga14234c2017-02-06 10:50:49 -08002871 if (whichfork == XFS_COW_FORK)
2872 state |= BMAP_COWFORK;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 /*
2875 * Check and set flags if this segment has a left neighbor.
2876 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002877 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002878 state |= BMAP_LEFT_VALID;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002879 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002880 if (isnullstartblock(left.br_startblock))
2881 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 /*
2885 * Check and set flags if this segment has a current value.
2886 * Not true if we're inserting into the "hole" at eof.
2887 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002888 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002889 state |= BMAP_RIGHT_VALID;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002890 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002891 if (isnullstartblock(right.br_startblock))
2892 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002894
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 /*
2896 * We're inserting a real allocation between "left" and "right".
2897 * Set the contiguity flags. Don't let extents get too large.
2898 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002899 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2900 left.br_startoff + left.br_blockcount == new->br_startoff &&
2901 left.br_startblock + left.br_blockcount == new->br_startblock &&
2902 left.br_state == new->br_state &&
2903 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2904 state |= BMAP_LEFT_CONTIG;
2905
2906 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2907 new->br_startoff + new->br_blockcount == right.br_startoff &&
2908 new->br_startblock + new->br_blockcount == right.br_startblock &&
2909 new->br_state == right.br_state &&
2910 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2911 (!(state & BMAP_LEFT_CONTIG) ||
2912 left.br_blockcount + new->br_blockcount +
2913 right.br_blockcount <= MAXEXTLEN))
2914 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002916 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 /*
2918 * Select which case we're in here, and implement it.
2919 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002920 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2921 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 /*
2923 * New allocation is contiguous with real allocations on the
2924 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002925 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002927 --*idx;
2928 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2929 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 left.br_blockcount + new->br_blockcount +
2931 right.br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002932 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002933
Christoph Hellwig6d045582017-04-11 16:45:54 -07002934 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002935
Christoph Hellwig6d045582017-04-11 16:45:54 -07002936 XFS_IFORK_NEXT_SET(ip, whichfork,
2937 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2938 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002939 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002940 } else {
2941 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002942 error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002943 right.br_startblock, right.br_blockcount,
2944 &i);
2945 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002946 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002947 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002948 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002949 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002950 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002951 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002952 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002953 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002954 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002955 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002956 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002957 left.br_startblock,
2958 left.br_blockcount +
2959 new->br_blockcount +
2960 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002961 left.br_state);
2962 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002963 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002965 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002967 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 /*
2969 * New allocation is contiguous with a real allocation
2970 * on the left.
2971 * Merge the new allocation with the left neighbor.
2972 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002973 --*idx;
2974 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2975 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 left.br_blockcount + new->br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002977 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002978
Christoph Hellwig6d045582017-04-11 16:45:54 -07002979 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002980 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002981 } else {
2982 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002983 error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002984 left.br_startblock, left.br_blockcount,
2985 &i);
2986 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002987 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002988 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002989 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002990 left.br_startblock,
2991 left.br_blockcount +
2992 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002993 left.br_state);
2994 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002995 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002997 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002999 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 /*
3001 * New allocation is contiguous with a real allocation
3002 * on the right.
3003 * Merge the new allocation with the right neighbor.
3004 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003005 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
3006 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003007 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 new->br_blockcount + right.br_blockcount,
3009 right.br_state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003010 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003011
Christoph Hellwig6d045582017-04-11 16:45:54 -07003012 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003013 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003014 } else {
3015 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003016 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003017 right.br_startoff,
3018 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003019 right.br_blockcount, &i);
3020 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003021 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003022 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003023 error = xfs_bmbt_update(cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003024 new->br_startblock,
3025 new->br_blockcount +
3026 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003027 right.br_state);
3028 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003029 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
3033 case 0:
3034 /*
3035 * New allocation is not contiguous with another
3036 * real allocation.
3037 * Insert a new entry.
3038 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003039 xfs_iext_insert(ip, *idx, 1, new, state);
3040 XFS_IFORK_NEXT_SET(ip, whichfork,
3041 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3042 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003043 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003044 } else {
3045 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003046 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003047 new->br_startoff,
3048 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003049 new->br_blockcount, &i);
3050 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003051 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003052 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003053 cur->bc_rec.b.br_state = new->br_state;
3054 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003055 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003056 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003057 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003059 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003061
Darrick J. Wong9c194642016-08-03 12:16:05 +10003062 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003063 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10003064 if (error)
3065 goto done;
3066
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003067 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003068 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003069 int tmp_logflags; /* partial log flag return val */
3070
Christoph Hellwig6d045582017-04-11 16:45:54 -07003071 ASSERT(cur == NULL);
3072 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003073 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003074 *logflagsp |= tmp_logflags;
3075 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003076 if (error)
3077 goto done;
3078 }
3079
3080 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003081 if (cur)
3082 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003083
Christoph Hellwig6d045582017-04-11 16:45:54 -07003084 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003085done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07003086 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003087 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088}
3089
Nathan Scottdd9f4382006-01-11 15:28:28 +11003090/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003091 * Functions used in the extent read, allocate and remove paths
3092 */
3093
3094/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003095 * Adjust the size of the new extent based on di_extsize and rt extsize.
3096 */
Dave Chinner68988112013-08-12 20:49:42 +10003097int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003098xfs_bmap_extsize_align(
3099 xfs_mount_t *mp,
3100 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3101 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3102 xfs_extlen_t extsz, /* align to this extent size */
3103 int rt, /* is this a realtime inode? */
3104 int eof, /* is extent at end-of-file? */
3105 int delay, /* creating delalloc extent? */
3106 int convert, /* overwriting unwritten extent? */
3107 xfs_fileoff_t *offp, /* in/out: aligned offset */
3108 xfs_extlen_t *lenp) /* in/out: aligned length */
3109{
3110 xfs_fileoff_t orig_off; /* original offset */
3111 xfs_extlen_t orig_alen; /* original length */
3112 xfs_fileoff_t orig_end; /* original off+len */
3113 xfs_fileoff_t nexto; /* next file offset */
3114 xfs_fileoff_t prevo; /* previous file offset */
3115 xfs_fileoff_t align_off; /* temp for offset */
3116 xfs_extlen_t align_alen; /* temp for length */
3117 xfs_extlen_t temp; /* temp for calculations */
3118
3119 if (convert)
3120 return 0;
3121
3122 orig_off = align_off = *offp;
3123 orig_alen = align_alen = *lenp;
3124 orig_end = orig_off + orig_alen;
3125
3126 /*
3127 * If this request overlaps an existing extent, then don't
3128 * attempt to perform any additional alignment.
3129 */
3130 if (!delay && !eof &&
3131 (orig_off >= gotp->br_startoff) &&
3132 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3133 return 0;
3134 }
3135
3136 /*
3137 * If the file offset is unaligned vs. the extent size
3138 * we need to align it. This will be possible unless
3139 * the file was previously written with a kernel that didn't
3140 * perform this alignment, or if a truncate shot us in the
3141 * foot.
3142 */
3143 temp = do_mod(orig_off, extsz);
3144 if (temp) {
3145 align_alen += temp;
3146 align_off -= temp;
3147 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003148
3149 /* Same adjustment for the end of the requested area. */
3150 temp = (align_alen % extsz);
3151 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003152 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003153
3154 /*
3155 * For large extent hint sizes, the aligned extent might be larger than
3156 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3157 * the length back under MAXEXTLEN. The outer allocation loops handle
3158 * short allocation just fine, so it is safe to do this. We only want to
3159 * do it when we are forced to, though, because it means more allocation
3160 * operations are required.
3161 */
3162 while (align_alen > MAXEXTLEN)
3163 align_alen -= extsz;
3164 ASSERT(align_alen <= MAXEXTLEN);
3165
Nathan Scottdd9f4382006-01-11 15:28:28 +11003166 /*
3167 * If the previous block overlaps with this proposed allocation
3168 * then move the start forward without adjusting the length.
3169 */
3170 if (prevp->br_startoff != NULLFILEOFF) {
3171 if (prevp->br_startblock == HOLESTARTBLOCK)
3172 prevo = prevp->br_startoff;
3173 else
3174 prevo = prevp->br_startoff + prevp->br_blockcount;
3175 } else
3176 prevo = 0;
3177 if (align_off != orig_off && align_off < prevo)
3178 align_off = prevo;
3179 /*
3180 * If the next block overlaps with this proposed allocation
3181 * then move the start back without adjusting the length,
3182 * but not before offset 0.
3183 * This may of course make the start overlap previous block,
3184 * and if we hit the offset 0 limit then the next block
3185 * can still overlap too.
3186 */
3187 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3188 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3189 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3190 nexto = gotp->br_startoff + gotp->br_blockcount;
3191 else
3192 nexto = gotp->br_startoff;
3193 } else
3194 nexto = NULLFILEOFF;
3195 if (!eof &&
3196 align_off + align_alen != orig_end &&
3197 align_off + align_alen > nexto)
3198 align_off = nexto > align_alen ? nexto - align_alen : 0;
3199 /*
3200 * If we're now overlapping the next or previous extent that
3201 * means we can't fit an extsz piece in this hole. Just move
3202 * the start forward to the first valid spot and set
3203 * the length so we hit the end.
3204 */
3205 if (align_off != orig_off && align_off < prevo)
3206 align_off = prevo;
3207 if (align_off + align_alen != orig_end &&
3208 align_off + align_alen > nexto &&
3209 nexto != NULLFILEOFF) {
3210 ASSERT(nexto > prevo);
3211 align_alen = nexto - align_off;
3212 }
3213
3214 /*
3215 * If realtime, and the result isn't a multiple of the realtime
3216 * extent size we need to remove blocks until it is.
3217 */
3218 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3219 /*
3220 * We're not covering the original request, or
3221 * we won't be able to once we fix the length.
3222 */
3223 if (orig_off < align_off ||
3224 orig_end > align_off + align_alen ||
3225 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003226 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003227 /*
3228 * Try to fix it by moving the start up.
3229 */
3230 if (align_off + temp <= orig_off) {
3231 align_alen -= temp;
3232 align_off += temp;
3233 }
3234 /*
3235 * Try to fix it by moving the end in.
3236 */
3237 else if (align_off + align_alen - temp >= orig_end)
3238 align_alen -= temp;
3239 /*
3240 * Set the start to the minimum then trim the length.
3241 */
3242 else {
3243 align_alen -= orig_off - align_off;
3244 align_off = orig_off;
3245 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3246 }
3247 /*
3248 * Result doesn't cover the request, fail it.
3249 */
3250 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003251 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003252 } else {
3253 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003254 /* see MAXEXTLEN handling above */
3255 ASSERT(orig_end <= align_off + align_alen ||
3256 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003257 }
3258
3259#ifdef DEBUG
3260 if (!eof && gotp->br_startoff != NULLFILEOFF)
3261 ASSERT(align_off + align_alen <= gotp->br_startoff);
3262 if (prevp->br_startoff != NULLFILEOFF)
3263 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3264#endif
3265
3266 *lenp = align_alen;
3267 *offp = align_off;
3268 return 0;
3269}
3270
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271#define XFS_ALLOC_GAP_UNITS 4
3272
Dave Chinner68988112013-08-12 20:49:42 +10003273void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003274xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003275 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276{
3277 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3279 xfs_mount_t *mp; /* mount point structure */
3280 int nullfb; /* true if ap->firstblock isn't set */
3281 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
3283#define ISVALID(x,y) \
3284 (rt ? \
3285 (x) < mp->m_sb.sb_rblocks : \
3286 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3287 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3288 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3289
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003291 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003292 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3293 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003294 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 /*
3296 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003297 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003299 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3300 !isnullstartblock(ap->prev.br_startblock) &&
3301 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3302 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003303 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 /*
3305 * Adjust for the gap between prevp and us.
3306 */
Dave Chinner3a756672011-09-18 20:40:58 +00003307 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003308 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003310 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3311 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 }
3313 /*
3314 * If not at eof, then compare the two neighbor blocks.
3315 * Figure out whether either one gives us a good starting point,
3316 * and pick the better one.
3317 */
3318 else if (!ap->eof) {
3319 xfs_fsblock_t gotbno; /* right side block number */
3320 xfs_fsblock_t gotdiff=0; /* right side difference */
3321 xfs_fsblock_t prevbno; /* left side block number */
3322 xfs_fsblock_t prevdiff=0; /* left side difference */
3323
3324 /*
3325 * If there's a previous (left) block, select a requested
3326 * start block based on it.
3327 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003328 if (ap->prev.br_startoff != NULLFILEOFF &&
3329 !isnullstartblock(ap->prev.br_startblock) &&
3330 (prevbno = ap->prev.br_startblock +
3331 ap->prev.br_blockcount) &&
3332 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 /*
3334 * Calculate gap to end of previous block.
3335 */
Dave Chinner3a756672011-09-18 20:40:58 +00003336 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003337 (ap->prev.br_startoff +
3338 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 /*
3340 * Figure the startblock based on the previous block's
3341 * end and the gap size.
3342 * Heuristic!
3343 * If the gap is large relative to the piece we're
3344 * allocating, or using it gives us an invalid block
3345 * number, then just use the end of the previous block.
3346 */
Dave Chinner3a756672011-09-18 20:40:58 +00003347 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003349 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 prevbno += adjust;
3351 else
3352 prevdiff += adjust;
3353 /*
3354 * If the firstblock forbids it, can't use it,
3355 * must use default.
3356 */
3357 if (!rt && !nullfb &&
3358 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3359 prevbno = NULLFSBLOCK;
3360 }
3361 /*
3362 * No previous block or can't follow it, just default.
3363 */
3364 else
3365 prevbno = NULLFSBLOCK;
3366 /*
3367 * If there's a following (right) block, select a requested
3368 * start block based on it.
3369 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003370 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 /*
3372 * Calculate gap to start of next block.
3373 */
Dave Chinner3a756672011-09-18 20:40:58 +00003374 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 /*
3376 * Figure the startblock based on the next block's
3377 * start and the gap size.
3378 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003379 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 /*
3381 * Heuristic!
3382 * If the gap is large relative to the piece we're
3383 * allocating, or using it gives us an invalid block
3384 * number, then just use the start of the next block
3385 * offset by our length.
3386 */
Dave Chinner3a756672011-09-18 20:40:58 +00003387 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 ISVALID(gotbno - gotdiff, gotbno))
3389 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003390 else if (ISVALID(gotbno - ap->length, gotbno)) {
3391 gotbno -= ap->length;
3392 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 } else
3394 gotdiff += adjust;
3395 /*
3396 * If the firstblock forbids it, can't use it,
3397 * must use default.
3398 */
3399 if (!rt && !nullfb &&
3400 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3401 gotbno = NULLFSBLOCK;
3402 }
3403 /*
3404 * No next block, just default.
3405 */
3406 else
3407 gotbno = NULLFSBLOCK;
3408 /*
3409 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003410 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 */
3412 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003413 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003415 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003417 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003419#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003420}
3421
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003422static int
3423xfs_bmap_longest_free_extent(
3424 struct xfs_trans *tp,
3425 xfs_agnumber_t ag,
3426 xfs_extlen_t *blen,
3427 int *notinit)
3428{
3429 struct xfs_mount *mp = tp->t_mountp;
3430 struct xfs_perag *pag;
3431 xfs_extlen_t longest;
3432 int error = 0;
3433
3434 pag = xfs_perag_get(mp, ag);
3435 if (!pag->pagf_init) {
3436 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3437 if (error)
3438 goto out;
3439
3440 if (!pag->pagf_init) {
3441 *notinit = 1;
3442 goto out;
3443 }
3444 }
3445
Dave Chinner50adbcb2015-06-22 10:04:31 +10003446 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003447 xfs_alloc_min_freelist(mp, pag),
3448 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003449 if (*blen < longest)
3450 *blen = longest;
3451
3452out:
3453 xfs_perag_put(pag);
3454 return error;
3455}
3456
3457static void
3458xfs_bmap_select_minlen(
3459 struct xfs_bmalloca *ap,
3460 struct xfs_alloc_arg *args,
3461 xfs_extlen_t *blen,
3462 int notinit)
3463{
3464 if (notinit || *blen < ap->minlen) {
3465 /*
3466 * Since we did a BUF_TRYLOCK above, it is possible that
3467 * there is space for this request.
3468 */
3469 args->minlen = ap->minlen;
3470 } else if (*blen < args->maxlen) {
3471 /*
3472 * If the best seen length is less than the request length,
3473 * use the best as the minimum.
3474 */
3475 args->minlen = *blen;
3476 } else {
3477 /*
3478 * Otherwise we've seen an extent as big as maxlen, use that
3479 * as the minimum.
3480 */
3481 args->minlen = args->maxlen;
3482 }
3483}
3484
Nathan Scotta365bdd2006-03-14 13:34:16 +11003485STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003486xfs_bmap_btalloc_nullfb(
3487 struct xfs_bmalloca *ap,
3488 struct xfs_alloc_arg *args,
3489 xfs_extlen_t *blen)
3490{
3491 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003492 xfs_agnumber_t ag, startag;
3493 int notinit = 0;
3494 int error;
3495
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003496 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003497 args->total = ap->total;
3498
Christoph Hellwigc467c042010-02-15 23:34:42 +00003499 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3500 if (startag == NULLAGNUMBER)
3501 startag = ag = 0;
3502
Dave Chinner14b064c2011-01-27 12:16:28 +11003503 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003504 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3505 &notinit);
3506 if (error)
3507 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003508
Christoph Hellwigc467c042010-02-15 23:34:42 +00003509 if (++ag == mp->m_sb.sb_agcount)
3510 ag = 0;
3511 if (ag == startag)
3512 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003513 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003514
3515 xfs_bmap_select_minlen(ap, args, blen, notinit);
3516 return 0;
3517}
3518
3519STATIC int
3520xfs_bmap_btalloc_filestreams(
3521 struct xfs_bmalloca *ap,
3522 struct xfs_alloc_arg *args,
3523 xfs_extlen_t *blen)
3524{
3525 struct xfs_mount *mp = ap->ip->i_mount;
3526 xfs_agnumber_t ag;
3527 int notinit = 0;
3528 int error;
3529
3530 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3531 args->total = ap->total;
3532
3533 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3534 if (ag == NULLAGNUMBER)
3535 ag = 0;
3536
3537 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3538 if (error)
3539 return error;
3540
3541 if (*blen < args->maxlen) {
3542 error = xfs_filestream_new_ag(ap, &ag);
3543 if (error)
3544 return error;
3545
3546 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3547 &notinit);
3548 if (error)
3549 return error;
3550
3551 }
3552
3553 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003554
3555 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003556 * Set the failure fallback case to look in the selected AG as stream
3557 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003558 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003559 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003560 return 0;
3561}
3562
3563STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003564xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003565 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003566{
3567 xfs_mount_t *mp; /* mount point structure */
3568 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003569 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003570 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003571 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003572 xfs_alloc_arg_t args;
3573 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003574 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003575 int nullfb; /* true if ap->firstblock isn't set */
3576 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003577 int tryagain;
3578 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003579 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003580
Dave Chinnera99ebf42011-12-01 11:24:20 +00003581 ASSERT(ap->length);
3582
Nathan Scotta365bdd2006-03-14 13:34:16 +11003583 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003584
3585 /* stripe alignment for allocation is determined by mount parameters */
3586 stripe_align = 0;
3587 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3588 stripe_align = mp->m_swidth;
3589 else if (mp->m_dalign)
3590 stripe_align = mp->m_dalign;
3591
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003592 if (ap->flags & XFS_BMAPI_COWFORK)
3593 align = xfs_get_cowextsz_hint(ap->ip);
3594 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003595 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003596 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003597 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003598 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003599 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003600 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003601 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003602 }
Dave Chinner33177f052013-12-12 16:34:36 +11003603
3604
Dave Chinner0937e0f2011-09-18 20:40:57 +00003605 nullfb = *ap->firstblock == NULLFSBLOCK;
3606 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003607 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003608 if (xfs_alloc_is_userdata(ap->datatype) &&
3609 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003610 ag = xfs_filestream_lookup_ag(ap->ip);
3611 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003612 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003613 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003614 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003615 }
3616 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003617 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003618
3619 xfs_bmap_adjacent(ap);
3620
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003622 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 * it's in the right allocation group.
3624 */
Dave Chinner3a756672011-09-18 20:40:58 +00003625 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 ;
3627 else
Dave Chinner3a756672011-09-18 20:40:58 +00003628 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 * Normal allocation, done through xfs_alloc_vextent.
3631 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003632 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003633 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003634 args.tp = ap->tp;
3635 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003636 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003637 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003638
3639 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003640 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003641 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003642 blen = 0;
3643 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003644 /*
3645 * Search for an allocation group with a single extent large
3646 * enough for the request. If one isn't found, then adjust
3647 * the minimum allocation size to the largest space found.
3648 */
Dave Chinner292378e2016-09-26 08:21:28 +10003649 if (xfs_alloc_is_userdata(ap->datatype) &&
3650 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003651 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3652 else
3653 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003654 if (error)
3655 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003656 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003657 if (xfs_inode_is_filestream(ap->ip))
3658 args.type = XFS_ALLOCTYPE_FIRST_AG;
3659 else
3660 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003661 args.total = args.minlen = ap->minlen;
3662 } else {
3663 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3664 args.total = ap->total;
3665 args.minlen = ap->minlen;
3666 }
David Chinner957d0eb2007-06-18 16:50:37 +10003667 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003668 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003669 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003670 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003671 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003672 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003673 args.prod = 1;
3674 args.mod = 0;
3675 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003676 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003677 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003678 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3679 }
3680 /*
3681 * If we are not low on available data blocks, and the
3682 * underlying logical volume manager is a stripe, and
3683 * the file offset is zero then try to allocate data
3684 * blocks on stripe unit boundary.
3685 * NOTE: ap->aeof is only set if the allocation length
3686 * is >= the stripe unit and the allocation offset is
3687 * at the end of file.
3688 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003689 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003690 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003691 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003692 atype = args.type;
3693 isaligned = 1;
3694 /*
3695 * Adjust for alignment
3696 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003697 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003698 args.minlen = blen - args.alignment;
3699 args.minalignslop = 0;
3700 } else {
3701 /*
3702 * First try an exact bno allocation.
3703 * If it fails then do a near or start bno
3704 * allocation with alignment turned on.
3705 */
3706 atype = args.type;
3707 tryagain = 1;
3708 args.type = XFS_ALLOCTYPE_THIS_BNO;
3709 args.alignment = 1;
3710 /*
3711 * Compute the minlen+alignment for the
3712 * next case. Set slop so that the value
3713 * of minlen+alignment+slop doesn't go up
3714 * between the calls.
3715 */
Dave Chinner33177f052013-12-12 16:34:36 +11003716 if (blen > stripe_align && blen <= args.maxlen)
3717 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003718 else
3719 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003720 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003721 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003722 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003723 args.minlen - 1;
3724 else
3725 args.minalignslop = 0;
3726 }
3727 } else {
3728 args.alignment = 1;
3729 args.minalignslop = 0;
3730 }
3731 args.minleft = ap->minleft;
3732 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003733 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003734 args.datatype = ap->datatype;
3735 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003736 args.ip = ap->ip;
3737
3738 error = xfs_alloc_vextent(&args);
3739 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003740 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003741
Nathan Scotta365bdd2006-03-14 13:34:16 +11003742 if (tryagain && args.fsbno == NULLFSBLOCK) {
3743 /*
3744 * Exact allocation failed. Now try with alignment
3745 * turned on.
3746 */
3747 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003748 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003749 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003750 args.minlen = nextminlen;
3751 args.minalignslop = 0;
3752 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 if ((error = xfs_alloc_vextent(&args)))
3754 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003755 }
3756 if (isaligned && args.fsbno == NULLFSBLOCK) {
3757 /*
3758 * allocation failed, so turn off alignment and
3759 * try again.
3760 */
3761 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003762 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003763 args.alignment = 0;
3764 if ((error = xfs_alloc_vextent(&args)))
3765 return error;
3766 }
3767 if (args.fsbno == NULLFSBLOCK && nullfb &&
3768 args.minlen > ap->minlen) {
3769 args.minlen = ap->minlen;
3770 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003771 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003772 if ((error = xfs_alloc_vextent(&args)))
3773 return error;
3774 }
3775 if (args.fsbno == NULLFSBLOCK && nullfb) {
3776 args.fsbno = 0;
3777 args.type = XFS_ALLOCTYPE_FIRST_AG;
3778 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003779 if ((error = xfs_alloc_vextent(&args)))
3780 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003781 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003782 }
3783 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003784 /*
3785 * check the allocation happened at the same or higher AG than
3786 * the first block that was allocated.
3787 */
3788 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003789 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3790 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003791
Dave Chinner3a756672011-09-18 20:40:58 +00003792 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003793 if (*ap->firstblock == NULLFSBLOCK)
3794 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003795 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003796 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003797 if (!(ap->flags & XFS_BMAPI_COWFORK))
3798 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003799 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3800 if (ap->wasdel)
3801 ap->ip->i_delayed_blks -= args.len;
3802 /*
3803 * Adjust the disk quota also. This was reserved
3804 * earlier.
3805 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003806 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003807 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3808 XFS_TRANS_DQ_BCOUNT,
3809 (long) args.len);
3810 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003811 ap->blkno = NULLFSBLOCK;
3812 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 }
3814 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003815}
3816
3817/*
3818 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3819 * It figures out where to ask the underlying allocator to put the new extent.
3820 */
3821STATIC int
3822xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003823 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003824{
Dave Chinner292378e2016-09-26 08:21:28 +10003825 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3826 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003827 return xfs_bmap_rtalloc(ap);
3828 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829}
3830
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003831/* Trim extent to fit a logical block range. */
3832void
3833xfs_trim_extent(
3834 struct xfs_bmbt_irec *irec,
3835 xfs_fileoff_t bno,
3836 xfs_filblks_t len)
3837{
3838 xfs_fileoff_t distance;
3839 xfs_fileoff_t end = bno + len;
3840
3841 if (irec->br_startoff + irec->br_blockcount <= bno ||
3842 irec->br_startoff >= end) {
3843 irec->br_blockcount = 0;
3844 return;
3845 }
3846
3847 if (irec->br_startoff < bno) {
3848 distance = bno - irec->br_startoff;
3849 if (isnullstartblock(irec->br_startblock))
3850 irec->br_startblock = DELAYSTARTBLOCK;
3851 if (irec->br_startblock != DELAYSTARTBLOCK &&
3852 irec->br_startblock != HOLESTARTBLOCK)
3853 irec->br_startblock += distance;
3854 irec->br_startoff += distance;
3855 irec->br_blockcount -= distance;
3856 }
3857
3858 if (end < irec->br_startoff + irec->br_blockcount) {
3859 distance = irec->br_startoff + irec->br_blockcount - end;
3860 irec->br_blockcount -= distance;
3861 }
3862}
3863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003865 * Trim the returned map to the required bounds
3866 */
3867STATIC void
3868xfs_bmapi_trim_map(
3869 struct xfs_bmbt_irec *mval,
3870 struct xfs_bmbt_irec *got,
3871 xfs_fileoff_t *bno,
3872 xfs_filblks_t len,
3873 xfs_fileoff_t obno,
3874 xfs_fileoff_t end,
3875 int n,
3876 int flags)
3877{
3878 if ((flags & XFS_BMAPI_ENTIRE) ||
3879 got->br_startoff + got->br_blockcount <= obno) {
3880 *mval = *got;
3881 if (isnullstartblock(got->br_startblock))
3882 mval->br_startblock = DELAYSTARTBLOCK;
3883 return;
3884 }
3885
3886 if (obno > *bno)
3887 *bno = obno;
3888 ASSERT((*bno >= obno) || (n == 0));
3889 ASSERT(*bno < end);
3890 mval->br_startoff = *bno;
3891 if (isnullstartblock(got->br_startblock))
3892 mval->br_startblock = DELAYSTARTBLOCK;
3893 else
3894 mval->br_startblock = got->br_startblock +
3895 (*bno - got->br_startoff);
3896 /*
3897 * Return the minimum of what we got and what we asked for for
3898 * the length. We can use the len variable here because it is
3899 * modified below and we could have been there before coming
3900 * here if the first part of the allocation didn't overlap what
3901 * was asked for.
3902 */
3903 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3904 got->br_blockcount - (*bno - got->br_startoff));
3905 mval->br_state = got->br_state;
3906 ASSERT(mval->br_blockcount <= len);
3907 return;
3908}
3909
3910/*
3911 * Update and validate the extent map to return
3912 */
3913STATIC void
3914xfs_bmapi_update_map(
3915 struct xfs_bmbt_irec **map,
3916 xfs_fileoff_t *bno,
3917 xfs_filblks_t *len,
3918 xfs_fileoff_t obno,
3919 xfs_fileoff_t end,
3920 int *n,
3921 int flags)
3922{
3923 xfs_bmbt_irec_t *mval = *map;
3924
3925 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3926 ((mval->br_startoff + mval->br_blockcount) <= end));
3927 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3928 (mval->br_startoff < obno));
3929
3930 *bno = mval->br_startoff + mval->br_blockcount;
3931 *len = end - *bno;
3932 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3933 /* update previous map with new information */
3934 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3935 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3936 ASSERT(mval->br_state == mval[-1].br_state);
3937 mval[-1].br_blockcount = mval->br_blockcount;
3938 mval[-1].br_state = mval->br_state;
3939 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3940 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3941 mval[-1].br_startblock != HOLESTARTBLOCK &&
3942 mval->br_startblock == mval[-1].br_startblock +
3943 mval[-1].br_blockcount &&
3944 ((flags & XFS_BMAPI_IGSTATE) ||
3945 mval[-1].br_state == mval->br_state)) {
3946 ASSERT(mval->br_startoff ==
3947 mval[-1].br_startoff + mval[-1].br_blockcount);
3948 mval[-1].br_blockcount += mval->br_blockcount;
3949 } else if (*n > 0 &&
3950 mval->br_startblock == DELAYSTARTBLOCK &&
3951 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3952 mval->br_startoff ==
3953 mval[-1].br_startoff + mval[-1].br_blockcount) {
3954 mval[-1].br_blockcount += mval->br_blockcount;
3955 mval[-1].br_state = mval->br_state;
3956 } else if (!((*n == 0) &&
3957 ((mval->br_startoff + mval->br_blockcount) <=
3958 obno))) {
3959 mval++;
3960 (*n)++;
3961 }
3962 *map = mval;
3963}
3964
3965/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003966 * Map file blocks to filesystem blocks without allocation.
3967 */
3968int
3969xfs_bmapi_read(
3970 struct xfs_inode *ip,
3971 xfs_fileoff_t bno,
3972 xfs_filblks_t len,
3973 struct xfs_bmbt_irec *mval,
3974 int *nmap,
3975 int flags)
3976{
3977 struct xfs_mount *mp = ip->i_mount;
3978 struct xfs_ifork *ifp;
3979 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003980 xfs_fileoff_t obno;
3981 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003982 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003983 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003984 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003985 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003986 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003987
3988 ASSERT(*nmap >= 1);
3989 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003990 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003991 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003992
3993 if (unlikely(XFS_TEST_ERROR(
3994 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3995 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
3996 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
3997 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003998 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003999 }
4000
4001 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004002 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004003
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004004 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004005
4006 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004007
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004008 /* No CoW fork? Return a hole. */
4009 if (whichfork == XFS_COW_FORK && !ifp) {
4010 mval->br_startoff = bno;
4011 mval->br_startblock = HOLESTARTBLOCK;
4012 mval->br_blockcount = len;
4013 mval->br_state = XFS_EXT_NORM;
4014 *nmap = 1;
4015 return 0;
4016 }
4017
Dave Chinner5c8ed202011-09-18 20:40:45 +00004018 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4019 error = xfs_iread_extents(NULL, ip, whichfork);
4020 if (error)
4021 return error;
4022 }
4023
Christoph Hellwig334f3422016-11-24 11:39:43 +11004024 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4025 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004026 end = bno + len;
4027 obno = bno;
4028
4029 while (bno < end && n < *nmap) {
4030 /* Reading past eof, act as though there's a hole up to end. */
4031 if (eof)
4032 got.br_startoff = end;
4033 if (got.br_startoff > bno) {
4034 /* Reading in a hole. */
4035 mval->br_startoff = bno;
4036 mval->br_startblock = HOLESTARTBLOCK;
4037 mval->br_blockcount =
4038 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4039 mval->br_state = XFS_EXT_NORM;
4040 bno += mval->br_blockcount;
4041 len -= mval->br_blockcount;
4042 mval++;
4043 n++;
4044 continue;
4045 }
4046
4047 /* set up the extent map to return. */
4048 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4049 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4050
4051 /* If we're done, stop now. */
4052 if (bno >= end || n >= *nmap)
4053 break;
4054
4055 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004056 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4057 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004058 }
4059 *nmap = n;
4060 return 0;
4061}
4062
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004063/*
4064 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4065 * global pool and the extent inserted into the inode in-core extent tree.
4066 *
4067 * On entry, got refers to the first extent beyond the offset of the extent to
4068 * allocate or eof is specified if no such extent exists. On return, got refers
4069 * to the extent record that was inserted to the inode fork.
4070 *
4071 * Note that the allocated extent may have been merged with contiguous extents
4072 * during insertion into the inode fork. Thus, got does not reflect the current
4073 * state of the inode fork on return. If necessary, the caller can use lastx to
4074 * look up the updated record in the inode fork.
4075 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10004076int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004077xfs_bmapi_reserve_delalloc(
4078 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004079 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004080 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004081 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004082 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004083 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004084 xfs_extnum_t *lastx,
4085 int eof)
4086{
4087 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004088 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004089 xfs_extlen_t alen;
4090 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004091 char rt = XFS_IS_REALTIME_INODE(ip);
4092 xfs_extlen_t extsz;
4093 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004094 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004095
Brian Foster974ae922016-11-28 14:57:42 +11004096 /*
4097 * Cap the alloc length. Keep track of prealloc so we know whether to
4098 * tag the inode before we return.
4099 */
4100 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004101 if (!eof)
4102 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004103 if (prealloc && alen >= len)
4104 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004105
4106 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004107 if (whichfork == XFS_COW_FORK)
4108 extsz = xfs_get_cowextsz_hint(ip);
4109 else
4110 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004111 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004112 struct xfs_bmbt_irec prev;
4113
4114 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4115 prev.br_startoff = NULLFILEOFF;
4116
4117 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004118 1, 0, &aoff, &alen);
4119 ASSERT(!error);
4120 }
4121
4122 if (rt)
4123 extsz = alen / mp->m_sb.sb_rextsize;
4124
4125 /*
4126 * Make a transaction-less quota reservation for delayed allocation
4127 * blocks. This number gets adjusted later. We return if we haven't
4128 * allocated blocks already inside this loop.
4129 */
4130 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4131 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4132 if (error)
4133 return error;
4134
4135 /*
4136 * Split changing sb for alen and indlen since they could be coming
4137 * from different places.
4138 */
4139 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4140 ASSERT(indlen > 0);
4141
4142 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004143 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004144 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004145 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004146 }
4147
4148 if (error)
4149 goto out_unreserve_quota;
4150
Dave Chinner0d485ad2015-02-23 21:22:03 +11004151 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004152 if (error)
4153 goto out_unreserve_blocks;
4154
4155
4156 ip->i_delayed_blks += alen;
4157
4158 got->br_startoff = aoff;
4159 got->br_startblock = nullstartblock(indlen);
4160 got->br_blockcount = alen;
4161 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004162
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004163 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004164
Brian Foster974ae922016-11-28 14:57:42 +11004165 /*
4166 * Tag the inode if blocks were preallocated. Note that COW fork
4167 * preallocation can occur at the start or end of the extent, even when
4168 * prealloc == 0, so we must also check the aligned offset and length.
4169 */
4170 if (whichfork == XFS_DATA_FORK && prealloc)
4171 xfs_inode_set_eofblocks_tag(ip);
4172 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4173 xfs_inode_set_cowblocks_tag(ip);
4174
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004175 return 0;
4176
4177out_unreserve_blocks:
4178 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004179 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004180 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004181 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004182out_unreserve_quota:
4183 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004184 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004185 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4186 return error;
4187}
4188
Dave Chinnercf11da92014-07-15 07:08:24 +10004189static int
4190xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004191 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004192{
4193 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004194 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004195 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004196 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004197 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004198
Dave Chinnera99ebf42011-12-01 11:24:20 +00004199 ASSERT(bma->length > 0);
4200
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004201 /*
4202 * For the wasdelay case, we could also just allocate the stuff asked
4203 * for in this bmap call but that wouldn't be as good.
4204 */
4205 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004206 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4207 bma->offset = bma->got.br_startoff;
Christoph Hellwig0e8d6302016-11-24 11:40:32 +11004208 if (bma->idx) {
Dave Chinnere0c3da52011-09-18 20:41:01 +00004209 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004210 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004211 }
4212 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004213 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004214 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004215 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004216 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004217 }
4218
4219 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004220 * Set the data type being allocated. For the data fork, the first data
4221 * in the file is treated differently to all other allocations. For the
4222 * attribute fork, we only need to ensure the allocated range is not on
4223 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004224 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004225 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004226 bma->datatype = XFS_ALLOC_NOBUSY;
4227 if (whichfork == XFS_DATA_FORK) {
4228 if (bma->offset == 0)
4229 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4230 else
4231 bma->datatype |= XFS_ALLOC_USERDATA;
4232 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004233 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004234 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004235 }
4236
Dave Chinnere04426b2012-10-05 11:06:59 +10004237 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004238
4239 /*
4240 * Only want to do the alignment at the eof if it is userdata and
4241 * allocation length is larger than a stripe unit.
4242 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004243 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004244 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004245 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004246 if (error)
4247 return error;
4248 }
4249
4250 error = xfs_bmap_alloc(bma);
4251 if (error)
4252 return error;
4253
Dave Chinner29c8d172011-09-18 20:41:00 +00004254 if (bma->cur)
4255 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004256 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004257 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004258 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4259 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4260 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004261 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004262 }
4263 /*
4264 * Bump the number of extents we've allocated
4265 * in this call.
4266 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004267 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004268
Dave Chinner29c8d172011-09-18 20:41:00 +00004269 if (bma->cur)
4270 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004271 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4272
Dave Chinner963c30c2011-09-18 20:40:59 +00004273 bma->got.br_startoff = bma->offset;
4274 bma->got.br_startblock = bma->blkno;
4275 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004276 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004277
4278 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004279 * In the data fork, a wasdelay extent has been initialized, so
4280 * shouldn't be flagged as unwritten.
4281 *
4282 * For the cow fork, however, we convert delalloc reservations
4283 * (extents allocated for speculative preallocation) to
4284 * allocated unwritten extents, and only convert the unwritten
4285 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004286 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004287 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4288 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004289 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004290 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004291
Christoph Hellwigc6534242011-09-18 20:41:05 +00004292 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004293 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004294 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004295 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4296 whichfork, &bma->idx, &bma->cur, &bma->got,
4297 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004298
Christoph Hellwigc315c902011-09-18 20:41:02 +00004299 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004300 if (error)
4301 return error;
4302
4303 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004304 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4305 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4306 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004307 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004308 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004309
Dave Chinner963c30c2011-09-18 20:40:59 +00004310 ASSERT(bma->got.br_startoff <= bma->offset);
4311 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4312 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004313 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4314 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004315 return 0;
4316}
4317
Dave Chinnerb447fe52011-09-18 20:40:51 +00004318STATIC int
4319xfs_bmapi_convert_unwritten(
4320 struct xfs_bmalloca *bma,
4321 struct xfs_bmbt_irec *mval,
4322 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004323 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004324{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004325 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004326 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004327 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004328 int error;
4329
Dave Chinnerb447fe52011-09-18 20:40:51 +00004330 /* check if we need to do unwritten->real conversion */
4331 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4332 (flags & XFS_BMAPI_PREALLOC))
4333 return 0;
4334
4335 /* check if we need to do real->unwritten conversion */
4336 if (mval->br_state == XFS_EXT_NORM &&
4337 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4338 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4339 return 0;
4340
4341 /*
4342 * Modify (by adding) the state flag, if writing.
4343 */
4344 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004345 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4346 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004347 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004348 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004349 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004350 }
4351 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4352 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4353
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004354 /*
4355 * Before insertion into the bmbt, zero the range being converted
4356 * if required.
4357 */
4358 if (flags & XFS_BMAPI_ZERO) {
4359 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4360 mval->br_blockcount);
4361 if (error)
4362 return error;
4363 }
4364
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004365 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4366 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004367 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004368 /*
4369 * Log the inode core unconditionally in the unwritten extent conversion
4370 * path because the conversion might not have done so (e.g., if the
4371 * extent count hasn't changed). We need to make sure the inode is dirty
4372 * in the transaction for the sake of fsync(), even if nothing has
4373 * changed, because fsync() will not force the log for this transaction
4374 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004375 *
4376 * Note: If we're only converting cow fork extents, there aren't
4377 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004378 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004379 if (whichfork != XFS_COW_FORK)
4380 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004381 if (error)
4382 return error;
4383
4384 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004385 * Update our extent pointer, given that
4386 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4387 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004388 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004389 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004390
4391 /*
4392 * We may have combined previously unwritten space with written space,
4393 * so generate another request.
4394 */
4395 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004396 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004397 return 0;
4398}
4399
Christoph Hellwig44032802011-09-18 20:40:48 +00004400/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004401 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4402 * extent state if necessary. Details behaviour is controlled by the flags
4403 * parameter. Only allocates blocks from a single allocation group, to avoid
4404 * locking problems.
4405 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 * The returned value in "firstblock" from the first call in a transaction
4407 * must be remembered and presented to subsequent calls in "firstblock".
4408 * An upper bound for the number of blocks to be allocated is supplied to
4409 * the first call in "total"; if no allocation group has that many free
4410 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4411 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004412int
4413xfs_bmapi_write(
4414 struct xfs_trans *tp, /* transaction pointer */
4415 struct xfs_inode *ip, /* incore inode */
4416 xfs_fileoff_t bno, /* starting file offs. mapped */
4417 xfs_filblks_t len, /* length to map in file */
4418 int flags, /* XFS_BMAPI_... */
4419 xfs_fsblock_t *firstblock, /* first allocated block
4420 controls a.g. for allocs */
4421 xfs_extlen_t total, /* total blocks needed */
4422 struct xfs_bmbt_irec *mval, /* output: map values */
4423 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004424 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004426 struct xfs_mount *mp = ip->i_mount;
4427 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004428 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004429 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004430 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004431 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004432 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004433 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004434 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004435
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004437 xfs_fileoff_t orig_bno; /* original block number value */
4438 int orig_flags; /* original flags arg value */
4439 xfs_filblks_t orig_len; /* original value of len arg */
4440 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4441 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
4443 orig_bno = bno;
4444 orig_len = len;
4445 orig_flags = flags;
4446 orig_mval = mval;
4447 orig_nmap = *nmap;
4448#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004449 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004450
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004452 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4453 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004454 ASSERT(tp != NULL ||
4455 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4456 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004457 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004458 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004459 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004460 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004461
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004462 /* zeroing is for currently only for data extents, not metadata */
4463 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4464 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4465 /*
4466 * we can allocate unwritten extents or pre-zero allocated blocks,
4467 * but it makes no sense to do both at once. This would result in
4468 * zeroing the unwritten extent twice, but it still being an
4469 * unwritten extent....
4470 */
4471 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4472 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4473
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 if (unlikely(XFS_TEST_ERROR(
4475 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004476 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004478 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004479 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004481
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004483 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004484
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004486
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004487 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004488
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004489 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004491 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004493 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004494 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004495 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004496 }
4497
4498 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4499 error = xfs_iread_extents(tp, ip, whichfork);
4500 if (error)
4501 goto error0;
4502 }
4503
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 n = 0;
4505 end = bno + len;
4506 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004507
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004508 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4509 eof = true;
4510 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4511 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004512 bma.tp = tp;
4513 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004514 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004515 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004516 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004517 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004518
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004520 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004521
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004522 /* in hole or beyoned EOF? */
4523 if (eof || bma.got.br_startoff > bno) {
4524 if (flags & XFS_BMAPI_DELALLOC) {
4525 /*
4526 * For the COW fork we can reasonably get a
4527 * request for converting an extent that races
4528 * with other threads already having converted
4529 * part of it, as there converting COW to
4530 * regular blocks is not protected using the
4531 * IOLOCK.
4532 */
4533 ASSERT(flags & XFS_BMAPI_COWFORK);
4534 if (!(flags & XFS_BMAPI_COWFORK)) {
4535 error = -EIO;
4536 goto error0;
4537 }
4538
4539 if (eof || bno >= end)
4540 break;
4541 } else {
4542 need_alloc = true;
4543 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004544 } else if (isnullstartblock(bma.got.br_startblock)) {
4545 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004546 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004547
4548 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 * First, deal with the hole before the allocated space
4550 * that we found, if any.
4551 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004552 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004553 bma.eof = eof;
4554 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4555 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004556 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004557 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004558
Dave Chinnera99ebf42011-12-01 11:24:20 +00004559 /*
4560 * There's a 32/64 bit type mismatch between the
4561 * allocation length request (which can be 64 bits in
4562 * length) and the bma length request, which is
4563 * xfs_extlen_t and therefore 32 bits. Hence we have to
4564 * check for 32-bit overflows and handle them here.
4565 */
4566 if (len > (xfs_filblks_t)MAXEXTLEN)
4567 bma.length = MAXEXTLEN;
4568 else
4569 bma.length = len;
4570
4571 ASSERT(len > 0);
4572 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004573 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 if (error)
4575 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004576 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004577 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004578
4579 /*
4580 * If this is a CoW allocation, record the data in
4581 * the refcount btree for orphan recovery.
4582 */
4583 if (whichfork == XFS_COW_FORK) {
4584 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4585 bma.blkno, bma.length);
4586 if (error)
4587 goto error0;
4588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004590
Dave Chinneraef9a892011-09-18 20:40:44 +00004591 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004592 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4593 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594
Dave Chinnerb447fe52011-09-18 20:40:51 +00004595 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004596 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004597 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004598 continue;
4599 if (error)
4600 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601
Dave Chinneraef9a892011-09-18 20:40:44 +00004602 /* update the extent map to return */
4603 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4604
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 /*
4606 * If we're done, stop now. Stop when we've allocated
4607 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4608 * the transaction may get too big.
4609 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004610 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004612
4613 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004614 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004615 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4616 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 /*
4621 * Transform from btree to extents, give it cur.
4622 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004623 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004624 int tmp_logflags = 0;
4625
Dave Chinner29c8d172011-09-18 20:41:00 +00004626 ASSERT(bma.cur);
4627 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004629 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 if (error)
4631 goto error0;
4632 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004633
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004635 XFS_IFORK_NEXTENTS(ip, whichfork) >
4636 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638error0:
4639 /*
4640 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004641 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004643 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004645 bma.logflags &= ~xfs_ilog_fext(whichfork);
4646 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004648 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 /*
4650 * Log whatever the flags say, even if error. Otherwise we might miss
4651 * detecting a case where the data is changed, there's an error,
4652 * and it's not logged so we don't shutdown when we should.
4653 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004654 if (bma.logflags)
4655 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004656
Dave Chinner29c8d172011-09-18 20:41:00 +00004657 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 if (!error) {
4659 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004660 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004662 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004663 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004665 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4667 }
4668 if (!error)
4669 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4670 orig_nmap, *nmap);
4671 return error;
4672}
4673
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004674static int
4675xfs_bmapi_remap(
4676 struct xfs_trans *tp,
4677 struct xfs_inode *ip,
4678 xfs_fileoff_t bno,
4679 xfs_filblks_t len,
4680 xfs_fsblock_t startblock,
4681 struct xfs_defer_ops *dfops)
4682{
4683 struct xfs_mount *mp = ip->i_mount;
4684 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4685 struct xfs_btree_cur *cur = NULL;
4686 xfs_fsblock_t firstblock = NULLFSBLOCK;
4687 struct xfs_bmbt_irec got;
4688 xfs_extnum_t idx;
4689 int logflags = 0, error;
4690
4691 ASSERT(len > 0);
4692 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4693 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4694
4695 if (unlikely(XFS_TEST_ERROR(
4696 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4697 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4698 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4699 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4700 return -EFSCORRUPTED;
4701 }
4702
4703 if (XFS_FORCED_SHUTDOWN(mp))
4704 return -EIO;
4705
4706 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4707 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4708 if (error)
4709 return error;
4710 }
4711
4712 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4713 /* make sure we only reflink into a hole. */
4714 ASSERT(got.br_startoff > bno);
4715 ASSERT(got.br_startoff - bno >= len);
4716 }
4717
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004718 ip->i_d.di_nblocks += len;
4719 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004720
4721 if (ifp->if_flags & XFS_IFBROOT) {
4722 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4723 cur->bc_private.b.firstblock = firstblock;
4724 cur->bc_private.b.dfops = dfops;
4725 cur->bc_private.b.flags = 0;
4726 }
4727
4728 got.br_startoff = bno;
4729 got.br_startblock = startblock;
4730 got.br_blockcount = len;
4731 got.br_state = XFS_EXT_NORM;
4732
4733 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4734 &got, &firstblock, dfops, &logflags);
4735 if (error)
4736 goto error0;
4737
4738 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4739 int tmp_logflags = 0;
4740
4741 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4742 &tmp_logflags, XFS_DATA_FORK);
4743 logflags |= tmp_logflags;
4744 }
4745
4746error0:
4747 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4748 logflags &= ~XFS_ILOG_DEXT;
4749 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4750 logflags &= ~XFS_ILOG_DBROOT;
4751
4752 if (logflags)
4753 xfs_trans_log_inode(tp, ip, logflags);
4754 if (cur) {
4755 xfs_btree_del_cursor(cur,
4756 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4757 }
4758 return error;
4759}
4760
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004762 * When a delalloc extent is split (e.g., due to a hole punch), the original
4763 * indlen reservation must be shared across the two new extents that are left
4764 * behind.
4765 *
4766 * Given the original reservation and the worst case indlen for the two new
4767 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004768 * reservation fairly across the two new extents. If necessary, steal available
4769 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4770 * ores == 1). The number of stolen blocks is returned. The availability and
4771 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004772 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004773static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004774xfs_bmap_split_indlen(
4775 xfs_filblks_t ores, /* original res. */
4776 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004777 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4778 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004779{
4780 xfs_filblks_t len1 = *indlen1;
4781 xfs_filblks_t len2 = *indlen2;
4782 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004783 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004784 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004785
4786 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004787 * Steal as many blocks as we can to try and satisfy the worst case
4788 * indlen for both new extents.
4789 */
Brian Foster75d65362017-02-13 22:48:30 -08004790 if (ores < nres && avail)
4791 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4792 ores += stolen;
4793
4794 /* nothing else to do if we've satisfied the new reservation */
4795 if (ores >= nres)
4796 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004797
4798 /*
Brian Foster75d65362017-02-13 22:48:30 -08004799 * We can't meet the total required reservation for the two extents.
4800 * Calculate the percent of the overall shortage between both extents
4801 * and apply this percentage to each of the requested indlen values.
4802 * This distributes the shortage fairly and reduces the chances that one
4803 * of the two extents is left with nothing when extents are repeatedly
4804 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004805 */
Brian Foster75d65362017-02-13 22:48:30 -08004806 resfactor = (ores * 100);
4807 do_div(resfactor, nres);
4808 len1 *= resfactor;
4809 do_div(len1, 100);
4810 len2 *= resfactor;
4811 do_div(len2, 100);
4812 ASSERT(len1 + len2 <= ores);
4813 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4814
4815 /*
4816 * Hand out the remainder to each extent. If one of the two reservations
4817 * is zero, we want to make sure that one gets a block first. The loop
4818 * below starts with len1, so hand len2 a block right off the bat if it
4819 * is zero.
4820 */
4821 ores -= (len1 + len2);
4822 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4823 if (ores && !len2 && *indlen2) {
4824 len2++;
4825 ores--;
4826 }
4827 while (ores) {
4828 if (len1 < *indlen1) {
4829 len1++;
4830 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004831 }
Brian Foster75d65362017-02-13 22:48:30 -08004832 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004833 break;
Brian Foster75d65362017-02-13 22:48:30 -08004834 if (len2 < *indlen2) {
4835 len2++;
4836 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004837 }
4838 }
4839
4840 *indlen1 = len1;
4841 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004842
4843 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004844}
4845
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004846int
4847xfs_bmap_del_extent_delay(
4848 struct xfs_inode *ip,
4849 int whichfork,
4850 xfs_extnum_t *idx,
4851 struct xfs_bmbt_irec *got,
4852 struct xfs_bmbt_irec *del)
4853{
4854 struct xfs_mount *mp = ip->i_mount;
4855 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4856 struct xfs_bmbt_irec new;
4857 int64_t da_old, da_new, da_diff = 0;
4858 xfs_fileoff_t del_endoff, got_endoff;
4859 xfs_filblks_t got_indlen, new_indlen, stolen;
4860 int error = 0, state = 0;
4861 bool isrt;
4862
4863 XFS_STATS_INC(mp, xs_del_exlist);
4864
4865 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4866 del_endoff = del->br_startoff + del->br_blockcount;
4867 got_endoff = got->br_startoff + got->br_blockcount;
4868 da_old = startblockval(got->br_startblock);
4869 da_new = 0;
4870
4871 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004872 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004873 ASSERT(del->br_blockcount > 0);
4874 ASSERT(got->br_startoff <= del->br_startoff);
4875 ASSERT(got_endoff >= del_endoff);
4876
4877 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004878 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004879
4880 do_div(rtexts, mp->m_sb.sb_rextsize);
4881 xfs_mod_frextents(mp, rtexts);
4882 }
4883
4884 /*
4885 * Update the inode delalloc counter now and wait to update the
4886 * sb counters as we might have to borrow some blocks for the
4887 * indirect block accounting.
4888 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004889 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4890 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004891 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004892 if (error)
4893 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004894 ip->i_delayed_blks -= del->br_blockcount;
4895
4896 if (whichfork == XFS_COW_FORK)
4897 state |= BMAP_COWFORK;
4898
4899 if (got->br_startoff == del->br_startoff)
4900 state |= BMAP_LEFT_CONTIG;
4901 if (got_endoff == del_endoff)
4902 state |= BMAP_RIGHT_CONTIG;
4903
4904 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4905 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4906 /*
4907 * Matches the whole extent. Delete the entry.
4908 */
4909 xfs_iext_remove(ip, *idx, 1, state);
4910 --*idx;
4911 break;
4912 case BMAP_LEFT_CONTIG:
4913 /*
4914 * Deleting the first part of the extent.
4915 */
4916 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4917 got->br_startoff = del_endoff;
4918 got->br_blockcount -= del->br_blockcount;
4919 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4920 got->br_blockcount), da_old);
4921 got->br_startblock = nullstartblock((int)da_new);
4922 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4923 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4924 break;
4925 case BMAP_RIGHT_CONTIG:
4926 /*
4927 * Deleting the last part of the extent.
4928 */
4929 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4930 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4931 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4932 got->br_blockcount), da_old);
4933 got->br_startblock = nullstartblock((int)da_new);
4934 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4935 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4936 break;
4937 case 0:
4938 /*
4939 * Deleting the middle of the extent.
4940 *
4941 * Distribute the original indlen reservation across the two new
4942 * extents. Steal blocks from the deleted extent if necessary.
4943 * Stealing blocks simply fudges the fdblocks accounting below.
4944 * Warn if either of the new indlen reservations is zero as this
4945 * can lead to delalloc problems.
4946 */
4947 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4948
4949 got->br_blockcount = del->br_startoff - got->br_startoff;
4950 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4951
4952 new.br_blockcount = got_endoff - del_endoff;
4953 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4954
4955 WARN_ON_ONCE(!got_indlen || !new_indlen);
4956 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4957 del->br_blockcount);
4958
4959 got->br_startblock = nullstartblock((int)got_indlen);
4960 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4961 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4962
4963 new.br_startoff = del_endoff;
4964 new.br_state = got->br_state;
4965 new.br_startblock = nullstartblock((int)new_indlen);
4966
4967 ++*idx;
4968 xfs_iext_insert(ip, *idx, 1, &new, state);
4969
4970 da_new = got_indlen + new_indlen - stolen;
4971 del->br_blockcount -= stolen;
4972 break;
4973 }
4974
4975 ASSERT(da_old >= da_new);
4976 da_diff = da_old - da_new;
4977 if (!isrt)
4978 da_diff += del->br_blockcount;
4979 if (da_diff)
4980 xfs_mod_fdblocks(mp, da_diff, false);
4981 return error;
4982}
4983
4984void
4985xfs_bmap_del_extent_cow(
4986 struct xfs_inode *ip,
4987 xfs_extnum_t *idx,
4988 struct xfs_bmbt_irec *got,
4989 struct xfs_bmbt_irec *del)
4990{
4991 struct xfs_mount *mp = ip->i_mount;
4992 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4993 struct xfs_bmbt_irec new;
4994 xfs_fileoff_t del_endoff, got_endoff;
4995 int state = BMAP_COWFORK;
4996
4997 XFS_STATS_INC(mp, xs_del_exlist);
4998
4999 del_endoff = del->br_startoff + del->br_blockcount;
5000 got_endoff = got->br_startoff + got->br_blockcount;
5001
5002 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11005003 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005004 ASSERT(del->br_blockcount > 0);
5005 ASSERT(got->br_startoff <= del->br_startoff);
5006 ASSERT(got_endoff >= del_endoff);
5007 ASSERT(!isnullstartblock(got->br_startblock));
5008
5009 if (got->br_startoff == del->br_startoff)
5010 state |= BMAP_LEFT_CONTIG;
5011 if (got_endoff == del_endoff)
5012 state |= BMAP_RIGHT_CONTIG;
5013
5014 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
5015 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
5016 /*
5017 * Matches the whole extent. Delete the entry.
5018 */
5019 xfs_iext_remove(ip, *idx, 1, state);
5020 --*idx;
5021 break;
5022 case BMAP_LEFT_CONTIG:
5023 /*
5024 * Deleting the first part of the extent.
5025 */
5026 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5027 got->br_startoff = del_endoff;
5028 got->br_blockcount -= del->br_blockcount;
5029 got->br_startblock = del->br_startblock + del->br_blockcount;
5030 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5031 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5032 break;
5033 case BMAP_RIGHT_CONTIG:
5034 /*
5035 * Deleting the last part of the extent.
5036 */
5037 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5038 got->br_blockcount -= del->br_blockcount;
5039 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5040 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5041 break;
5042 case 0:
5043 /*
5044 * Deleting the middle of the extent.
5045 */
5046 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5047 got->br_blockcount = del->br_startoff - got->br_startoff;
5048 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5049 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5050
5051 new.br_startoff = del_endoff;
5052 new.br_blockcount = got_endoff - del_endoff;
5053 new.br_state = got->br_state;
5054 new.br_startblock = del->br_startblock + del->br_blockcount;
5055
5056 ++*idx;
5057 xfs_iext_insert(ip, *idx, 1, &new, state);
5058 break;
5059 }
5060}
5061
Brian Fostera9bd24a2016-03-15 11:42:46 +11005062/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005063 * Called by xfs_bmapi to update file extent records and the btree
5064 * after removing space (or undoing a delayed allocation).
5065 */
5066STATIC int /* error */
5067xfs_bmap_del_extent(
5068 xfs_inode_t *ip, /* incore inode pointer */
5069 xfs_trans_t *tp, /* current transaction pointer */
5070 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005071 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005072 xfs_btree_cur_t *cur, /* if null, not a btree */
5073 xfs_bmbt_irec_t *del, /* data to remove from extents */
5074 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005075 int whichfork, /* data or attr fork */
5076 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005077{
5078 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5079 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5080 xfs_fsblock_t del_endblock=0; /* first block past del */
5081 xfs_fileoff_t del_endoff; /* first offset past del */
5082 int delay; /* current block is delayed allocated */
5083 int do_fx; /* free extent at end of routine */
5084 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5085 int error; /* error return value */
5086 int flags; /* inode logging flags */
5087 xfs_bmbt_irec_t got; /* current extent entry */
5088 xfs_fileoff_t got_endoff; /* first offset past got */
5089 int i; /* temp state */
5090 xfs_ifork_t *ifp; /* inode fork pointer */
5091 xfs_mount_t *mp; /* mount structure */
5092 xfs_filblks_t nblks; /* quota/sb block count */
5093 xfs_bmbt_irec_t new; /* new record to be inserted */
5094 /* REFERENCED */
5095 uint qfield; /* quota field to update */
5096 xfs_filblks_t temp; /* for indirect length calculations */
5097 xfs_filblks_t temp2; /* for indirect length calculations */
5098 int state = 0;
5099
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005100 mp = ip->i_mount;
5101 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005102
5103 if (whichfork == XFS_ATTR_FORK)
5104 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005105 else if (whichfork == XFS_COW_FORK)
5106 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005107
Dave Chinner9e5987a2013-02-25 12:31:26 +11005108 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005109 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005110 ASSERT(del->br_blockcount > 0);
5111 ep = xfs_iext_get_ext(ifp, *idx);
5112 xfs_bmbt_get_all(ep, &got);
5113 ASSERT(got.br_startoff <= del->br_startoff);
5114 del_endoff = del->br_startoff + del->br_blockcount;
5115 got_endoff = got.br_startoff + got.br_blockcount;
5116 ASSERT(got_endoff >= del_endoff);
5117 delay = isnullstartblock(got.br_startblock);
5118 ASSERT(isnullstartblock(del->br_startblock) == delay);
5119 flags = 0;
5120 qfield = 0;
5121 error = 0;
5122 /*
5123 * If deleting a real allocation, must free up the disk space.
5124 */
5125 if (!delay) {
5126 flags = XFS_ILOG_CORE;
5127 /*
5128 * Realtime allocation. Free it and record di_nblocks update.
5129 */
5130 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5131 xfs_fsblock_t bno;
5132 xfs_filblks_t len;
5133
5134 ASSERT(do_mod(del->br_blockcount,
5135 mp->m_sb.sb_rextsize) == 0);
5136 ASSERT(do_mod(del->br_startblock,
5137 mp->m_sb.sb_rextsize) == 0);
5138 bno = del->br_startblock;
5139 len = del->br_blockcount;
5140 do_div(bno, mp->m_sb.sb_rextsize);
5141 do_div(len, mp->m_sb.sb_rextsize);
5142 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5143 if (error)
5144 goto done;
5145 do_fx = 0;
5146 nblks = len * mp->m_sb.sb_rextsize;
5147 qfield = XFS_TRANS_DQ_RTBCOUNT;
5148 }
5149 /*
5150 * Ordinary allocation.
5151 */
5152 else {
5153 do_fx = 1;
5154 nblks = del->br_blockcount;
5155 qfield = XFS_TRANS_DQ_BCOUNT;
5156 }
5157 /*
5158 * Set up del_endblock and cur for later.
5159 */
5160 del_endblock = del->br_startblock + del->br_blockcount;
5161 if (cur) {
5162 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5163 got.br_startblock, got.br_blockcount,
5164 &i)))
5165 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005166 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005167 }
5168 da_old = da_new = 0;
5169 } else {
5170 da_old = startblockval(got.br_startblock);
5171 da_new = 0;
5172 nblks = 0;
5173 do_fx = 0;
5174 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005175
Dave Chinner9e5987a2013-02-25 12:31:26 +11005176 /*
5177 * Set flag value to use in switch statement.
5178 * Left-contig is 2, right-contig is 1.
5179 */
5180 switch (((got.br_startoff == del->br_startoff) << 1) |
5181 (got_endoff == del_endoff)) {
5182 case 3:
5183 /*
5184 * Matches the whole extent. Delete the entry.
5185 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005186 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005187 xfs_iext_remove(ip, *idx, 1,
5188 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5189 --*idx;
5190 if (delay)
5191 break;
5192
5193 XFS_IFORK_NEXT_SET(ip, whichfork,
5194 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5195 flags |= XFS_ILOG_CORE;
5196 if (!cur) {
5197 flags |= xfs_ilog_fext(whichfork);
5198 break;
5199 }
5200 if ((error = xfs_btree_delete(cur, &i)))
5201 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005202 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005203 break;
5204
5205 case 2:
5206 /*
5207 * Deleting the first part of the extent.
5208 */
5209 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5210 xfs_bmbt_set_startoff(ep, del_endoff);
5211 temp = got.br_blockcount - del->br_blockcount;
5212 xfs_bmbt_set_blockcount(ep, temp);
5213 if (delay) {
5214 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5215 da_old);
5216 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5217 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5218 da_new = temp;
5219 break;
5220 }
5221 xfs_bmbt_set_startblock(ep, del_endblock);
5222 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5223 if (!cur) {
5224 flags |= xfs_ilog_fext(whichfork);
5225 break;
5226 }
5227 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5228 got.br_blockcount - del->br_blockcount,
5229 got.br_state)))
5230 goto done;
5231 break;
5232
5233 case 1:
5234 /*
5235 * Deleting the last part of the extent.
5236 */
5237 temp = got.br_blockcount - del->br_blockcount;
5238 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5239 xfs_bmbt_set_blockcount(ep, temp);
5240 if (delay) {
5241 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5242 da_old);
5243 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5244 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5245 da_new = temp;
5246 break;
5247 }
5248 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5249 if (!cur) {
5250 flags |= xfs_ilog_fext(whichfork);
5251 break;
5252 }
5253 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5254 got.br_startblock,
5255 got.br_blockcount - del->br_blockcount,
5256 got.br_state)))
5257 goto done;
5258 break;
5259
5260 case 0:
5261 /*
5262 * Deleting the middle of the extent.
5263 */
5264 temp = del->br_startoff - got.br_startoff;
5265 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5266 xfs_bmbt_set_blockcount(ep, temp);
5267 new.br_startoff = del_endoff;
5268 temp2 = got_endoff - del_endoff;
5269 new.br_blockcount = temp2;
5270 new.br_state = got.br_state;
5271 if (!delay) {
5272 new.br_startblock = del_endblock;
5273 flags |= XFS_ILOG_CORE;
5274 if (cur) {
5275 if ((error = xfs_bmbt_update(cur,
5276 got.br_startoff,
5277 got.br_startblock, temp,
5278 got.br_state)))
5279 goto done;
5280 if ((error = xfs_btree_increment(cur, 0, &i)))
5281 goto done;
5282 cur->bc_rec.b = new;
5283 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10005284 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005285 goto done;
5286 /*
5287 * If get no-space back from btree insert,
5288 * it tried a split, and we have a zero
5289 * block reservation.
5290 * Fix up our state and return the error.
5291 */
Dave Chinner24513372014-06-25 14:58:08 +10005292 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11005293 /*
5294 * Reset the cursor, don't trust
5295 * it after any insert operation.
5296 */
5297 if ((error = xfs_bmbt_lookup_eq(cur,
5298 got.br_startoff,
5299 got.br_startblock,
5300 temp, &i)))
5301 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005302 XFS_WANT_CORRUPTED_GOTO(mp,
5303 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005304 /*
5305 * Update the btree record back
5306 * to the original value.
5307 */
5308 if ((error = xfs_bmbt_update(cur,
5309 got.br_startoff,
5310 got.br_startblock,
5311 got.br_blockcount,
5312 got.br_state)))
5313 goto done;
5314 /*
5315 * Reset the extent record back
5316 * to the original value.
5317 */
5318 xfs_bmbt_set_blockcount(ep,
5319 got.br_blockcount);
5320 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005321 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005322 goto done;
5323 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005324 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005325 } else
5326 flags |= xfs_ilog_fext(whichfork);
5327 XFS_IFORK_NEXT_SET(ip, whichfork,
5328 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5329 } else {
Brian Fosterd34999c92016-03-15 11:42:47 +11005330 xfs_filblks_t stolen;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005331 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005332
5333 /*
5334 * Distribute the original indlen reservation across the
Brian Fosterd34999c92016-03-15 11:42:47 +11005335 * two new extents. Steal blocks from the deleted extent
5336 * if necessary. Stealing blocks simply fudges the
5337 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005338 */
5339 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5340 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c92016-03-15 11:42:47 +11005341 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5342 del->br_blockcount);
5343 da_new = temp + temp2 - stolen;
5344 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005345
5346 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11005347 * Set the reservation for each extent. Warn if either
5348 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005349 */
Brian Fosterd34999c92016-03-15 11:42:47 +11005350 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005351 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005352 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005353 }
5354 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5355 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5356 ++*idx;
5357 break;
5358 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005359
5360 /* remove reverse mapping */
5361 if (!delay) {
5362 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5363 if (error)
5364 goto done;
5365 }
5366
Dave Chinner9e5987a2013-02-25 12:31:26 +11005367 /*
5368 * If we need to, add to list of extents to delete.
5369 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005370 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005371 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5372 error = xfs_refcount_decrease_extent(mp, dfops, del);
5373 if (error)
5374 goto done;
5375 } else
5376 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5377 del->br_blockcount, NULL);
5378 }
5379
Dave Chinner9e5987a2013-02-25 12:31:26 +11005380 /*
5381 * Adjust inode # blocks in the file.
5382 */
5383 if (nblks)
5384 ip->i_d.di_nblocks -= nblks;
5385 /*
5386 * Adjust quota data.
5387 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005388 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005389 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5390
5391 /*
5392 * Account for change in delayed indirect blocks.
5393 * Nothing to do for disk quota accounting here.
5394 */
5395 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005396 if (da_old > da_new)
5397 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005398done:
5399 *logflagsp = flags;
5400 return error;
5401}
5402
5403/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 * Unmap (remove) blocks from a file.
5405 * If nexts is nonzero then the number of extents to remove is limited to
5406 * that value. If not all extents in the block range can be removed then
5407 * *done is set.
5408 */
5409int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005410__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 xfs_trans_t *tp, /* transaction pointer */
5412 struct xfs_inode *ip, /* incore inode */
5413 xfs_fileoff_t bno, /* starting offset to unmap */
Darrick J. Wong44535932016-10-03 09:11:29 -07005414 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 int flags, /* misc flags */
5416 xfs_extnum_t nexts, /* number of extents max */
5417 xfs_fsblock_t *firstblock, /* first allocated block
5418 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005419 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420{
5421 xfs_btree_cur_t *cur; /* bmap btree cursor */
5422 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 int error; /* error return value */
5424 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005425 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 xfs_ifork_t *ifp; /* inode fork pointer */
5427 int isrt; /* freeing in rt area */
5428 xfs_extnum_t lastx; /* last extent index used */
5429 int logflags; /* transaction logging flags */
5430 xfs_extlen_t mod; /* rt extent offset */
5431 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 xfs_fileoff_t start; /* first file offset deleted */
5433 int tmp_logflags; /* partial logging flags */
5434 int wasdel; /* was a delayed alloc extent */
5435 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005437 xfs_filblks_t len = *rlen; /* length to unmap in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005439 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5440
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005441 whichfork = xfs_bmapi_whichfork(flags);
5442 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 ifp = XFS_IFORK_PTR(ip, whichfork);
5444 if (unlikely(
5445 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5446 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5447 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5448 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005449 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 }
5451 mp = ip->i_mount;
5452 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005453 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005454
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005455 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 ASSERT(len > 0);
5457 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005458
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5460 (error = xfs_iread_extents(tp, ip, whichfork)))
5461 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005462 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005463 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 return 0;
5465 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005466 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005467 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 start = bno;
5469 bno = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005470
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 /*
5472 * Check to see if the given block number is past the end of the
5473 * file, back up to the last block if so...
5474 */
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005475 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5476 ASSERT(lastx > 0);
5477 xfs_iext_get_extent(ifp, --lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 bno = got.br_startoff + got.br_blockcount - 1;
5479 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005480
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 logflags = 0;
5482 if (ifp->if_flags & XFS_IFBROOT) {
5483 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005484 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005486 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 cur->bc_private.b.flags = 0;
5488 } else
5489 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005490
5491 if (isrt) {
5492 /*
5493 * Synchronize by locking the bitmap inode.
5494 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005495 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005496 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005497 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5498 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005499 }
5500
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 extno = 0;
5502 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5503 (nexts == 0 || extno < nexts)) {
5504 /*
5505 * Is the found extent after a hole in which bno lives?
5506 * Just back up to the previous extent, if so.
5507 */
5508 if (got.br_startoff > bno) {
5509 if (--lastx < 0)
5510 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005511 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 }
5513 /*
5514 * Is the last block of this extent before the range
5515 * we're supposed to delete? If so, we're done.
5516 */
5517 bno = XFS_FILEOFF_MIN(bno,
5518 got.br_startoff + got.br_blockcount - 1);
5519 if (bno < start)
5520 break;
5521 /*
5522 * Then deal with the (possibly delayed) allocated space
5523 * we found.
5524 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005526 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 if (got.br_startoff < start) {
5528 del.br_startoff = start;
5529 del.br_blockcount -= start - got.br_startoff;
5530 if (!wasdel)
5531 del.br_startblock += start - got.br_startoff;
5532 }
5533 if (del.br_startoff + del.br_blockcount > bno + 1)
5534 del.br_blockcount = bno + 1 - del.br_startoff;
5535 sum = del.br_startblock + del.br_blockcount;
5536 if (isrt &&
5537 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5538 /*
5539 * Realtime extent not lined up at the end.
5540 * The extent could have been split into written
5541 * and unwritten pieces, or we could just be
5542 * unmapping part of it. But we can't really
5543 * get rid of part of a realtime extent.
5544 */
5545 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005546 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 /*
5548 * This piece is unwritten, or we're not
5549 * using unwritten extents. Skip over it.
5550 */
5551 ASSERT(bno >= mod);
5552 bno -= mod > del.br_blockcount ?
5553 del.br_blockcount : mod;
5554 if (bno < got.br_startoff) {
5555 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005556 xfs_bmbt_get_all(xfs_iext_get_ext(
5557 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 }
5559 continue;
5560 }
5561 /*
5562 * It's written, turn it unwritten.
5563 * This is better than zeroing it.
5564 */
5565 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005566 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 /*
5568 * If this spans a realtime extent boundary,
5569 * chop it back to the start of the one we end at.
5570 */
5571 if (del.br_blockcount > mod) {
5572 del.br_startoff += del.br_blockcount - mod;
5573 del.br_startblock += del.br_blockcount - mod;
5574 del.br_blockcount = mod;
5575 }
5576 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005577 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005578 whichfork, &lastx, &cur, &del,
5579 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 if (error)
5581 goto error0;
5582 goto nodelete;
5583 }
5584 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5585 /*
5586 * Realtime extent is lined up at the end but not
5587 * at the front. We'll get rid of full extents if
5588 * we can.
5589 */
5590 mod = mp->m_sb.sb_rextsize - mod;
5591 if (del.br_blockcount > mod) {
5592 del.br_blockcount -= mod;
5593 del.br_startoff += mod;
5594 del.br_startblock += mod;
5595 } else if ((del.br_startoff == start &&
5596 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005597 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005598 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 /*
5600 * Can't make it unwritten. There isn't
5601 * a full extent here so just skip it.
5602 */
5603 ASSERT(bno >= del.br_blockcount);
5604 bno -= del.br_blockcount;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005605 if (got.br_startoff > bno && --lastx >= 0)
5606 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 continue;
5608 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005609 struct xfs_bmbt_irec prev;
5610
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 /*
5612 * This one is already unwritten.
5613 * It must have a written left neighbor.
5614 * Unwrite the killed part of that one and
5615 * try again.
5616 */
5617 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005618 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005620 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 ASSERT(del.br_startblock ==
5622 prev.br_startblock + prev.br_blockcount);
5623 if (prev.br_startoff < start) {
5624 mod = start - prev.br_startoff;
5625 prev.br_blockcount -= mod;
5626 prev.br_startblock += mod;
5627 prev.br_startoff = start;
5628 }
5629 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005630 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005631 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005632 ip, whichfork, &lastx, &cur,
5633 &prev, firstblock, dfops,
5634 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 if (error)
5636 goto error0;
5637 goto nodelete;
5638 } else {
5639 ASSERT(del.br_state == XFS_EXT_NORM);
5640 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005641 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005642 ip, whichfork, &lastx, &cur,
5643 &del, firstblock, dfops,
5644 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 if (error)
5646 goto error0;
5647 goto nodelete;
5648 }
5649 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005650
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 /*
5652 * If it's the case where the directory code is running
5653 * with no block reservation, and the deleted block is in
5654 * the middle of its extent, and the resulting insert
5655 * of an extent would cause transformation to btree format,
5656 * then reject it. The calling code will then swap
5657 * blocks around instead.
5658 * We have to do this now, rather than waiting for the
5659 * conversion to btree format, since the transaction
5660 * will be dirty.
5661 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005662 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005664 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5665 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 del.br_startoff > got.br_startoff &&
5667 del.br_startoff + del.br_blockcount <
5668 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005669 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 goto error0;
5671 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005672
5673 /*
5674 * Unreserve quota and update realtime free space, if
5675 * appropriate. If delayed allocation, update the inode delalloc
5676 * counter now and wait to update the sb counters as
5677 * xfs_bmap_del_extent() might need to borrow some blocks.
5678 */
5679 if (wasdel) {
5680 ASSERT(startblockval(del.br_startblock) > 0);
5681 if (isrt) {
5682 xfs_filblks_t rtexts;
5683
5684 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5685 do_div(rtexts, mp->m_sb.sb_rextsize);
5686 xfs_mod_frextents(mp, (int64_t)rtexts);
5687 (void)xfs_trans_reserve_quota_nblks(NULL,
5688 ip, -((long)del.br_blockcount), 0,
5689 XFS_QMOPT_RES_RTBLKS);
5690 } else {
5691 (void)xfs_trans_reserve_quota_nblks(NULL,
5692 ip, -((long)del.br_blockcount), 0,
5693 XFS_QMOPT_RES_REGBLKS);
5694 }
5695 ip->i_delayed_blks -= del.br_blockcount;
5696 if (cur)
5697 cur->bc_private.b.flags |=
5698 XFS_BTCUR_BPRV_WASDEL;
5699 } else if (cur)
5700 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5701
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005702 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005703 &tmp_logflags, whichfork, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 logflags |= tmp_logflags;
5705 if (error)
5706 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005707
5708 if (!isrt && wasdel)
5709 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5710
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 bno = del.br_startoff - 1;
5712nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 /*
5714 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005717 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005718 xfs_iext_get_extent(ifp, lastx, &got);
5719 if (got.br_startoff > bno && --lastx >= 0)
5720 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 extno++;
5723 }
5724 }
Darrick J. Wong44535932016-10-03 09:11:29 -07005725 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5726 *rlen = 0;
5727 else
5728 *rlen = bno - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005729
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 /*
5731 * Convert to a btree if necessary.
5732 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005733 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005735 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 &cur, 0, &tmp_logflags, whichfork);
5737 logflags |= tmp_logflags;
5738 if (error)
5739 goto error0;
5740 }
5741 /*
5742 * transform from btree to extents, give it cur
5743 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005744 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 ASSERT(cur != NULL);
5746 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5747 whichfork);
5748 logflags |= tmp_logflags;
5749 if (error)
5750 goto error0;
5751 }
5752 /*
5753 * transform from extents to local?
5754 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 error = 0;
5756error0:
5757 /*
5758 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005759 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005761 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005763 logflags &= ~xfs_ilog_fext(whichfork);
5764 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005766 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 /*
5768 * Log inode even in the error case, if the transaction
5769 * is dirty we'll need to shut down the filesystem.
5770 */
5771 if (logflags)
5772 xfs_trans_log_inode(tp, ip, logflags);
5773 if (cur) {
5774 if (!error) {
5775 *firstblock = cur->bc_private.b.firstblock;
5776 cur->bc_private.b.allocated = 0;
5777 }
5778 xfs_btree_del_cursor(cur,
5779 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5780 }
5781 return error;
5782}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005783
Darrick J. Wong44535932016-10-03 09:11:29 -07005784/* Unmap a range of a file. */
5785int
5786xfs_bunmapi(
5787 xfs_trans_t *tp,
5788 struct xfs_inode *ip,
5789 xfs_fileoff_t bno,
5790 xfs_filblks_t len,
5791 int flags,
5792 xfs_extnum_t nexts,
5793 xfs_fsblock_t *firstblock,
5794 struct xfs_defer_ops *dfops,
5795 int *done)
5796{
5797 int error;
5798
5799 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5800 dfops);
5801 *done = (len == 0);
5802 return error;
5803}
5804
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005805/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005806 * Determine whether an extent shift can be accomplished by a merge with the
5807 * extent that precedes the target hole of the shift.
5808 */
5809STATIC bool
5810xfs_bmse_can_merge(
5811 struct xfs_bmbt_irec *left, /* preceding extent */
5812 struct xfs_bmbt_irec *got, /* current extent to shift */
5813 xfs_fileoff_t shift) /* shift fsb */
5814{
5815 xfs_fileoff_t startoff;
5816
5817 startoff = got->br_startoff - shift;
5818
5819 /*
5820 * The extent, once shifted, must be adjacent in-file and on-disk with
5821 * the preceding extent.
5822 */
5823 if ((left->br_startoff + left->br_blockcount != startoff) ||
5824 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5825 (left->br_state != got->br_state) ||
5826 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5827 return false;
5828
5829 return true;
5830}
5831
5832/*
5833 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5834 * hole in the file. If an extent shift would result in the extent being fully
5835 * adjacent to the extent that currently precedes the hole, we can merge with
5836 * the preceding extent rather than do the shift.
5837 *
5838 * This function assumes the caller has verified a shift-by-merge is possible
5839 * with the provided extents via xfs_bmse_can_merge().
5840 */
5841STATIC int
5842xfs_bmse_merge(
5843 struct xfs_inode *ip,
5844 int whichfork,
5845 xfs_fileoff_t shift, /* shift fsb */
5846 int current_ext, /* idx of gotp */
5847 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5848 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5849 struct xfs_btree_cur *cur,
5850 int *logflags) /* output */
5851{
Brian Fosterddb19e32014-09-23 15:38:09 +10005852 struct xfs_bmbt_irec got;
5853 struct xfs_bmbt_irec left;
5854 xfs_filblks_t blockcount;
5855 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005856 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005857
Brian Fosterddb19e32014-09-23 15:38:09 +10005858 xfs_bmbt_get_all(gotp, &got);
5859 xfs_bmbt_get_all(leftp, &left);
5860 blockcount = left.br_blockcount + got.br_blockcount;
5861
5862 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5863 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5864 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5865
5866 /*
5867 * Merge the in-core extents. Note that the host record pointers and
5868 * current_ext index are invalid once the extent has been removed via
5869 * xfs_iext_remove().
5870 */
5871 xfs_bmbt_set_blockcount(leftp, blockcount);
5872 xfs_iext_remove(ip, current_ext, 1, 0);
5873
5874 /*
5875 * Update the on-disk extent count, the btree if necessary and log the
5876 * inode.
5877 */
5878 XFS_IFORK_NEXT_SET(ip, whichfork,
5879 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5880 *logflags |= XFS_ILOG_CORE;
5881 if (!cur) {
5882 *logflags |= XFS_ILOG_DEXT;
5883 return 0;
5884 }
5885
5886 /* lookup and remove the extent to merge */
5887 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5888 got.br_blockcount, &i);
5889 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005890 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005891 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005892
5893 error = xfs_btree_delete(cur, &i);
5894 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005895 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005896 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005897
5898 /* lookup and update size of the previous extent */
5899 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5900 left.br_blockcount, &i);
5901 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005902 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005903 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005904
5905 left.br_blockcount = blockcount;
5906
Dave Chinner4db431f2014-12-04 09:42:40 +11005907 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5908 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005909}
5910
5911/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005912 * Shift a single extent.
5913 */
5914STATIC int
5915xfs_bmse_shift_one(
5916 struct xfs_inode *ip,
5917 int whichfork,
5918 xfs_fileoff_t offset_shift_fsb,
5919 int *current_ext,
5920 struct xfs_bmbt_rec_host *gotp,
5921 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005922 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005923 enum shift_direction direction,
5924 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005925{
5926 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005927 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005928 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005929 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005930 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005931 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005932 int error;
5933 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005934 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005935
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005936 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005937 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005938 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005939
5940 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005941
Brian Fosterf71721d2014-09-23 15:39:05 +10005942 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005943 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005944
Namjae Jeona904b1c2015-03-25 15:08:56 +11005945 if (direction == SHIFT_LEFT) {
5946 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005947
Namjae Jeona904b1c2015-03-25 15:08:56 +11005948 /*
5949 * Check for merge if we've got an extent to the left,
5950 * otherwise make sure there's enough room at the start
5951 * of the file for the shift.
5952 */
5953 if (!*current_ext) {
5954 if (got.br_startoff < offset_shift_fsb)
5955 return -EINVAL;
5956 goto update_current_ext;
5957 }
5958 /*
5959 * grab the left extent and check for a large
5960 * enough hole.
5961 */
5962 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5963 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5964
5965 if (startoff <
5966 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005967 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005968
Dave Chinnerb11bd672014-12-04 09:42:24 +11005969 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005970 if (xfs_bmse_can_merge(&adj_irec, &got,
5971 offset_shift_fsb)) {
Darrick J. Wong9c194642016-08-03 12:16:05 +10005972 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5973 *current_ext, gotp, adj_irecp,
5974 cur, logflags);
5975 if (error)
5976 return error;
5977 adj_irec = got;
5978 goto update_rmap;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005979 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005980 } else {
5981 startoff = got.br_startoff + offset_shift_fsb;
5982 /* nothing to move if this is the last extent */
5983 if (*current_ext >= (total_extents - 1))
5984 goto update_current_ext;
5985 /*
5986 * If this is not the last extent in the file, make sure there
5987 * is enough room between current extent and next extent for
5988 * accommodating the shift.
5989 */
5990 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5991 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5992 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5993 return -EINVAL;
5994 /*
5995 * Unlike a left shift (which involves a hole punch),
5996 * a right shift does not modify extent neighbors
5997 * in any way. We should never find mergeable extents
5998 * in this scenario. Check anyways and warn if we
5999 * encounter two extents that could be one.
6000 */
6001 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
6002 WARN_ON_ONCE(1);
6003 }
Brian Fostera979bdf2014-09-23 15:39:04 +10006004 /*
6005 * Increment the extent index for the next iteration, update the start
6006 * offset of the in-core extent and update the btree if applicable.
6007 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11006008update_current_ext:
6009 if (direction == SHIFT_LEFT)
6010 (*current_ext)++;
6011 else
6012 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10006013 xfs_bmbt_set_startoff(gotp, startoff);
6014 *logflags |= XFS_ILOG_CORE;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006015 adj_irec = got;
Brian Fostera979bdf2014-09-23 15:39:04 +10006016 if (!cur) {
6017 *logflags |= XFS_ILOG_DEXT;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006018 goto update_rmap;
Brian Fostera979bdf2014-09-23 15:39:04 +10006019 }
6020
6021 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
6022 got.br_blockcount, &i);
6023 if (error)
6024 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11006025 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10006026
6027 got.br_startoff = startoff;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006028 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
6029 got.br_blockcount, got.br_state);
6030 if (error)
6031 return error;
6032
6033update_rmap:
6034 /* update reverse mapping */
6035 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
6036 if (error)
6037 return error;
6038 adj_irec.br_startoff = startoff;
6039 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
Brian Fostera979bdf2014-09-23 15:39:04 +10006040}
6041
6042/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11006043 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006044 *
Brian Foster2c845f52014-09-23 15:37:09 +10006045 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11006046 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10006047 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6048 * is the length by which each extent is shifted. If there is no hole to shift
6049 * the extents into, this will be considered invalid operation and we abort
6050 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006051 */
6052int
6053xfs_bmap_shift_extents(
6054 struct xfs_trans *tp,
6055 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006056 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006057 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10006058 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006059 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006060 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006061 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006062 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006063 int num_exts)
6064{
Brian Fosterca446d82014-09-02 12:12:53 +10006065 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006066 struct xfs_bmbt_rec_host *gotp;
6067 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006068 struct xfs_mount *mp = ip->i_mount;
6069 struct xfs_ifork *ifp;
6070 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10006071 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006072 xfs_extnum_t total_extents;
6073 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006074 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006075 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10006076 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006077
6078 if (unlikely(XFS_TEST_ERROR(
6079 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6080 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6081 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6082 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6083 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10006084 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006085 }
6086
6087 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10006088 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006089
Brian Foster2c845f52014-09-23 15:37:09 +10006090 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6091 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006092 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6093 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006094
6095 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006096 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6097 /* Read in all the extents */
6098 error = xfs_iread_extents(tp, ip, whichfork);
6099 if (error)
6100 return error;
6101 }
6102
Brian Fosterddb19e32014-09-23 15:38:09 +10006103 if (ifp->if_flags & XFS_IFBROOT) {
6104 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6105 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006106 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006107 cur->bc_private.b.flags = 0;
6108 }
6109
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006110 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006111 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006112 * are collapsing out, so we cannot use the count of real extents here.
6113 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006114 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006115 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006116 if (total_extents == 0) {
6117 *done = 1;
6118 goto del_cursor;
6119 }
6120
6121 /*
6122 * In case of first right shift, we need to initialize next_fsb
6123 */
6124 if (*next_fsb == NULLFSBLOCK) {
6125 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6126 xfs_bmbt_get_all(gotp, &got);
6127 *next_fsb = got.br_startoff;
6128 if (stop_fsb > *next_fsb) {
6129 *done = 1;
6130 goto del_cursor;
6131 }
6132 }
6133
6134 /* Lookup the extent index at which we have to stop */
6135 if (direction == SHIFT_RIGHT) {
6136 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6137 /* Make stop_extent exclusive of shift range */
6138 stop_extent--;
6139 } else
6140 stop_extent = total_extents;
6141
6142 /*
6143 * Look up the extent index for the fsb where we start shifting. We can
6144 * henceforth iterate with current_ext as extent list changes are locked
6145 * out via ilock.
6146 *
6147 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6148 * *next_fsb lies in a hole beyond which there are no extents. Either
6149 * way, we are done.
6150 */
6151 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
6152 if (!gotp) {
6153 *done = 1;
6154 goto del_cursor;
6155 }
6156
6157 /* some sanity checking before we finally start shifting extents */
6158 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6159 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6160 error = -EIO;
6161 goto del_cursor;
6162 }
6163
6164 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006165 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006166 &current_ext, gotp, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006167 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006168 if (error)
6169 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006170 /*
6171 * If there was an extent merge during the shift, the extent
6172 * count can change. Update the total and grade the next record.
6173 */
6174 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006175 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006176 stop_extent = total_extents;
6177 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006178
Namjae Jeona904b1c2015-03-25 15:08:56 +11006179 if (current_ext == stop_extent) {
6180 *done = 1;
6181 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006182 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006183 }
Brian Fosterddb19e32014-09-23 15:38:09 +10006184 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006185 }
6186
Namjae Jeona904b1c2015-03-25 15:08:56 +11006187 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006188 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10006189 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10006190 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006191
6192del_cursor:
6193 if (cur)
6194 xfs_btree_del_cursor(cur,
6195 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6196
Brian Fosterca446d82014-09-02 12:12:53 +10006197 if (logflags)
6198 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006199
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006200 return error;
6201}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006202
6203/*
6204 * Splits an extent into two extents at split_fsb block such that it is
6205 * the first block of the current_ext. @current_ext is a target extent
6206 * to be split. @split_fsb is a block where the extents is split.
6207 * If split_fsb lies in a hole or the first block of extents, just return 0.
6208 */
6209STATIC int
6210xfs_bmap_split_extent_at(
6211 struct xfs_trans *tp,
6212 struct xfs_inode *ip,
6213 xfs_fileoff_t split_fsb,
6214 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006215 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006216{
6217 int whichfork = XFS_DATA_FORK;
6218 struct xfs_btree_cur *cur = NULL;
6219 struct xfs_bmbt_rec_host *gotp;
6220 struct xfs_bmbt_irec got;
6221 struct xfs_bmbt_irec new; /* split extent */
6222 struct xfs_mount *mp = ip->i_mount;
6223 struct xfs_ifork *ifp;
6224 xfs_fsblock_t gotblkcnt; /* new block count for got */
6225 xfs_extnum_t current_ext;
6226 int error = 0;
6227 int logflags = 0;
6228 int i = 0;
6229
6230 if (unlikely(XFS_TEST_ERROR(
6231 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6232 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6233 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6234 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6235 XFS_ERRLEVEL_LOW, mp);
6236 return -EFSCORRUPTED;
6237 }
6238
6239 if (XFS_FORCED_SHUTDOWN(mp))
6240 return -EIO;
6241
6242 ifp = XFS_IFORK_PTR(ip, whichfork);
6243 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6244 /* Read in all the extents */
6245 error = xfs_iread_extents(tp, ip, whichfork);
6246 if (error)
6247 return error;
6248 }
6249
6250 /*
6251 * gotp can be null in 2 cases: 1) if there are no extents
6252 * or 2) split_fsb lies in a hole beyond which there are
6253 * no extents. Either way, we are done.
6254 */
6255 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
6256 if (!gotp)
6257 return 0;
6258
6259 xfs_bmbt_get_all(gotp, &got);
6260
6261 /*
6262 * Check split_fsb lies in a hole or the start boundary offset
6263 * of the extent.
6264 */
6265 if (got.br_startoff >= split_fsb)
6266 return 0;
6267
6268 gotblkcnt = split_fsb - got.br_startoff;
6269 new.br_startoff = split_fsb;
6270 new.br_startblock = got.br_startblock + gotblkcnt;
6271 new.br_blockcount = got.br_blockcount - gotblkcnt;
6272 new.br_state = got.br_state;
6273
6274 if (ifp->if_flags & XFS_IFBROOT) {
6275 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6276 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006277 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006278 cur->bc_private.b.flags = 0;
6279 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6280 got.br_startblock,
6281 got.br_blockcount,
6282 &i);
6283 if (error)
6284 goto del_cursor;
6285 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6286 }
6287
6288 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6289 got.br_blockcount = gotblkcnt;
6290
6291 logflags = XFS_ILOG_CORE;
6292 if (cur) {
6293 error = xfs_bmbt_update(cur, got.br_startoff,
6294 got.br_startblock,
6295 got.br_blockcount,
6296 got.br_state);
6297 if (error)
6298 goto del_cursor;
6299 } else
6300 logflags |= XFS_ILOG_DEXT;
6301
6302 /* Add new extent */
6303 current_ext++;
6304 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6305 XFS_IFORK_NEXT_SET(ip, whichfork,
6306 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6307
6308 if (cur) {
6309 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6310 new.br_startblock, new.br_blockcount,
6311 &i);
6312 if (error)
6313 goto del_cursor;
6314 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6315 cur->bc_rec.b.br_state = new.br_state;
6316
6317 error = xfs_btree_insert(cur, &i);
6318 if (error)
6319 goto del_cursor;
6320 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6321 }
6322
6323 /*
6324 * Convert to a btree if necessary.
6325 */
6326 if (xfs_bmap_needs_btree(ip, whichfork)) {
6327 int tmp_logflags; /* partial log flag return val */
6328
6329 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006330 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006331 &cur, 0, &tmp_logflags, whichfork);
6332 logflags |= tmp_logflags;
6333 }
6334
6335del_cursor:
6336 if (cur) {
6337 cur->bc_private.b.allocated = 0;
6338 xfs_btree_del_cursor(cur,
6339 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6340 }
6341
6342 if (logflags)
6343 xfs_trans_log_inode(tp, ip, logflags);
6344 return error;
6345}
6346
6347int
6348xfs_bmap_split_extent(
6349 struct xfs_inode *ip,
6350 xfs_fileoff_t split_fsb)
6351{
6352 struct xfs_mount *mp = ip->i_mount;
6353 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006354 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006355 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006356 int error;
6357
Christoph Hellwig253f4912016-04-06 09:19:55 +10006358 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6359 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6360 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006361 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006362
6363 xfs_ilock(ip, XFS_ILOCK_EXCL);
6364 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6365
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006366 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006367
6368 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006369 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006370 if (error)
6371 goto out;
6372
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006373 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006374 if (error)
6375 goto out;
6376
Christoph Hellwig70393312015-06-04 13:48:08 +10006377 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006378
6379out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006380 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006381 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006382 return error;
6383}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006384
6385/* Deferred mapping is only for real extents in the data fork. */
6386static bool
6387xfs_bmap_is_update_needed(
6388 struct xfs_bmbt_irec *bmap)
6389{
6390 return bmap->br_startblock != HOLESTARTBLOCK &&
6391 bmap->br_startblock != DELAYSTARTBLOCK;
6392}
6393
6394/* Record a bmap intent. */
6395static int
6396__xfs_bmap_add(
6397 struct xfs_mount *mp,
6398 struct xfs_defer_ops *dfops,
6399 enum xfs_bmap_intent_type type,
6400 struct xfs_inode *ip,
6401 int whichfork,
6402 struct xfs_bmbt_irec *bmap)
6403{
6404 int error;
6405 struct xfs_bmap_intent *bi;
6406
6407 trace_xfs_bmap_defer(mp,
6408 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6409 type,
6410 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6411 ip->i_ino, whichfork,
6412 bmap->br_startoff,
6413 bmap->br_blockcount,
6414 bmap->br_state);
6415
6416 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6417 INIT_LIST_HEAD(&bi->bi_list);
6418 bi->bi_type = type;
6419 bi->bi_owner = ip;
6420 bi->bi_whichfork = whichfork;
6421 bi->bi_bmap = *bmap;
6422
6423 error = xfs_defer_join(dfops, bi->bi_owner);
6424 if (error) {
6425 kmem_free(bi);
6426 return error;
6427 }
6428
6429 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6430 return 0;
6431}
6432
6433/* Map an extent into a file. */
6434int
6435xfs_bmap_map_extent(
6436 struct xfs_mount *mp,
6437 struct xfs_defer_ops *dfops,
6438 struct xfs_inode *ip,
6439 struct xfs_bmbt_irec *PREV)
6440{
6441 if (!xfs_bmap_is_update_needed(PREV))
6442 return 0;
6443
6444 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6445 XFS_DATA_FORK, PREV);
6446}
6447
6448/* Unmap an extent out of a file. */
6449int
6450xfs_bmap_unmap_extent(
6451 struct xfs_mount *mp,
6452 struct xfs_defer_ops *dfops,
6453 struct xfs_inode *ip,
6454 struct xfs_bmbt_irec *PREV)
6455{
6456 if (!xfs_bmap_is_update_needed(PREV))
6457 return 0;
6458
6459 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6460 XFS_DATA_FORK, PREV);
6461}
6462
6463/*
6464 * Process one of the deferred bmap operations. We pass back the
6465 * btree cursor to maintain our lock on the bmapbt between calls.
6466 */
6467int
6468xfs_bmap_finish_one(
6469 struct xfs_trans *tp,
6470 struct xfs_defer_ops *dfops,
6471 struct xfs_inode *ip,
6472 enum xfs_bmap_intent_type type,
6473 int whichfork,
6474 xfs_fileoff_t startoff,
6475 xfs_fsblock_t startblock,
6476 xfs_filblks_t blockcount,
6477 xfs_exntst_t state)
6478{
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006479 int error = 0, done;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006480
6481 trace_xfs_bmap_deferred(tp->t_mountp,
6482 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6483 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6484 ip->i_ino, whichfork, startoff, blockcount, state);
6485
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006486 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006487 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006488
6489 if (XFS_TEST_ERROR(false, tp->t_mountp,
6490 XFS_ERRTAG_BMAP_FINISH_ONE,
6491 XFS_RANDOM_BMAP_FINISH_ONE))
6492 return -EIO;
6493
6494 switch (type) {
6495 case XFS_BMAP_MAP:
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006496 error = xfs_bmapi_remap(tp, ip, startoff, blockcount,
6497 startblock, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006498 break;
6499 case XFS_BMAP_UNMAP:
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006500 error = xfs_bunmapi(tp, ip, startoff, blockcount,
6501 XFS_BMAPI_REMAP, 1, &startblock, dfops, &done);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006502 ASSERT(done);
6503 break;
6504 default:
6505 ASSERT(0);
6506 error = -EFSCORRUPTED;
6507 }
6508
6509 return error;
6510}