blob: bfa59a1a2d0981afc4ab4c4cd0d2f2daaec91aef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100030#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_extfree_item.h"
36#include "xfs_alloc.h"
37#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100038#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110039#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_rtalloc.h"
41#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_quota.h"
43#include "xfs_trans_space.h"
44#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110046#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110047#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110048#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100049#include "xfs_rmap.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100050#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070051#include "xfs_refcount.h"
Darrick J. Wongfd26a882016-11-28 14:57:42 +110052#include "xfs_rmap_btree.h"
Brian Foster974ae922016-11-28 14:57:42 +110053#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056kmem_zone_t *xfs_bmap_free_item_zone;
57
58/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110059 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110063 * Compute and fill in the value of the maximum depth of a bmap btree
64 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110066void
67xfs_bmap_compute_maxlevels(
68 xfs_mount_t *mp, /* file system mount structure */
69 int whichfork) /* data or attr fork */
70{
71 int level; /* btree level */
72 uint maxblocks; /* max blocks at this level */
73 uint maxleafents; /* max leaf entries possible */
74 int maxrootrecs; /* max records in root block */
75 int minleafrecs; /* min records in leaf block */
76 int minnoderecs; /* min records in node block */
77 int sz; /* root block size */
78
79 /*
80 * The maximum number of extents in a file, hence the maximum
81 * number of leaf entries, is controlled by the type of di_nextents
82 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
83 * (a signed 16-bit number, xfs_aextnum_t).
84 *
85 * Note that we can no longer assume that if we are in ATTR1 that
86 * the fork offset of all the inodes will be
87 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
88 * with ATTR2 and then mounted back with ATTR1, keeping the
89 * di_forkoff's fixed but probably at various positions. Therefore,
90 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
91 * of a minimum size available.
92 */
93 if (whichfork == XFS_DATA_FORK) {
94 maxleafents = MAXEXTNUM;
95 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
96 } else {
97 maxleafents = MAXAEXTNUM;
98 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
99 }
Eric Sandeen152d93b2014-04-14 18:58:51 +1000100 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100101 minleafrecs = mp->m_bmap_dmnr[0];
102 minnoderecs = mp->m_bmap_dmnr[1];
103 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
104 for (level = 1; maxblocks > 1; level++) {
105 if (maxblocks <= maxrootrecs)
106 maxblocks = 1;
107 else
108 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
109 }
110 mp->m_bm_maxlevels[whichfork] = level;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100113STATIC int /* error */
114xfs_bmbt_lookup_eq(
115 struct xfs_btree_cur *cur,
116 xfs_fileoff_t off,
117 xfs_fsblock_t bno,
118 xfs_filblks_t len,
119 int *stat) /* success/failure */
120{
121 cur->bc_rec.b.br_startoff = off;
122 cur->bc_rec.b.br_startblock = bno;
123 cur->bc_rec.b.br_blockcount = len;
124 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
125}
126
127STATIC int /* error */
128xfs_bmbt_lookup_ge(
129 struct xfs_btree_cur *cur,
130 xfs_fileoff_t off,
131 xfs_fsblock_t bno,
132 xfs_filblks_t len,
133 int *stat) /* success/failure */
134{
135 cur->bc_rec.b.br_startoff = off;
136 cur->bc_rec.b.br_startblock = bno;
137 cur->bc_rec.b.br_blockcount = len;
138 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
139}
140
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100141/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000142 * Check if the inode needs to be converted to btree format.
143 */
144static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
145{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700146 return whichfork != XFS_COW_FORK &&
147 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000148 XFS_IFORK_NEXTENTS(ip, whichfork) >
149 XFS_IFORK_MAXEXT(ip, whichfork);
150}
151
152/*
153 * Check if the inode should be converted to extent format.
154 */
155static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
156{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700157 return whichfork != XFS_COW_FORK &&
158 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000159 XFS_IFORK_NEXTENTS(ip, whichfork) <=
160 XFS_IFORK_MAXEXT(ip, whichfork);
161}
162
163/*
164 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100165 * by [off, bno, len, state].
166 * This either works (return 0) or gets an EFSCORRUPTED error.
167 */
168STATIC int
169xfs_bmbt_update(
170 struct xfs_btree_cur *cur,
171 xfs_fileoff_t off,
172 xfs_fsblock_t bno,
173 xfs_filblks_t len,
174 xfs_exntst_t state)
175{
176 union xfs_btree_rec rec;
177
178 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
179 return xfs_btree_update(cur, &rec);
180}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100183 * Compute the worst-case number of indirect blocks that will be used
184 * for ip's delayed extent of length "len".
185 */
186STATIC xfs_filblks_t
187xfs_bmap_worst_indlen(
188 xfs_inode_t *ip, /* incore inode pointer */
189 xfs_filblks_t len) /* delayed extent length */
190{
191 int level; /* btree level number */
192 int maxrecs; /* maximum record count at this level */
193 xfs_mount_t *mp; /* mount structure */
194 xfs_filblks_t rval; /* return value */
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100195 xfs_filblks_t orig_len;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100196
197 mp = ip->i_mount;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100198
199 /* Calculate the worst-case size of the bmbt. */
200 orig_len = len;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100201 maxrecs = mp->m_bmap_dmxr[0];
202 for (level = 0, rval = 0;
203 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
204 level++) {
205 len += maxrecs - 1;
206 do_div(len, maxrecs);
207 rval += len;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100208 if (len == 1) {
209 rval += XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100210 level - 1;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100211 break;
212 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100213 if (level == 0)
214 maxrecs = mp->m_bmap_dmxr[1];
215 }
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100216
217 /* Calculate the worst-case size of the rmapbt. */
218 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
219 rval += 1 + xfs_rmapbt_calc_size(mp, orig_len) +
220 mp->m_rmap_maxlevels;
221
Dave Chinner9e5987a2013-02-25 12:31:26 +1100222 return rval;
223}
224
225/*
226 * Calculate the default attribute fork offset for newly created inodes.
227 */
228uint
229xfs_default_attroffset(
230 struct xfs_inode *ip)
231{
232 struct xfs_mount *mp = ip->i_mount;
233 uint offset;
234
235 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100236 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100237 XFS_BMDR_SPACE_CALC(MINABTPTRS);
238 } else {
239 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
240 }
241
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100242 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100243 return offset;
244}
245
246/*
247 * Helper routine to reset inode di_forkoff field when switching
248 * attribute fork from local to extent format - we reset it where
249 * possible to make space available for inline data fork extents.
250 */
251STATIC void
252xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100253 xfs_inode_t *ip,
254 int whichfork)
255{
256 if (whichfork == XFS_ATTR_FORK &&
257 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
258 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
259 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
260 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
261
262 if (dfl_forkoff > ip->i_d.di_forkoff)
263 ip->i_d.di_forkoff = dfl_forkoff;
264 }
265}
266
Dave Chinner9e5987a2013-02-25 12:31:26 +1100267#ifdef DEBUG
268STATIC struct xfs_buf *
269xfs_bmap_get_bp(
270 struct xfs_btree_cur *cur,
271 xfs_fsblock_t bno)
272{
273 struct xfs_log_item_desc *lidp;
274 int i;
275
276 if (!cur)
277 return NULL;
278
279 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
280 if (!cur->bc_bufs[i])
281 break;
282 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
283 return cur->bc_bufs[i];
284 }
285
286 /* Chase down all the log items to see if the bp is there */
287 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
288 struct xfs_buf_log_item *bip;
289 bip = (struct xfs_buf_log_item *)lidp->lid_item;
290 if (bip->bli_item.li_type == XFS_LI_BUF &&
291 XFS_BUF_ADDR(bip->bli_buf) == bno)
292 return bip->bli_buf;
293 }
294
295 return NULL;
296}
297
298STATIC void
299xfs_check_block(
300 struct xfs_btree_block *block,
301 xfs_mount_t *mp,
302 int root,
303 short sz)
304{
305 int i, j, dmxr;
306 __be64 *pp, *thispa; /* pointer to block address */
307 xfs_bmbt_key_t *prevp, *keyp;
308
309 ASSERT(be16_to_cpu(block->bb_level) > 0);
310
311 prevp = NULL;
312 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
313 dmxr = mp->m_bmap_dmxr[0];
314 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
315
316 if (prevp) {
317 ASSERT(be64_to_cpu(prevp->br_startoff) <
318 be64_to_cpu(keyp->br_startoff));
319 }
320 prevp = keyp;
321
322 /*
323 * Compare the block numbers to see if there are dups.
324 */
325 if (root)
326 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
327 else
328 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
329
330 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
331 if (root)
332 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
333 else
334 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
335 if (*thispa == *pp) {
336 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
337 __func__, j, i,
338 (unsigned long long)be64_to_cpu(*thispa));
339 panic("%s: ptrs are equal in node\n",
340 __func__);
341 }
342 }
343 }
344}
345
346/*
347 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100348 * btree leaves. THis becomes prohibitively expensive for large extent count
349 * files, so don't bother with inodes that have more than 10,000 extents in
350 * them. The btree record ordering checks will still be done, so for such large
351 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100352 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100353STATIC void
354xfs_bmap_check_leaf_extents(
355 xfs_btree_cur_t *cur, /* btree cursor or null */
356 xfs_inode_t *ip, /* incore inode pointer */
357 int whichfork) /* data or attr fork */
358{
359 struct xfs_btree_block *block; /* current btree block */
360 xfs_fsblock_t bno; /* block # of "block" */
361 xfs_buf_t *bp; /* buffer for "block" */
362 int error; /* error return value */
363 xfs_extnum_t i=0, j; /* index into the extents list */
364 xfs_ifork_t *ifp; /* fork structure */
365 int level; /* btree level, for checking */
366 xfs_mount_t *mp; /* file system mount structure */
367 __be64 *pp; /* pointer to block address */
368 xfs_bmbt_rec_t *ep; /* pointer to current extent */
369 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
370 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
371 int bp_release = 0;
372
373 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
374 return;
375 }
376
Dave Chinnere3543812016-01-08 11:28:49 +1100377 /* skip large extent count inodes */
378 if (ip->i_d.di_nextents > 10000)
379 return;
380
Dave Chinner9e5987a2013-02-25 12:31:26 +1100381 bno = NULLFSBLOCK;
382 mp = ip->i_mount;
383 ifp = XFS_IFORK_PTR(ip, whichfork);
384 block = ifp->if_broot;
385 /*
386 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
387 */
388 level = be16_to_cpu(block->bb_level);
389 ASSERT(level > 0);
390 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
391 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
392 bno = be64_to_cpu(*pp);
393
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000394 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100395 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
396 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
397
398 /*
399 * Go down the tree until leaf level is reached, following the first
400 * pointer (leftmost) at each level.
401 */
402 while (level-- > 0) {
403 /* See if buf is in cur first */
404 bp_release = 0;
405 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
406 if (!bp) {
407 bp_release = 1;
408 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
409 XFS_BMAP_BTREE_REF,
410 &xfs_bmbt_buf_ops);
411 if (error)
412 goto error_norelse;
413 }
414 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100415 if (level == 0)
416 break;
417
418 /*
419 * Check this block for basic sanity (increasing keys and
420 * no duplicate blocks).
421 */
422
423 xfs_check_block(block, mp, 0, 0);
424 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
425 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100426 XFS_WANT_CORRUPTED_GOTO(mp,
427 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100428 if (bp_release) {
429 bp_release = 0;
430 xfs_trans_brelse(NULL, bp);
431 }
432 }
433
434 /*
435 * Here with bp and block set to the leftmost leaf node in the tree.
436 */
437 i = 0;
438
439 /*
440 * Loop over all leaf nodes checking that all extents are in the right order.
441 */
442 for (;;) {
443 xfs_fsblock_t nextbno;
444 xfs_extnum_t num_recs;
445
446
447 num_recs = xfs_btree_get_numrecs(block);
448
449 /*
450 * Read-ahead the next leaf block, if any.
451 */
452
453 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
454
455 /*
456 * Check all the extents to make sure they are OK.
457 * If we had a previous block, the last entry should
458 * conform with the first entry in this one.
459 */
460
461 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
462 if (i) {
463 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
464 xfs_bmbt_disk_get_blockcount(&last) <=
465 xfs_bmbt_disk_get_startoff(ep));
466 }
467 for (j = 1; j < num_recs; j++) {
468 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
469 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
470 xfs_bmbt_disk_get_blockcount(ep) <=
471 xfs_bmbt_disk_get_startoff(nextp));
472 ep = nextp;
473 }
474
475 last = *ep;
476 i += num_recs;
477 if (bp_release) {
478 bp_release = 0;
479 xfs_trans_brelse(NULL, bp);
480 }
481 bno = nextbno;
482 /*
483 * If we've reached the end, stop.
484 */
485 if (bno == NULLFSBLOCK)
486 break;
487
488 bp_release = 0;
489 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
490 if (!bp) {
491 bp_release = 1;
492 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
493 XFS_BMAP_BTREE_REF,
494 &xfs_bmbt_buf_ops);
495 if (error)
496 goto error_norelse;
497 }
498 block = XFS_BUF_TO_BLOCK(bp);
499 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100500
Dave Chinner9e5987a2013-02-25 12:31:26 +1100501 return;
502
503error0:
504 xfs_warn(mp, "%s: at error0", __func__);
505 if (bp_release)
506 xfs_trans_brelse(NULL, bp);
507error_norelse:
508 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
509 __func__, i);
510 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
511 return;
512}
513
514/*
515 * Add bmap trace insert entries for all the contents of the extent records.
516 */
517void
518xfs_bmap_trace_exlist(
519 xfs_inode_t *ip, /* incore inode pointer */
520 xfs_extnum_t cnt, /* count of entries in the list */
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100521 int whichfork, /* data or attr or cow fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100522 unsigned long caller_ip)
523{
524 xfs_extnum_t idx; /* extent record index */
525 xfs_ifork_t *ifp; /* inode fork pointer */
526 int state = 0;
527
528 if (whichfork == XFS_ATTR_FORK)
529 state |= BMAP_ATTRFORK;
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100530 else if (whichfork == XFS_COW_FORK)
531 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100532
533 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +1100534 ASSERT(cnt == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100535 for (idx = 0; idx < cnt; idx++)
Eric Sandeen77105172016-12-05 12:31:50 +1100536 trace_xfs_extlist(ip, idx, state, caller_ip);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100537}
538
539/*
540 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000541 * given the caller's original parameters. Specifically check the
542 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100543 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
544 */
545STATIC void
546xfs_bmap_validate_ret(
547 xfs_fileoff_t bno,
548 xfs_filblks_t len,
549 int flags,
550 xfs_bmbt_irec_t *mval,
551 int nmap,
552 int ret_nmap)
553{
554 int i; /* index to map values */
555
556 ASSERT(ret_nmap <= nmap);
557
558 for (i = 0; i < ret_nmap; i++) {
559 ASSERT(mval[i].br_blockcount > 0);
560 if (!(flags & XFS_BMAPI_ENTIRE)) {
561 ASSERT(mval[i].br_startoff >= bno);
562 ASSERT(mval[i].br_blockcount <= len);
563 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
564 bno + len);
565 } else {
566 ASSERT(mval[i].br_startoff < bno + len);
567 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
568 bno);
569 }
570 ASSERT(i == 0 ||
571 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
572 mval[i].br_startoff);
573 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
574 mval[i].br_startblock != HOLESTARTBLOCK);
575 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
576 mval[i].br_state == XFS_EXT_UNWRITTEN);
577 }
578}
579
580#else
581#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
582#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
583#endif /* DEBUG */
584
585/*
586 * bmap free list manipulation functions
587 */
588
589/*
590 * Add the extent to the list of extents to be free at transaction end.
591 * The list is maintained sorted (by block number).
592 */
593void
594xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000595 struct xfs_mount *mp,
596 struct xfs_defer_ops *dfops,
597 xfs_fsblock_t bno,
598 xfs_filblks_t len,
599 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100600{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000601 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100602#ifdef DEBUG
603 xfs_agnumber_t agno;
604 xfs_agblock_t agbno;
605
606 ASSERT(bno != NULLFSBLOCK);
607 ASSERT(len > 0);
608 ASSERT(len <= MAXEXTLEN);
609 ASSERT(!isnullstartblock(bno));
610 agno = XFS_FSB_TO_AGNO(mp, bno);
611 agbno = XFS_FSB_TO_AGBNO(mp, bno);
612 ASSERT(agno < mp->m_sb.sb_agcount);
613 ASSERT(agbno < mp->m_sb.sb_agblocks);
614 ASSERT(len < mp->m_sb.sb_agblocks);
615 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
616#endif
617 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000618
Dave Chinner9e5987a2013-02-25 12:31:26 +1100619 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000620 new->xefi_startblock = bno;
621 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000622 if (oinfo)
623 new->xefi_oinfo = *oinfo;
624 else
625 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000626 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
627 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000628 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100629}
630
631/*
632 * Inode fork format manipulation functions
633 */
634
635/*
636 * Transform a btree format file with only one leaf node, where the
637 * extents list will fit in the inode, into an extents format file.
638 * Since the file extents are already in-core, all we have to do is
639 * give up the space for the btree root and pitch the leaf block.
640 */
641STATIC int /* error */
642xfs_bmap_btree_to_extents(
643 xfs_trans_t *tp, /* transaction pointer */
644 xfs_inode_t *ip, /* incore inode pointer */
645 xfs_btree_cur_t *cur, /* btree cursor */
646 int *logflagsp, /* inode logging flags */
647 int whichfork) /* data or attr fork */
648{
649 /* REFERENCED */
650 struct xfs_btree_block *cblock;/* child btree block */
651 xfs_fsblock_t cbno; /* child block number */
652 xfs_buf_t *cbp; /* child block's buffer */
653 int error; /* error return value */
654 xfs_ifork_t *ifp; /* inode fork data */
655 xfs_mount_t *mp; /* mount point structure */
656 __be64 *pp; /* ptr to block address */
657 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000658 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100659
660 mp = ip->i_mount;
661 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700662 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100663 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
664 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
665 rblock = ifp->if_broot;
666 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
667 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
668 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
669 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
670 cbno = be64_to_cpu(*pp);
671 *logflagsp = 0;
672#ifdef DEBUG
673 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
674 return error;
675#endif
676 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
677 &xfs_bmbt_buf_ops);
678 if (error)
679 return error;
680 cblock = XFS_BUF_TO_BLOCK(cbp);
681 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
682 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000683 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
684 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100685 ip->i_d.di_nblocks--;
686 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
687 xfs_trans_binval(tp, cbp);
688 if (cur->bc_bufs[0] == cbp)
689 cur->bc_bufs[0] = NULL;
690 xfs_iroot_realloc(ip, -1, whichfork);
691 ASSERT(ifp->if_broot == NULL);
692 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
693 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
694 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
695 return 0;
696}
697
698/*
699 * Convert an extents-format file into a btree-format file.
700 * The new file will have a root block (in the inode) and a single child block.
701 */
702STATIC int /* error */
703xfs_bmap_extents_to_btree(
704 xfs_trans_t *tp, /* transaction pointer */
705 xfs_inode_t *ip, /* incore inode pointer */
706 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000707 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100708 xfs_btree_cur_t **curp, /* cursor returned to caller */
709 int wasdel, /* converting a delayed alloc */
710 int *logflagsp, /* inode logging flags */
711 int whichfork) /* data or attr fork */
712{
713 struct xfs_btree_block *ablock; /* allocated (child) bt block */
714 xfs_buf_t *abp; /* buffer for ablock */
715 xfs_alloc_arg_t args; /* allocation arguments */
716 xfs_bmbt_rec_t *arp; /* child record pointer */
717 struct xfs_btree_block *block; /* btree root block */
718 xfs_btree_cur_t *cur; /* bmap btree cursor */
719 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
720 int error; /* error return value */
721 xfs_extnum_t i, cnt; /* extent record index */
722 xfs_ifork_t *ifp; /* inode fork pointer */
723 xfs_bmbt_key_t *kp; /* root block key pointer */
724 xfs_mount_t *mp; /* mount structure */
725 xfs_extnum_t nextents; /* number of file extents */
726 xfs_bmbt_ptr_t *pp; /* root block address pointer */
727
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500728 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700729 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100730 ifp = XFS_IFORK_PTR(ip, whichfork);
731 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
732
733 /*
734 * Make space in the inode incore.
735 */
736 xfs_iroot_realloc(ip, 1, whichfork);
737 ifp->if_flags |= XFS_IFBROOT;
738
739 /*
740 * Fill in the root.
741 */
742 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800743 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
744 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800745 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100746 /*
747 * Need a cursor. Can't allocate until bb_level is filled in.
748 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100749 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
750 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000751 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100752 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
753 /*
754 * Convert to a btree with two levels, one record in root.
755 */
756 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
757 memset(&args, 0, sizeof(args));
758 args.tp = tp;
759 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000760 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100761 args.firstblock = *firstblock;
762 if (*firstblock == NULLFSBLOCK) {
763 args.type = XFS_ALLOCTYPE_START_BNO;
764 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000765 } else if (dfops->dop_low) {
Darrick J. Wong90e20562016-10-03 09:11:45 -0700766try_another_ag:
Dave Chinner9e5987a2013-02-25 12:31:26 +1100767 args.type = XFS_ALLOCTYPE_START_BNO;
768 args.fsbno = *firstblock;
769 } else {
770 args.type = XFS_ALLOCTYPE_NEAR_BNO;
771 args.fsbno = *firstblock;
772 }
773 args.minlen = args.maxlen = args.prod = 1;
774 args.wasdel = wasdel;
775 *logflagsp = 0;
776 if ((error = xfs_alloc_vextent(&args))) {
777 xfs_iroot_realloc(ip, -1, whichfork);
778 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
779 return error;
780 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700781
782 /*
783 * During a CoW operation, the allocation and bmbt updates occur in
784 * different transactions. The mapping code tries to put new bmbt
785 * blocks near extents being mapped, but the only way to guarantee this
786 * is if the alloc and the mapping happen in a single transaction that
787 * has a block reservation. That isn't the case here, so if we run out
788 * of space we'll try again with another AG.
789 */
790 if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) &&
791 args.fsbno == NULLFSBLOCK &&
792 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
793 dfops->dop_low = true;
794 goto try_another_ag;
795 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100796 /*
797 * Allocation can't fail, the space was reserved.
798 */
799 ASSERT(args.fsbno != NULLFSBLOCK);
800 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800801 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100802 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
803 cur->bc_private.b.allocated++;
804 ip->i_d.di_nblocks++;
805 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
806 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
807 /*
808 * Fill in the child block.
809 */
810 abp->b_ops = &xfs_bmbt_buf_ops;
811 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800812 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
813 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500814 XFS_BTREE_LONG_PTRS);
815
Dave Chinner9e5987a2013-02-25 12:31:26 +1100816 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100817 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100818 for (cnt = i = 0; i < nextents; i++) {
819 ep = xfs_iext_get_ext(ifp, i);
820 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
821 arp->l0 = cpu_to_be64(ep->l0);
822 arp->l1 = cpu_to_be64(ep->l1);
823 arp++; cnt++;
824 }
825 }
826 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
827 xfs_btree_set_numrecs(ablock, cnt);
828
829 /*
830 * Fill in the root key and pointer.
831 */
832 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
833 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
834 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
835 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
836 be16_to_cpu(block->bb_level)));
837 *pp = cpu_to_be64(args.fsbno);
838
839 /*
840 * Do all this logging at the end so that
841 * the root is at the right level.
842 */
843 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
844 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
845 ASSERT(*curp == NULL);
846 *curp = cur;
847 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
848 return 0;
849}
850
851/*
852 * Convert a local file to an extents file.
853 * This code is out of bounds for data forks of regular files,
854 * since the file data needs to get logged so things will stay consistent.
855 * (The bmap-level manipulations are ok, though).
856 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000857void
858xfs_bmap_local_to_extents_empty(
859 struct xfs_inode *ip,
860 int whichfork)
861{
862 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
863
Darrick J. Wong60b49842016-10-03 09:11:34 -0700864 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000865 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
866 ASSERT(ifp->if_bytes == 0);
867 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
868
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000869 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000870 ifp->if_flags &= ~XFS_IFINLINE;
871 ifp->if_flags |= XFS_IFEXTENTS;
872 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
873}
874
875
Dave Chinner9e5987a2013-02-25 12:31:26 +1100876STATIC int /* error */
877xfs_bmap_local_to_extents(
878 xfs_trans_t *tp, /* transaction pointer */
879 xfs_inode_t *ip, /* incore inode pointer */
880 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
881 xfs_extlen_t total, /* total blocks needed by transaction */
882 int *logflagsp, /* inode logging flags */
883 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500884 void (*init_fn)(struct xfs_trans *tp,
885 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100886 struct xfs_inode *ip,
887 struct xfs_ifork *ifp))
888{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000889 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100890 int flags; /* logging flags returned */
891 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000892 xfs_alloc_arg_t args; /* allocation arguments */
893 xfs_buf_t *bp; /* buffer for extent block */
894 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100895
896 /*
897 * We don't want to deal with the case of keeping inode data inline yet.
898 * So sending the data fork of a regular inode is invalid.
899 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100900 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100901 ifp = XFS_IFORK_PTR(ip, whichfork);
902 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000903
904 if (!ifp->if_bytes) {
905 xfs_bmap_local_to_extents_empty(ip, whichfork);
906 flags = XFS_ILOG_CORE;
907 goto done;
908 }
909
Dave Chinner9e5987a2013-02-25 12:31:26 +1100910 flags = 0;
911 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000912 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
913 XFS_IFINLINE);
914 memset(&args, 0, sizeof(args));
915 args.tp = tp;
916 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000917 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000918 args.firstblock = *firstblock;
919 /*
920 * Allocate a block. We know we need only one, since the
921 * file currently fits in an inode.
922 */
923 if (*firstblock == NULLFSBLOCK) {
Darrick J. Wong90e20562016-10-03 09:11:45 -0700924try_another_ag:
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000925 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
926 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100927 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000928 args.fsbno = *firstblock;
929 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100930 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000931 args.total = total;
932 args.minlen = args.maxlen = args.prod = 1;
933 error = xfs_alloc_vextent(&args);
934 if (error)
935 goto done;
936
Darrick J. Wong90e20562016-10-03 09:11:45 -0700937 /*
938 * During a CoW operation, the allocation and bmbt updates occur in
939 * different transactions. The mapping code tries to put new bmbt
940 * blocks near extents being mapped, but the only way to guarantee this
941 * is if the alloc and the mapping happen in a single transaction that
942 * has a block reservation. That isn't the case here, so if we run out
943 * of space we'll try again with another AG.
944 */
945 if (xfs_sb_version_hasreflink(&ip->i_mount->m_sb) &&
946 args.fsbno == NULLFSBLOCK &&
947 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
948 goto try_another_ag;
949 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000950 /* Can't fail, the space was reserved. */
951 ASSERT(args.fsbno != NULLFSBLOCK);
952 ASSERT(args.len == 1);
953 *firstblock = args.fsbno;
954 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
955
Dave Chinnerfe22d552015-01-22 09:30:06 +1100956 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100957 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100958 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100959 * The callout is responsible for logging because the remote format
960 * might differ from the local format and thus we don't know how much to
961 * log here. Note that init_fn must also set the buffer log item type
962 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100963 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000964 init_fn(tp, bp, ip, ifp);
965
Brian Fosterb7cdc662015-10-12 15:40:24 +1100966 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000967 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
968 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100969 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000970
971 xfs_iext_add(ifp, 0, 1);
972 ep = xfs_iext_get_ext(ifp, 0);
973 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
974 trace_xfs_bmap_post_update(ip, 0,
975 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
976 _THIS_IP_);
977 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
978 ip->i_d.di_nblocks = 1;
979 xfs_trans_mod_dquot_byino(tp, ip,
980 XFS_TRANS_DQ_BCOUNT, 1L);
981 flags |= xfs_ilog_fext(whichfork);
982
Dave Chinner9e5987a2013-02-25 12:31:26 +1100983done:
984 *logflagsp = flags;
985 return error;
986}
987
988/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 * Called from xfs_bmap_add_attrfork to handle btree format files.
990 */
991STATIC int /* error */
992xfs_bmap_add_attrfork_btree(
993 xfs_trans_t *tp, /* transaction pointer */
994 xfs_inode_t *ip, /* incore inode pointer */
995 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000996 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 int *flags) /* inode logging flags */
998{
999 xfs_btree_cur_t *cur; /* btree cursor */
1000 int error; /* error return value */
1001 xfs_mount_t *mp; /* file system mount struct */
1002 int stat; /* newroot status */
1003
1004 mp = ip->i_mount;
1005 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1006 *flags |= XFS_ILOG_DBROOT;
1007 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001008 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001009 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 cur->bc_private.b.firstblock = *firstblock;
1011 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1012 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001013 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001014 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001015 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 goto error0;
1017 if (stat == 0) {
1018 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +10001019 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 *firstblock = cur->bc_private.b.firstblock;
1022 cur->bc_private.b.allocated = 0;
1023 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1024 }
1025 return 0;
1026error0:
1027 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1028 return error;
1029}
1030
1031/*
1032 * Called from xfs_bmap_add_attrfork to handle extents format files.
1033 */
1034STATIC int /* error */
1035xfs_bmap_add_attrfork_extents(
1036 xfs_trans_t *tp, /* transaction pointer */
1037 xfs_inode_t *ip, /* incore inode pointer */
1038 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001039 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int *flags) /* inode logging flags */
1041{
1042 xfs_btree_cur_t *cur; /* bmap btree cursor */
1043 int error; /* error return value */
1044
1045 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1046 return 0;
1047 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001048 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 flags, XFS_DATA_FORK);
1050 if (cur) {
1051 cur->bc_private.b.allocated = 0;
1052 xfs_btree_del_cursor(cur,
1053 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1054 }
1055 return error;
1056}
1057
1058/*
Dave Chinner1e823792013-02-11 15:58:13 +11001059 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1060 * different data fork content type needs a different callout to do the
1061 * conversion. Some are basic and only require special block initialisation
1062 * callouts for the data formating, others (directories) are so specialised they
1063 * handle everything themselves.
1064 *
1065 * XXX (dgc): investigate whether directory conversion can use the generic
1066 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001067 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 */
1069STATIC int /* error */
1070xfs_bmap_add_attrfork_local(
1071 xfs_trans_t *tp, /* transaction pointer */
1072 xfs_inode_t *ip, /* incore inode pointer */
1073 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001074 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 int *flags) /* inode logging flags */
1076{
1077 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1080 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001081
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001082 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001084 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 dargs.dp = ip;
1086 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001087 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001088 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 dargs.whichfork = XFS_DATA_FORK;
1090 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001091 return xfs_dir2_sf_to_block(&dargs);
1092 }
1093
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001094 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001095 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1096 flags, XFS_DATA_FORK,
1097 xfs_symlink_local_to_remote);
1098
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001099 /* should only be called for types that support local format data */
1100 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001101 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001105 * Convert inode from non-attributed to attributed.
1106 * Must not be in a transaction, ip must not be locked.
1107 */
1108int /* error code */
1109xfs_bmap_add_attrfork(
1110 xfs_inode_t *ip, /* incore inode pointer */
1111 int size, /* space new attribute needs */
1112 int rsvd) /* xact may use reserved blks */
1113{
1114 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001115 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001116 xfs_mount_t *mp; /* mount structure */
1117 xfs_trans_t *tp; /* transaction pointer */
1118 int blks; /* space reservation */
1119 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001120 int logflags; /* logging flags */
1121 int error; /* error return value */
1122
1123 ASSERT(XFS_IFORK_Q(ip) == 0);
1124
1125 mp = ip->i_mount;
1126 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001127
Dave Chinner9e5987a2013-02-25 12:31:26 +11001128 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001129
1130 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1131 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1132 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001133 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001134
Dave Chinner9e5987a2013-02-25 12:31:26 +11001135 xfs_ilock(ip, XFS_ILOCK_EXCL);
1136 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1137 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1138 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001139 if (error)
1140 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001141 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001142 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001143 if (ip->i_d.di_anextents != 0) {
1144 error = -EFSCORRUPTED;
1145 goto trans_cancel;
1146 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001147 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1148 /*
1149 * For inodes coming from pre-6.2 filesystems.
1150 */
1151 ASSERT(ip->i_d.di_aformat == 0);
1152 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1153 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001154
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001155 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001156 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1157
1158 switch (ip->i_d.di_format) {
1159 case XFS_DINODE_FMT_DEV:
1160 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1161 break;
1162 case XFS_DINODE_FMT_UUID:
1163 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1164 break;
1165 case XFS_DINODE_FMT_LOCAL:
1166 case XFS_DINODE_FMT_EXTENTS:
1167 case XFS_DINODE_FMT_BTREE:
1168 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1169 if (!ip->i_d.di_forkoff)
1170 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1171 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1172 version = 2;
1173 break;
1174 default:
1175 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001176 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001177 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001178 }
1179
1180 ASSERT(ip->i_afp == NULL);
1181 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1182 ip->i_afp->if_flags = XFS_IFEXTENTS;
1183 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001184 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001185 switch (ip->i_d.di_format) {
1186 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001187 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001188 &logflags);
1189 break;
1190 case XFS_DINODE_FMT_EXTENTS:
1191 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001192 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001193 break;
1194 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001195 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001196 &logflags);
1197 break;
1198 default:
1199 error = 0;
1200 break;
1201 }
1202 if (logflags)
1203 xfs_trans_log_inode(tp, ip, logflags);
1204 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001205 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001206 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1207 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001208 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001209
1210 spin_lock(&mp->m_sb_lock);
1211 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1212 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001213 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001214 }
1215 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1216 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001217 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001218 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001219 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001220 if (log_sb)
1221 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001222 }
1223
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001224 error = xfs_defer_finish(&tp, &dfops, NULL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001225 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001226 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001227 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001228 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001229 return error;
1230
1231bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001232 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001233trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001234 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001235 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001236 return error;
1237}
1238
1239/*
1240 * Internal and external extent tree search functions.
1241 */
1242
1243/*
1244 * Read in the extents to if_extents.
1245 * All inode fields are set up by caller, we just traverse the btree
1246 * and copy the records in. If the file system cannot contain unwritten
1247 * extents, the records are checked for no "state" flags.
1248 */
1249int /* error */
1250xfs_bmap_read_extents(
1251 xfs_trans_t *tp, /* transaction pointer */
1252 xfs_inode_t *ip, /* incore inode */
1253 int whichfork) /* data or attr fork */
1254{
1255 struct xfs_btree_block *block; /* current btree block */
1256 xfs_fsblock_t bno; /* block # of "block" */
1257 xfs_buf_t *bp; /* buffer for "block" */
1258 int error; /* error return value */
1259 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1260 xfs_extnum_t i, j; /* index into the extents list */
1261 xfs_ifork_t *ifp; /* fork structure */
1262 int level; /* btree level, for checking */
1263 xfs_mount_t *mp; /* file system mount structure */
1264 __be64 *pp; /* pointer to block address */
1265 /* REFERENCED */
1266 xfs_extnum_t room; /* number of entries there's room for */
1267
Dave Chinner9e5987a2013-02-25 12:31:26 +11001268 mp = ip->i_mount;
1269 ifp = XFS_IFORK_PTR(ip, whichfork);
1270 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1271 XFS_EXTFMT_INODE(ip);
1272 block = ifp->if_broot;
1273 /*
1274 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1275 */
1276 level = be16_to_cpu(block->bb_level);
1277 ASSERT(level > 0);
1278 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1279 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001280
Dave Chinner9e5987a2013-02-25 12:31:26 +11001281 /*
1282 * Go down the tree until leaf level is reached, following the first
1283 * pointer (leftmost) at each level.
1284 */
1285 while (level-- > 0) {
1286 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1287 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1288 if (error)
1289 return error;
1290 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001291 if (level == 0)
1292 break;
1293 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1294 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001295 XFS_WANT_CORRUPTED_GOTO(mp,
1296 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001297 xfs_trans_brelse(tp, bp);
1298 }
1299 /*
1300 * Here with bp and block set to the leftmost leaf node in the tree.
1301 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001302 room = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001303 i = 0;
1304 /*
1305 * Loop over all leaf nodes. Copy information to the extent records.
1306 */
1307 for (;;) {
1308 xfs_bmbt_rec_t *frp;
1309 xfs_fsblock_t nextbno;
1310 xfs_extnum_t num_recs;
1311 xfs_extnum_t start;
1312
1313 num_recs = xfs_btree_get_numrecs(block);
1314 if (unlikely(i + num_recs > room)) {
1315 ASSERT(i + num_recs <= room);
1316 xfs_warn(ip->i_mount,
1317 "corrupt dinode %Lu, (btree extents).",
1318 (unsigned long long) ip->i_ino);
1319 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1320 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1321 goto error0;
1322 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001323 /*
1324 * Read-ahead the next leaf block, if any.
1325 */
1326 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1327 if (nextbno != NULLFSBLOCK)
1328 xfs_btree_reada_bufl(mp, nextbno, 1,
1329 &xfs_bmbt_buf_ops);
1330 /*
1331 * Copy records into the extent records.
1332 */
1333 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1334 start = i;
1335 for (j = 0; j < num_recs; j++, i++, frp++) {
1336 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1337 trp->l0 = be64_to_cpu(frp->l0);
1338 trp->l1 = be64_to_cpu(frp->l1);
1339 }
1340 if (exntf == XFS_EXTFMT_NOSTATE) {
1341 /*
1342 * Check all attribute bmap btree records and
1343 * any "older" data bmap btree records for a
1344 * set bit in the "extent flag" position.
1345 */
1346 if (unlikely(xfs_check_nostate_extents(ifp,
1347 start, num_recs))) {
1348 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1349 XFS_ERRLEVEL_LOW,
1350 ip->i_mount);
1351 goto error0;
1352 }
1353 }
1354 xfs_trans_brelse(tp, bp);
1355 bno = nextbno;
1356 /*
1357 * If we've reached the end, stop.
1358 */
1359 if (bno == NULLFSBLOCK)
1360 break;
1361 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1362 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1363 if (error)
1364 return error;
1365 block = XFS_BUF_TO_BLOCK(bp);
1366 }
Darrick J. Wong356a3222016-12-05 12:36:56 +11001367 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1368 return -EFSCORRUPTED;
Eric Sandeen5d829302016-11-08 12:59:42 +11001369 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11001370 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1371 return 0;
1372error0:
1373 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001374 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001375}
1376
Dave Chinner9e5987a2013-02-25 12:31:26 +11001377/*
1378 * Returns the file-relative block number of the first unused block(s)
1379 * in the file with at least "len" logically contiguous blocks free.
1380 * This is the lowest-address hole if the file has holes, else the first block
1381 * past the end of file.
1382 * Return 0 if the file is currently local (in-inode).
1383 */
1384int /* error */
1385xfs_bmap_first_unused(
1386 xfs_trans_t *tp, /* transaction pointer */
1387 xfs_inode_t *ip, /* incore inode */
1388 xfs_extlen_t len, /* size of hole to find */
1389 xfs_fileoff_t *first_unused, /* unused block */
1390 int whichfork) /* data or attr fork */
1391{
1392 int error; /* error return value */
1393 int idx; /* extent record index */
1394 xfs_ifork_t *ifp; /* inode fork pointer */
1395 xfs_fileoff_t lastaddr; /* last block number seen */
1396 xfs_fileoff_t lowest; /* lowest useful block */
1397 xfs_fileoff_t max; /* starting useful block */
1398 xfs_fileoff_t off; /* offset for this block */
1399 xfs_extnum_t nextents; /* number of extent entries */
1400
1401 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1402 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1403 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1404 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1405 *first_unused = 0;
1406 return 0;
1407 }
1408 ifp = XFS_IFORK_PTR(ip, whichfork);
1409 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1410 (error = xfs_iread_extents(tp, ip, whichfork)))
1411 return error;
1412 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001413 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001414 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1415 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1416 off = xfs_bmbt_get_startoff(ep);
1417 /*
1418 * See if the hole before this extent will work.
1419 */
1420 if (off >= lowest + len && off - max >= len) {
1421 *first_unused = max;
1422 return 0;
1423 }
1424 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1425 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1426 }
1427 *first_unused = max;
1428 return 0;
1429}
1430
1431/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001432 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001433 * last_block (input value) in the file.
1434 * This is not based on i_size, it is based on the extent records.
1435 * Returns 0 for local files, as they do not have extent records.
1436 */
1437int /* error */
1438xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001439 struct xfs_trans *tp, /* transaction pointer */
1440 struct xfs_inode *ip, /* incore inode */
1441 xfs_fileoff_t *last_block, /* last block */
1442 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001443{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001444 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1445 struct xfs_bmbt_irec got;
1446 xfs_extnum_t idx;
1447 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001448
Christoph Hellwig86685f72016-11-24 11:39:38 +11001449 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1450 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001451 *last_block = 0;
1452 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001453 case XFS_DINODE_FMT_BTREE:
1454 case XFS_DINODE_FMT_EXTENTS:
1455 break;
1456 default:
1457 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001458 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001459
1460 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1461 error = xfs_iread_extents(tp, ip, whichfork);
1462 if (error)
1463 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001464 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001465
1466 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1467 if (got.br_startoff <= *last_block - 1)
1468 return 0;
1469 }
1470
1471 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1472 *last_block = got.br_startoff + got.br_blockcount;
1473 return 0;
1474 }
1475
1476 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001477 return 0;
1478}
1479
Dave Chinner68988112013-08-12 20:49:42 +10001480int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001481xfs_bmap_last_extent(
1482 struct xfs_trans *tp,
1483 struct xfs_inode *ip,
1484 int whichfork,
1485 struct xfs_bmbt_irec *rec,
1486 int *is_empty)
1487{
1488 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1489 int error;
1490 int nextents;
1491
1492 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1493 error = xfs_iread_extents(tp, ip, whichfork);
1494 if (error)
1495 return error;
1496 }
1497
Eric Sandeen5d829302016-11-08 12:59:42 +11001498 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001499 if (nextents == 0) {
1500 *is_empty = 1;
1501 return 0;
1502 }
1503
1504 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1505 *is_empty = 0;
1506 return 0;
1507}
1508
1509/*
1510 * Check the last inode extent to determine whether this allocation will result
1511 * in blocks being allocated at the end of the file. When we allocate new data
1512 * blocks at the end of the file which do not start at the previous data block,
1513 * we will try to align the new blocks at stripe unit boundaries.
1514 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001515 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001516 * at, or past the EOF.
1517 */
1518STATIC int
1519xfs_bmap_isaeof(
1520 struct xfs_bmalloca *bma,
1521 int whichfork)
1522{
1523 struct xfs_bmbt_irec rec;
1524 int is_empty;
1525 int error;
1526
1527 bma->aeof = 0;
1528 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1529 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001530 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001531 return error;
1532
Dave Chinner6e708bc2013-11-22 10:41:16 +11001533 if (is_empty) {
1534 bma->aeof = 1;
1535 return 0;
1536 }
1537
Dave Chinner9e5987a2013-02-25 12:31:26 +11001538 /*
1539 * Check if we are allocation or past the last extent, or at least into
1540 * the last delayed allocated extent.
1541 */
1542 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1543 (bma->offset >= rec.br_startoff &&
1544 isnullstartblock(rec.br_startblock));
1545 return 0;
1546}
1547
1548/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001549 * Returns the file-relative block number of the first block past eof in
1550 * the file. This is not based on i_size, it is based on the extent records.
1551 * Returns 0 for local files, as they do not have extent records.
1552 */
1553int
1554xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001555 struct xfs_inode *ip,
1556 xfs_fileoff_t *last_block,
1557 int whichfork)
1558{
1559 struct xfs_bmbt_irec rec;
1560 int is_empty;
1561 int error;
1562
1563 *last_block = 0;
1564
1565 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1566 return 0;
1567
1568 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1569 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001570 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001571
1572 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1573 if (error || is_empty)
1574 return error;
1575
1576 *last_block = rec.br_startoff + rec.br_blockcount;
1577 return 0;
1578}
1579
1580/*
1581 * Returns whether the selected fork of the inode has exactly one
1582 * block or not. For the data fork we check this matches di_size,
1583 * implying the file's range is 0..bsize-1.
1584 */
1585int /* 1=>1 block, 0=>otherwise */
1586xfs_bmap_one_block(
1587 xfs_inode_t *ip, /* incore inode */
1588 int whichfork) /* data or attr fork */
1589{
1590 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1591 xfs_ifork_t *ifp; /* inode fork pointer */
1592 int rval; /* return value */
1593 xfs_bmbt_irec_t s; /* internal version of extent */
1594
1595#ifndef DEBUG
1596 if (whichfork == XFS_DATA_FORK)
1597 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1598#endif /* !DEBUG */
1599 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1600 return 0;
1601 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1602 return 0;
1603 ifp = XFS_IFORK_PTR(ip, whichfork);
1604 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1605 ep = xfs_iext_get_ext(ifp, 0);
1606 xfs_bmbt_get_all(ep, &s);
1607 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1608 if (rval && whichfork == XFS_DATA_FORK)
1609 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1610 return rval;
1611}
1612
1613/*
1614 * Extent tree manipulation functions used during allocation.
1615 */
1616
1617/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001618 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 */
1620STATIC int /* error */
1621xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001622 struct xfs_bmalloca *bma,
1623 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001625 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001627 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001630 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 xfs_fileoff_t new_endoff; /* end offset of new entry */
1632 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1633 /* left is 0, right is 1, prev is 2 */
1634 int rval=0; /* return value (logging flags) */
1635 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001636 xfs_filblks_t da_new; /* new count del alloc blocks used */
1637 xfs_filblks_t da_old; /* old count del alloc blocks used */
1638 xfs_filblks_t temp=0; /* value for da_new calculations */
1639 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001641 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001642 xfs_extnum_t *nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001644 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001645 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001646 ASSERT(whichfork != XFS_ATTR_FORK);
1647 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1648 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001649
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001650 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001651 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001652 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001653 ASSERT(!bma->cur ||
1654 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001655
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001656 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658#define LEFT r[0]
1659#define RIGHT r[1]
1660#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Darrick J. Wong60b49842016-10-03 09:11:34 -07001662 if (whichfork == XFS_COW_FORK)
1663 state |= BMAP_COWFORK;
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 /*
1666 * Set up a bunch of variables to make the tests simpler.
1667 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001668 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 xfs_bmbt_get_all(ep, &PREV);
1670 new_endoff = new->br_startoff + new->br_blockcount;
1671 ASSERT(PREV.br_startoff <= new->br_startoff);
1672 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001673
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001674 da_old = startblockval(PREV.br_startblock);
1675 da_new = 0;
1676
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /*
1678 * Set flags determining what part of the previous delayed allocation
1679 * extent is being replaced by a real allocation.
1680 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001681 if (PREV.br_startoff == new->br_startoff)
1682 state |= BMAP_LEFT_FILLING;
1683 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1684 state |= BMAP_RIGHT_FILLING;
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 /*
1687 * Check and set flags if this segment has a left neighbor.
1688 * Don't set contiguous if the combined extent would be too large.
1689 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001690 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001691 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001692 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001693
1694 if (isnullstartblock(LEFT.br_startblock))
1695 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001697
1698 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1699 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1700 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1701 LEFT.br_state == new->br_state &&
1702 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1703 state |= BMAP_LEFT_CONTIG;
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 /*
1706 * Check and set flags if this segment has a right neighbor.
1707 * Don't set contiguous if the combined extent would be too large.
1708 * Also check for all-three-contiguous being too large.
1709 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001710 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001711 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001712 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001713
1714 if (isnullstartblock(RIGHT.br_startblock))
1715 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001717
1718 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1719 new_endoff == RIGHT.br_startoff &&
1720 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1721 new->br_state == RIGHT.br_state &&
1722 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1723 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1724 BMAP_RIGHT_FILLING)) !=
1725 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1726 BMAP_RIGHT_FILLING) ||
1727 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1728 <= MAXEXTLEN))
1729 state |= BMAP_RIGHT_CONTIG;
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 error = 0;
1732 /*
1733 * Switch out based on the FILLING and CONTIG state bits.
1734 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001735 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1736 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1737 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1738 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 /*
1740 * Filling in all of a previously delayed allocation extent.
1741 * The left and right neighbors are both contiguous with new.
1742 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001743 bma->idx--;
1744 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1745 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 LEFT.br_blockcount + PREV.br_blockcount +
1747 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001748 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001749
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001750 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001751 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001752 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1754 else {
1755 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001756 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001758 RIGHT.br_blockcount, &i);
1759 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001761 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001762 error = xfs_btree_delete(bma->cur, &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_btree_decrement(bma->cur, 0, &i);
1767 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001769 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001770 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 LEFT.br_startblock,
1772 LEFT.br_blockcount +
1773 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001774 RIGHT.br_blockcount, LEFT.br_state);
1775 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto done;
1777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 break;
1779
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001780 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 /*
1782 * Filling in all of a previously delayed allocation extent.
1783 * The left neighbor is contiguous, the right is not.
1784 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001785 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001786
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001787 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1788 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001789 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001790 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001791
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001792 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1793 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 rval = XFS_ILOG_DEXT;
1795 else {
1796 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001797 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001799 &i);
1800 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001802 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001803 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 LEFT.br_startblock,
1805 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001806 PREV.br_blockcount, LEFT.br_state);
1807 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 goto done;
1809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 break;
1811
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001812 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 /*
1814 * Filling in all of a previously delayed allocation extent.
1815 * The right neighbor is contiguous, the left is not.
1816 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001817 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 xfs_bmbt_set_startblock(ep, new->br_startblock);
1819 xfs_bmbt_set_blockcount(ep,
1820 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001821 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001822
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001823 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1824 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 rval = XFS_ILOG_DEXT;
1826 else {
1827 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001828 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001830 RIGHT.br_blockcount, &i);
1831 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001833 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001834 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 new->br_startblock,
1836 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001837 RIGHT.br_blockcount, PREV.br_state);
1838 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 goto done;
1840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 break;
1842
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001843 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /*
1845 * Filling in all of a previously delayed allocation extent.
1846 * Neither the left nor right neighbors are contiguous with
1847 * the new one.
1848 */
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_startblock(ep, new->br_startblock);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08001851 xfs_bmbt_set_state(ep, new->br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001852 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001853
Darrick J. Wong60b49842016-10-03 09:11:34 -07001854 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001855 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1857 else {
1858 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001859 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001861 &i);
1862 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001864 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001865 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1866 error = xfs_btree_insert(bma->cur, &i);
1867 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001869 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 break;
1872
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001873 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 /*
1875 * Filling in the first part of a previous delayed allocation.
1876 * The left neighbor is contiguous.
1877 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001878 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1879 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 LEFT.br_blockcount + new->br_blockcount);
1881 xfs_bmbt_set_startoff(ep,
1882 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001883 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001886 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001888 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 rval = XFS_ILOG_DEXT;
1890 else {
1891 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001892 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 LEFT.br_startblock, LEFT.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 == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001898 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 LEFT.br_startblock,
1900 LEFT.br_blockcount +
1901 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001902 LEFT.br_state);
1903 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto done;
1905 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001906 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001907 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001908 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001909 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001910
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001911 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 break;
1913
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001914 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 /*
1916 * Filling in the first part of a previous delayed allocation.
1917 * The left neighbor is not contiguous.
1918 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001919 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 xfs_bmbt_set_startoff(ep, new_endoff);
1921 temp = PREV.br_blockcount - new->br_blockcount;
1922 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001923 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001924 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001925 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1927 else {
1928 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001929 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001931 &i);
1932 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001934 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001935 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1936 error = xfs_btree_insert(bma->cur, &i);
1937 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001939 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001941
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001942 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001943 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001944 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001945 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 rval |= tmp_rval;
1947 if (error)
1948 goto done;
1949 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001950 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001951 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001952 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1953 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001954 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001955 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 break;
1957
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001958 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 /*
1960 * Filling in the last part of a previous delayed allocation.
1961 * The right neighbor is contiguous with the new allocation.
1962 */
1963 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001964 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001966 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001967 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 new->br_blockcount + RIGHT.br_blockcount,
1969 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001970 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1971 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 rval = XFS_ILOG_DEXT;
1973 else {
1974 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001975 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001977 RIGHT.br_blockcount, &i);
1978 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001980 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001981 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 new->br_startblock,
1983 new->br_blockcount +
1984 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001985 RIGHT.br_state);
1986 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 goto done;
1988 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001989
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001990 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001991 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001992 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001993 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001994 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001995
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001996 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 break;
1998
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001999 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 /*
2001 * Filling in the last part of a previous delayed allocation.
2002 * The right neighbor is not contiguous.
2003 */
2004 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002005 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002007 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002008 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002009 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2011 else {
2012 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002013 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002015 &i);
2016 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002018 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002019 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2020 error = xfs_btree_insert(bma->cur, &i);
2021 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002023 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002025
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002026 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002027 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002028 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002029 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 rval |= tmp_rval;
2031 if (error)
2032 goto done;
2033 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002034 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002035 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002036 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2037 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002038 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002039 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002040
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002041 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 break;
2043
2044 case 0:
2045 /*
2046 * Filling in the middle part of a previous delayed allocation.
2047 * Contiguity is impossible here.
2048 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002049 *
2050 * We start with a delayed allocation:
2051 *
2052 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2053 * PREV @ idx
2054 *
2055 * and we are allocating:
2056 * +rrrrrrrrrrrrrrrrr+
2057 * new
2058 *
2059 * and we set it up for insertion as:
2060 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2061 * new
2062 * PREV @ idx LEFT RIGHT
2063 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 */
2065 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002067 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002068 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2069 LEFT = *new;
2070 RIGHT.br_state = PREV.br_state;
2071 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002072 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002073 RIGHT.br_startoff = new_endoff;
2074 RIGHT.br_blockcount = temp2;
2075 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002076 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002077 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002078 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2080 else {
2081 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002082 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002084 &i);
2085 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002087 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002088 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2089 error = xfs_btree_insert(bma->cur, &i);
2090 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002092 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002094
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002095 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002096 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002097 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002098 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 rval |= tmp_rval;
2100 if (error)
2101 goto done;
2102 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002103 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2104 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002105 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002106 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002107 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002108 error = xfs_mod_fdblocks(bma->ip->i_mount,
2109 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002110 ASSERT(!error);
2111 if (error)
2112 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002114
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002115 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002116 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002117 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2118 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2119 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002120 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002121 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002122
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002123 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002124 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 break;
2126
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002127 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2128 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2129 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2130 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2131 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2132 case BMAP_LEFT_CONTIG:
2133 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 /*
2135 * These cases are all impossible.
2136 */
2137 ASSERT(0);
2138 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002139
Darrick J. Wong9c194642016-08-03 12:16:05 +10002140 /* add reverse mapping */
2141 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2142 if (error)
2143 goto done;
2144
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002145 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002146 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002147 int tmp_logflags; /* partial log flag return val */
2148
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002149 ASSERT(bma->cur == NULL);
2150 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002151 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002152 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002153 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002154 if (error)
2155 goto done;
2156 }
2157
2158 /* adjust for changes in reserved delayed indirect blocks */
2159 if (da_old || da_new) {
2160 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002161 if (bma->cur)
2162 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002163 ASSERT(temp <= da_old);
2164 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002165 xfs_mod_fdblocks(bma->ip->i_mount,
2166 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002167 }
2168
2169 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002170 if (bma->cur)
2171 bma->cur->bc_private.b.allocated = 0;
2172
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002173 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002175 if (whichfork != XFS_COW_FORK)
2176 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 return error;
2178#undef LEFT
2179#undef RIGHT
2180#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181}
2182
2183/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002184 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 */
2186STATIC int /* error */
2187xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002188 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002190 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002191 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002193 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002194 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002195 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002196 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002199 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002202 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 xfs_fileoff_t new_endoff; /* end offset of new entry */
2204 xfs_exntst_t newext; /* new extent state */
2205 xfs_exntst_t oldext; /* old extent state */
2206 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2207 /* left is 0, right is 1, prev is 2 */
2208 int rval=0; /* return value (logging flags) */
2209 int state = 0;/* state bits, accessed thru macros */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002210 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002212 *logflagsp = 0;
2213
2214 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002215 ifp = XFS_IFORK_PTR(ip, whichfork);
2216 if (whichfork == XFS_COW_FORK)
2217 state |= BMAP_COWFORK;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002218
2219 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002220 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002221 ASSERT(!isnullstartblock(new->br_startblock));
2222
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002223 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225#define LEFT r[0]
2226#define RIGHT r[1]
2227#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002228
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 /*
2230 * Set up a bunch of variables to make the tests simpler.
2231 */
2232 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002233 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 xfs_bmbt_get_all(ep, &PREV);
2235 newext = new->br_state;
2236 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2237 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2238 ASSERT(PREV.br_state == oldext);
2239 new_endoff = new->br_startoff + new->br_blockcount;
2240 ASSERT(PREV.br_startoff <= new->br_startoff);
2241 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 /*
2244 * Set flags determining what part of the previous oldext allocation
2245 * extent is being replaced by a newext allocation.
2246 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002247 if (PREV.br_startoff == new->br_startoff)
2248 state |= BMAP_LEFT_FILLING;
2249 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2250 state |= BMAP_RIGHT_FILLING;
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 /*
2253 * Check and set flags if this segment has a left neighbor.
2254 * Don't set contiguous if the combined extent would be too large.
2255 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002256 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002257 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002258 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002259
2260 if (isnullstartblock(LEFT.br_startblock))
2261 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002263
2264 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2265 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2266 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2267 LEFT.br_state == newext &&
2268 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2269 state |= BMAP_LEFT_CONTIG;
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /*
2272 * Check and set flags if this segment has a right neighbor.
2273 * Don't set contiguous if the combined extent would be too large.
2274 * Also check for all-three-contiguous being too large.
2275 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002276 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002277 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002278 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002279 if (isnullstartblock(RIGHT.br_startblock))
2280 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002282
2283 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2284 new_endoff == RIGHT.br_startoff &&
2285 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2286 newext == RIGHT.br_state &&
2287 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2288 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2289 BMAP_RIGHT_FILLING)) !=
2290 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2291 BMAP_RIGHT_FILLING) ||
2292 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2293 <= MAXEXTLEN))
2294 state |= BMAP_RIGHT_CONTIG;
2295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 /*
2297 * Switch out based on the FILLING and CONTIG state bits.
2298 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002299 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2300 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2301 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2302 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 /*
2304 * Setting all of a previous oldext extent to newext.
2305 * The left and right neighbors are both contiguous with new.
2306 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002307 --*idx;
2308
2309 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2310 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 LEFT.br_blockcount + PREV.br_blockcount +
2312 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002313 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002314
Christoph Hellwigec90c552011-05-23 08:52:53 +00002315 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002316 XFS_IFORK_NEXT_SET(ip, whichfork,
2317 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (cur == NULL)
2319 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2320 else {
2321 rval = XFS_ILOG_CORE;
2322 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2323 RIGHT.br_startblock,
2324 RIGHT.br_blockcount, &i)))
2325 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002326 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002327 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002329 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002330 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002332 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002333 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002335 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002336 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002338 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2340 LEFT.br_startblock,
2341 LEFT.br_blockcount + PREV.br_blockcount +
2342 RIGHT.br_blockcount, LEFT.br_state)))
2343 goto done;
2344 }
2345 break;
2346
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002347 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 /*
2349 * Setting all of a previous oldext extent to newext.
2350 * The left neighbor is contiguous, the right is not.
2351 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002352 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002353
Christoph Hellwigec90c552011-05-23 08:52:53 +00002354 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2355 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2356 LEFT.br_blockcount + PREV.br_blockcount);
2357 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2358
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, PREV.br_startoff,
2367 PREV.br_startblock, PREV.br_blockcount,
2368 &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, LEFT.br_startoff,
2378 LEFT.br_startblock,
2379 LEFT.br_blockcount + PREV.br_blockcount,
2380 LEFT.br_state)))
2381 goto done;
2382 }
2383 break;
2384
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002385 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 /*
2387 * Setting all of a previous oldext extent to newext.
2388 * The right neighbor is contiguous, the left is not.
2389 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002390 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 xfs_bmbt_set_blockcount(ep,
2392 PREV.br_blockcount + RIGHT.br_blockcount);
2393 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002394 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2395 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002396 XFS_IFORK_NEXT_SET(ip, whichfork,
2397 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (cur == NULL)
2399 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2400 else {
2401 rval = XFS_ILOG_CORE;
2402 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2403 RIGHT.br_startblock,
2404 RIGHT.br_blockcount, &i)))
2405 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002406 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002407 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002409 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002410 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002412 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2414 new->br_startblock,
2415 new->br_blockcount + RIGHT.br_blockcount,
2416 newext)))
2417 goto done;
2418 }
2419 break;
2420
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002421 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 /*
2423 * Setting all of a previous oldext extent to newext.
2424 * Neither the left nor right neighbors are contiguous with
2425 * the new one.
2426 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002427 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002429 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if (cur == NULL)
2432 rval = XFS_ILOG_DEXT;
2433 else {
2434 rval = 0;
2435 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2436 new->br_startblock, new->br_blockcount,
2437 &i)))
2438 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002439 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2441 new->br_startblock, new->br_blockcount,
2442 newext)))
2443 goto done;
2444 }
2445 break;
2446
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002447 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 /*
2449 * Setting the first part of a previous oldext extent to newext.
2450 * The left neighbor is contiguous.
2451 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002452 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2453 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 LEFT.br_blockcount + new->br_blockcount);
2455 xfs_bmbt_set_startoff(ep,
2456 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002457 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002458
Christoph Hellwigec90c552011-05-23 08:52:53 +00002459 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 xfs_bmbt_set_startblock(ep,
2461 new->br_startblock + new->br_blockcount);
2462 xfs_bmbt_set_blockcount(ep,
2463 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002464 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002465
Christoph Hellwigec90c552011-05-23 08:52:53 +00002466 --*idx;
2467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (cur == NULL)
2469 rval = XFS_ILOG_DEXT;
2470 else {
2471 rval = 0;
2472 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2473 PREV.br_startblock, PREV.br_blockcount,
2474 &i)))
2475 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002476 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if ((error = xfs_bmbt_update(cur,
2478 PREV.br_startoff + new->br_blockcount,
2479 PREV.br_startblock + new->br_blockcount,
2480 PREV.br_blockcount - new->br_blockcount,
2481 oldext)))
2482 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002483 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002485 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 LEFT.br_startblock,
2487 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002488 LEFT.br_state);
2489 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 goto done;
2491 }
2492 break;
2493
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002494 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 /*
2496 * Setting the first part of a previous oldext extent to newext.
2497 * The left neighbor is not contiguous.
2498 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002499 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2501 xfs_bmbt_set_startoff(ep, new_endoff);
2502 xfs_bmbt_set_blockcount(ep,
2503 PREV.br_blockcount - new->br_blockcount);
2504 xfs_bmbt_set_startblock(ep,
2505 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002506 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002507
Christoph Hellwigec90c552011-05-23 08:52:53 +00002508 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002509 XFS_IFORK_NEXT_SET(ip, whichfork,
2510 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (cur == NULL)
2512 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2513 else {
2514 rval = XFS_ILOG_CORE;
2515 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2516 PREV.br_startblock, PREV.br_blockcount,
2517 &i)))
2518 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002519 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if ((error = xfs_bmbt_update(cur,
2521 PREV.br_startoff + new->br_blockcount,
2522 PREV.br_startblock + new->br_blockcount,
2523 PREV.br_blockcount - new->br_blockcount,
2524 oldext)))
2525 goto done;
2526 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002527 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002529 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 }
2531 break;
2532
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002533 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 /*
2535 * Setting the last part of a previous oldext extent to newext.
2536 * The right neighbor is contiguous with the new allocation.
2537 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002538 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 xfs_bmbt_set_blockcount(ep,
2540 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002541 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2542
2543 ++*idx;
2544
2545 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2546 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002547 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002549 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002550
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if (cur == NULL)
2552 rval = XFS_ILOG_DEXT;
2553 else {
2554 rval = 0;
2555 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2556 PREV.br_startblock,
2557 PREV.br_blockcount, &i)))
2558 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002559 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2561 PREV.br_startblock,
2562 PREV.br_blockcount - new->br_blockcount,
2563 oldext)))
2564 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002565 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 goto done;
2567 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2568 new->br_startblock,
2569 new->br_blockcount + RIGHT.br_blockcount,
2570 newext)))
2571 goto done;
2572 }
2573 break;
2574
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002575 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 /*
2577 * Setting the last part of a previous oldext extent to newext.
2578 * The right neighbor is not contiguous.
2579 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002580 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 xfs_bmbt_set_blockcount(ep,
2582 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002583 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002584
Christoph Hellwigec90c552011-05-23 08:52:53 +00002585 ++*idx;
2586 xfs_iext_insert(ip, *idx, 1, new, state);
2587
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002588 XFS_IFORK_NEXT_SET(ip, whichfork,
2589 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (cur == NULL)
2591 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2592 else {
2593 rval = XFS_ILOG_CORE;
2594 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2595 PREV.br_startblock, PREV.br_blockcount,
2596 &i)))
2597 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002598 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2600 PREV.br_startblock,
2601 PREV.br_blockcount - new->br_blockcount,
2602 oldext)))
2603 goto done;
2604 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2605 new->br_startblock, new->br_blockcount,
2606 &i)))
2607 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002608 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002610 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002612 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
2614 break;
2615
2616 case 0:
2617 /*
2618 * Setting the middle part of a previous oldext extent to
2619 * newext. Contiguity is impossible here.
2620 * One extent becomes three extents.
2621 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002622 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 xfs_bmbt_set_blockcount(ep,
2624 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002625 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 r[0] = *new;
2628 r[1].br_startoff = new_endoff;
2629 r[1].br_blockcount =
2630 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2631 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2632 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002633
2634 ++*idx;
2635 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2636
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002637 XFS_IFORK_NEXT_SET(ip, whichfork,
2638 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if (cur == NULL)
2640 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2641 else {
2642 rval = XFS_ILOG_CORE;
2643 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2644 PREV.br_startblock, PREV.br_blockcount,
2645 &i)))
2646 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002647 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 /* new right extent - oldext */
2649 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2650 r[1].br_startblock, r[1].br_blockcount,
2651 r[1].br_state)))
2652 goto done;
2653 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002655 cur->bc_rec.b.br_blockcount =
2656 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002657 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002659 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002660 /*
2661 * Reset the cursor to the position of the new extent
2662 * we are about to insert as we can't trust it after
2663 * the previous insert.
2664 */
2665 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2666 new->br_startblock, new->br_blockcount,
2667 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002669 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002671 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002672 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002674 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
2676 break;
2677
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002678 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2679 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2680 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2681 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2682 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2683 case BMAP_LEFT_CONTIG:
2684 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 /*
2686 * These cases are all impossible.
2687 */
2688 ASSERT(0);
2689 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002690
Darrick J. Wong9c194642016-08-03 12:16:05 +10002691 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002692 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002693 if (error)
2694 goto done;
2695
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002696 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002697 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002698 int tmp_logflags; /* partial log flag return val */
2699
2700 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002701 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002702 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002703 *logflagsp |= tmp_logflags;
2704 if (error)
2705 goto done;
2706 }
2707
2708 /* clear out the allocated field, done with it now in any case. */
2709 if (cur) {
2710 cur->bc_private.b.allocated = 0;
2711 *curp = cur;
2712 }
2713
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002714 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002716 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 return error;
2718#undef LEFT
2719#undef RIGHT
2720#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
2723/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002724 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002726STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727xfs_bmap_add_extent_hole_delay(
2728 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002729 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002730 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002731 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002733 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2735 xfs_filblks_t newlen=0; /* new indirect size */
2736 xfs_filblks_t oldlen=0; /* old indirect size */
2737 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2738 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002739 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002741 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002743 if (whichfork == XFS_COW_FORK)
2744 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002745 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 /*
2748 * Check and set flags if this segment has a left neighbor
2749 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002750 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002751 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002752 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002753
2754 if (isnullstartblock(left.br_startblock))
2755 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 /*
2759 * Check and set flags if the current (right) segment exists.
2760 * If it doesn't exist, we're converting the hole at end-of-file.
2761 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002762 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002763 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002764 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002765
2766 if (isnullstartblock(right.br_startblock))
2767 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /*
2771 * Set contiguity flags on the left and right neighbors.
2772 * Don't let extents get too large, even if the pieces are contiguous.
2773 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002774 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2775 left.br_startoff + left.br_blockcount == new->br_startoff &&
2776 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2777 state |= BMAP_LEFT_CONTIG;
2778
2779 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2780 new->br_startoff + new->br_blockcount == right.br_startoff &&
2781 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2782 (!(state & BMAP_LEFT_CONTIG) ||
2783 (left.br_blockcount + new->br_blockcount +
2784 right.br_blockcount <= MAXEXTLEN)))
2785 state |= BMAP_RIGHT_CONTIG;
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 /*
2788 * Switch out based on the contiguity flags.
2789 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002790 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2791 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 /*
2793 * New allocation is contiguous with delayed allocations
2794 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002795 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002797 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 temp = left.br_blockcount + new->br_blockcount +
2799 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002800
Christoph Hellwigec90c552011-05-23 08:52:53 +00002801 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2802 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002803 oldlen = startblockval(left.br_startblock) +
2804 startblockval(new->br_startblock) +
2805 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002806 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2807 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002808 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002809 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002810 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002811
Christoph Hellwigec90c552011-05-23 08:52:53 +00002812 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 break;
2814
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002815 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 /*
2817 * New allocation is contiguous with a delayed allocation
2818 * on the left.
2819 * Merge the new allocation with the left neighbor.
2820 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002821 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002823
2824 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2825 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002826 oldlen = startblockval(left.br_startblock) +
2827 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002828 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2829 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002830 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002831 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002832 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 break;
2834
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002835 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 /*
2837 * New allocation is contiguous with a delayed allocation
2838 * on the right.
2839 * Merge the new allocation with the right neighbor.
2840 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002841 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002843 oldlen = startblockval(new->br_startblock) +
2844 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002845 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2846 oldlen);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002847 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2848 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002849 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002850 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 break;
2852
2853 case 0:
2854 /*
2855 * New allocation is not contiguous with another
2856 * delayed allocation.
2857 * Insert a new entry.
2858 */
2859 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002860 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 break;
2862 }
2863 if (oldlen != newlen) {
2864 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002865 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2866 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 /*
2868 * Nothing to do for disk quota accounting here.
2869 */
2870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
2873/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002874 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 */
2876STATIC int /* error */
2877xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002878 struct xfs_bmalloca *bma,
2879 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002881 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 int i; /* temp state */
2884 xfs_ifork_t *ifp; /* inode fork pointer */
2885 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2886 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002887 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002889 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Eric Sandeenf1f96c42016-01-04 16:10:42 +11002891 mp = bma->ip->i_mount;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002892 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002893
Christoph Hellwigc6534242011-09-18 20:41:05 +00002894 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002895 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002896 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002897 ASSERT(!bma->cur ||
2898 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002899
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002900 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002901
2902 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002903 if (whichfork == XFS_ATTR_FORK)
2904 state |= BMAP_ATTRFORK;
Christoph Hellwiga14234c2017-02-06 10:50:49 -08002905 if (whichfork == XFS_COW_FORK)
2906 state |= BMAP_COWFORK;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002907
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 /*
2909 * Check and set flags if this segment has a left neighbor.
2910 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002911 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002912 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002913 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002914 if (isnullstartblock(left.br_startblock))
2915 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 /*
2919 * Check and set flags if this segment has a current value.
2920 * Not true if we're inserting into the "hole" at eof.
2921 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002922 if (bma->idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002923 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002924 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002925 if (isnullstartblock(right.br_startblock))
2926 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002928
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 /*
2930 * We're inserting a real allocation between "left" and "right".
2931 * Set the contiguity flags. Don't let extents get too large.
2932 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002933 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2934 left.br_startoff + left.br_blockcount == new->br_startoff &&
2935 left.br_startblock + left.br_blockcount == new->br_startblock &&
2936 left.br_state == new->br_state &&
2937 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2938 state |= BMAP_LEFT_CONTIG;
2939
2940 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2941 new->br_startoff + new->br_blockcount == right.br_startoff &&
2942 new->br_startblock + new->br_blockcount == right.br_startblock &&
2943 new->br_state == right.br_state &&
2944 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2945 (!(state & BMAP_LEFT_CONTIG) ||
2946 left.br_blockcount + new->br_blockcount +
2947 right.br_blockcount <= MAXEXTLEN))
2948 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002950 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 /*
2952 * Select which case we're in here, and implement it.
2953 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002954 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2955 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 /*
2957 * New allocation is contiguous with real allocations on the
2958 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002959 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002961 --bma->idx;
2962 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2963 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 left.br_blockcount + new->br_blockcount +
2965 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002966 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002967
Christoph Hellwigc6534242011-09-18 20:41:05 +00002968 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002969
Christoph Hellwigc6534242011-09-18 20:41:05 +00002970 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
2971 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
2972 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002973 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002974 } else {
2975 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002976 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
2977 right.br_startblock, right.br_blockcount,
2978 &i);
2979 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002980 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002981 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002982 error = xfs_btree_delete(bma->cur, &i);
2983 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002984 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002985 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002986 error = xfs_btree_decrement(bma->cur, 0, &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 Hellwigc6534242011-09-18 20:41:05 +00002990 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002991 left.br_startblock,
2992 left.br_blockcount +
2993 new->br_blockcount +
2994 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002995 left.br_state);
2996 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002997 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002999 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003001 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 /*
3003 * New allocation is contiguous with a real allocation
3004 * on the left.
3005 * Merge the new allocation with the left neighbor.
3006 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003007 --bma->idx;
3008 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3009 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003011 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003012
Christoph Hellwigc6534242011-09-18 20:41:05 +00003013 if (bma->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 Hellwigc6534242011-09-18 20:41:05 +00003017 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3018 left.br_startblock, left.br_blockcount,
3019 &i);
3020 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003021 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003022 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003023 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003024 left.br_startblock,
3025 left.br_blockcount +
3026 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003027 left.br_state);
3028 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003029 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003033 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 /*
3035 * New allocation is contiguous with a real allocation
3036 * on the right.
3037 * Merge the new allocation with the right neighbor.
3038 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003039 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3040 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003041 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 new->br_blockcount + right.br_blockcount,
3043 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003044 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003045
Christoph Hellwigc6534242011-09-18 20:41:05 +00003046 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003047 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003048 } else {
3049 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003050 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003051 right.br_startoff,
3052 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003053 right.br_blockcount, &i);
3054 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003055 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003056 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003057 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003058 new->br_startblock,
3059 new->br_blockcount +
3060 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003061 right.br_state);
3062 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003063 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003065 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
3067 case 0:
3068 /*
3069 * New allocation is not contiguous with another
3070 * real allocation.
3071 * Insert a new entry.
3072 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003073 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3074 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3075 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3076 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003077 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003078 } else {
3079 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003080 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003081 new->br_startoff,
3082 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003083 new->br_blockcount, &i);
3084 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003085 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003086 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003087 bma->cur->bc_rec.b.br_state = new->br_state;
3088 error = xfs_btree_insert(bma->cur, &i);
3089 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003090 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003091 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003093 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003095
Darrick J. Wong9c194642016-08-03 12:16:05 +10003096 /* add reverse mapping */
3097 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
3098 if (error)
3099 goto done;
3100
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003101 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003102 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003103 int tmp_logflags; /* partial log flag return val */
3104
Christoph Hellwigc6534242011-09-18 20:41:05 +00003105 ASSERT(bma->cur == NULL);
3106 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003107 bma->firstblock, bma->dfops, &bma->cur,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003108 0, &tmp_logflags, whichfork);
3109 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003110 if (error)
3111 goto done;
3112 }
3113
3114 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003115 if (bma->cur)
3116 bma->cur->bc_private.b.allocated = 0;
3117
3118 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003119done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003120 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003121 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122}
3123
Nathan Scottdd9f4382006-01-11 15:28:28 +11003124/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003125 * Functions used in the extent read, allocate and remove paths
3126 */
3127
3128/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003129 * Adjust the size of the new extent based on di_extsize and rt extsize.
3130 */
Dave Chinner68988112013-08-12 20:49:42 +10003131int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003132xfs_bmap_extsize_align(
3133 xfs_mount_t *mp,
3134 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3135 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3136 xfs_extlen_t extsz, /* align to this extent size */
3137 int rt, /* is this a realtime inode? */
3138 int eof, /* is extent at end-of-file? */
3139 int delay, /* creating delalloc extent? */
3140 int convert, /* overwriting unwritten extent? */
3141 xfs_fileoff_t *offp, /* in/out: aligned offset */
3142 xfs_extlen_t *lenp) /* in/out: aligned length */
3143{
3144 xfs_fileoff_t orig_off; /* original offset */
3145 xfs_extlen_t orig_alen; /* original length */
3146 xfs_fileoff_t orig_end; /* original off+len */
3147 xfs_fileoff_t nexto; /* next file offset */
3148 xfs_fileoff_t prevo; /* previous file offset */
3149 xfs_fileoff_t align_off; /* temp for offset */
3150 xfs_extlen_t align_alen; /* temp for length */
3151 xfs_extlen_t temp; /* temp for calculations */
3152
3153 if (convert)
3154 return 0;
3155
3156 orig_off = align_off = *offp;
3157 orig_alen = align_alen = *lenp;
3158 orig_end = orig_off + orig_alen;
3159
3160 /*
3161 * If this request overlaps an existing extent, then don't
3162 * attempt to perform any additional alignment.
3163 */
3164 if (!delay && !eof &&
3165 (orig_off >= gotp->br_startoff) &&
3166 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3167 return 0;
3168 }
3169
3170 /*
3171 * If the file offset is unaligned vs. the extent size
3172 * we need to align it. This will be possible unless
3173 * the file was previously written with a kernel that didn't
3174 * perform this alignment, or if a truncate shot us in the
3175 * foot.
3176 */
3177 temp = do_mod(orig_off, extsz);
3178 if (temp) {
3179 align_alen += temp;
3180 align_off -= temp;
3181 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003182
3183 /* Same adjustment for the end of the requested area. */
3184 temp = (align_alen % extsz);
3185 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003186 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003187
3188 /*
3189 * For large extent hint sizes, the aligned extent might be larger than
3190 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3191 * the length back under MAXEXTLEN. The outer allocation loops handle
3192 * short allocation just fine, so it is safe to do this. We only want to
3193 * do it when we are forced to, though, because it means more allocation
3194 * operations are required.
3195 */
3196 while (align_alen > MAXEXTLEN)
3197 align_alen -= extsz;
3198 ASSERT(align_alen <= MAXEXTLEN);
3199
Nathan Scottdd9f4382006-01-11 15:28:28 +11003200 /*
3201 * If the previous block overlaps with this proposed allocation
3202 * then move the start forward without adjusting the length.
3203 */
3204 if (prevp->br_startoff != NULLFILEOFF) {
3205 if (prevp->br_startblock == HOLESTARTBLOCK)
3206 prevo = prevp->br_startoff;
3207 else
3208 prevo = prevp->br_startoff + prevp->br_blockcount;
3209 } else
3210 prevo = 0;
3211 if (align_off != orig_off && align_off < prevo)
3212 align_off = prevo;
3213 /*
3214 * If the next block overlaps with this proposed allocation
3215 * then move the start back without adjusting the length,
3216 * but not before offset 0.
3217 * This may of course make the start overlap previous block,
3218 * and if we hit the offset 0 limit then the next block
3219 * can still overlap too.
3220 */
3221 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3222 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3223 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3224 nexto = gotp->br_startoff + gotp->br_blockcount;
3225 else
3226 nexto = gotp->br_startoff;
3227 } else
3228 nexto = NULLFILEOFF;
3229 if (!eof &&
3230 align_off + align_alen != orig_end &&
3231 align_off + align_alen > nexto)
3232 align_off = nexto > align_alen ? nexto - align_alen : 0;
3233 /*
3234 * If we're now overlapping the next or previous extent that
3235 * means we can't fit an extsz piece in this hole. Just move
3236 * the start forward to the first valid spot and set
3237 * the length so we hit the end.
3238 */
3239 if (align_off != orig_off && align_off < prevo)
3240 align_off = prevo;
3241 if (align_off + align_alen != orig_end &&
3242 align_off + align_alen > nexto &&
3243 nexto != NULLFILEOFF) {
3244 ASSERT(nexto > prevo);
3245 align_alen = nexto - align_off;
3246 }
3247
3248 /*
3249 * If realtime, and the result isn't a multiple of the realtime
3250 * extent size we need to remove blocks until it is.
3251 */
3252 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3253 /*
3254 * We're not covering the original request, or
3255 * we won't be able to once we fix the length.
3256 */
3257 if (orig_off < align_off ||
3258 orig_end > align_off + align_alen ||
3259 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003260 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003261 /*
3262 * Try to fix it by moving the start up.
3263 */
3264 if (align_off + temp <= orig_off) {
3265 align_alen -= temp;
3266 align_off += temp;
3267 }
3268 /*
3269 * Try to fix it by moving the end in.
3270 */
3271 else if (align_off + align_alen - temp >= orig_end)
3272 align_alen -= temp;
3273 /*
3274 * Set the start to the minimum then trim the length.
3275 */
3276 else {
3277 align_alen -= orig_off - align_off;
3278 align_off = orig_off;
3279 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3280 }
3281 /*
3282 * Result doesn't cover the request, fail it.
3283 */
3284 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003285 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003286 } else {
3287 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003288 /* see MAXEXTLEN handling above */
3289 ASSERT(orig_end <= align_off + align_alen ||
3290 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003291 }
3292
3293#ifdef DEBUG
3294 if (!eof && gotp->br_startoff != NULLFILEOFF)
3295 ASSERT(align_off + align_alen <= gotp->br_startoff);
3296 if (prevp->br_startoff != NULLFILEOFF)
3297 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3298#endif
3299
3300 *lenp = align_alen;
3301 *offp = align_off;
3302 return 0;
3303}
3304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305#define XFS_ALLOC_GAP_UNITS 4
3306
Dave Chinner68988112013-08-12 20:49:42 +10003307void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003308xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003309 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310{
3311 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3313 xfs_mount_t *mp; /* mount point structure */
3314 int nullfb; /* true if ap->firstblock isn't set */
3315 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317#define ISVALID(x,y) \
3318 (rt ? \
3319 (x) < mp->m_sb.sb_rblocks : \
3320 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3321 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3322 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003325 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003326 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3327 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003328 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 /*
3330 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003331 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003333 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3334 !isnullstartblock(ap->prev.br_startblock) &&
3335 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3336 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003337 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 /*
3339 * Adjust for the gap between prevp and us.
3340 */
Dave Chinner3a756672011-09-18 20:40:58 +00003341 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003342 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003344 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3345 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 }
3347 /*
3348 * If not at eof, then compare the two neighbor blocks.
3349 * Figure out whether either one gives us a good starting point,
3350 * and pick the better one.
3351 */
3352 else if (!ap->eof) {
3353 xfs_fsblock_t gotbno; /* right side block number */
3354 xfs_fsblock_t gotdiff=0; /* right side difference */
3355 xfs_fsblock_t prevbno; /* left side block number */
3356 xfs_fsblock_t prevdiff=0; /* left side difference */
3357
3358 /*
3359 * If there's a previous (left) block, select a requested
3360 * start block based on it.
3361 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003362 if (ap->prev.br_startoff != NULLFILEOFF &&
3363 !isnullstartblock(ap->prev.br_startblock) &&
3364 (prevbno = ap->prev.br_startblock +
3365 ap->prev.br_blockcount) &&
3366 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 /*
3368 * Calculate gap to end of previous block.
3369 */
Dave Chinner3a756672011-09-18 20:40:58 +00003370 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003371 (ap->prev.br_startoff +
3372 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 /*
3374 * Figure the startblock based on the previous block's
3375 * end and the gap size.
3376 * Heuristic!
3377 * If the gap is large relative to the piece we're
3378 * allocating, or using it gives us an invalid block
3379 * number, then just use the end of the previous block.
3380 */
Dave Chinner3a756672011-09-18 20:40:58 +00003381 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003383 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 prevbno += adjust;
3385 else
3386 prevdiff += adjust;
3387 /*
3388 * If the firstblock forbids it, can't use it,
3389 * must use default.
3390 */
3391 if (!rt && !nullfb &&
3392 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3393 prevbno = NULLFSBLOCK;
3394 }
3395 /*
3396 * No previous block or can't follow it, just default.
3397 */
3398 else
3399 prevbno = NULLFSBLOCK;
3400 /*
3401 * If there's a following (right) block, select a requested
3402 * start block based on it.
3403 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003404 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 /*
3406 * Calculate gap to start of next block.
3407 */
Dave Chinner3a756672011-09-18 20:40:58 +00003408 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 /*
3410 * Figure the startblock based on the next block's
3411 * start and the gap size.
3412 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003413 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 /*
3415 * Heuristic!
3416 * If the gap is large relative to the piece we're
3417 * allocating, or using it gives us an invalid block
3418 * number, then just use the start of the next block
3419 * offset by our length.
3420 */
Dave Chinner3a756672011-09-18 20:40:58 +00003421 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 ISVALID(gotbno - gotdiff, gotbno))
3423 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003424 else if (ISVALID(gotbno - ap->length, gotbno)) {
3425 gotbno -= ap->length;
3426 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 } else
3428 gotdiff += adjust;
3429 /*
3430 * If the firstblock forbids it, can't use it,
3431 * must use default.
3432 */
3433 if (!rt && !nullfb &&
3434 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3435 gotbno = NULLFSBLOCK;
3436 }
3437 /*
3438 * No next block, just default.
3439 */
3440 else
3441 gotbno = NULLFSBLOCK;
3442 /*
3443 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003444 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 */
3446 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003447 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003449 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003451 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003453#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003454}
3455
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003456static int
3457xfs_bmap_longest_free_extent(
3458 struct xfs_trans *tp,
3459 xfs_agnumber_t ag,
3460 xfs_extlen_t *blen,
3461 int *notinit)
3462{
3463 struct xfs_mount *mp = tp->t_mountp;
3464 struct xfs_perag *pag;
3465 xfs_extlen_t longest;
3466 int error = 0;
3467
3468 pag = xfs_perag_get(mp, ag);
3469 if (!pag->pagf_init) {
3470 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3471 if (error)
3472 goto out;
3473
3474 if (!pag->pagf_init) {
3475 *notinit = 1;
3476 goto out;
3477 }
3478 }
3479
Dave Chinner50adbcb2015-06-22 10:04:31 +10003480 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003481 xfs_alloc_min_freelist(mp, pag),
3482 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003483 if (*blen < longest)
3484 *blen = longest;
3485
3486out:
3487 xfs_perag_put(pag);
3488 return error;
3489}
3490
3491static void
3492xfs_bmap_select_minlen(
3493 struct xfs_bmalloca *ap,
3494 struct xfs_alloc_arg *args,
3495 xfs_extlen_t *blen,
3496 int notinit)
3497{
3498 if (notinit || *blen < ap->minlen) {
3499 /*
3500 * Since we did a BUF_TRYLOCK above, it is possible that
3501 * there is space for this request.
3502 */
3503 args->minlen = ap->minlen;
3504 } else if (*blen < args->maxlen) {
3505 /*
3506 * If the best seen length is less than the request length,
3507 * use the best as the minimum.
3508 */
3509 args->minlen = *blen;
3510 } else {
3511 /*
3512 * Otherwise we've seen an extent as big as maxlen, use that
3513 * as the minimum.
3514 */
3515 args->minlen = args->maxlen;
3516 }
3517}
3518
Nathan Scotta365bdd2006-03-14 13:34:16 +11003519STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003520xfs_bmap_btalloc_nullfb(
3521 struct xfs_bmalloca *ap,
3522 struct xfs_alloc_arg *args,
3523 xfs_extlen_t *blen)
3524{
3525 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003526 xfs_agnumber_t ag, startag;
3527 int notinit = 0;
3528 int error;
3529
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003530 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003531 args->total = ap->total;
3532
Christoph Hellwigc467c042010-02-15 23:34:42 +00003533 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3534 if (startag == NULLAGNUMBER)
3535 startag = ag = 0;
3536
Dave Chinner14b064c2011-01-27 12:16:28 +11003537 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003538 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3539 &notinit);
3540 if (error)
3541 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003542
Christoph Hellwigc467c042010-02-15 23:34:42 +00003543 if (++ag == mp->m_sb.sb_agcount)
3544 ag = 0;
3545 if (ag == startag)
3546 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003547 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003548
3549 xfs_bmap_select_minlen(ap, args, blen, notinit);
3550 return 0;
3551}
3552
3553STATIC int
3554xfs_bmap_btalloc_filestreams(
3555 struct xfs_bmalloca *ap,
3556 struct xfs_alloc_arg *args,
3557 xfs_extlen_t *blen)
3558{
3559 struct xfs_mount *mp = ap->ip->i_mount;
3560 xfs_agnumber_t ag;
3561 int notinit = 0;
3562 int error;
3563
3564 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3565 args->total = ap->total;
3566
3567 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3568 if (ag == NULLAGNUMBER)
3569 ag = 0;
3570
3571 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3572 if (error)
3573 return error;
3574
3575 if (*blen < args->maxlen) {
3576 error = xfs_filestream_new_ag(ap, &ag);
3577 if (error)
3578 return error;
3579
3580 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3581 &notinit);
3582 if (error)
3583 return error;
3584
3585 }
3586
3587 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003588
3589 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003590 * Set the failure fallback case to look in the selected AG as stream
3591 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003592 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003593 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003594 return 0;
3595}
3596
3597STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003598xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003599 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003600{
3601 xfs_mount_t *mp; /* mount point structure */
3602 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003603 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003604 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003605 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003606 xfs_alloc_arg_t args;
3607 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003608 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003609 int nullfb; /* true if ap->firstblock isn't set */
3610 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003611 int tryagain;
3612 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003613 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003614
Dave Chinnera99ebf42011-12-01 11:24:20 +00003615 ASSERT(ap->length);
3616
Nathan Scotta365bdd2006-03-14 13:34:16 +11003617 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003618
3619 /* stripe alignment for allocation is determined by mount parameters */
3620 stripe_align = 0;
3621 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3622 stripe_align = mp->m_swidth;
3623 else if (mp->m_dalign)
3624 stripe_align = mp->m_dalign;
3625
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003626 if (ap->flags & XFS_BMAPI_COWFORK)
3627 align = xfs_get_cowextsz_hint(ap->ip);
3628 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003629 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003630 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003631 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003632 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003633 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003634 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003635 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003636 }
Dave Chinner33177f052013-12-12 16:34:36 +11003637
3638
Dave Chinner0937e0f2011-09-18 20:40:57 +00003639 nullfb = *ap->firstblock == NULLFSBLOCK;
3640 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003641 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003642 if (xfs_alloc_is_userdata(ap->datatype) &&
3643 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003644 ag = xfs_filestream_lookup_ag(ap->ip);
3645 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003646 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003647 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003648 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003649 }
3650 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003651 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003652
3653 xfs_bmap_adjacent(ap);
3654
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003656 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 * it's in the right allocation group.
3658 */
Dave Chinner3a756672011-09-18 20:40:58 +00003659 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 ;
3661 else
Dave Chinner3a756672011-09-18 20:40:58 +00003662 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 * Normal allocation, done through xfs_alloc_vextent.
3665 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003666 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003667 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003668 args.tp = ap->tp;
3669 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003670 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003671 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003672
3673 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003674 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003675 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003676 blen = 0;
3677 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003678 /*
3679 * Search for an allocation group with a single extent large
3680 * enough for the request. If one isn't found, then adjust
3681 * the minimum allocation size to the largest space found.
3682 */
Dave Chinner292378e2016-09-26 08:21:28 +10003683 if (xfs_alloc_is_userdata(ap->datatype) &&
3684 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003685 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3686 else
3687 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003688 if (error)
3689 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003690 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003691 if (xfs_inode_is_filestream(ap->ip))
3692 args.type = XFS_ALLOCTYPE_FIRST_AG;
3693 else
3694 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003695 args.total = args.minlen = ap->minlen;
3696 } else {
3697 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3698 args.total = ap->total;
3699 args.minlen = ap->minlen;
3700 }
David Chinner957d0eb2007-06-18 16:50:37 +10003701 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003702 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003703 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003704 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003705 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003706 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003707 args.prod = 1;
3708 args.mod = 0;
3709 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003710 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003711 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003712 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3713 }
3714 /*
3715 * If we are not low on available data blocks, and the
3716 * underlying logical volume manager is a stripe, and
3717 * the file offset is zero then try to allocate data
3718 * blocks on stripe unit boundary.
3719 * NOTE: ap->aeof is only set if the allocation length
3720 * is >= the stripe unit and the allocation offset is
3721 * at the end of file.
3722 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003723 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003724 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003725 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003726 atype = args.type;
3727 isaligned = 1;
3728 /*
3729 * Adjust for alignment
3730 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003731 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003732 args.minlen = blen - args.alignment;
3733 args.minalignslop = 0;
3734 } else {
3735 /*
3736 * First try an exact bno allocation.
3737 * If it fails then do a near or start bno
3738 * allocation with alignment turned on.
3739 */
3740 atype = args.type;
3741 tryagain = 1;
3742 args.type = XFS_ALLOCTYPE_THIS_BNO;
3743 args.alignment = 1;
3744 /*
3745 * Compute the minlen+alignment for the
3746 * next case. Set slop so that the value
3747 * of minlen+alignment+slop doesn't go up
3748 * between the calls.
3749 */
Dave Chinner33177f052013-12-12 16:34:36 +11003750 if (blen > stripe_align && blen <= args.maxlen)
3751 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003752 else
3753 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003754 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003755 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003756 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003757 args.minlen - 1;
3758 else
3759 args.minalignslop = 0;
3760 }
3761 } else {
3762 args.alignment = 1;
3763 args.minalignslop = 0;
3764 }
3765 args.minleft = ap->minleft;
3766 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003767 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003768 args.datatype = ap->datatype;
3769 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003770 args.ip = ap->ip;
3771
3772 error = xfs_alloc_vextent(&args);
3773 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003774 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003775
Nathan Scotta365bdd2006-03-14 13:34:16 +11003776 if (tryagain && args.fsbno == NULLFSBLOCK) {
3777 /*
3778 * Exact allocation failed. Now try with alignment
3779 * turned on.
3780 */
3781 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003782 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003783 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003784 args.minlen = nextminlen;
3785 args.minalignslop = 0;
3786 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 if ((error = xfs_alloc_vextent(&args)))
3788 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003789 }
3790 if (isaligned && args.fsbno == NULLFSBLOCK) {
3791 /*
3792 * allocation failed, so turn off alignment and
3793 * try again.
3794 */
3795 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003796 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003797 args.alignment = 0;
3798 if ((error = xfs_alloc_vextent(&args)))
3799 return error;
3800 }
3801 if (args.fsbno == NULLFSBLOCK && nullfb &&
3802 args.minlen > ap->minlen) {
3803 args.minlen = ap->minlen;
3804 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003805 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003806 if ((error = xfs_alloc_vextent(&args)))
3807 return error;
3808 }
3809 if (args.fsbno == NULLFSBLOCK && nullfb) {
3810 args.fsbno = 0;
3811 args.type = XFS_ALLOCTYPE_FIRST_AG;
3812 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003813 if ((error = xfs_alloc_vextent(&args)))
3814 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003815 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003816 }
3817 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003818 /*
3819 * check the allocation happened at the same or higher AG than
3820 * the first block that was allocated.
3821 */
3822 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003823 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3824 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003825
Dave Chinner3a756672011-09-18 20:40:58 +00003826 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003827 if (*ap->firstblock == NULLFSBLOCK)
3828 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003829 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003830 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003831 if (!(ap->flags & XFS_BMAPI_COWFORK))
3832 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003833 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3834 if (ap->wasdel)
3835 ap->ip->i_delayed_blks -= args.len;
3836 /*
3837 * Adjust the disk quota also. This was reserved
3838 * earlier.
3839 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003840 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003841 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3842 XFS_TRANS_DQ_BCOUNT,
3843 (long) args.len);
3844 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003845 ap->blkno = NULLFSBLOCK;
3846 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 }
3848 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003849}
3850
3851/*
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003852 * For a remap operation, just "allocate" an extent at the address that the
3853 * caller passed in, and ensure that the AGFL is the right size. The caller
3854 * will then map the "allocated" extent into the file somewhere.
3855 */
3856STATIC int
3857xfs_bmap_remap_alloc(
3858 struct xfs_bmalloca *ap)
3859{
3860 struct xfs_trans *tp = ap->tp;
3861 struct xfs_mount *mp = tp->t_mountp;
3862 xfs_agblock_t bno;
3863 struct xfs_alloc_arg args;
3864 int error;
3865
3866 /*
3867 * validate that the block number is legal - the enables us to detect
3868 * and handle a silent filesystem corruption rather than crashing.
3869 */
3870 memset(&args, 0, sizeof(struct xfs_alloc_arg));
3871 args.tp = ap->tp;
3872 args.mp = ap->tp->t_mountp;
3873 bno = *ap->firstblock;
3874 args.agno = XFS_FSB_TO_AGNO(mp, bno);
3875 args.agbno = XFS_FSB_TO_AGBNO(mp, bno);
3876 if (args.agno >= mp->m_sb.sb_agcount ||
3877 args.agbno >= mp->m_sb.sb_agblocks)
3878 return -EFSCORRUPTED;
3879
3880 /* "Allocate" the extent from the range we passed in. */
3881 trace_xfs_bmap_remap_alloc(ap->ip, *ap->firstblock, ap->length);
3882 ap->blkno = bno;
3883 ap->ip->i_d.di_nblocks += ap->length;
3884 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3885
3886 /* Fix the freelist, like a real allocator does. */
3887 args.datatype = ap->datatype;
3888 args.pag = xfs_perag_get(args.mp, args.agno);
3889 ASSERT(args.pag);
3890
3891 /*
3892 * The freelist fixing code will decline the allocation if
3893 * the size and shape of the free space doesn't allow for
3894 * allocating the extent and updating all the metadata that
3895 * happens during an allocation. We're remapping, not
3896 * allocating, so skip that check by pretending to be freeing.
3897 */
3898 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003899 xfs_perag_put(args.pag);
3900 if (error)
3901 trace_xfs_bmap_remap_alloc_error(ap->ip, error, _RET_IP_);
3902 return error;
3903}
3904
3905/*
Nathan Scotta365bdd2006-03-14 13:34:16 +11003906 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3907 * It figures out where to ask the underlying allocator to put the new extent.
3908 */
3909STATIC int
3910xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003911 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003912{
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003913 if (ap->flags & XFS_BMAPI_REMAP)
3914 return xfs_bmap_remap_alloc(ap);
Dave Chinner292378e2016-09-26 08:21:28 +10003915 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3916 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003917 return xfs_bmap_rtalloc(ap);
3918 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919}
3920
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003921/* Trim extent to fit a logical block range. */
3922void
3923xfs_trim_extent(
3924 struct xfs_bmbt_irec *irec,
3925 xfs_fileoff_t bno,
3926 xfs_filblks_t len)
3927{
3928 xfs_fileoff_t distance;
3929 xfs_fileoff_t end = bno + len;
3930
3931 if (irec->br_startoff + irec->br_blockcount <= bno ||
3932 irec->br_startoff >= end) {
3933 irec->br_blockcount = 0;
3934 return;
3935 }
3936
3937 if (irec->br_startoff < bno) {
3938 distance = bno - irec->br_startoff;
3939 if (isnullstartblock(irec->br_startblock))
3940 irec->br_startblock = DELAYSTARTBLOCK;
3941 if (irec->br_startblock != DELAYSTARTBLOCK &&
3942 irec->br_startblock != HOLESTARTBLOCK)
3943 irec->br_startblock += distance;
3944 irec->br_startoff += distance;
3945 irec->br_blockcount -= distance;
3946 }
3947
3948 if (end < irec->br_startoff + irec->br_blockcount) {
3949 distance = irec->br_startoff + irec->br_blockcount - end;
3950 irec->br_blockcount -= distance;
3951 }
3952}
3953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003955 * Trim the returned map to the required bounds
3956 */
3957STATIC void
3958xfs_bmapi_trim_map(
3959 struct xfs_bmbt_irec *mval,
3960 struct xfs_bmbt_irec *got,
3961 xfs_fileoff_t *bno,
3962 xfs_filblks_t len,
3963 xfs_fileoff_t obno,
3964 xfs_fileoff_t end,
3965 int n,
3966 int flags)
3967{
3968 if ((flags & XFS_BMAPI_ENTIRE) ||
3969 got->br_startoff + got->br_blockcount <= obno) {
3970 *mval = *got;
3971 if (isnullstartblock(got->br_startblock))
3972 mval->br_startblock = DELAYSTARTBLOCK;
3973 return;
3974 }
3975
3976 if (obno > *bno)
3977 *bno = obno;
3978 ASSERT((*bno >= obno) || (n == 0));
3979 ASSERT(*bno < end);
3980 mval->br_startoff = *bno;
3981 if (isnullstartblock(got->br_startblock))
3982 mval->br_startblock = DELAYSTARTBLOCK;
3983 else
3984 mval->br_startblock = got->br_startblock +
3985 (*bno - got->br_startoff);
3986 /*
3987 * Return the minimum of what we got and what we asked for for
3988 * the length. We can use the len variable here because it is
3989 * modified below and we could have been there before coming
3990 * here if the first part of the allocation didn't overlap what
3991 * was asked for.
3992 */
3993 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3994 got->br_blockcount - (*bno - got->br_startoff));
3995 mval->br_state = got->br_state;
3996 ASSERT(mval->br_blockcount <= len);
3997 return;
3998}
3999
4000/*
4001 * Update and validate the extent map to return
4002 */
4003STATIC void
4004xfs_bmapi_update_map(
4005 struct xfs_bmbt_irec **map,
4006 xfs_fileoff_t *bno,
4007 xfs_filblks_t *len,
4008 xfs_fileoff_t obno,
4009 xfs_fileoff_t end,
4010 int *n,
4011 int flags)
4012{
4013 xfs_bmbt_irec_t *mval = *map;
4014
4015 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4016 ((mval->br_startoff + mval->br_blockcount) <= end));
4017 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4018 (mval->br_startoff < obno));
4019
4020 *bno = mval->br_startoff + mval->br_blockcount;
4021 *len = end - *bno;
4022 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4023 /* update previous map with new information */
4024 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4025 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4026 ASSERT(mval->br_state == mval[-1].br_state);
4027 mval[-1].br_blockcount = mval->br_blockcount;
4028 mval[-1].br_state = mval->br_state;
4029 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4030 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4031 mval[-1].br_startblock != HOLESTARTBLOCK &&
4032 mval->br_startblock == mval[-1].br_startblock +
4033 mval[-1].br_blockcount &&
4034 ((flags & XFS_BMAPI_IGSTATE) ||
4035 mval[-1].br_state == mval->br_state)) {
4036 ASSERT(mval->br_startoff ==
4037 mval[-1].br_startoff + mval[-1].br_blockcount);
4038 mval[-1].br_blockcount += mval->br_blockcount;
4039 } else if (*n > 0 &&
4040 mval->br_startblock == DELAYSTARTBLOCK &&
4041 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4042 mval->br_startoff ==
4043 mval[-1].br_startoff + mval[-1].br_blockcount) {
4044 mval[-1].br_blockcount += mval->br_blockcount;
4045 mval[-1].br_state = mval->br_state;
4046 } else if (!((*n == 0) &&
4047 ((mval->br_startoff + mval->br_blockcount) <=
4048 obno))) {
4049 mval++;
4050 (*n)++;
4051 }
4052 *map = mval;
4053}
4054
4055/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004056 * Map file blocks to filesystem blocks without allocation.
4057 */
4058int
4059xfs_bmapi_read(
4060 struct xfs_inode *ip,
4061 xfs_fileoff_t bno,
4062 xfs_filblks_t len,
4063 struct xfs_bmbt_irec *mval,
4064 int *nmap,
4065 int flags)
4066{
4067 struct xfs_mount *mp = ip->i_mount;
4068 struct xfs_ifork *ifp;
4069 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004070 xfs_fileoff_t obno;
4071 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11004072 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004073 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11004074 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004075 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004076 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004077
4078 ASSERT(*nmap >= 1);
4079 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004080 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004081 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004082
4083 if (unlikely(XFS_TEST_ERROR(
4084 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4085 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4086 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4087 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004088 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004089 }
4090
4091 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004092 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004093
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004094 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004095
4096 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004097
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004098 /* No CoW fork? Return a hole. */
4099 if (whichfork == XFS_COW_FORK && !ifp) {
4100 mval->br_startoff = bno;
4101 mval->br_startblock = HOLESTARTBLOCK;
4102 mval->br_blockcount = len;
4103 mval->br_state = XFS_EXT_NORM;
4104 *nmap = 1;
4105 return 0;
4106 }
4107
Dave Chinner5c8ed202011-09-18 20:40:45 +00004108 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4109 error = xfs_iread_extents(NULL, ip, whichfork);
4110 if (error)
4111 return error;
4112 }
4113
Christoph Hellwig334f3422016-11-24 11:39:43 +11004114 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4115 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004116 end = bno + len;
4117 obno = bno;
4118
4119 while (bno < end && n < *nmap) {
4120 /* Reading past eof, act as though there's a hole up to end. */
4121 if (eof)
4122 got.br_startoff = end;
4123 if (got.br_startoff > bno) {
4124 /* Reading in a hole. */
4125 mval->br_startoff = bno;
4126 mval->br_startblock = HOLESTARTBLOCK;
4127 mval->br_blockcount =
4128 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4129 mval->br_state = XFS_EXT_NORM;
4130 bno += mval->br_blockcount;
4131 len -= mval->br_blockcount;
4132 mval++;
4133 n++;
4134 continue;
4135 }
4136
4137 /* set up the extent map to return. */
4138 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4139 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4140
4141 /* If we're done, stop now. */
4142 if (bno >= end || n >= *nmap)
4143 break;
4144
4145 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004146 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4147 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004148 }
4149 *nmap = n;
4150 return 0;
4151}
4152
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004153/*
4154 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4155 * global pool and the extent inserted into the inode in-core extent tree.
4156 *
4157 * On entry, got refers to the first extent beyond the offset of the extent to
4158 * allocate or eof is specified if no such extent exists. On return, got refers
4159 * to the extent record that was inserted to the inode fork.
4160 *
4161 * Note that the allocated extent may have been merged with contiguous extents
4162 * during insertion into the inode fork. Thus, got does not reflect the current
4163 * state of the inode fork on return. If necessary, the caller can use lastx to
4164 * look up the updated record in the inode fork.
4165 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10004166int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004167xfs_bmapi_reserve_delalloc(
4168 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004169 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004170 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004171 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004172 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004173 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004174 xfs_extnum_t *lastx,
4175 int eof)
4176{
4177 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004178 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004179 xfs_extlen_t alen;
4180 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004181 char rt = XFS_IS_REALTIME_INODE(ip);
4182 xfs_extlen_t extsz;
4183 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004184 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004185
Brian Foster974ae922016-11-28 14:57:42 +11004186 /*
4187 * Cap the alloc length. Keep track of prealloc so we know whether to
4188 * tag the inode before we return.
4189 */
4190 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004191 if (!eof)
4192 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004193 if (prealloc && alen >= len)
4194 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004195
4196 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004197 if (whichfork == XFS_COW_FORK)
4198 extsz = xfs_get_cowextsz_hint(ip);
4199 else
4200 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004201 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004202 struct xfs_bmbt_irec prev;
4203
4204 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4205 prev.br_startoff = NULLFILEOFF;
4206
4207 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004208 1, 0, &aoff, &alen);
4209 ASSERT(!error);
4210 }
4211
4212 if (rt)
4213 extsz = alen / mp->m_sb.sb_rextsize;
4214
4215 /*
4216 * Make a transaction-less quota reservation for delayed allocation
4217 * blocks. This number gets adjusted later. We return if we haven't
4218 * allocated blocks already inside this loop.
4219 */
4220 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4221 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4222 if (error)
4223 return error;
4224
4225 /*
4226 * Split changing sb for alen and indlen since they could be coming
4227 * from different places.
4228 */
4229 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4230 ASSERT(indlen > 0);
4231
4232 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004233 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004234 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004235 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004236 }
4237
4238 if (error)
4239 goto out_unreserve_quota;
4240
Dave Chinner0d485ad2015-02-23 21:22:03 +11004241 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004242 if (error)
4243 goto out_unreserve_blocks;
4244
4245
4246 ip->i_delayed_blks += alen;
4247
4248 got->br_startoff = aoff;
4249 got->br_startblock = nullstartblock(indlen);
4250 got->br_blockcount = alen;
4251 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004252
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004253 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004254
Brian Foster974ae922016-11-28 14:57:42 +11004255 /*
4256 * Tag the inode if blocks were preallocated. Note that COW fork
4257 * preallocation can occur at the start or end of the extent, even when
4258 * prealloc == 0, so we must also check the aligned offset and length.
4259 */
4260 if (whichfork == XFS_DATA_FORK && prealloc)
4261 xfs_inode_set_eofblocks_tag(ip);
4262 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4263 xfs_inode_set_cowblocks_tag(ip);
4264
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004265 return 0;
4266
4267out_unreserve_blocks:
4268 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004269 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004270 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004271 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004272out_unreserve_quota:
4273 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004274 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004275 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4276 return error;
4277}
4278
Dave Chinnercf11da92014-07-15 07:08:24 +10004279static int
4280xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004281 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004282{
4283 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004284 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004285 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004286 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004287 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004288
Dave Chinnera99ebf42011-12-01 11:24:20 +00004289 ASSERT(bma->length > 0);
4290
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004291 /*
4292 * For the wasdelay case, we could also just allocate the stuff asked
4293 * for in this bmap call but that wouldn't be as good.
4294 */
4295 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004296 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4297 bma->offset = bma->got.br_startoff;
Christoph Hellwig0e8d6302016-11-24 11:40:32 +11004298 if (bma->idx) {
Dave Chinnere0c3da52011-09-18 20:41:01 +00004299 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004300 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004301 }
4302 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004303 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004304 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004305 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004306 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004307 }
4308
4309 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004310 * Set the data type being allocated. For the data fork, the first data
4311 * in the file is treated differently to all other allocations. For the
4312 * attribute fork, we only need to ensure the allocated range is not on
4313 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004314 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004315 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004316 bma->datatype = XFS_ALLOC_NOBUSY;
4317 if (whichfork == XFS_DATA_FORK) {
4318 if (bma->offset == 0)
4319 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4320 else
4321 bma->datatype |= XFS_ALLOC_USERDATA;
4322 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004323 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004324 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004325 }
4326
Dave Chinnere04426b2012-10-05 11:06:59 +10004327 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004328
4329 /*
4330 * Only want to do the alignment at the eof if it is userdata and
4331 * allocation length is larger than a stripe unit.
4332 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004333 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004334 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004335 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004336 if (error)
4337 return error;
4338 }
4339
4340 error = xfs_bmap_alloc(bma);
4341 if (error)
4342 return error;
4343
Dave Chinner29c8d172011-09-18 20:41:00 +00004344 if (bma->cur)
4345 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004346 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004347 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004348 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4349 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4350 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004351 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004352 }
4353 /*
4354 * Bump the number of extents we've allocated
4355 * in this call.
4356 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004357 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004358
Dave Chinner29c8d172011-09-18 20:41:00 +00004359 if (bma->cur)
4360 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004361 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4362
Dave Chinner963c30c2011-09-18 20:40:59 +00004363 bma->got.br_startoff = bma->offset;
4364 bma->got.br_startblock = bma->blkno;
4365 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004366 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004367
4368 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004369 * In the data fork, a wasdelay extent has been initialized, so
4370 * shouldn't be flagged as unwritten.
4371 *
4372 * For the cow fork, however, we convert delalloc reservations
4373 * (extents allocated for speculative preallocation) to
4374 * allocated unwritten extents, and only convert the unwritten
4375 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004376 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004377 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4378 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004379 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004380 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004381
Christoph Hellwigc6534242011-09-18 20:41:05 +00004382 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004383 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004384 else
4385 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004386
Christoph Hellwigc315c902011-09-18 20:41:02 +00004387 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004388 if (error)
4389 return error;
4390
4391 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004392 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4393 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4394 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004395 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004396 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004397
Dave Chinner963c30c2011-09-18 20:40:59 +00004398 ASSERT(bma->got.br_startoff <= bma->offset);
4399 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4400 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004401 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4402 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004403 return 0;
4404}
4405
Dave Chinnerb447fe52011-09-18 20:40:51 +00004406STATIC int
4407xfs_bmapi_convert_unwritten(
4408 struct xfs_bmalloca *bma,
4409 struct xfs_bmbt_irec *mval,
4410 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004411 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004412{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004413 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004414 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004415 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004416 int error;
4417
Dave Chinnerb447fe52011-09-18 20:40:51 +00004418 /* check if we need to do unwritten->real conversion */
4419 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4420 (flags & XFS_BMAPI_PREALLOC))
4421 return 0;
4422
4423 /* check if we need to do real->unwritten conversion */
4424 if (mval->br_state == XFS_EXT_NORM &&
4425 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4426 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4427 return 0;
4428
4429 /*
4430 * Modify (by adding) the state flag, if writing.
4431 */
4432 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004433 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4434 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004435 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004436 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004437 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004438 }
4439 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4440 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4441
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004442 /*
4443 * Before insertion into the bmbt, zero the range being converted
4444 * if required.
4445 */
4446 if (flags & XFS_BMAPI_ZERO) {
4447 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4448 mval->br_blockcount);
4449 if (error)
4450 return error;
4451 }
4452
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004453 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4454 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004455 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004456 /*
4457 * Log the inode core unconditionally in the unwritten extent conversion
4458 * path because the conversion might not have done so (e.g., if the
4459 * extent count hasn't changed). We need to make sure the inode is dirty
4460 * in the transaction for the sake of fsync(), even if nothing has
4461 * changed, because fsync() will not force the log for this transaction
4462 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004463 *
4464 * Note: If we're only converting cow fork extents, there aren't
4465 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004466 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004467 if (whichfork != XFS_COW_FORK)
4468 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004469 if (error)
4470 return error;
4471
4472 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004473 * Update our extent pointer, given that
4474 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4475 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004476 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004477 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004478
4479 /*
4480 * We may have combined previously unwritten space with written space,
4481 * so generate another request.
4482 */
4483 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004484 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004485 return 0;
4486}
4487
Christoph Hellwig44032802011-09-18 20:40:48 +00004488/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004489 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4490 * extent state if necessary. Details behaviour is controlled by the flags
4491 * parameter. Only allocates blocks from a single allocation group, to avoid
4492 * locking problems.
4493 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 * The returned value in "firstblock" from the first call in a transaction
4495 * must be remembered and presented to subsequent calls in "firstblock".
4496 * An upper bound for the number of blocks to be allocated is supplied to
4497 * the first call in "total"; if no allocation group has that many free
4498 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4499 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004500int
4501xfs_bmapi_write(
4502 struct xfs_trans *tp, /* transaction pointer */
4503 struct xfs_inode *ip, /* incore inode */
4504 xfs_fileoff_t bno, /* starting file offs. mapped */
4505 xfs_filblks_t len, /* length to map in file */
4506 int flags, /* XFS_BMAPI_... */
4507 xfs_fsblock_t *firstblock, /* first allocated block
4508 controls a.g. for allocs */
4509 xfs_extlen_t total, /* total blocks needed */
4510 struct xfs_bmbt_irec *mval, /* output: map values */
4511 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004512 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004514 struct xfs_mount *mp = ip->i_mount;
4515 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004516 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004517 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004518 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004519 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004520 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004521 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004522 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004523
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004525 xfs_fileoff_t orig_bno; /* original block number value */
4526 int orig_flags; /* original flags arg value */
4527 xfs_filblks_t orig_len; /* original value of len arg */
4528 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4529 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530
4531 orig_bno = bno;
4532 orig_len = len;
4533 orig_flags = flags;
4534 orig_mval = mval;
4535 orig_nmap = *nmap;
4536#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004537 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004540 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4541 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004542 ASSERT(tp != NULL ||
4543 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4544 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004545 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004546 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004547 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004548 ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK);
4549 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP));
4550 ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004551
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004552 /* zeroing is for currently only for data extents, not metadata */
4553 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4554 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4555 /*
4556 * we can allocate unwritten extents or pre-zero allocated blocks,
4557 * but it makes no sense to do both at once. This would result in
4558 * zeroing the unwritten extent twice, but it still being an
4559 * unwritten extent....
4560 */
4561 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4562 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 if (unlikely(XFS_TEST_ERROR(
4565 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004566 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004568 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004569 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004571
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004573 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004574
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004576
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004577 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004578
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004579 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004581 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004583 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004584 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004585 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004586 }
4587
4588 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4589 error = xfs_iread_extents(tp, ip, whichfork);
4590 if (error)
4591 goto error0;
4592 }
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 n = 0;
4595 end = bno + len;
4596 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004597
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004598 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4599 eof = true;
4600 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4601 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004602 bma.tp = tp;
4603 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004604 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004605 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004606 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004607 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004610 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004611
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004612 /* in hole or beyoned EOF? */
4613 if (eof || bma.got.br_startoff > bno) {
4614 if (flags & XFS_BMAPI_DELALLOC) {
4615 /*
4616 * For the COW fork we can reasonably get a
4617 * request for converting an extent that races
4618 * with other threads already having converted
4619 * part of it, as there converting COW to
4620 * regular blocks is not protected using the
4621 * IOLOCK.
4622 */
4623 ASSERT(flags & XFS_BMAPI_COWFORK);
4624 if (!(flags & XFS_BMAPI_COWFORK)) {
4625 error = -EIO;
4626 goto error0;
4627 }
4628
4629 if (eof || bno >= end)
4630 break;
4631 } else {
4632 need_alloc = true;
4633 }
4634 } else {
4635 /*
4636 * Make sure we only reflink into a hole.
4637 */
4638 ASSERT(!(flags & XFS_BMAPI_REMAP));
4639 if (isnullstartblock(bma.got.br_startblock))
4640 wasdelay = true;
4641 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004642
4643 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 * First, deal with the hole before the allocated space
4645 * that we found, if any.
4646 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004647 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004648 bma.eof = eof;
4649 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4650 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004651 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004652 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004653
Dave Chinnera99ebf42011-12-01 11:24:20 +00004654 /*
4655 * There's a 32/64 bit type mismatch between the
4656 * allocation length request (which can be 64 bits in
4657 * length) and the bma length request, which is
4658 * xfs_extlen_t and therefore 32 bits. Hence we have to
4659 * check for 32-bit overflows and handle them here.
4660 */
4661 if (len > (xfs_filblks_t)MAXEXTLEN)
4662 bma.length = MAXEXTLEN;
4663 else
4664 bma.length = len;
4665
4666 ASSERT(len > 0);
4667 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004668 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 if (error)
4670 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004671 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004672 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004673
4674 /*
4675 * If this is a CoW allocation, record the data in
4676 * the refcount btree for orphan recovery.
4677 */
4678 if (whichfork == XFS_COW_FORK) {
4679 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4680 bma.blkno, bma.length);
4681 if (error)
4682 goto error0;
4683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004685
Dave Chinneraef9a892011-09-18 20:40:44 +00004686 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004687 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4688 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689
Dave Chinnerb447fe52011-09-18 20:40:51 +00004690 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004691 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004692 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004693 continue;
4694 if (error)
4695 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696
Dave Chinneraef9a892011-09-18 20:40:44 +00004697 /* update the extent map to return */
4698 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4699
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 /*
4701 * If we're done, stop now. Stop when we've allocated
4702 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4703 * the transaction may get too big.
4704 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004705 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004707
4708 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004709 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004710 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4711 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004714
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 /*
4716 * Transform from btree to extents, give it cur.
4717 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004718 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004719 int tmp_logflags = 0;
4720
Dave Chinner29c8d172011-09-18 20:41:00 +00004721 ASSERT(bma.cur);
4722 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004724 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 if (error)
4726 goto error0;
4727 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004728
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004730 XFS_IFORK_NEXTENTS(ip, whichfork) >
4731 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733error0:
4734 /*
4735 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004736 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004738 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004740 bma.logflags &= ~xfs_ilog_fext(whichfork);
4741 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004743 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 /*
4745 * Log whatever the flags say, even if error. Otherwise we might miss
4746 * detecting a case where the data is changed, there's an error,
4747 * and it's not logged so we don't shutdown when we should.
4748 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004749 if (bma.logflags)
4750 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004751
Dave Chinner29c8d172011-09-18 20:41:00 +00004752 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 if (!error) {
4754 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004755 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004757 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004758 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004760 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4762 }
4763 if (!error)
4764 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4765 orig_nmap, *nmap);
4766 return error;
4767}
4768
4769/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004770 * When a delalloc extent is split (e.g., due to a hole punch), the original
4771 * indlen reservation must be shared across the two new extents that are left
4772 * behind.
4773 *
4774 * Given the original reservation and the worst case indlen for the two new
4775 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004776 * reservation fairly across the two new extents. If necessary, steal available
4777 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4778 * ores == 1). The number of stolen blocks is returned. The availability and
4779 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004780 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004781static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004782xfs_bmap_split_indlen(
4783 xfs_filblks_t ores, /* original res. */
4784 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004785 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4786 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004787{
4788 xfs_filblks_t len1 = *indlen1;
4789 xfs_filblks_t len2 = *indlen2;
4790 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004791 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004792 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004793
4794 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004795 * Steal as many blocks as we can to try and satisfy the worst case
4796 * indlen for both new extents.
4797 */
Brian Foster75d65362017-02-13 22:48:30 -08004798 if (ores < nres && avail)
4799 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4800 ores += stolen;
4801
4802 /* nothing else to do if we've satisfied the new reservation */
4803 if (ores >= nres)
4804 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004805
4806 /*
Brian Foster75d65362017-02-13 22:48:30 -08004807 * We can't meet the total required reservation for the two extents.
4808 * Calculate the percent of the overall shortage between both extents
4809 * and apply this percentage to each of the requested indlen values.
4810 * This distributes the shortage fairly and reduces the chances that one
4811 * of the two extents is left with nothing when extents are repeatedly
4812 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004813 */
Brian Foster75d65362017-02-13 22:48:30 -08004814 resfactor = (ores * 100);
4815 do_div(resfactor, nres);
4816 len1 *= resfactor;
4817 do_div(len1, 100);
4818 len2 *= resfactor;
4819 do_div(len2, 100);
4820 ASSERT(len1 + len2 <= ores);
4821 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4822
4823 /*
4824 * Hand out the remainder to each extent. If one of the two reservations
4825 * is zero, we want to make sure that one gets a block first. The loop
4826 * below starts with len1, so hand len2 a block right off the bat if it
4827 * is zero.
4828 */
4829 ores -= (len1 + len2);
4830 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4831 if (ores && !len2 && *indlen2) {
4832 len2++;
4833 ores--;
4834 }
4835 while (ores) {
4836 if (len1 < *indlen1) {
4837 len1++;
4838 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004839 }
Brian Foster75d65362017-02-13 22:48:30 -08004840 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004841 break;
Brian Foster75d65362017-02-13 22:48:30 -08004842 if (len2 < *indlen2) {
4843 len2++;
4844 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004845 }
4846 }
4847
4848 *indlen1 = len1;
4849 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004850
4851 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004852}
4853
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004854int
4855xfs_bmap_del_extent_delay(
4856 struct xfs_inode *ip,
4857 int whichfork,
4858 xfs_extnum_t *idx,
4859 struct xfs_bmbt_irec *got,
4860 struct xfs_bmbt_irec *del)
4861{
4862 struct xfs_mount *mp = ip->i_mount;
4863 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4864 struct xfs_bmbt_irec new;
4865 int64_t da_old, da_new, da_diff = 0;
4866 xfs_fileoff_t del_endoff, got_endoff;
4867 xfs_filblks_t got_indlen, new_indlen, stolen;
4868 int error = 0, state = 0;
4869 bool isrt;
4870
4871 XFS_STATS_INC(mp, xs_del_exlist);
4872
4873 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4874 del_endoff = del->br_startoff + del->br_blockcount;
4875 got_endoff = got->br_startoff + got->br_blockcount;
4876 da_old = startblockval(got->br_startblock);
4877 da_new = 0;
4878
4879 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004880 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004881 ASSERT(del->br_blockcount > 0);
4882 ASSERT(got->br_startoff <= del->br_startoff);
4883 ASSERT(got_endoff >= del_endoff);
4884
4885 if (isrt) {
4886 int64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4887
4888 do_div(rtexts, mp->m_sb.sb_rextsize);
4889 xfs_mod_frextents(mp, rtexts);
4890 }
4891
4892 /*
4893 * Update the inode delalloc counter now and wait to update the
4894 * sb counters as we might have to borrow some blocks for the
4895 * indirect block accounting.
4896 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004897 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4898 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004899 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004900 if (error)
4901 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004902 ip->i_delayed_blks -= del->br_blockcount;
4903
4904 if (whichfork == XFS_COW_FORK)
4905 state |= BMAP_COWFORK;
4906
4907 if (got->br_startoff == del->br_startoff)
4908 state |= BMAP_LEFT_CONTIG;
4909 if (got_endoff == del_endoff)
4910 state |= BMAP_RIGHT_CONTIG;
4911
4912 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4913 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4914 /*
4915 * Matches the whole extent. Delete the entry.
4916 */
4917 xfs_iext_remove(ip, *idx, 1, state);
4918 --*idx;
4919 break;
4920 case BMAP_LEFT_CONTIG:
4921 /*
4922 * Deleting the first part of the extent.
4923 */
4924 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4925 got->br_startoff = del_endoff;
4926 got->br_blockcount -= del->br_blockcount;
4927 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4928 got->br_blockcount), da_old);
4929 got->br_startblock = nullstartblock((int)da_new);
4930 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4931 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4932 break;
4933 case BMAP_RIGHT_CONTIG:
4934 /*
4935 * Deleting the last part of the extent.
4936 */
4937 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4938 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4939 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4940 got->br_blockcount), da_old);
4941 got->br_startblock = nullstartblock((int)da_new);
4942 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4943 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4944 break;
4945 case 0:
4946 /*
4947 * Deleting the middle of the extent.
4948 *
4949 * Distribute the original indlen reservation across the two new
4950 * extents. Steal blocks from the deleted extent if necessary.
4951 * Stealing blocks simply fudges the fdblocks accounting below.
4952 * Warn if either of the new indlen reservations is zero as this
4953 * can lead to delalloc problems.
4954 */
4955 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4956
4957 got->br_blockcount = del->br_startoff - got->br_startoff;
4958 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4959
4960 new.br_blockcount = got_endoff - del_endoff;
4961 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4962
4963 WARN_ON_ONCE(!got_indlen || !new_indlen);
4964 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4965 del->br_blockcount);
4966
4967 got->br_startblock = nullstartblock((int)got_indlen);
4968 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4969 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4970
4971 new.br_startoff = del_endoff;
4972 new.br_state = got->br_state;
4973 new.br_startblock = nullstartblock((int)new_indlen);
4974
4975 ++*idx;
4976 xfs_iext_insert(ip, *idx, 1, &new, state);
4977
4978 da_new = got_indlen + new_indlen - stolen;
4979 del->br_blockcount -= stolen;
4980 break;
4981 }
4982
4983 ASSERT(da_old >= da_new);
4984 da_diff = da_old - da_new;
4985 if (!isrt)
4986 da_diff += del->br_blockcount;
4987 if (da_diff)
4988 xfs_mod_fdblocks(mp, da_diff, false);
4989 return error;
4990}
4991
4992void
4993xfs_bmap_del_extent_cow(
4994 struct xfs_inode *ip,
4995 xfs_extnum_t *idx,
4996 struct xfs_bmbt_irec *got,
4997 struct xfs_bmbt_irec *del)
4998{
4999 struct xfs_mount *mp = ip->i_mount;
5000 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
5001 struct xfs_bmbt_irec new;
5002 xfs_fileoff_t del_endoff, got_endoff;
5003 int state = BMAP_COWFORK;
5004
5005 XFS_STATS_INC(mp, xs_del_exlist);
5006
5007 del_endoff = del->br_startoff + del->br_blockcount;
5008 got_endoff = got->br_startoff + got->br_blockcount;
5009
5010 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11005011 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005012 ASSERT(del->br_blockcount > 0);
5013 ASSERT(got->br_startoff <= del->br_startoff);
5014 ASSERT(got_endoff >= del_endoff);
5015 ASSERT(!isnullstartblock(got->br_startblock));
5016
5017 if (got->br_startoff == del->br_startoff)
5018 state |= BMAP_LEFT_CONTIG;
5019 if (got_endoff == del_endoff)
5020 state |= BMAP_RIGHT_CONTIG;
5021
5022 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
5023 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
5024 /*
5025 * Matches the whole extent. Delete the entry.
5026 */
5027 xfs_iext_remove(ip, *idx, 1, state);
5028 --*idx;
5029 break;
5030 case BMAP_LEFT_CONTIG:
5031 /*
5032 * Deleting the first part of the extent.
5033 */
5034 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5035 got->br_startoff = del_endoff;
5036 got->br_blockcount -= del->br_blockcount;
5037 got->br_startblock = del->br_startblock + del->br_blockcount;
5038 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5039 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5040 break;
5041 case BMAP_RIGHT_CONTIG:
5042 /*
5043 * Deleting the last part of the extent.
5044 */
5045 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5046 got->br_blockcount -= del->br_blockcount;
5047 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5048 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5049 break;
5050 case 0:
5051 /*
5052 * Deleting the middle of the extent.
5053 */
5054 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5055 got->br_blockcount = del->br_startoff - got->br_startoff;
5056 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5057 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5058
5059 new.br_startoff = del_endoff;
5060 new.br_blockcount = got_endoff - del_endoff;
5061 new.br_state = got->br_state;
5062 new.br_startblock = del->br_startblock + del->br_blockcount;
5063
5064 ++*idx;
5065 xfs_iext_insert(ip, *idx, 1, &new, state);
5066 break;
5067 }
5068}
5069
Brian Fostera9bd24a2016-03-15 11:42:46 +11005070/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005071 * Called by xfs_bmapi to update file extent records and the btree
5072 * after removing space (or undoing a delayed allocation).
5073 */
5074STATIC int /* error */
5075xfs_bmap_del_extent(
5076 xfs_inode_t *ip, /* incore inode pointer */
5077 xfs_trans_t *tp, /* current transaction pointer */
5078 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005079 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005080 xfs_btree_cur_t *cur, /* if null, not a btree */
5081 xfs_bmbt_irec_t *del, /* data to remove from extents */
5082 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005083 int whichfork, /* data or attr fork */
5084 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005085{
5086 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5087 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5088 xfs_fsblock_t del_endblock=0; /* first block past del */
5089 xfs_fileoff_t del_endoff; /* first offset past del */
5090 int delay; /* current block is delayed allocated */
5091 int do_fx; /* free extent at end of routine */
5092 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5093 int error; /* error return value */
5094 int flags; /* inode logging flags */
5095 xfs_bmbt_irec_t got; /* current extent entry */
5096 xfs_fileoff_t got_endoff; /* first offset past got */
5097 int i; /* temp state */
5098 xfs_ifork_t *ifp; /* inode fork pointer */
5099 xfs_mount_t *mp; /* mount structure */
5100 xfs_filblks_t nblks; /* quota/sb block count */
5101 xfs_bmbt_irec_t new; /* new record to be inserted */
5102 /* REFERENCED */
5103 uint qfield; /* quota field to update */
5104 xfs_filblks_t temp; /* for indirect length calculations */
5105 xfs_filblks_t temp2; /* for indirect length calculations */
5106 int state = 0;
5107
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005108 mp = ip->i_mount;
5109 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005110
5111 if (whichfork == XFS_ATTR_FORK)
5112 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005113 else if (whichfork == XFS_COW_FORK)
5114 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005115
Dave Chinner9e5987a2013-02-25 12:31:26 +11005116 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005117 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005118 ASSERT(del->br_blockcount > 0);
5119 ep = xfs_iext_get_ext(ifp, *idx);
5120 xfs_bmbt_get_all(ep, &got);
5121 ASSERT(got.br_startoff <= del->br_startoff);
5122 del_endoff = del->br_startoff + del->br_blockcount;
5123 got_endoff = got.br_startoff + got.br_blockcount;
5124 ASSERT(got_endoff >= del_endoff);
5125 delay = isnullstartblock(got.br_startblock);
5126 ASSERT(isnullstartblock(del->br_startblock) == delay);
5127 flags = 0;
5128 qfield = 0;
5129 error = 0;
5130 /*
5131 * If deleting a real allocation, must free up the disk space.
5132 */
5133 if (!delay) {
5134 flags = XFS_ILOG_CORE;
5135 /*
5136 * Realtime allocation. Free it and record di_nblocks update.
5137 */
5138 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5139 xfs_fsblock_t bno;
5140 xfs_filblks_t len;
5141
5142 ASSERT(do_mod(del->br_blockcount,
5143 mp->m_sb.sb_rextsize) == 0);
5144 ASSERT(do_mod(del->br_startblock,
5145 mp->m_sb.sb_rextsize) == 0);
5146 bno = del->br_startblock;
5147 len = del->br_blockcount;
5148 do_div(bno, mp->m_sb.sb_rextsize);
5149 do_div(len, mp->m_sb.sb_rextsize);
5150 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5151 if (error)
5152 goto done;
5153 do_fx = 0;
5154 nblks = len * mp->m_sb.sb_rextsize;
5155 qfield = XFS_TRANS_DQ_RTBCOUNT;
5156 }
5157 /*
5158 * Ordinary allocation.
5159 */
5160 else {
5161 do_fx = 1;
5162 nblks = del->br_blockcount;
5163 qfield = XFS_TRANS_DQ_BCOUNT;
5164 }
5165 /*
5166 * Set up del_endblock and cur for later.
5167 */
5168 del_endblock = del->br_startblock + del->br_blockcount;
5169 if (cur) {
5170 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5171 got.br_startblock, got.br_blockcount,
5172 &i)))
5173 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005174 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005175 }
5176 da_old = da_new = 0;
5177 } else {
5178 da_old = startblockval(got.br_startblock);
5179 da_new = 0;
5180 nblks = 0;
5181 do_fx = 0;
5182 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005183
Dave Chinner9e5987a2013-02-25 12:31:26 +11005184 /*
5185 * Set flag value to use in switch statement.
5186 * Left-contig is 2, right-contig is 1.
5187 */
5188 switch (((got.br_startoff == del->br_startoff) << 1) |
5189 (got_endoff == del_endoff)) {
5190 case 3:
5191 /*
5192 * Matches the whole extent. Delete the entry.
5193 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005194 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005195 xfs_iext_remove(ip, *idx, 1,
5196 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5197 --*idx;
5198 if (delay)
5199 break;
5200
5201 XFS_IFORK_NEXT_SET(ip, whichfork,
5202 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5203 flags |= XFS_ILOG_CORE;
5204 if (!cur) {
5205 flags |= xfs_ilog_fext(whichfork);
5206 break;
5207 }
5208 if ((error = xfs_btree_delete(cur, &i)))
5209 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005210 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005211 break;
5212
5213 case 2:
5214 /*
5215 * Deleting the first part of the extent.
5216 */
5217 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5218 xfs_bmbt_set_startoff(ep, del_endoff);
5219 temp = got.br_blockcount - del->br_blockcount;
5220 xfs_bmbt_set_blockcount(ep, temp);
5221 if (delay) {
5222 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5223 da_old);
5224 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5225 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5226 da_new = temp;
5227 break;
5228 }
5229 xfs_bmbt_set_startblock(ep, del_endblock);
5230 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5231 if (!cur) {
5232 flags |= xfs_ilog_fext(whichfork);
5233 break;
5234 }
5235 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5236 got.br_blockcount - del->br_blockcount,
5237 got.br_state)))
5238 goto done;
5239 break;
5240
5241 case 1:
5242 /*
5243 * Deleting the last part of the extent.
5244 */
5245 temp = got.br_blockcount - del->br_blockcount;
5246 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5247 xfs_bmbt_set_blockcount(ep, temp);
5248 if (delay) {
5249 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5250 da_old);
5251 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5252 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5253 da_new = temp;
5254 break;
5255 }
5256 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5257 if (!cur) {
5258 flags |= xfs_ilog_fext(whichfork);
5259 break;
5260 }
5261 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5262 got.br_startblock,
5263 got.br_blockcount - del->br_blockcount,
5264 got.br_state)))
5265 goto done;
5266 break;
5267
5268 case 0:
5269 /*
5270 * Deleting the middle of the extent.
5271 */
5272 temp = del->br_startoff - got.br_startoff;
5273 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5274 xfs_bmbt_set_blockcount(ep, temp);
5275 new.br_startoff = del_endoff;
5276 temp2 = got_endoff - del_endoff;
5277 new.br_blockcount = temp2;
5278 new.br_state = got.br_state;
5279 if (!delay) {
5280 new.br_startblock = del_endblock;
5281 flags |= XFS_ILOG_CORE;
5282 if (cur) {
5283 if ((error = xfs_bmbt_update(cur,
5284 got.br_startoff,
5285 got.br_startblock, temp,
5286 got.br_state)))
5287 goto done;
5288 if ((error = xfs_btree_increment(cur, 0, &i)))
5289 goto done;
5290 cur->bc_rec.b = new;
5291 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10005292 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005293 goto done;
5294 /*
5295 * If get no-space back from btree insert,
5296 * it tried a split, and we have a zero
5297 * block reservation.
5298 * Fix up our state and return the error.
5299 */
Dave Chinner24513372014-06-25 14:58:08 +10005300 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11005301 /*
5302 * Reset the cursor, don't trust
5303 * it after any insert operation.
5304 */
5305 if ((error = xfs_bmbt_lookup_eq(cur,
5306 got.br_startoff,
5307 got.br_startblock,
5308 temp, &i)))
5309 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005310 XFS_WANT_CORRUPTED_GOTO(mp,
5311 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005312 /*
5313 * Update the btree record back
5314 * to the original value.
5315 */
5316 if ((error = xfs_bmbt_update(cur,
5317 got.br_startoff,
5318 got.br_startblock,
5319 got.br_blockcount,
5320 got.br_state)))
5321 goto done;
5322 /*
5323 * Reset the extent record back
5324 * to the original value.
5325 */
5326 xfs_bmbt_set_blockcount(ep,
5327 got.br_blockcount);
5328 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005329 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005330 goto done;
5331 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005332 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005333 } else
5334 flags |= xfs_ilog_fext(whichfork);
5335 XFS_IFORK_NEXT_SET(ip, whichfork,
5336 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5337 } else {
Brian Fosterd34999c92016-03-15 11:42:47 +11005338 xfs_filblks_t stolen;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005339 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005340
5341 /*
5342 * Distribute the original indlen reservation across the
Brian Fosterd34999c92016-03-15 11:42:47 +11005343 * two new extents. Steal blocks from the deleted extent
5344 * if necessary. Stealing blocks simply fudges the
5345 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005346 */
5347 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5348 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c92016-03-15 11:42:47 +11005349 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5350 del->br_blockcount);
5351 da_new = temp + temp2 - stolen;
5352 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005353
5354 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11005355 * Set the reservation for each extent. Warn if either
5356 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005357 */
Brian Fosterd34999c92016-03-15 11:42:47 +11005358 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005359 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005360 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005361 }
5362 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5363 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5364 ++*idx;
5365 break;
5366 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005367
5368 /* remove reverse mapping */
5369 if (!delay) {
5370 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5371 if (error)
5372 goto done;
5373 }
5374
Dave Chinner9e5987a2013-02-25 12:31:26 +11005375 /*
5376 * If we need to, add to list of extents to delete.
5377 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005378 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005379 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5380 error = xfs_refcount_decrease_extent(mp, dfops, del);
5381 if (error)
5382 goto done;
5383 } else
5384 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5385 del->br_blockcount, NULL);
5386 }
5387
Dave Chinner9e5987a2013-02-25 12:31:26 +11005388 /*
5389 * Adjust inode # blocks in the file.
5390 */
5391 if (nblks)
5392 ip->i_d.di_nblocks -= nblks;
5393 /*
5394 * Adjust quota data.
5395 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005396 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005397 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5398
5399 /*
5400 * Account for change in delayed indirect blocks.
5401 * Nothing to do for disk quota accounting here.
5402 */
5403 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005404 if (da_old > da_new)
5405 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005406done:
5407 *logflagsp = flags;
5408 return error;
5409}
5410
5411/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005412 * Unmap (remove) blocks from a file.
5413 * If nexts is nonzero then the number of extents to remove is limited to
5414 * that value. If not all extents in the block range can be removed then
5415 * *done is set.
5416 */
5417int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005418__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 xfs_trans_t *tp, /* transaction pointer */
5420 struct xfs_inode *ip, /* incore inode */
5421 xfs_fileoff_t bno, /* starting offset to unmap */
Darrick J. Wong44535932016-10-03 09:11:29 -07005422 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 int flags, /* misc flags */
5424 xfs_extnum_t nexts, /* number of extents max */
5425 xfs_fsblock_t *firstblock, /* first allocated block
5426 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005427 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428{
5429 xfs_btree_cur_t *cur; /* bmap btree cursor */
5430 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 int error; /* error return value */
5432 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005433 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 xfs_ifork_t *ifp; /* inode fork pointer */
5435 int isrt; /* freeing in rt area */
5436 xfs_extnum_t lastx; /* last extent index used */
5437 int logflags; /* transaction logging flags */
5438 xfs_extlen_t mod; /* rt extent offset */
5439 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 xfs_fileoff_t start; /* first file offset deleted */
5441 int tmp_logflags; /* partial logging flags */
5442 int wasdel; /* was a delayed alloc extent */
5443 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005445 xfs_filblks_t len = *rlen; /* length to unmap in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005447 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5448
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005449 whichfork = xfs_bmapi_whichfork(flags);
5450 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 ifp = XFS_IFORK_PTR(ip, whichfork);
5452 if (unlikely(
5453 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5454 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5455 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5456 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005457 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 }
5459 mp = ip->i_mount;
5460 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005461 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005462
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005463 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 ASSERT(len > 0);
5465 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005466
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5468 (error = xfs_iread_extents(tp, ip, whichfork)))
5469 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005470 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005471 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 return 0;
5473 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005474 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005475 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 start = bno;
5477 bno = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005478
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479 /*
5480 * Check to see if the given block number is past the end of the
5481 * file, back up to the last block if so...
5482 */
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005483 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5484 ASSERT(lastx > 0);
5485 xfs_iext_get_extent(ifp, --lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005486 bno = got.br_startoff + got.br_blockcount - 1;
5487 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005488
Linus Torvalds1da177e2005-04-16 15:20:36 -07005489 logflags = 0;
5490 if (ifp->if_flags & XFS_IFBROOT) {
5491 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005492 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005494 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 cur->bc_private.b.flags = 0;
5496 } else
5497 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005498
5499 if (isrt) {
5500 /*
5501 * Synchronize by locking the bitmap inode.
5502 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005503 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005504 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005505 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5506 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005507 }
5508
Linus Torvalds1da177e2005-04-16 15:20:36 -07005509 extno = 0;
5510 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5511 (nexts == 0 || extno < nexts)) {
5512 /*
5513 * Is the found extent after a hole in which bno lives?
5514 * Just back up to the previous extent, if so.
5515 */
5516 if (got.br_startoff > bno) {
5517 if (--lastx < 0)
5518 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005519 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 }
5521 /*
5522 * Is the last block of this extent before the range
5523 * we're supposed to delete? If so, we're done.
5524 */
5525 bno = XFS_FILEOFF_MIN(bno,
5526 got.br_startoff + got.br_blockcount - 1);
5527 if (bno < start)
5528 break;
5529 /*
5530 * Then deal with the (possibly delayed) allocated space
5531 * we found.
5532 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005534 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 if (got.br_startoff < start) {
5536 del.br_startoff = start;
5537 del.br_blockcount -= start - got.br_startoff;
5538 if (!wasdel)
5539 del.br_startblock += start - got.br_startoff;
5540 }
5541 if (del.br_startoff + del.br_blockcount > bno + 1)
5542 del.br_blockcount = bno + 1 - del.br_startoff;
5543 sum = del.br_startblock + del.br_blockcount;
5544 if (isrt &&
5545 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5546 /*
5547 * Realtime extent not lined up at the end.
5548 * The extent could have been split into written
5549 * and unwritten pieces, or we could just be
5550 * unmapping part of it. But we can't really
5551 * get rid of part of a realtime extent.
5552 */
5553 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005554 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005555 /*
5556 * This piece is unwritten, or we're not
5557 * using unwritten extents. Skip over it.
5558 */
5559 ASSERT(bno >= mod);
5560 bno -= mod > del.br_blockcount ?
5561 del.br_blockcount : mod;
5562 if (bno < got.br_startoff) {
5563 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005564 xfs_bmbt_get_all(xfs_iext_get_ext(
5565 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 }
5567 continue;
5568 }
5569 /*
5570 * It's written, turn it unwritten.
5571 * This is better than zeroing it.
5572 */
5573 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005574 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 /*
5576 * If this spans a realtime extent boundary,
5577 * chop it back to the start of the one we end at.
5578 */
5579 if (del.br_blockcount > mod) {
5580 del.br_startoff += del.br_blockcount - mod;
5581 del.br_startblock += del.br_blockcount - mod;
5582 del.br_blockcount = mod;
5583 }
5584 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005585 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005586 whichfork, &lastx, &cur, &del,
5587 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 if (error)
5589 goto error0;
5590 goto nodelete;
5591 }
5592 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5593 /*
5594 * Realtime extent is lined up at the end but not
5595 * at the front. We'll get rid of full extents if
5596 * we can.
5597 */
5598 mod = mp->m_sb.sb_rextsize - mod;
5599 if (del.br_blockcount > mod) {
5600 del.br_blockcount -= mod;
5601 del.br_startoff += mod;
5602 del.br_startblock += mod;
5603 } else if ((del.br_startoff == start &&
5604 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005605 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005606 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 /*
5608 * Can't make it unwritten. There isn't
5609 * a full extent here so just skip it.
5610 */
5611 ASSERT(bno >= del.br_blockcount);
5612 bno -= del.br_blockcount;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005613 if (got.br_startoff > bno && --lastx >= 0)
5614 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 continue;
5616 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005617 struct xfs_bmbt_irec prev;
5618
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 /*
5620 * This one is already unwritten.
5621 * It must have a written left neighbor.
5622 * Unwrite the killed part of that one and
5623 * try again.
5624 */
5625 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005626 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005628 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 ASSERT(del.br_startblock ==
5630 prev.br_startblock + prev.br_blockcount);
5631 if (prev.br_startoff < start) {
5632 mod = start - prev.br_startoff;
5633 prev.br_blockcount -= mod;
5634 prev.br_startblock += mod;
5635 prev.br_startoff = start;
5636 }
5637 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005638 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005639 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005640 ip, whichfork, &lastx, &cur,
5641 &prev, firstblock, dfops,
5642 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 if (error)
5644 goto error0;
5645 goto nodelete;
5646 } else {
5647 ASSERT(del.br_state == XFS_EXT_NORM);
5648 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005649 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005650 ip, whichfork, &lastx, &cur,
5651 &del, firstblock, dfops,
5652 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 if (error)
5654 goto error0;
5655 goto nodelete;
5656 }
5657 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005658
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 /*
5660 * If it's the case where the directory code is running
5661 * with no block reservation, and the deleted block is in
5662 * the middle of its extent, and the resulting insert
5663 * of an extent would cause transformation to btree format,
5664 * then reject it. The calling code will then swap
5665 * blocks around instead.
5666 * We have to do this now, rather than waiting for the
5667 * conversion to btree format, since the transaction
5668 * will be dirty.
5669 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005670 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005672 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5673 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 del.br_startoff > got.br_startoff &&
5675 del.br_startoff + del.br_blockcount <
5676 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005677 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 goto error0;
5679 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005680
5681 /*
5682 * Unreserve quota and update realtime free space, if
5683 * appropriate. If delayed allocation, update the inode delalloc
5684 * counter now and wait to update the sb counters as
5685 * xfs_bmap_del_extent() might need to borrow some blocks.
5686 */
5687 if (wasdel) {
5688 ASSERT(startblockval(del.br_startblock) > 0);
5689 if (isrt) {
5690 xfs_filblks_t rtexts;
5691
5692 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5693 do_div(rtexts, mp->m_sb.sb_rextsize);
5694 xfs_mod_frextents(mp, (int64_t)rtexts);
5695 (void)xfs_trans_reserve_quota_nblks(NULL,
5696 ip, -((long)del.br_blockcount), 0,
5697 XFS_QMOPT_RES_RTBLKS);
5698 } else {
5699 (void)xfs_trans_reserve_quota_nblks(NULL,
5700 ip, -((long)del.br_blockcount), 0,
5701 XFS_QMOPT_RES_REGBLKS);
5702 }
5703 ip->i_delayed_blks -= del.br_blockcount;
5704 if (cur)
5705 cur->bc_private.b.flags |=
5706 XFS_BTCUR_BPRV_WASDEL;
5707 } else if (cur)
5708 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5709
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005710 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005711 &tmp_logflags, whichfork, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005712 logflags |= tmp_logflags;
5713 if (error)
5714 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005715
5716 if (!isrt && wasdel)
5717 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5718
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 bno = del.br_startoff - 1;
5720nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 /*
5722 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005725 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005726 xfs_iext_get_extent(ifp, lastx, &got);
5727 if (got.br_startoff > bno && --lastx >= 0)
5728 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 extno++;
5731 }
5732 }
Darrick J. Wong44535932016-10-03 09:11:29 -07005733 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5734 *rlen = 0;
5735 else
5736 *rlen = bno - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005737
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 /*
5739 * Convert to a btree if necessary.
5740 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005741 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005743 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 &cur, 0, &tmp_logflags, whichfork);
5745 logflags |= tmp_logflags;
5746 if (error)
5747 goto error0;
5748 }
5749 /*
5750 * transform from btree to extents, give it cur
5751 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005752 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 ASSERT(cur != NULL);
5754 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5755 whichfork);
5756 logflags |= tmp_logflags;
5757 if (error)
5758 goto error0;
5759 }
5760 /*
5761 * transform from extents to local?
5762 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 error = 0;
5764error0:
5765 /*
5766 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005767 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005769 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005771 logflags &= ~xfs_ilog_fext(whichfork);
5772 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005774 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 /*
5776 * Log inode even in the error case, if the transaction
5777 * is dirty we'll need to shut down the filesystem.
5778 */
5779 if (logflags)
5780 xfs_trans_log_inode(tp, ip, logflags);
5781 if (cur) {
5782 if (!error) {
5783 *firstblock = cur->bc_private.b.firstblock;
5784 cur->bc_private.b.allocated = 0;
5785 }
5786 xfs_btree_del_cursor(cur,
5787 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5788 }
5789 return error;
5790}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005791
Darrick J. Wong44535932016-10-03 09:11:29 -07005792/* Unmap a range of a file. */
5793int
5794xfs_bunmapi(
5795 xfs_trans_t *tp,
5796 struct xfs_inode *ip,
5797 xfs_fileoff_t bno,
5798 xfs_filblks_t len,
5799 int flags,
5800 xfs_extnum_t nexts,
5801 xfs_fsblock_t *firstblock,
5802 struct xfs_defer_ops *dfops,
5803 int *done)
5804{
5805 int error;
5806
5807 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5808 dfops);
5809 *done = (len == 0);
5810 return error;
5811}
5812
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005813/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005814 * Determine whether an extent shift can be accomplished by a merge with the
5815 * extent that precedes the target hole of the shift.
5816 */
5817STATIC bool
5818xfs_bmse_can_merge(
5819 struct xfs_bmbt_irec *left, /* preceding extent */
5820 struct xfs_bmbt_irec *got, /* current extent to shift */
5821 xfs_fileoff_t shift) /* shift fsb */
5822{
5823 xfs_fileoff_t startoff;
5824
5825 startoff = got->br_startoff - shift;
5826
5827 /*
5828 * The extent, once shifted, must be adjacent in-file and on-disk with
5829 * the preceding extent.
5830 */
5831 if ((left->br_startoff + left->br_blockcount != startoff) ||
5832 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5833 (left->br_state != got->br_state) ||
5834 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5835 return false;
5836
5837 return true;
5838}
5839
5840/*
5841 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5842 * hole in the file. If an extent shift would result in the extent being fully
5843 * adjacent to the extent that currently precedes the hole, we can merge with
5844 * the preceding extent rather than do the shift.
5845 *
5846 * This function assumes the caller has verified a shift-by-merge is possible
5847 * with the provided extents via xfs_bmse_can_merge().
5848 */
5849STATIC int
5850xfs_bmse_merge(
5851 struct xfs_inode *ip,
5852 int whichfork,
5853 xfs_fileoff_t shift, /* shift fsb */
5854 int current_ext, /* idx of gotp */
5855 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5856 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5857 struct xfs_btree_cur *cur,
5858 int *logflags) /* output */
5859{
Brian Fosterddb19e32014-09-23 15:38:09 +10005860 struct xfs_bmbt_irec got;
5861 struct xfs_bmbt_irec left;
5862 xfs_filblks_t blockcount;
5863 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005864 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005865
Brian Fosterddb19e32014-09-23 15:38:09 +10005866 xfs_bmbt_get_all(gotp, &got);
5867 xfs_bmbt_get_all(leftp, &left);
5868 blockcount = left.br_blockcount + got.br_blockcount;
5869
5870 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5871 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5872 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5873
5874 /*
5875 * Merge the in-core extents. Note that the host record pointers and
5876 * current_ext index are invalid once the extent has been removed via
5877 * xfs_iext_remove().
5878 */
5879 xfs_bmbt_set_blockcount(leftp, blockcount);
5880 xfs_iext_remove(ip, current_ext, 1, 0);
5881
5882 /*
5883 * Update the on-disk extent count, the btree if necessary and log the
5884 * inode.
5885 */
5886 XFS_IFORK_NEXT_SET(ip, whichfork,
5887 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5888 *logflags |= XFS_ILOG_CORE;
5889 if (!cur) {
5890 *logflags |= XFS_ILOG_DEXT;
5891 return 0;
5892 }
5893
5894 /* lookup and remove the extent to merge */
5895 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5896 got.br_blockcount, &i);
5897 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005898 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005899 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005900
5901 error = xfs_btree_delete(cur, &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 /* lookup and update size of the previous extent */
5907 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5908 left.br_blockcount, &i);
5909 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005910 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005911 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005912
5913 left.br_blockcount = blockcount;
5914
Dave Chinner4db431f2014-12-04 09:42:40 +11005915 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5916 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005917}
5918
5919/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005920 * Shift a single extent.
5921 */
5922STATIC int
5923xfs_bmse_shift_one(
5924 struct xfs_inode *ip,
5925 int whichfork,
5926 xfs_fileoff_t offset_shift_fsb,
5927 int *current_ext,
5928 struct xfs_bmbt_rec_host *gotp,
5929 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005930 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005931 enum shift_direction direction,
5932 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005933{
5934 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005935 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005936 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005937 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005938 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005939 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005940 int error;
5941 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005942 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005943
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005944 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005945 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005946 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005947
5948 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005949
Brian Fosterf71721d2014-09-23 15:39:05 +10005950 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005951 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005952
Namjae Jeona904b1c2015-03-25 15:08:56 +11005953 if (direction == SHIFT_LEFT) {
5954 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005955
Namjae Jeona904b1c2015-03-25 15:08:56 +11005956 /*
5957 * Check for merge if we've got an extent to the left,
5958 * otherwise make sure there's enough room at the start
5959 * of the file for the shift.
5960 */
5961 if (!*current_ext) {
5962 if (got.br_startoff < offset_shift_fsb)
5963 return -EINVAL;
5964 goto update_current_ext;
5965 }
5966 /*
5967 * grab the left extent and check for a large
5968 * enough hole.
5969 */
5970 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5971 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5972
5973 if (startoff <
5974 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005975 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005976
Dave Chinnerb11bd672014-12-04 09:42:24 +11005977 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005978 if (xfs_bmse_can_merge(&adj_irec, &got,
5979 offset_shift_fsb)) {
Darrick J. Wong9c194642016-08-03 12:16:05 +10005980 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5981 *current_ext, gotp, adj_irecp,
5982 cur, logflags);
5983 if (error)
5984 return error;
5985 adj_irec = got;
5986 goto update_rmap;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005987 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005988 } else {
5989 startoff = got.br_startoff + offset_shift_fsb;
5990 /* nothing to move if this is the last extent */
5991 if (*current_ext >= (total_extents - 1))
5992 goto update_current_ext;
5993 /*
5994 * If this is not the last extent in the file, make sure there
5995 * is enough room between current extent and next extent for
5996 * accommodating the shift.
5997 */
5998 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5999 xfs_bmbt_get_all(adj_irecp, &adj_irec);
6000 if (startoff + got.br_blockcount > adj_irec.br_startoff)
6001 return -EINVAL;
6002 /*
6003 * Unlike a left shift (which involves a hole punch),
6004 * a right shift does not modify extent neighbors
6005 * in any way. We should never find mergeable extents
6006 * in this scenario. Check anyways and warn if we
6007 * encounter two extents that could be one.
6008 */
6009 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
6010 WARN_ON_ONCE(1);
6011 }
Brian Fostera979bdf2014-09-23 15:39:04 +10006012 /*
6013 * Increment the extent index for the next iteration, update the start
6014 * offset of the in-core extent and update the btree if applicable.
6015 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11006016update_current_ext:
6017 if (direction == SHIFT_LEFT)
6018 (*current_ext)++;
6019 else
6020 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10006021 xfs_bmbt_set_startoff(gotp, startoff);
6022 *logflags |= XFS_ILOG_CORE;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006023 adj_irec = got;
Brian Fostera979bdf2014-09-23 15:39:04 +10006024 if (!cur) {
6025 *logflags |= XFS_ILOG_DEXT;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006026 goto update_rmap;
Brian Fostera979bdf2014-09-23 15:39:04 +10006027 }
6028
6029 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
6030 got.br_blockcount, &i);
6031 if (error)
6032 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11006033 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10006034
6035 got.br_startoff = startoff;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006036 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
6037 got.br_blockcount, got.br_state);
6038 if (error)
6039 return error;
6040
6041update_rmap:
6042 /* update reverse mapping */
6043 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
6044 if (error)
6045 return error;
6046 adj_irec.br_startoff = startoff;
6047 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
Brian Fostera979bdf2014-09-23 15:39:04 +10006048}
6049
6050/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11006051 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006052 *
Brian Foster2c845f52014-09-23 15:37:09 +10006053 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11006054 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10006055 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6056 * is the length by which each extent is shifted. If there is no hole to shift
6057 * the extents into, this will be considered invalid operation and we abort
6058 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006059 */
6060int
6061xfs_bmap_shift_extents(
6062 struct xfs_trans *tp,
6063 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006064 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006065 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10006066 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006067 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006068 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006069 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006070 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006071 int num_exts)
6072{
Brian Fosterca446d82014-09-02 12:12:53 +10006073 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006074 struct xfs_bmbt_rec_host *gotp;
6075 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006076 struct xfs_mount *mp = ip->i_mount;
6077 struct xfs_ifork *ifp;
6078 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10006079 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006080 xfs_extnum_t total_extents;
6081 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006082 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006083 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10006084 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006085
6086 if (unlikely(XFS_TEST_ERROR(
6087 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6088 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6089 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6090 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6091 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10006092 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006093 }
6094
6095 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10006096 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006097
Brian Foster2c845f52014-09-23 15:37:09 +10006098 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6099 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006100 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6101 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006102
6103 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006104 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6105 /* Read in all the extents */
6106 error = xfs_iread_extents(tp, ip, whichfork);
6107 if (error)
6108 return error;
6109 }
6110
Brian Fosterddb19e32014-09-23 15:38:09 +10006111 if (ifp->if_flags & XFS_IFBROOT) {
6112 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6113 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006114 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006115 cur->bc_private.b.flags = 0;
6116 }
6117
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006118 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006119 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006120 * are collapsing out, so we cannot use the count of real extents here.
6121 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006122 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006123 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006124 if (total_extents == 0) {
6125 *done = 1;
6126 goto del_cursor;
6127 }
6128
6129 /*
6130 * In case of first right shift, we need to initialize next_fsb
6131 */
6132 if (*next_fsb == NULLFSBLOCK) {
6133 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6134 xfs_bmbt_get_all(gotp, &got);
6135 *next_fsb = got.br_startoff;
6136 if (stop_fsb > *next_fsb) {
6137 *done = 1;
6138 goto del_cursor;
6139 }
6140 }
6141
6142 /* Lookup the extent index at which we have to stop */
6143 if (direction == SHIFT_RIGHT) {
6144 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6145 /* Make stop_extent exclusive of shift range */
6146 stop_extent--;
6147 } else
6148 stop_extent = total_extents;
6149
6150 /*
6151 * Look up the extent index for the fsb where we start shifting. We can
6152 * henceforth iterate with current_ext as extent list changes are locked
6153 * out via ilock.
6154 *
6155 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6156 * *next_fsb lies in a hole beyond which there are no extents. Either
6157 * way, we are done.
6158 */
6159 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
6160 if (!gotp) {
6161 *done = 1;
6162 goto del_cursor;
6163 }
6164
6165 /* some sanity checking before we finally start shifting extents */
6166 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6167 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6168 error = -EIO;
6169 goto del_cursor;
6170 }
6171
6172 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006173 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006174 &current_ext, gotp, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006175 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006176 if (error)
6177 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006178 /*
6179 * If there was an extent merge during the shift, the extent
6180 * count can change. Update the total and grade the next record.
6181 */
6182 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006183 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006184 stop_extent = total_extents;
6185 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006186
Namjae Jeona904b1c2015-03-25 15:08:56 +11006187 if (current_ext == stop_extent) {
6188 *done = 1;
6189 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006190 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006191 }
Brian Fosterddb19e32014-09-23 15:38:09 +10006192 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006193 }
6194
Namjae Jeona904b1c2015-03-25 15:08:56 +11006195 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006196 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10006197 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10006198 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006199
6200del_cursor:
6201 if (cur)
6202 xfs_btree_del_cursor(cur,
6203 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6204
Brian Fosterca446d82014-09-02 12:12:53 +10006205 if (logflags)
6206 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006207
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006208 return error;
6209}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006210
6211/*
6212 * Splits an extent into two extents at split_fsb block such that it is
6213 * the first block of the current_ext. @current_ext is a target extent
6214 * to be split. @split_fsb is a block where the extents is split.
6215 * If split_fsb lies in a hole or the first block of extents, just return 0.
6216 */
6217STATIC int
6218xfs_bmap_split_extent_at(
6219 struct xfs_trans *tp,
6220 struct xfs_inode *ip,
6221 xfs_fileoff_t split_fsb,
6222 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006223 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006224{
6225 int whichfork = XFS_DATA_FORK;
6226 struct xfs_btree_cur *cur = NULL;
6227 struct xfs_bmbt_rec_host *gotp;
6228 struct xfs_bmbt_irec got;
6229 struct xfs_bmbt_irec new; /* split extent */
6230 struct xfs_mount *mp = ip->i_mount;
6231 struct xfs_ifork *ifp;
6232 xfs_fsblock_t gotblkcnt; /* new block count for got */
6233 xfs_extnum_t current_ext;
6234 int error = 0;
6235 int logflags = 0;
6236 int i = 0;
6237
6238 if (unlikely(XFS_TEST_ERROR(
6239 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6240 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6241 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6242 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6243 XFS_ERRLEVEL_LOW, mp);
6244 return -EFSCORRUPTED;
6245 }
6246
6247 if (XFS_FORCED_SHUTDOWN(mp))
6248 return -EIO;
6249
6250 ifp = XFS_IFORK_PTR(ip, whichfork);
6251 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6252 /* Read in all the extents */
6253 error = xfs_iread_extents(tp, ip, whichfork);
6254 if (error)
6255 return error;
6256 }
6257
6258 /*
6259 * gotp can be null in 2 cases: 1) if there are no extents
6260 * or 2) split_fsb lies in a hole beyond which there are
6261 * no extents. Either way, we are done.
6262 */
6263 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
6264 if (!gotp)
6265 return 0;
6266
6267 xfs_bmbt_get_all(gotp, &got);
6268
6269 /*
6270 * Check split_fsb lies in a hole or the start boundary offset
6271 * of the extent.
6272 */
6273 if (got.br_startoff >= split_fsb)
6274 return 0;
6275
6276 gotblkcnt = split_fsb - got.br_startoff;
6277 new.br_startoff = split_fsb;
6278 new.br_startblock = got.br_startblock + gotblkcnt;
6279 new.br_blockcount = got.br_blockcount - gotblkcnt;
6280 new.br_state = got.br_state;
6281
6282 if (ifp->if_flags & XFS_IFBROOT) {
6283 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6284 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006285 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006286 cur->bc_private.b.flags = 0;
6287 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6288 got.br_startblock,
6289 got.br_blockcount,
6290 &i);
6291 if (error)
6292 goto del_cursor;
6293 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6294 }
6295
6296 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6297 got.br_blockcount = gotblkcnt;
6298
6299 logflags = XFS_ILOG_CORE;
6300 if (cur) {
6301 error = xfs_bmbt_update(cur, got.br_startoff,
6302 got.br_startblock,
6303 got.br_blockcount,
6304 got.br_state);
6305 if (error)
6306 goto del_cursor;
6307 } else
6308 logflags |= XFS_ILOG_DEXT;
6309
6310 /* Add new extent */
6311 current_ext++;
6312 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6313 XFS_IFORK_NEXT_SET(ip, whichfork,
6314 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6315
6316 if (cur) {
6317 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6318 new.br_startblock, new.br_blockcount,
6319 &i);
6320 if (error)
6321 goto del_cursor;
6322 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6323 cur->bc_rec.b.br_state = new.br_state;
6324
6325 error = xfs_btree_insert(cur, &i);
6326 if (error)
6327 goto del_cursor;
6328 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6329 }
6330
6331 /*
6332 * Convert to a btree if necessary.
6333 */
6334 if (xfs_bmap_needs_btree(ip, whichfork)) {
6335 int tmp_logflags; /* partial log flag return val */
6336
6337 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006338 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006339 &cur, 0, &tmp_logflags, whichfork);
6340 logflags |= tmp_logflags;
6341 }
6342
6343del_cursor:
6344 if (cur) {
6345 cur->bc_private.b.allocated = 0;
6346 xfs_btree_del_cursor(cur,
6347 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6348 }
6349
6350 if (logflags)
6351 xfs_trans_log_inode(tp, ip, logflags);
6352 return error;
6353}
6354
6355int
6356xfs_bmap_split_extent(
6357 struct xfs_inode *ip,
6358 xfs_fileoff_t split_fsb)
6359{
6360 struct xfs_mount *mp = ip->i_mount;
6361 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006362 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006363 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006364 int error;
6365
Christoph Hellwig253f4912016-04-06 09:19:55 +10006366 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6367 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6368 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006369 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006370
6371 xfs_ilock(ip, XFS_ILOCK_EXCL);
6372 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6373
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006374 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006375
6376 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006377 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006378 if (error)
6379 goto out;
6380
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006381 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006382 if (error)
6383 goto out;
6384
Christoph Hellwig70393312015-06-04 13:48:08 +10006385 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006386
6387out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006388 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006389 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006390 return error;
6391}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006392
6393/* Deferred mapping is only for real extents in the data fork. */
6394static bool
6395xfs_bmap_is_update_needed(
6396 struct xfs_bmbt_irec *bmap)
6397{
6398 return bmap->br_startblock != HOLESTARTBLOCK &&
6399 bmap->br_startblock != DELAYSTARTBLOCK;
6400}
6401
6402/* Record a bmap intent. */
6403static int
6404__xfs_bmap_add(
6405 struct xfs_mount *mp,
6406 struct xfs_defer_ops *dfops,
6407 enum xfs_bmap_intent_type type,
6408 struct xfs_inode *ip,
6409 int whichfork,
6410 struct xfs_bmbt_irec *bmap)
6411{
6412 int error;
6413 struct xfs_bmap_intent *bi;
6414
6415 trace_xfs_bmap_defer(mp,
6416 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6417 type,
6418 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6419 ip->i_ino, whichfork,
6420 bmap->br_startoff,
6421 bmap->br_blockcount,
6422 bmap->br_state);
6423
6424 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6425 INIT_LIST_HEAD(&bi->bi_list);
6426 bi->bi_type = type;
6427 bi->bi_owner = ip;
6428 bi->bi_whichfork = whichfork;
6429 bi->bi_bmap = *bmap;
6430
6431 error = xfs_defer_join(dfops, bi->bi_owner);
6432 if (error) {
6433 kmem_free(bi);
6434 return error;
6435 }
6436
6437 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6438 return 0;
6439}
6440
6441/* Map an extent into a file. */
6442int
6443xfs_bmap_map_extent(
6444 struct xfs_mount *mp,
6445 struct xfs_defer_ops *dfops,
6446 struct xfs_inode *ip,
6447 struct xfs_bmbt_irec *PREV)
6448{
6449 if (!xfs_bmap_is_update_needed(PREV))
6450 return 0;
6451
6452 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6453 XFS_DATA_FORK, PREV);
6454}
6455
6456/* Unmap an extent out of a file. */
6457int
6458xfs_bmap_unmap_extent(
6459 struct xfs_mount *mp,
6460 struct xfs_defer_ops *dfops,
6461 struct xfs_inode *ip,
6462 struct xfs_bmbt_irec *PREV)
6463{
6464 if (!xfs_bmap_is_update_needed(PREV))
6465 return 0;
6466
6467 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6468 XFS_DATA_FORK, PREV);
6469}
6470
6471/*
6472 * Process one of the deferred bmap operations. We pass back the
6473 * btree cursor to maintain our lock on the bmapbt between calls.
6474 */
6475int
6476xfs_bmap_finish_one(
6477 struct xfs_trans *tp,
6478 struct xfs_defer_ops *dfops,
6479 struct xfs_inode *ip,
6480 enum xfs_bmap_intent_type type,
6481 int whichfork,
6482 xfs_fileoff_t startoff,
6483 xfs_fsblock_t startblock,
6484 xfs_filblks_t blockcount,
6485 xfs_exntst_t state)
6486{
6487 struct xfs_bmbt_irec bmap;
6488 int nimaps = 1;
6489 xfs_fsblock_t firstfsb;
6490 int flags = XFS_BMAPI_REMAP;
6491 int done;
6492 int error = 0;
6493
6494 bmap.br_startblock = startblock;
6495 bmap.br_startoff = startoff;
6496 bmap.br_blockcount = blockcount;
6497 bmap.br_state = state;
6498
6499 trace_xfs_bmap_deferred(tp->t_mountp,
6500 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6501 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6502 ip->i_ino, whichfork, startoff, blockcount, state);
6503
6504 if (whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK)
6505 return -EFSCORRUPTED;
6506 if (whichfork == XFS_ATTR_FORK)
6507 flags |= XFS_BMAPI_ATTRFORK;
6508
6509 if (XFS_TEST_ERROR(false, tp->t_mountp,
6510 XFS_ERRTAG_BMAP_FINISH_ONE,
6511 XFS_RANDOM_BMAP_FINISH_ONE))
6512 return -EIO;
6513
6514 switch (type) {
6515 case XFS_BMAP_MAP:
6516 firstfsb = bmap.br_startblock;
6517 error = xfs_bmapi_write(tp, ip, bmap.br_startoff,
6518 bmap.br_blockcount, flags, &firstfsb,
6519 bmap.br_blockcount, &bmap, &nimaps,
6520 dfops);
6521 break;
6522 case XFS_BMAP_UNMAP:
6523 error = xfs_bunmapi(tp, ip, bmap.br_startoff,
6524 bmap.br_blockcount, flags, 1, &firstfsb,
6525 dfops, &done);
6526 ASSERT(done);
6527 break;
6528 default:
6529 ASSERT(0);
6530 error = -EFSCORRUPTED;
6531 }
6532
6533 return error;
6534}