blob: bfc00de5c6f17a75c7addae26e709ee6cbfde3a9 [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;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500743 if (xfs_sb_version_hascrc(&mp->m_sb))
744 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
745 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
746 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
747 else
748 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
749 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
750 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100751
752 /*
753 * Need a cursor. Can't allocate until bb_level is filled in.
754 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100755 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
756 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000757 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100758 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
759 /*
760 * Convert to a btree with two levels, one record in root.
761 */
762 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
763 memset(&args, 0, sizeof(args));
764 args.tp = tp;
765 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000766 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100767 args.firstblock = *firstblock;
768 if (*firstblock == NULLFSBLOCK) {
769 args.type = XFS_ALLOCTYPE_START_BNO;
770 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000771 } else if (dfops->dop_low) {
Darrick J. Wong90e20562016-10-03 09:11:45 -0700772try_another_ag:
Dave Chinner9e5987a2013-02-25 12:31:26 +1100773 args.type = XFS_ALLOCTYPE_START_BNO;
774 args.fsbno = *firstblock;
775 } else {
776 args.type = XFS_ALLOCTYPE_NEAR_BNO;
777 args.fsbno = *firstblock;
778 }
779 args.minlen = args.maxlen = args.prod = 1;
780 args.wasdel = wasdel;
781 *logflagsp = 0;
782 if ((error = xfs_alloc_vextent(&args))) {
783 xfs_iroot_realloc(ip, -1, whichfork);
784 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
785 return error;
786 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700787
788 /*
789 * During a CoW operation, the allocation and bmbt updates occur in
790 * different transactions. The mapping code tries to put new bmbt
791 * blocks near extents being mapped, but the only way to guarantee this
792 * is if the alloc and the mapping happen in a single transaction that
793 * has a block reservation. That isn't the case here, so if we run out
794 * of space we'll try again with another AG.
795 */
796 if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) &&
797 args.fsbno == NULLFSBLOCK &&
798 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
799 dfops->dop_low = true;
800 goto try_another_ag;
801 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100802 /*
803 * Allocation can't fail, the space was reserved.
804 */
805 ASSERT(args.fsbno != NULLFSBLOCK);
806 ASSERT(*firstblock == NULLFSBLOCK ||
807 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000808 (dfops->dop_low &&
Dave Chinner9e5987a2013-02-25 12:31:26 +1100809 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
810 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
811 cur->bc_private.b.allocated++;
812 ip->i_d.di_nblocks++;
813 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
814 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
815 /*
816 * Fill in the child block.
817 */
818 abp->b_ops = &xfs_bmbt_buf_ops;
819 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500820 if (xfs_sb_version_hascrc(&mp->m_sb))
821 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
822 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
823 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
824 else
825 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
826 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
827 XFS_BTREE_LONG_PTRS);
828
Dave Chinner9e5987a2013-02-25 12:31:26 +1100829 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100830 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100831 for (cnt = i = 0; i < nextents; i++) {
832 ep = xfs_iext_get_ext(ifp, i);
833 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
834 arp->l0 = cpu_to_be64(ep->l0);
835 arp->l1 = cpu_to_be64(ep->l1);
836 arp++; cnt++;
837 }
838 }
839 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
840 xfs_btree_set_numrecs(ablock, cnt);
841
842 /*
843 * Fill in the root key and pointer.
844 */
845 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
846 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
847 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
848 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
849 be16_to_cpu(block->bb_level)));
850 *pp = cpu_to_be64(args.fsbno);
851
852 /*
853 * Do all this logging at the end so that
854 * the root is at the right level.
855 */
856 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
857 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
858 ASSERT(*curp == NULL);
859 *curp = cur;
860 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
861 return 0;
862}
863
864/*
865 * Convert a local file to an extents file.
866 * This code is out of bounds for data forks of regular files,
867 * since the file data needs to get logged so things will stay consistent.
868 * (The bmap-level manipulations are ok, though).
869 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000870void
871xfs_bmap_local_to_extents_empty(
872 struct xfs_inode *ip,
873 int whichfork)
874{
875 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
876
Darrick J. Wong60b49842016-10-03 09:11:34 -0700877 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000878 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
879 ASSERT(ifp->if_bytes == 0);
880 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
881
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000882 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000883 ifp->if_flags &= ~XFS_IFINLINE;
884 ifp->if_flags |= XFS_IFEXTENTS;
885 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
886}
887
888
Dave Chinner9e5987a2013-02-25 12:31:26 +1100889STATIC int /* error */
890xfs_bmap_local_to_extents(
891 xfs_trans_t *tp, /* transaction pointer */
892 xfs_inode_t *ip, /* incore inode pointer */
893 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
894 xfs_extlen_t total, /* total blocks needed by transaction */
895 int *logflagsp, /* inode logging flags */
896 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500897 void (*init_fn)(struct xfs_trans *tp,
898 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100899 struct xfs_inode *ip,
900 struct xfs_ifork *ifp))
901{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000902 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100903 int flags; /* logging flags returned */
904 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000905 xfs_alloc_arg_t args; /* allocation arguments */
906 xfs_buf_t *bp; /* buffer for extent block */
907 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100908
909 /*
910 * We don't want to deal with the case of keeping inode data inline yet.
911 * So sending the data fork of a regular inode is invalid.
912 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100913 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100914 ifp = XFS_IFORK_PTR(ip, whichfork);
915 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000916
917 if (!ifp->if_bytes) {
918 xfs_bmap_local_to_extents_empty(ip, whichfork);
919 flags = XFS_ILOG_CORE;
920 goto done;
921 }
922
Dave Chinner9e5987a2013-02-25 12:31:26 +1100923 flags = 0;
924 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000925 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
926 XFS_IFINLINE);
927 memset(&args, 0, sizeof(args));
928 args.tp = tp;
929 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000930 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000931 args.firstblock = *firstblock;
932 /*
933 * Allocate a block. We know we need only one, since the
934 * file currently fits in an inode.
935 */
936 if (*firstblock == NULLFSBLOCK) {
Darrick J. Wong90e20562016-10-03 09:11:45 -0700937try_another_ag:
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000938 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
939 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100940 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000941 args.fsbno = *firstblock;
942 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100943 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000944 args.total = total;
945 args.minlen = args.maxlen = args.prod = 1;
946 error = xfs_alloc_vextent(&args);
947 if (error)
948 goto done;
949
Darrick J. Wong90e20562016-10-03 09:11:45 -0700950 /*
951 * During a CoW operation, the allocation and bmbt updates occur in
952 * different transactions. The mapping code tries to put new bmbt
953 * blocks near extents being mapped, but the only way to guarantee this
954 * is if the alloc and the mapping happen in a single transaction that
955 * has a block reservation. That isn't the case here, so if we run out
956 * of space we'll try again with another AG.
957 */
958 if (xfs_sb_version_hasreflink(&ip->i_mount->m_sb) &&
959 args.fsbno == NULLFSBLOCK &&
960 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
961 goto try_another_ag;
962 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000963 /* Can't fail, the space was reserved. */
964 ASSERT(args.fsbno != NULLFSBLOCK);
965 ASSERT(args.len == 1);
966 *firstblock = args.fsbno;
967 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
968
Dave Chinnerfe22d552015-01-22 09:30:06 +1100969 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100970 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100971 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100972 * The callout is responsible for logging because the remote format
973 * might differ from the local format and thus we don't know how much to
974 * log here. Note that init_fn must also set the buffer log item type
975 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100976 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000977 init_fn(tp, bp, ip, ifp);
978
Brian Fosterb7cdc662015-10-12 15:40:24 +1100979 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000980 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
981 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100982 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000983
984 xfs_iext_add(ifp, 0, 1);
985 ep = xfs_iext_get_ext(ifp, 0);
986 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
987 trace_xfs_bmap_post_update(ip, 0,
988 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
989 _THIS_IP_);
990 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
991 ip->i_d.di_nblocks = 1;
992 xfs_trans_mod_dquot_byino(tp, ip,
993 XFS_TRANS_DQ_BCOUNT, 1L);
994 flags |= xfs_ilog_fext(whichfork);
995
Dave Chinner9e5987a2013-02-25 12:31:26 +1100996done:
997 *logflagsp = flags;
998 return error;
999}
1000
1001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 * Called from xfs_bmap_add_attrfork to handle btree format files.
1003 */
1004STATIC int /* error */
1005xfs_bmap_add_attrfork_btree(
1006 xfs_trans_t *tp, /* transaction pointer */
1007 xfs_inode_t *ip, /* incore inode pointer */
1008 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001009 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 int *flags) /* inode logging flags */
1011{
1012 xfs_btree_cur_t *cur; /* btree cursor */
1013 int error; /* error return value */
1014 xfs_mount_t *mp; /* file system mount struct */
1015 int stat; /* newroot status */
1016
1017 mp = ip->i_mount;
1018 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1019 *flags |= XFS_ILOG_DBROOT;
1020 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001021 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001022 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 cur->bc_private.b.firstblock = *firstblock;
1024 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1025 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001026 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001027 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001028 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 goto error0;
1030 if (stat == 0) {
1031 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +10001032 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034 *firstblock = cur->bc_private.b.firstblock;
1035 cur->bc_private.b.allocated = 0;
1036 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1037 }
1038 return 0;
1039error0:
1040 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1041 return error;
1042}
1043
1044/*
1045 * Called from xfs_bmap_add_attrfork to handle extents format files.
1046 */
1047STATIC int /* error */
1048xfs_bmap_add_attrfork_extents(
1049 xfs_trans_t *tp, /* transaction pointer */
1050 xfs_inode_t *ip, /* incore inode pointer */
1051 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001052 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 int *flags) /* inode logging flags */
1054{
1055 xfs_btree_cur_t *cur; /* bmap btree cursor */
1056 int error; /* error return value */
1057
1058 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1059 return 0;
1060 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001061 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 flags, XFS_DATA_FORK);
1063 if (cur) {
1064 cur->bc_private.b.allocated = 0;
1065 xfs_btree_del_cursor(cur,
1066 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1067 }
1068 return error;
1069}
1070
1071/*
Dave Chinner1e823792013-02-11 15:58:13 +11001072 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1073 * different data fork content type needs a different callout to do the
1074 * conversion. Some are basic and only require special block initialisation
1075 * callouts for the data formating, others (directories) are so specialised they
1076 * handle everything themselves.
1077 *
1078 * XXX (dgc): investigate whether directory conversion can use the generic
1079 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001080 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
1082STATIC int /* error */
1083xfs_bmap_add_attrfork_local(
1084 xfs_trans_t *tp, /* transaction pointer */
1085 xfs_inode_t *ip, /* incore inode pointer */
1086 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001087 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 int *flags) /* inode logging flags */
1089{
1090 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1093 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001094
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001095 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001097 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 dargs.dp = ip;
1099 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001100 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001101 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 dargs.whichfork = XFS_DATA_FORK;
1103 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001104 return xfs_dir2_sf_to_block(&dargs);
1105 }
1106
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001107 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001108 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1109 flags, XFS_DATA_FORK,
1110 xfs_symlink_local_to_remote);
1111
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001112 /* should only be called for types that support local format data */
1113 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001114 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
1117/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001118 * Convert inode from non-attributed to attributed.
1119 * Must not be in a transaction, ip must not be locked.
1120 */
1121int /* error code */
1122xfs_bmap_add_attrfork(
1123 xfs_inode_t *ip, /* incore inode pointer */
1124 int size, /* space new attribute needs */
1125 int rsvd) /* xact may use reserved blks */
1126{
1127 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001128 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001129 xfs_mount_t *mp; /* mount structure */
1130 xfs_trans_t *tp; /* transaction pointer */
1131 int blks; /* space reservation */
1132 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001133 int logflags; /* logging flags */
1134 int error; /* error return value */
1135
1136 ASSERT(XFS_IFORK_Q(ip) == 0);
1137
1138 mp = ip->i_mount;
1139 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001140
Dave Chinner9e5987a2013-02-25 12:31:26 +11001141 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001142
1143 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1144 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1145 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001146 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001147
Dave Chinner9e5987a2013-02-25 12:31:26 +11001148 xfs_ilock(ip, XFS_ILOCK_EXCL);
1149 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1150 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1151 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001152 if (error)
1153 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001154 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001155 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001156 if (ip->i_d.di_anextents != 0) {
1157 error = -EFSCORRUPTED;
1158 goto trans_cancel;
1159 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001160 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1161 /*
1162 * For inodes coming from pre-6.2 filesystems.
1163 */
1164 ASSERT(ip->i_d.di_aformat == 0);
1165 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1166 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001167
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001168 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001169 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1170
1171 switch (ip->i_d.di_format) {
1172 case XFS_DINODE_FMT_DEV:
1173 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1174 break;
1175 case XFS_DINODE_FMT_UUID:
1176 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1177 break;
1178 case XFS_DINODE_FMT_LOCAL:
1179 case XFS_DINODE_FMT_EXTENTS:
1180 case XFS_DINODE_FMT_BTREE:
1181 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1182 if (!ip->i_d.di_forkoff)
1183 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1184 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1185 version = 2;
1186 break;
1187 default:
1188 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001189 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001190 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001191 }
1192
1193 ASSERT(ip->i_afp == NULL);
1194 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1195 ip->i_afp->if_flags = XFS_IFEXTENTS;
1196 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001197 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001198 switch (ip->i_d.di_format) {
1199 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001200 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001201 &logflags);
1202 break;
1203 case XFS_DINODE_FMT_EXTENTS:
1204 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001205 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001206 break;
1207 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001208 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001209 &logflags);
1210 break;
1211 default:
1212 error = 0;
1213 break;
1214 }
1215 if (logflags)
1216 xfs_trans_log_inode(tp, ip, logflags);
1217 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001218 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001219 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1220 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001221 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001222
1223 spin_lock(&mp->m_sb_lock);
1224 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1225 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001226 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001227 }
1228 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1229 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001230 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001231 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001232 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001233 if (log_sb)
1234 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001235 }
1236
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001237 error = xfs_defer_finish(&tp, &dfops, NULL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001238 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001239 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001240 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001241 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001242 return error;
1243
1244bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001245 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001246trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001247 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001248 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001249 return error;
1250}
1251
1252/*
1253 * Internal and external extent tree search functions.
1254 */
1255
1256/*
1257 * Read in the extents to if_extents.
1258 * All inode fields are set up by caller, we just traverse the btree
1259 * and copy the records in. If the file system cannot contain unwritten
1260 * extents, the records are checked for no "state" flags.
1261 */
1262int /* error */
1263xfs_bmap_read_extents(
1264 xfs_trans_t *tp, /* transaction pointer */
1265 xfs_inode_t *ip, /* incore inode */
1266 int whichfork) /* data or attr fork */
1267{
1268 struct xfs_btree_block *block; /* current btree block */
1269 xfs_fsblock_t bno; /* block # of "block" */
1270 xfs_buf_t *bp; /* buffer for "block" */
1271 int error; /* error return value */
1272 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1273 xfs_extnum_t i, j; /* index into the extents list */
1274 xfs_ifork_t *ifp; /* fork structure */
1275 int level; /* btree level, for checking */
1276 xfs_mount_t *mp; /* file system mount structure */
1277 __be64 *pp; /* pointer to block address */
1278 /* REFERENCED */
1279 xfs_extnum_t room; /* number of entries there's room for */
1280
1281 bno = NULLFSBLOCK;
1282 mp = ip->i_mount;
1283 ifp = XFS_IFORK_PTR(ip, whichfork);
1284 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1285 XFS_EXTFMT_INODE(ip);
1286 block = ifp->if_broot;
1287 /*
1288 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1289 */
1290 level = be16_to_cpu(block->bb_level);
1291 ASSERT(level > 0);
1292 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1293 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001294 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001295 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1296 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1297 /*
1298 * Go down the tree until leaf level is reached, following the first
1299 * pointer (leftmost) at each level.
1300 */
1301 while (level-- > 0) {
1302 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1303 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1304 if (error)
1305 return error;
1306 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001307 if (level == 0)
1308 break;
1309 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1310 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001311 XFS_WANT_CORRUPTED_GOTO(mp,
1312 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001313 xfs_trans_brelse(tp, bp);
1314 }
1315 /*
1316 * Here with bp and block set to the leftmost leaf node in the tree.
1317 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001318 room = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001319 i = 0;
1320 /*
1321 * Loop over all leaf nodes. Copy information to the extent records.
1322 */
1323 for (;;) {
1324 xfs_bmbt_rec_t *frp;
1325 xfs_fsblock_t nextbno;
1326 xfs_extnum_t num_recs;
1327 xfs_extnum_t start;
1328
1329 num_recs = xfs_btree_get_numrecs(block);
1330 if (unlikely(i + num_recs > room)) {
1331 ASSERT(i + num_recs <= room);
1332 xfs_warn(ip->i_mount,
1333 "corrupt dinode %Lu, (btree extents).",
1334 (unsigned long long) ip->i_ino);
1335 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1336 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1337 goto error0;
1338 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001339 /*
1340 * Read-ahead the next leaf block, if any.
1341 */
1342 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1343 if (nextbno != NULLFSBLOCK)
1344 xfs_btree_reada_bufl(mp, nextbno, 1,
1345 &xfs_bmbt_buf_ops);
1346 /*
1347 * Copy records into the extent records.
1348 */
1349 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1350 start = i;
1351 for (j = 0; j < num_recs; j++, i++, frp++) {
1352 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1353 trp->l0 = be64_to_cpu(frp->l0);
1354 trp->l1 = be64_to_cpu(frp->l1);
1355 }
1356 if (exntf == XFS_EXTFMT_NOSTATE) {
1357 /*
1358 * Check all attribute bmap btree records and
1359 * any "older" data bmap btree records for a
1360 * set bit in the "extent flag" position.
1361 */
1362 if (unlikely(xfs_check_nostate_extents(ifp,
1363 start, num_recs))) {
1364 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1365 XFS_ERRLEVEL_LOW,
1366 ip->i_mount);
1367 goto error0;
1368 }
1369 }
1370 xfs_trans_brelse(tp, bp);
1371 bno = nextbno;
1372 /*
1373 * If we've reached the end, stop.
1374 */
1375 if (bno == NULLFSBLOCK)
1376 break;
1377 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1378 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1379 if (error)
1380 return error;
1381 block = XFS_BUF_TO_BLOCK(bp);
1382 }
Darrick J. Wong356a3222016-12-05 12:36:56 +11001383 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1384 return -EFSCORRUPTED;
Eric Sandeen5d829302016-11-08 12:59:42 +11001385 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11001386 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1387 return 0;
1388error0:
1389 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001390 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001391}
1392
Dave Chinner9e5987a2013-02-25 12:31:26 +11001393/*
1394 * Returns the file-relative block number of the first unused block(s)
1395 * in the file with at least "len" logically contiguous blocks free.
1396 * This is the lowest-address hole if the file has holes, else the first block
1397 * past the end of file.
1398 * Return 0 if the file is currently local (in-inode).
1399 */
1400int /* error */
1401xfs_bmap_first_unused(
1402 xfs_trans_t *tp, /* transaction pointer */
1403 xfs_inode_t *ip, /* incore inode */
1404 xfs_extlen_t len, /* size of hole to find */
1405 xfs_fileoff_t *first_unused, /* unused block */
1406 int whichfork) /* data or attr fork */
1407{
1408 int error; /* error return value */
1409 int idx; /* extent record index */
1410 xfs_ifork_t *ifp; /* inode fork pointer */
1411 xfs_fileoff_t lastaddr; /* last block number seen */
1412 xfs_fileoff_t lowest; /* lowest useful block */
1413 xfs_fileoff_t max; /* starting useful block */
1414 xfs_fileoff_t off; /* offset for this block */
1415 xfs_extnum_t nextents; /* number of extent entries */
1416
1417 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1418 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1419 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1420 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1421 *first_unused = 0;
1422 return 0;
1423 }
1424 ifp = XFS_IFORK_PTR(ip, whichfork);
1425 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1426 (error = xfs_iread_extents(tp, ip, whichfork)))
1427 return error;
1428 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001429 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001430 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1431 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1432 off = xfs_bmbt_get_startoff(ep);
1433 /*
1434 * See if the hole before this extent will work.
1435 */
1436 if (off >= lowest + len && off - max >= len) {
1437 *first_unused = max;
1438 return 0;
1439 }
1440 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1441 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1442 }
1443 *first_unused = max;
1444 return 0;
1445}
1446
1447/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001448 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001449 * last_block (input value) in the file.
1450 * This is not based on i_size, it is based on the extent records.
1451 * Returns 0 for local files, as they do not have extent records.
1452 */
1453int /* error */
1454xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001455 struct xfs_trans *tp, /* transaction pointer */
1456 struct xfs_inode *ip, /* incore inode */
1457 xfs_fileoff_t *last_block, /* last block */
1458 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001459{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001460 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1461 struct xfs_bmbt_irec got;
1462 xfs_extnum_t idx;
1463 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001464
Christoph Hellwig86685f72016-11-24 11:39:38 +11001465 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1466 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001467 *last_block = 0;
1468 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001469 case XFS_DINODE_FMT_BTREE:
1470 case XFS_DINODE_FMT_EXTENTS:
1471 break;
1472 default:
1473 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001474 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001475
1476 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1477 error = xfs_iread_extents(tp, ip, whichfork);
1478 if (error)
1479 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001480 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001481
1482 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1483 if (got.br_startoff <= *last_block - 1)
1484 return 0;
1485 }
1486
1487 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1488 *last_block = got.br_startoff + got.br_blockcount;
1489 return 0;
1490 }
1491
1492 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001493 return 0;
1494}
1495
Dave Chinner68988112013-08-12 20:49:42 +10001496int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001497xfs_bmap_last_extent(
1498 struct xfs_trans *tp,
1499 struct xfs_inode *ip,
1500 int whichfork,
1501 struct xfs_bmbt_irec *rec,
1502 int *is_empty)
1503{
1504 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1505 int error;
1506 int nextents;
1507
1508 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1509 error = xfs_iread_extents(tp, ip, whichfork);
1510 if (error)
1511 return error;
1512 }
1513
Eric Sandeen5d829302016-11-08 12:59:42 +11001514 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001515 if (nextents == 0) {
1516 *is_empty = 1;
1517 return 0;
1518 }
1519
1520 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1521 *is_empty = 0;
1522 return 0;
1523}
1524
1525/*
1526 * Check the last inode extent to determine whether this allocation will result
1527 * in blocks being allocated at the end of the file. When we allocate new data
1528 * blocks at the end of the file which do not start at the previous data block,
1529 * we will try to align the new blocks at stripe unit boundaries.
1530 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001531 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001532 * at, or past the EOF.
1533 */
1534STATIC int
1535xfs_bmap_isaeof(
1536 struct xfs_bmalloca *bma,
1537 int whichfork)
1538{
1539 struct xfs_bmbt_irec rec;
1540 int is_empty;
1541 int error;
1542
1543 bma->aeof = 0;
1544 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1545 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001546 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001547 return error;
1548
Dave Chinner6e708bc2013-11-22 10:41:16 +11001549 if (is_empty) {
1550 bma->aeof = 1;
1551 return 0;
1552 }
1553
Dave Chinner9e5987a2013-02-25 12:31:26 +11001554 /*
1555 * Check if we are allocation or past the last extent, or at least into
1556 * the last delayed allocated extent.
1557 */
1558 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1559 (bma->offset >= rec.br_startoff &&
1560 isnullstartblock(rec.br_startblock));
1561 return 0;
1562}
1563
1564/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001565 * Returns the file-relative block number of the first block past eof in
1566 * the file. This is not based on i_size, it is based on the extent records.
1567 * Returns 0 for local files, as they do not have extent records.
1568 */
1569int
1570xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001571 struct xfs_inode *ip,
1572 xfs_fileoff_t *last_block,
1573 int whichfork)
1574{
1575 struct xfs_bmbt_irec rec;
1576 int is_empty;
1577 int error;
1578
1579 *last_block = 0;
1580
1581 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1582 return 0;
1583
1584 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1585 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001586 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001587
1588 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1589 if (error || is_empty)
1590 return error;
1591
1592 *last_block = rec.br_startoff + rec.br_blockcount;
1593 return 0;
1594}
1595
1596/*
1597 * Returns whether the selected fork of the inode has exactly one
1598 * block or not. For the data fork we check this matches di_size,
1599 * implying the file's range is 0..bsize-1.
1600 */
1601int /* 1=>1 block, 0=>otherwise */
1602xfs_bmap_one_block(
1603 xfs_inode_t *ip, /* incore inode */
1604 int whichfork) /* data or attr fork */
1605{
1606 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1607 xfs_ifork_t *ifp; /* inode fork pointer */
1608 int rval; /* return value */
1609 xfs_bmbt_irec_t s; /* internal version of extent */
1610
1611#ifndef DEBUG
1612 if (whichfork == XFS_DATA_FORK)
1613 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1614#endif /* !DEBUG */
1615 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1616 return 0;
1617 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1618 return 0;
1619 ifp = XFS_IFORK_PTR(ip, whichfork);
1620 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1621 ep = xfs_iext_get_ext(ifp, 0);
1622 xfs_bmbt_get_all(ep, &s);
1623 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1624 if (rval && whichfork == XFS_DATA_FORK)
1625 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1626 return rval;
1627}
1628
1629/*
1630 * Extent tree manipulation functions used during allocation.
1631 */
1632
1633/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001634 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 */
1636STATIC int /* error */
1637xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001638 struct xfs_bmalloca *bma,
1639 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001641 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001643 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001646 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 xfs_fileoff_t new_endoff; /* end offset of new entry */
1648 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1649 /* left is 0, right is 1, prev is 2 */
1650 int rval=0; /* return value (logging flags) */
1651 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001652 xfs_filblks_t da_new; /* new count del alloc blocks used */
1653 xfs_filblks_t da_old; /* old count del alloc blocks used */
1654 xfs_filblks_t temp=0; /* value for da_new calculations */
1655 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001657 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001658 xfs_extnum_t *nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001660 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001661 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001662 ASSERT(whichfork != XFS_ATTR_FORK);
1663 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1664 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001665
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001666 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001667 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001668 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001669 ASSERT(!bma->cur ||
1670 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001671
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001672 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001673
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674#define LEFT r[0]
1675#define RIGHT r[1]
1676#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Darrick J. Wong60b49842016-10-03 09:11:34 -07001678 if (whichfork == XFS_COW_FORK)
1679 state |= BMAP_COWFORK;
1680
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 /*
1682 * Set up a bunch of variables to make the tests simpler.
1683 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001684 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 xfs_bmbt_get_all(ep, &PREV);
1686 new_endoff = new->br_startoff + new->br_blockcount;
1687 ASSERT(PREV.br_startoff <= new->br_startoff);
1688 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001689
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001690 da_old = startblockval(PREV.br_startblock);
1691 da_new = 0;
1692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 /*
1694 * Set flags determining what part of the previous delayed allocation
1695 * extent is being replaced by a real allocation.
1696 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001697 if (PREV.br_startoff == new->br_startoff)
1698 state |= BMAP_LEFT_FILLING;
1699 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1700 state |= BMAP_RIGHT_FILLING;
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 /*
1703 * Check and set flags if this segment has a left neighbor.
1704 * Don't set contiguous if the combined extent would be too large.
1705 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001706 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001707 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001708 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001709
1710 if (isnullstartblock(LEFT.br_startblock))
1711 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001713
1714 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1715 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1716 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1717 LEFT.br_state == new->br_state &&
1718 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1719 state |= BMAP_LEFT_CONTIG;
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /*
1722 * Check and set flags if this segment has a right neighbor.
1723 * Don't set contiguous if the combined extent would be too large.
1724 * Also check for all-three-contiguous being too large.
1725 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001726 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001727 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001728 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001729
1730 if (isnullstartblock(RIGHT.br_startblock))
1731 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001733
1734 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1735 new_endoff == RIGHT.br_startoff &&
1736 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1737 new->br_state == RIGHT.br_state &&
1738 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1739 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1740 BMAP_RIGHT_FILLING)) !=
1741 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1742 BMAP_RIGHT_FILLING) ||
1743 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1744 <= MAXEXTLEN))
1745 state |= BMAP_RIGHT_CONTIG;
1746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 error = 0;
1748 /*
1749 * Switch out based on the FILLING and CONTIG state bits.
1750 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001751 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1752 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1753 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1754 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 /*
1756 * Filling in all of a previously delayed allocation extent.
1757 * The left and right neighbors are both contiguous with new.
1758 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001759 bma->idx--;
1760 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1761 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 LEFT.br_blockcount + PREV.br_blockcount +
1763 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001764 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001765
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001766 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001767 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001768 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1770 else {
1771 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001772 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001774 RIGHT.br_blockcount, &i);
1775 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001777 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001778 error = xfs_btree_delete(bma->cur, &i);
1779 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001781 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001782 error = xfs_btree_decrement(bma->cur, 0, &i);
1783 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001785 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001786 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 LEFT.br_startblock,
1788 LEFT.br_blockcount +
1789 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001790 RIGHT.br_blockcount, LEFT.br_state);
1791 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto done;
1793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 break;
1795
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001796 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 /*
1798 * Filling in all of a previously delayed allocation extent.
1799 * The left neighbor is contiguous, the right is not.
1800 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001801 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001802
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001803 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1804 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001805 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001806 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001807
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001808 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1809 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 rval = XFS_ILOG_DEXT;
1811 else {
1812 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001813 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001815 &i);
1816 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001818 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 LEFT.br_startblock,
1821 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001822 PREV.br_blockcount, LEFT.br_state);
1823 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 goto done;
1825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 break;
1827
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001828 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 /*
1830 * Filling in all of a previously delayed allocation extent.
1831 * The right neighbor is contiguous, the left is not.
1832 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001833 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 xfs_bmbt_set_startblock(ep, new->br_startblock);
1835 xfs_bmbt_set_blockcount(ep,
1836 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001837 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001838
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001839 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1840 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 rval = XFS_ILOG_DEXT;
1842 else {
1843 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001844 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001846 RIGHT.br_blockcount, &i);
1847 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001849 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001850 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 new->br_startblock,
1852 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001853 RIGHT.br_blockcount, PREV.br_state);
1854 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 goto done;
1856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 break;
1858
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001859 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /*
1861 * Filling in all of a previously delayed allocation extent.
1862 * Neither the left nor right neighbors are contiguous with
1863 * the new one.
1864 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001865 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001867 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001868
Darrick J. Wong60b49842016-10-03 09:11:34 -07001869 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001870 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1872 else {
1873 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001874 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001876 &i);
1877 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001879 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001880 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1881 error = xfs_btree_insert(bma->cur, &i);
1882 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001884 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 break;
1887
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001888 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 /*
1890 * Filling in the first part of a previous delayed allocation.
1891 * The left neighbor is contiguous.
1892 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001893 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1894 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 LEFT.br_blockcount + new->br_blockcount);
1896 xfs_bmbt_set_startoff(ep,
1897 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001898 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001901 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001903 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 rval = XFS_ILOG_DEXT;
1905 else {
1906 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001907 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001909 &i);
1910 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001912 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001913 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 LEFT.br_startblock,
1915 LEFT.br_blockcount +
1916 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001917 LEFT.br_state);
1918 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 goto done;
1920 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001921 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001922 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001923 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001924 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001925
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001926 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 break;
1928
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001929 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 /*
1931 * Filling in the first part of a previous delayed allocation.
1932 * The left neighbor is not contiguous.
1933 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001934 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 xfs_bmbt_set_startoff(ep, new_endoff);
1936 temp = PREV.br_blockcount - new->br_blockcount;
1937 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001938 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001939 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001940 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1942 else {
1943 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001944 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001946 &i);
1947 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001949 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001950 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1951 error = xfs_btree_insert(bma->cur, &i);
1952 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001954 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001956
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001957 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001959 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001960 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 rval |= tmp_rval;
1962 if (error)
1963 goto done;
1964 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001965 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001966 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001967 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1968 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001969 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001970 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 break;
1972
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001973 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /*
1975 * Filling in the last part of a previous delayed allocation.
1976 * The right neighbor is contiguous with the new allocation.
1977 */
1978 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001979 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001981 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001982 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 new->br_blockcount + RIGHT.br_blockcount,
1984 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001985 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1986 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 rval = XFS_ILOG_DEXT;
1988 else {
1989 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001990 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001992 RIGHT.br_blockcount, &i);
1993 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001995 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001996 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 new->br_startblock,
1998 new->br_blockcount +
1999 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002000 RIGHT.br_state);
2001 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 goto done;
2003 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002004
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002005 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002006 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002007 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002008 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002009 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002010
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002011 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 break;
2013
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002014 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /*
2016 * Filling in the last part of a previous delayed allocation.
2017 * The right neighbor is not contiguous.
2018 */
2019 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002020 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002022 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002023 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002024 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2026 else {
2027 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002028 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002030 &i);
2031 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002033 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002034 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2035 error = xfs_btree_insert(bma->cur, &i);
2036 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002038 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002040
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002041 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002042 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002043 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002044 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 rval |= tmp_rval;
2046 if (error)
2047 goto done;
2048 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002049 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002050 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002051 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2052 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002053 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002054 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002055
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002056 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 break;
2058
2059 case 0:
2060 /*
2061 * Filling in the middle part of a previous delayed allocation.
2062 * Contiguity is impossible here.
2063 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002064 *
2065 * We start with a delayed allocation:
2066 *
2067 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2068 * PREV @ idx
2069 *
2070 * and we are allocating:
2071 * +rrrrrrrrrrrrrrrrr+
2072 * new
2073 *
2074 * and we set it up for insertion as:
2075 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2076 * new
2077 * PREV @ idx LEFT RIGHT
2078 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 */
2080 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002082 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002083 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2084 LEFT = *new;
2085 RIGHT.br_state = PREV.br_state;
2086 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002087 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002088 RIGHT.br_startoff = new_endoff;
2089 RIGHT.br_blockcount = temp2;
2090 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002091 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002092 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002093 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2095 else {
2096 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002097 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002099 &i);
2100 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002102 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002103 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2104 error = xfs_btree_insert(bma->cur, &i);
2105 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002107 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002109
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002110 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002111 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002112 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002113 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 rval |= tmp_rval;
2115 if (error)
2116 goto done;
2117 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002118 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2119 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002120 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002121 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002122 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002123 error = xfs_mod_fdblocks(bma->ip->i_mount,
2124 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002125 ASSERT(!error);
2126 if (error)
2127 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002129
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002130 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002131 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002132 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2133 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2134 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002135 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002136 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002137
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002138 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002139 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 break;
2141
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002142 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2143 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2144 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2145 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2146 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2147 case BMAP_LEFT_CONTIG:
2148 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 /*
2150 * These cases are all impossible.
2151 */
2152 ASSERT(0);
2153 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002154
Darrick J. Wong9c194642016-08-03 12:16:05 +10002155 /* add reverse mapping */
2156 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2157 if (error)
2158 goto done;
2159
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002160 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002161 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002162 int tmp_logflags; /* partial log flag return val */
2163
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002164 ASSERT(bma->cur == NULL);
2165 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002166 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002167 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002168 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002169 if (error)
2170 goto done;
2171 }
2172
2173 /* adjust for changes in reserved delayed indirect blocks */
2174 if (da_old || da_new) {
2175 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002176 if (bma->cur)
2177 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002178 ASSERT(temp <= da_old);
2179 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002180 xfs_mod_fdblocks(bma->ip->i_mount,
2181 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002182 }
2183
2184 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002185 if (bma->cur)
2186 bma->cur->bc_private.b.allocated = 0;
2187
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002188 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002190 if (whichfork != XFS_COW_FORK)
2191 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return error;
2193#undef LEFT
2194#undef RIGHT
2195#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196}
2197
2198/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002199 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 */
2201STATIC int /* error */
2202xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002203 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002205 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002207 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002208 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002209 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002210 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002213 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002216 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 xfs_fileoff_t new_endoff; /* end offset of new entry */
2218 xfs_exntst_t newext; /* new extent state */
2219 xfs_exntst_t oldext; /* old extent state */
2220 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2221 /* left is 0, right is 1, prev is 2 */
2222 int rval=0; /* return value (logging flags) */
2223 int state = 0;/* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002224 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002226 *logflagsp = 0;
2227
2228 cur = *curp;
2229 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2230
2231 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002232 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002233 ASSERT(!isnullstartblock(new->br_startblock));
2234
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002235 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237#define LEFT r[0]
2238#define RIGHT r[1]
2239#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 /*
2242 * Set up a bunch of variables to make the tests simpler.
2243 */
2244 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002245 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 xfs_bmbt_get_all(ep, &PREV);
2247 newext = new->br_state;
2248 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2249 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2250 ASSERT(PREV.br_state == oldext);
2251 new_endoff = new->br_startoff + new->br_blockcount;
2252 ASSERT(PREV.br_startoff <= new->br_startoff);
2253 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 /*
2256 * Set flags determining what part of the previous oldext allocation
2257 * extent is being replaced by a newext allocation.
2258 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002259 if (PREV.br_startoff == new->br_startoff)
2260 state |= BMAP_LEFT_FILLING;
2261 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2262 state |= BMAP_RIGHT_FILLING;
2263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 /*
2265 * Check and set flags if this segment has a left neighbor.
2266 * Don't set contiguous if the combined extent would be too large.
2267 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002268 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002269 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002270 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002271
2272 if (isnullstartblock(LEFT.br_startblock))
2273 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002275
2276 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2277 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2278 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2279 LEFT.br_state == newext &&
2280 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2281 state |= BMAP_LEFT_CONTIG;
2282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 /*
2284 * Check and set flags if this segment has a right neighbor.
2285 * Don't set contiguous if the combined extent would be too large.
2286 * Also check for all-three-contiguous being too large.
2287 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002288 if (*idx < xfs_iext_count(&ip->i_df) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002289 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002290 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002291 if (isnullstartblock(RIGHT.br_startblock))
2292 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002294
2295 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2296 new_endoff == RIGHT.br_startoff &&
2297 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2298 newext == RIGHT.br_state &&
2299 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2300 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2301 BMAP_RIGHT_FILLING)) !=
2302 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2303 BMAP_RIGHT_FILLING) ||
2304 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2305 <= MAXEXTLEN))
2306 state |= BMAP_RIGHT_CONTIG;
2307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 /*
2309 * Switch out based on the FILLING and CONTIG state bits.
2310 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002311 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2312 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2313 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2314 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 /*
2316 * Setting all of a previous oldext extent to newext.
2317 * The left and right neighbors are both contiguous with new.
2318 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002319 --*idx;
2320
2321 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2322 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 LEFT.br_blockcount + PREV.br_blockcount +
2324 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002325 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002326
Christoph Hellwigec90c552011-05-23 08:52:53 +00002327 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 ip->i_d.di_nextents -= 2;
2329 if (cur == NULL)
2330 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2331 else {
2332 rval = XFS_ILOG_CORE;
2333 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2334 RIGHT.br_startblock,
2335 RIGHT.br_blockcount, &i)))
2336 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002337 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002338 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002341 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002343 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002344 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002346 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002347 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002349 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2351 LEFT.br_startblock,
2352 LEFT.br_blockcount + PREV.br_blockcount +
2353 RIGHT.br_blockcount, LEFT.br_state)))
2354 goto done;
2355 }
2356 break;
2357
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002358 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 /*
2360 * Setting all of a previous oldext extent to newext.
2361 * The left neighbor is contiguous, the right is not.
2362 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002363 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002364
Christoph Hellwigec90c552011-05-23 08:52:53 +00002365 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2366 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2367 LEFT.br_blockcount + PREV.br_blockcount);
2368 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2369
2370 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 ip->i_d.di_nextents--;
2372 if (cur == NULL)
2373 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2374 else {
2375 rval = XFS_ILOG_CORE;
2376 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2377 PREV.br_startblock, PREV.br_blockcount,
2378 &i)))
2379 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002380 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002381 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002383 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002384 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002386 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2388 LEFT.br_startblock,
2389 LEFT.br_blockcount + PREV.br_blockcount,
2390 LEFT.br_state)))
2391 goto done;
2392 }
2393 break;
2394
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002395 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 /*
2397 * Setting all of a previous oldext extent to newext.
2398 * The right neighbor is contiguous, the left is not.
2399 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002400 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 xfs_bmbt_set_blockcount(ep,
2402 PREV.br_blockcount + RIGHT.br_blockcount);
2403 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002404 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2405 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 ip->i_d.di_nextents--;
2407 if (cur == NULL)
2408 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2409 else {
2410 rval = XFS_ILOG_CORE;
2411 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2412 RIGHT.br_startblock,
2413 RIGHT.br_blockcount, &i)))
2414 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002415 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002416 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002418 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002419 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002421 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2423 new->br_startblock,
2424 new->br_blockcount + RIGHT.br_blockcount,
2425 newext)))
2426 goto done;
2427 }
2428 break;
2429
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002430 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 /*
2432 * Setting all of a previous oldext extent to newext.
2433 * Neither the left nor right neighbors are contiguous with
2434 * the new one.
2435 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002436 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002438 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (cur == NULL)
2441 rval = XFS_ILOG_DEXT;
2442 else {
2443 rval = 0;
2444 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2445 new->br_startblock, new->br_blockcount,
2446 &i)))
2447 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002448 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2450 new->br_startblock, new->br_blockcount,
2451 newext)))
2452 goto done;
2453 }
2454 break;
2455
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002456 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 /*
2458 * Setting the first part of a previous oldext extent to newext.
2459 * The left neighbor is contiguous.
2460 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002461 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2462 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 LEFT.br_blockcount + new->br_blockcount);
2464 xfs_bmbt_set_startoff(ep,
2465 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002466 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002467
Christoph Hellwigec90c552011-05-23 08:52:53 +00002468 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 xfs_bmbt_set_startblock(ep,
2470 new->br_startblock + new->br_blockcount);
2471 xfs_bmbt_set_blockcount(ep,
2472 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002473 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002474
Christoph Hellwigec90c552011-05-23 08:52:53 +00002475 --*idx;
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if (cur == NULL)
2478 rval = XFS_ILOG_DEXT;
2479 else {
2480 rval = 0;
2481 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2482 PREV.br_startblock, PREV.br_blockcount,
2483 &i)))
2484 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002485 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if ((error = xfs_bmbt_update(cur,
2487 PREV.br_startoff + new->br_blockcount,
2488 PREV.br_startblock + new->br_blockcount,
2489 PREV.br_blockcount - new->br_blockcount,
2490 oldext)))
2491 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002492 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002494 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 LEFT.br_startblock,
2496 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002497 LEFT.br_state);
2498 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 goto done;
2500 }
2501 break;
2502
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002503 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 /*
2505 * Setting the first part of a previous oldext extent to newext.
2506 * The left neighbor is not contiguous.
2507 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002508 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2510 xfs_bmbt_set_startoff(ep, new_endoff);
2511 xfs_bmbt_set_blockcount(ep,
2512 PREV.br_blockcount - new->br_blockcount);
2513 xfs_bmbt_set_startblock(ep,
2514 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002515 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002516
Christoph Hellwigec90c552011-05-23 08:52:53 +00002517 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 ip->i_d.di_nextents++;
2519 if (cur == NULL)
2520 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2521 else {
2522 rval = XFS_ILOG_CORE;
2523 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2524 PREV.br_startblock, PREV.br_blockcount,
2525 &i)))
2526 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002527 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 if ((error = xfs_bmbt_update(cur,
2529 PREV.br_startoff + new->br_blockcount,
2530 PREV.br_startblock + new->br_blockcount,
2531 PREV.br_blockcount - new->br_blockcount,
2532 oldext)))
2533 goto done;
2534 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002535 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002537 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
2539 break;
2540
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002541 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 /*
2543 * Setting the last part of a previous oldext extent to newext.
2544 * The right neighbor is contiguous with the new allocation.
2545 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002546 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 xfs_bmbt_set_blockcount(ep,
2548 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002549 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2550
2551 ++*idx;
2552
2553 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2554 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002555 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002557 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (cur == NULL)
2560 rval = XFS_ILOG_DEXT;
2561 else {
2562 rval = 0;
2563 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2564 PREV.br_startblock,
2565 PREV.br_blockcount, &i)))
2566 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002567 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2569 PREV.br_startblock,
2570 PREV.br_blockcount - new->br_blockcount,
2571 oldext)))
2572 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002573 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 goto done;
2575 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2576 new->br_startblock,
2577 new->br_blockcount + RIGHT.br_blockcount,
2578 newext)))
2579 goto done;
2580 }
2581 break;
2582
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002583 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 /*
2585 * Setting the last part of a previous oldext extent to newext.
2586 * The right neighbor is not contiguous.
2587 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002588 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 xfs_bmbt_set_blockcount(ep,
2590 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002591 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002592
Christoph Hellwigec90c552011-05-23 08:52:53 +00002593 ++*idx;
2594 xfs_iext_insert(ip, *idx, 1, new, state);
2595
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 ip->i_d.di_nextents++;
2597 if (cur == NULL)
2598 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2599 else {
2600 rval = XFS_ILOG_CORE;
2601 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2602 PREV.br_startblock, PREV.br_blockcount,
2603 &i)))
2604 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002605 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2607 PREV.br_startblock,
2608 PREV.br_blockcount - new->br_blockcount,
2609 oldext)))
2610 goto done;
2611 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2612 new->br_startblock, new->br_blockcount,
2613 &i)))
2614 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002615 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002617 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002619 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 }
2621 break;
2622
2623 case 0:
2624 /*
2625 * Setting the middle part of a previous oldext extent to
2626 * newext. Contiguity is impossible here.
2627 * One extent becomes three extents.
2628 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002629 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 xfs_bmbt_set_blockcount(ep,
2631 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002632 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 r[0] = *new;
2635 r[1].br_startoff = new_endoff;
2636 r[1].br_blockcount =
2637 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2638 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2639 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002640
2641 ++*idx;
2642 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2643
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 ip->i_d.di_nextents += 2;
2645 if (cur == NULL)
2646 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2647 else {
2648 rval = XFS_ILOG_CORE;
2649 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2650 PREV.br_startblock, PREV.br_blockcount,
2651 &i)))
2652 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002653 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 /* new right extent - oldext */
2655 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2656 r[1].br_startblock, r[1].br_blockcount,
2657 r[1].br_state)))
2658 goto done;
2659 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002661 cur->bc_rec.b.br_blockcount =
2662 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002663 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002665 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002666 /*
2667 * Reset the cursor to the position of the new extent
2668 * we are about to insert as we can't trust it after
2669 * the previous insert.
2670 */
2671 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2672 new->br_startblock, new->br_blockcount,
2673 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002675 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002677 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002678 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002680 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 }
2682 break;
2683
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002684 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2685 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2686 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2687 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2688 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2689 case BMAP_LEFT_CONTIG:
2690 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 /*
2692 * These cases are all impossible.
2693 */
2694 ASSERT(0);
2695 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002696
Darrick J. Wong9c194642016-08-03 12:16:05 +10002697 /* update reverse mappings */
2698 error = xfs_rmap_convert_extent(mp, dfops, ip, XFS_DATA_FORK, new);
2699 if (error)
2700 goto done;
2701
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002702 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002703 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002704 int tmp_logflags; /* partial log flag return val */
2705
2706 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002707 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002708 0, &tmp_logflags, XFS_DATA_FORK);
2709 *logflagsp |= tmp_logflags;
2710 if (error)
2711 goto done;
2712 }
2713
2714 /* clear out the allocated field, done with it now in any case. */
2715 if (cur) {
2716 cur->bc_private.b.allocated = 0;
2717 *curp = cur;
2718 }
2719
2720 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002722 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return error;
2724#undef LEFT
2725#undef RIGHT
2726#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727}
2728
2729/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002730 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002732STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733xfs_bmap_add_extent_hole_delay(
2734 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002735 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002736 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002737 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002739 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2741 xfs_filblks_t newlen=0; /* new indirect size */
2742 xfs_filblks_t oldlen=0; /* old indirect size */
2743 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2744 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002745 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002747 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002749 if (whichfork == XFS_COW_FORK)
2750 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002751 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 /*
2754 * Check and set flags if this segment has a left neighbor
2755 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002756 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002757 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002758 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002759
2760 if (isnullstartblock(left.br_startblock))
2761 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 /*
2765 * Check and set flags if the current (right) segment exists.
2766 * If it doesn't exist, we're converting the hole at end-of-file.
2767 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002768 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002769 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002770 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002771
2772 if (isnullstartblock(right.br_startblock))
2773 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 /*
2777 * Set contiguity flags on the left and right neighbors.
2778 * Don't let extents get too large, even if the pieces are contiguous.
2779 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002780 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2781 left.br_startoff + left.br_blockcount == new->br_startoff &&
2782 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2783 state |= BMAP_LEFT_CONTIG;
2784
2785 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2786 new->br_startoff + new->br_blockcount == right.br_startoff &&
2787 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2788 (!(state & BMAP_LEFT_CONTIG) ||
2789 (left.br_blockcount + new->br_blockcount +
2790 right.br_blockcount <= MAXEXTLEN)))
2791 state |= BMAP_RIGHT_CONTIG;
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 /*
2794 * Switch out based on the contiguity flags.
2795 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002796 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2797 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 /*
2799 * New allocation is contiguous with delayed allocations
2800 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002801 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002803 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 temp = left.br_blockcount + new->br_blockcount +
2805 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002806
Christoph Hellwigec90c552011-05-23 08:52:53 +00002807 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2808 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002809 oldlen = startblockval(left.br_startblock) +
2810 startblockval(new->br_startblock) +
2811 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002813 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002814 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002815 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002816
Christoph Hellwigec90c552011-05-23 08:52:53 +00002817 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 break;
2819
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002820 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 /*
2822 * New allocation is contiguous with a delayed allocation
2823 * on the left.
2824 * Merge the new allocation with the left neighbor.
2825 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002826 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002828
2829 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2830 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002831 oldlen = startblockval(left.br_startblock) +
2832 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002834 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002835 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002836 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 break;
2838
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002839 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 /*
2841 * New allocation is contiguous with a delayed allocation
2842 * on the right.
2843 * Merge the new allocation with the right neighbor.
2844 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002845 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002847 oldlen = startblockval(new->br_startblock) +
2848 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002850 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2851 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002852 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002853 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 break;
2855
2856 case 0:
2857 /*
2858 * New allocation is not contiguous with another
2859 * delayed allocation.
2860 * Insert a new entry.
2861 */
2862 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002863 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 break;
2865 }
2866 if (oldlen != newlen) {
2867 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002868 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2869 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 /*
2871 * Nothing to do for disk quota accounting here.
2872 */
2873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874}
2875
2876/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002877 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 */
2879STATIC int /* error */
2880xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002881 struct xfs_bmalloca *bma,
2882 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002884 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 int i; /* temp state */
2887 xfs_ifork_t *ifp; /* inode fork pointer */
2888 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2889 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002890 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002892 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Eric Sandeenf1f96c42016-01-04 16:10:42 +11002894 mp = bma->ip->i_mount;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002895 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002896
Christoph Hellwigc6534242011-09-18 20:41:05 +00002897 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002898 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002899 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002900 ASSERT(!bma->cur ||
2901 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Darrick J. Wong3993bae2016-10-03 09:11:32 -07002902 ASSERT(whichfork != XFS_COW_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002903
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002904 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002905
2906 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002907 if (whichfork == XFS_ATTR_FORK)
2908 state |= BMAP_ATTRFORK;
2909
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 /*
4371 * A wasdelay extent has been initialized, so shouldn't be flagged
4372 * as unwritten.
4373 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004374 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004375 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004376 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004377
Christoph Hellwigc6534242011-09-18 20:41:05 +00004378 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004379 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004380 else
4381 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004382
Christoph Hellwigc315c902011-09-18 20:41:02 +00004383 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004384 if (error)
4385 return error;
4386
4387 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004388 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4389 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4390 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004391 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004392 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004393
Dave Chinner963c30c2011-09-18 20:40:59 +00004394 ASSERT(bma->got.br_startoff <= bma->offset);
4395 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4396 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004397 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4398 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004399 return 0;
4400}
4401
Dave Chinnerb447fe52011-09-18 20:40:51 +00004402STATIC int
4403xfs_bmapi_convert_unwritten(
4404 struct xfs_bmalloca *bma,
4405 struct xfs_bmbt_irec *mval,
4406 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004407 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004408{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004409 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004410 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004411 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004412 int error;
4413
Dave Chinnerb447fe52011-09-18 20:40:51 +00004414 /* check if we need to do unwritten->real conversion */
4415 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4416 (flags & XFS_BMAPI_PREALLOC))
4417 return 0;
4418
4419 /* check if we need to do real->unwritten conversion */
4420 if (mval->br_state == XFS_EXT_NORM &&
4421 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4422 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4423 return 0;
4424
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004425 ASSERT(whichfork != XFS_COW_FORK);
4426
Dave Chinnerb447fe52011-09-18 20:40:51 +00004427 /*
4428 * Modify (by adding) the state flag, if writing.
4429 */
4430 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004431 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4432 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004433 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004434 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004435 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004436 }
4437 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4438 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4439
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004440 /*
4441 * Before insertion into the bmbt, zero the range being converted
4442 * if required.
4443 */
4444 if (flags & XFS_BMAPI_ZERO) {
4445 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4446 mval->br_blockcount);
4447 if (error)
4448 return error;
4449 }
4450
Dave Chinnere0c3da52011-09-18 20:41:01 +00004451 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004452 &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004453 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004454 /*
4455 * Log the inode core unconditionally in the unwritten extent conversion
4456 * path because the conversion might not have done so (e.g., if the
4457 * extent count hasn't changed). We need to make sure the inode is dirty
4458 * in the transaction for the sake of fsync(), even if nothing has
4459 * changed, because fsync() will not force the log for this transaction
4460 * unless it sees the inode pinned.
4461 */
4462 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004463 if (error)
4464 return error;
4465
4466 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004467 * Update our extent pointer, given that
4468 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4469 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004470 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004471 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004472
4473 /*
4474 * We may have combined previously unwritten space with written space,
4475 * so generate another request.
4476 */
4477 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004478 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004479 return 0;
4480}
4481
Christoph Hellwig44032802011-09-18 20:40:48 +00004482/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004483 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4484 * extent state if necessary. Details behaviour is controlled by the flags
4485 * parameter. Only allocates blocks from a single allocation group, to avoid
4486 * locking problems.
4487 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 * The returned value in "firstblock" from the first call in a transaction
4489 * must be remembered and presented to subsequent calls in "firstblock".
4490 * An upper bound for the number of blocks to be allocated is supplied to
4491 * the first call in "total"; if no allocation group has that many free
4492 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4493 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004494int
4495xfs_bmapi_write(
4496 struct xfs_trans *tp, /* transaction pointer */
4497 struct xfs_inode *ip, /* incore inode */
4498 xfs_fileoff_t bno, /* starting file offs. mapped */
4499 xfs_filblks_t len, /* length to map in file */
4500 int flags, /* XFS_BMAPI_... */
4501 xfs_fsblock_t *firstblock, /* first allocated block
4502 controls a.g. for allocs */
4503 xfs_extlen_t total, /* total blocks needed */
4504 struct xfs_bmbt_irec *mval, /* output: map values */
4505 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004506 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004508 struct xfs_mount *mp = ip->i_mount;
4509 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004510 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004511 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004512 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004513 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004514 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004515 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004516 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004517
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004519 xfs_fileoff_t orig_bno; /* original block number value */
4520 int orig_flags; /* original flags arg value */
4521 xfs_filblks_t orig_len; /* original value of len arg */
4522 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4523 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
4525 orig_bno = bno;
4526 orig_len = len;
4527 orig_flags = flags;
4528 orig_mval = mval;
4529 orig_nmap = *nmap;
4530#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004531 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004532
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004534 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4535 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4536 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004537 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004538 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004539 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004540 ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK);
4541 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP));
4542 ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP));
Darrick J. Wong60b49842016-10-03 09:11:34 -07004543 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || whichfork != XFS_COW_FORK);
4544 ASSERT(!(flags & XFS_BMAPI_CONVERT) || whichfork != XFS_COW_FORK);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004545
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004546 /* zeroing is for currently only for data extents, not metadata */
4547 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4548 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4549 /*
4550 * we can allocate unwritten extents or pre-zero allocated blocks,
4551 * but it makes no sense to do both at once. This would result in
4552 * zeroing the unwritten extent twice, but it still being an
4553 * unwritten extent....
4554 */
4555 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4556 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 if (unlikely(XFS_TEST_ERROR(
4559 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004560 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004562 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004563 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004567 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004568
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004570
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004571 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004572
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004573 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004575 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004577 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004578 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004579 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004580 }
4581
4582 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4583 error = xfs_iread_extents(tp, ip, whichfork);
4584 if (error)
4585 goto error0;
4586 }
4587
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 n = 0;
4589 end = bno + len;
4590 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004591
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004592 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4593 eof = true;
4594 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4595 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004596 bma.tp = tp;
4597 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004598 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004599 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004600 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004601 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004604 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004605
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004606 /* in hole or beyoned EOF? */
4607 if (eof || bma.got.br_startoff > bno) {
4608 if (flags & XFS_BMAPI_DELALLOC) {
4609 /*
4610 * For the COW fork we can reasonably get a
4611 * request for converting an extent that races
4612 * with other threads already having converted
4613 * part of it, as there converting COW to
4614 * regular blocks is not protected using the
4615 * IOLOCK.
4616 */
4617 ASSERT(flags & XFS_BMAPI_COWFORK);
4618 if (!(flags & XFS_BMAPI_COWFORK)) {
4619 error = -EIO;
4620 goto error0;
4621 }
4622
4623 if (eof || bno >= end)
4624 break;
4625 } else {
4626 need_alloc = true;
4627 }
4628 } else {
4629 /*
4630 * Make sure we only reflink into a hole.
4631 */
4632 ASSERT(!(flags & XFS_BMAPI_REMAP));
4633 if (isnullstartblock(bma.got.br_startblock))
4634 wasdelay = true;
4635 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004636
4637 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 * First, deal with the hole before the allocated space
4639 * that we found, if any.
4640 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004641 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004642 bma.eof = eof;
4643 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4644 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004645 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004646 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004647
Dave Chinnera99ebf42011-12-01 11:24:20 +00004648 /*
4649 * There's a 32/64 bit type mismatch between the
4650 * allocation length request (which can be 64 bits in
4651 * length) and the bma length request, which is
4652 * xfs_extlen_t and therefore 32 bits. Hence we have to
4653 * check for 32-bit overflows and handle them here.
4654 */
4655 if (len > (xfs_filblks_t)MAXEXTLEN)
4656 bma.length = MAXEXTLEN;
4657 else
4658 bma.length = len;
4659
4660 ASSERT(len > 0);
4661 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004662 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 if (error)
4664 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004665 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004666 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004667
4668 /*
4669 * If this is a CoW allocation, record the data in
4670 * the refcount btree for orphan recovery.
4671 */
4672 if (whichfork == XFS_COW_FORK) {
4673 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4674 bma.blkno, bma.length);
4675 if (error)
4676 goto error0;
4677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004679
Dave Chinneraef9a892011-09-18 20:40:44 +00004680 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004681 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4682 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683
Dave Chinnerb447fe52011-09-18 20:40:51 +00004684 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004685 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004686 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004687 continue;
4688 if (error)
4689 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690
Dave Chinneraef9a892011-09-18 20:40:44 +00004691 /* update the extent map to return */
4692 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4693
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 /*
4695 * If we're done, stop now. Stop when we've allocated
4696 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4697 * the transaction may get too big.
4698 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004699 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004701
4702 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004703 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004704 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4705 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004708
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 /*
4710 * Transform from btree to extents, give it cur.
4711 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004712 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004713 int tmp_logflags = 0;
4714
Dave Chinner29c8d172011-09-18 20:41:00 +00004715 ASSERT(bma.cur);
4716 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004718 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 if (error)
4720 goto error0;
4721 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004722
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004724 XFS_IFORK_NEXTENTS(ip, whichfork) >
4725 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727error0:
4728 /*
4729 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004730 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004732 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004734 bma.logflags &= ~xfs_ilog_fext(whichfork);
4735 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004737 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 /*
4739 * Log whatever the flags say, even if error. Otherwise we might miss
4740 * detecting a case where the data is changed, there's an error,
4741 * and it's not logged so we don't shutdown when we should.
4742 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004743 if (bma.logflags)
4744 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004745
Dave Chinner29c8d172011-09-18 20:41:00 +00004746 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 if (!error) {
4748 ASSERT(*firstblock == NULLFSBLOCK ||
4749 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4750 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004751 bma.cur->bc_private.b.firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004752 (dfops->dop_low &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 XFS_FSB_TO_AGNO(mp, *firstblock) <
4754 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004755 bma.cur->bc_private.b.firstblock)));
4756 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004758 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4760 }
4761 if (!error)
4762 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4763 orig_nmap, *nmap);
4764 return error;
4765}
4766
4767/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004768 * When a delalloc extent is split (e.g., due to a hole punch), the original
4769 * indlen reservation must be shared across the two new extents that are left
4770 * behind.
4771 *
4772 * Given the original reservation and the worst case indlen for the two new
4773 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004774 * reservation fairly across the two new extents. If necessary, steal available
4775 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4776 * ores == 1). The number of stolen blocks is returned. The availability and
4777 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004778 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004779static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004780xfs_bmap_split_indlen(
4781 xfs_filblks_t ores, /* original res. */
4782 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004783 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4784 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004785{
4786 xfs_filblks_t len1 = *indlen1;
4787 xfs_filblks_t len2 = *indlen2;
4788 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004789 xfs_filblks_t stolen = 0;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004790
4791 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004792 * Steal as many blocks as we can to try and satisfy the worst case
4793 * indlen for both new extents.
4794 */
4795 while (nres > ores && avail) {
4796 nres--;
4797 avail--;
4798 stolen++;
4799 }
4800
4801 /*
4802 * The only blocks available are those reserved for the original
4803 * extent and what we can steal from the extent being removed.
4804 * If this still isn't enough to satisfy the combined
4805 * requirements for the two new extents, skim blocks off of each
4806 * of the new reservations until they match what is available.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004807 */
4808 while (nres > ores) {
4809 if (len1) {
4810 len1--;
4811 nres--;
4812 }
4813 if (nres == ores)
4814 break;
4815 if (len2) {
4816 len2--;
4817 nres--;
4818 }
4819 }
4820
4821 *indlen1 = len1;
4822 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004823
4824 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004825}
4826
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004827int
4828xfs_bmap_del_extent_delay(
4829 struct xfs_inode *ip,
4830 int whichfork,
4831 xfs_extnum_t *idx,
4832 struct xfs_bmbt_irec *got,
4833 struct xfs_bmbt_irec *del)
4834{
4835 struct xfs_mount *mp = ip->i_mount;
4836 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4837 struct xfs_bmbt_irec new;
4838 int64_t da_old, da_new, da_diff = 0;
4839 xfs_fileoff_t del_endoff, got_endoff;
4840 xfs_filblks_t got_indlen, new_indlen, stolen;
4841 int error = 0, state = 0;
4842 bool isrt;
4843
4844 XFS_STATS_INC(mp, xs_del_exlist);
4845
4846 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4847 del_endoff = del->br_startoff + del->br_blockcount;
4848 got_endoff = got->br_startoff + got->br_blockcount;
4849 da_old = startblockval(got->br_startblock);
4850 da_new = 0;
4851
4852 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004853 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004854 ASSERT(del->br_blockcount > 0);
4855 ASSERT(got->br_startoff <= del->br_startoff);
4856 ASSERT(got_endoff >= del_endoff);
4857
4858 if (isrt) {
4859 int64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4860
4861 do_div(rtexts, mp->m_sb.sb_rextsize);
4862 xfs_mod_frextents(mp, rtexts);
4863 }
4864
4865 /*
4866 * Update the inode delalloc counter now and wait to update the
4867 * sb counters as we might have to borrow some blocks for the
4868 * indirect block accounting.
4869 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004870 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4871 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004872 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004873 if (error)
4874 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004875 ip->i_delayed_blks -= del->br_blockcount;
4876
4877 if (whichfork == XFS_COW_FORK)
4878 state |= BMAP_COWFORK;
4879
4880 if (got->br_startoff == del->br_startoff)
4881 state |= BMAP_LEFT_CONTIG;
4882 if (got_endoff == del_endoff)
4883 state |= BMAP_RIGHT_CONTIG;
4884
4885 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4886 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4887 /*
4888 * Matches the whole extent. Delete the entry.
4889 */
4890 xfs_iext_remove(ip, *idx, 1, state);
4891 --*idx;
4892 break;
4893 case BMAP_LEFT_CONTIG:
4894 /*
4895 * Deleting the first part of the extent.
4896 */
4897 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4898 got->br_startoff = del_endoff;
4899 got->br_blockcount -= del->br_blockcount;
4900 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4901 got->br_blockcount), da_old);
4902 got->br_startblock = nullstartblock((int)da_new);
4903 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4904 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4905 break;
4906 case BMAP_RIGHT_CONTIG:
4907 /*
4908 * Deleting the last part of the extent.
4909 */
4910 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4911 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4912 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4913 got->br_blockcount), da_old);
4914 got->br_startblock = nullstartblock((int)da_new);
4915 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4916 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4917 break;
4918 case 0:
4919 /*
4920 * Deleting the middle of the extent.
4921 *
4922 * Distribute the original indlen reservation across the two new
4923 * extents. Steal blocks from the deleted extent if necessary.
4924 * Stealing blocks simply fudges the fdblocks accounting below.
4925 * Warn if either of the new indlen reservations is zero as this
4926 * can lead to delalloc problems.
4927 */
4928 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4929
4930 got->br_blockcount = del->br_startoff - got->br_startoff;
4931 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4932
4933 new.br_blockcount = got_endoff - del_endoff;
4934 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4935
4936 WARN_ON_ONCE(!got_indlen || !new_indlen);
4937 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4938 del->br_blockcount);
4939
4940 got->br_startblock = nullstartblock((int)got_indlen);
4941 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4942 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4943
4944 new.br_startoff = del_endoff;
4945 new.br_state = got->br_state;
4946 new.br_startblock = nullstartblock((int)new_indlen);
4947
4948 ++*idx;
4949 xfs_iext_insert(ip, *idx, 1, &new, state);
4950
4951 da_new = got_indlen + new_indlen - stolen;
4952 del->br_blockcount -= stolen;
4953 break;
4954 }
4955
4956 ASSERT(da_old >= da_new);
4957 da_diff = da_old - da_new;
4958 if (!isrt)
4959 da_diff += del->br_blockcount;
4960 if (da_diff)
4961 xfs_mod_fdblocks(mp, da_diff, false);
4962 return error;
4963}
4964
4965void
4966xfs_bmap_del_extent_cow(
4967 struct xfs_inode *ip,
4968 xfs_extnum_t *idx,
4969 struct xfs_bmbt_irec *got,
4970 struct xfs_bmbt_irec *del)
4971{
4972 struct xfs_mount *mp = ip->i_mount;
4973 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4974 struct xfs_bmbt_irec new;
4975 xfs_fileoff_t del_endoff, got_endoff;
4976 int state = BMAP_COWFORK;
4977
4978 XFS_STATS_INC(mp, xs_del_exlist);
4979
4980 del_endoff = del->br_startoff + del->br_blockcount;
4981 got_endoff = got->br_startoff + got->br_blockcount;
4982
4983 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004984 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004985 ASSERT(del->br_blockcount > 0);
4986 ASSERT(got->br_startoff <= del->br_startoff);
4987 ASSERT(got_endoff >= del_endoff);
4988 ASSERT(!isnullstartblock(got->br_startblock));
4989
4990 if (got->br_startoff == del->br_startoff)
4991 state |= BMAP_LEFT_CONTIG;
4992 if (got_endoff == del_endoff)
4993 state |= BMAP_RIGHT_CONTIG;
4994
4995 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4996 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4997 /*
4998 * Matches the whole extent. Delete the entry.
4999 */
5000 xfs_iext_remove(ip, *idx, 1, state);
5001 --*idx;
5002 break;
5003 case BMAP_LEFT_CONTIG:
5004 /*
5005 * Deleting the first part of the extent.
5006 */
5007 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5008 got->br_startoff = del_endoff;
5009 got->br_blockcount -= del->br_blockcount;
5010 got->br_startblock = del->br_startblock + del->br_blockcount;
5011 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5012 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5013 break;
5014 case BMAP_RIGHT_CONTIG:
5015 /*
5016 * Deleting the last part of the extent.
5017 */
5018 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5019 got->br_blockcount -= del->br_blockcount;
5020 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5021 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5022 break;
5023 case 0:
5024 /*
5025 * Deleting the middle of the extent.
5026 */
5027 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5028 got->br_blockcount = del->br_startoff - got->br_startoff;
5029 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5030 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5031
5032 new.br_startoff = del_endoff;
5033 new.br_blockcount = got_endoff - del_endoff;
5034 new.br_state = got->br_state;
5035 new.br_startblock = del->br_startblock + del->br_blockcount;
5036
5037 ++*idx;
5038 xfs_iext_insert(ip, *idx, 1, &new, state);
5039 break;
5040 }
5041}
5042
Brian Fostera9bd24a2016-03-15 11:42:46 +11005043/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005044 * Called by xfs_bmapi to update file extent records and the btree
5045 * after removing space (or undoing a delayed allocation).
5046 */
5047STATIC int /* error */
5048xfs_bmap_del_extent(
5049 xfs_inode_t *ip, /* incore inode pointer */
5050 xfs_trans_t *tp, /* current transaction pointer */
5051 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005052 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005053 xfs_btree_cur_t *cur, /* if null, not a btree */
5054 xfs_bmbt_irec_t *del, /* data to remove from extents */
5055 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005056 int whichfork, /* data or attr fork */
5057 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005058{
5059 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5060 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5061 xfs_fsblock_t del_endblock=0; /* first block past del */
5062 xfs_fileoff_t del_endoff; /* first offset past del */
5063 int delay; /* current block is delayed allocated */
5064 int do_fx; /* free extent at end of routine */
5065 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5066 int error; /* error return value */
5067 int flags; /* inode logging flags */
5068 xfs_bmbt_irec_t got; /* current extent entry */
5069 xfs_fileoff_t got_endoff; /* first offset past got */
5070 int i; /* temp state */
5071 xfs_ifork_t *ifp; /* inode fork pointer */
5072 xfs_mount_t *mp; /* mount structure */
5073 xfs_filblks_t nblks; /* quota/sb block count */
5074 xfs_bmbt_irec_t new; /* new record to be inserted */
5075 /* REFERENCED */
5076 uint qfield; /* quota field to update */
5077 xfs_filblks_t temp; /* for indirect length calculations */
5078 xfs_filblks_t temp2; /* for indirect length calculations */
5079 int state = 0;
5080
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005081 mp = ip->i_mount;
5082 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005083
5084 if (whichfork == XFS_ATTR_FORK)
5085 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005086 else if (whichfork == XFS_COW_FORK)
5087 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005088
Dave Chinner9e5987a2013-02-25 12:31:26 +11005089 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005090 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005091 ASSERT(del->br_blockcount > 0);
5092 ep = xfs_iext_get_ext(ifp, *idx);
5093 xfs_bmbt_get_all(ep, &got);
5094 ASSERT(got.br_startoff <= del->br_startoff);
5095 del_endoff = del->br_startoff + del->br_blockcount;
5096 got_endoff = got.br_startoff + got.br_blockcount;
5097 ASSERT(got_endoff >= del_endoff);
5098 delay = isnullstartblock(got.br_startblock);
5099 ASSERT(isnullstartblock(del->br_startblock) == delay);
5100 flags = 0;
5101 qfield = 0;
5102 error = 0;
5103 /*
5104 * If deleting a real allocation, must free up the disk space.
5105 */
5106 if (!delay) {
5107 flags = XFS_ILOG_CORE;
5108 /*
5109 * Realtime allocation. Free it and record di_nblocks update.
5110 */
5111 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5112 xfs_fsblock_t bno;
5113 xfs_filblks_t len;
5114
5115 ASSERT(do_mod(del->br_blockcount,
5116 mp->m_sb.sb_rextsize) == 0);
5117 ASSERT(do_mod(del->br_startblock,
5118 mp->m_sb.sb_rextsize) == 0);
5119 bno = del->br_startblock;
5120 len = del->br_blockcount;
5121 do_div(bno, mp->m_sb.sb_rextsize);
5122 do_div(len, mp->m_sb.sb_rextsize);
5123 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5124 if (error)
5125 goto done;
5126 do_fx = 0;
5127 nblks = len * mp->m_sb.sb_rextsize;
5128 qfield = XFS_TRANS_DQ_RTBCOUNT;
5129 }
5130 /*
5131 * Ordinary allocation.
5132 */
5133 else {
5134 do_fx = 1;
5135 nblks = del->br_blockcount;
5136 qfield = XFS_TRANS_DQ_BCOUNT;
5137 }
5138 /*
5139 * Set up del_endblock and cur for later.
5140 */
5141 del_endblock = del->br_startblock + del->br_blockcount;
5142 if (cur) {
5143 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5144 got.br_startblock, got.br_blockcount,
5145 &i)))
5146 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005147 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005148 }
5149 da_old = da_new = 0;
5150 } else {
5151 da_old = startblockval(got.br_startblock);
5152 da_new = 0;
5153 nblks = 0;
5154 do_fx = 0;
5155 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005156
Dave Chinner9e5987a2013-02-25 12:31:26 +11005157 /*
5158 * Set flag value to use in switch statement.
5159 * Left-contig is 2, right-contig is 1.
5160 */
5161 switch (((got.br_startoff == del->br_startoff) << 1) |
5162 (got_endoff == del_endoff)) {
5163 case 3:
5164 /*
5165 * Matches the whole extent. Delete the entry.
5166 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005167 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005168 xfs_iext_remove(ip, *idx, 1,
5169 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5170 --*idx;
5171 if (delay)
5172 break;
5173
5174 XFS_IFORK_NEXT_SET(ip, whichfork,
5175 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5176 flags |= XFS_ILOG_CORE;
5177 if (!cur) {
5178 flags |= xfs_ilog_fext(whichfork);
5179 break;
5180 }
5181 if ((error = xfs_btree_delete(cur, &i)))
5182 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005183 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005184 break;
5185
5186 case 2:
5187 /*
5188 * Deleting the first part of the extent.
5189 */
5190 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5191 xfs_bmbt_set_startoff(ep, del_endoff);
5192 temp = got.br_blockcount - del->br_blockcount;
5193 xfs_bmbt_set_blockcount(ep, temp);
5194 if (delay) {
5195 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5196 da_old);
5197 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5198 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5199 da_new = temp;
5200 break;
5201 }
5202 xfs_bmbt_set_startblock(ep, del_endblock);
5203 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5204 if (!cur) {
5205 flags |= xfs_ilog_fext(whichfork);
5206 break;
5207 }
5208 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5209 got.br_blockcount - del->br_blockcount,
5210 got.br_state)))
5211 goto done;
5212 break;
5213
5214 case 1:
5215 /*
5216 * Deleting the last part of the extent.
5217 */
5218 temp = got.br_blockcount - del->br_blockcount;
5219 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5220 xfs_bmbt_set_blockcount(ep, temp);
5221 if (delay) {
5222 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5223 da_old);
5224 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5225 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5226 da_new = temp;
5227 break;
5228 }
5229 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5230 if (!cur) {
5231 flags |= xfs_ilog_fext(whichfork);
5232 break;
5233 }
5234 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5235 got.br_startblock,
5236 got.br_blockcount - del->br_blockcount,
5237 got.br_state)))
5238 goto done;
5239 break;
5240
5241 case 0:
5242 /*
5243 * Deleting the middle of the extent.
5244 */
5245 temp = del->br_startoff - got.br_startoff;
5246 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5247 xfs_bmbt_set_blockcount(ep, temp);
5248 new.br_startoff = del_endoff;
5249 temp2 = got_endoff - del_endoff;
5250 new.br_blockcount = temp2;
5251 new.br_state = got.br_state;
5252 if (!delay) {
5253 new.br_startblock = del_endblock;
5254 flags |= XFS_ILOG_CORE;
5255 if (cur) {
5256 if ((error = xfs_bmbt_update(cur,
5257 got.br_startoff,
5258 got.br_startblock, temp,
5259 got.br_state)))
5260 goto done;
5261 if ((error = xfs_btree_increment(cur, 0, &i)))
5262 goto done;
5263 cur->bc_rec.b = new;
5264 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10005265 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005266 goto done;
5267 /*
5268 * If get no-space back from btree insert,
5269 * it tried a split, and we have a zero
5270 * block reservation.
5271 * Fix up our state and return the error.
5272 */
Dave Chinner24513372014-06-25 14:58:08 +10005273 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11005274 /*
5275 * Reset the cursor, don't trust
5276 * it after any insert operation.
5277 */
5278 if ((error = xfs_bmbt_lookup_eq(cur,
5279 got.br_startoff,
5280 got.br_startblock,
5281 temp, &i)))
5282 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005283 XFS_WANT_CORRUPTED_GOTO(mp,
5284 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005285 /*
5286 * Update the btree record back
5287 * to the original value.
5288 */
5289 if ((error = xfs_bmbt_update(cur,
5290 got.br_startoff,
5291 got.br_startblock,
5292 got.br_blockcount,
5293 got.br_state)))
5294 goto done;
5295 /*
5296 * Reset the extent record back
5297 * to the original value.
5298 */
5299 xfs_bmbt_set_blockcount(ep,
5300 got.br_blockcount);
5301 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005302 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005303 goto done;
5304 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005305 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005306 } else
5307 flags |= xfs_ilog_fext(whichfork);
5308 XFS_IFORK_NEXT_SET(ip, whichfork,
5309 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5310 } else {
Brian Fosterd34999c92016-03-15 11:42:47 +11005311 xfs_filblks_t stolen;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005312 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005313
5314 /*
5315 * Distribute the original indlen reservation across the
Brian Fosterd34999c92016-03-15 11:42:47 +11005316 * two new extents. Steal blocks from the deleted extent
5317 * if necessary. Stealing blocks simply fudges the
5318 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005319 */
5320 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5321 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c92016-03-15 11:42:47 +11005322 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5323 del->br_blockcount);
5324 da_new = temp + temp2 - stolen;
5325 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005326
5327 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11005328 * Set the reservation for each extent. Warn if either
5329 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005330 */
Brian Fosterd34999c92016-03-15 11:42:47 +11005331 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005332 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005333 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005334 }
5335 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5336 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5337 ++*idx;
5338 break;
5339 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005340
5341 /* remove reverse mapping */
5342 if (!delay) {
5343 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5344 if (error)
5345 goto done;
5346 }
5347
Dave Chinner9e5987a2013-02-25 12:31:26 +11005348 /*
5349 * If we need to, add to list of extents to delete.
5350 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005351 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005352 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5353 error = xfs_refcount_decrease_extent(mp, dfops, del);
5354 if (error)
5355 goto done;
5356 } else
5357 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5358 del->br_blockcount, NULL);
5359 }
5360
Dave Chinner9e5987a2013-02-25 12:31:26 +11005361 /*
5362 * Adjust inode # blocks in the file.
5363 */
5364 if (nblks)
5365 ip->i_d.di_nblocks -= nblks;
5366 /*
5367 * Adjust quota data.
5368 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005369 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005370 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5371
5372 /*
5373 * Account for change in delayed indirect blocks.
5374 * Nothing to do for disk quota accounting here.
5375 */
5376 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005377 if (da_old > da_new)
5378 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005379done:
5380 *logflagsp = flags;
5381 return error;
5382}
5383
5384/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 * Unmap (remove) blocks from a file.
5386 * If nexts is nonzero then the number of extents to remove is limited to
5387 * that value. If not all extents in the block range can be removed then
5388 * *done is set.
5389 */
5390int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005391__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392 xfs_trans_t *tp, /* transaction pointer */
5393 struct xfs_inode *ip, /* incore inode */
5394 xfs_fileoff_t bno, /* starting offset to unmap */
Darrick J. Wong44535932016-10-03 09:11:29 -07005395 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396 int flags, /* misc flags */
5397 xfs_extnum_t nexts, /* number of extents max */
5398 xfs_fsblock_t *firstblock, /* first allocated block
5399 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005400 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401{
5402 xfs_btree_cur_t *cur; /* bmap btree cursor */
5403 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 int error; /* error return value */
5405 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005406 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 xfs_ifork_t *ifp; /* inode fork pointer */
5408 int isrt; /* freeing in rt area */
5409 xfs_extnum_t lastx; /* last extent index used */
5410 int logflags; /* transaction logging flags */
5411 xfs_extlen_t mod; /* rt extent offset */
5412 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 xfs_fileoff_t start; /* first file offset deleted */
5414 int tmp_logflags; /* partial logging flags */
5415 int wasdel; /* was a delayed alloc extent */
5416 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005418 xfs_filblks_t len = *rlen; /* length to unmap in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005420 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5421
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005422 whichfork = xfs_bmapi_whichfork(flags);
5423 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 ifp = XFS_IFORK_PTR(ip, whichfork);
5425 if (unlikely(
5426 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5427 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5428 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5429 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005430 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 }
5432 mp = ip->i_mount;
5433 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005434 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005435
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005436 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 ASSERT(len > 0);
5438 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005439
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5441 (error = xfs_iread_extents(tp, ip, whichfork)))
5442 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005443 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005444 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 return 0;
5446 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005447 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005448 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 start = bno;
5450 bno = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005451
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 /*
5453 * Check to see if the given block number is past the end of the
5454 * file, back up to the last block if so...
5455 */
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005456 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5457 ASSERT(lastx > 0);
5458 xfs_iext_get_extent(ifp, --lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 bno = got.br_startoff + got.br_blockcount - 1;
5460 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005461
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 logflags = 0;
5463 if (ifp->if_flags & XFS_IFBROOT) {
5464 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005465 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005467 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 cur->bc_private.b.flags = 0;
5469 } else
5470 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005471
5472 if (isrt) {
5473 /*
5474 * Synchronize by locking the bitmap inode.
5475 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005476 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005477 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005478 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5479 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005480 }
5481
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 extno = 0;
5483 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5484 (nexts == 0 || extno < nexts)) {
5485 /*
5486 * Is the found extent after a hole in which bno lives?
5487 * Just back up to the previous extent, if so.
5488 */
5489 if (got.br_startoff > bno) {
5490 if (--lastx < 0)
5491 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005492 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 }
5494 /*
5495 * Is the last block of this extent before the range
5496 * we're supposed to delete? If so, we're done.
5497 */
5498 bno = XFS_FILEOFF_MIN(bno,
5499 got.br_startoff + got.br_blockcount - 1);
5500 if (bno < start)
5501 break;
5502 /*
5503 * Then deal with the (possibly delayed) allocated space
5504 * we found.
5505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005506 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005507 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 if (got.br_startoff < start) {
5509 del.br_startoff = start;
5510 del.br_blockcount -= start - got.br_startoff;
5511 if (!wasdel)
5512 del.br_startblock += start - got.br_startoff;
5513 }
5514 if (del.br_startoff + del.br_blockcount > bno + 1)
5515 del.br_blockcount = bno + 1 - del.br_startoff;
5516 sum = del.br_startblock + del.br_blockcount;
5517 if (isrt &&
5518 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5519 /*
5520 * Realtime extent not lined up at the end.
5521 * The extent could have been split into written
5522 * and unwritten pieces, or we could just be
5523 * unmapping part of it. But we can't really
5524 * get rid of part of a realtime extent.
5525 */
5526 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005527 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 /*
5529 * This piece is unwritten, or we're not
5530 * using unwritten extents. Skip over it.
5531 */
5532 ASSERT(bno >= mod);
5533 bno -= mod > del.br_blockcount ?
5534 del.br_blockcount : mod;
5535 if (bno < got.br_startoff) {
5536 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005537 xfs_bmbt_get_all(xfs_iext_get_ext(
5538 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 }
5540 continue;
5541 }
5542 /*
5543 * It's written, turn it unwritten.
5544 * This is better than zeroing it.
5545 */
5546 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005547 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 /*
5549 * If this spans a realtime extent boundary,
5550 * chop it back to the start of the one we end at.
5551 */
5552 if (del.br_blockcount > mod) {
5553 del.br_startoff += del.br_blockcount - mod;
5554 del.br_startblock += del.br_blockcount - mod;
5555 del.br_blockcount = mod;
5556 }
5557 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005558 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005559 &lastx, &cur, &del, firstblock, dfops,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005560 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 if (error)
5562 goto error0;
5563 goto nodelete;
5564 }
5565 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5566 /*
5567 * Realtime extent is lined up at the end but not
5568 * at the front. We'll get rid of full extents if
5569 * we can.
5570 */
5571 mod = mp->m_sb.sb_rextsize - mod;
5572 if (del.br_blockcount > mod) {
5573 del.br_blockcount -= mod;
5574 del.br_startoff += mod;
5575 del.br_startblock += mod;
5576 } else if ((del.br_startoff == start &&
5577 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005578 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005579 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580 /*
5581 * Can't make it unwritten. There isn't
5582 * a full extent here so just skip it.
5583 */
5584 ASSERT(bno >= del.br_blockcount);
5585 bno -= del.br_blockcount;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005586 if (got.br_startoff > bno && --lastx >= 0)
5587 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 continue;
5589 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005590 struct xfs_bmbt_irec prev;
5591
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 /*
5593 * This one is already unwritten.
5594 * It must have a written left neighbor.
5595 * Unwrite the killed part of that one and
5596 * try again.
5597 */
5598 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005599 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005601 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 ASSERT(del.br_startblock ==
5603 prev.br_startblock + prev.br_blockcount);
5604 if (prev.br_startoff < start) {
5605 mod = start - prev.br_startoff;
5606 prev.br_blockcount -= mod;
5607 prev.br_startblock += mod;
5608 prev.br_startoff = start;
5609 }
5610 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005611 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005612 error = xfs_bmap_add_extent_unwritten_real(tp,
5613 ip, &lastx, &cur, &prev,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005614 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 if (error)
5616 goto error0;
5617 goto nodelete;
5618 } else {
5619 ASSERT(del.br_state == XFS_EXT_NORM);
5620 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005621 error = xfs_bmap_add_extent_unwritten_real(tp,
5622 ip, &lastx, &cur, &del,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005623 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 if (error)
5625 goto error0;
5626 goto nodelete;
5627 }
5628 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005629
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 /*
5631 * If it's the case where the directory code is running
5632 * with no block reservation, and the deleted block is in
5633 * the middle of its extent, and the resulting insert
5634 * of an extent would cause transformation to btree format,
5635 * then reject it. The calling code will then swap
5636 * blocks around instead.
5637 * We have to do this now, rather than waiting for the
5638 * conversion to btree format, since the transaction
5639 * will be dirty.
5640 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005641 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005643 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5644 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 del.br_startoff > got.br_startoff &&
5646 del.br_startoff + del.br_blockcount <
5647 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005648 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 goto error0;
5650 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005651
5652 /*
5653 * Unreserve quota and update realtime free space, if
5654 * appropriate. If delayed allocation, update the inode delalloc
5655 * counter now and wait to update the sb counters as
5656 * xfs_bmap_del_extent() might need to borrow some blocks.
5657 */
5658 if (wasdel) {
5659 ASSERT(startblockval(del.br_startblock) > 0);
5660 if (isrt) {
5661 xfs_filblks_t rtexts;
5662
5663 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5664 do_div(rtexts, mp->m_sb.sb_rextsize);
5665 xfs_mod_frextents(mp, (int64_t)rtexts);
5666 (void)xfs_trans_reserve_quota_nblks(NULL,
5667 ip, -((long)del.br_blockcount), 0,
5668 XFS_QMOPT_RES_RTBLKS);
5669 } else {
5670 (void)xfs_trans_reserve_quota_nblks(NULL,
5671 ip, -((long)del.br_blockcount), 0,
5672 XFS_QMOPT_RES_REGBLKS);
5673 }
5674 ip->i_delayed_blks -= del.br_blockcount;
5675 if (cur)
5676 cur->bc_private.b.flags |=
5677 XFS_BTCUR_BPRV_WASDEL;
5678 } else if (cur)
5679 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5680
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005681 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005682 &tmp_logflags, whichfork, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 logflags |= tmp_logflags;
5684 if (error)
5685 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005686
5687 if (!isrt && wasdel)
5688 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5689
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 bno = del.br_startoff - 1;
5691nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 /*
5693 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005696 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005697 xfs_iext_get_extent(ifp, lastx, &got);
5698 if (got.br_startoff > bno && --lastx >= 0)
5699 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 extno++;
5702 }
5703 }
Darrick J. Wong44535932016-10-03 09:11:29 -07005704 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5705 *rlen = 0;
5706 else
5707 *rlen = bno - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005708
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 /*
5710 * Convert to a btree if necessary.
5711 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005712 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005714 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 &cur, 0, &tmp_logflags, whichfork);
5716 logflags |= tmp_logflags;
5717 if (error)
5718 goto error0;
5719 }
5720 /*
5721 * transform from btree to extents, give it cur
5722 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005723 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 ASSERT(cur != NULL);
5725 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5726 whichfork);
5727 logflags |= tmp_logflags;
5728 if (error)
5729 goto error0;
5730 }
5731 /*
5732 * transform from extents to local?
5733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 error = 0;
5735error0:
5736 /*
5737 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005738 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005740 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005742 logflags &= ~xfs_ilog_fext(whichfork);
5743 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005745 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 /*
5747 * Log inode even in the error case, if the transaction
5748 * is dirty we'll need to shut down the filesystem.
5749 */
5750 if (logflags)
5751 xfs_trans_log_inode(tp, ip, logflags);
5752 if (cur) {
5753 if (!error) {
5754 *firstblock = cur->bc_private.b.firstblock;
5755 cur->bc_private.b.allocated = 0;
5756 }
5757 xfs_btree_del_cursor(cur,
5758 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5759 }
5760 return error;
5761}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005762
Darrick J. Wong44535932016-10-03 09:11:29 -07005763/* Unmap a range of a file. */
5764int
5765xfs_bunmapi(
5766 xfs_trans_t *tp,
5767 struct xfs_inode *ip,
5768 xfs_fileoff_t bno,
5769 xfs_filblks_t len,
5770 int flags,
5771 xfs_extnum_t nexts,
5772 xfs_fsblock_t *firstblock,
5773 struct xfs_defer_ops *dfops,
5774 int *done)
5775{
5776 int error;
5777
5778 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5779 dfops);
5780 *done = (len == 0);
5781 return error;
5782}
5783
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005784/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005785 * Determine whether an extent shift can be accomplished by a merge with the
5786 * extent that precedes the target hole of the shift.
5787 */
5788STATIC bool
5789xfs_bmse_can_merge(
5790 struct xfs_bmbt_irec *left, /* preceding extent */
5791 struct xfs_bmbt_irec *got, /* current extent to shift */
5792 xfs_fileoff_t shift) /* shift fsb */
5793{
5794 xfs_fileoff_t startoff;
5795
5796 startoff = got->br_startoff - shift;
5797
5798 /*
5799 * The extent, once shifted, must be adjacent in-file and on-disk with
5800 * the preceding extent.
5801 */
5802 if ((left->br_startoff + left->br_blockcount != startoff) ||
5803 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5804 (left->br_state != got->br_state) ||
5805 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5806 return false;
5807
5808 return true;
5809}
5810
5811/*
5812 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5813 * hole in the file. If an extent shift would result in the extent being fully
5814 * adjacent to the extent that currently precedes the hole, we can merge with
5815 * the preceding extent rather than do the shift.
5816 *
5817 * This function assumes the caller has verified a shift-by-merge is possible
5818 * with the provided extents via xfs_bmse_can_merge().
5819 */
5820STATIC int
5821xfs_bmse_merge(
5822 struct xfs_inode *ip,
5823 int whichfork,
5824 xfs_fileoff_t shift, /* shift fsb */
5825 int current_ext, /* idx of gotp */
5826 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5827 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5828 struct xfs_btree_cur *cur,
5829 int *logflags) /* output */
5830{
Brian Fosterddb19e32014-09-23 15:38:09 +10005831 struct xfs_bmbt_irec got;
5832 struct xfs_bmbt_irec left;
5833 xfs_filblks_t blockcount;
5834 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005835 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005836
Brian Fosterddb19e32014-09-23 15:38:09 +10005837 xfs_bmbt_get_all(gotp, &got);
5838 xfs_bmbt_get_all(leftp, &left);
5839 blockcount = left.br_blockcount + got.br_blockcount;
5840
5841 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5842 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5843 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5844
5845 /*
5846 * Merge the in-core extents. Note that the host record pointers and
5847 * current_ext index are invalid once the extent has been removed via
5848 * xfs_iext_remove().
5849 */
5850 xfs_bmbt_set_blockcount(leftp, blockcount);
5851 xfs_iext_remove(ip, current_ext, 1, 0);
5852
5853 /*
5854 * Update the on-disk extent count, the btree if necessary and log the
5855 * inode.
5856 */
5857 XFS_IFORK_NEXT_SET(ip, whichfork,
5858 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5859 *logflags |= XFS_ILOG_CORE;
5860 if (!cur) {
5861 *logflags |= XFS_ILOG_DEXT;
5862 return 0;
5863 }
5864
5865 /* lookup and remove the extent to merge */
5866 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5867 got.br_blockcount, &i);
5868 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005869 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005870 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005871
5872 error = xfs_btree_delete(cur, &i);
5873 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005874 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005875 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005876
5877 /* lookup and update size of the previous extent */
5878 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5879 left.br_blockcount, &i);
5880 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005881 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005882 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005883
5884 left.br_blockcount = blockcount;
5885
Dave Chinner4db431f2014-12-04 09:42:40 +11005886 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5887 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005888}
5889
5890/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005891 * Shift a single extent.
5892 */
5893STATIC int
5894xfs_bmse_shift_one(
5895 struct xfs_inode *ip,
5896 int whichfork,
5897 xfs_fileoff_t offset_shift_fsb,
5898 int *current_ext,
5899 struct xfs_bmbt_rec_host *gotp,
5900 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005901 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005902 enum shift_direction direction,
5903 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005904{
5905 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005906 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005907 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005908 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005909 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005910 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005911 int error;
5912 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005913 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005914
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005915 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005916 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005917 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005918
5919 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005920
Brian Fosterf71721d2014-09-23 15:39:05 +10005921 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005922 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005923
Namjae Jeona904b1c2015-03-25 15:08:56 +11005924 if (direction == SHIFT_LEFT) {
5925 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005926
Namjae Jeona904b1c2015-03-25 15:08:56 +11005927 /*
5928 * Check for merge if we've got an extent to the left,
5929 * otherwise make sure there's enough room at the start
5930 * of the file for the shift.
5931 */
5932 if (!*current_ext) {
5933 if (got.br_startoff < offset_shift_fsb)
5934 return -EINVAL;
5935 goto update_current_ext;
5936 }
5937 /*
5938 * grab the left extent and check for a large
5939 * enough hole.
5940 */
5941 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5942 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5943
5944 if (startoff <
5945 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005946 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005947
Dave Chinnerb11bd672014-12-04 09:42:24 +11005948 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005949 if (xfs_bmse_can_merge(&adj_irec, &got,
5950 offset_shift_fsb)) {
Darrick J. Wong9c194642016-08-03 12:16:05 +10005951 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5952 *current_ext, gotp, adj_irecp,
5953 cur, logflags);
5954 if (error)
5955 return error;
5956 adj_irec = got;
5957 goto update_rmap;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005958 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005959 } else {
5960 startoff = got.br_startoff + offset_shift_fsb;
5961 /* nothing to move if this is the last extent */
5962 if (*current_ext >= (total_extents - 1))
5963 goto update_current_ext;
5964 /*
5965 * If this is not the last extent in the file, make sure there
5966 * is enough room between current extent and next extent for
5967 * accommodating the shift.
5968 */
5969 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5970 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5971 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5972 return -EINVAL;
5973 /*
5974 * Unlike a left shift (which involves a hole punch),
5975 * a right shift does not modify extent neighbors
5976 * in any way. We should never find mergeable extents
5977 * in this scenario. Check anyways and warn if we
5978 * encounter two extents that could be one.
5979 */
5980 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5981 WARN_ON_ONCE(1);
5982 }
Brian Fostera979bdf2014-09-23 15:39:04 +10005983 /*
5984 * Increment the extent index for the next iteration, update the start
5985 * offset of the in-core extent and update the btree if applicable.
5986 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005987update_current_ext:
5988 if (direction == SHIFT_LEFT)
5989 (*current_ext)++;
5990 else
5991 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005992 xfs_bmbt_set_startoff(gotp, startoff);
5993 *logflags |= XFS_ILOG_CORE;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005994 adj_irec = got;
Brian Fostera979bdf2014-09-23 15:39:04 +10005995 if (!cur) {
5996 *logflags |= XFS_ILOG_DEXT;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005997 goto update_rmap;
Brian Fostera979bdf2014-09-23 15:39:04 +10005998 }
5999
6000 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
6001 got.br_blockcount, &i);
6002 if (error)
6003 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11006004 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10006005
6006 got.br_startoff = startoff;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006007 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
6008 got.br_blockcount, got.br_state);
6009 if (error)
6010 return error;
6011
6012update_rmap:
6013 /* update reverse mapping */
6014 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
6015 if (error)
6016 return error;
6017 adj_irec.br_startoff = startoff;
6018 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
Brian Fostera979bdf2014-09-23 15:39:04 +10006019}
6020
6021/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11006022 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006023 *
Brian Foster2c845f52014-09-23 15:37:09 +10006024 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11006025 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10006026 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6027 * is the length by which each extent is shifted. If there is no hole to shift
6028 * the extents into, this will be considered invalid operation and we abort
6029 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006030 */
6031int
6032xfs_bmap_shift_extents(
6033 struct xfs_trans *tp,
6034 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006035 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006036 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10006037 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006038 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006039 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006040 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006041 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006042 int num_exts)
6043{
Brian Fosterca446d82014-09-02 12:12:53 +10006044 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006045 struct xfs_bmbt_rec_host *gotp;
6046 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006047 struct xfs_mount *mp = ip->i_mount;
6048 struct xfs_ifork *ifp;
6049 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10006050 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006051 xfs_extnum_t total_extents;
6052 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006053 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006054 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10006055 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006056
6057 if (unlikely(XFS_TEST_ERROR(
6058 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6059 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6060 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6061 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6062 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10006063 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006064 }
6065
6066 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10006067 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006068
Brian Foster2c845f52014-09-23 15:37:09 +10006069 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6070 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006071 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6072 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006073
6074 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006075 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6076 /* Read in all the extents */
6077 error = xfs_iread_extents(tp, ip, whichfork);
6078 if (error)
6079 return error;
6080 }
6081
Brian Fosterddb19e32014-09-23 15:38:09 +10006082 if (ifp->if_flags & XFS_IFBROOT) {
6083 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6084 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006085 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006086 cur->bc_private.b.flags = 0;
6087 }
6088
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006089 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006090 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006091 * are collapsing out, so we cannot use the count of real extents here.
6092 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006093 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006094 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006095 if (total_extents == 0) {
6096 *done = 1;
6097 goto del_cursor;
6098 }
6099
6100 /*
6101 * In case of first right shift, we need to initialize next_fsb
6102 */
6103 if (*next_fsb == NULLFSBLOCK) {
6104 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6105 xfs_bmbt_get_all(gotp, &got);
6106 *next_fsb = got.br_startoff;
6107 if (stop_fsb > *next_fsb) {
6108 *done = 1;
6109 goto del_cursor;
6110 }
6111 }
6112
6113 /* Lookup the extent index at which we have to stop */
6114 if (direction == SHIFT_RIGHT) {
6115 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6116 /* Make stop_extent exclusive of shift range */
6117 stop_extent--;
6118 } else
6119 stop_extent = total_extents;
6120
6121 /*
6122 * Look up the extent index for the fsb where we start shifting. We can
6123 * henceforth iterate with current_ext as extent list changes are locked
6124 * out via ilock.
6125 *
6126 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6127 * *next_fsb lies in a hole beyond which there are no extents. Either
6128 * way, we are done.
6129 */
6130 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
6131 if (!gotp) {
6132 *done = 1;
6133 goto del_cursor;
6134 }
6135
6136 /* some sanity checking before we finally start shifting extents */
6137 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6138 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6139 error = -EIO;
6140 goto del_cursor;
6141 }
6142
6143 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006144 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006145 &current_ext, gotp, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006146 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006147 if (error)
6148 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006149 /*
6150 * If there was an extent merge during the shift, the extent
6151 * count can change. Update the total and grade the next record.
6152 */
6153 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006154 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006155 stop_extent = total_extents;
6156 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006157
Namjae Jeona904b1c2015-03-25 15:08:56 +11006158 if (current_ext == stop_extent) {
6159 *done = 1;
6160 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006161 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006162 }
Brian Fosterddb19e32014-09-23 15:38:09 +10006163 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006164 }
6165
Namjae Jeona904b1c2015-03-25 15:08:56 +11006166 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006167 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10006168 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10006169 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006170
6171del_cursor:
6172 if (cur)
6173 xfs_btree_del_cursor(cur,
6174 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6175
Brian Fosterca446d82014-09-02 12:12:53 +10006176 if (logflags)
6177 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006178
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006179 return error;
6180}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006181
6182/*
6183 * Splits an extent into two extents at split_fsb block such that it is
6184 * the first block of the current_ext. @current_ext is a target extent
6185 * to be split. @split_fsb is a block where the extents is split.
6186 * If split_fsb lies in a hole or the first block of extents, just return 0.
6187 */
6188STATIC int
6189xfs_bmap_split_extent_at(
6190 struct xfs_trans *tp,
6191 struct xfs_inode *ip,
6192 xfs_fileoff_t split_fsb,
6193 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006194 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006195{
6196 int whichfork = XFS_DATA_FORK;
6197 struct xfs_btree_cur *cur = NULL;
6198 struct xfs_bmbt_rec_host *gotp;
6199 struct xfs_bmbt_irec got;
6200 struct xfs_bmbt_irec new; /* split extent */
6201 struct xfs_mount *mp = ip->i_mount;
6202 struct xfs_ifork *ifp;
6203 xfs_fsblock_t gotblkcnt; /* new block count for got */
6204 xfs_extnum_t current_ext;
6205 int error = 0;
6206 int logflags = 0;
6207 int i = 0;
6208
6209 if (unlikely(XFS_TEST_ERROR(
6210 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6211 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6212 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6213 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6214 XFS_ERRLEVEL_LOW, mp);
6215 return -EFSCORRUPTED;
6216 }
6217
6218 if (XFS_FORCED_SHUTDOWN(mp))
6219 return -EIO;
6220
6221 ifp = XFS_IFORK_PTR(ip, whichfork);
6222 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6223 /* Read in all the extents */
6224 error = xfs_iread_extents(tp, ip, whichfork);
6225 if (error)
6226 return error;
6227 }
6228
6229 /*
6230 * gotp can be null in 2 cases: 1) if there are no extents
6231 * or 2) split_fsb lies in a hole beyond which there are
6232 * no extents. Either way, we are done.
6233 */
6234 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
6235 if (!gotp)
6236 return 0;
6237
6238 xfs_bmbt_get_all(gotp, &got);
6239
6240 /*
6241 * Check split_fsb lies in a hole or the start boundary offset
6242 * of the extent.
6243 */
6244 if (got.br_startoff >= split_fsb)
6245 return 0;
6246
6247 gotblkcnt = split_fsb - got.br_startoff;
6248 new.br_startoff = split_fsb;
6249 new.br_startblock = got.br_startblock + gotblkcnt;
6250 new.br_blockcount = got.br_blockcount - gotblkcnt;
6251 new.br_state = got.br_state;
6252
6253 if (ifp->if_flags & XFS_IFBROOT) {
6254 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6255 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006256 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006257 cur->bc_private.b.flags = 0;
6258 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6259 got.br_startblock,
6260 got.br_blockcount,
6261 &i);
6262 if (error)
6263 goto del_cursor;
6264 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6265 }
6266
6267 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6268 got.br_blockcount = gotblkcnt;
6269
6270 logflags = XFS_ILOG_CORE;
6271 if (cur) {
6272 error = xfs_bmbt_update(cur, got.br_startoff,
6273 got.br_startblock,
6274 got.br_blockcount,
6275 got.br_state);
6276 if (error)
6277 goto del_cursor;
6278 } else
6279 logflags |= XFS_ILOG_DEXT;
6280
6281 /* Add new extent */
6282 current_ext++;
6283 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6284 XFS_IFORK_NEXT_SET(ip, whichfork,
6285 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6286
6287 if (cur) {
6288 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6289 new.br_startblock, new.br_blockcount,
6290 &i);
6291 if (error)
6292 goto del_cursor;
6293 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6294 cur->bc_rec.b.br_state = new.br_state;
6295
6296 error = xfs_btree_insert(cur, &i);
6297 if (error)
6298 goto del_cursor;
6299 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6300 }
6301
6302 /*
6303 * Convert to a btree if necessary.
6304 */
6305 if (xfs_bmap_needs_btree(ip, whichfork)) {
6306 int tmp_logflags; /* partial log flag return val */
6307
6308 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006309 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006310 &cur, 0, &tmp_logflags, whichfork);
6311 logflags |= tmp_logflags;
6312 }
6313
6314del_cursor:
6315 if (cur) {
6316 cur->bc_private.b.allocated = 0;
6317 xfs_btree_del_cursor(cur,
6318 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6319 }
6320
6321 if (logflags)
6322 xfs_trans_log_inode(tp, ip, logflags);
6323 return error;
6324}
6325
6326int
6327xfs_bmap_split_extent(
6328 struct xfs_inode *ip,
6329 xfs_fileoff_t split_fsb)
6330{
6331 struct xfs_mount *mp = ip->i_mount;
6332 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006333 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006334 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006335 int error;
6336
Christoph Hellwig253f4912016-04-06 09:19:55 +10006337 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6338 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6339 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006340 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006341
6342 xfs_ilock(ip, XFS_ILOCK_EXCL);
6343 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6344
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006345 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006346
6347 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006348 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006349 if (error)
6350 goto out;
6351
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006352 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006353 if (error)
6354 goto out;
6355
Christoph Hellwig70393312015-06-04 13:48:08 +10006356 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006357
6358out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006359 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006360 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006361 return error;
6362}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006363
6364/* Deferred mapping is only for real extents in the data fork. */
6365static bool
6366xfs_bmap_is_update_needed(
6367 struct xfs_bmbt_irec *bmap)
6368{
6369 return bmap->br_startblock != HOLESTARTBLOCK &&
6370 bmap->br_startblock != DELAYSTARTBLOCK;
6371}
6372
6373/* Record a bmap intent. */
6374static int
6375__xfs_bmap_add(
6376 struct xfs_mount *mp,
6377 struct xfs_defer_ops *dfops,
6378 enum xfs_bmap_intent_type type,
6379 struct xfs_inode *ip,
6380 int whichfork,
6381 struct xfs_bmbt_irec *bmap)
6382{
6383 int error;
6384 struct xfs_bmap_intent *bi;
6385
6386 trace_xfs_bmap_defer(mp,
6387 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6388 type,
6389 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6390 ip->i_ino, whichfork,
6391 bmap->br_startoff,
6392 bmap->br_blockcount,
6393 bmap->br_state);
6394
6395 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6396 INIT_LIST_HEAD(&bi->bi_list);
6397 bi->bi_type = type;
6398 bi->bi_owner = ip;
6399 bi->bi_whichfork = whichfork;
6400 bi->bi_bmap = *bmap;
6401
6402 error = xfs_defer_join(dfops, bi->bi_owner);
6403 if (error) {
6404 kmem_free(bi);
6405 return error;
6406 }
6407
6408 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6409 return 0;
6410}
6411
6412/* Map an extent into a file. */
6413int
6414xfs_bmap_map_extent(
6415 struct xfs_mount *mp,
6416 struct xfs_defer_ops *dfops,
6417 struct xfs_inode *ip,
6418 struct xfs_bmbt_irec *PREV)
6419{
6420 if (!xfs_bmap_is_update_needed(PREV))
6421 return 0;
6422
6423 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6424 XFS_DATA_FORK, PREV);
6425}
6426
6427/* Unmap an extent out of a file. */
6428int
6429xfs_bmap_unmap_extent(
6430 struct xfs_mount *mp,
6431 struct xfs_defer_ops *dfops,
6432 struct xfs_inode *ip,
6433 struct xfs_bmbt_irec *PREV)
6434{
6435 if (!xfs_bmap_is_update_needed(PREV))
6436 return 0;
6437
6438 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6439 XFS_DATA_FORK, PREV);
6440}
6441
6442/*
6443 * Process one of the deferred bmap operations. We pass back the
6444 * btree cursor to maintain our lock on the bmapbt between calls.
6445 */
6446int
6447xfs_bmap_finish_one(
6448 struct xfs_trans *tp,
6449 struct xfs_defer_ops *dfops,
6450 struct xfs_inode *ip,
6451 enum xfs_bmap_intent_type type,
6452 int whichfork,
6453 xfs_fileoff_t startoff,
6454 xfs_fsblock_t startblock,
6455 xfs_filblks_t blockcount,
6456 xfs_exntst_t state)
6457{
6458 struct xfs_bmbt_irec bmap;
6459 int nimaps = 1;
6460 xfs_fsblock_t firstfsb;
6461 int flags = XFS_BMAPI_REMAP;
6462 int done;
6463 int error = 0;
6464
6465 bmap.br_startblock = startblock;
6466 bmap.br_startoff = startoff;
6467 bmap.br_blockcount = blockcount;
6468 bmap.br_state = state;
6469
6470 trace_xfs_bmap_deferred(tp->t_mountp,
6471 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6472 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6473 ip->i_ino, whichfork, startoff, blockcount, state);
6474
6475 if (whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK)
6476 return -EFSCORRUPTED;
6477 if (whichfork == XFS_ATTR_FORK)
6478 flags |= XFS_BMAPI_ATTRFORK;
6479
6480 if (XFS_TEST_ERROR(false, tp->t_mountp,
6481 XFS_ERRTAG_BMAP_FINISH_ONE,
6482 XFS_RANDOM_BMAP_FINISH_ONE))
6483 return -EIO;
6484
6485 switch (type) {
6486 case XFS_BMAP_MAP:
6487 firstfsb = bmap.br_startblock;
6488 error = xfs_bmapi_write(tp, ip, bmap.br_startoff,
6489 bmap.br_blockcount, flags, &firstfsb,
6490 bmap.br_blockcount, &bmap, &nimaps,
6491 dfops);
6492 break;
6493 case XFS_BMAP_UNMAP:
6494 error = xfs_bunmapi(tp, ip, bmap.br_startoff,
6495 bmap.br_blockcount, flags, 1, &firstfsb,
6496 dfops, &done);
6497 ASSERT(done);
6498 break;
6499 default:
6500 ASSERT(0);
6501 error = -EFSCORRUPTED;
6502 }
6503
6504 return error;
6505}