blob: 8adb91b0558831b00ac8579748b72f73bf2bd128 [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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100210 level - 1;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100211 break;
212 }
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100352 */
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100600{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000601 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100746 /*
747 * Need a cursor. Can't allocate until bb_level is filled in.
748 */
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100786 /*
787 * Allocation can't fail, the space was reserved.
788 */
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +11001129
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001130 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +11001197 }
1198
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001199 error = xfs_defer_finish(&tp, &dfops, NULL);
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +11001242 mp = ip->i_mount;
1243 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +11001338}
1339
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +11001440 return 0;
1441}
1442
Dave Chinner68988112013-08-12 20:49:42 +10001443int
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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);
Brian Foster0daaeca2017-05-12 10:44:08 -07002068 diff = (int)(temp + temp2 -
2069 (startblockval(PREV.br_startblock) -
2070 (bma->cur ?
2071 bma->cur->bc_private.b.allocated : 0)));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002072 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002073 error = xfs_mod_fdblocks(bma->ip->i_mount,
2074 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002075 ASSERT(!error);
2076 if (error)
2077 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002079
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002080 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002081 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002082 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2083 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2084 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002085 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002086 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002087
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002088 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002089 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 break;
2091
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002092 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2093 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2094 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2095 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2096 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2097 case BMAP_LEFT_CONTIG:
2098 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 /*
2100 * These cases are all impossible.
2101 */
2102 ASSERT(0);
2103 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002104
Darrick J. Wong9c194642016-08-03 12:16:05 +10002105 /* add reverse mapping */
2106 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2107 if (error)
2108 goto done;
2109
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002110 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002111 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002112 int tmp_logflags; /* partial log flag return val */
2113
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002114 ASSERT(bma->cur == NULL);
2115 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002116 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002117 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002118 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002119 if (error)
2120 goto done;
2121 }
2122
2123 /* adjust for changes in reserved delayed indirect blocks */
2124 if (da_old || da_new) {
2125 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002126 if (bma->cur)
2127 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002128 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002129 xfs_mod_fdblocks(bma->ip->i_mount,
2130 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002131 }
2132
2133 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002134 if (bma->cur)
2135 bma->cur->bc_private.b.allocated = 0;
2136
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002137 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002139 if (whichfork != XFS_COW_FORK)
2140 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return error;
2142#undef LEFT
2143#undef RIGHT
2144#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
2147/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002148 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 */
2150STATIC int /* error */
2151xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002152 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002154 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002155 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002157 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002158 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002159 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002160 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002163 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002166 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 xfs_fileoff_t new_endoff; /* end offset of new entry */
2168 xfs_exntst_t newext; /* new extent state */
2169 xfs_exntst_t oldext; /* old extent state */
2170 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2171 /* left is 0, right is 1, prev is 2 */
2172 int rval=0; /* return value (logging flags) */
2173 int state = 0;/* state bits, accessed thru macros */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002174 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002176 *logflagsp = 0;
2177
2178 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002179 ifp = XFS_IFORK_PTR(ip, whichfork);
2180 if (whichfork == XFS_COW_FORK)
2181 state |= BMAP_COWFORK;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002182
2183 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002184 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002185 ASSERT(!isnullstartblock(new->br_startblock));
2186
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002187 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189#define LEFT r[0]
2190#define RIGHT r[1]
2191#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /*
2194 * Set up a bunch of variables to make the tests simpler.
2195 */
2196 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002197 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 xfs_bmbt_get_all(ep, &PREV);
2199 newext = new->br_state;
2200 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2201 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2202 ASSERT(PREV.br_state == oldext);
2203 new_endoff = new->br_startoff + new->br_blockcount;
2204 ASSERT(PREV.br_startoff <= new->br_startoff);
2205 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 /*
2208 * Set flags determining what part of the previous oldext allocation
2209 * extent is being replaced by a newext allocation.
2210 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002211 if (PREV.br_startoff == new->br_startoff)
2212 state |= BMAP_LEFT_FILLING;
2213 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2214 state |= BMAP_RIGHT_FILLING;
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 /*
2217 * Check and set flags if this segment has a left neighbor.
2218 * Don't set contiguous if the combined extent would be too large.
2219 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002220 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002221 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002222 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002223
2224 if (isnullstartblock(LEFT.br_startblock))
2225 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002227
2228 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2229 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2230 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2231 LEFT.br_state == newext &&
2232 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2233 state |= BMAP_LEFT_CONTIG;
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 /*
2236 * Check and set flags if this segment has a right neighbor.
2237 * Don't set contiguous if the combined extent would be too large.
2238 * Also check for all-three-contiguous being too large.
2239 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002240 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002241 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002242 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002243 if (isnullstartblock(RIGHT.br_startblock))
2244 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002246
2247 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2248 new_endoff == RIGHT.br_startoff &&
2249 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2250 newext == RIGHT.br_state &&
2251 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2252 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2253 BMAP_RIGHT_FILLING)) !=
2254 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2255 BMAP_RIGHT_FILLING) ||
2256 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2257 <= MAXEXTLEN))
2258 state |= BMAP_RIGHT_CONTIG;
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 /*
2261 * Switch out based on the FILLING and CONTIG state bits.
2262 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002263 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2264 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2265 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2266 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /*
2268 * Setting all of a previous oldext extent to newext.
2269 * The left and right neighbors are both contiguous with new.
2270 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002271 --*idx;
2272
2273 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2274 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 LEFT.br_blockcount + PREV.br_blockcount +
2276 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002277 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002278
Christoph Hellwigec90c552011-05-23 08:52:53 +00002279 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002280 XFS_IFORK_NEXT_SET(ip, whichfork,
2281 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (cur == NULL)
2283 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2284 else {
2285 rval = XFS_ILOG_CORE;
2286 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2287 RIGHT.br_startblock,
2288 RIGHT.br_blockcount, &i)))
2289 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002290 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002291 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002293 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002294 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002296 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002297 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002299 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002300 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002302 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2304 LEFT.br_startblock,
2305 LEFT.br_blockcount + PREV.br_blockcount +
2306 RIGHT.br_blockcount, LEFT.br_state)))
2307 goto done;
2308 }
2309 break;
2310
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002311 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /*
2313 * Setting all of a previous oldext extent to newext.
2314 * The left neighbor is contiguous, the right is not.
2315 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002316 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002317
Christoph Hellwigec90c552011-05-23 08:52:53 +00002318 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2319 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2320 LEFT.br_blockcount + PREV.br_blockcount);
2321 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2322
2323 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002324 XFS_IFORK_NEXT_SET(ip, whichfork,
2325 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (cur == NULL)
2327 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2328 else {
2329 rval = XFS_ILOG_CORE;
2330 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2331 PREV.br_startblock, PREV.br_blockcount,
2332 &i)))
2333 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002334 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002335 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002337 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002338 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002340 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2342 LEFT.br_startblock,
2343 LEFT.br_blockcount + PREV.br_blockcount,
2344 LEFT.br_state)))
2345 goto done;
2346 }
2347 break;
2348
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002349 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 /*
2351 * Setting all of a previous oldext extent to newext.
2352 * The right neighbor is contiguous, the left is not.
2353 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002354 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 xfs_bmbt_set_blockcount(ep,
2356 PREV.br_blockcount + RIGHT.br_blockcount);
2357 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002358 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2359 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002360 XFS_IFORK_NEXT_SET(ip, whichfork,
2361 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (cur == NULL)
2363 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2364 else {
2365 rval = XFS_ILOG_CORE;
2366 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2367 RIGHT.br_startblock,
2368 RIGHT.br_blockcount, &i)))
2369 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002370 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002371 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002373 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002374 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002376 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2378 new->br_startblock,
2379 new->br_blockcount + RIGHT.br_blockcount,
2380 newext)))
2381 goto done;
2382 }
2383 break;
2384
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002385 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 /*
2387 * Setting all of a previous oldext extent to newext.
2388 * Neither the left nor right neighbors are contiguous with
2389 * the new one.
2390 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002391 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002393 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 if (cur == NULL)
2396 rval = XFS_ILOG_DEXT;
2397 else {
2398 rval = 0;
2399 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2400 new->br_startblock, new->br_blockcount,
2401 &i)))
2402 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002403 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2405 new->br_startblock, new->br_blockcount,
2406 newext)))
2407 goto done;
2408 }
2409 break;
2410
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002411 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 /*
2413 * Setting the first part of a previous oldext extent to newext.
2414 * The left neighbor is contiguous.
2415 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002416 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2417 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 LEFT.br_blockcount + new->br_blockcount);
2419 xfs_bmbt_set_startoff(ep,
2420 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002421 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002422
Christoph Hellwigec90c552011-05-23 08:52:53 +00002423 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 xfs_bmbt_set_startblock(ep,
2425 new->br_startblock + new->br_blockcount);
2426 xfs_bmbt_set_blockcount(ep,
2427 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002428 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002429
Christoph Hellwigec90c552011-05-23 08:52:53 +00002430 --*idx;
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 if (cur == NULL)
2433 rval = XFS_ILOG_DEXT;
2434 else {
2435 rval = 0;
2436 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2437 PREV.br_startblock, PREV.br_blockcount,
2438 &i)))
2439 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002440 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if ((error = xfs_bmbt_update(cur,
2442 PREV.br_startoff + new->br_blockcount,
2443 PREV.br_startblock + new->br_blockcount,
2444 PREV.br_blockcount - new->br_blockcount,
2445 oldext)))
2446 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002447 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002449 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 LEFT.br_startblock,
2451 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002452 LEFT.br_state);
2453 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 goto done;
2455 }
2456 break;
2457
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002458 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 /*
2460 * Setting the first part of a previous oldext extent to newext.
2461 * The left neighbor is not contiguous.
2462 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002463 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2465 xfs_bmbt_set_startoff(ep, new_endoff);
2466 xfs_bmbt_set_blockcount(ep,
2467 PREV.br_blockcount - new->br_blockcount);
2468 xfs_bmbt_set_startblock(ep,
2469 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002470 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002471
Christoph Hellwigec90c552011-05-23 08:52:53 +00002472 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002473 XFS_IFORK_NEXT_SET(ip, whichfork,
2474 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 if (cur == NULL)
2476 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2477 else {
2478 rval = XFS_ILOG_CORE;
2479 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2480 PREV.br_startblock, PREV.br_blockcount,
2481 &i)))
2482 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002483 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if ((error = xfs_bmbt_update(cur,
2485 PREV.br_startoff + new->br_blockcount,
2486 PREV.br_startblock + new->br_blockcount,
2487 PREV.br_blockcount - new->br_blockcount,
2488 oldext)))
2489 goto done;
2490 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002491 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002493 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 }
2495 break;
2496
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002497 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 /*
2499 * Setting the last part of a previous oldext extent to newext.
2500 * The right neighbor is contiguous with the new allocation.
2501 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002502 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 xfs_bmbt_set_blockcount(ep,
2504 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002505 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2506
2507 ++*idx;
2508
2509 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2510 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002511 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002513 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 if (cur == NULL)
2516 rval = XFS_ILOG_DEXT;
2517 else {
2518 rval = 0;
2519 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2520 PREV.br_startblock,
2521 PREV.br_blockcount, &i)))
2522 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002523 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2525 PREV.br_startblock,
2526 PREV.br_blockcount - new->br_blockcount,
2527 oldext)))
2528 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002529 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 goto done;
2531 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2532 new->br_startblock,
2533 new->br_blockcount + RIGHT.br_blockcount,
2534 newext)))
2535 goto done;
2536 }
2537 break;
2538
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002539 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /*
2541 * Setting the last part of a previous oldext extent to newext.
2542 * The right neighbor is not contiguous.
2543 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002544 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 xfs_bmbt_set_blockcount(ep,
2546 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002547 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002548
Christoph Hellwigec90c552011-05-23 08:52:53 +00002549 ++*idx;
2550 xfs_iext_insert(ip, *idx, 1, new, state);
2551
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002552 XFS_IFORK_NEXT_SET(ip, whichfork,
2553 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (cur == NULL)
2555 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2556 else {
2557 rval = XFS_ILOG_CORE;
2558 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2559 PREV.br_startblock, PREV.br_blockcount,
2560 &i)))
2561 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002562 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2564 PREV.br_startblock,
2565 PREV.br_blockcount - new->br_blockcount,
2566 oldext)))
2567 goto done;
2568 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2569 new->br_startblock, new->br_blockcount,
2570 &i)))
2571 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002572 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002574 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002576 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 }
2578 break;
2579
2580 case 0:
2581 /*
2582 * Setting the middle part of a previous oldext extent to
2583 * newext. Contiguity is impossible here.
2584 * One extent becomes three extents.
2585 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002586 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 xfs_bmbt_set_blockcount(ep,
2588 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002589 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002590
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 r[0] = *new;
2592 r[1].br_startoff = new_endoff;
2593 r[1].br_blockcount =
2594 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2595 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2596 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002597
2598 ++*idx;
2599 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2600
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002601 XFS_IFORK_NEXT_SET(ip, whichfork,
2602 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 if (cur == NULL)
2604 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2605 else {
2606 rval = XFS_ILOG_CORE;
2607 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2608 PREV.br_startblock, PREV.br_blockcount,
2609 &i)))
2610 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002611 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 /* new right extent - oldext */
2613 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2614 r[1].br_startblock, r[1].br_blockcount,
2615 r[1].br_state)))
2616 goto done;
2617 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002619 cur->bc_rec.b.br_blockcount =
2620 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002621 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002623 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002624 /*
2625 * Reset the cursor to the position of the new extent
2626 * we are about to insert as we can't trust it after
2627 * the previous insert.
2628 */
2629 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2630 new->br_startblock, new->br_blockcount,
2631 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002633 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002635 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002636 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002638 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
2640 break;
2641
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002642 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2643 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2644 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2645 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2646 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2647 case BMAP_LEFT_CONTIG:
2648 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 /*
2650 * These cases are all impossible.
2651 */
2652 ASSERT(0);
2653 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002654
Darrick J. Wong9c194642016-08-03 12:16:05 +10002655 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002656 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002657 if (error)
2658 goto done;
2659
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002660 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002661 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002662 int tmp_logflags; /* partial log flag return val */
2663
2664 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002665 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002666 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002667 *logflagsp |= tmp_logflags;
2668 if (error)
2669 goto done;
2670 }
2671
2672 /* clear out the allocated field, done with it now in any case. */
2673 if (cur) {
2674 cur->bc_private.b.allocated = 0;
2675 *curp = cur;
2676 }
2677
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002678 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002680 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 return error;
2682#undef LEFT
2683#undef RIGHT
2684#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
2686
2687/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002688 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002690STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691xfs_bmap_add_extent_hole_delay(
2692 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002693 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002694 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002695 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002697 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2699 xfs_filblks_t newlen=0; /* new indirect size */
2700 xfs_filblks_t oldlen=0; /* old indirect size */
2701 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2702 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002703 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002705 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002707 if (whichfork == XFS_COW_FORK)
2708 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002709 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 /*
2712 * Check and set flags if this segment has a left neighbor
2713 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002714 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002715 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002716 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002717
2718 if (isnullstartblock(left.br_startblock))
2719 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 /*
2723 * Check and set flags if the current (right) segment exists.
2724 * If it doesn't exist, we're converting the hole at end-of-file.
2725 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002726 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002727 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002728 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002729
2730 if (isnullstartblock(right.br_startblock))
2731 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /*
2735 * Set contiguity flags on the left and right neighbors.
2736 * Don't let extents get too large, even if the pieces are contiguous.
2737 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002738 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2739 left.br_startoff + left.br_blockcount == new->br_startoff &&
2740 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2741 state |= BMAP_LEFT_CONTIG;
2742
2743 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2744 new->br_startoff + new->br_blockcount == right.br_startoff &&
2745 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2746 (!(state & BMAP_LEFT_CONTIG) ||
2747 (left.br_blockcount + new->br_blockcount +
2748 right.br_blockcount <= MAXEXTLEN)))
2749 state |= BMAP_RIGHT_CONTIG;
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 /*
2752 * Switch out based on the contiguity flags.
2753 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002754 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2755 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 /*
2757 * New allocation is contiguous with delayed allocations
2758 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002759 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002761 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 temp = left.br_blockcount + new->br_blockcount +
2763 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002764
Christoph Hellwigec90c552011-05-23 08:52:53 +00002765 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2766 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002767 oldlen = startblockval(left.br_startblock) +
2768 startblockval(new->br_startblock) +
2769 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002770 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2771 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002772 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002773 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002774 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002775
Christoph Hellwigec90c552011-05-23 08:52:53 +00002776 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 break;
2778
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002779 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 /*
2781 * New allocation is contiguous with a delayed allocation
2782 * on the left.
2783 * Merge the new allocation with the left neighbor.
2784 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002785 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002787
2788 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2789 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002790 oldlen = startblockval(left.br_startblock) +
2791 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002792 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2793 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002794 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002795 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002796 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 break;
2798
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002799 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 /*
2801 * New allocation is contiguous with a delayed allocation
2802 * on the right.
2803 * Merge the new allocation with the right neighbor.
2804 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002805 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002807 oldlen = startblockval(new->br_startblock) +
2808 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002809 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2810 oldlen);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002811 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2812 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002813 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002814 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 break;
2816
2817 case 0:
2818 /*
2819 * New allocation is not contiguous with another
2820 * delayed allocation.
2821 * Insert a new entry.
2822 */
2823 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002824 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 break;
2826 }
2827 if (oldlen != newlen) {
2828 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002829 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2830 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 /*
2832 * Nothing to do for disk quota accounting here.
2833 */
2834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835}
2836
2837/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002838 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 */
2840STATIC int /* error */
2841xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002842 struct xfs_trans *tp,
2843 struct xfs_inode *ip,
2844 int whichfork,
2845 xfs_extnum_t *idx,
2846 struct xfs_btree_cur **curp,
2847 struct xfs_bmbt_irec *new,
2848 xfs_fsblock_t *first,
2849 struct xfs_defer_ops *dfops,
2850 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002852 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2853 struct xfs_mount *mp = ip->i_mount;
2854 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2858 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002859 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Christoph Hellwig6d045582017-04-11 16:45:54 -07002862 ASSERT(*idx >= 0);
2863 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002864 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002865 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002866
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002867 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002868
2869 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002870 if (whichfork == XFS_ATTR_FORK)
2871 state |= BMAP_ATTRFORK;
Christoph Hellwiga14234c2017-02-06 10:50:49 -08002872 if (whichfork == XFS_COW_FORK)
2873 state |= BMAP_COWFORK;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 /*
2876 * Check and set flags if this segment has a left neighbor.
2877 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002878 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002879 state |= BMAP_LEFT_VALID;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002880 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002881 if (isnullstartblock(left.br_startblock))
2882 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 /*
2886 * Check and set flags if this segment has a current value.
2887 * Not true if we're inserting into the "hole" at eof.
2888 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002889 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002890 state |= BMAP_RIGHT_VALID;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002891 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002892 if (isnullstartblock(right.br_startblock))
2893 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 /*
2897 * We're inserting a real allocation between "left" and "right".
2898 * Set the contiguity flags. Don't let extents get too large.
2899 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002900 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2901 left.br_startoff + left.br_blockcount == new->br_startoff &&
2902 left.br_startblock + left.br_blockcount == new->br_startblock &&
2903 left.br_state == new->br_state &&
2904 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2905 state |= BMAP_LEFT_CONTIG;
2906
2907 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2908 new->br_startoff + new->br_blockcount == right.br_startoff &&
2909 new->br_startblock + new->br_blockcount == right.br_startblock &&
2910 new->br_state == right.br_state &&
2911 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2912 (!(state & BMAP_LEFT_CONTIG) ||
2913 left.br_blockcount + new->br_blockcount +
2914 right.br_blockcount <= MAXEXTLEN))
2915 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002917 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 /*
2919 * Select which case we're in here, and implement it.
2920 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002921 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2922 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 /*
2924 * New allocation is contiguous with real allocations on the
2925 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002926 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002928 --*idx;
2929 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2930 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 left.br_blockcount + new->br_blockcount +
2932 right.br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002933 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002934
Christoph Hellwig6d045582017-04-11 16:45:54 -07002935 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002936
Christoph Hellwig6d045582017-04-11 16:45:54 -07002937 XFS_IFORK_NEXT_SET(ip, whichfork,
2938 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2939 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002940 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002941 } else {
2942 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002943 error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002944 right.br_startblock, right.br_blockcount,
2945 &i);
2946 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002947 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002948 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002949 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002950 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002951 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002952 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002953 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002954 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002955 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002956 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002957 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002958 left.br_startblock,
2959 left.br_blockcount +
2960 new->br_blockcount +
2961 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002962 left.br_state);
2963 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002964 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002966 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002968 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 /*
2970 * New allocation is contiguous with a real allocation
2971 * on the left.
2972 * Merge the new allocation with the left neighbor.
2973 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002974 --*idx;
2975 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2976 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 left.br_blockcount + new->br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002978 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002979
Christoph Hellwig6d045582017-04-11 16:45:54 -07002980 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002981 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002982 } else {
2983 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002984 error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002985 left.br_startblock, left.br_blockcount,
2986 &i);
2987 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002988 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002989 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002990 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002991 left.br_startblock,
2992 left.br_blockcount +
2993 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002994 left.br_state);
2995 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002996 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002998 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003000 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 /*
3002 * New allocation is contiguous with a real allocation
3003 * on the right.
3004 * Merge the new allocation with the right neighbor.
3005 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003006 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
3007 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003008 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 new->br_blockcount + right.br_blockcount,
3010 right.br_state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003011 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003012
Christoph Hellwig6d045582017-04-11 16:45:54 -07003013 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003014 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003015 } else {
3016 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003017 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003018 right.br_startoff,
3019 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003020 right.br_blockcount, &i);
3021 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003022 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003023 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003024 error = xfs_bmbt_update(cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003025 new->br_startblock,
3026 new->br_blockcount +
3027 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003028 right.br_state);
3029 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003030 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003032 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034 case 0:
3035 /*
3036 * New allocation is not contiguous with another
3037 * real allocation.
3038 * Insert a new entry.
3039 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003040 xfs_iext_insert(ip, *idx, 1, new, state);
3041 XFS_IFORK_NEXT_SET(ip, whichfork,
3042 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3043 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003044 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003045 } else {
3046 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003047 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003048 new->br_startoff,
3049 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003050 new->br_blockcount, &i);
3051 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003052 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003053 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003054 cur->bc_rec.b.br_state = new->br_state;
3055 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003056 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003057 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003058 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003060 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003062
Darrick J. Wong9c194642016-08-03 12:16:05 +10003063 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003064 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10003065 if (error)
3066 goto done;
3067
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003068 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003069 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003070 int tmp_logflags; /* partial log flag return val */
3071
Christoph Hellwig6d045582017-04-11 16:45:54 -07003072 ASSERT(cur == NULL);
3073 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003074 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003075 *logflagsp |= tmp_logflags;
3076 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003077 if (error)
3078 goto done;
3079 }
3080
3081 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003082 if (cur)
3083 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003084
Christoph Hellwig6d045582017-04-11 16:45:54 -07003085 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003086done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07003087 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003088 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089}
3090
Nathan Scottdd9f4382006-01-11 15:28:28 +11003091/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11003092 * Functions used in the extent read, allocate and remove paths
3093 */
3094
3095/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003096 * Adjust the size of the new extent based on di_extsize and rt extsize.
3097 */
Dave Chinner68988112013-08-12 20:49:42 +10003098int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003099xfs_bmap_extsize_align(
3100 xfs_mount_t *mp,
3101 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3102 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3103 xfs_extlen_t extsz, /* align to this extent size */
3104 int rt, /* is this a realtime inode? */
3105 int eof, /* is extent at end-of-file? */
3106 int delay, /* creating delalloc extent? */
3107 int convert, /* overwriting unwritten extent? */
3108 xfs_fileoff_t *offp, /* in/out: aligned offset */
3109 xfs_extlen_t *lenp) /* in/out: aligned length */
3110{
3111 xfs_fileoff_t orig_off; /* original offset */
3112 xfs_extlen_t orig_alen; /* original length */
3113 xfs_fileoff_t orig_end; /* original off+len */
3114 xfs_fileoff_t nexto; /* next file offset */
3115 xfs_fileoff_t prevo; /* previous file offset */
3116 xfs_fileoff_t align_off; /* temp for offset */
3117 xfs_extlen_t align_alen; /* temp for length */
3118 xfs_extlen_t temp; /* temp for calculations */
3119
3120 if (convert)
3121 return 0;
3122
3123 orig_off = align_off = *offp;
3124 orig_alen = align_alen = *lenp;
3125 orig_end = orig_off + orig_alen;
3126
3127 /*
3128 * If this request overlaps an existing extent, then don't
3129 * attempt to perform any additional alignment.
3130 */
3131 if (!delay && !eof &&
3132 (orig_off >= gotp->br_startoff) &&
3133 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3134 return 0;
3135 }
3136
3137 /*
3138 * If the file offset is unaligned vs. the extent size
3139 * we need to align it. This will be possible unless
3140 * the file was previously written with a kernel that didn't
3141 * perform this alignment, or if a truncate shot us in the
3142 * foot.
3143 */
3144 temp = do_mod(orig_off, extsz);
3145 if (temp) {
3146 align_alen += temp;
3147 align_off -= temp;
3148 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003149
3150 /* Same adjustment for the end of the requested area. */
3151 temp = (align_alen % extsz);
3152 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003153 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003154
3155 /*
3156 * For large extent hint sizes, the aligned extent might be larger than
3157 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3158 * the length back under MAXEXTLEN. The outer allocation loops handle
3159 * short allocation just fine, so it is safe to do this. We only want to
3160 * do it when we are forced to, though, because it means more allocation
3161 * operations are required.
3162 */
3163 while (align_alen > MAXEXTLEN)
3164 align_alen -= extsz;
3165 ASSERT(align_alen <= MAXEXTLEN);
3166
Nathan Scottdd9f4382006-01-11 15:28:28 +11003167 /*
3168 * If the previous block overlaps with this proposed allocation
3169 * then move the start forward without adjusting the length.
3170 */
3171 if (prevp->br_startoff != NULLFILEOFF) {
3172 if (prevp->br_startblock == HOLESTARTBLOCK)
3173 prevo = prevp->br_startoff;
3174 else
3175 prevo = prevp->br_startoff + prevp->br_blockcount;
3176 } else
3177 prevo = 0;
3178 if (align_off != orig_off && align_off < prevo)
3179 align_off = prevo;
3180 /*
3181 * If the next block overlaps with this proposed allocation
3182 * then move the start back without adjusting the length,
3183 * but not before offset 0.
3184 * This may of course make the start overlap previous block,
3185 * and if we hit the offset 0 limit then the next block
3186 * can still overlap too.
3187 */
3188 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3189 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3190 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3191 nexto = gotp->br_startoff + gotp->br_blockcount;
3192 else
3193 nexto = gotp->br_startoff;
3194 } else
3195 nexto = NULLFILEOFF;
3196 if (!eof &&
3197 align_off + align_alen != orig_end &&
3198 align_off + align_alen > nexto)
3199 align_off = nexto > align_alen ? nexto - align_alen : 0;
3200 /*
3201 * If we're now overlapping the next or previous extent that
3202 * means we can't fit an extsz piece in this hole. Just move
3203 * the start forward to the first valid spot and set
3204 * the length so we hit the end.
3205 */
3206 if (align_off != orig_off && align_off < prevo)
3207 align_off = prevo;
3208 if (align_off + align_alen != orig_end &&
3209 align_off + align_alen > nexto &&
3210 nexto != NULLFILEOFF) {
3211 ASSERT(nexto > prevo);
3212 align_alen = nexto - align_off;
3213 }
3214
3215 /*
3216 * If realtime, and the result isn't a multiple of the realtime
3217 * extent size we need to remove blocks until it is.
3218 */
3219 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3220 /*
3221 * We're not covering the original request, or
3222 * we won't be able to once we fix the length.
3223 */
3224 if (orig_off < align_off ||
3225 orig_end > align_off + align_alen ||
3226 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003227 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003228 /*
3229 * Try to fix it by moving the start up.
3230 */
3231 if (align_off + temp <= orig_off) {
3232 align_alen -= temp;
3233 align_off += temp;
3234 }
3235 /*
3236 * Try to fix it by moving the end in.
3237 */
3238 else if (align_off + align_alen - temp >= orig_end)
3239 align_alen -= temp;
3240 /*
3241 * Set the start to the minimum then trim the length.
3242 */
3243 else {
3244 align_alen -= orig_off - align_off;
3245 align_off = orig_off;
3246 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3247 }
3248 /*
3249 * Result doesn't cover the request, fail it.
3250 */
3251 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003252 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003253 } else {
3254 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003255 /* see MAXEXTLEN handling above */
3256 ASSERT(orig_end <= align_off + align_alen ||
3257 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003258 }
3259
3260#ifdef DEBUG
3261 if (!eof && gotp->br_startoff != NULLFILEOFF)
3262 ASSERT(align_off + align_alen <= gotp->br_startoff);
3263 if (prevp->br_startoff != NULLFILEOFF)
3264 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3265#endif
3266
3267 *lenp = align_alen;
3268 *offp = align_off;
3269 return 0;
3270}
3271
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272#define XFS_ALLOC_GAP_UNITS 4
3273
Dave Chinner68988112013-08-12 20:49:42 +10003274void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003275xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003276 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277{
3278 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3280 xfs_mount_t *mp; /* mount point structure */
3281 int nullfb; /* true if ap->firstblock isn't set */
3282 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283
3284#define ISVALID(x,y) \
3285 (rt ? \
3286 (x) < mp->m_sb.sb_rblocks : \
3287 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3288 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3289 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003292 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003293 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3294 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003295 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 /*
3297 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003298 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003300 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3301 !isnullstartblock(ap->prev.br_startblock) &&
3302 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3303 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003304 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 /*
3306 * Adjust for the gap between prevp and us.
3307 */
Dave Chinner3a756672011-09-18 20:40:58 +00003308 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003309 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003311 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3312 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 }
3314 /*
3315 * If not at eof, then compare the two neighbor blocks.
3316 * Figure out whether either one gives us a good starting point,
3317 * and pick the better one.
3318 */
3319 else if (!ap->eof) {
3320 xfs_fsblock_t gotbno; /* right side block number */
3321 xfs_fsblock_t gotdiff=0; /* right side difference */
3322 xfs_fsblock_t prevbno; /* left side block number */
3323 xfs_fsblock_t prevdiff=0; /* left side difference */
3324
3325 /*
3326 * If there's a previous (left) block, select a requested
3327 * start block based on it.
3328 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003329 if (ap->prev.br_startoff != NULLFILEOFF &&
3330 !isnullstartblock(ap->prev.br_startblock) &&
3331 (prevbno = ap->prev.br_startblock +
3332 ap->prev.br_blockcount) &&
3333 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 /*
3335 * Calculate gap to end of previous block.
3336 */
Dave Chinner3a756672011-09-18 20:40:58 +00003337 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003338 (ap->prev.br_startoff +
3339 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 /*
3341 * Figure the startblock based on the previous block's
3342 * end and the gap size.
3343 * Heuristic!
3344 * If the gap is large relative to the piece we're
3345 * allocating, or using it gives us an invalid block
3346 * number, then just use the end of the previous block.
3347 */
Dave Chinner3a756672011-09-18 20:40:58 +00003348 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003350 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 prevbno += adjust;
3352 else
3353 prevdiff += adjust;
3354 /*
3355 * If the firstblock forbids it, can't use it,
3356 * must use default.
3357 */
3358 if (!rt && !nullfb &&
3359 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3360 prevbno = NULLFSBLOCK;
3361 }
3362 /*
3363 * No previous block or can't follow it, just default.
3364 */
3365 else
3366 prevbno = NULLFSBLOCK;
3367 /*
3368 * If there's a following (right) block, select a requested
3369 * start block based on it.
3370 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003371 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 /*
3373 * Calculate gap to start of next block.
3374 */
Dave Chinner3a756672011-09-18 20:40:58 +00003375 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 /*
3377 * Figure the startblock based on the next block's
3378 * start and the gap size.
3379 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003380 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 /*
3382 * Heuristic!
3383 * If the gap is large relative to the piece we're
3384 * allocating, or using it gives us an invalid block
3385 * number, then just use the start of the next block
3386 * offset by our length.
3387 */
Dave Chinner3a756672011-09-18 20:40:58 +00003388 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 ISVALID(gotbno - gotdiff, gotbno))
3390 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003391 else if (ISVALID(gotbno - ap->length, gotbno)) {
3392 gotbno -= ap->length;
3393 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 } else
3395 gotdiff += adjust;
3396 /*
3397 * If the firstblock forbids it, can't use it,
3398 * must use default.
3399 */
3400 if (!rt && !nullfb &&
3401 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3402 gotbno = NULLFSBLOCK;
3403 }
3404 /*
3405 * No next block, just default.
3406 */
3407 else
3408 gotbno = NULLFSBLOCK;
3409 /*
3410 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003411 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 */
3413 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003414 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003416 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003418 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003420#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003421}
3422
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003423static int
3424xfs_bmap_longest_free_extent(
3425 struct xfs_trans *tp,
3426 xfs_agnumber_t ag,
3427 xfs_extlen_t *blen,
3428 int *notinit)
3429{
3430 struct xfs_mount *mp = tp->t_mountp;
3431 struct xfs_perag *pag;
3432 xfs_extlen_t longest;
3433 int error = 0;
3434
3435 pag = xfs_perag_get(mp, ag);
3436 if (!pag->pagf_init) {
3437 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3438 if (error)
3439 goto out;
3440
3441 if (!pag->pagf_init) {
3442 *notinit = 1;
3443 goto out;
3444 }
3445 }
3446
Dave Chinner50adbcb2015-06-22 10:04:31 +10003447 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003448 xfs_alloc_min_freelist(mp, pag),
3449 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003450 if (*blen < longest)
3451 *blen = longest;
3452
3453out:
3454 xfs_perag_put(pag);
3455 return error;
3456}
3457
3458static void
3459xfs_bmap_select_minlen(
3460 struct xfs_bmalloca *ap,
3461 struct xfs_alloc_arg *args,
3462 xfs_extlen_t *blen,
3463 int notinit)
3464{
3465 if (notinit || *blen < ap->minlen) {
3466 /*
3467 * Since we did a BUF_TRYLOCK above, it is possible that
3468 * there is space for this request.
3469 */
3470 args->minlen = ap->minlen;
3471 } else if (*blen < args->maxlen) {
3472 /*
3473 * If the best seen length is less than the request length,
3474 * use the best as the minimum.
3475 */
3476 args->minlen = *blen;
3477 } else {
3478 /*
3479 * Otherwise we've seen an extent as big as maxlen, use that
3480 * as the minimum.
3481 */
3482 args->minlen = args->maxlen;
3483 }
3484}
3485
Nathan Scotta365bdd2006-03-14 13:34:16 +11003486STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003487xfs_bmap_btalloc_nullfb(
3488 struct xfs_bmalloca *ap,
3489 struct xfs_alloc_arg *args,
3490 xfs_extlen_t *blen)
3491{
3492 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003493 xfs_agnumber_t ag, startag;
3494 int notinit = 0;
3495 int error;
3496
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003497 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003498 args->total = ap->total;
3499
Christoph Hellwigc467c042010-02-15 23:34:42 +00003500 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3501 if (startag == NULLAGNUMBER)
3502 startag = ag = 0;
3503
Dave Chinner14b064c2011-01-27 12:16:28 +11003504 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003505 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3506 &notinit);
3507 if (error)
3508 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003509
Christoph Hellwigc467c042010-02-15 23:34:42 +00003510 if (++ag == mp->m_sb.sb_agcount)
3511 ag = 0;
3512 if (ag == startag)
3513 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003514 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003515
3516 xfs_bmap_select_minlen(ap, args, blen, notinit);
3517 return 0;
3518}
3519
3520STATIC int
3521xfs_bmap_btalloc_filestreams(
3522 struct xfs_bmalloca *ap,
3523 struct xfs_alloc_arg *args,
3524 xfs_extlen_t *blen)
3525{
3526 struct xfs_mount *mp = ap->ip->i_mount;
3527 xfs_agnumber_t ag;
3528 int notinit = 0;
3529 int error;
3530
3531 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3532 args->total = ap->total;
3533
3534 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3535 if (ag == NULLAGNUMBER)
3536 ag = 0;
3537
3538 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3539 if (error)
3540 return error;
3541
3542 if (*blen < args->maxlen) {
3543 error = xfs_filestream_new_ag(ap, &ag);
3544 if (error)
3545 return error;
3546
3547 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3548 &notinit);
3549 if (error)
3550 return error;
3551
3552 }
3553
3554 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003555
3556 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003557 * Set the failure fallback case to look in the selected AG as stream
3558 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003559 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003560 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003561 return 0;
3562}
3563
3564STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003565xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003566 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003567{
3568 xfs_mount_t *mp; /* mount point structure */
3569 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003570 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003571 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003572 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003573 xfs_alloc_arg_t args;
3574 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003575 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003576 int nullfb; /* true if ap->firstblock isn't set */
3577 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003578 int tryagain;
3579 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003580 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003581
Dave Chinnera99ebf42011-12-01 11:24:20 +00003582 ASSERT(ap->length);
3583
Nathan Scotta365bdd2006-03-14 13:34:16 +11003584 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003585
3586 /* stripe alignment for allocation is determined by mount parameters */
3587 stripe_align = 0;
3588 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3589 stripe_align = mp->m_swidth;
3590 else if (mp->m_dalign)
3591 stripe_align = mp->m_dalign;
3592
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003593 if (ap->flags & XFS_BMAPI_COWFORK)
3594 align = xfs_get_cowextsz_hint(ap->ip);
3595 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003596 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003597 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003598 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003599 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003600 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003601 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003602 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003603 }
Dave Chinner33177f052013-12-12 16:34:36 +11003604
3605
Dave Chinner0937e0f2011-09-18 20:40:57 +00003606 nullfb = *ap->firstblock == NULLFSBLOCK;
3607 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003608 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003609 if (xfs_alloc_is_userdata(ap->datatype) &&
3610 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003611 ag = xfs_filestream_lookup_ag(ap->ip);
3612 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003613 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003614 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003615 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003616 }
3617 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003618 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003619
3620 xfs_bmap_adjacent(ap);
3621
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003623 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 * it's in the right allocation group.
3625 */
Dave Chinner3a756672011-09-18 20:40:58 +00003626 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 ;
3628 else
Dave Chinner3a756672011-09-18 20:40:58 +00003629 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 * Normal allocation, done through xfs_alloc_vextent.
3632 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003633 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003634 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003635 args.tp = ap->tp;
3636 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003637 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003638 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003639
3640 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003641 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003642 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003643 blen = 0;
3644 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003645 /*
3646 * Search for an allocation group with a single extent large
3647 * enough for the request. If one isn't found, then adjust
3648 * the minimum allocation size to the largest space found.
3649 */
Dave Chinner292378e2016-09-26 08:21:28 +10003650 if (xfs_alloc_is_userdata(ap->datatype) &&
3651 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003652 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3653 else
3654 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003655 if (error)
3656 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003657 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003658 if (xfs_inode_is_filestream(ap->ip))
3659 args.type = XFS_ALLOCTYPE_FIRST_AG;
3660 else
3661 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003662 args.total = args.minlen = ap->minlen;
3663 } else {
3664 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3665 args.total = ap->total;
3666 args.minlen = ap->minlen;
3667 }
David Chinner957d0eb2007-06-18 16:50:37 +10003668 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003669 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003670 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003671 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003672 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003673 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003674 args.prod = 1;
3675 args.mod = 0;
3676 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003677 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003678 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003679 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3680 }
3681 /*
3682 * If we are not low on available data blocks, and the
3683 * underlying logical volume manager is a stripe, and
3684 * the file offset is zero then try to allocate data
3685 * blocks on stripe unit boundary.
3686 * NOTE: ap->aeof is only set if the allocation length
3687 * is >= the stripe unit and the allocation offset is
3688 * at the end of file.
3689 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003690 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003691 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003692 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003693 atype = args.type;
3694 isaligned = 1;
3695 /*
3696 * Adjust for alignment
3697 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003698 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003699 args.minlen = blen - args.alignment;
3700 args.minalignslop = 0;
3701 } else {
3702 /*
3703 * First try an exact bno allocation.
3704 * If it fails then do a near or start bno
3705 * allocation with alignment turned on.
3706 */
3707 atype = args.type;
3708 tryagain = 1;
3709 args.type = XFS_ALLOCTYPE_THIS_BNO;
3710 args.alignment = 1;
3711 /*
3712 * Compute the minlen+alignment for the
3713 * next case. Set slop so that the value
3714 * of minlen+alignment+slop doesn't go up
3715 * between the calls.
3716 */
Dave Chinner33177f052013-12-12 16:34:36 +11003717 if (blen > stripe_align && blen <= args.maxlen)
3718 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003719 else
3720 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003721 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003722 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003723 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003724 args.minlen - 1;
3725 else
3726 args.minalignslop = 0;
3727 }
3728 } else {
3729 args.alignment = 1;
3730 args.minalignslop = 0;
3731 }
3732 args.minleft = ap->minleft;
3733 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003734 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003735 args.datatype = ap->datatype;
3736 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003737 args.ip = ap->ip;
3738
3739 error = xfs_alloc_vextent(&args);
3740 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003741 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003742
Nathan Scotta365bdd2006-03-14 13:34:16 +11003743 if (tryagain && args.fsbno == NULLFSBLOCK) {
3744 /*
3745 * Exact allocation failed. Now try with alignment
3746 * turned on.
3747 */
3748 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003749 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003750 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003751 args.minlen = nextminlen;
3752 args.minalignslop = 0;
3753 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 if ((error = xfs_alloc_vextent(&args)))
3755 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003756 }
3757 if (isaligned && args.fsbno == NULLFSBLOCK) {
3758 /*
3759 * allocation failed, so turn off alignment and
3760 * try again.
3761 */
3762 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003763 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003764 args.alignment = 0;
3765 if ((error = xfs_alloc_vextent(&args)))
3766 return error;
3767 }
3768 if (args.fsbno == NULLFSBLOCK && nullfb &&
3769 args.minlen > ap->minlen) {
3770 args.minlen = ap->minlen;
3771 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003772 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003773 if ((error = xfs_alloc_vextent(&args)))
3774 return error;
3775 }
3776 if (args.fsbno == NULLFSBLOCK && nullfb) {
3777 args.fsbno = 0;
3778 args.type = XFS_ALLOCTYPE_FIRST_AG;
3779 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003780 if ((error = xfs_alloc_vextent(&args)))
3781 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003782 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003783 }
3784 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003785 /*
3786 * check the allocation happened at the same or higher AG than
3787 * the first block that was allocated.
3788 */
3789 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003790 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3791 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003792
Dave Chinner3a756672011-09-18 20:40:58 +00003793 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003794 if (*ap->firstblock == NULLFSBLOCK)
3795 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003796 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003797 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003798 if (!(ap->flags & XFS_BMAPI_COWFORK))
3799 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003800 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3801 if (ap->wasdel)
3802 ap->ip->i_delayed_blks -= args.len;
3803 /*
3804 * Adjust the disk quota also. This was reserved
3805 * earlier.
3806 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003807 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003808 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3809 XFS_TRANS_DQ_BCOUNT,
3810 (long) args.len);
3811 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003812 ap->blkno = NULLFSBLOCK;
3813 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 }
3815 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003816}
3817
3818/*
3819 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3820 * It figures out where to ask the underlying allocator to put the new extent.
3821 */
3822STATIC int
3823xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003824 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003825{
Dave Chinner292378e2016-09-26 08:21:28 +10003826 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3827 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003828 return xfs_bmap_rtalloc(ap);
3829 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830}
3831
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003832/* Trim extent to fit a logical block range. */
3833void
3834xfs_trim_extent(
3835 struct xfs_bmbt_irec *irec,
3836 xfs_fileoff_t bno,
3837 xfs_filblks_t len)
3838{
3839 xfs_fileoff_t distance;
3840 xfs_fileoff_t end = bno + len;
3841
3842 if (irec->br_startoff + irec->br_blockcount <= bno ||
3843 irec->br_startoff >= end) {
3844 irec->br_blockcount = 0;
3845 return;
3846 }
3847
3848 if (irec->br_startoff < bno) {
3849 distance = bno - irec->br_startoff;
3850 if (isnullstartblock(irec->br_startblock))
3851 irec->br_startblock = DELAYSTARTBLOCK;
3852 if (irec->br_startblock != DELAYSTARTBLOCK &&
3853 irec->br_startblock != HOLESTARTBLOCK)
3854 irec->br_startblock += distance;
3855 irec->br_startoff += distance;
3856 irec->br_blockcount -= distance;
3857 }
3858
3859 if (end < irec->br_startoff + irec->br_blockcount) {
3860 distance = irec->br_startoff + irec->br_blockcount - end;
3861 irec->br_blockcount -= distance;
3862 }
3863}
3864
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003866 * Trim the returned map to the required bounds
3867 */
3868STATIC void
3869xfs_bmapi_trim_map(
3870 struct xfs_bmbt_irec *mval,
3871 struct xfs_bmbt_irec *got,
3872 xfs_fileoff_t *bno,
3873 xfs_filblks_t len,
3874 xfs_fileoff_t obno,
3875 xfs_fileoff_t end,
3876 int n,
3877 int flags)
3878{
3879 if ((flags & XFS_BMAPI_ENTIRE) ||
3880 got->br_startoff + got->br_blockcount <= obno) {
3881 *mval = *got;
3882 if (isnullstartblock(got->br_startblock))
3883 mval->br_startblock = DELAYSTARTBLOCK;
3884 return;
3885 }
3886
3887 if (obno > *bno)
3888 *bno = obno;
3889 ASSERT((*bno >= obno) || (n == 0));
3890 ASSERT(*bno < end);
3891 mval->br_startoff = *bno;
3892 if (isnullstartblock(got->br_startblock))
3893 mval->br_startblock = DELAYSTARTBLOCK;
3894 else
3895 mval->br_startblock = got->br_startblock +
3896 (*bno - got->br_startoff);
3897 /*
3898 * Return the minimum of what we got and what we asked for for
3899 * the length. We can use the len variable here because it is
3900 * modified below and we could have been there before coming
3901 * here if the first part of the allocation didn't overlap what
3902 * was asked for.
3903 */
3904 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3905 got->br_blockcount - (*bno - got->br_startoff));
3906 mval->br_state = got->br_state;
3907 ASSERT(mval->br_blockcount <= len);
3908 return;
3909}
3910
3911/*
3912 * Update and validate the extent map to return
3913 */
3914STATIC void
3915xfs_bmapi_update_map(
3916 struct xfs_bmbt_irec **map,
3917 xfs_fileoff_t *bno,
3918 xfs_filblks_t *len,
3919 xfs_fileoff_t obno,
3920 xfs_fileoff_t end,
3921 int *n,
3922 int flags)
3923{
3924 xfs_bmbt_irec_t *mval = *map;
3925
3926 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3927 ((mval->br_startoff + mval->br_blockcount) <= end));
3928 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3929 (mval->br_startoff < obno));
3930
3931 *bno = mval->br_startoff + mval->br_blockcount;
3932 *len = end - *bno;
3933 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3934 /* update previous map with new information */
3935 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3936 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3937 ASSERT(mval->br_state == mval[-1].br_state);
3938 mval[-1].br_blockcount = mval->br_blockcount;
3939 mval[-1].br_state = mval->br_state;
3940 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3941 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3942 mval[-1].br_startblock != HOLESTARTBLOCK &&
3943 mval->br_startblock == mval[-1].br_startblock +
3944 mval[-1].br_blockcount &&
3945 ((flags & XFS_BMAPI_IGSTATE) ||
3946 mval[-1].br_state == mval->br_state)) {
3947 ASSERT(mval->br_startoff ==
3948 mval[-1].br_startoff + mval[-1].br_blockcount);
3949 mval[-1].br_blockcount += mval->br_blockcount;
3950 } else if (*n > 0 &&
3951 mval->br_startblock == DELAYSTARTBLOCK &&
3952 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3953 mval->br_startoff ==
3954 mval[-1].br_startoff + mval[-1].br_blockcount) {
3955 mval[-1].br_blockcount += mval->br_blockcount;
3956 mval[-1].br_state = mval->br_state;
3957 } else if (!((*n == 0) &&
3958 ((mval->br_startoff + mval->br_blockcount) <=
3959 obno))) {
3960 mval++;
3961 (*n)++;
3962 }
3963 *map = mval;
3964}
3965
3966/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003967 * Map file blocks to filesystem blocks without allocation.
3968 */
3969int
3970xfs_bmapi_read(
3971 struct xfs_inode *ip,
3972 xfs_fileoff_t bno,
3973 xfs_filblks_t len,
3974 struct xfs_bmbt_irec *mval,
3975 int *nmap,
3976 int flags)
3977{
3978 struct xfs_mount *mp = ip->i_mount;
3979 struct xfs_ifork *ifp;
3980 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003981 xfs_fileoff_t obno;
3982 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003983 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003984 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003985 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003986 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003987 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003988
3989 ASSERT(*nmap >= 1);
3990 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003991 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003992 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003993
3994 if (unlikely(XFS_TEST_ERROR(
3995 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3996 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
3997 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
3998 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003999 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004000 }
4001
4002 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004003 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004004
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004005 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004006
4007 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004008
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004009 /* No CoW fork? Return a hole. */
4010 if (whichfork == XFS_COW_FORK && !ifp) {
4011 mval->br_startoff = bno;
4012 mval->br_startblock = HOLESTARTBLOCK;
4013 mval->br_blockcount = len;
4014 mval->br_state = XFS_EXT_NORM;
4015 *nmap = 1;
4016 return 0;
4017 }
4018
Dave Chinner5c8ed202011-09-18 20:40:45 +00004019 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4020 error = xfs_iread_extents(NULL, ip, whichfork);
4021 if (error)
4022 return error;
4023 }
4024
Christoph Hellwig334f3422016-11-24 11:39:43 +11004025 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4026 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004027 end = bno + len;
4028 obno = bno;
4029
4030 while (bno < end && n < *nmap) {
4031 /* Reading past eof, act as though there's a hole up to end. */
4032 if (eof)
4033 got.br_startoff = end;
4034 if (got.br_startoff > bno) {
4035 /* Reading in a hole. */
4036 mval->br_startoff = bno;
4037 mval->br_startblock = HOLESTARTBLOCK;
4038 mval->br_blockcount =
4039 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4040 mval->br_state = XFS_EXT_NORM;
4041 bno += mval->br_blockcount;
4042 len -= mval->br_blockcount;
4043 mval++;
4044 n++;
4045 continue;
4046 }
4047
4048 /* set up the extent map to return. */
4049 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4050 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4051
4052 /* If we're done, stop now. */
4053 if (bno >= end || n >= *nmap)
4054 break;
4055
4056 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004057 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4058 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004059 }
4060 *nmap = n;
4061 return 0;
4062}
4063
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004064/*
4065 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4066 * global pool and the extent inserted into the inode in-core extent tree.
4067 *
4068 * On entry, got refers to the first extent beyond the offset of the extent to
4069 * allocate or eof is specified if no such extent exists. On return, got refers
4070 * to the extent record that was inserted to the inode fork.
4071 *
4072 * Note that the allocated extent may have been merged with contiguous extents
4073 * during insertion into the inode fork. Thus, got does not reflect the current
4074 * state of the inode fork on return. If necessary, the caller can use lastx to
4075 * look up the updated record in the inode fork.
4076 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10004077int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004078xfs_bmapi_reserve_delalloc(
4079 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004080 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004081 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004082 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004083 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004084 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004085 xfs_extnum_t *lastx,
4086 int eof)
4087{
4088 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004089 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004090 xfs_extlen_t alen;
4091 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004092 char rt = XFS_IS_REALTIME_INODE(ip);
4093 xfs_extlen_t extsz;
4094 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004095 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004096
Brian Foster974ae922016-11-28 14:57:42 +11004097 /*
4098 * Cap the alloc length. Keep track of prealloc so we know whether to
4099 * tag the inode before we return.
4100 */
4101 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004102 if (!eof)
4103 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004104 if (prealloc && alen >= len)
4105 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004106
4107 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004108 if (whichfork == XFS_COW_FORK)
4109 extsz = xfs_get_cowextsz_hint(ip);
4110 else
4111 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004112 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004113 struct xfs_bmbt_irec prev;
4114
4115 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4116 prev.br_startoff = NULLFILEOFF;
4117
4118 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004119 1, 0, &aoff, &alen);
4120 ASSERT(!error);
4121 }
4122
4123 if (rt)
4124 extsz = alen / mp->m_sb.sb_rextsize;
4125
4126 /*
4127 * Make a transaction-less quota reservation for delayed allocation
4128 * blocks. This number gets adjusted later. We return if we haven't
4129 * allocated blocks already inside this loop.
4130 */
4131 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4132 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4133 if (error)
4134 return error;
4135
4136 /*
4137 * Split changing sb for alen and indlen since they could be coming
4138 * from different places.
4139 */
4140 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4141 ASSERT(indlen > 0);
4142
4143 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004144 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004145 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004146 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004147 }
4148
4149 if (error)
4150 goto out_unreserve_quota;
4151
Dave Chinner0d485ad2015-02-23 21:22:03 +11004152 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004153 if (error)
4154 goto out_unreserve_blocks;
4155
4156
4157 ip->i_delayed_blks += alen;
4158
4159 got->br_startoff = aoff;
4160 got->br_startblock = nullstartblock(indlen);
4161 got->br_blockcount = alen;
4162 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004163
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004164 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004165
Brian Foster974ae922016-11-28 14:57:42 +11004166 /*
4167 * Tag the inode if blocks were preallocated. Note that COW fork
4168 * preallocation can occur at the start or end of the extent, even when
4169 * prealloc == 0, so we must also check the aligned offset and length.
4170 */
4171 if (whichfork == XFS_DATA_FORK && prealloc)
4172 xfs_inode_set_eofblocks_tag(ip);
4173 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4174 xfs_inode_set_cowblocks_tag(ip);
4175
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004176 return 0;
4177
4178out_unreserve_blocks:
4179 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004180 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004181 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004182 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004183out_unreserve_quota:
4184 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004185 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004186 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4187 return error;
4188}
4189
Dave Chinnercf11da92014-07-15 07:08:24 +10004190static int
4191xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004192 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004193{
4194 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004195 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004196 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004197 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004198 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004199
Dave Chinnera99ebf42011-12-01 11:24:20 +00004200 ASSERT(bma->length > 0);
4201
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004202 /*
4203 * For the wasdelay case, we could also just allocate the stuff asked
4204 * for in this bmap call but that wouldn't be as good.
4205 */
4206 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004207 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4208 bma->offset = bma->got.br_startoff;
Christoph Hellwig0e8d6302016-11-24 11:40:32 +11004209 if (bma->idx) {
Dave Chinnere0c3da52011-09-18 20:41:01 +00004210 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004211 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004212 }
4213 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004214 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004215 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004216 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004217 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004218 }
4219
4220 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004221 * Set the data type being allocated. For the data fork, the first data
4222 * in the file is treated differently to all other allocations. For the
4223 * attribute fork, we only need to ensure the allocated range is not on
4224 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004225 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004226 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004227 bma->datatype = XFS_ALLOC_NOBUSY;
4228 if (whichfork == XFS_DATA_FORK) {
4229 if (bma->offset == 0)
4230 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4231 else
4232 bma->datatype |= XFS_ALLOC_USERDATA;
4233 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004234 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004235 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004236 }
4237
Dave Chinnere04426b2012-10-05 11:06:59 +10004238 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004239
4240 /*
4241 * Only want to do the alignment at the eof if it is userdata and
4242 * allocation length is larger than a stripe unit.
4243 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004244 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004245 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004246 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004247 if (error)
4248 return error;
4249 }
4250
4251 error = xfs_bmap_alloc(bma);
4252 if (error)
4253 return error;
4254
Dave Chinner29c8d172011-09-18 20:41:00 +00004255 if (bma->cur)
4256 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004257 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004258 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004259 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4260 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4261 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004262 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004263 }
4264 /*
4265 * Bump the number of extents we've allocated
4266 * in this call.
4267 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004268 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004269
Dave Chinner29c8d172011-09-18 20:41:00 +00004270 if (bma->cur)
4271 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004272 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4273
Dave Chinner963c30c2011-09-18 20:40:59 +00004274 bma->got.br_startoff = bma->offset;
4275 bma->got.br_startblock = bma->blkno;
4276 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004277 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004278
4279 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004280 * In the data fork, a wasdelay extent has been initialized, so
4281 * shouldn't be flagged as unwritten.
4282 *
4283 * For the cow fork, however, we convert delalloc reservations
4284 * (extents allocated for speculative preallocation) to
4285 * allocated unwritten extents, and only convert the unwritten
4286 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004287 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004288 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4289 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004290 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004291 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004292
Christoph Hellwigc6534242011-09-18 20:41:05 +00004293 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004294 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004295 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004296 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4297 whichfork, &bma->idx, &bma->cur, &bma->got,
4298 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004299
Christoph Hellwigc315c902011-09-18 20:41:02 +00004300 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004301 if (error)
4302 return error;
4303
4304 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004305 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4306 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4307 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004308 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004309 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004310
Dave Chinner963c30c2011-09-18 20:40:59 +00004311 ASSERT(bma->got.br_startoff <= bma->offset);
4312 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4313 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004314 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4315 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004316 return 0;
4317}
4318
Dave Chinnerb447fe52011-09-18 20:40:51 +00004319STATIC int
4320xfs_bmapi_convert_unwritten(
4321 struct xfs_bmalloca *bma,
4322 struct xfs_bmbt_irec *mval,
4323 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004324 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004325{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004326 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004327 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004328 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004329 int error;
4330
Dave Chinnerb447fe52011-09-18 20:40:51 +00004331 /* check if we need to do unwritten->real conversion */
4332 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4333 (flags & XFS_BMAPI_PREALLOC))
4334 return 0;
4335
4336 /* check if we need to do real->unwritten conversion */
4337 if (mval->br_state == XFS_EXT_NORM &&
4338 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4339 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4340 return 0;
4341
4342 /*
4343 * Modify (by adding) the state flag, if writing.
4344 */
4345 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004346 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4347 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004348 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004349 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004350 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004351 }
4352 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4353 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4354
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004355 /*
4356 * Before insertion into the bmbt, zero the range being converted
4357 * if required.
4358 */
4359 if (flags & XFS_BMAPI_ZERO) {
4360 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4361 mval->br_blockcount);
4362 if (error)
4363 return error;
4364 }
4365
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004366 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4367 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004368 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004369 /*
4370 * Log the inode core unconditionally in the unwritten extent conversion
4371 * path because the conversion might not have done so (e.g., if the
4372 * extent count hasn't changed). We need to make sure the inode is dirty
4373 * in the transaction for the sake of fsync(), even if nothing has
4374 * changed, because fsync() will not force the log for this transaction
4375 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004376 *
4377 * Note: If we're only converting cow fork extents, there aren't
4378 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004379 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004380 if (whichfork != XFS_COW_FORK)
4381 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004382 if (error)
4383 return error;
4384
4385 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004386 * Update our extent pointer, given that
4387 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4388 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004389 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004390 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004391
4392 /*
4393 * We may have combined previously unwritten space with written space,
4394 * so generate another request.
4395 */
4396 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004397 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004398 return 0;
4399}
4400
Christoph Hellwig44032802011-09-18 20:40:48 +00004401/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004402 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4403 * extent state if necessary. Details behaviour is controlled by the flags
4404 * parameter. Only allocates blocks from a single allocation group, to avoid
4405 * locking problems.
4406 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 * The returned value in "firstblock" from the first call in a transaction
4408 * must be remembered and presented to subsequent calls in "firstblock".
4409 * An upper bound for the number of blocks to be allocated is supplied to
4410 * the first call in "total"; if no allocation group has that many free
4411 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4412 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004413int
4414xfs_bmapi_write(
4415 struct xfs_trans *tp, /* transaction pointer */
4416 struct xfs_inode *ip, /* incore inode */
4417 xfs_fileoff_t bno, /* starting file offs. mapped */
4418 xfs_filblks_t len, /* length to map in file */
4419 int flags, /* XFS_BMAPI_... */
4420 xfs_fsblock_t *firstblock, /* first allocated block
4421 controls a.g. for allocs */
4422 xfs_extlen_t total, /* total blocks needed */
4423 struct xfs_bmbt_irec *mval, /* output: map values */
4424 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004425 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004427 struct xfs_mount *mp = ip->i_mount;
4428 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004429 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004430 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004431 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004432 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004433 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004434 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004435 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004436
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004438 xfs_fileoff_t orig_bno; /* original block number value */
4439 int orig_flags; /* original flags arg value */
4440 xfs_filblks_t orig_len; /* original value of len arg */
4441 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4442 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
4444 orig_bno = bno;
4445 orig_len = len;
4446 orig_flags = flags;
4447 orig_mval = mval;
4448 orig_nmap = *nmap;
4449#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004450 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004451
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004453 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4454 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004455 ASSERT(tp != NULL ||
4456 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4457 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004458 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004459 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004460 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004461 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004462
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004463 /* zeroing is for currently only for data extents, not metadata */
4464 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4465 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4466 /*
4467 * we can allocate unwritten extents or pre-zero allocated blocks,
4468 * but it makes no sense to do both at once. This would result in
4469 * zeroing the unwritten extent twice, but it still being an
4470 * unwritten extent....
4471 */
4472 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4473 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4474
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 if (unlikely(XFS_TEST_ERROR(
4476 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004477 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004479 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004480 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004484 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004487
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004488 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004489
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004490 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004492 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004494 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004495 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004496 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004497 }
4498
4499 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4500 error = xfs_iread_extents(tp, ip, whichfork);
4501 if (error)
4502 goto error0;
4503 }
4504
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 n = 0;
4506 end = bno + len;
4507 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004508
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004509 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4510 eof = true;
4511 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4512 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004513 bma.tp = tp;
4514 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004515 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004516 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004517 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004518 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004519
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004521 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004522
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004523 /* in hole or beyoned EOF? */
4524 if (eof || bma.got.br_startoff > bno) {
4525 if (flags & XFS_BMAPI_DELALLOC) {
4526 /*
4527 * For the COW fork we can reasonably get a
4528 * request for converting an extent that races
4529 * with other threads already having converted
4530 * part of it, as there converting COW to
4531 * regular blocks is not protected using the
4532 * IOLOCK.
4533 */
4534 ASSERT(flags & XFS_BMAPI_COWFORK);
4535 if (!(flags & XFS_BMAPI_COWFORK)) {
4536 error = -EIO;
4537 goto error0;
4538 }
4539
4540 if (eof || bno >= end)
4541 break;
4542 } else {
4543 need_alloc = true;
4544 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004545 } else if (isnullstartblock(bma.got.br_startblock)) {
4546 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004547 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004548
4549 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 * First, deal with the hole before the allocated space
4551 * that we found, if any.
4552 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004553 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004554 bma.eof = eof;
4555 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4556 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004557 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004558 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004559
Dave Chinnera99ebf42011-12-01 11:24:20 +00004560 /*
4561 * There's a 32/64 bit type mismatch between the
4562 * allocation length request (which can be 64 bits in
4563 * length) and the bma length request, which is
4564 * xfs_extlen_t and therefore 32 bits. Hence we have to
4565 * check for 32-bit overflows and handle them here.
4566 */
4567 if (len > (xfs_filblks_t)MAXEXTLEN)
4568 bma.length = MAXEXTLEN;
4569 else
4570 bma.length = len;
4571
4572 ASSERT(len > 0);
4573 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004574 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 if (error)
4576 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004577 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004578 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004579
4580 /*
4581 * If this is a CoW allocation, record the data in
4582 * the refcount btree for orphan recovery.
4583 */
4584 if (whichfork == XFS_COW_FORK) {
4585 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4586 bma.blkno, bma.length);
4587 if (error)
4588 goto error0;
4589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004591
Dave Chinneraef9a892011-09-18 20:40:44 +00004592 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004593 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4594 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
Dave Chinnerb447fe52011-09-18 20:40:51 +00004596 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004597 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004598 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004599 continue;
4600 if (error)
4601 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
Dave Chinneraef9a892011-09-18 20:40:44 +00004603 /* update the extent map to return */
4604 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4605
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 /*
4607 * If we're done, stop now. Stop when we've allocated
4608 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4609 * the transaction may get too big.
4610 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004611 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004613
4614 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004615 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004616 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4617 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004620
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 /*
4622 * Transform from btree to extents, give it cur.
4623 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004624 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004625 int tmp_logflags = 0;
4626
Dave Chinner29c8d172011-09-18 20:41:00 +00004627 ASSERT(bma.cur);
4628 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004630 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 if (error)
4632 goto error0;
4633 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004634
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004636 XFS_IFORK_NEXTENTS(ip, whichfork) >
4637 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639error0:
4640 /*
4641 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004642 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004644 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004646 bma.logflags &= ~xfs_ilog_fext(whichfork);
4647 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004649 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 /*
4651 * Log whatever the flags say, even if error. Otherwise we might miss
4652 * detecting a case where the data is changed, there's an error,
4653 * and it's not logged so we don't shutdown when we should.
4654 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004655 if (bma.logflags)
4656 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004657
Dave Chinner29c8d172011-09-18 20:41:00 +00004658 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 if (!error) {
4660 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004661 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004663 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004664 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004666 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4668 }
4669 if (!error)
4670 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4671 orig_nmap, *nmap);
4672 return error;
4673}
4674
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004675static int
4676xfs_bmapi_remap(
4677 struct xfs_trans *tp,
4678 struct xfs_inode *ip,
4679 xfs_fileoff_t bno,
4680 xfs_filblks_t len,
4681 xfs_fsblock_t startblock,
4682 struct xfs_defer_ops *dfops)
4683{
4684 struct xfs_mount *mp = ip->i_mount;
4685 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4686 struct xfs_btree_cur *cur = NULL;
4687 xfs_fsblock_t firstblock = NULLFSBLOCK;
4688 struct xfs_bmbt_irec got;
4689 xfs_extnum_t idx;
4690 int logflags = 0, error;
4691
4692 ASSERT(len > 0);
4693 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4694 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4695
4696 if (unlikely(XFS_TEST_ERROR(
4697 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4698 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4699 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4700 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4701 return -EFSCORRUPTED;
4702 }
4703
4704 if (XFS_FORCED_SHUTDOWN(mp))
4705 return -EIO;
4706
4707 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4708 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4709 if (error)
4710 return error;
4711 }
4712
4713 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4714 /* make sure we only reflink into a hole. */
4715 ASSERT(got.br_startoff > bno);
4716 ASSERT(got.br_startoff - bno >= len);
4717 }
4718
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004719 ip->i_d.di_nblocks += len;
4720 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004721
4722 if (ifp->if_flags & XFS_IFBROOT) {
4723 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4724 cur->bc_private.b.firstblock = firstblock;
4725 cur->bc_private.b.dfops = dfops;
4726 cur->bc_private.b.flags = 0;
4727 }
4728
4729 got.br_startoff = bno;
4730 got.br_startblock = startblock;
4731 got.br_blockcount = len;
4732 got.br_state = XFS_EXT_NORM;
4733
4734 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4735 &got, &firstblock, dfops, &logflags);
4736 if (error)
4737 goto error0;
4738
4739 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4740 int tmp_logflags = 0;
4741
4742 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4743 &tmp_logflags, XFS_DATA_FORK);
4744 logflags |= tmp_logflags;
4745 }
4746
4747error0:
4748 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4749 logflags &= ~XFS_ILOG_DEXT;
4750 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4751 logflags &= ~XFS_ILOG_DBROOT;
4752
4753 if (logflags)
4754 xfs_trans_log_inode(tp, ip, logflags);
4755 if (cur) {
4756 xfs_btree_del_cursor(cur,
4757 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4758 }
4759 return error;
4760}
4761
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004763 * When a delalloc extent is split (e.g., due to a hole punch), the original
4764 * indlen reservation must be shared across the two new extents that are left
4765 * behind.
4766 *
4767 * Given the original reservation and the worst case indlen for the two new
4768 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004769 * reservation fairly across the two new extents. If necessary, steal available
4770 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4771 * ores == 1). The number of stolen blocks is returned. The availability and
4772 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004773 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004774static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004775xfs_bmap_split_indlen(
4776 xfs_filblks_t ores, /* original res. */
4777 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004778 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4779 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004780{
4781 xfs_filblks_t len1 = *indlen1;
4782 xfs_filblks_t len2 = *indlen2;
4783 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004784 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004785 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004786
4787 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004788 * Steal as many blocks as we can to try and satisfy the worst case
4789 * indlen for both new extents.
4790 */
Brian Foster75d65362017-02-13 22:48:30 -08004791 if (ores < nres && avail)
4792 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4793 ores += stolen;
4794
4795 /* nothing else to do if we've satisfied the new reservation */
4796 if (ores >= nres)
4797 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004798
4799 /*
Brian Foster75d65362017-02-13 22:48:30 -08004800 * We can't meet the total required reservation for the two extents.
4801 * Calculate the percent of the overall shortage between both extents
4802 * and apply this percentage to each of the requested indlen values.
4803 * This distributes the shortage fairly and reduces the chances that one
4804 * of the two extents is left with nothing when extents are repeatedly
4805 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004806 */
Brian Foster75d65362017-02-13 22:48:30 -08004807 resfactor = (ores * 100);
4808 do_div(resfactor, nres);
4809 len1 *= resfactor;
4810 do_div(len1, 100);
4811 len2 *= resfactor;
4812 do_div(len2, 100);
4813 ASSERT(len1 + len2 <= ores);
4814 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4815
4816 /*
4817 * Hand out the remainder to each extent. If one of the two reservations
4818 * is zero, we want to make sure that one gets a block first. The loop
4819 * below starts with len1, so hand len2 a block right off the bat if it
4820 * is zero.
4821 */
4822 ores -= (len1 + len2);
4823 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4824 if (ores && !len2 && *indlen2) {
4825 len2++;
4826 ores--;
4827 }
4828 while (ores) {
4829 if (len1 < *indlen1) {
4830 len1++;
4831 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004832 }
Brian Foster75d65362017-02-13 22:48:30 -08004833 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004834 break;
Brian Foster75d65362017-02-13 22:48:30 -08004835 if (len2 < *indlen2) {
4836 len2++;
4837 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004838 }
4839 }
4840
4841 *indlen1 = len1;
4842 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004843
4844 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004845}
4846
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004847int
4848xfs_bmap_del_extent_delay(
4849 struct xfs_inode *ip,
4850 int whichfork,
4851 xfs_extnum_t *idx,
4852 struct xfs_bmbt_irec *got,
4853 struct xfs_bmbt_irec *del)
4854{
4855 struct xfs_mount *mp = ip->i_mount;
4856 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4857 struct xfs_bmbt_irec new;
4858 int64_t da_old, da_new, da_diff = 0;
4859 xfs_fileoff_t del_endoff, got_endoff;
4860 xfs_filblks_t got_indlen, new_indlen, stolen;
4861 int error = 0, state = 0;
4862 bool isrt;
4863
4864 XFS_STATS_INC(mp, xs_del_exlist);
4865
4866 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4867 del_endoff = del->br_startoff + del->br_blockcount;
4868 got_endoff = got->br_startoff + got->br_blockcount;
4869 da_old = startblockval(got->br_startblock);
4870 da_new = 0;
4871
4872 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004873 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004874 ASSERT(del->br_blockcount > 0);
4875 ASSERT(got->br_startoff <= del->br_startoff);
4876 ASSERT(got_endoff >= del_endoff);
4877
4878 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004879 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004880
4881 do_div(rtexts, mp->m_sb.sb_rextsize);
4882 xfs_mod_frextents(mp, rtexts);
4883 }
4884
4885 /*
4886 * Update the inode delalloc counter now and wait to update the
4887 * sb counters as we might have to borrow some blocks for the
4888 * indirect block accounting.
4889 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004890 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4891 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004892 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004893 if (error)
4894 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004895 ip->i_delayed_blks -= del->br_blockcount;
4896
4897 if (whichfork == XFS_COW_FORK)
4898 state |= BMAP_COWFORK;
4899
4900 if (got->br_startoff == del->br_startoff)
4901 state |= BMAP_LEFT_CONTIG;
4902 if (got_endoff == del_endoff)
4903 state |= BMAP_RIGHT_CONTIG;
4904
4905 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4906 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4907 /*
4908 * Matches the whole extent. Delete the entry.
4909 */
4910 xfs_iext_remove(ip, *idx, 1, state);
4911 --*idx;
4912 break;
4913 case BMAP_LEFT_CONTIG:
4914 /*
4915 * Deleting the first part of the extent.
4916 */
4917 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4918 got->br_startoff = del_endoff;
4919 got->br_blockcount -= del->br_blockcount;
4920 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4921 got->br_blockcount), da_old);
4922 got->br_startblock = nullstartblock((int)da_new);
4923 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4924 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4925 break;
4926 case BMAP_RIGHT_CONTIG:
4927 /*
4928 * Deleting the last part of the extent.
4929 */
4930 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4931 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4932 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4933 got->br_blockcount), da_old);
4934 got->br_startblock = nullstartblock((int)da_new);
4935 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4936 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4937 break;
4938 case 0:
4939 /*
4940 * Deleting the middle of the extent.
4941 *
4942 * Distribute the original indlen reservation across the two new
4943 * extents. Steal blocks from the deleted extent if necessary.
4944 * Stealing blocks simply fudges the fdblocks accounting below.
4945 * Warn if either of the new indlen reservations is zero as this
4946 * can lead to delalloc problems.
4947 */
4948 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4949
4950 got->br_blockcount = del->br_startoff - got->br_startoff;
4951 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4952
4953 new.br_blockcount = got_endoff - del_endoff;
4954 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4955
4956 WARN_ON_ONCE(!got_indlen || !new_indlen);
4957 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4958 del->br_blockcount);
4959
4960 got->br_startblock = nullstartblock((int)got_indlen);
4961 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4962 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4963
4964 new.br_startoff = del_endoff;
4965 new.br_state = got->br_state;
4966 new.br_startblock = nullstartblock((int)new_indlen);
4967
4968 ++*idx;
4969 xfs_iext_insert(ip, *idx, 1, &new, state);
4970
4971 da_new = got_indlen + new_indlen - stolen;
4972 del->br_blockcount -= stolen;
4973 break;
4974 }
4975
4976 ASSERT(da_old >= da_new);
4977 da_diff = da_old - da_new;
4978 if (!isrt)
4979 da_diff += del->br_blockcount;
4980 if (da_diff)
4981 xfs_mod_fdblocks(mp, da_diff, false);
4982 return error;
4983}
4984
4985void
4986xfs_bmap_del_extent_cow(
4987 struct xfs_inode *ip,
4988 xfs_extnum_t *idx,
4989 struct xfs_bmbt_irec *got,
4990 struct xfs_bmbt_irec *del)
4991{
4992 struct xfs_mount *mp = ip->i_mount;
4993 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4994 struct xfs_bmbt_irec new;
4995 xfs_fileoff_t del_endoff, got_endoff;
4996 int state = BMAP_COWFORK;
4997
4998 XFS_STATS_INC(mp, xs_del_exlist);
4999
5000 del_endoff = del->br_startoff + del->br_blockcount;
5001 got_endoff = got->br_startoff + got->br_blockcount;
5002
5003 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11005004 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005005 ASSERT(del->br_blockcount > 0);
5006 ASSERT(got->br_startoff <= del->br_startoff);
5007 ASSERT(got_endoff >= del_endoff);
5008 ASSERT(!isnullstartblock(got->br_startblock));
5009
5010 if (got->br_startoff == del->br_startoff)
5011 state |= BMAP_LEFT_CONTIG;
5012 if (got_endoff == del_endoff)
5013 state |= BMAP_RIGHT_CONTIG;
5014
5015 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
5016 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
5017 /*
5018 * Matches the whole extent. Delete the entry.
5019 */
5020 xfs_iext_remove(ip, *idx, 1, state);
5021 --*idx;
5022 break;
5023 case BMAP_LEFT_CONTIG:
5024 /*
5025 * Deleting the first part of the extent.
5026 */
5027 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5028 got->br_startoff = del_endoff;
5029 got->br_blockcount -= del->br_blockcount;
5030 got->br_startblock = del->br_startblock + del->br_blockcount;
5031 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5032 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5033 break;
5034 case BMAP_RIGHT_CONTIG:
5035 /*
5036 * Deleting the last part of the extent.
5037 */
5038 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5039 got->br_blockcount -= del->br_blockcount;
5040 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5041 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5042 break;
5043 case 0:
5044 /*
5045 * Deleting the middle of the extent.
5046 */
5047 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5048 got->br_blockcount = del->br_startoff - got->br_startoff;
5049 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5050 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5051
5052 new.br_startoff = del_endoff;
5053 new.br_blockcount = got_endoff - del_endoff;
5054 new.br_state = got->br_state;
5055 new.br_startblock = del->br_startblock + del->br_blockcount;
5056
5057 ++*idx;
5058 xfs_iext_insert(ip, *idx, 1, &new, state);
5059 break;
5060 }
5061}
5062
Brian Fostera9bd24a2016-03-15 11:42:46 +11005063/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11005064 * Called by xfs_bmapi to update file extent records and the btree
5065 * after removing space (or undoing a delayed allocation).
5066 */
5067STATIC int /* error */
5068xfs_bmap_del_extent(
5069 xfs_inode_t *ip, /* incore inode pointer */
5070 xfs_trans_t *tp, /* current transaction pointer */
5071 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005072 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005073 xfs_btree_cur_t *cur, /* if null, not a btree */
5074 xfs_bmbt_irec_t *del, /* data to remove from extents */
5075 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005076 int whichfork, /* data or attr fork */
5077 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005078{
5079 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5080 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5081 xfs_fsblock_t del_endblock=0; /* first block past del */
5082 xfs_fileoff_t del_endoff; /* first offset past del */
5083 int delay; /* current block is delayed allocated */
5084 int do_fx; /* free extent at end of routine */
5085 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5086 int error; /* error return value */
5087 int flags; /* inode logging flags */
5088 xfs_bmbt_irec_t got; /* current extent entry */
5089 xfs_fileoff_t got_endoff; /* first offset past got */
5090 int i; /* temp state */
5091 xfs_ifork_t *ifp; /* inode fork pointer */
5092 xfs_mount_t *mp; /* mount structure */
5093 xfs_filblks_t nblks; /* quota/sb block count */
5094 xfs_bmbt_irec_t new; /* new record to be inserted */
5095 /* REFERENCED */
5096 uint qfield; /* quota field to update */
5097 xfs_filblks_t temp; /* for indirect length calculations */
5098 xfs_filblks_t temp2; /* for indirect length calculations */
5099 int state = 0;
5100
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005101 mp = ip->i_mount;
5102 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005103
5104 if (whichfork == XFS_ATTR_FORK)
5105 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005106 else if (whichfork == XFS_COW_FORK)
5107 state |= BMAP_COWFORK;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005108
Dave Chinner9e5987a72013-02-25 12:31:26 +11005109 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005110 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005111 ASSERT(del->br_blockcount > 0);
5112 ep = xfs_iext_get_ext(ifp, *idx);
5113 xfs_bmbt_get_all(ep, &got);
5114 ASSERT(got.br_startoff <= del->br_startoff);
5115 del_endoff = del->br_startoff + del->br_blockcount;
5116 got_endoff = got.br_startoff + got.br_blockcount;
5117 ASSERT(got_endoff >= del_endoff);
5118 delay = isnullstartblock(got.br_startblock);
5119 ASSERT(isnullstartblock(del->br_startblock) == delay);
5120 flags = 0;
5121 qfield = 0;
5122 error = 0;
5123 /*
5124 * If deleting a real allocation, must free up the disk space.
5125 */
5126 if (!delay) {
5127 flags = XFS_ILOG_CORE;
5128 /*
5129 * Realtime allocation. Free it and record di_nblocks update.
5130 */
5131 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5132 xfs_fsblock_t bno;
5133 xfs_filblks_t len;
5134
5135 ASSERT(do_mod(del->br_blockcount,
5136 mp->m_sb.sb_rextsize) == 0);
5137 ASSERT(do_mod(del->br_startblock,
5138 mp->m_sb.sb_rextsize) == 0);
5139 bno = del->br_startblock;
5140 len = del->br_blockcount;
5141 do_div(bno, mp->m_sb.sb_rextsize);
5142 do_div(len, mp->m_sb.sb_rextsize);
5143 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5144 if (error)
5145 goto done;
5146 do_fx = 0;
5147 nblks = len * mp->m_sb.sb_rextsize;
5148 qfield = XFS_TRANS_DQ_RTBCOUNT;
5149 }
5150 /*
5151 * Ordinary allocation.
5152 */
5153 else {
5154 do_fx = 1;
5155 nblks = del->br_blockcount;
5156 qfield = XFS_TRANS_DQ_BCOUNT;
5157 }
5158 /*
5159 * Set up del_endblock and cur for later.
5160 */
5161 del_endblock = del->br_startblock + del->br_blockcount;
5162 if (cur) {
5163 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5164 got.br_startblock, got.br_blockcount,
5165 &i)))
5166 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005167 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005168 }
5169 da_old = da_new = 0;
5170 } else {
5171 da_old = startblockval(got.br_startblock);
5172 da_new = 0;
5173 nblks = 0;
5174 do_fx = 0;
5175 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005176
Dave Chinner9e5987a72013-02-25 12:31:26 +11005177 /*
5178 * Set flag value to use in switch statement.
5179 * Left-contig is 2, right-contig is 1.
5180 */
5181 switch (((got.br_startoff == del->br_startoff) << 1) |
5182 (got_endoff == del_endoff)) {
5183 case 3:
5184 /*
5185 * Matches the whole extent. Delete the entry.
5186 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005187 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005188 xfs_iext_remove(ip, *idx, 1,
5189 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5190 --*idx;
5191 if (delay)
5192 break;
5193
5194 XFS_IFORK_NEXT_SET(ip, whichfork,
5195 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5196 flags |= XFS_ILOG_CORE;
5197 if (!cur) {
5198 flags |= xfs_ilog_fext(whichfork);
5199 break;
5200 }
5201 if ((error = xfs_btree_delete(cur, &i)))
5202 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005203 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005204 break;
5205
5206 case 2:
5207 /*
5208 * Deleting the first part of the extent.
5209 */
5210 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5211 xfs_bmbt_set_startoff(ep, del_endoff);
5212 temp = got.br_blockcount - del->br_blockcount;
5213 xfs_bmbt_set_blockcount(ep, temp);
5214 if (delay) {
5215 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5216 da_old);
5217 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5218 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5219 da_new = temp;
5220 break;
5221 }
5222 xfs_bmbt_set_startblock(ep, del_endblock);
5223 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5224 if (!cur) {
5225 flags |= xfs_ilog_fext(whichfork);
5226 break;
5227 }
5228 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5229 got.br_blockcount - del->br_blockcount,
5230 got.br_state)))
5231 goto done;
5232 break;
5233
5234 case 1:
5235 /*
5236 * Deleting the last part of the extent.
5237 */
5238 temp = got.br_blockcount - del->br_blockcount;
5239 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5240 xfs_bmbt_set_blockcount(ep, temp);
5241 if (delay) {
5242 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5243 da_old);
5244 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5245 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5246 da_new = temp;
5247 break;
5248 }
5249 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5250 if (!cur) {
5251 flags |= xfs_ilog_fext(whichfork);
5252 break;
5253 }
5254 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5255 got.br_startblock,
5256 got.br_blockcount - del->br_blockcount,
5257 got.br_state)))
5258 goto done;
5259 break;
5260
5261 case 0:
5262 /*
5263 * Deleting the middle of the extent.
5264 */
5265 temp = del->br_startoff - got.br_startoff;
5266 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5267 xfs_bmbt_set_blockcount(ep, temp);
5268 new.br_startoff = del_endoff;
5269 temp2 = got_endoff - del_endoff;
5270 new.br_blockcount = temp2;
5271 new.br_state = got.br_state;
5272 if (!delay) {
5273 new.br_startblock = del_endblock;
5274 flags |= XFS_ILOG_CORE;
5275 if (cur) {
5276 if ((error = xfs_bmbt_update(cur,
5277 got.br_startoff,
5278 got.br_startblock, temp,
5279 got.br_state)))
5280 goto done;
5281 if ((error = xfs_btree_increment(cur, 0, &i)))
5282 goto done;
5283 cur->bc_rec.b = new;
5284 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10005285 if (error && error != -ENOSPC)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005286 goto done;
5287 /*
5288 * If get no-space back from btree insert,
5289 * it tried a split, and we have a zero
5290 * block reservation.
5291 * Fix up our state and return the error.
5292 */
Dave Chinner24513372014-06-25 14:58:08 +10005293 if (error == -ENOSPC) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11005294 /*
5295 * Reset the cursor, don't trust
5296 * it after any insert operation.
5297 */
5298 if ((error = xfs_bmbt_lookup_eq(cur,
5299 got.br_startoff,
5300 got.br_startblock,
5301 temp, &i)))
5302 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005303 XFS_WANT_CORRUPTED_GOTO(mp,
5304 i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005305 /*
5306 * Update the btree record back
5307 * to the original value.
5308 */
5309 if ((error = xfs_bmbt_update(cur,
5310 got.br_startoff,
5311 got.br_startblock,
5312 got.br_blockcount,
5313 got.br_state)))
5314 goto done;
5315 /*
5316 * Reset the extent record back
5317 * to the original value.
5318 */
5319 xfs_bmbt_set_blockcount(ep,
5320 got.br_blockcount);
5321 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005322 error = -ENOSPC;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005323 goto done;
5324 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005325 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005326 } else
5327 flags |= xfs_ilog_fext(whichfork);
5328 XFS_IFORK_NEXT_SET(ip, whichfork,
5329 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5330 } else {
Brian Fosterd34999c2016-03-15 11:42:47 +11005331 xfs_filblks_t stolen;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005332 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005333
5334 /*
5335 * Distribute the original indlen reservation across the
Brian Fosterd34999c2016-03-15 11:42:47 +11005336 * two new extents. Steal blocks from the deleted extent
5337 * if necessary. Stealing blocks simply fudges the
5338 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005339 */
5340 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5341 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c2016-03-15 11:42:47 +11005342 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5343 del->br_blockcount);
5344 da_new = temp + temp2 - stolen;
5345 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005346
5347 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11005348 * Set the reservation for each extent. Warn if either
5349 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005350 */
Brian Fosterd34999c2016-03-15 11:42:47 +11005351 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005352 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005353 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005354 }
5355 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5356 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5357 ++*idx;
5358 break;
5359 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005360
5361 /* remove reverse mapping */
5362 if (!delay) {
5363 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5364 if (error)
5365 goto done;
5366 }
5367
Dave Chinner9e5987a72013-02-25 12:31:26 +11005368 /*
5369 * If we need to, add to list of extents to delete.
5370 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005371 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005372 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5373 error = xfs_refcount_decrease_extent(mp, dfops, del);
5374 if (error)
5375 goto done;
5376 } else
5377 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5378 del->br_blockcount, NULL);
5379 }
5380
Dave Chinner9e5987a72013-02-25 12:31:26 +11005381 /*
5382 * Adjust inode # blocks in the file.
5383 */
5384 if (nblks)
5385 ip->i_d.di_nblocks -= nblks;
5386 /*
5387 * Adjust quota data.
5388 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005389 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005390 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5391
5392 /*
5393 * Account for change in delayed indirect blocks.
5394 * Nothing to do for disk quota accounting here.
5395 */
5396 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005397 if (da_old > da_new)
5398 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005399done:
5400 *logflagsp = flags;
5401 return error;
5402}
5403
5404/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 * Unmap (remove) blocks from a file.
5406 * If nexts is nonzero then the number of extents to remove is limited to
5407 * that value. If not all extents in the block range can be removed then
5408 * *done is set.
5409 */
5410int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005411__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 xfs_trans_t *tp, /* transaction pointer */
5413 struct xfs_inode *ip, /* incore inode */
5414 xfs_fileoff_t bno, /* starting offset to unmap */
Darrick J. Wong44535932016-10-03 09:11:29 -07005415 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 int flags, /* misc flags */
5417 xfs_extnum_t nexts, /* number of extents max */
5418 xfs_fsblock_t *firstblock, /* first allocated block
5419 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005420 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421{
5422 xfs_btree_cur_t *cur; /* bmap btree cursor */
5423 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 int error; /* error return value */
5425 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005426 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 xfs_ifork_t *ifp; /* inode fork pointer */
5428 int isrt; /* freeing in rt area */
5429 xfs_extnum_t lastx; /* last extent index used */
5430 int logflags; /* transaction logging flags */
5431 xfs_extlen_t mod; /* rt extent offset */
5432 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 xfs_fileoff_t start; /* first file offset deleted */
5434 int tmp_logflags; /* partial logging flags */
5435 int wasdel; /* was a delayed alloc extent */
5436 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005438 xfs_filblks_t len = *rlen; /* length to unmap in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005440 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5441
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005442 whichfork = xfs_bmapi_whichfork(flags);
5443 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 ifp = XFS_IFORK_PTR(ip, whichfork);
5445 if (unlikely(
5446 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5447 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5448 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5449 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005450 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 }
5452 mp = ip->i_mount;
5453 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005454 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005455
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005456 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 ASSERT(len > 0);
5458 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005459
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5461 (error = xfs_iread_extents(tp, ip, whichfork)))
5462 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005463 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005464 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 return 0;
5466 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005467 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005468 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 start = bno;
5470 bno = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005471
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 /*
5473 * Check to see if the given block number is past the end of the
5474 * file, back up to the last block if so...
5475 */
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005476 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5477 ASSERT(lastx > 0);
5478 xfs_iext_get_extent(ifp, --lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 bno = got.br_startoff + got.br_blockcount - 1;
5480 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005481
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 logflags = 0;
5483 if (ifp->if_flags & XFS_IFBROOT) {
5484 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005485 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005487 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 cur->bc_private.b.flags = 0;
5489 } else
5490 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005491
5492 if (isrt) {
5493 /*
5494 * Synchronize by locking the bitmap inode.
5495 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005496 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005497 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005498 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5499 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005500 }
5501
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 extno = 0;
5503 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5504 (nexts == 0 || extno < nexts)) {
5505 /*
5506 * Is the found extent after a hole in which bno lives?
5507 * Just back up to the previous extent, if so.
5508 */
5509 if (got.br_startoff > bno) {
5510 if (--lastx < 0)
5511 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005512 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 }
5514 /*
5515 * Is the last block of this extent before the range
5516 * we're supposed to delete? If so, we're done.
5517 */
5518 bno = XFS_FILEOFF_MIN(bno,
5519 got.br_startoff + got.br_blockcount - 1);
5520 if (bno < start)
5521 break;
5522 /*
5523 * Then deal with the (possibly delayed) allocated space
5524 * we found.
5525 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005527 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 if (got.br_startoff < start) {
5529 del.br_startoff = start;
5530 del.br_blockcount -= start - got.br_startoff;
5531 if (!wasdel)
5532 del.br_startblock += start - got.br_startoff;
5533 }
5534 if (del.br_startoff + del.br_blockcount > bno + 1)
5535 del.br_blockcount = bno + 1 - del.br_startoff;
5536 sum = del.br_startblock + del.br_blockcount;
5537 if (isrt &&
5538 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5539 /*
5540 * Realtime extent not lined up at the end.
5541 * The extent could have been split into written
5542 * and unwritten pieces, or we could just be
5543 * unmapping part of it. But we can't really
5544 * get rid of part of a realtime extent.
5545 */
5546 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005547 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 /*
5549 * This piece is unwritten, or we're not
5550 * using unwritten extents. Skip over it.
5551 */
5552 ASSERT(bno >= mod);
5553 bno -= mod > del.br_blockcount ?
5554 del.br_blockcount : mod;
5555 if (bno < got.br_startoff) {
5556 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005557 xfs_bmbt_get_all(xfs_iext_get_ext(
5558 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 }
5560 continue;
5561 }
5562 /*
5563 * It's written, turn it unwritten.
5564 * This is better than zeroing it.
5565 */
5566 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005567 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 /*
5569 * If this spans a realtime extent boundary,
5570 * chop it back to the start of the one we end at.
5571 */
5572 if (del.br_blockcount > mod) {
5573 del.br_startoff += del.br_blockcount - mod;
5574 del.br_startblock += del.br_blockcount - mod;
5575 del.br_blockcount = mod;
5576 }
5577 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005578 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005579 whichfork, &lastx, &cur, &del,
5580 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 if (error)
5582 goto error0;
5583 goto nodelete;
5584 }
5585 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5586 /*
5587 * Realtime extent is lined up at the end but not
5588 * at the front. We'll get rid of full extents if
5589 * we can.
5590 */
5591 mod = mp->m_sb.sb_rextsize - mod;
5592 if (del.br_blockcount > mod) {
5593 del.br_blockcount -= mod;
5594 del.br_startoff += mod;
5595 del.br_startblock += mod;
5596 } else if ((del.br_startoff == start &&
5597 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005598 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005599 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 /*
5601 * Can't make it unwritten. There isn't
5602 * a full extent here so just skip it.
5603 */
5604 ASSERT(bno >= del.br_blockcount);
5605 bno -= del.br_blockcount;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005606 if (got.br_startoff > bno && --lastx >= 0)
5607 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005608 continue;
5609 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005610 struct xfs_bmbt_irec prev;
5611
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612 /*
5613 * This one is already unwritten.
5614 * It must have a written left neighbor.
5615 * Unwrite the killed part of that one and
5616 * try again.
5617 */
5618 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005619 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005621 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 ASSERT(del.br_startblock ==
5623 prev.br_startblock + prev.br_blockcount);
5624 if (prev.br_startoff < start) {
5625 mod = start - prev.br_startoff;
5626 prev.br_blockcount -= mod;
5627 prev.br_startblock += mod;
5628 prev.br_startoff = start;
5629 }
5630 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005631 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005632 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005633 ip, whichfork, &lastx, &cur,
5634 &prev, firstblock, dfops,
5635 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 if (error)
5637 goto error0;
5638 goto nodelete;
5639 } else {
5640 ASSERT(del.br_state == XFS_EXT_NORM);
5641 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005642 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005643 ip, whichfork, &lastx, &cur,
5644 &del, firstblock, dfops,
5645 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 if (error)
5647 goto error0;
5648 goto nodelete;
5649 }
5650 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005651
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 /*
5653 * If it's the case where the directory code is running
5654 * with no block reservation, and the deleted block is in
5655 * the middle of its extent, and the resulting insert
5656 * of an extent would cause transformation to btree format,
5657 * then reject it. The calling code will then swap
5658 * blocks around instead.
5659 * We have to do this now, rather than waiting for the
5660 * conversion to btree format, since the transaction
5661 * will be dirty.
5662 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005663 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005665 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5666 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 del.br_startoff > got.br_startoff &&
5668 del.br_startoff + del.br_blockcount <
5669 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005670 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 goto error0;
5672 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005673
5674 /*
5675 * Unreserve quota and update realtime free space, if
5676 * appropriate. If delayed allocation, update the inode delalloc
5677 * counter now and wait to update the sb counters as
5678 * xfs_bmap_del_extent() might need to borrow some blocks.
5679 */
5680 if (wasdel) {
5681 ASSERT(startblockval(del.br_startblock) > 0);
5682 if (isrt) {
5683 xfs_filblks_t rtexts;
5684
5685 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5686 do_div(rtexts, mp->m_sb.sb_rextsize);
5687 xfs_mod_frextents(mp, (int64_t)rtexts);
5688 (void)xfs_trans_reserve_quota_nblks(NULL,
5689 ip, -((long)del.br_blockcount), 0,
5690 XFS_QMOPT_RES_RTBLKS);
5691 } else {
5692 (void)xfs_trans_reserve_quota_nblks(NULL,
5693 ip, -((long)del.br_blockcount), 0,
5694 XFS_QMOPT_RES_REGBLKS);
5695 }
5696 ip->i_delayed_blks -= del.br_blockcount;
5697 if (cur)
5698 cur->bc_private.b.flags |=
5699 XFS_BTCUR_BPRV_WASDEL;
5700 } else if (cur)
5701 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5702
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005703 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005704 &tmp_logflags, whichfork, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 logflags |= tmp_logflags;
5706 if (error)
5707 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005708
5709 if (!isrt && wasdel)
5710 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5711
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 bno = del.br_startoff - 1;
5713nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 /*
5715 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005718 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005719 xfs_iext_get_extent(ifp, lastx, &got);
5720 if (got.br_startoff > bno && --lastx >= 0)
5721 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 extno++;
5724 }
5725 }
Darrick J. Wong44535932016-10-03 09:11:29 -07005726 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5727 *rlen = 0;
5728 else
5729 *rlen = bno - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005730
Linus Torvalds1da177e2005-04-16 15:20:36 -07005731 /*
5732 * Convert to a btree if necessary.
5733 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005734 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005736 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 &cur, 0, &tmp_logflags, whichfork);
5738 logflags |= tmp_logflags;
5739 if (error)
5740 goto error0;
5741 }
5742 /*
5743 * transform from btree to extents, give it cur
5744 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005745 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 ASSERT(cur != NULL);
5747 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5748 whichfork);
5749 logflags |= tmp_logflags;
5750 if (error)
5751 goto error0;
5752 }
5753 /*
5754 * transform from extents to local?
5755 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 error = 0;
5757error0:
5758 /*
5759 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005760 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005762 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005764 logflags &= ~xfs_ilog_fext(whichfork);
5765 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005767 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 /*
5769 * Log inode even in the error case, if the transaction
5770 * is dirty we'll need to shut down the filesystem.
5771 */
5772 if (logflags)
5773 xfs_trans_log_inode(tp, ip, logflags);
5774 if (cur) {
5775 if (!error) {
5776 *firstblock = cur->bc_private.b.firstblock;
5777 cur->bc_private.b.allocated = 0;
5778 }
5779 xfs_btree_del_cursor(cur,
5780 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5781 }
5782 return error;
5783}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005784
Darrick J. Wong44535932016-10-03 09:11:29 -07005785/* Unmap a range of a file. */
5786int
5787xfs_bunmapi(
5788 xfs_trans_t *tp,
5789 struct xfs_inode *ip,
5790 xfs_fileoff_t bno,
5791 xfs_filblks_t len,
5792 int flags,
5793 xfs_extnum_t nexts,
5794 xfs_fsblock_t *firstblock,
5795 struct xfs_defer_ops *dfops,
5796 int *done)
5797{
5798 int error;
5799
5800 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5801 dfops);
5802 *done = (len == 0);
5803 return error;
5804}
5805
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005806/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005807 * Determine whether an extent shift can be accomplished by a merge with the
5808 * extent that precedes the target hole of the shift.
5809 */
5810STATIC bool
5811xfs_bmse_can_merge(
5812 struct xfs_bmbt_irec *left, /* preceding extent */
5813 struct xfs_bmbt_irec *got, /* current extent to shift */
5814 xfs_fileoff_t shift) /* shift fsb */
5815{
5816 xfs_fileoff_t startoff;
5817
5818 startoff = got->br_startoff - shift;
5819
5820 /*
5821 * The extent, once shifted, must be adjacent in-file and on-disk with
5822 * the preceding extent.
5823 */
5824 if ((left->br_startoff + left->br_blockcount != startoff) ||
5825 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5826 (left->br_state != got->br_state) ||
5827 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5828 return false;
5829
5830 return true;
5831}
5832
5833/*
5834 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5835 * hole in the file. If an extent shift would result in the extent being fully
5836 * adjacent to the extent that currently precedes the hole, we can merge with
5837 * the preceding extent rather than do the shift.
5838 *
5839 * This function assumes the caller has verified a shift-by-merge is possible
5840 * with the provided extents via xfs_bmse_can_merge().
5841 */
5842STATIC int
5843xfs_bmse_merge(
5844 struct xfs_inode *ip,
5845 int whichfork,
5846 xfs_fileoff_t shift, /* shift fsb */
5847 int current_ext, /* idx of gotp */
5848 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5849 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5850 struct xfs_btree_cur *cur,
5851 int *logflags) /* output */
5852{
Brian Fosterddb19e32014-09-23 15:38:09 +10005853 struct xfs_bmbt_irec got;
5854 struct xfs_bmbt_irec left;
5855 xfs_filblks_t blockcount;
5856 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005857 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005858
Brian Fosterddb19e32014-09-23 15:38:09 +10005859 xfs_bmbt_get_all(gotp, &got);
5860 xfs_bmbt_get_all(leftp, &left);
5861 blockcount = left.br_blockcount + got.br_blockcount;
5862
5863 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5864 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5865 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5866
5867 /*
5868 * Merge the in-core extents. Note that the host record pointers and
5869 * current_ext index are invalid once the extent has been removed via
5870 * xfs_iext_remove().
5871 */
5872 xfs_bmbt_set_blockcount(leftp, blockcount);
5873 xfs_iext_remove(ip, current_ext, 1, 0);
5874
5875 /*
5876 * Update the on-disk extent count, the btree if necessary and log the
5877 * inode.
5878 */
5879 XFS_IFORK_NEXT_SET(ip, whichfork,
5880 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5881 *logflags |= XFS_ILOG_CORE;
5882 if (!cur) {
5883 *logflags |= XFS_ILOG_DEXT;
5884 return 0;
5885 }
5886
5887 /* lookup and remove the extent to merge */
5888 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5889 got.br_blockcount, &i);
5890 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005891 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005892 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005893
5894 error = xfs_btree_delete(cur, &i);
5895 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005896 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005897 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005898
5899 /* lookup and update size of the previous extent */
5900 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5901 left.br_blockcount, &i);
5902 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005903 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005904 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005905
5906 left.br_blockcount = blockcount;
5907
Dave Chinner4db431f2014-12-04 09:42:40 +11005908 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5909 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005910}
5911
5912/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005913 * Shift a single extent.
5914 */
5915STATIC int
5916xfs_bmse_shift_one(
5917 struct xfs_inode *ip,
5918 int whichfork,
5919 xfs_fileoff_t offset_shift_fsb,
5920 int *current_ext,
5921 struct xfs_bmbt_rec_host *gotp,
5922 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005923 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005924 enum shift_direction direction,
5925 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005926{
5927 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005928 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005929 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005930 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005931 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005932 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005933 int error;
5934 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005935 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005936
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005937 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005938 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005939 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005940
5941 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005942
Brian Fosterf71721d2014-09-23 15:39:05 +10005943 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005944 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005945
Namjae Jeona904b1c2015-03-25 15:08:56 +11005946 if (direction == SHIFT_LEFT) {
5947 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005948
Namjae Jeona904b1c2015-03-25 15:08:56 +11005949 /*
5950 * Check for merge if we've got an extent to the left,
5951 * otherwise make sure there's enough room at the start
5952 * of the file for the shift.
5953 */
5954 if (!*current_ext) {
5955 if (got.br_startoff < offset_shift_fsb)
5956 return -EINVAL;
5957 goto update_current_ext;
5958 }
5959 /*
5960 * grab the left extent and check for a large
5961 * enough hole.
5962 */
5963 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5964 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5965
5966 if (startoff <
5967 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005968 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005969
Dave Chinnerb11bd672014-12-04 09:42:24 +11005970 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005971 if (xfs_bmse_can_merge(&adj_irec, &got,
5972 offset_shift_fsb)) {
Darrick J. Wong9c194642016-08-03 12:16:05 +10005973 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5974 *current_ext, gotp, adj_irecp,
5975 cur, logflags);
5976 if (error)
5977 return error;
5978 adj_irec = got;
5979 goto update_rmap;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005980 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005981 } else {
5982 startoff = got.br_startoff + offset_shift_fsb;
5983 /* nothing to move if this is the last extent */
5984 if (*current_ext >= (total_extents - 1))
5985 goto update_current_ext;
5986 /*
5987 * If this is not the last extent in the file, make sure there
5988 * is enough room between current extent and next extent for
5989 * accommodating the shift.
5990 */
5991 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5992 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5993 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5994 return -EINVAL;
5995 /*
5996 * Unlike a left shift (which involves a hole punch),
5997 * a right shift does not modify extent neighbors
5998 * in any way. We should never find mergeable extents
5999 * in this scenario. Check anyways and warn if we
6000 * encounter two extents that could be one.
6001 */
6002 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
6003 WARN_ON_ONCE(1);
6004 }
Brian Fostera979bdf2014-09-23 15:39:04 +10006005 /*
6006 * Increment the extent index for the next iteration, update the start
6007 * offset of the in-core extent and update the btree if applicable.
6008 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11006009update_current_ext:
6010 if (direction == SHIFT_LEFT)
6011 (*current_ext)++;
6012 else
6013 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10006014 xfs_bmbt_set_startoff(gotp, startoff);
6015 *logflags |= XFS_ILOG_CORE;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006016 adj_irec = got;
Brian Fostera979bdf2014-09-23 15:39:04 +10006017 if (!cur) {
6018 *logflags |= XFS_ILOG_DEXT;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006019 goto update_rmap;
Brian Fostera979bdf2014-09-23 15:39:04 +10006020 }
6021
6022 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
6023 got.br_blockcount, &i);
6024 if (error)
6025 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11006026 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10006027
6028 got.br_startoff = startoff;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006029 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
6030 got.br_blockcount, got.br_state);
6031 if (error)
6032 return error;
6033
6034update_rmap:
6035 /* update reverse mapping */
6036 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
6037 if (error)
6038 return error;
6039 adj_irec.br_startoff = startoff;
6040 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
Brian Fostera979bdf2014-09-23 15:39:04 +10006041}
6042
6043/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11006044 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006045 *
Brian Foster2c845f52014-09-23 15:37:09 +10006046 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11006047 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10006048 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6049 * is the length by which each extent is shifted. If there is no hole to shift
6050 * the extents into, this will be considered invalid operation and we abort
6051 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006052 */
6053int
6054xfs_bmap_shift_extents(
6055 struct xfs_trans *tp,
6056 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006057 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006058 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10006059 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006060 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006061 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006062 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006063 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006064 int num_exts)
6065{
Brian Fosterca446d82014-09-02 12:12:53 +10006066 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006067 struct xfs_bmbt_rec_host *gotp;
6068 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006069 struct xfs_mount *mp = ip->i_mount;
6070 struct xfs_ifork *ifp;
6071 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10006072 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006073 xfs_extnum_t total_extents;
6074 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006075 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006076 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10006077 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006078
6079 if (unlikely(XFS_TEST_ERROR(
6080 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6081 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6082 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6083 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6084 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10006085 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006086 }
6087
6088 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10006089 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006090
Brian Foster2c845f52014-09-23 15:37:09 +10006091 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6092 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006093 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6094 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006095
6096 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006097 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6098 /* Read in all the extents */
6099 error = xfs_iread_extents(tp, ip, whichfork);
6100 if (error)
6101 return error;
6102 }
6103
Brian Fosterddb19e32014-09-23 15:38:09 +10006104 if (ifp->if_flags & XFS_IFBROOT) {
6105 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6106 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006107 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006108 cur->bc_private.b.flags = 0;
6109 }
6110
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006111 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006112 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006113 * are collapsing out, so we cannot use the count of real extents here.
6114 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006115 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006116 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006117 if (total_extents == 0) {
6118 *done = 1;
6119 goto del_cursor;
6120 }
6121
6122 /*
6123 * In case of first right shift, we need to initialize next_fsb
6124 */
6125 if (*next_fsb == NULLFSBLOCK) {
6126 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6127 xfs_bmbt_get_all(gotp, &got);
6128 *next_fsb = got.br_startoff;
6129 if (stop_fsb > *next_fsb) {
6130 *done = 1;
6131 goto del_cursor;
6132 }
6133 }
6134
6135 /* Lookup the extent index at which we have to stop */
6136 if (direction == SHIFT_RIGHT) {
6137 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6138 /* Make stop_extent exclusive of shift range */
6139 stop_extent--;
6140 } else
6141 stop_extent = total_extents;
6142
6143 /*
6144 * Look up the extent index for the fsb where we start shifting. We can
6145 * henceforth iterate with current_ext as extent list changes are locked
6146 * out via ilock.
6147 *
6148 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6149 * *next_fsb lies in a hole beyond which there are no extents. Either
6150 * way, we are done.
6151 */
6152 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
6153 if (!gotp) {
6154 *done = 1;
6155 goto del_cursor;
6156 }
6157
6158 /* some sanity checking before we finally start shifting extents */
6159 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6160 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6161 error = -EIO;
6162 goto del_cursor;
6163 }
6164
6165 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006166 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006167 &current_ext, gotp, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006168 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006169 if (error)
6170 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006171 /*
6172 * If there was an extent merge during the shift, the extent
6173 * count can change. Update the total and grade the next record.
6174 */
6175 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006176 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006177 stop_extent = total_extents;
6178 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006179
Namjae Jeona904b1c2015-03-25 15:08:56 +11006180 if (current_ext == stop_extent) {
6181 *done = 1;
6182 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006183 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006184 }
Brian Fosterddb19e32014-09-23 15:38:09 +10006185 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006186 }
6187
Namjae Jeona904b1c2015-03-25 15:08:56 +11006188 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006189 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10006190 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10006191 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006192
6193del_cursor:
6194 if (cur)
6195 xfs_btree_del_cursor(cur,
6196 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6197
Brian Fosterca446d82014-09-02 12:12:53 +10006198 if (logflags)
6199 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006200
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006201 return error;
6202}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006203
6204/*
6205 * Splits an extent into two extents at split_fsb block such that it is
6206 * the first block of the current_ext. @current_ext is a target extent
6207 * to be split. @split_fsb is a block where the extents is split.
6208 * If split_fsb lies in a hole or the first block of extents, just return 0.
6209 */
6210STATIC int
6211xfs_bmap_split_extent_at(
6212 struct xfs_trans *tp,
6213 struct xfs_inode *ip,
6214 xfs_fileoff_t split_fsb,
6215 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006216 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006217{
6218 int whichfork = XFS_DATA_FORK;
6219 struct xfs_btree_cur *cur = NULL;
6220 struct xfs_bmbt_rec_host *gotp;
6221 struct xfs_bmbt_irec got;
6222 struct xfs_bmbt_irec new; /* split extent */
6223 struct xfs_mount *mp = ip->i_mount;
6224 struct xfs_ifork *ifp;
6225 xfs_fsblock_t gotblkcnt; /* new block count for got */
6226 xfs_extnum_t current_ext;
6227 int error = 0;
6228 int logflags = 0;
6229 int i = 0;
6230
6231 if (unlikely(XFS_TEST_ERROR(
6232 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6233 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6234 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6235 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6236 XFS_ERRLEVEL_LOW, mp);
6237 return -EFSCORRUPTED;
6238 }
6239
6240 if (XFS_FORCED_SHUTDOWN(mp))
6241 return -EIO;
6242
6243 ifp = XFS_IFORK_PTR(ip, whichfork);
6244 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6245 /* Read in all the extents */
6246 error = xfs_iread_extents(tp, ip, whichfork);
6247 if (error)
6248 return error;
6249 }
6250
6251 /*
6252 * gotp can be null in 2 cases: 1) if there are no extents
6253 * or 2) split_fsb lies in a hole beyond which there are
6254 * no extents. Either way, we are done.
6255 */
6256 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
6257 if (!gotp)
6258 return 0;
6259
6260 xfs_bmbt_get_all(gotp, &got);
6261
6262 /*
6263 * Check split_fsb lies in a hole or the start boundary offset
6264 * of the extent.
6265 */
6266 if (got.br_startoff >= split_fsb)
6267 return 0;
6268
6269 gotblkcnt = split_fsb - got.br_startoff;
6270 new.br_startoff = split_fsb;
6271 new.br_startblock = got.br_startblock + gotblkcnt;
6272 new.br_blockcount = got.br_blockcount - gotblkcnt;
6273 new.br_state = got.br_state;
6274
6275 if (ifp->if_flags & XFS_IFBROOT) {
6276 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6277 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006278 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006279 cur->bc_private.b.flags = 0;
6280 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6281 got.br_startblock,
6282 got.br_blockcount,
6283 &i);
6284 if (error)
6285 goto del_cursor;
6286 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6287 }
6288
6289 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6290 got.br_blockcount = gotblkcnt;
6291
6292 logflags = XFS_ILOG_CORE;
6293 if (cur) {
6294 error = xfs_bmbt_update(cur, got.br_startoff,
6295 got.br_startblock,
6296 got.br_blockcount,
6297 got.br_state);
6298 if (error)
6299 goto del_cursor;
6300 } else
6301 logflags |= XFS_ILOG_DEXT;
6302
6303 /* Add new extent */
6304 current_ext++;
6305 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6306 XFS_IFORK_NEXT_SET(ip, whichfork,
6307 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6308
6309 if (cur) {
6310 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6311 new.br_startblock, new.br_blockcount,
6312 &i);
6313 if (error)
6314 goto del_cursor;
6315 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6316 cur->bc_rec.b.br_state = new.br_state;
6317
6318 error = xfs_btree_insert(cur, &i);
6319 if (error)
6320 goto del_cursor;
6321 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6322 }
6323
6324 /*
6325 * Convert to a btree if necessary.
6326 */
6327 if (xfs_bmap_needs_btree(ip, whichfork)) {
6328 int tmp_logflags; /* partial log flag return val */
6329
6330 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006331 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006332 &cur, 0, &tmp_logflags, whichfork);
6333 logflags |= tmp_logflags;
6334 }
6335
6336del_cursor:
6337 if (cur) {
6338 cur->bc_private.b.allocated = 0;
6339 xfs_btree_del_cursor(cur,
6340 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6341 }
6342
6343 if (logflags)
6344 xfs_trans_log_inode(tp, ip, logflags);
6345 return error;
6346}
6347
6348int
6349xfs_bmap_split_extent(
6350 struct xfs_inode *ip,
6351 xfs_fileoff_t split_fsb)
6352{
6353 struct xfs_mount *mp = ip->i_mount;
6354 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006355 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006356 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006357 int error;
6358
Christoph Hellwig253f4912016-04-06 09:19:55 +10006359 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6360 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6361 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006362 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006363
6364 xfs_ilock(ip, XFS_ILOCK_EXCL);
6365 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6366
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006367 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006368
6369 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006370 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006371 if (error)
6372 goto out;
6373
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006374 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006375 if (error)
6376 goto out;
6377
Christoph Hellwig70393312015-06-04 13:48:08 +10006378 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006379
6380out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006381 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006382 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006383 return error;
6384}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006385
6386/* Deferred mapping is only for real extents in the data fork. */
6387static bool
6388xfs_bmap_is_update_needed(
6389 struct xfs_bmbt_irec *bmap)
6390{
6391 return bmap->br_startblock != HOLESTARTBLOCK &&
6392 bmap->br_startblock != DELAYSTARTBLOCK;
6393}
6394
6395/* Record a bmap intent. */
6396static int
6397__xfs_bmap_add(
6398 struct xfs_mount *mp,
6399 struct xfs_defer_ops *dfops,
6400 enum xfs_bmap_intent_type type,
6401 struct xfs_inode *ip,
6402 int whichfork,
6403 struct xfs_bmbt_irec *bmap)
6404{
6405 int error;
6406 struct xfs_bmap_intent *bi;
6407
6408 trace_xfs_bmap_defer(mp,
6409 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6410 type,
6411 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6412 ip->i_ino, whichfork,
6413 bmap->br_startoff,
6414 bmap->br_blockcount,
6415 bmap->br_state);
6416
6417 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6418 INIT_LIST_HEAD(&bi->bi_list);
6419 bi->bi_type = type;
6420 bi->bi_owner = ip;
6421 bi->bi_whichfork = whichfork;
6422 bi->bi_bmap = *bmap;
6423
6424 error = xfs_defer_join(dfops, bi->bi_owner);
6425 if (error) {
6426 kmem_free(bi);
6427 return error;
6428 }
6429
6430 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6431 return 0;
6432}
6433
6434/* Map an extent into a file. */
6435int
6436xfs_bmap_map_extent(
6437 struct xfs_mount *mp,
6438 struct xfs_defer_ops *dfops,
6439 struct xfs_inode *ip,
6440 struct xfs_bmbt_irec *PREV)
6441{
6442 if (!xfs_bmap_is_update_needed(PREV))
6443 return 0;
6444
6445 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6446 XFS_DATA_FORK, PREV);
6447}
6448
6449/* Unmap an extent out of a file. */
6450int
6451xfs_bmap_unmap_extent(
6452 struct xfs_mount *mp,
6453 struct xfs_defer_ops *dfops,
6454 struct xfs_inode *ip,
6455 struct xfs_bmbt_irec *PREV)
6456{
6457 if (!xfs_bmap_is_update_needed(PREV))
6458 return 0;
6459
6460 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6461 XFS_DATA_FORK, PREV);
6462}
6463
6464/*
6465 * Process one of the deferred bmap operations. We pass back the
6466 * btree cursor to maintain our lock on the bmapbt between calls.
6467 */
6468int
6469xfs_bmap_finish_one(
6470 struct xfs_trans *tp,
6471 struct xfs_defer_ops *dfops,
6472 struct xfs_inode *ip,
6473 enum xfs_bmap_intent_type type,
6474 int whichfork,
6475 xfs_fileoff_t startoff,
6476 xfs_fsblock_t startblock,
6477 xfs_filblks_t blockcount,
6478 xfs_exntst_t state)
6479{
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006480 int error = 0, done;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006481
6482 trace_xfs_bmap_deferred(tp->t_mountp,
6483 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6484 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6485 ip->i_ino, whichfork, startoff, blockcount, state);
6486
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006487 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006488 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006489
6490 if (XFS_TEST_ERROR(false, tp->t_mountp,
6491 XFS_ERRTAG_BMAP_FINISH_ONE,
6492 XFS_RANDOM_BMAP_FINISH_ONE))
6493 return -EIO;
6494
6495 switch (type) {
6496 case XFS_BMAP_MAP:
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006497 error = xfs_bmapi_remap(tp, ip, startoff, blockcount,
6498 startblock, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006499 break;
6500 case XFS_BMAP_UNMAP:
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006501 error = xfs_bunmapi(tp, ip, startoff, blockcount,
6502 XFS_BMAPI_REMAP, 1, &startblock, dfops, &done);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006503 ASSERT(done);
6504 break;
6505 default:
6506 ASSERT(0);
6507 error = -EFSCORRUPTED;
6508 }
6509
6510 return error;
6511}