blob: 2ae55db8c977dde4b175a6fe67bc6b637e2cddd7 [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 ||
801 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000802 (dfops->dop_low &&
Dave Chinner9e5987a2013-02-25 12:31:26 +1100803 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
804 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
805 cur->bc_private.b.allocated++;
806 ip->i_d.di_nblocks++;
807 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
808 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
809 /*
810 * Fill in the child block.
811 */
812 abp->b_ops = &xfs_bmbt_buf_ops;
813 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800814 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
815 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500816 XFS_BTREE_LONG_PTRS);
817
Dave Chinner9e5987a2013-02-25 12:31:26 +1100818 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100819 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100820 for (cnt = i = 0; i < nextents; i++) {
821 ep = xfs_iext_get_ext(ifp, i);
822 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
823 arp->l0 = cpu_to_be64(ep->l0);
824 arp->l1 = cpu_to_be64(ep->l1);
825 arp++; cnt++;
826 }
827 }
828 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
829 xfs_btree_set_numrecs(ablock, cnt);
830
831 /*
832 * Fill in the root key and pointer.
833 */
834 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
835 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
836 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
837 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
838 be16_to_cpu(block->bb_level)));
839 *pp = cpu_to_be64(args.fsbno);
840
841 /*
842 * Do all this logging at the end so that
843 * the root is at the right level.
844 */
845 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
846 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
847 ASSERT(*curp == NULL);
848 *curp = cur;
849 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
850 return 0;
851}
852
853/*
854 * Convert a local file to an extents file.
855 * This code is out of bounds for data forks of regular files,
856 * since the file data needs to get logged so things will stay consistent.
857 * (The bmap-level manipulations are ok, though).
858 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000859void
860xfs_bmap_local_to_extents_empty(
861 struct xfs_inode *ip,
862 int whichfork)
863{
864 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
865
Darrick J. Wong60b49842016-10-03 09:11:34 -0700866 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000867 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
868 ASSERT(ifp->if_bytes == 0);
869 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
870
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000871 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000872 ifp->if_flags &= ~XFS_IFINLINE;
873 ifp->if_flags |= XFS_IFEXTENTS;
874 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
875}
876
877
Dave Chinner9e5987a2013-02-25 12:31:26 +1100878STATIC int /* error */
879xfs_bmap_local_to_extents(
880 xfs_trans_t *tp, /* transaction pointer */
881 xfs_inode_t *ip, /* incore inode pointer */
882 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
883 xfs_extlen_t total, /* total blocks needed by transaction */
884 int *logflagsp, /* inode logging flags */
885 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500886 void (*init_fn)(struct xfs_trans *tp,
887 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100888 struct xfs_inode *ip,
889 struct xfs_ifork *ifp))
890{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000891 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100892 int flags; /* logging flags returned */
893 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000894 xfs_alloc_arg_t args; /* allocation arguments */
895 xfs_buf_t *bp; /* buffer for extent block */
896 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100897
898 /*
899 * We don't want to deal with the case of keeping inode data inline yet.
900 * So sending the data fork of a regular inode is invalid.
901 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100902 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100903 ifp = XFS_IFORK_PTR(ip, whichfork);
904 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000905
906 if (!ifp->if_bytes) {
907 xfs_bmap_local_to_extents_empty(ip, whichfork);
908 flags = XFS_ILOG_CORE;
909 goto done;
910 }
911
Dave Chinner9e5987a2013-02-25 12:31:26 +1100912 flags = 0;
913 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000914 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
915 XFS_IFINLINE);
916 memset(&args, 0, sizeof(args));
917 args.tp = tp;
918 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000919 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000920 args.firstblock = *firstblock;
921 /*
922 * Allocate a block. We know we need only one, since the
923 * file currently fits in an inode.
924 */
925 if (*firstblock == NULLFSBLOCK) {
Darrick J. Wong90e20562016-10-03 09:11:45 -0700926try_another_ag:
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000927 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
928 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100929 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000930 args.fsbno = *firstblock;
931 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100932 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000933 args.total = total;
934 args.minlen = args.maxlen = args.prod = 1;
935 error = xfs_alloc_vextent(&args);
936 if (error)
937 goto done;
938
Darrick J. Wong90e20562016-10-03 09:11:45 -0700939 /*
940 * During a CoW operation, the allocation and bmbt updates occur in
941 * different transactions. The mapping code tries to put new bmbt
942 * blocks near extents being mapped, but the only way to guarantee this
943 * is if the alloc and the mapping happen in a single transaction that
944 * has a block reservation. That isn't the case here, so if we run out
945 * of space we'll try again with another AG.
946 */
947 if (xfs_sb_version_hasreflink(&ip->i_mount->m_sb) &&
948 args.fsbno == NULLFSBLOCK &&
949 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
950 goto try_another_ag;
951 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000952 /* Can't fail, the space was reserved. */
953 ASSERT(args.fsbno != NULLFSBLOCK);
954 ASSERT(args.len == 1);
955 *firstblock = args.fsbno;
956 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
957
Dave Chinnerfe22d552015-01-22 09:30:06 +1100958 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100959 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100960 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100961 * The callout is responsible for logging because the remote format
962 * might differ from the local format and thus we don't know how much to
963 * log here. Note that init_fn must also set the buffer log item type
964 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100965 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000966 init_fn(tp, bp, ip, ifp);
967
Brian Fosterb7cdc662015-10-12 15:40:24 +1100968 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000969 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
970 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100971 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000972
973 xfs_iext_add(ifp, 0, 1);
974 ep = xfs_iext_get_ext(ifp, 0);
975 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
976 trace_xfs_bmap_post_update(ip, 0,
977 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
978 _THIS_IP_);
979 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
980 ip->i_d.di_nblocks = 1;
981 xfs_trans_mod_dquot_byino(tp, ip,
982 XFS_TRANS_DQ_BCOUNT, 1L);
983 flags |= xfs_ilog_fext(whichfork);
984
Dave Chinner9e5987a2013-02-25 12:31:26 +1100985done:
986 *logflagsp = flags;
987 return error;
988}
989
990/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 * Called from xfs_bmap_add_attrfork to handle btree format files.
992 */
993STATIC int /* error */
994xfs_bmap_add_attrfork_btree(
995 xfs_trans_t *tp, /* transaction pointer */
996 xfs_inode_t *ip, /* incore inode pointer */
997 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000998 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 int *flags) /* inode logging flags */
1000{
1001 xfs_btree_cur_t *cur; /* btree cursor */
1002 int error; /* error return value */
1003 xfs_mount_t *mp; /* file system mount struct */
1004 int stat; /* newroot status */
1005
1006 mp = ip->i_mount;
1007 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1008 *flags |= XFS_ILOG_DBROOT;
1009 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001010 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001011 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 cur->bc_private.b.firstblock = *firstblock;
1013 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1014 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001015 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001016 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001017 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 goto error0;
1019 if (stat == 0) {
1020 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +10001021 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023 *firstblock = cur->bc_private.b.firstblock;
1024 cur->bc_private.b.allocated = 0;
1025 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1026 }
1027 return 0;
1028error0:
1029 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1030 return error;
1031}
1032
1033/*
1034 * Called from xfs_bmap_add_attrfork to handle extents format files.
1035 */
1036STATIC int /* error */
1037xfs_bmap_add_attrfork_extents(
1038 xfs_trans_t *tp, /* transaction pointer */
1039 xfs_inode_t *ip, /* incore inode pointer */
1040 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001041 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 int *flags) /* inode logging flags */
1043{
1044 xfs_btree_cur_t *cur; /* bmap btree cursor */
1045 int error; /* error return value */
1046
1047 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1048 return 0;
1049 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001050 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 flags, XFS_DATA_FORK);
1052 if (cur) {
1053 cur->bc_private.b.allocated = 0;
1054 xfs_btree_del_cursor(cur,
1055 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1056 }
1057 return error;
1058}
1059
1060/*
Dave Chinner1e823792013-02-11 15:58:13 +11001061 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1062 * different data fork content type needs a different callout to do the
1063 * conversion. Some are basic and only require special block initialisation
1064 * callouts for the data formating, others (directories) are so specialised they
1065 * handle everything themselves.
1066 *
1067 * XXX (dgc): investigate whether directory conversion can use the generic
1068 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001069 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 */
1071STATIC int /* error */
1072xfs_bmap_add_attrfork_local(
1073 xfs_trans_t *tp, /* transaction pointer */
1074 xfs_inode_t *ip, /* incore inode pointer */
1075 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001076 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 int *flags) /* inode logging flags */
1078{
1079 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1082 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001083
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001084 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001086 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 dargs.dp = ip;
1088 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001089 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001090 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 dargs.whichfork = XFS_DATA_FORK;
1092 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001093 return xfs_dir2_sf_to_block(&dargs);
1094 }
1095
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001096 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001097 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1098 flags, XFS_DATA_FORK,
1099 xfs_symlink_local_to_remote);
1100
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001101 /* should only be called for types that support local format data */
1102 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001103 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105
1106/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001107 * Convert inode from non-attributed to attributed.
1108 * Must not be in a transaction, ip must not be locked.
1109 */
1110int /* error code */
1111xfs_bmap_add_attrfork(
1112 xfs_inode_t *ip, /* incore inode pointer */
1113 int size, /* space new attribute needs */
1114 int rsvd) /* xact may use reserved blks */
1115{
1116 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001117 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001118 xfs_mount_t *mp; /* mount structure */
1119 xfs_trans_t *tp; /* transaction pointer */
1120 int blks; /* space reservation */
1121 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001122 int logflags; /* logging flags */
1123 int error; /* error return value */
1124
1125 ASSERT(XFS_IFORK_Q(ip) == 0);
1126
1127 mp = ip->i_mount;
1128 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001129
Dave Chinner9e5987a2013-02-25 12:31:26 +11001130 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001131
1132 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1133 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1134 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001135 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001136
Dave Chinner9e5987a2013-02-25 12:31:26 +11001137 xfs_ilock(ip, XFS_ILOCK_EXCL);
1138 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1139 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1140 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001141 if (error)
1142 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001143 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001144 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001145 if (ip->i_d.di_anextents != 0) {
1146 error = -EFSCORRUPTED;
1147 goto trans_cancel;
1148 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001149 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1150 /*
1151 * For inodes coming from pre-6.2 filesystems.
1152 */
1153 ASSERT(ip->i_d.di_aformat == 0);
1154 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1155 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001156
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001157 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001158 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1159
1160 switch (ip->i_d.di_format) {
1161 case XFS_DINODE_FMT_DEV:
1162 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1163 break;
1164 case XFS_DINODE_FMT_UUID:
1165 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1166 break;
1167 case XFS_DINODE_FMT_LOCAL:
1168 case XFS_DINODE_FMT_EXTENTS:
1169 case XFS_DINODE_FMT_BTREE:
1170 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1171 if (!ip->i_d.di_forkoff)
1172 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1173 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1174 version = 2;
1175 break;
1176 default:
1177 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001178 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001179 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001180 }
1181
1182 ASSERT(ip->i_afp == NULL);
1183 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1184 ip->i_afp->if_flags = XFS_IFEXTENTS;
1185 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001186 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001187 switch (ip->i_d.di_format) {
1188 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001189 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001190 &logflags);
1191 break;
1192 case XFS_DINODE_FMT_EXTENTS:
1193 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001194 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001195 break;
1196 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001197 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001198 &logflags);
1199 break;
1200 default:
1201 error = 0;
1202 break;
1203 }
1204 if (logflags)
1205 xfs_trans_log_inode(tp, ip, logflags);
1206 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001207 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001208 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1209 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001210 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001211
1212 spin_lock(&mp->m_sb_lock);
1213 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1214 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001215 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001216 }
1217 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1218 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001219 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001220 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001221 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001222 if (log_sb)
1223 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001224 }
1225
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001226 error = xfs_defer_finish(&tp, &dfops, NULL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001227 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001228 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001229 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001230 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001231 return error;
1232
1233bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001234 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001235trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001236 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001237 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001238 return error;
1239}
1240
1241/*
1242 * Internal and external extent tree search functions.
1243 */
1244
1245/*
1246 * Read in the extents to if_extents.
1247 * All inode fields are set up by caller, we just traverse the btree
1248 * and copy the records in. If the file system cannot contain unwritten
1249 * extents, the records are checked for no "state" flags.
1250 */
1251int /* error */
1252xfs_bmap_read_extents(
1253 xfs_trans_t *tp, /* transaction pointer */
1254 xfs_inode_t *ip, /* incore inode */
1255 int whichfork) /* data or attr fork */
1256{
1257 struct xfs_btree_block *block; /* current btree block */
1258 xfs_fsblock_t bno; /* block # of "block" */
1259 xfs_buf_t *bp; /* buffer for "block" */
1260 int error; /* error return value */
1261 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1262 xfs_extnum_t i, j; /* index into the extents list */
1263 xfs_ifork_t *ifp; /* fork structure */
1264 int level; /* btree level, for checking */
1265 xfs_mount_t *mp; /* file system mount structure */
1266 __be64 *pp; /* pointer to block address */
1267 /* REFERENCED */
1268 xfs_extnum_t room; /* number of entries there's room for */
1269
Dave Chinner9e5987a2013-02-25 12:31:26 +11001270 mp = ip->i_mount;
1271 ifp = XFS_IFORK_PTR(ip, whichfork);
1272 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1273 XFS_EXTFMT_INODE(ip);
1274 block = ifp->if_broot;
1275 /*
1276 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1277 */
1278 level = be16_to_cpu(block->bb_level);
1279 ASSERT(level > 0);
1280 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1281 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001282
Dave Chinner9e5987a2013-02-25 12:31:26 +11001283 /*
1284 * Go down the tree until leaf level is reached, following the first
1285 * pointer (leftmost) at each level.
1286 */
1287 while (level-- > 0) {
1288 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1289 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1290 if (error)
1291 return error;
1292 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001293 if (level == 0)
1294 break;
1295 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1296 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001297 XFS_WANT_CORRUPTED_GOTO(mp,
1298 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001299 xfs_trans_brelse(tp, bp);
1300 }
1301 /*
1302 * Here with bp and block set to the leftmost leaf node in the tree.
1303 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001304 room = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001305 i = 0;
1306 /*
1307 * Loop over all leaf nodes. Copy information to the extent records.
1308 */
1309 for (;;) {
1310 xfs_bmbt_rec_t *frp;
1311 xfs_fsblock_t nextbno;
1312 xfs_extnum_t num_recs;
1313 xfs_extnum_t start;
1314
1315 num_recs = xfs_btree_get_numrecs(block);
1316 if (unlikely(i + num_recs > room)) {
1317 ASSERT(i + num_recs <= room);
1318 xfs_warn(ip->i_mount,
1319 "corrupt dinode %Lu, (btree extents).",
1320 (unsigned long long) ip->i_ino);
1321 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1322 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1323 goto error0;
1324 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001325 /*
1326 * Read-ahead the next leaf block, if any.
1327 */
1328 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1329 if (nextbno != NULLFSBLOCK)
1330 xfs_btree_reada_bufl(mp, nextbno, 1,
1331 &xfs_bmbt_buf_ops);
1332 /*
1333 * Copy records into the extent records.
1334 */
1335 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1336 start = i;
1337 for (j = 0; j < num_recs; j++, i++, frp++) {
1338 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1339 trp->l0 = be64_to_cpu(frp->l0);
1340 trp->l1 = be64_to_cpu(frp->l1);
1341 }
1342 if (exntf == XFS_EXTFMT_NOSTATE) {
1343 /*
1344 * Check all attribute bmap btree records and
1345 * any "older" data bmap btree records for a
1346 * set bit in the "extent flag" position.
1347 */
1348 if (unlikely(xfs_check_nostate_extents(ifp,
1349 start, num_recs))) {
1350 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1351 XFS_ERRLEVEL_LOW,
1352 ip->i_mount);
1353 goto error0;
1354 }
1355 }
1356 xfs_trans_brelse(tp, bp);
1357 bno = nextbno;
1358 /*
1359 * If we've reached the end, stop.
1360 */
1361 if (bno == NULLFSBLOCK)
1362 break;
1363 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1364 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1365 if (error)
1366 return error;
1367 block = XFS_BUF_TO_BLOCK(bp);
1368 }
Darrick J. Wong356a3222016-12-05 12:36:56 +11001369 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1370 return -EFSCORRUPTED;
Eric Sandeen5d829302016-11-08 12:59:42 +11001371 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11001372 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1373 return 0;
1374error0:
1375 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001376 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001377}
1378
Dave Chinner9e5987a2013-02-25 12:31:26 +11001379/*
1380 * Returns the file-relative block number of the first unused block(s)
1381 * in the file with at least "len" logically contiguous blocks free.
1382 * This is the lowest-address hole if the file has holes, else the first block
1383 * past the end of file.
1384 * Return 0 if the file is currently local (in-inode).
1385 */
1386int /* error */
1387xfs_bmap_first_unused(
1388 xfs_trans_t *tp, /* transaction pointer */
1389 xfs_inode_t *ip, /* incore inode */
1390 xfs_extlen_t len, /* size of hole to find */
1391 xfs_fileoff_t *first_unused, /* unused block */
1392 int whichfork) /* data or attr fork */
1393{
1394 int error; /* error return value */
1395 int idx; /* extent record index */
1396 xfs_ifork_t *ifp; /* inode fork pointer */
1397 xfs_fileoff_t lastaddr; /* last block number seen */
1398 xfs_fileoff_t lowest; /* lowest useful block */
1399 xfs_fileoff_t max; /* starting useful block */
1400 xfs_fileoff_t off; /* offset for this block */
1401 xfs_extnum_t nextents; /* number of extent entries */
1402
1403 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1404 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1405 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1406 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1407 *first_unused = 0;
1408 return 0;
1409 }
1410 ifp = XFS_IFORK_PTR(ip, whichfork);
1411 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1412 (error = xfs_iread_extents(tp, ip, whichfork)))
1413 return error;
1414 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001415 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001416 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1417 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1418 off = xfs_bmbt_get_startoff(ep);
1419 /*
1420 * See if the hole before this extent will work.
1421 */
1422 if (off >= lowest + len && off - max >= len) {
1423 *first_unused = max;
1424 return 0;
1425 }
1426 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1427 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1428 }
1429 *first_unused = max;
1430 return 0;
1431}
1432
1433/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001434 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001435 * last_block (input value) in the file.
1436 * This is not based on i_size, it is based on the extent records.
1437 * Returns 0 for local files, as they do not have extent records.
1438 */
1439int /* error */
1440xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001441 struct xfs_trans *tp, /* transaction pointer */
1442 struct xfs_inode *ip, /* incore inode */
1443 xfs_fileoff_t *last_block, /* last block */
1444 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001445{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001446 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1447 struct xfs_bmbt_irec got;
1448 xfs_extnum_t idx;
1449 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001450
Christoph Hellwig86685f72016-11-24 11:39:38 +11001451 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1452 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001453 *last_block = 0;
1454 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001455 case XFS_DINODE_FMT_BTREE:
1456 case XFS_DINODE_FMT_EXTENTS:
1457 break;
1458 default:
1459 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001460 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001461
1462 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1463 error = xfs_iread_extents(tp, ip, whichfork);
1464 if (error)
1465 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001466 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001467
1468 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1469 if (got.br_startoff <= *last_block - 1)
1470 return 0;
1471 }
1472
1473 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1474 *last_block = got.br_startoff + got.br_blockcount;
1475 return 0;
1476 }
1477
1478 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001479 return 0;
1480}
1481
Dave Chinner68988112013-08-12 20:49:42 +10001482int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001483xfs_bmap_last_extent(
1484 struct xfs_trans *tp,
1485 struct xfs_inode *ip,
1486 int whichfork,
1487 struct xfs_bmbt_irec *rec,
1488 int *is_empty)
1489{
1490 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1491 int error;
1492 int nextents;
1493
1494 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1495 error = xfs_iread_extents(tp, ip, whichfork);
1496 if (error)
1497 return error;
1498 }
1499
Eric Sandeen5d829302016-11-08 12:59:42 +11001500 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001501 if (nextents == 0) {
1502 *is_empty = 1;
1503 return 0;
1504 }
1505
1506 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1507 *is_empty = 0;
1508 return 0;
1509}
1510
1511/*
1512 * Check the last inode extent to determine whether this allocation will result
1513 * in blocks being allocated at the end of the file. When we allocate new data
1514 * blocks at the end of the file which do not start at the previous data block,
1515 * we will try to align the new blocks at stripe unit boundaries.
1516 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001517 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001518 * at, or past the EOF.
1519 */
1520STATIC int
1521xfs_bmap_isaeof(
1522 struct xfs_bmalloca *bma,
1523 int whichfork)
1524{
1525 struct xfs_bmbt_irec rec;
1526 int is_empty;
1527 int error;
1528
1529 bma->aeof = 0;
1530 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1531 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001532 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001533 return error;
1534
Dave Chinner6e708bc2013-11-22 10:41:16 +11001535 if (is_empty) {
1536 bma->aeof = 1;
1537 return 0;
1538 }
1539
Dave Chinner9e5987a2013-02-25 12:31:26 +11001540 /*
1541 * Check if we are allocation or past the last extent, or at least into
1542 * the last delayed allocated extent.
1543 */
1544 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1545 (bma->offset >= rec.br_startoff &&
1546 isnullstartblock(rec.br_startblock));
1547 return 0;
1548}
1549
1550/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001551 * Returns the file-relative block number of the first block past eof in
1552 * the file. This is not based on i_size, it is based on the extent records.
1553 * Returns 0 for local files, as they do not have extent records.
1554 */
1555int
1556xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001557 struct xfs_inode *ip,
1558 xfs_fileoff_t *last_block,
1559 int whichfork)
1560{
1561 struct xfs_bmbt_irec rec;
1562 int is_empty;
1563 int error;
1564
1565 *last_block = 0;
1566
1567 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1568 return 0;
1569
1570 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1571 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001572 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001573
1574 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1575 if (error || is_empty)
1576 return error;
1577
1578 *last_block = rec.br_startoff + rec.br_blockcount;
1579 return 0;
1580}
1581
1582/*
1583 * Returns whether the selected fork of the inode has exactly one
1584 * block or not. For the data fork we check this matches di_size,
1585 * implying the file's range is 0..bsize-1.
1586 */
1587int /* 1=>1 block, 0=>otherwise */
1588xfs_bmap_one_block(
1589 xfs_inode_t *ip, /* incore inode */
1590 int whichfork) /* data or attr fork */
1591{
1592 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1593 xfs_ifork_t *ifp; /* inode fork pointer */
1594 int rval; /* return value */
1595 xfs_bmbt_irec_t s; /* internal version of extent */
1596
1597#ifndef DEBUG
1598 if (whichfork == XFS_DATA_FORK)
1599 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1600#endif /* !DEBUG */
1601 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1602 return 0;
1603 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1604 return 0;
1605 ifp = XFS_IFORK_PTR(ip, whichfork);
1606 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1607 ep = xfs_iext_get_ext(ifp, 0);
1608 xfs_bmbt_get_all(ep, &s);
1609 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1610 if (rval && whichfork == XFS_DATA_FORK)
1611 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1612 return rval;
1613}
1614
1615/*
1616 * Extent tree manipulation functions used during allocation.
1617 */
1618
1619/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001620 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 */
1622STATIC int /* error */
1623xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001624 struct xfs_bmalloca *bma,
1625 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001627 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001629 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001632 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 xfs_fileoff_t new_endoff; /* end offset of new entry */
1634 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1635 /* left is 0, right is 1, prev is 2 */
1636 int rval=0; /* return value (logging flags) */
1637 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001638 xfs_filblks_t da_new; /* new count del alloc blocks used */
1639 xfs_filblks_t da_old; /* old count del alloc blocks used */
1640 xfs_filblks_t temp=0; /* value for da_new calculations */
1641 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001643 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001644 xfs_extnum_t *nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001646 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001647 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001648 ASSERT(whichfork != XFS_ATTR_FORK);
1649 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1650 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001651
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001652 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001653 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001654 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001655 ASSERT(!bma->cur ||
1656 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001657
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001658 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660#define LEFT r[0]
1661#define RIGHT r[1]
1662#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Darrick J. Wong60b49842016-10-03 09:11:34 -07001664 if (whichfork == XFS_COW_FORK)
1665 state |= BMAP_COWFORK;
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 /*
1668 * Set up a bunch of variables to make the tests simpler.
1669 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001670 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 xfs_bmbt_get_all(ep, &PREV);
1672 new_endoff = new->br_startoff + new->br_blockcount;
1673 ASSERT(PREV.br_startoff <= new->br_startoff);
1674 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001675
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001676 da_old = startblockval(PREV.br_startblock);
1677 da_new = 0;
1678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /*
1680 * Set flags determining what part of the previous delayed allocation
1681 * extent is being replaced by a real allocation.
1682 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001683 if (PREV.br_startoff == new->br_startoff)
1684 state |= BMAP_LEFT_FILLING;
1685 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1686 state |= BMAP_RIGHT_FILLING;
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 /*
1689 * Check and set flags if this segment has a left neighbor.
1690 * Don't set contiguous if the combined extent would be too large.
1691 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001692 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001693 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001694 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001695
1696 if (isnullstartblock(LEFT.br_startblock))
1697 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001699
1700 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1701 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1702 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1703 LEFT.br_state == new->br_state &&
1704 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1705 state |= BMAP_LEFT_CONTIG;
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 /*
1708 * Check and set flags if this segment has a right neighbor.
1709 * Don't set contiguous if the combined extent would be too large.
1710 * Also check for all-three-contiguous being too large.
1711 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001712 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001713 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001714 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001715
1716 if (isnullstartblock(RIGHT.br_startblock))
1717 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001719
1720 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1721 new_endoff == RIGHT.br_startoff &&
1722 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1723 new->br_state == RIGHT.br_state &&
1724 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1725 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1726 BMAP_RIGHT_FILLING)) !=
1727 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1728 BMAP_RIGHT_FILLING) ||
1729 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1730 <= MAXEXTLEN))
1731 state |= BMAP_RIGHT_CONTIG;
1732
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 error = 0;
1734 /*
1735 * Switch out based on the FILLING and CONTIG state bits.
1736 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001737 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1738 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1739 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1740 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 /*
1742 * Filling in all of a previously delayed allocation extent.
1743 * The left and right neighbors are both contiguous with new.
1744 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001745 bma->idx--;
1746 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1747 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 LEFT.br_blockcount + PREV.br_blockcount +
1749 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001750 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001751
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001752 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001753 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001754 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1756 else {
1757 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001758 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001760 RIGHT.br_blockcount, &i);
1761 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001763 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001764 error = xfs_btree_delete(bma->cur, &i);
1765 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001767 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001768 error = xfs_btree_decrement(bma->cur, 0, &i);
1769 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001771 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001772 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 LEFT.br_startblock,
1774 LEFT.br_blockcount +
1775 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001776 RIGHT.br_blockcount, LEFT.br_state);
1777 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto done;
1779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 break;
1781
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001782 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /*
1784 * Filling in all of a previously delayed allocation extent.
1785 * The left neighbor is contiguous, the right is not.
1786 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001787 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001788
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001789 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1790 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001791 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001792 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001793
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001794 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1795 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 rval = XFS_ILOG_DEXT;
1797 else {
1798 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001799 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001801 &i);
1802 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001804 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001805 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 LEFT.br_startblock,
1807 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001808 PREV.br_blockcount, LEFT.br_state);
1809 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 goto done;
1811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 break;
1813
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001814 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /*
1816 * Filling in all of a previously delayed allocation extent.
1817 * The right neighbor is contiguous, the left is not.
1818 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 xfs_bmbt_set_startblock(ep, new->br_startblock);
1821 xfs_bmbt_set_blockcount(ep,
1822 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001823 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001824
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001825 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1826 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 rval = XFS_ILOG_DEXT;
1828 else {
1829 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001830 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001832 RIGHT.br_blockcount, &i);
1833 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001835 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001836 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 new->br_startblock,
1838 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001839 RIGHT.br_blockcount, PREV.br_state);
1840 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 goto done;
1842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 break;
1844
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001845 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /*
1847 * Filling in all of a previously delayed allocation extent.
1848 * Neither the left nor right neighbors are contiguous with
1849 * the new one.
1850 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001851 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 xfs_bmbt_set_startblock(ep, new->br_startblock);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08001853 xfs_bmbt_set_state(ep, new->br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001854 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001855
Darrick J. Wong60b49842016-10-03 09:11:34 -07001856 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001857 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1859 else {
1860 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001861 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001863 &i);
1864 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001866 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001867 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1868 error = xfs_btree_insert(bma->cur, &i);
1869 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001871 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 break;
1874
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001875 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 /*
1877 * Filling in the first part of a previous delayed allocation.
1878 * The left neighbor is contiguous.
1879 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001880 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1881 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 LEFT.br_blockcount + new->br_blockcount);
1883 xfs_bmbt_set_startoff(ep,
1884 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001885 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001888 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001890 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 rval = XFS_ILOG_DEXT;
1892 else {
1893 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001894 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001896 &i);
1897 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001899 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001900 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 LEFT.br_startblock,
1902 LEFT.br_blockcount +
1903 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001904 LEFT.br_state);
1905 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 goto done;
1907 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001908 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001909 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001910 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001911 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001912
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001913 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 break;
1915
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001916 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /*
1918 * Filling in the first part of a previous delayed allocation.
1919 * The left neighbor is not contiguous.
1920 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001921 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 xfs_bmbt_set_startoff(ep, new_endoff);
1923 temp = PREV.br_blockcount - new->br_blockcount;
1924 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001925 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001926 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001927 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1929 else {
1930 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001931 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001933 &i);
1934 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001936 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001937 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1938 error = xfs_btree_insert(bma->cur, &i);
1939 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001941 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001943
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001944 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001945 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001946 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001947 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 rval |= tmp_rval;
1949 if (error)
1950 goto done;
1951 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001952 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001953 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001954 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1955 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001956 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001957 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 break;
1959
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001960 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /*
1962 * Filling in the last part of a previous delayed allocation.
1963 * The right neighbor is contiguous with the new allocation.
1964 */
1965 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001966 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001968 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001969 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 new->br_blockcount + RIGHT.br_blockcount,
1971 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001972 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1973 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 rval = XFS_ILOG_DEXT;
1975 else {
1976 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001977 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001979 RIGHT.br_blockcount, &i);
1980 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001982 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001983 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 new->br_startblock,
1985 new->br_blockcount +
1986 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001987 RIGHT.br_state);
1988 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 goto done;
1990 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001991
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001992 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001993 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001994 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001995 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001996 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001997
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001998 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 break;
2000
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002001 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 /*
2003 * Filling in the last part of a previous delayed allocation.
2004 * The right neighbor is not contiguous.
2005 */
2006 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002007 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002009 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002010 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002011 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2013 else {
2014 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002015 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002017 &i);
2018 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002020 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002021 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2022 error = xfs_btree_insert(bma->cur, &i);
2023 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002025 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002027
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002028 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002029 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002030 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002031 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 rval |= tmp_rval;
2033 if (error)
2034 goto done;
2035 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002036 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002037 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002038 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2039 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002040 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002041 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002042
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002043 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 break;
2045
2046 case 0:
2047 /*
2048 * Filling in the middle part of a previous delayed allocation.
2049 * Contiguity is impossible here.
2050 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002051 *
2052 * We start with a delayed allocation:
2053 *
2054 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2055 * PREV @ idx
2056 *
2057 * and we are allocating:
2058 * +rrrrrrrrrrrrrrrrr+
2059 * new
2060 *
2061 * and we set it up for insertion as:
2062 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2063 * new
2064 * PREV @ idx LEFT RIGHT
2065 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 */
2067 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002069 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002070 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2071 LEFT = *new;
2072 RIGHT.br_state = PREV.br_state;
2073 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002074 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002075 RIGHT.br_startoff = new_endoff;
2076 RIGHT.br_blockcount = temp2;
2077 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002078 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002079 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002080 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2082 else {
2083 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002084 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002086 &i);
2087 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002089 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002090 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2091 error = xfs_btree_insert(bma->cur, &i);
2092 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002094 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002096
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002097 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002098 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002099 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002100 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 rval |= tmp_rval;
2102 if (error)
2103 goto done;
2104 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002105 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2106 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002107 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002108 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002109 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002110 error = xfs_mod_fdblocks(bma->ip->i_mount,
2111 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002112 ASSERT(!error);
2113 if (error)
2114 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002116
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002117 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002118 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002119 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2120 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2121 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002122 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002123 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002124
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002125 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002126 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 break;
2128
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002129 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2130 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2131 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2132 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2133 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2134 case BMAP_LEFT_CONTIG:
2135 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /*
2137 * These cases are all impossible.
2138 */
2139 ASSERT(0);
2140 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002141
Darrick J. Wong9c194642016-08-03 12:16:05 +10002142 /* add reverse mapping */
2143 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2144 if (error)
2145 goto done;
2146
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002147 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002148 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002149 int tmp_logflags; /* partial log flag return val */
2150
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002151 ASSERT(bma->cur == NULL);
2152 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002153 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002154 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002155 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002156 if (error)
2157 goto done;
2158 }
2159
2160 /* adjust for changes in reserved delayed indirect blocks */
2161 if (da_old || da_new) {
2162 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002163 if (bma->cur)
2164 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002165 ASSERT(temp <= da_old);
2166 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002167 xfs_mod_fdblocks(bma->ip->i_mount,
2168 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002169 }
2170
2171 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002172 if (bma->cur)
2173 bma->cur->bc_private.b.allocated = 0;
2174
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002175 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002177 if (whichfork != XFS_COW_FORK)
2178 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return error;
2180#undef LEFT
2181#undef RIGHT
2182#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183}
2184
2185/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002186 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 */
2188STATIC int /* error */
2189xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002190 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002192 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002193 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002195 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002196 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002197 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002198 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002201 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002204 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 xfs_fileoff_t new_endoff; /* end offset of new entry */
2206 xfs_exntst_t newext; /* new extent state */
2207 xfs_exntst_t oldext; /* old extent state */
2208 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2209 /* left is 0, right is 1, prev is 2 */
2210 int rval=0; /* return value (logging flags) */
2211 int state = 0;/* state bits, accessed thru macros */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002212 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002214 *logflagsp = 0;
2215
2216 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002217 ifp = XFS_IFORK_PTR(ip, whichfork);
2218 if (whichfork == XFS_COW_FORK)
2219 state |= BMAP_COWFORK;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002220
2221 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002222 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002223 ASSERT(!isnullstartblock(new->br_startblock));
2224
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002225 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227#define LEFT r[0]
2228#define RIGHT r[1]
2229#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 /*
2232 * Set up a bunch of variables to make the tests simpler.
2233 */
2234 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002235 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 xfs_bmbt_get_all(ep, &PREV);
2237 newext = new->br_state;
2238 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2239 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2240 ASSERT(PREV.br_state == oldext);
2241 new_endoff = new->br_startoff + new->br_blockcount;
2242 ASSERT(PREV.br_startoff <= new->br_startoff);
2243 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 /*
2246 * Set flags determining what part of the previous oldext allocation
2247 * extent is being replaced by a newext allocation.
2248 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002249 if (PREV.br_startoff == new->br_startoff)
2250 state |= BMAP_LEFT_FILLING;
2251 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2252 state |= BMAP_RIGHT_FILLING;
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 /*
2255 * Check and set flags if this segment has a left neighbor.
2256 * Don't set contiguous if the combined extent would be too large.
2257 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002258 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002259 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002260 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002261
2262 if (isnullstartblock(LEFT.br_startblock))
2263 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002265
2266 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2267 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2268 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2269 LEFT.br_state == newext &&
2270 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2271 state |= BMAP_LEFT_CONTIG;
2272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 /*
2274 * Check and set flags if this segment has a right neighbor.
2275 * Don't set contiguous if the combined extent would be too large.
2276 * Also check for all-three-contiguous being too large.
2277 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002278 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002279 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002280 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002281 if (isnullstartblock(RIGHT.br_startblock))
2282 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002284
2285 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2286 new_endoff == RIGHT.br_startoff &&
2287 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2288 newext == RIGHT.br_state &&
2289 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2290 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2291 BMAP_RIGHT_FILLING)) !=
2292 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2293 BMAP_RIGHT_FILLING) ||
2294 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2295 <= MAXEXTLEN))
2296 state |= BMAP_RIGHT_CONTIG;
2297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /*
2299 * Switch out based on the FILLING and CONTIG state bits.
2300 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002301 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2302 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2303 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2304 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 /*
2306 * Setting all of a previous oldext extent to newext.
2307 * The left and right neighbors are both contiguous with new.
2308 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002309 --*idx;
2310
2311 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2312 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 LEFT.br_blockcount + PREV.br_blockcount +
2314 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002315 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002316
Christoph Hellwigec90c552011-05-23 08:52:53 +00002317 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002318 XFS_IFORK_NEXT_SET(ip, whichfork,
2319 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (cur == NULL)
2321 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2322 else {
2323 rval = XFS_ILOG_CORE;
2324 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2325 RIGHT.br_startblock,
2326 RIGHT.br_blockcount, &i)))
2327 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002328 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002329 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002331 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002332 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002334 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002335 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002337 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002338 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002340 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2342 LEFT.br_startblock,
2343 LEFT.br_blockcount + PREV.br_blockcount +
2344 RIGHT.br_blockcount, LEFT.br_state)))
2345 goto done;
2346 }
2347 break;
2348
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002349 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 /*
2351 * Setting all of a previous oldext extent to newext.
2352 * The left neighbor is contiguous, the right is not.
2353 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002354 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002355
Christoph Hellwigec90c552011-05-23 08:52:53 +00002356 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2357 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2358 LEFT.br_blockcount + PREV.br_blockcount);
2359 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2360
2361 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002362 XFS_IFORK_NEXT_SET(ip, whichfork,
2363 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 if (cur == NULL)
2365 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2366 else {
2367 rval = XFS_ILOG_CORE;
2368 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2369 PREV.br_startblock, PREV.br_blockcount,
2370 &i)))
2371 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002372 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002373 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002375 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002376 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002378 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2380 LEFT.br_startblock,
2381 LEFT.br_blockcount + PREV.br_blockcount,
2382 LEFT.br_state)))
2383 goto done;
2384 }
2385 break;
2386
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002387 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 /*
2389 * Setting all of a previous oldext extent to newext.
2390 * The right neighbor is contiguous, the left is not.
2391 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002392 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 xfs_bmbt_set_blockcount(ep,
2394 PREV.br_blockcount + RIGHT.br_blockcount);
2395 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002396 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2397 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002398 XFS_IFORK_NEXT_SET(ip, whichfork,
2399 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (cur == NULL)
2401 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2402 else {
2403 rval = XFS_ILOG_CORE;
2404 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2405 RIGHT.br_startblock,
2406 RIGHT.br_blockcount, &i)))
2407 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002408 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002409 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002411 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002412 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002414 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2416 new->br_startblock,
2417 new->br_blockcount + RIGHT.br_blockcount,
2418 newext)))
2419 goto done;
2420 }
2421 break;
2422
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002423 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 /*
2425 * Setting all of a previous oldext extent to newext.
2426 * Neither the left nor right neighbors are contiguous with
2427 * the new one.
2428 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002429 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002431 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002432
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if (cur == NULL)
2434 rval = XFS_ILOG_DEXT;
2435 else {
2436 rval = 0;
2437 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2438 new->br_startblock, new->br_blockcount,
2439 &i)))
2440 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002441 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2443 new->br_startblock, new->br_blockcount,
2444 newext)))
2445 goto done;
2446 }
2447 break;
2448
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002449 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 /*
2451 * Setting the first part of a previous oldext extent to newext.
2452 * The left neighbor is contiguous.
2453 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002454 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2455 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 LEFT.br_blockcount + new->br_blockcount);
2457 xfs_bmbt_set_startoff(ep,
2458 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002459 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002460
Christoph Hellwigec90c552011-05-23 08:52:53 +00002461 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 xfs_bmbt_set_startblock(ep,
2463 new->br_startblock + new->br_blockcount);
2464 xfs_bmbt_set_blockcount(ep,
2465 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002466 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002467
Christoph Hellwigec90c552011-05-23 08:52:53 +00002468 --*idx;
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if (cur == NULL)
2471 rval = XFS_ILOG_DEXT;
2472 else {
2473 rval = 0;
2474 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2475 PREV.br_startblock, PREV.br_blockcount,
2476 &i)))
2477 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002478 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if ((error = xfs_bmbt_update(cur,
2480 PREV.br_startoff + new->br_blockcount,
2481 PREV.br_startblock + new->br_blockcount,
2482 PREV.br_blockcount - new->br_blockcount,
2483 oldext)))
2484 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002485 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002487 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 LEFT.br_startblock,
2489 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002490 LEFT.br_state);
2491 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 goto done;
2493 }
2494 break;
2495
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002496 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 /*
2498 * Setting the first part of a previous oldext extent to newext.
2499 * The left neighbor is not contiguous.
2500 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002501 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2503 xfs_bmbt_set_startoff(ep, new_endoff);
2504 xfs_bmbt_set_blockcount(ep,
2505 PREV.br_blockcount - new->br_blockcount);
2506 xfs_bmbt_set_startblock(ep,
2507 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002508 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002509
Christoph Hellwigec90c552011-05-23 08:52:53 +00002510 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002511 XFS_IFORK_NEXT_SET(ip, whichfork,
2512 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if (cur == NULL)
2514 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2515 else {
2516 rval = XFS_ILOG_CORE;
2517 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2518 PREV.br_startblock, PREV.br_blockcount,
2519 &i)))
2520 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002521 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if ((error = xfs_bmbt_update(cur,
2523 PREV.br_startoff + new->br_blockcount,
2524 PREV.br_startblock + new->br_blockcount,
2525 PREV.br_blockcount - new->br_blockcount,
2526 oldext)))
2527 goto done;
2528 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002529 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002531 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
2533 break;
2534
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002535 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /*
2537 * Setting the last part of a previous oldext extent to newext.
2538 * The right neighbor is contiguous with the new allocation.
2539 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002540 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 xfs_bmbt_set_blockcount(ep,
2542 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002543 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2544
2545 ++*idx;
2546
2547 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2548 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002549 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002551 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (cur == NULL)
2554 rval = XFS_ILOG_DEXT;
2555 else {
2556 rval = 0;
2557 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2558 PREV.br_startblock,
2559 PREV.br_blockcount, &i)))
2560 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002561 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2563 PREV.br_startblock,
2564 PREV.br_blockcount - new->br_blockcount,
2565 oldext)))
2566 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002567 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 goto done;
2569 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2570 new->br_startblock,
2571 new->br_blockcount + RIGHT.br_blockcount,
2572 newext)))
2573 goto done;
2574 }
2575 break;
2576
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002577 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /*
2579 * Setting the last part of a previous oldext extent to newext.
2580 * The right neighbor is not contiguous.
2581 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002582 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 xfs_bmbt_set_blockcount(ep,
2584 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002585 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002586
Christoph Hellwigec90c552011-05-23 08:52:53 +00002587 ++*idx;
2588 xfs_iext_insert(ip, *idx, 1, new, state);
2589
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002590 XFS_IFORK_NEXT_SET(ip, whichfork,
2591 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 if (cur == NULL)
2593 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2594 else {
2595 rval = XFS_ILOG_CORE;
2596 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2597 PREV.br_startblock, PREV.br_blockcount,
2598 &i)))
2599 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002600 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2602 PREV.br_startblock,
2603 PREV.br_blockcount - new->br_blockcount,
2604 oldext)))
2605 goto done;
2606 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2607 new->br_startblock, new->br_blockcount,
2608 &i)))
2609 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002610 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002612 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002614 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
2616 break;
2617
2618 case 0:
2619 /*
2620 * Setting the middle part of a previous oldext extent to
2621 * newext. Contiguity is impossible here.
2622 * One extent becomes three extents.
2623 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002624 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 xfs_bmbt_set_blockcount(ep,
2626 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002627 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 r[0] = *new;
2630 r[1].br_startoff = new_endoff;
2631 r[1].br_blockcount =
2632 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2633 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2634 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002635
2636 ++*idx;
2637 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2638
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002639 XFS_IFORK_NEXT_SET(ip, whichfork,
2640 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (cur == NULL)
2642 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2643 else {
2644 rval = XFS_ILOG_CORE;
2645 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2646 PREV.br_startblock, PREV.br_blockcount,
2647 &i)))
2648 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002649 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 /* new right extent - oldext */
2651 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2652 r[1].br_startblock, r[1].br_blockcount,
2653 r[1].br_state)))
2654 goto done;
2655 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002657 cur->bc_rec.b.br_blockcount =
2658 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002659 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002661 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002662 /*
2663 * Reset the cursor to the position of the new extent
2664 * we are about to insert as we can't trust it after
2665 * the previous insert.
2666 */
2667 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2668 new->br_startblock, new->br_blockcount,
2669 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002671 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002673 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002674 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002676 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
2678 break;
2679
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002680 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2681 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2682 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2683 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2684 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2685 case BMAP_LEFT_CONTIG:
2686 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 /*
2688 * These cases are all impossible.
2689 */
2690 ASSERT(0);
2691 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002692
Darrick J. Wong9c194642016-08-03 12:16:05 +10002693 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002694 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002695 if (error)
2696 goto done;
2697
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002698 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002699 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002700 int tmp_logflags; /* partial log flag return val */
2701
2702 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002703 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002704 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002705 *logflagsp |= tmp_logflags;
2706 if (error)
2707 goto done;
2708 }
2709
2710 /* clear out the allocated field, done with it now in any case. */
2711 if (cur) {
2712 cur->bc_private.b.allocated = 0;
2713 *curp = cur;
2714 }
2715
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002716 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002718 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 return error;
2720#undef LEFT
2721#undef RIGHT
2722#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723}
2724
2725/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002726 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002728STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729xfs_bmap_add_extent_hole_delay(
2730 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002731 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002732 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002733 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002735 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2737 xfs_filblks_t newlen=0; /* new indirect size */
2738 xfs_filblks_t oldlen=0; /* old indirect size */
2739 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2740 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002741 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002743 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002745 if (whichfork == XFS_COW_FORK)
2746 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002747 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002748
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 /*
2750 * Check and set flags if this segment has a left neighbor
2751 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002752 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002753 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002754 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002755
2756 if (isnullstartblock(left.br_startblock))
2757 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 /*
2761 * Check and set flags if the current (right) segment exists.
2762 * If it doesn't exist, we're converting the hole at end-of-file.
2763 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002764 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002765 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002766 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002767
2768 if (isnullstartblock(right.br_startblock))
2769 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002771
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 /*
2773 * Set contiguity flags on the left and right neighbors.
2774 * Don't let extents get too large, even if the pieces are contiguous.
2775 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002776 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2777 left.br_startoff + left.br_blockcount == new->br_startoff &&
2778 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2779 state |= BMAP_LEFT_CONTIG;
2780
2781 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2782 new->br_startoff + new->br_blockcount == right.br_startoff &&
2783 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2784 (!(state & BMAP_LEFT_CONTIG) ||
2785 (left.br_blockcount + new->br_blockcount +
2786 right.br_blockcount <= MAXEXTLEN)))
2787 state |= BMAP_RIGHT_CONTIG;
2788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 /*
2790 * Switch out based on the contiguity flags.
2791 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002792 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2793 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 /*
2795 * New allocation is contiguous with delayed allocations
2796 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002797 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002799 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 temp = left.br_blockcount + new->br_blockcount +
2801 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002802
Christoph Hellwigec90c552011-05-23 08:52:53 +00002803 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2804 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002805 oldlen = startblockval(left.br_startblock) +
2806 startblockval(new->br_startblock) +
2807 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002808 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2809 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002810 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002811 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002812 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002813
Christoph Hellwigec90c552011-05-23 08:52:53 +00002814 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 break;
2816
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002817 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 /*
2819 * New allocation is contiguous with a delayed allocation
2820 * on the left.
2821 * Merge the new allocation with the left neighbor.
2822 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002823 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002825
2826 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2827 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002828 oldlen = startblockval(left.br_startblock) +
2829 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002830 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2831 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002832 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002833 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002834 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 break;
2836
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002837 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 /*
2839 * New allocation is contiguous with a delayed allocation
2840 * on the right.
2841 * Merge the new allocation with the right neighbor.
2842 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002843 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002845 oldlen = startblockval(new->br_startblock) +
2846 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002847 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2848 oldlen);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002849 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2850 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002851 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002852 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 break;
2854
2855 case 0:
2856 /*
2857 * New allocation is not contiguous with another
2858 * delayed allocation.
2859 * Insert a new entry.
2860 */
2861 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002862 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 break;
2864 }
2865 if (oldlen != newlen) {
2866 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002867 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2868 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 /*
2870 * Nothing to do for disk quota accounting here.
2871 */
2872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873}
2874
2875/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002876 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 */
2878STATIC int /* error */
2879xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002880 struct xfs_bmalloca *bma,
2881 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002883 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 int i; /* temp state */
2886 xfs_ifork_t *ifp; /* inode fork pointer */
2887 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2888 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002889 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002891 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Eric Sandeenf1f96c42016-01-04 16:10:42 +11002893 mp = bma->ip->i_mount;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002894 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002895
Christoph Hellwigc6534242011-09-18 20:41:05 +00002896 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002897 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002898 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002899 ASSERT(!bma->cur ||
2900 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002901
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002902 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002903
2904 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002905 if (whichfork == XFS_ATTR_FORK)
2906 state |= BMAP_ATTRFORK;
Christoph Hellwiga14234c2017-02-06 10:50:49 -08002907 if (whichfork == XFS_COW_FORK)
2908 state |= BMAP_COWFORK;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 /*
2911 * Check and set flags if this segment has a left neighbor.
2912 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002913 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002914 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002915 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002916 if (isnullstartblock(left.br_startblock))
2917 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 /*
2921 * Check and set flags if this segment has a current value.
2922 * Not true if we're inserting into the "hole" at eof.
2923 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002924 if (bma->idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002925 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002926 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002927 if (isnullstartblock(right.br_startblock))
2928 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 /*
2932 * We're inserting a real allocation between "left" and "right".
2933 * Set the contiguity flags. Don't let extents get too large.
2934 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002935 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2936 left.br_startoff + left.br_blockcount == new->br_startoff &&
2937 left.br_startblock + left.br_blockcount == new->br_startblock &&
2938 left.br_state == new->br_state &&
2939 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2940 state |= BMAP_LEFT_CONTIG;
2941
2942 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2943 new->br_startoff + new->br_blockcount == right.br_startoff &&
2944 new->br_startblock + new->br_blockcount == right.br_startblock &&
2945 new->br_state == right.br_state &&
2946 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2947 (!(state & BMAP_LEFT_CONTIG) ||
2948 left.br_blockcount + new->br_blockcount +
2949 right.br_blockcount <= MAXEXTLEN))
2950 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002952 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 /*
2954 * Select which case we're in here, and implement it.
2955 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002956 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2957 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 /*
2959 * New allocation is contiguous with real allocations on the
2960 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002961 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002963 --bma->idx;
2964 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2965 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 left.br_blockcount + new->br_blockcount +
2967 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002968 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002969
Christoph Hellwigc6534242011-09-18 20:41:05 +00002970 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002971
Christoph Hellwigc6534242011-09-18 20:41:05 +00002972 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
2973 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
2974 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002975 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002976 } else {
2977 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002978 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
2979 right.br_startblock, right.br_blockcount,
2980 &i);
2981 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002982 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002983 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002984 error = xfs_btree_delete(bma->cur, &i);
2985 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002986 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002987 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002988 error = xfs_btree_decrement(bma->cur, 0, &i);
2989 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002990 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002991 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002992 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002993 left.br_startblock,
2994 left.br_blockcount +
2995 new->br_blockcount +
2996 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002997 left.br_state);
2998 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002999 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003001 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003003 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 /*
3005 * New allocation is contiguous with a real allocation
3006 * on the left.
3007 * Merge the new allocation with the left neighbor.
3008 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003009 --bma->idx;
3010 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3011 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003013 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003014
Christoph Hellwigc6534242011-09-18 20:41:05 +00003015 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003016 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003017 } else {
3018 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003019 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3020 left.br_startblock, left.br_blockcount,
3021 &i);
3022 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003023 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003024 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003025 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003026 left.br_startblock,
3027 left.br_blockcount +
3028 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003029 left.br_state);
3030 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003031 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003033 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003035 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 /*
3037 * New allocation is contiguous with a real allocation
3038 * on the right.
3039 * Merge the new allocation with the right neighbor.
3040 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003041 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3042 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003043 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 new->br_blockcount + right.br_blockcount,
3045 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003046 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003047
Christoph Hellwigc6534242011-09-18 20:41:05 +00003048 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003049 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003050 } else {
3051 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003052 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003053 right.br_startoff,
3054 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003055 right.br_blockcount, &i);
3056 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003057 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003058 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003059 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003060 new->br_startblock,
3061 new->br_blockcount +
3062 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003063 right.br_state);
3064 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003065 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003067 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 case 0:
3070 /*
3071 * New allocation is not contiguous with another
3072 * real allocation.
3073 * Insert a new entry.
3074 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003075 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3076 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3077 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3078 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003079 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003080 } else {
3081 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003082 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003083 new->br_startoff,
3084 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003085 new->br_blockcount, &i);
3086 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003087 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003088 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003089 bma->cur->bc_rec.b.br_state = new->br_state;
3090 error = xfs_btree_insert(bma->cur, &i);
3091 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003092 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003093 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003095 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003097
Darrick J. Wong9c194642016-08-03 12:16:05 +10003098 /* add reverse mapping */
3099 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
3100 if (error)
3101 goto done;
3102
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003103 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003104 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003105 int tmp_logflags; /* partial log flag return val */
3106
Christoph Hellwigc6534242011-09-18 20:41:05 +00003107 ASSERT(bma->cur == NULL);
3108 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003109 bma->firstblock, bma->dfops, &bma->cur,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003110 0, &tmp_logflags, whichfork);
3111 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003112 if (error)
3113 goto done;
3114 }
3115
3116 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003117 if (bma->cur)
3118 bma->cur->bc_private.b.allocated = 0;
3119
3120 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003121done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003122 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003123 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
Nathan Scottdd9f4382006-01-11 15:28:28 +11003126/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003127 * Functions used in the extent read, allocate and remove paths
3128 */
3129
3130/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003131 * Adjust the size of the new extent based on di_extsize and rt extsize.
3132 */
Dave Chinner68988112013-08-12 20:49:42 +10003133int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003134xfs_bmap_extsize_align(
3135 xfs_mount_t *mp,
3136 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3137 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3138 xfs_extlen_t extsz, /* align to this extent size */
3139 int rt, /* is this a realtime inode? */
3140 int eof, /* is extent at end-of-file? */
3141 int delay, /* creating delalloc extent? */
3142 int convert, /* overwriting unwritten extent? */
3143 xfs_fileoff_t *offp, /* in/out: aligned offset */
3144 xfs_extlen_t *lenp) /* in/out: aligned length */
3145{
3146 xfs_fileoff_t orig_off; /* original offset */
3147 xfs_extlen_t orig_alen; /* original length */
3148 xfs_fileoff_t orig_end; /* original off+len */
3149 xfs_fileoff_t nexto; /* next file offset */
3150 xfs_fileoff_t prevo; /* previous file offset */
3151 xfs_fileoff_t align_off; /* temp for offset */
3152 xfs_extlen_t align_alen; /* temp for length */
3153 xfs_extlen_t temp; /* temp for calculations */
3154
3155 if (convert)
3156 return 0;
3157
3158 orig_off = align_off = *offp;
3159 orig_alen = align_alen = *lenp;
3160 orig_end = orig_off + orig_alen;
3161
3162 /*
3163 * If this request overlaps an existing extent, then don't
3164 * attempt to perform any additional alignment.
3165 */
3166 if (!delay && !eof &&
3167 (orig_off >= gotp->br_startoff) &&
3168 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3169 return 0;
3170 }
3171
3172 /*
3173 * If the file offset is unaligned vs. the extent size
3174 * we need to align it. This will be possible unless
3175 * the file was previously written with a kernel that didn't
3176 * perform this alignment, or if a truncate shot us in the
3177 * foot.
3178 */
3179 temp = do_mod(orig_off, extsz);
3180 if (temp) {
3181 align_alen += temp;
3182 align_off -= temp;
3183 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003184
3185 /* Same adjustment for the end of the requested area. */
3186 temp = (align_alen % extsz);
3187 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003188 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003189
3190 /*
3191 * For large extent hint sizes, the aligned extent might be larger than
3192 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3193 * the length back under MAXEXTLEN. The outer allocation loops handle
3194 * short allocation just fine, so it is safe to do this. We only want to
3195 * do it when we are forced to, though, because it means more allocation
3196 * operations are required.
3197 */
3198 while (align_alen > MAXEXTLEN)
3199 align_alen -= extsz;
3200 ASSERT(align_alen <= MAXEXTLEN);
3201
Nathan Scottdd9f4382006-01-11 15:28:28 +11003202 /*
3203 * If the previous block overlaps with this proposed allocation
3204 * then move the start forward without adjusting the length.
3205 */
3206 if (prevp->br_startoff != NULLFILEOFF) {
3207 if (prevp->br_startblock == HOLESTARTBLOCK)
3208 prevo = prevp->br_startoff;
3209 else
3210 prevo = prevp->br_startoff + prevp->br_blockcount;
3211 } else
3212 prevo = 0;
3213 if (align_off != orig_off && align_off < prevo)
3214 align_off = prevo;
3215 /*
3216 * If the next block overlaps with this proposed allocation
3217 * then move the start back without adjusting the length,
3218 * but not before offset 0.
3219 * This may of course make the start overlap previous block,
3220 * and if we hit the offset 0 limit then the next block
3221 * can still overlap too.
3222 */
3223 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3224 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3225 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3226 nexto = gotp->br_startoff + gotp->br_blockcount;
3227 else
3228 nexto = gotp->br_startoff;
3229 } else
3230 nexto = NULLFILEOFF;
3231 if (!eof &&
3232 align_off + align_alen != orig_end &&
3233 align_off + align_alen > nexto)
3234 align_off = nexto > align_alen ? nexto - align_alen : 0;
3235 /*
3236 * If we're now overlapping the next or previous extent that
3237 * means we can't fit an extsz piece in this hole. Just move
3238 * the start forward to the first valid spot and set
3239 * the length so we hit the end.
3240 */
3241 if (align_off != orig_off && align_off < prevo)
3242 align_off = prevo;
3243 if (align_off + align_alen != orig_end &&
3244 align_off + align_alen > nexto &&
3245 nexto != NULLFILEOFF) {
3246 ASSERT(nexto > prevo);
3247 align_alen = nexto - align_off;
3248 }
3249
3250 /*
3251 * If realtime, and the result isn't a multiple of the realtime
3252 * extent size we need to remove blocks until it is.
3253 */
3254 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3255 /*
3256 * We're not covering the original request, or
3257 * we won't be able to once we fix the length.
3258 */
3259 if (orig_off < align_off ||
3260 orig_end > align_off + align_alen ||
3261 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003262 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003263 /*
3264 * Try to fix it by moving the start up.
3265 */
3266 if (align_off + temp <= orig_off) {
3267 align_alen -= temp;
3268 align_off += temp;
3269 }
3270 /*
3271 * Try to fix it by moving the end in.
3272 */
3273 else if (align_off + align_alen - temp >= orig_end)
3274 align_alen -= temp;
3275 /*
3276 * Set the start to the minimum then trim the length.
3277 */
3278 else {
3279 align_alen -= orig_off - align_off;
3280 align_off = orig_off;
3281 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3282 }
3283 /*
3284 * Result doesn't cover the request, fail it.
3285 */
3286 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003287 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003288 } else {
3289 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003290 /* see MAXEXTLEN handling above */
3291 ASSERT(orig_end <= align_off + align_alen ||
3292 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003293 }
3294
3295#ifdef DEBUG
3296 if (!eof && gotp->br_startoff != NULLFILEOFF)
3297 ASSERT(align_off + align_alen <= gotp->br_startoff);
3298 if (prevp->br_startoff != NULLFILEOFF)
3299 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3300#endif
3301
3302 *lenp = align_alen;
3303 *offp = align_off;
3304 return 0;
3305}
3306
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307#define XFS_ALLOC_GAP_UNITS 4
3308
Dave Chinner68988112013-08-12 20:49:42 +10003309void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003310xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003311 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
3313 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3315 xfs_mount_t *mp; /* mount point structure */
3316 int nullfb; /* true if ap->firstblock isn't set */
3317 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
3319#define ISVALID(x,y) \
3320 (rt ? \
3321 (x) < mp->m_sb.sb_rblocks : \
3322 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3323 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3324 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3325
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003327 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003328 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3329 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003330 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 /*
3332 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003333 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003335 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3336 !isnullstartblock(ap->prev.br_startblock) &&
3337 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3338 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003339 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 /*
3341 * Adjust for the gap between prevp and us.
3342 */
Dave Chinner3a756672011-09-18 20:40:58 +00003343 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003344 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003346 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3347 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 }
3349 /*
3350 * If not at eof, then compare the two neighbor blocks.
3351 * Figure out whether either one gives us a good starting point,
3352 * and pick the better one.
3353 */
3354 else if (!ap->eof) {
3355 xfs_fsblock_t gotbno; /* right side block number */
3356 xfs_fsblock_t gotdiff=0; /* right side difference */
3357 xfs_fsblock_t prevbno; /* left side block number */
3358 xfs_fsblock_t prevdiff=0; /* left side difference */
3359
3360 /*
3361 * If there's a previous (left) block, select a requested
3362 * start block based on it.
3363 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003364 if (ap->prev.br_startoff != NULLFILEOFF &&
3365 !isnullstartblock(ap->prev.br_startblock) &&
3366 (prevbno = ap->prev.br_startblock +
3367 ap->prev.br_blockcount) &&
3368 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 /*
3370 * Calculate gap to end of previous block.
3371 */
Dave Chinner3a756672011-09-18 20:40:58 +00003372 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003373 (ap->prev.br_startoff +
3374 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 /*
3376 * Figure the startblock based on the previous block's
3377 * end and the gap size.
3378 * Heuristic!
3379 * If the gap is large relative to the piece we're
3380 * allocating, or using it gives us an invalid block
3381 * number, then just use the end of the previous block.
3382 */
Dave Chinner3a756672011-09-18 20:40:58 +00003383 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003385 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 prevbno += adjust;
3387 else
3388 prevdiff += adjust;
3389 /*
3390 * If the firstblock forbids it, can't use it,
3391 * must use default.
3392 */
3393 if (!rt && !nullfb &&
3394 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3395 prevbno = NULLFSBLOCK;
3396 }
3397 /*
3398 * No previous block or can't follow it, just default.
3399 */
3400 else
3401 prevbno = NULLFSBLOCK;
3402 /*
3403 * If there's a following (right) block, select a requested
3404 * start block based on it.
3405 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003406 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 /*
3408 * Calculate gap to start of next block.
3409 */
Dave Chinner3a756672011-09-18 20:40:58 +00003410 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 /*
3412 * Figure the startblock based on the next block's
3413 * start and the gap size.
3414 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003415 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 /*
3417 * Heuristic!
3418 * If the gap is large relative to the piece we're
3419 * allocating, or using it gives us an invalid block
3420 * number, then just use the start of the next block
3421 * offset by our length.
3422 */
Dave Chinner3a756672011-09-18 20:40:58 +00003423 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 ISVALID(gotbno - gotdiff, gotbno))
3425 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003426 else if (ISVALID(gotbno - ap->length, gotbno)) {
3427 gotbno -= ap->length;
3428 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 } else
3430 gotdiff += adjust;
3431 /*
3432 * If the firstblock forbids it, can't use it,
3433 * must use default.
3434 */
3435 if (!rt && !nullfb &&
3436 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3437 gotbno = NULLFSBLOCK;
3438 }
3439 /*
3440 * No next block, just default.
3441 */
3442 else
3443 gotbno = NULLFSBLOCK;
3444 /*
3445 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003446 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 */
3448 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003449 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003451 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003453 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003455#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003456}
3457
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003458static int
3459xfs_bmap_longest_free_extent(
3460 struct xfs_trans *tp,
3461 xfs_agnumber_t ag,
3462 xfs_extlen_t *blen,
3463 int *notinit)
3464{
3465 struct xfs_mount *mp = tp->t_mountp;
3466 struct xfs_perag *pag;
3467 xfs_extlen_t longest;
3468 int error = 0;
3469
3470 pag = xfs_perag_get(mp, ag);
3471 if (!pag->pagf_init) {
3472 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3473 if (error)
3474 goto out;
3475
3476 if (!pag->pagf_init) {
3477 *notinit = 1;
3478 goto out;
3479 }
3480 }
3481
Dave Chinner50adbcb2015-06-22 10:04:31 +10003482 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003483 xfs_alloc_min_freelist(mp, pag),
3484 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003485 if (*blen < longest)
3486 *blen = longest;
3487
3488out:
3489 xfs_perag_put(pag);
3490 return error;
3491}
3492
3493static void
3494xfs_bmap_select_minlen(
3495 struct xfs_bmalloca *ap,
3496 struct xfs_alloc_arg *args,
3497 xfs_extlen_t *blen,
3498 int notinit)
3499{
3500 if (notinit || *blen < ap->minlen) {
3501 /*
3502 * Since we did a BUF_TRYLOCK above, it is possible that
3503 * there is space for this request.
3504 */
3505 args->minlen = ap->minlen;
3506 } else if (*blen < args->maxlen) {
3507 /*
3508 * If the best seen length is less than the request length,
3509 * use the best as the minimum.
3510 */
3511 args->minlen = *blen;
3512 } else {
3513 /*
3514 * Otherwise we've seen an extent as big as maxlen, use that
3515 * as the minimum.
3516 */
3517 args->minlen = args->maxlen;
3518 }
3519}
3520
Nathan Scotta365bdd2006-03-14 13:34:16 +11003521STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003522xfs_bmap_btalloc_nullfb(
3523 struct xfs_bmalloca *ap,
3524 struct xfs_alloc_arg *args,
3525 xfs_extlen_t *blen)
3526{
3527 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003528 xfs_agnumber_t ag, startag;
3529 int notinit = 0;
3530 int error;
3531
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003532 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003533 args->total = ap->total;
3534
Christoph Hellwigc467c042010-02-15 23:34:42 +00003535 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3536 if (startag == NULLAGNUMBER)
3537 startag = ag = 0;
3538
Dave Chinner14b064c2011-01-27 12:16:28 +11003539 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003540 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3541 &notinit);
3542 if (error)
3543 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003544
Christoph Hellwigc467c042010-02-15 23:34:42 +00003545 if (++ag == mp->m_sb.sb_agcount)
3546 ag = 0;
3547 if (ag == startag)
3548 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003549 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003550
3551 xfs_bmap_select_minlen(ap, args, blen, notinit);
3552 return 0;
3553}
3554
3555STATIC int
3556xfs_bmap_btalloc_filestreams(
3557 struct xfs_bmalloca *ap,
3558 struct xfs_alloc_arg *args,
3559 xfs_extlen_t *blen)
3560{
3561 struct xfs_mount *mp = ap->ip->i_mount;
3562 xfs_agnumber_t ag;
3563 int notinit = 0;
3564 int error;
3565
3566 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3567 args->total = ap->total;
3568
3569 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3570 if (ag == NULLAGNUMBER)
3571 ag = 0;
3572
3573 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3574 if (error)
3575 return error;
3576
3577 if (*blen < args->maxlen) {
3578 error = xfs_filestream_new_ag(ap, &ag);
3579 if (error)
3580 return error;
3581
3582 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3583 &notinit);
3584 if (error)
3585 return error;
3586
3587 }
3588
3589 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003590
3591 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003592 * Set the failure fallback case to look in the selected AG as stream
3593 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003594 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003595 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003596 return 0;
3597}
3598
3599STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003600xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003601 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003602{
3603 xfs_mount_t *mp; /* mount point structure */
3604 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003605 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003606 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003607 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003608 xfs_alloc_arg_t args;
3609 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003610 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003611 int nullfb; /* true if ap->firstblock isn't set */
3612 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003613 int tryagain;
3614 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003615 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003616
Dave Chinnera99ebf42011-12-01 11:24:20 +00003617 ASSERT(ap->length);
3618
Nathan Scotta365bdd2006-03-14 13:34:16 +11003619 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003620
3621 /* stripe alignment for allocation is determined by mount parameters */
3622 stripe_align = 0;
3623 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3624 stripe_align = mp->m_swidth;
3625 else if (mp->m_dalign)
3626 stripe_align = mp->m_dalign;
3627
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003628 if (ap->flags & XFS_BMAPI_COWFORK)
3629 align = xfs_get_cowextsz_hint(ap->ip);
3630 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003631 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003632 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003633 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003634 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003635 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003636 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003637 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003638 }
Dave Chinner33177f052013-12-12 16:34:36 +11003639
3640
Dave Chinner0937e0f2011-09-18 20:40:57 +00003641 nullfb = *ap->firstblock == NULLFSBLOCK;
3642 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003643 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003644 if (xfs_alloc_is_userdata(ap->datatype) &&
3645 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003646 ag = xfs_filestream_lookup_ag(ap->ip);
3647 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003648 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003649 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003650 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003651 }
3652 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003653 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003654
3655 xfs_bmap_adjacent(ap);
3656
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003658 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 * it's in the right allocation group.
3660 */
Dave Chinner3a756672011-09-18 20:40:58 +00003661 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 ;
3663 else
Dave Chinner3a756672011-09-18 20:40:58 +00003664 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 * Normal allocation, done through xfs_alloc_vextent.
3667 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003668 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003669 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003670 args.tp = ap->tp;
3671 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003672 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003673 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003674
3675 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003676 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003677 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003678 blen = 0;
3679 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003680 /*
3681 * Search for an allocation group with a single extent large
3682 * enough for the request. If one isn't found, then adjust
3683 * the minimum allocation size to the largest space found.
3684 */
Dave Chinner292378e2016-09-26 08:21:28 +10003685 if (xfs_alloc_is_userdata(ap->datatype) &&
3686 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003687 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3688 else
3689 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003690 if (error)
3691 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003692 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003693 if (xfs_inode_is_filestream(ap->ip))
3694 args.type = XFS_ALLOCTYPE_FIRST_AG;
3695 else
3696 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003697 args.total = args.minlen = ap->minlen;
3698 } else {
3699 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3700 args.total = ap->total;
3701 args.minlen = ap->minlen;
3702 }
David Chinner957d0eb2007-06-18 16:50:37 +10003703 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003704 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003705 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003706 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003707 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003708 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003709 args.prod = 1;
3710 args.mod = 0;
3711 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003712 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003713 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003714 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3715 }
3716 /*
3717 * If we are not low on available data blocks, and the
3718 * underlying logical volume manager is a stripe, and
3719 * the file offset is zero then try to allocate data
3720 * blocks on stripe unit boundary.
3721 * NOTE: ap->aeof is only set if the allocation length
3722 * is >= the stripe unit and the allocation offset is
3723 * at the end of file.
3724 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003725 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003726 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003727 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003728 atype = args.type;
3729 isaligned = 1;
3730 /*
3731 * Adjust for alignment
3732 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003733 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003734 args.minlen = blen - args.alignment;
3735 args.minalignslop = 0;
3736 } else {
3737 /*
3738 * First try an exact bno allocation.
3739 * If it fails then do a near or start bno
3740 * allocation with alignment turned on.
3741 */
3742 atype = args.type;
3743 tryagain = 1;
3744 args.type = XFS_ALLOCTYPE_THIS_BNO;
3745 args.alignment = 1;
3746 /*
3747 * Compute the minlen+alignment for the
3748 * next case. Set slop so that the value
3749 * of minlen+alignment+slop doesn't go up
3750 * between the calls.
3751 */
Dave Chinner33177f052013-12-12 16:34:36 +11003752 if (blen > stripe_align && blen <= args.maxlen)
3753 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003754 else
3755 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003756 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003757 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003758 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003759 args.minlen - 1;
3760 else
3761 args.minalignslop = 0;
3762 }
3763 } else {
3764 args.alignment = 1;
3765 args.minalignslop = 0;
3766 }
3767 args.minleft = ap->minleft;
3768 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003769 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003770 args.datatype = ap->datatype;
3771 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003772 args.ip = ap->ip;
3773
3774 error = xfs_alloc_vextent(&args);
3775 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003776 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003777
Nathan Scotta365bdd2006-03-14 13:34:16 +11003778 if (tryagain && args.fsbno == NULLFSBLOCK) {
3779 /*
3780 * Exact allocation failed. Now try with alignment
3781 * turned on.
3782 */
3783 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003784 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003785 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003786 args.minlen = nextminlen;
3787 args.minalignslop = 0;
3788 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 if ((error = xfs_alloc_vextent(&args)))
3790 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003791 }
3792 if (isaligned && args.fsbno == NULLFSBLOCK) {
3793 /*
3794 * allocation failed, so turn off alignment and
3795 * try again.
3796 */
3797 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003798 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003799 args.alignment = 0;
3800 if ((error = xfs_alloc_vextent(&args)))
3801 return error;
3802 }
3803 if (args.fsbno == NULLFSBLOCK && nullfb &&
3804 args.minlen > ap->minlen) {
3805 args.minlen = ap->minlen;
3806 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003807 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003808 if ((error = xfs_alloc_vextent(&args)))
3809 return error;
3810 }
3811 if (args.fsbno == NULLFSBLOCK && nullfb) {
3812 args.fsbno = 0;
3813 args.type = XFS_ALLOCTYPE_FIRST_AG;
3814 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003815 if ((error = xfs_alloc_vextent(&args)))
3816 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003817 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003818 }
3819 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003820 /*
3821 * check the allocation happened at the same or higher AG than
3822 * the first block that was allocated.
3823 */
3824 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3825 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3826 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003827 (ap->dfops->dop_low &&
Dave Chinner0937e0f2011-09-18 20:40:57 +00003828 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3829 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3830
Dave Chinner3a756672011-09-18 20:40:58 +00003831 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003832 if (*ap->firstblock == NULLFSBLOCK)
3833 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003834 ASSERT(nullfb || fb_agno == args.agno ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003835 (ap->dfops->dop_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003836 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003837 if (!(ap->flags & XFS_BMAPI_COWFORK))
3838 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003839 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3840 if (ap->wasdel)
3841 ap->ip->i_delayed_blks -= args.len;
3842 /*
3843 * Adjust the disk quota also. This was reserved
3844 * earlier.
3845 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003846 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003847 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3848 XFS_TRANS_DQ_BCOUNT,
3849 (long) args.len);
3850 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003851 ap->blkno = NULLFSBLOCK;
3852 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 }
3854 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003855}
3856
3857/*
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003858 * For a remap operation, just "allocate" an extent at the address that the
3859 * caller passed in, and ensure that the AGFL is the right size. The caller
3860 * will then map the "allocated" extent into the file somewhere.
3861 */
3862STATIC int
3863xfs_bmap_remap_alloc(
3864 struct xfs_bmalloca *ap)
3865{
3866 struct xfs_trans *tp = ap->tp;
3867 struct xfs_mount *mp = tp->t_mountp;
3868 xfs_agblock_t bno;
3869 struct xfs_alloc_arg args;
3870 int error;
3871
3872 /*
3873 * validate that the block number is legal - the enables us to detect
3874 * and handle a silent filesystem corruption rather than crashing.
3875 */
3876 memset(&args, 0, sizeof(struct xfs_alloc_arg));
3877 args.tp = ap->tp;
3878 args.mp = ap->tp->t_mountp;
3879 bno = *ap->firstblock;
3880 args.agno = XFS_FSB_TO_AGNO(mp, bno);
3881 args.agbno = XFS_FSB_TO_AGBNO(mp, bno);
3882 if (args.agno >= mp->m_sb.sb_agcount ||
3883 args.agbno >= mp->m_sb.sb_agblocks)
3884 return -EFSCORRUPTED;
3885
3886 /* "Allocate" the extent from the range we passed in. */
3887 trace_xfs_bmap_remap_alloc(ap->ip, *ap->firstblock, ap->length);
3888 ap->blkno = bno;
3889 ap->ip->i_d.di_nblocks += ap->length;
3890 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3891
3892 /* Fix the freelist, like a real allocator does. */
3893 args.datatype = ap->datatype;
3894 args.pag = xfs_perag_get(args.mp, args.agno);
3895 ASSERT(args.pag);
3896
3897 /*
3898 * The freelist fixing code will decline the allocation if
3899 * the size and shape of the free space doesn't allow for
3900 * allocating the extent and updating all the metadata that
3901 * happens during an allocation. We're remapping, not
3902 * allocating, so skip that check by pretending to be freeing.
3903 */
3904 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003905 xfs_perag_put(args.pag);
3906 if (error)
3907 trace_xfs_bmap_remap_alloc_error(ap->ip, error, _RET_IP_);
3908 return error;
3909}
3910
3911/*
Nathan Scotta365bdd2006-03-14 13:34:16 +11003912 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3913 * It figures out where to ask the underlying allocator to put the new extent.
3914 */
3915STATIC int
3916xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003917 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003918{
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003919 if (ap->flags & XFS_BMAPI_REMAP)
3920 return xfs_bmap_remap_alloc(ap);
Dave Chinner292378e2016-09-26 08:21:28 +10003921 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3922 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003923 return xfs_bmap_rtalloc(ap);
3924 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925}
3926
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003927/* Trim extent to fit a logical block range. */
3928void
3929xfs_trim_extent(
3930 struct xfs_bmbt_irec *irec,
3931 xfs_fileoff_t bno,
3932 xfs_filblks_t len)
3933{
3934 xfs_fileoff_t distance;
3935 xfs_fileoff_t end = bno + len;
3936
3937 if (irec->br_startoff + irec->br_blockcount <= bno ||
3938 irec->br_startoff >= end) {
3939 irec->br_blockcount = 0;
3940 return;
3941 }
3942
3943 if (irec->br_startoff < bno) {
3944 distance = bno - irec->br_startoff;
3945 if (isnullstartblock(irec->br_startblock))
3946 irec->br_startblock = DELAYSTARTBLOCK;
3947 if (irec->br_startblock != DELAYSTARTBLOCK &&
3948 irec->br_startblock != HOLESTARTBLOCK)
3949 irec->br_startblock += distance;
3950 irec->br_startoff += distance;
3951 irec->br_blockcount -= distance;
3952 }
3953
3954 if (end < irec->br_startoff + irec->br_blockcount) {
3955 distance = irec->br_startoff + irec->br_blockcount - end;
3956 irec->br_blockcount -= distance;
3957 }
3958}
3959
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003961 * Trim the returned map to the required bounds
3962 */
3963STATIC void
3964xfs_bmapi_trim_map(
3965 struct xfs_bmbt_irec *mval,
3966 struct xfs_bmbt_irec *got,
3967 xfs_fileoff_t *bno,
3968 xfs_filblks_t len,
3969 xfs_fileoff_t obno,
3970 xfs_fileoff_t end,
3971 int n,
3972 int flags)
3973{
3974 if ((flags & XFS_BMAPI_ENTIRE) ||
3975 got->br_startoff + got->br_blockcount <= obno) {
3976 *mval = *got;
3977 if (isnullstartblock(got->br_startblock))
3978 mval->br_startblock = DELAYSTARTBLOCK;
3979 return;
3980 }
3981
3982 if (obno > *bno)
3983 *bno = obno;
3984 ASSERT((*bno >= obno) || (n == 0));
3985 ASSERT(*bno < end);
3986 mval->br_startoff = *bno;
3987 if (isnullstartblock(got->br_startblock))
3988 mval->br_startblock = DELAYSTARTBLOCK;
3989 else
3990 mval->br_startblock = got->br_startblock +
3991 (*bno - got->br_startoff);
3992 /*
3993 * Return the minimum of what we got and what we asked for for
3994 * the length. We can use the len variable here because it is
3995 * modified below and we could have been there before coming
3996 * here if the first part of the allocation didn't overlap what
3997 * was asked for.
3998 */
3999 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4000 got->br_blockcount - (*bno - got->br_startoff));
4001 mval->br_state = got->br_state;
4002 ASSERT(mval->br_blockcount <= len);
4003 return;
4004}
4005
4006/*
4007 * Update and validate the extent map to return
4008 */
4009STATIC void
4010xfs_bmapi_update_map(
4011 struct xfs_bmbt_irec **map,
4012 xfs_fileoff_t *bno,
4013 xfs_filblks_t *len,
4014 xfs_fileoff_t obno,
4015 xfs_fileoff_t end,
4016 int *n,
4017 int flags)
4018{
4019 xfs_bmbt_irec_t *mval = *map;
4020
4021 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4022 ((mval->br_startoff + mval->br_blockcount) <= end));
4023 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4024 (mval->br_startoff < obno));
4025
4026 *bno = mval->br_startoff + mval->br_blockcount;
4027 *len = end - *bno;
4028 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4029 /* update previous map with new information */
4030 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4031 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4032 ASSERT(mval->br_state == mval[-1].br_state);
4033 mval[-1].br_blockcount = mval->br_blockcount;
4034 mval[-1].br_state = mval->br_state;
4035 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4036 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4037 mval[-1].br_startblock != HOLESTARTBLOCK &&
4038 mval->br_startblock == mval[-1].br_startblock +
4039 mval[-1].br_blockcount &&
4040 ((flags & XFS_BMAPI_IGSTATE) ||
4041 mval[-1].br_state == mval->br_state)) {
4042 ASSERT(mval->br_startoff ==
4043 mval[-1].br_startoff + mval[-1].br_blockcount);
4044 mval[-1].br_blockcount += mval->br_blockcount;
4045 } else if (*n > 0 &&
4046 mval->br_startblock == DELAYSTARTBLOCK &&
4047 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4048 mval->br_startoff ==
4049 mval[-1].br_startoff + mval[-1].br_blockcount) {
4050 mval[-1].br_blockcount += mval->br_blockcount;
4051 mval[-1].br_state = mval->br_state;
4052 } else if (!((*n == 0) &&
4053 ((mval->br_startoff + mval->br_blockcount) <=
4054 obno))) {
4055 mval++;
4056 (*n)++;
4057 }
4058 *map = mval;
4059}
4060
4061/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004062 * Map file blocks to filesystem blocks without allocation.
4063 */
4064int
4065xfs_bmapi_read(
4066 struct xfs_inode *ip,
4067 xfs_fileoff_t bno,
4068 xfs_filblks_t len,
4069 struct xfs_bmbt_irec *mval,
4070 int *nmap,
4071 int flags)
4072{
4073 struct xfs_mount *mp = ip->i_mount;
4074 struct xfs_ifork *ifp;
4075 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004076 xfs_fileoff_t obno;
4077 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11004078 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004079 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11004080 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004081 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004082 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004083
4084 ASSERT(*nmap >= 1);
4085 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004086 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004087 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004088
4089 if (unlikely(XFS_TEST_ERROR(
4090 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4091 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4092 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4093 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004094 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004095 }
4096
4097 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004098 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004099
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004100 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004101
4102 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004103
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004104 /* No CoW fork? Return a hole. */
4105 if (whichfork == XFS_COW_FORK && !ifp) {
4106 mval->br_startoff = bno;
4107 mval->br_startblock = HOLESTARTBLOCK;
4108 mval->br_blockcount = len;
4109 mval->br_state = XFS_EXT_NORM;
4110 *nmap = 1;
4111 return 0;
4112 }
4113
Dave Chinner5c8ed202011-09-18 20:40:45 +00004114 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4115 error = xfs_iread_extents(NULL, ip, whichfork);
4116 if (error)
4117 return error;
4118 }
4119
Christoph Hellwig334f3422016-11-24 11:39:43 +11004120 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4121 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004122 end = bno + len;
4123 obno = bno;
4124
4125 while (bno < end && n < *nmap) {
4126 /* Reading past eof, act as though there's a hole up to end. */
4127 if (eof)
4128 got.br_startoff = end;
4129 if (got.br_startoff > bno) {
4130 /* Reading in a hole. */
4131 mval->br_startoff = bno;
4132 mval->br_startblock = HOLESTARTBLOCK;
4133 mval->br_blockcount =
4134 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4135 mval->br_state = XFS_EXT_NORM;
4136 bno += mval->br_blockcount;
4137 len -= mval->br_blockcount;
4138 mval++;
4139 n++;
4140 continue;
4141 }
4142
4143 /* set up the extent map to return. */
4144 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4145 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4146
4147 /* If we're done, stop now. */
4148 if (bno >= end || n >= *nmap)
4149 break;
4150
4151 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004152 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4153 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004154 }
4155 *nmap = n;
4156 return 0;
4157}
4158
Christoph Hellwig51446f52016-09-19 11:10:21 +10004159int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004160xfs_bmapi_reserve_delalloc(
4161 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004162 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004163 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004164 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004165 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004166 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004167 xfs_extnum_t *lastx,
4168 int eof)
4169{
4170 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004171 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004172 xfs_extlen_t alen;
4173 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004174 char rt = XFS_IS_REALTIME_INODE(ip);
4175 xfs_extlen_t extsz;
4176 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004177 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004178
Brian Foster974ae922016-11-28 14:57:42 +11004179 /*
4180 * Cap the alloc length. Keep track of prealloc so we know whether to
4181 * tag the inode before we return.
4182 */
4183 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004184 if (!eof)
4185 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004186 if (prealloc && alen >= len)
4187 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004188
4189 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004190 if (whichfork == XFS_COW_FORK)
4191 extsz = xfs_get_cowextsz_hint(ip);
4192 else
4193 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004194 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004195 struct xfs_bmbt_irec prev;
4196
4197 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4198 prev.br_startoff = NULLFILEOFF;
4199
4200 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004201 1, 0, &aoff, &alen);
4202 ASSERT(!error);
4203 }
4204
4205 if (rt)
4206 extsz = alen / mp->m_sb.sb_rextsize;
4207
4208 /*
4209 * Make a transaction-less quota reservation for delayed allocation
4210 * blocks. This number gets adjusted later. We return if we haven't
4211 * allocated blocks already inside this loop.
4212 */
4213 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4214 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4215 if (error)
4216 return error;
4217
4218 /*
4219 * Split changing sb for alen and indlen since they could be coming
4220 * from different places.
4221 */
4222 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4223 ASSERT(indlen > 0);
4224
4225 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004226 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004227 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004228 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004229 }
4230
4231 if (error)
4232 goto out_unreserve_quota;
4233
Dave Chinner0d485ad2015-02-23 21:22:03 +11004234 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004235 if (error)
4236 goto out_unreserve_blocks;
4237
4238
4239 ip->i_delayed_blks += alen;
4240
4241 got->br_startoff = aoff;
4242 got->br_startblock = nullstartblock(indlen);
4243 got->br_blockcount = alen;
4244 got->br_state = XFS_EXT_NORM;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004245 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004246
4247 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004248 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4249 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004250 */
4251 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4252
Brian Foster974ae922016-11-28 14:57:42 +11004253 /*
4254 * Tag the inode if blocks were preallocated. Note that COW fork
4255 * preallocation can occur at the start or end of the extent, even when
4256 * prealloc == 0, so we must also check the aligned offset and length.
4257 */
4258 if (whichfork == XFS_DATA_FORK && prealloc)
4259 xfs_inode_set_eofblocks_tag(ip);
4260 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4261 xfs_inode_set_cowblocks_tag(ip);
4262
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004263 ASSERT(got->br_startoff <= aoff);
4264 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4265 ASSERT(isnullstartblock(got->br_startblock));
4266 ASSERT(got->br_state == XFS_EXT_NORM);
4267 return 0;
4268
4269out_unreserve_blocks:
4270 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004271 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004272 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004273 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004274out_unreserve_quota:
4275 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004276 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004277 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4278 return error;
4279}
4280
Dave Chinnercf11da92014-07-15 07:08:24 +10004281static int
4282xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004283 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004284{
4285 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004286 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004287 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004288 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004289 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004290
Dave Chinnera99ebf42011-12-01 11:24:20 +00004291 ASSERT(bma->length > 0);
4292
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004293 /*
4294 * For the wasdelay case, we could also just allocate the stuff asked
4295 * for in this bmap call but that wouldn't be as good.
4296 */
4297 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004298 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4299 bma->offset = bma->got.br_startoff;
Christoph Hellwig0e8d6302016-11-24 11:40:32 +11004300 if (bma->idx) {
Dave Chinnere0c3da52011-09-18 20:41:01 +00004301 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004302 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004303 }
4304 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004305 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004306 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004307 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004308 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004309 }
4310
4311 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004312 * Set the data type being allocated. For the data fork, the first data
4313 * in the file is treated differently to all other allocations. For the
4314 * attribute fork, we only need to ensure the allocated range is not on
4315 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004316 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004317 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004318 bma->datatype = XFS_ALLOC_NOBUSY;
4319 if (whichfork == XFS_DATA_FORK) {
4320 if (bma->offset == 0)
4321 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4322 else
4323 bma->datatype |= XFS_ALLOC_USERDATA;
4324 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004325 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004326 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004327 }
4328
Dave Chinnere04426b2012-10-05 11:06:59 +10004329 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004330
4331 /*
4332 * Only want to do the alignment at the eof if it is userdata and
4333 * allocation length is larger than a stripe unit.
4334 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004335 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004336 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004337 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004338 if (error)
4339 return error;
4340 }
4341
4342 error = xfs_bmap_alloc(bma);
4343 if (error)
4344 return error;
4345
Dave Chinner29c8d172011-09-18 20:41:00 +00004346 if (bma->cur)
4347 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004348 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004349 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004350 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4351 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4352 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004353 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004354 }
4355 /*
4356 * Bump the number of extents we've allocated
4357 * in this call.
4358 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004359 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004360
Dave Chinner29c8d172011-09-18 20:41:00 +00004361 if (bma->cur)
4362 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004363 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4364
Dave Chinner963c30c2011-09-18 20:40:59 +00004365 bma->got.br_startoff = bma->offset;
4366 bma->got.br_startblock = bma->blkno;
4367 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004368 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004369
4370 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004371 * In the data fork, a wasdelay extent has been initialized, so
4372 * shouldn't be flagged as unwritten.
4373 *
4374 * For the cow fork, however, we convert delalloc reservations
4375 * (extents allocated for speculative preallocation) to
4376 * allocated unwritten extents, and only convert the unwritten
4377 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004378 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004379 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4380 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004381 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004382 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004383
Christoph Hellwigc6534242011-09-18 20:41:05 +00004384 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004385 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004386 else
4387 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004388
Christoph Hellwigc315c902011-09-18 20:41:02 +00004389 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004390 if (error)
4391 return error;
4392
4393 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004394 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4395 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4396 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004397 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004398 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004399
Dave Chinner963c30c2011-09-18 20:40:59 +00004400 ASSERT(bma->got.br_startoff <= bma->offset);
4401 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4402 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004403 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4404 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004405 return 0;
4406}
4407
Dave Chinnerb447fe52011-09-18 20:40:51 +00004408STATIC int
4409xfs_bmapi_convert_unwritten(
4410 struct xfs_bmalloca *bma,
4411 struct xfs_bmbt_irec *mval,
4412 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004413 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004414{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004415 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004416 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004417 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004418 int error;
4419
Dave Chinnerb447fe52011-09-18 20:40:51 +00004420 /* check if we need to do unwritten->real conversion */
4421 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4422 (flags & XFS_BMAPI_PREALLOC))
4423 return 0;
4424
4425 /* check if we need to do real->unwritten conversion */
4426 if (mval->br_state == XFS_EXT_NORM &&
4427 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4428 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4429 return 0;
4430
4431 /*
4432 * Modify (by adding) the state flag, if writing.
4433 */
4434 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004435 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4436 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004437 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004438 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004439 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004440 }
4441 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4442 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4443
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004444 /*
4445 * Before insertion into the bmbt, zero the range being converted
4446 * if required.
4447 */
4448 if (flags & XFS_BMAPI_ZERO) {
4449 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4450 mval->br_blockcount);
4451 if (error)
4452 return error;
4453 }
4454
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004455 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4456 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004457 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004458 /*
4459 * Log the inode core unconditionally in the unwritten extent conversion
4460 * path because the conversion might not have done so (e.g., if the
4461 * extent count hasn't changed). We need to make sure the inode is dirty
4462 * in the transaction for the sake of fsync(), even if nothing has
4463 * changed, because fsync() will not force the log for this transaction
4464 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004465 *
4466 * Note: If we're only converting cow fork extents, there aren't
4467 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004468 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004469 if (whichfork != XFS_COW_FORK)
4470 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004471 if (error)
4472 return error;
4473
4474 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004475 * Update our extent pointer, given that
4476 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4477 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004478 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004479 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004480
4481 /*
4482 * We may have combined previously unwritten space with written space,
4483 * so generate another request.
4484 */
4485 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004486 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004487 return 0;
4488}
4489
Christoph Hellwig44032802011-09-18 20:40:48 +00004490/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004491 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4492 * extent state if necessary. Details behaviour is controlled by the flags
4493 * parameter. Only allocates blocks from a single allocation group, to avoid
4494 * locking problems.
4495 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 * The returned value in "firstblock" from the first call in a transaction
4497 * must be remembered and presented to subsequent calls in "firstblock".
4498 * An upper bound for the number of blocks to be allocated is supplied to
4499 * the first call in "total"; if no allocation group has that many free
4500 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4501 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004502int
4503xfs_bmapi_write(
4504 struct xfs_trans *tp, /* transaction pointer */
4505 struct xfs_inode *ip, /* incore inode */
4506 xfs_fileoff_t bno, /* starting file offs. mapped */
4507 xfs_filblks_t len, /* length to map in file */
4508 int flags, /* XFS_BMAPI_... */
4509 xfs_fsblock_t *firstblock, /* first allocated block
4510 controls a.g. for allocs */
4511 xfs_extlen_t total, /* total blocks needed */
4512 struct xfs_bmbt_irec *mval, /* output: map values */
4513 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004514 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004516 struct xfs_mount *mp = ip->i_mount;
4517 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004518 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004519 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004520 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004521 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004522 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004523 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004524 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004525
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004527 xfs_fileoff_t orig_bno; /* original block number value */
4528 int orig_flags; /* original flags arg value */
4529 xfs_filblks_t orig_len; /* original value of len arg */
4530 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4531 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
4533 orig_bno = bno;
4534 orig_len = len;
4535 orig_flags = flags;
4536 orig_mval = mval;
4537 orig_nmap = *nmap;
4538#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004539 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004540
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004542 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4543 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004544 ASSERT(tp != NULL ||
4545 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4546 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004547 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004548 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004549 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004550 ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK);
4551 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP));
4552 ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004553
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004554 /* zeroing is for currently only for data extents, not metadata */
4555 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4556 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4557 /*
4558 * we can allocate unwritten extents or pre-zero allocated blocks,
4559 * but it makes no sense to do both at once. This would result in
4560 * zeroing the unwritten extent twice, but it still being an
4561 * unwritten extent....
4562 */
4563 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4564 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 if (unlikely(XFS_TEST_ERROR(
4567 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004568 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004570 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004571 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004573
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004575 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004576
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004578
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004579 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004580
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004581 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004583 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004585 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004586 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004587 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004588 }
4589
4590 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4591 error = xfs_iread_extents(tp, ip, whichfork);
4592 if (error)
4593 goto error0;
4594 }
4595
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 n = 0;
4597 end = bno + len;
4598 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004599
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004600 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4601 eof = true;
4602 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4603 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004604 bma.tp = tp;
4605 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004606 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004607 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004608 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004609 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004612 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004613
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004614 /* in hole or beyoned EOF? */
4615 if (eof || bma.got.br_startoff > bno) {
4616 if (flags & XFS_BMAPI_DELALLOC) {
4617 /*
4618 * For the COW fork we can reasonably get a
4619 * request for converting an extent that races
4620 * with other threads already having converted
4621 * part of it, as there converting COW to
4622 * regular blocks is not protected using the
4623 * IOLOCK.
4624 */
4625 ASSERT(flags & XFS_BMAPI_COWFORK);
4626 if (!(flags & XFS_BMAPI_COWFORK)) {
4627 error = -EIO;
4628 goto error0;
4629 }
4630
4631 if (eof || bno >= end)
4632 break;
4633 } else {
4634 need_alloc = true;
4635 }
4636 } else {
4637 /*
4638 * Make sure we only reflink into a hole.
4639 */
4640 ASSERT(!(flags & XFS_BMAPI_REMAP));
4641 if (isnullstartblock(bma.got.br_startblock))
4642 wasdelay = true;
4643 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004644
4645 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 * First, deal with the hole before the allocated space
4647 * that we found, if any.
4648 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004649 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004650 bma.eof = eof;
4651 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4652 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004653 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004654 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004655
Dave Chinnera99ebf42011-12-01 11:24:20 +00004656 /*
4657 * There's a 32/64 bit type mismatch between the
4658 * allocation length request (which can be 64 bits in
4659 * length) and the bma length request, which is
4660 * xfs_extlen_t and therefore 32 bits. Hence we have to
4661 * check for 32-bit overflows and handle them here.
4662 */
4663 if (len > (xfs_filblks_t)MAXEXTLEN)
4664 bma.length = MAXEXTLEN;
4665 else
4666 bma.length = len;
4667
4668 ASSERT(len > 0);
4669 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004670 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 if (error)
4672 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004673 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004674 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004675
4676 /*
4677 * If this is a CoW allocation, record the data in
4678 * the refcount btree for orphan recovery.
4679 */
4680 if (whichfork == XFS_COW_FORK) {
4681 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4682 bma.blkno, bma.length);
4683 if (error)
4684 goto error0;
4685 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004687
Dave Chinneraef9a892011-09-18 20:40:44 +00004688 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004689 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4690 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
Dave Chinnerb447fe52011-09-18 20:40:51 +00004692 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004693 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004694 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004695 continue;
4696 if (error)
4697 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
Dave Chinneraef9a892011-09-18 20:40:44 +00004699 /* update the extent map to return */
4700 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4701
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 /*
4703 * If we're done, stop now. Stop when we've allocated
4704 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4705 * the transaction may get too big.
4706 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004707 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004709
4710 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004711 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004712 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4713 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004716
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 /*
4718 * Transform from btree to extents, give it cur.
4719 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004720 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004721 int tmp_logflags = 0;
4722
Dave Chinner29c8d172011-09-18 20:41:00 +00004723 ASSERT(bma.cur);
4724 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004726 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 if (error)
4728 goto error0;
4729 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004730
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004732 XFS_IFORK_NEXTENTS(ip, whichfork) >
4733 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735error0:
4736 /*
4737 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004738 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004740 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004742 bma.logflags &= ~xfs_ilog_fext(whichfork);
4743 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004745 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 /*
4747 * Log whatever the flags say, even if error. Otherwise we might miss
4748 * detecting a case where the data is changed, there's an error,
4749 * and it's not logged so we don't shutdown when we should.
4750 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004751 if (bma.logflags)
4752 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004753
Dave Chinner29c8d172011-09-18 20:41:00 +00004754 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 if (!error) {
4756 ASSERT(*firstblock == NULLFSBLOCK ||
4757 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4758 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004759 bma.cur->bc_private.b.firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004760 (dfops->dop_low &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 XFS_FSB_TO_AGNO(mp, *firstblock) <
4762 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004763 bma.cur->bc_private.b.firstblock)));
4764 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004766 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4768 }
4769 if (!error)
4770 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4771 orig_nmap, *nmap);
4772 return error;
4773}
4774
4775/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004776 * When a delalloc extent is split (e.g., due to a hole punch), the original
4777 * indlen reservation must be shared across the two new extents that are left
4778 * behind.
4779 *
4780 * Given the original reservation and the worst case indlen for the two new
4781 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004782 * reservation fairly across the two new extents. If necessary, steal available
4783 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4784 * ores == 1). The number of stolen blocks is returned. The availability and
4785 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004786 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004787static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004788xfs_bmap_split_indlen(
4789 xfs_filblks_t ores, /* original res. */
4790 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004791 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4792 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004793{
4794 xfs_filblks_t len1 = *indlen1;
4795 xfs_filblks_t len2 = *indlen2;
4796 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004797 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004798 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004799
4800 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004801 * Steal as many blocks as we can to try and satisfy the worst case
4802 * indlen for both new extents.
4803 */
Brian Foster75d65362017-02-13 22:48:30 -08004804 if (ores < nres && avail)
4805 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4806 ores += stolen;
4807
4808 /* nothing else to do if we've satisfied the new reservation */
4809 if (ores >= nres)
4810 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004811
4812 /*
Brian Foster75d65362017-02-13 22:48:30 -08004813 * We can't meet the total required reservation for the two extents.
4814 * Calculate the percent of the overall shortage between both extents
4815 * and apply this percentage to each of the requested indlen values.
4816 * This distributes the shortage fairly and reduces the chances that one
4817 * of the two extents is left with nothing when extents are repeatedly
4818 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004819 */
Brian Foster75d65362017-02-13 22:48:30 -08004820 resfactor = (ores * 100);
4821 do_div(resfactor, nres);
4822 len1 *= resfactor;
4823 do_div(len1, 100);
4824 len2 *= resfactor;
4825 do_div(len2, 100);
4826 ASSERT(len1 + len2 <= ores);
4827 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4828
4829 /*
4830 * Hand out the remainder to each extent. If one of the two reservations
4831 * is zero, we want to make sure that one gets a block first. The loop
4832 * below starts with len1, so hand len2 a block right off the bat if it
4833 * is zero.
4834 */
4835 ores -= (len1 + len2);
4836 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4837 if (ores && !len2 && *indlen2) {
4838 len2++;
4839 ores--;
4840 }
4841 while (ores) {
4842 if (len1 < *indlen1) {
4843 len1++;
4844 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004845 }
Brian Foster75d65362017-02-13 22:48:30 -08004846 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004847 break;
Brian Foster75d65362017-02-13 22:48:30 -08004848 if (len2 < *indlen2) {
4849 len2++;
4850 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004851 }
4852 }
4853
4854 *indlen1 = len1;
4855 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004856
4857 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004858}
4859
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004860int
4861xfs_bmap_del_extent_delay(
4862 struct xfs_inode *ip,
4863 int whichfork,
4864 xfs_extnum_t *idx,
4865 struct xfs_bmbt_irec *got,
4866 struct xfs_bmbt_irec *del)
4867{
4868 struct xfs_mount *mp = ip->i_mount;
4869 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4870 struct xfs_bmbt_irec new;
4871 int64_t da_old, da_new, da_diff = 0;
4872 xfs_fileoff_t del_endoff, got_endoff;
4873 xfs_filblks_t got_indlen, new_indlen, stolen;
4874 int error = 0, state = 0;
4875 bool isrt;
4876
4877 XFS_STATS_INC(mp, xs_del_exlist);
4878
4879 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4880 del_endoff = del->br_startoff + del->br_blockcount;
4881 got_endoff = got->br_startoff + got->br_blockcount;
4882 da_old = startblockval(got->br_startblock);
4883 da_new = 0;
4884
4885 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004886 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004887 ASSERT(del->br_blockcount > 0);
4888 ASSERT(got->br_startoff <= del->br_startoff);
4889 ASSERT(got_endoff >= del_endoff);
4890
4891 if (isrt) {
4892 int64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4893
4894 do_div(rtexts, mp->m_sb.sb_rextsize);
4895 xfs_mod_frextents(mp, rtexts);
4896 }
4897
4898 /*
4899 * Update the inode delalloc counter now and wait to update the
4900 * sb counters as we might have to borrow some blocks for the
4901 * indirect block accounting.
4902 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004903 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4904 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004905 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004906 if (error)
4907 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004908 ip->i_delayed_blks -= del->br_blockcount;
4909
4910 if (whichfork == XFS_COW_FORK)
4911 state |= BMAP_COWFORK;
4912
4913 if (got->br_startoff == del->br_startoff)
4914 state |= BMAP_LEFT_CONTIG;
4915 if (got_endoff == del_endoff)
4916 state |= BMAP_RIGHT_CONTIG;
4917
4918 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4919 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4920 /*
4921 * Matches the whole extent. Delete the entry.
4922 */
4923 xfs_iext_remove(ip, *idx, 1, state);
4924 --*idx;
4925 break;
4926 case BMAP_LEFT_CONTIG:
4927 /*
4928 * Deleting the first part of the extent.
4929 */
4930 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4931 got->br_startoff = del_endoff;
4932 got->br_blockcount -= del->br_blockcount;
4933 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4934 got->br_blockcount), da_old);
4935 got->br_startblock = nullstartblock((int)da_new);
4936 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4937 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4938 break;
4939 case BMAP_RIGHT_CONTIG:
4940 /*
4941 * Deleting the last part of the extent.
4942 */
4943 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4944 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4945 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4946 got->br_blockcount), da_old);
4947 got->br_startblock = nullstartblock((int)da_new);
4948 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4949 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4950 break;
4951 case 0:
4952 /*
4953 * Deleting the middle of the extent.
4954 *
4955 * Distribute the original indlen reservation across the two new
4956 * extents. Steal blocks from the deleted extent if necessary.
4957 * Stealing blocks simply fudges the fdblocks accounting below.
4958 * Warn if either of the new indlen reservations is zero as this
4959 * can lead to delalloc problems.
4960 */
4961 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4962
4963 got->br_blockcount = del->br_startoff - got->br_startoff;
4964 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4965
4966 new.br_blockcount = got_endoff - del_endoff;
4967 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4968
4969 WARN_ON_ONCE(!got_indlen || !new_indlen);
4970 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4971 del->br_blockcount);
4972
4973 got->br_startblock = nullstartblock((int)got_indlen);
4974 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4975 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4976
4977 new.br_startoff = del_endoff;
4978 new.br_state = got->br_state;
4979 new.br_startblock = nullstartblock((int)new_indlen);
4980
4981 ++*idx;
4982 xfs_iext_insert(ip, *idx, 1, &new, state);
4983
4984 da_new = got_indlen + new_indlen - stolen;
4985 del->br_blockcount -= stolen;
4986 break;
4987 }
4988
4989 ASSERT(da_old >= da_new);
4990 da_diff = da_old - da_new;
4991 if (!isrt)
4992 da_diff += del->br_blockcount;
4993 if (da_diff)
4994 xfs_mod_fdblocks(mp, da_diff, false);
4995 return error;
4996}
4997
4998void
4999xfs_bmap_del_extent_cow(
5000 struct xfs_inode *ip,
5001 xfs_extnum_t *idx,
5002 struct xfs_bmbt_irec *got,
5003 struct xfs_bmbt_irec *del)
5004{
5005 struct xfs_mount *mp = ip->i_mount;
5006 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
5007 struct xfs_bmbt_irec new;
5008 xfs_fileoff_t del_endoff, got_endoff;
5009 int state = BMAP_COWFORK;
5010
5011 XFS_STATS_INC(mp, xs_del_exlist);
5012
5013 del_endoff = del->br_startoff + del->br_blockcount;
5014 got_endoff = got->br_startoff + got->br_blockcount;
5015
5016 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11005017 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005018 ASSERT(del->br_blockcount > 0);
5019 ASSERT(got->br_startoff <= del->br_startoff);
5020 ASSERT(got_endoff >= del_endoff);
5021 ASSERT(!isnullstartblock(got->br_startblock));
5022
5023 if (got->br_startoff == del->br_startoff)
5024 state |= BMAP_LEFT_CONTIG;
5025 if (got_endoff == del_endoff)
5026 state |= BMAP_RIGHT_CONTIG;
5027
5028 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
5029 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
5030 /*
5031 * Matches the whole extent. Delete the entry.
5032 */
5033 xfs_iext_remove(ip, *idx, 1, state);
5034 --*idx;
5035 break;
5036 case BMAP_LEFT_CONTIG:
5037 /*
5038 * Deleting the first part of the extent.
5039 */
5040 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5041 got->br_startoff = del_endoff;
5042 got->br_blockcount -= del->br_blockcount;
5043 got->br_startblock = del->br_startblock + del->br_blockcount;
5044 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5045 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5046 break;
5047 case BMAP_RIGHT_CONTIG:
5048 /*
5049 * Deleting the last part of the extent.
5050 */
5051 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5052 got->br_blockcount -= del->br_blockcount;
5053 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5054 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5055 break;
5056 case 0:
5057 /*
5058 * Deleting the middle of the extent.
5059 */
5060 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5061 got->br_blockcount = del->br_startoff - got->br_startoff;
5062 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5063 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5064
5065 new.br_startoff = del_endoff;
5066 new.br_blockcount = got_endoff - del_endoff;
5067 new.br_state = got->br_state;
5068 new.br_startblock = del->br_startblock + del->br_blockcount;
5069
5070 ++*idx;
5071 xfs_iext_insert(ip, *idx, 1, &new, state);
5072 break;
5073 }
5074}
5075
Brian Fostera9bd24a2016-03-15 11:42:46 +11005076/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005077 * Called by xfs_bmapi to update file extent records and the btree
5078 * after removing space (or undoing a delayed allocation).
5079 */
5080STATIC int /* error */
5081xfs_bmap_del_extent(
5082 xfs_inode_t *ip, /* incore inode pointer */
5083 xfs_trans_t *tp, /* current transaction pointer */
5084 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005085 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005086 xfs_btree_cur_t *cur, /* if null, not a btree */
5087 xfs_bmbt_irec_t *del, /* data to remove from extents */
5088 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005089 int whichfork, /* data or attr fork */
5090 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005091{
5092 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5093 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5094 xfs_fsblock_t del_endblock=0; /* first block past del */
5095 xfs_fileoff_t del_endoff; /* first offset past del */
5096 int delay; /* current block is delayed allocated */
5097 int do_fx; /* free extent at end of routine */
5098 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5099 int error; /* error return value */
5100 int flags; /* inode logging flags */
5101 xfs_bmbt_irec_t got; /* current extent entry */
5102 xfs_fileoff_t got_endoff; /* first offset past got */
5103 int i; /* temp state */
5104 xfs_ifork_t *ifp; /* inode fork pointer */
5105 xfs_mount_t *mp; /* mount structure */
5106 xfs_filblks_t nblks; /* quota/sb block count */
5107 xfs_bmbt_irec_t new; /* new record to be inserted */
5108 /* REFERENCED */
5109 uint qfield; /* quota field to update */
5110 xfs_filblks_t temp; /* for indirect length calculations */
5111 xfs_filblks_t temp2; /* for indirect length calculations */
5112 int state = 0;
5113
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005114 mp = ip->i_mount;
5115 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005116
5117 if (whichfork == XFS_ATTR_FORK)
5118 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005119 else if (whichfork == XFS_COW_FORK)
5120 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005121
Dave Chinner9e5987a2013-02-25 12:31:26 +11005122 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005123 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005124 ASSERT(del->br_blockcount > 0);
5125 ep = xfs_iext_get_ext(ifp, *idx);
5126 xfs_bmbt_get_all(ep, &got);
5127 ASSERT(got.br_startoff <= del->br_startoff);
5128 del_endoff = del->br_startoff + del->br_blockcount;
5129 got_endoff = got.br_startoff + got.br_blockcount;
5130 ASSERT(got_endoff >= del_endoff);
5131 delay = isnullstartblock(got.br_startblock);
5132 ASSERT(isnullstartblock(del->br_startblock) == delay);
5133 flags = 0;
5134 qfield = 0;
5135 error = 0;
5136 /*
5137 * If deleting a real allocation, must free up the disk space.
5138 */
5139 if (!delay) {
5140 flags = XFS_ILOG_CORE;
5141 /*
5142 * Realtime allocation. Free it and record di_nblocks update.
5143 */
5144 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5145 xfs_fsblock_t bno;
5146 xfs_filblks_t len;
5147
5148 ASSERT(do_mod(del->br_blockcount,
5149 mp->m_sb.sb_rextsize) == 0);
5150 ASSERT(do_mod(del->br_startblock,
5151 mp->m_sb.sb_rextsize) == 0);
5152 bno = del->br_startblock;
5153 len = del->br_blockcount;
5154 do_div(bno, mp->m_sb.sb_rextsize);
5155 do_div(len, mp->m_sb.sb_rextsize);
5156 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5157 if (error)
5158 goto done;
5159 do_fx = 0;
5160 nblks = len * mp->m_sb.sb_rextsize;
5161 qfield = XFS_TRANS_DQ_RTBCOUNT;
5162 }
5163 /*
5164 * Ordinary allocation.
5165 */
5166 else {
5167 do_fx = 1;
5168 nblks = del->br_blockcount;
5169 qfield = XFS_TRANS_DQ_BCOUNT;
5170 }
5171 /*
5172 * Set up del_endblock and cur for later.
5173 */
5174 del_endblock = del->br_startblock + del->br_blockcount;
5175 if (cur) {
5176 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5177 got.br_startblock, got.br_blockcount,
5178 &i)))
5179 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005180 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005181 }
5182 da_old = da_new = 0;
5183 } else {
5184 da_old = startblockval(got.br_startblock);
5185 da_new = 0;
5186 nblks = 0;
5187 do_fx = 0;
5188 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005189
Dave Chinner9e5987a2013-02-25 12:31:26 +11005190 /*
5191 * Set flag value to use in switch statement.
5192 * Left-contig is 2, right-contig is 1.
5193 */
5194 switch (((got.br_startoff == del->br_startoff) << 1) |
5195 (got_endoff == del_endoff)) {
5196 case 3:
5197 /*
5198 * Matches the whole extent. Delete the entry.
5199 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005200 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005201 xfs_iext_remove(ip, *idx, 1,
5202 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5203 --*idx;
5204 if (delay)
5205 break;
5206
5207 XFS_IFORK_NEXT_SET(ip, whichfork,
5208 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5209 flags |= XFS_ILOG_CORE;
5210 if (!cur) {
5211 flags |= xfs_ilog_fext(whichfork);
5212 break;
5213 }
5214 if ((error = xfs_btree_delete(cur, &i)))
5215 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005216 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005217 break;
5218
5219 case 2:
5220 /*
5221 * Deleting the first part of the extent.
5222 */
5223 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5224 xfs_bmbt_set_startoff(ep, del_endoff);
5225 temp = got.br_blockcount - del->br_blockcount;
5226 xfs_bmbt_set_blockcount(ep, temp);
5227 if (delay) {
5228 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5229 da_old);
5230 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5231 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5232 da_new = temp;
5233 break;
5234 }
5235 xfs_bmbt_set_startblock(ep, del_endblock);
5236 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5237 if (!cur) {
5238 flags |= xfs_ilog_fext(whichfork);
5239 break;
5240 }
5241 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5242 got.br_blockcount - del->br_blockcount,
5243 got.br_state)))
5244 goto done;
5245 break;
5246
5247 case 1:
5248 /*
5249 * Deleting the last part of the extent.
5250 */
5251 temp = got.br_blockcount - del->br_blockcount;
5252 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5253 xfs_bmbt_set_blockcount(ep, temp);
5254 if (delay) {
5255 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5256 da_old);
5257 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5258 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5259 da_new = temp;
5260 break;
5261 }
5262 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5263 if (!cur) {
5264 flags |= xfs_ilog_fext(whichfork);
5265 break;
5266 }
5267 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5268 got.br_startblock,
5269 got.br_blockcount - del->br_blockcount,
5270 got.br_state)))
5271 goto done;
5272 break;
5273
5274 case 0:
5275 /*
5276 * Deleting the middle of the extent.
5277 */
5278 temp = del->br_startoff - got.br_startoff;
5279 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5280 xfs_bmbt_set_blockcount(ep, temp);
5281 new.br_startoff = del_endoff;
5282 temp2 = got_endoff - del_endoff;
5283 new.br_blockcount = temp2;
5284 new.br_state = got.br_state;
5285 if (!delay) {
5286 new.br_startblock = del_endblock;
5287 flags |= XFS_ILOG_CORE;
5288 if (cur) {
5289 if ((error = xfs_bmbt_update(cur,
5290 got.br_startoff,
5291 got.br_startblock, temp,
5292 got.br_state)))
5293 goto done;
5294 if ((error = xfs_btree_increment(cur, 0, &i)))
5295 goto done;
5296 cur->bc_rec.b = new;
5297 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10005298 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005299 goto done;
5300 /*
5301 * If get no-space back from btree insert,
5302 * it tried a split, and we have a zero
5303 * block reservation.
5304 * Fix up our state and return the error.
5305 */
Dave Chinner24513372014-06-25 14:58:08 +10005306 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11005307 /*
5308 * Reset the cursor, don't trust
5309 * it after any insert operation.
5310 */
5311 if ((error = xfs_bmbt_lookup_eq(cur,
5312 got.br_startoff,
5313 got.br_startblock,
5314 temp, &i)))
5315 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005316 XFS_WANT_CORRUPTED_GOTO(mp,
5317 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005318 /*
5319 * Update the btree record back
5320 * to the original value.
5321 */
5322 if ((error = xfs_bmbt_update(cur,
5323 got.br_startoff,
5324 got.br_startblock,
5325 got.br_blockcount,
5326 got.br_state)))
5327 goto done;
5328 /*
5329 * Reset the extent record back
5330 * to the original value.
5331 */
5332 xfs_bmbt_set_blockcount(ep,
5333 got.br_blockcount);
5334 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005335 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005336 goto done;
5337 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005338 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005339 } else
5340 flags |= xfs_ilog_fext(whichfork);
5341 XFS_IFORK_NEXT_SET(ip, whichfork,
5342 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5343 } else {
Brian Fosterd34999c92016-03-15 11:42:47 +11005344 xfs_filblks_t stolen;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005345 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005346
5347 /*
5348 * Distribute the original indlen reservation across the
Brian Fosterd34999c92016-03-15 11:42:47 +11005349 * two new extents. Steal blocks from the deleted extent
5350 * if necessary. Stealing blocks simply fudges the
5351 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005352 */
5353 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5354 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c92016-03-15 11:42:47 +11005355 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5356 del->br_blockcount);
5357 da_new = temp + temp2 - stolen;
5358 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005359
5360 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11005361 * Set the reservation for each extent. Warn if either
5362 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005363 */
Brian Fosterd34999c92016-03-15 11:42:47 +11005364 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005365 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005366 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005367 }
5368 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5369 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5370 ++*idx;
5371 break;
5372 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005373
5374 /* remove reverse mapping */
5375 if (!delay) {
5376 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5377 if (error)
5378 goto done;
5379 }
5380
Dave Chinner9e5987a2013-02-25 12:31:26 +11005381 /*
5382 * If we need to, add to list of extents to delete.
5383 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005384 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005385 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5386 error = xfs_refcount_decrease_extent(mp, dfops, del);
5387 if (error)
5388 goto done;
5389 } else
5390 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5391 del->br_blockcount, NULL);
5392 }
5393
Dave Chinner9e5987a2013-02-25 12:31:26 +11005394 /*
5395 * Adjust inode # blocks in the file.
5396 */
5397 if (nblks)
5398 ip->i_d.di_nblocks -= nblks;
5399 /*
5400 * Adjust quota data.
5401 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005402 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005403 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5404
5405 /*
5406 * Account for change in delayed indirect blocks.
5407 * Nothing to do for disk quota accounting here.
5408 */
5409 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005410 if (da_old > da_new)
5411 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005412done:
5413 *logflagsp = flags;
5414 return error;
5415}
5416
5417/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 * Unmap (remove) blocks from a file.
5419 * If nexts is nonzero then the number of extents to remove is limited to
5420 * that value. If not all extents in the block range can be removed then
5421 * *done is set.
5422 */
5423int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005424__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 xfs_trans_t *tp, /* transaction pointer */
5426 struct xfs_inode *ip, /* incore inode */
5427 xfs_fileoff_t bno, /* starting offset to unmap */
Darrick J. Wong44535932016-10-03 09:11:29 -07005428 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 int flags, /* misc flags */
5430 xfs_extnum_t nexts, /* number of extents max */
5431 xfs_fsblock_t *firstblock, /* first allocated block
5432 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005433 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434{
5435 xfs_btree_cur_t *cur; /* bmap btree cursor */
5436 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 int error; /* error return value */
5438 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005439 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 xfs_ifork_t *ifp; /* inode fork pointer */
5441 int isrt; /* freeing in rt area */
5442 xfs_extnum_t lastx; /* last extent index used */
5443 int logflags; /* transaction logging flags */
5444 xfs_extlen_t mod; /* rt extent offset */
5445 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 xfs_fileoff_t start; /* first file offset deleted */
5447 int tmp_logflags; /* partial logging flags */
5448 int wasdel; /* was a delayed alloc extent */
5449 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005451 xfs_filblks_t len = *rlen; /* length to unmap in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005453 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5454
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005455 whichfork = xfs_bmapi_whichfork(flags);
5456 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 ifp = XFS_IFORK_PTR(ip, whichfork);
5458 if (unlikely(
5459 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5460 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5461 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5462 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005463 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 }
5465 mp = ip->i_mount;
5466 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005467 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005468
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005469 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 ASSERT(len > 0);
5471 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005472
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5474 (error = xfs_iread_extents(tp, ip, whichfork)))
5475 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005476 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005477 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 return 0;
5479 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005480 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005481 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 start = bno;
5483 bno = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005484
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 /*
5486 * Check to see if the given block number is past the end of the
5487 * file, back up to the last block if so...
5488 */
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005489 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5490 ASSERT(lastx > 0);
5491 xfs_iext_get_extent(ifp, --lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 bno = got.br_startoff + got.br_blockcount - 1;
5493 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005494
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 logflags = 0;
5496 if (ifp->if_flags & XFS_IFBROOT) {
5497 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005498 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005500 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 cur->bc_private.b.flags = 0;
5502 } else
5503 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005504
5505 if (isrt) {
5506 /*
5507 * Synchronize by locking the bitmap inode.
5508 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005509 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005510 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005511 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5512 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005513 }
5514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 extno = 0;
5516 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5517 (nexts == 0 || extno < nexts)) {
5518 /*
5519 * Is the found extent after a hole in which bno lives?
5520 * Just back up to the previous extent, if so.
5521 */
5522 if (got.br_startoff > bno) {
5523 if (--lastx < 0)
5524 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005525 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 }
5527 /*
5528 * Is the last block of this extent before the range
5529 * we're supposed to delete? If so, we're done.
5530 */
5531 bno = XFS_FILEOFF_MIN(bno,
5532 got.br_startoff + got.br_blockcount - 1);
5533 if (bno < start)
5534 break;
5535 /*
5536 * Then deal with the (possibly delayed) allocated space
5537 * we found.
5538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005540 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 if (got.br_startoff < start) {
5542 del.br_startoff = start;
5543 del.br_blockcount -= start - got.br_startoff;
5544 if (!wasdel)
5545 del.br_startblock += start - got.br_startoff;
5546 }
5547 if (del.br_startoff + del.br_blockcount > bno + 1)
5548 del.br_blockcount = bno + 1 - del.br_startoff;
5549 sum = del.br_startblock + del.br_blockcount;
5550 if (isrt &&
5551 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5552 /*
5553 * Realtime extent not lined up at the end.
5554 * The extent could have been split into written
5555 * and unwritten pieces, or we could just be
5556 * unmapping part of it. But we can't really
5557 * get rid of part of a realtime extent.
5558 */
5559 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005560 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 /*
5562 * This piece is unwritten, or we're not
5563 * using unwritten extents. Skip over it.
5564 */
5565 ASSERT(bno >= mod);
5566 bno -= mod > del.br_blockcount ?
5567 del.br_blockcount : mod;
5568 if (bno < got.br_startoff) {
5569 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005570 xfs_bmbt_get_all(xfs_iext_get_ext(
5571 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 }
5573 continue;
5574 }
5575 /*
5576 * It's written, turn it unwritten.
5577 * This is better than zeroing it.
5578 */
5579 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005580 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 /*
5582 * If this spans a realtime extent boundary,
5583 * chop it back to the start of the one we end at.
5584 */
5585 if (del.br_blockcount > mod) {
5586 del.br_startoff += del.br_blockcount - mod;
5587 del.br_startblock += del.br_blockcount - mod;
5588 del.br_blockcount = mod;
5589 }
5590 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005591 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005592 whichfork, &lastx, &cur, &del,
5593 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 if (error)
5595 goto error0;
5596 goto nodelete;
5597 }
5598 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5599 /*
5600 * Realtime extent is lined up at the end but not
5601 * at the front. We'll get rid of full extents if
5602 * we can.
5603 */
5604 mod = mp->m_sb.sb_rextsize - mod;
5605 if (del.br_blockcount > mod) {
5606 del.br_blockcount -= mod;
5607 del.br_startoff += mod;
5608 del.br_startblock += mod;
5609 } else if ((del.br_startoff == start &&
5610 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005611 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005612 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 /*
5614 * Can't make it unwritten. There isn't
5615 * a full extent here so just skip it.
5616 */
5617 ASSERT(bno >= del.br_blockcount);
5618 bno -= del.br_blockcount;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005619 if (got.br_startoff > bno && --lastx >= 0)
5620 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 continue;
5622 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005623 struct xfs_bmbt_irec prev;
5624
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 /*
5626 * This one is already unwritten.
5627 * It must have a written left neighbor.
5628 * Unwrite the killed part of that one and
5629 * try again.
5630 */
5631 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005632 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005634 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 ASSERT(del.br_startblock ==
5636 prev.br_startblock + prev.br_blockcount);
5637 if (prev.br_startoff < start) {
5638 mod = start - prev.br_startoff;
5639 prev.br_blockcount -= mod;
5640 prev.br_startblock += mod;
5641 prev.br_startoff = start;
5642 }
5643 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005644 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005645 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005646 ip, whichfork, &lastx, &cur,
5647 &prev, firstblock, dfops,
5648 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 if (error)
5650 goto error0;
5651 goto nodelete;
5652 } else {
5653 ASSERT(del.br_state == XFS_EXT_NORM);
5654 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005655 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005656 ip, whichfork, &lastx, &cur,
5657 &del, firstblock, dfops,
5658 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 if (error)
5660 goto error0;
5661 goto nodelete;
5662 }
5663 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005664
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 /*
5666 * If it's the case where the directory code is running
5667 * with no block reservation, and the deleted block is in
5668 * the middle of its extent, and the resulting insert
5669 * of an extent would cause transformation to btree format,
5670 * then reject it. The calling code will then swap
5671 * blocks around instead.
5672 * We have to do this now, rather than waiting for the
5673 * conversion to btree format, since the transaction
5674 * will be dirty.
5675 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005676 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005678 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5679 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680 del.br_startoff > got.br_startoff &&
5681 del.br_startoff + del.br_blockcount <
5682 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005683 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 goto error0;
5685 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005686
5687 /*
5688 * Unreserve quota and update realtime free space, if
5689 * appropriate. If delayed allocation, update the inode delalloc
5690 * counter now and wait to update the sb counters as
5691 * xfs_bmap_del_extent() might need to borrow some blocks.
5692 */
5693 if (wasdel) {
5694 ASSERT(startblockval(del.br_startblock) > 0);
5695 if (isrt) {
5696 xfs_filblks_t rtexts;
5697
5698 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5699 do_div(rtexts, mp->m_sb.sb_rextsize);
5700 xfs_mod_frextents(mp, (int64_t)rtexts);
5701 (void)xfs_trans_reserve_quota_nblks(NULL,
5702 ip, -((long)del.br_blockcount), 0,
5703 XFS_QMOPT_RES_RTBLKS);
5704 } else {
5705 (void)xfs_trans_reserve_quota_nblks(NULL,
5706 ip, -((long)del.br_blockcount), 0,
5707 XFS_QMOPT_RES_REGBLKS);
5708 }
5709 ip->i_delayed_blks -= del.br_blockcount;
5710 if (cur)
5711 cur->bc_private.b.flags |=
5712 XFS_BTCUR_BPRV_WASDEL;
5713 } else if (cur)
5714 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5715
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005716 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005717 &tmp_logflags, whichfork, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 logflags |= tmp_logflags;
5719 if (error)
5720 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005721
5722 if (!isrt && wasdel)
5723 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5724
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 bno = del.br_startoff - 1;
5726nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 /*
5728 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005731 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005732 xfs_iext_get_extent(ifp, lastx, &got);
5733 if (got.br_startoff > bno && --lastx >= 0)
5734 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 extno++;
5737 }
5738 }
Darrick J. Wong44535932016-10-03 09:11:29 -07005739 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5740 *rlen = 0;
5741 else
5742 *rlen = bno - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005743
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 /*
5745 * Convert to a btree if necessary.
5746 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005747 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005749 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 &cur, 0, &tmp_logflags, whichfork);
5751 logflags |= tmp_logflags;
5752 if (error)
5753 goto error0;
5754 }
5755 /*
5756 * transform from btree to extents, give it cur
5757 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005758 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 ASSERT(cur != NULL);
5760 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5761 whichfork);
5762 logflags |= tmp_logflags;
5763 if (error)
5764 goto error0;
5765 }
5766 /*
5767 * transform from extents to local?
5768 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 error = 0;
5770error0:
5771 /*
5772 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005773 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005775 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005777 logflags &= ~xfs_ilog_fext(whichfork);
5778 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005780 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 /*
5782 * Log inode even in the error case, if the transaction
5783 * is dirty we'll need to shut down the filesystem.
5784 */
5785 if (logflags)
5786 xfs_trans_log_inode(tp, ip, logflags);
5787 if (cur) {
5788 if (!error) {
5789 *firstblock = cur->bc_private.b.firstblock;
5790 cur->bc_private.b.allocated = 0;
5791 }
5792 xfs_btree_del_cursor(cur,
5793 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5794 }
5795 return error;
5796}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005797
Darrick J. Wong44535932016-10-03 09:11:29 -07005798/* Unmap a range of a file. */
5799int
5800xfs_bunmapi(
5801 xfs_trans_t *tp,
5802 struct xfs_inode *ip,
5803 xfs_fileoff_t bno,
5804 xfs_filblks_t len,
5805 int flags,
5806 xfs_extnum_t nexts,
5807 xfs_fsblock_t *firstblock,
5808 struct xfs_defer_ops *dfops,
5809 int *done)
5810{
5811 int error;
5812
5813 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5814 dfops);
5815 *done = (len == 0);
5816 return error;
5817}
5818
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005819/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005820 * Determine whether an extent shift can be accomplished by a merge with the
5821 * extent that precedes the target hole of the shift.
5822 */
5823STATIC bool
5824xfs_bmse_can_merge(
5825 struct xfs_bmbt_irec *left, /* preceding extent */
5826 struct xfs_bmbt_irec *got, /* current extent to shift */
5827 xfs_fileoff_t shift) /* shift fsb */
5828{
5829 xfs_fileoff_t startoff;
5830
5831 startoff = got->br_startoff - shift;
5832
5833 /*
5834 * The extent, once shifted, must be adjacent in-file and on-disk with
5835 * the preceding extent.
5836 */
5837 if ((left->br_startoff + left->br_blockcount != startoff) ||
5838 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5839 (left->br_state != got->br_state) ||
5840 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5841 return false;
5842
5843 return true;
5844}
5845
5846/*
5847 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5848 * hole in the file. If an extent shift would result in the extent being fully
5849 * adjacent to the extent that currently precedes the hole, we can merge with
5850 * the preceding extent rather than do the shift.
5851 *
5852 * This function assumes the caller has verified a shift-by-merge is possible
5853 * with the provided extents via xfs_bmse_can_merge().
5854 */
5855STATIC int
5856xfs_bmse_merge(
5857 struct xfs_inode *ip,
5858 int whichfork,
5859 xfs_fileoff_t shift, /* shift fsb */
5860 int current_ext, /* idx of gotp */
5861 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5862 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5863 struct xfs_btree_cur *cur,
5864 int *logflags) /* output */
5865{
Brian Fosterddb19e32014-09-23 15:38:09 +10005866 struct xfs_bmbt_irec got;
5867 struct xfs_bmbt_irec left;
5868 xfs_filblks_t blockcount;
5869 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005870 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005871
Brian Fosterddb19e32014-09-23 15:38:09 +10005872 xfs_bmbt_get_all(gotp, &got);
5873 xfs_bmbt_get_all(leftp, &left);
5874 blockcount = left.br_blockcount + got.br_blockcount;
5875
5876 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5877 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5878 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5879
5880 /*
5881 * Merge the in-core extents. Note that the host record pointers and
5882 * current_ext index are invalid once the extent has been removed via
5883 * xfs_iext_remove().
5884 */
5885 xfs_bmbt_set_blockcount(leftp, blockcount);
5886 xfs_iext_remove(ip, current_ext, 1, 0);
5887
5888 /*
5889 * Update the on-disk extent count, the btree if necessary and log the
5890 * inode.
5891 */
5892 XFS_IFORK_NEXT_SET(ip, whichfork,
5893 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5894 *logflags |= XFS_ILOG_CORE;
5895 if (!cur) {
5896 *logflags |= XFS_ILOG_DEXT;
5897 return 0;
5898 }
5899
5900 /* lookup and remove the extent to merge */
5901 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5902 got.br_blockcount, &i);
5903 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005904 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005905 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005906
5907 error = xfs_btree_delete(cur, &i);
5908 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005909 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005910 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005911
5912 /* lookup and update size of the previous extent */
5913 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5914 left.br_blockcount, &i);
5915 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005916 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005917 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005918
5919 left.br_blockcount = blockcount;
5920
Dave Chinner4db431f2014-12-04 09:42:40 +11005921 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5922 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005923}
5924
5925/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005926 * Shift a single extent.
5927 */
5928STATIC int
5929xfs_bmse_shift_one(
5930 struct xfs_inode *ip,
5931 int whichfork,
5932 xfs_fileoff_t offset_shift_fsb,
5933 int *current_ext,
5934 struct xfs_bmbt_rec_host *gotp,
5935 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005936 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005937 enum shift_direction direction,
5938 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005939{
5940 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005941 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005942 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005943 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005944 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005945 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005946 int error;
5947 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005948 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005949
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005950 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005951 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005952 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005953
5954 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005955
Brian Fosterf71721d2014-09-23 15:39:05 +10005956 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005957 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005958
Namjae Jeona904b1c2015-03-25 15:08:56 +11005959 if (direction == SHIFT_LEFT) {
5960 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005961
Namjae Jeona904b1c2015-03-25 15:08:56 +11005962 /*
5963 * Check for merge if we've got an extent to the left,
5964 * otherwise make sure there's enough room at the start
5965 * of the file for the shift.
5966 */
5967 if (!*current_ext) {
5968 if (got.br_startoff < offset_shift_fsb)
5969 return -EINVAL;
5970 goto update_current_ext;
5971 }
5972 /*
5973 * grab the left extent and check for a large
5974 * enough hole.
5975 */
5976 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5977 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5978
5979 if (startoff <
5980 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005981 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005982
Dave Chinnerb11bd672014-12-04 09:42:24 +11005983 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005984 if (xfs_bmse_can_merge(&adj_irec, &got,
5985 offset_shift_fsb)) {
Darrick J. Wong9c194642016-08-03 12:16:05 +10005986 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5987 *current_ext, gotp, adj_irecp,
5988 cur, logflags);
5989 if (error)
5990 return error;
5991 adj_irec = got;
5992 goto update_rmap;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005993 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005994 } else {
5995 startoff = got.br_startoff + offset_shift_fsb;
5996 /* nothing to move if this is the last extent */
5997 if (*current_ext >= (total_extents - 1))
5998 goto update_current_ext;
5999 /*
6000 * If this is not the last extent in the file, make sure there
6001 * is enough room between current extent and next extent for
6002 * accommodating the shift.
6003 */
6004 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
6005 xfs_bmbt_get_all(adj_irecp, &adj_irec);
6006 if (startoff + got.br_blockcount > adj_irec.br_startoff)
6007 return -EINVAL;
6008 /*
6009 * Unlike a left shift (which involves a hole punch),
6010 * a right shift does not modify extent neighbors
6011 * in any way. We should never find mergeable extents
6012 * in this scenario. Check anyways and warn if we
6013 * encounter two extents that could be one.
6014 */
6015 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
6016 WARN_ON_ONCE(1);
6017 }
Brian Fostera979bdf2014-09-23 15:39:04 +10006018 /*
6019 * Increment the extent index for the next iteration, update the start
6020 * offset of the in-core extent and update the btree if applicable.
6021 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11006022update_current_ext:
6023 if (direction == SHIFT_LEFT)
6024 (*current_ext)++;
6025 else
6026 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10006027 xfs_bmbt_set_startoff(gotp, startoff);
6028 *logflags |= XFS_ILOG_CORE;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006029 adj_irec = got;
Brian Fostera979bdf2014-09-23 15:39:04 +10006030 if (!cur) {
6031 *logflags |= XFS_ILOG_DEXT;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006032 goto update_rmap;
Brian Fostera979bdf2014-09-23 15:39:04 +10006033 }
6034
6035 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
6036 got.br_blockcount, &i);
6037 if (error)
6038 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11006039 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10006040
6041 got.br_startoff = startoff;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006042 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
6043 got.br_blockcount, got.br_state);
6044 if (error)
6045 return error;
6046
6047update_rmap:
6048 /* update reverse mapping */
6049 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
6050 if (error)
6051 return error;
6052 adj_irec.br_startoff = startoff;
6053 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
Brian Fostera979bdf2014-09-23 15:39:04 +10006054}
6055
6056/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11006057 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006058 *
Brian Foster2c845f52014-09-23 15:37:09 +10006059 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11006060 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10006061 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6062 * is the length by which each extent is shifted. If there is no hole to shift
6063 * the extents into, this will be considered invalid operation and we abort
6064 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006065 */
6066int
6067xfs_bmap_shift_extents(
6068 struct xfs_trans *tp,
6069 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006070 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006071 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10006072 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006073 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006074 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006075 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006076 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006077 int num_exts)
6078{
Brian Fosterca446d82014-09-02 12:12:53 +10006079 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006080 struct xfs_bmbt_rec_host *gotp;
6081 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006082 struct xfs_mount *mp = ip->i_mount;
6083 struct xfs_ifork *ifp;
6084 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10006085 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006086 xfs_extnum_t total_extents;
6087 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006088 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006089 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10006090 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006091
6092 if (unlikely(XFS_TEST_ERROR(
6093 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6094 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6095 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6096 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6097 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10006098 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006099 }
6100
6101 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10006102 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006103
Brian Foster2c845f52014-09-23 15:37:09 +10006104 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6105 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006106 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6107 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006108
6109 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006110 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6111 /* Read in all the extents */
6112 error = xfs_iread_extents(tp, ip, whichfork);
6113 if (error)
6114 return error;
6115 }
6116
Brian Fosterddb19e32014-09-23 15:38:09 +10006117 if (ifp->if_flags & XFS_IFBROOT) {
6118 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6119 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006120 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006121 cur->bc_private.b.flags = 0;
6122 }
6123
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006124 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006125 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006126 * are collapsing out, so we cannot use the count of real extents here.
6127 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006128 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006129 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006130 if (total_extents == 0) {
6131 *done = 1;
6132 goto del_cursor;
6133 }
6134
6135 /*
6136 * In case of first right shift, we need to initialize next_fsb
6137 */
6138 if (*next_fsb == NULLFSBLOCK) {
6139 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6140 xfs_bmbt_get_all(gotp, &got);
6141 *next_fsb = got.br_startoff;
6142 if (stop_fsb > *next_fsb) {
6143 *done = 1;
6144 goto del_cursor;
6145 }
6146 }
6147
6148 /* Lookup the extent index at which we have to stop */
6149 if (direction == SHIFT_RIGHT) {
6150 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6151 /* Make stop_extent exclusive of shift range */
6152 stop_extent--;
6153 } else
6154 stop_extent = total_extents;
6155
6156 /*
6157 * Look up the extent index for the fsb where we start shifting. We can
6158 * henceforth iterate with current_ext as extent list changes are locked
6159 * out via ilock.
6160 *
6161 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6162 * *next_fsb lies in a hole beyond which there are no extents. Either
6163 * way, we are done.
6164 */
6165 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
6166 if (!gotp) {
6167 *done = 1;
6168 goto del_cursor;
6169 }
6170
6171 /* some sanity checking before we finally start shifting extents */
6172 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6173 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6174 error = -EIO;
6175 goto del_cursor;
6176 }
6177
6178 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006179 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006180 &current_ext, gotp, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006181 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006182 if (error)
6183 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006184 /*
6185 * If there was an extent merge during the shift, the extent
6186 * count can change. Update the total and grade the next record.
6187 */
6188 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006189 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006190 stop_extent = total_extents;
6191 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006192
Namjae Jeona904b1c2015-03-25 15:08:56 +11006193 if (current_ext == stop_extent) {
6194 *done = 1;
6195 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006196 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006197 }
Brian Fosterddb19e32014-09-23 15:38:09 +10006198 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006199 }
6200
Namjae Jeona904b1c2015-03-25 15:08:56 +11006201 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006202 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10006203 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10006204 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006205
6206del_cursor:
6207 if (cur)
6208 xfs_btree_del_cursor(cur,
6209 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6210
Brian Fosterca446d82014-09-02 12:12:53 +10006211 if (logflags)
6212 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006213
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006214 return error;
6215}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006216
6217/*
6218 * Splits an extent into two extents at split_fsb block such that it is
6219 * the first block of the current_ext. @current_ext is a target extent
6220 * to be split. @split_fsb is a block where the extents is split.
6221 * If split_fsb lies in a hole or the first block of extents, just return 0.
6222 */
6223STATIC int
6224xfs_bmap_split_extent_at(
6225 struct xfs_trans *tp,
6226 struct xfs_inode *ip,
6227 xfs_fileoff_t split_fsb,
6228 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006229 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006230{
6231 int whichfork = XFS_DATA_FORK;
6232 struct xfs_btree_cur *cur = NULL;
6233 struct xfs_bmbt_rec_host *gotp;
6234 struct xfs_bmbt_irec got;
6235 struct xfs_bmbt_irec new; /* split extent */
6236 struct xfs_mount *mp = ip->i_mount;
6237 struct xfs_ifork *ifp;
6238 xfs_fsblock_t gotblkcnt; /* new block count for got */
6239 xfs_extnum_t current_ext;
6240 int error = 0;
6241 int logflags = 0;
6242 int i = 0;
6243
6244 if (unlikely(XFS_TEST_ERROR(
6245 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6246 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6247 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6248 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6249 XFS_ERRLEVEL_LOW, mp);
6250 return -EFSCORRUPTED;
6251 }
6252
6253 if (XFS_FORCED_SHUTDOWN(mp))
6254 return -EIO;
6255
6256 ifp = XFS_IFORK_PTR(ip, whichfork);
6257 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6258 /* Read in all the extents */
6259 error = xfs_iread_extents(tp, ip, whichfork);
6260 if (error)
6261 return error;
6262 }
6263
6264 /*
6265 * gotp can be null in 2 cases: 1) if there are no extents
6266 * or 2) split_fsb lies in a hole beyond which there are
6267 * no extents. Either way, we are done.
6268 */
6269 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
6270 if (!gotp)
6271 return 0;
6272
6273 xfs_bmbt_get_all(gotp, &got);
6274
6275 /*
6276 * Check split_fsb lies in a hole or the start boundary offset
6277 * of the extent.
6278 */
6279 if (got.br_startoff >= split_fsb)
6280 return 0;
6281
6282 gotblkcnt = split_fsb - got.br_startoff;
6283 new.br_startoff = split_fsb;
6284 new.br_startblock = got.br_startblock + gotblkcnt;
6285 new.br_blockcount = got.br_blockcount - gotblkcnt;
6286 new.br_state = got.br_state;
6287
6288 if (ifp->if_flags & XFS_IFBROOT) {
6289 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6290 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006291 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006292 cur->bc_private.b.flags = 0;
6293 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6294 got.br_startblock,
6295 got.br_blockcount,
6296 &i);
6297 if (error)
6298 goto del_cursor;
6299 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6300 }
6301
6302 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6303 got.br_blockcount = gotblkcnt;
6304
6305 logflags = XFS_ILOG_CORE;
6306 if (cur) {
6307 error = xfs_bmbt_update(cur, got.br_startoff,
6308 got.br_startblock,
6309 got.br_blockcount,
6310 got.br_state);
6311 if (error)
6312 goto del_cursor;
6313 } else
6314 logflags |= XFS_ILOG_DEXT;
6315
6316 /* Add new extent */
6317 current_ext++;
6318 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6319 XFS_IFORK_NEXT_SET(ip, whichfork,
6320 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6321
6322 if (cur) {
6323 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6324 new.br_startblock, new.br_blockcount,
6325 &i);
6326 if (error)
6327 goto del_cursor;
6328 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6329 cur->bc_rec.b.br_state = new.br_state;
6330
6331 error = xfs_btree_insert(cur, &i);
6332 if (error)
6333 goto del_cursor;
6334 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6335 }
6336
6337 /*
6338 * Convert to a btree if necessary.
6339 */
6340 if (xfs_bmap_needs_btree(ip, whichfork)) {
6341 int tmp_logflags; /* partial log flag return val */
6342
6343 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006344 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006345 &cur, 0, &tmp_logflags, whichfork);
6346 logflags |= tmp_logflags;
6347 }
6348
6349del_cursor:
6350 if (cur) {
6351 cur->bc_private.b.allocated = 0;
6352 xfs_btree_del_cursor(cur,
6353 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6354 }
6355
6356 if (logflags)
6357 xfs_trans_log_inode(tp, ip, logflags);
6358 return error;
6359}
6360
6361int
6362xfs_bmap_split_extent(
6363 struct xfs_inode *ip,
6364 xfs_fileoff_t split_fsb)
6365{
6366 struct xfs_mount *mp = ip->i_mount;
6367 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006368 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006369 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006370 int error;
6371
Christoph Hellwig253f4912016-04-06 09:19:55 +10006372 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6373 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6374 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006375 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006376
6377 xfs_ilock(ip, XFS_ILOCK_EXCL);
6378 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6379
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006380 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006381
6382 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006383 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006384 if (error)
6385 goto out;
6386
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006387 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006388 if (error)
6389 goto out;
6390
Christoph Hellwig70393312015-06-04 13:48:08 +10006391 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006392
6393out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006394 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006395 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006396 return error;
6397}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006398
6399/* Deferred mapping is only for real extents in the data fork. */
6400static bool
6401xfs_bmap_is_update_needed(
6402 struct xfs_bmbt_irec *bmap)
6403{
6404 return bmap->br_startblock != HOLESTARTBLOCK &&
6405 bmap->br_startblock != DELAYSTARTBLOCK;
6406}
6407
6408/* Record a bmap intent. */
6409static int
6410__xfs_bmap_add(
6411 struct xfs_mount *mp,
6412 struct xfs_defer_ops *dfops,
6413 enum xfs_bmap_intent_type type,
6414 struct xfs_inode *ip,
6415 int whichfork,
6416 struct xfs_bmbt_irec *bmap)
6417{
6418 int error;
6419 struct xfs_bmap_intent *bi;
6420
6421 trace_xfs_bmap_defer(mp,
6422 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6423 type,
6424 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6425 ip->i_ino, whichfork,
6426 bmap->br_startoff,
6427 bmap->br_blockcount,
6428 bmap->br_state);
6429
6430 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6431 INIT_LIST_HEAD(&bi->bi_list);
6432 bi->bi_type = type;
6433 bi->bi_owner = ip;
6434 bi->bi_whichfork = whichfork;
6435 bi->bi_bmap = *bmap;
6436
6437 error = xfs_defer_join(dfops, bi->bi_owner);
6438 if (error) {
6439 kmem_free(bi);
6440 return error;
6441 }
6442
6443 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6444 return 0;
6445}
6446
6447/* Map an extent into a file. */
6448int
6449xfs_bmap_map_extent(
6450 struct xfs_mount *mp,
6451 struct xfs_defer_ops *dfops,
6452 struct xfs_inode *ip,
6453 struct xfs_bmbt_irec *PREV)
6454{
6455 if (!xfs_bmap_is_update_needed(PREV))
6456 return 0;
6457
6458 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6459 XFS_DATA_FORK, PREV);
6460}
6461
6462/* Unmap an extent out of a file. */
6463int
6464xfs_bmap_unmap_extent(
6465 struct xfs_mount *mp,
6466 struct xfs_defer_ops *dfops,
6467 struct xfs_inode *ip,
6468 struct xfs_bmbt_irec *PREV)
6469{
6470 if (!xfs_bmap_is_update_needed(PREV))
6471 return 0;
6472
6473 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6474 XFS_DATA_FORK, PREV);
6475}
6476
6477/*
6478 * Process one of the deferred bmap operations. We pass back the
6479 * btree cursor to maintain our lock on the bmapbt between calls.
6480 */
6481int
6482xfs_bmap_finish_one(
6483 struct xfs_trans *tp,
6484 struct xfs_defer_ops *dfops,
6485 struct xfs_inode *ip,
6486 enum xfs_bmap_intent_type type,
6487 int whichfork,
6488 xfs_fileoff_t startoff,
6489 xfs_fsblock_t startblock,
6490 xfs_filblks_t blockcount,
6491 xfs_exntst_t state)
6492{
6493 struct xfs_bmbt_irec bmap;
6494 int nimaps = 1;
6495 xfs_fsblock_t firstfsb;
6496 int flags = XFS_BMAPI_REMAP;
6497 int done;
6498 int error = 0;
6499
6500 bmap.br_startblock = startblock;
6501 bmap.br_startoff = startoff;
6502 bmap.br_blockcount = blockcount;
6503 bmap.br_state = state;
6504
6505 trace_xfs_bmap_deferred(tp->t_mountp,
6506 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6507 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6508 ip->i_ino, whichfork, startoff, blockcount, state);
6509
6510 if (whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK)
6511 return -EFSCORRUPTED;
6512 if (whichfork == XFS_ATTR_FORK)
6513 flags |= XFS_BMAPI_ATTRFORK;
6514
6515 if (XFS_TEST_ERROR(false, tp->t_mountp,
6516 XFS_ERRTAG_BMAP_FINISH_ONE,
6517 XFS_RANDOM_BMAP_FINISH_ONE))
6518 return -EIO;
6519
6520 switch (type) {
6521 case XFS_BMAP_MAP:
6522 firstfsb = bmap.br_startblock;
6523 error = xfs_bmapi_write(tp, ip, bmap.br_startoff,
6524 bmap.br_blockcount, flags, &firstfsb,
6525 bmap.br_blockcount, &bmap, &nimaps,
6526 dfops);
6527 break;
6528 case XFS_BMAP_UNMAP:
6529 error = xfs_bunmapi(tp, ip, bmap.br_startoff,
6530 bmap.br_blockcount, flags, 1, &firstfsb,
6531 dfops, &done);
6532 ASSERT(done);
6533 break;
6534 default:
6535 ASSERT(0);
6536 error = -EFSCORRUPTED;
6537 }
6538
6539 return error;
6540}