blob: 7f42f6067eb5e68d60fea0a18dcf4d7769f4609a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100030#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_extfree_item.h"
36#include "xfs_alloc.h"
37#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100038#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110039#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_rtalloc.h"
41#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_quota.h"
43#include "xfs_trans_space.h"
44#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110046#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110047#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110048#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100049#include "xfs_rmap.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100050#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070051#include "xfs_refcount.h"
Darrick J. Wongfd26a882016-11-28 14:57:42 +110052#include "xfs_rmap_btree.h"
Brian Foster974ae922016-11-28 14:57:42 +110053#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056kmem_zone_t *xfs_bmap_free_item_zone;
57
58/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110059 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110063 * Compute and fill in the value of the maximum depth of a bmap btree
64 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110066void
67xfs_bmap_compute_maxlevels(
68 xfs_mount_t *mp, /* file system mount structure */
69 int whichfork) /* data or attr fork */
70{
71 int level; /* btree level */
72 uint maxblocks; /* max blocks at this level */
73 uint maxleafents; /* max leaf entries possible */
74 int maxrootrecs; /* max records in root block */
75 int minleafrecs; /* min records in leaf block */
76 int minnoderecs; /* min records in node block */
77 int sz; /* root block size */
78
79 /*
80 * The maximum number of extents in a file, hence the maximum
81 * number of leaf entries, is controlled by the type of di_nextents
82 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
83 * (a signed 16-bit number, xfs_aextnum_t).
84 *
85 * Note that we can no longer assume that if we are in ATTR1 that
86 * the fork offset of all the inodes will be
87 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
88 * with ATTR2 and then mounted back with ATTR1, keeping the
89 * di_forkoff's fixed but probably at various positions. Therefore,
90 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
91 * of a minimum size available.
92 */
93 if (whichfork == XFS_DATA_FORK) {
94 maxleafents = MAXEXTNUM;
95 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
96 } else {
97 maxleafents = MAXAEXTNUM;
98 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
99 }
Eric Sandeen152d93b2014-04-14 18:58:51 +1000100 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100101 minleafrecs = mp->m_bmap_dmnr[0];
102 minnoderecs = mp->m_bmap_dmnr[1];
103 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
104 for (level = 1; maxblocks > 1; level++) {
105 if (maxblocks <= maxrootrecs)
106 maxblocks = 1;
107 else
108 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
109 }
110 mp->m_bm_maxlevels[whichfork] = level;
111}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100113STATIC int /* error */
114xfs_bmbt_lookup_eq(
115 struct xfs_btree_cur *cur,
116 xfs_fileoff_t off,
117 xfs_fsblock_t bno,
118 xfs_filblks_t len,
119 int *stat) /* success/failure */
120{
121 cur->bc_rec.b.br_startoff = off;
122 cur->bc_rec.b.br_startblock = bno;
123 cur->bc_rec.b.br_blockcount = len;
124 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
125}
126
127STATIC int /* error */
128xfs_bmbt_lookup_ge(
129 struct xfs_btree_cur *cur,
130 xfs_fileoff_t off,
131 xfs_fsblock_t bno,
132 xfs_filblks_t len,
133 int *stat) /* success/failure */
134{
135 cur->bc_rec.b.br_startoff = off;
136 cur->bc_rec.b.br_startblock = bno;
137 cur->bc_rec.b.br_blockcount = len;
138 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
139}
140
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100141/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000142 * Check if the inode needs to be converted to btree format.
143 */
144static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
145{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700146 return whichfork != XFS_COW_FORK &&
147 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000148 XFS_IFORK_NEXTENTS(ip, whichfork) >
149 XFS_IFORK_MAXEXT(ip, whichfork);
150}
151
152/*
153 * Check if the inode should be converted to extent format.
154 */
155static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
156{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700157 return whichfork != XFS_COW_FORK &&
158 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000159 XFS_IFORK_NEXTENTS(ip, whichfork) <=
160 XFS_IFORK_MAXEXT(ip, whichfork);
161}
162
163/*
164 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100165 * by [off, bno, len, state].
166 * This either works (return 0) or gets an EFSCORRUPTED error.
167 */
168STATIC int
169xfs_bmbt_update(
170 struct xfs_btree_cur *cur,
171 xfs_fileoff_t off,
172 xfs_fsblock_t bno,
173 xfs_filblks_t len,
174 xfs_exntst_t state)
175{
176 union xfs_btree_rec rec;
177
178 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
179 return xfs_btree_update(cur, &rec);
180}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100183 * Compute the worst-case number of indirect blocks that will be used
184 * for ip's delayed extent of length "len".
185 */
186STATIC xfs_filblks_t
187xfs_bmap_worst_indlen(
188 xfs_inode_t *ip, /* incore inode pointer */
189 xfs_filblks_t len) /* delayed extent length */
190{
191 int level; /* btree level number */
192 int maxrecs; /* maximum record count at this level */
193 xfs_mount_t *mp; /* mount structure */
194 xfs_filblks_t rval; /* return value */
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100195 xfs_filblks_t orig_len;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100196
197 mp = ip->i_mount;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100198
199 /* Calculate the worst-case size of the bmbt. */
200 orig_len = len;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100201 maxrecs = mp->m_bmap_dmxr[0];
202 for (level = 0, rval = 0;
203 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
204 level++) {
205 len += maxrecs - 1;
206 do_div(len, maxrecs);
207 rval += len;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100208 if (len == 1) {
209 rval += XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100210 level - 1;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100211 break;
212 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100213 if (level == 0)
214 maxrecs = mp->m_bmap_dmxr[1];
215 }
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100216
217 /* Calculate the worst-case size of the rmapbt. */
218 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
219 rval += 1 + xfs_rmapbt_calc_size(mp, orig_len) +
220 mp->m_rmap_maxlevels;
221
Dave Chinner9e5987a2013-02-25 12:31:26 +1100222 return rval;
223}
224
225/*
226 * Calculate the default attribute fork offset for newly created inodes.
227 */
228uint
229xfs_default_attroffset(
230 struct xfs_inode *ip)
231{
232 struct xfs_mount *mp = ip->i_mount;
233 uint offset;
234
235 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100236 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100237 XFS_BMDR_SPACE_CALC(MINABTPTRS);
238 } else {
239 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
240 }
241
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100242 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100243 return offset;
244}
245
246/*
247 * Helper routine to reset inode di_forkoff field when switching
248 * attribute fork from local to extent format - we reset it where
249 * possible to make space available for inline data fork extents.
250 */
251STATIC void
252xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100253 xfs_inode_t *ip,
254 int whichfork)
255{
256 if (whichfork == XFS_ATTR_FORK &&
257 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
258 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
259 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
260 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
261
262 if (dfl_forkoff > ip->i_d.di_forkoff)
263 ip->i_d.di_forkoff = dfl_forkoff;
264 }
265}
266
Dave Chinner9e5987a2013-02-25 12:31:26 +1100267#ifdef DEBUG
268STATIC struct xfs_buf *
269xfs_bmap_get_bp(
270 struct xfs_btree_cur *cur,
271 xfs_fsblock_t bno)
272{
273 struct xfs_log_item_desc *lidp;
274 int i;
275
276 if (!cur)
277 return NULL;
278
279 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
280 if (!cur->bc_bufs[i])
281 break;
282 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
283 return cur->bc_bufs[i];
284 }
285
286 /* Chase down all the log items to see if the bp is there */
287 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
288 struct xfs_buf_log_item *bip;
289 bip = (struct xfs_buf_log_item *)lidp->lid_item;
290 if (bip->bli_item.li_type == XFS_LI_BUF &&
291 XFS_BUF_ADDR(bip->bli_buf) == bno)
292 return bip->bli_buf;
293 }
294
295 return NULL;
296}
297
298STATIC void
299xfs_check_block(
300 struct xfs_btree_block *block,
301 xfs_mount_t *mp,
302 int root,
303 short sz)
304{
305 int i, j, dmxr;
306 __be64 *pp, *thispa; /* pointer to block address */
307 xfs_bmbt_key_t *prevp, *keyp;
308
309 ASSERT(be16_to_cpu(block->bb_level) > 0);
310
311 prevp = NULL;
312 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
313 dmxr = mp->m_bmap_dmxr[0];
314 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
315
316 if (prevp) {
317 ASSERT(be64_to_cpu(prevp->br_startoff) <
318 be64_to_cpu(keyp->br_startoff));
319 }
320 prevp = keyp;
321
322 /*
323 * Compare the block numbers to see if there are dups.
324 */
325 if (root)
326 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
327 else
328 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
329
330 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
331 if (root)
332 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
333 else
334 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
335 if (*thispa == *pp) {
336 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
337 __func__, j, i,
338 (unsigned long long)be64_to_cpu(*thispa));
339 panic("%s: ptrs are equal in node\n",
340 __func__);
341 }
342 }
343 }
344}
345
346/*
347 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100348 * btree leaves. THis becomes prohibitively expensive for large extent count
349 * files, so don't bother with inodes that have more than 10,000 extents in
350 * them. The btree record ordering checks will still be done, so for such large
351 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100352 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100353STATIC void
354xfs_bmap_check_leaf_extents(
355 xfs_btree_cur_t *cur, /* btree cursor or null */
356 xfs_inode_t *ip, /* incore inode pointer */
357 int whichfork) /* data or attr fork */
358{
359 struct xfs_btree_block *block; /* current btree block */
360 xfs_fsblock_t bno; /* block # of "block" */
361 xfs_buf_t *bp; /* buffer for "block" */
362 int error; /* error return value */
363 xfs_extnum_t i=0, j; /* index into the extents list */
364 xfs_ifork_t *ifp; /* fork structure */
365 int level; /* btree level, for checking */
366 xfs_mount_t *mp; /* file system mount structure */
367 __be64 *pp; /* pointer to block address */
368 xfs_bmbt_rec_t *ep; /* pointer to current extent */
369 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
370 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
371 int bp_release = 0;
372
373 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
374 return;
375 }
376
Dave Chinnere3543812016-01-08 11:28:49 +1100377 /* skip large extent count inodes */
378 if (ip->i_d.di_nextents > 10000)
379 return;
380
Dave Chinner9e5987a2013-02-25 12:31:26 +1100381 bno = NULLFSBLOCK;
382 mp = ip->i_mount;
383 ifp = XFS_IFORK_PTR(ip, whichfork);
384 block = ifp->if_broot;
385 /*
386 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
387 */
388 level = be16_to_cpu(block->bb_level);
389 ASSERT(level > 0);
390 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
391 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
392 bno = be64_to_cpu(*pp);
393
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000394 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100395 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
396 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
397
398 /*
399 * Go down the tree until leaf level is reached, following the first
400 * pointer (leftmost) at each level.
401 */
402 while (level-- > 0) {
403 /* See if buf is in cur first */
404 bp_release = 0;
405 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
406 if (!bp) {
407 bp_release = 1;
408 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
409 XFS_BMAP_BTREE_REF,
410 &xfs_bmbt_buf_ops);
411 if (error)
412 goto error_norelse;
413 }
414 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100415 if (level == 0)
416 break;
417
418 /*
419 * Check this block for basic sanity (increasing keys and
420 * no duplicate blocks).
421 */
422
423 xfs_check_block(block, mp, 0, 0);
424 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
425 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100426 XFS_WANT_CORRUPTED_GOTO(mp,
427 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100428 if (bp_release) {
429 bp_release = 0;
430 xfs_trans_brelse(NULL, bp);
431 }
432 }
433
434 /*
435 * Here with bp and block set to the leftmost leaf node in the tree.
436 */
437 i = 0;
438
439 /*
440 * Loop over all leaf nodes checking that all extents are in the right order.
441 */
442 for (;;) {
443 xfs_fsblock_t nextbno;
444 xfs_extnum_t num_recs;
445
446
447 num_recs = xfs_btree_get_numrecs(block);
448
449 /*
450 * Read-ahead the next leaf block, if any.
451 */
452
453 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
454
455 /*
456 * Check all the extents to make sure they are OK.
457 * If we had a previous block, the last entry should
458 * conform with the first entry in this one.
459 */
460
461 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
462 if (i) {
463 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
464 xfs_bmbt_disk_get_blockcount(&last) <=
465 xfs_bmbt_disk_get_startoff(ep));
466 }
467 for (j = 1; j < num_recs; j++) {
468 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
469 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
470 xfs_bmbt_disk_get_blockcount(ep) <=
471 xfs_bmbt_disk_get_startoff(nextp));
472 ep = nextp;
473 }
474
475 last = *ep;
476 i += num_recs;
477 if (bp_release) {
478 bp_release = 0;
479 xfs_trans_brelse(NULL, bp);
480 }
481 bno = nextbno;
482 /*
483 * If we've reached the end, stop.
484 */
485 if (bno == NULLFSBLOCK)
486 break;
487
488 bp_release = 0;
489 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
490 if (!bp) {
491 bp_release = 1;
492 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
493 XFS_BMAP_BTREE_REF,
494 &xfs_bmbt_buf_ops);
495 if (error)
496 goto error_norelse;
497 }
498 block = XFS_BUF_TO_BLOCK(bp);
499 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100500
Dave Chinner9e5987a2013-02-25 12:31:26 +1100501 return;
502
503error0:
504 xfs_warn(mp, "%s: at error0", __func__);
505 if (bp_release)
506 xfs_trans_brelse(NULL, bp);
507error_norelse:
508 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
509 __func__, i);
510 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
511 return;
512}
513
514/*
515 * Add bmap trace insert entries for all the contents of the extent records.
516 */
517void
518xfs_bmap_trace_exlist(
519 xfs_inode_t *ip, /* incore inode pointer */
520 xfs_extnum_t cnt, /* count of entries in the list */
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100521 int whichfork, /* data or attr or cow fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100522 unsigned long caller_ip)
523{
524 xfs_extnum_t idx; /* extent record index */
525 xfs_ifork_t *ifp; /* inode fork pointer */
526 int state = 0;
527
528 if (whichfork == XFS_ATTR_FORK)
529 state |= BMAP_ATTRFORK;
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100530 else if (whichfork == XFS_COW_FORK)
531 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100532
533 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +1100534 ASSERT(cnt == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100535 for (idx = 0; idx < cnt; idx++)
Eric Sandeen77105172016-12-05 12:31:50 +1100536 trace_xfs_extlist(ip, idx, state, caller_ip);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100537}
538
539/*
540 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000541 * given the caller's original parameters. Specifically check the
542 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100543 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
544 */
545STATIC void
546xfs_bmap_validate_ret(
547 xfs_fileoff_t bno,
548 xfs_filblks_t len,
549 int flags,
550 xfs_bmbt_irec_t *mval,
551 int nmap,
552 int ret_nmap)
553{
554 int i; /* index to map values */
555
556 ASSERT(ret_nmap <= nmap);
557
558 for (i = 0; i < ret_nmap; i++) {
559 ASSERT(mval[i].br_blockcount > 0);
560 if (!(flags & XFS_BMAPI_ENTIRE)) {
561 ASSERT(mval[i].br_startoff >= bno);
562 ASSERT(mval[i].br_blockcount <= len);
563 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
564 bno + len);
565 } else {
566 ASSERT(mval[i].br_startoff < bno + len);
567 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
568 bno);
569 }
570 ASSERT(i == 0 ||
571 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
572 mval[i].br_startoff);
573 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
574 mval[i].br_startblock != HOLESTARTBLOCK);
575 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
576 mval[i].br_state == XFS_EXT_UNWRITTEN);
577 }
578}
579
580#else
581#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
582#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
583#endif /* DEBUG */
584
585/*
586 * bmap free list manipulation functions
587 */
588
589/*
590 * Add the extent to the list of extents to be free at transaction end.
591 * The list is maintained sorted (by block number).
592 */
593void
594xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000595 struct xfs_mount *mp,
596 struct xfs_defer_ops *dfops,
597 xfs_fsblock_t bno,
598 xfs_filblks_t len,
599 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100600{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000601 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100602#ifdef DEBUG
603 xfs_agnumber_t agno;
604 xfs_agblock_t agbno;
605
606 ASSERT(bno != NULLFSBLOCK);
607 ASSERT(len > 0);
608 ASSERT(len <= MAXEXTLEN);
609 ASSERT(!isnullstartblock(bno));
610 agno = XFS_FSB_TO_AGNO(mp, bno);
611 agbno = XFS_FSB_TO_AGBNO(mp, bno);
612 ASSERT(agno < mp->m_sb.sb_agcount);
613 ASSERT(agbno < mp->m_sb.sb_agblocks);
614 ASSERT(len < mp->m_sb.sb_agblocks);
615 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
616#endif
617 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000618
Dave Chinner9e5987a2013-02-25 12:31:26 +1100619 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000620 new->xefi_startblock = bno;
621 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000622 if (oinfo)
623 new->xefi_oinfo = *oinfo;
624 else
625 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000626 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
627 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000628 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100629}
630
631/*
632 * Inode fork format manipulation functions
633 */
634
635/*
636 * Transform a btree format file with only one leaf node, where the
637 * extents list will fit in the inode, into an extents format file.
638 * Since the file extents are already in-core, all we have to do is
639 * give up the space for the btree root and pitch the leaf block.
640 */
641STATIC int /* error */
642xfs_bmap_btree_to_extents(
643 xfs_trans_t *tp, /* transaction pointer */
644 xfs_inode_t *ip, /* incore inode pointer */
645 xfs_btree_cur_t *cur, /* btree cursor */
646 int *logflagsp, /* inode logging flags */
647 int whichfork) /* data or attr fork */
648{
649 /* REFERENCED */
650 struct xfs_btree_block *cblock;/* child btree block */
651 xfs_fsblock_t cbno; /* child block number */
652 xfs_buf_t *cbp; /* child block's buffer */
653 int error; /* error return value */
654 xfs_ifork_t *ifp; /* inode fork data */
655 xfs_mount_t *mp; /* mount point structure */
656 __be64 *pp; /* ptr to block address */
657 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000658 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100659
660 mp = ip->i_mount;
661 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700662 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100663 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
664 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
665 rblock = ifp->if_broot;
666 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
667 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
668 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
669 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
670 cbno = be64_to_cpu(*pp);
671 *logflagsp = 0;
672#ifdef DEBUG
673 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
674 return error;
675#endif
676 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
677 &xfs_bmbt_buf_ops);
678 if (error)
679 return error;
680 cblock = XFS_BUF_TO_BLOCK(cbp);
681 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
682 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000683 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
684 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100685 ip->i_d.di_nblocks--;
686 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
687 xfs_trans_binval(tp, cbp);
688 if (cur->bc_bufs[0] == cbp)
689 cur->bc_bufs[0] = NULL;
690 xfs_iroot_realloc(ip, -1, whichfork);
691 ASSERT(ifp->if_broot == NULL);
692 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
693 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
694 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
695 return 0;
696}
697
698/*
699 * Convert an extents-format file into a btree-format file.
700 * The new file will have a root block (in the inode) and a single child block.
701 */
702STATIC int /* error */
703xfs_bmap_extents_to_btree(
704 xfs_trans_t *tp, /* transaction pointer */
705 xfs_inode_t *ip, /* incore inode pointer */
706 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000707 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100708 xfs_btree_cur_t **curp, /* cursor returned to caller */
709 int wasdel, /* converting a delayed alloc */
710 int *logflagsp, /* inode logging flags */
711 int whichfork) /* data or attr fork */
712{
713 struct xfs_btree_block *ablock; /* allocated (child) bt block */
714 xfs_buf_t *abp; /* buffer for ablock */
715 xfs_alloc_arg_t args; /* allocation arguments */
716 xfs_bmbt_rec_t *arp; /* child record pointer */
717 struct xfs_btree_block *block; /* btree root block */
718 xfs_btree_cur_t *cur; /* bmap btree cursor */
719 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
720 int error; /* error return value */
721 xfs_extnum_t i, cnt; /* extent record index */
722 xfs_ifork_t *ifp; /* inode fork pointer */
723 xfs_bmbt_key_t *kp; /* root block key pointer */
724 xfs_mount_t *mp; /* mount structure */
725 xfs_extnum_t nextents; /* number of file extents */
726 xfs_bmbt_ptr_t *pp; /* root block address pointer */
727
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500728 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700729 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100730 ifp = XFS_IFORK_PTR(ip, whichfork);
731 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
732
733 /*
734 * Make space in the inode incore.
735 */
736 xfs_iroot_realloc(ip, 1, whichfork);
737 ifp->if_flags |= XFS_IFBROOT;
738
739 /*
740 * Fill in the root.
741 */
742 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800743 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
744 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800745 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100746 /*
747 * Need a cursor. Can't allocate until bb_level is filled in.
748 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100749 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
750 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000751 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100752 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
753 /*
754 * Convert to a btree with two levels, one record in root.
755 */
756 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
757 memset(&args, 0, sizeof(args));
758 args.tp = tp;
759 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000760 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100761 args.firstblock = *firstblock;
762 if (*firstblock == NULLFSBLOCK) {
763 args.type = XFS_ALLOCTYPE_START_BNO;
764 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000765 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100766 args.type = XFS_ALLOCTYPE_START_BNO;
767 args.fsbno = *firstblock;
768 } else {
769 args.type = XFS_ALLOCTYPE_NEAR_BNO;
770 args.fsbno = *firstblock;
771 }
772 args.minlen = args.maxlen = args.prod = 1;
773 args.wasdel = wasdel;
774 *logflagsp = 0;
775 if ((error = xfs_alloc_vextent(&args))) {
776 xfs_iroot_realloc(ip, -1, whichfork);
777 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
778 return error;
779 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700780
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800781 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
782 xfs_iroot_realloc(ip, -1, whichfork);
783 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
784 return -ENOSPC;
785 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100786 /*
787 * Allocation can't fail, the space was reserved.
788 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100789 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800790 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100791 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
792 cur->bc_private.b.allocated++;
793 ip->i_d.di_nblocks++;
794 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
795 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
796 /*
797 * Fill in the child block.
798 */
799 abp->b_ops = &xfs_bmbt_buf_ops;
800 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800801 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
802 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500803 XFS_BTREE_LONG_PTRS);
804
Dave Chinner9e5987a2013-02-25 12:31:26 +1100805 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100806 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100807 for (cnt = i = 0; i < nextents; i++) {
808 ep = xfs_iext_get_ext(ifp, i);
809 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
810 arp->l0 = cpu_to_be64(ep->l0);
811 arp->l1 = cpu_to_be64(ep->l1);
812 arp++; cnt++;
813 }
814 }
815 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
816 xfs_btree_set_numrecs(ablock, cnt);
817
818 /*
819 * Fill in the root key and pointer.
820 */
821 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
822 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
823 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
824 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
825 be16_to_cpu(block->bb_level)));
826 *pp = cpu_to_be64(args.fsbno);
827
828 /*
829 * Do all this logging at the end so that
830 * the root is at the right level.
831 */
832 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
833 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
834 ASSERT(*curp == NULL);
835 *curp = cur;
836 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
837 return 0;
838}
839
840/*
841 * Convert a local file to an extents file.
842 * This code is out of bounds for data forks of regular files,
843 * since the file data needs to get logged so things will stay consistent.
844 * (The bmap-level manipulations are ok, though).
845 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000846void
847xfs_bmap_local_to_extents_empty(
848 struct xfs_inode *ip,
849 int whichfork)
850{
851 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
852
Darrick J. Wong60b49842016-10-03 09:11:34 -0700853 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000854 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
855 ASSERT(ifp->if_bytes == 0);
856 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
857
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000858 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000859 ifp->if_flags &= ~XFS_IFINLINE;
860 ifp->if_flags |= XFS_IFEXTENTS;
861 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
862}
863
864
Dave Chinner9e5987a2013-02-25 12:31:26 +1100865STATIC int /* error */
866xfs_bmap_local_to_extents(
867 xfs_trans_t *tp, /* transaction pointer */
868 xfs_inode_t *ip, /* incore inode pointer */
869 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
870 xfs_extlen_t total, /* total blocks needed by transaction */
871 int *logflagsp, /* inode logging flags */
872 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500873 void (*init_fn)(struct xfs_trans *tp,
874 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100875 struct xfs_inode *ip,
876 struct xfs_ifork *ifp))
877{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000878 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100879 int flags; /* logging flags returned */
880 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000881 xfs_alloc_arg_t args; /* allocation arguments */
882 xfs_buf_t *bp; /* buffer for extent block */
883 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100884
885 /*
886 * We don't want to deal with the case of keeping inode data inline yet.
887 * So sending the data fork of a regular inode is invalid.
888 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100889 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100890 ifp = XFS_IFORK_PTR(ip, whichfork);
891 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000892
893 if (!ifp->if_bytes) {
894 xfs_bmap_local_to_extents_empty(ip, whichfork);
895 flags = XFS_ILOG_CORE;
896 goto done;
897 }
898
Dave Chinner9e5987a2013-02-25 12:31:26 +1100899 flags = 0;
900 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000901 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
902 XFS_IFINLINE);
903 memset(&args, 0, sizeof(args));
904 args.tp = tp;
905 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000906 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000907 args.firstblock = *firstblock;
908 /*
909 * Allocate a block. We know we need only one, since the
910 * file currently fits in an inode.
911 */
912 if (*firstblock == NULLFSBLOCK) {
913 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
914 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100915 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000916 args.fsbno = *firstblock;
917 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100918 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000919 args.total = total;
920 args.minlen = args.maxlen = args.prod = 1;
921 error = xfs_alloc_vextent(&args);
922 if (error)
923 goto done;
924
925 /* Can't fail, the space was reserved. */
926 ASSERT(args.fsbno != NULLFSBLOCK);
927 ASSERT(args.len == 1);
928 *firstblock = args.fsbno;
929 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
930
Dave Chinnerfe22d552015-01-22 09:30:06 +1100931 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100932 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100933 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100934 * The callout is responsible for logging because the remote format
935 * might differ from the local format and thus we don't know how much to
936 * log here. Note that init_fn must also set the buffer log item type
937 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100938 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000939 init_fn(tp, bp, ip, ifp);
940
Brian Fosterb7cdc662015-10-12 15:40:24 +1100941 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000942 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
943 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100944 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000945
946 xfs_iext_add(ifp, 0, 1);
947 ep = xfs_iext_get_ext(ifp, 0);
948 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
949 trace_xfs_bmap_post_update(ip, 0,
950 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
951 _THIS_IP_);
952 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
953 ip->i_d.di_nblocks = 1;
954 xfs_trans_mod_dquot_byino(tp, ip,
955 XFS_TRANS_DQ_BCOUNT, 1L);
956 flags |= xfs_ilog_fext(whichfork);
957
Dave Chinner9e5987a2013-02-25 12:31:26 +1100958done:
959 *logflagsp = flags;
960 return error;
961}
962
963/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 * Called from xfs_bmap_add_attrfork to handle btree format files.
965 */
966STATIC int /* error */
967xfs_bmap_add_attrfork_btree(
968 xfs_trans_t *tp, /* transaction pointer */
969 xfs_inode_t *ip, /* incore inode pointer */
970 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000971 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 int *flags) /* inode logging flags */
973{
974 xfs_btree_cur_t *cur; /* btree cursor */
975 int error; /* error return value */
976 xfs_mount_t *mp; /* file system mount struct */
977 int stat; /* newroot status */
978
979 mp = ip->i_mount;
980 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
981 *flags |= XFS_ILOG_DBROOT;
982 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100983 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000984 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 cur->bc_private.b.firstblock = *firstblock;
986 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
987 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000988 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100989 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100990 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 goto error0;
992 if (stat == 0) {
993 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000994 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
996 *firstblock = cur->bc_private.b.firstblock;
997 cur->bc_private.b.allocated = 0;
998 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
999 }
1000 return 0;
1001error0:
1002 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1003 return error;
1004}
1005
1006/*
1007 * Called from xfs_bmap_add_attrfork to handle extents format files.
1008 */
1009STATIC int /* error */
1010xfs_bmap_add_attrfork_extents(
1011 xfs_trans_t *tp, /* transaction pointer */
1012 xfs_inode_t *ip, /* incore inode pointer */
1013 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001014 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 int *flags) /* inode logging flags */
1016{
1017 xfs_btree_cur_t *cur; /* bmap btree cursor */
1018 int error; /* error return value */
1019
1020 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1021 return 0;
1022 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001023 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 flags, XFS_DATA_FORK);
1025 if (cur) {
1026 cur->bc_private.b.allocated = 0;
1027 xfs_btree_del_cursor(cur,
1028 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1029 }
1030 return error;
1031}
1032
1033/*
Dave Chinner1e823792013-02-11 15:58:13 +11001034 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1035 * different data fork content type needs a different callout to do the
1036 * conversion. Some are basic and only require special block initialisation
1037 * callouts for the data formating, others (directories) are so specialised they
1038 * handle everything themselves.
1039 *
1040 * XXX (dgc): investigate whether directory conversion can use the generic
1041 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001042 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 */
1044STATIC int /* error */
1045xfs_bmap_add_attrfork_local(
1046 xfs_trans_t *tp, /* transaction pointer */
1047 xfs_inode_t *ip, /* incore inode pointer */
1048 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001049 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 int *flags) /* inode logging flags */
1051{
1052 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1055 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001056
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001057 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001059 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 dargs.dp = ip;
1061 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001062 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001063 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 dargs.whichfork = XFS_DATA_FORK;
1065 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001066 return xfs_dir2_sf_to_block(&dargs);
1067 }
1068
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001069 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001070 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1071 flags, XFS_DATA_FORK,
1072 xfs_symlink_local_to_remote);
1073
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001074 /* should only be called for types that support local format data */
1075 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001076 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
1079/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001080 * Convert inode from non-attributed to attributed.
1081 * Must not be in a transaction, ip must not be locked.
1082 */
1083int /* error code */
1084xfs_bmap_add_attrfork(
1085 xfs_inode_t *ip, /* incore inode pointer */
1086 int size, /* space new attribute needs */
1087 int rsvd) /* xact may use reserved blks */
1088{
1089 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001090 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001091 xfs_mount_t *mp; /* mount structure */
1092 xfs_trans_t *tp; /* transaction pointer */
1093 int blks; /* space reservation */
1094 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001095 int logflags; /* logging flags */
1096 int error; /* error return value */
1097
1098 ASSERT(XFS_IFORK_Q(ip) == 0);
1099
1100 mp = ip->i_mount;
1101 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001102
Dave Chinner9e5987a2013-02-25 12:31:26 +11001103 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001104
1105 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1106 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1107 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001108 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001109
Dave Chinner9e5987a2013-02-25 12:31:26 +11001110 xfs_ilock(ip, XFS_ILOCK_EXCL);
1111 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1112 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1113 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001114 if (error)
1115 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001116 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001117 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001118 if (ip->i_d.di_anextents != 0) {
1119 error = -EFSCORRUPTED;
1120 goto trans_cancel;
1121 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001122 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1123 /*
1124 * For inodes coming from pre-6.2 filesystems.
1125 */
1126 ASSERT(ip->i_d.di_aformat == 0);
1127 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1128 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001129
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001130 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001131 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1132
1133 switch (ip->i_d.di_format) {
1134 case XFS_DINODE_FMT_DEV:
1135 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1136 break;
1137 case XFS_DINODE_FMT_UUID:
1138 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1139 break;
1140 case XFS_DINODE_FMT_LOCAL:
1141 case XFS_DINODE_FMT_EXTENTS:
1142 case XFS_DINODE_FMT_BTREE:
1143 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1144 if (!ip->i_d.di_forkoff)
1145 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1146 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1147 version = 2;
1148 break;
1149 default:
1150 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001151 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001152 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001153 }
1154
1155 ASSERT(ip->i_afp == NULL);
1156 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1157 ip->i_afp->if_flags = XFS_IFEXTENTS;
1158 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001159 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001160 switch (ip->i_d.di_format) {
1161 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001162 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001163 &logflags);
1164 break;
1165 case XFS_DINODE_FMT_EXTENTS:
1166 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001167 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001168 break;
1169 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001170 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001171 &logflags);
1172 break;
1173 default:
1174 error = 0;
1175 break;
1176 }
1177 if (logflags)
1178 xfs_trans_log_inode(tp, ip, logflags);
1179 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001180 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001181 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1182 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001183 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001184
1185 spin_lock(&mp->m_sb_lock);
1186 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1187 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001188 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001189 }
1190 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1191 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001192 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001193 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001194 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001195 if (log_sb)
1196 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001197 }
1198
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001199 error = xfs_defer_finish(&tp, &dfops, NULL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001200 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001201 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001202 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001203 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001204 return error;
1205
1206bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001207 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001208trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001209 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001210 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001211 return error;
1212}
1213
1214/*
1215 * Internal and external extent tree search functions.
1216 */
1217
1218/*
1219 * Read in the extents to if_extents.
1220 * All inode fields are set up by caller, we just traverse the btree
1221 * and copy the records in. If the file system cannot contain unwritten
1222 * extents, the records are checked for no "state" flags.
1223 */
1224int /* error */
1225xfs_bmap_read_extents(
1226 xfs_trans_t *tp, /* transaction pointer */
1227 xfs_inode_t *ip, /* incore inode */
1228 int whichfork) /* data or attr fork */
1229{
1230 struct xfs_btree_block *block; /* current btree block */
1231 xfs_fsblock_t bno; /* block # of "block" */
1232 xfs_buf_t *bp; /* buffer for "block" */
1233 int error; /* error return value */
1234 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1235 xfs_extnum_t i, j; /* index into the extents list */
1236 xfs_ifork_t *ifp; /* fork structure */
1237 int level; /* btree level, for checking */
1238 xfs_mount_t *mp; /* file system mount structure */
1239 __be64 *pp; /* pointer to block address */
1240 /* REFERENCED */
1241 xfs_extnum_t room; /* number of entries there's room for */
1242
Dave Chinner9e5987a2013-02-25 12:31:26 +11001243 mp = ip->i_mount;
1244 ifp = XFS_IFORK_PTR(ip, whichfork);
1245 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1246 XFS_EXTFMT_INODE(ip);
1247 block = ifp->if_broot;
1248 /*
1249 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1250 */
1251 level = be16_to_cpu(block->bb_level);
1252 ASSERT(level > 0);
1253 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1254 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001255
Dave Chinner9e5987a2013-02-25 12:31:26 +11001256 /*
1257 * Go down the tree until leaf level is reached, following the first
1258 * pointer (leftmost) at each level.
1259 */
1260 while (level-- > 0) {
1261 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1262 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1263 if (error)
1264 return error;
1265 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001266 if (level == 0)
1267 break;
1268 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1269 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001270 XFS_WANT_CORRUPTED_GOTO(mp,
1271 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001272 xfs_trans_brelse(tp, bp);
1273 }
1274 /*
1275 * Here with bp and block set to the leftmost leaf node in the tree.
1276 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001277 room = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001278 i = 0;
1279 /*
1280 * Loop over all leaf nodes. Copy information to the extent records.
1281 */
1282 for (;;) {
1283 xfs_bmbt_rec_t *frp;
1284 xfs_fsblock_t nextbno;
1285 xfs_extnum_t num_recs;
1286 xfs_extnum_t start;
1287
1288 num_recs = xfs_btree_get_numrecs(block);
1289 if (unlikely(i + num_recs > room)) {
1290 ASSERT(i + num_recs <= room);
1291 xfs_warn(ip->i_mount,
1292 "corrupt dinode %Lu, (btree extents).",
1293 (unsigned long long) ip->i_ino);
1294 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1295 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1296 goto error0;
1297 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001298 /*
1299 * Read-ahead the next leaf block, if any.
1300 */
1301 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1302 if (nextbno != NULLFSBLOCK)
1303 xfs_btree_reada_bufl(mp, nextbno, 1,
1304 &xfs_bmbt_buf_ops);
1305 /*
1306 * Copy records into the extent records.
1307 */
1308 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1309 start = i;
1310 for (j = 0; j < num_recs; j++, i++, frp++) {
1311 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1312 trp->l0 = be64_to_cpu(frp->l0);
1313 trp->l1 = be64_to_cpu(frp->l1);
1314 }
1315 if (exntf == XFS_EXTFMT_NOSTATE) {
1316 /*
1317 * Check all attribute bmap btree records and
1318 * any "older" data bmap btree records for a
1319 * set bit in the "extent flag" position.
1320 */
1321 if (unlikely(xfs_check_nostate_extents(ifp,
1322 start, num_recs))) {
1323 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1324 XFS_ERRLEVEL_LOW,
1325 ip->i_mount);
1326 goto error0;
1327 }
1328 }
1329 xfs_trans_brelse(tp, bp);
1330 bno = nextbno;
1331 /*
1332 * If we've reached the end, stop.
1333 */
1334 if (bno == NULLFSBLOCK)
1335 break;
1336 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1337 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1338 if (error)
1339 return error;
1340 block = XFS_BUF_TO_BLOCK(bp);
1341 }
Darrick J. Wong356a3222016-12-05 12:36:56 +11001342 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1343 return -EFSCORRUPTED;
Eric Sandeen5d829302016-11-08 12:59:42 +11001344 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11001345 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1346 return 0;
1347error0:
1348 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001349 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001350}
1351
Dave Chinner9e5987a2013-02-25 12:31:26 +11001352/*
1353 * Returns the file-relative block number of the first unused block(s)
1354 * in the file with at least "len" logically contiguous blocks free.
1355 * This is the lowest-address hole if the file has holes, else the first block
1356 * past the end of file.
1357 * Return 0 if the file is currently local (in-inode).
1358 */
1359int /* error */
1360xfs_bmap_first_unused(
1361 xfs_trans_t *tp, /* transaction pointer */
1362 xfs_inode_t *ip, /* incore inode */
1363 xfs_extlen_t len, /* size of hole to find */
1364 xfs_fileoff_t *first_unused, /* unused block */
1365 int whichfork) /* data or attr fork */
1366{
1367 int error; /* error return value */
1368 int idx; /* extent record index */
1369 xfs_ifork_t *ifp; /* inode fork pointer */
1370 xfs_fileoff_t lastaddr; /* last block number seen */
1371 xfs_fileoff_t lowest; /* lowest useful block */
1372 xfs_fileoff_t max; /* starting useful block */
1373 xfs_fileoff_t off; /* offset for this block */
1374 xfs_extnum_t nextents; /* number of extent entries */
1375
1376 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1377 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1378 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1379 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1380 *first_unused = 0;
1381 return 0;
1382 }
1383 ifp = XFS_IFORK_PTR(ip, whichfork);
1384 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1385 (error = xfs_iread_extents(tp, ip, whichfork)))
1386 return error;
1387 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001388 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001389 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1390 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1391 off = xfs_bmbt_get_startoff(ep);
1392 /*
1393 * See if the hole before this extent will work.
1394 */
1395 if (off >= lowest + len && off - max >= len) {
1396 *first_unused = max;
1397 return 0;
1398 }
1399 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1400 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1401 }
1402 *first_unused = max;
1403 return 0;
1404}
1405
1406/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001407 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001408 * last_block (input value) in the file.
1409 * This is not based on i_size, it is based on the extent records.
1410 * Returns 0 for local files, as they do not have extent records.
1411 */
1412int /* error */
1413xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001414 struct xfs_trans *tp, /* transaction pointer */
1415 struct xfs_inode *ip, /* incore inode */
1416 xfs_fileoff_t *last_block, /* last block */
1417 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001418{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001419 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1420 struct xfs_bmbt_irec got;
1421 xfs_extnum_t idx;
1422 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001423
Christoph Hellwig86685f72016-11-24 11:39:38 +11001424 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1425 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001426 *last_block = 0;
1427 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001428 case XFS_DINODE_FMT_BTREE:
1429 case XFS_DINODE_FMT_EXTENTS:
1430 break;
1431 default:
1432 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001433 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001434
1435 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1436 error = xfs_iread_extents(tp, ip, whichfork);
1437 if (error)
1438 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001439 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001440
1441 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1442 if (got.br_startoff <= *last_block - 1)
1443 return 0;
1444 }
1445
1446 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1447 *last_block = got.br_startoff + got.br_blockcount;
1448 return 0;
1449 }
1450
1451 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001452 return 0;
1453}
1454
Dave Chinner68988112013-08-12 20:49:42 +10001455int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001456xfs_bmap_last_extent(
1457 struct xfs_trans *tp,
1458 struct xfs_inode *ip,
1459 int whichfork,
1460 struct xfs_bmbt_irec *rec,
1461 int *is_empty)
1462{
1463 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1464 int error;
1465 int nextents;
1466
1467 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1468 error = xfs_iread_extents(tp, ip, whichfork);
1469 if (error)
1470 return error;
1471 }
1472
Eric Sandeen5d829302016-11-08 12:59:42 +11001473 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001474 if (nextents == 0) {
1475 *is_empty = 1;
1476 return 0;
1477 }
1478
1479 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1480 *is_empty = 0;
1481 return 0;
1482}
1483
1484/*
1485 * Check the last inode extent to determine whether this allocation will result
1486 * in blocks being allocated at the end of the file. When we allocate new data
1487 * blocks at the end of the file which do not start at the previous data block,
1488 * we will try to align the new blocks at stripe unit boundaries.
1489 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001490 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001491 * at, or past the EOF.
1492 */
1493STATIC int
1494xfs_bmap_isaeof(
1495 struct xfs_bmalloca *bma,
1496 int whichfork)
1497{
1498 struct xfs_bmbt_irec rec;
1499 int is_empty;
1500 int error;
1501
1502 bma->aeof = 0;
1503 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1504 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001505 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001506 return error;
1507
Dave Chinner6e708bc2013-11-22 10:41:16 +11001508 if (is_empty) {
1509 bma->aeof = 1;
1510 return 0;
1511 }
1512
Dave Chinner9e5987a2013-02-25 12:31:26 +11001513 /*
1514 * Check if we are allocation or past the last extent, or at least into
1515 * the last delayed allocated extent.
1516 */
1517 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1518 (bma->offset >= rec.br_startoff &&
1519 isnullstartblock(rec.br_startblock));
1520 return 0;
1521}
1522
1523/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001524 * Returns the file-relative block number of the first block past eof in
1525 * the file. This is not based on i_size, it is based on the extent records.
1526 * Returns 0 for local files, as they do not have extent records.
1527 */
1528int
1529xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001530 struct xfs_inode *ip,
1531 xfs_fileoff_t *last_block,
1532 int whichfork)
1533{
1534 struct xfs_bmbt_irec rec;
1535 int is_empty;
1536 int error;
1537
1538 *last_block = 0;
1539
1540 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1541 return 0;
1542
1543 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1544 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001545 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001546
1547 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1548 if (error || is_empty)
1549 return error;
1550
1551 *last_block = rec.br_startoff + rec.br_blockcount;
1552 return 0;
1553}
1554
1555/*
1556 * Returns whether the selected fork of the inode has exactly one
1557 * block or not. For the data fork we check this matches di_size,
1558 * implying the file's range is 0..bsize-1.
1559 */
1560int /* 1=>1 block, 0=>otherwise */
1561xfs_bmap_one_block(
1562 xfs_inode_t *ip, /* incore inode */
1563 int whichfork) /* data or attr fork */
1564{
1565 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1566 xfs_ifork_t *ifp; /* inode fork pointer */
1567 int rval; /* return value */
1568 xfs_bmbt_irec_t s; /* internal version of extent */
1569
1570#ifndef DEBUG
1571 if (whichfork == XFS_DATA_FORK)
1572 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1573#endif /* !DEBUG */
1574 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1575 return 0;
1576 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1577 return 0;
1578 ifp = XFS_IFORK_PTR(ip, whichfork);
1579 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1580 ep = xfs_iext_get_ext(ifp, 0);
1581 xfs_bmbt_get_all(ep, &s);
1582 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1583 if (rval && whichfork == XFS_DATA_FORK)
1584 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1585 return rval;
1586}
1587
1588/*
1589 * Extent tree manipulation functions used during allocation.
1590 */
1591
1592/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001593 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 */
1595STATIC int /* error */
1596xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001597 struct xfs_bmalloca *bma,
1598 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001600 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001602 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001605 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 xfs_fileoff_t new_endoff; /* end offset of new entry */
1607 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1608 /* left is 0, right is 1, prev is 2 */
1609 int rval=0; /* return value (logging flags) */
1610 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001611 xfs_filblks_t da_new; /* new count del alloc blocks used */
1612 xfs_filblks_t da_old; /* old count del alloc blocks used */
1613 xfs_filblks_t temp=0; /* value for da_new calculations */
1614 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001616 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001617 xfs_extnum_t *nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001619 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001620 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001621 ASSERT(whichfork != XFS_ATTR_FORK);
1622 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1623 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001624
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001625 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001626 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001627 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001628 ASSERT(!bma->cur ||
1629 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001630
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001631 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001632
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633#define LEFT r[0]
1634#define RIGHT r[1]
1635#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Darrick J. Wong60b49842016-10-03 09:11:34 -07001637 if (whichfork == XFS_COW_FORK)
1638 state |= BMAP_COWFORK;
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /*
1641 * Set up a bunch of variables to make the tests simpler.
1642 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001643 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 xfs_bmbt_get_all(ep, &PREV);
1645 new_endoff = new->br_startoff + new->br_blockcount;
1646 ASSERT(PREV.br_startoff <= new->br_startoff);
1647 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001648
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001649 da_old = startblockval(PREV.br_startblock);
1650 da_new = 0;
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /*
1653 * Set flags determining what part of the previous delayed allocation
1654 * extent is being replaced by a real allocation.
1655 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001656 if (PREV.br_startoff == new->br_startoff)
1657 state |= BMAP_LEFT_FILLING;
1658 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1659 state |= BMAP_RIGHT_FILLING;
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 /*
1662 * Check and set flags if this segment has a left neighbor.
1663 * Don't set contiguous if the combined extent would be too large.
1664 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001665 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001666 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001667 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001668
1669 if (isnullstartblock(LEFT.br_startblock))
1670 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001672
1673 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1674 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1675 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1676 LEFT.br_state == new->br_state &&
1677 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1678 state |= BMAP_LEFT_CONTIG;
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 /*
1681 * Check and set flags if this segment has a right neighbor.
1682 * Don't set contiguous if the combined extent would be too large.
1683 * Also check for all-three-contiguous being too large.
1684 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001685 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001686 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001687 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001688
1689 if (isnullstartblock(RIGHT.br_startblock))
1690 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001692
1693 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1694 new_endoff == RIGHT.br_startoff &&
1695 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1696 new->br_state == RIGHT.br_state &&
1697 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1698 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1699 BMAP_RIGHT_FILLING)) !=
1700 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1701 BMAP_RIGHT_FILLING) ||
1702 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1703 <= MAXEXTLEN))
1704 state |= BMAP_RIGHT_CONTIG;
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 error = 0;
1707 /*
1708 * Switch out based on the FILLING and CONTIG state bits.
1709 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001710 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1711 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1712 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1713 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 /*
1715 * Filling in all of a previously delayed allocation extent.
1716 * The left and right neighbors are both contiguous with new.
1717 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001718 bma->idx--;
1719 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1720 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 LEFT.br_blockcount + PREV.br_blockcount +
1722 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001723 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001724
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001725 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001726 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001727 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1729 else {
1730 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001731 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001733 RIGHT.br_blockcount, &i);
1734 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001736 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001737 error = xfs_btree_delete(bma->cur, &i);
1738 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001740 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001741 error = xfs_btree_decrement(bma->cur, 0, &i);
1742 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001744 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001745 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 LEFT.br_startblock,
1747 LEFT.br_blockcount +
1748 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001749 RIGHT.br_blockcount, LEFT.br_state);
1750 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 goto done;
1752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 break;
1754
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001755 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /*
1757 * Filling in all of a previously delayed allocation extent.
1758 * The left neighbor is contiguous, the right is not.
1759 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001760 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001761
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001762 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1763 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001764 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001765 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001766
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001767 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1768 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 rval = XFS_ILOG_DEXT;
1770 else {
1771 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001772 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001774 &i);
1775 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001777 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001778 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 LEFT.br_startblock,
1780 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001781 PREV.br_blockcount, LEFT.br_state);
1782 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 goto done;
1784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 break;
1786
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001787 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 /*
1789 * Filling in all of a previously delayed allocation extent.
1790 * The right neighbor is contiguous, the left is not.
1791 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001792 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 xfs_bmbt_set_startblock(ep, new->br_startblock);
1794 xfs_bmbt_set_blockcount(ep,
1795 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001796 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001797
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001798 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1799 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 rval = XFS_ILOG_DEXT;
1801 else {
1802 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001803 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001805 RIGHT.br_blockcount, &i);
1806 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001808 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001809 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 new->br_startblock,
1811 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001812 RIGHT.br_blockcount, PREV.br_state);
1813 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 goto done;
1815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 break;
1817
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001818 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 /*
1820 * Filling in all of a previously delayed allocation extent.
1821 * Neither the left nor right neighbors are contiguous with
1822 * the new one.
1823 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001824 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 xfs_bmbt_set_startblock(ep, new->br_startblock);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08001826 xfs_bmbt_set_state(ep, new->br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001827 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001828
Darrick J. Wong60b49842016-10-03 09:11:34 -07001829 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001830 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1832 else {
1833 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001834 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001836 &i);
1837 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001839 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001840 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1841 error = xfs_btree_insert(bma->cur, &i);
1842 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001844 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 break;
1847
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001848 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 /*
1850 * Filling in the first part of a previous delayed allocation.
1851 * The left neighbor is contiguous.
1852 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001853 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1854 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 LEFT.br_blockcount + new->br_blockcount);
1856 xfs_bmbt_set_startoff(ep,
1857 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001858 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001861 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001863 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 rval = XFS_ILOG_DEXT;
1865 else {
1866 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001867 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001869 &i);
1870 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001872 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001873 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 LEFT.br_startblock,
1875 LEFT.br_blockcount +
1876 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001877 LEFT.br_state);
1878 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 goto done;
1880 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001881 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001882 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001883 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001884 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001885
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001886 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 break;
1888
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001889 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 /*
1891 * Filling in the first part of a previous delayed allocation.
1892 * The left neighbor is not contiguous.
1893 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001894 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 xfs_bmbt_set_startoff(ep, new_endoff);
1896 temp = PREV.br_blockcount - new->br_blockcount;
1897 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001898 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001899 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001900 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1902 else {
1903 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001904 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 new->br_startblock, new->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 == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001910 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1911 error = xfs_btree_insert(bma->cur, &i);
1912 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001914 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001916
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001917 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001918 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001919 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001920 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 rval |= tmp_rval;
1922 if (error)
1923 goto done;
1924 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001925 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001926 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001927 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1928 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001929 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001930 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 break;
1932
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001933 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 /*
1935 * Filling in the last part of a previous delayed allocation.
1936 * The right neighbor is contiguous with the new allocation.
1937 */
1938 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001939 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001941 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001942 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 new->br_blockcount + RIGHT.br_blockcount,
1944 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001945 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1946 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 rval = XFS_ILOG_DEXT;
1948 else {
1949 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001950 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001952 RIGHT.br_blockcount, &i);
1953 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001955 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001956 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 new->br_startblock,
1958 new->br_blockcount +
1959 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001960 RIGHT.br_state);
1961 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 goto done;
1963 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001964
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001965 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001966 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001967 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001968 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001969 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001970
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001971 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 break;
1973
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001974 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 /*
1976 * Filling in the last part of a previous delayed allocation.
1977 * The right neighbor is not contiguous.
1978 */
1979 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001980 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001982 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001983 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001984 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1986 else {
1987 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001988 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001990 &i);
1991 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001993 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001994 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1995 error = xfs_btree_insert(bma->cur, &i);
1996 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001998 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002000
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002001 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002002 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002003 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002004 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 rval |= tmp_rval;
2006 if (error)
2007 goto done;
2008 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002009 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002010 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002011 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2012 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002013 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002014 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002015
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002016 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 break;
2018
2019 case 0:
2020 /*
2021 * Filling in the middle part of a previous delayed allocation.
2022 * Contiguity is impossible here.
2023 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002024 *
2025 * We start with a delayed allocation:
2026 *
2027 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2028 * PREV @ idx
2029 *
2030 * and we are allocating:
2031 * +rrrrrrrrrrrrrrrrr+
2032 * new
2033 *
2034 * and we set it up for insertion as:
2035 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2036 * new
2037 * PREV @ idx LEFT RIGHT
2038 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 */
2040 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002042 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002043 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2044 LEFT = *new;
2045 RIGHT.br_state = PREV.br_state;
2046 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002047 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002048 RIGHT.br_startoff = new_endoff;
2049 RIGHT.br_blockcount = temp2;
2050 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002051 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002052 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002053 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2055 else {
2056 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002057 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002059 &i);
2060 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002062 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002063 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2064 error = xfs_btree_insert(bma->cur, &i);
2065 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002067 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002069
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002070 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002071 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002072 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002073 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 rval |= tmp_rval;
2075 if (error)
2076 goto done;
2077 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002078 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2079 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002080 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002081 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002082 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002083 error = xfs_mod_fdblocks(bma->ip->i_mount,
2084 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002085 ASSERT(!error);
2086 if (error)
2087 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002089
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002090 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002091 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002092 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2093 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2094 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002095 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002096 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002097
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002098 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002099 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 break;
2101
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002102 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2103 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2104 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2105 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2106 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2107 case BMAP_LEFT_CONTIG:
2108 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 /*
2110 * These cases are all impossible.
2111 */
2112 ASSERT(0);
2113 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002114
Darrick J. Wong9c194642016-08-03 12:16:05 +10002115 /* add reverse mapping */
2116 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2117 if (error)
2118 goto done;
2119
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002120 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002121 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002122 int tmp_logflags; /* partial log flag return val */
2123
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002124 ASSERT(bma->cur == NULL);
2125 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002126 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002127 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002128 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002129 if (error)
2130 goto done;
2131 }
2132
2133 /* adjust for changes in reserved delayed indirect blocks */
2134 if (da_old || da_new) {
2135 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002136 if (bma->cur)
2137 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002138 ASSERT(temp <= da_old);
2139 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002140 xfs_mod_fdblocks(bma->ip->i_mount,
2141 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002142 }
2143
2144 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002145 if (bma->cur)
2146 bma->cur->bc_private.b.allocated = 0;
2147
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002148 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002150 if (whichfork != XFS_COW_FORK)
2151 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return error;
2153#undef LEFT
2154#undef RIGHT
2155#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156}
2157
2158/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002159 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 */
2161STATIC int /* error */
2162xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002163 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002165 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002166 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002168 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002169 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002170 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002171 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002174 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002177 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 xfs_fileoff_t new_endoff; /* end offset of new entry */
2179 xfs_exntst_t newext; /* new extent state */
2180 xfs_exntst_t oldext; /* old extent state */
2181 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2182 /* left is 0, right is 1, prev is 2 */
2183 int rval=0; /* return value (logging flags) */
2184 int state = 0;/* state bits, accessed thru macros */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002185 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002187 *logflagsp = 0;
2188
2189 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002190 ifp = XFS_IFORK_PTR(ip, whichfork);
2191 if (whichfork == XFS_COW_FORK)
2192 state |= BMAP_COWFORK;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002193
2194 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002195 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002196 ASSERT(!isnullstartblock(new->br_startblock));
2197
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002198 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200#define LEFT r[0]
2201#define RIGHT r[1]
2202#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /*
2205 * Set up a bunch of variables to make the tests simpler.
2206 */
2207 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002208 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 xfs_bmbt_get_all(ep, &PREV);
2210 newext = new->br_state;
2211 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2212 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2213 ASSERT(PREV.br_state == oldext);
2214 new_endoff = new->br_startoff + new->br_blockcount;
2215 ASSERT(PREV.br_startoff <= new->br_startoff);
2216 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /*
2219 * Set flags determining what part of the previous oldext allocation
2220 * extent is being replaced by a newext allocation.
2221 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002222 if (PREV.br_startoff == new->br_startoff)
2223 state |= BMAP_LEFT_FILLING;
2224 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2225 state |= BMAP_RIGHT_FILLING;
2226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /*
2228 * Check and set flags if this segment has a left neighbor.
2229 * Don't set contiguous if the combined extent would be too large.
2230 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002231 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002232 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002233 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002234
2235 if (isnullstartblock(LEFT.br_startblock))
2236 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002238
2239 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2240 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2241 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2242 LEFT.br_state == newext &&
2243 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2244 state |= BMAP_LEFT_CONTIG;
2245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 /*
2247 * Check and set flags if this segment has a right neighbor.
2248 * Don't set contiguous if the combined extent would be too large.
2249 * Also check for all-three-contiguous being too large.
2250 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002251 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002252 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002253 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002254 if (isnullstartblock(RIGHT.br_startblock))
2255 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002257
2258 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2259 new_endoff == RIGHT.br_startoff &&
2260 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2261 newext == RIGHT.br_state &&
2262 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2263 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2264 BMAP_RIGHT_FILLING)) !=
2265 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2266 BMAP_RIGHT_FILLING) ||
2267 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2268 <= MAXEXTLEN))
2269 state |= BMAP_RIGHT_CONTIG;
2270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /*
2272 * Switch out based on the FILLING and CONTIG state bits.
2273 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002274 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2275 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2276 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2277 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 /*
2279 * Setting all of a previous oldext extent to newext.
2280 * The left and right neighbors are both contiguous with new.
2281 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002282 --*idx;
2283
2284 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2285 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 LEFT.br_blockcount + PREV.br_blockcount +
2287 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002288 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002289
Christoph Hellwigec90c552011-05-23 08:52:53 +00002290 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002291 XFS_IFORK_NEXT_SET(ip, whichfork,
2292 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (cur == NULL)
2294 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2295 else {
2296 rval = XFS_ILOG_CORE;
2297 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2298 RIGHT.br_startblock,
2299 RIGHT.br_blockcount, &i)))
2300 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002301 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002302 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002304 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002305 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002307 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002308 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002310 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002311 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002313 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2315 LEFT.br_startblock,
2316 LEFT.br_blockcount + PREV.br_blockcount +
2317 RIGHT.br_blockcount, LEFT.br_state)))
2318 goto done;
2319 }
2320 break;
2321
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002322 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 /*
2324 * Setting all of a previous oldext extent to newext.
2325 * The left neighbor is contiguous, the right is not.
2326 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002327 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002328
Christoph Hellwigec90c552011-05-23 08:52:53 +00002329 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2330 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2331 LEFT.br_blockcount + PREV.br_blockcount);
2332 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2333
2334 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002335 XFS_IFORK_NEXT_SET(ip, whichfork,
2336 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (cur == NULL)
2338 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2339 else {
2340 rval = XFS_ILOG_CORE;
2341 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2342 PREV.br_startblock, PREV.br_blockcount,
2343 &i)))
2344 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002345 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002346 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002348 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002349 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002351 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2353 LEFT.br_startblock,
2354 LEFT.br_blockcount + PREV.br_blockcount,
2355 LEFT.br_state)))
2356 goto done;
2357 }
2358 break;
2359
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002360 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 /*
2362 * Setting all of a previous oldext extent to newext.
2363 * The right neighbor is contiguous, the left is not.
2364 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002365 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 xfs_bmbt_set_blockcount(ep,
2367 PREV.br_blockcount + RIGHT.br_blockcount);
2368 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002369 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2370 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002371 XFS_IFORK_NEXT_SET(ip, whichfork,
2372 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (cur == NULL)
2374 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2375 else {
2376 rval = XFS_ILOG_CORE;
2377 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2378 RIGHT.br_startblock,
2379 RIGHT.br_blockcount, &i)))
2380 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002381 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002382 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002384 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002385 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002387 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2389 new->br_startblock,
2390 new->br_blockcount + RIGHT.br_blockcount,
2391 newext)))
2392 goto done;
2393 }
2394 break;
2395
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002396 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 /*
2398 * Setting all of a previous oldext extent to newext.
2399 * Neither the left nor right neighbors are contiguous with
2400 * the new one.
2401 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002402 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002404 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 if (cur == NULL)
2407 rval = XFS_ILOG_DEXT;
2408 else {
2409 rval = 0;
2410 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2411 new->br_startblock, new->br_blockcount,
2412 &i)))
2413 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002414 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2416 new->br_startblock, new->br_blockcount,
2417 newext)))
2418 goto done;
2419 }
2420 break;
2421
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002422 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 /*
2424 * Setting the first part of a previous oldext extent to newext.
2425 * The left neighbor is contiguous.
2426 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002427 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2428 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 LEFT.br_blockcount + new->br_blockcount);
2430 xfs_bmbt_set_startoff(ep,
2431 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002432 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002433
Christoph Hellwigec90c552011-05-23 08:52:53 +00002434 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 xfs_bmbt_set_startblock(ep,
2436 new->br_startblock + new->br_blockcount);
2437 xfs_bmbt_set_blockcount(ep,
2438 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002439 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002440
Christoph Hellwigec90c552011-05-23 08:52:53 +00002441 --*idx;
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 if (cur == NULL)
2444 rval = XFS_ILOG_DEXT;
2445 else {
2446 rval = 0;
2447 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2448 PREV.br_startblock, PREV.br_blockcount,
2449 &i)))
2450 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002451 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 if ((error = xfs_bmbt_update(cur,
2453 PREV.br_startoff + new->br_blockcount,
2454 PREV.br_startblock + new->br_blockcount,
2455 PREV.br_blockcount - new->br_blockcount,
2456 oldext)))
2457 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002458 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002460 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 LEFT.br_startblock,
2462 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002463 LEFT.br_state);
2464 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 goto done;
2466 }
2467 break;
2468
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002469 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 /*
2471 * Setting the first part of a previous oldext extent to newext.
2472 * The left neighbor is not contiguous.
2473 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002474 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2476 xfs_bmbt_set_startoff(ep, new_endoff);
2477 xfs_bmbt_set_blockcount(ep,
2478 PREV.br_blockcount - new->br_blockcount);
2479 xfs_bmbt_set_startblock(ep,
2480 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002481 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002482
Christoph Hellwigec90c552011-05-23 08:52:53 +00002483 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002484 XFS_IFORK_NEXT_SET(ip, whichfork,
2485 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 if (cur == NULL)
2487 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2488 else {
2489 rval = XFS_ILOG_CORE;
2490 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2491 PREV.br_startblock, PREV.br_blockcount,
2492 &i)))
2493 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002494 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 if ((error = xfs_bmbt_update(cur,
2496 PREV.br_startoff + new->br_blockcount,
2497 PREV.br_startblock + new->br_blockcount,
2498 PREV.br_blockcount - new->br_blockcount,
2499 oldext)))
2500 goto done;
2501 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002502 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002504 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 }
2506 break;
2507
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002508 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 /*
2510 * Setting the last part of a previous oldext extent to newext.
2511 * The right neighbor is contiguous with the new allocation.
2512 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002513 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 xfs_bmbt_set_blockcount(ep,
2515 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002516 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2517
2518 ++*idx;
2519
2520 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2521 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002522 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002524 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (cur == NULL)
2527 rval = XFS_ILOG_DEXT;
2528 else {
2529 rval = 0;
2530 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2531 PREV.br_startblock,
2532 PREV.br_blockcount, &i)))
2533 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 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2536 PREV.br_startblock,
2537 PREV.br_blockcount - new->br_blockcount,
2538 oldext)))
2539 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002540 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 goto done;
2542 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2543 new->br_startblock,
2544 new->br_blockcount + RIGHT.br_blockcount,
2545 newext)))
2546 goto done;
2547 }
2548 break;
2549
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002550 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 /*
2552 * Setting the last part of a previous oldext extent to newext.
2553 * The right neighbor is not contiguous.
2554 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002555 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 xfs_bmbt_set_blockcount(ep,
2557 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002558 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002559
Christoph Hellwigec90c552011-05-23 08:52:53 +00002560 ++*idx;
2561 xfs_iext_insert(ip, *idx, 1, new, state);
2562
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002563 XFS_IFORK_NEXT_SET(ip, whichfork,
2564 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (cur == NULL)
2566 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2567 else {
2568 rval = XFS_ILOG_CORE;
2569 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2570 PREV.br_startblock, PREV.br_blockcount,
2571 &i)))
2572 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002573 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2575 PREV.br_startblock,
2576 PREV.br_blockcount - new->br_blockcount,
2577 oldext)))
2578 goto done;
2579 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2580 new->br_startblock, new->br_blockcount,
2581 &i)))
2582 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002583 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002585 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002587 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 }
2589 break;
2590
2591 case 0:
2592 /*
2593 * Setting the middle part of a previous oldext extent to
2594 * newext. Contiguity is impossible here.
2595 * One extent becomes three extents.
2596 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002597 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 xfs_bmbt_set_blockcount(ep,
2599 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002600 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 r[0] = *new;
2603 r[1].br_startoff = new_endoff;
2604 r[1].br_blockcount =
2605 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2606 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2607 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002608
2609 ++*idx;
2610 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2611
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002612 XFS_IFORK_NEXT_SET(ip, whichfork,
2613 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 if (cur == NULL)
2615 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2616 else {
2617 rval = XFS_ILOG_CORE;
2618 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2619 PREV.br_startblock, PREV.br_blockcount,
2620 &i)))
2621 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002622 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 /* new right extent - oldext */
2624 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2625 r[1].br_startblock, r[1].br_blockcount,
2626 r[1].br_state)))
2627 goto done;
2628 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002630 cur->bc_rec.b.br_blockcount =
2631 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002632 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002634 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002635 /*
2636 * Reset the cursor to the position of the new extent
2637 * we are about to insert as we can't trust it after
2638 * the previous insert.
2639 */
2640 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2641 new->br_startblock, new->br_blockcount,
2642 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002644 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002646 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002647 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002649 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 }
2651 break;
2652
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002653 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2654 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2655 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2656 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2657 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2658 case BMAP_LEFT_CONTIG:
2659 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 /*
2661 * These cases are all impossible.
2662 */
2663 ASSERT(0);
2664 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002665
Darrick J. Wong9c194642016-08-03 12:16:05 +10002666 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002667 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002668 if (error)
2669 goto done;
2670
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002671 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002672 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002673 int tmp_logflags; /* partial log flag return val */
2674
2675 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002676 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002677 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002678 *logflagsp |= tmp_logflags;
2679 if (error)
2680 goto done;
2681 }
2682
2683 /* clear out the allocated field, done with it now in any case. */
2684 if (cur) {
2685 cur->bc_private.b.allocated = 0;
2686 *curp = cur;
2687 }
2688
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002689 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002691 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return error;
2693#undef LEFT
2694#undef RIGHT
2695#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
2697
2698/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002699 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002701STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702xfs_bmap_add_extent_hole_delay(
2703 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002704 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002705 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002706 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002708 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2710 xfs_filblks_t newlen=0; /* new indirect size */
2711 xfs_filblks_t oldlen=0; /* old indirect size */
2712 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2713 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002714 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002716 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002718 if (whichfork == XFS_COW_FORK)
2719 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002720 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 /*
2723 * Check and set flags if this segment has a left neighbor
2724 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002725 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002726 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002727 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002728
2729 if (isnullstartblock(left.br_startblock))
2730 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002732
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 /*
2734 * Check and set flags if the current (right) segment exists.
2735 * If it doesn't exist, we're converting the hole at end-of-file.
2736 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002737 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002738 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002739 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002740
2741 if (isnullstartblock(right.br_startblock))
2742 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002744
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 /*
2746 * Set contiguity flags on the left and right neighbors.
2747 * Don't let extents get too large, even if the pieces are contiguous.
2748 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002749 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2750 left.br_startoff + left.br_blockcount == new->br_startoff &&
2751 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2752 state |= BMAP_LEFT_CONTIG;
2753
2754 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2755 new->br_startoff + new->br_blockcount == right.br_startoff &&
2756 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2757 (!(state & BMAP_LEFT_CONTIG) ||
2758 (left.br_blockcount + new->br_blockcount +
2759 right.br_blockcount <= MAXEXTLEN)))
2760 state |= BMAP_RIGHT_CONTIG;
2761
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 /*
2763 * Switch out based on the contiguity flags.
2764 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002765 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2766 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 /*
2768 * New allocation is contiguous with delayed allocations
2769 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002770 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002772 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 temp = left.br_blockcount + new->br_blockcount +
2774 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002775
Christoph Hellwigec90c552011-05-23 08:52:53 +00002776 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2777 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002778 oldlen = startblockval(left.br_startblock) +
2779 startblockval(new->br_startblock) +
2780 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002781 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2782 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002783 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002784 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002785 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002786
Christoph Hellwigec90c552011-05-23 08:52:53 +00002787 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 break;
2789
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002790 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 /*
2792 * New allocation is contiguous with a delayed allocation
2793 * on the left.
2794 * Merge the new allocation with the left neighbor.
2795 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002796 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002798
2799 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2800 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002801 oldlen = startblockval(left.br_startblock) +
2802 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002803 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2804 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002805 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002806 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002807 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 break;
2809
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002810 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 /*
2812 * New allocation is contiguous with a delayed allocation
2813 * on the right.
2814 * Merge the new allocation with the right neighbor.
2815 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002816 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002818 oldlen = startblockval(new->br_startblock) +
2819 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002820 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2821 oldlen);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002822 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2823 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002824 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002825 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 break;
2827
2828 case 0:
2829 /*
2830 * New allocation is not contiguous with another
2831 * delayed allocation.
2832 * Insert a new entry.
2833 */
2834 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002835 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 break;
2837 }
2838 if (oldlen != newlen) {
2839 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002840 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2841 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 /*
2843 * Nothing to do for disk quota accounting here.
2844 */
2845 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
2847
2848/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002849 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 */
2851STATIC int /* error */
2852xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002853 struct xfs_trans *tp,
2854 struct xfs_inode *ip,
2855 int whichfork,
2856 xfs_extnum_t *idx,
2857 struct xfs_btree_cur **curp,
2858 struct xfs_bmbt_irec *new,
2859 xfs_fsblock_t *first,
2860 struct xfs_defer_ops *dfops,
2861 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002863 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2864 struct xfs_mount *mp = ip->i_mount;
2865 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2869 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002870 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Christoph Hellwig6d045582017-04-11 16:45:54 -07002873 ASSERT(*idx >= 0);
2874 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002875 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002876 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002877
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002878 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002879
2880 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002881 if (whichfork == XFS_ATTR_FORK)
2882 state |= BMAP_ATTRFORK;
Christoph Hellwiga14234c2017-02-06 10:50:49 -08002883 if (whichfork == XFS_COW_FORK)
2884 state |= BMAP_COWFORK;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 /*
2887 * Check and set flags if this segment has a left neighbor.
2888 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002889 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002890 state |= BMAP_LEFT_VALID;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002891 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002892 if (isnullstartblock(left.br_startblock))
2893 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002895
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 /*
2897 * Check and set flags if this segment has a current value.
2898 * Not true if we're inserting into the "hole" at eof.
2899 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002900 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002901 state |= BMAP_RIGHT_VALID;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002902 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002903 if (isnullstartblock(right.br_startblock))
2904 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 /*
2908 * We're inserting a real allocation between "left" and "right".
2909 * Set the contiguity flags. Don't let extents get too large.
2910 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002911 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2912 left.br_startoff + left.br_blockcount == new->br_startoff &&
2913 left.br_startblock + left.br_blockcount == new->br_startblock &&
2914 left.br_state == new->br_state &&
2915 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2916 state |= BMAP_LEFT_CONTIG;
2917
2918 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2919 new->br_startoff + new->br_blockcount == right.br_startoff &&
2920 new->br_startblock + new->br_blockcount == right.br_startblock &&
2921 new->br_state == right.br_state &&
2922 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2923 (!(state & BMAP_LEFT_CONTIG) ||
2924 left.br_blockcount + new->br_blockcount +
2925 right.br_blockcount <= MAXEXTLEN))
2926 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002928 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 /*
2930 * Select which case we're in here, and implement it.
2931 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002932 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2933 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 /*
2935 * New allocation is contiguous with real allocations on the
2936 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002937 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002939 --*idx;
2940 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2941 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 left.br_blockcount + new->br_blockcount +
2943 right.br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002944 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002945
Christoph Hellwig6d045582017-04-11 16:45:54 -07002946 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002947
Christoph Hellwig6d045582017-04-11 16:45:54 -07002948 XFS_IFORK_NEXT_SET(ip, whichfork,
2949 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2950 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002951 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002952 } else {
2953 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002954 error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002955 right.br_startblock, right.br_blockcount,
2956 &i);
2957 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002958 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002959 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002960 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002961 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002962 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002963 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002964 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002965 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002966 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002967 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002968 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002969 left.br_startblock,
2970 left.br_blockcount +
2971 new->br_blockcount +
2972 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002973 left.br_state);
2974 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002975 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002977 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002979 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 /*
2981 * New allocation is contiguous with a real allocation
2982 * on the left.
2983 * Merge the new allocation with the left neighbor.
2984 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002985 --*idx;
2986 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2987 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 left.br_blockcount + new->br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002989 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002990
Christoph Hellwig6d045582017-04-11 16:45:54 -07002991 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002992 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002993 } else {
2994 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002995 error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002996 left.br_startblock, left.br_blockcount,
2997 &i);
2998 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002999 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003000 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003001 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003002 left.br_startblock,
3003 left.br_blockcount +
3004 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003005 left.br_state);
3006 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003007 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003009 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003011 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 /*
3013 * New allocation is contiguous with a real allocation
3014 * on the right.
3015 * Merge the new allocation with the right neighbor.
3016 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003017 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
3018 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003019 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 new->br_blockcount + right.br_blockcount,
3021 right.br_state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003022 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003023
Christoph Hellwig6d045582017-04-11 16:45:54 -07003024 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003025 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003026 } else {
3027 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003028 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003029 right.br_startoff,
3030 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003031 right.br_blockcount, &i);
3032 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003033 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003034 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003035 error = xfs_bmbt_update(cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003036 new->br_startblock,
3037 new->br_blockcount +
3038 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003039 right.br_state);
3040 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003041 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003043 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045 case 0:
3046 /*
3047 * New allocation is not contiguous with another
3048 * real allocation.
3049 * Insert a new entry.
3050 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003051 xfs_iext_insert(ip, *idx, 1, new, state);
3052 XFS_IFORK_NEXT_SET(ip, whichfork,
3053 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3054 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003055 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003056 } else {
3057 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003058 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003059 new->br_startoff,
3060 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003061 new->br_blockcount, &i);
3062 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003063 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003064 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003065 cur->bc_rec.b.br_state = new->br_state;
3066 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003067 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003068 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003069 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003071 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003073
Darrick J. Wong9c194642016-08-03 12:16:05 +10003074 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003075 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10003076 if (error)
3077 goto done;
3078
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003079 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003080 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003081 int tmp_logflags; /* partial log flag return val */
3082
Christoph Hellwig6d045582017-04-11 16:45:54 -07003083 ASSERT(cur == NULL);
3084 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003085 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003086 *logflagsp |= tmp_logflags;
3087 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003088 if (error)
3089 goto done;
3090 }
3091
3092 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003093 if (cur)
3094 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003095
Christoph Hellwig6d045582017-04-11 16:45:54 -07003096 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003097done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07003098 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003099 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100}
3101
Nathan Scottdd9f4382006-01-11 15:28:28 +11003102/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003103 * Functions used in the extent read, allocate and remove paths
3104 */
3105
3106/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003107 * Adjust the size of the new extent based on di_extsize and rt extsize.
3108 */
Dave Chinner68988112013-08-12 20:49:42 +10003109int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003110xfs_bmap_extsize_align(
3111 xfs_mount_t *mp,
3112 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3113 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3114 xfs_extlen_t extsz, /* align to this extent size */
3115 int rt, /* is this a realtime inode? */
3116 int eof, /* is extent at end-of-file? */
3117 int delay, /* creating delalloc extent? */
3118 int convert, /* overwriting unwritten extent? */
3119 xfs_fileoff_t *offp, /* in/out: aligned offset */
3120 xfs_extlen_t *lenp) /* in/out: aligned length */
3121{
3122 xfs_fileoff_t orig_off; /* original offset */
3123 xfs_extlen_t orig_alen; /* original length */
3124 xfs_fileoff_t orig_end; /* original off+len */
3125 xfs_fileoff_t nexto; /* next file offset */
3126 xfs_fileoff_t prevo; /* previous file offset */
3127 xfs_fileoff_t align_off; /* temp for offset */
3128 xfs_extlen_t align_alen; /* temp for length */
3129 xfs_extlen_t temp; /* temp for calculations */
3130
3131 if (convert)
3132 return 0;
3133
3134 orig_off = align_off = *offp;
3135 orig_alen = align_alen = *lenp;
3136 orig_end = orig_off + orig_alen;
3137
3138 /*
3139 * If this request overlaps an existing extent, then don't
3140 * attempt to perform any additional alignment.
3141 */
3142 if (!delay && !eof &&
3143 (orig_off >= gotp->br_startoff) &&
3144 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3145 return 0;
3146 }
3147
3148 /*
3149 * If the file offset is unaligned vs. the extent size
3150 * we need to align it. This will be possible unless
3151 * the file was previously written with a kernel that didn't
3152 * perform this alignment, or if a truncate shot us in the
3153 * foot.
3154 */
3155 temp = do_mod(orig_off, extsz);
3156 if (temp) {
3157 align_alen += temp;
3158 align_off -= temp;
3159 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003160
3161 /* Same adjustment for the end of the requested area. */
3162 temp = (align_alen % extsz);
3163 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003164 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003165
3166 /*
3167 * For large extent hint sizes, the aligned extent might be larger than
3168 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3169 * the length back under MAXEXTLEN. The outer allocation loops handle
3170 * short allocation just fine, so it is safe to do this. We only want to
3171 * do it when we are forced to, though, because it means more allocation
3172 * operations are required.
3173 */
3174 while (align_alen > MAXEXTLEN)
3175 align_alen -= extsz;
3176 ASSERT(align_alen <= MAXEXTLEN);
3177
Nathan Scottdd9f4382006-01-11 15:28:28 +11003178 /*
3179 * If the previous block overlaps with this proposed allocation
3180 * then move the start forward without adjusting the length.
3181 */
3182 if (prevp->br_startoff != NULLFILEOFF) {
3183 if (prevp->br_startblock == HOLESTARTBLOCK)
3184 prevo = prevp->br_startoff;
3185 else
3186 prevo = prevp->br_startoff + prevp->br_blockcount;
3187 } else
3188 prevo = 0;
3189 if (align_off != orig_off && align_off < prevo)
3190 align_off = prevo;
3191 /*
3192 * If the next block overlaps with this proposed allocation
3193 * then move the start back without adjusting the length,
3194 * but not before offset 0.
3195 * This may of course make the start overlap previous block,
3196 * and if we hit the offset 0 limit then the next block
3197 * can still overlap too.
3198 */
3199 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3200 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3201 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3202 nexto = gotp->br_startoff + gotp->br_blockcount;
3203 else
3204 nexto = gotp->br_startoff;
3205 } else
3206 nexto = NULLFILEOFF;
3207 if (!eof &&
3208 align_off + align_alen != orig_end &&
3209 align_off + align_alen > nexto)
3210 align_off = nexto > align_alen ? nexto - align_alen : 0;
3211 /*
3212 * If we're now overlapping the next or previous extent that
3213 * means we can't fit an extsz piece in this hole. Just move
3214 * the start forward to the first valid spot and set
3215 * the length so we hit the end.
3216 */
3217 if (align_off != orig_off && align_off < prevo)
3218 align_off = prevo;
3219 if (align_off + align_alen != orig_end &&
3220 align_off + align_alen > nexto &&
3221 nexto != NULLFILEOFF) {
3222 ASSERT(nexto > prevo);
3223 align_alen = nexto - align_off;
3224 }
3225
3226 /*
3227 * If realtime, and the result isn't a multiple of the realtime
3228 * extent size we need to remove blocks until it is.
3229 */
3230 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3231 /*
3232 * We're not covering the original request, or
3233 * we won't be able to once we fix the length.
3234 */
3235 if (orig_off < align_off ||
3236 orig_end > align_off + align_alen ||
3237 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003238 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003239 /*
3240 * Try to fix it by moving the start up.
3241 */
3242 if (align_off + temp <= orig_off) {
3243 align_alen -= temp;
3244 align_off += temp;
3245 }
3246 /*
3247 * Try to fix it by moving the end in.
3248 */
3249 else if (align_off + align_alen - temp >= orig_end)
3250 align_alen -= temp;
3251 /*
3252 * Set the start to the minimum then trim the length.
3253 */
3254 else {
3255 align_alen -= orig_off - align_off;
3256 align_off = orig_off;
3257 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3258 }
3259 /*
3260 * Result doesn't cover the request, fail it.
3261 */
3262 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003263 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003264 } else {
3265 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003266 /* see MAXEXTLEN handling above */
3267 ASSERT(orig_end <= align_off + align_alen ||
3268 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003269 }
3270
3271#ifdef DEBUG
3272 if (!eof && gotp->br_startoff != NULLFILEOFF)
3273 ASSERT(align_off + align_alen <= gotp->br_startoff);
3274 if (prevp->br_startoff != NULLFILEOFF)
3275 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3276#endif
3277
3278 *lenp = align_alen;
3279 *offp = align_off;
3280 return 0;
3281}
3282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283#define XFS_ALLOC_GAP_UNITS 4
3284
Dave Chinner68988112013-08-12 20:49:42 +10003285void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003286xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003287 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288{
3289 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3291 xfs_mount_t *mp; /* mount point structure */
3292 int nullfb; /* true if ap->firstblock isn't set */
3293 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294
3295#define ISVALID(x,y) \
3296 (rt ? \
3297 (x) < mp->m_sb.sb_rblocks : \
3298 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3299 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3300 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003303 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003304 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3305 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003306 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 /*
3308 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003309 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003311 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3312 !isnullstartblock(ap->prev.br_startblock) &&
3313 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3314 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003315 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 /*
3317 * Adjust for the gap between prevp and us.
3318 */
Dave Chinner3a756672011-09-18 20:40:58 +00003319 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003320 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003322 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3323 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 }
3325 /*
3326 * If not at eof, then compare the two neighbor blocks.
3327 * Figure out whether either one gives us a good starting point,
3328 * and pick the better one.
3329 */
3330 else if (!ap->eof) {
3331 xfs_fsblock_t gotbno; /* right side block number */
3332 xfs_fsblock_t gotdiff=0; /* right side difference */
3333 xfs_fsblock_t prevbno; /* left side block number */
3334 xfs_fsblock_t prevdiff=0; /* left side difference */
3335
3336 /*
3337 * If there's a previous (left) block, select a requested
3338 * start block based on it.
3339 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003340 if (ap->prev.br_startoff != NULLFILEOFF &&
3341 !isnullstartblock(ap->prev.br_startblock) &&
3342 (prevbno = ap->prev.br_startblock +
3343 ap->prev.br_blockcount) &&
3344 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 /*
3346 * Calculate gap to end of previous block.
3347 */
Dave Chinner3a756672011-09-18 20:40:58 +00003348 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003349 (ap->prev.br_startoff +
3350 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 /*
3352 * Figure the startblock based on the previous block's
3353 * end and the gap size.
3354 * Heuristic!
3355 * If the gap is large relative to the piece we're
3356 * allocating, or using it gives us an invalid block
3357 * number, then just use the end of the previous block.
3358 */
Dave Chinner3a756672011-09-18 20:40:58 +00003359 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003361 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 prevbno += adjust;
3363 else
3364 prevdiff += adjust;
3365 /*
3366 * If the firstblock forbids it, can't use it,
3367 * must use default.
3368 */
3369 if (!rt && !nullfb &&
3370 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3371 prevbno = NULLFSBLOCK;
3372 }
3373 /*
3374 * No previous block or can't follow it, just default.
3375 */
3376 else
3377 prevbno = NULLFSBLOCK;
3378 /*
3379 * If there's a following (right) block, select a requested
3380 * start block based on it.
3381 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003382 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 /*
3384 * Calculate gap to start of next block.
3385 */
Dave Chinner3a756672011-09-18 20:40:58 +00003386 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 /*
3388 * Figure the startblock based on the next block's
3389 * start and the gap size.
3390 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003391 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 /*
3393 * Heuristic!
3394 * If the gap is large relative to the piece we're
3395 * allocating, or using it gives us an invalid block
3396 * number, then just use the start of the next block
3397 * offset by our length.
3398 */
Dave Chinner3a756672011-09-18 20:40:58 +00003399 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 ISVALID(gotbno - gotdiff, gotbno))
3401 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003402 else if (ISVALID(gotbno - ap->length, gotbno)) {
3403 gotbno -= ap->length;
3404 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 } else
3406 gotdiff += adjust;
3407 /*
3408 * If the firstblock forbids it, can't use it,
3409 * must use default.
3410 */
3411 if (!rt && !nullfb &&
3412 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3413 gotbno = NULLFSBLOCK;
3414 }
3415 /*
3416 * No next block, just default.
3417 */
3418 else
3419 gotbno = NULLFSBLOCK;
3420 /*
3421 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003422 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 */
3424 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003425 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003427 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003429 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003431#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003432}
3433
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003434static int
3435xfs_bmap_longest_free_extent(
3436 struct xfs_trans *tp,
3437 xfs_agnumber_t ag,
3438 xfs_extlen_t *blen,
3439 int *notinit)
3440{
3441 struct xfs_mount *mp = tp->t_mountp;
3442 struct xfs_perag *pag;
3443 xfs_extlen_t longest;
3444 int error = 0;
3445
3446 pag = xfs_perag_get(mp, ag);
3447 if (!pag->pagf_init) {
3448 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3449 if (error)
3450 goto out;
3451
3452 if (!pag->pagf_init) {
3453 *notinit = 1;
3454 goto out;
3455 }
3456 }
3457
Dave Chinner50adbcb2015-06-22 10:04:31 +10003458 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003459 xfs_alloc_min_freelist(mp, pag),
3460 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003461 if (*blen < longest)
3462 *blen = longest;
3463
3464out:
3465 xfs_perag_put(pag);
3466 return error;
3467}
3468
3469static void
3470xfs_bmap_select_minlen(
3471 struct xfs_bmalloca *ap,
3472 struct xfs_alloc_arg *args,
3473 xfs_extlen_t *blen,
3474 int notinit)
3475{
3476 if (notinit || *blen < ap->minlen) {
3477 /*
3478 * Since we did a BUF_TRYLOCK above, it is possible that
3479 * there is space for this request.
3480 */
3481 args->minlen = ap->minlen;
3482 } else if (*blen < args->maxlen) {
3483 /*
3484 * If the best seen length is less than the request length,
3485 * use the best as the minimum.
3486 */
3487 args->minlen = *blen;
3488 } else {
3489 /*
3490 * Otherwise we've seen an extent as big as maxlen, use that
3491 * as the minimum.
3492 */
3493 args->minlen = args->maxlen;
3494 }
3495}
3496
Nathan Scotta365bdd2006-03-14 13:34:16 +11003497STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003498xfs_bmap_btalloc_nullfb(
3499 struct xfs_bmalloca *ap,
3500 struct xfs_alloc_arg *args,
3501 xfs_extlen_t *blen)
3502{
3503 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003504 xfs_agnumber_t ag, startag;
3505 int notinit = 0;
3506 int error;
3507
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003508 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003509 args->total = ap->total;
3510
Christoph Hellwigc467c042010-02-15 23:34:42 +00003511 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3512 if (startag == NULLAGNUMBER)
3513 startag = ag = 0;
3514
Dave Chinner14b064c2011-01-27 12:16:28 +11003515 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003516 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3517 &notinit);
3518 if (error)
3519 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003520
Christoph Hellwigc467c042010-02-15 23:34:42 +00003521 if (++ag == mp->m_sb.sb_agcount)
3522 ag = 0;
3523 if (ag == startag)
3524 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003525 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003526
3527 xfs_bmap_select_minlen(ap, args, blen, notinit);
3528 return 0;
3529}
3530
3531STATIC int
3532xfs_bmap_btalloc_filestreams(
3533 struct xfs_bmalloca *ap,
3534 struct xfs_alloc_arg *args,
3535 xfs_extlen_t *blen)
3536{
3537 struct xfs_mount *mp = ap->ip->i_mount;
3538 xfs_agnumber_t ag;
3539 int notinit = 0;
3540 int error;
3541
3542 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3543 args->total = ap->total;
3544
3545 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3546 if (ag == NULLAGNUMBER)
3547 ag = 0;
3548
3549 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3550 if (error)
3551 return error;
3552
3553 if (*blen < args->maxlen) {
3554 error = xfs_filestream_new_ag(ap, &ag);
3555 if (error)
3556 return error;
3557
3558 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3559 &notinit);
3560 if (error)
3561 return error;
3562
3563 }
3564
3565 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003566
3567 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003568 * Set the failure fallback case to look in the selected AG as stream
3569 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003570 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003571 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003572 return 0;
3573}
3574
3575STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003576xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003577 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003578{
3579 xfs_mount_t *mp; /* mount point structure */
3580 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003581 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003582 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003583 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003584 xfs_alloc_arg_t args;
3585 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003586 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003587 int nullfb; /* true if ap->firstblock isn't set */
3588 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003589 int tryagain;
3590 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003591 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003592
Dave Chinnera99ebf42011-12-01 11:24:20 +00003593 ASSERT(ap->length);
3594
Nathan Scotta365bdd2006-03-14 13:34:16 +11003595 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003596
3597 /* stripe alignment for allocation is determined by mount parameters */
3598 stripe_align = 0;
3599 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3600 stripe_align = mp->m_swidth;
3601 else if (mp->m_dalign)
3602 stripe_align = mp->m_dalign;
3603
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003604 if (ap->flags & XFS_BMAPI_COWFORK)
3605 align = xfs_get_cowextsz_hint(ap->ip);
3606 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003607 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003608 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003609 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003610 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003611 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003612 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003613 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003614 }
Dave Chinner33177f052013-12-12 16:34:36 +11003615
3616
Dave Chinner0937e0f2011-09-18 20:40:57 +00003617 nullfb = *ap->firstblock == NULLFSBLOCK;
3618 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003619 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003620 if (xfs_alloc_is_userdata(ap->datatype) &&
3621 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003622 ag = xfs_filestream_lookup_ag(ap->ip);
3623 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003624 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003625 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003626 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003627 }
3628 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003629 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003630
3631 xfs_bmap_adjacent(ap);
3632
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003634 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 * it's in the right allocation group.
3636 */
Dave Chinner3a756672011-09-18 20:40:58 +00003637 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 ;
3639 else
Dave Chinner3a756672011-09-18 20:40:58 +00003640 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 * Normal allocation, done through xfs_alloc_vextent.
3643 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003644 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003645 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003646 args.tp = ap->tp;
3647 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003648 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003649 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003650
3651 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003652 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003653 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003654 blen = 0;
3655 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003656 /*
3657 * Search for an allocation group with a single extent large
3658 * enough for the request. If one isn't found, then adjust
3659 * the minimum allocation size to the largest space found.
3660 */
Dave Chinner292378e2016-09-26 08:21:28 +10003661 if (xfs_alloc_is_userdata(ap->datatype) &&
3662 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003663 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3664 else
3665 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003666 if (error)
3667 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003668 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003669 if (xfs_inode_is_filestream(ap->ip))
3670 args.type = XFS_ALLOCTYPE_FIRST_AG;
3671 else
3672 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003673 args.total = args.minlen = ap->minlen;
3674 } else {
3675 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3676 args.total = ap->total;
3677 args.minlen = ap->minlen;
3678 }
David Chinner957d0eb2007-06-18 16:50:37 +10003679 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003680 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003681 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003682 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003683 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003684 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003685 args.prod = 1;
3686 args.mod = 0;
3687 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003688 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003689 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003690 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3691 }
3692 /*
3693 * If we are not low on available data blocks, and the
3694 * underlying logical volume manager is a stripe, and
3695 * the file offset is zero then try to allocate data
3696 * blocks on stripe unit boundary.
3697 * NOTE: ap->aeof is only set if the allocation length
3698 * is >= the stripe unit and the allocation offset is
3699 * at the end of file.
3700 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003701 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003702 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003703 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003704 atype = args.type;
3705 isaligned = 1;
3706 /*
3707 * Adjust for alignment
3708 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003709 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003710 args.minlen = blen - args.alignment;
3711 args.minalignslop = 0;
3712 } else {
3713 /*
3714 * First try an exact bno allocation.
3715 * If it fails then do a near or start bno
3716 * allocation with alignment turned on.
3717 */
3718 atype = args.type;
3719 tryagain = 1;
3720 args.type = XFS_ALLOCTYPE_THIS_BNO;
3721 args.alignment = 1;
3722 /*
3723 * Compute the minlen+alignment for the
3724 * next case. Set slop so that the value
3725 * of minlen+alignment+slop doesn't go up
3726 * between the calls.
3727 */
Dave Chinner33177f052013-12-12 16:34:36 +11003728 if (blen > stripe_align && blen <= args.maxlen)
3729 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003730 else
3731 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003732 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003733 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003734 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003735 args.minlen - 1;
3736 else
3737 args.minalignslop = 0;
3738 }
3739 } else {
3740 args.alignment = 1;
3741 args.minalignslop = 0;
3742 }
3743 args.minleft = ap->minleft;
3744 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003745 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003746 args.datatype = ap->datatype;
3747 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003748 args.ip = ap->ip;
3749
3750 error = xfs_alloc_vextent(&args);
3751 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003752 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003753
Nathan Scotta365bdd2006-03-14 13:34:16 +11003754 if (tryagain && args.fsbno == NULLFSBLOCK) {
3755 /*
3756 * Exact allocation failed. Now try with alignment
3757 * turned on.
3758 */
3759 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003760 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003761 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003762 args.minlen = nextminlen;
3763 args.minalignslop = 0;
3764 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 if ((error = xfs_alloc_vextent(&args)))
3766 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003767 }
3768 if (isaligned && args.fsbno == NULLFSBLOCK) {
3769 /*
3770 * allocation failed, so turn off alignment and
3771 * try again.
3772 */
3773 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003774 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003775 args.alignment = 0;
3776 if ((error = xfs_alloc_vextent(&args)))
3777 return error;
3778 }
3779 if (args.fsbno == NULLFSBLOCK && nullfb &&
3780 args.minlen > ap->minlen) {
3781 args.minlen = ap->minlen;
3782 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003783 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003784 if ((error = xfs_alloc_vextent(&args)))
3785 return error;
3786 }
3787 if (args.fsbno == NULLFSBLOCK && nullfb) {
3788 args.fsbno = 0;
3789 args.type = XFS_ALLOCTYPE_FIRST_AG;
3790 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003791 if ((error = xfs_alloc_vextent(&args)))
3792 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003793 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003794 }
3795 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003796 /*
3797 * check the allocation happened at the same or higher AG than
3798 * the first block that was allocated.
3799 */
3800 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003801 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3802 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003803
Dave Chinner3a756672011-09-18 20:40:58 +00003804 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003805 if (*ap->firstblock == NULLFSBLOCK)
3806 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003807 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003808 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003809 if (!(ap->flags & XFS_BMAPI_COWFORK))
3810 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003811 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3812 if (ap->wasdel)
3813 ap->ip->i_delayed_blks -= args.len;
3814 /*
3815 * Adjust the disk quota also. This was reserved
3816 * earlier.
3817 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003818 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003819 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3820 XFS_TRANS_DQ_BCOUNT,
3821 (long) args.len);
3822 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003823 ap->blkno = NULLFSBLOCK;
3824 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 }
3826 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003827}
3828
3829/*
3830 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3831 * It figures out where to ask the underlying allocator to put the new extent.
3832 */
3833STATIC int
3834xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003835 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003836{
Dave Chinner292378e2016-09-26 08:21:28 +10003837 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3838 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003839 return xfs_bmap_rtalloc(ap);
3840 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841}
3842
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003843/* Trim extent to fit a logical block range. */
3844void
3845xfs_trim_extent(
3846 struct xfs_bmbt_irec *irec,
3847 xfs_fileoff_t bno,
3848 xfs_filblks_t len)
3849{
3850 xfs_fileoff_t distance;
3851 xfs_fileoff_t end = bno + len;
3852
3853 if (irec->br_startoff + irec->br_blockcount <= bno ||
3854 irec->br_startoff >= end) {
3855 irec->br_blockcount = 0;
3856 return;
3857 }
3858
3859 if (irec->br_startoff < bno) {
3860 distance = bno - irec->br_startoff;
3861 if (isnullstartblock(irec->br_startblock))
3862 irec->br_startblock = DELAYSTARTBLOCK;
3863 if (irec->br_startblock != DELAYSTARTBLOCK &&
3864 irec->br_startblock != HOLESTARTBLOCK)
3865 irec->br_startblock += distance;
3866 irec->br_startoff += distance;
3867 irec->br_blockcount -= distance;
3868 }
3869
3870 if (end < irec->br_startoff + irec->br_blockcount) {
3871 distance = irec->br_startoff + irec->br_blockcount - end;
3872 irec->br_blockcount -= distance;
3873 }
3874}
3875
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003877 * Trim the returned map to the required bounds
3878 */
3879STATIC void
3880xfs_bmapi_trim_map(
3881 struct xfs_bmbt_irec *mval,
3882 struct xfs_bmbt_irec *got,
3883 xfs_fileoff_t *bno,
3884 xfs_filblks_t len,
3885 xfs_fileoff_t obno,
3886 xfs_fileoff_t end,
3887 int n,
3888 int flags)
3889{
3890 if ((flags & XFS_BMAPI_ENTIRE) ||
3891 got->br_startoff + got->br_blockcount <= obno) {
3892 *mval = *got;
3893 if (isnullstartblock(got->br_startblock))
3894 mval->br_startblock = DELAYSTARTBLOCK;
3895 return;
3896 }
3897
3898 if (obno > *bno)
3899 *bno = obno;
3900 ASSERT((*bno >= obno) || (n == 0));
3901 ASSERT(*bno < end);
3902 mval->br_startoff = *bno;
3903 if (isnullstartblock(got->br_startblock))
3904 mval->br_startblock = DELAYSTARTBLOCK;
3905 else
3906 mval->br_startblock = got->br_startblock +
3907 (*bno - got->br_startoff);
3908 /*
3909 * Return the minimum of what we got and what we asked for for
3910 * the length. We can use the len variable here because it is
3911 * modified below and we could have been there before coming
3912 * here if the first part of the allocation didn't overlap what
3913 * was asked for.
3914 */
3915 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3916 got->br_blockcount - (*bno - got->br_startoff));
3917 mval->br_state = got->br_state;
3918 ASSERT(mval->br_blockcount <= len);
3919 return;
3920}
3921
3922/*
3923 * Update and validate the extent map to return
3924 */
3925STATIC void
3926xfs_bmapi_update_map(
3927 struct xfs_bmbt_irec **map,
3928 xfs_fileoff_t *bno,
3929 xfs_filblks_t *len,
3930 xfs_fileoff_t obno,
3931 xfs_fileoff_t end,
3932 int *n,
3933 int flags)
3934{
3935 xfs_bmbt_irec_t *mval = *map;
3936
3937 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3938 ((mval->br_startoff + mval->br_blockcount) <= end));
3939 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3940 (mval->br_startoff < obno));
3941
3942 *bno = mval->br_startoff + mval->br_blockcount;
3943 *len = end - *bno;
3944 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3945 /* update previous map with new information */
3946 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3947 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3948 ASSERT(mval->br_state == mval[-1].br_state);
3949 mval[-1].br_blockcount = mval->br_blockcount;
3950 mval[-1].br_state = mval->br_state;
3951 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3952 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3953 mval[-1].br_startblock != HOLESTARTBLOCK &&
3954 mval->br_startblock == mval[-1].br_startblock +
3955 mval[-1].br_blockcount &&
3956 ((flags & XFS_BMAPI_IGSTATE) ||
3957 mval[-1].br_state == mval->br_state)) {
3958 ASSERT(mval->br_startoff ==
3959 mval[-1].br_startoff + mval[-1].br_blockcount);
3960 mval[-1].br_blockcount += mval->br_blockcount;
3961 } else if (*n > 0 &&
3962 mval->br_startblock == DELAYSTARTBLOCK &&
3963 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3964 mval->br_startoff ==
3965 mval[-1].br_startoff + mval[-1].br_blockcount) {
3966 mval[-1].br_blockcount += mval->br_blockcount;
3967 mval[-1].br_state = mval->br_state;
3968 } else if (!((*n == 0) &&
3969 ((mval->br_startoff + mval->br_blockcount) <=
3970 obno))) {
3971 mval++;
3972 (*n)++;
3973 }
3974 *map = mval;
3975}
3976
3977/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003978 * Map file blocks to filesystem blocks without allocation.
3979 */
3980int
3981xfs_bmapi_read(
3982 struct xfs_inode *ip,
3983 xfs_fileoff_t bno,
3984 xfs_filblks_t len,
3985 struct xfs_bmbt_irec *mval,
3986 int *nmap,
3987 int flags)
3988{
3989 struct xfs_mount *mp = ip->i_mount;
3990 struct xfs_ifork *ifp;
3991 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003992 xfs_fileoff_t obno;
3993 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003994 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003995 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003996 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003997 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003998 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003999
4000 ASSERT(*nmap >= 1);
4001 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004002 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004003 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004004
4005 if (unlikely(XFS_TEST_ERROR(
4006 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4007 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4008 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4009 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004010 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004011 }
4012
4013 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004014 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004015
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004016 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004017
4018 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004019
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004020 /* No CoW fork? Return a hole. */
4021 if (whichfork == XFS_COW_FORK && !ifp) {
4022 mval->br_startoff = bno;
4023 mval->br_startblock = HOLESTARTBLOCK;
4024 mval->br_blockcount = len;
4025 mval->br_state = XFS_EXT_NORM;
4026 *nmap = 1;
4027 return 0;
4028 }
4029
Dave Chinner5c8ed202011-09-18 20:40:45 +00004030 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4031 error = xfs_iread_extents(NULL, ip, whichfork);
4032 if (error)
4033 return error;
4034 }
4035
Christoph Hellwig334f3422016-11-24 11:39:43 +11004036 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4037 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004038 end = bno + len;
4039 obno = bno;
4040
4041 while (bno < end && n < *nmap) {
4042 /* Reading past eof, act as though there's a hole up to end. */
4043 if (eof)
4044 got.br_startoff = end;
4045 if (got.br_startoff > bno) {
4046 /* Reading in a hole. */
4047 mval->br_startoff = bno;
4048 mval->br_startblock = HOLESTARTBLOCK;
4049 mval->br_blockcount =
4050 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4051 mval->br_state = XFS_EXT_NORM;
4052 bno += mval->br_blockcount;
4053 len -= mval->br_blockcount;
4054 mval++;
4055 n++;
4056 continue;
4057 }
4058
4059 /* set up the extent map to return. */
4060 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4061 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4062
4063 /* If we're done, stop now. */
4064 if (bno >= end || n >= *nmap)
4065 break;
4066
4067 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004068 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4069 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004070 }
4071 *nmap = n;
4072 return 0;
4073}
4074
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004075/*
4076 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4077 * global pool and the extent inserted into the inode in-core extent tree.
4078 *
4079 * On entry, got refers to the first extent beyond the offset of the extent to
4080 * allocate or eof is specified if no such extent exists. On return, got refers
4081 * to the extent record that was inserted to the inode fork.
4082 *
4083 * Note that the allocated extent may have been merged with contiguous extents
4084 * during insertion into the inode fork. Thus, got does not reflect the current
4085 * state of the inode fork on return. If necessary, the caller can use lastx to
4086 * look up the updated record in the inode fork.
4087 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10004088int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004089xfs_bmapi_reserve_delalloc(
4090 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004091 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004092 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004093 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004094 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004095 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004096 xfs_extnum_t *lastx,
4097 int eof)
4098{
4099 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004100 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004101 xfs_extlen_t alen;
4102 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004103 char rt = XFS_IS_REALTIME_INODE(ip);
4104 xfs_extlen_t extsz;
4105 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004106 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004107
Brian Foster974ae922016-11-28 14:57:42 +11004108 /*
4109 * Cap the alloc length. Keep track of prealloc so we know whether to
4110 * tag the inode before we return.
4111 */
4112 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004113 if (!eof)
4114 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004115 if (prealloc && alen >= len)
4116 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004117
4118 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004119 if (whichfork == XFS_COW_FORK)
4120 extsz = xfs_get_cowextsz_hint(ip);
4121 else
4122 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004123 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004124 struct xfs_bmbt_irec prev;
4125
4126 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4127 prev.br_startoff = NULLFILEOFF;
4128
4129 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004130 1, 0, &aoff, &alen);
4131 ASSERT(!error);
4132 }
4133
4134 if (rt)
4135 extsz = alen / mp->m_sb.sb_rextsize;
4136
4137 /*
4138 * Make a transaction-less quota reservation for delayed allocation
4139 * blocks. This number gets adjusted later. We return if we haven't
4140 * allocated blocks already inside this loop.
4141 */
4142 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4143 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4144 if (error)
4145 return error;
4146
4147 /*
4148 * Split changing sb for alen and indlen since they could be coming
4149 * from different places.
4150 */
4151 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4152 ASSERT(indlen > 0);
4153
4154 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004155 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004156 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004157 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004158 }
4159
4160 if (error)
4161 goto out_unreserve_quota;
4162
Dave Chinner0d485ad2015-02-23 21:22:03 +11004163 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004164 if (error)
4165 goto out_unreserve_blocks;
4166
4167
4168 ip->i_delayed_blks += alen;
4169
4170 got->br_startoff = aoff;
4171 got->br_startblock = nullstartblock(indlen);
4172 got->br_blockcount = alen;
4173 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004174
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004175 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004176
Brian Foster974ae922016-11-28 14:57:42 +11004177 /*
4178 * Tag the inode if blocks were preallocated. Note that COW fork
4179 * preallocation can occur at the start or end of the extent, even when
4180 * prealloc == 0, so we must also check the aligned offset and length.
4181 */
4182 if (whichfork == XFS_DATA_FORK && prealloc)
4183 xfs_inode_set_eofblocks_tag(ip);
4184 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4185 xfs_inode_set_cowblocks_tag(ip);
4186
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004187 return 0;
4188
4189out_unreserve_blocks:
4190 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004191 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004192 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004193 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004194out_unreserve_quota:
4195 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004196 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004197 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4198 return error;
4199}
4200
Dave Chinnercf11da92014-07-15 07:08:24 +10004201static int
4202xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004203 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004204{
4205 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004206 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004207 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004208 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004209 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004210
Dave Chinnera99ebf42011-12-01 11:24:20 +00004211 ASSERT(bma->length > 0);
4212
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004213 /*
4214 * For the wasdelay case, we could also just allocate the stuff asked
4215 * for in this bmap call but that wouldn't be as good.
4216 */
4217 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004218 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4219 bma->offset = bma->got.br_startoff;
Christoph Hellwig0e8d6302016-11-24 11:40:32 +11004220 if (bma->idx) {
Dave Chinnere0c3da52011-09-18 20:41:01 +00004221 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004222 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004223 }
4224 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004225 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004226 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004227 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004228 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004229 }
4230
4231 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004232 * Set the data type being allocated. For the data fork, the first data
4233 * in the file is treated differently to all other allocations. For the
4234 * attribute fork, we only need to ensure the allocated range is not on
4235 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004236 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004237 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004238 bma->datatype = XFS_ALLOC_NOBUSY;
4239 if (whichfork == XFS_DATA_FORK) {
4240 if (bma->offset == 0)
4241 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4242 else
4243 bma->datatype |= XFS_ALLOC_USERDATA;
4244 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004245 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004246 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004247 }
4248
Dave Chinnere04426b2012-10-05 11:06:59 +10004249 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004250
4251 /*
4252 * Only want to do the alignment at the eof if it is userdata and
4253 * allocation length is larger than a stripe unit.
4254 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004255 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004256 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004257 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004258 if (error)
4259 return error;
4260 }
4261
4262 error = xfs_bmap_alloc(bma);
4263 if (error)
4264 return error;
4265
Dave Chinner29c8d172011-09-18 20:41:00 +00004266 if (bma->cur)
4267 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004268 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004269 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004270 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4271 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4272 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004273 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004274 }
4275 /*
4276 * Bump the number of extents we've allocated
4277 * in this call.
4278 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004279 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004280
Dave Chinner29c8d172011-09-18 20:41:00 +00004281 if (bma->cur)
4282 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004283 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4284
Dave Chinner963c30c2011-09-18 20:40:59 +00004285 bma->got.br_startoff = bma->offset;
4286 bma->got.br_startblock = bma->blkno;
4287 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004288 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004289
4290 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004291 * In the data fork, a wasdelay extent has been initialized, so
4292 * shouldn't be flagged as unwritten.
4293 *
4294 * For the cow fork, however, we convert delalloc reservations
4295 * (extents allocated for speculative preallocation) to
4296 * allocated unwritten extents, and only convert the unwritten
4297 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004298 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004299 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4300 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004301 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004302 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004303
Christoph Hellwigc6534242011-09-18 20:41:05 +00004304 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004305 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004306 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004307 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4308 whichfork, &bma->idx, &bma->cur, &bma->got,
4309 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004310
Christoph Hellwigc315c902011-09-18 20:41:02 +00004311 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004312 if (error)
4313 return error;
4314
4315 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004316 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4317 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4318 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004319 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004320 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004321
Dave Chinner963c30c2011-09-18 20:40:59 +00004322 ASSERT(bma->got.br_startoff <= bma->offset);
4323 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4324 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004325 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4326 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004327 return 0;
4328}
4329
Dave Chinnerb447fe52011-09-18 20:40:51 +00004330STATIC int
4331xfs_bmapi_convert_unwritten(
4332 struct xfs_bmalloca *bma,
4333 struct xfs_bmbt_irec *mval,
4334 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004335 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004336{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004337 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004338 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004339 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004340 int error;
4341
Dave Chinnerb447fe52011-09-18 20:40:51 +00004342 /* check if we need to do unwritten->real conversion */
4343 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4344 (flags & XFS_BMAPI_PREALLOC))
4345 return 0;
4346
4347 /* check if we need to do real->unwritten conversion */
4348 if (mval->br_state == XFS_EXT_NORM &&
4349 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4350 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4351 return 0;
4352
4353 /*
4354 * Modify (by adding) the state flag, if writing.
4355 */
4356 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004357 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4358 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004359 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004360 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004361 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004362 }
4363 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4364 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4365
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004366 /*
4367 * Before insertion into the bmbt, zero the range being converted
4368 * if required.
4369 */
4370 if (flags & XFS_BMAPI_ZERO) {
4371 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4372 mval->br_blockcount);
4373 if (error)
4374 return error;
4375 }
4376
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004377 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4378 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004379 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004380 /*
4381 * Log the inode core unconditionally in the unwritten extent conversion
4382 * path because the conversion might not have done so (e.g., if the
4383 * extent count hasn't changed). We need to make sure the inode is dirty
4384 * in the transaction for the sake of fsync(), even if nothing has
4385 * changed, because fsync() will not force the log for this transaction
4386 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004387 *
4388 * Note: If we're only converting cow fork extents, there aren't
4389 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004390 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004391 if (whichfork != XFS_COW_FORK)
4392 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004393 if (error)
4394 return error;
4395
4396 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004397 * Update our extent pointer, given that
4398 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4399 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004400 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004401 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004402
4403 /*
4404 * We may have combined previously unwritten space with written space,
4405 * so generate another request.
4406 */
4407 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004408 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004409 return 0;
4410}
4411
Christoph Hellwig44032802011-09-18 20:40:48 +00004412/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004413 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4414 * extent state if necessary. Details behaviour is controlled by the flags
4415 * parameter. Only allocates blocks from a single allocation group, to avoid
4416 * locking problems.
4417 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 * The returned value in "firstblock" from the first call in a transaction
4419 * must be remembered and presented to subsequent calls in "firstblock".
4420 * An upper bound for the number of blocks to be allocated is supplied to
4421 * the first call in "total"; if no allocation group has that many free
4422 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4423 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004424int
4425xfs_bmapi_write(
4426 struct xfs_trans *tp, /* transaction pointer */
4427 struct xfs_inode *ip, /* incore inode */
4428 xfs_fileoff_t bno, /* starting file offs. mapped */
4429 xfs_filblks_t len, /* length to map in file */
4430 int flags, /* XFS_BMAPI_... */
4431 xfs_fsblock_t *firstblock, /* first allocated block
4432 controls a.g. for allocs */
4433 xfs_extlen_t total, /* total blocks needed */
4434 struct xfs_bmbt_irec *mval, /* output: map values */
4435 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004436 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004438 struct xfs_mount *mp = ip->i_mount;
4439 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004440 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004441 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004442 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004443 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004444 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004445 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004446 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004449 xfs_fileoff_t orig_bno; /* original block number value */
4450 int orig_flags; /* original flags arg value */
4451 xfs_filblks_t orig_len; /* original value of len arg */
4452 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4453 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
4455 orig_bno = bno;
4456 orig_len = len;
4457 orig_flags = flags;
4458 orig_mval = mval;
4459 orig_nmap = *nmap;
4460#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004461 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004464 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4465 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004466 ASSERT(tp != NULL ||
4467 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4468 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004469 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004470 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004471 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004472 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004473
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004474 /* zeroing is for currently only for data extents, not metadata */
4475 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4476 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4477 /*
4478 * we can allocate unwritten extents or pre-zero allocated blocks,
4479 * but it makes no sense to do both at once. This would result in
4480 * zeroing the unwritten extent twice, but it still being an
4481 * unwritten extent....
4482 */
4483 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4484 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 if (unlikely(XFS_TEST_ERROR(
4487 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004488 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004490 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004491 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004495 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004496
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004498
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004499 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004500
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004501 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004503 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004505 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004506 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004507 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004508 }
4509
4510 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4511 error = xfs_iread_extents(tp, ip, whichfork);
4512 if (error)
4513 goto error0;
4514 }
4515
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 n = 0;
4517 end = bno + len;
4518 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004519
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004520 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4521 eof = true;
4522 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4523 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004524 bma.tp = tp;
4525 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004526 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004527 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004528 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004529 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004530
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004532 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004533
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004534 /* in hole or beyoned EOF? */
4535 if (eof || bma.got.br_startoff > bno) {
4536 if (flags & XFS_BMAPI_DELALLOC) {
4537 /*
4538 * For the COW fork we can reasonably get a
4539 * request for converting an extent that races
4540 * with other threads already having converted
4541 * part of it, as there converting COW to
4542 * regular blocks is not protected using the
4543 * IOLOCK.
4544 */
4545 ASSERT(flags & XFS_BMAPI_COWFORK);
4546 if (!(flags & XFS_BMAPI_COWFORK)) {
4547 error = -EIO;
4548 goto error0;
4549 }
4550
4551 if (eof || bno >= end)
4552 break;
4553 } else {
4554 need_alloc = true;
4555 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004556 } else if (isnullstartblock(bma.got.br_startblock)) {
4557 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004558 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004559
4560 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 * First, deal with the hole before the allocated space
4562 * that we found, if any.
4563 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004564 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004565 bma.eof = eof;
4566 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4567 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004568 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004569 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004570
Dave Chinnera99ebf42011-12-01 11:24:20 +00004571 /*
4572 * There's a 32/64 bit type mismatch between the
4573 * allocation length request (which can be 64 bits in
4574 * length) and the bma length request, which is
4575 * xfs_extlen_t and therefore 32 bits. Hence we have to
4576 * check for 32-bit overflows and handle them here.
4577 */
4578 if (len > (xfs_filblks_t)MAXEXTLEN)
4579 bma.length = MAXEXTLEN;
4580 else
4581 bma.length = len;
4582
4583 ASSERT(len > 0);
4584 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004585 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 if (error)
4587 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004588 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004589 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004590
4591 /*
4592 * If this is a CoW allocation, record the data in
4593 * the refcount btree for orphan recovery.
4594 */
4595 if (whichfork == XFS_COW_FORK) {
4596 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4597 bma.blkno, bma.length);
4598 if (error)
4599 goto error0;
4600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004602
Dave Chinneraef9a892011-09-18 20:40:44 +00004603 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004604 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4605 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606
Dave Chinnerb447fe52011-09-18 20:40:51 +00004607 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004608 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004609 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004610 continue;
4611 if (error)
4612 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Dave Chinneraef9a892011-09-18 20:40:44 +00004614 /* update the extent map to return */
4615 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4616
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 /*
4618 * If we're done, stop now. Stop when we've allocated
4619 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4620 * the transaction may get too big.
4621 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004622 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004624
4625 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004626 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004627 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4628 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004631
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 /*
4633 * Transform from btree to extents, give it cur.
4634 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004635 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004636 int tmp_logflags = 0;
4637
Dave Chinner29c8d172011-09-18 20:41:00 +00004638 ASSERT(bma.cur);
4639 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004641 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 if (error)
4643 goto error0;
4644 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004647 XFS_IFORK_NEXTENTS(ip, whichfork) >
4648 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650error0:
4651 /*
4652 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004653 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004655 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004657 bma.logflags &= ~xfs_ilog_fext(whichfork);
4658 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004660 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 /*
4662 * Log whatever the flags say, even if error. Otherwise we might miss
4663 * detecting a case where the data is changed, there's an error,
4664 * and it's not logged so we don't shutdown when we should.
4665 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004666 if (bma.logflags)
4667 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004668
Dave Chinner29c8d172011-09-18 20:41:00 +00004669 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 if (!error) {
4671 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004672 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004674 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004675 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004677 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4679 }
4680 if (!error)
4681 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4682 orig_nmap, *nmap);
4683 return error;
4684}
4685
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004686static int
4687xfs_bmapi_remap(
4688 struct xfs_trans *tp,
4689 struct xfs_inode *ip,
4690 xfs_fileoff_t bno,
4691 xfs_filblks_t len,
4692 xfs_fsblock_t startblock,
4693 struct xfs_defer_ops *dfops)
4694{
4695 struct xfs_mount *mp = ip->i_mount;
4696 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4697 struct xfs_btree_cur *cur = NULL;
4698 xfs_fsblock_t firstblock = NULLFSBLOCK;
4699 struct xfs_bmbt_irec got;
4700 xfs_extnum_t idx;
4701 int logflags = 0, error;
4702
4703 ASSERT(len > 0);
4704 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4705 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4706
4707 if (unlikely(XFS_TEST_ERROR(
4708 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4709 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4710 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4711 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4712 return -EFSCORRUPTED;
4713 }
4714
4715 if (XFS_FORCED_SHUTDOWN(mp))
4716 return -EIO;
4717
4718 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4719 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4720 if (error)
4721 return error;
4722 }
4723
4724 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4725 /* make sure we only reflink into a hole. */
4726 ASSERT(got.br_startoff > bno);
4727 ASSERT(got.br_startoff - bno >= len);
4728 }
4729
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004730 ip->i_d.di_nblocks += len;
4731 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004732
4733 if (ifp->if_flags & XFS_IFBROOT) {
4734 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4735 cur->bc_private.b.firstblock = firstblock;
4736 cur->bc_private.b.dfops = dfops;
4737 cur->bc_private.b.flags = 0;
4738 }
4739
4740 got.br_startoff = bno;
4741 got.br_startblock = startblock;
4742 got.br_blockcount = len;
4743 got.br_state = XFS_EXT_NORM;
4744
4745 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4746 &got, &firstblock, dfops, &logflags);
4747 if (error)
4748 goto error0;
4749
4750 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4751 int tmp_logflags = 0;
4752
4753 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4754 &tmp_logflags, XFS_DATA_FORK);
4755 logflags |= tmp_logflags;
4756 }
4757
4758error0:
4759 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4760 logflags &= ~XFS_ILOG_DEXT;
4761 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4762 logflags &= ~XFS_ILOG_DBROOT;
4763
4764 if (logflags)
4765 xfs_trans_log_inode(tp, ip, logflags);
4766 if (cur) {
4767 xfs_btree_del_cursor(cur,
4768 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4769 }
4770 return error;
4771}
4772
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004774 * When a delalloc extent is split (e.g., due to a hole punch), the original
4775 * indlen reservation must be shared across the two new extents that are left
4776 * behind.
4777 *
4778 * Given the original reservation and the worst case indlen for the two new
4779 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004780 * reservation fairly across the two new extents. If necessary, steal available
4781 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4782 * ores == 1). The number of stolen blocks is returned. The availability and
4783 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004784 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004785static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004786xfs_bmap_split_indlen(
4787 xfs_filblks_t ores, /* original res. */
4788 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004789 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4790 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004791{
4792 xfs_filblks_t len1 = *indlen1;
4793 xfs_filblks_t len2 = *indlen2;
4794 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004795 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004796 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004797
4798 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004799 * Steal as many blocks as we can to try and satisfy the worst case
4800 * indlen for both new extents.
4801 */
Brian Foster75d65362017-02-13 22:48:30 -08004802 if (ores < nres && avail)
4803 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4804 ores += stolen;
4805
4806 /* nothing else to do if we've satisfied the new reservation */
4807 if (ores >= nres)
4808 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004809
4810 /*
Brian Foster75d65362017-02-13 22:48:30 -08004811 * We can't meet the total required reservation for the two extents.
4812 * Calculate the percent of the overall shortage between both extents
4813 * and apply this percentage to each of the requested indlen values.
4814 * This distributes the shortage fairly and reduces the chances that one
4815 * of the two extents is left with nothing when extents are repeatedly
4816 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004817 */
Brian Foster75d65362017-02-13 22:48:30 -08004818 resfactor = (ores * 100);
4819 do_div(resfactor, nres);
4820 len1 *= resfactor;
4821 do_div(len1, 100);
4822 len2 *= resfactor;
4823 do_div(len2, 100);
4824 ASSERT(len1 + len2 <= ores);
4825 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4826
4827 /*
4828 * Hand out the remainder to each extent. If one of the two reservations
4829 * is zero, we want to make sure that one gets a block first. The loop
4830 * below starts with len1, so hand len2 a block right off the bat if it
4831 * is zero.
4832 */
4833 ores -= (len1 + len2);
4834 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4835 if (ores && !len2 && *indlen2) {
4836 len2++;
4837 ores--;
4838 }
4839 while (ores) {
4840 if (len1 < *indlen1) {
4841 len1++;
4842 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004843 }
Brian Foster75d65362017-02-13 22:48:30 -08004844 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004845 break;
Brian Foster75d65362017-02-13 22:48:30 -08004846 if (len2 < *indlen2) {
4847 len2++;
4848 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004849 }
4850 }
4851
4852 *indlen1 = len1;
4853 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004854
4855 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004856}
4857
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004858int
4859xfs_bmap_del_extent_delay(
4860 struct xfs_inode *ip,
4861 int whichfork,
4862 xfs_extnum_t *idx,
4863 struct xfs_bmbt_irec *got,
4864 struct xfs_bmbt_irec *del)
4865{
4866 struct xfs_mount *mp = ip->i_mount;
4867 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4868 struct xfs_bmbt_irec new;
4869 int64_t da_old, da_new, da_diff = 0;
4870 xfs_fileoff_t del_endoff, got_endoff;
4871 xfs_filblks_t got_indlen, new_indlen, stolen;
4872 int error = 0, state = 0;
4873 bool isrt;
4874
4875 XFS_STATS_INC(mp, xs_del_exlist);
4876
4877 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4878 del_endoff = del->br_startoff + del->br_blockcount;
4879 got_endoff = got->br_startoff + got->br_blockcount;
4880 da_old = startblockval(got->br_startblock);
4881 da_new = 0;
4882
4883 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004884 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004885 ASSERT(del->br_blockcount > 0);
4886 ASSERT(got->br_startoff <= del->br_startoff);
4887 ASSERT(got_endoff >= del_endoff);
4888
4889 if (isrt) {
4890 int64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4891
4892 do_div(rtexts, mp->m_sb.sb_rextsize);
4893 xfs_mod_frextents(mp, rtexts);
4894 }
4895
4896 /*
4897 * Update the inode delalloc counter now and wait to update the
4898 * sb counters as we might have to borrow some blocks for the
4899 * indirect block accounting.
4900 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004901 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4902 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004903 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004904 if (error)
4905 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004906 ip->i_delayed_blks -= del->br_blockcount;
4907
4908 if (whichfork == XFS_COW_FORK)
4909 state |= BMAP_COWFORK;
4910
4911 if (got->br_startoff == del->br_startoff)
4912 state |= BMAP_LEFT_CONTIG;
4913 if (got_endoff == del_endoff)
4914 state |= BMAP_RIGHT_CONTIG;
4915
4916 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4917 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4918 /*
4919 * Matches the whole extent. Delete the entry.
4920 */
4921 xfs_iext_remove(ip, *idx, 1, state);
4922 --*idx;
4923 break;
4924 case BMAP_LEFT_CONTIG:
4925 /*
4926 * Deleting the first part of the extent.
4927 */
4928 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4929 got->br_startoff = del_endoff;
4930 got->br_blockcount -= del->br_blockcount;
4931 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4932 got->br_blockcount), da_old);
4933 got->br_startblock = nullstartblock((int)da_new);
4934 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4935 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4936 break;
4937 case BMAP_RIGHT_CONTIG:
4938 /*
4939 * Deleting the last part of the extent.
4940 */
4941 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4942 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4943 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4944 got->br_blockcount), da_old);
4945 got->br_startblock = nullstartblock((int)da_new);
4946 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4947 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4948 break;
4949 case 0:
4950 /*
4951 * Deleting the middle of the extent.
4952 *
4953 * Distribute the original indlen reservation across the two new
4954 * extents. Steal blocks from the deleted extent if necessary.
4955 * Stealing blocks simply fudges the fdblocks accounting below.
4956 * Warn if either of the new indlen reservations is zero as this
4957 * can lead to delalloc problems.
4958 */
4959 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4960
4961 got->br_blockcount = del->br_startoff - got->br_startoff;
4962 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4963
4964 new.br_blockcount = got_endoff - del_endoff;
4965 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4966
4967 WARN_ON_ONCE(!got_indlen || !new_indlen);
4968 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4969 del->br_blockcount);
4970
4971 got->br_startblock = nullstartblock((int)got_indlen);
4972 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4973 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4974
4975 new.br_startoff = del_endoff;
4976 new.br_state = got->br_state;
4977 new.br_startblock = nullstartblock((int)new_indlen);
4978
4979 ++*idx;
4980 xfs_iext_insert(ip, *idx, 1, &new, state);
4981
4982 da_new = got_indlen + new_indlen - stolen;
4983 del->br_blockcount -= stolen;
4984 break;
4985 }
4986
4987 ASSERT(da_old >= da_new);
4988 da_diff = da_old - da_new;
4989 if (!isrt)
4990 da_diff += del->br_blockcount;
4991 if (da_diff)
4992 xfs_mod_fdblocks(mp, da_diff, false);
4993 return error;
4994}
4995
4996void
4997xfs_bmap_del_extent_cow(
4998 struct xfs_inode *ip,
4999 xfs_extnum_t *idx,
5000 struct xfs_bmbt_irec *got,
5001 struct xfs_bmbt_irec *del)
5002{
5003 struct xfs_mount *mp = ip->i_mount;
5004 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
5005 struct xfs_bmbt_irec new;
5006 xfs_fileoff_t del_endoff, got_endoff;
5007 int state = BMAP_COWFORK;
5008
5009 XFS_STATS_INC(mp, xs_del_exlist);
5010
5011 del_endoff = del->br_startoff + del->br_blockcount;
5012 got_endoff = got->br_startoff + got->br_blockcount;
5013
5014 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11005015 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005016 ASSERT(del->br_blockcount > 0);
5017 ASSERT(got->br_startoff <= del->br_startoff);
5018 ASSERT(got_endoff >= del_endoff);
5019 ASSERT(!isnullstartblock(got->br_startblock));
5020
5021 if (got->br_startoff == del->br_startoff)
5022 state |= BMAP_LEFT_CONTIG;
5023 if (got_endoff == del_endoff)
5024 state |= BMAP_RIGHT_CONTIG;
5025
5026 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
5027 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
5028 /*
5029 * Matches the whole extent. Delete the entry.
5030 */
5031 xfs_iext_remove(ip, *idx, 1, state);
5032 --*idx;
5033 break;
5034 case BMAP_LEFT_CONTIG:
5035 /*
5036 * Deleting the first part of the extent.
5037 */
5038 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5039 got->br_startoff = del_endoff;
5040 got->br_blockcount -= del->br_blockcount;
5041 got->br_startblock = del->br_startblock + del->br_blockcount;
5042 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5043 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5044 break;
5045 case BMAP_RIGHT_CONTIG:
5046 /*
5047 * Deleting the last part of the extent.
5048 */
5049 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5050 got->br_blockcount -= del->br_blockcount;
5051 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5052 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5053 break;
5054 case 0:
5055 /*
5056 * Deleting the middle of the extent.
5057 */
5058 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5059 got->br_blockcount = del->br_startoff - got->br_startoff;
5060 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5061 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5062
5063 new.br_startoff = del_endoff;
5064 new.br_blockcount = got_endoff - del_endoff;
5065 new.br_state = got->br_state;
5066 new.br_startblock = del->br_startblock + del->br_blockcount;
5067
5068 ++*idx;
5069 xfs_iext_insert(ip, *idx, 1, &new, state);
5070 break;
5071 }
5072}
5073
Brian Fostera9bd24a2016-03-15 11:42:46 +11005074/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005075 * Called by xfs_bmapi to update file extent records and the btree
5076 * after removing space (or undoing a delayed allocation).
5077 */
5078STATIC int /* error */
5079xfs_bmap_del_extent(
5080 xfs_inode_t *ip, /* incore inode pointer */
5081 xfs_trans_t *tp, /* current transaction pointer */
5082 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005083 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005084 xfs_btree_cur_t *cur, /* if null, not a btree */
5085 xfs_bmbt_irec_t *del, /* data to remove from extents */
5086 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005087 int whichfork, /* data or attr fork */
5088 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005089{
5090 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5091 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5092 xfs_fsblock_t del_endblock=0; /* first block past del */
5093 xfs_fileoff_t del_endoff; /* first offset past del */
5094 int delay; /* current block is delayed allocated */
5095 int do_fx; /* free extent at end of routine */
5096 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5097 int error; /* error return value */
5098 int flags; /* inode logging flags */
5099 xfs_bmbt_irec_t got; /* current extent entry */
5100 xfs_fileoff_t got_endoff; /* first offset past got */
5101 int i; /* temp state */
5102 xfs_ifork_t *ifp; /* inode fork pointer */
5103 xfs_mount_t *mp; /* mount structure */
5104 xfs_filblks_t nblks; /* quota/sb block count */
5105 xfs_bmbt_irec_t new; /* new record to be inserted */
5106 /* REFERENCED */
5107 uint qfield; /* quota field to update */
5108 xfs_filblks_t temp; /* for indirect length calculations */
5109 xfs_filblks_t temp2; /* for indirect length calculations */
5110 int state = 0;
5111
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005112 mp = ip->i_mount;
5113 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005114
5115 if (whichfork == XFS_ATTR_FORK)
5116 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005117 else if (whichfork == XFS_COW_FORK)
5118 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005119
Dave Chinner9e5987a2013-02-25 12:31:26 +11005120 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005121 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005122 ASSERT(del->br_blockcount > 0);
5123 ep = xfs_iext_get_ext(ifp, *idx);
5124 xfs_bmbt_get_all(ep, &got);
5125 ASSERT(got.br_startoff <= del->br_startoff);
5126 del_endoff = del->br_startoff + del->br_blockcount;
5127 got_endoff = got.br_startoff + got.br_blockcount;
5128 ASSERT(got_endoff >= del_endoff);
5129 delay = isnullstartblock(got.br_startblock);
5130 ASSERT(isnullstartblock(del->br_startblock) == delay);
5131 flags = 0;
5132 qfield = 0;
5133 error = 0;
5134 /*
5135 * If deleting a real allocation, must free up the disk space.
5136 */
5137 if (!delay) {
5138 flags = XFS_ILOG_CORE;
5139 /*
5140 * Realtime allocation. Free it and record di_nblocks update.
5141 */
5142 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5143 xfs_fsblock_t bno;
5144 xfs_filblks_t len;
5145
5146 ASSERT(do_mod(del->br_blockcount,
5147 mp->m_sb.sb_rextsize) == 0);
5148 ASSERT(do_mod(del->br_startblock,
5149 mp->m_sb.sb_rextsize) == 0);
5150 bno = del->br_startblock;
5151 len = del->br_blockcount;
5152 do_div(bno, mp->m_sb.sb_rextsize);
5153 do_div(len, mp->m_sb.sb_rextsize);
5154 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5155 if (error)
5156 goto done;
5157 do_fx = 0;
5158 nblks = len * mp->m_sb.sb_rextsize;
5159 qfield = XFS_TRANS_DQ_RTBCOUNT;
5160 }
5161 /*
5162 * Ordinary allocation.
5163 */
5164 else {
5165 do_fx = 1;
5166 nblks = del->br_blockcount;
5167 qfield = XFS_TRANS_DQ_BCOUNT;
5168 }
5169 /*
5170 * Set up del_endblock and cur for later.
5171 */
5172 del_endblock = del->br_startblock + del->br_blockcount;
5173 if (cur) {
5174 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5175 got.br_startblock, got.br_blockcount,
5176 &i)))
5177 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005178 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005179 }
5180 da_old = da_new = 0;
5181 } else {
5182 da_old = startblockval(got.br_startblock);
5183 da_new = 0;
5184 nblks = 0;
5185 do_fx = 0;
5186 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005187
Dave Chinner9e5987a2013-02-25 12:31:26 +11005188 /*
5189 * Set flag value to use in switch statement.
5190 * Left-contig is 2, right-contig is 1.
5191 */
5192 switch (((got.br_startoff == del->br_startoff) << 1) |
5193 (got_endoff == del_endoff)) {
5194 case 3:
5195 /*
5196 * Matches the whole extent. Delete the entry.
5197 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005198 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005199 xfs_iext_remove(ip, *idx, 1,
5200 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5201 --*idx;
5202 if (delay)
5203 break;
5204
5205 XFS_IFORK_NEXT_SET(ip, whichfork,
5206 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5207 flags |= XFS_ILOG_CORE;
5208 if (!cur) {
5209 flags |= xfs_ilog_fext(whichfork);
5210 break;
5211 }
5212 if ((error = xfs_btree_delete(cur, &i)))
5213 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005214 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005215 break;
5216
5217 case 2:
5218 /*
5219 * Deleting the first part of the extent.
5220 */
5221 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5222 xfs_bmbt_set_startoff(ep, del_endoff);
5223 temp = got.br_blockcount - del->br_blockcount;
5224 xfs_bmbt_set_blockcount(ep, temp);
5225 if (delay) {
5226 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5227 da_old);
5228 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5229 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5230 da_new = temp;
5231 break;
5232 }
5233 xfs_bmbt_set_startblock(ep, del_endblock);
5234 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5235 if (!cur) {
5236 flags |= xfs_ilog_fext(whichfork);
5237 break;
5238 }
5239 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5240 got.br_blockcount - del->br_blockcount,
5241 got.br_state)))
5242 goto done;
5243 break;
5244
5245 case 1:
5246 /*
5247 * Deleting the last part of the extent.
5248 */
5249 temp = got.br_blockcount - del->br_blockcount;
5250 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5251 xfs_bmbt_set_blockcount(ep, temp);
5252 if (delay) {
5253 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5254 da_old);
5255 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5256 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5257 da_new = temp;
5258 break;
5259 }
5260 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5261 if (!cur) {
5262 flags |= xfs_ilog_fext(whichfork);
5263 break;
5264 }
5265 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5266 got.br_startblock,
5267 got.br_blockcount - del->br_blockcount,
5268 got.br_state)))
5269 goto done;
5270 break;
5271
5272 case 0:
5273 /*
5274 * Deleting the middle of the extent.
5275 */
5276 temp = del->br_startoff - got.br_startoff;
5277 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5278 xfs_bmbt_set_blockcount(ep, temp);
5279 new.br_startoff = del_endoff;
5280 temp2 = got_endoff - del_endoff;
5281 new.br_blockcount = temp2;
5282 new.br_state = got.br_state;
5283 if (!delay) {
5284 new.br_startblock = del_endblock;
5285 flags |= XFS_ILOG_CORE;
5286 if (cur) {
5287 if ((error = xfs_bmbt_update(cur,
5288 got.br_startoff,
5289 got.br_startblock, temp,
5290 got.br_state)))
5291 goto done;
5292 if ((error = xfs_btree_increment(cur, 0, &i)))
5293 goto done;
5294 cur->bc_rec.b = new;
5295 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10005296 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005297 goto done;
5298 /*
5299 * If get no-space back from btree insert,
5300 * it tried a split, and we have a zero
5301 * block reservation.
5302 * Fix up our state and return the error.
5303 */
Dave Chinner24513372014-06-25 14:58:08 +10005304 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11005305 /*
5306 * Reset the cursor, don't trust
5307 * it after any insert operation.
5308 */
5309 if ((error = xfs_bmbt_lookup_eq(cur,
5310 got.br_startoff,
5311 got.br_startblock,
5312 temp, &i)))
5313 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005314 XFS_WANT_CORRUPTED_GOTO(mp,
5315 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005316 /*
5317 * Update the btree record back
5318 * to the original value.
5319 */
5320 if ((error = xfs_bmbt_update(cur,
5321 got.br_startoff,
5322 got.br_startblock,
5323 got.br_blockcount,
5324 got.br_state)))
5325 goto done;
5326 /*
5327 * Reset the extent record back
5328 * to the original value.
5329 */
5330 xfs_bmbt_set_blockcount(ep,
5331 got.br_blockcount);
5332 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005333 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005334 goto done;
5335 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005336 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005337 } else
5338 flags |= xfs_ilog_fext(whichfork);
5339 XFS_IFORK_NEXT_SET(ip, whichfork,
5340 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5341 } else {
Brian Fosterd34999c92016-03-15 11:42:47 +11005342 xfs_filblks_t stolen;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005343 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005344
5345 /*
5346 * Distribute the original indlen reservation across the
Brian Fosterd34999c92016-03-15 11:42:47 +11005347 * two new extents. Steal blocks from the deleted extent
5348 * if necessary. Stealing blocks simply fudges the
5349 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005350 */
5351 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5352 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c92016-03-15 11:42:47 +11005353 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5354 del->br_blockcount);
5355 da_new = temp + temp2 - stolen;
5356 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005357
5358 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11005359 * Set the reservation for each extent. Warn if either
5360 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005361 */
Brian Fosterd34999c92016-03-15 11:42:47 +11005362 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005363 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005364 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005365 }
5366 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5367 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5368 ++*idx;
5369 break;
5370 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005371
5372 /* remove reverse mapping */
5373 if (!delay) {
5374 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5375 if (error)
5376 goto done;
5377 }
5378
Dave Chinner9e5987a2013-02-25 12:31:26 +11005379 /*
5380 * If we need to, add to list of extents to delete.
5381 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005382 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005383 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5384 error = xfs_refcount_decrease_extent(mp, dfops, del);
5385 if (error)
5386 goto done;
5387 } else
5388 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5389 del->br_blockcount, NULL);
5390 }
5391
Dave Chinner9e5987a2013-02-25 12:31:26 +11005392 /*
5393 * Adjust inode # blocks in the file.
5394 */
5395 if (nblks)
5396 ip->i_d.di_nblocks -= nblks;
5397 /*
5398 * Adjust quota data.
5399 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005400 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005401 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5402
5403 /*
5404 * Account for change in delayed indirect blocks.
5405 * Nothing to do for disk quota accounting here.
5406 */
5407 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005408 if (da_old > da_new)
5409 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005410done:
5411 *logflagsp = flags;
5412 return error;
5413}
5414
5415/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 * Unmap (remove) blocks from a file.
5417 * If nexts is nonzero then the number of extents to remove is limited to
5418 * that value. If not all extents in the block range can be removed then
5419 * *done is set.
5420 */
5421int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005422__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 xfs_trans_t *tp, /* transaction pointer */
5424 struct xfs_inode *ip, /* incore inode */
5425 xfs_fileoff_t bno, /* starting offset to unmap */
Darrick J. Wong44535932016-10-03 09:11:29 -07005426 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 int flags, /* misc flags */
5428 xfs_extnum_t nexts, /* number of extents max */
5429 xfs_fsblock_t *firstblock, /* first allocated block
5430 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005431 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432{
5433 xfs_btree_cur_t *cur; /* bmap btree cursor */
5434 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 int error; /* error return value */
5436 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005437 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 xfs_ifork_t *ifp; /* inode fork pointer */
5439 int isrt; /* freeing in rt area */
5440 xfs_extnum_t lastx; /* last extent index used */
5441 int logflags; /* transaction logging flags */
5442 xfs_extlen_t mod; /* rt extent offset */
5443 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 xfs_fileoff_t start; /* first file offset deleted */
5445 int tmp_logflags; /* partial logging flags */
5446 int wasdel; /* was a delayed alloc extent */
5447 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005449 xfs_filblks_t len = *rlen; /* length to unmap in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005451 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5452
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005453 whichfork = xfs_bmapi_whichfork(flags);
5454 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 ifp = XFS_IFORK_PTR(ip, whichfork);
5456 if (unlikely(
5457 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5458 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5459 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5460 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005461 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 }
5463 mp = ip->i_mount;
5464 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005465 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005466
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005467 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 ASSERT(len > 0);
5469 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005470
Linus Torvalds1da177e2005-04-16 15:20:36 -07005471 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5472 (error = xfs_iread_extents(tp, ip, whichfork)))
5473 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005474 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005475 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 return 0;
5477 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005478 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005479 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 start = bno;
5481 bno = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005482
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 /*
5484 * Check to see if the given block number is past the end of the
5485 * file, back up to the last block if so...
5486 */
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005487 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5488 ASSERT(lastx > 0);
5489 xfs_iext_get_extent(ifp, --lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005490 bno = got.br_startoff + got.br_blockcount - 1;
5491 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005492
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 logflags = 0;
5494 if (ifp->if_flags & XFS_IFBROOT) {
5495 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005496 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005498 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 cur->bc_private.b.flags = 0;
5500 } else
5501 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005502
5503 if (isrt) {
5504 /*
5505 * Synchronize by locking the bitmap inode.
5506 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005507 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005508 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005509 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5510 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005511 }
5512
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 extno = 0;
5514 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5515 (nexts == 0 || extno < nexts)) {
5516 /*
5517 * Is the found extent after a hole in which bno lives?
5518 * Just back up to the previous extent, if so.
5519 */
5520 if (got.br_startoff > bno) {
5521 if (--lastx < 0)
5522 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005523 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005524 }
5525 /*
5526 * Is the last block of this extent before the range
5527 * we're supposed to delete? If so, we're done.
5528 */
5529 bno = XFS_FILEOFF_MIN(bno,
5530 got.br_startoff + got.br_blockcount - 1);
5531 if (bno < start)
5532 break;
5533 /*
5534 * Then deal with the (possibly delayed) allocated space
5535 * we found.
5536 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005538 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 if (got.br_startoff < start) {
5540 del.br_startoff = start;
5541 del.br_blockcount -= start - got.br_startoff;
5542 if (!wasdel)
5543 del.br_startblock += start - got.br_startoff;
5544 }
5545 if (del.br_startoff + del.br_blockcount > bno + 1)
5546 del.br_blockcount = bno + 1 - del.br_startoff;
5547 sum = del.br_startblock + del.br_blockcount;
5548 if (isrt &&
5549 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5550 /*
5551 * Realtime extent not lined up at the end.
5552 * The extent could have been split into written
5553 * and unwritten pieces, or we could just be
5554 * unmapping part of it. But we can't really
5555 * get rid of part of a realtime extent.
5556 */
5557 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005558 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 /*
5560 * This piece is unwritten, or we're not
5561 * using unwritten extents. Skip over it.
5562 */
5563 ASSERT(bno >= mod);
5564 bno -= mod > del.br_blockcount ?
5565 del.br_blockcount : mod;
5566 if (bno < got.br_startoff) {
5567 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005568 xfs_bmbt_get_all(xfs_iext_get_ext(
5569 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 }
5571 continue;
5572 }
5573 /*
5574 * It's written, turn it unwritten.
5575 * This is better than zeroing it.
5576 */
5577 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005578 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 /*
5580 * If this spans a realtime extent boundary,
5581 * chop it back to the start of the one we end at.
5582 */
5583 if (del.br_blockcount > mod) {
5584 del.br_startoff += del.br_blockcount - mod;
5585 del.br_startblock += del.br_blockcount - mod;
5586 del.br_blockcount = mod;
5587 }
5588 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005589 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005590 whichfork, &lastx, &cur, &del,
5591 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 if (error)
5593 goto error0;
5594 goto nodelete;
5595 }
5596 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5597 /*
5598 * Realtime extent is lined up at the end but not
5599 * at the front. We'll get rid of full extents if
5600 * we can.
5601 */
5602 mod = mp->m_sb.sb_rextsize - mod;
5603 if (del.br_blockcount > mod) {
5604 del.br_blockcount -= mod;
5605 del.br_startoff += mod;
5606 del.br_startblock += mod;
5607 } else if ((del.br_startoff == start &&
5608 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005609 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005610 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005611 /*
5612 * Can't make it unwritten. There isn't
5613 * a full extent here so just skip it.
5614 */
5615 ASSERT(bno >= del.br_blockcount);
5616 bno -= del.br_blockcount;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005617 if (got.br_startoff > bno && --lastx >= 0)
5618 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 continue;
5620 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005621 struct xfs_bmbt_irec prev;
5622
Linus Torvalds1da177e2005-04-16 15:20:36 -07005623 /*
5624 * This one is already unwritten.
5625 * It must have a written left neighbor.
5626 * Unwrite the killed part of that one and
5627 * try again.
5628 */
5629 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005630 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005632 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 ASSERT(del.br_startblock ==
5634 prev.br_startblock + prev.br_blockcount);
5635 if (prev.br_startoff < start) {
5636 mod = start - prev.br_startoff;
5637 prev.br_blockcount -= mod;
5638 prev.br_startblock += mod;
5639 prev.br_startoff = start;
5640 }
5641 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005642 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005643 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005644 ip, whichfork, &lastx, &cur,
5645 &prev, firstblock, dfops,
5646 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 if (error)
5648 goto error0;
5649 goto nodelete;
5650 } else {
5651 ASSERT(del.br_state == XFS_EXT_NORM);
5652 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005653 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005654 ip, whichfork, &lastx, &cur,
5655 &del, firstblock, dfops,
5656 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 if (error)
5658 goto error0;
5659 goto nodelete;
5660 }
5661 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005662
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 /*
5664 * If it's the case where the directory code is running
5665 * with no block reservation, and the deleted block is in
5666 * the middle of its extent, and the resulting insert
5667 * of an extent would cause transformation to btree format,
5668 * then reject it. The calling code will then swap
5669 * blocks around instead.
5670 * We have to do this now, rather than waiting for the
5671 * conversion to btree format, since the transaction
5672 * will be dirty.
5673 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005674 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005676 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5677 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 del.br_startoff > got.br_startoff &&
5679 del.br_startoff + del.br_blockcount <
5680 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005681 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 goto error0;
5683 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005684
5685 /*
5686 * Unreserve quota and update realtime free space, if
5687 * appropriate. If delayed allocation, update the inode delalloc
5688 * counter now and wait to update the sb counters as
5689 * xfs_bmap_del_extent() might need to borrow some blocks.
5690 */
5691 if (wasdel) {
5692 ASSERT(startblockval(del.br_startblock) > 0);
5693 if (isrt) {
5694 xfs_filblks_t rtexts;
5695
5696 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5697 do_div(rtexts, mp->m_sb.sb_rextsize);
5698 xfs_mod_frextents(mp, (int64_t)rtexts);
5699 (void)xfs_trans_reserve_quota_nblks(NULL,
5700 ip, -((long)del.br_blockcount), 0,
5701 XFS_QMOPT_RES_RTBLKS);
5702 } else {
5703 (void)xfs_trans_reserve_quota_nblks(NULL,
5704 ip, -((long)del.br_blockcount), 0,
5705 XFS_QMOPT_RES_REGBLKS);
5706 }
5707 ip->i_delayed_blks -= del.br_blockcount;
5708 if (cur)
5709 cur->bc_private.b.flags |=
5710 XFS_BTCUR_BPRV_WASDEL;
5711 } else if (cur)
5712 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5713
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005714 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005715 &tmp_logflags, whichfork, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 logflags |= tmp_logflags;
5717 if (error)
5718 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005719
5720 if (!isrt && wasdel)
5721 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5722
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 bno = del.br_startoff - 1;
5724nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 /*
5726 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005729 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005730 xfs_iext_get_extent(ifp, lastx, &got);
5731 if (got.br_startoff > bno && --lastx >= 0)
5732 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 extno++;
5735 }
5736 }
Darrick J. Wong44535932016-10-03 09:11:29 -07005737 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5738 *rlen = 0;
5739 else
5740 *rlen = bno - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005741
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 /*
5743 * Convert to a btree if necessary.
5744 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005745 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005747 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 &cur, 0, &tmp_logflags, whichfork);
5749 logflags |= tmp_logflags;
5750 if (error)
5751 goto error0;
5752 }
5753 /*
5754 * transform from btree to extents, give it cur
5755 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005756 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 ASSERT(cur != NULL);
5758 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5759 whichfork);
5760 logflags |= tmp_logflags;
5761 if (error)
5762 goto error0;
5763 }
5764 /*
5765 * transform from extents to local?
5766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 error = 0;
5768error0:
5769 /*
5770 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005771 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005773 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005775 logflags &= ~xfs_ilog_fext(whichfork);
5776 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005778 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 /*
5780 * Log inode even in the error case, if the transaction
5781 * is dirty we'll need to shut down the filesystem.
5782 */
5783 if (logflags)
5784 xfs_trans_log_inode(tp, ip, logflags);
5785 if (cur) {
5786 if (!error) {
5787 *firstblock = cur->bc_private.b.firstblock;
5788 cur->bc_private.b.allocated = 0;
5789 }
5790 xfs_btree_del_cursor(cur,
5791 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5792 }
5793 return error;
5794}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005795
Darrick J. Wong44535932016-10-03 09:11:29 -07005796/* Unmap a range of a file. */
5797int
5798xfs_bunmapi(
5799 xfs_trans_t *tp,
5800 struct xfs_inode *ip,
5801 xfs_fileoff_t bno,
5802 xfs_filblks_t len,
5803 int flags,
5804 xfs_extnum_t nexts,
5805 xfs_fsblock_t *firstblock,
5806 struct xfs_defer_ops *dfops,
5807 int *done)
5808{
5809 int error;
5810
5811 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5812 dfops);
5813 *done = (len == 0);
5814 return error;
5815}
5816
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005817/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005818 * Determine whether an extent shift can be accomplished by a merge with the
5819 * extent that precedes the target hole of the shift.
5820 */
5821STATIC bool
5822xfs_bmse_can_merge(
5823 struct xfs_bmbt_irec *left, /* preceding extent */
5824 struct xfs_bmbt_irec *got, /* current extent to shift */
5825 xfs_fileoff_t shift) /* shift fsb */
5826{
5827 xfs_fileoff_t startoff;
5828
5829 startoff = got->br_startoff - shift;
5830
5831 /*
5832 * The extent, once shifted, must be adjacent in-file and on-disk with
5833 * the preceding extent.
5834 */
5835 if ((left->br_startoff + left->br_blockcount != startoff) ||
5836 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5837 (left->br_state != got->br_state) ||
5838 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5839 return false;
5840
5841 return true;
5842}
5843
5844/*
5845 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5846 * hole in the file. If an extent shift would result in the extent being fully
5847 * adjacent to the extent that currently precedes the hole, we can merge with
5848 * the preceding extent rather than do the shift.
5849 *
5850 * This function assumes the caller has verified a shift-by-merge is possible
5851 * with the provided extents via xfs_bmse_can_merge().
5852 */
5853STATIC int
5854xfs_bmse_merge(
5855 struct xfs_inode *ip,
5856 int whichfork,
5857 xfs_fileoff_t shift, /* shift fsb */
5858 int current_ext, /* idx of gotp */
5859 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5860 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5861 struct xfs_btree_cur *cur,
5862 int *logflags) /* output */
5863{
Brian Fosterddb19e32014-09-23 15:38:09 +10005864 struct xfs_bmbt_irec got;
5865 struct xfs_bmbt_irec left;
5866 xfs_filblks_t blockcount;
5867 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005868 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005869
Brian Fosterddb19e32014-09-23 15:38:09 +10005870 xfs_bmbt_get_all(gotp, &got);
5871 xfs_bmbt_get_all(leftp, &left);
5872 blockcount = left.br_blockcount + got.br_blockcount;
5873
5874 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5875 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5876 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5877
5878 /*
5879 * Merge the in-core extents. Note that the host record pointers and
5880 * current_ext index are invalid once the extent has been removed via
5881 * xfs_iext_remove().
5882 */
5883 xfs_bmbt_set_blockcount(leftp, blockcount);
5884 xfs_iext_remove(ip, current_ext, 1, 0);
5885
5886 /*
5887 * Update the on-disk extent count, the btree if necessary and log the
5888 * inode.
5889 */
5890 XFS_IFORK_NEXT_SET(ip, whichfork,
5891 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5892 *logflags |= XFS_ILOG_CORE;
5893 if (!cur) {
5894 *logflags |= XFS_ILOG_DEXT;
5895 return 0;
5896 }
5897
5898 /* lookup and remove the extent to merge */
5899 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5900 got.br_blockcount, &i);
5901 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005902 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005903 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005904
5905 error = xfs_btree_delete(cur, &i);
5906 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005907 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005908 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005909
5910 /* lookup and update size of the previous extent */
5911 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5912 left.br_blockcount, &i);
5913 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005914 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005915 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005916
5917 left.br_blockcount = blockcount;
5918
Dave Chinner4db431f2014-12-04 09:42:40 +11005919 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5920 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005921}
5922
5923/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005924 * Shift a single extent.
5925 */
5926STATIC int
5927xfs_bmse_shift_one(
5928 struct xfs_inode *ip,
5929 int whichfork,
5930 xfs_fileoff_t offset_shift_fsb,
5931 int *current_ext,
5932 struct xfs_bmbt_rec_host *gotp,
5933 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005934 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005935 enum shift_direction direction,
5936 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005937{
5938 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005939 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005940 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005941 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005942 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005943 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005944 int error;
5945 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005946 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005947
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005948 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005949 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005950 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005951
5952 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005953
Brian Fosterf71721d2014-09-23 15:39:05 +10005954 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005955 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005956
Namjae Jeona904b1c2015-03-25 15:08:56 +11005957 if (direction == SHIFT_LEFT) {
5958 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005959
Namjae Jeona904b1c2015-03-25 15:08:56 +11005960 /*
5961 * Check for merge if we've got an extent to the left,
5962 * otherwise make sure there's enough room at the start
5963 * of the file for the shift.
5964 */
5965 if (!*current_ext) {
5966 if (got.br_startoff < offset_shift_fsb)
5967 return -EINVAL;
5968 goto update_current_ext;
5969 }
5970 /*
5971 * grab the left extent and check for a large
5972 * enough hole.
5973 */
5974 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5975 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5976
5977 if (startoff <
5978 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005979 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005980
Dave Chinnerb11bd672014-12-04 09:42:24 +11005981 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005982 if (xfs_bmse_can_merge(&adj_irec, &got,
5983 offset_shift_fsb)) {
Darrick J. Wong9c194642016-08-03 12:16:05 +10005984 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5985 *current_ext, gotp, adj_irecp,
5986 cur, logflags);
5987 if (error)
5988 return error;
5989 adj_irec = got;
5990 goto update_rmap;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005991 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005992 } else {
5993 startoff = got.br_startoff + offset_shift_fsb;
5994 /* nothing to move if this is the last extent */
5995 if (*current_ext >= (total_extents - 1))
5996 goto update_current_ext;
5997 /*
5998 * If this is not the last extent in the file, make sure there
5999 * is enough room between current extent and next extent for
6000 * accommodating the shift.
6001 */
6002 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
6003 xfs_bmbt_get_all(adj_irecp, &adj_irec);
6004 if (startoff + got.br_blockcount > adj_irec.br_startoff)
6005 return -EINVAL;
6006 /*
6007 * Unlike a left shift (which involves a hole punch),
6008 * a right shift does not modify extent neighbors
6009 * in any way. We should never find mergeable extents
6010 * in this scenario. Check anyways and warn if we
6011 * encounter two extents that could be one.
6012 */
6013 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
6014 WARN_ON_ONCE(1);
6015 }
Brian Fostera979bdf2014-09-23 15:39:04 +10006016 /*
6017 * Increment the extent index for the next iteration, update the start
6018 * offset of the in-core extent and update the btree if applicable.
6019 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11006020update_current_ext:
6021 if (direction == SHIFT_LEFT)
6022 (*current_ext)++;
6023 else
6024 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10006025 xfs_bmbt_set_startoff(gotp, startoff);
6026 *logflags |= XFS_ILOG_CORE;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006027 adj_irec = got;
Brian Fostera979bdf2014-09-23 15:39:04 +10006028 if (!cur) {
6029 *logflags |= XFS_ILOG_DEXT;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006030 goto update_rmap;
Brian Fostera979bdf2014-09-23 15:39:04 +10006031 }
6032
6033 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
6034 got.br_blockcount, &i);
6035 if (error)
6036 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11006037 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10006038
6039 got.br_startoff = startoff;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006040 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
6041 got.br_blockcount, got.br_state);
6042 if (error)
6043 return error;
6044
6045update_rmap:
6046 /* update reverse mapping */
6047 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
6048 if (error)
6049 return error;
6050 adj_irec.br_startoff = startoff;
6051 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
Brian Fostera979bdf2014-09-23 15:39:04 +10006052}
6053
6054/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11006055 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006056 *
Brian Foster2c845f52014-09-23 15:37:09 +10006057 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11006058 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10006059 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6060 * is the length by which each extent is shifted. If there is no hole to shift
6061 * the extents into, this will be considered invalid operation and we abort
6062 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006063 */
6064int
6065xfs_bmap_shift_extents(
6066 struct xfs_trans *tp,
6067 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006068 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006069 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10006070 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006071 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006072 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006073 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006074 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006075 int num_exts)
6076{
Brian Fosterca446d82014-09-02 12:12:53 +10006077 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006078 struct xfs_bmbt_rec_host *gotp;
6079 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006080 struct xfs_mount *mp = ip->i_mount;
6081 struct xfs_ifork *ifp;
6082 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10006083 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006084 xfs_extnum_t total_extents;
6085 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006086 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006087 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10006088 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006089
6090 if (unlikely(XFS_TEST_ERROR(
6091 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6092 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6093 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6094 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6095 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10006096 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006097 }
6098
6099 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10006100 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006101
Brian Foster2c845f52014-09-23 15:37:09 +10006102 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6103 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006104 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6105 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006106
6107 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006108 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6109 /* Read in all the extents */
6110 error = xfs_iread_extents(tp, ip, whichfork);
6111 if (error)
6112 return error;
6113 }
6114
Brian Fosterddb19e32014-09-23 15:38:09 +10006115 if (ifp->if_flags & XFS_IFBROOT) {
6116 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6117 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006118 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006119 cur->bc_private.b.flags = 0;
6120 }
6121
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006122 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006123 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006124 * are collapsing out, so we cannot use the count of real extents here.
6125 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006126 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006127 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006128 if (total_extents == 0) {
6129 *done = 1;
6130 goto del_cursor;
6131 }
6132
6133 /*
6134 * In case of first right shift, we need to initialize next_fsb
6135 */
6136 if (*next_fsb == NULLFSBLOCK) {
6137 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6138 xfs_bmbt_get_all(gotp, &got);
6139 *next_fsb = got.br_startoff;
6140 if (stop_fsb > *next_fsb) {
6141 *done = 1;
6142 goto del_cursor;
6143 }
6144 }
6145
6146 /* Lookup the extent index at which we have to stop */
6147 if (direction == SHIFT_RIGHT) {
6148 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6149 /* Make stop_extent exclusive of shift range */
6150 stop_extent--;
6151 } else
6152 stop_extent = total_extents;
6153
6154 /*
6155 * Look up the extent index for the fsb where we start shifting. We can
6156 * henceforth iterate with current_ext as extent list changes are locked
6157 * out via ilock.
6158 *
6159 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6160 * *next_fsb lies in a hole beyond which there are no extents. Either
6161 * way, we are done.
6162 */
6163 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
6164 if (!gotp) {
6165 *done = 1;
6166 goto del_cursor;
6167 }
6168
6169 /* some sanity checking before we finally start shifting extents */
6170 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6171 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6172 error = -EIO;
6173 goto del_cursor;
6174 }
6175
6176 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006177 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006178 &current_ext, gotp, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006179 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006180 if (error)
6181 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006182 /*
6183 * If there was an extent merge during the shift, the extent
6184 * count can change. Update the total and grade the next record.
6185 */
6186 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006187 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006188 stop_extent = total_extents;
6189 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006190
Namjae Jeona904b1c2015-03-25 15:08:56 +11006191 if (current_ext == stop_extent) {
6192 *done = 1;
6193 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006194 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006195 }
Brian Fosterddb19e32014-09-23 15:38:09 +10006196 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006197 }
6198
Namjae Jeona904b1c2015-03-25 15:08:56 +11006199 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006200 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10006201 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10006202 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006203
6204del_cursor:
6205 if (cur)
6206 xfs_btree_del_cursor(cur,
6207 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6208
Brian Fosterca446d82014-09-02 12:12:53 +10006209 if (logflags)
6210 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006211
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006212 return error;
6213}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006214
6215/*
6216 * Splits an extent into two extents at split_fsb block such that it is
6217 * the first block of the current_ext. @current_ext is a target extent
6218 * to be split. @split_fsb is a block where the extents is split.
6219 * If split_fsb lies in a hole or the first block of extents, just return 0.
6220 */
6221STATIC int
6222xfs_bmap_split_extent_at(
6223 struct xfs_trans *tp,
6224 struct xfs_inode *ip,
6225 xfs_fileoff_t split_fsb,
6226 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006227 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006228{
6229 int whichfork = XFS_DATA_FORK;
6230 struct xfs_btree_cur *cur = NULL;
6231 struct xfs_bmbt_rec_host *gotp;
6232 struct xfs_bmbt_irec got;
6233 struct xfs_bmbt_irec new; /* split extent */
6234 struct xfs_mount *mp = ip->i_mount;
6235 struct xfs_ifork *ifp;
6236 xfs_fsblock_t gotblkcnt; /* new block count for got */
6237 xfs_extnum_t current_ext;
6238 int error = 0;
6239 int logflags = 0;
6240 int i = 0;
6241
6242 if (unlikely(XFS_TEST_ERROR(
6243 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6244 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6245 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6246 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6247 XFS_ERRLEVEL_LOW, mp);
6248 return -EFSCORRUPTED;
6249 }
6250
6251 if (XFS_FORCED_SHUTDOWN(mp))
6252 return -EIO;
6253
6254 ifp = XFS_IFORK_PTR(ip, whichfork);
6255 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6256 /* Read in all the extents */
6257 error = xfs_iread_extents(tp, ip, whichfork);
6258 if (error)
6259 return error;
6260 }
6261
6262 /*
6263 * gotp can be null in 2 cases: 1) if there are no extents
6264 * or 2) split_fsb lies in a hole beyond which there are
6265 * no extents. Either way, we are done.
6266 */
6267 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
6268 if (!gotp)
6269 return 0;
6270
6271 xfs_bmbt_get_all(gotp, &got);
6272
6273 /*
6274 * Check split_fsb lies in a hole or the start boundary offset
6275 * of the extent.
6276 */
6277 if (got.br_startoff >= split_fsb)
6278 return 0;
6279
6280 gotblkcnt = split_fsb - got.br_startoff;
6281 new.br_startoff = split_fsb;
6282 new.br_startblock = got.br_startblock + gotblkcnt;
6283 new.br_blockcount = got.br_blockcount - gotblkcnt;
6284 new.br_state = got.br_state;
6285
6286 if (ifp->if_flags & XFS_IFBROOT) {
6287 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6288 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006289 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006290 cur->bc_private.b.flags = 0;
6291 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6292 got.br_startblock,
6293 got.br_blockcount,
6294 &i);
6295 if (error)
6296 goto del_cursor;
6297 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6298 }
6299
6300 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6301 got.br_blockcount = gotblkcnt;
6302
6303 logflags = XFS_ILOG_CORE;
6304 if (cur) {
6305 error = xfs_bmbt_update(cur, got.br_startoff,
6306 got.br_startblock,
6307 got.br_blockcount,
6308 got.br_state);
6309 if (error)
6310 goto del_cursor;
6311 } else
6312 logflags |= XFS_ILOG_DEXT;
6313
6314 /* Add new extent */
6315 current_ext++;
6316 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6317 XFS_IFORK_NEXT_SET(ip, whichfork,
6318 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6319
6320 if (cur) {
6321 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6322 new.br_startblock, new.br_blockcount,
6323 &i);
6324 if (error)
6325 goto del_cursor;
6326 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6327 cur->bc_rec.b.br_state = new.br_state;
6328
6329 error = xfs_btree_insert(cur, &i);
6330 if (error)
6331 goto del_cursor;
6332 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6333 }
6334
6335 /*
6336 * Convert to a btree if necessary.
6337 */
6338 if (xfs_bmap_needs_btree(ip, whichfork)) {
6339 int tmp_logflags; /* partial log flag return val */
6340
6341 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006342 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006343 &cur, 0, &tmp_logflags, whichfork);
6344 logflags |= tmp_logflags;
6345 }
6346
6347del_cursor:
6348 if (cur) {
6349 cur->bc_private.b.allocated = 0;
6350 xfs_btree_del_cursor(cur,
6351 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6352 }
6353
6354 if (logflags)
6355 xfs_trans_log_inode(tp, ip, logflags);
6356 return error;
6357}
6358
6359int
6360xfs_bmap_split_extent(
6361 struct xfs_inode *ip,
6362 xfs_fileoff_t split_fsb)
6363{
6364 struct xfs_mount *mp = ip->i_mount;
6365 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006366 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006367 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006368 int error;
6369
Christoph Hellwig253f4912016-04-06 09:19:55 +10006370 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6371 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6372 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006373 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006374
6375 xfs_ilock(ip, XFS_ILOCK_EXCL);
6376 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6377
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006378 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006379
6380 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006381 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006382 if (error)
6383 goto out;
6384
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006385 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006386 if (error)
6387 goto out;
6388
Christoph Hellwig70393312015-06-04 13:48:08 +10006389 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006390
6391out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006392 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006393 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006394 return error;
6395}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006396
6397/* Deferred mapping is only for real extents in the data fork. */
6398static bool
6399xfs_bmap_is_update_needed(
6400 struct xfs_bmbt_irec *bmap)
6401{
6402 return bmap->br_startblock != HOLESTARTBLOCK &&
6403 bmap->br_startblock != DELAYSTARTBLOCK;
6404}
6405
6406/* Record a bmap intent. */
6407static int
6408__xfs_bmap_add(
6409 struct xfs_mount *mp,
6410 struct xfs_defer_ops *dfops,
6411 enum xfs_bmap_intent_type type,
6412 struct xfs_inode *ip,
6413 int whichfork,
6414 struct xfs_bmbt_irec *bmap)
6415{
6416 int error;
6417 struct xfs_bmap_intent *bi;
6418
6419 trace_xfs_bmap_defer(mp,
6420 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6421 type,
6422 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6423 ip->i_ino, whichfork,
6424 bmap->br_startoff,
6425 bmap->br_blockcount,
6426 bmap->br_state);
6427
6428 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6429 INIT_LIST_HEAD(&bi->bi_list);
6430 bi->bi_type = type;
6431 bi->bi_owner = ip;
6432 bi->bi_whichfork = whichfork;
6433 bi->bi_bmap = *bmap;
6434
6435 error = xfs_defer_join(dfops, bi->bi_owner);
6436 if (error) {
6437 kmem_free(bi);
6438 return error;
6439 }
6440
6441 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6442 return 0;
6443}
6444
6445/* Map an extent into a file. */
6446int
6447xfs_bmap_map_extent(
6448 struct xfs_mount *mp,
6449 struct xfs_defer_ops *dfops,
6450 struct xfs_inode *ip,
6451 struct xfs_bmbt_irec *PREV)
6452{
6453 if (!xfs_bmap_is_update_needed(PREV))
6454 return 0;
6455
6456 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6457 XFS_DATA_FORK, PREV);
6458}
6459
6460/* Unmap an extent out of a file. */
6461int
6462xfs_bmap_unmap_extent(
6463 struct xfs_mount *mp,
6464 struct xfs_defer_ops *dfops,
6465 struct xfs_inode *ip,
6466 struct xfs_bmbt_irec *PREV)
6467{
6468 if (!xfs_bmap_is_update_needed(PREV))
6469 return 0;
6470
6471 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6472 XFS_DATA_FORK, PREV);
6473}
6474
6475/*
6476 * Process one of the deferred bmap operations. We pass back the
6477 * btree cursor to maintain our lock on the bmapbt between calls.
6478 */
6479int
6480xfs_bmap_finish_one(
6481 struct xfs_trans *tp,
6482 struct xfs_defer_ops *dfops,
6483 struct xfs_inode *ip,
6484 enum xfs_bmap_intent_type type,
6485 int whichfork,
6486 xfs_fileoff_t startoff,
6487 xfs_fsblock_t startblock,
6488 xfs_filblks_t blockcount,
6489 xfs_exntst_t state)
6490{
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006491 int error = 0, done;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006492
6493 trace_xfs_bmap_deferred(tp->t_mountp,
6494 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6495 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6496 ip->i_ino, whichfork, startoff, blockcount, state);
6497
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006498 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006499 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006500
6501 if (XFS_TEST_ERROR(false, tp->t_mountp,
6502 XFS_ERRTAG_BMAP_FINISH_ONE,
6503 XFS_RANDOM_BMAP_FINISH_ONE))
6504 return -EIO;
6505
6506 switch (type) {
6507 case XFS_BMAP_MAP:
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006508 error = xfs_bmapi_remap(tp, ip, startoff, blockcount,
6509 startblock, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006510 break;
6511 case XFS_BMAP_UNMAP:
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006512 error = xfs_bunmapi(tp, ip, startoff, blockcount,
6513 XFS_BMAPI_REMAP, 1, &startblock, dfops, &done);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006514 ASSERT(done);
6515 break;
6516 default:
6517 ASSERT(0);
6518 error = -EFSCORRUPTED;
6519 }
6520
6521 return error;
6522}