blob: 6b7e6eb294145392c0d84b98ffe65a3da4d78ba6 [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 */
521 int whichfork, /* data or attr fork */
522 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;
530
531 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +1100532 ASSERT(cnt == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100533 for (idx = 0; idx < cnt; idx++)
534 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
535}
536
537/*
538 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000539 * given the caller's original parameters. Specifically check the
540 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100541 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
542 */
543STATIC void
544xfs_bmap_validate_ret(
545 xfs_fileoff_t bno,
546 xfs_filblks_t len,
547 int flags,
548 xfs_bmbt_irec_t *mval,
549 int nmap,
550 int ret_nmap)
551{
552 int i; /* index to map values */
553
554 ASSERT(ret_nmap <= nmap);
555
556 for (i = 0; i < ret_nmap; i++) {
557 ASSERT(mval[i].br_blockcount > 0);
558 if (!(flags & XFS_BMAPI_ENTIRE)) {
559 ASSERT(mval[i].br_startoff >= bno);
560 ASSERT(mval[i].br_blockcount <= len);
561 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
562 bno + len);
563 } else {
564 ASSERT(mval[i].br_startoff < bno + len);
565 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
566 bno);
567 }
568 ASSERT(i == 0 ||
569 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
570 mval[i].br_startoff);
571 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
572 mval[i].br_startblock != HOLESTARTBLOCK);
573 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
574 mval[i].br_state == XFS_EXT_UNWRITTEN);
575 }
576}
577
578#else
579#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
580#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
581#endif /* DEBUG */
582
583/*
584 * bmap free list manipulation functions
585 */
586
587/*
588 * Add the extent to the list of extents to be free at transaction end.
589 * The list is maintained sorted (by block number).
590 */
591void
592xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000593 struct xfs_mount *mp,
594 struct xfs_defer_ops *dfops,
595 xfs_fsblock_t bno,
596 xfs_filblks_t len,
597 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100598{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000599 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100600#ifdef DEBUG
601 xfs_agnumber_t agno;
602 xfs_agblock_t agbno;
603
604 ASSERT(bno != NULLFSBLOCK);
605 ASSERT(len > 0);
606 ASSERT(len <= MAXEXTLEN);
607 ASSERT(!isnullstartblock(bno));
608 agno = XFS_FSB_TO_AGNO(mp, bno);
609 agbno = XFS_FSB_TO_AGBNO(mp, bno);
610 ASSERT(agno < mp->m_sb.sb_agcount);
611 ASSERT(agbno < mp->m_sb.sb_agblocks);
612 ASSERT(len < mp->m_sb.sb_agblocks);
613 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
614#endif
615 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000616
Dave Chinner9e5987a2013-02-25 12:31:26 +1100617 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000618 new->xefi_startblock = bno;
619 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000620 if (oinfo)
621 new->xefi_oinfo = *oinfo;
622 else
623 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000624 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
625 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000626 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100627}
628
629/*
630 * Inode fork format manipulation functions
631 */
632
633/*
634 * Transform a btree format file with only one leaf node, where the
635 * extents list will fit in the inode, into an extents format file.
636 * Since the file extents are already in-core, all we have to do is
637 * give up the space for the btree root and pitch the leaf block.
638 */
639STATIC int /* error */
640xfs_bmap_btree_to_extents(
641 xfs_trans_t *tp, /* transaction pointer */
642 xfs_inode_t *ip, /* incore inode pointer */
643 xfs_btree_cur_t *cur, /* btree cursor */
644 int *logflagsp, /* inode logging flags */
645 int whichfork) /* data or attr fork */
646{
647 /* REFERENCED */
648 struct xfs_btree_block *cblock;/* child btree block */
649 xfs_fsblock_t cbno; /* child block number */
650 xfs_buf_t *cbp; /* child block's buffer */
651 int error; /* error return value */
652 xfs_ifork_t *ifp; /* inode fork data */
653 xfs_mount_t *mp; /* mount point structure */
654 __be64 *pp; /* ptr to block address */
655 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000656 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100657
658 mp = ip->i_mount;
659 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700660 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100661 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
662 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
663 rblock = ifp->if_broot;
664 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
665 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
666 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
667 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
668 cbno = be64_to_cpu(*pp);
669 *logflagsp = 0;
670#ifdef DEBUG
671 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
672 return error;
673#endif
674 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
675 &xfs_bmbt_buf_ops);
676 if (error)
677 return error;
678 cblock = XFS_BUF_TO_BLOCK(cbp);
679 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
680 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000681 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
682 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100683 ip->i_d.di_nblocks--;
684 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
685 xfs_trans_binval(tp, cbp);
686 if (cur->bc_bufs[0] == cbp)
687 cur->bc_bufs[0] = NULL;
688 xfs_iroot_realloc(ip, -1, whichfork);
689 ASSERT(ifp->if_broot == NULL);
690 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
691 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
692 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
693 return 0;
694}
695
696/*
697 * Convert an extents-format file into a btree-format file.
698 * The new file will have a root block (in the inode) and a single child block.
699 */
700STATIC int /* error */
701xfs_bmap_extents_to_btree(
702 xfs_trans_t *tp, /* transaction pointer */
703 xfs_inode_t *ip, /* incore inode pointer */
704 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000705 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100706 xfs_btree_cur_t **curp, /* cursor returned to caller */
707 int wasdel, /* converting a delayed alloc */
708 int *logflagsp, /* inode logging flags */
709 int whichfork) /* data or attr fork */
710{
711 struct xfs_btree_block *ablock; /* allocated (child) bt block */
712 xfs_buf_t *abp; /* buffer for ablock */
713 xfs_alloc_arg_t args; /* allocation arguments */
714 xfs_bmbt_rec_t *arp; /* child record pointer */
715 struct xfs_btree_block *block; /* btree root block */
716 xfs_btree_cur_t *cur; /* bmap btree cursor */
717 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
718 int error; /* error return value */
719 xfs_extnum_t i, cnt; /* extent record index */
720 xfs_ifork_t *ifp; /* inode fork pointer */
721 xfs_bmbt_key_t *kp; /* root block key pointer */
722 xfs_mount_t *mp; /* mount structure */
723 xfs_extnum_t nextents; /* number of file extents */
724 xfs_bmbt_ptr_t *pp; /* root block address pointer */
725
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500726 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700727 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100728 ifp = XFS_IFORK_PTR(ip, whichfork);
729 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
730
731 /*
732 * Make space in the inode incore.
733 */
734 xfs_iroot_realloc(ip, 1, whichfork);
735 ifp->if_flags |= XFS_IFBROOT;
736
737 /*
738 * Fill in the root.
739 */
740 block = ifp->if_broot;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500741 if (xfs_sb_version_hascrc(&mp->m_sb))
742 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
743 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
744 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
745 else
746 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
747 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
748 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100749
750 /*
751 * Need a cursor. Can't allocate until bb_level is filled in.
752 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100753 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
754 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000755 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100756 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
757 /*
758 * Convert to a btree with two levels, one record in root.
759 */
760 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
761 memset(&args, 0, sizeof(args));
762 args.tp = tp;
763 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000764 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100765 args.firstblock = *firstblock;
766 if (*firstblock == NULLFSBLOCK) {
767 args.type = XFS_ALLOCTYPE_START_BNO;
768 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000769 } else if (dfops->dop_low) {
Darrick J. Wong90e20562016-10-03 09:11:45 -0700770try_another_ag:
Dave Chinner9e5987a2013-02-25 12:31:26 +1100771 args.type = XFS_ALLOCTYPE_START_BNO;
772 args.fsbno = *firstblock;
773 } else {
774 args.type = XFS_ALLOCTYPE_NEAR_BNO;
775 args.fsbno = *firstblock;
776 }
777 args.minlen = args.maxlen = args.prod = 1;
778 args.wasdel = wasdel;
779 *logflagsp = 0;
780 if ((error = xfs_alloc_vextent(&args))) {
781 xfs_iroot_realloc(ip, -1, whichfork);
782 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
783 return error;
784 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700785
786 /*
787 * During a CoW operation, the allocation and bmbt updates occur in
788 * different transactions. The mapping code tries to put new bmbt
789 * blocks near extents being mapped, but the only way to guarantee this
790 * is if the alloc and the mapping happen in a single transaction that
791 * has a block reservation. That isn't the case here, so if we run out
792 * of space we'll try again with another AG.
793 */
794 if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) &&
795 args.fsbno == NULLFSBLOCK &&
796 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
797 dfops->dop_low = true;
798 goto try_another_ag;
799 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100800 /*
801 * Allocation can't fail, the space was reserved.
802 */
803 ASSERT(args.fsbno != NULLFSBLOCK);
804 ASSERT(*firstblock == NULLFSBLOCK ||
805 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000806 (dfops->dop_low &&
Dave Chinner9e5987a2013-02-25 12:31:26 +1100807 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
808 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
809 cur->bc_private.b.allocated++;
810 ip->i_d.di_nblocks++;
811 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
812 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
813 /*
814 * Fill in the child block.
815 */
816 abp->b_ops = &xfs_bmbt_buf_ops;
817 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500818 if (xfs_sb_version_hascrc(&mp->m_sb))
819 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
820 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
821 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
822 else
823 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
824 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
825 XFS_BTREE_LONG_PTRS);
826
Dave Chinner9e5987a2013-02-25 12:31:26 +1100827 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100828 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100829 for (cnt = i = 0; i < nextents; i++) {
830 ep = xfs_iext_get_ext(ifp, i);
831 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
832 arp->l0 = cpu_to_be64(ep->l0);
833 arp->l1 = cpu_to_be64(ep->l1);
834 arp++; cnt++;
835 }
836 }
837 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
838 xfs_btree_set_numrecs(ablock, cnt);
839
840 /*
841 * Fill in the root key and pointer.
842 */
843 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
844 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
845 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
846 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
847 be16_to_cpu(block->bb_level)));
848 *pp = cpu_to_be64(args.fsbno);
849
850 /*
851 * Do all this logging at the end so that
852 * the root is at the right level.
853 */
854 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
855 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
856 ASSERT(*curp == NULL);
857 *curp = cur;
858 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
859 return 0;
860}
861
862/*
863 * Convert a local file to an extents file.
864 * This code is out of bounds for data forks of regular files,
865 * since the file data needs to get logged so things will stay consistent.
866 * (The bmap-level manipulations are ok, though).
867 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000868void
869xfs_bmap_local_to_extents_empty(
870 struct xfs_inode *ip,
871 int whichfork)
872{
873 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
874
Darrick J. Wong60b49842016-10-03 09:11:34 -0700875 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000876 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
877 ASSERT(ifp->if_bytes == 0);
878 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
879
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000880 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000881 ifp->if_flags &= ~XFS_IFINLINE;
882 ifp->if_flags |= XFS_IFEXTENTS;
883 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
884}
885
886
Dave Chinner9e5987a2013-02-25 12:31:26 +1100887STATIC int /* error */
888xfs_bmap_local_to_extents(
889 xfs_trans_t *tp, /* transaction pointer */
890 xfs_inode_t *ip, /* incore inode pointer */
891 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
892 xfs_extlen_t total, /* total blocks needed by transaction */
893 int *logflagsp, /* inode logging flags */
894 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500895 void (*init_fn)(struct xfs_trans *tp,
896 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100897 struct xfs_inode *ip,
898 struct xfs_ifork *ifp))
899{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000900 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100901 int flags; /* logging flags returned */
902 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000903 xfs_alloc_arg_t args; /* allocation arguments */
904 xfs_buf_t *bp; /* buffer for extent block */
905 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100906
907 /*
908 * We don't want to deal with the case of keeping inode data inline yet.
909 * So sending the data fork of a regular inode is invalid.
910 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100911 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100912 ifp = XFS_IFORK_PTR(ip, whichfork);
913 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000914
915 if (!ifp->if_bytes) {
916 xfs_bmap_local_to_extents_empty(ip, whichfork);
917 flags = XFS_ILOG_CORE;
918 goto done;
919 }
920
Dave Chinner9e5987a2013-02-25 12:31:26 +1100921 flags = 0;
922 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000923 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
924 XFS_IFINLINE);
925 memset(&args, 0, sizeof(args));
926 args.tp = tp;
927 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000928 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000929 args.firstblock = *firstblock;
930 /*
931 * Allocate a block. We know we need only one, since the
932 * file currently fits in an inode.
933 */
934 if (*firstblock == NULLFSBLOCK) {
Darrick J. Wong90e20562016-10-03 09:11:45 -0700935try_another_ag:
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000936 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
937 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100938 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000939 args.fsbno = *firstblock;
940 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100941 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000942 args.total = total;
943 args.minlen = args.maxlen = args.prod = 1;
944 error = xfs_alloc_vextent(&args);
945 if (error)
946 goto done;
947
Darrick J. Wong90e20562016-10-03 09:11:45 -0700948 /*
949 * During a CoW operation, the allocation and bmbt updates occur in
950 * different transactions. The mapping code tries to put new bmbt
951 * blocks near extents being mapped, but the only way to guarantee this
952 * is if the alloc and the mapping happen in a single transaction that
953 * has a block reservation. That isn't the case here, so if we run out
954 * of space we'll try again with another AG.
955 */
956 if (xfs_sb_version_hasreflink(&ip->i_mount->m_sb) &&
957 args.fsbno == NULLFSBLOCK &&
958 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
959 goto try_another_ag;
960 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000961 /* Can't fail, the space was reserved. */
962 ASSERT(args.fsbno != NULLFSBLOCK);
963 ASSERT(args.len == 1);
964 *firstblock = args.fsbno;
965 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
966
Dave Chinnerfe22d552015-01-22 09:30:06 +1100967 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100968 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100969 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100970 * The callout is responsible for logging because the remote format
971 * might differ from the local format and thus we don't know how much to
972 * log here. Note that init_fn must also set the buffer log item type
973 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100974 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000975 init_fn(tp, bp, ip, ifp);
976
Brian Fosterb7cdc662015-10-12 15:40:24 +1100977 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000978 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
979 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100980 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000981
982 xfs_iext_add(ifp, 0, 1);
983 ep = xfs_iext_get_ext(ifp, 0);
984 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
985 trace_xfs_bmap_post_update(ip, 0,
986 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
987 _THIS_IP_);
988 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
989 ip->i_d.di_nblocks = 1;
990 xfs_trans_mod_dquot_byino(tp, ip,
991 XFS_TRANS_DQ_BCOUNT, 1L);
992 flags |= xfs_ilog_fext(whichfork);
993
Dave Chinner9e5987a2013-02-25 12:31:26 +1100994done:
995 *logflagsp = flags;
996 return error;
997}
998
999/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 * Called from xfs_bmap_add_attrfork to handle btree format files.
1001 */
1002STATIC int /* error */
1003xfs_bmap_add_attrfork_btree(
1004 xfs_trans_t *tp, /* transaction pointer */
1005 xfs_inode_t *ip, /* incore inode pointer */
1006 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001007 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 int *flags) /* inode logging flags */
1009{
1010 xfs_btree_cur_t *cur; /* btree cursor */
1011 int error; /* error return value */
1012 xfs_mount_t *mp; /* file system mount struct */
1013 int stat; /* newroot status */
1014
1015 mp = ip->i_mount;
1016 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1017 *flags |= XFS_ILOG_DBROOT;
1018 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001019 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001020 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 cur->bc_private.b.firstblock = *firstblock;
1022 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1023 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001024 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001025 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001026 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 goto error0;
1028 if (stat == 0) {
1029 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +10001030 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032 *firstblock = cur->bc_private.b.firstblock;
1033 cur->bc_private.b.allocated = 0;
1034 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1035 }
1036 return 0;
1037error0:
1038 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1039 return error;
1040}
1041
1042/*
1043 * Called from xfs_bmap_add_attrfork to handle extents format files.
1044 */
1045STATIC int /* error */
1046xfs_bmap_add_attrfork_extents(
1047 xfs_trans_t *tp, /* transaction pointer */
1048 xfs_inode_t *ip, /* incore inode pointer */
1049 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001050 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 int *flags) /* inode logging flags */
1052{
1053 xfs_btree_cur_t *cur; /* bmap btree cursor */
1054 int error; /* error return value */
1055
1056 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1057 return 0;
1058 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001059 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 flags, XFS_DATA_FORK);
1061 if (cur) {
1062 cur->bc_private.b.allocated = 0;
1063 xfs_btree_del_cursor(cur,
1064 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1065 }
1066 return error;
1067}
1068
1069/*
Dave Chinner1e823792013-02-11 15:58:13 +11001070 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1071 * different data fork content type needs a different callout to do the
1072 * conversion. Some are basic and only require special block initialisation
1073 * callouts for the data formating, others (directories) are so specialised they
1074 * handle everything themselves.
1075 *
1076 * XXX (dgc): investigate whether directory conversion can use the generic
1077 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001078 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 */
1080STATIC int /* error */
1081xfs_bmap_add_attrfork_local(
1082 xfs_trans_t *tp, /* transaction pointer */
1083 xfs_inode_t *ip, /* incore inode pointer */
1084 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001085 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int *flags) /* inode logging flags */
1087{
1088 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1091 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001092
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001093 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001095 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 dargs.dp = ip;
1097 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001098 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001099 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 dargs.whichfork = XFS_DATA_FORK;
1101 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001102 return xfs_dir2_sf_to_block(&dargs);
1103 }
1104
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001105 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001106 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1107 flags, XFS_DATA_FORK,
1108 xfs_symlink_local_to_remote);
1109
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001110 /* should only be called for types that support local format data */
1111 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001112 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
1115/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001116 * Convert inode from non-attributed to attributed.
1117 * Must not be in a transaction, ip must not be locked.
1118 */
1119int /* error code */
1120xfs_bmap_add_attrfork(
1121 xfs_inode_t *ip, /* incore inode pointer */
1122 int size, /* space new attribute needs */
1123 int rsvd) /* xact may use reserved blks */
1124{
1125 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001126 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001127 xfs_mount_t *mp; /* mount structure */
1128 xfs_trans_t *tp; /* transaction pointer */
1129 int blks; /* space reservation */
1130 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001131 int logflags; /* logging flags */
1132 int error; /* error return value */
1133
1134 ASSERT(XFS_IFORK_Q(ip) == 0);
1135
1136 mp = ip->i_mount;
1137 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001138
Dave Chinner9e5987a2013-02-25 12:31:26 +11001139 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001140
1141 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1142 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1143 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001144 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001145
Dave Chinner9e5987a2013-02-25 12:31:26 +11001146 xfs_ilock(ip, XFS_ILOCK_EXCL);
1147 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1148 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1149 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001150 if (error)
1151 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001152 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001153 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001154 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1155 /*
1156 * For inodes coming from pre-6.2 filesystems.
1157 */
1158 ASSERT(ip->i_d.di_aformat == 0);
1159 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1160 }
1161 ASSERT(ip->i_d.di_anextents == 0);
1162
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001163 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001164 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1165
1166 switch (ip->i_d.di_format) {
1167 case XFS_DINODE_FMT_DEV:
1168 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1169 break;
1170 case XFS_DINODE_FMT_UUID:
1171 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1172 break;
1173 case XFS_DINODE_FMT_LOCAL:
1174 case XFS_DINODE_FMT_EXTENTS:
1175 case XFS_DINODE_FMT_BTREE:
1176 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1177 if (!ip->i_d.di_forkoff)
1178 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1179 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1180 version = 2;
1181 break;
1182 default:
1183 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001184 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001185 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001186 }
1187
1188 ASSERT(ip->i_afp == NULL);
1189 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1190 ip->i_afp->if_flags = XFS_IFEXTENTS;
1191 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001192 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001193 switch (ip->i_d.di_format) {
1194 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001195 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001196 &logflags);
1197 break;
1198 case XFS_DINODE_FMT_EXTENTS:
1199 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001200 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001201 break;
1202 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001203 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001204 &logflags);
1205 break;
1206 default:
1207 error = 0;
1208 break;
1209 }
1210 if (logflags)
1211 xfs_trans_log_inode(tp, ip, logflags);
1212 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001213 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001214 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1215 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001216 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001217
1218 spin_lock(&mp->m_sb_lock);
1219 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1220 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001221 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001222 }
1223 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1224 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001225 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001226 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001227 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001228 if (log_sb)
1229 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001230 }
1231
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001232 error = xfs_defer_finish(&tp, &dfops, NULL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001233 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001234 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001235 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001236 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001237 return error;
1238
1239bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001240 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001241trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001242 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001243 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001244 return error;
1245}
1246
1247/*
1248 * Internal and external extent tree search functions.
1249 */
1250
1251/*
1252 * Read in the extents to if_extents.
1253 * All inode fields are set up by caller, we just traverse the btree
1254 * and copy the records in. If the file system cannot contain unwritten
1255 * extents, the records are checked for no "state" flags.
1256 */
1257int /* error */
1258xfs_bmap_read_extents(
1259 xfs_trans_t *tp, /* transaction pointer */
1260 xfs_inode_t *ip, /* incore inode */
1261 int whichfork) /* data or attr fork */
1262{
1263 struct xfs_btree_block *block; /* current btree block */
1264 xfs_fsblock_t bno; /* block # of "block" */
1265 xfs_buf_t *bp; /* buffer for "block" */
1266 int error; /* error return value */
1267 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1268 xfs_extnum_t i, j; /* index into the extents list */
1269 xfs_ifork_t *ifp; /* fork structure */
1270 int level; /* btree level, for checking */
1271 xfs_mount_t *mp; /* file system mount structure */
1272 __be64 *pp; /* pointer to block address */
1273 /* REFERENCED */
1274 xfs_extnum_t room; /* number of entries there's room for */
1275
1276 bno = NULLFSBLOCK;
1277 mp = ip->i_mount;
1278 ifp = XFS_IFORK_PTR(ip, whichfork);
1279 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1280 XFS_EXTFMT_INODE(ip);
1281 block = ifp->if_broot;
1282 /*
1283 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1284 */
1285 level = be16_to_cpu(block->bb_level);
1286 ASSERT(level > 0);
1287 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1288 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001289 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001290 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1291 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1292 /*
1293 * Go down the tree until leaf level is reached, following the first
1294 * pointer (leftmost) at each level.
1295 */
1296 while (level-- > 0) {
1297 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1298 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1299 if (error)
1300 return error;
1301 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001302 if (level == 0)
1303 break;
1304 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1305 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001306 XFS_WANT_CORRUPTED_GOTO(mp,
1307 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001308 xfs_trans_brelse(tp, bp);
1309 }
1310 /*
1311 * Here with bp and block set to the leftmost leaf node in the tree.
1312 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001313 room = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001314 i = 0;
1315 /*
1316 * Loop over all leaf nodes. Copy information to the extent records.
1317 */
1318 for (;;) {
1319 xfs_bmbt_rec_t *frp;
1320 xfs_fsblock_t nextbno;
1321 xfs_extnum_t num_recs;
1322 xfs_extnum_t start;
1323
1324 num_recs = xfs_btree_get_numrecs(block);
1325 if (unlikely(i + num_recs > room)) {
1326 ASSERT(i + num_recs <= room);
1327 xfs_warn(ip->i_mount,
1328 "corrupt dinode %Lu, (btree extents).",
1329 (unsigned long long) ip->i_ino);
1330 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1331 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1332 goto error0;
1333 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001334 /*
1335 * Read-ahead the next leaf block, if any.
1336 */
1337 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1338 if (nextbno != NULLFSBLOCK)
1339 xfs_btree_reada_bufl(mp, nextbno, 1,
1340 &xfs_bmbt_buf_ops);
1341 /*
1342 * Copy records into the extent records.
1343 */
1344 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1345 start = i;
1346 for (j = 0; j < num_recs; j++, i++, frp++) {
1347 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1348 trp->l0 = be64_to_cpu(frp->l0);
1349 trp->l1 = be64_to_cpu(frp->l1);
1350 }
1351 if (exntf == XFS_EXTFMT_NOSTATE) {
1352 /*
1353 * Check all attribute bmap btree records and
1354 * any "older" data bmap btree records for a
1355 * set bit in the "extent flag" position.
1356 */
1357 if (unlikely(xfs_check_nostate_extents(ifp,
1358 start, num_recs))) {
1359 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1360 XFS_ERRLEVEL_LOW,
1361 ip->i_mount);
1362 goto error0;
1363 }
1364 }
1365 xfs_trans_brelse(tp, bp);
1366 bno = nextbno;
1367 /*
1368 * If we've reached the end, stop.
1369 */
1370 if (bno == NULLFSBLOCK)
1371 break;
1372 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1373 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1374 if (error)
1375 return error;
1376 block = XFS_BUF_TO_BLOCK(bp);
1377 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001378 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11001379 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1380 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1381 return 0;
1382error0:
1383 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001384 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001385}
1386
Dave Chinner9e5987a2013-02-25 12:31:26 +11001387/*
1388 * Returns the file-relative block number of the first unused block(s)
1389 * in the file with at least "len" logically contiguous blocks free.
1390 * This is the lowest-address hole if the file has holes, else the first block
1391 * past the end of file.
1392 * Return 0 if the file is currently local (in-inode).
1393 */
1394int /* error */
1395xfs_bmap_first_unused(
1396 xfs_trans_t *tp, /* transaction pointer */
1397 xfs_inode_t *ip, /* incore inode */
1398 xfs_extlen_t len, /* size of hole to find */
1399 xfs_fileoff_t *first_unused, /* unused block */
1400 int whichfork) /* data or attr fork */
1401{
1402 int error; /* error return value */
1403 int idx; /* extent record index */
1404 xfs_ifork_t *ifp; /* inode fork pointer */
1405 xfs_fileoff_t lastaddr; /* last block number seen */
1406 xfs_fileoff_t lowest; /* lowest useful block */
1407 xfs_fileoff_t max; /* starting useful block */
1408 xfs_fileoff_t off; /* offset for this block */
1409 xfs_extnum_t nextents; /* number of extent entries */
1410
1411 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1412 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1413 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1414 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1415 *first_unused = 0;
1416 return 0;
1417 }
1418 ifp = XFS_IFORK_PTR(ip, whichfork);
1419 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1420 (error = xfs_iread_extents(tp, ip, whichfork)))
1421 return error;
1422 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001423 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001424 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1425 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1426 off = xfs_bmbt_get_startoff(ep);
1427 /*
1428 * See if the hole before this extent will work.
1429 */
1430 if (off >= lowest + len && off - max >= len) {
1431 *first_unused = max;
1432 return 0;
1433 }
1434 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1435 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1436 }
1437 *first_unused = max;
1438 return 0;
1439}
1440
1441/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001442 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001443 * last_block (input value) in the file.
1444 * This is not based on i_size, it is based on the extent records.
1445 * Returns 0 for local files, as they do not have extent records.
1446 */
1447int /* error */
1448xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001449 struct xfs_trans *tp, /* transaction pointer */
1450 struct xfs_inode *ip, /* incore inode */
1451 xfs_fileoff_t *last_block, /* last block */
1452 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001453{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001454 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1455 struct xfs_bmbt_irec got;
1456 xfs_extnum_t idx;
1457 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001458
Christoph Hellwig86685f72016-11-24 11:39:38 +11001459 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1460 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001461 *last_block = 0;
1462 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001463 case XFS_DINODE_FMT_BTREE:
1464 case XFS_DINODE_FMT_EXTENTS:
1465 break;
1466 default:
1467 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001468 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001469
1470 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1471 error = xfs_iread_extents(tp, ip, whichfork);
1472 if (error)
1473 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001474 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001475
1476 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1477 if (got.br_startoff <= *last_block - 1)
1478 return 0;
1479 }
1480
1481 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1482 *last_block = got.br_startoff + got.br_blockcount;
1483 return 0;
1484 }
1485
1486 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001487 return 0;
1488}
1489
Dave Chinner68988112013-08-12 20:49:42 +10001490int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001491xfs_bmap_last_extent(
1492 struct xfs_trans *tp,
1493 struct xfs_inode *ip,
1494 int whichfork,
1495 struct xfs_bmbt_irec *rec,
1496 int *is_empty)
1497{
1498 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1499 int error;
1500 int nextents;
1501
1502 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1503 error = xfs_iread_extents(tp, ip, whichfork);
1504 if (error)
1505 return error;
1506 }
1507
Eric Sandeen5d829302016-11-08 12:59:42 +11001508 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001509 if (nextents == 0) {
1510 *is_empty = 1;
1511 return 0;
1512 }
1513
1514 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1515 *is_empty = 0;
1516 return 0;
1517}
1518
1519/*
1520 * Check the last inode extent to determine whether this allocation will result
1521 * in blocks being allocated at the end of the file. When we allocate new data
1522 * blocks at the end of the file which do not start at the previous data block,
1523 * we will try to align the new blocks at stripe unit boundaries.
1524 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001525 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001526 * at, or past the EOF.
1527 */
1528STATIC int
1529xfs_bmap_isaeof(
1530 struct xfs_bmalloca *bma,
1531 int whichfork)
1532{
1533 struct xfs_bmbt_irec rec;
1534 int is_empty;
1535 int error;
1536
1537 bma->aeof = 0;
1538 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1539 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001540 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001541 return error;
1542
Dave Chinner6e708bc2013-11-22 10:41:16 +11001543 if (is_empty) {
1544 bma->aeof = 1;
1545 return 0;
1546 }
1547
Dave Chinner9e5987a2013-02-25 12:31:26 +11001548 /*
1549 * Check if we are allocation or past the last extent, or at least into
1550 * the last delayed allocated extent.
1551 */
1552 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1553 (bma->offset >= rec.br_startoff &&
1554 isnullstartblock(rec.br_startblock));
1555 return 0;
1556}
1557
1558/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001559 * Returns the file-relative block number of the first block past eof in
1560 * the file. This is not based on i_size, it is based on the extent records.
1561 * Returns 0 for local files, as they do not have extent records.
1562 */
1563int
1564xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001565 struct xfs_inode *ip,
1566 xfs_fileoff_t *last_block,
1567 int whichfork)
1568{
1569 struct xfs_bmbt_irec rec;
1570 int is_empty;
1571 int error;
1572
1573 *last_block = 0;
1574
1575 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1576 return 0;
1577
1578 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1579 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001580 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001581
1582 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1583 if (error || is_empty)
1584 return error;
1585
1586 *last_block = rec.br_startoff + rec.br_blockcount;
1587 return 0;
1588}
1589
1590/*
1591 * Returns whether the selected fork of the inode has exactly one
1592 * block or not. For the data fork we check this matches di_size,
1593 * implying the file's range is 0..bsize-1.
1594 */
1595int /* 1=>1 block, 0=>otherwise */
1596xfs_bmap_one_block(
1597 xfs_inode_t *ip, /* incore inode */
1598 int whichfork) /* data or attr fork */
1599{
1600 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1601 xfs_ifork_t *ifp; /* inode fork pointer */
1602 int rval; /* return value */
1603 xfs_bmbt_irec_t s; /* internal version of extent */
1604
1605#ifndef DEBUG
1606 if (whichfork == XFS_DATA_FORK)
1607 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1608#endif /* !DEBUG */
1609 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1610 return 0;
1611 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1612 return 0;
1613 ifp = XFS_IFORK_PTR(ip, whichfork);
1614 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1615 ep = xfs_iext_get_ext(ifp, 0);
1616 xfs_bmbt_get_all(ep, &s);
1617 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1618 if (rval && whichfork == XFS_DATA_FORK)
1619 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1620 return rval;
1621}
1622
1623/*
1624 * Extent tree manipulation functions used during allocation.
1625 */
1626
1627/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001628 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 */
1630STATIC int /* error */
1631xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001632 struct xfs_bmalloca *bma,
1633 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001635 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001637 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001640 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 xfs_fileoff_t new_endoff; /* end offset of new entry */
1642 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1643 /* left is 0, right is 1, prev is 2 */
1644 int rval=0; /* return value (logging flags) */
1645 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001646 xfs_filblks_t da_new; /* new count del alloc blocks used */
1647 xfs_filblks_t da_old; /* old count del alloc blocks used */
1648 xfs_filblks_t temp=0; /* value for da_new calculations */
1649 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001651 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001652 xfs_extnum_t *nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001654 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001655 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001656 ASSERT(whichfork != XFS_ATTR_FORK);
1657 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1658 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001659
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001660 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001661 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001662 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001663 ASSERT(!bma->cur ||
1664 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001665
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001666 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668#define LEFT r[0]
1669#define RIGHT r[1]
1670#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Darrick J. Wong60b49842016-10-03 09:11:34 -07001672 if (whichfork == XFS_COW_FORK)
1673 state |= BMAP_COWFORK;
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 /*
1676 * Set up a bunch of variables to make the tests simpler.
1677 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001678 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 xfs_bmbt_get_all(ep, &PREV);
1680 new_endoff = new->br_startoff + new->br_blockcount;
1681 ASSERT(PREV.br_startoff <= new->br_startoff);
1682 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001683
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001684 da_old = startblockval(PREV.br_startblock);
1685 da_new = 0;
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 /*
1688 * Set flags determining what part of the previous delayed allocation
1689 * extent is being replaced by a real allocation.
1690 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001691 if (PREV.br_startoff == new->br_startoff)
1692 state |= BMAP_LEFT_FILLING;
1693 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1694 state |= BMAP_RIGHT_FILLING;
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 /*
1697 * Check and set flags if this segment has a left neighbor.
1698 * Don't set contiguous if the combined extent would be too large.
1699 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001700 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001701 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001702 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001703
1704 if (isnullstartblock(LEFT.br_startblock))
1705 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001707
1708 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1709 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1710 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1711 LEFT.br_state == new->br_state &&
1712 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1713 state |= BMAP_LEFT_CONTIG;
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 /*
1716 * Check and set flags if this segment has a right neighbor.
1717 * Don't set contiguous if the combined extent would be too large.
1718 * Also check for all-three-contiguous being too large.
1719 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001720 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001721 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001722 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001723
1724 if (isnullstartblock(RIGHT.br_startblock))
1725 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001727
1728 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1729 new_endoff == RIGHT.br_startoff &&
1730 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1731 new->br_state == RIGHT.br_state &&
1732 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1733 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1734 BMAP_RIGHT_FILLING)) !=
1735 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1736 BMAP_RIGHT_FILLING) ||
1737 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1738 <= MAXEXTLEN))
1739 state |= BMAP_RIGHT_CONTIG;
1740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 error = 0;
1742 /*
1743 * Switch out based on the FILLING and CONTIG state bits.
1744 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001745 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1746 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1747 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1748 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 /*
1750 * Filling in all of a previously delayed allocation extent.
1751 * The left and right neighbors are both contiguous with new.
1752 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001753 bma->idx--;
1754 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1755 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 LEFT.br_blockcount + PREV.br_blockcount +
1757 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001758 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001759
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001760 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001761 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001762 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1764 else {
1765 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001766 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001768 RIGHT.br_blockcount, &i);
1769 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001771 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001772 error = xfs_btree_delete(bma->cur, &i);
1773 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001775 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001776 error = xfs_btree_decrement(bma->cur, 0, &i);
1777 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001779 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001780 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 LEFT.br_startblock,
1782 LEFT.br_blockcount +
1783 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001784 RIGHT.br_blockcount, LEFT.br_state);
1785 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 goto done;
1787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 break;
1789
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001790 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 /*
1792 * Filling in all of a previously delayed allocation extent.
1793 * The left neighbor is contiguous, the right is not.
1794 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001795 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001796
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001797 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1798 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001799 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001800 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001801
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001802 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1803 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 rval = XFS_ILOG_DEXT;
1805 else {
1806 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001807 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001809 &i);
1810 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001812 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001813 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 LEFT.br_startblock,
1815 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001816 PREV.br_blockcount, LEFT.br_state);
1817 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 goto done;
1819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 break;
1821
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001822 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 /*
1824 * Filling in all of a previously delayed allocation extent.
1825 * The right neighbor is contiguous, the left is not.
1826 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001827 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 xfs_bmbt_set_startblock(ep, new->br_startblock);
1829 xfs_bmbt_set_blockcount(ep,
1830 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001831 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001832
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001833 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1834 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 rval = XFS_ILOG_DEXT;
1836 else {
1837 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001838 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001840 RIGHT.br_blockcount, &i);
1841 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001843 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001844 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 new->br_startblock,
1846 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 RIGHT.br_blockcount, PREV.br_state);
1848 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto done;
1850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 break;
1852
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001853 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 /*
1855 * Filling in all of a previously delayed allocation extent.
1856 * Neither the left nor right neighbors are contiguous with
1857 * the new one.
1858 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001859 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001861 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001862
Darrick J. Wong60b49842016-10-03 09:11:34 -07001863 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001864 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1866 else {
1867 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001868 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001870 &i);
1871 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001873 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001874 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1875 error = xfs_btree_insert(bma->cur, &i);
1876 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001878 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 break;
1881
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001882 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 /*
1884 * Filling in the first part of a previous delayed allocation.
1885 * The left neighbor is contiguous.
1886 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001887 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1888 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 LEFT.br_blockcount + new->br_blockcount);
1890 xfs_bmbt_set_startoff(ep,
1891 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001892 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001895 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001897 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 rval = XFS_ILOG_DEXT;
1899 else {
1900 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001901 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001903 &i);
1904 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001906 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001907 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 LEFT.br_startblock,
1909 LEFT.br_blockcount +
1910 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001911 LEFT.br_state);
1912 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 goto done;
1914 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001915 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001916 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001917 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001918 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001919
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001920 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 break;
1922
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001923 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 /*
1925 * Filling in the first part of a previous delayed allocation.
1926 * The left neighbor is not contiguous.
1927 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001928 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 xfs_bmbt_set_startoff(ep, new_endoff);
1930 temp = PREV.br_blockcount - new->br_blockcount;
1931 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001932 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001933 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001934 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1936 else {
1937 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001938 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001940 &i);
1941 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001943 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001944 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1945 error = xfs_btree_insert(bma->cur, &i);
1946 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001948 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001950
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001951 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001952 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001953 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001954 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 rval |= tmp_rval;
1956 if (error)
1957 goto done;
1958 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001959 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001960 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001961 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1962 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001963 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001964 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 break;
1966
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001967 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 /*
1969 * Filling in the last part of a previous delayed allocation.
1970 * The right neighbor is contiguous with the new allocation.
1971 */
1972 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001973 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001975 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001976 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 new->br_blockcount + RIGHT.br_blockcount,
1978 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001979 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1980 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 rval = XFS_ILOG_DEXT;
1982 else {
1983 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001984 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001986 RIGHT.br_blockcount, &i);
1987 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001989 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001990 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 new->br_startblock,
1992 new->br_blockcount +
1993 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001994 RIGHT.br_state);
1995 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 goto done;
1997 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001998
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001999 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002000 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002001 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002002 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002003 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002004
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002005 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 break;
2007
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002008 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 /*
2010 * Filling in the last part of a previous delayed allocation.
2011 * The right neighbor is not contiguous.
2012 */
2013 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002014 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002016 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002017 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002018 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2020 else {
2021 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002022 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002024 &i);
2025 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002027 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002028 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2029 error = xfs_btree_insert(bma->cur, &i);
2030 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002032 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002034
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002035 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002036 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002037 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002038 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 rval |= tmp_rval;
2040 if (error)
2041 goto done;
2042 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002043 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002044 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002045 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2046 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002047 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002048 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002049
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002050 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 break;
2052
2053 case 0:
2054 /*
2055 * Filling in the middle part of a previous delayed allocation.
2056 * Contiguity is impossible here.
2057 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002058 *
2059 * We start with a delayed allocation:
2060 *
2061 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2062 * PREV @ idx
2063 *
2064 * and we are allocating:
2065 * +rrrrrrrrrrrrrrrrr+
2066 * new
2067 *
2068 * and we set it up for insertion as:
2069 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2070 * new
2071 * PREV @ idx LEFT RIGHT
2072 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 */
2074 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002076 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002077 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2078 LEFT = *new;
2079 RIGHT.br_state = PREV.br_state;
2080 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002081 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002082 RIGHT.br_startoff = new_endoff;
2083 RIGHT.br_blockcount = temp2;
2084 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002085 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002086 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002087 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2089 else {
2090 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002091 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002093 &i);
2094 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002096 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002097 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2098 error = xfs_btree_insert(bma->cur, &i);
2099 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002101 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002103
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002104 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002105 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002106 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002107 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 rval |= tmp_rval;
2109 if (error)
2110 goto done;
2111 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002112 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2113 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002114 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002115 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002116 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002117 error = xfs_mod_fdblocks(bma->ip->i_mount,
2118 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002119 ASSERT(!error);
2120 if (error)
2121 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002123
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002124 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002125 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002126 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2127 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2128 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002129 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002130 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002131
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002132 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002133 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 break;
2135
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002136 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2137 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2138 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2139 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2140 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2141 case BMAP_LEFT_CONTIG:
2142 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 /*
2144 * These cases are all impossible.
2145 */
2146 ASSERT(0);
2147 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002148
Darrick J. Wong9c194642016-08-03 12:16:05 +10002149 /* add reverse mapping */
2150 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2151 if (error)
2152 goto done;
2153
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002154 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002155 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002156 int tmp_logflags; /* partial log flag return val */
2157
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002158 ASSERT(bma->cur == NULL);
2159 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002160 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002161 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002162 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002163 if (error)
2164 goto done;
2165 }
2166
2167 /* adjust for changes in reserved delayed indirect blocks */
2168 if (da_old || da_new) {
2169 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002170 if (bma->cur)
2171 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002172 ASSERT(temp <= da_old);
2173 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002174 xfs_mod_fdblocks(bma->ip->i_mount,
2175 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002176 }
2177
2178 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002179 if (bma->cur)
2180 bma->cur->bc_private.b.allocated = 0;
2181
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002182 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002184 if (whichfork != XFS_COW_FORK)
2185 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return error;
2187#undef LEFT
2188#undef RIGHT
2189#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
2192/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002193 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 */
2195STATIC int /* error */
2196xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002197 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002199 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002201 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002202 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002203 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002204 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002207 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002210 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 xfs_fileoff_t new_endoff; /* end offset of new entry */
2212 xfs_exntst_t newext; /* new extent state */
2213 xfs_exntst_t oldext; /* old extent state */
2214 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2215 /* left is 0, right is 1, prev is 2 */
2216 int rval=0; /* return value (logging flags) */
2217 int state = 0;/* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002218 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002220 *logflagsp = 0;
2221
2222 cur = *curp;
2223 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2224
2225 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002226 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002227 ASSERT(!isnullstartblock(new->br_startblock));
2228
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002229 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231#define LEFT r[0]
2232#define RIGHT r[1]
2233#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 /*
2236 * Set up a bunch of variables to make the tests simpler.
2237 */
2238 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002239 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 xfs_bmbt_get_all(ep, &PREV);
2241 newext = new->br_state;
2242 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2243 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2244 ASSERT(PREV.br_state == oldext);
2245 new_endoff = new->br_startoff + new->br_blockcount;
2246 ASSERT(PREV.br_startoff <= new->br_startoff);
2247 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 /*
2250 * Set flags determining what part of the previous oldext allocation
2251 * extent is being replaced by a newext allocation.
2252 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002253 if (PREV.br_startoff == new->br_startoff)
2254 state |= BMAP_LEFT_FILLING;
2255 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2256 state |= BMAP_RIGHT_FILLING;
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 /*
2259 * Check and set flags if this segment has a left neighbor.
2260 * Don't set contiguous if the combined extent would be too large.
2261 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002262 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002263 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002264 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002265
2266 if (isnullstartblock(LEFT.br_startblock))
2267 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002269
2270 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2271 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2272 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2273 LEFT.br_state == newext &&
2274 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2275 state |= BMAP_LEFT_CONTIG;
2276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 /*
2278 * Check and set flags if this segment has a right neighbor.
2279 * Don't set contiguous if the combined extent would be too large.
2280 * Also check for all-three-contiguous being too large.
2281 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002282 if (*idx < xfs_iext_count(&ip->i_df) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002283 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002284 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002285 if (isnullstartblock(RIGHT.br_startblock))
2286 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002288
2289 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2290 new_endoff == RIGHT.br_startoff &&
2291 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2292 newext == RIGHT.br_state &&
2293 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2294 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2295 BMAP_RIGHT_FILLING)) !=
2296 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2297 BMAP_RIGHT_FILLING) ||
2298 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2299 <= MAXEXTLEN))
2300 state |= BMAP_RIGHT_CONTIG;
2301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 /*
2303 * Switch out based on the FILLING and CONTIG state bits.
2304 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002305 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2306 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2307 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2308 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 /*
2310 * Setting all of a previous oldext extent to newext.
2311 * The left and right neighbors are both contiguous with new.
2312 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002313 --*idx;
2314
2315 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2316 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 LEFT.br_blockcount + PREV.br_blockcount +
2318 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002319 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002320
Christoph Hellwigec90c552011-05-23 08:52:53 +00002321 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 ip->i_d.di_nextents -= 2;
2323 if (cur == NULL)
2324 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2325 else {
2326 rval = XFS_ILOG_CORE;
2327 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2328 RIGHT.br_startblock,
2329 RIGHT.br_blockcount, &i)))
2330 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002331 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002332 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002334 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002335 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002337 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2345 LEFT.br_startblock,
2346 LEFT.br_blockcount + PREV.br_blockcount +
2347 RIGHT.br_blockcount, LEFT.br_state)))
2348 goto done;
2349 }
2350 break;
2351
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002352 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 /*
2354 * Setting all of a previous oldext extent to newext.
2355 * The left neighbor is contiguous, the right is not.
2356 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002357 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002358
Christoph Hellwigec90c552011-05-23 08:52:53 +00002359 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2360 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2361 LEFT.br_blockcount + PREV.br_blockcount);
2362 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2363
2364 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 ip->i_d.di_nextents--;
2366 if (cur == NULL)
2367 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2368 else {
2369 rval = XFS_ILOG_CORE;
2370 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2371 PREV.br_startblock, PREV.br_blockcount,
2372 &i)))
2373 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002374 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002375 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002377 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002378 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002380 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2382 LEFT.br_startblock,
2383 LEFT.br_blockcount + PREV.br_blockcount,
2384 LEFT.br_state)))
2385 goto done;
2386 }
2387 break;
2388
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002389 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 /*
2391 * Setting all of a previous oldext extent to newext.
2392 * The right neighbor is contiguous, the left is not.
2393 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002394 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 xfs_bmbt_set_blockcount(ep,
2396 PREV.br_blockcount + RIGHT.br_blockcount);
2397 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002398 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2399 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 ip->i_d.di_nextents--;
2401 if (cur == NULL)
2402 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2403 else {
2404 rval = XFS_ILOG_CORE;
2405 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2406 RIGHT.br_startblock,
2407 RIGHT.br_blockcount, &i)))
2408 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002409 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002410 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002412 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002413 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002415 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2417 new->br_startblock,
2418 new->br_blockcount + RIGHT.br_blockcount,
2419 newext)))
2420 goto done;
2421 }
2422 break;
2423
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002424 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 /*
2426 * Setting all of a previous oldext extent to newext.
2427 * Neither the left nor right neighbors are contiguous with
2428 * the new one.
2429 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002430 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002432 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002433
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 if (cur == NULL)
2435 rval = XFS_ILOG_DEXT;
2436 else {
2437 rval = 0;
2438 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2439 new->br_startblock, new->br_blockcount,
2440 &i)))
2441 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002442 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2444 new->br_startblock, new->br_blockcount,
2445 newext)))
2446 goto done;
2447 }
2448 break;
2449
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002450 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 /*
2452 * Setting the first part of a previous oldext extent to newext.
2453 * The left neighbor is contiguous.
2454 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002455 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2456 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 LEFT.br_blockcount + new->br_blockcount);
2458 xfs_bmbt_set_startoff(ep,
2459 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002460 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002461
Christoph Hellwigec90c552011-05-23 08:52:53 +00002462 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 xfs_bmbt_set_startblock(ep,
2464 new->br_startblock + new->br_blockcount);
2465 xfs_bmbt_set_blockcount(ep,
2466 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002467 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002468
Christoph Hellwigec90c552011-05-23 08:52:53 +00002469 --*idx;
2470
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (cur == NULL)
2472 rval = XFS_ILOG_DEXT;
2473 else {
2474 rval = 0;
2475 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2476 PREV.br_startblock, PREV.br_blockcount,
2477 &i)))
2478 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002479 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 if ((error = xfs_bmbt_update(cur,
2481 PREV.br_startoff + new->br_blockcount,
2482 PREV.br_startblock + new->br_blockcount,
2483 PREV.br_blockcount - new->br_blockcount,
2484 oldext)))
2485 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002486 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002488 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 LEFT.br_startblock,
2490 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002491 LEFT.br_state);
2492 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 goto done;
2494 }
2495 break;
2496
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002497 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 /*
2499 * Setting the first part of a previous oldext extent to newext.
2500 * The left neighbor is not contiguous.
2501 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002502 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2504 xfs_bmbt_set_startoff(ep, new_endoff);
2505 xfs_bmbt_set_blockcount(ep,
2506 PREV.br_blockcount - new->br_blockcount);
2507 xfs_bmbt_set_startblock(ep,
2508 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002509 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002510
Christoph Hellwigec90c552011-05-23 08:52:53 +00002511 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 ip->i_d.di_nextents++;
2513 if (cur == NULL)
2514 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2515 else {
2516 rval = XFS_ILOG_CORE;
2517 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2518 PREV.br_startblock, PREV.br_blockcount,
2519 &i)))
2520 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002521 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 if ((error = xfs_bmbt_update(cur,
2523 PREV.br_startoff + new->br_blockcount,
2524 PREV.br_startblock + new->br_blockcount,
2525 PREV.br_blockcount - new->br_blockcount,
2526 oldext)))
2527 goto done;
2528 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002529 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002531 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
2533 break;
2534
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002535 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /*
2537 * Setting the last part of a previous oldext extent to newext.
2538 * The right neighbor is contiguous with the new allocation.
2539 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002540 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 xfs_bmbt_set_blockcount(ep,
2542 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002543 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2544
2545 ++*idx;
2546
2547 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2548 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002549 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002551 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002552
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (cur == NULL)
2554 rval = XFS_ILOG_DEXT;
2555 else {
2556 rval = 0;
2557 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2558 PREV.br_startblock,
2559 PREV.br_blockcount, &i)))
2560 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002561 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2563 PREV.br_startblock,
2564 PREV.br_blockcount - new->br_blockcount,
2565 oldext)))
2566 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002567 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 goto done;
2569 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2570 new->br_startblock,
2571 new->br_blockcount + RIGHT.br_blockcount,
2572 newext)))
2573 goto done;
2574 }
2575 break;
2576
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002577 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /*
2579 * Setting the last part of a previous oldext extent to newext.
2580 * The right neighbor is not contiguous.
2581 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002582 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 xfs_bmbt_set_blockcount(ep,
2584 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002585 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002586
Christoph Hellwigec90c552011-05-23 08:52:53 +00002587 ++*idx;
2588 xfs_iext_insert(ip, *idx, 1, new, state);
2589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 ip->i_d.di_nextents++;
2591 if (cur == NULL)
2592 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2593 else {
2594 rval = XFS_ILOG_CORE;
2595 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2596 PREV.br_startblock, PREV.br_blockcount,
2597 &i)))
2598 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002599 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2601 PREV.br_startblock,
2602 PREV.br_blockcount - new->br_blockcount,
2603 oldext)))
2604 goto done;
2605 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2606 new->br_startblock, new->br_blockcount,
2607 &i)))
2608 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002609 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002611 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002613 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 }
2615 break;
2616
2617 case 0:
2618 /*
2619 * Setting the middle part of a previous oldext extent to
2620 * newext. Contiguity is impossible here.
2621 * One extent becomes three extents.
2622 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002623 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 xfs_bmbt_set_blockcount(ep,
2625 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002626 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 r[0] = *new;
2629 r[1].br_startoff = new_endoff;
2630 r[1].br_blockcount =
2631 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2632 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2633 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002634
2635 ++*idx;
2636 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 ip->i_d.di_nextents += 2;
2639 if (cur == NULL)
2640 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2641 else {
2642 rval = XFS_ILOG_CORE;
2643 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2644 PREV.br_startblock, PREV.br_blockcount,
2645 &i)))
2646 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002647 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 /* new right extent - oldext */
2649 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2650 r[1].br_startblock, r[1].br_blockcount,
2651 r[1].br_state)))
2652 goto done;
2653 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002655 cur->bc_rec.b.br_blockcount =
2656 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002657 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002659 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002660 /*
2661 * Reset the cursor to the position of the new extent
2662 * we are about to insert as we can't trust it after
2663 * the previous insert.
2664 */
2665 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2666 new->br_startblock, new->br_blockcount,
2667 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002669 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002671 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002672 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002674 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
2676 break;
2677
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002678 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2679 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2680 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2681 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2682 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2683 case BMAP_LEFT_CONTIG:
2684 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 /*
2686 * These cases are all impossible.
2687 */
2688 ASSERT(0);
2689 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002690
Darrick J. Wong9c194642016-08-03 12:16:05 +10002691 /* update reverse mappings */
2692 error = xfs_rmap_convert_extent(mp, dfops, ip, XFS_DATA_FORK, new);
2693 if (error)
2694 goto done;
2695
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002696 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002697 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002698 int tmp_logflags; /* partial log flag return val */
2699
2700 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002701 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002702 0, &tmp_logflags, XFS_DATA_FORK);
2703 *logflagsp |= tmp_logflags;
2704 if (error)
2705 goto done;
2706 }
2707
2708 /* clear out the allocated field, done with it now in any case. */
2709 if (cur) {
2710 cur->bc_private.b.allocated = 0;
2711 *curp = cur;
2712 }
2713
2714 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002716 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 return error;
2718#undef LEFT
2719#undef RIGHT
2720#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
2723/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002724 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002726STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727xfs_bmap_add_extent_hole_delay(
2728 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002729 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002730 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002731 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002733 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2735 xfs_filblks_t newlen=0; /* new indirect size */
2736 xfs_filblks_t oldlen=0; /* old indirect size */
2737 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2738 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002739 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002741 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002743 if (whichfork == XFS_COW_FORK)
2744 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002745 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002746
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 /*
2748 * Check and set flags if this segment has a left neighbor
2749 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002750 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002751 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002752 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002753
2754 if (isnullstartblock(left.br_startblock))
2755 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 /*
2759 * Check and set flags if the current (right) segment exists.
2760 * If it doesn't exist, we're converting the hole at end-of-file.
2761 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002762 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002763 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002764 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002765
2766 if (isnullstartblock(right.br_startblock))
2767 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /*
2771 * Set contiguity flags on the left and right neighbors.
2772 * Don't let extents get too large, even if the pieces are contiguous.
2773 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002774 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2775 left.br_startoff + left.br_blockcount == new->br_startoff &&
2776 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2777 state |= BMAP_LEFT_CONTIG;
2778
2779 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2780 new->br_startoff + new->br_blockcount == right.br_startoff &&
2781 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2782 (!(state & BMAP_LEFT_CONTIG) ||
2783 (left.br_blockcount + new->br_blockcount +
2784 right.br_blockcount <= MAXEXTLEN)))
2785 state |= BMAP_RIGHT_CONTIG;
2786
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 /*
2788 * Switch out based on the contiguity flags.
2789 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002790 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2791 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 /*
2793 * New allocation is contiguous with delayed allocations
2794 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002795 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002797 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 temp = left.br_blockcount + new->br_blockcount +
2799 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002800
Christoph Hellwigec90c552011-05-23 08:52:53 +00002801 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2802 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002803 oldlen = startblockval(left.br_startblock) +
2804 startblockval(new->br_startblock) +
2805 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002807 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002808 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002809 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002810
Christoph Hellwigec90c552011-05-23 08:52:53 +00002811 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 break;
2813
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002814 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 /*
2816 * New allocation is contiguous with a delayed allocation
2817 * on the left.
2818 * Merge the new allocation with the left neighbor.
2819 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002820 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002822
2823 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2824 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002825 oldlen = startblockval(left.br_startblock) +
2826 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002828 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002829 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002830 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 break;
2832
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002833 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 /*
2835 * New allocation is contiguous with a delayed allocation
2836 * on the right.
2837 * Merge the new allocation with the right neighbor.
2838 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002839 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002841 oldlen = startblockval(new->br_startblock) +
2842 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002844 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2845 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002846 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002847 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 break;
2849
2850 case 0:
2851 /*
2852 * New allocation is not contiguous with another
2853 * delayed allocation.
2854 * Insert a new entry.
2855 */
2856 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002857 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 break;
2859 }
2860 if (oldlen != newlen) {
2861 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002862 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2863 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 /*
2865 * Nothing to do for disk quota accounting here.
2866 */
2867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868}
2869
2870/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002871 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 */
2873STATIC int /* error */
2874xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002875 struct xfs_bmalloca *bma,
2876 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002878 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 int i; /* temp state */
2881 xfs_ifork_t *ifp; /* inode fork pointer */
2882 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2883 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002884 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002886 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Eric Sandeenf1f96c42016-01-04 16:10:42 +11002888 mp = bma->ip->i_mount;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002889 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002890
Christoph Hellwigc6534242011-09-18 20:41:05 +00002891 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002892 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002893 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002894 ASSERT(!bma->cur ||
2895 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Darrick J. Wong3993bae2016-10-03 09:11:32 -07002896 ASSERT(whichfork != XFS_COW_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002897
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002898 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002899
2900 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002901 if (whichfork == XFS_ATTR_FORK)
2902 state |= BMAP_ATTRFORK;
2903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 /*
2905 * Check and set flags if this segment has a left neighbor.
2906 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002907 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002908 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002909 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002910 if (isnullstartblock(left.br_startblock))
2911 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002913
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 /*
2915 * Check and set flags if this segment has a current value.
2916 * Not true if we're inserting into the "hole" at eof.
2917 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002918 if (bma->idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002919 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002920 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002921 if (isnullstartblock(right.br_startblock))
2922 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002924
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 /*
2926 * We're inserting a real allocation between "left" and "right".
2927 * Set the contiguity flags. Don't let extents get too large.
2928 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002929 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2930 left.br_startoff + left.br_blockcount == new->br_startoff &&
2931 left.br_startblock + left.br_blockcount == new->br_startblock &&
2932 left.br_state == new->br_state &&
2933 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2934 state |= BMAP_LEFT_CONTIG;
2935
2936 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2937 new->br_startoff + new->br_blockcount == right.br_startoff &&
2938 new->br_startblock + new->br_blockcount == right.br_startblock &&
2939 new->br_state == right.br_state &&
2940 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2941 (!(state & BMAP_LEFT_CONTIG) ||
2942 left.br_blockcount + new->br_blockcount +
2943 right.br_blockcount <= MAXEXTLEN))
2944 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002946 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 /*
2948 * Select which case we're in here, and implement it.
2949 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002950 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2951 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 /*
2953 * New allocation is contiguous with real allocations on the
2954 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002955 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002957 --bma->idx;
2958 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2959 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 left.br_blockcount + new->br_blockcount +
2961 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002962 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002963
Christoph Hellwigc6534242011-09-18 20:41:05 +00002964 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002965
Christoph Hellwigc6534242011-09-18 20:41:05 +00002966 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
2967 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
2968 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002969 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002970 } else {
2971 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002972 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
2973 right.br_startblock, right.br_blockcount,
2974 &i);
2975 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002976 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002977 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002978 error = xfs_btree_delete(bma->cur, &i);
2979 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002980 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002981 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002982 error = xfs_btree_decrement(bma->cur, 0, &i);
2983 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002984 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002985 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002986 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002987 left.br_startblock,
2988 left.br_blockcount +
2989 new->br_blockcount +
2990 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002991 left.br_state);
2992 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002993 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002995 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002997 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 /*
2999 * New allocation is contiguous with a real allocation
3000 * on the left.
3001 * Merge the new allocation with the left neighbor.
3002 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003003 --bma->idx;
3004 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3005 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003007 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003008
Christoph Hellwigc6534242011-09-18 20:41:05 +00003009 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003010 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003011 } else {
3012 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003013 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3014 left.br_startblock, left.br_blockcount,
3015 &i);
3016 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003017 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003018 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003019 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003020 left.br_startblock,
3021 left.br_blockcount +
3022 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003023 left.br_state);
3024 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003025 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003027 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003029 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 /*
3031 * New allocation is contiguous with a real allocation
3032 * on the right.
3033 * Merge the new allocation with the right neighbor.
3034 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003035 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3036 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003037 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 new->br_blockcount + right.br_blockcount,
3039 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003040 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003041
Christoph Hellwigc6534242011-09-18 20:41:05 +00003042 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003043 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003044 } else {
3045 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003046 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003047 right.br_startoff,
3048 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003049 right.br_blockcount, &i);
3050 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003051 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003052 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003053 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003054 new->br_startblock,
3055 new->br_blockcount +
3056 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003057 right.br_state);
3058 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003059 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003061 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
3063 case 0:
3064 /*
3065 * New allocation is not contiguous with another
3066 * real allocation.
3067 * Insert a new entry.
3068 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003069 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3070 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3071 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3072 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003073 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003074 } else {
3075 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003076 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003077 new->br_startoff,
3078 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003079 new->br_blockcount, &i);
3080 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003081 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003082 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003083 bma->cur->bc_rec.b.br_state = new->br_state;
3084 error = xfs_btree_insert(bma->cur, &i);
3085 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003086 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003087 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003089 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003091
Darrick J. Wong9c194642016-08-03 12:16:05 +10003092 /* add reverse mapping */
3093 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
3094 if (error)
3095 goto done;
3096
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003097 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003098 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003099 int tmp_logflags; /* partial log flag return val */
3100
Christoph Hellwigc6534242011-09-18 20:41:05 +00003101 ASSERT(bma->cur == NULL);
3102 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003103 bma->firstblock, bma->dfops, &bma->cur,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003104 0, &tmp_logflags, whichfork);
3105 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003106 if (error)
3107 goto done;
3108 }
3109
3110 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003111 if (bma->cur)
3112 bma->cur->bc_private.b.allocated = 0;
3113
3114 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003115done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003116 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003117 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
Nathan Scottdd9f4382006-01-11 15:28:28 +11003120/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003121 * Functions used in the extent read, allocate and remove paths
3122 */
3123
3124/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003125 * Adjust the size of the new extent based on di_extsize and rt extsize.
3126 */
Dave Chinner68988112013-08-12 20:49:42 +10003127int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003128xfs_bmap_extsize_align(
3129 xfs_mount_t *mp,
3130 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3131 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3132 xfs_extlen_t extsz, /* align to this extent size */
3133 int rt, /* is this a realtime inode? */
3134 int eof, /* is extent at end-of-file? */
3135 int delay, /* creating delalloc extent? */
3136 int convert, /* overwriting unwritten extent? */
3137 xfs_fileoff_t *offp, /* in/out: aligned offset */
3138 xfs_extlen_t *lenp) /* in/out: aligned length */
3139{
3140 xfs_fileoff_t orig_off; /* original offset */
3141 xfs_extlen_t orig_alen; /* original length */
3142 xfs_fileoff_t orig_end; /* original off+len */
3143 xfs_fileoff_t nexto; /* next file offset */
3144 xfs_fileoff_t prevo; /* previous file offset */
3145 xfs_fileoff_t align_off; /* temp for offset */
3146 xfs_extlen_t align_alen; /* temp for length */
3147 xfs_extlen_t temp; /* temp for calculations */
3148
3149 if (convert)
3150 return 0;
3151
3152 orig_off = align_off = *offp;
3153 orig_alen = align_alen = *lenp;
3154 orig_end = orig_off + orig_alen;
3155
3156 /*
3157 * If this request overlaps an existing extent, then don't
3158 * attempt to perform any additional alignment.
3159 */
3160 if (!delay && !eof &&
3161 (orig_off >= gotp->br_startoff) &&
3162 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3163 return 0;
3164 }
3165
3166 /*
3167 * If the file offset is unaligned vs. the extent size
3168 * we need to align it. This will be possible unless
3169 * the file was previously written with a kernel that didn't
3170 * perform this alignment, or if a truncate shot us in the
3171 * foot.
3172 */
3173 temp = do_mod(orig_off, extsz);
3174 if (temp) {
3175 align_alen += temp;
3176 align_off -= temp;
3177 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003178
3179 /* Same adjustment for the end of the requested area. */
3180 temp = (align_alen % extsz);
3181 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003182 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003183
3184 /*
3185 * For large extent hint sizes, the aligned extent might be larger than
3186 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3187 * the length back under MAXEXTLEN. The outer allocation loops handle
3188 * short allocation just fine, so it is safe to do this. We only want to
3189 * do it when we are forced to, though, because it means more allocation
3190 * operations are required.
3191 */
3192 while (align_alen > MAXEXTLEN)
3193 align_alen -= extsz;
3194 ASSERT(align_alen <= MAXEXTLEN);
3195
Nathan Scottdd9f4382006-01-11 15:28:28 +11003196 /*
3197 * If the previous block overlaps with this proposed allocation
3198 * then move the start forward without adjusting the length.
3199 */
3200 if (prevp->br_startoff != NULLFILEOFF) {
3201 if (prevp->br_startblock == HOLESTARTBLOCK)
3202 prevo = prevp->br_startoff;
3203 else
3204 prevo = prevp->br_startoff + prevp->br_blockcount;
3205 } else
3206 prevo = 0;
3207 if (align_off != orig_off && align_off < prevo)
3208 align_off = prevo;
3209 /*
3210 * If the next block overlaps with this proposed allocation
3211 * then move the start back without adjusting the length,
3212 * but not before offset 0.
3213 * This may of course make the start overlap previous block,
3214 * and if we hit the offset 0 limit then the next block
3215 * can still overlap too.
3216 */
3217 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3218 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3219 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3220 nexto = gotp->br_startoff + gotp->br_blockcount;
3221 else
3222 nexto = gotp->br_startoff;
3223 } else
3224 nexto = NULLFILEOFF;
3225 if (!eof &&
3226 align_off + align_alen != orig_end &&
3227 align_off + align_alen > nexto)
3228 align_off = nexto > align_alen ? nexto - align_alen : 0;
3229 /*
3230 * If we're now overlapping the next or previous extent that
3231 * means we can't fit an extsz piece in this hole. Just move
3232 * the start forward to the first valid spot and set
3233 * the length so we hit the end.
3234 */
3235 if (align_off != orig_off && align_off < prevo)
3236 align_off = prevo;
3237 if (align_off + align_alen != orig_end &&
3238 align_off + align_alen > nexto &&
3239 nexto != NULLFILEOFF) {
3240 ASSERT(nexto > prevo);
3241 align_alen = nexto - align_off;
3242 }
3243
3244 /*
3245 * If realtime, and the result isn't a multiple of the realtime
3246 * extent size we need to remove blocks until it is.
3247 */
3248 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3249 /*
3250 * We're not covering the original request, or
3251 * we won't be able to once we fix the length.
3252 */
3253 if (orig_off < align_off ||
3254 orig_end > align_off + align_alen ||
3255 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003256 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003257 /*
3258 * Try to fix it by moving the start up.
3259 */
3260 if (align_off + temp <= orig_off) {
3261 align_alen -= temp;
3262 align_off += temp;
3263 }
3264 /*
3265 * Try to fix it by moving the end in.
3266 */
3267 else if (align_off + align_alen - temp >= orig_end)
3268 align_alen -= temp;
3269 /*
3270 * Set the start to the minimum then trim the length.
3271 */
3272 else {
3273 align_alen -= orig_off - align_off;
3274 align_off = orig_off;
3275 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3276 }
3277 /*
3278 * Result doesn't cover the request, fail it.
3279 */
3280 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003281 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003282 } else {
3283 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003284 /* see MAXEXTLEN handling above */
3285 ASSERT(orig_end <= align_off + align_alen ||
3286 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003287 }
3288
3289#ifdef DEBUG
3290 if (!eof && gotp->br_startoff != NULLFILEOFF)
3291 ASSERT(align_off + align_alen <= gotp->br_startoff);
3292 if (prevp->br_startoff != NULLFILEOFF)
3293 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3294#endif
3295
3296 *lenp = align_alen;
3297 *offp = align_off;
3298 return 0;
3299}
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301#define XFS_ALLOC_GAP_UNITS 4
3302
Dave Chinner68988112013-08-12 20:49:42 +10003303void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003304xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003305 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
3307 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3309 xfs_mount_t *mp; /* mount point structure */
3310 int nullfb; /* true if ap->firstblock isn't set */
3311 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
3313#define ISVALID(x,y) \
3314 (rt ? \
3315 (x) < mp->m_sb.sb_rblocks : \
3316 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3317 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3318 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003321 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003322 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3323 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003324 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 /*
3326 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003327 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003329 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3330 !isnullstartblock(ap->prev.br_startblock) &&
3331 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3332 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003333 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 /*
3335 * Adjust for the gap between prevp and us.
3336 */
Dave Chinner3a756672011-09-18 20:40:58 +00003337 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003338 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003340 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3341 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 }
3343 /*
3344 * If not at eof, then compare the two neighbor blocks.
3345 * Figure out whether either one gives us a good starting point,
3346 * and pick the better one.
3347 */
3348 else if (!ap->eof) {
3349 xfs_fsblock_t gotbno; /* right side block number */
3350 xfs_fsblock_t gotdiff=0; /* right side difference */
3351 xfs_fsblock_t prevbno; /* left side block number */
3352 xfs_fsblock_t prevdiff=0; /* left side difference */
3353
3354 /*
3355 * If there's a previous (left) block, select a requested
3356 * start block based on it.
3357 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003358 if (ap->prev.br_startoff != NULLFILEOFF &&
3359 !isnullstartblock(ap->prev.br_startblock) &&
3360 (prevbno = ap->prev.br_startblock +
3361 ap->prev.br_blockcount) &&
3362 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 /*
3364 * Calculate gap to end of previous block.
3365 */
Dave Chinner3a756672011-09-18 20:40:58 +00003366 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003367 (ap->prev.br_startoff +
3368 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 /*
3370 * Figure the startblock based on the previous block's
3371 * end and the gap size.
3372 * Heuristic!
3373 * If the gap is large relative to the piece we're
3374 * allocating, or using it gives us an invalid block
3375 * number, then just use the end of the previous block.
3376 */
Dave Chinner3a756672011-09-18 20:40:58 +00003377 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003379 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 prevbno += adjust;
3381 else
3382 prevdiff += adjust;
3383 /*
3384 * If the firstblock forbids it, can't use it,
3385 * must use default.
3386 */
3387 if (!rt && !nullfb &&
3388 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3389 prevbno = NULLFSBLOCK;
3390 }
3391 /*
3392 * No previous block or can't follow it, just default.
3393 */
3394 else
3395 prevbno = NULLFSBLOCK;
3396 /*
3397 * If there's a following (right) block, select a requested
3398 * start block based on it.
3399 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003400 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 /*
3402 * Calculate gap to start of next block.
3403 */
Dave Chinner3a756672011-09-18 20:40:58 +00003404 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 /*
3406 * Figure the startblock based on the next block's
3407 * start and the gap size.
3408 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003409 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 /*
3411 * Heuristic!
3412 * If the gap is large relative to the piece we're
3413 * allocating, or using it gives us an invalid block
3414 * number, then just use the start of the next block
3415 * offset by our length.
3416 */
Dave Chinner3a756672011-09-18 20:40:58 +00003417 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 ISVALID(gotbno - gotdiff, gotbno))
3419 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003420 else if (ISVALID(gotbno - ap->length, gotbno)) {
3421 gotbno -= ap->length;
3422 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 } else
3424 gotdiff += adjust;
3425 /*
3426 * If the firstblock forbids it, can't use it,
3427 * must use default.
3428 */
3429 if (!rt && !nullfb &&
3430 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3431 gotbno = NULLFSBLOCK;
3432 }
3433 /*
3434 * No next block, just default.
3435 */
3436 else
3437 gotbno = NULLFSBLOCK;
3438 /*
3439 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003440 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 */
3442 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003443 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003445 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003447 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003449#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003450}
3451
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003452static int
3453xfs_bmap_longest_free_extent(
3454 struct xfs_trans *tp,
3455 xfs_agnumber_t ag,
3456 xfs_extlen_t *blen,
3457 int *notinit)
3458{
3459 struct xfs_mount *mp = tp->t_mountp;
3460 struct xfs_perag *pag;
3461 xfs_extlen_t longest;
3462 int error = 0;
3463
3464 pag = xfs_perag_get(mp, ag);
3465 if (!pag->pagf_init) {
3466 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3467 if (error)
3468 goto out;
3469
3470 if (!pag->pagf_init) {
3471 *notinit = 1;
3472 goto out;
3473 }
3474 }
3475
Dave Chinner50adbcb2015-06-22 10:04:31 +10003476 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003477 xfs_alloc_min_freelist(mp, pag),
3478 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003479 if (*blen < longest)
3480 *blen = longest;
3481
3482out:
3483 xfs_perag_put(pag);
3484 return error;
3485}
3486
3487static void
3488xfs_bmap_select_minlen(
3489 struct xfs_bmalloca *ap,
3490 struct xfs_alloc_arg *args,
3491 xfs_extlen_t *blen,
3492 int notinit)
3493{
3494 if (notinit || *blen < ap->minlen) {
3495 /*
3496 * Since we did a BUF_TRYLOCK above, it is possible that
3497 * there is space for this request.
3498 */
3499 args->minlen = ap->minlen;
3500 } else if (*blen < args->maxlen) {
3501 /*
3502 * If the best seen length is less than the request length,
3503 * use the best as the minimum.
3504 */
3505 args->minlen = *blen;
3506 } else {
3507 /*
3508 * Otherwise we've seen an extent as big as maxlen, use that
3509 * as the minimum.
3510 */
3511 args->minlen = args->maxlen;
3512 }
3513}
3514
Nathan Scotta365bdd2006-03-14 13:34:16 +11003515STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003516xfs_bmap_btalloc_nullfb(
3517 struct xfs_bmalloca *ap,
3518 struct xfs_alloc_arg *args,
3519 xfs_extlen_t *blen)
3520{
3521 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003522 xfs_agnumber_t ag, startag;
3523 int notinit = 0;
3524 int error;
3525
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003526 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003527 args->total = ap->total;
3528
Christoph Hellwigc467c042010-02-15 23:34:42 +00003529 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3530 if (startag == NULLAGNUMBER)
3531 startag = ag = 0;
3532
Dave Chinner14b064c2011-01-27 12:16:28 +11003533 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003534 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3535 &notinit);
3536 if (error)
3537 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003538
Christoph Hellwigc467c042010-02-15 23:34:42 +00003539 if (++ag == mp->m_sb.sb_agcount)
3540 ag = 0;
3541 if (ag == startag)
3542 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003543 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003544
3545 xfs_bmap_select_minlen(ap, args, blen, notinit);
3546 return 0;
3547}
3548
3549STATIC int
3550xfs_bmap_btalloc_filestreams(
3551 struct xfs_bmalloca *ap,
3552 struct xfs_alloc_arg *args,
3553 xfs_extlen_t *blen)
3554{
3555 struct xfs_mount *mp = ap->ip->i_mount;
3556 xfs_agnumber_t ag;
3557 int notinit = 0;
3558 int error;
3559
3560 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3561 args->total = ap->total;
3562
3563 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3564 if (ag == NULLAGNUMBER)
3565 ag = 0;
3566
3567 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3568 if (error)
3569 return error;
3570
3571 if (*blen < args->maxlen) {
3572 error = xfs_filestream_new_ag(ap, &ag);
3573 if (error)
3574 return error;
3575
3576 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3577 &notinit);
3578 if (error)
3579 return error;
3580
3581 }
3582
3583 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003584
3585 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003586 * Set the failure fallback case to look in the selected AG as stream
3587 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003588 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003589 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003590 return 0;
3591}
3592
3593STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003594xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003595 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003596{
3597 xfs_mount_t *mp; /* mount point structure */
3598 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003599 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003600 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003601 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003602 xfs_alloc_arg_t args;
3603 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003604 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003605 int nullfb; /* true if ap->firstblock isn't set */
3606 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003607 int tryagain;
3608 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003609 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003610
Dave Chinnera99ebf42011-12-01 11:24:20 +00003611 ASSERT(ap->length);
3612
Nathan Scotta365bdd2006-03-14 13:34:16 +11003613 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003614
3615 /* stripe alignment for allocation is determined by mount parameters */
3616 stripe_align = 0;
3617 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3618 stripe_align = mp->m_swidth;
3619 else if (mp->m_dalign)
3620 stripe_align = mp->m_dalign;
3621
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003622 if (ap->flags & XFS_BMAPI_COWFORK)
3623 align = xfs_get_cowextsz_hint(ap->ip);
3624 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003625 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003626 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003627 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003628 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003629 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003630 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003631 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003632 }
Dave Chinner33177f052013-12-12 16:34:36 +11003633
3634
Dave Chinner0937e0f2011-09-18 20:40:57 +00003635 nullfb = *ap->firstblock == NULLFSBLOCK;
3636 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003637 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003638 if (xfs_alloc_is_userdata(ap->datatype) &&
3639 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003640 ag = xfs_filestream_lookup_ag(ap->ip);
3641 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003642 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003643 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003644 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003645 }
3646 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003647 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003648
3649 xfs_bmap_adjacent(ap);
3650
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003652 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 * it's in the right allocation group.
3654 */
Dave Chinner3a756672011-09-18 20:40:58 +00003655 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 ;
3657 else
Dave Chinner3a756672011-09-18 20:40:58 +00003658 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 * Normal allocation, done through xfs_alloc_vextent.
3661 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003662 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003663 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003664 args.tp = ap->tp;
3665 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003666 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003667 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003668
3669 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003670 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003671 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003672 blen = 0;
3673 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003674 /*
3675 * Search for an allocation group with a single extent large
3676 * enough for the request. If one isn't found, then adjust
3677 * the minimum allocation size to the largest space found.
3678 */
Dave Chinner292378e2016-09-26 08:21:28 +10003679 if (xfs_alloc_is_userdata(ap->datatype) &&
3680 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003681 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3682 else
3683 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003684 if (error)
3685 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003686 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003687 if (xfs_inode_is_filestream(ap->ip))
3688 args.type = XFS_ALLOCTYPE_FIRST_AG;
3689 else
3690 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003691 args.total = args.minlen = ap->minlen;
3692 } else {
3693 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3694 args.total = ap->total;
3695 args.minlen = ap->minlen;
3696 }
David Chinner957d0eb2007-06-18 16:50:37 +10003697 /* apply extent size hints if obtained earlier */
3698 if (unlikely(align)) {
3699 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003700 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003701 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003702 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003703 args.prod = 1;
3704 args.mod = 0;
3705 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003706 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003707 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003708 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3709 }
3710 /*
3711 * If we are not low on available data blocks, and the
3712 * underlying logical volume manager is a stripe, and
3713 * the file offset is zero then try to allocate data
3714 * blocks on stripe unit boundary.
3715 * NOTE: ap->aeof is only set if the allocation length
3716 * is >= the stripe unit and the allocation offset is
3717 * at the end of file.
3718 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003719 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003720 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003721 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003722 atype = args.type;
3723 isaligned = 1;
3724 /*
3725 * Adjust for alignment
3726 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003727 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003728 args.minlen = blen - args.alignment;
3729 args.minalignslop = 0;
3730 } else {
3731 /*
3732 * First try an exact bno allocation.
3733 * If it fails then do a near or start bno
3734 * allocation with alignment turned on.
3735 */
3736 atype = args.type;
3737 tryagain = 1;
3738 args.type = XFS_ALLOCTYPE_THIS_BNO;
3739 args.alignment = 1;
3740 /*
3741 * Compute the minlen+alignment for the
3742 * next case. Set slop so that the value
3743 * of minlen+alignment+slop doesn't go up
3744 * between the calls.
3745 */
Dave Chinner33177f052013-12-12 16:34:36 +11003746 if (blen > stripe_align && blen <= args.maxlen)
3747 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003748 else
3749 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003750 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003751 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003752 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003753 args.minlen - 1;
3754 else
3755 args.minalignslop = 0;
3756 }
3757 } else {
3758 args.alignment = 1;
3759 args.minalignslop = 0;
3760 }
3761 args.minleft = ap->minleft;
3762 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003763 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003764 args.datatype = ap->datatype;
3765 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003766 args.ip = ap->ip;
3767
3768 error = xfs_alloc_vextent(&args);
3769 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003770 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003771
Nathan Scotta365bdd2006-03-14 13:34:16 +11003772 if (tryagain && args.fsbno == NULLFSBLOCK) {
3773 /*
3774 * Exact allocation failed. Now try with alignment
3775 * turned on.
3776 */
3777 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003778 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003779 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003780 args.minlen = nextminlen;
3781 args.minalignslop = 0;
3782 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 if ((error = xfs_alloc_vextent(&args)))
3784 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003785 }
3786 if (isaligned && args.fsbno == NULLFSBLOCK) {
3787 /*
3788 * allocation failed, so turn off alignment and
3789 * try again.
3790 */
3791 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003792 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003793 args.alignment = 0;
3794 if ((error = xfs_alloc_vextent(&args)))
3795 return error;
3796 }
3797 if (args.fsbno == NULLFSBLOCK && nullfb &&
3798 args.minlen > ap->minlen) {
3799 args.minlen = ap->minlen;
3800 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003801 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003802 if ((error = xfs_alloc_vextent(&args)))
3803 return error;
3804 }
3805 if (args.fsbno == NULLFSBLOCK && nullfb) {
3806 args.fsbno = 0;
3807 args.type = XFS_ALLOCTYPE_FIRST_AG;
3808 args.total = ap->minlen;
3809 args.minleft = 0;
3810 if ((error = xfs_alloc_vextent(&args)))
3811 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003812 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003813 }
3814 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003815 /*
3816 * check the allocation happened at the same or higher AG than
3817 * the first block that was allocated.
3818 */
3819 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3820 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3821 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003822 (ap->dfops->dop_low &&
Dave Chinner0937e0f2011-09-18 20:40:57 +00003823 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3824 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3825
Dave Chinner3a756672011-09-18 20:40:58 +00003826 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003827 if (*ap->firstblock == NULLFSBLOCK)
3828 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003829 ASSERT(nullfb || fb_agno == args.agno ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003830 (ap->dfops->dop_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003831 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003832 if (!(ap->flags & XFS_BMAPI_COWFORK))
3833 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003834 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3835 if (ap->wasdel)
3836 ap->ip->i_delayed_blks -= args.len;
3837 /*
3838 * Adjust the disk quota also. This was reserved
3839 * earlier.
3840 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003841 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003842 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3843 XFS_TRANS_DQ_BCOUNT,
3844 (long) args.len);
3845 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003846 ap->blkno = NULLFSBLOCK;
3847 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 }
3849 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003850}
3851
3852/*
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003853 * For a remap operation, just "allocate" an extent at the address that the
3854 * caller passed in, and ensure that the AGFL is the right size. The caller
3855 * will then map the "allocated" extent into the file somewhere.
3856 */
3857STATIC int
3858xfs_bmap_remap_alloc(
3859 struct xfs_bmalloca *ap)
3860{
3861 struct xfs_trans *tp = ap->tp;
3862 struct xfs_mount *mp = tp->t_mountp;
3863 xfs_agblock_t bno;
3864 struct xfs_alloc_arg args;
3865 int error;
3866
3867 /*
3868 * validate that the block number is legal - the enables us to detect
3869 * and handle a silent filesystem corruption rather than crashing.
3870 */
3871 memset(&args, 0, sizeof(struct xfs_alloc_arg));
3872 args.tp = ap->tp;
3873 args.mp = ap->tp->t_mountp;
3874 bno = *ap->firstblock;
3875 args.agno = XFS_FSB_TO_AGNO(mp, bno);
3876 args.agbno = XFS_FSB_TO_AGBNO(mp, bno);
3877 if (args.agno >= mp->m_sb.sb_agcount ||
3878 args.agbno >= mp->m_sb.sb_agblocks)
3879 return -EFSCORRUPTED;
3880
3881 /* "Allocate" the extent from the range we passed in. */
3882 trace_xfs_bmap_remap_alloc(ap->ip, *ap->firstblock, ap->length);
3883 ap->blkno = bno;
3884 ap->ip->i_d.di_nblocks += ap->length;
3885 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3886
3887 /* Fix the freelist, like a real allocator does. */
3888 args.datatype = ap->datatype;
3889 args.pag = xfs_perag_get(args.mp, args.agno);
3890 ASSERT(args.pag);
3891
3892 /*
3893 * The freelist fixing code will decline the allocation if
3894 * the size and shape of the free space doesn't allow for
3895 * allocating the extent and updating all the metadata that
3896 * happens during an allocation. We're remapping, not
3897 * allocating, so skip that check by pretending to be freeing.
3898 */
3899 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003900 xfs_perag_put(args.pag);
3901 if (error)
3902 trace_xfs_bmap_remap_alloc_error(ap->ip, error, _RET_IP_);
3903 return error;
3904}
3905
3906/*
Nathan Scotta365bdd2006-03-14 13:34:16 +11003907 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3908 * It figures out where to ask the underlying allocator to put the new extent.
3909 */
3910STATIC int
3911xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003912 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003913{
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003914 if (ap->flags & XFS_BMAPI_REMAP)
3915 return xfs_bmap_remap_alloc(ap);
Dave Chinner292378e2016-09-26 08:21:28 +10003916 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3917 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003918 return xfs_bmap_rtalloc(ap);
3919 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920}
3921
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003922/* Trim extent to fit a logical block range. */
3923void
3924xfs_trim_extent(
3925 struct xfs_bmbt_irec *irec,
3926 xfs_fileoff_t bno,
3927 xfs_filblks_t len)
3928{
3929 xfs_fileoff_t distance;
3930 xfs_fileoff_t end = bno + len;
3931
3932 if (irec->br_startoff + irec->br_blockcount <= bno ||
3933 irec->br_startoff >= end) {
3934 irec->br_blockcount = 0;
3935 return;
3936 }
3937
3938 if (irec->br_startoff < bno) {
3939 distance = bno - irec->br_startoff;
3940 if (isnullstartblock(irec->br_startblock))
3941 irec->br_startblock = DELAYSTARTBLOCK;
3942 if (irec->br_startblock != DELAYSTARTBLOCK &&
3943 irec->br_startblock != HOLESTARTBLOCK)
3944 irec->br_startblock += distance;
3945 irec->br_startoff += distance;
3946 irec->br_blockcount -= distance;
3947 }
3948
3949 if (end < irec->br_startoff + irec->br_blockcount) {
3950 distance = irec->br_startoff + irec->br_blockcount - end;
3951 irec->br_blockcount -= distance;
3952 }
3953}
3954
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003956 * Trim the returned map to the required bounds
3957 */
3958STATIC void
3959xfs_bmapi_trim_map(
3960 struct xfs_bmbt_irec *mval,
3961 struct xfs_bmbt_irec *got,
3962 xfs_fileoff_t *bno,
3963 xfs_filblks_t len,
3964 xfs_fileoff_t obno,
3965 xfs_fileoff_t end,
3966 int n,
3967 int flags)
3968{
3969 if ((flags & XFS_BMAPI_ENTIRE) ||
3970 got->br_startoff + got->br_blockcount <= obno) {
3971 *mval = *got;
3972 if (isnullstartblock(got->br_startblock))
3973 mval->br_startblock = DELAYSTARTBLOCK;
3974 return;
3975 }
3976
3977 if (obno > *bno)
3978 *bno = obno;
3979 ASSERT((*bno >= obno) || (n == 0));
3980 ASSERT(*bno < end);
3981 mval->br_startoff = *bno;
3982 if (isnullstartblock(got->br_startblock))
3983 mval->br_startblock = DELAYSTARTBLOCK;
3984 else
3985 mval->br_startblock = got->br_startblock +
3986 (*bno - got->br_startoff);
3987 /*
3988 * Return the minimum of what we got and what we asked for for
3989 * the length. We can use the len variable here because it is
3990 * modified below and we could have been there before coming
3991 * here if the first part of the allocation didn't overlap what
3992 * was asked for.
3993 */
3994 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3995 got->br_blockcount - (*bno - got->br_startoff));
3996 mval->br_state = got->br_state;
3997 ASSERT(mval->br_blockcount <= len);
3998 return;
3999}
4000
4001/*
4002 * Update and validate the extent map to return
4003 */
4004STATIC void
4005xfs_bmapi_update_map(
4006 struct xfs_bmbt_irec **map,
4007 xfs_fileoff_t *bno,
4008 xfs_filblks_t *len,
4009 xfs_fileoff_t obno,
4010 xfs_fileoff_t end,
4011 int *n,
4012 int flags)
4013{
4014 xfs_bmbt_irec_t *mval = *map;
4015
4016 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4017 ((mval->br_startoff + mval->br_blockcount) <= end));
4018 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4019 (mval->br_startoff < obno));
4020
4021 *bno = mval->br_startoff + mval->br_blockcount;
4022 *len = end - *bno;
4023 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4024 /* update previous map with new information */
4025 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4026 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4027 ASSERT(mval->br_state == mval[-1].br_state);
4028 mval[-1].br_blockcount = mval->br_blockcount;
4029 mval[-1].br_state = mval->br_state;
4030 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4031 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4032 mval[-1].br_startblock != HOLESTARTBLOCK &&
4033 mval->br_startblock == mval[-1].br_startblock +
4034 mval[-1].br_blockcount &&
4035 ((flags & XFS_BMAPI_IGSTATE) ||
4036 mval[-1].br_state == mval->br_state)) {
4037 ASSERT(mval->br_startoff ==
4038 mval[-1].br_startoff + mval[-1].br_blockcount);
4039 mval[-1].br_blockcount += mval->br_blockcount;
4040 } else if (*n > 0 &&
4041 mval->br_startblock == DELAYSTARTBLOCK &&
4042 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4043 mval->br_startoff ==
4044 mval[-1].br_startoff + mval[-1].br_blockcount) {
4045 mval[-1].br_blockcount += mval->br_blockcount;
4046 mval[-1].br_state = mval->br_state;
4047 } else if (!((*n == 0) &&
4048 ((mval->br_startoff + mval->br_blockcount) <=
4049 obno))) {
4050 mval++;
4051 (*n)++;
4052 }
4053 *map = mval;
4054}
4055
4056/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004057 * Map file blocks to filesystem blocks without allocation.
4058 */
4059int
4060xfs_bmapi_read(
4061 struct xfs_inode *ip,
4062 xfs_fileoff_t bno,
4063 xfs_filblks_t len,
4064 struct xfs_bmbt_irec *mval,
4065 int *nmap,
4066 int flags)
4067{
4068 struct xfs_mount *mp = ip->i_mount;
4069 struct xfs_ifork *ifp;
4070 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004071 xfs_fileoff_t obno;
4072 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11004073 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004074 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11004075 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004076 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004077 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004078
4079 ASSERT(*nmap >= 1);
4080 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004081 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004082 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004083
4084 if (unlikely(XFS_TEST_ERROR(
4085 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4086 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4087 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4088 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004089 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004090 }
4091
4092 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004093 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004094
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004095 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004096
4097 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004098
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004099 /* No CoW fork? Return a hole. */
4100 if (whichfork == XFS_COW_FORK && !ifp) {
4101 mval->br_startoff = bno;
4102 mval->br_startblock = HOLESTARTBLOCK;
4103 mval->br_blockcount = len;
4104 mval->br_state = XFS_EXT_NORM;
4105 *nmap = 1;
4106 return 0;
4107 }
4108
Dave Chinner5c8ed202011-09-18 20:40:45 +00004109 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4110 error = xfs_iread_extents(NULL, ip, whichfork);
4111 if (error)
4112 return error;
4113 }
4114
Christoph Hellwig334f3422016-11-24 11:39:43 +11004115 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4116 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004117 end = bno + len;
4118 obno = bno;
4119
4120 while (bno < end && n < *nmap) {
4121 /* Reading past eof, act as though there's a hole up to end. */
4122 if (eof)
4123 got.br_startoff = end;
4124 if (got.br_startoff > bno) {
4125 /* Reading in a hole. */
4126 mval->br_startoff = bno;
4127 mval->br_startblock = HOLESTARTBLOCK;
4128 mval->br_blockcount =
4129 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4130 mval->br_state = XFS_EXT_NORM;
4131 bno += mval->br_blockcount;
4132 len -= mval->br_blockcount;
4133 mval++;
4134 n++;
4135 continue;
4136 }
4137
4138 /* set up the extent map to return. */
4139 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4140 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4141
4142 /* If we're done, stop now. */
4143 if (bno >= end || n >= *nmap)
4144 break;
4145
4146 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004147 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4148 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004149 }
4150 *nmap = n;
4151 return 0;
4152}
4153
Christoph Hellwig51446f52016-09-19 11:10:21 +10004154int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004155xfs_bmapi_reserve_delalloc(
4156 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004157 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004158 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004159 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004160 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004161 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004162 xfs_extnum_t *lastx,
4163 int eof)
4164{
4165 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004166 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004167 xfs_extlen_t alen;
4168 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004169 char rt = XFS_IS_REALTIME_INODE(ip);
4170 xfs_extlen_t extsz;
4171 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004172 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004173
Brian Foster974ae922016-11-28 14:57:42 +11004174 /*
4175 * Cap the alloc length. Keep track of prealloc so we know whether to
4176 * tag the inode before we return.
4177 */
4178 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004179 if (!eof)
4180 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004181 if (prealloc && alen >= len)
4182 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004183
4184 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004185 if (whichfork == XFS_COW_FORK)
4186 extsz = xfs_get_cowextsz_hint(ip);
4187 else
4188 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004189 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004190 struct xfs_bmbt_irec prev;
4191
4192 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4193 prev.br_startoff = NULLFILEOFF;
4194
4195 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004196 1, 0, &aoff, &alen);
4197 ASSERT(!error);
4198 }
4199
4200 if (rt)
4201 extsz = alen / mp->m_sb.sb_rextsize;
4202
4203 /*
4204 * Make a transaction-less quota reservation for delayed allocation
4205 * blocks. This number gets adjusted later. We return if we haven't
4206 * allocated blocks already inside this loop.
4207 */
4208 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4209 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4210 if (error)
4211 return error;
4212
4213 /*
4214 * Split changing sb for alen and indlen since they could be coming
4215 * from different places.
4216 */
4217 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4218 ASSERT(indlen > 0);
4219
4220 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004221 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004222 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004223 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004224 }
4225
4226 if (error)
4227 goto out_unreserve_quota;
4228
Dave Chinner0d485ad2015-02-23 21:22:03 +11004229 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004230 if (error)
4231 goto out_unreserve_blocks;
4232
4233
4234 ip->i_delayed_blks += alen;
4235
4236 got->br_startoff = aoff;
4237 got->br_startblock = nullstartblock(indlen);
4238 got->br_blockcount = alen;
4239 got->br_state = XFS_EXT_NORM;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004240 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004241
4242 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004243 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4244 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004245 */
4246 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4247
Brian Foster974ae922016-11-28 14:57:42 +11004248 /*
4249 * Tag the inode if blocks were preallocated. Note that COW fork
4250 * preallocation can occur at the start or end of the extent, even when
4251 * prealloc == 0, so we must also check the aligned offset and length.
4252 */
4253 if (whichfork == XFS_DATA_FORK && prealloc)
4254 xfs_inode_set_eofblocks_tag(ip);
4255 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4256 xfs_inode_set_cowblocks_tag(ip);
4257
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004258 ASSERT(got->br_startoff <= aoff);
4259 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4260 ASSERT(isnullstartblock(got->br_startblock));
4261 ASSERT(got->br_state == XFS_EXT_NORM);
4262 return 0;
4263
4264out_unreserve_blocks:
4265 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004266 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004267 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004268 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004269out_unreserve_quota:
4270 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004271 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004272 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4273 return error;
4274}
4275
Dave Chinnercf11da92014-07-15 07:08:24 +10004276static int
4277xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004278 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004279{
4280 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004281 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004282 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004283 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004284 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004285
Dave Chinnera99ebf42011-12-01 11:24:20 +00004286 ASSERT(bma->length > 0);
4287
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004288 /*
4289 * For the wasdelay case, we could also just allocate the stuff asked
4290 * for in this bmap call but that wouldn't be as good.
4291 */
4292 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004293 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4294 bma->offset = bma->got.br_startoff;
Christoph Hellwig0e8d6302016-11-24 11:40:32 +11004295 if (bma->idx) {
Dave Chinnere0c3da52011-09-18 20:41:01 +00004296 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004297 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004298 }
4299 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004300 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004301 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004302 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004303 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004304 }
4305
4306 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004307 * Set the data type being allocated. For the data fork, the first data
4308 * in the file is treated differently to all other allocations. For the
4309 * attribute fork, we only need to ensure the allocated range is not on
4310 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004311 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004312 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004313 bma->datatype = XFS_ALLOC_NOBUSY;
4314 if (whichfork == XFS_DATA_FORK) {
4315 if (bma->offset == 0)
4316 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4317 else
4318 bma->datatype |= XFS_ALLOC_USERDATA;
4319 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004320 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004321 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004322 }
4323
Dave Chinnere04426b2012-10-05 11:06:59 +10004324 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004325
4326 /*
4327 * Only want to do the alignment at the eof if it is userdata and
4328 * allocation length is larger than a stripe unit.
4329 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004330 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004331 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004332 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004333 if (error)
4334 return error;
4335 }
4336
4337 error = xfs_bmap_alloc(bma);
4338 if (error)
4339 return error;
4340
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004341 if (bma->dfops->dop_low)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004342 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004343 if (bma->cur)
4344 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004345 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004346 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004347 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4348 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4349 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004350 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004351 }
4352 /*
4353 * Bump the number of extents we've allocated
4354 * in this call.
4355 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004356 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004357
Dave Chinner29c8d172011-09-18 20:41:00 +00004358 if (bma->cur)
4359 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004360 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4361
Dave Chinner963c30c2011-09-18 20:40:59 +00004362 bma->got.br_startoff = bma->offset;
4363 bma->got.br_startblock = bma->blkno;
4364 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004365 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004366
4367 /*
4368 * A wasdelay extent has been initialized, so shouldn't be flagged
4369 * as unwritten.
4370 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004371 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004372 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004373 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004374
Christoph Hellwigc6534242011-09-18 20:41:05 +00004375 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004376 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004377 else
4378 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004379
Christoph Hellwigc315c902011-09-18 20:41:02 +00004380 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004381 if (error)
4382 return error;
4383
4384 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004385 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4386 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4387 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004388 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004389 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004390
Dave Chinner963c30c2011-09-18 20:40:59 +00004391 ASSERT(bma->got.br_startoff <= bma->offset);
4392 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4393 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004394 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4395 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004396 return 0;
4397}
4398
Dave Chinnerb447fe52011-09-18 20:40:51 +00004399STATIC int
4400xfs_bmapi_convert_unwritten(
4401 struct xfs_bmalloca *bma,
4402 struct xfs_bmbt_irec *mval,
4403 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004404 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004405{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004406 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004407 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004408 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004409 int error;
4410
Dave Chinnerb447fe52011-09-18 20:40:51 +00004411 /* check if we need to do unwritten->real conversion */
4412 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4413 (flags & XFS_BMAPI_PREALLOC))
4414 return 0;
4415
4416 /* check if we need to do real->unwritten conversion */
4417 if (mval->br_state == XFS_EXT_NORM &&
4418 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4419 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4420 return 0;
4421
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004422 ASSERT(whichfork != XFS_COW_FORK);
4423
Dave Chinnerb447fe52011-09-18 20:40:51 +00004424 /*
4425 * Modify (by adding) the state flag, if writing.
4426 */
4427 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004428 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4429 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004430 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004431 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004432 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004433 }
4434 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4435 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4436
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004437 /*
4438 * Before insertion into the bmbt, zero the range being converted
4439 * if required.
4440 */
4441 if (flags & XFS_BMAPI_ZERO) {
4442 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4443 mval->br_blockcount);
4444 if (error)
4445 return error;
4446 }
4447
Dave Chinnere0c3da52011-09-18 20:41:01 +00004448 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004449 &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004450 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004451 /*
4452 * Log the inode core unconditionally in the unwritten extent conversion
4453 * path because the conversion might not have done so (e.g., if the
4454 * extent count hasn't changed). We need to make sure the inode is dirty
4455 * in the transaction for the sake of fsync(), even if nothing has
4456 * changed, because fsync() will not force the log for this transaction
4457 * unless it sees the inode pinned.
4458 */
4459 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004460 if (error)
4461 return error;
4462
4463 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004464 * Update our extent pointer, given that
4465 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4466 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004467 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004468 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004469
4470 /*
4471 * We may have combined previously unwritten space with written space,
4472 * so generate another request.
4473 */
4474 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004475 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004476 return 0;
4477}
4478
Christoph Hellwig44032802011-09-18 20:40:48 +00004479/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004480 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4481 * extent state if necessary. Details behaviour is controlled by the flags
4482 * parameter. Only allocates blocks from a single allocation group, to avoid
4483 * locking problems.
4484 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 * The returned value in "firstblock" from the first call in a transaction
4486 * must be remembered and presented to subsequent calls in "firstblock".
4487 * An upper bound for the number of blocks to be allocated is supplied to
4488 * the first call in "total"; if no allocation group has that many free
4489 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4490 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004491int
4492xfs_bmapi_write(
4493 struct xfs_trans *tp, /* transaction pointer */
4494 struct xfs_inode *ip, /* incore inode */
4495 xfs_fileoff_t bno, /* starting file offs. mapped */
4496 xfs_filblks_t len, /* length to map in file */
4497 int flags, /* XFS_BMAPI_... */
4498 xfs_fsblock_t *firstblock, /* first allocated block
4499 controls a.g. for allocs */
4500 xfs_extlen_t total, /* total blocks needed */
4501 struct xfs_bmbt_irec *mval, /* output: map values */
4502 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004503 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004505 struct xfs_mount *mp = ip->i_mount;
4506 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004507 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004508 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004509 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004510 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004511 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004512 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004513 int whichfork; /* data or attr fork */
4514 char inhole; /* current location is hole in file */
4515 char wasdelay; /* old extent was delayed */
4516
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004518 xfs_fileoff_t orig_bno; /* original block number value */
4519 int orig_flags; /* original flags arg value */
4520 xfs_filblks_t orig_len; /* original value of len arg */
4521 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4522 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
4524 orig_bno = bno;
4525 orig_len = len;
4526 orig_flags = flags;
4527 orig_mval = mval;
4528 orig_nmap = *nmap;
4529#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004530 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004531
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004533 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4534 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4535 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004536 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004537 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004538 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004539 ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK);
4540 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP));
4541 ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP));
Darrick J. Wong60b49842016-10-03 09:11:34 -07004542 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || whichfork != XFS_COW_FORK);
4543 ASSERT(!(flags & XFS_BMAPI_CONVERT) || whichfork != XFS_COW_FORK);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004544
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004545 /* zeroing is for currently only for data extents, not metadata */
4546 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4547 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4548 /*
4549 * we can allocate unwritten extents or pre-zero allocated blocks,
4550 * but it makes no sense to do both at once. This would result in
4551 * zeroing the unwritten extent twice, but it still being an
4552 * unwritten extent....
4553 */
4554 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4555 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4556
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 if (unlikely(XFS_TEST_ERROR(
4558 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004559 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004561 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004562 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004564
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004566 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004569
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004570 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004571
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004572 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004574 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004576 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004577 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004578 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004579 }
4580
4581 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4582 error = xfs_iread_extents(tp, ip, whichfork);
4583 if (error)
4584 goto error0;
4585 }
4586
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 n = 0;
4588 end = bno + len;
4589 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004590
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004591 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4592 eof = true;
4593 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4594 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004595 bma.tp = tp;
4596 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004597 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004598 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004599 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004600 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004601
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004603 inhole = eof || bma.got.br_startoff > bno;
4604 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004605
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 /*
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004607 * Make sure we only reflink into a hole.
4608 */
4609 if (flags & XFS_BMAPI_REMAP)
4610 ASSERT(inhole);
Darrick J. Wong60b49842016-10-03 09:11:34 -07004611 if (flags & XFS_BMAPI_COWFORK)
4612 ASSERT(!inhole);
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004613
4614 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 * First, deal with the hole before the allocated space
4616 * that we found, if any.
4617 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004618 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004619 bma.eof = eof;
4620 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4621 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004622 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004623 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004624
Dave Chinnera99ebf42011-12-01 11:24:20 +00004625 /*
4626 * There's a 32/64 bit type mismatch between the
4627 * allocation length request (which can be 64 bits in
4628 * length) and the bma length request, which is
4629 * xfs_extlen_t and therefore 32 bits. Hence we have to
4630 * check for 32-bit overflows and handle them here.
4631 */
4632 if (len > (xfs_filblks_t)MAXEXTLEN)
4633 bma.length = MAXEXTLEN;
4634 else
4635 bma.length = len;
4636
4637 ASSERT(len > 0);
4638 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004639 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 if (error)
4641 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004642 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004643 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004644
4645 /*
4646 * If this is a CoW allocation, record the data in
4647 * the refcount btree for orphan recovery.
4648 */
4649 if (whichfork == XFS_COW_FORK) {
4650 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4651 bma.blkno, bma.length);
4652 if (error)
4653 goto error0;
4654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004656
Dave Chinneraef9a892011-09-18 20:40:44 +00004657 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004658 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4659 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660
Dave Chinnerb447fe52011-09-18 20:40:51 +00004661 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004662 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004663 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004664 continue;
4665 if (error)
4666 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Dave Chinneraef9a892011-09-18 20:40:44 +00004668 /* update the extent map to return */
4669 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 /*
4672 * If we're done, stop now. Stop when we've allocated
4673 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4674 * the transaction may get too big.
4675 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004676 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004678
4679 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004680 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004681 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4682 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 /*
4687 * Transform from btree to extents, give it cur.
4688 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004689 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004690 int tmp_logflags = 0;
4691
Dave Chinner29c8d172011-09-18 20:41:00 +00004692 ASSERT(bma.cur);
4693 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004695 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 if (error)
4697 goto error0;
4698 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004699
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004701 XFS_IFORK_NEXTENTS(ip, whichfork) >
4702 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704error0:
4705 /*
4706 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004707 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004709 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004711 bma.logflags &= ~xfs_ilog_fext(whichfork);
4712 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004714 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 /*
4716 * Log whatever the flags say, even if error. Otherwise we might miss
4717 * detecting a case where the data is changed, there's an error,
4718 * and it's not logged so we don't shutdown when we should.
4719 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004720 if (bma.logflags)
4721 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004722
Dave Chinner29c8d172011-09-18 20:41:00 +00004723 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 if (!error) {
4725 ASSERT(*firstblock == NULLFSBLOCK ||
4726 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4727 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004728 bma.cur->bc_private.b.firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004729 (dfops->dop_low &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 XFS_FSB_TO_AGNO(mp, *firstblock) <
4731 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004732 bma.cur->bc_private.b.firstblock)));
4733 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004735 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4737 }
4738 if (!error)
4739 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4740 orig_nmap, *nmap);
4741 return error;
4742}
4743
4744/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004745 * When a delalloc extent is split (e.g., due to a hole punch), the original
4746 * indlen reservation must be shared across the two new extents that are left
4747 * behind.
4748 *
4749 * Given the original reservation and the worst case indlen for the two new
4750 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004751 * reservation fairly across the two new extents. If necessary, steal available
4752 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4753 * ores == 1). The number of stolen blocks is returned. The availability and
4754 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004755 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004756static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004757xfs_bmap_split_indlen(
4758 xfs_filblks_t ores, /* original res. */
4759 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004760 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4761 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004762{
4763 xfs_filblks_t len1 = *indlen1;
4764 xfs_filblks_t len2 = *indlen2;
4765 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004766 xfs_filblks_t stolen = 0;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004767
4768 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004769 * Steal as many blocks as we can to try and satisfy the worst case
4770 * indlen for both new extents.
4771 */
4772 while (nres > ores && avail) {
4773 nres--;
4774 avail--;
4775 stolen++;
4776 }
4777
4778 /*
4779 * The only blocks available are those reserved for the original
4780 * extent and what we can steal from the extent being removed.
4781 * If this still isn't enough to satisfy the combined
4782 * requirements for the two new extents, skim blocks off of each
4783 * of the new reservations until they match what is available.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004784 */
4785 while (nres > ores) {
4786 if (len1) {
4787 len1--;
4788 nres--;
4789 }
4790 if (nres == ores)
4791 break;
4792 if (len2) {
4793 len2--;
4794 nres--;
4795 }
4796 }
4797
4798 *indlen1 = len1;
4799 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004800
4801 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004802}
4803
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004804int
4805xfs_bmap_del_extent_delay(
4806 struct xfs_inode *ip,
4807 int whichfork,
4808 xfs_extnum_t *idx,
4809 struct xfs_bmbt_irec *got,
4810 struct xfs_bmbt_irec *del)
4811{
4812 struct xfs_mount *mp = ip->i_mount;
4813 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4814 struct xfs_bmbt_irec new;
4815 int64_t da_old, da_new, da_diff = 0;
4816 xfs_fileoff_t del_endoff, got_endoff;
4817 xfs_filblks_t got_indlen, new_indlen, stolen;
4818 int error = 0, state = 0;
4819 bool isrt;
4820
4821 XFS_STATS_INC(mp, xs_del_exlist);
4822
4823 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4824 del_endoff = del->br_startoff + del->br_blockcount;
4825 got_endoff = got->br_startoff + got->br_blockcount;
4826 da_old = startblockval(got->br_startblock);
4827 da_new = 0;
4828
4829 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004830 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004831 ASSERT(del->br_blockcount > 0);
4832 ASSERT(got->br_startoff <= del->br_startoff);
4833 ASSERT(got_endoff >= del_endoff);
4834
4835 if (isrt) {
4836 int64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4837
4838 do_div(rtexts, mp->m_sb.sb_rextsize);
4839 xfs_mod_frextents(mp, rtexts);
4840 }
4841
4842 /*
4843 * Update the inode delalloc counter now and wait to update the
4844 * sb counters as we might have to borrow some blocks for the
4845 * indirect block accounting.
4846 */
4847 xfs_trans_reserve_quota_nblks(NULL, ip, -((long)del->br_blockcount), 0,
4848 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4849 ip->i_delayed_blks -= del->br_blockcount;
4850
4851 if (whichfork == XFS_COW_FORK)
4852 state |= BMAP_COWFORK;
4853
4854 if (got->br_startoff == del->br_startoff)
4855 state |= BMAP_LEFT_CONTIG;
4856 if (got_endoff == del_endoff)
4857 state |= BMAP_RIGHT_CONTIG;
4858
4859 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4860 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4861 /*
4862 * Matches the whole extent. Delete the entry.
4863 */
4864 xfs_iext_remove(ip, *idx, 1, state);
4865 --*idx;
4866 break;
4867 case BMAP_LEFT_CONTIG:
4868 /*
4869 * Deleting the first part of the extent.
4870 */
4871 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4872 got->br_startoff = del_endoff;
4873 got->br_blockcount -= del->br_blockcount;
4874 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4875 got->br_blockcount), da_old);
4876 got->br_startblock = nullstartblock((int)da_new);
4877 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4878 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4879 break;
4880 case BMAP_RIGHT_CONTIG:
4881 /*
4882 * Deleting the last part of the extent.
4883 */
4884 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4885 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4886 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4887 got->br_blockcount), da_old);
4888 got->br_startblock = nullstartblock((int)da_new);
4889 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4890 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4891 break;
4892 case 0:
4893 /*
4894 * Deleting the middle of the extent.
4895 *
4896 * Distribute the original indlen reservation across the two new
4897 * extents. Steal blocks from the deleted extent if necessary.
4898 * Stealing blocks simply fudges the fdblocks accounting below.
4899 * Warn if either of the new indlen reservations is zero as this
4900 * can lead to delalloc problems.
4901 */
4902 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4903
4904 got->br_blockcount = del->br_startoff - got->br_startoff;
4905 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4906
4907 new.br_blockcount = got_endoff - del_endoff;
4908 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4909
4910 WARN_ON_ONCE(!got_indlen || !new_indlen);
4911 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4912 del->br_blockcount);
4913
4914 got->br_startblock = nullstartblock((int)got_indlen);
4915 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4916 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4917
4918 new.br_startoff = del_endoff;
4919 new.br_state = got->br_state;
4920 new.br_startblock = nullstartblock((int)new_indlen);
4921
4922 ++*idx;
4923 xfs_iext_insert(ip, *idx, 1, &new, state);
4924
4925 da_new = got_indlen + new_indlen - stolen;
4926 del->br_blockcount -= stolen;
4927 break;
4928 }
4929
4930 ASSERT(da_old >= da_new);
4931 da_diff = da_old - da_new;
4932 if (!isrt)
4933 da_diff += del->br_blockcount;
4934 if (da_diff)
4935 xfs_mod_fdblocks(mp, da_diff, false);
4936 return error;
4937}
4938
4939void
4940xfs_bmap_del_extent_cow(
4941 struct xfs_inode *ip,
4942 xfs_extnum_t *idx,
4943 struct xfs_bmbt_irec *got,
4944 struct xfs_bmbt_irec *del)
4945{
4946 struct xfs_mount *mp = ip->i_mount;
4947 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4948 struct xfs_bmbt_irec new;
4949 xfs_fileoff_t del_endoff, got_endoff;
4950 int state = BMAP_COWFORK;
4951
4952 XFS_STATS_INC(mp, xs_del_exlist);
4953
4954 del_endoff = del->br_startoff + del->br_blockcount;
4955 got_endoff = got->br_startoff + got->br_blockcount;
4956
4957 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004958 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004959 ASSERT(del->br_blockcount > 0);
4960 ASSERT(got->br_startoff <= del->br_startoff);
4961 ASSERT(got_endoff >= del_endoff);
4962 ASSERT(!isnullstartblock(got->br_startblock));
4963
4964 if (got->br_startoff == del->br_startoff)
4965 state |= BMAP_LEFT_CONTIG;
4966 if (got_endoff == del_endoff)
4967 state |= BMAP_RIGHT_CONTIG;
4968
4969 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4970 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4971 /*
4972 * Matches the whole extent. Delete the entry.
4973 */
4974 xfs_iext_remove(ip, *idx, 1, state);
4975 --*idx;
4976 break;
4977 case BMAP_LEFT_CONTIG:
4978 /*
4979 * Deleting the first part of the extent.
4980 */
4981 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4982 got->br_startoff = del_endoff;
4983 got->br_blockcount -= del->br_blockcount;
4984 got->br_startblock = del->br_startblock + del->br_blockcount;
4985 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4986 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4987 break;
4988 case BMAP_RIGHT_CONTIG:
4989 /*
4990 * Deleting the last part of the extent.
4991 */
4992 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4993 got->br_blockcount -= del->br_blockcount;
4994 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4995 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4996 break;
4997 case 0:
4998 /*
4999 * Deleting the middle of the extent.
5000 */
5001 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5002 got->br_blockcount = del->br_startoff - got->br_startoff;
5003 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5004 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5005
5006 new.br_startoff = del_endoff;
5007 new.br_blockcount = got_endoff - del_endoff;
5008 new.br_state = got->br_state;
5009 new.br_startblock = del->br_startblock + del->br_blockcount;
5010
5011 ++*idx;
5012 xfs_iext_insert(ip, *idx, 1, &new, state);
5013 break;
5014 }
5015}
5016
Brian Fostera9bd24a2016-03-15 11:42:46 +11005017/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005018 * Called by xfs_bmapi to update file extent records and the btree
5019 * after removing space (or undoing a delayed allocation).
5020 */
5021STATIC int /* error */
5022xfs_bmap_del_extent(
5023 xfs_inode_t *ip, /* incore inode pointer */
5024 xfs_trans_t *tp, /* current transaction pointer */
5025 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005026 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005027 xfs_btree_cur_t *cur, /* if null, not a btree */
5028 xfs_bmbt_irec_t *del, /* data to remove from extents */
5029 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005030 int whichfork, /* data or attr fork */
5031 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005032{
5033 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5034 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5035 xfs_fsblock_t del_endblock=0; /* first block past del */
5036 xfs_fileoff_t del_endoff; /* first offset past del */
5037 int delay; /* current block is delayed allocated */
5038 int do_fx; /* free extent at end of routine */
5039 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5040 int error; /* error return value */
5041 int flags; /* inode logging flags */
5042 xfs_bmbt_irec_t got; /* current extent entry */
5043 xfs_fileoff_t got_endoff; /* first offset past got */
5044 int i; /* temp state */
5045 xfs_ifork_t *ifp; /* inode fork pointer */
5046 xfs_mount_t *mp; /* mount structure */
5047 xfs_filblks_t nblks; /* quota/sb block count */
5048 xfs_bmbt_irec_t new; /* new record to be inserted */
5049 /* REFERENCED */
5050 uint qfield; /* quota field to update */
5051 xfs_filblks_t temp; /* for indirect length calculations */
5052 xfs_filblks_t temp2; /* for indirect length calculations */
5053 int state = 0;
5054
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005055 mp = ip->i_mount;
5056 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005057
5058 if (whichfork == XFS_ATTR_FORK)
5059 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005060 else if (whichfork == XFS_COW_FORK)
5061 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005062
Dave Chinner9e5987a2013-02-25 12:31:26 +11005063 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005064 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005065 ASSERT(del->br_blockcount > 0);
5066 ep = xfs_iext_get_ext(ifp, *idx);
5067 xfs_bmbt_get_all(ep, &got);
5068 ASSERT(got.br_startoff <= del->br_startoff);
5069 del_endoff = del->br_startoff + del->br_blockcount;
5070 got_endoff = got.br_startoff + got.br_blockcount;
5071 ASSERT(got_endoff >= del_endoff);
5072 delay = isnullstartblock(got.br_startblock);
5073 ASSERT(isnullstartblock(del->br_startblock) == delay);
5074 flags = 0;
5075 qfield = 0;
5076 error = 0;
5077 /*
5078 * If deleting a real allocation, must free up the disk space.
5079 */
5080 if (!delay) {
5081 flags = XFS_ILOG_CORE;
5082 /*
5083 * Realtime allocation. Free it and record di_nblocks update.
5084 */
5085 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5086 xfs_fsblock_t bno;
5087 xfs_filblks_t len;
5088
5089 ASSERT(do_mod(del->br_blockcount,
5090 mp->m_sb.sb_rextsize) == 0);
5091 ASSERT(do_mod(del->br_startblock,
5092 mp->m_sb.sb_rextsize) == 0);
5093 bno = del->br_startblock;
5094 len = del->br_blockcount;
5095 do_div(bno, mp->m_sb.sb_rextsize);
5096 do_div(len, mp->m_sb.sb_rextsize);
5097 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5098 if (error)
5099 goto done;
5100 do_fx = 0;
5101 nblks = len * mp->m_sb.sb_rextsize;
5102 qfield = XFS_TRANS_DQ_RTBCOUNT;
5103 }
5104 /*
5105 * Ordinary allocation.
5106 */
5107 else {
5108 do_fx = 1;
5109 nblks = del->br_blockcount;
5110 qfield = XFS_TRANS_DQ_BCOUNT;
5111 }
5112 /*
5113 * Set up del_endblock and cur for later.
5114 */
5115 del_endblock = del->br_startblock + del->br_blockcount;
5116 if (cur) {
5117 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5118 got.br_startblock, got.br_blockcount,
5119 &i)))
5120 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005121 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005122 }
5123 da_old = da_new = 0;
5124 } else {
5125 da_old = startblockval(got.br_startblock);
5126 da_new = 0;
5127 nblks = 0;
5128 do_fx = 0;
5129 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005130
Dave Chinner9e5987a2013-02-25 12:31:26 +11005131 /*
5132 * Set flag value to use in switch statement.
5133 * Left-contig is 2, right-contig is 1.
5134 */
5135 switch (((got.br_startoff == del->br_startoff) << 1) |
5136 (got_endoff == del_endoff)) {
5137 case 3:
5138 /*
5139 * Matches the whole extent. Delete the entry.
5140 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005141 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005142 xfs_iext_remove(ip, *idx, 1,
5143 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5144 --*idx;
5145 if (delay)
5146 break;
5147
5148 XFS_IFORK_NEXT_SET(ip, whichfork,
5149 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5150 flags |= XFS_ILOG_CORE;
5151 if (!cur) {
5152 flags |= xfs_ilog_fext(whichfork);
5153 break;
5154 }
5155 if ((error = xfs_btree_delete(cur, &i)))
5156 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005157 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005158 break;
5159
5160 case 2:
5161 /*
5162 * Deleting the first part of the extent.
5163 */
5164 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5165 xfs_bmbt_set_startoff(ep, del_endoff);
5166 temp = got.br_blockcount - del->br_blockcount;
5167 xfs_bmbt_set_blockcount(ep, temp);
5168 if (delay) {
5169 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5170 da_old);
5171 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5172 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5173 da_new = temp;
5174 break;
5175 }
5176 xfs_bmbt_set_startblock(ep, del_endblock);
5177 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5178 if (!cur) {
5179 flags |= xfs_ilog_fext(whichfork);
5180 break;
5181 }
5182 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5183 got.br_blockcount - del->br_blockcount,
5184 got.br_state)))
5185 goto done;
5186 break;
5187
5188 case 1:
5189 /*
5190 * Deleting the last part of the extent.
5191 */
5192 temp = got.br_blockcount - del->br_blockcount;
5193 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5194 xfs_bmbt_set_blockcount(ep, temp);
5195 if (delay) {
5196 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5197 da_old);
5198 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5199 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5200 da_new = temp;
5201 break;
5202 }
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, got.br_startoff,
5209 got.br_startblock,
5210 got.br_blockcount - del->br_blockcount,
5211 got.br_state)))
5212 goto done;
5213 break;
5214
5215 case 0:
5216 /*
5217 * Deleting the middle of the extent.
5218 */
5219 temp = del->br_startoff - got.br_startoff;
5220 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5221 xfs_bmbt_set_blockcount(ep, temp);
5222 new.br_startoff = del_endoff;
5223 temp2 = got_endoff - del_endoff;
5224 new.br_blockcount = temp2;
5225 new.br_state = got.br_state;
5226 if (!delay) {
5227 new.br_startblock = del_endblock;
5228 flags |= XFS_ILOG_CORE;
5229 if (cur) {
5230 if ((error = xfs_bmbt_update(cur,
5231 got.br_startoff,
5232 got.br_startblock, temp,
5233 got.br_state)))
5234 goto done;
5235 if ((error = xfs_btree_increment(cur, 0, &i)))
5236 goto done;
5237 cur->bc_rec.b = new;
5238 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10005239 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005240 goto done;
5241 /*
5242 * If get no-space back from btree insert,
5243 * it tried a split, and we have a zero
5244 * block reservation.
5245 * Fix up our state and return the error.
5246 */
Dave Chinner24513372014-06-25 14:58:08 +10005247 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11005248 /*
5249 * Reset the cursor, don't trust
5250 * it after any insert operation.
5251 */
5252 if ((error = xfs_bmbt_lookup_eq(cur,
5253 got.br_startoff,
5254 got.br_startblock,
5255 temp, &i)))
5256 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005257 XFS_WANT_CORRUPTED_GOTO(mp,
5258 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005259 /*
5260 * Update the btree record back
5261 * to the original value.
5262 */
5263 if ((error = xfs_bmbt_update(cur,
5264 got.br_startoff,
5265 got.br_startblock,
5266 got.br_blockcount,
5267 got.br_state)))
5268 goto done;
5269 /*
5270 * Reset the extent record back
5271 * to the original value.
5272 */
5273 xfs_bmbt_set_blockcount(ep,
5274 got.br_blockcount);
5275 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005276 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005277 goto done;
5278 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005279 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005280 } else
5281 flags |= xfs_ilog_fext(whichfork);
5282 XFS_IFORK_NEXT_SET(ip, whichfork,
5283 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5284 } else {
Brian Fosterd34999c92016-03-15 11:42:47 +11005285 xfs_filblks_t stolen;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005286 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005287
5288 /*
5289 * Distribute the original indlen reservation across the
Brian Fosterd34999c92016-03-15 11:42:47 +11005290 * two new extents. Steal blocks from the deleted extent
5291 * if necessary. Stealing blocks simply fudges the
5292 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005293 */
5294 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5295 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c92016-03-15 11:42:47 +11005296 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5297 del->br_blockcount);
5298 da_new = temp + temp2 - stolen;
5299 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005300
5301 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11005302 * Set the reservation for each extent. Warn if either
5303 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005304 */
Brian Fosterd34999c92016-03-15 11:42:47 +11005305 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005306 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005307 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005308 }
5309 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5310 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5311 ++*idx;
5312 break;
5313 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005314
5315 /* remove reverse mapping */
5316 if (!delay) {
5317 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5318 if (error)
5319 goto done;
5320 }
5321
Dave Chinner9e5987a2013-02-25 12:31:26 +11005322 /*
5323 * If we need to, add to list of extents to delete.
5324 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005325 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005326 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5327 error = xfs_refcount_decrease_extent(mp, dfops, del);
5328 if (error)
5329 goto done;
5330 } else
5331 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5332 del->br_blockcount, NULL);
5333 }
5334
Dave Chinner9e5987a2013-02-25 12:31:26 +11005335 /*
5336 * Adjust inode # blocks in the file.
5337 */
5338 if (nblks)
5339 ip->i_d.di_nblocks -= nblks;
5340 /*
5341 * Adjust quota data.
5342 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005343 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005344 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5345
5346 /*
5347 * Account for change in delayed indirect blocks.
5348 * Nothing to do for disk quota accounting here.
5349 */
5350 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005351 if (da_old > da_new)
5352 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005353done:
5354 *logflagsp = flags;
5355 return error;
5356}
5357
5358/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 * Unmap (remove) blocks from a file.
5360 * If nexts is nonzero then the number of extents to remove is limited to
5361 * that value. If not all extents in the block range can be removed then
5362 * *done is set.
5363 */
5364int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005365__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 xfs_trans_t *tp, /* transaction pointer */
5367 struct xfs_inode *ip, /* incore inode */
5368 xfs_fileoff_t bno, /* starting offset to unmap */
Darrick J. Wong44535932016-10-03 09:11:29 -07005369 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 int flags, /* misc flags */
5371 xfs_extnum_t nexts, /* number of extents max */
5372 xfs_fsblock_t *firstblock, /* first allocated block
5373 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005374 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375{
5376 xfs_btree_cur_t *cur; /* bmap btree cursor */
5377 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378 int error; /* error return value */
5379 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005380 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 xfs_ifork_t *ifp; /* inode fork pointer */
5382 int isrt; /* freeing in rt area */
5383 xfs_extnum_t lastx; /* last extent index used */
5384 int logflags; /* transaction logging flags */
5385 xfs_extlen_t mod; /* rt extent offset */
5386 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 xfs_fileoff_t start; /* first file offset deleted */
5388 int tmp_logflags; /* partial logging flags */
5389 int wasdel; /* was a delayed alloc extent */
5390 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005392 xfs_filblks_t len = *rlen; /* length to unmap in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005394 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5395
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005396 whichfork = xfs_bmapi_whichfork(flags);
5397 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 ifp = XFS_IFORK_PTR(ip, whichfork);
5399 if (unlikely(
5400 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5401 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5402 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5403 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005404 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 }
5406 mp = ip->i_mount;
5407 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005408 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005409
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005410 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 ASSERT(len > 0);
5412 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005413
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5415 (error = xfs_iread_extents(tp, ip, whichfork)))
5416 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005417 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005418 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 return 0;
5420 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005421 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005422 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 start = bno;
5424 bno = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005425
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 /*
5427 * Check to see if the given block number is past the end of the
5428 * file, back up to the last block if so...
5429 */
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005430 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5431 ASSERT(lastx > 0);
5432 xfs_iext_get_extent(ifp, --lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 bno = got.br_startoff + got.br_blockcount - 1;
5434 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005435
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 logflags = 0;
5437 if (ifp->if_flags & XFS_IFBROOT) {
5438 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005439 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005441 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 cur->bc_private.b.flags = 0;
5443 } else
5444 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005445
5446 if (isrt) {
5447 /*
5448 * Synchronize by locking the bitmap inode.
5449 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005450 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005451 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005452 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5453 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005454 }
5455
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 extno = 0;
5457 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5458 (nexts == 0 || extno < nexts)) {
5459 /*
5460 * Is the found extent after a hole in which bno lives?
5461 * Just back up to the previous extent, if so.
5462 */
5463 if (got.br_startoff > bno) {
5464 if (--lastx < 0)
5465 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005466 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 }
5468 /*
5469 * Is the last block of this extent before the range
5470 * we're supposed to delete? If so, we're done.
5471 */
5472 bno = XFS_FILEOFF_MIN(bno,
5473 got.br_startoff + got.br_blockcount - 1);
5474 if (bno < start)
5475 break;
5476 /*
5477 * Then deal with the (possibly delayed) allocated space
5478 * we found.
5479 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005481 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 if (got.br_startoff < start) {
5483 del.br_startoff = start;
5484 del.br_blockcount -= start - got.br_startoff;
5485 if (!wasdel)
5486 del.br_startblock += start - got.br_startoff;
5487 }
5488 if (del.br_startoff + del.br_blockcount > bno + 1)
5489 del.br_blockcount = bno + 1 - del.br_startoff;
5490 sum = del.br_startblock + del.br_blockcount;
5491 if (isrt &&
5492 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5493 /*
5494 * Realtime extent not lined up at the end.
5495 * The extent could have been split into written
5496 * and unwritten pieces, or we could just be
5497 * unmapping part of it. But we can't really
5498 * get rid of part of a realtime extent.
5499 */
5500 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005501 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 /*
5503 * This piece is unwritten, or we're not
5504 * using unwritten extents. Skip over it.
5505 */
5506 ASSERT(bno >= mod);
5507 bno -= mod > del.br_blockcount ?
5508 del.br_blockcount : mod;
5509 if (bno < got.br_startoff) {
5510 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005511 xfs_bmbt_get_all(xfs_iext_get_ext(
5512 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 }
5514 continue;
5515 }
5516 /*
5517 * It's written, turn it unwritten.
5518 * This is better than zeroing it.
5519 */
5520 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005521 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 /*
5523 * If this spans a realtime extent boundary,
5524 * chop it back to the start of the one we end at.
5525 */
5526 if (del.br_blockcount > mod) {
5527 del.br_startoff += del.br_blockcount - mod;
5528 del.br_startblock += del.br_blockcount - mod;
5529 del.br_blockcount = mod;
5530 }
5531 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005532 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005533 &lastx, &cur, &del, firstblock, dfops,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005534 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 if (error)
5536 goto error0;
5537 goto nodelete;
5538 }
5539 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5540 /*
5541 * Realtime extent is lined up at the end but not
5542 * at the front. We'll get rid of full extents if
5543 * we can.
5544 */
5545 mod = mp->m_sb.sb_rextsize - mod;
5546 if (del.br_blockcount > mod) {
5547 del.br_blockcount -= mod;
5548 del.br_startoff += mod;
5549 del.br_startblock += mod;
5550 } else if ((del.br_startoff == start &&
5551 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005552 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005553 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 /*
5555 * Can't make it unwritten. There isn't
5556 * a full extent here so just skip it.
5557 */
5558 ASSERT(bno >= del.br_blockcount);
5559 bno -= del.br_blockcount;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005560 if (got.br_startoff > bno && --lastx >= 0)
5561 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 continue;
5563 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005564 struct xfs_bmbt_irec prev;
5565
Linus Torvalds1da177e2005-04-16 15:20:36 -07005566 /*
5567 * This one is already unwritten.
5568 * It must have a written left neighbor.
5569 * Unwrite the killed part of that one and
5570 * try again.
5571 */
5572 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005573 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005575 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005576 ASSERT(del.br_startblock ==
5577 prev.br_startblock + prev.br_blockcount);
5578 if (prev.br_startoff < start) {
5579 mod = start - prev.br_startoff;
5580 prev.br_blockcount -= mod;
5581 prev.br_startblock += mod;
5582 prev.br_startoff = start;
5583 }
5584 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005585 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005586 error = xfs_bmap_add_extent_unwritten_real(tp,
5587 ip, &lastx, &cur, &prev,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005588 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 if (error)
5590 goto error0;
5591 goto nodelete;
5592 } else {
5593 ASSERT(del.br_state == XFS_EXT_NORM);
5594 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005595 error = xfs_bmap_add_extent_unwritten_real(tp,
5596 ip, &lastx, &cur, &del,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005597 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598 if (error)
5599 goto error0;
5600 goto nodelete;
5601 }
5602 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005603
Linus Torvalds1da177e2005-04-16 15:20:36 -07005604 /*
5605 * If it's the case where the directory code is running
5606 * with no block reservation, and the deleted block is in
5607 * the middle of its extent, and the resulting insert
5608 * of an extent would cause transformation to btree format,
5609 * then reject it. The calling code will then swap
5610 * blocks around instead.
5611 * We have to do this now, rather than waiting for the
5612 * conversion to btree format, since the transaction
5613 * will be dirty.
5614 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005615 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005617 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5618 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 del.br_startoff > got.br_startoff &&
5620 del.br_startoff + del.br_blockcount <
5621 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005622 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 goto error0;
5624 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005625
5626 /*
5627 * Unreserve quota and update realtime free space, if
5628 * appropriate. If delayed allocation, update the inode delalloc
5629 * counter now and wait to update the sb counters as
5630 * xfs_bmap_del_extent() might need to borrow some blocks.
5631 */
5632 if (wasdel) {
5633 ASSERT(startblockval(del.br_startblock) > 0);
5634 if (isrt) {
5635 xfs_filblks_t rtexts;
5636
5637 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5638 do_div(rtexts, mp->m_sb.sb_rextsize);
5639 xfs_mod_frextents(mp, (int64_t)rtexts);
5640 (void)xfs_trans_reserve_quota_nblks(NULL,
5641 ip, -((long)del.br_blockcount), 0,
5642 XFS_QMOPT_RES_RTBLKS);
5643 } else {
5644 (void)xfs_trans_reserve_quota_nblks(NULL,
5645 ip, -((long)del.br_blockcount), 0,
5646 XFS_QMOPT_RES_REGBLKS);
5647 }
5648 ip->i_delayed_blks -= del.br_blockcount;
5649 if (cur)
5650 cur->bc_private.b.flags |=
5651 XFS_BTCUR_BPRV_WASDEL;
5652 } else if (cur)
5653 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5654
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005655 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005656 &tmp_logflags, whichfork, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 logflags |= tmp_logflags;
5658 if (error)
5659 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005660
5661 if (!isrt && wasdel)
5662 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5663
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 bno = del.br_startoff - 1;
5665nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 /*
5667 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005670 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005671 xfs_iext_get_extent(ifp, lastx, &got);
5672 if (got.br_startoff > bno && --lastx >= 0)
5673 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 extno++;
5676 }
5677 }
Darrick J. Wong44535932016-10-03 09:11:29 -07005678 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5679 *rlen = 0;
5680 else
5681 *rlen = bno - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005682
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 /*
5684 * Convert to a btree if necessary.
5685 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005686 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005688 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 &cur, 0, &tmp_logflags, whichfork);
5690 logflags |= tmp_logflags;
5691 if (error)
5692 goto error0;
5693 }
5694 /*
5695 * transform from btree to extents, give it cur
5696 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005697 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 ASSERT(cur != NULL);
5699 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5700 whichfork);
5701 logflags |= tmp_logflags;
5702 if (error)
5703 goto error0;
5704 }
5705 /*
5706 * transform from extents to local?
5707 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 error = 0;
5709error0:
5710 /*
5711 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005712 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005714 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005716 logflags &= ~xfs_ilog_fext(whichfork);
5717 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005719 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 /*
5721 * Log inode even in the error case, if the transaction
5722 * is dirty we'll need to shut down the filesystem.
5723 */
5724 if (logflags)
5725 xfs_trans_log_inode(tp, ip, logflags);
5726 if (cur) {
5727 if (!error) {
5728 *firstblock = cur->bc_private.b.firstblock;
5729 cur->bc_private.b.allocated = 0;
5730 }
5731 xfs_btree_del_cursor(cur,
5732 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5733 }
5734 return error;
5735}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005736
Darrick J. Wong44535932016-10-03 09:11:29 -07005737/* Unmap a range of a file. */
5738int
5739xfs_bunmapi(
5740 xfs_trans_t *tp,
5741 struct xfs_inode *ip,
5742 xfs_fileoff_t bno,
5743 xfs_filblks_t len,
5744 int flags,
5745 xfs_extnum_t nexts,
5746 xfs_fsblock_t *firstblock,
5747 struct xfs_defer_ops *dfops,
5748 int *done)
5749{
5750 int error;
5751
5752 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5753 dfops);
5754 *done = (len == 0);
5755 return error;
5756}
5757
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005758/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005759 * Determine whether an extent shift can be accomplished by a merge with the
5760 * extent that precedes the target hole of the shift.
5761 */
5762STATIC bool
5763xfs_bmse_can_merge(
5764 struct xfs_bmbt_irec *left, /* preceding extent */
5765 struct xfs_bmbt_irec *got, /* current extent to shift */
5766 xfs_fileoff_t shift) /* shift fsb */
5767{
5768 xfs_fileoff_t startoff;
5769
5770 startoff = got->br_startoff - shift;
5771
5772 /*
5773 * The extent, once shifted, must be adjacent in-file and on-disk with
5774 * the preceding extent.
5775 */
5776 if ((left->br_startoff + left->br_blockcount != startoff) ||
5777 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5778 (left->br_state != got->br_state) ||
5779 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5780 return false;
5781
5782 return true;
5783}
5784
5785/*
5786 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5787 * hole in the file. If an extent shift would result in the extent being fully
5788 * adjacent to the extent that currently precedes the hole, we can merge with
5789 * the preceding extent rather than do the shift.
5790 *
5791 * This function assumes the caller has verified a shift-by-merge is possible
5792 * with the provided extents via xfs_bmse_can_merge().
5793 */
5794STATIC int
5795xfs_bmse_merge(
5796 struct xfs_inode *ip,
5797 int whichfork,
5798 xfs_fileoff_t shift, /* shift fsb */
5799 int current_ext, /* idx of gotp */
5800 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5801 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5802 struct xfs_btree_cur *cur,
5803 int *logflags) /* output */
5804{
Brian Fosterddb19e32014-09-23 15:38:09 +10005805 struct xfs_bmbt_irec got;
5806 struct xfs_bmbt_irec left;
5807 xfs_filblks_t blockcount;
5808 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005809 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005810
Brian Fosterddb19e32014-09-23 15:38:09 +10005811 xfs_bmbt_get_all(gotp, &got);
5812 xfs_bmbt_get_all(leftp, &left);
5813 blockcount = left.br_blockcount + got.br_blockcount;
5814
5815 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5816 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5817 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5818
5819 /*
5820 * Merge the in-core extents. Note that the host record pointers and
5821 * current_ext index are invalid once the extent has been removed via
5822 * xfs_iext_remove().
5823 */
5824 xfs_bmbt_set_blockcount(leftp, blockcount);
5825 xfs_iext_remove(ip, current_ext, 1, 0);
5826
5827 /*
5828 * Update the on-disk extent count, the btree if necessary and log the
5829 * inode.
5830 */
5831 XFS_IFORK_NEXT_SET(ip, whichfork,
5832 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5833 *logflags |= XFS_ILOG_CORE;
5834 if (!cur) {
5835 *logflags |= XFS_ILOG_DEXT;
5836 return 0;
5837 }
5838
5839 /* lookup and remove the extent to merge */
5840 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5841 got.br_blockcount, &i);
5842 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005843 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005844 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005845
5846 error = xfs_btree_delete(cur, &i);
5847 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005848 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005849 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005850
5851 /* lookup and update size of the previous extent */
5852 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5853 left.br_blockcount, &i);
5854 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005855 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005856 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005857
5858 left.br_blockcount = blockcount;
5859
Dave Chinner4db431f2014-12-04 09:42:40 +11005860 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5861 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005862}
5863
5864/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005865 * Shift a single extent.
5866 */
5867STATIC int
5868xfs_bmse_shift_one(
5869 struct xfs_inode *ip,
5870 int whichfork,
5871 xfs_fileoff_t offset_shift_fsb,
5872 int *current_ext,
5873 struct xfs_bmbt_rec_host *gotp,
5874 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005875 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005876 enum shift_direction direction,
5877 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005878{
5879 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005880 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005881 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005882 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005883 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005884 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005885 int error;
5886 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005887 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005888
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005889 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005890 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005891 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005892
5893 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005894
Brian Fosterf71721d2014-09-23 15:39:05 +10005895 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005896 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005897
Namjae Jeona904b1c2015-03-25 15:08:56 +11005898 if (direction == SHIFT_LEFT) {
5899 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005900
Namjae Jeona904b1c2015-03-25 15:08:56 +11005901 /*
5902 * Check for merge if we've got an extent to the left,
5903 * otherwise make sure there's enough room at the start
5904 * of the file for the shift.
5905 */
5906 if (!*current_ext) {
5907 if (got.br_startoff < offset_shift_fsb)
5908 return -EINVAL;
5909 goto update_current_ext;
5910 }
5911 /*
5912 * grab the left extent and check for a large
5913 * enough hole.
5914 */
5915 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5916 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5917
5918 if (startoff <
5919 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005920 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005921
Dave Chinnerb11bd672014-12-04 09:42:24 +11005922 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005923 if (xfs_bmse_can_merge(&adj_irec, &got,
5924 offset_shift_fsb)) {
Darrick J. Wong9c194642016-08-03 12:16:05 +10005925 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5926 *current_ext, gotp, adj_irecp,
5927 cur, logflags);
5928 if (error)
5929 return error;
5930 adj_irec = got;
5931 goto update_rmap;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005932 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005933 } else {
5934 startoff = got.br_startoff + offset_shift_fsb;
5935 /* nothing to move if this is the last extent */
5936 if (*current_ext >= (total_extents - 1))
5937 goto update_current_ext;
5938 /*
5939 * If this is not the last extent in the file, make sure there
5940 * is enough room between current extent and next extent for
5941 * accommodating the shift.
5942 */
5943 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5944 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5945 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5946 return -EINVAL;
5947 /*
5948 * Unlike a left shift (which involves a hole punch),
5949 * a right shift does not modify extent neighbors
5950 * in any way. We should never find mergeable extents
5951 * in this scenario. Check anyways and warn if we
5952 * encounter two extents that could be one.
5953 */
5954 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5955 WARN_ON_ONCE(1);
5956 }
Brian Fostera979bdf2014-09-23 15:39:04 +10005957 /*
5958 * Increment the extent index for the next iteration, update the start
5959 * offset of the in-core extent and update the btree if applicable.
5960 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005961update_current_ext:
5962 if (direction == SHIFT_LEFT)
5963 (*current_ext)++;
5964 else
5965 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005966 xfs_bmbt_set_startoff(gotp, startoff);
5967 *logflags |= XFS_ILOG_CORE;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005968 adj_irec = got;
Brian Fostera979bdf2014-09-23 15:39:04 +10005969 if (!cur) {
5970 *logflags |= XFS_ILOG_DEXT;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005971 goto update_rmap;
Brian Fostera979bdf2014-09-23 15:39:04 +10005972 }
5973
5974 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5975 got.br_blockcount, &i);
5976 if (error)
5977 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005978 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10005979
5980 got.br_startoff = startoff;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005981 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5982 got.br_blockcount, got.br_state);
5983 if (error)
5984 return error;
5985
5986update_rmap:
5987 /* update reverse mapping */
5988 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
5989 if (error)
5990 return error;
5991 adj_irec.br_startoff = startoff;
5992 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
Brian Fostera979bdf2014-09-23 15:39:04 +10005993}
5994
5995/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005996 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005997 *
Brian Foster2c845f52014-09-23 15:37:09 +10005998 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005999 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10006000 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6001 * is the length by which each extent is shifted. If there is no hole to shift
6002 * the extents into, this will be considered invalid operation and we abort
6003 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006004 */
6005int
6006xfs_bmap_shift_extents(
6007 struct xfs_trans *tp,
6008 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006009 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006010 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10006011 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006012 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006013 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006014 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006015 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006016 int num_exts)
6017{
Brian Fosterca446d82014-09-02 12:12:53 +10006018 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006019 struct xfs_bmbt_rec_host *gotp;
6020 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006021 struct xfs_mount *mp = ip->i_mount;
6022 struct xfs_ifork *ifp;
6023 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10006024 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006025 xfs_extnum_t total_extents;
6026 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006027 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006028 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10006029 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006030
6031 if (unlikely(XFS_TEST_ERROR(
6032 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6033 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6034 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6035 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6036 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10006037 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006038 }
6039
6040 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10006041 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006042
Brian Foster2c845f52014-09-23 15:37:09 +10006043 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6044 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006045 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6046 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006047
6048 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006049 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6050 /* Read in all the extents */
6051 error = xfs_iread_extents(tp, ip, whichfork);
6052 if (error)
6053 return error;
6054 }
6055
Brian Fosterddb19e32014-09-23 15:38:09 +10006056 if (ifp->if_flags & XFS_IFBROOT) {
6057 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6058 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006059 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006060 cur->bc_private.b.flags = 0;
6061 }
6062
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006063 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006064 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006065 * are collapsing out, so we cannot use the count of real extents here.
6066 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006067 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006068 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006069 if (total_extents == 0) {
6070 *done = 1;
6071 goto del_cursor;
6072 }
6073
6074 /*
6075 * In case of first right shift, we need to initialize next_fsb
6076 */
6077 if (*next_fsb == NULLFSBLOCK) {
6078 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6079 xfs_bmbt_get_all(gotp, &got);
6080 *next_fsb = got.br_startoff;
6081 if (stop_fsb > *next_fsb) {
6082 *done = 1;
6083 goto del_cursor;
6084 }
6085 }
6086
6087 /* Lookup the extent index at which we have to stop */
6088 if (direction == SHIFT_RIGHT) {
6089 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6090 /* Make stop_extent exclusive of shift range */
6091 stop_extent--;
6092 } else
6093 stop_extent = total_extents;
6094
6095 /*
6096 * Look up the extent index for the fsb where we start shifting. We can
6097 * henceforth iterate with current_ext as extent list changes are locked
6098 * out via ilock.
6099 *
6100 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6101 * *next_fsb lies in a hole beyond which there are no extents. Either
6102 * way, we are done.
6103 */
6104 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
6105 if (!gotp) {
6106 *done = 1;
6107 goto del_cursor;
6108 }
6109
6110 /* some sanity checking before we finally start shifting extents */
6111 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6112 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6113 error = -EIO;
6114 goto del_cursor;
6115 }
6116
6117 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006118 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006119 &current_ext, gotp, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006120 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006121 if (error)
6122 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006123 /*
6124 * If there was an extent merge during the shift, the extent
6125 * count can change. Update the total and grade the next record.
6126 */
6127 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006128 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006129 stop_extent = total_extents;
6130 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006131
Namjae Jeona904b1c2015-03-25 15:08:56 +11006132 if (current_ext == stop_extent) {
6133 *done = 1;
6134 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006135 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006136 }
Brian Fosterddb19e32014-09-23 15:38:09 +10006137 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006138 }
6139
Namjae Jeona904b1c2015-03-25 15:08:56 +11006140 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006141 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10006142 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10006143 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006144
6145del_cursor:
6146 if (cur)
6147 xfs_btree_del_cursor(cur,
6148 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6149
Brian Fosterca446d82014-09-02 12:12:53 +10006150 if (logflags)
6151 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006152
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006153 return error;
6154}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006155
6156/*
6157 * Splits an extent into two extents at split_fsb block such that it is
6158 * the first block of the current_ext. @current_ext is a target extent
6159 * to be split. @split_fsb is a block where the extents is split.
6160 * If split_fsb lies in a hole or the first block of extents, just return 0.
6161 */
6162STATIC int
6163xfs_bmap_split_extent_at(
6164 struct xfs_trans *tp,
6165 struct xfs_inode *ip,
6166 xfs_fileoff_t split_fsb,
6167 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006168 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006169{
6170 int whichfork = XFS_DATA_FORK;
6171 struct xfs_btree_cur *cur = NULL;
6172 struct xfs_bmbt_rec_host *gotp;
6173 struct xfs_bmbt_irec got;
6174 struct xfs_bmbt_irec new; /* split extent */
6175 struct xfs_mount *mp = ip->i_mount;
6176 struct xfs_ifork *ifp;
6177 xfs_fsblock_t gotblkcnt; /* new block count for got */
6178 xfs_extnum_t current_ext;
6179 int error = 0;
6180 int logflags = 0;
6181 int i = 0;
6182
6183 if (unlikely(XFS_TEST_ERROR(
6184 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6185 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6186 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6187 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6188 XFS_ERRLEVEL_LOW, mp);
6189 return -EFSCORRUPTED;
6190 }
6191
6192 if (XFS_FORCED_SHUTDOWN(mp))
6193 return -EIO;
6194
6195 ifp = XFS_IFORK_PTR(ip, whichfork);
6196 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6197 /* Read in all the extents */
6198 error = xfs_iread_extents(tp, ip, whichfork);
6199 if (error)
6200 return error;
6201 }
6202
6203 /*
6204 * gotp can be null in 2 cases: 1) if there are no extents
6205 * or 2) split_fsb lies in a hole beyond which there are
6206 * no extents. Either way, we are done.
6207 */
6208 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
6209 if (!gotp)
6210 return 0;
6211
6212 xfs_bmbt_get_all(gotp, &got);
6213
6214 /*
6215 * Check split_fsb lies in a hole or the start boundary offset
6216 * of the extent.
6217 */
6218 if (got.br_startoff >= split_fsb)
6219 return 0;
6220
6221 gotblkcnt = split_fsb - got.br_startoff;
6222 new.br_startoff = split_fsb;
6223 new.br_startblock = got.br_startblock + gotblkcnt;
6224 new.br_blockcount = got.br_blockcount - gotblkcnt;
6225 new.br_state = got.br_state;
6226
6227 if (ifp->if_flags & XFS_IFBROOT) {
6228 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6229 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006230 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006231 cur->bc_private.b.flags = 0;
6232 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6233 got.br_startblock,
6234 got.br_blockcount,
6235 &i);
6236 if (error)
6237 goto del_cursor;
6238 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6239 }
6240
6241 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6242 got.br_blockcount = gotblkcnt;
6243
6244 logflags = XFS_ILOG_CORE;
6245 if (cur) {
6246 error = xfs_bmbt_update(cur, got.br_startoff,
6247 got.br_startblock,
6248 got.br_blockcount,
6249 got.br_state);
6250 if (error)
6251 goto del_cursor;
6252 } else
6253 logflags |= XFS_ILOG_DEXT;
6254
6255 /* Add new extent */
6256 current_ext++;
6257 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6258 XFS_IFORK_NEXT_SET(ip, whichfork,
6259 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6260
6261 if (cur) {
6262 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6263 new.br_startblock, new.br_blockcount,
6264 &i);
6265 if (error)
6266 goto del_cursor;
6267 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6268 cur->bc_rec.b.br_state = new.br_state;
6269
6270 error = xfs_btree_insert(cur, &i);
6271 if (error)
6272 goto del_cursor;
6273 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6274 }
6275
6276 /*
6277 * Convert to a btree if necessary.
6278 */
6279 if (xfs_bmap_needs_btree(ip, whichfork)) {
6280 int tmp_logflags; /* partial log flag return val */
6281
6282 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006283 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006284 &cur, 0, &tmp_logflags, whichfork);
6285 logflags |= tmp_logflags;
6286 }
6287
6288del_cursor:
6289 if (cur) {
6290 cur->bc_private.b.allocated = 0;
6291 xfs_btree_del_cursor(cur,
6292 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6293 }
6294
6295 if (logflags)
6296 xfs_trans_log_inode(tp, ip, logflags);
6297 return error;
6298}
6299
6300int
6301xfs_bmap_split_extent(
6302 struct xfs_inode *ip,
6303 xfs_fileoff_t split_fsb)
6304{
6305 struct xfs_mount *mp = ip->i_mount;
6306 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006307 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006308 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006309 int error;
6310
Christoph Hellwig253f4912016-04-06 09:19:55 +10006311 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6312 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6313 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006314 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006315
6316 xfs_ilock(ip, XFS_ILOCK_EXCL);
6317 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6318
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006319 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006320
6321 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006322 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006323 if (error)
6324 goto out;
6325
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006326 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006327 if (error)
6328 goto out;
6329
Christoph Hellwig70393312015-06-04 13:48:08 +10006330 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006331
6332out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006333 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006334 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006335 return error;
6336}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006337
6338/* Deferred mapping is only for real extents in the data fork. */
6339static bool
6340xfs_bmap_is_update_needed(
6341 struct xfs_bmbt_irec *bmap)
6342{
6343 return bmap->br_startblock != HOLESTARTBLOCK &&
6344 bmap->br_startblock != DELAYSTARTBLOCK;
6345}
6346
6347/* Record a bmap intent. */
6348static int
6349__xfs_bmap_add(
6350 struct xfs_mount *mp,
6351 struct xfs_defer_ops *dfops,
6352 enum xfs_bmap_intent_type type,
6353 struct xfs_inode *ip,
6354 int whichfork,
6355 struct xfs_bmbt_irec *bmap)
6356{
6357 int error;
6358 struct xfs_bmap_intent *bi;
6359
6360 trace_xfs_bmap_defer(mp,
6361 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6362 type,
6363 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6364 ip->i_ino, whichfork,
6365 bmap->br_startoff,
6366 bmap->br_blockcount,
6367 bmap->br_state);
6368
6369 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6370 INIT_LIST_HEAD(&bi->bi_list);
6371 bi->bi_type = type;
6372 bi->bi_owner = ip;
6373 bi->bi_whichfork = whichfork;
6374 bi->bi_bmap = *bmap;
6375
6376 error = xfs_defer_join(dfops, bi->bi_owner);
6377 if (error) {
6378 kmem_free(bi);
6379 return error;
6380 }
6381
6382 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6383 return 0;
6384}
6385
6386/* Map an extent into a file. */
6387int
6388xfs_bmap_map_extent(
6389 struct xfs_mount *mp,
6390 struct xfs_defer_ops *dfops,
6391 struct xfs_inode *ip,
6392 struct xfs_bmbt_irec *PREV)
6393{
6394 if (!xfs_bmap_is_update_needed(PREV))
6395 return 0;
6396
6397 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6398 XFS_DATA_FORK, PREV);
6399}
6400
6401/* Unmap an extent out of a file. */
6402int
6403xfs_bmap_unmap_extent(
6404 struct xfs_mount *mp,
6405 struct xfs_defer_ops *dfops,
6406 struct xfs_inode *ip,
6407 struct xfs_bmbt_irec *PREV)
6408{
6409 if (!xfs_bmap_is_update_needed(PREV))
6410 return 0;
6411
6412 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6413 XFS_DATA_FORK, PREV);
6414}
6415
6416/*
6417 * Process one of the deferred bmap operations. We pass back the
6418 * btree cursor to maintain our lock on the bmapbt between calls.
6419 */
6420int
6421xfs_bmap_finish_one(
6422 struct xfs_trans *tp,
6423 struct xfs_defer_ops *dfops,
6424 struct xfs_inode *ip,
6425 enum xfs_bmap_intent_type type,
6426 int whichfork,
6427 xfs_fileoff_t startoff,
6428 xfs_fsblock_t startblock,
6429 xfs_filblks_t blockcount,
6430 xfs_exntst_t state)
6431{
6432 struct xfs_bmbt_irec bmap;
6433 int nimaps = 1;
6434 xfs_fsblock_t firstfsb;
6435 int flags = XFS_BMAPI_REMAP;
6436 int done;
6437 int error = 0;
6438
6439 bmap.br_startblock = startblock;
6440 bmap.br_startoff = startoff;
6441 bmap.br_blockcount = blockcount;
6442 bmap.br_state = state;
6443
6444 trace_xfs_bmap_deferred(tp->t_mountp,
6445 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6446 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6447 ip->i_ino, whichfork, startoff, blockcount, state);
6448
6449 if (whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK)
6450 return -EFSCORRUPTED;
6451 if (whichfork == XFS_ATTR_FORK)
6452 flags |= XFS_BMAPI_ATTRFORK;
6453
6454 if (XFS_TEST_ERROR(false, tp->t_mountp,
6455 XFS_ERRTAG_BMAP_FINISH_ONE,
6456 XFS_RANDOM_BMAP_FINISH_ONE))
6457 return -EIO;
6458
6459 switch (type) {
6460 case XFS_BMAP_MAP:
6461 firstfsb = bmap.br_startblock;
6462 error = xfs_bmapi_write(tp, ip, bmap.br_startoff,
6463 bmap.br_blockcount, flags, &firstfsb,
6464 bmap.br_blockcount, &bmap, &nimaps,
6465 dfops);
6466 break;
6467 case XFS_BMAP_UNMAP:
6468 error = xfs_bunmapi(tp, ip, bmap.br_startoff,
6469 bmap.br_blockcount, flags, 1, &firstfsb,
6470 dfops, &done);
6471 ASSERT(done);
6472 break;
6473 default:
6474 ASSERT(0);
6475 error = -EFSCORRUPTED;
6476 }
6477
6478 return error;
6479}