blob: 829ad632533b80eac845008972f4a2d24a15bf3a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100030#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_extfree_item.h"
36#include "xfs_alloc.h"
37#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100038#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110039#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_rtalloc.h"
41#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_quota.h"
43#include "xfs_trans_space.h"
44#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110046#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110047#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110048#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100049#include "xfs_rmap.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100050#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070051#include "xfs_refcount.h"
Darrick J. Wongfd26a882016-11-28 14:57:42 +110052#include "xfs_rmap_btree.h"
Brian Foster974ae922016-11-28 14:57:42 +110053#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056kmem_zone_t *xfs_bmap_free_item_zone;
57
58/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110059 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110063 * Compute and fill in the value of the maximum depth of a bmap btree
64 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110066void
67xfs_bmap_compute_maxlevels(
68 xfs_mount_t *mp, /* file system mount structure */
69 int whichfork) /* data or attr fork */
70{
71 int level; /* btree level */
72 uint maxblocks; /* max blocks at this level */
73 uint maxleafents; /* max leaf entries possible */
74 int maxrootrecs; /* max records in root block */
75 int minleafrecs; /* min records in leaf block */
76 int minnoderecs; /* min records in node block */
77 int sz; /* root block size */
78
79 /*
80 * The maximum number of extents in a file, hence the maximum
81 * number of leaf entries, is controlled by the type of di_nextents
82 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
83 * (a signed 16-bit number, xfs_aextnum_t).
84 *
85 * Note that we can no longer assume that if we are in ATTR1 that
86 * the fork offset of all the inodes will be
87 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
88 * with ATTR2 and then mounted back with ATTR1, keeping the
89 * di_forkoff's fixed but probably at various positions. Therefore,
90 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
91 * of a minimum size available.
92 */
93 if (whichfork == XFS_DATA_FORK) {
94 maxleafents = MAXEXTNUM;
95 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
96 } else {
97 maxleafents = MAXAEXTNUM;
98 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
99 }
Eric Sandeen152d93b2014-04-14 18:58:51 +1000100 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100101 minleafrecs = mp->m_bmap_dmnr[0];
102 minnoderecs = mp->m_bmap_dmnr[1];
103 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
104 for (level = 1; maxblocks > 1; level++) {
105 if (maxblocks <= maxrootrecs)
106 maxblocks = 1;
107 else
108 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
109 }
110 mp->m_bm_maxlevels[whichfork] = level;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100113STATIC int /* error */
114xfs_bmbt_lookup_eq(
115 struct xfs_btree_cur *cur,
116 xfs_fileoff_t off,
117 xfs_fsblock_t bno,
118 xfs_filblks_t len,
119 int *stat) /* success/failure */
120{
121 cur->bc_rec.b.br_startoff = off;
122 cur->bc_rec.b.br_startblock = bno;
123 cur->bc_rec.b.br_blockcount = len;
124 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
125}
126
127STATIC int /* error */
128xfs_bmbt_lookup_ge(
129 struct xfs_btree_cur *cur,
130 xfs_fileoff_t off,
131 xfs_fsblock_t bno,
132 xfs_filblks_t len,
133 int *stat) /* success/failure */
134{
135 cur->bc_rec.b.br_startoff = off;
136 cur->bc_rec.b.br_startblock = bno;
137 cur->bc_rec.b.br_blockcount = len;
138 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
139}
140
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100141/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000142 * Check if the inode needs to be converted to btree format.
143 */
144static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
145{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700146 return whichfork != XFS_COW_FORK &&
147 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000148 XFS_IFORK_NEXTENTS(ip, whichfork) >
149 XFS_IFORK_MAXEXT(ip, whichfork);
150}
151
152/*
153 * Check if the inode should be converted to extent format.
154 */
155static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
156{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700157 return whichfork != XFS_COW_FORK &&
158 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000159 XFS_IFORK_NEXTENTS(ip, whichfork) <=
160 XFS_IFORK_MAXEXT(ip, whichfork);
161}
162
163/*
164 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100165 * by [off, bno, len, state].
166 * This either works (return 0) or gets an EFSCORRUPTED error.
167 */
168STATIC int
169xfs_bmbt_update(
170 struct xfs_btree_cur *cur,
171 xfs_fileoff_t off,
172 xfs_fsblock_t bno,
173 xfs_filblks_t len,
174 xfs_exntst_t state)
175{
176 union xfs_btree_rec rec;
177
178 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
179 return xfs_btree_update(cur, &rec);
180}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100183 * Compute the worst-case number of indirect blocks that will be used
184 * for ip's delayed extent of length "len".
185 */
186STATIC xfs_filblks_t
187xfs_bmap_worst_indlen(
188 xfs_inode_t *ip, /* incore inode pointer */
189 xfs_filblks_t len) /* delayed extent length */
190{
191 int level; /* btree level number */
192 int maxrecs; /* maximum record count at this level */
193 xfs_mount_t *mp; /* mount structure */
194 xfs_filblks_t rval; /* return value */
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100195 xfs_filblks_t orig_len;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100196
197 mp = ip->i_mount;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100198
199 /* Calculate the worst-case size of the bmbt. */
200 orig_len = len;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100201 maxrecs = mp->m_bmap_dmxr[0];
202 for (level = 0, rval = 0;
203 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
204 level++) {
205 len += maxrecs - 1;
206 do_div(len, maxrecs);
207 rval += len;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100208 if (len == 1) {
209 rval += XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100210 level - 1;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100211 break;
212 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100213 if (level == 0)
214 maxrecs = mp->m_bmap_dmxr[1];
215 }
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100216
217 /* Calculate the worst-case size of the rmapbt. */
218 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
219 rval += 1 + xfs_rmapbt_calc_size(mp, orig_len) +
220 mp->m_rmap_maxlevels;
221
Dave Chinner9e5987a2013-02-25 12:31:26 +1100222 return rval;
223}
224
225/*
226 * Calculate the default attribute fork offset for newly created inodes.
227 */
228uint
229xfs_default_attroffset(
230 struct xfs_inode *ip)
231{
232 struct xfs_mount *mp = ip->i_mount;
233 uint offset;
234
235 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100236 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100237 XFS_BMDR_SPACE_CALC(MINABTPTRS);
238 } else {
239 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
240 }
241
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100242 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100243 return offset;
244}
245
246/*
247 * Helper routine to reset inode di_forkoff field when switching
248 * attribute fork from local to extent format - we reset it where
249 * possible to make space available for inline data fork extents.
250 */
251STATIC void
252xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100253 xfs_inode_t *ip,
254 int whichfork)
255{
256 if (whichfork == XFS_ATTR_FORK &&
257 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
258 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
259 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
260 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
261
262 if (dfl_forkoff > ip->i_d.di_forkoff)
263 ip->i_d.di_forkoff = dfl_forkoff;
264 }
265}
266
Dave Chinner9e5987a2013-02-25 12:31:26 +1100267#ifdef DEBUG
268STATIC struct xfs_buf *
269xfs_bmap_get_bp(
270 struct xfs_btree_cur *cur,
271 xfs_fsblock_t bno)
272{
273 struct xfs_log_item_desc *lidp;
274 int i;
275
276 if (!cur)
277 return NULL;
278
279 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
280 if (!cur->bc_bufs[i])
281 break;
282 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
283 return cur->bc_bufs[i];
284 }
285
286 /* Chase down all the log items to see if the bp is there */
287 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
288 struct xfs_buf_log_item *bip;
289 bip = (struct xfs_buf_log_item *)lidp->lid_item;
290 if (bip->bli_item.li_type == XFS_LI_BUF &&
291 XFS_BUF_ADDR(bip->bli_buf) == bno)
292 return bip->bli_buf;
293 }
294
295 return NULL;
296}
297
298STATIC void
299xfs_check_block(
300 struct xfs_btree_block *block,
301 xfs_mount_t *mp,
302 int root,
303 short sz)
304{
305 int i, j, dmxr;
306 __be64 *pp, *thispa; /* pointer to block address */
307 xfs_bmbt_key_t *prevp, *keyp;
308
309 ASSERT(be16_to_cpu(block->bb_level) > 0);
310
311 prevp = NULL;
312 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
313 dmxr = mp->m_bmap_dmxr[0];
314 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
315
316 if (prevp) {
317 ASSERT(be64_to_cpu(prevp->br_startoff) <
318 be64_to_cpu(keyp->br_startoff));
319 }
320 prevp = keyp;
321
322 /*
323 * Compare the block numbers to see if there are dups.
324 */
325 if (root)
326 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
327 else
328 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
329
330 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
331 if (root)
332 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
333 else
334 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
335 if (*thispa == *pp) {
336 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
337 __func__, j, i,
338 (unsigned long long)be64_to_cpu(*thispa));
339 panic("%s: ptrs are equal in node\n",
340 __func__);
341 }
342 }
343 }
344}
345
346/*
347 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100348 * btree leaves. THis becomes prohibitively expensive for large extent count
349 * files, so don't bother with inodes that have more than 10,000 extents in
350 * them. The btree record ordering checks will still be done, so for such large
351 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100352 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100353STATIC void
354xfs_bmap_check_leaf_extents(
355 xfs_btree_cur_t *cur, /* btree cursor or null */
356 xfs_inode_t *ip, /* incore inode pointer */
357 int whichfork) /* data or attr fork */
358{
359 struct xfs_btree_block *block; /* current btree block */
360 xfs_fsblock_t bno; /* block # of "block" */
361 xfs_buf_t *bp; /* buffer for "block" */
362 int error; /* error return value */
363 xfs_extnum_t i=0, j; /* index into the extents list */
364 xfs_ifork_t *ifp; /* fork structure */
365 int level; /* btree level, for checking */
366 xfs_mount_t *mp; /* file system mount structure */
367 __be64 *pp; /* pointer to block address */
368 xfs_bmbt_rec_t *ep; /* pointer to current extent */
369 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
370 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
371 int bp_release = 0;
372
373 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
374 return;
375 }
376
Dave Chinnere3543812016-01-08 11:28:49 +1100377 /* skip large extent count inodes */
378 if (ip->i_d.di_nextents > 10000)
379 return;
380
Dave Chinner9e5987a2013-02-25 12:31:26 +1100381 bno = NULLFSBLOCK;
382 mp = ip->i_mount;
383 ifp = XFS_IFORK_PTR(ip, whichfork);
384 block = ifp->if_broot;
385 /*
386 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
387 */
388 level = be16_to_cpu(block->bb_level);
389 ASSERT(level > 0);
390 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
391 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
392 bno = be64_to_cpu(*pp);
393
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000394 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100395 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
396 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
397
398 /*
399 * Go down the tree until leaf level is reached, following the first
400 * pointer (leftmost) at each level.
401 */
402 while (level-- > 0) {
403 /* See if buf is in cur first */
404 bp_release = 0;
405 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
406 if (!bp) {
407 bp_release = 1;
408 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
409 XFS_BMAP_BTREE_REF,
410 &xfs_bmbt_buf_ops);
411 if (error)
412 goto error_norelse;
413 }
414 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100415 if (level == 0)
416 break;
417
418 /*
419 * Check this block for basic sanity (increasing keys and
420 * no duplicate blocks).
421 */
422
423 xfs_check_block(block, mp, 0, 0);
424 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
425 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100426 XFS_WANT_CORRUPTED_GOTO(mp,
427 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100428 if (bp_release) {
429 bp_release = 0;
430 xfs_trans_brelse(NULL, bp);
431 }
432 }
433
434 /*
435 * Here with bp and block set to the leftmost leaf node in the tree.
436 */
437 i = 0;
438
439 /*
440 * Loop over all leaf nodes checking that all extents are in the right order.
441 */
442 for (;;) {
443 xfs_fsblock_t nextbno;
444 xfs_extnum_t num_recs;
445
446
447 num_recs = xfs_btree_get_numrecs(block);
448
449 /*
450 * Read-ahead the next leaf block, if any.
451 */
452
453 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
454
455 /*
456 * Check all the extents to make sure they are OK.
457 * If we had a previous block, the last entry should
458 * conform with the first entry in this one.
459 */
460
461 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
462 if (i) {
463 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
464 xfs_bmbt_disk_get_blockcount(&last) <=
465 xfs_bmbt_disk_get_startoff(ep));
466 }
467 for (j = 1; j < num_recs; j++) {
468 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
469 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
470 xfs_bmbt_disk_get_blockcount(ep) <=
471 xfs_bmbt_disk_get_startoff(nextp));
472 ep = nextp;
473 }
474
475 last = *ep;
476 i += num_recs;
477 if (bp_release) {
478 bp_release = 0;
479 xfs_trans_brelse(NULL, bp);
480 }
481 bno = nextbno;
482 /*
483 * If we've reached the end, stop.
484 */
485 if (bno == NULLFSBLOCK)
486 break;
487
488 bp_release = 0;
489 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
490 if (!bp) {
491 bp_release = 1;
492 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
493 XFS_BMAP_BTREE_REF,
494 &xfs_bmbt_buf_ops);
495 if (error)
496 goto error_norelse;
497 }
498 block = XFS_BUF_TO_BLOCK(bp);
499 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100500
Dave Chinner9e5987a2013-02-25 12:31:26 +1100501 return;
502
503error0:
504 xfs_warn(mp, "%s: at error0", __func__);
505 if (bp_release)
506 xfs_trans_brelse(NULL, bp);
507error_norelse:
508 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
509 __func__, i);
510 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
511 return;
512}
513
514/*
515 * Add bmap trace insert entries for all the contents of the extent records.
516 */
517void
518xfs_bmap_trace_exlist(
519 xfs_inode_t *ip, /* incore inode pointer */
520 xfs_extnum_t cnt, /* count of entries in the list */
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100521 int whichfork, /* data or attr or cow fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100522 unsigned long caller_ip)
523{
524 xfs_extnum_t idx; /* extent record index */
525 xfs_ifork_t *ifp; /* inode fork pointer */
526 int state = 0;
527
528 if (whichfork == XFS_ATTR_FORK)
529 state |= BMAP_ATTRFORK;
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100530 else if (whichfork == XFS_COW_FORK)
531 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100532
533 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +1100534 ASSERT(cnt == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100535 for (idx = 0; idx < cnt; idx++)
Eric Sandeen77105172016-12-05 12:31:50 +1100536 trace_xfs_extlist(ip, idx, state, caller_ip);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100537}
538
539/*
540 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000541 * given the caller's original parameters. Specifically check the
542 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100543 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
544 */
545STATIC void
546xfs_bmap_validate_ret(
547 xfs_fileoff_t bno,
548 xfs_filblks_t len,
549 int flags,
550 xfs_bmbt_irec_t *mval,
551 int nmap,
552 int ret_nmap)
553{
554 int i; /* index to map values */
555
556 ASSERT(ret_nmap <= nmap);
557
558 for (i = 0; i < ret_nmap; i++) {
559 ASSERT(mval[i].br_blockcount > 0);
560 if (!(flags & XFS_BMAPI_ENTIRE)) {
561 ASSERT(mval[i].br_startoff >= bno);
562 ASSERT(mval[i].br_blockcount <= len);
563 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
564 bno + len);
565 } else {
566 ASSERT(mval[i].br_startoff < bno + len);
567 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
568 bno);
569 }
570 ASSERT(i == 0 ||
571 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
572 mval[i].br_startoff);
573 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
574 mval[i].br_startblock != HOLESTARTBLOCK);
575 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
576 mval[i].br_state == XFS_EXT_UNWRITTEN);
577 }
578}
579
580#else
581#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
582#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
583#endif /* DEBUG */
584
585/*
586 * bmap free list manipulation functions
587 */
588
589/*
590 * Add the extent to the list of extents to be free at transaction end.
591 * The list is maintained sorted (by block number).
592 */
593void
594xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000595 struct xfs_mount *mp,
596 struct xfs_defer_ops *dfops,
597 xfs_fsblock_t bno,
598 xfs_filblks_t len,
599 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100600{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000601 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100602#ifdef DEBUG
603 xfs_agnumber_t agno;
604 xfs_agblock_t agbno;
605
606 ASSERT(bno != NULLFSBLOCK);
607 ASSERT(len > 0);
608 ASSERT(len <= MAXEXTLEN);
609 ASSERT(!isnullstartblock(bno));
610 agno = XFS_FSB_TO_AGNO(mp, bno);
611 agbno = XFS_FSB_TO_AGBNO(mp, bno);
612 ASSERT(agno < mp->m_sb.sb_agcount);
613 ASSERT(agbno < mp->m_sb.sb_agblocks);
614 ASSERT(len < mp->m_sb.sb_agblocks);
615 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
616#endif
617 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000618
Dave Chinner9e5987a2013-02-25 12:31:26 +1100619 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000620 new->xefi_startblock = bno;
621 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000622 if (oinfo)
623 new->xefi_oinfo = *oinfo;
624 else
625 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000626 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
627 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000628 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100629}
630
631/*
632 * Inode fork format manipulation functions
633 */
634
635/*
636 * Transform a btree format file with only one leaf node, where the
637 * extents list will fit in the inode, into an extents format file.
638 * Since the file extents are already in-core, all we have to do is
639 * give up the space for the btree root and pitch the leaf block.
640 */
641STATIC int /* error */
642xfs_bmap_btree_to_extents(
643 xfs_trans_t *tp, /* transaction pointer */
644 xfs_inode_t *ip, /* incore inode pointer */
645 xfs_btree_cur_t *cur, /* btree cursor */
646 int *logflagsp, /* inode logging flags */
647 int whichfork) /* data or attr fork */
648{
649 /* REFERENCED */
650 struct xfs_btree_block *cblock;/* child btree block */
651 xfs_fsblock_t cbno; /* child block number */
652 xfs_buf_t *cbp; /* child block's buffer */
653 int error; /* error return value */
654 xfs_ifork_t *ifp; /* inode fork data */
655 xfs_mount_t *mp; /* mount point structure */
656 __be64 *pp; /* ptr to block address */
657 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000658 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100659
660 mp = ip->i_mount;
661 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700662 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100663 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
664 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
665 rblock = ifp->if_broot;
666 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
667 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
668 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
669 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
670 cbno = be64_to_cpu(*pp);
671 *logflagsp = 0;
672#ifdef DEBUG
673 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
674 return error;
675#endif
676 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
677 &xfs_bmbt_buf_ops);
678 if (error)
679 return error;
680 cblock = XFS_BUF_TO_BLOCK(cbp);
681 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
682 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000683 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
684 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100685 ip->i_d.di_nblocks--;
686 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
687 xfs_trans_binval(tp, cbp);
688 if (cur->bc_bufs[0] == cbp)
689 cur->bc_bufs[0] = NULL;
690 xfs_iroot_realloc(ip, -1, whichfork);
691 ASSERT(ifp->if_broot == NULL);
692 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
693 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
694 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
695 return 0;
696}
697
698/*
699 * Convert an extents-format file into a btree-format file.
700 * The new file will have a root block (in the inode) and a single child block.
701 */
702STATIC int /* error */
703xfs_bmap_extents_to_btree(
704 xfs_trans_t *tp, /* transaction pointer */
705 xfs_inode_t *ip, /* incore inode pointer */
706 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000707 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100708 xfs_btree_cur_t **curp, /* cursor returned to caller */
709 int wasdel, /* converting a delayed alloc */
710 int *logflagsp, /* inode logging flags */
711 int whichfork) /* data or attr fork */
712{
713 struct xfs_btree_block *ablock; /* allocated (child) bt block */
714 xfs_buf_t *abp; /* buffer for ablock */
715 xfs_alloc_arg_t args; /* allocation arguments */
716 xfs_bmbt_rec_t *arp; /* child record pointer */
717 struct xfs_btree_block *block; /* btree root block */
718 xfs_btree_cur_t *cur; /* bmap btree cursor */
719 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
720 int error; /* error return value */
721 xfs_extnum_t i, cnt; /* extent record index */
722 xfs_ifork_t *ifp; /* inode fork pointer */
723 xfs_bmbt_key_t *kp; /* root block key pointer */
724 xfs_mount_t *mp; /* mount structure */
725 xfs_extnum_t nextents; /* number of file extents */
726 xfs_bmbt_ptr_t *pp; /* root block address pointer */
727
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500728 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700729 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100730 ifp = XFS_IFORK_PTR(ip, whichfork);
731 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
732
733 /*
734 * Make space in the inode incore.
735 */
736 xfs_iroot_realloc(ip, 1, whichfork);
737 ifp->if_flags |= XFS_IFBROOT;
738
739 /*
740 * Fill in the root.
741 */
742 block = ifp->if_broot;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500743 if (xfs_sb_version_hascrc(&mp->m_sb))
744 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
745 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
746 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
747 else
748 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
749 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
750 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100751
752 /*
753 * Need a cursor. Can't allocate until bb_level is filled in.
754 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100755 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
756 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000757 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100758 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
759 /*
760 * Convert to a btree with two levels, one record in root.
761 */
762 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
763 memset(&args, 0, sizeof(args));
764 args.tp = tp;
765 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000766 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100767 args.firstblock = *firstblock;
768 if (*firstblock == NULLFSBLOCK) {
769 args.type = XFS_ALLOCTYPE_START_BNO;
770 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000771 } else if (dfops->dop_low) {
Darrick J. Wong90e20562016-10-03 09:11:45 -0700772try_another_ag:
Dave Chinner9e5987a2013-02-25 12:31:26 +1100773 args.type = XFS_ALLOCTYPE_START_BNO;
774 args.fsbno = *firstblock;
775 } else {
776 args.type = XFS_ALLOCTYPE_NEAR_BNO;
777 args.fsbno = *firstblock;
778 }
779 args.minlen = args.maxlen = args.prod = 1;
780 args.wasdel = wasdel;
781 *logflagsp = 0;
782 if ((error = xfs_alloc_vextent(&args))) {
783 xfs_iroot_realloc(ip, -1, whichfork);
784 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
785 return error;
786 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700787
788 /*
789 * During a CoW operation, the allocation and bmbt updates occur in
790 * different transactions. The mapping code tries to put new bmbt
791 * blocks near extents being mapped, but the only way to guarantee this
792 * is if the alloc and the mapping happen in a single transaction that
793 * has a block reservation. That isn't the case here, so if we run out
794 * of space we'll try again with another AG.
795 */
796 if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) &&
797 args.fsbno == NULLFSBLOCK &&
798 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
799 dfops->dop_low = true;
800 goto try_another_ag;
801 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100802 /*
803 * Allocation can't fail, the space was reserved.
804 */
805 ASSERT(args.fsbno != NULLFSBLOCK);
806 ASSERT(*firstblock == NULLFSBLOCK ||
807 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000808 (dfops->dop_low &&
Dave Chinner9e5987a2013-02-25 12:31:26 +1100809 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
810 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
811 cur->bc_private.b.allocated++;
812 ip->i_d.di_nblocks++;
813 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
814 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
815 /*
816 * Fill in the child block.
817 */
818 abp->b_ops = &xfs_bmbt_buf_ops;
819 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500820 if (xfs_sb_version_hascrc(&mp->m_sb))
821 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
822 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
823 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
824 else
825 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
826 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
827 XFS_BTREE_LONG_PTRS);
828
Dave Chinner9e5987a2013-02-25 12:31:26 +1100829 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100830 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100831 for (cnt = i = 0; i < nextents; i++) {
832 ep = xfs_iext_get_ext(ifp, i);
833 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
834 arp->l0 = cpu_to_be64(ep->l0);
835 arp->l1 = cpu_to_be64(ep->l1);
836 arp++; cnt++;
837 }
838 }
839 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
840 xfs_btree_set_numrecs(ablock, cnt);
841
842 /*
843 * Fill in the root key and pointer.
844 */
845 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
846 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
847 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
848 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
849 be16_to_cpu(block->bb_level)));
850 *pp = cpu_to_be64(args.fsbno);
851
852 /*
853 * Do all this logging at the end so that
854 * the root is at the right level.
855 */
856 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
857 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
858 ASSERT(*curp == NULL);
859 *curp = cur;
860 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
861 return 0;
862}
863
864/*
865 * Convert a local file to an extents file.
866 * This code is out of bounds for data forks of regular files,
867 * since the file data needs to get logged so things will stay consistent.
868 * (The bmap-level manipulations are ok, though).
869 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000870void
871xfs_bmap_local_to_extents_empty(
872 struct xfs_inode *ip,
873 int whichfork)
874{
875 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
876
Darrick J. Wong60b49842016-10-03 09:11:34 -0700877 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000878 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
879 ASSERT(ifp->if_bytes == 0);
880 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
881
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000882 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000883 ifp->if_flags &= ~XFS_IFINLINE;
884 ifp->if_flags |= XFS_IFEXTENTS;
885 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
886}
887
888
Dave Chinner9e5987a2013-02-25 12:31:26 +1100889STATIC int /* error */
890xfs_bmap_local_to_extents(
891 xfs_trans_t *tp, /* transaction pointer */
892 xfs_inode_t *ip, /* incore inode pointer */
893 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
894 xfs_extlen_t total, /* total blocks needed by transaction */
895 int *logflagsp, /* inode logging flags */
896 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500897 void (*init_fn)(struct xfs_trans *tp,
898 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100899 struct xfs_inode *ip,
900 struct xfs_ifork *ifp))
901{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000902 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100903 int flags; /* logging flags returned */
904 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000905 xfs_alloc_arg_t args; /* allocation arguments */
906 xfs_buf_t *bp; /* buffer for extent block */
907 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100908
909 /*
910 * We don't want to deal with the case of keeping inode data inline yet.
911 * So sending the data fork of a regular inode is invalid.
912 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100913 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100914 ifp = XFS_IFORK_PTR(ip, whichfork);
915 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000916
917 if (!ifp->if_bytes) {
918 xfs_bmap_local_to_extents_empty(ip, whichfork);
919 flags = XFS_ILOG_CORE;
920 goto done;
921 }
922
Dave Chinner9e5987a2013-02-25 12:31:26 +1100923 flags = 0;
924 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000925 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
926 XFS_IFINLINE);
927 memset(&args, 0, sizeof(args));
928 args.tp = tp;
929 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000930 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000931 args.firstblock = *firstblock;
932 /*
933 * Allocate a block. We know we need only one, since the
934 * file currently fits in an inode.
935 */
936 if (*firstblock == NULLFSBLOCK) {
Darrick J. Wong90e20562016-10-03 09:11:45 -0700937try_another_ag:
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000938 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
939 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100940 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000941 args.fsbno = *firstblock;
942 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100943 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000944 args.total = total;
945 args.minlen = args.maxlen = args.prod = 1;
946 error = xfs_alloc_vextent(&args);
947 if (error)
948 goto done;
949
Darrick J. Wong90e20562016-10-03 09:11:45 -0700950 /*
951 * During a CoW operation, the allocation and bmbt updates occur in
952 * different transactions. The mapping code tries to put new bmbt
953 * blocks near extents being mapped, but the only way to guarantee this
954 * is if the alloc and the mapping happen in a single transaction that
955 * has a block reservation. That isn't the case here, so if we run out
956 * of space we'll try again with another AG.
957 */
958 if (xfs_sb_version_hasreflink(&ip->i_mount->m_sb) &&
959 args.fsbno == NULLFSBLOCK &&
960 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
961 goto try_another_ag;
962 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000963 /* Can't fail, the space was reserved. */
964 ASSERT(args.fsbno != NULLFSBLOCK);
965 ASSERT(args.len == 1);
966 *firstblock = args.fsbno;
967 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
968
Dave Chinnerfe22d552015-01-22 09:30:06 +1100969 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100970 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100971 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100972 * The callout is responsible for logging because the remote format
973 * might differ from the local format and thus we don't know how much to
974 * log here. Note that init_fn must also set the buffer log item type
975 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100976 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000977 init_fn(tp, bp, ip, ifp);
978
Brian Fosterb7cdc662015-10-12 15:40:24 +1100979 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000980 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
981 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100982 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000983
984 xfs_iext_add(ifp, 0, 1);
985 ep = xfs_iext_get_ext(ifp, 0);
986 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
987 trace_xfs_bmap_post_update(ip, 0,
988 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
989 _THIS_IP_);
990 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
991 ip->i_d.di_nblocks = 1;
992 xfs_trans_mod_dquot_byino(tp, ip,
993 XFS_TRANS_DQ_BCOUNT, 1L);
994 flags |= xfs_ilog_fext(whichfork);
995
Dave Chinner9e5987a2013-02-25 12:31:26 +1100996done:
997 *logflagsp = flags;
998 return error;
999}
1000
1001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 * Called from xfs_bmap_add_attrfork to handle btree format files.
1003 */
1004STATIC int /* error */
1005xfs_bmap_add_attrfork_btree(
1006 xfs_trans_t *tp, /* transaction pointer */
1007 xfs_inode_t *ip, /* incore inode pointer */
1008 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001009 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 int *flags) /* inode logging flags */
1011{
1012 xfs_btree_cur_t *cur; /* btree cursor */
1013 int error; /* error return value */
1014 xfs_mount_t *mp; /* file system mount struct */
1015 int stat; /* newroot status */
1016
1017 mp = ip->i_mount;
1018 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1019 *flags |= XFS_ILOG_DBROOT;
1020 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001021 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001022 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 cur->bc_private.b.firstblock = *firstblock;
1024 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1025 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001026 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001027 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001028 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 goto error0;
1030 if (stat == 0) {
1031 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +10001032 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034 *firstblock = cur->bc_private.b.firstblock;
1035 cur->bc_private.b.allocated = 0;
1036 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1037 }
1038 return 0;
1039error0:
1040 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1041 return error;
1042}
1043
1044/*
1045 * Called from xfs_bmap_add_attrfork to handle extents format files.
1046 */
1047STATIC int /* error */
1048xfs_bmap_add_attrfork_extents(
1049 xfs_trans_t *tp, /* transaction pointer */
1050 xfs_inode_t *ip, /* incore inode pointer */
1051 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001052 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 int *flags) /* inode logging flags */
1054{
1055 xfs_btree_cur_t *cur; /* bmap btree cursor */
1056 int error; /* error return value */
1057
1058 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1059 return 0;
1060 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001061 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 flags, XFS_DATA_FORK);
1063 if (cur) {
1064 cur->bc_private.b.allocated = 0;
1065 xfs_btree_del_cursor(cur,
1066 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1067 }
1068 return error;
1069}
1070
1071/*
Dave Chinner1e823792013-02-11 15:58:13 +11001072 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1073 * different data fork content type needs a different callout to do the
1074 * conversion. Some are basic and only require special block initialisation
1075 * callouts for the data formating, others (directories) are so specialised they
1076 * handle everything themselves.
1077 *
1078 * XXX (dgc): investigate whether directory conversion can use the generic
1079 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001080 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 */
1082STATIC int /* error */
1083xfs_bmap_add_attrfork_local(
1084 xfs_trans_t *tp, /* transaction pointer */
1085 xfs_inode_t *ip, /* incore inode pointer */
1086 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001087 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 int *flags) /* inode logging flags */
1089{
1090 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
1092 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1093 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001094
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001095 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001097 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 dargs.dp = ip;
1099 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001100 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001101 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 dargs.whichfork = XFS_DATA_FORK;
1103 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001104 return xfs_dir2_sf_to_block(&dargs);
1105 }
1106
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001107 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001108 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1109 flags, XFS_DATA_FORK,
1110 xfs_symlink_local_to_remote);
1111
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001112 /* should only be called for types that support local format data */
1113 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001114 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115}
1116
1117/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001118 * Convert inode from non-attributed to attributed.
1119 * Must not be in a transaction, ip must not be locked.
1120 */
1121int /* error code */
1122xfs_bmap_add_attrfork(
1123 xfs_inode_t *ip, /* incore inode pointer */
1124 int size, /* space new attribute needs */
1125 int rsvd) /* xact may use reserved blks */
1126{
1127 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001128 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001129 xfs_mount_t *mp; /* mount structure */
1130 xfs_trans_t *tp; /* transaction pointer */
1131 int blks; /* space reservation */
1132 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001133 int logflags; /* logging flags */
1134 int error; /* error return value */
1135
1136 ASSERT(XFS_IFORK_Q(ip) == 0);
1137
1138 mp = ip->i_mount;
1139 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001140
Dave Chinner9e5987a2013-02-25 12:31:26 +11001141 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001142
1143 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1144 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1145 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001146 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001147
Dave Chinner9e5987a2013-02-25 12:31:26 +11001148 xfs_ilock(ip, XFS_ILOCK_EXCL);
1149 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1150 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1151 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001152 if (error)
1153 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001154 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001155 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001156 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1157 /*
1158 * For inodes coming from pre-6.2 filesystems.
1159 */
1160 ASSERT(ip->i_d.di_aformat == 0);
1161 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1162 }
1163 ASSERT(ip->i_d.di_anextents == 0);
1164
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001165 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001166 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1167
1168 switch (ip->i_d.di_format) {
1169 case XFS_DINODE_FMT_DEV:
1170 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1171 break;
1172 case XFS_DINODE_FMT_UUID:
1173 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1174 break;
1175 case XFS_DINODE_FMT_LOCAL:
1176 case XFS_DINODE_FMT_EXTENTS:
1177 case XFS_DINODE_FMT_BTREE:
1178 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1179 if (!ip->i_d.di_forkoff)
1180 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1181 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1182 version = 2;
1183 break;
1184 default:
1185 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001186 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001187 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001188 }
1189
1190 ASSERT(ip->i_afp == NULL);
1191 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1192 ip->i_afp->if_flags = XFS_IFEXTENTS;
1193 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001194 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001195 switch (ip->i_d.di_format) {
1196 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001197 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001198 &logflags);
1199 break;
1200 case XFS_DINODE_FMT_EXTENTS:
1201 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001202 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001203 break;
1204 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001205 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001206 &logflags);
1207 break;
1208 default:
1209 error = 0;
1210 break;
1211 }
1212 if (logflags)
1213 xfs_trans_log_inode(tp, ip, logflags);
1214 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001215 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001216 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1217 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001218 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001219
1220 spin_lock(&mp->m_sb_lock);
1221 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1222 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001223 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001224 }
1225 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1226 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001227 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001228 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001229 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001230 if (log_sb)
1231 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001232 }
1233
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001234 error = xfs_defer_finish(&tp, &dfops, NULL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001235 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001236 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001237 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001238 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001239 return error;
1240
1241bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001242 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001243trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001244 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001245 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001246 return error;
1247}
1248
1249/*
1250 * Internal and external extent tree search functions.
1251 */
1252
1253/*
1254 * Read in the extents to if_extents.
1255 * All inode fields are set up by caller, we just traverse the btree
1256 * and copy the records in. If the file system cannot contain unwritten
1257 * extents, the records are checked for no "state" flags.
1258 */
1259int /* error */
1260xfs_bmap_read_extents(
1261 xfs_trans_t *tp, /* transaction pointer */
1262 xfs_inode_t *ip, /* incore inode */
1263 int whichfork) /* data or attr fork */
1264{
1265 struct xfs_btree_block *block; /* current btree block */
1266 xfs_fsblock_t bno; /* block # of "block" */
1267 xfs_buf_t *bp; /* buffer for "block" */
1268 int error; /* error return value */
1269 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1270 xfs_extnum_t i, j; /* index into the extents list */
1271 xfs_ifork_t *ifp; /* fork structure */
1272 int level; /* btree level, for checking */
1273 xfs_mount_t *mp; /* file system mount structure */
1274 __be64 *pp; /* pointer to block address */
1275 /* REFERENCED */
1276 xfs_extnum_t room; /* number of entries there's room for */
1277
1278 bno = NULLFSBLOCK;
1279 mp = ip->i_mount;
1280 ifp = XFS_IFORK_PTR(ip, whichfork);
1281 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1282 XFS_EXTFMT_INODE(ip);
1283 block = ifp->if_broot;
1284 /*
1285 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1286 */
1287 level = be16_to_cpu(block->bb_level);
1288 ASSERT(level > 0);
1289 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1290 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001291 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001292 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1293 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1294 /*
1295 * Go down the tree until leaf level is reached, following the first
1296 * pointer (leftmost) at each level.
1297 */
1298 while (level-- > 0) {
1299 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1300 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1301 if (error)
1302 return error;
1303 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001304 if (level == 0)
1305 break;
1306 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1307 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001308 XFS_WANT_CORRUPTED_GOTO(mp,
1309 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001310 xfs_trans_brelse(tp, bp);
1311 }
1312 /*
1313 * Here with bp and block set to the leftmost leaf node in the tree.
1314 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001315 room = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001316 i = 0;
1317 /*
1318 * Loop over all leaf nodes. Copy information to the extent records.
1319 */
1320 for (;;) {
1321 xfs_bmbt_rec_t *frp;
1322 xfs_fsblock_t nextbno;
1323 xfs_extnum_t num_recs;
1324 xfs_extnum_t start;
1325
1326 num_recs = xfs_btree_get_numrecs(block);
1327 if (unlikely(i + num_recs > room)) {
1328 ASSERT(i + num_recs <= room);
1329 xfs_warn(ip->i_mount,
1330 "corrupt dinode %Lu, (btree extents).",
1331 (unsigned long long) ip->i_ino);
1332 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1333 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1334 goto error0;
1335 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001336 /*
1337 * Read-ahead the next leaf block, if any.
1338 */
1339 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1340 if (nextbno != NULLFSBLOCK)
1341 xfs_btree_reada_bufl(mp, nextbno, 1,
1342 &xfs_bmbt_buf_ops);
1343 /*
1344 * Copy records into the extent records.
1345 */
1346 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1347 start = i;
1348 for (j = 0; j < num_recs; j++, i++, frp++) {
1349 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1350 trp->l0 = be64_to_cpu(frp->l0);
1351 trp->l1 = be64_to_cpu(frp->l1);
1352 }
1353 if (exntf == XFS_EXTFMT_NOSTATE) {
1354 /*
1355 * Check all attribute bmap btree records and
1356 * any "older" data bmap btree records for a
1357 * set bit in the "extent flag" position.
1358 */
1359 if (unlikely(xfs_check_nostate_extents(ifp,
1360 start, num_recs))) {
1361 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1362 XFS_ERRLEVEL_LOW,
1363 ip->i_mount);
1364 goto error0;
1365 }
1366 }
1367 xfs_trans_brelse(tp, bp);
1368 bno = nextbno;
1369 /*
1370 * If we've reached the end, stop.
1371 */
1372 if (bno == NULLFSBLOCK)
1373 break;
1374 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1375 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1376 if (error)
1377 return error;
1378 block = XFS_BUF_TO_BLOCK(bp);
1379 }
Darrick J. Wong356a3222016-12-05 12:36:56 +11001380 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1381 return -EFSCORRUPTED;
Eric Sandeen5d829302016-11-08 12:59:42 +11001382 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11001383 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1384 return 0;
1385error0:
1386 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001387 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001388}
1389
Dave Chinner9e5987a2013-02-25 12:31:26 +11001390/*
1391 * Returns the file-relative block number of the first unused block(s)
1392 * in the file with at least "len" logically contiguous blocks free.
1393 * This is the lowest-address hole if the file has holes, else the first block
1394 * past the end of file.
1395 * Return 0 if the file is currently local (in-inode).
1396 */
1397int /* error */
1398xfs_bmap_first_unused(
1399 xfs_trans_t *tp, /* transaction pointer */
1400 xfs_inode_t *ip, /* incore inode */
1401 xfs_extlen_t len, /* size of hole to find */
1402 xfs_fileoff_t *first_unused, /* unused block */
1403 int whichfork) /* data or attr fork */
1404{
1405 int error; /* error return value */
1406 int idx; /* extent record index */
1407 xfs_ifork_t *ifp; /* inode fork pointer */
1408 xfs_fileoff_t lastaddr; /* last block number seen */
1409 xfs_fileoff_t lowest; /* lowest useful block */
1410 xfs_fileoff_t max; /* starting useful block */
1411 xfs_fileoff_t off; /* offset for this block */
1412 xfs_extnum_t nextents; /* number of extent entries */
1413
1414 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1415 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1416 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1417 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1418 *first_unused = 0;
1419 return 0;
1420 }
1421 ifp = XFS_IFORK_PTR(ip, whichfork);
1422 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1423 (error = xfs_iread_extents(tp, ip, whichfork)))
1424 return error;
1425 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001426 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001427 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1428 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1429 off = xfs_bmbt_get_startoff(ep);
1430 /*
1431 * See if the hole before this extent will work.
1432 */
1433 if (off >= lowest + len && off - max >= len) {
1434 *first_unused = max;
1435 return 0;
1436 }
1437 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1438 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1439 }
1440 *first_unused = max;
1441 return 0;
1442}
1443
1444/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001445 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001446 * last_block (input value) in the file.
1447 * This is not based on i_size, it is based on the extent records.
1448 * Returns 0 for local files, as they do not have extent records.
1449 */
1450int /* error */
1451xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001452 struct xfs_trans *tp, /* transaction pointer */
1453 struct xfs_inode *ip, /* incore inode */
1454 xfs_fileoff_t *last_block, /* last block */
1455 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001456{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001457 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1458 struct xfs_bmbt_irec got;
1459 xfs_extnum_t idx;
1460 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001461
Christoph Hellwig86685f72016-11-24 11:39:38 +11001462 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1463 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001464 *last_block = 0;
1465 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001466 case XFS_DINODE_FMT_BTREE:
1467 case XFS_DINODE_FMT_EXTENTS:
1468 break;
1469 default:
1470 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001471 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001472
1473 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1474 error = xfs_iread_extents(tp, ip, whichfork);
1475 if (error)
1476 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001477 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001478
1479 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1480 if (got.br_startoff <= *last_block - 1)
1481 return 0;
1482 }
1483
1484 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1485 *last_block = got.br_startoff + got.br_blockcount;
1486 return 0;
1487 }
1488
1489 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001490 return 0;
1491}
1492
Dave Chinner68988112013-08-12 20:49:42 +10001493int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001494xfs_bmap_last_extent(
1495 struct xfs_trans *tp,
1496 struct xfs_inode *ip,
1497 int whichfork,
1498 struct xfs_bmbt_irec *rec,
1499 int *is_empty)
1500{
1501 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1502 int error;
1503 int nextents;
1504
1505 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1506 error = xfs_iread_extents(tp, ip, whichfork);
1507 if (error)
1508 return error;
1509 }
1510
Eric Sandeen5d829302016-11-08 12:59:42 +11001511 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001512 if (nextents == 0) {
1513 *is_empty = 1;
1514 return 0;
1515 }
1516
1517 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1518 *is_empty = 0;
1519 return 0;
1520}
1521
1522/*
1523 * Check the last inode extent to determine whether this allocation will result
1524 * in blocks being allocated at the end of the file. When we allocate new data
1525 * blocks at the end of the file which do not start at the previous data block,
1526 * we will try to align the new blocks at stripe unit boundaries.
1527 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001528 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001529 * at, or past the EOF.
1530 */
1531STATIC int
1532xfs_bmap_isaeof(
1533 struct xfs_bmalloca *bma,
1534 int whichfork)
1535{
1536 struct xfs_bmbt_irec rec;
1537 int is_empty;
1538 int error;
1539
1540 bma->aeof = 0;
1541 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1542 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001543 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001544 return error;
1545
Dave Chinner6e708bc2013-11-22 10:41:16 +11001546 if (is_empty) {
1547 bma->aeof = 1;
1548 return 0;
1549 }
1550
Dave Chinner9e5987a2013-02-25 12:31:26 +11001551 /*
1552 * Check if we are allocation or past the last extent, or at least into
1553 * the last delayed allocated extent.
1554 */
1555 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1556 (bma->offset >= rec.br_startoff &&
1557 isnullstartblock(rec.br_startblock));
1558 return 0;
1559}
1560
1561/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001562 * Returns the file-relative block number of the first block past eof in
1563 * the file. This is not based on i_size, it is based on the extent records.
1564 * Returns 0 for local files, as they do not have extent records.
1565 */
1566int
1567xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001568 struct xfs_inode *ip,
1569 xfs_fileoff_t *last_block,
1570 int whichfork)
1571{
1572 struct xfs_bmbt_irec rec;
1573 int is_empty;
1574 int error;
1575
1576 *last_block = 0;
1577
1578 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1579 return 0;
1580
1581 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1582 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001583 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001584
1585 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1586 if (error || is_empty)
1587 return error;
1588
1589 *last_block = rec.br_startoff + rec.br_blockcount;
1590 return 0;
1591}
1592
1593/*
1594 * Returns whether the selected fork of the inode has exactly one
1595 * block or not. For the data fork we check this matches di_size,
1596 * implying the file's range is 0..bsize-1.
1597 */
1598int /* 1=>1 block, 0=>otherwise */
1599xfs_bmap_one_block(
1600 xfs_inode_t *ip, /* incore inode */
1601 int whichfork) /* data or attr fork */
1602{
1603 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1604 xfs_ifork_t *ifp; /* inode fork pointer */
1605 int rval; /* return value */
1606 xfs_bmbt_irec_t s; /* internal version of extent */
1607
1608#ifndef DEBUG
1609 if (whichfork == XFS_DATA_FORK)
1610 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1611#endif /* !DEBUG */
1612 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1613 return 0;
1614 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1615 return 0;
1616 ifp = XFS_IFORK_PTR(ip, whichfork);
1617 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1618 ep = xfs_iext_get_ext(ifp, 0);
1619 xfs_bmbt_get_all(ep, &s);
1620 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1621 if (rval && whichfork == XFS_DATA_FORK)
1622 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1623 return rval;
1624}
1625
1626/*
1627 * Extent tree manipulation functions used during allocation.
1628 */
1629
1630/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001631 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 */
1633STATIC int /* error */
1634xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001635 struct xfs_bmalloca *bma,
1636 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001638 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001640 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001643 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 xfs_fileoff_t new_endoff; /* end offset of new entry */
1645 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1646 /* left is 0, right is 1, prev is 2 */
1647 int rval=0; /* return value (logging flags) */
1648 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001649 xfs_filblks_t da_new; /* new count del alloc blocks used */
1650 xfs_filblks_t da_old; /* old count del alloc blocks used */
1651 xfs_filblks_t temp=0; /* value for da_new calculations */
1652 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001654 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001655 xfs_extnum_t *nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001657 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001658 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001659 ASSERT(whichfork != XFS_ATTR_FORK);
1660 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1661 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001662
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001663 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001664 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001665 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001666 ASSERT(!bma->cur ||
1667 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001668
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001669 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671#define LEFT r[0]
1672#define RIGHT r[1]
1673#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Darrick J. Wong60b49842016-10-03 09:11:34 -07001675 if (whichfork == XFS_COW_FORK)
1676 state |= BMAP_COWFORK;
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 /*
1679 * Set up a bunch of variables to make the tests simpler.
1680 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001681 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 xfs_bmbt_get_all(ep, &PREV);
1683 new_endoff = new->br_startoff + new->br_blockcount;
1684 ASSERT(PREV.br_startoff <= new->br_startoff);
1685 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001686
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001687 da_old = startblockval(PREV.br_startblock);
1688 da_new = 0;
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 /*
1691 * Set flags determining what part of the previous delayed allocation
1692 * extent is being replaced by a real allocation.
1693 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001694 if (PREV.br_startoff == new->br_startoff)
1695 state |= BMAP_LEFT_FILLING;
1696 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1697 state |= BMAP_RIGHT_FILLING;
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /*
1700 * Check and set flags if this segment has a left neighbor.
1701 * Don't set contiguous if the combined extent would be too large.
1702 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001703 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001704 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001705 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001706
1707 if (isnullstartblock(LEFT.br_startblock))
1708 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001710
1711 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1712 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1713 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1714 LEFT.br_state == new->br_state &&
1715 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1716 state |= BMAP_LEFT_CONTIG;
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 /*
1719 * Check and set flags if this segment has a right neighbor.
1720 * Don't set contiguous if the combined extent would be too large.
1721 * Also check for all-three-contiguous being too large.
1722 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001723 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001724 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001725 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001726
1727 if (isnullstartblock(RIGHT.br_startblock))
1728 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001730
1731 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1732 new_endoff == RIGHT.br_startoff &&
1733 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1734 new->br_state == RIGHT.br_state &&
1735 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1736 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1737 BMAP_RIGHT_FILLING)) !=
1738 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1739 BMAP_RIGHT_FILLING) ||
1740 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1741 <= MAXEXTLEN))
1742 state |= BMAP_RIGHT_CONTIG;
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 error = 0;
1745 /*
1746 * Switch out based on the FILLING and CONTIG state bits.
1747 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001748 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1749 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1750 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1751 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 /*
1753 * Filling in all of a previously delayed allocation extent.
1754 * The left and right neighbors are both contiguous with new.
1755 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001756 bma->idx--;
1757 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1758 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 LEFT.br_blockcount + PREV.br_blockcount +
1760 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001761 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001762
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001763 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001764 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001765 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1767 else {
1768 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001769 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001771 RIGHT.br_blockcount, &i);
1772 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001774 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001775 error = xfs_btree_delete(bma->cur, &i);
1776 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001778 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001779 error = xfs_btree_decrement(bma->cur, 0, &i);
1780 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001782 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001783 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 LEFT.br_startblock,
1785 LEFT.br_blockcount +
1786 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001787 RIGHT.br_blockcount, LEFT.br_state);
1788 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 goto done;
1790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 break;
1792
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001793 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /*
1795 * Filling in all of a previously delayed allocation extent.
1796 * The left neighbor is contiguous, the right is not.
1797 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001798 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001799
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001800 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1801 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001802 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001803 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001804
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001805 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1806 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 rval = XFS_ILOG_DEXT;
1808 else {
1809 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001810 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001812 &i);
1813 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001815 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001816 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 LEFT.br_startblock,
1818 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 PREV.br_blockcount, LEFT.br_state);
1820 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 goto done;
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 break;
1824
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001825 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /*
1827 * Filling in all of a previously delayed allocation extent.
1828 * The right neighbor is contiguous, the left is not.
1829 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001830 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 xfs_bmbt_set_startblock(ep, new->br_startblock);
1832 xfs_bmbt_set_blockcount(ep,
1833 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001834 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001835
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001836 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1837 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 rval = XFS_ILOG_DEXT;
1839 else {
1840 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001841 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001843 RIGHT.br_blockcount, &i);
1844 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001846 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 new->br_startblock,
1849 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001850 RIGHT.br_blockcount, PREV.br_state);
1851 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto done;
1853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 break;
1855
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001856 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /*
1858 * Filling in all of a previously delayed allocation extent.
1859 * Neither the left nor right neighbors are contiguous with
1860 * the new one.
1861 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001862 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001864 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001865
Darrick J. Wong60b49842016-10-03 09:11:34 -07001866 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001867 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1869 else {
1870 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001871 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001873 &i);
1874 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001876 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001877 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1878 error = xfs_btree_insert(bma->cur, &i);
1879 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001881 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 break;
1884
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001885 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 /*
1887 * Filling in the first part of a previous delayed allocation.
1888 * The left neighbor is contiguous.
1889 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001890 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1891 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 LEFT.br_blockcount + new->br_blockcount);
1893 xfs_bmbt_set_startoff(ep,
1894 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001895 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001898 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001900 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 rval = XFS_ILOG_DEXT;
1902 else {
1903 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001904 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001906 &i);
1907 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001909 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001910 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 LEFT.br_startblock,
1912 LEFT.br_blockcount +
1913 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001914 LEFT.br_state);
1915 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 goto done;
1917 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001918 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001919 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001920 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001921 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001922
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001923 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 break;
1925
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001926 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 /*
1928 * Filling in the first part of a previous delayed allocation.
1929 * The left neighbor is not contiguous.
1930 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001931 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 xfs_bmbt_set_startoff(ep, new_endoff);
1933 temp = PREV.br_blockcount - new->br_blockcount;
1934 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001935 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001936 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001937 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1939 else {
1940 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001941 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001943 &i);
1944 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001946 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001947 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1948 error = xfs_btree_insert(bma->cur, &i);
1949 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001951 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001953
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001954 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001955 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001956 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001957 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 rval |= tmp_rval;
1959 if (error)
1960 goto done;
1961 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001962 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001963 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001964 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1965 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001966 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001967 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 break;
1969
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001970 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /*
1972 * Filling in the last part of a previous delayed allocation.
1973 * The right neighbor is contiguous with the new allocation.
1974 */
1975 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001976 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001978 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001979 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 new->br_blockcount + RIGHT.br_blockcount,
1981 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001982 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1983 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 rval = XFS_ILOG_DEXT;
1985 else {
1986 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001987 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001989 RIGHT.br_blockcount, &i);
1990 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001992 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001993 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 new->br_startblock,
1995 new->br_blockcount +
1996 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001997 RIGHT.br_state);
1998 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 goto done;
2000 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002001
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002002 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002003 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002004 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002005 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002006 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002007
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002008 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 break;
2010
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002011 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 /*
2013 * Filling in the last part of a previous delayed allocation.
2014 * The right neighbor is not contiguous.
2015 */
2016 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002017 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002019 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002020 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002021 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2023 else {
2024 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002025 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002027 &i);
2028 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002030 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002031 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2032 error = xfs_btree_insert(bma->cur, &i);
2033 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002035 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002037
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002038 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002039 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002040 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002041 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 rval |= tmp_rval;
2043 if (error)
2044 goto done;
2045 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002046 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002047 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002048 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2049 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002050 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002051 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002052
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002053 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 break;
2055
2056 case 0:
2057 /*
2058 * Filling in the middle part of a previous delayed allocation.
2059 * Contiguity is impossible here.
2060 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002061 *
2062 * We start with a delayed allocation:
2063 *
2064 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2065 * PREV @ idx
2066 *
2067 * and we are allocating:
2068 * +rrrrrrrrrrrrrrrrr+
2069 * new
2070 *
2071 * and we set it up for insertion as:
2072 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2073 * new
2074 * PREV @ idx LEFT RIGHT
2075 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 */
2077 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002079 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002080 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2081 LEFT = *new;
2082 RIGHT.br_state = PREV.br_state;
2083 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002084 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002085 RIGHT.br_startoff = new_endoff;
2086 RIGHT.br_blockcount = temp2;
2087 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002088 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002089 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002090 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2092 else {
2093 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002094 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002096 &i);
2097 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002099 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002100 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2101 error = xfs_btree_insert(bma->cur, &i);
2102 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002104 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002106
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002107 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002108 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002109 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002110 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 rval |= tmp_rval;
2112 if (error)
2113 goto done;
2114 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002115 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2116 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002117 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002118 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002119 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002120 error = xfs_mod_fdblocks(bma->ip->i_mount,
2121 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002122 ASSERT(!error);
2123 if (error)
2124 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002126
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002127 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002128 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002129 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2130 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2131 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002132 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002133 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002134
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002135 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002136 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 break;
2138
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002139 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2140 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2141 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2142 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2143 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2144 case BMAP_LEFT_CONTIG:
2145 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 /*
2147 * These cases are all impossible.
2148 */
2149 ASSERT(0);
2150 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002151
Darrick J. Wong9c194642016-08-03 12:16:05 +10002152 /* add reverse mapping */
2153 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2154 if (error)
2155 goto done;
2156
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002157 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002158 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002159 int tmp_logflags; /* partial log flag return val */
2160
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002161 ASSERT(bma->cur == NULL);
2162 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002163 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002164 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002165 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002166 if (error)
2167 goto done;
2168 }
2169
2170 /* adjust for changes in reserved delayed indirect blocks */
2171 if (da_old || da_new) {
2172 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002173 if (bma->cur)
2174 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002175 ASSERT(temp <= da_old);
2176 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002177 xfs_mod_fdblocks(bma->ip->i_mount,
2178 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002179 }
2180
2181 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002182 if (bma->cur)
2183 bma->cur->bc_private.b.allocated = 0;
2184
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002185 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002187 if (whichfork != XFS_COW_FORK)
2188 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return error;
2190#undef LEFT
2191#undef RIGHT
2192#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193}
2194
2195/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002196 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 */
2198STATIC int /* error */
2199xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002200 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002202 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002204 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002205 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002206 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002207 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002210 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002213 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 xfs_fileoff_t new_endoff; /* end offset of new entry */
2215 xfs_exntst_t newext; /* new extent state */
2216 xfs_exntst_t oldext; /* old extent state */
2217 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2218 /* left is 0, right is 1, prev is 2 */
2219 int rval=0; /* return value (logging flags) */
2220 int state = 0;/* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002221 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002223 *logflagsp = 0;
2224
2225 cur = *curp;
2226 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2227
2228 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002229 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002230 ASSERT(!isnullstartblock(new->br_startblock));
2231
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002232 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002233
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234#define LEFT r[0]
2235#define RIGHT r[1]
2236#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 /*
2239 * Set up a bunch of variables to make the tests simpler.
2240 */
2241 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002242 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 xfs_bmbt_get_all(ep, &PREV);
2244 newext = new->br_state;
2245 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2246 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2247 ASSERT(PREV.br_state == oldext);
2248 new_endoff = new->br_startoff + new->br_blockcount;
2249 ASSERT(PREV.br_startoff <= new->br_startoff);
2250 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 /*
2253 * Set flags determining what part of the previous oldext allocation
2254 * extent is being replaced by a newext allocation.
2255 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002256 if (PREV.br_startoff == new->br_startoff)
2257 state |= BMAP_LEFT_FILLING;
2258 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2259 state |= BMAP_RIGHT_FILLING;
2260
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 /*
2262 * Check and set flags if this segment has a left neighbor.
2263 * Don't set contiguous if the combined extent would be too large.
2264 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002265 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002266 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002267 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002268
2269 if (isnullstartblock(LEFT.br_startblock))
2270 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002272
2273 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2274 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2275 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2276 LEFT.br_state == newext &&
2277 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2278 state |= BMAP_LEFT_CONTIG;
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 /*
2281 * Check and set flags if this segment has a right neighbor.
2282 * Don't set contiguous if the combined extent would be too large.
2283 * Also check for all-three-contiguous being too large.
2284 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002285 if (*idx < xfs_iext_count(&ip->i_df) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002286 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002287 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002288 if (isnullstartblock(RIGHT.br_startblock))
2289 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002291
2292 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2293 new_endoff == RIGHT.br_startoff &&
2294 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2295 newext == RIGHT.br_state &&
2296 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2297 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2298 BMAP_RIGHT_FILLING)) !=
2299 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2300 BMAP_RIGHT_FILLING) ||
2301 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2302 <= MAXEXTLEN))
2303 state |= BMAP_RIGHT_CONTIG;
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 /*
2306 * Switch out based on the FILLING and CONTIG state bits.
2307 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002308 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2309 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2310 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2311 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /*
2313 * Setting all of a previous oldext extent to newext.
2314 * The left and right neighbors are both contiguous with new.
2315 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002316 --*idx;
2317
2318 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2319 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 LEFT.br_blockcount + PREV.br_blockcount +
2321 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002322 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002323
Christoph Hellwigec90c552011-05-23 08:52:53 +00002324 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 ip->i_d.di_nextents -= 2;
2326 if (cur == NULL)
2327 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2328 else {
2329 rval = XFS_ILOG_CORE;
2330 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2331 RIGHT.br_startblock,
2332 RIGHT.br_blockcount, &i)))
2333 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002334 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002335 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002337 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002338 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002340 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002341 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002343 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002344 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002346 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2348 LEFT.br_startblock,
2349 LEFT.br_blockcount + PREV.br_blockcount +
2350 RIGHT.br_blockcount, LEFT.br_state)))
2351 goto done;
2352 }
2353 break;
2354
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002355 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 /*
2357 * Setting all of a previous oldext extent to newext.
2358 * The left neighbor is contiguous, the right is not.
2359 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002360 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002361
Christoph Hellwigec90c552011-05-23 08:52:53 +00002362 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2363 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2364 LEFT.br_blockcount + PREV.br_blockcount);
2365 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2366
2367 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 ip->i_d.di_nextents--;
2369 if (cur == NULL)
2370 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2371 else {
2372 rval = XFS_ILOG_CORE;
2373 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2374 PREV.br_startblock, PREV.br_blockcount,
2375 &i)))
2376 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002377 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002378 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002381 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002383 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2385 LEFT.br_startblock,
2386 LEFT.br_blockcount + PREV.br_blockcount,
2387 LEFT.br_state)))
2388 goto done;
2389 }
2390 break;
2391
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002392 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 /*
2394 * Setting all of a previous oldext extent to newext.
2395 * The right neighbor is contiguous, the left is not.
2396 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002397 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 xfs_bmbt_set_blockcount(ep,
2399 PREV.br_blockcount + RIGHT.br_blockcount);
2400 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002401 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2402 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 ip->i_d.di_nextents--;
2404 if (cur == NULL)
2405 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2406 else {
2407 rval = XFS_ILOG_CORE;
2408 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2409 RIGHT.br_startblock,
2410 RIGHT.br_blockcount, &i)))
2411 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002412 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002413 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002416 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002418 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2420 new->br_startblock,
2421 new->br_blockcount + RIGHT.br_blockcount,
2422 newext)))
2423 goto done;
2424 }
2425 break;
2426
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002427 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 /*
2429 * Setting all of a previous oldext extent to newext.
2430 * Neither the left nor right neighbors are contiguous with
2431 * the new one.
2432 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002433 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002435 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002436
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 if (cur == NULL)
2438 rval = XFS_ILOG_DEXT;
2439 else {
2440 rval = 0;
2441 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2442 new->br_startblock, new->br_blockcount,
2443 &i)))
2444 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002445 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2447 new->br_startblock, new->br_blockcount,
2448 newext)))
2449 goto done;
2450 }
2451 break;
2452
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002453 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 /*
2455 * Setting the first part of a previous oldext extent to newext.
2456 * The left neighbor is contiguous.
2457 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002458 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2459 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 LEFT.br_blockcount + new->br_blockcount);
2461 xfs_bmbt_set_startoff(ep,
2462 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002463 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002464
Christoph Hellwigec90c552011-05-23 08:52:53 +00002465 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 xfs_bmbt_set_startblock(ep,
2467 new->br_startblock + new->br_blockcount);
2468 xfs_bmbt_set_blockcount(ep,
2469 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002470 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002471
Christoph Hellwigec90c552011-05-23 08:52:53 +00002472 --*idx;
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (cur == NULL)
2475 rval = XFS_ILOG_DEXT;
2476 else {
2477 rval = 0;
2478 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2479 PREV.br_startblock, PREV.br_blockcount,
2480 &i)))
2481 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002482 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 if ((error = xfs_bmbt_update(cur,
2484 PREV.br_startoff + new->br_blockcount,
2485 PREV.br_startblock + new->br_blockcount,
2486 PREV.br_blockcount - new->br_blockcount,
2487 oldext)))
2488 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002489 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002491 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 LEFT.br_startblock,
2493 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002494 LEFT.br_state);
2495 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 goto done;
2497 }
2498 break;
2499
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002500 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 /*
2502 * Setting the first part of a previous oldext extent to newext.
2503 * The left neighbor is not contiguous.
2504 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002505 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2507 xfs_bmbt_set_startoff(ep, new_endoff);
2508 xfs_bmbt_set_blockcount(ep,
2509 PREV.br_blockcount - new->br_blockcount);
2510 xfs_bmbt_set_startblock(ep,
2511 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002512 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002513
Christoph Hellwigec90c552011-05-23 08:52:53 +00002514 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 ip->i_d.di_nextents++;
2516 if (cur == NULL)
2517 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2518 else {
2519 rval = XFS_ILOG_CORE;
2520 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2521 PREV.br_startblock, PREV.br_blockcount,
2522 &i)))
2523 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002524 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 if ((error = xfs_bmbt_update(cur,
2526 PREV.br_startoff + new->br_blockcount,
2527 PREV.br_startblock + new->br_blockcount,
2528 PREV.br_blockcount - new->br_blockcount,
2529 oldext)))
2530 goto done;
2531 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002532 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002534 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 }
2536 break;
2537
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002538 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 /*
2540 * Setting the last part of a previous oldext extent to newext.
2541 * The right neighbor is contiguous with the new allocation.
2542 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002543 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 xfs_bmbt_set_blockcount(ep,
2545 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002546 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2547
2548 ++*idx;
2549
2550 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2551 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002552 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002554 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002555
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (cur == NULL)
2557 rval = XFS_ILOG_DEXT;
2558 else {
2559 rval = 0;
2560 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2561 PREV.br_startblock,
2562 PREV.br_blockcount, &i)))
2563 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002564 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2566 PREV.br_startblock,
2567 PREV.br_blockcount - new->br_blockcount,
2568 oldext)))
2569 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002570 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 goto done;
2572 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2573 new->br_startblock,
2574 new->br_blockcount + RIGHT.br_blockcount,
2575 newext)))
2576 goto done;
2577 }
2578 break;
2579
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002580 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 /*
2582 * Setting the last part of a previous oldext extent to newext.
2583 * The right neighbor is not contiguous.
2584 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002585 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 xfs_bmbt_set_blockcount(ep,
2587 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002588 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002589
Christoph Hellwigec90c552011-05-23 08:52:53 +00002590 ++*idx;
2591 xfs_iext_insert(ip, *idx, 1, new, state);
2592
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 ip->i_d.di_nextents++;
2594 if (cur == NULL)
2595 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2596 else {
2597 rval = XFS_ILOG_CORE;
2598 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2599 PREV.br_startblock, PREV.br_blockcount,
2600 &i)))
2601 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002602 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2604 PREV.br_startblock,
2605 PREV.br_blockcount - new->br_blockcount,
2606 oldext)))
2607 goto done;
2608 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2609 new->br_startblock, new->br_blockcount,
2610 &i)))
2611 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002612 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002614 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002616 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
2618 break;
2619
2620 case 0:
2621 /*
2622 * Setting the middle part of a previous oldext extent to
2623 * newext. Contiguity is impossible here.
2624 * One extent becomes three extents.
2625 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002626 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 xfs_bmbt_set_blockcount(ep,
2628 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002629 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002630
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 r[0] = *new;
2632 r[1].br_startoff = new_endoff;
2633 r[1].br_blockcount =
2634 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2635 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2636 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002637
2638 ++*idx;
2639 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 ip->i_d.di_nextents += 2;
2642 if (cur == NULL)
2643 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2644 else {
2645 rval = XFS_ILOG_CORE;
2646 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2647 PREV.br_startblock, PREV.br_blockcount,
2648 &i)))
2649 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002650 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 /* new right extent - oldext */
2652 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2653 r[1].br_startblock, r[1].br_blockcount,
2654 r[1].br_state)))
2655 goto done;
2656 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002658 cur->bc_rec.b.br_blockcount =
2659 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002660 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002662 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002663 /*
2664 * Reset the cursor to the position of the new extent
2665 * we are about to insert as we can't trust it after
2666 * the previous insert.
2667 */
2668 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2669 new->br_startblock, new->br_blockcount,
2670 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002672 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002674 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002675 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002677 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
2679 break;
2680
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002681 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2682 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2683 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2684 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2685 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2686 case BMAP_LEFT_CONTIG:
2687 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 /*
2689 * These cases are all impossible.
2690 */
2691 ASSERT(0);
2692 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002693
Darrick J. Wong9c194642016-08-03 12:16:05 +10002694 /* update reverse mappings */
2695 error = xfs_rmap_convert_extent(mp, dfops, ip, XFS_DATA_FORK, new);
2696 if (error)
2697 goto done;
2698
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002699 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002700 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002701 int tmp_logflags; /* partial log flag return val */
2702
2703 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002704 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002705 0, &tmp_logflags, XFS_DATA_FORK);
2706 *logflagsp |= tmp_logflags;
2707 if (error)
2708 goto done;
2709 }
2710
2711 /* clear out the allocated field, done with it now in any case. */
2712 if (cur) {
2713 cur->bc_private.b.allocated = 0;
2714 *curp = cur;
2715 }
2716
2717 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002719 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 return error;
2721#undef LEFT
2722#undef RIGHT
2723#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
2725
2726/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002727 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002729STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730xfs_bmap_add_extent_hole_delay(
2731 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002732 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002733 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002734 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002736 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2738 xfs_filblks_t newlen=0; /* new indirect size */
2739 xfs_filblks_t oldlen=0; /* old indirect size */
2740 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2741 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002742 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002744 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002746 if (whichfork == XFS_COW_FORK)
2747 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002748 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 /*
2751 * Check and set flags if this segment has a left neighbor
2752 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002753 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002754 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002755 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002756
2757 if (isnullstartblock(left.br_startblock))
2758 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 /*
2762 * Check and set flags if the current (right) segment exists.
2763 * If it doesn't exist, we're converting the hole at end-of-file.
2764 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002765 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002766 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002767 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002768
2769 if (isnullstartblock(right.br_startblock))
2770 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 /*
2774 * Set contiguity flags on the left and right neighbors.
2775 * Don't let extents get too large, even if the pieces are contiguous.
2776 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002777 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2778 left.br_startoff + left.br_blockcount == new->br_startoff &&
2779 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2780 state |= BMAP_LEFT_CONTIG;
2781
2782 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2783 new->br_startoff + new->br_blockcount == right.br_startoff &&
2784 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2785 (!(state & BMAP_LEFT_CONTIG) ||
2786 (left.br_blockcount + new->br_blockcount +
2787 right.br_blockcount <= MAXEXTLEN)))
2788 state |= BMAP_RIGHT_CONTIG;
2789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 /*
2791 * Switch out based on the contiguity flags.
2792 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002793 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2794 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 /*
2796 * New allocation is contiguous with delayed allocations
2797 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002798 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002800 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 temp = left.br_blockcount + new->br_blockcount +
2802 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002803
Christoph Hellwigec90c552011-05-23 08:52:53 +00002804 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2805 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002806 oldlen = startblockval(left.br_startblock) +
2807 startblockval(new->br_startblock) +
2808 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002810 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002811 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002812 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002813
Christoph Hellwigec90c552011-05-23 08:52:53 +00002814 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 break;
2816
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002817 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 /*
2819 * New allocation is contiguous with a delayed allocation
2820 * on the left.
2821 * Merge the new allocation with the left neighbor.
2822 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002823 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002825
2826 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2827 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002828 oldlen = startblockval(left.br_startblock) +
2829 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002831 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002832 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002833 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 break;
2835
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002836 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 /*
2838 * New allocation is contiguous with a delayed allocation
2839 * on the right.
2840 * Merge the new allocation with the right neighbor.
2841 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002842 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002844 oldlen = startblockval(new->br_startblock) +
2845 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002847 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2848 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002849 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002850 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 break;
2852
2853 case 0:
2854 /*
2855 * New allocation is not contiguous with another
2856 * delayed allocation.
2857 * Insert a new entry.
2858 */
2859 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002860 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 break;
2862 }
2863 if (oldlen != newlen) {
2864 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002865 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2866 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 /*
2868 * Nothing to do for disk quota accounting here.
2869 */
2870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871}
2872
2873/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002874 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 */
2876STATIC int /* error */
2877xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002878 struct xfs_bmalloca *bma,
2879 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002881 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 int i; /* temp state */
2884 xfs_ifork_t *ifp; /* inode fork pointer */
2885 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2886 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002887 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002889 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Eric Sandeenf1f96c42016-01-04 16:10:42 +11002891 mp = bma->ip->i_mount;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002892 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002893
Christoph Hellwigc6534242011-09-18 20:41:05 +00002894 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002895 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002896 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002897 ASSERT(!bma->cur ||
2898 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Darrick J. Wong3993bae2016-10-03 09:11:32 -07002899 ASSERT(whichfork != XFS_COW_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002900
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002901 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002902
2903 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002904 if (whichfork == XFS_ATTR_FORK)
2905 state |= BMAP_ATTRFORK;
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 /*
2908 * Check and set flags if this segment has a left neighbor.
2909 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002910 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002911 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002912 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002913 if (isnullstartblock(left.br_startblock))
2914 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 /*
2918 * Check and set flags if this segment has a current value.
2919 * Not true if we're inserting into the "hole" at eof.
2920 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002921 if (bma->idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002922 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002923 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002924 if (isnullstartblock(right.br_startblock))
2925 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 /*
2929 * We're inserting a real allocation between "left" and "right".
2930 * Set the contiguity flags. Don't let extents get too large.
2931 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002932 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2933 left.br_startoff + left.br_blockcount == new->br_startoff &&
2934 left.br_startblock + left.br_blockcount == new->br_startblock &&
2935 left.br_state == new->br_state &&
2936 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2937 state |= BMAP_LEFT_CONTIG;
2938
2939 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2940 new->br_startoff + new->br_blockcount == right.br_startoff &&
2941 new->br_startblock + new->br_blockcount == right.br_startblock &&
2942 new->br_state == right.br_state &&
2943 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2944 (!(state & BMAP_LEFT_CONTIG) ||
2945 left.br_blockcount + new->br_blockcount +
2946 right.br_blockcount <= MAXEXTLEN))
2947 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002949 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 /*
2951 * Select which case we're in here, and implement it.
2952 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002953 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2954 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 /*
2956 * New allocation is contiguous with real allocations on the
2957 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002958 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002960 --bma->idx;
2961 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2962 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 left.br_blockcount + new->br_blockcount +
2964 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002965 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002966
Christoph Hellwigc6534242011-09-18 20:41:05 +00002967 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002968
Christoph Hellwigc6534242011-09-18 20:41:05 +00002969 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
2970 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
2971 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002972 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002973 } else {
2974 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002975 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
2976 right.br_startblock, right.br_blockcount,
2977 &i);
2978 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002979 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002980 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002981 error = xfs_btree_delete(bma->cur, &i);
2982 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002983 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002984 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002985 error = xfs_btree_decrement(bma->cur, 0, &i);
2986 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002987 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002988 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002989 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002990 left.br_startblock,
2991 left.br_blockcount +
2992 new->br_blockcount +
2993 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002994 left.br_state);
2995 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002996 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002998 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003000 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 /*
3002 * New allocation is contiguous with a real allocation
3003 * on the left.
3004 * Merge the new allocation with the left neighbor.
3005 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003006 --bma->idx;
3007 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3008 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003010 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003011
Christoph Hellwigc6534242011-09-18 20:41:05 +00003012 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003013 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003014 } else {
3015 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003016 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3017 left.br_startblock, left.br_blockcount,
3018 &i);
3019 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003020 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003021 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003022 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003023 left.br_startblock,
3024 left.br_blockcount +
3025 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003026 left.br_state);
3027 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003028 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003030 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003032 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 /*
3034 * New allocation is contiguous with a real allocation
3035 * on the right.
3036 * Merge the new allocation with the right neighbor.
3037 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003038 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3039 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003040 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 new->br_blockcount + right.br_blockcount,
3042 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003043 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003044
Christoph Hellwigc6534242011-09-18 20:41:05 +00003045 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003046 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003047 } else {
3048 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003049 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003050 right.br_startoff,
3051 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003052 right.br_blockcount, &i);
3053 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003054 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003055 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003056 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003057 new->br_startblock,
3058 new->br_blockcount +
3059 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003060 right.br_state);
3061 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003062 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003064 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066 case 0:
3067 /*
3068 * New allocation is not contiguous with another
3069 * real allocation.
3070 * Insert a new entry.
3071 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003072 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3073 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3074 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3075 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003076 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003077 } else {
3078 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003079 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003080 new->br_startoff,
3081 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003082 new->br_blockcount, &i);
3083 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003084 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003085 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003086 bma->cur->bc_rec.b.br_state = new->br_state;
3087 error = xfs_btree_insert(bma->cur, &i);
3088 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003089 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003090 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003092 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003094
Darrick J. Wong9c194642016-08-03 12:16:05 +10003095 /* add reverse mapping */
3096 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
3097 if (error)
3098 goto done;
3099
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003100 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003101 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003102 int tmp_logflags; /* partial log flag return val */
3103
Christoph Hellwigc6534242011-09-18 20:41:05 +00003104 ASSERT(bma->cur == NULL);
3105 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003106 bma->firstblock, bma->dfops, &bma->cur,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003107 0, &tmp_logflags, whichfork);
3108 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003109 if (error)
3110 goto done;
3111 }
3112
3113 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003114 if (bma->cur)
3115 bma->cur->bc_private.b.allocated = 0;
3116
3117 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003118done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003119 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003120 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
Nathan Scottdd9f4382006-01-11 15:28:28 +11003123/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003124 * Functions used in the extent read, allocate and remove paths
3125 */
3126
3127/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003128 * Adjust the size of the new extent based on di_extsize and rt extsize.
3129 */
Dave Chinner68988112013-08-12 20:49:42 +10003130int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003131xfs_bmap_extsize_align(
3132 xfs_mount_t *mp,
3133 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3134 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3135 xfs_extlen_t extsz, /* align to this extent size */
3136 int rt, /* is this a realtime inode? */
3137 int eof, /* is extent at end-of-file? */
3138 int delay, /* creating delalloc extent? */
3139 int convert, /* overwriting unwritten extent? */
3140 xfs_fileoff_t *offp, /* in/out: aligned offset */
3141 xfs_extlen_t *lenp) /* in/out: aligned length */
3142{
3143 xfs_fileoff_t orig_off; /* original offset */
3144 xfs_extlen_t orig_alen; /* original length */
3145 xfs_fileoff_t orig_end; /* original off+len */
3146 xfs_fileoff_t nexto; /* next file offset */
3147 xfs_fileoff_t prevo; /* previous file offset */
3148 xfs_fileoff_t align_off; /* temp for offset */
3149 xfs_extlen_t align_alen; /* temp for length */
3150 xfs_extlen_t temp; /* temp for calculations */
3151
3152 if (convert)
3153 return 0;
3154
3155 orig_off = align_off = *offp;
3156 orig_alen = align_alen = *lenp;
3157 orig_end = orig_off + orig_alen;
3158
3159 /*
3160 * If this request overlaps an existing extent, then don't
3161 * attempt to perform any additional alignment.
3162 */
3163 if (!delay && !eof &&
3164 (orig_off >= gotp->br_startoff) &&
3165 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3166 return 0;
3167 }
3168
3169 /*
3170 * If the file offset is unaligned vs. the extent size
3171 * we need to align it. This will be possible unless
3172 * the file was previously written with a kernel that didn't
3173 * perform this alignment, or if a truncate shot us in the
3174 * foot.
3175 */
3176 temp = do_mod(orig_off, extsz);
3177 if (temp) {
3178 align_alen += temp;
3179 align_off -= temp;
3180 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003181
3182 /* Same adjustment for the end of the requested area. */
3183 temp = (align_alen % extsz);
3184 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003185 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003186
3187 /*
3188 * For large extent hint sizes, the aligned extent might be larger than
3189 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3190 * the length back under MAXEXTLEN. The outer allocation loops handle
3191 * short allocation just fine, so it is safe to do this. We only want to
3192 * do it when we are forced to, though, because it means more allocation
3193 * operations are required.
3194 */
3195 while (align_alen > MAXEXTLEN)
3196 align_alen -= extsz;
3197 ASSERT(align_alen <= MAXEXTLEN);
3198
Nathan Scottdd9f4382006-01-11 15:28:28 +11003199 /*
3200 * If the previous block overlaps with this proposed allocation
3201 * then move the start forward without adjusting the length.
3202 */
3203 if (prevp->br_startoff != NULLFILEOFF) {
3204 if (prevp->br_startblock == HOLESTARTBLOCK)
3205 prevo = prevp->br_startoff;
3206 else
3207 prevo = prevp->br_startoff + prevp->br_blockcount;
3208 } else
3209 prevo = 0;
3210 if (align_off != orig_off && align_off < prevo)
3211 align_off = prevo;
3212 /*
3213 * If the next block overlaps with this proposed allocation
3214 * then move the start back without adjusting the length,
3215 * but not before offset 0.
3216 * This may of course make the start overlap previous block,
3217 * and if we hit the offset 0 limit then the next block
3218 * can still overlap too.
3219 */
3220 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3221 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3222 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3223 nexto = gotp->br_startoff + gotp->br_blockcount;
3224 else
3225 nexto = gotp->br_startoff;
3226 } else
3227 nexto = NULLFILEOFF;
3228 if (!eof &&
3229 align_off + align_alen != orig_end &&
3230 align_off + align_alen > nexto)
3231 align_off = nexto > align_alen ? nexto - align_alen : 0;
3232 /*
3233 * If we're now overlapping the next or previous extent that
3234 * means we can't fit an extsz piece in this hole. Just move
3235 * the start forward to the first valid spot and set
3236 * the length so we hit the end.
3237 */
3238 if (align_off != orig_off && align_off < prevo)
3239 align_off = prevo;
3240 if (align_off + align_alen != orig_end &&
3241 align_off + align_alen > nexto &&
3242 nexto != NULLFILEOFF) {
3243 ASSERT(nexto > prevo);
3244 align_alen = nexto - align_off;
3245 }
3246
3247 /*
3248 * If realtime, and the result isn't a multiple of the realtime
3249 * extent size we need to remove blocks until it is.
3250 */
3251 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3252 /*
3253 * We're not covering the original request, or
3254 * we won't be able to once we fix the length.
3255 */
3256 if (orig_off < align_off ||
3257 orig_end > align_off + align_alen ||
3258 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003259 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003260 /*
3261 * Try to fix it by moving the start up.
3262 */
3263 if (align_off + temp <= orig_off) {
3264 align_alen -= temp;
3265 align_off += temp;
3266 }
3267 /*
3268 * Try to fix it by moving the end in.
3269 */
3270 else if (align_off + align_alen - temp >= orig_end)
3271 align_alen -= temp;
3272 /*
3273 * Set the start to the minimum then trim the length.
3274 */
3275 else {
3276 align_alen -= orig_off - align_off;
3277 align_off = orig_off;
3278 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3279 }
3280 /*
3281 * Result doesn't cover the request, fail it.
3282 */
3283 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003284 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003285 } else {
3286 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003287 /* see MAXEXTLEN handling above */
3288 ASSERT(orig_end <= align_off + align_alen ||
3289 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003290 }
3291
3292#ifdef DEBUG
3293 if (!eof && gotp->br_startoff != NULLFILEOFF)
3294 ASSERT(align_off + align_alen <= gotp->br_startoff);
3295 if (prevp->br_startoff != NULLFILEOFF)
3296 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3297#endif
3298
3299 *lenp = align_alen;
3300 *offp = align_off;
3301 return 0;
3302}
3303
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304#define XFS_ALLOC_GAP_UNITS 4
3305
Dave Chinner68988112013-08-12 20:49:42 +10003306void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003307xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003308 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
3310 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3312 xfs_mount_t *mp; /* mount point structure */
3313 int nullfb; /* true if ap->firstblock isn't set */
3314 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
3316#define ISVALID(x,y) \
3317 (rt ? \
3318 (x) < mp->m_sb.sb_rblocks : \
3319 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3320 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3321 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3322
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003324 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003325 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3326 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003327 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 /*
3329 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003330 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003332 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3333 !isnullstartblock(ap->prev.br_startblock) &&
3334 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3335 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003336 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 /*
3338 * Adjust for the gap between prevp and us.
3339 */
Dave Chinner3a756672011-09-18 20:40:58 +00003340 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003341 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003343 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3344 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 }
3346 /*
3347 * If not at eof, then compare the two neighbor blocks.
3348 * Figure out whether either one gives us a good starting point,
3349 * and pick the better one.
3350 */
3351 else if (!ap->eof) {
3352 xfs_fsblock_t gotbno; /* right side block number */
3353 xfs_fsblock_t gotdiff=0; /* right side difference */
3354 xfs_fsblock_t prevbno; /* left side block number */
3355 xfs_fsblock_t prevdiff=0; /* left side difference */
3356
3357 /*
3358 * If there's a previous (left) block, select a requested
3359 * start block based on it.
3360 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003361 if (ap->prev.br_startoff != NULLFILEOFF &&
3362 !isnullstartblock(ap->prev.br_startblock) &&
3363 (prevbno = ap->prev.br_startblock +
3364 ap->prev.br_blockcount) &&
3365 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 /*
3367 * Calculate gap to end of previous block.
3368 */
Dave Chinner3a756672011-09-18 20:40:58 +00003369 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003370 (ap->prev.br_startoff +
3371 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 /*
3373 * Figure the startblock based on the previous block's
3374 * end and the gap size.
3375 * Heuristic!
3376 * If the gap is large relative to the piece we're
3377 * allocating, or using it gives us an invalid block
3378 * number, then just use the end of the previous block.
3379 */
Dave Chinner3a756672011-09-18 20:40:58 +00003380 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003382 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 prevbno += adjust;
3384 else
3385 prevdiff += adjust;
3386 /*
3387 * If the firstblock forbids it, can't use it,
3388 * must use default.
3389 */
3390 if (!rt && !nullfb &&
3391 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3392 prevbno = NULLFSBLOCK;
3393 }
3394 /*
3395 * No previous block or can't follow it, just default.
3396 */
3397 else
3398 prevbno = NULLFSBLOCK;
3399 /*
3400 * If there's a following (right) block, select a requested
3401 * start block based on it.
3402 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003403 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 /*
3405 * Calculate gap to start of next block.
3406 */
Dave Chinner3a756672011-09-18 20:40:58 +00003407 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 /*
3409 * Figure the startblock based on the next block's
3410 * start and the gap size.
3411 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003412 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 /*
3414 * Heuristic!
3415 * If the gap is large relative to the piece we're
3416 * allocating, or using it gives us an invalid block
3417 * number, then just use the start of the next block
3418 * offset by our length.
3419 */
Dave Chinner3a756672011-09-18 20:40:58 +00003420 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 ISVALID(gotbno - gotdiff, gotbno))
3422 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003423 else if (ISVALID(gotbno - ap->length, gotbno)) {
3424 gotbno -= ap->length;
3425 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 } else
3427 gotdiff += adjust;
3428 /*
3429 * If the firstblock forbids it, can't use it,
3430 * must use default.
3431 */
3432 if (!rt && !nullfb &&
3433 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3434 gotbno = NULLFSBLOCK;
3435 }
3436 /*
3437 * No next block, just default.
3438 */
3439 else
3440 gotbno = NULLFSBLOCK;
3441 /*
3442 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003443 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 */
3445 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003446 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003448 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003450 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003452#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003453}
3454
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003455static int
3456xfs_bmap_longest_free_extent(
3457 struct xfs_trans *tp,
3458 xfs_agnumber_t ag,
3459 xfs_extlen_t *blen,
3460 int *notinit)
3461{
3462 struct xfs_mount *mp = tp->t_mountp;
3463 struct xfs_perag *pag;
3464 xfs_extlen_t longest;
3465 int error = 0;
3466
3467 pag = xfs_perag_get(mp, ag);
3468 if (!pag->pagf_init) {
3469 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3470 if (error)
3471 goto out;
3472
3473 if (!pag->pagf_init) {
3474 *notinit = 1;
3475 goto out;
3476 }
3477 }
3478
Dave Chinner50adbcb2015-06-22 10:04:31 +10003479 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003480 xfs_alloc_min_freelist(mp, pag),
3481 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003482 if (*blen < longest)
3483 *blen = longest;
3484
3485out:
3486 xfs_perag_put(pag);
3487 return error;
3488}
3489
3490static void
3491xfs_bmap_select_minlen(
3492 struct xfs_bmalloca *ap,
3493 struct xfs_alloc_arg *args,
3494 xfs_extlen_t *blen,
3495 int notinit)
3496{
3497 if (notinit || *blen < ap->minlen) {
3498 /*
3499 * Since we did a BUF_TRYLOCK above, it is possible that
3500 * there is space for this request.
3501 */
3502 args->minlen = ap->minlen;
3503 } else if (*blen < args->maxlen) {
3504 /*
3505 * If the best seen length is less than the request length,
3506 * use the best as the minimum.
3507 */
3508 args->minlen = *blen;
3509 } else {
3510 /*
3511 * Otherwise we've seen an extent as big as maxlen, use that
3512 * as the minimum.
3513 */
3514 args->minlen = args->maxlen;
3515 }
3516}
3517
Nathan Scotta365bdd2006-03-14 13:34:16 +11003518STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003519xfs_bmap_btalloc_nullfb(
3520 struct xfs_bmalloca *ap,
3521 struct xfs_alloc_arg *args,
3522 xfs_extlen_t *blen)
3523{
3524 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003525 xfs_agnumber_t ag, startag;
3526 int notinit = 0;
3527 int error;
3528
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003529 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003530 args->total = ap->total;
3531
Christoph Hellwigc467c042010-02-15 23:34:42 +00003532 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3533 if (startag == NULLAGNUMBER)
3534 startag = ag = 0;
3535
Dave Chinner14b064c2011-01-27 12:16:28 +11003536 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003537 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3538 &notinit);
3539 if (error)
3540 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003541
Christoph Hellwigc467c042010-02-15 23:34:42 +00003542 if (++ag == mp->m_sb.sb_agcount)
3543 ag = 0;
3544 if (ag == startag)
3545 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003546 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003547
3548 xfs_bmap_select_minlen(ap, args, blen, notinit);
3549 return 0;
3550}
3551
3552STATIC int
3553xfs_bmap_btalloc_filestreams(
3554 struct xfs_bmalloca *ap,
3555 struct xfs_alloc_arg *args,
3556 xfs_extlen_t *blen)
3557{
3558 struct xfs_mount *mp = ap->ip->i_mount;
3559 xfs_agnumber_t ag;
3560 int notinit = 0;
3561 int error;
3562
3563 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3564 args->total = ap->total;
3565
3566 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3567 if (ag == NULLAGNUMBER)
3568 ag = 0;
3569
3570 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3571 if (error)
3572 return error;
3573
3574 if (*blen < args->maxlen) {
3575 error = xfs_filestream_new_ag(ap, &ag);
3576 if (error)
3577 return error;
3578
3579 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3580 &notinit);
3581 if (error)
3582 return error;
3583
3584 }
3585
3586 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003587
3588 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003589 * Set the failure fallback case to look in the selected AG as stream
3590 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003591 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003592 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003593 return 0;
3594}
3595
3596STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003597xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003598 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003599{
3600 xfs_mount_t *mp; /* mount point structure */
3601 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003602 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003603 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003604 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003605 xfs_alloc_arg_t args;
3606 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003607 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003608 int nullfb; /* true if ap->firstblock isn't set */
3609 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003610 int tryagain;
3611 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003612 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003613
Dave Chinnera99ebf42011-12-01 11:24:20 +00003614 ASSERT(ap->length);
3615
Nathan Scotta365bdd2006-03-14 13:34:16 +11003616 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003617
3618 /* stripe alignment for allocation is determined by mount parameters */
3619 stripe_align = 0;
3620 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3621 stripe_align = mp->m_swidth;
3622 else if (mp->m_dalign)
3623 stripe_align = mp->m_dalign;
3624
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003625 if (ap->flags & XFS_BMAPI_COWFORK)
3626 align = xfs_get_cowextsz_hint(ap->ip);
3627 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003628 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003629 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003630 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003631 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003632 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003633 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003634 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003635 }
Dave Chinner33177f052013-12-12 16:34:36 +11003636
3637
Dave Chinner0937e0f2011-09-18 20:40:57 +00003638 nullfb = *ap->firstblock == NULLFSBLOCK;
3639 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003640 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003641 if (xfs_alloc_is_userdata(ap->datatype) &&
3642 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003643 ag = xfs_filestream_lookup_ag(ap->ip);
3644 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003645 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003646 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003647 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003648 }
3649 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003650 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003651
3652 xfs_bmap_adjacent(ap);
3653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003655 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 * it's in the right allocation group.
3657 */
Dave Chinner3a756672011-09-18 20:40:58 +00003658 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 ;
3660 else
Dave Chinner3a756672011-09-18 20:40:58 +00003661 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663 * Normal allocation, done through xfs_alloc_vextent.
3664 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003665 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003666 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003667 args.tp = ap->tp;
3668 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003669 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003670 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003671
3672 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003673 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003674 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003675 blen = 0;
3676 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003677 /*
3678 * Search for an allocation group with a single extent large
3679 * enough for the request. If one isn't found, then adjust
3680 * the minimum allocation size to the largest space found.
3681 */
Dave Chinner292378e2016-09-26 08:21:28 +10003682 if (xfs_alloc_is_userdata(ap->datatype) &&
3683 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003684 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3685 else
3686 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003687 if (error)
3688 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003689 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003690 if (xfs_inode_is_filestream(ap->ip))
3691 args.type = XFS_ALLOCTYPE_FIRST_AG;
3692 else
3693 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003694 args.total = args.minlen = ap->minlen;
3695 } else {
3696 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3697 args.total = ap->total;
3698 args.minlen = ap->minlen;
3699 }
David Chinner957d0eb2007-06-18 16:50:37 +10003700 /* apply extent size hints if obtained earlier */
3701 if (unlikely(align)) {
3702 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003703 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003704 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003705 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003706 args.prod = 1;
3707 args.mod = 0;
3708 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003709 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003710 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003711 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3712 }
3713 /*
3714 * If we are not low on available data blocks, and the
3715 * underlying logical volume manager is a stripe, and
3716 * the file offset is zero then try to allocate data
3717 * blocks on stripe unit boundary.
3718 * NOTE: ap->aeof is only set if the allocation length
3719 * is >= the stripe unit and the allocation offset is
3720 * at the end of file.
3721 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003722 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003723 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003724 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003725 atype = args.type;
3726 isaligned = 1;
3727 /*
3728 * Adjust for alignment
3729 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003730 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003731 args.minlen = blen - args.alignment;
3732 args.minalignslop = 0;
3733 } else {
3734 /*
3735 * First try an exact bno allocation.
3736 * If it fails then do a near or start bno
3737 * allocation with alignment turned on.
3738 */
3739 atype = args.type;
3740 tryagain = 1;
3741 args.type = XFS_ALLOCTYPE_THIS_BNO;
3742 args.alignment = 1;
3743 /*
3744 * Compute the minlen+alignment for the
3745 * next case. Set slop so that the value
3746 * of minlen+alignment+slop doesn't go up
3747 * between the calls.
3748 */
Dave Chinner33177f052013-12-12 16:34:36 +11003749 if (blen > stripe_align && blen <= args.maxlen)
3750 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003751 else
3752 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003753 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003754 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003755 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003756 args.minlen - 1;
3757 else
3758 args.minalignslop = 0;
3759 }
3760 } else {
3761 args.alignment = 1;
3762 args.minalignslop = 0;
3763 }
3764 args.minleft = ap->minleft;
3765 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003766 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003767 args.datatype = ap->datatype;
3768 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003769 args.ip = ap->ip;
3770
3771 error = xfs_alloc_vextent(&args);
3772 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003773 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003774
Nathan Scotta365bdd2006-03-14 13:34:16 +11003775 if (tryagain && args.fsbno == NULLFSBLOCK) {
3776 /*
3777 * Exact allocation failed. Now try with alignment
3778 * turned on.
3779 */
3780 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003781 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003782 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003783 args.minlen = nextminlen;
3784 args.minalignslop = 0;
3785 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 if ((error = xfs_alloc_vextent(&args)))
3787 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003788 }
3789 if (isaligned && args.fsbno == NULLFSBLOCK) {
3790 /*
3791 * allocation failed, so turn off alignment and
3792 * try again.
3793 */
3794 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003795 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003796 args.alignment = 0;
3797 if ((error = xfs_alloc_vextent(&args)))
3798 return error;
3799 }
3800 if (args.fsbno == NULLFSBLOCK && nullfb &&
3801 args.minlen > ap->minlen) {
3802 args.minlen = ap->minlen;
3803 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003804 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003805 if ((error = xfs_alloc_vextent(&args)))
3806 return error;
3807 }
3808 if (args.fsbno == NULLFSBLOCK && nullfb) {
3809 args.fsbno = 0;
3810 args.type = XFS_ALLOCTYPE_FIRST_AG;
3811 args.total = ap->minlen;
3812 args.minleft = 0;
3813 if ((error = xfs_alloc_vextent(&args)))
3814 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003815 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003816 }
3817 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003818 /*
3819 * check the allocation happened at the same or higher AG than
3820 * the first block that was allocated.
3821 */
3822 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3823 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3824 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003825 (ap->dfops->dop_low &&
Dave Chinner0937e0f2011-09-18 20:40:57 +00003826 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3827 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3828
Dave Chinner3a756672011-09-18 20:40:58 +00003829 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003830 if (*ap->firstblock == NULLFSBLOCK)
3831 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003832 ASSERT(nullfb || fb_agno == args.agno ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003833 (ap->dfops->dop_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003834 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003835 if (!(ap->flags & XFS_BMAPI_COWFORK))
3836 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003837 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3838 if (ap->wasdel)
3839 ap->ip->i_delayed_blks -= args.len;
3840 /*
3841 * Adjust the disk quota also. This was reserved
3842 * earlier.
3843 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003844 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003845 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3846 XFS_TRANS_DQ_BCOUNT,
3847 (long) args.len);
3848 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003849 ap->blkno = NULLFSBLOCK;
3850 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 }
3852 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003853}
3854
3855/*
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003856 * For a remap operation, just "allocate" an extent at the address that the
3857 * caller passed in, and ensure that the AGFL is the right size. The caller
3858 * will then map the "allocated" extent into the file somewhere.
3859 */
3860STATIC int
3861xfs_bmap_remap_alloc(
3862 struct xfs_bmalloca *ap)
3863{
3864 struct xfs_trans *tp = ap->tp;
3865 struct xfs_mount *mp = tp->t_mountp;
3866 xfs_agblock_t bno;
3867 struct xfs_alloc_arg args;
3868 int error;
3869
3870 /*
3871 * validate that the block number is legal - the enables us to detect
3872 * and handle a silent filesystem corruption rather than crashing.
3873 */
3874 memset(&args, 0, sizeof(struct xfs_alloc_arg));
3875 args.tp = ap->tp;
3876 args.mp = ap->tp->t_mountp;
3877 bno = *ap->firstblock;
3878 args.agno = XFS_FSB_TO_AGNO(mp, bno);
3879 args.agbno = XFS_FSB_TO_AGBNO(mp, bno);
3880 if (args.agno >= mp->m_sb.sb_agcount ||
3881 args.agbno >= mp->m_sb.sb_agblocks)
3882 return -EFSCORRUPTED;
3883
3884 /* "Allocate" the extent from the range we passed in. */
3885 trace_xfs_bmap_remap_alloc(ap->ip, *ap->firstblock, ap->length);
3886 ap->blkno = bno;
3887 ap->ip->i_d.di_nblocks += ap->length;
3888 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3889
3890 /* Fix the freelist, like a real allocator does. */
3891 args.datatype = ap->datatype;
3892 args.pag = xfs_perag_get(args.mp, args.agno);
3893 ASSERT(args.pag);
3894
3895 /*
3896 * The freelist fixing code will decline the allocation if
3897 * the size and shape of the free space doesn't allow for
3898 * allocating the extent and updating all the metadata that
3899 * happens during an allocation. We're remapping, not
3900 * allocating, so skip that check by pretending to be freeing.
3901 */
3902 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003903 xfs_perag_put(args.pag);
3904 if (error)
3905 trace_xfs_bmap_remap_alloc_error(ap->ip, error, _RET_IP_);
3906 return error;
3907}
3908
3909/*
Nathan Scotta365bdd2006-03-14 13:34:16 +11003910 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3911 * It figures out where to ask the underlying allocator to put the new extent.
3912 */
3913STATIC int
3914xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003915 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003916{
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003917 if (ap->flags & XFS_BMAPI_REMAP)
3918 return xfs_bmap_remap_alloc(ap);
Dave Chinner292378e2016-09-26 08:21:28 +10003919 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3920 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003921 return xfs_bmap_rtalloc(ap);
3922 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923}
3924
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003925/* Trim extent to fit a logical block range. */
3926void
3927xfs_trim_extent(
3928 struct xfs_bmbt_irec *irec,
3929 xfs_fileoff_t bno,
3930 xfs_filblks_t len)
3931{
3932 xfs_fileoff_t distance;
3933 xfs_fileoff_t end = bno + len;
3934
3935 if (irec->br_startoff + irec->br_blockcount <= bno ||
3936 irec->br_startoff >= end) {
3937 irec->br_blockcount = 0;
3938 return;
3939 }
3940
3941 if (irec->br_startoff < bno) {
3942 distance = bno - irec->br_startoff;
3943 if (isnullstartblock(irec->br_startblock))
3944 irec->br_startblock = DELAYSTARTBLOCK;
3945 if (irec->br_startblock != DELAYSTARTBLOCK &&
3946 irec->br_startblock != HOLESTARTBLOCK)
3947 irec->br_startblock += distance;
3948 irec->br_startoff += distance;
3949 irec->br_blockcount -= distance;
3950 }
3951
3952 if (end < irec->br_startoff + irec->br_blockcount) {
3953 distance = irec->br_startoff + irec->br_blockcount - end;
3954 irec->br_blockcount -= distance;
3955 }
3956}
3957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003959 * Trim the returned map to the required bounds
3960 */
3961STATIC void
3962xfs_bmapi_trim_map(
3963 struct xfs_bmbt_irec *mval,
3964 struct xfs_bmbt_irec *got,
3965 xfs_fileoff_t *bno,
3966 xfs_filblks_t len,
3967 xfs_fileoff_t obno,
3968 xfs_fileoff_t end,
3969 int n,
3970 int flags)
3971{
3972 if ((flags & XFS_BMAPI_ENTIRE) ||
3973 got->br_startoff + got->br_blockcount <= obno) {
3974 *mval = *got;
3975 if (isnullstartblock(got->br_startblock))
3976 mval->br_startblock = DELAYSTARTBLOCK;
3977 return;
3978 }
3979
3980 if (obno > *bno)
3981 *bno = obno;
3982 ASSERT((*bno >= obno) || (n == 0));
3983 ASSERT(*bno < end);
3984 mval->br_startoff = *bno;
3985 if (isnullstartblock(got->br_startblock))
3986 mval->br_startblock = DELAYSTARTBLOCK;
3987 else
3988 mval->br_startblock = got->br_startblock +
3989 (*bno - got->br_startoff);
3990 /*
3991 * Return the minimum of what we got and what we asked for for
3992 * the length. We can use the len variable here because it is
3993 * modified below and we could have been there before coming
3994 * here if the first part of the allocation didn't overlap what
3995 * was asked for.
3996 */
3997 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3998 got->br_blockcount - (*bno - got->br_startoff));
3999 mval->br_state = got->br_state;
4000 ASSERT(mval->br_blockcount <= len);
4001 return;
4002}
4003
4004/*
4005 * Update and validate the extent map to return
4006 */
4007STATIC void
4008xfs_bmapi_update_map(
4009 struct xfs_bmbt_irec **map,
4010 xfs_fileoff_t *bno,
4011 xfs_filblks_t *len,
4012 xfs_fileoff_t obno,
4013 xfs_fileoff_t end,
4014 int *n,
4015 int flags)
4016{
4017 xfs_bmbt_irec_t *mval = *map;
4018
4019 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4020 ((mval->br_startoff + mval->br_blockcount) <= end));
4021 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4022 (mval->br_startoff < obno));
4023
4024 *bno = mval->br_startoff + mval->br_blockcount;
4025 *len = end - *bno;
4026 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4027 /* update previous map with new information */
4028 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4029 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4030 ASSERT(mval->br_state == mval[-1].br_state);
4031 mval[-1].br_blockcount = mval->br_blockcount;
4032 mval[-1].br_state = mval->br_state;
4033 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4034 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4035 mval[-1].br_startblock != HOLESTARTBLOCK &&
4036 mval->br_startblock == mval[-1].br_startblock +
4037 mval[-1].br_blockcount &&
4038 ((flags & XFS_BMAPI_IGSTATE) ||
4039 mval[-1].br_state == mval->br_state)) {
4040 ASSERT(mval->br_startoff ==
4041 mval[-1].br_startoff + mval[-1].br_blockcount);
4042 mval[-1].br_blockcount += mval->br_blockcount;
4043 } else if (*n > 0 &&
4044 mval->br_startblock == DELAYSTARTBLOCK &&
4045 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4046 mval->br_startoff ==
4047 mval[-1].br_startoff + mval[-1].br_blockcount) {
4048 mval[-1].br_blockcount += mval->br_blockcount;
4049 mval[-1].br_state = mval->br_state;
4050 } else if (!((*n == 0) &&
4051 ((mval->br_startoff + mval->br_blockcount) <=
4052 obno))) {
4053 mval++;
4054 (*n)++;
4055 }
4056 *map = mval;
4057}
4058
4059/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004060 * Map file blocks to filesystem blocks without allocation.
4061 */
4062int
4063xfs_bmapi_read(
4064 struct xfs_inode *ip,
4065 xfs_fileoff_t bno,
4066 xfs_filblks_t len,
4067 struct xfs_bmbt_irec *mval,
4068 int *nmap,
4069 int flags)
4070{
4071 struct xfs_mount *mp = ip->i_mount;
4072 struct xfs_ifork *ifp;
4073 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004074 xfs_fileoff_t obno;
4075 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11004076 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004077 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11004078 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004079 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004080 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004081
4082 ASSERT(*nmap >= 1);
4083 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004084 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004085 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004086
4087 if (unlikely(XFS_TEST_ERROR(
4088 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4089 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4090 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4091 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004092 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004093 }
4094
4095 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004096 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004097
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004098 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004099
4100 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004101
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004102 /* No CoW fork? Return a hole. */
4103 if (whichfork == XFS_COW_FORK && !ifp) {
4104 mval->br_startoff = bno;
4105 mval->br_startblock = HOLESTARTBLOCK;
4106 mval->br_blockcount = len;
4107 mval->br_state = XFS_EXT_NORM;
4108 *nmap = 1;
4109 return 0;
4110 }
4111
Dave Chinner5c8ed202011-09-18 20:40:45 +00004112 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4113 error = xfs_iread_extents(NULL, ip, whichfork);
4114 if (error)
4115 return error;
4116 }
4117
Christoph Hellwig334f3422016-11-24 11:39:43 +11004118 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4119 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004120 end = bno + len;
4121 obno = bno;
4122
4123 while (bno < end && n < *nmap) {
4124 /* Reading past eof, act as though there's a hole up to end. */
4125 if (eof)
4126 got.br_startoff = end;
4127 if (got.br_startoff > bno) {
4128 /* Reading in a hole. */
4129 mval->br_startoff = bno;
4130 mval->br_startblock = HOLESTARTBLOCK;
4131 mval->br_blockcount =
4132 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4133 mval->br_state = XFS_EXT_NORM;
4134 bno += mval->br_blockcount;
4135 len -= mval->br_blockcount;
4136 mval++;
4137 n++;
4138 continue;
4139 }
4140
4141 /* set up the extent map to return. */
4142 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4143 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4144
4145 /* If we're done, stop now. */
4146 if (bno >= end || n >= *nmap)
4147 break;
4148
4149 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004150 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4151 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004152 }
4153 *nmap = n;
4154 return 0;
4155}
4156
Christoph Hellwig51446f52016-09-19 11:10:21 +10004157int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004158xfs_bmapi_reserve_delalloc(
4159 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004160 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004161 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004162 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004163 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004164 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004165 xfs_extnum_t *lastx,
4166 int eof)
4167{
4168 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004169 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004170 xfs_extlen_t alen;
4171 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004172 char rt = XFS_IS_REALTIME_INODE(ip);
4173 xfs_extlen_t extsz;
4174 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004175 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004176
Brian Foster974ae922016-11-28 14:57:42 +11004177 /*
4178 * Cap the alloc length. Keep track of prealloc so we know whether to
4179 * tag the inode before we return.
4180 */
4181 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004182 if (!eof)
4183 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004184 if (prealloc && alen >= len)
4185 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004186
4187 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004188 if (whichfork == XFS_COW_FORK)
4189 extsz = xfs_get_cowextsz_hint(ip);
4190 else
4191 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004192 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004193 struct xfs_bmbt_irec prev;
4194
4195 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4196 prev.br_startoff = NULLFILEOFF;
4197
4198 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004199 1, 0, &aoff, &alen);
4200 ASSERT(!error);
4201 }
4202
4203 if (rt)
4204 extsz = alen / mp->m_sb.sb_rextsize;
4205
4206 /*
4207 * Make a transaction-less quota reservation for delayed allocation
4208 * blocks. This number gets adjusted later. We return if we haven't
4209 * allocated blocks already inside this loop.
4210 */
4211 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4212 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4213 if (error)
4214 return error;
4215
4216 /*
4217 * Split changing sb for alen and indlen since they could be coming
4218 * from different places.
4219 */
4220 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4221 ASSERT(indlen > 0);
4222
4223 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004224 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004225 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004226 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004227 }
4228
4229 if (error)
4230 goto out_unreserve_quota;
4231
Dave Chinner0d485ad2015-02-23 21:22:03 +11004232 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004233 if (error)
4234 goto out_unreserve_blocks;
4235
4236
4237 ip->i_delayed_blks += alen;
4238
4239 got->br_startoff = aoff;
4240 got->br_startblock = nullstartblock(indlen);
4241 got->br_blockcount = alen;
4242 got->br_state = XFS_EXT_NORM;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004243 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004244
4245 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004246 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4247 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004248 */
4249 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4250
Brian Foster974ae922016-11-28 14:57:42 +11004251 /*
4252 * Tag the inode if blocks were preallocated. Note that COW fork
4253 * preallocation can occur at the start or end of the extent, even when
4254 * prealloc == 0, so we must also check the aligned offset and length.
4255 */
4256 if (whichfork == XFS_DATA_FORK && prealloc)
4257 xfs_inode_set_eofblocks_tag(ip);
4258 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4259 xfs_inode_set_cowblocks_tag(ip);
4260
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004261 ASSERT(got->br_startoff <= aoff);
4262 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4263 ASSERT(isnullstartblock(got->br_startblock));
4264 ASSERT(got->br_state == XFS_EXT_NORM);
4265 return 0;
4266
4267out_unreserve_blocks:
4268 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004269 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004270 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004271 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004272out_unreserve_quota:
4273 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004274 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004275 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4276 return error;
4277}
4278
Dave Chinnercf11da92014-07-15 07:08:24 +10004279static int
4280xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004281 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004282{
4283 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004284 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004285 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004286 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004287 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004288
Dave Chinnera99ebf42011-12-01 11:24:20 +00004289 ASSERT(bma->length > 0);
4290
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004291 /*
4292 * For the wasdelay case, we could also just allocate the stuff asked
4293 * for in this bmap call but that wouldn't be as good.
4294 */
4295 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004296 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4297 bma->offset = bma->got.br_startoff;
Christoph Hellwig0e8d6302016-11-24 11:40:32 +11004298 if (bma->idx) {
Dave Chinnere0c3da52011-09-18 20:41:01 +00004299 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004300 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004301 }
4302 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004303 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004304 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004305 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004306 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004307 }
4308
4309 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004310 * Set the data type being allocated. For the data fork, the first data
4311 * in the file is treated differently to all other allocations. For the
4312 * attribute fork, we only need to ensure the allocated range is not on
4313 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004314 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004315 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004316 bma->datatype = XFS_ALLOC_NOBUSY;
4317 if (whichfork == XFS_DATA_FORK) {
4318 if (bma->offset == 0)
4319 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4320 else
4321 bma->datatype |= XFS_ALLOC_USERDATA;
4322 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004323 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004324 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004325 }
4326
Dave Chinnere04426b2012-10-05 11:06:59 +10004327 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004328
4329 /*
4330 * Only want to do the alignment at the eof if it is userdata and
4331 * allocation length is larger than a stripe unit.
4332 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004333 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004334 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004335 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004336 if (error)
4337 return error;
4338 }
4339
4340 error = xfs_bmap_alloc(bma);
4341 if (error)
4342 return error;
4343
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004344 if (bma->dfops->dop_low)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004345 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004346 if (bma->cur)
4347 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004348 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004349 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004350 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4351 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4352 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004353 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004354 }
4355 /*
4356 * Bump the number of extents we've allocated
4357 * in this call.
4358 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004359 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004360
Dave Chinner29c8d172011-09-18 20:41:00 +00004361 if (bma->cur)
4362 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004363 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4364
Dave Chinner963c30c2011-09-18 20:40:59 +00004365 bma->got.br_startoff = bma->offset;
4366 bma->got.br_startblock = bma->blkno;
4367 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004368 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004369
4370 /*
4371 * A wasdelay extent has been initialized, so shouldn't be flagged
4372 * as unwritten.
4373 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004374 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004375 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004376 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004377
Christoph Hellwigc6534242011-09-18 20:41:05 +00004378 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004379 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004380 else
4381 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004382
Christoph Hellwigc315c902011-09-18 20:41:02 +00004383 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004384 if (error)
4385 return error;
4386
4387 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004388 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4389 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4390 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004391 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004392 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004393
Dave Chinner963c30c2011-09-18 20:40:59 +00004394 ASSERT(bma->got.br_startoff <= bma->offset);
4395 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4396 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004397 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4398 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004399 return 0;
4400}
4401
Dave Chinnerb447fe52011-09-18 20:40:51 +00004402STATIC int
4403xfs_bmapi_convert_unwritten(
4404 struct xfs_bmalloca *bma,
4405 struct xfs_bmbt_irec *mval,
4406 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004407 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004408{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004409 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004410 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004411 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004412 int error;
4413
Dave Chinnerb447fe52011-09-18 20:40:51 +00004414 /* check if we need to do unwritten->real conversion */
4415 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4416 (flags & XFS_BMAPI_PREALLOC))
4417 return 0;
4418
4419 /* check if we need to do real->unwritten conversion */
4420 if (mval->br_state == XFS_EXT_NORM &&
4421 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4422 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4423 return 0;
4424
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004425 ASSERT(whichfork != XFS_COW_FORK);
4426
Dave Chinnerb447fe52011-09-18 20:40:51 +00004427 /*
4428 * Modify (by adding) the state flag, if writing.
4429 */
4430 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004431 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4432 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004433 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004434 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004435 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004436 }
4437 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4438 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4439
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004440 /*
4441 * Before insertion into the bmbt, zero the range being converted
4442 * if required.
4443 */
4444 if (flags & XFS_BMAPI_ZERO) {
4445 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4446 mval->br_blockcount);
4447 if (error)
4448 return error;
4449 }
4450
Dave Chinnere0c3da52011-09-18 20:41:01 +00004451 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004452 &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004453 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004454 /*
4455 * Log the inode core unconditionally in the unwritten extent conversion
4456 * path because the conversion might not have done so (e.g., if the
4457 * extent count hasn't changed). We need to make sure the inode is dirty
4458 * in the transaction for the sake of fsync(), even if nothing has
4459 * changed, because fsync() will not force the log for this transaction
4460 * unless it sees the inode pinned.
4461 */
4462 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004463 if (error)
4464 return error;
4465
4466 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004467 * Update our extent pointer, given that
4468 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4469 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004470 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004471 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004472
4473 /*
4474 * We may have combined previously unwritten space with written space,
4475 * so generate another request.
4476 */
4477 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004478 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004479 return 0;
4480}
4481
Christoph Hellwig44032802011-09-18 20:40:48 +00004482/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004483 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4484 * extent state if necessary. Details behaviour is controlled by the flags
4485 * parameter. Only allocates blocks from a single allocation group, to avoid
4486 * locking problems.
4487 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 * The returned value in "firstblock" from the first call in a transaction
4489 * must be remembered and presented to subsequent calls in "firstblock".
4490 * An upper bound for the number of blocks to be allocated is supplied to
4491 * the first call in "total"; if no allocation group has that many free
4492 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4493 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004494int
4495xfs_bmapi_write(
4496 struct xfs_trans *tp, /* transaction pointer */
4497 struct xfs_inode *ip, /* incore inode */
4498 xfs_fileoff_t bno, /* starting file offs. mapped */
4499 xfs_filblks_t len, /* length to map in file */
4500 int flags, /* XFS_BMAPI_... */
4501 xfs_fsblock_t *firstblock, /* first allocated block
4502 controls a.g. for allocs */
4503 xfs_extlen_t total, /* total blocks needed */
4504 struct xfs_bmbt_irec *mval, /* output: map values */
4505 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004506 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004508 struct xfs_mount *mp = ip->i_mount;
4509 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004510 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004511 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004512 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004513 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004514 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004515 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004516 int whichfork; /* data or attr fork */
4517 char inhole; /* current location is hole in file */
4518 char wasdelay; /* old extent was delayed */
4519
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004521 xfs_fileoff_t orig_bno; /* original block number value */
4522 int orig_flags; /* original flags arg value */
4523 xfs_filblks_t orig_len; /* original value of len arg */
4524 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4525 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526
4527 orig_bno = bno;
4528 orig_len = len;
4529 orig_flags = flags;
4530 orig_mval = mval;
4531 orig_nmap = *nmap;
4532#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004533 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004534
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004536 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4537 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4538 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004539 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004540 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004541 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004542 ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK);
4543 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP));
4544 ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP));
Darrick J. Wong60b49842016-10-03 09:11:34 -07004545 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || whichfork != XFS_COW_FORK);
4546 ASSERT(!(flags & XFS_BMAPI_CONVERT) || whichfork != XFS_COW_FORK);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004547
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004548 /* zeroing is for currently only for data extents, not metadata */
4549 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4550 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4551 /*
4552 * we can allocate unwritten extents or pre-zero allocated blocks,
4553 * but it makes no sense to do both at once. This would result in
4554 * zeroing the unwritten extent twice, but it still being an
4555 * unwritten extent....
4556 */
4557 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4558 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4559
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 if (unlikely(XFS_TEST_ERROR(
4561 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004562 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004564 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004565 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004567
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004569 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004570
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004572
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004573 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004574
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004575 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004577 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004579 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004580 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004581 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004582 }
4583
4584 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4585 error = xfs_iread_extents(tp, ip, whichfork);
4586 if (error)
4587 goto error0;
4588 }
4589
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 n = 0;
4591 end = bno + len;
4592 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004593
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004594 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4595 eof = true;
4596 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4597 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004598 bma.tp = tp;
4599 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004600 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004601 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004602 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004603 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004604
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004606 inhole = eof || bma.got.br_startoff > bno;
4607 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 /*
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004610 * Make sure we only reflink into a hole.
4611 */
4612 if (flags & XFS_BMAPI_REMAP)
4613 ASSERT(inhole);
Darrick J. Wong60b49842016-10-03 09:11:34 -07004614 if (flags & XFS_BMAPI_COWFORK)
4615 ASSERT(!inhole);
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004616
4617 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 * First, deal with the hole before the allocated space
4619 * that we found, if any.
4620 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004621 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004622 bma.eof = eof;
4623 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4624 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004625 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004626 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004627
Dave Chinnera99ebf42011-12-01 11:24:20 +00004628 /*
4629 * There's a 32/64 bit type mismatch between the
4630 * allocation length request (which can be 64 bits in
4631 * length) and the bma length request, which is
4632 * xfs_extlen_t and therefore 32 bits. Hence we have to
4633 * check for 32-bit overflows and handle them here.
4634 */
4635 if (len > (xfs_filblks_t)MAXEXTLEN)
4636 bma.length = MAXEXTLEN;
4637 else
4638 bma.length = len;
4639
4640 ASSERT(len > 0);
4641 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004642 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 if (error)
4644 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004645 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004646 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004647
4648 /*
4649 * If this is a CoW allocation, record the data in
4650 * the refcount btree for orphan recovery.
4651 */
4652 if (whichfork == XFS_COW_FORK) {
4653 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4654 bma.blkno, bma.length);
4655 if (error)
4656 goto error0;
4657 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004659
Dave Chinneraef9a892011-09-18 20:40:44 +00004660 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004661 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4662 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Dave Chinnerb447fe52011-09-18 20:40:51 +00004664 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004665 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004666 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004667 continue;
4668 if (error)
4669 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670
Dave Chinneraef9a892011-09-18 20:40:44 +00004671 /* update the extent map to return */
4672 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 /*
4675 * If we're done, stop now. Stop when we've allocated
4676 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4677 * the transaction may get too big.
4678 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004679 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004681
4682 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004683 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004684 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4685 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004688
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 /*
4690 * Transform from btree to extents, give it cur.
4691 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004692 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004693 int tmp_logflags = 0;
4694
Dave Chinner29c8d172011-09-18 20:41:00 +00004695 ASSERT(bma.cur);
4696 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004698 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 if (error)
4700 goto error0;
4701 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004702
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004704 XFS_IFORK_NEXTENTS(ip, whichfork) >
4705 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707error0:
4708 /*
4709 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004710 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004712 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004714 bma.logflags &= ~xfs_ilog_fext(whichfork);
4715 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004717 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 /*
4719 * Log whatever the flags say, even if error. Otherwise we might miss
4720 * detecting a case where the data is changed, there's an error,
4721 * and it's not logged so we don't shutdown when we should.
4722 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004723 if (bma.logflags)
4724 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004725
Dave Chinner29c8d172011-09-18 20:41:00 +00004726 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004727 if (!error) {
4728 ASSERT(*firstblock == NULLFSBLOCK ||
4729 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4730 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004731 bma.cur->bc_private.b.firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004732 (dfops->dop_low &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 XFS_FSB_TO_AGNO(mp, *firstblock) <
4734 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004735 bma.cur->bc_private.b.firstblock)));
4736 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004738 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4740 }
4741 if (!error)
4742 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4743 orig_nmap, *nmap);
4744 return error;
4745}
4746
4747/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004748 * When a delalloc extent is split (e.g., due to a hole punch), the original
4749 * indlen reservation must be shared across the two new extents that are left
4750 * behind.
4751 *
4752 * Given the original reservation and the worst case indlen for the two new
4753 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004754 * reservation fairly across the two new extents. If necessary, steal available
4755 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4756 * ores == 1). The number of stolen blocks is returned. The availability and
4757 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004758 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004759static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004760xfs_bmap_split_indlen(
4761 xfs_filblks_t ores, /* original res. */
4762 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004763 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4764 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004765{
4766 xfs_filblks_t len1 = *indlen1;
4767 xfs_filblks_t len2 = *indlen2;
4768 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004769 xfs_filblks_t stolen = 0;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004770
4771 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004772 * Steal as many blocks as we can to try and satisfy the worst case
4773 * indlen for both new extents.
4774 */
4775 while (nres > ores && avail) {
4776 nres--;
4777 avail--;
4778 stolen++;
4779 }
4780
4781 /*
4782 * The only blocks available are those reserved for the original
4783 * extent and what we can steal from the extent being removed.
4784 * If this still isn't enough to satisfy the combined
4785 * requirements for the two new extents, skim blocks off of each
4786 * of the new reservations until they match what is available.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004787 */
4788 while (nres > ores) {
4789 if (len1) {
4790 len1--;
4791 nres--;
4792 }
4793 if (nres == ores)
4794 break;
4795 if (len2) {
4796 len2--;
4797 nres--;
4798 }
4799 }
4800
4801 *indlen1 = len1;
4802 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004803
4804 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004805}
4806
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004807int
4808xfs_bmap_del_extent_delay(
4809 struct xfs_inode *ip,
4810 int whichfork,
4811 xfs_extnum_t *idx,
4812 struct xfs_bmbt_irec *got,
4813 struct xfs_bmbt_irec *del)
4814{
4815 struct xfs_mount *mp = ip->i_mount;
4816 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4817 struct xfs_bmbt_irec new;
4818 int64_t da_old, da_new, da_diff = 0;
4819 xfs_fileoff_t del_endoff, got_endoff;
4820 xfs_filblks_t got_indlen, new_indlen, stolen;
4821 int error = 0, state = 0;
4822 bool isrt;
4823
4824 XFS_STATS_INC(mp, xs_del_exlist);
4825
4826 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4827 del_endoff = del->br_startoff + del->br_blockcount;
4828 got_endoff = got->br_startoff + got->br_blockcount;
4829 da_old = startblockval(got->br_startblock);
4830 da_new = 0;
4831
4832 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004833 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004834 ASSERT(del->br_blockcount > 0);
4835 ASSERT(got->br_startoff <= del->br_startoff);
4836 ASSERT(got_endoff >= del_endoff);
4837
4838 if (isrt) {
4839 int64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4840
4841 do_div(rtexts, mp->m_sb.sb_rextsize);
4842 xfs_mod_frextents(mp, rtexts);
4843 }
4844
4845 /*
4846 * Update the inode delalloc counter now and wait to update the
4847 * sb counters as we might have to borrow some blocks for the
4848 * indirect block accounting.
4849 */
4850 xfs_trans_reserve_quota_nblks(NULL, ip, -((long)del->br_blockcount), 0,
4851 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4852 ip->i_delayed_blks -= del->br_blockcount;
4853
4854 if (whichfork == XFS_COW_FORK)
4855 state |= BMAP_COWFORK;
4856
4857 if (got->br_startoff == del->br_startoff)
4858 state |= BMAP_LEFT_CONTIG;
4859 if (got_endoff == del_endoff)
4860 state |= BMAP_RIGHT_CONTIG;
4861
4862 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4863 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4864 /*
4865 * Matches the whole extent. Delete the entry.
4866 */
4867 xfs_iext_remove(ip, *idx, 1, state);
4868 --*idx;
4869 break;
4870 case BMAP_LEFT_CONTIG:
4871 /*
4872 * Deleting the first part of the extent.
4873 */
4874 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4875 got->br_startoff = del_endoff;
4876 got->br_blockcount -= del->br_blockcount;
4877 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4878 got->br_blockcount), da_old);
4879 got->br_startblock = nullstartblock((int)da_new);
4880 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4881 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4882 break;
4883 case BMAP_RIGHT_CONTIG:
4884 /*
4885 * Deleting the last part of the extent.
4886 */
4887 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4888 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4889 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4890 got->br_blockcount), da_old);
4891 got->br_startblock = nullstartblock((int)da_new);
4892 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4893 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4894 break;
4895 case 0:
4896 /*
4897 * Deleting the middle of the extent.
4898 *
4899 * Distribute the original indlen reservation across the two new
4900 * extents. Steal blocks from the deleted extent if necessary.
4901 * Stealing blocks simply fudges the fdblocks accounting below.
4902 * Warn if either of the new indlen reservations is zero as this
4903 * can lead to delalloc problems.
4904 */
4905 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4906
4907 got->br_blockcount = del->br_startoff - got->br_startoff;
4908 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4909
4910 new.br_blockcount = got_endoff - del_endoff;
4911 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4912
4913 WARN_ON_ONCE(!got_indlen || !new_indlen);
4914 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4915 del->br_blockcount);
4916
4917 got->br_startblock = nullstartblock((int)got_indlen);
4918 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4919 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4920
4921 new.br_startoff = del_endoff;
4922 new.br_state = got->br_state;
4923 new.br_startblock = nullstartblock((int)new_indlen);
4924
4925 ++*idx;
4926 xfs_iext_insert(ip, *idx, 1, &new, state);
4927
4928 da_new = got_indlen + new_indlen - stolen;
4929 del->br_blockcount -= stolen;
4930 break;
4931 }
4932
4933 ASSERT(da_old >= da_new);
4934 da_diff = da_old - da_new;
4935 if (!isrt)
4936 da_diff += del->br_blockcount;
4937 if (da_diff)
4938 xfs_mod_fdblocks(mp, da_diff, false);
4939 return error;
4940}
4941
4942void
4943xfs_bmap_del_extent_cow(
4944 struct xfs_inode *ip,
4945 xfs_extnum_t *idx,
4946 struct xfs_bmbt_irec *got,
4947 struct xfs_bmbt_irec *del)
4948{
4949 struct xfs_mount *mp = ip->i_mount;
4950 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4951 struct xfs_bmbt_irec new;
4952 xfs_fileoff_t del_endoff, got_endoff;
4953 int state = BMAP_COWFORK;
4954
4955 XFS_STATS_INC(mp, xs_del_exlist);
4956
4957 del_endoff = del->br_startoff + del->br_blockcount;
4958 got_endoff = got->br_startoff + got->br_blockcount;
4959
4960 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004961 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004962 ASSERT(del->br_blockcount > 0);
4963 ASSERT(got->br_startoff <= del->br_startoff);
4964 ASSERT(got_endoff >= del_endoff);
4965 ASSERT(!isnullstartblock(got->br_startblock));
4966
4967 if (got->br_startoff == del->br_startoff)
4968 state |= BMAP_LEFT_CONTIG;
4969 if (got_endoff == del_endoff)
4970 state |= BMAP_RIGHT_CONTIG;
4971
4972 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4973 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4974 /*
4975 * Matches the whole extent. Delete the entry.
4976 */
4977 xfs_iext_remove(ip, *idx, 1, state);
4978 --*idx;
4979 break;
4980 case BMAP_LEFT_CONTIG:
4981 /*
4982 * Deleting the first part of the extent.
4983 */
4984 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4985 got->br_startoff = del_endoff;
4986 got->br_blockcount -= del->br_blockcount;
4987 got->br_startblock = del->br_startblock + del->br_blockcount;
4988 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4989 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4990 break;
4991 case BMAP_RIGHT_CONTIG:
4992 /*
4993 * Deleting the last part of the extent.
4994 */
4995 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4996 got->br_blockcount -= del->br_blockcount;
4997 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4998 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4999 break;
5000 case 0:
5001 /*
5002 * Deleting the middle of the extent.
5003 */
5004 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5005 got->br_blockcount = del->br_startoff - got->br_startoff;
5006 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5007 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5008
5009 new.br_startoff = del_endoff;
5010 new.br_blockcount = got_endoff - del_endoff;
5011 new.br_state = got->br_state;
5012 new.br_startblock = del->br_startblock + del->br_blockcount;
5013
5014 ++*idx;
5015 xfs_iext_insert(ip, *idx, 1, &new, state);
5016 break;
5017 }
5018}
5019
Brian Fostera9bd24a2016-03-15 11:42:46 +11005020/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005021 * Called by xfs_bmapi to update file extent records and the btree
5022 * after removing space (or undoing a delayed allocation).
5023 */
5024STATIC int /* error */
5025xfs_bmap_del_extent(
5026 xfs_inode_t *ip, /* incore inode pointer */
5027 xfs_trans_t *tp, /* current transaction pointer */
5028 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005029 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005030 xfs_btree_cur_t *cur, /* if null, not a btree */
5031 xfs_bmbt_irec_t *del, /* data to remove from extents */
5032 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005033 int whichfork, /* data or attr fork */
5034 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005035{
5036 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5037 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5038 xfs_fsblock_t del_endblock=0; /* first block past del */
5039 xfs_fileoff_t del_endoff; /* first offset past del */
5040 int delay; /* current block is delayed allocated */
5041 int do_fx; /* free extent at end of routine */
5042 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5043 int error; /* error return value */
5044 int flags; /* inode logging flags */
5045 xfs_bmbt_irec_t got; /* current extent entry */
5046 xfs_fileoff_t got_endoff; /* first offset past got */
5047 int i; /* temp state */
5048 xfs_ifork_t *ifp; /* inode fork pointer */
5049 xfs_mount_t *mp; /* mount structure */
5050 xfs_filblks_t nblks; /* quota/sb block count */
5051 xfs_bmbt_irec_t new; /* new record to be inserted */
5052 /* REFERENCED */
5053 uint qfield; /* quota field to update */
5054 xfs_filblks_t temp; /* for indirect length calculations */
5055 xfs_filblks_t temp2; /* for indirect length calculations */
5056 int state = 0;
5057
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005058 mp = ip->i_mount;
5059 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005060
5061 if (whichfork == XFS_ATTR_FORK)
5062 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005063 else if (whichfork == XFS_COW_FORK)
5064 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005065
Dave Chinner9e5987a2013-02-25 12:31:26 +11005066 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005067 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005068 ASSERT(del->br_blockcount > 0);
5069 ep = xfs_iext_get_ext(ifp, *idx);
5070 xfs_bmbt_get_all(ep, &got);
5071 ASSERT(got.br_startoff <= del->br_startoff);
5072 del_endoff = del->br_startoff + del->br_blockcount;
5073 got_endoff = got.br_startoff + got.br_blockcount;
5074 ASSERT(got_endoff >= del_endoff);
5075 delay = isnullstartblock(got.br_startblock);
5076 ASSERT(isnullstartblock(del->br_startblock) == delay);
5077 flags = 0;
5078 qfield = 0;
5079 error = 0;
5080 /*
5081 * If deleting a real allocation, must free up the disk space.
5082 */
5083 if (!delay) {
5084 flags = XFS_ILOG_CORE;
5085 /*
5086 * Realtime allocation. Free it and record di_nblocks update.
5087 */
5088 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5089 xfs_fsblock_t bno;
5090 xfs_filblks_t len;
5091
5092 ASSERT(do_mod(del->br_blockcount,
5093 mp->m_sb.sb_rextsize) == 0);
5094 ASSERT(do_mod(del->br_startblock,
5095 mp->m_sb.sb_rextsize) == 0);
5096 bno = del->br_startblock;
5097 len = del->br_blockcount;
5098 do_div(bno, mp->m_sb.sb_rextsize);
5099 do_div(len, mp->m_sb.sb_rextsize);
5100 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5101 if (error)
5102 goto done;
5103 do_fx = 0;
5104 nblks = len * mp->m_sb.sb_rextsize;
5105 qfield = XFS_TRANS_DQ_RTBCOUNT;
5106 }
5107 /*
5108 * Ordinary allocation.
5109 */
5110 else {
5111 do_fx = 1;
5112 nblks = del->br_blockcount;
5113 qfield = XFS_TRANS_DQ_BCOUNT;
5114 }
5115 /*
5116 * Set up del_endblock and cur for later.
5117 */
5118 del_endblock = del->br_startblock + del->br_blockcount;
5119 if (cur) {
5120 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5121 got.br_startblock, got.br_blockcount,
5122 &i)))
5123 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005124 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005125 }
5126 da_old = da_new = 0;
5127 } else {
5128 da_old = startblockval(got.br_startblock);
5129 da_new = 0;
5130 nblks = 0;
5131 do_fx = 0;
5132 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005133
Dave Chinner9e5987a2013-02-25 12:31:26 +11005134 /*
5135 * Set flag value to use in switch statement.
5136 * Left-contig is 2, right-contig is 1.
5137 */
5138 switch (((got.br_startoff == del->br_startoff) << 1) |
5139 (got_endoff == del_endoff)) {
5140 case 3:
5141 /*
5142 * Matches the whole extent. Delete the entry.
5143 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005144 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005145 xfs_iext_remove(ip, *idx, 1,
5146 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5147 --*idx;
5148 if (delay)
5149 break;
5150
5151 XFS_IFORK_NEXT_SET(ip, whichfork,
5152 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5153 flags |= XFS_ILOG_CORE;
5154 if (!cur) {
5155 flags |= xfs_ilog_fext(whichfork);
5156 break;
5157 }
5158 if ((error = xfs_btree_delete(cur, &i)))
5159 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005160 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005161 break;
5162
5163 case 2:
5164 /*
5165 * Deleting the first part of the extent.
5166 */
5167 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5168 xfs_bmbt_set_startoff(ep, del_endoff);
5169 temp = got.br_blockcount - del->br_blockcount;
5170 xfs_bmbt_set_blockcount(ep, temp);
5171 if (delay) {
5172 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5173 da_old);
5174 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5175 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5176 da_new = temp;
5177 break;
5178 }
5179 xfs_bmbt_set_startblock(ep, del_endblock);
5180 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5181 if (!cur) {
5182 flags |= xfs_ilog_fext(whichfork);
5183 break;
5184 }
5185 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5186 got.br_blockcount - del->br_blockcount,
5187 got.br_state)))
5188 goto done;
5189 break;
5190
5191 case 1:
5192 /*
5193 * Deleting the last part of the extent.
5194 */
5195 temp = got.br_blockcount - del->br_blockcount;
5196 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5197 xfs_bmbt_set_blockcount(ep, temp);
5198 if (delay) {
5199 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5200 da_old);
5201 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5202 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5203 da_new = temp;
5204 break;
5205 }
5206 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5207 if (!cur) {
5208 flags |= xfs_ilog_fext(whichfork);
5209 break;
5210 }
5211 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5212 got.br_startblock,
5213 got.br_blockcount - del->br_blockcount,
5214 got.br_state)))
5215 goto done;
5216 break;
5217
5218 case 0:
5219 /*
5220 * Deleting the middle of the extent.
5221 */
5222 temp = del->br_startoff - got.br_startoff;
5223 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5224 xfs_bmbt_set_blockcount(ep, temp);
5225 new.br_startoff = del_endoff;
5226 temp2 = got_endoff - del_endoff;
5227 new.br_blockcount = temp2;
5228 new.br_state = got.br_state;
5229 if (!delay) {
5230 new.br_startblock = del_endblock;
5231 flags |= XFS_ILOG_CORE;
5232 if (cur) {
5233 if ((error = xfs_bmbt_update(cur,
5234 got.br_startoff,
5235 got.br_startblock, temp,
5236 got.br_state)))
5237 goto done;
5238 if ((error = xfs_btree_increment(cur, 0, &i)))
5239 goto done;
5240 cur->bc_rec.b = new;
5241 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10005242 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005243 goto done;
5244 /*
5245 * If get no-space back from btree insert,
5246 * it tried a split, and we have a zero
5247 * block reservation.
5248 * Fix up our state and return the error.
5249 */
Dave Chinner24513372014-06-25 14:58:08 +10005250 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11005251 /*
5252 * Reset the cursor, don't trust
5253 * it after any insert operation.
5254 */
5255 if ((error = xfs_bmbt_lookup_eq(cur,
5256 got.br_startoff,
5257 got.br_startblock,
5258 temp, &i)))
5259 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005260 XFS_WANT_CORRUPTED_GOTO(mp,
5261 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005262 /*
5263 * Update the btree record back
5264 * to the original value.
5265 */
5266 if ((error = xfs_bmbt_update(cur,
5267 got.br_startoff,
5268 got.br_startblock,
5269 got.br_blockcount,
5270 got.br_state)))
5271 goto done;
5272 /*
5273 * Reset the extent record back
5274 * to the original value.
5275 */
5276 xfs_bmbt_set_blockcount(ep,
5277 got.br_blockcount);
5278 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005279 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005280 goto done;
5281 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005282 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005283 } else
5284 flags |= xfs_ilog_fext(whichfork);
5285 XFS_IFORK_NEXT_SET(ip, whichfork,
5286 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5287 } else {
Brian Fosterd34999c92016-03-15 11:42:47 +11005288 xfs_filblks_t stolen;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005289 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005290
5291 /*
5292 * Distribute the original indlen reservation across the
Brian Fosterd34999c92016-03-15 11:42:47 +11005293 * two new extents. Steal blocks from the deleted extent
5294 * if necessary. Stealing blocks simply fudges the
5295 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005296 */
5297 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5298 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c92016-03-15 11:42:47 +11005299 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5300 del->br_blockcount);
5301 da_new = temp + temp2 - stolen;
5302 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005303
5304 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11005305 * Set the reservation for each extent. Warn if either
5306 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005307 */
Brian Fosterd34999c92016-03-15 11:42:47 +11005308 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005309 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005310 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005311 }
5312 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5313 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5314 ++*idx;
5315 break;
5316 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005317
5318 /* remove reverse mapping */
5319 if (!delay) {
5320 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5321 if (error)
5322 goto done;
5323 }
5324
Dave Chinner9e5987a2013-02-25 12:31:26 +11005325 /*
5326 * If we need to, add to list of extents to delete.
5327 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005328 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005329 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5330 error = xfs_refcount_decrease_extent(mp, dfops, del);
5331 if (error)
5332 goto done;
5333 } else
5334 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5335 del->br_blockcount, NULL);
5336 }
5337
Dave Chinner9e5987a2013-02-25 12:31:26 +11005338 /*
5339 * Adjust inode # blocks in the file.
5340 */
5341 if (nblks)
5342 ip->i_d.di_nblocks -= nblks;
5343 /*
5344 * Adjust quota data.
5345 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005346 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005347 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5348
5349 /*
5350 * Account for change in delayed indirect blocks.
5351 * Nothing to do for disk quota accounting here.
5352 */
5353 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005354 if (da_old > da_new)
5355 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005356done:
5357 *logflagsp = flags;
5358 return error;
5359}
5360
5361/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 * Unmap (remove) blocks from a file.
5363 * If nexts is nonzero then the number of extents to remove is limited to
5364 * that value. If not all extents in the block range can be removed then
5365 * *done is set.
5366 */
5367int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005368__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 xfs_trans_t *tp, /* transaction pointer */
5370 struct xfs_inode *ip, /* incore inode */
5371 xfs_fileoff_t bno, /* starting offset to unmap */
Darrick J. Wong44535932016-10-03 09:11:29 -07005372 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 int flags, /* misc flags */
5374 xfs_extnum_t nexts, /* number of extents max */
5375 xfs_fsblock_t *firstblock, /* first allocated block
5376 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005377 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378{
5379 xfs_btree_cur_t *cur; /* bmap btree cursor */
5380 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 int error; /* error return value */
5382 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005383 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 xfs_ifork_t *ifp; /* inode fork pointer */
5385 int isrt; /* freeing in rt area */
5386 xfs_extnum_t lastx; /* last extent index used */
5387 int logflags; /* transaction logging flags */
5388 xfs_extlen_t mod; /* rt extent offset */
5389 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 xfs_fileoff_t start; /* first file offset deleted */
5391 int tmp_logflags; /* partial logging flags */
5392 int wasdel; /* was a delayed alloc extent */
5393 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005395 xfs_filblks_t len = *rlen; /* length to unmap in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005396
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005397 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5398
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005399 whichfork = xfs_bmapi_whichfork(flags);
5400 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 ifp = XFS_IFORK_PTR(ip, whichfork);
5402 if (unlikely(
5403 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5404 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5405 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5406 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005407 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 }
5409 mp = ip->i_mount;
5410 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005411 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005412
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005413 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 ASSERT(len > 0);
5415 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005416
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5418 (error = xfs_iread_extents(tp, ip, whichfork)))
5419 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005420 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005421 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 return 0;
5423 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005424 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005425 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 start = bno;
5427 bno = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005428
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 /*
5430 * Check to see if the given block number is past the end of the
5431 * file, back up to the last block if so...
5432 */
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005433 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5434 ASSERT(lastx > 0);
5435 xfs_iext_get_extent(ifp, --lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 bno = got.br_startoff + got.br_blockcount - 1;
5437 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005438
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 logflags = 0;
5440 if (ifp->if_flags & XFS_IFBROOT) {
5441 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005442 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005444 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 cur->bc_private.b.flags = 0;
5446 } else
5447 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005448
5449 if (isrt) {
5450 /*
5451 * Synchronize by locking the bitmap inode.
5452 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005453 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005454 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005455 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5456 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005457 }
5458
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 extno = 0;
5460 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5461 (nexts == 0 || extno < nexts)) {
5462 /*
5463 * Is the found extent after a hole in which bno lives?
5464 * Just back up to the previous extent, if so.
5465 */
5466 if (got.br_startoff > bno) {
5467 if (--lastx < 0)
5468 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005469 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 }
5471 /*
5472 * Is the last block of this extent before the range
5473 * we're supposed to delete? If so, we're done.
5474 */
5475 bno = XFS_FILEOFF_MIN(bno,
5476 got.br_startoff + got.br_blockcount - 1);
5477 if (bno < start)
5478 break;
5479 /*
5480 * Then deal with the (possibly delayed) allocated space
5481 * we found.
5482 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005484 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 if (got.br_startoff < start) {
5486 del.br_startoff = start;
5487 del.br_blockcount -= start - got.br_startoff;
5488 if (!wasdel)
5489 del.br_startblock += start - got.br_startoff;
5490 }
5491 if (del.br_startoff + del.br_blockcount > bno + 1)
5492 del.br_blockcount = bno + 1 - del.br_startoff;
5493 sum = del.br_startblock + del.br_blockcount;
5494 if (isrt &&
5495 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5496 /*
5497 * Realtime extent not lined up at the end.
5498 * The extent could have been split into written
5499 * and unwritten pieces, or we could just be
5500 * unmapping part of it. But we can't really
5501 * get rid of part of a realtime extent.
5502 */
5503 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005504 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 /*
5506 * This piece is unwritten, or we're not
5507 * using unwritten extents. Skip over it.
5508 */
5509 ASSERT(bno >= mod);
5510 bno -= mod > del.br_blockcount ?
5511 del.br_blockcount : mod;
5512 if (bno < got.br_startoff) {
5513 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005514 xfs_bmbt_get_all(xfs_iext_get_ext(
5515 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 }
5517 continue;
5518 }
5519 /*
5520 * It's written, turn it unwritten.
5521 * This is better than zeroing it.
5522 */
5523 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005524 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 /*
5526 * If this spans a realtime extent boundary,
5527 * chop it back to the start of the one we end at.
5528 */
5529 if (del.br_blockcount > mod) {
5530 del.br_startoff += del.br_blockcount - mod;
5531 del.br_startblock += del.br_blockcount - mod;
5532 del.br_blockcount = mod;
5533 }
5534 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005535 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005536 &lastx, &cur, &del, firstblock, dfops,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005537 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 if (error)
5539 goto error0;
5540 goto nodelete;
5541 }
5542 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5543 /*
5544 * Realtime extent is lined up at the end but not
5545 * at the front. We'll get rid of full extents if
5546 * we can.
5547 */
5548 mod = mp->m_sb.sb_rextsize - mod;
5549 if (del.br_blockcount > mod) {
5550 del.br_blockcount -= mod;
5551 del.br_startoff += mod;
5552 del.br_startblock += mod;
5553 } else if ((del.br_startoff == start &&
5554 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005555 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005556 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 /*
5558 * Can't make it unwritten. There isn't
5559 * a full extent here so just skip it.
5560 */
5561 ASSERT(bno >= del.br_blockcount);
5562 bno -= del.br_blockcount;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005563 if (got.br_startoff > bno && --lastx >= 0)
5564 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 continue;
5566 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005567 struct xfs_bmbt_irec prev;
5568
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 /*
5570 * This one is already unwritten.
5571 * It must have a written left neighbor.
5572 * Unwrite the killed part of that one and
5573 * try again.
5574 */
5575 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005576 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005578 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 ASSERT(del.br_startblock ==
5580 prev.br_startblock + prev.br_blockcount);
5581 if (prev.br_startoff < start) {
5582 mod = start - prev.br_startoff;
5583 prev.br_blockcount -= mod;
5584 prev.br_startblock += mod;
5585 prev.br_startoff = start;
5586 }
5587 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005588 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005589 error = xfs_bmap_add_extent_unwritten_real(tp,
5590 ip, &lastx, &cur, &prev,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005591 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 if (error)
5593 goto error0;
5594 goto nodelete;
5595 } else {
5596 ASSERT(del.br_state == XFS_EXT_NORM);
5597 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005598 error = xfs_bmap_add_extent_unwritten_real(tp,
5599 ip, &lastx, &cur, &del,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005600 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 if (error)
5602 goto error0;
5603 goto nodelete;
5604 }
5605 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005606
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 /*
5608 * If it's the case where the directory code is running
5609 * with no block reservation, and the deleted block is in
5610 * the middle of its extent, and the resulting insert
5611 * of an extent would cause transformation to btree format,
5612 * then reject it. The calling code will then swap
5613 * blocks around instead.
5614 * We have to do this now, rather than waiting for the
5615 * conversion to btree format, since the transaction
5616 * will be dirty.
5617 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005618 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005620 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5621 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 del.br_startoff > got.br_startoff &&
5623 del.br_startoff + del.br_blockcount <
5624 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005625 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 goto error0;
5627 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005628
5629 /*
5630 * Unreserve quota and update realtime free space, if
5631 * appropriate. If delayed allocation, update the inode delalloc
5632 * counter now and wait to update the sb counters as
5633 * xfs_bmap_del_extent() might need to borrow some blocks.
5634 */
5635 if (wasdel) {
5636 ASSERT(startblockval(del.br_startblock) > 0);
5637 if (isrt) {
5638 xfs_filblks_t rtexts;
5639
5640 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5641 do_div(rtexts, mp->m_sb.sb_rextsize);
5642 xfs_mod_frextents(mp, (int64_t)rtexts);
5643 (void)xfs_trans_reserve_quota_nblks(NULL,
5644 ip, -((long)del.br_blockcount), 0,
5645 XFS_QMOPT_RES_RTBLKS);
5646 } else {
5647 (void)xfs_trans_reserve_quota_nblks(NULL,
5648 ip, -((long)del.br_blockcount), 0,
5649 XFS_QMOPT_RES_REGBLKS);
5650 }
5651 ip->i_delayed_blks -= del.br_blockcount;
5652 if (cur)
5653 cur->bc_private.b.flags |=
5654 XFS_BTCUR_BPRV_WASDEL;
5655 } else if (cur)
5656 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5657
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005658 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005659 &tmp_logflags, whichfork, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 logflags |= tmp_logflags;
5661 if (error)
5662 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005663
5664 if (!isrt && wasdel)
5665 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5666
Linus Torvalds1da177e2005-04-16 15:20:36 -07005667 bno = del.br_startoff - 1;
5668nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 /*
5670 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005673 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005674 xfs_iext_get_extent(ifp, lastx, &got);
5675 if (got.br_startoff > bno && --lastx >= 0)
5676 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 extno++;
5679 }
5680 }
Darrick J. Wong44535932016-10-03 09:11:29 -07005681 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5682 *rlen = 0;
5683 else
5684 *rlen = bno - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005685
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 /*
5687 * Convert to a btree if necessary.
5688 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005689 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005691 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692 &cur, 0, &tmp_logflags, whichfork);
5693 logflags |= tmp_logflags;
5694 if (error)
5695 goto error0;
5696 }
5697 /*
5698 * transform from btree to extents, give it cur
5699 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005700 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005701 ASSERT(cur != NULL);
5702 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5703 whichfork);
5704 logflags |= tmp_logflags;
5705 if (error)
5706 goto error0;
5707 }
5708 /*
5709 * transform from extents to local?
5710 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 error = 0;
5712error0:
5713 /*
5714 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005715 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005717 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005719 logflags &= ~xfs_ilog_fext(whichfork);
5720 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005722 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 /*
5724 * Log inode even in the error case, if the transaction
5725 * is dirty we'll need to shut down the filesystem.
5726 */
5727 if (logflags)
5728 xfs_trans_log_inode(tp, ip, logflags);
5729 if (cur) {
5730 if (!error) {
5731 *firstblock = cur->bc_private.b.firstblock;
5732 cur->bc_private.b.allocated = 0;
5733 }
5734 xfs_btree_del_cursor(cur,
5735 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5736 }
5737 return error;
5738}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005739
Darrick J. Wong44535932016-10-03 09:11:29 -07005740/* Unmap a range of a file. */
5741int
5742xfs_bunmapi(
5743 xfs_trans_t *tp,
5744 struct xfs_inode *ip,
5745 xfs_fileoff_t bno,
5746 xfs_filblks_t len,
5747 int flags,
5748 xfs_extnum_t nexts,
5749 xfs_fsblock_t *firstblock,
5750 struct xfs_defer_ops *dfops,
5751 int *done)
5752{
5753 int error;
5754
5755 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5756 dfops);
5757 *done = (len == 0);
5758 return error;
5759}
5760
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005761/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005762 * Determine whether an extent shift can be accomplished by a merge with the
5763 * extent that precedes the target hole of the shift.
5764 */
5765STATIC bool
5766xfs_bmse_can_merge(
5767 struct xfs_bmbt_irec *left, /* preceding extent */
5768 struct xfs_bmbt_irec *got, /* current extent to shift */
5769 xfs_fileoff_t shift) /* shift fsb */
5770{
5771 xfs_fileoff_t startoff;
5772
5773 startoff = got->br_startoff - shift;
5774
5775 /*
5776 * The extent, once shifted, must be adjacent in-file and on-disk with
5777 * the preceding extent.
5778 */
5779 if ((left->br_startoff + left->br_blockcount != startoff) ||
5780 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5781 (left->br_state != got->br_state) ||
5782 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5783 return false;
5784
5785 return true;
5786}
5787
5788/*
5789 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5790 * hole in the file. If an extent shift would result in the extent being fully
5791 * adjacent to the extent that currently precedes the hole, we can merge with
5792 * the preceding extent rather than do the shift.
5793 *
5794 * This function assumes the caller has verified a shift-by-merge is possible
5795 * with the provided extents via xfs_bmse_can_merge().
5796 */
5797STATIC int
5798xfs_bmse_merge(
5799 struct xfs_inode *ip,
5800 int whichfork,
5801 xfs_fileoff_t shift, /* shift fsb */
5802 int current_ext, /* idx of gotp */
5803 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5804 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5805 struct xfs_btree_cur *cur,
5806 int *logflags) /* output */
5807{
Brian Fosterddb19e32014-09-23 15:38:09 +10005808 struct xfs_bmbt_irec got;
5809 struct xfs_bmbt_irec left;
5810 xfs_filblks_t blockcount;
5811 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005812 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005813
Brian Fosterddb19e32014-09-23 15:38:09 +10005814 xfs_bmbt_get_all(gotp, &got);
5815 xfs_bmbt_get_all(leftp, &left);
5816 blockcount = left.br_blockcount + got.br_blockcount;
5817
5818 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5819 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5820 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5821
5822 /*
5823 * Merge the in-core extents. Note that the host record pointers and
5824 * current_ext index are invalid once the extent has been removed via
5825 * xfs_iext_remove().
5826 */
5827 xfs_bmbt_set_blockcount(leftp, blockcount);
5828 xfs_iext_remove(ip, current_ext, 1, 0);
5829
5830 /*
5831 * Update the on-disk extent count, the btree if necessary and log the
5832 * inode.
5833 */
5834 XFS_IFORK_NEXT_SET(ip, whichfork,
5835 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5836 *logflags |= XFS_ILOG_CORE;
5837 if (!cur) {
5838 *logflags |= XFS_ILOG_DEXT;
5839 return 0;
5840 }
5841
5842 /* lookup and remove the extent to merge */
5843 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5844 got.br_blockcount, &i);
5845 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005846 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005847 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005848
5849 error = xfs_btree_delete(cur, &i);
5850 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005851 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005852 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005853
5854 /* lookup and update size of the previous extent */
5855 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5856 left.br_blockcount, &i);
5857 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005858 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005859 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005860
5861 left.br_blockcount = blockcount;
5862
Dave Chinner4db431f2014-12-04 09:42:40 +11005863 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5864 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005865}
5866
5867/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005868 * Shift a single extent.
5869 */
5870STATIC int
5871xfs_bmse_shift_one(
5872 struct xfs_inode *ip,
5873 int whichfork,
5874 xfs_fileoff_t offset_shift_fsb,
5875 int *current_ext,
5876 struct xfs_bmbt_rec_host *gotp,
5877 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005878 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005879 enum shift_direction direction,
5880 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005881{
5882 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005883 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005884 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005885 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005886 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005887 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005888 int error;
5889 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005890 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005891
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005892 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005893 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005894 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005895
5896 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005897
Brian Fosterf71721d2014-09-23 15:39:05 +10005898 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005899 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005900
Namjae Jeona904b1c2015-03-25 15:08:56 +11005901 if (direction == SHIFT_LEFT) {
5902 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005903
Namjae Jeona904b1c2015-03-25 15:08:56 +11005904 /*
5905 * Check for merge if we've got an extent to the left,
5906 * otherwise make sure there's enough room at the start
5907 * of the file for the shift.
5908 */
5909 if (!*current_ext) {
5910 if (got.br_startoff < offset_shift_fsb)
5911 return -EINVAL;
5912 goto update_current_ext;
5913 }
5914 /*
5915 * grab the left extent and check for a large
5916 * enough hole.
5917 */
5918 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5919 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5920
5921 if (startoff <
5922 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005923 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005924
Dave Chinnerb11bd672014-12-04 09:42:24 +11005925 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005926 if (xfs_bmse_can_merge(&adj_irec, &got,
5927 offset_shift_fsb)) {
Darrick J. Wong9c194642016-08-03 12:16:05 +10005928 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5929 *current_ext, gotp, adj_irecp,
5930 cur, logflags);
5931 if (error)
5932 return error;
5933 adj_irec = got;
5934 goto update_rmap;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005935 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005936 } else {
5937 startoff = got.br_startoff + offset_shift_fsb;
5938 /* nothing to move if this is the last extent */
5939 if (*current_ext >= (total_extents - 1))
5940 goto update_current_ext;
5941 /*
5942 * If this is not the last extent in the file, make sure there
5943 * is enough room between current extent and next extent for
5944 * accommodating the shift.
5945 */
5946 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5947 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5948 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5949 return -EINVAL;
5950 /*
5951 * Unlike a left shift (which involves a hole punch),
5952 * a right shift does not modify extent neighbors
5953 * in any way. We should never find mergeable extents
5954 * in this scenario. Check anyways and warn if we
5955 * encounter two extents that could be one.
5956 */
5957 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5958 WARN_ON_ONCE(1);
5959 }
Brian Fostera979bdf2014-09-23 15:39:04 +10005960 /*
5961 * Increment the extent index for the next iteration, update the start
5962 * offset of the in-core extent and update the btree if applicable.
5963 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005964update_current_ext:
5965 if (direction == SHIFT_LEFT)
5966 (*current_ext)++;
5967 else
5968 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005969 xfs_bmbt_set_startoff(gotp, startoff);
5970 *logflags |= XFS_ILOG_CORE;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005971 adj_irec = got;
Brian Fostera979bdf2014-09-23 15:39:04 +10005972 if (!cur) {
5973 *logflags |= XFS_ILOG_DEXT;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005974 goto update_rmap;
Brian Fostera979bdf2014-09-23 15:39:04 +10005975 }
5976
5977 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5978 got.br_blockcount, &i);
5979 if (error)
5980 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005981 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10005982
5983 got.br_startoff = startoff;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005984 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5985 got.br_blockcount, got.br_state);
5986 if (error)
5987 return error;
5988
5989update_rmap:
5990 /* update reverse mapping */
5991 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
5992 if (error)
5993 return error;
5994 adj_irec.br_startoff = startoff;
5995 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
Brian Fostera979bdf2014-09-23 15:39:04 +10005996}
5997
5998/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005999 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006000 *
Brian Foster2c845f52014-09-23 15:37:09 +10006001 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11006002 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10006003 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6004 * is the length by which each extent is shifted. If there is no hole to shift
6005 * the extents into, this will be considered invalid operation and we abort
6006 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006007 */
6008int
6009xfs_bmap_shift_extents(
6010 struct xfs_trans *tp,
6011 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006012 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006013 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10006014 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006015 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006016 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006017 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006018 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006019 int num_exts)
6020{
Brian Fosterca446d82014-09-02 12:12:53 +10006021 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006022 struct xfs_bmbt_rec_host *gotp;
6023 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006024 struct xfs_mount *mp = ip->i_mount;
6025 struct xfs_ifork *ifp;
6026 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10006027 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006028 xfs_extnum_t total_extents;
6029 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006030 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006031 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10006032 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006033
6034 if (unlikely(XFS_TEST_ERROR(
6035 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6036 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6037 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6038 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6039 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10006040 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006041 }
6042
6043 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10006044 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006045
Brian Foster2c845f52014-09-23 15:37:09 +10006046 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6047 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006048 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6049 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006050
6051 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006052 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6053 /* Read in all the extents */
6054 error = xfs_iread_extents(tp, ip, whichfork);
6055 if (error)
6056 return error;
6057 }
6058
Brian Fosterddb19e32014-09-23 15:38:09 +10006059 if (ifp->if_flags & XFS_IFBROOT) {
6060 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6061 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006062 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006063 cur->bc_private.b.flags = 0;
6064 }
6065
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006066 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006067 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006068 * are collapsing out, so we cannot use the count of real extents here.
6069 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006070 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006071 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006072 if (total_extents == 0) {
6073 *done = 1;
6074 goto del_cursor;
6075 }
6076
6077 /*
6078 * In case of first right shift, we need to initialize next_fsb
6079 */
6080 if (*next_fsb == NULLFSBLOCK) {
6081 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6082 xfs_bmbt_get_all(gotp, &got);
6083 *next_fsb = got.br_startoff;
6084 if (stop_fsb > *next_fsb) {
6085 *done = 1;
6086 goto del_cursor;
6087 }
6088 }
6089
6090 /* Lookup the extent index at which we have to stop */
6091 if (direction == SHIFT_RIGHT) {
6092 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6093 /* Make stop_extent exclusive of shift range */
6094 stop_extent--;
6095 } else
6096 stop_extent = total_extents;
6097
6098 /*
6099 * Look up the extent index for the fsb where we start shifting. We can
6100 * henceforth iterate with current_ext as extent list changes are locked
6101 * out via ilock.
6102 *
6103 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6104 * *next_fsb lies in a hole beyond which there are no extents. Either
6105 * way, we are done.
6106 */
6107 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
6108 if (!gotp) {
6109 *done = 1;
6110 goto del_cursor;
6111 }
6112
6113 /* some sanity checking before we finally start shifting extents */
6114 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6115 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6116 error = -EIO;
6117 goto del_cursor;
6118 }
6119
6120 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006121 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006122 &current_ext, gotp, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006123 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006124 if (error)
6125 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006126 /*
6127 * If there was an extent merge during the shift, the extent
6128 * count can change. Update the total and grade the next record.
6129 */
6130 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006131 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006132 stop_extent = total_extents;
6133 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006134
Namjae Jeona904b1c2015-03-25 15:08:56 +11006135 if (current_ext == stop_extent) {
6136 *done = 1;
6137 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006138 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006139 }
Brian Fosterddb19e32014-09-23 15:38:09 +10006140 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006141 }
6142
Namjae Jeona904b1c2015-03-25 15:08:56 +11006143 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006144 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10006145 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10006146 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006147
6148del_cursor:
6149 if (cur)
6150 xfs_btree_del_cursor(cur,
6151 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6152
Brian Fosterca446d82014-09-02 12:12:53 +10006153 if (logflags)
6154 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006155
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006156 return error;
6157}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006158
6159/*
6160 * Splits an extent into two extents at split_fsb block such that it is
6161 * the first block of the current_ext. @current_ext is a target extent
6162 * to be split. @split_fsb is a block where the extents is split.
6163 * If split_fsb lies in a hole or the first block of extents, just return 0.
6164 */
6165STATIC int
6166xfs_bmap_split_extent_at(
6167 struct xfs_trans *tp,
6168 struct xfs_inode *ip,
6169 xfs_fileoff_t split_fsb,
6170 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006171 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006172{
6173 int whichfork = XFS_DATA_FORK;
6174 struct xfs_btree_cur *cur = NULL;
6175 struct xfs_bmbt_rec_host *gotp;
6176 struct xfs_bmbt_irec got;
6177 struct xfs_bmbt_irec new; /* split extent */
6178 struct xfs_mount *mp = ip->i_mount;
6179 struct xfs_ifork *ifp;
6180 xfs_fsblock_t gotblkcnt; /* new block count for got */
6181 xfs_extnum_t current_ext;
6182 int error = 0;
6183 int logflags = 0;
6184 int i = 0;
6185
6186 if (unlikely(XFS_TEST_ERROR(
6187 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6188 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6189 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6190 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6191 XFS_ERRLEVEL_LOW, mp);
6192 return -EFSCORRUPTED;
6193 }
6194
6195 if (XFS_FORCED_SHUTDOWN(mp))
6196 return -EIO;
6197
6198 ifp = XFS_IFORK_PTR(ip, whichfork);
6199 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6200 /* Read in all the extents */
6201 error = xfs_iread_extents(tp, ip, whichfork);
6202 if (error)
6203 return error;
6204 }
6205
6206 /*
6207 * gotp can be null in 2 cases: 1) if there are no extents
6208 * or 2) split_fsb lies in a hole beyond which there are
6209 * no extents. Either way, we are done.
6210 */
6211 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
6212 if (!gotp)
6213 return 0;
6214
6215 xfs_bmbt_get_all(gotp, &got);
6216
6217 /*
6218 * Check split_fsb lies in a hole or the start boundary offset
6219 * of the extent.
6220 */
6221 if (got.br_startoff >= split_fsb)
6222 return 0;
6223
6224 gotblkcnt = split_fsb - got.br_startoff;
6225 new.br_startoff = split_fsb;
6226 new.br_startblock = got.br_startblock + gotblkcnt;
6227 new.br_blockcount = got.br_blockcount - gotblkcnt;
6228 new.br_state = got.br_state;
6229
6230 if (ifp->if_flags & XFS_IFBROOT) {
6231 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6232 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006233 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006234 cur->bc_private.b.flags = 0;
6235 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6236 got.br_startblock,
6237 got.br_blockcount,
6238 &i);
6239 if (error)
6240 goto del_cursor;
6241 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6242 }
6243
6244 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6245 got.br_blockcount = gotblkcnt;
6246
6247 logflags = XFS_ILOG_CORE;
6248 if (cur) {
6249 error = xfs_bmbt_update(cur, got.br_startoff,
6250 got.br_startblock,
6251 got.br_blockcount,
6252 got.br_state);
6253 if (error)
6254 goto del_cursor;
6255 } else
6256 logflags |= XFS_ILOG_DEXT;
6257
6258 /* Add new extent */
6259 current_ext++;
6260 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6261 XFS_IFORK_NEXT_SET(ip, whichfork,
6262 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6263
6264 if (cur) {
6265 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6266 new.br_startblock, new.br_blockcount,
6267 &i);
6268 if (error)
6269 goto del_cursor;
6270 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6271 cur->bc_rec.b.br_state = new.br_state;
6272
6273 error = xfs_btree_insert(cur, &i);
6274 if (error)
6275 goto del_cursor;
6276 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6277 }
6278
6279 /*
6280 * Convert to a btree if necessary.
6281 */
6282 if (xfs_bmap_needs_btree(ip, whichfork)) {
6283 int tmp_logflags; /* partial log flag return val */
6284
6285 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006286 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006287 &cur, 0, &tmp_logflags, whichfork);
6288 logflags |= tmp_logflags;
6289 }
6290
6291del_cursor:
6292 if (cur) {
6293 cur->bc_private.b.allocated = 0;
6294 xfs_btree_del_cursor(cur,
6295 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6296 }
6297
6298 if (logflags)
6299 xfs_trans_log_inode(tp, ip, logflags);
6300 return error;
6301}
6302
6303int
6304xfs_bmap_split_extent(
6305 struct xfs_inode *ip,
6306 xfs_fileoff_t split_fsb)
6307{
6308 struct xfs_mount *mp = ip->i_mount;
6309 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006310 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006311 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006312 int error;
6313
Christoph Hellwig253f4912016-04-06 09:19:55 +10006314 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6315 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6316 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006317 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006318
6319 xfs_ilock(ip, XFS_ILOCK_EXCL);
6320 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6321
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006322 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006323
6324 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006325 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006326 if (error)
6327 goto out;
6328
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006329 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006330 if (error)
6331 goto out;
6332
Christoph Hellwig70393312015-06-04 13:48:08 +10006333 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006334
6335out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006336 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006337 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006338 return error;
6339}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006340
6341/* Deferred mapping is only for real extents in the data fork. */
6342static bool
6343xfs_bmap_is_update_needed(
6344 struct xfs_bmbt_irec *bmap)
6345{
6346 return bmap->br_startblock != HOLESTARTBLOCK &&
6347 bmap->br_startblock != DELAYSTARTBLOCK;
6348}
6349
6350/* Record a bmap intent. */
6351static int
6352__xfs_bmap_add(
6353 struct xfs_mount *mp,
6354 struct xfs_defer_ops *dfops,
6355 enum xfs_bmap_intent_type type,
6356 struct xfs_inode *ip,
6357 int whichfork,
6358 struct xfs_bmbt_irec *bmap)
6359{
6360 int error;
6361 struct xfs_bmap_intent *bi;
6362
6363 trace_xfs_bmap_defer(mp,
6364 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6365 type,
6366 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6367 ip->i_ino, whichfork,
6368 bmap->br_startoff,
6369 bmap->br_blockcount,
6370 bmap->br_state);
6371
6372 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6373 INIT_LIST_HEAD(&bi->bi_list);
6374 bi->bi_type = type;
6375 bi->bi_owner = ip;
6376 bi->bi_whichfork = whichfork;
6377 bi->bi_bmap = *bmap;
6378
6379 error = xfs_defer_join(dfops, bi->bi_owner);
6380 if (error) {
6381 kmem_free(bi);
6382 return error;
6383 }
6384
6385 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6386 return 0;
6387}
6388
6389/* Map an extent into a file. */
6390int
6391xfs_bmap_map_extent(
6392 struct xfs_mount *mp,
6393 struct xfs_defer_ops *dfops,
6394 struct xfs_inode *ip,
6395 struct xfs_bmbt_irec *PREV)
6396{
6397 if (!xfs_bmap_is_update_needed(PREV))
6398 return 0;
6399
6400 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6401 XFS_DATA_FORK, PREV);
6402}
6403
6404/* Unmap an extent out of a file. */
6405int
6406xfs_bmap_unmap_extent(
6407 struct xfs_mount *mp,
6408 struct xfs_defer_ops *dfops,
6409 struct xfs_inode *ip,
6410 struct xfs_bmbt_irec *PREV)
6411{
6412 if (!xfs_bmap_is_update_needed(PREV))
6413 return 0;
6414
6415 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6416 XFS_DATA_FORK, PREV);
6417}
6418
6419/*
6420 * Process one of the deferred bmap operations. We pass back the
6421 * btree cursor to maintain our lock on the bmapbt between calls.
6422 */
6423int
6424xfs_bmap_finish_one(
6425 struct xfs_trans *tp,
6426 struct xfs_defer_ops *dfops,
6427 struct xfs_inode *ip,
6428 enum xfs_bmap_intent_type type,
6429 int whichfork,
6430 xfs_fileoff_t startoff,
6431 xfs_fsblock_t startblock,
6432 xfs_filblks_t blockcount,
6433 xfs_exntst_t state)
6434{
6435 struct xfs_bmbt_irec bmap;
6436 int nimaps = 1;
6437 xfs_fsblock_t firstfsb;
6438 int flags = XFS_BMAPI_REMAP;
6439 int done;
6440 int error = 0;
6441
6442 bmap.br_startblock = startblock;
6443 bmap.br_startoff = startoff;
6444 bmap.br_blockcount = blockcount;
6445 bmap.br_state = state;
6446
6447 trace_xfs_bmap_deferred(tp->t_mountp,
6448 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6449 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6450 ip->i_ino, whichfork, startoff, blockcount, state);
6451
6452 if (whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK)
6453 return -EFSCORRUPTED;
6454 if (whichfork == XFS_ATTR_FORK)
6455 flags |= XFS_BMAPI_ATTRFORK;
6456
6457 if (XFS_TEST_ERROR(false, tp->t_mountp,
6458 XFS_ERRTAG_BMAP_FINISH_ONE,
6459 XFS_RANDOM_BMAP_FINISH_ONE))
6460 return -EIO;
6461
6462 switch (type) {
6463 case XFS_BMAP_MAP:
6464 firstfsb = bmap.br_startblock;
6465 error = xfs_bmapi_write(tp, ip, bmap.br_startoff,
6466 bmap.br_blockcount, flags, &firstfsb,
6467 bmap.br_blockcount, &bmap, &nimaps,
6468 dfops);
6469 break;
6470 case XFS_BMAP_UNMAP:
6471 error = xfs_bunmapi(tp, ip, bmap.br_startoff,
6472 bmap.br_blockcount, flags, 1, &firstfsb,
6473 dfops, &done);
6474 ASSERT(done);
6475 break;
6476 default:
6477 ASSERT(0);
6478 error = -EFSCORRUPTED;
6479 }
6480
6481 return error;
6482}