blob: 9558f5ee1bf96b928544d0b9ecfe1c4ef881e1ed [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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100210 level - 1;
Darrick J. Wongfd26a882016-11-28 14:57:42 +1100211 break;
212 }
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100352 */
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100600{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000601 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100746 /*
747 * Need a cursor. Can't allocate until bb_level is filled in.
748 */
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100786 /*
787 * Allocation can't fail, the space was reserved.
788 */
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700883 struct xfs_bmbt_irec rec;
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100944 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000945
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700946 rec.br_startoff = 0;
947 rec.br_startblock = args.fsbno;
948 rec.br_blockcount = 1;
949 rec.br_state = XFS_EXT_NORM;
950 xfs_iext_insert(ip, 0, 1, &rec, 0);
951
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000952 trace_xfs_bmap_post_update(ip, 0,
953 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
954 _THIS_IP_);
955 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
956 ip->i_d.di_nblocks = 1;
957 xfs_trans_mod_dquot_byino(tp, ip,
958 XFS_TRANS_DQ_BCOUNT, 1L);
959 flags |= xfs_ilog_fext(whichfork);
960
Dave Chinner9e5987a72013-02-25 12:31:26 +1100961done:
962 *logflagsp = flags;
963 return error;
964}
965
966/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 * Called from xfs_bmap_add_attrfork to handle btree format files.
968 */
969STATIC int /* error */
970xfs_bmap_add_attrfork_btree(
971 xfs_trans_t *tp, /* transaction pointer */
972 xfs_inode_t *ip, /* incore inode pointer */
973 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000974 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 int *flags) /* inode logging flags */
976{
977 xfs_btree_cur_t *cur; /* btree cursor */
978 int error; /* error return value */
979 xfs_mount_t *mp; /* file system mount struct */
980 int stat; /* newroot status */
981
982 mp = ip->i_mount;
983 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
984 *flags |= XFS_ILOG_DBROOT;
985 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100986 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000987 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 cur->bc_private.b.firstblock = *firstblock;
989 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
990 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000991 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100992 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100993 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 goto error0;
995 if (stat == 0) {
996 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000997 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999 *firstblock = cur->bc_private.b.firstblock;
1000 cur->bc_private.b.allocated = 0;
1001 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1002 }
1003 return 0;
1004error0:
1005 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1006 return error;
1007}
1008
1009/*
1010 * Called from xfs_bmap_add_attrfork to handle extents format files.
1011 */
1012STATIC int /* error */
1013xfs_bmap_add_attrfork_extents(
1014 xfs_trans_t *tp, /* transaction pointer */
1015 xfs_inode_t *ip, /* incore inode pointer */
1016 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001017 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 int *flags) /* inode logging flags */
1019{
1020 xfs_btree_cur_t *cur; /* bmap btree cursor */
1021 int error; /* error return value */
1022
1023 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1024 return 0;
1025 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001026 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 flags, XFS_DATA_FORK);
1028 if (cur) {
1029 cur->bc_private.b.allocated = 0;
1030 xfs_btree_del_cursor(cur,
1031 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1032 }
1033 return error;
1034}
1035
1036/*
Dave Chinner1e823792013-02-11 15:58:13 +11001037 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1038 * different data fork content type needs a different callout to do the
1039 * conversion. Some are basic and only require special block initialisation
1040 * callouts for the data formating, others (directories) are so specialised they
1041 * handle everything themselves.
1042 *
1043 * XXX (dgc): investigate whether directory conversion can use the generic
1044 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001045 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 */
1047STATIC int /* error */
1048xfs_bmap_add_attrfork_local(
1049 xfs_trans_t *tp, /* transaction pointer */
1050 xfs_inode_t *ip, /* incore inode pointer */
1051 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001052 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 int *flags) /* inode logging flags */
1054{
1055 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1058 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001059
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001060 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001062 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 dargs.dp = ip;
1064 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001065 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001066 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 dargs.whichfork = XFS_DATA_FORK;
1068 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001069 return xfs_dir2_sf_to_block(&dargs);
1070 }
1071
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001072 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001073 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1074 flags, XFS_DATA_FORK,
1075 xfs_symlink_local_to_remote);
1076
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001077 /* should only be called for types that support local format data */
1078 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001079 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001083 * Convert inode from non-attributed to attributed.
1084 * Must not be in a transaction, ip must not be locked.
1085 */
1086int /* error code */
1087xfs_bmap_add_attrfork(
1088 xfs_inode_t *ip, /* incore inode pointer */
1089 int size, /* space new attribute needs */
1090 int rsvd) /* xact may use reserved blks */
1091{
1092 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001093 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001094 xfs_mount_t *mp; /* mount structure */
1095 xfs_trans_t *tp; /* transaction pointer */
1096 int blks; /* space reservation */
1097 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001098 int logflags; /* logging flags */
1099 int error; /* error return value */
1100
1101 ASSERT(XFS_IFORK_Q(ip) == 0);
1102
1103 mp = ip->i_mount;
1104 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001105
Dave Chinner9e5987a72013-02-25 12:31:26 +11001106 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001107
1108 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1109 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1110 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001111 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001112
Dave Chinner9e5987a72013-02-25 12:31:26 +11001113 xfs_ilock(ip, XFS_ILOCK_EXCL);
1114 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1115 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1116 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001117 if (error)
1118 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001119 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001120 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001121 if (ip->i_d.di_anextents != 0) {
1122 error = -EFSCORRUPTED;
1123 goto trans_cancel;
1124 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001125 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1126 /*
1127 * For inodes coming from pre-6.2 filesystems.
1128 */
1129 ASSERT(ip->i_d.di_aformat == 0);
1130 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1131 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001132
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001133 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001134 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1135
1136 switch (ip->i_d.di_format) {
1137 case XFS_DINODE_FMT_DEV:
1138 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1139 break;
1140 case XFS_DINODE_FMT_UUID:
1141 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1142 break;
1143 case XFS_DINODE_FMT_LOCAL:
1144 case XFS_DINODE_FMT_EXTENTS:
1145 case XFS_DINODE_FMT_BTREE:
1146 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1147 if (!ip->i_d.di_forkoff)
1148 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1149 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1150 version = 2;
1151 break;
1152 default:
1153 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001154 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001155 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001156 }
1157
1158 ASSERT(ip->i_afp == NULL);
1159 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1160 ip->i_afp->if_flags = XFS_IFEXTENTS;
1161 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001162 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001163 switch (ip->i_d.di_format) {
1164 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001165 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001166 &logflags);
1167 break;
1168 case XFS_DINODE_FMT_EXTENTS:
1169 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001170 &dfops, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001171 break;
1172 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001173 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001174 &logflags);
1175 break;
1176 default:
1177 error = 0;
1178 break;
1179 }
1180 if (logflags)
1181 xfs_trans_log_inode(tp, ip, logflags);
1182 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001183 goto bmap_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001184 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1185 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001186 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001187
1188 spin_lock(&mp->m_sb_lock);
1189 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1190 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001191 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001192 }
1193 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1194 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001195 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001196 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001197 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001198 if (log_sb)
1199 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001200 }
1201
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001202 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001203 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001204 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001205 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001206 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001207 return error;
1208
1209bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001210 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001211trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001212 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001213 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001214 return error;
1215}
1216
1217/*
1218 * Internal and external extent tree search functions.
1219 */
1220
1221/*
1222 * Read in the extents to if_extents.
1223 * All inode fields are set up by caller, we just traverse the btree
1224 * and copy the records in. If the file system cannot contain unwritten
1225 * extents, the records are checked for no "state" flags.
1226 */
1227int /* error */
1228xfs_bmap_read_extents(
1229 xfs_trans_t *tp, /* transaction pointer */
1230 xfs_inode_t *ip, /* incore inode */
1231 int whichfork) /* data or attr fork */
1232{
1233 struct xfs_btree_block *block; /* current btree block */
1234 xfs_fsblock_t bno; /* block # of "block" */
1235 xfs_buf_t *bp; /* buffer for "block" */
1236 int error; /* error return value */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001237 xfs_extnum_t i, j; /* index into the extents list */
1238 xfs_ifork_t *ifp; /* fork structure */
1239 int level; /* btree level, for checking */
1240 xfs_mount_t *mp; /* file system mount structure */
1241 __be64 *pp; /* pointer to block address */
1242 /* REFERENCED */
1243 xfs_extnum_t room; /* number of entries there's room for */
1244
Dave Chinner9e5987a72013-02-25 12:31:26 +11001245 mp = ip->i_mount;
1246 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001247 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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001286
1287 num_recs = xfs_btree_get_numrecs(block);
1288 if (unlikely(i + num_recs > room)) {
1289 ASSERT(i + num_recs <= room);
1290 xfs_warn(ip->i_mount,
1291 "corrupt dinode %Lu, (btree extents).",
1292 (unsigned long long) ip->i_ino);
1293 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1294 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1295 goto error0;
1296 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001297 /*
1298 * Read-ahead the next leaf block, if any.
1299 */
1300 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1301 if (nextbno != NULLFSBLOCK)
1302 xfs_btree_reada_bufl(mp, nextbno, 1,
1303 &xfs_bmbt_buf_ops);
1304 /*
1305 * Copy records into the extent records.
1306 */
1307 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001308 for (j = 0; j < num_recs; j++, i++, frp++) {
1309 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1310 trp->l0 = be64_to_cpu(frp->l0);
1311 trp->l1 = be64_to_cpu(frp->l1);
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001312 if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001313 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001314 XFS_ERRLEVEL_LOW, mp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001315 goto error0;
1316 }
1317 }
1318 xfs_trans_brelse(tp, bp);
1319 bno = nextbno;
1320 /*
1321 * If we've reached the end, stop.
1322 */
1323 if (bno == NULLFSBLOCK)
1324 break;
1325 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1326 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1327 if (error)
1328 return error;
1329 block = XFS_BUF_TO_BLOCK(bp);
1330 }
Darrick J. Wong356a3222016-12-05 12:36:56 +11001331 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1332 return -EFSCORRUPTED;
Eric Sandeen5d829302016-11-08 12:59:42 +11001333 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a72013-02-25 12:31:26 +11001334 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1335 return 0;
1336error0:
1337 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001338 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001339}
1340
Dave Chinner9e5987a72013-02-25 12:31:26 +11001341/*
1342 * Returns the file-relative block number of the first unused block(s)
1343 * in the file with at least "len" logically contiguous blocks free.
1344 * This is the lowest-address hole if the file has holes, else the first block
1345 * past the end of file.
1346 * Return 0 if the file is currently local (in-inode).
1347 */
1348int /* error */
1349xfs_bmap_first_unused(
1350 xfs_trans_t *tp, /* transaction pointer */
1351 xfs_inode_t *ip, /* incore inode */
1352 xfs_extlen_t len, /* size of hole to find */
1353 xfs_fileoff_t *first_unused, /* unused block */
1354 int whichfork) /* data or attr fork */
1355{
1356 int error; /* error return value */
1357 int idx; /* extent record index */
1358 xfs_ifork_t *ifp; /* inode fork pointer */
1359 xfs_fileoff_t lastaddr; /* last block number seen */
1360 xfs_fileoff_t lowest; /* lowest useful block */
1361 xfs_fileoff_t max; /* starting useful block */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001362 xfs_extnum_t nextents; /* number of extent entries */
1363
1364 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1365 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1366 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1367 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1368 *first_unused = 0;
1369 return 0;
1370 }
1371 ifp = XFS_IFORK_PTR(ip, whichfork);
1372 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1373 (error = xfs_iread_extents(tp, ip, whichfork)))
1374 return error;
1375 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001376 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001377 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001378 struct xfs_bmbt_irec got;
1379
1380 xfs_iext_get_extent(ifp, idx, &got);
1381
Dave Chinner9e5987a72013-02-25 12:31:26 +11001382 /*
1383 * See if the hole before this extent will work.
1384 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001385 if (got.br_startoff >= lowest + len &&
1386 got.br_startoff - max >= len) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001387 *first_unused = max;
1388 return 0;
1389 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001390 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001391 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1392 }
1393 *first_unused = max;
1394 return 0;
1395}
1396
1397/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001398 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001399 * last_block (input value) in the file.
1400 * This is not based on i_size, it is based on the extent records.
1401 * Returns 0 for local files, as they do not have extent records.
1402 */
1403int /* error */
1404xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001405 struct xfs_trans *tp, /* transaction pointer */
1406 struct xfs_inode *ip, /* incore inode */
1407 xfs_fileoff_t *last_block, /* last block */
1408 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001409{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001410 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1411 struct xfs_bmbt_irec got;
1412 xfs_extnum_t idx;
1413 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001414
Christoph Hellwig86685f72016-11-24 11:39:38 +11001415 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1416 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001417 *last_block = 0;
1418 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001419 case XFS_DINODE_FMT_BTREE:
1420 case XFS_DINODE_FMT_EXTENTS:
1421 break;
1422 default:
1423 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001424 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001425
1426 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1427 error = xfs_iread_extents(tp, ip, whichfork);
1428 if (error)
1429 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001430 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001431
1432 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1433 if (got.br_startoff <= *last_block - 1)
1434 return 0;
1435 }
1436
1437 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1438 *last_block = got.br_startoff + got.br_blockcount;
1439 return 0;
1440 }
1441
1442 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001443 return 0;
1444}
1445
Dave Chinner68988112013-08-12 20:49:42 +10001446int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001447xfs_bmap_last_extent(
1448 struct xfs_trans *tp,
1449 struct xfs_inode *ip,
1450 int whichfork,
1451 struct xfs_bmbt_irec *rec,
1452 int *is_empty)
1453{
1454 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1455 int error;
1456 int nextents;
1457
1458 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1459 error = xfs_iread_extents(tp, ip, whichfork);
1460 if (error)
1461 return error;
1462 }
1463
Eric Sandeen5d829302016-11-08 12:59:42 +11001464 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001465 if (nextents == 0) {
1466 *is_empty = 1;
1467 return 0;
1468 }
1469
1470 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1471 *is_empty = 0;
1472 return 0;
1473}
1474
1475/*
1476 * Check the last inode extent to determine whether this allocation will result
1477 * in blocks being allocated at the end of the file. When we allocate new data
1478 * blocks at the end of the file which do not start at the previous data block,
1479 * we will try to align the new blocks at stripe unit boundaries.
1480 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001481 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001482 * at, or past the EOF.
1483 */
1484STATIC int
1485xfs_bmap_isaeof(
1486 struct xfs_bmalloca *bma,
1487 int whichfork)
1488{
1489 struct xfs_bmbt_irec rec;
1490 int is_empty;
1491 int error;
1492
1493 bma->aeof = 0;
1494 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1495 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001496 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001497 return error;
1498
Dave Chinner6e708bc2013-11-22 10:41:16 +11001499 if (is_empty) {
1500 bma->aeof = 1;
1501 return 0;
1502 }
1503
Dave Chinner9e5987a72013-02-25 12:31:26 +11001504 /*
1505 * Check if we are allocation or past the last extent, or at least into
1506 * the last delayed allocated extent.
1507 */
1508 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1509 (bma->offset >= rec.br_startoff &&
1510 isnullstartblock(rec.br_startblock));
1511 return 0;
1512}
1513
1514/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001515 * Returns the file-relative block number of the first block past eof in
1516 * the file. This is not based on i_size, it is based on the extent records.
1517 * Returns 0 for local files, as they do not have extent records.
1518 */
1519int
1520xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001521 struct xfs_inode *ip,
1522 xfs_fileoff_t *last_block,
1523 int whichfork)
1524{
1525 struct xfs_bmbt_irec rec;
1526 int is_empty;
1527 int error;
1528
1529 *last_block = 0;
1530
1531 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1532 return 0;
1533
1534 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1535 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001536 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001537
1538 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1539 if (error || is_empty)
1540 return error;
1541
1542 *last_block = rec.br_startoff + rec.br_blockcount;
1543 return 0;
1544}
1545
1546/*
1547 * Returns whether the selected fork of the inode has exactly one
1548 * block or not. For the data fork we check this matches di_size,
1549 * implying the file's range is 0..bsize-1.
1550 */
1551int /* 1=>1 block, 0=>otherwise */
1552xfs_bmap_one_block(
1553 xfs_inode_t *ip, /* incore inode */
1554 int whichfork) /* data or attr fork */
1555{
1556 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1557 xfs_ifork_t *ifp; /* inode fork pointer */
1558 int rval; /* return value */
1559 xfs_bmbt_irec_t s; /* internal version of extent */
1560
1561#ifndef DEBUG
1562 if (whichfork == XFS_DATA_FORK)
1563 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1564#endif /* !DEBUG */
1565 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1566 return 0;
1567 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1568 return 0;
1569 ifp = XFS_IFORK_PTR(ip, whichfork);
1570 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1571 ep = xfs_iext_get_ext(ifp, 0);
1572 xfs_bmbt_get_all(ep, &s);
1573 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1574 if (rval && whichfork == XFS_DATA_FORK)
1575 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1576 return rval;
1577}
1578
1579/*
1580 * Extent tree manipulation functions used during allocation.
1581 */
1582
1583/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001584 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 */
1586STATIC int /* error */
1587xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001588 struct xfs_bmalloca *bma,
1589 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001591 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001593 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001596 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 xfs_fileoff_t new_endoff; /* end offset of new entry */
1598 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1599 /* left is 0, right is 1, prev is 2 */
1600 int rval=0; /* return value (logging flags) */
1601 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001602 xfs_filblks_t da_new; /* new count del alloc blocks used */
1603 xfs_filblks_t da_old; /* old count del alloc blocks used */
1604 xfs_filblks_t temp=0; /* value for da_new calculations */
1605 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001607 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001608 xfs_extnum_t *nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001610 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001611 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001612 ASSERT(whichfork != XFS_ATTR_FORK);
1613 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1614 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001615
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001616 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001617 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001618 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001619 ASSERT(!bma->cur ||
1620 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001621
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001622 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624#define LEFT r[0]
1625#define RIGHT r[1]
1626#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Darrick J. Wong60b49842016-10-03 09:11:34 -07001628 if (whichfork == XFS_COW_FORK)
1629 state |= BMAP_COWFORK;
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /*
1632 * Set up a bunch of variables to make the tests simpler.
1633 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001634 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 xfs_bmbt_get_all(ep, &PREV);
1636 new_endoff = new->br_startoff + new->br_blockcount;
1637 ASSERT(PREV.br_startoff <= new->br_startoff);
1638 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001639
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001640 da_old = startblockval(PREV.br_startblock);
1641 da_new = 0;
1642
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 /*
1644 * Set flags determining what part of the previous delayed allocation
1645 * extent is being replaced by a real allocation.
1646 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001647 if (PREV.br_startoff == new->br_startoff)
1648 state |= BMAP_LEFT_FILLING;
1649 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1650 state |= BMAP_RIGHT_FILLING;
1651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 /*
1653 * Check and set flags if this segment has a left neighbor.
1654 * Don't set contiguous if the combined extent would be too large.
1655 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001656 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001657 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001658 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001659
1660 if (isnullstartblock(LEFT.br_startblock))
1661 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001663
1664 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1665 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1666 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1667 LEFT.br_state == new->br_state &&
1668 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1669 state |= BMAP_LEFT_CONTIG;
1670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 /*
1672 * Check and set flags if this segment has a right neighbor.
1673 * Don't set contiguous if the combined extent would be too large.
1674 * Also check for all-three-contiguous being too large.
1675 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001676 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001677 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001678 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001679
1680 if (isnullstartblock(RIGHT.br_startblock))
1681 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001683
1684 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1685 new_endoff == RIGHT.br_startoff &&
1686 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1687 new->br_state == RIGHT.br_state &&
1688 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1689 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1690 BMAP_RIGHT_FILLING)) !=
1691 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1692 BMAP_RIGHT_FILLING) ||
1693 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1694 <= MAXEXTLEN))
1695 state |= BMAP_RIGHT_CONTIG;
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 error = 0;
1698 /*
1699 * Switch out based on the FILLING and CONTIG state bits.
1700 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001701 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1702 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1703 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1704 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 /*
1706 * Filling in all of a previously delayed allocation extent.
1707 * The left and right neighbors are both contiguous with new.
1708 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001709 bma->idx--;
1710 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1711 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 LEFT.br_blockcount + PREV.br_blockcount +
1713 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001714 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001715
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001716 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001717 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001718 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1720 else {
1721 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001722 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001724 RIGHT.br_blockcount, &i);
1725 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001727 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001728 error = xfs_btree_delete(bma->cur, &i);
1729 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001731 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001732 error = xfs_btree_decrement(bma->cur, 0, &i);
1733 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001735 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001736 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 LEFT.br_startblock,
1738 LEFT.br_blockcount +
1739 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001740 RIGHT.br_blockcount, LEFT.br_state);
1741 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 goto done;
1743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 break;
1745
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001746 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 /*
1748 * Filling in all of a previously delayed allocation extent.
1749 * The left neighbor is contiguous, the right is not.
1750 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001751 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001752
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001753 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1754 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001755 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001756 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001757
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001758 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1759 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 rval = XFS_ILOG_DEXT;
1761 else {
1762 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001763 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001765 &i);
1766 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001768 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001769 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 LEFT.br_startblock,
1771 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001772 PREV.br_blockcount, LEFT.br_state);
1773 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 goto done;
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 break;
1777
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001778 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 /*
1780 * Filling in all of a previously delayed allocation extent.
1781 * The right neighbor is contiguous, the left is not.
1782 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001783 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 xfs_bmbt_set_startblock(ep, new->br_startblock);
1785 xfs_bmbt_set_blockcount(ep,
1786 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001787 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001788
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001789 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1790 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 rval = XFS_ILOG_DEXT;
1792 else {
1793 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001794 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001796 RIGHT.br_blockcount, &i);
1797 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001799 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001800 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 new->br_startblock,
1802 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001803 RIGHT.br_blockcount, PREV.br_state);
1804 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 goto done;
1806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 break;
1808
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001809 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 /*
1811 * Filling in all of a previously delayed allocation extent.
1812 * Neither the left nor right neighbors are contiguous with
1813 * the new one.
1814 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001815 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 xfs_bmbt_set_startblock(ep, new->br_startblock);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08001817 xfs_bmbt_set_state(ep, new->br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001818 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001819
Darrick J. Wong60b49842016-10-03 09:11:34 -07001820 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001821 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1823 else {
1824 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001825 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001827 &i);
1828 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001830 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001831 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1832 error = xfs_btree_insert(bma->cur, &i);
1833 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001835 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 break;
1838
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001839 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 /*
1841 * Filling in the first part of a previous delayed allocation.
1842 * The left neighbor is contiguous.
1843 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001844 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1845 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 LEFT.br_blockcount + new->br_blockcount);
1847 xfs_bmbt_set_startoff(ep,
1848 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001849 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001852 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001854 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 rval = XFS_ILOG_DEXT;
1856 else {
1857 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001858 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001860 &i);
1861 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001863 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001864 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 LEFT.br_startblock,
1866 LEFT.br_blockcount +
1867 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001868 LEFT.br_state);
1869 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto done;
1871 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001872 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001873 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001874 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001875 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001876
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001877 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 break;
1879
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001880 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 /*
1882 * Filling in the first part of a previous delayed allocation.
1883 * The left neighbor is not contiguous.
1884 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001885 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 xfs_bmbt_set_startoff(ep, new_endoff);
1887 temp = PREV.br_blockcount - new->br_blockcount;
1888 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001889 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001890 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001891 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1893 else {
1894 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001895 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001897 &i);
1898 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001900 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001901 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1902 error = xfs_btree_insert(bma->cur, &i);
1903 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001905 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001907
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001908 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001909 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001910 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001911 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 rval |= tmp_rval;
1913 if (error)
1914 goto done;
1915 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001916 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001917 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001918 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1919 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001920 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001921 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 break;
1923
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001924 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 /*
1926 * Filling in the last part of a previous delayed allocation.
1927 * The right neighbor is contiguous with the new allocation.
1928 */
1929 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001930 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001932 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001933 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 new->br_blockcount + RIGHT.br_blockcount,
1935 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001936 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1937 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 rval = XFS_ILOG_DEXT;
1939 else {
1940 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001941 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001943 RIGHT.br_blockcount, &i);
1944 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001946 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001947 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 new->br_startblock,
1949 new->br_blockcount +
1950 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001951 RIGHT.br_state);
1952 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 goto done;
1954 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001955
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001956 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001957 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001959 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001960 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001961
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001962 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 break;
1964
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001965 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 /*
1967 * Filling in the last part of a previous delayed allocation.
1968 * The right neighbor is not contiguous.
1969 */
1970 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001971 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001973 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001974 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001975 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1977 else {
1978 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001979 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001981 &i);
1982 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001984 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001985 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1986 error = xfs_btree_insert(bma->cur, &i);
1987 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001989 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001991
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001992 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001993 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001994 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001995 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 rval |= tmp_rval;
1997 if (error)
1998 goto done;
1999 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002000 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002001 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002002 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2003 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002004 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002005 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002006
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002007 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 break;
2009
2010 case 0:
2011 /*
2012 * Filling in the middle part of a previous delayed allocation.
2013 * Contiguity is impossible here.
2014 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002015 *
2016 * We start with a delayed allocation:
2017 *
2018 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2019 * PREV @ idx
2020 *
2021 * and we are allocating:
2022 * +rrrrrrrrrrrrrrrrr+
2023 * new
2024 *
2025 * and we set it up for insertion as:
2026 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2027 * new
2028 * PREV @ idx LEFT RIGHT
2029 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 */
2031 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002033 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002034 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2035 LEFT = *new;
2036 RIGHT.br_state = PREV.br_state;
2037 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002038 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002039 RIGHT.br_startoff = new_endoff;
2040 RIGHT.br_blockcount = temp2;
2041 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002042 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002043 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002044 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2046 else {
2047 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002048 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002050 &i);
2051 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002053 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002054 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2055 error = xfs_btree_insert(bma->cur, &i);
2056 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002058 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002060
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002061 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002062 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002063 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002064 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 rval |= tmp_rval;
2066 if (error)
2067 goto done;
2068 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002069 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2070 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Brian Foster0daaeca2017-05-12 10:44:08 -07002071 diff = (int)(temp + temp2 -
2072 (startblockval(PREV.br_startblock) -
2073 (bma->cur ?
2074 bma->cur->bc_private.b.allocated : 0)));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002075 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002076 error = xfs_mod_fdblocks(bma->ip->i_mount,
2077 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002078 ASSERT(!error);
2079 if (error)
2080 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002082
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002083 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002084 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002085 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2086 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2087 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002088 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002089 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002090
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002091 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002092 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 break;
2094
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002095 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2096 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2097 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2098 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2099 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2100 case BMAP_LEFT_CONTIG:
2101 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 /*
2103 * These cases are all impossible.
2104 */
2105 ASSERT(0);
2106 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002107
Darrick J. Wong9c194642016-08-03 12:16:05 +10002108 /* add reverse mapping */
2109 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2110 if (error)
2111 goto done;
2112
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002113 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002114 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002115 int tmp_logflags; /* partial log flag return val */
2116
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002117 ASSERT(bma->cur == NULL);
2118 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002119 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002120 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002121 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002122 if (error)
2123 goto done;
2124 }
2125
2126 /* adjust for changes in reserved delayed indirect blocks */
2127 if (da_old || da_new) {
2128 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002129 if (bma->cur)
2130 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002131 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002132 xfs_mod_fdblocks(bma->ip->i_mount,
2133 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002134 }
2135
2136 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002137 if (bma->cur)
2138 bma->cur->bc_private.b.allocated = 0;
2139
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002140 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002142 if (whichfork != XFS_COW_FORK)
2143 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return error;
2145#undef LEFT
2146#undef RIGHT
2147#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
2150/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002151 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 */
2153STATIC int /* error */
2154xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002155 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002157 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002158 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002160 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002161 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002162 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002163 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002166 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002169 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 xfs_fileoff_t new_endoff; /* end offset of new entry */
2171 xfs_exntst_t newext; /* new extent state */
2172 xfs_exntst_t oldext; /* old extent state */
2173 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2174 /* left is 0, right is 1, prev is 2 */
2175 int rval=0; /* return value (logging flags) */
2176 int state = 0;/* state bits, accessed thru macros */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002177 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002179 *logflagsp = 0;
2180
2181 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002182 ifp = XFS_IFORK_PTR(ip, whichfork);
2183 if (whichfork == XFS_COW_FORK)
2184 state |= BMAP_COWFORK;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002185
2186 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002187 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002188 ASSERT(!isnullstartblock(new->br_startblock));
2189
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002190 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002191
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192#define LEFT r[0]
2193#define RIGHT r[1]
2194#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 /*
2197 * Set up a bunch of variables to make the tests simpler.
2198 */
2199 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002200 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 xfs_bmbt_get_all(ep, &PREV);
2202 newext = new->br_state;
2203 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2204 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2205 ASSERT(PREV.br_state == oldext);
2206 new_endoff = new->br_startoff + new->br_blockcount;
2207 ASSERT(PREV.br_startoff <= new->br_startoff);
2208 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002209
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 /*
2211 * Set flags determining what part of the previous oldext allocation
2212 * extent is being replaced by a newext allocation.
2213 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002214 if (PREV.br_startoff == new->br_startoff)
2215 state |= BMAP_LEFT_FILLING;
2216 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2217 state |= BMAP_RIGHT_FILLING;
2218
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 /*
2220 * Check and set flags if this segment has a left neighbor.
2221 * Don't set contiguous if the combined extent would be too large.
2222 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002223 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002224 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002225 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002226
2227 if (isnullstartblock(LEFT.br_startblock))
2228 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002230
2231 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2232 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2233 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2234 LEFT.br_state == newext &&
2235 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2236 state |= BMAP_LEFT_CONTIG;
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 /*
2239 * Check and set flags if this segment has a right neighbor.
2240 * Don't set contiguous if the combined extent would be too large.
2241 * Also check for all-three-contiguous being too large.
2242 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002243 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002244 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002245 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002246 if (isnullstartblock(RIGHT.br_startblock))
2247 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002249
2250 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2251 new_endoff == RIGHT.br_startoff &&
2252 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2253 newext == RIGHT.br_state &&
2254 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2255 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2256 BMAP_RIGHT_FILLING)) !=
2257 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2258 BMAP_RIGHT_FILLING) ||
2259 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2260 <= MAXEXTLEN))
2261 state |= BMAP_RIGHT_CONTIG;
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 /*
2264 * Switch out based on the FILLING and CONTIG state bits.
2265 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002266 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2267 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2268 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2269 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 /*
2271 * Setting all of a previous oldext extent to newext.
2272 * The left and right neighbors are both contiguous with new.
2273 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002274 --*idx;
2275
2276 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2277 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 LEFT.br_blockcount + PREV.br_blockcount +
2279 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002280 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002281
Christoph Hellwigec90c552011-05-23 08:52:53 +00002282 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002283 XFS_IFORK_NEXT_SET(ip, whichfork,
2284 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (cur == NULL)
2286 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2287 else {
2288 rval = XFS_ILOG_CORE;
2289 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2290 RIGHT.br_startblock,
2291 RIGHT.br_blockcount, &i)))
2292 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002293 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002294 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002296 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002297 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002299 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002300 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002302 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002303 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002305 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2307 LEFT.br_startblock,
2308 LEFT.br_blockcount + PREV.br_blockcount +
2309 RIGHT.br_blockcount, LEFT.br_state)))
2310 goto done;
2311 }
2312 break;
2313
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002314 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 /*
2316 * Setting all of a previous oldext extent to newext.
2317 * The left neighbor is contiguous, the right is not.
2318 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002319 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002320
Christoph Hellwigec90c552011-05-23 08:52:53 +00002321 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2322 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2323 LEFT.br_blockcount + PREV.br_blockcount);
2324 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2325
2326 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002327 XFS_IFORK_NEXT_SET(ip, whichfork,
2328 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if (cur == NULL)
2330 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2331 else {
2332 rval = XFS_ILOG_CORE;
2333 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2334 PREV.br_startblock, PREV.br_blockcount,
2335 &i)))
2336 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002337 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002338 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002340 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002341 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002343 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2345 LEFT.br_startblock,
2346 LEFT.br_blockcount + PREV.br_blockcount,
2347 LEFT.br_state)))
2348 goto done;
2349 }
2350 break;
2351
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002352 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 /*
2354 * Setting all of a previous oldext extent to newext.
2355 * The right neighbor is contiguous, the left is not.
2356 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002357 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 xfs_bmbt_set_blockcount(ep,
2359 PREV.br_blockcount + RIGHT.br_blockcount);
2360 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002361 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2362 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002363 XFS_IFORK_NEXT_SET(ip, whichfork,
2364 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if (cur == NULL)
2366 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2367 else {
2368 rval = XFS_ILOG_CORE;
2369 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2370 RIGHT.br_startblock,
2371 RIGHT.br_blockcount, &i)))
2372 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002373 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002374 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002376 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002377 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002379 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2381 new->br_startblock,
2382 new->br_blockcount + RIGHT.br_blockcount,
2383 newext)))
2384 goto done;
2385 }
2386 break;
2387
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002388 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 /*
2390 * Setting all of a previous oldext extent to newext.
2391 * Neither the left nor right neighbors are contiguous with
2392 * the new one.
2393 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002394 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002396 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (cur == NULL)
2399 rval = XFS_ILOG_DEXT;
2400 else {
2401 rval = 0;
2402 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2403 new->br_startblock, new->br_blockcount,
2404 &i)))
2405 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002406 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2408 new->br_startblock, new->br_blockcount,
2409 newext)))
2410 goto done;
2411 }
2412 break;
2413
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002414 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 /*
2416 * Setting the first part of a previous oldext extent to newext.
2417 * The left neighbor is contiguous.
2418 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002419 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2420 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 LEFT.br_blockcount + new->br_blockcount);
2422 xfs_bmbt_set_startoff(ep,
2423 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002424 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002425
Christoph Hellwigec90c552011-05-23 08:52:53 +00002426 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 xfs_bmbt_set_startblock(ep,
2428 new->br_startblock + new->br_blockcount);
2429 xfs_bmbt_set_blockcount(ep,
2430 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002431 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002432
Christoph Hellwigec90c552011-05-23 08:52:53 +00002433 --*idx;
2434
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 if (cur == NULL)
2436 rval = XFS_ILOG_DEXT;
2437 else {
2438 rval = 0;
2439 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2440 PREV.br_startblock, PREV.br_blockcount,
2441 &i)))
2442 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002443 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if ((error = xfs_bmbt_update(cur,
2445 PREV.br_startoff + new->br_blockcount,
2446 PREV.br_startblock + new->br_blockcount,
2447 PREV.br_blockcount - new->br_blockcount,
2448 oldext)))
2449 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002450 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002452 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 LEFT.br_startblock,
2454 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002455 LEFT.br_state);
2456 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 goto done;
2458 }
2459 break;
2460
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002461 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 /*
2463 * Setting the first part of a previous oldext extent to newext.
2464 * The left neighbor is not contiguous.
2465 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002466 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2468 xfs_bmbt_set_startoff(ep, new_endoff);
2469 xfs_bmbt_set_blockcount(ep,
2470 PREV.br_blockcount - new->br_blockcount);
2471 xfs_bmbt_set_startblock(ep,
2472 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002473 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002474
Christoph Hellwigec90c552011-05-23 08:52:53 +00002475 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002476 XFS_IFORK_NEXT_SET(ip, whichfork,
2477 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (cur == NULL)
2479 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2480 else {
2481 rval = XFS_ILOG_CORE;
2482 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2483 PREV.br_startblock, PREV.br_blockcount,
2484 &i)))
2485 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002486 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 if ((error = xfs_bmbt_update(cur,
2488 PREV.br_startoff + new->br_blockcount,
2489 PREV.br_startblock + new->br_blockcount,
2490 PREV.br_blockcount - new->br_blockcount,
2491 oldext)))
2492 goto done;
2493 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002494 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002496 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498 break;
2499
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002500 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 /*
2502 * Setting the last part of a previous oldext extent to newext.
2503 * The right neighbor is contiguous with the new allocation.
2504 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002505 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 xfs_bmbt_set_blockcount(ep,
2507 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002508 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2509
2510 ++*idx;
2511
2512 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2513 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002514 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002516 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (cur == NULL)
2519 rval = XFS_ILOG_DEXT;
2520 else {
2521 rval = 0;
2522 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2523 PREV.br_startblock,
2524 PREV.br_blockcount, &i)))
2525 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002526 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2528 PREV.br_startblock,
2529 PREV.br_blockcount - new->br_blockcount,
2530 oldext)))
2531 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002532 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 goto done;
2534 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2535 new->br_startblock,
2536 new->br_blockcount + RIGHT.br_blockcount,
2537 newext)))
2538 goto done;
2539 }
2540 break;
2541
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002542 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 /*
2544 * Setting the last part of a previous oldext extent to newext.
2545 * The right neighbor is not contiguous.
2546 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002547 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 xfs_bmbt_set_blockcount(ep,
2549 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002550 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002551
Christoph Hellwigec90c552011-05-23 08:52:53 +00002552 ++*idx;
2553 xfs_iext_insert(ip, *idx, 1, new, state);
2554
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002555 XFS_IFORK_NEXT_SET(ip, whichfork,
2556 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (cur == NULL)
2558 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2559 else {
2560 rval = XFS_ILOG_CORE;
2561 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2562 PREV.br_startblock, PREV.br_blockcount,
2563 &i)))
2564 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002565 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2567 PREV.br_startblock,
2568 PREV.br_blockcount - new->br_blockcount,
2569 oldext)))
2570 goto done;
2571 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2572 new->br_startblock, new->br_blockcount,
2573 &i)))
2574 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002575 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002577 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002579 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
2581 break;
2582
2583 case 0:
2584 /*
2585 * Setting the middle part of a previous oldext extent to
2586 * newext. Contiguity is impossible here.
2587 * One extent becomes three extents.
2588 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002589 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 xfs_bmbt_set_blockcount(ep,
2591 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002592 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002593
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 r[0] = *new;
2595 r[1].br_startoff = new_endoff;
2596 r[1].br_blockcount =
2597 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2598 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2599 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002600
2601 ++*idx;
2602 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2603
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002604 XFS_IFORK_NEXT_SET(ip, whichfork,
2605 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 if (cur == NULL)
2607 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2608 else {
2609 rval = XFS_ILOG_CORE;
2610 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2611 PREV.br_startblock, PREV.br_blockcount,
2612 &i)))
2613 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002614 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 /* new right extent - oldext */
2616 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2617 r[1].br_startblock, r[1].br_blockcount,
2618 r[1].br_state)))
2619 goto done;
2620 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002622 cur->bc_rec.b.br_blockcount =
2623 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002624 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002626 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002627 /*
2628 * Reset the cursor to the position of the new extent
2629 * we are about to insert as we can't trust it after
2630 * the previous insert.
2631 */
2632 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2633 new->br_startblock, new->br_blockcount,
2634 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002636 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002638 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002639 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002641 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 }
2643 break;
2644
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002645 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2646 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2647 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2648 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2649 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2650 case BMAP_LEFT_CONTIG:
2651 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 /*
2653 * These cases are all impossible.
2654 */
2655 ASSERT(0);
2656 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002657
Darrick J. Wong9c194642016-08-03 12:16:05 +10002658 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002659 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002660 if (error)
2661 goto done;
2662
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002663 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002664 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002665 int tmp_logflags; /* partial log flag return val */
2666
2667 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002668 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002669 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002670 *logflagsp |= tmp_logflags;
2671 if (error)
2672 goto done;
2673 }
2674
2675 /* clear out the allocated field, done with it now in any case. */
2676 if (cur) {
2677 cur->bc_private.b.allocated = 0;
2678 *curp = cur;
2679 }
2680
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002681 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002683 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 return error;
2685#undef LEFT
2686#undef RIGHT
2687#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688}
2689
2690/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002691 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002693STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694xfs_bmap_add_extent_hole_delay(
2695 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002696 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002697 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002698 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002700 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2702 xfs_filblks_t newlen=0; /* new indirect size */
2703 xfs_filblks_t oldlen=0; /* old indirect size */
2704 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2705 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002706 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002708 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002710 if (whichfork == XFS_COW_FORK)
2711 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002712 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 /*
2715 * Check and set flags if this segment has a left neighbor
2716 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002717 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002718 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002719 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002720
2721 if (isnullstartblock(left.br_startblock))
2722 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 /*
2726 * Check and set flags if the current (right) segment exists.
2727 * If it doesn't exist, we're converting the hole at end-of-file.
2728 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002729 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002730 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002731 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002732
2733 if (isnullstartblock(right.br_startblock))
2734 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002736
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 /*
2738 * Set contiguity flags on the left and right neighbors.
2739 * Don't let extents get too large, even if the pieces are contiguous.
2740 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002741 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2742 left.br_startoff + left.br_blockcount == new->br_startoff &&
2743 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2744 state |= BMAP_LEFT_CONTIG;
2745
2746 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2747 new->br_startoff + new->br_blockcount == right.br_startoff &&
2748 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2749 (!(state & BMAP_LEFT_CONTIG) ||
2750 (left.br_blockcount + new->br_blockcount +
2751 right.br_blockcount <= MAXEXTLEN)))
2752 state |= BMAP_RIGHT_CONTIG;
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 /*
2755 * Switch out based on the contiguity flags.
2756 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002757 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2758 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 /*
2760 * New allocation is contiguous with delayed allocations
2761 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002762 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002764 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 temp = left.br_blockcount + new->br_blockcount +
2766 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002767
Christoph Hellwigec90c552011-05-23 08:52:53 +00002768 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2769 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002770 oldlen = startblockval(left.br_startblock) +
2771 startblockval(new->br_startblock) +
2772 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002773 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2774 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002775 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002776 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002777 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002778
Christoph Hellwigec90c552011-05-23 08:52:53 +00002779 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 break;
2781
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002782 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 /*
2784 * New allocation is contiguous with a delayed allocation
2785 * on the left.
2786 * Merge the new allocation with the left neighbor.
2787 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002788 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002790
2791 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2792 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002793 oldlen = startblockval(left.br_startblock) +
2794 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002795 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2796 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002797 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002798 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002799 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 break;
2801
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002802 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 /*
2804 * New allocation is contiguous with a delayed allocation
2805 * on the right.
2806 * Merge the new allocation with the right neighbor.
2807 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002808 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002810 oldlen = startblockval(new->br_startblock) +
2811 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002812 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2813 oldlen);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002814 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2815 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002816 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002817 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 break;
2819
2820 case 0:
2821 /*
2822 * New allocation is not contiguous with another
2823 * delayed allocation.
2824 * Insert a new entry.
2825 */
2826 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002827 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 break;
2829 }
2830 if (oldlen != newlen) {
2831 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002832 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2833 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 /*
2835 * Nothing to do for disk quota accounting here.
2836 */
2837 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
2839
2840/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002841 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 */
2843STATIC int /* error */
2844xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002845 struct xfs_trans *tp,
2846 struct xfs_inode *ip,
2847 int whichfork,
2848 xfs_extnum_t *idx,
2849 struct xfs_btree_cur **curp,
2850 struct xfs_bmbt_irec *new,
2851 xfs_fsblock_t *first,
2852 struct xfs_defer_ops *dfops,
2853 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002855 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2856 struct xfs_mount *mp = ip->i_mount;
2857 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2861 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002862 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Christoph Hellwig6d045582017-04-11 16:45:54 -07002865 ASSERT(*idx >= 0);
2866 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002867 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002868 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002869
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002870 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002871
2872 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002873 if (whichfork == XFS_ATTR_FORK)
2874 state |= BMAP_ATTRFORK;
Christoph Hellwiga14234c2017-02-06 10:50:49 -08002875 if (whichfork == XFS_COW_FORK)
2876 state |= BMAP_COWFORK;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002877
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 /*
2879 * Check and set flags if this segment has a left neighbor.
2880 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002881 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002882 state |= BMAP_LEFT_VALID;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002883 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002884 if (isnullstartblock(left.br_startblock))
2885 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002887
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 /*
2889 * Check and set flags if this segment has a current value.
2890 * Not true if we're inserting into the "hole" at eof.
2891 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002892 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002893 state |= BMAP_RIGHT_VALID;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002894 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002895 if (isnullstartblock(right.br_startblock))
2896 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 /*
2900 * We're inserting a real allocation between "left" and "right".
2901 * Set the contiguity flags. Don't let extents get too large.
2902 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002903 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2904 left.br_startoff + left.br_blockcount == new->br_startoff &&
2905 left.br_startblock + left.br_blockcount == new->br_startblock &&
2906 left.br_state == new->br_state &&
2907 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2908 state |= BMAP_LEFT_CONTIG;
2909
2910 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2911 new->br_startoff + new->br_blockcount == right.br_startoff &&
2912 new->br_startblock + new->br_blockcount == right.br_startblock &&
2913 new->br_state == right.br_state &&
2914 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2915 (!(state & BMAP_LEFT_CONTIG) ||
2916 left.br_blockcount + new->br_blockcount +
2917 right.br_blockcount <= MAXEXTLEN))
2918 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002920 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 /*
2922 * Select which case we're in here, and implement it.
2923 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002924 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2925 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 /*
2927 * New allocation is contiguous with real allocations on the
2928 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002929 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002931 --*idx;
2932 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2933 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 left.br_blockcount + new->br_blockcount +
2935 right.br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002936 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002937
Christoph Hellwig6d045582017-04-11 16:45:54 -07002938 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002939
Christoph Hellwig6d045582017-04-11 16:45:54 -07002940 XFS_IFORK_NEXT_SET(ip, whichfork,
2941 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2942 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002943 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002944 } else {
2945 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002946 error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002947 right.br_startblock, right.br_blockcount,
2948 &i);
2949 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002950 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002951 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002952 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002953 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002954 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002955 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002956 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002957 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_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002961 left.br_startblock,
2962 left.br_blockcount +
2963 new->br_blockcount +
2964 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002965 left.br_state);
2966 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002967 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002971 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 /*
2973 * New allocation is contiguous with a real allocation
2974 * on the left.
2975 * Merge the new allocation with the left neighbor.
2976 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002977 --*idx;
2978 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2979 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 left.br_blockcount + new->br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002981 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002982
Christoph Hellwig6d045582017-04-11 16:45:54 -07002983 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002984 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002985 } else {
2986 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002987 error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002988 left.br_startblock, left.br_blockcount,
2989 &i);
2990 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002991 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002992 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002993 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002994 left.br_startblock,
2995 left.br_blockcount +
2996 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002997 left.br_state);
2998 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002999 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003001 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003003 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 /*
3005 * New allocation is contiguous with a real allocation
3006 * on the right.
3007 * Merge the new allocation with the right neighbor.
3008 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003009 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
3010 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003011 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 new->br_blockcount + right.br_blockcount,
3013 right.br_state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003014 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003015
Christoph Hellwig6d045582017-04-11 16:45:54 -07003016 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003017 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003018 } else {
3019 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003020 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003021 right.br_startoff,
3022 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003023 right.br_blockcount, &i);
3024 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003025 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003026 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003027 error = xfs_bmbt_update(cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003028 new->br_startblock,
3029 new->br_blockcount +
3030 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003031 right.br_state);
3032 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003033 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003035 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
3037 case 0:
3038 /*
3039 * New allocation is not contiguous with another
3040 * real allocation.
3041 * Insert a new entry.
3042 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003043 xfs_iext_insert(ip, *idx, 1, new, state);
3044 XFS_IFORK_NEXT_SET(ip, whichfork,
3045 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3046 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003047 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003048 } else {
3049 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003050 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003051 new->br_startoff,
3052 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003053 new->br_blockcount, &i);
3054 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003055 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003056 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003057 cur->bc_rec.b.br_state = new->br_state;
3058 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003059 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003060 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003061 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003063 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003065
Darrick J. Wong9c194642016-08-03 12:16:05 +10003066 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003067 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10003068 if (error)
3069 goto done;
3070
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003071 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003072 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003073 int tmp_logflags; /* partial log flag return val */
3074
Christoph Hellwig6d045582017-04-11 16:45:54 -07003075 ASSERT(cur == NULL);
3076 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003077 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003078 *logflagsp |= tmp_logflags;
3079 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003080 if (error)
3081 goto done;
3082 }
3083
3084 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003085 if (cur)
3086 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003087
Christoph Hellwig6d045582017-04-11 16:45:54 -07003088 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003089done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07003090 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003091 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092}
3093
Nathan Scottdd9f4382006-01-11 15:28:28 +11003094/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11003095 * Functions used in the extent read, allocate and remove paths
3096 */
3097
3098/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003099 * Adjust the size of the new extent based on di_extsize and rt extsize.
3100 */
Dave Chinner68988112013-08-12 20:49:42 +10003101int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003102xfs_bmap_extsize_align(
3103 xfs_mount_t *mp,
3104 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3105 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3106 xfs_extlen_t extsz, /* align to this extent size */
3107 int rt, /* is this a realtime inode? */
3108 int eof, /* is extent at end-of-file? */
3109 int delay, /* creating delalloc extent? */
3110 int convert, /* overwriting unwritten extent? */
3111 xfs_fileoff_t *offp, /* in/out: aligned offset */
3112 xfs_extlen_t *lenp) /* in/out: aligned length */
3113{
3114 xfs_fileoff_t orig_off; /* original offset */
3115 xfs_extlen_t orig_alen; /* original length */
3116 xfs_fileoff_t orig_end; /* original off+len */
3117 xfs_fileoff_t nexto; /* next file offset */
3118 xfs_fileoff_t prevo; /* previous file offset */
3119 xfs_fileoff_t align_off; /* temp for offset */
3120 xfs_extlen_t align_alen; /* temp for length */
3121 xfs_extlen_t temp; /* temp for calculations */
3122
3123 if (convert)
3124 return 0;
3125
3126 orig_off = align_off = *offp;
3127 orig_alen = align_alen = *lenp;
3128 orig_end = orig_off + orig_alen;
3129
3130 /*
3131 * If this request overlaps an existing extent, then don't
3132 * attempt to perform any additional alignment.
3133 */
3134 if (!delay && !eof &&
3135 (orig_off >= gotp->br_startoff) &&
3136 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3137 return 0;
3138 }
3139
3140 /*
3141 * If the file offset is unaligned vs. the extent size
3142 * we need to align it. This will be possible unless
3143 * the file was previously written with a kernel that didn't
3144 * perform this alignment, or if a truncate shot us in the
3145 * foot.
3146 */
3147 temp = do_mod(orig_off, extsz);
3148 if (temp) {
3149 align_alen += temp;
3150 align_off -= temp;
3151 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003152
3153 /* Same adjustment for the end of the requested area. */
3154 temp = (align_alen % extsz);
3155 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003156 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003157
3158 /*
3159 * For large extent hint sizes, the aligned extent might be larger than
3160 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3161 * the length back under MAXEXTLEN. The outer allocation loops handle
3162 * short allocation just fine, so it is safe to do this. We only want to
3163 * do it when we are forced to, though, because it means more allocation
3164 * operations are required.
3165 */
3166 while (align_alen > MAXEXTLEN)
3167 align_alen -= extsz;
3168 ASSERT(align_alen <= MAXEXTLEN);
3169
Nathan Scottdd9f4382006-01-11 15:28:28 +11003170 /*
3171 * If the previous block overlaps with this proposed allocation
3172 * then move the start forward without adjusting the length.
3173 */
3174 if (prevp->br_startoff != NULLFILEOFF) {
3175 if (prevp->br_startblock == HOLESTARTBLOCK)
3176 prevo = prevp->br_startoff;
3177 else
3178 prevo = prevp->br_startoff + prevp->br_blockcount;
3179 } else
3180 prevo = 0;
3181 if (align_off != orig_off && align_off < prevo)
3182 align_off = prevo;
3183 /*
3184 * If the next block overlaps with this proposed allocation
3185 * then move the start back without adjusting the length,
3186 * but not before offset 0.
3187 * This may of course make the start overlap previous block,
3188 * and if we hit the offset 0 limit then the next block
3189 * can still overlap too.
3190 */
3191 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3192 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3193 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3194 nexto = gotp->br_startoff + gotp->br_blockcount;
3195 else
3196 nexto = gotp->br_startoff;
3197 } else
3198 nexto = NULLFILEOFF;
3199 if (!eof &&
3200 align_off + align_alen != orig_end &&
3201 align_off + align_alen > nexto)
3202 align_off = nexto > align_alen ? nexto - align_alen : 0;
3203 /*
3204 * If we're now overlapping the next or previous extent that
3205 * means we can't fit an extsz piece in this hole. Just move
3206 * the start forward to the first valid spot and set
3207 * the length so we hit the end.
3208 */
3209 if (align_off != orig_off && align_off < prevo)
3210 align_off = prevo;
3211 if (align_off + align_alen != orig_end &&
3212 align_off + align_alen > nexto &&
3213 nexto != NULLFILEOFF) {
3214 ASSERT(nexto > prevo);
3215 align_alen = nexto - align_off;
3216 }
3217
3218 /*
3219 * If realtime, and the result isn't a multiple of the realtime
3220 * extent size we need to remove blocks until it is.
3221 */
3222 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3223 /*
3224 * We're not covering the original request, or
3225 * we won't be able to once we fix the length.
3226 */
3227 if (orig_off < align_off ||
3228 orig_end > align_off + align_alen ||
3229 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003230 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003231 /*
3232 * Try to fix it by moving the start up.
3233 */
3234 if (align_off + temp <= orig_off) {
3235 align_alen -= temp;
3236 align_off += temp;
3237 }
3238 /*
3239 * Try to fix it by moving the end in.
3240 */
3241 else if (align_off + align_alen - temp >= orig_end)
3242 align_alen -= temp;
3243 /*
3244 * Set the start to the minimum then trim the length.
3245 */
3246 else {
3247 align_alen -= orig_off - align_off;
3248 align_off = orig_off;
3249 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3250 }
3251 /*
3252 * Result doesn't cover the request, fail it.
3253 */
3254 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003255 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003256 } else {
3257 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003258 /* see MAXEXTLEN handling above */
3259 ASSERT(orig_end <= align_off + align_alen ||
3260 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003261 }
3262
3263#ifdef DEBUG
3264 if (!eof && gotp->br_startoff != NULLFILEOFF)
3265 ASSERT(align_off + align_alen <= gotp->br_startoff);
3266 if (prevp->br_startoff != NULLFILEOFF)
3267 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3268#endif
3269
3270 *lenp = align_alen;
3271 *offp = align_off;
3272 return 0;
3273}
3274
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275#define XFS_ALLOC_GAP_UNITS 4
3276
Dave Chinner68988112013-08-12 20:49:42 +10003277void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003278xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003279 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280{
3281 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3283 xfs_mount_t *mp; /* mount point structure */
3284 int nullfb; /* true if ap->firstblock isn't set */
3285 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
3287#define ISVALID(x,y) \
3288 (rt ? \
3289 (x) < mp->m_sb.sb_rblocks : \
3290 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3291 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3292 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003295 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003296 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3297 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003298 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 /*
3300 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003301 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003303 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3304 !isnullstartblock(ap->prev.br_startblock) &&
3305 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3306 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003307 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 /*
3309 * Adjust for the gap between prevp and us.
3310 */
Dave Chinner3a756672011-09-18 20:40:58 +00003311 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003312 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003314 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3315 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 }
3317 /*
3318 * If not at eof, then compare the two neighbor blocks.
3319 * Figure out whether either one gives us a good starting point,
3320 * and pick the better one.
3321 */
3322 else if (!ap->eof) {
3323 xfs_fsblock_t gotbno; /* right side block number */
3324 xfs_fsblock_t gotdiff=0; /* right side difference */
3325 xfs_fsblock_t prevbno; /* left side block number */
3326 xfs_fsblock_t prevdiff=0; /* left side difference */
3327
3328 /*
3329 * If there's a previous (left) block, select a requested
3330 * start block based on it.
3331 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003332 if (ap->prev.br_startoff != NULLFILEOFF &&
3333 !isnullstartblock(ap->prev.br_startblock) &&
3334 (prevbno = ap->prev.br_startblock +
3335 ap->prev.br_blockcount) &&
3336 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 /*
3338 * Calculate gap to end of previous block.
3339 */
Dave Chinner3a756672011-09-18 20:40:58 +00003340 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003341 (ap->prev.br_startoff +
3342 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 /*
3344 * Figure the startblock based on the previous block's
3345 * end and the gap size.
3346 * Heuristic!
3347 * If the gap is large relative to the piece we're
3348 * allocating, or using it gives us an invalid block
3349 * number, then just use the end of the previous block.
3350 */
Dave Chinner3a756672011-09-18 20:40:58 +00003351 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003353 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 prevbno += adjust;
3355 else
3356 prevdiff += adjust;
3357 /*
3358 * If the firstblock forbids it, can't use it,
3359 * must use default.
3360 */
3361 if (!rt && !nullfb &&
3362 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3363 prevbno = NULLFSBLOCK;
3364 }
3365 /*
3366 * No previous block or can't follow it, just default.
3367 */
3368 else
3369 prevbno = NULLFSBLOCK;
3370 /*
3371 * If there's a following (right) block, select a requested
3372 * start block based on it.
3373 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003374 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 /*
3376 * Calculate gap to start of next block.
3377 */
Dave Chinner3a756672011-09-18 20:40:58 +00003378 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 /*
3380 * Figure the startblock based on the next block's
3381 * start and the gap size.
3382 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003383 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 /*
3385 * Heuristic!
3386 * If the gap is large relative to the piece we're
3387 * allocating, or using it gives us an invalid block
3388 * number, then just use the start of the next block
3389 * offset by our length.
3390 */
Dave Chinner3a756672011-09-18 20:40:58 +00003391 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 ISVALID(gotbno - gotdiff, gotbno))
3393 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003394 else if (ISVALID(gotbno - ap->length, gotbno)) {
3395 gotbno -= ap->length;
3396 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 } else
3398 gotdiff += adjust;
3399 /*
3400 * If the firstblock forbids it, can't use it,
3401 * must use default.
3402 */
3403 if (!rt && !nullfb &&
3404 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3405 gotbno = NULLFSBLOCK;
3406 }
3407 /*
3408 * No next block, just default.
3409 */
3410 else
3411 gotbno = NULLFSBLOCK;
3412 /*
3413 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003414 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 */
3416 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003417 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003419 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003421 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003423#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003424}
3425
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003426static int
3427xfs_bmap_longest_free_extent(
3428 struct xfs_trans *tp,
3429 xfs_agnumber_t ag,
3430 xfs_extlen_t *blen,
3431 int *notinit)
3432{
3433 struct xfs_mount *mp = tp->t_mountp;
3434 struct xfs_perag *pag;
3435 xfs_extlen_t longest;
3436 int error = 0;
3437
3438 pag = xfs_perag_get(mp, ag);
3439 if (!pag->pagf_init) {
3440 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3441 if (error)
3442 goto out;
3443
3444 if (!pag->pagf_init) {
3445 *notinit = 1;
3446 goto out;
3447 }
3448 }
3449
Dave Chinner50adbcb2015-06-22 10:04:31 +10003450 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003451 xfs_alloc_min_freelist(mp, pag),
3452 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003453 if (*blen < longest)
3454 *blen = longest;
3455
3456out:
3457 xfs_perag_put(pag);
3458 return error;
3459}
3460
3461static void
3462xfs_bmap_select_minlen(
3463 struct xfs_bmalloca *ap,
3464 struct xfs_alloc_arg *args,
3465 xfs_extlen_t *blen,
3466 int notinit)
3467{
3468 if (notinit || *blen < ap->minlen) {
3469 /*
3470 * Since we did a BUF_TRYLOCK above, it is possible that
3471 * there is space for this request.
3472 */
3473 args->minlen = ap->minlen;
3474 } else if (*blen < args->maxlen) {
3475 /*
3476 * If the best seen length is less than the request length,
3477 * use the best as the minimum.
3478 */
3479 args->minlen = *blen;
3480 } else {
3481 /*
3482 * Otherwise we've seen an extent as big as maxlen, use that
3483 * as the minimum.
3484 */
3485 args->minlen = args->maxlen;
3486 }
3487}
3488
Nathan Scotta365bdd2006-03-14 13:34:16 +11003489STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003490xfs_bmap_btalloc_nullfb(
3491 struct xfs_bmalloca *ap,
3492 struct xfs_alloc_arg *args,
3493 xfs_extlen_t *blen)
3494{
3495 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003496 xfs_agnumber_t ag, startag;
3497 int notinit = 0;
3498 int error;
3499
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003500 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003501 args->total = ap->total;
3502
Christoph Hellwigc467c042010-02-15 23:34:42 +00003503 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3504 if (startag == NULLAGNUMBER)
3505 startag = ag = 0;
3506
Dave Chinner14b064c2011-01-27 12:16:28 +11003507 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003508 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3509 &notinit);
3510 if (error)
3511 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003512
Christoph Hellwigc467c042010-02-15 23:34:42 +00003513 if (++ag == mp->m_sb.sb_agcount)
3514 ag = 0;
3515 if (ag == startag)
3516 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003517 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003518
3519 xfs_bmap_select_minlen(ap, args, blen, notinit);
3520 return 0;
3521}
3522
3523STATIC int
3524xfs_bmap_btalloc_filestreams(
3525 struct xfs_bmalloca *ap,
3526 struct xfs_alloc_arg *args,
3527 xfs_extlen_t *blen)
3528{
3529 struct xfs_mount *mp = ap->ip->i_mount;
3530 xfs_agnumber_t ag;
3531 int notinit = 0;
3532 int error;
3533
3534 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3535 args->total = ap->total;
3536
3537 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3538 if (ag == NULLAGNUMBER)
3539 ag = 0;
3540
3541 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3542 if (error)
3543 return error;
3544
3545 if (*blen < args->maxlen) {
3546 error = xfs_filestream_new_ag(ap, &ag);
3547 if (error)
3548 return error;
3549
3550 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3551 &notinit);
3552 if (error)
3553 return error;
3554
3555 }
3556
3557 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003558
3559 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003560 * Set the failure fallback case to look in the selected AG as stream
3561 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003562 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003563 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003564 return 0;
3565}
3566
3567STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003568xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003569 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003570{
3571 xfs_mount_t *mp; /* mount point structure */
3572 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003573 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003574 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003575 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003576 xfs_alloc_arg_t args;
3577 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003578 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003579 int nullfb; /* true if ap->firstblock isn't set */
3580 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003581 int tryagain;
3582 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003583 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003584
Dave Chinnera99ebf42011-12-01 11:24:20 +00003585 ASSERT(ap->length);
3586
Nathan Scotta365bdd2006-03-14 13:34:16 +11003587 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003588
3589 /* stripe alignment for allocation is determined by mount parameters */
3590 stripe_align = 0;
3591 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3592 stripe_align = mp->m_swidth;
3593 else if (mp->m_dalign)
3594 stripe_align = mp->m_dalign;
3595
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003596 if (ap->flags & XFS_BMAPI_COWFORK)
3597 align = xfs_get_cowextsz_hint(ap->ip);
3598 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003599 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003600 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003601 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003602 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003603 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003604 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003605 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003606 }
Dave Chinner33177f052013-12-12 16:34:36 +11003607
3608
Dave Chinner0937e0f2011-09-18 20:40:57 +00003609 nullfb = *ap->firstblock == NULLFSBLOCK;
3610 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003611 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003612 if (xfs_alloc_is_userdata(ap->datatype) &&
3613 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003614 ag = xfs_filestream_lookup_ag(ap->ip);
3615 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003616 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003617 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003618 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003619 }
3620 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003621 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003622
3623 xfs_bmap_adjacent(ap);
3624
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003626 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 * it's in the right allocation group.
3628 */
Dave Chinner3a756672011-09-18 20:40:58 +00003629 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 ;
3631 else
Dave Chinner3a756672011-09-18 20:40:58 +00003632 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 * Normal allocation, done through xfs_alloc_vextent.
3635 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003636 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003637 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003638 args.tp = ap->tp;
3639 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003640 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003641 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003642
3643 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003644 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003645 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003646 blen = 0;
3647 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003648 /*
3649 * Search for an allocation group with a single extent large
3650 * enough for the request. If one isn't found, then adjust
3651 * the minimum allocation size to the largest space found.
3652 */
Dave Chinner292378e2016-09-26 08:21:28 +10003653 if (xfs_alloc_is_userdata(ap->datatype) &&
3654 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003655 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3656 else
3657 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003658 if (error)
3659 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003660 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003661 if (xfs_inode_is_filestream(ap->ip))
3662 args.type = XFS_ALLOCTYPE_FIRST_AG;
3663 else
3664 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003665 args.total = args.minlen = ap->minlen;
3666 } else {
3667 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3668 args.total = ap->total;
3669 args.minlen = ap->minlen;
3670 }
David Chinner957d0eb2007-06-18 16:50:37 +10003671 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003672 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003673 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003674 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003675 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003676 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003677 args.prod = 1;
3678 args.mod = 0;
3679 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003680 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003681 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003682 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3683 }
3684 /*
3685 * If we are not low on available data blocks, and the
3686 * underlying logical volume manager is a stripe, and
3687 * the file offset is zero then try to allocate data
3688 * blocks on stripe unit boundary.
3689 * NOTE: ap->aeof is only set if the allocation length
3690 * is >= the stripe unit and the allocation offset is
3691 * at the end of file.
3692 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003693 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003694 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003695 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003696 atype = args.type;
3697 isaligned = 1;
3698 /*
3699 * Adjust for alignment
3700 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003701 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003702 args.minlen = blen - args.alignment;
3703 args.minalignslop = 0;
3704 } else {
3705 /*
3706 * First try an exact bno allocation.
3707 * If it fails then do a near or start bno
3708 * allocation with alignment turned on.
3709 */
3710 atype = args.type;
3711 tryagain = 1;
3712 args.type = XFS_ALLOCTYPE_THIS_BNO;
3713 args.alignment = 1;
3714 /*
3715 * Compute the minlen+alignment for the
3716 * next case. Set slop so that the value
3717 * of minlen+alignment+slop doesn't go up
3718 * between the calls.
3719 */
Dave Chinner33177f052013-12-12 16:34:36 +11003720 if (blen > stripe_align && blen <= args.maxlen)
3721 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003722 else
3723 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003724 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003725 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003726 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003727 args.minlen - 1;
3728 else
3729 args.minalignslop = 0;
3730 }
3731 } else {
3732 args.alignment = 1;
3733 args.minalignslop = 0;
3734 }
3735 args.minleft = ap->minleft;
3736 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003737 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003738 args.datatype = ap->datatype;
3739 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003740 args.ip = ap->ip;
3741
3742 error = xfs_alloc_vextent(&args);
3743 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003744 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003745
Nathan Scotta365bdd2006-03-14 13:34:16 +11003746 if (tryagain && args.fsbno == NULLFSBLOCK) {
3747 /*
3748 * Exact allocation failed. Now try with alignment
3749 * turned on.
3750 */
3751 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003752 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003753 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003754 args.minlen = nextminlen;
3755 args.minalignslop = 0;
3756 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 if ((error = xfs_alloc_vextent(&args)))
3758 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003759 }
3760 if (isaligned && args.fsbno == NULLFSBLOCK) {
3761 /*
3762 * allocation failed, so turn off alignment and
3763 * try again.
3764 */
3765 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003766 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003767 args.alignment = 0;
3768 if ((error = xfs_alloc_vextent(&args)))
3769 return error;
3770 }
3771 if (args.fsbno == NULLFSBLOCK && nullfb &&
3772 args.minlen > ap->minlen) {
3773 args.minlen = ap->minlen;
3774 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003775 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003776 if ((error = xfs_alloc_vextent(&args)))
3777 return error;
3778 }
3779 if (args.fsbno == NULLFSBLOCK && nullfb) {
3780 args.fsbno = 0;
3781 args.type = XFS_ALLOCTYPE_FIRST_AG;
3782 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003783 if ((error = xfs_alloc_vextent(&args)))
3784 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003785 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003786 }
3787 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003788 /*
3789 * check the allocation happened at the same or higher AG than
3790 * the first block that was allocated.
3791 */
3792 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003793 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3794 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003795
Dave Chinner3a756672011-09-18 20:40:58 +00003796 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003797 if (*ap->firstblock == NULLFSBLOCK)
3798 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003799 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003800 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003801 if (!(ap->flags & XFS_BMAPI_COWFORK))
3802 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003803 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3804 if (ap->wasdel)
3805 ap->ip->i_delayed_blks -= args.len;
3806 /*
3807 * Adjust the disk quota also. This was reserved
3808 * earlier.
3809 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003810 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003811 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3812 XFS_TRANS_DQ_BCOUNT,
3813 (long) args.len);
3814 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003815 ap->blkno = NULLFSBLOCK;
3816 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 }
3818 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003819}
3820
3821/*
3822 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3823 * It figures out where to ask the underlying allocator to put the new extent.
3824 */
3825STATIC int
3826xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003827 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003828{
Dave Chinner292378e2016-09-26 08:21:28 +10003829 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3830 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003831 return xfs_bmap_rtalloc(ap);
3832 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833}
3834
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003835/* Trim extent to fit a logical block range. */
3836void
3837xfs_trim_extent(
3838 struct xfs_bmbt_irec *irec,
3839 xfs_fileoff_t bno,
3840 xfs_filblks_t len)
3841{
3842 xfs_fileoff_t distance;
3843 xfs_fileoff_t end = bno + len;
3844
3845 if (irec->br_startoff + irec->br_blockcount <= bno ||
3846 irec->br_startoff >= end) {
3847 irec->br_blockcount = 0;
3848 return;
3849 }
3850
3851 if (irec->br_startoff < bno) {
3852 distance = bno - irec->br_startoff;
3853 if (isnullstartblock(irec->br_startblock))
3854 irec->br_startblock = DELAYSTARTBLOCK;
3855 if (irec->br_startblock != DELAYSTARTBLOCK &&
3856 irec->br_startblock != HOLESTARTBLOCK)
3857 irec->br_startblock += distance;
3858 irec->br_startoff += distance;
3859 irec->br_blockcount -= distance;
3860 }
3861
3862 if (end < irec->br_startoff + irec->br_blockcount) {
3863 distance = irec->br_startoff + irec->br_blockcount - end;
3864 irec->br_blockcount -= distance;
3865 }
3866}
3867
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003869 * Trim the returned map to the required bounds
3870 */
3871STATIC void
3872xfs_bmapi_trim_map(
3873 struct xfs_bmbt_irec *mval,
3874 struct xfs_bmbt_irec *got,
3875 xfs_fileoff_t *bno,
3876 xfs_filblks_t len,
3877 xfs_fileoff_t obno,
3878 xfs_fileoff_t end,
3879 int n,
3880 int flags)
3881{
3882 if ((flags & XFS_BMAPI_ENTIRE) ||
3883 got->br_startoff + got->br_blockcount <= obno) {
3884 *mval = *got;
3885 if (isnullstartblock(got->br_startblock))
3886 mval->br_startblock = DELAYSTARTBLOCK;
3887 return;
3888 }
3889
3890 if (obno > *bno)
3891 *bno = obno;
3892 ASSERT((*bno >= obno) || (n == 0));
3893 ASSERT(*bno < end);
3894 mval->br_startoff = *bno;
3895 if (isnullstartblock(got->br_startblock))
3896 mval->br_startblock = DELAYSTARTBLOCK;
3897 else
3898 mval->br_startblock = got->br_startblock +
3899 (*bno - got->br_startoff);
3900 /*
3901 * Return the minimum of what we got and what we asked for for
3902 * the length. We can use the len variable here because it is
3903 * modified below and we could have been there before coming
3904 * here if the first part of the allocation didn't overlap what
3905 * was asked for.
3906 */
3907 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3908 got->br_blockcount - (*bno - got->br_startoff));
3909 mval->br_state = got->br_state;
3910 ASSERT(mval->br_blockcount <= len);
3911 return;
3912}
3913
3914/*
3915 * Update and validate the extent map to return
3916 */
3917STATIC void
3918xfs_bmapi_update_map(
3919 struct xfs_bmbt_irec **map,
3920 xfs_fileoff_t *bno,
3921 xfs_filblks_t *len,
3922 xfs_fileoff_t obno,
3923 xfs_fileoff_t end,
3924 int *n,
3925 int flags)
3926{
3927 xfs_bmbt_irec_t *mval = *map;
3928
3929 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3930 ((mval->br_startoff + mval->br_blockcount) <= end));
3931 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3932 (mval->br_startoff < obno));
3933
3934 *bno = mval->br_startoff + mval->br_blockcount;
3935 *len = end - *bno;
3936 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3937 /* update previous map with new information */
3938 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3939 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3940 ASSERT(mval->br_state == mval[-1].br_state);
3941 mval[-1].br_blockcount = mval->br_blockcount;
3942 mval[-1].br_state = mval->br_state;
3943 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3944 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3945 mval[-1].br_startblock != HOLESTARTBLOCK &&
3946 mval->br_startblock == mval[-1].br_startblock +
3947 mval[-1].br_blockcount &&
3948 ((flags & XFS_BMAPI_IGSTATE) ||
3949 mval[-1].br_state == mval->br_state)) {
3950 ASSERT(mval->br_startoff ==
3951 mval[-1].br_startoff + mval[-1].br_blockcount);
3952 mval[-1].br_blockcount += mval->br_blockcount;
3953 } else if (*n > 0 &&
3954 mval->br_startblock == DELAYSTARTBLOCK &&
3955 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3956 mval->br_startoff ==
3957 mval[-1].br_startoff + mval[-1].br_blockcount) {
3958 mval[-1].br_blockcount += mval->br_blockcount;
3959 mval[-1].br_state = mval->br_state;
3960 } else if (!((*n == 0) &&
3961 ((mval->br_startoff + mval->br_blockcount) <=
3962 obno))) {
3963 mval++;
3964 (*n)++;
3965 }
3966 *map = mval;
3967}
3968
3969/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003970 * Map file blocks to filesystem blocks without allocation.
3971 */
3972int
3973xfs_bmapi_read(
3974 struct xfs_inode *ip,
3975 xfs_fileoff_t bno,
3976 xfs_filblks_t len,
3977 struct xfs_bmbt_irec *mval,
3978 int *nmap,
3979 int flags)
3980{
3981 struct xfs_mount *mp = ip->i_mount;
3982 struct xfs_ifork *ifp;
3983 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003984 xfs_fileoff_t obno;
3985 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003986 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003987 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003988 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003989 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003990 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003991
3992 ASSERT(*nmap >= 1);
3993 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003994 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003995 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003996
3997 if (unlikely(XFS_TEST_ERROR(
3998 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3999 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004000 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00004001 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004002 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004003 }
4004
4005 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004006 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004007
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004008 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004009
4010 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004011
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004012 /* No CoW fork? Return a hole. */
4013 if (whichfork == XFS_COW_FORK && !ifp) {
4014 mval->br_startoff = bno;
4015 mval->br_startblock = HOLESTARTBLOCK;
4016 mval->br_blockcount = len;
4017 mval->br_state = XFS_EXT_NORM;
4018 *nmap = 1;
4019 return 0;
4020 }
4021
Dave Chinner5c8ed202011-09-18 20:40:45 +00004022 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4023 error = xfs_iread_extents(NULL, ip, whichfork);
4024 if (error)
4025 return error;
4026 }
4027
Christoph Hellwig334f3422016-11-24 11:39:43 +11004028 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4029 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004030 end = bno + len;
4031 obno = bno;
4032
4033 while (bno < end && n < *nmap) {
4034 /* Reading past eof, act as though there's a hole up to end. */
4035 if (eof)
4036 got.br_startoff = end;
4037 if (got.br_startoff > bno) {
4038 /* Reading in a hole. */
4039 mval->br_startoff = bno;
4040 mval->br_startblock = HOLESTARTBLOCK;
4041 mval->br_blockcount =
4042 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4043 mval->br_state = XFS_EXT_NORM;
4044 bno += mval->br_blockcount;
4045 len -= mval->br_blockcount;
4046 mval++;
4047 n++;
4048 continue;
4049 }
4050
4051 /* set up the extent map to return. */
4052 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4053 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4054
4055 /* If we're done, stop now. */
4056 if (bno >= end || n >= *nmap)
4057 break;
4058
4059 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004060 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4061 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004062 }
4063 *nmap = n;
4064 return 0;
4065}
4066
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004067/*
4068 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4069 * global pool and the extent inserted into the inode in-core extent tree.
4070 *
4071 * On entry, got refers to the first extent beyond the offset of the extent to
4072 * allocate or eof is specified if no such extent exists. On return, got refers
4073 * to the extent record that was inserted to the inode fork.
4074 *
4075 * Note that the allocated extent may have been merged with contiguous extents
4076 * during insertion into the inode fork. Thus, got does not reflect the current
4077 * state of the inode fork on return. If necessary, the caller can use lastx to
4078 * look up the updated record in the inode fork.
4079 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10004080int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004081xfs_bmapi_reserve_delalloc(
4082 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004083 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004084 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004085 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004086 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004087 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004088 xfs_extnum_t *lastx,
4089 int eof)
4090{
4091 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004092 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004093 xfs_extlen_t alen;
4094 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004095 char rt = XFS_IS_REALTIME_INODE(ip);
4096 xfs_extlen_t extsz;
4097 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004098 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004099
Brian Foster974ae922016-11-28 14:57:42 +11004100 /*
4101 * Cap the alloc length. Keep track of prealloc so we know whether to
4102 * tag the inode before we return.
4103 */
4104 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004105 if (!eof)
4106 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004107 if (prealloc && alen >= len)
4108 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004109
4110 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004111 if (whichfork == XFS_COW_FORK)
4112 extsz = xfs_get_cowextsz_hint(ip);
4113 else
4114 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004115 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004116 struct xfs_bmbt_irec prev;
4117
4118 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4119 prev.br_startoff = NULLFILEOFF;
4120
4121 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004122 1, 0, &aoff, &alen);
4123 ASSERT(!error);
4124 }
4125
4126 if (rt)
4127 extsz = alen / mp->m_sb.sb_rextsize;
4128
4129 /*
4130 * Make a transaction-less quota reservation for delayed allocation
4131 * blocks. This number gets adjusted later. We return if we haven't
4132 * allocated blocks already inside this loop.
4133 */
4134 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4135 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4136 if (error)
4137 return error;
4138
4139 /*
4140 * Split changing sb for alen and indlen since they could be coming
4141 * from different places.
4142 */
4143 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4144 ASSERT(indlen > 0);
4145
4146 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004147 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004148 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004149 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004150 }
4151
4152 if (error)
4153 goto out_unreserve_quota;
4154
Dave Chinner0d485ad2015-02-23 21:22:03 +11004155 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004156 if (error)
4157 goto out_unreserve_blocks;
4158
4159
4160 ip->i_delayed_blks += alen;
4161
4162 got->br_startoff = aoff;
4163 got->br_startblock = nullstartblock(indlen);
4164 got->br_blockcount = alen;
4165 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004166
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004167 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004168
Brian Foster974ae922016-11-28 14:57:42 +11004169 /*
4170 * Tag the inode if blocks were preallocated. Note that COW fork
4171 * preallocation can occur at the start or end of the extent, even when
4172 * prealloc == 0, so we must also check the aligned offset and length.
4173 */
4174 if (whichfork == XFS_DATA_FORK && prealloc)
4175 xfs_inode_set_eofblocks_tag(ip);
4176 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4177 xfs_inode_set_cowblocks_tag(ip);
4178
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004179 return 0;
4180
4181out_unreserve_blocks:
4182 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004183 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004184 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004185 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004186out_unreserve_quota:
4187 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004188 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004189 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4190 return error;
4191}
4192
Dave Chinnercf11da92014-07-15 07:08:24 +10004193static int
4194xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004195 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004196{
4197 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004198 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004199 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004200 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004201 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004202
Dave Chinnera99ebf42011-12-01 11:24:20 +00004203 ASSERT(bma->length > 0);
4204
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004205 /*
4206 * For the wasdelay case, we could also just allocate the stuff asked
4207 * for in this bmap call but that wouldn't be as good.
4208 */
4209 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004210 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4211 bma->offset = bma->got.br_startoff;
Christoph Hellwig0e8d6302016-11-24 11:40:32 +11004212 if (bma->idx) {
Dave Chinnere0c3da52011-09-18 20:41:01 +00004213 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004214 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004215 }
4216 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004217 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004218 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004219 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004220 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004221 }
4222
4223 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004224 * Set the data type being allocated. For the data fork, the first data
4225 * in the file is treated differently to all other allocations. For the
4226 * attribute fork, we only need to ensure the allocated range is not on
4227 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004228 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004229 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004230 bma->datatype = XFS_ALLOC_NOBUSY;
4231 if (whichfork == XFS_DATA_FORK) {
4232 if (bma->offset == 0)
4233 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4234 else
4235 bma->datatype |= XFS_ALLOC_USERDATA;
4236 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004237 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004238 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004239 }
4240
Dave Chinnere04426b2012-10-05 11:06:59 +10004241 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004242
4243 /*
4244 * Only want to do the alignment at the eof if it is userdata and
4245 * allocation length is larger than a stripe unit.
4246 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004247 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004248 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004249 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004250 if (error)
4251 return error;
4252 }
4253
4254 error = xfs_bmap_alloc(bma);
4255 if (error)
4256 return error;
4257
Dave Chinner29c8d172011-09-18 20:41:00 +00004258 if (bma->cur)
4259 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004260 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004261 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004262 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4263 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4264 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004265 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004266 }
4267 /*
4268 * Bump the number of extents we've allocated
4269 * in this call.
4270 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004271 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004272
Dave Chinner29c8d172011-09-18 20:41:00 +00004273 if (bma->cur)
4274 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004275 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4276
Dave Chinner963c30c2011-09-18 20:40:59 +00004277 bma->got.br_startoff = bma->offset;
4278 bma->got.br_startblock = bma->blkno;
4279 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004280 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004281
4282 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004283 * In the data fork, a wasdelay extent has been initialized, so
4284 * shouldn't be flagged as unwritten.
4285 *
4286 * For the cow fork, however, we convert delalloc reservations
4287 * (extents allocated for speculative preallocation) to
4288 * allocated unwritten extents, and only convert the unwritten
4289 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004290 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004291 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4292 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004293 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004294 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004295
Christoph Hellwigc6534242011-09-18 20:41:05 +00004296 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004297 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004298 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004299 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4300 whichfork, &bma->idx, &bma->cur, &bma->got,
4301 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004302
Christoph Hellwigc315c902011-09-18 20:41:02 +00004303 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004304 if (error)
4305 return error;
4306
4307 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004308 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4309 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4310 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004311 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004312 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004313
Dave Chinner963c30c2011-09-18 20:40:59 +00004314 ASSERT(bma->got.br_startoff <= bma->offset);
4315 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4316 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004317 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4318 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004319 return 0;
4320}
4321
Dave Chinnerb447fe52011-09-18 20:40:51 +00004322STATIC int
4323xfs_bmapi_convert_unwritten(
4324 struct xfs_bmalloca *bma,
4325 struct xfs_bmbt_irec *mval,
4326 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004327 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004328{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004329 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004330 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004331 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004332 int error;
4333
Dave Chinnerb447fe52011-09-18 20:40:51 +00004334 /* check if we need to do unwritten->real conversion */
4335 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4336 (flags & XFS_BMAPI_PREALLOC))
4337 return 0;
4338
4339 /* check if we need to do real->unwritten conversion */
4340 if (mval->br_state == XFS_EXT_NORM &&
4341 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4342 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4343 return 0;
4344
4345 /*
4346 * Modify (by adding) the state flag, if writing.
4347 */
4348 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004349 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4350 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004351 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004352 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004353 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004354 }
4355 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4356 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4357
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004358 /*
4359 * Before insertion into the bmbt, zero the range being converted
4360 * if required.
4361 */
4362 if (flags & XFS_BMAPI_ZERO) {
4363 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4364 mval->br_blockcount);
4365 if (error)
4366 return error;
4367 }
4368
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004369 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4370 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004371 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004372 /*
4373 * Log the inode core unconditionally in the unwritten extent conversion
4374 * path because the conversion might not have done so (e.g., if the
4375 * extent count hasn't changed). We need to make sure the inode is dirty
4376 * in the transaction for the sake of fsync(), even if nothing has
4377 * changed, because fsync() will not force the log for this transaction
4378 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004379 *
4380 * Note: If we're only converting cow fork extents, there aren't
4381 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004382 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004383 if (whichfork != XFS_COW_FORK)
4384 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004385 if (error)
4386 return error;
4387
4388 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004389 * Update our extent pointer, given that
4390 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4391 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004392 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004393 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004394
4395 /*
4396 * We may have combined previously unwritten space with written space,
4397 * so generate another request.
4398 */
4399 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004400 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004401 return 0;
4402}
4403
Christoph Hellwig44032802011-09-18 20:40:48 +00004404/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004405 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4406 * extent state if necessary. Details behaviour is controlled by the flags
4407 * parameter. Only allocates blocks from a single allocation group, to avoid
4408 * locking problems.
4409 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 * The returned value in "firstblock" from the first call in a transaction
4411 * must be remembered and presented to subsequent calls in "firstblock".
4412 * An upper bound for the number of blocks to be allocated is supplied to
4413 * the first call in "total"; if no allocation group has that many free
4414 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4415 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004416int
4417xfs_bmapi_write(
4418 struct xfs_trans *tp, /* transaction pointer */
4419 struct xfs_inode *ip, /* incore inode */
4420 xfs_fileoff_t bno, /* starting file offs. mapped */
4421 xfs_filblks_t len, /* length to map in file */
4422 int flags, /* XFS_BMAPI_... */
4423 xfs_fsblock_t *firstblock, /* first allocated block
4424 controls a.g. for allocs */
4425 xfs_extlen_t total, /* total blocks needed */
4426 struct xfs_bmbt_irec *mval, /* output: map values */
4427 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004428 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004430 struct xfs_mount *mp = ip->i_mount;
4431 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004432 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004433 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004434 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004435 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004436 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004437 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004438 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004439
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004441 xfs_fileoff_t orig_bno; /* original block number value */
4442 int orig_flags; /* original flags arg value */
4443 xfs_filblks_t orig_len; /* original value of len arg */
4444 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4445 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
4447 orig_bno = bno;
4448 orig_len = len;
4449 orig_flags = flags;
4450 orig_mval = mval;
4451 orig_nmap = *nmap;
4452#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004453 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004454
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004456 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4457 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004458 ASSERT(tp != NULL ||
4459 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4460 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004461 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004462 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004463 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004464 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004465
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004466 /* zeroing is for currently only for data extents, not metadata */
4467 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4468 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4469 /*
4470 * we can allocate unwritten extents or pre-zero allocated blocks,
4471 * but it makes no sense to do both at once. This would result in
4472 * zeroing the unwritten extent twice, but it still being an
4473 * unwritten extent....
4474 */
4475 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4476 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4477
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 if (unlikely(XFS_TEST_ERROR(
4479 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004480 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004481 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004482 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004483 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004487 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004488
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004490
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004491 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004492
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004493 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004495 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004497 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004498 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004499 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004500 }
4501
4502 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4503 error = xfs_iread_extents(tp, ip, whichfork);
4504 if (error)
4505 goto error0;
4506 }
4507
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 n = 0;
4509 end = bno + len;
4510 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004511
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004512 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4513 eof = true;
4514 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4515 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004516 bma.tp = tp;
4517 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004518 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004519 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004520 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004521 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004522
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004524 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004525
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004526 /* in hole or beyoned EOF? */
4527 if (eof || bma.got.br_startoff > bno) {
4528 if (flags & XFS_BMAPI_DELALLOC) {
4529 /*
4530 * For the COW fork we can reasonably get a
4531 * request for converting an extent that races
4532 * with other threads already having converted
4533 * part of it, as there converting COW to
4534 * regular blocks is not protected using the
4535 * IOLOCK.
4536 */
4537 ASSERT(flags & XFS_BMAPI_COWFORK);
4538 if (!(flags & XFS_BMAPI_COWFORK)) {
4539 error = -EIO;
4540 goto error0;
4541 }
4542
4543 if (eof || bno >= end)
4544 break;
4545 } else {
4546 need_alloc = true;
4547 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004548 } else if (isnullstartblock(bma.got.br_startblock)) {
4549 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004550 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004551
4552 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 * First, deal with the hole before the allocated space
4554 * that we found, if any.
4555 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004556 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004557 bma.eof = eof;
4558 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4559 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004560 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004561 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004562
Dave Chinnera99ebf42011-12-01 11:24:20 +00004563 /*
4564 * There's a 32/64 bit type mismatch between the
4565 * allocation length request (which can be 64 bits in
4566 * length) and the bma length request, which is
4567 * xfs_extlen_t and therefore 32 bits. Hence we have to
4568 * check for 32-bit overflows and handle them here.
4569 */
4570 if (len > (xfs_filblks_t)MAXEXTLEN)
4571 bma.length = MAXEXTLEN;
4572 else
4573 bma.length = len;
4574
4575 ASSERT(len > 0);
4576 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004577 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 if (error)
4579 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004580 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004581 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004582
4583 /*
4584 * If this is a CoW allocation, record the data in
4585 * the refcount btree for orphan recovery.
4586 */
4587 if (whichfork == XFS_COW_FORK) {
4588 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4589 bma.blkno, bma.length);
4590 if (error)
4591 goto error0;
4592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004594
Dave Chinneraef9a892011-09-18 20:40:44 +00004595 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004596 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4597 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Dave Chinnerb447fe52011-09-18 20:40:51 +00004599 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004600 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004601 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004602 continue;
4603 if (error)
4604 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
Dave Chinneraef9a892011-09-18 20:40:44 +00004606 /* update the extent map to return */
4607 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 /*
4610 * If we're done, stop now. Stop when we've allocated
4611 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4612 * the transaction may get too big.
4613 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004614 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004616
4617 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004618 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004619 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4620 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 /*
4625 * Transform from btree to extents, give it cur.
4626 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004627 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004628 int tmp_logflags = 0;
4629
Dave Chinner29c8d172011-09-18 20:41:00 +00004630 ASSERT(bma.cur);
4631 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004633 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 if (error)
4635 goto error0;
4636 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004637
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004639 XFS_IFORK_NEXTENTS(ip, whichfork) >
4640 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642error0:
4643 /*
4644 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004645 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004647 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004649 bma.logflags &= ~xfs_ilog_fext(whichfork);
4650 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004652 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 /*
4654 * Log whatever the flags say, even if error. Otherwise we might miss
4655 * detecting a case where the data is changed, there's an error,
4656 * and it's not logged so we don't shutdown when we should.
4657 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004658 if (bma.logflags)
4659 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004660
Dave Chinner29c8d172011-09-18 20:41:00 +00004661 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 if (!error) {
4663 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004664 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004666 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004667 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004669 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4671 }
4672 if (!error)
4673 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4674 orig_nmap, *nmap);
4675 return error;
4676}
4677
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004678static int
4679xfs_bmapi_remap(
4680 struct xfs_trans *tp,
4681 struct xfs_inode *ip,
4682 xfs_fileoff_t bno,
4683 xfs_filblks_t len,
4684 xfs_fsblock_t startblock,
4685 struct xfs_defer_ops *dfops)
4686{
4687 struct xfs_mount *mp = ip->i_mount;
4688 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4689 struct xfs_btree_cur *cur = NULL;
4690 xfs_fsblock_t firstblock = NULLFSBLOCK;
4691 struct xfs_bmbt_irec got;
4692 xfs_extnum_t idx;
4693 int logflags = 0, error;
4694
4695 ASSERT(len > 0);
4696 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4697 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4698
4699 if (unlikely(XFS_TEST_ERROR(
4700 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4701 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004702 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004703 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4704 return -EFSCORRUPTED;
4705 }
4706
4707 if (XFS_FORCED_SHUTDOWN(mp))
4708 return -EIO;
4709
4710 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4711 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4712 if (error)
4713 return error;
4714 }
4715
4716 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4717 /* make sure we only reflink into a hole. */
4718 ASSERT(got.br_startoff > bno);
4719 ASSERT(got.br_startoff - bno >= len);
4720 }
4721
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004722 ip->i_d.di_nblocks += len;
4723 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004724
4725 if (ifp->if_flags & XFS_IFBROOT) {
4726 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4727 cur->bc_private.b.firstblock = firstblock;
4728 cur->bc_private.b.dfops = dfops;
4729 cur->bc_private.b.flags = 0;
4730 }
4731
4732 got.br_startoff = bno;
4733 got.br_startblock = startblock;
4734 got.br_blockcount = len;
4735 got.br_state = XFS_EXT_NORM;
4736
4737 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4738 &got, &firstblock, dfops, &logflags);
4739 if (error)
4740 goto error0;
4741
4742 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4743 int tmp_logflags = 0;
4744
4745 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4746 &tmp_logflags, XFS_DATA_FORK);
4747 logflags |= tmp_logflags;
4748 }
4749
4750error0:
4751 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4752 logflags &= ~XFS_ILOG_DEXT;
4753 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4754 logflags &= ~XFS_ILOG_DBROOT;
4755
4756 if (logflags)
4757 xfs_trans_log_inode(tp, ip, logflags);
4758 if (cur) {
4759 xfs_btree_del_cursor(cur,
4760 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4761 }
4762 return error;
4763}
4764
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004766 * When a delalloc extent is split (e.g., due to a hole punch), the original
4767 * indlen reservation must be shared across the two new extents that are left
4768 * behind.
4769 *
4770 * Given the original reservation and the worst case indlen for the two new
4771 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004772 * reservation fairly across the two new extents. If necessary, steal available
4773 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4774 * ores == 1). The number of stolen blocks is returned. The availability and
4775 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004776 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004777static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004778xfs_bmap_split_indlen(
4779 xfs_filblks_t ores, /* original res. */
4780 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004781 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4782 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004783{
4784 xfs_filblks_t len1 = *indlen1;
4785 xfs_filblks_t len2 = *indlen2;
4786 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004787 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004788 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004789
4790 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004791 * Steal as many blocks as we can to try and satisfy the worst case
4792 * indlen for both new extents.
4793 */
Brian Foster75d65362017-02-13 22:48:30 -08004794 if (ores < nres && avail)
4795 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4796 ores += stolen;
4797
4798 /* nothing else to do if we've satisfied the new reservation */
4799 if (ores >= nres)
4800 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004801
4802 /*
Brian Foster75d65362017-02-13 22:48:30 -08004803 * We can't meet the total required reservation for the two extents.
4804 * Calculate the percent of the overall shortage between both extents
4805 * and apply this percentage to each of the requested indlen values.
4806 * This distributes the shortage fairly and reduces the chances that one
4807 * of the two extents is left with nothing when extents are repeatedly
4808 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004809 */
Brian Foster75d65362017-02-13 22:48:30 -08004810 resfactor = (ores * 100);
4811 do_div(resfactor, nres);
4812 len1 *= resfactor;
4813 do_div(len1, 100);
4814 len2 *= resfactor;
4815 do_div(len2, 100);
4816 ASSERT(len1 + len2 <= ores);
4817 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4818
4819 /*
4820 * Hand out the remainder to each extent. If one of the two reservations
4821 * is zero, we want to make sure that one gets a block first. The loop
4822 * below starts with len1, so hand len2 a block right off the bat if it
4823 * is zero.
4824 */
4825 ores -= (len1 + len2);
4826 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4827 if (ores && !len2 && *indlen2) {
4828 len2++;
4829 ores--;
4830 }
4831 while (ores) {
4832 if (len1 < *indlen1) {
4833 len1++;
4834 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004835 }
Brian Foster75d65362017-02-13 22:48:30 -08004836 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004837 break;
Brian Foster75d65362017-02-13 22:48:30 -08004838 if (len2 < *indlen2) {
4839 len2++;
4840 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004841 }
4842 }
4843
4844 *indlen1 = len1;
4845 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004846
4847 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004848}
4849
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004850int
4851xfs_bmap_del_extent_delay(
4852 struct xfs_inode *ip,
4853 int whichfork,
4854 xfs_extnum_t *idx,
4855 struct xfs_bmbt_irec *got,
4856 struct xfs_bmbt_irec *del)
4857{
4858 struct xfs_mount *mp = ip->i_mount;
4859 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4860 struct xfs_bmbt_irec new;
4861 int64_t da_old, da_new, da_diff = 0;
4862 xfs_fileoff_t del_endoff, got_endoff;
4863 xfs_filblks_t got_indlen, new_indlen, stolen;
4864 int error = 0, state = 0;
4865 bool isrt;
4866
4867 XFS_STATS_INC(mp, xs_del_exlist);
4868
4869 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4870 del_endoff = del->br_startoff + del->br_blockcount;
4871 got_endoff = got->br_startoff + got->br_blockcount;
4872 da_old = startblockval(got->br_startblock);
4873 da_new = 0;
4874
4875 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004876 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004877 ASSERT(del->br_blockcount > 0);
4878 ASSERT(got->br_startoff <= del->br_startoff);
4879 ASSERT(got_endoff >= del_endoff);
4880
4881 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004882 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004883
4884 do_div(rtexts, mp->m_sb.sb_rextsize);
4885 xfs_mod_frextents(mp, rtexts);
4886 }
4887
4888 /*
4889 * Update the inode delalloc counter now and wait to update the
4890 * sb counters as we might have to borrow some blocks for the
4891 * indirect block accounting.
4892 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004893 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4894 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004895 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004896 if (error)
4897 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004898 ip->i_delayed_blks -= del->br_blockcount;
4899
4900 if (whichfork == XFS_COW_FORK)
4901 state |= BMAP_COWFORK;
4902
4903 if (got->br_startoff == del->br_startoff)
4904 state |= BMAP_LEFT_CONTIG;
4905 if (got_endoff == del_endoff)
4906 state |= BMAP_RIGHT_CONTIG;
4907
4908 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4909 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4910 /*
4911 * Matches the whole extent. Delete the entry.
4912 */
4913 xfs_iext_remove(ip, *idx, 1, state);
4914 --*idx;
4915 break;
4916 case BMAP_LEFT_CONTIG:
4917 /*
4918 * Deleting the first part of the extent.
4919 */
4920 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4921 got->br_startoff = del_endoff;
4922 got->br_blockcount -= del->br_blockcount;
4923 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4924 got->br_blockcount), da_old);
4925 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004926 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004927 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4928 break;
4929 case BMAP_RIGHT_CONTIG:
4930 /*
4931 * Deleting the last part of the extent.
4932 */
4933 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4934 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4935 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4936 got->br_blockcount), da_old);
4937 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004938 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004939 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4940 break;
4941 case 0:
4942 /*
4943 * Deleting the middle of the extent.
4944 *
4945 * Distribute the original indlen reservation across the two new
4946 * extents. Steal blocks from the deleted extent if necessary.
4947 * Stealing blocks simply fudges the fdblocks accounting below.
4948 * Warn if either of the new indlen reservations is zero as this
4949 * can lead to delalloc problems.
4950 */
4951 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4952
4953 got->br_blockcount = del->br_startoff - got->br_startoff;
4954 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4955
4956 new.br_blockcount = got_endoff - del_endoff;
4957 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4958
4959 WARN_ON_ONCE(!got_indlen || !new_indlen);
4960 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4961 del->br_blockcount);
4962
4963 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004964 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004965 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4966
4967 new.br_startoff = del_endoff;
4968 new.br_state = got->br_state;
4969 new.br_startblock = nullstartblock((int)new_indlen);
4970
4971 ++*idx;
4972 xfs_iext_insert(ip, *idx, 1, &new, state);
4973
4974 da_new = got_indlen + new_indlen - stolen;
4975 del->br_blockcount -= stolen;
4976 break;
4977 }
4978
4979 ASSERT(da_old >= da_new);
4980 da_diff = da_old - da_new;
4981 if (!isrt)
4982 da_diff += del->br_blockcount;
4983 if (da_diff)
4984 xfs_mod_fdblocks(mp, da_diff, false);
4985 return error;
4986}
4987
4988void
4989xfs_bmap_del_extent_cow(
4990 struct xfs_inode *ip,
4991 xfs_extnum_t *idx,
4992 struct xfs_bmbt_irec *got,
4993 struct xfs_bmbt_irec *del)
4994{
4995 struct xfs_mount *mp = ip->i_mount;
4996 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4997 struct xfs_bmbt_irec new;
4998 xfs_fileoff_t del_endoff, got_endoff;
4999 int state = BMAP_COWFORK;
5000
5001 XFS_STATS_INC(mp, xs_del_exlist);
5002
5003 del_endoff = del->br_startoff + del->br_blockcount;
5004 got_endoff = got->br_startoff + got->br_blockcount;
5005
5006 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11005007 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005008 ASSERT(del->br_blockcount > 0);
5009 ASSERT(got->br_startoff <= del->br_startoff);
5010 ASSERT(got_endoff >= del_endoff);
5011 ASSERT(!isnullstartblock(got->br_startblock));
5012
5013 if (got->br_startoff == del->br_startoff)
5014 state |= BMAP_LEFT_CONTIG;
5015 if (got_endoff == del_endoff)
5016 state |= BMAP_RIGHT_CONTIG;
5017
5018 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
5019 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
5020 /*
5021 * Matches the whole extent. Delete the entry.
5022 */
5023 xfs_iext_remove(ip, *idx, 1, state);
5024 --*idx;
5025 break;
5026 case BMAP_LEFT_CONTIG:
5027 /*
5028 * Deleting the first part of the extent.
5029 */
5030 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5031 got->br_startoff = del_endoff;
5032 got->br_blockcount -= del->br_blockcount;
5033 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005034 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005035 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5036 break;
5037 case BMAP_RIGHT_CONTIG:
5038 /*
5039 * Deleting the last part of the extent.
5040 */
5041 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5042 got->br_blockcount -= del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005043 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005044 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5045 break;
5046 case 0:
5047 /*
5048 * Deleting the middle of the extent.
5049 */
5050 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5051 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005052 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005053 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5054
5055 new.br_startoff = del_endoff;
5056 new.br_blockcount = got_endoff - del_endoff;
5057 new.br_state = got->br_state;
5058 new.br_startblock = del->br_startblock + del->br_blockcount;
5059
5060 ++*idx;
5061 xfs_iext_insert(ip, *idx, 1, &new, state);
5062 break;
5063 }
5064}
5065
Brian Fostera9bd24a2016-03-15 11:42:46 +11005066/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11005067 * Called by xfs_bmapi to update file extent records and the btree
5068 * after removing space (or undoing a delayed allocation).
5069 */
5070STATIC int /* error */
5071xfs_bmap_del_extent(
5072 xfs_inode_t *ip, /* incore inode pointer */
5073 xfs_trans_t *tp, /* current transaction pointer */
5074 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005075 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005076 xfs_btree_cur_t *cur, /* if null, not a btree */
5077 xfs_bmbt_irec_t *del, /* data to remove from extents */
5078 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005079 int whichfork, /* data or attr fork */
5080 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005081{
5082 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5083 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5084 xfs_fsblock_t del_endblock=0; /* first block past del */
5085 xfs_fileoff_t del_endoff; /* first offset past del */
5086 int delay; /* current block is delayed allocated */
5087 int do_fx; /* free extent at end of routine */
5088 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5089 int error; /* error return value */
5090 int flags; /* inode logging flags */
5091 xfs_bmbt_irec_t got; /* current extent entry */
5092 xfs_fileoff_t got_endoff; /* first offset past got */
5093 int i; /* temp state */
5094 xfs_ifork_t *ifp; /* inode fork pointer */
5095 xfs_mount_t *mp; /* mount structure */
5096 xfs_filblks_t nblks; /* quota/sb block count */
5097 xfs_bmbt_irec_t new; /* new record to be inserted */
5098 /* REFERENCED */
5099 uint qfield; /* quota field to update */
5100 xfs_filblks_t temp; /* for indirect length calculations */
5101 xfs_filblks_t temp2; /* for indirect length calculations */
5102 int state = 0;
5103
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005104 mp = ip->i_mount;
5105 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005106
5107 if (whichfork == XFS_ATTR_FORK)
5108 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005109 else if (whichfork == XFS_COW_FORK)
5110 state |= BMAP_COWFORK;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005111
Dave Chinner9e5987a72013-02-25 12:31:26 +11005112 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005113 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005114 ASSERT(del->br_blockcount > 0);
5115 ep = xfs_iext_get_ext(ifp, *idx);
5116 xfs_bmbt_get_all(ep, &got);
5117 ASSERT(got.br_startoff <= del->br_startoff);
5118 del_endoff = del->br_startoff + del->br_blockcount;
5119 got_endoff = got.br_startoff + got.br_blockcount;
5120 ASSERT(got_endoff >= del_endoff);
5121 delay = isnullstartblock(got.br_startblock);
5122 ASSERT(isnullstartblock(del->br_startblock) == delay);
5123 flags = 0;
5124 qfield = 0;
5125 error = 0;
5126 /*
5127 * If deleting a real allocation, must free up the disk space.
5128 */
5129 if (!delay) {
5130 flags = XFS_ILOG_CORE;
5131 /*
5132 * Realtime allocation. Free it and record di_nblocks update.
5133 */
5134 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5135 xfs_fsblock_t bno;
5136 xfs_filblks_t len;
5137
5138 ASSERT(do_mod(del->br_blockcount,
5139 mp->m_sb.sb_rextsize) == 0);
5140 ASSERT(do_mod(del->br_startblock,
5141 mp->m_sb.sb_rextsize) == 0);
5142 bno = del->br_startblock;
5143 len = del->br_blockcount;
5144 do_div(bno, mp->m_sb.sb_rextsize);
5145 do_div(len, mp->m_sb.sb_rextsize);
5146 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5147 if (error)
5148 goto done;
5149 do_fx = 0;
5150 nblks = len * mp->m_sb.sb_rextsize;
5151 qfield = XFS_TRANS_DQ_RTBCOUNT;
5152 }
5153 /*
5154 * Ordinary allocation.
5155 */
5156 else {
5157 do_fx = 1;
5158 nblks = del->br_blockcount;
5159 qfield = XFS_TRANS_DQ_BCOUNT;
5160 }
5161 /*
5162 * Set up del_endblock and cur for later.
5163 */
5164 del_endblock = del->br_startblock + del->br_blockcount;
5165 if (cur) {
5166 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5167 got.br_startblock, got.br_blockcount,
5168 &i)))
5169 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005170 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005171 }
5172 da_old = da_new = 0;
5173 } else {
5174 da_old = startblockval(got.br_startblock);
5175 da_new = 0;
5176 nblks = 0;
5177 do_fx = 0;
5178 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005179
Dave Chinner9e5987a72013-02-25 12:31:26 +11005180 /*
5181 * Set flag value to use in switch statement.
5182 * Left-contig is 2, right-contig is 1.
5183 */
5184 switch (((got.br_startoff == del->br_startoff) << 1) |
5185 (got_endoff == del_endoff)) {
5186 case 3:
5187 /*
5188 * Matches the whole extent. Delete the entry.
5189 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005190 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005191 xfs_iext_remove(ip, *idx, 1,
5192 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5193 --*idx;
5194 if (delay)
5195 break;
5196
5197 XFS_IFORK_NEXT_SET(ip, whichfork,
5198 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5199 flags |= XFS_ILOG_CORE;
5200 if (!cur) {
5201 flags |= xfs_ilog_fext(whichfork);
5202 break;
5203 }
5204 if ((error = xfs_btree_delete(cur, &i)))
5205 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005206 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005207 break;
5208
5209 case 2:
5210 /*
5211 * Deleting the first part of the extent.
5212 */
5213 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5214 xfs_bmbt_set_startoff(ep, del_endoff);
5215 temp = got.br_blockcount - del->br_blockcount;
5216 xfs_bmbt_set_blockcount(ep, temp);
5217 if (delay) {
5218 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5219 da_old);
5220 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5221 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5222 da_new = temp;
5223 break;
5224 }
5225 xfs_bmbt_set_startblock(ep, del_endblock);
5226 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5227 if (!cur) {
5228 flags |= xfs_ilog_fext(whichfork);
5229 break;
5230 }
5231 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5232 got.br_blockcount - del->br_blockcount,
5233 got.br_state)))
5234 goto done;
5235 break;
5236
5237 case 1:
5238 /*
5239 * Deleting the last part of the extent.
5240 */
5241 temp = got.br_blockcount - del->br_blockcount;
5242 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5243 xfs_bmbt_set_blockcount(ep, temp);
5244 if (delay) {
5245 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5246 da_old);
5247 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5248 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5249 da_new = temp;
5250 break;
5251 }
5252 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5253 if (!cur) {
5254 flags |= xfs_ilog_fext(whichfork);
5255 break;
5256 }
5257 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5258 got.br_startblock,
5259 got.br_blockcount - del->br_blockcount,
5260 got.br_state)))
5261 goto done;
5262 break;
5263
5264 case 0:
5265 /*
5266 * Deleting the middle of the extent.
5267 */
5268 temp = del->br_startoff - got.br_startoff;
5269 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5270 xfs_bmbt_set_blockcount(ep, temp);
5271 new.br_startoff = del_endoff;
5272 temp2 = got_endoff - del_endoff;
5273 new.br_blockcount = temp2;
5274 new.br_state = got.br_state;
5275 if (!delay) {
5276 new.br_startblock = del_endblock;
5277 flags |= XFS_ILOG_CORE;
5278 if (cur) {
5279 if ((error = xfs_bmbt_update(cur,
5280 got.br_startoff,
5281 got.br_startblock, temp,
5282 got.br_state)))
5283 goto done;
5284 if ((error = xfs_btree_increment(cur, 0, &i)))
5285 goto done;
5286 cur->bc_rec.b = new;
5287 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10005288 if (error && error != -ENOSPC)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005289 goto done;
5290 /*
5291 * If get no-space back from btree insert,
5292 * it tried a split, and we have a zero
5293 * block reservation.
5294 * Fix up our state and return the error.
5295 */
Dave Chinner24513372014-06-25 14:58:08 +10005296 if (error == -ENOSPC) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11005297 /*
5298 * Reset the cursor, don't trust
5299 * it after any insert operation.
5300 */
5301 if ((error = xfs_bmbt_lookup_eq(cur,
5302 got.br_startoff,
5303 got.br_startblock,
5304 temp, &i)))
5305 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005306 XFS_WANT_CORRUPTED_GOTO(mp,
5307 i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005308 /*
5309 * Update the btree record back
5310 * to the original value.
5311 */
5312 if ((error = xfs_bmbt_update(cur,
5313 got.br_startoff,
5314 got.br_startblock,
5315 got.br_blockcount,
5316 got.br_state)))
5317 goto done;
5318 /*
5319 * Reset the extent record back
5320 * to the original value.
5321 */
5322 xfs_bmbt_set_blockcount(ep,
5323 got.br_blockcount);
5324 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005325 error = -ENOSPC;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005326 goto done;
5327 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005328 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005329 } else
5330 flags |= xfs_ilog_fext(whichfork);
5331 XFS_IFORK_NEXT_SET(ip, whichfork,
5332 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5333 } else {
Brian Fosterd34999c2016-03-15 11:42:47 +11005334 xfs_filblks_t stolen;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005335 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005336
5337 /*
5338 * Distribute the original indlen reservation across the
Brian Fosterd34999c2016-03-15 11:42:47 +11005339 * two new extents. Steal blocks from the deleted extent
5340 * if necessary. Stealing blocks simply fudges the
5341 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005342 */
5343 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5344 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c2016-03-15 11:42:47 +11005345 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5346 del->br_blockcount);
5347 da_new = temp + temp2 - stolen;
5348 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005349
5350 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11005351 * Set the reservation for each extent. Warn if either
5352 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005353 */
Brian Fosterd34999c2016-03-15 11:42:47 +11005354 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005355 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005356 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005357 }
5358 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5359 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5360 ++*idx;
5361 break;
5362 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005363
5364 /* remove reverse mapping */
5365 if (!delay) {
5366 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5367 if (error)
5368 goto done;
5369 }
5370
Dave Chinner9e5987a72013-02-25 12:31:26 +11005371 /*
5372 * If we need to, add to list of extents to delete.
5373 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005374 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005375 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5376 error = xfs_refcount_decrease_extent(mp, dfops, del);
5377 if (error)
5378 goto done;
5379 } else
5380 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5381 del->br_blockcount, NULL);
5382 }
5383
Dave Chinner9e5987a72013-02-25 12:31:26 +11005384 /*
5385 * Adjust inode # blocks in the file.
5386 */
5387 if (nblks)
5388 ip->i_d.di_nblocks -= nblks;
5389 /*
5390 * Adjust quota data.
5391 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005392 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005393 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5394
5395 /*
5396 * Account for change in delayed indirect blocks.
5397 * Nothing to do for disk quota accounting here.
5398 */
5399 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005400 if (da_old > da_new)
5401 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005402done:
5403 *logflagsp = flags;
5404 return error;
5405}
5406
5407/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 * Unmap (remove) blocks from a file.
5409 * If nexts is nonzero then the number of extents to remove is limited to
5410 * that value. If not all extents in the block range can be removed then
5411 * *done is set.
5412 */
5413int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005414__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 xfs_trans_t *tp, /* transaction pointer */
5416 struct xfs_inode *ip, /* incore inode */
5417 xfs_fileoff_t bno, /* starting offset to unmap */
Darrick J. Wong44535932016-10-03 09:11:29 -07005418 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 int flags, /* misc flags */
5420 xfs_extnum_t nexts, /* number of extents max */
5421 xfs_fsblock_t *firstblock, /* first allocated block
5422 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005423 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424{
5425 xfs_btree_cur_t *cur; /* bmap btree cursor */
5426 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 int error; /* error return value */
5428 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005429 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 xfs_ifork_t *ifp; /* inode fork pointer */
5431 int isrt; /* freeing in rt area */
5432 xfs_extnum_t lastx; /* last extent index used */
5433 int logflags; /* transaction logging flags */
5434 xfs_extlen_t mod; /* rt extent offset */
5435 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 xfs_fileoff_t start; /* first file offset deleted */
5437 int tmp_logflags; /* partial logging flags */
5438 int wasdel; /* was a delayed alloc extent */
5439 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005441 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005442 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005443 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005445 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5446
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005447 whichfork = xfs_bmapi_whichfork(flags);
5448 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 ifp = XFS_IFORK_PTR(ip, whichfork);
5450 if (unlikely(
5451 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5452 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5453 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5454 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005455 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 }
5457 mp = ip->i_mount;
5458 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005459 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005460
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005461 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 ASSERT(len > 0);
5463 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005464
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005465 /*
5466 * Guesstimate how many blocks we can unmap without running the risk of
5467 * blowing out the transaction with a mix of EFIs and reflink
5468 * adjustments.
5469 */
5470 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5471 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5472 else
5473 max_len = len;
5474
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5476 (error = xfs_iread_extents(tp, ip, whichfork)))
5477 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005478 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005479 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 return 0;
5481 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005482 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005483 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 start = bno;
5485 bno = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005486
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 /*
5488 * Check to see if the given block number is past the end of the
5489 * file, back up to the last block if so...
5490 */
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005491 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5492 ASSERT(lastx > 0);
5493 xfs_iext_get_extent(ifp, --lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 bno = got.br_startoff + got.br_blockcount - 1;
5495 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005496
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 logflags = 0;
5498 if (ifp->if_flags & XFS_IFBROOT) {
5499 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005500 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005502 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005503 cur->bc_private.b.flags = 0;
5504 } else
5505 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005506
5507 if (isrt) {
5508 /*
5509 * Synchronize by locking the bitmap inode.
5510 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005511 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005512 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005513 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5514 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005515 }
5516
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 extno = 0;
5518 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005519 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 /*
5521 * Is the found extent after a hole in which bno lives?
5522 * Just back up to the previous extent, if so.
5523 */
5524 if (got.br_startoff > bno) {
5525 if (--lastx < 0)
5526 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005527 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005528 }
5529 /*
5530 * Is the last block of this extent before the range
5531 * we're supposed to delete? If so, we're done.
5532 */
5533 bno = XFS_FILEOFF_MIN(bno,
5534 got.br_startoff + got.br_blockcount - 1);
5535 if (bno < start)
5536 break;
5537 /*
5538 * Then deal with the (possibly delayed) allocated space
5539 * we found.
5540 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005542 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005543
5544 /*
5545 * Make sure we don't touch multiple AGF headers out of order
5546 * in a single transaction, as that could cause AB-BA deadlocks.
5547 */
5548 if (!wasdel) {
5549 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5550 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5551 break;
5552 prev_agno = agno;
5553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554 if (got.br_startoff < start) {
5555 del.br_startoff = start;
5556 del.br_blockcount -= start - got.br_startoff;
5557 if (!wasdel)
5558 del.br_startblock += start - got.br_startoff;
5559 }
5560 if (del.br_startoff + del.br_blockcount > bno + 1)
5561 del.br_blockcount = bno + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005562
5563 /* How much can we safely unmap? */
5564 if (max_len < del.br_blockcount) {
5565 del.br_startoff += del.br_blockcount - max_len;
5566 if (!wasdel)
5567 del.br_startblock += del.br_blockcount - max_len;
5568 del.br_blockcount = max_len;
5569 }
5570
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 sum = del.br_startblock + del.br_blockcount;
5572 if (isrt &&
5573 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5574 /*
5575 * Realtime extent not lined up at the end.
5576 * The extent could have been split into written
5577 * and unwritten pieces, or we could just be
5578 * unmapping part of it. But we can't really
5579 * get rid of part of a realtime extent.
5580 */
5581 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005582 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 /*
5584 * This piece is unwritten, or we're not
5585 * using unwritten extents. Skip over it.
5586 */
5587 ASSERT(bno >= mod);
5588 bno -= mod > del.br_blockcount ?
5589 del.br_blockcount : mod;
5590 if (bno < got.br_startoff) {
5591 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005592 xfs_bmbt_get_all(xfs_iext_get_ext(
5593 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 }
5595 continue;
5596 }
5597 /*
5598 * It's written, turn it unwritten.
5599 * This is better than zeroing it.
5600 */
5601 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005602 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 /*
5604 * If this spans a realtime extent boundary,
5605 * chop it back to the start of the one we end at.
5606 */
5607 if (del.br_blockcount > mod) {
5608 del.br_startoff += del.br_blockcount - mod;
5609 del.br_startblock += del.br_blockcount - mod;
5610 del.br_blockcount = mod;
5611 }
5612 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005613 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005614 whichfork, &lastx, &cur, &del,
5615 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 if (error)
5617 goto error0;
5618 goto nodelete;
5619 }
5620 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5621 /*
5622 * Realtime extent is lined up at the end but not
5623 * at the front. We'll get rid of full extents if
5624 * we can.
5625 */
5626 mod = mp->m_sb.sb_rextsize - mod;
5627 if (del.br_blockcount > mod) {
5628 del.br_blockcount -= mod;
5629 del.br_startoff += mod;
5630 del.br_startblock += mod;
5631 } else if ((del.br_startoff == start &&
5632 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005633 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005634 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 /*
5636 * Can't make it unwritten. There isn't
5637 * a full extent here so just skip it.
5638 */
5639 ASSERT(bno >= del.br_blockcount);
5640 bno -= del.br_blockcount;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005641 if (got.br_startoff > bno && --lastx >= 0)
5642 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005643 continue;
5644 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005645 struct xfs_bmbt_irec prev;
5646
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 /*
5648 * This one is already unwritten.
5649 * It must have a written left neighbor.
5650 * Unwrite the killed part of that one and
5651 * try again.
5652 */
5653 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005654 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005656 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 ASSERT(del.br_startblock ==
5658 prev.br_startblock + prev.br_blockcount);
5659 if (prev.br_startoff < start) {
5660 mod = start - prev.br_startoff;
5661 prev.br_blockcount -= mod;
5662 prev.br_startblock += mod;
5663 prev.br_startoff = start;
5664 }
5665 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005666 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005667 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005668 ip, whichfork, &lastx, &cur,
5669 &prev, firstblock, dfops,
5670 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 if (error)
5672 goto error0;
5673 goto nodelete;
5674 } else {
5675 ASSERT(del.br_state == XFS_EXT_NORM);
5676 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005677 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005678 ip, whichfork, &lastx, &cur,
5679 &del, firstblock, dfops,
5680 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 if (error)
5682 goto error0;
5683 goto nodelete;
5684 }
5685 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005686
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 /*
5688 * If it's the case where the directory code is running
5689 * with no block reservation, and the deleted block is in
5690 * the middle of its extent, and the resulting insert
5691 * of an extent would cause transformation to btree format,
5692 * then reject it. The calling code will then swap
5693 * blocks around instead.
5694 * We have to do this now, rather than waiting for the
5695 * conversion to btree format, since the transaction
5696 * will be dirty.
5697 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005698 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005700 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5701 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 del.br_startoff > got.br_startoff &&
5703 del.br_startoff + del.br_blockcount <
5704 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005705 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 goto error0;
5707 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005708
5709 /*
5710 * Unreserve quota and update realtime free space, if
5711 * appropriate. If delayed allocation, update the inode delalloc
5712 * counter now and wait to update the sb counters as
5713 * xfs_bmap_del_extent() might need to borrow some blocks.
5714 */
5715 if (wasdel) {
5716 ASSERT(startblockval(del.br_startblock) > 0);
5717 if (isrt) {
5718 xfs_filblks_t rtexts;
5719
5720 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5721 do_div(rtexts, mp->m_sb.sb_rextsize);
5722 xfs_mod_frextents(mp, (int64_t)rtexts);
5723 (void)xfs_trans_reserve_quota_nblks(NULL,
5724 ip, -((long)del.br_blockcount), 0,
5725 XFS_QMOPT_RES_RTBLKS);
5726 } else {
5727 (void)xfs_trans_reserve_quota_nblks(NULL,
5728 ip, -((long)del.br_blockcount), 0,
5729 XFS_QMOPT_RES_REGBLKS);
5730 }
5731 ip->i_delayed_blks -= del.br_blockcount;
5732 if (cur)
5733 cur->bc_private.b.flags |=
5734 XFS_BTCUR_BPRV_WASDEL;
5735 } else if (cur)
5736 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5737
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005738 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005739 &tmp_logflags, whichfork, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 logflags |= tmp_logflags;
5741 if (error)
5742 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005743
5744 if (!isrt && wasdel)
5745 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5746
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005747 max_len -= del.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 bno = del.br_startoff - 1;
5749nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 /*
5751 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005754 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005755 xfs_iext_get_extent(ifp, lastx, &got);
5756 if (got.br_startoff > bno && --lastx >= 0)
5757 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 extno++;
5760 }
5761 }
Darrick J. Wong44535932016-10-03 09:11:29 -07005762 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5763 *rlen = 0;
5764 else
5765 *rlen = bno - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005766
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 /*
5768 * Convert to a btree if necessary.
5769 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005770 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005772 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 &cur, 0, &tmp_logflags, whichfork);
5774 logflags |= tmp_logflags;
5775 if (error)
5776 goto error0;
5777 }
5778 /*
5779 * transform from btree to extents, give it cur
5780 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005781 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 ASSERT(cur != NULL);
5783 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5784 whichfork);
5785 logflags |= tmp_logflags;
5786 if (error)
5787 goto error0;
5788 }
5789 /*
5790 * transform from extents to local?
5791 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 error = 0;
5793error0:
5794 /*
5795 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005796 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005798 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005800 logflags &= ~xfs_ilog_fext(whichfork);
5801 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005803 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 /*
5805 * Log inode even in the error case, if the transaction
5806 * is dirty we'll need to shut down the filesystem.
5807 */
5808 if (logflags)
5809 xfs_trans_log_inode(tp, ip, logflags);
5810 if (cur) {
5811 if (!error) {
5812 *firstblock = cur->bc_private.b.firstblock;
5813 cur->bc_private.b.allocated = 0;
5814 }
5815 xfs_btree_del_cursor(cur,
5816 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5817 }
5818 return error;
5819}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005820
Darrick J. Wong44535932016-10-03 09:11:29 -07005821/* Unmap a range of a file. */
5822int
5823xfs_bunmapi(
5824 xfs_trans_t *tp,
5825 struct xfs_inode *ip,
5826 xfs_fileoff_t bno,
5827 xfs_filblks_t len,
5828 int flags,
5829 xfs_extnum_t nexts,
5830 xfs_fsblock_t *firstblock,
5831 struct xfs_defer_ops *dfops,
5832 int *done)
5833{
5834 int error;
5835
5836 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5837 dfops);
5838 *done = (len == 0);
5839 return error;
5840}
5841
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005842/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005843 * Determine whether an extent shift can be accomplished by a merge with the
5844 * extent that precedes the target hole of the shift.
5845 */
5846STATIC bool
5847xfs_bmse_can_merge(
5848 struct xfs_bmbt_irec *left, /* preceding extent */
5849 struct xfs_bmbt_irec *got, /* current extent to shift */
5850 xfs_fileoff_t shift) /* shift fsb */
5851{
5852 xfs_fileoff_t startoff;
5853
5854 startoff = got->br_startoff - shift;
5855
5856 /*
5857 * The extent, once shifted, must be adjacent in-file and on-disk with
5858 * the preceding extent.
5859 */
5860 if ((left->br_startoff + left->br_blockcount != startoff) ||
5861 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5862 (left->br_state != got->br_state) ||
5863 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5864 return false;
5865
5866 return true;
5867}
5868
5869/*
5870 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5871 * hole in the file. If an extent shift would result in the extent being fully
5872 * adjacent to the extent that currently precedes the hole, we can merge with
5873 * the preceding extent rather than do the shift.
5874 *
5875 * This function assumes the caller has verified a shift-by-merge is possible
5876 * with the provided extents via xfs_bmse_can_merge().
5877 */
5878STATIC int
5879xfs_bmse_merge(
5880 struct xfs_inode *ip,
5881 int whichfork,
5882 xfs_fileoff_t shift, /* shift fsb */
5883 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005884 struct xfs_bmbt_irec *got, /* extent to shift */
5885 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005886 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005887 int *logflags, /* output */
5888 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005889{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005890 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5891 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005892 xfs_filblks_t blockcount;
5893 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005894 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005895
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005896 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005897
5898 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5899 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005900 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005901
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005902 new = *left;
5903 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005904
5905 /*
5906 * Update the on-disk extent count, the btree if necessary and log the
5907 * inode.
5908 */
5909 XFS_IFORK_NEXT_SET(ip, whichfork,
5910 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5911 *logflags |= XFS_ILOG_CORE;
5912 if (!cur) {
5913 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005914 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005915 }
5916
5917 /* lookup and remove the extent to merge */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005918 error = xfs_bmbt_lookup_eq(cur, got->br_startoff, got->br_startblock,
5919 got->br_blockcount, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005920 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005921 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005922 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005923
5924 error = xfs_btree_delete(cur, &i);
5925 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005926 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005927 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005928
5929 /* lookup and update size of the previous extent */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005930 error = xfs_bmbt_lookup_eq(cur, left->br_startoff, left->br_startblock,
5931 left->br_blockcount, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005932 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005933 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005934 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005935
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005936 error = xfs_bmbt_update(cur, new.br_startoff, new.br_startblock,
5937 new.br_blockcount, new.br_state);
5938 if (error)
5939 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005940
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005941done:
5942 xfs_iext_update_extent(ifp, current_ext - 1, &new);
5943 xfs_iext_remove(ip, current_ext, 1, 0);
5944
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005945 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005946 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5947 if (error)
5948 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005949 memcpy(&new, got, sizeof(new));
5950 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005951 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005952}
5953
5954/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005955 * Shift a single extent.
5956 */
5957STATIC int
5958xfs_bmse_shift_one(
5959 struct xfs_inode *ip,
5960 int whichfork,
5961 xfs_fileoff_t offset_shift_fsb,
5962 int *current_ext,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005963 struct xfs_bmbt_irec *got,
Brian Fostera979bdf2014-09-23 15:39:04 +10005964 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005965 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005966 enum shift_direction direction,
5967 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005968{
5969 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005970 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005971 xfs_fileoff_t startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005972 struct xfs_bmbt_irec adj_irec, new;
Brian Fostera979bdf2014-09-23 15:39:04 +10005973 int error;
5974 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005975 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005976
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005977 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005978 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005979 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005980
Brian Fosterf71721d2014-09-23 15:39:05 +10005981 /* delalloc extents should be prevented by caller */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005982 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got->br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005983
Namjae Jeona904b1c2015-03-25 15:08:56 +11005984 if (direction == SHIFT_LEFT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005985 startoff = got->br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005986
Namjae Jeona904b1c2015-03-25 15:08:56 +11005987 /*
5988 * Check for merge if we've got an extent to the left,
5989 * otherwise make sure there's enough room at the start
5990 * of the file for the shift.
5991 */
5992 if (!*current_ext) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005993 if (got->br_startoff < offset_shift_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005994 return -EINVAL;
5995 goto update_current_ext;
5996 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005997
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005998 /*
5999 * grab the left extent and check for a large enough hole.
6000 */
6001 xfs_iext_get_extent(ifp, *current_ext - 1, &adj_irec);
6002 if (startoff < adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10006003 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10006004
Dave Chinnerb11bd672014-12-04 09:42:24 +11006005 /* check whether to merge the extent or shift it down */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006006 if (xfs_bmse_can_merge(&adj_irec, got, offset_shift_fsb)) {
6007 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
6008 *current_ext, got, &adj_irec,
6009 cur, logflags, dfops);
Dave Chinnerb11bd672014-12-04 09:42:24 +11006010 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006011 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006012 startoff = got->br_startoff + offset_shift_fsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006013 /* nothing to move if this is the last extent */
6014 if (*current_ext >= (total_extents - 1))
6015 goto update_current_ext;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006016
Namjae Jeona904b1c2015-03-25 15:08:56 +11006017 /*
6018 * If this is not the last extent in the file, make sure there
6019 * is enough room between current extent and next extent for
6020 * accommodating the shift.
6021 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006022 xfs_iext_get_extent(ifp, *current_ext + 1, &adj_irec);
6023 if (startoff + got->br_blockcount > adj_irec.br_startoff)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006024 return -EINVAL;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006025
Namjae Jeona904b1c2015-03-25 15:08:56 +11006026 /*
6027 * Unlike a left shift (which involves a hole punch),
6028 * a right shift does not modify extent neighbors
6029 * in any way. We should never find mergeable extents
6030 * in this scenario. Check anyways and warn if we
6031 * encounter two extents that could be one.
6032 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006033 if (xfs_bmse_can_merge(got, &adj_irec, offset_shift_fsb))
Namjae Jeona904b1c2015-03-25 15:08:56 +11006034 WARN_ON_ONCE(1);
6035 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006036
Brian Fostera979bdf2014-09-23 15:39:04 +10006037 /*
6038 * Increment the extent index for the next iteration, update the start
6039 * offset of the in-core extent and update the btree if applicable.
6040 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11006041update_current_ext:
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006042 *logflags |= XFS_ILOG_CORE;
6043
6044 new = *got;
6045 new.br_startoff = startoff;
6046
6047 if (cur) {
6048 error = xfs_bmbt_lookup_eq(cur, got->br_startoff,
6049 got->br_startblock, got->br_blockcount, &i);
6050 if (error)
6051 return error;
6052 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
6053
6054 error = xfs_bmbt_update(cur, new.br_startoff,
6055 new.br_startblock, new.br_blockcount,
6056 new.br_state);
6057 if (error)
6058 return error;
6059 } else {
6060 *logflags |= XFS_ILOG_DEXT;
6061 }
6062
6063 xfs_iext_update_extent(ifp, *current_ext, &new);
6064
Namjae Jeona904b1c2015-03-25 15:08:56 +11006065 if (direction == SHIFT_LEFT)
6066 (*current_ext)++;
6067 else
6068 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10006069
Darrick J. Wong9c194642016-08-03 12:16:05 +10006070 /* update reverse mapping */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006071 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
Darrick J. Wong9c194642016-08-03 12:16:05 +10006072 if (error)
6073 return error;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006074 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fostera979bdf2014-09-23 15:39:04 +10006075}
6076
6077/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11006078 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006079 *
Brian Foster2c845f52014-09-23 15:37:09 +10006080 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11006081 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10006082 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6083 * is the length by which each extent is shifted. If there is no hole to shift
6084 * the extents into, this will be considered invalid operation and we abort
6085 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006086 */
6087int
6088xfs_bmap_shift_extents(
6089 struct xfs_trans *tp,
6090 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006091 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006092 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10006093 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006094 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006095 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006096 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006097 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006098 int num_exts)
6099{
Brian Fosterca446d82014-09-02 12:12:53 +10006100 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006101 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006102 struct xfs_mount *mp = ip->i_mount;
6103 struct xfs_ifork *ifp;
6104 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10006105 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006106 xfs_extnum_t total_extents;
6107 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006108 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006109 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10006110 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006111
6112 if (unlikely(XFS_TEST_ERROR(
6113 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6114 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006115 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006116 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6117 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10006118 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006119 }
6120
6121 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10006122 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006123
Brian Foster2c845f52014-09-23 15:37:09 +10006124 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6125 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006126 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006127
6128 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006129 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6130 /* Read in all the extents */
6131 error = xfs_iread_extents(tp, ip, whichfork);
6132 if (error)
6133 return error;
6134 }
6135
Brian Fosterddb19e32014-09-23 15:38:09 +10006136 if (ifp->if_flags & XFS_IFBROOT) {
6137 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6138 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006139 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006140 cur->bc_private.b.flags = 0;
6141 }
6142
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006143 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006144 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006145 * are collapsing out, so we cannot use the count of real extents here.
6146 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006147 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006148 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006149 if (total_extents == 0) {
6150 *done = 1;
6151 goto del_cursor;
6152 }
6153
6154 /*
6155 * In case of first right shift, we need to initialize next_fsb
6156 */
6157 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006158 ASSERT(direction == SHIFT_RIGHT);
6159
6160 current_ext = total_extents - 1;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006161 xfs_iext_get_extent(ifp, current_ext, &got);
6162 if (stop_fsb > got.br_startoff) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006163 *done = 1;
6164 goto del_cursor;
6165 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006166 *next_fsb = got.br_startoff;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006167 } else {
6168 /*
6169 * Look up the extent index for the fsb where we start shifting. We can
6170 * henceforth iterate with current_ext as extent list changes are locked
6171 * out via ilock.
6172 *
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006173 * If next_fsb lies in a hole beyond which there are no extents we are
6174 * done.
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006175 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006176 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
6177 &got)) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006178 *done = 1;
6179 goto del_cursor;
6180 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006181 }
6182
6183 /* Lookup the extent index at which we have to stop */
6184 if (direction == SHIFT_RIGHT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006185 struct xfs_bmbt_irec s;
6186
6187 xfs_iext_lookup_extent(ip, ifp, stop_fsb, &stop_extent, &s);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006188 /* Make stop_extent exclusive of shift range */
6189 stop_extent--;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006190 if (current_ext <= stop_extent) {
6191 error = -EIO;
6192 goto del_cursor;
6193 }
6194 } else {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006195 stop_extent = total_extents;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006196 if (current_ext >= stop_extent) {
6197 error = -EIO;
6198 goto del_cursor;
6199 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006200 }
6201
6202 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006203 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006204 &current_ext, &got, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006205 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006206 if (error)
6207 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006208 /*
6209 * If there was an extent merge during the shift, the extent
6210 * count can change. Update the total and grade the next record.
6211 */
6212 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006213 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006214 stop_extent = total_extents;
6215 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006216
Namjae Jeona904b1c2015-03-25 15:08:56 +11006217 if (current_ext == stop_extent) {
6218 *done = 1;
6219 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006220 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006221 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006222 xfs_iext_get_extent(ifp, current_ext, &got);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006223 }
6224
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006225 if (!*done)
Brian Foster2c845f52014-09-23 15:37:09 +10006226 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006227
6228del_cursor:
6229 if (cur)
6230 xfs_btree_del_cursor(cur,
6231 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6232
Brian Fosterca446d82014-09-02 12:12:53 +10006233 if (logflags)
6234 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006235
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006236 return error;
6237}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006238
6239/*
6240 * Splits an extent into two extents at split_fsb block such that it is
6241 * the first block of the current_ext. @current_ext is a target extent
6242 * to be split. @split_fsb is a block where the extents is split.
6243 * If split_fsb lies in a hole or the first block of extents, just return 0.
6244 */
6245STATIC int
6246xfs_bmap_split_extent_at(
6247 struct xfs_trans *tp,
6248 struct xfs_inode *ip,
6249 xfs_fileoff_t split_fsb,
6250 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006251 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006252{
6253 int whichfork = XFS_DATA_FORK;
6254 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006255 struct xfs_bmbt_irec got;
6256 struct xfs_bmbt_irec new; /* split extent */
6257 struct xfs_mount *mp = ip->i_mount;
6258 struct xfs_ifork *ifp;
6259 xfs_fsblock_t gotblkcnt; /* new block count for got */
6260 xfs_extnum_t current_ext;
6261 int error = 0;
6262 int logflags = 0;
6263 int i = 0;
6264
6265 if (unlikely(XFS_TEST_ERROR(
6266 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6267 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006268 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006269 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6270 XFS_ERRLEVEL_LOW, mp);
6271 return -EFSCORRUPTED;
6272 }
6273
6274 if (XFS_FORCED_SHUTDOWN(mp))
6275 return -EIO;
6276
6277 ifp = XFS_IFORK_PTR(ip, whichfork);
6278 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6279 /* Read in all the extents */
6280 error = xfs_iread_extents(tp, ip, whichfork);
6281 if (error)
6282 return error;
6283 }
6284
6285 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006286 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006287 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006288 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
6289 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006290 return 0;
6291
6292 gotblkcnt = split_fsb - got.br_startoff;
6293 new.br_startoff = split_fsb;
6294 new.br_startblock = got.br_startblock + gotblkcnt;
6295 new.br_blockcount = got.br_blockcount - gotblkcnt;
6296 new.br_state = got.br_state;
6297
6298 if (ifp->if_flags & XFS_IFBROOT) {
6299 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6300 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006301 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006302 cur->bc_private.b.flags = 0;
6303 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6304 got.br_startblock,
6305 got.br_blockcount,
6306 &i);
6307 if (error)
6308 goto del_cursor;
6309 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6310 }
6311
Namjae Jeona904b1c2015-03-25 15:08:56 +11006312 got.br_blockcount = gotblkcnt;
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006313 xfs_iext_update_extent(ifp, current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006314
6315 logflags = XFS_ILOG_CORE;
6316 if (cur) {
6317 error = xfs_bmbt_update(cur, got.br_startoff,
6318 got.br_startblock,
6319 got.br_blockcount,
6320 got.br_state);
6321 if (error)
6322 goto del_cursor;
6323 } else
6324 logflags |= XFS_ILOG_DEXT;
6325
6326 /* Add new extent */
6327 current_ext++;
6328 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6329 XFS_IFORK_NEXT_SET(ip, whichfork,
6330 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6331
6332 if (cur) {
6333 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6334 new.br_startblock, new.br_blockcount,
6335 &i);
6336 if (error)
6337 goto del_cursor;
6338 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6339 cur->bc_rec.b.br_state = new.br_state;
6340
6341 error = xfs_btree_insert(cur, &i);
6342 if (error)
6343 goto del_cursor;
6344 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6345 }
6346
6347 /*
6348 * Convert to a btree if necessary.
6349 */
6350 if (xfs_bmap_needs_btree(ip, whichfork)) {
6351 int tmp_logflags; /* partial log flag return val */
6352
6353 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006354 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006355 &cur, 0, &tmp_logflags, whichfork);
6356 logflags |= tmp_logflags;
6357 }
6358
6359del_cursor:
6360 if (cur) {
6361 cur->bc_private.b.allocated = 0;
6362 xfs_btree_del_cursor(cur,
6363 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6364 }
6365
6366 if (logflags)
6367 xfs_trans_log_inode(tp, ip, logflags);
6368 return error;
6369}
6370
6371int
6372xfs_bmap_split_extent(
6373 struct xfs_inode *ip,
6374 xfs_fileoff_t split_fsb)
6375{
6376 struct xfs_mount *mp = ip->i_mount;
6377 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006378 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006379 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006380 int error;
6381
Christoph Hellwig253f4912016-04-06 09:19:55 +10006382 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6383 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6384 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006385 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006386
6387 xfs_ilock(ip, XFS_ILOCK_EXCL);
6388 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6389
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006390 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006391
6392 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006393 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006394 if (error)
6395 goto out;
6396
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006397 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006398 if (error)
6399 goto out;
6400
Christoph Hellwig70393312015-06-04 13:48:08 +10006401 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006402
6403out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006404 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006405 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006406 return error;
6407}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006408
6409/* Deferred mapping is only for real extents in the data fork. */
6410static bool
6411xfs_bmap_is_update_needed(
6412 struct xfs_bmbt_irec *bmap)
6413{
6414 return bmap->br_startblock != HOLESTARTBLOCK &&
6415 bmap->br_startblock != DELAYSTARTBLOCK;
6416}
6417
6418/* Record a bmap intent. */
6419static int
6420__xfs_bmap_add(
6421 struct xfs_mount *mp,
6422 struct xfs_defer_ops *dfops,
6423 enum xfs_bmap_intent_type type,
6424 struct xfs_inode *ip,
6425 int whichfork,
6426 struct xfs_bmbt_irec *bmap)
6427{
6428 int error;
6429 struct xfs_bmap_intent *bi;
6430
6431 trace_xfs_bmap_defer(mp,
6432 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6433 type,
6434 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6435 ip->i_ino, whichfork,
6436 bmap->br_startoff,
6437 bmap->br_blockcount,
6438 bmap->br_state);
6439
6440 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6441 INIT_LIST_HEAD(&bi->bi_list);
6442 bi->bi_type = type;
6443 bi->bi_owner = ip;
6444 bi->bi_whichfork = whichfork;
6445 bi->bi_bmap = *bmap;
6446
Christoph Hellwig882d8782017-08-28 10:21:03 -07006447 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006448 if (error) {
6449 kmem_free(bi);
6450 return error;
6451 }
6452
6453 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6454 return 0;
6455}
6456
6457/* Map an extent into a file. */
6458int
6459xfs_bmap_map_extent(
6460 struct xfs_mount *mp,
6461 struct xfs_defer_ops *dfops,
6462 struct xfs_inode *ip,
6463 struct xfs_bmbt_irec *PREV)
6464{
6465 if (!xfs_bmap_is_update_needed(PREV))
6466 return 0;
6467
6468 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6469 XFS_DATA_FORK, PREV);
6470}
6471
6472/* Unmap an extent out of a file. */
6473int
6474xfs_bmap_unmap_extent(
6475 struct xfs_mount *mp,
6476 struct xfs_defer_ops *dfops,
6477 struct xfs_inode *ip,
6478 struct xfs_bmbt_irec *PREV)
6479{
6480 if (!xfs_bmap_is_update_needed(PREV))
6481 return 0;
6482
6483 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6484 XFS_DATA_FORK, PREV);
6485}
6486
6487/*
6488 * Process one of the deferred bmap operations. We pass back the
6489 * btree cursor to maintain our lock on the bmapbt between calls.
6490 */
6491int
6492xfs_bmap_finish_one(
6493 struct xfs_trans *tp,
6494 struct xfs_defer_ops *dfops,
6495 struct xfs_inode *ip,
6496 enum xfs_bmap_intent_type type,
6497 int whichfork,
6498 xfs_fileoff_t startoff,
6499 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006500 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006501 xfs_exntst_t state)
6502{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006503 xfs_fsblock_t firstfsb;
6504 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006505
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006506 /*
6507 * firstfsb is tied to the transaction lifetime and is used to
6508 * ensure correct AG locking order and schedule work item
6509 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6510 * to only making one bmap call per transaction, so it should
6511 * be safe to have it as a local variable here.
6512 */
6513 firstfsb = NULLFSBLOCK;
6514
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006515 trace_xfs_bmap_deferred(tp->t_mountp,
6516 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6517 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006518 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006519
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006520 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006521 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006522
6523 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006524 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006525 return -EIO;
6526
6527 switch (type) {
6528 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006529 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006530 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006531 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006532 break;
6533 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006534 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6535 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006536 break;
6537 default:
6538 ASSERT(0);
6539 error = -EFSCORRUPTED;
6540 }
6541
6542 return error;
6543}