blob: 9702f4c112bf42c82745890c845a4d95be315172 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054kmem_zone_t *xfs_bmap_free_item_zone;
55
56/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110057 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
Dave Chinner9e5987a72013-02-25 12:31:26 +110061 * Compute and fill in the value of the maximum depth of a bmap btree
62 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 */
Dave Chinner9e5987a72013-02-25 12:31:26 +110064void
65xfs_bmap_compute_maxlevels(
66 xfs_mount_t *mp, /* file system mount structure */
67 int whichfork) /* data or attr fork */
68{
69 int level; /* btree level */
70 uint maxblocks; /* max blocks at this level */
71 uint maxleafents; /* max leaf entries possible */
72 int maxrootrecs; /* max records in root block */
73 int minleafrecs; /* min records in leaf block */
74 int minnoderecs; /* min records in node block */
75 int sz; /* root block size */
76
77 /*
78 * The maximum number of extents in a file, hence the maximum
79 * number of leaf entries, is controlled by the type of di_nextents
80 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
81 * (a signed 16-bit number, xfs_aextnum_t).
82 *
83 * Note that we can no longer assume that if we are in ATTR1 that
84 * the fork offset of all the inodes will be
85 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
86 * with ATTR2 and then mounted back with ATTR1, keeping the
87 * di_forkoff's fixed but probably at various positions. Therefore,
88 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
89 * of a minimum size available.
90 */
91 if (whichfork == XFS_DATA_FORK) {
92 maxleafents = MAXEXTNUM;
93 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
94 } else {
95 maxleafents = MAXAEXTNUM;
96 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
97 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100098 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +110099 minleafrecs = mp->m_bmap_dmnr[0];
100 minnoderecs = mp->m_bmap_dmnr[1];
101 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
102 for (level = 1; maxblocks > 1; level++) {
103 if (maxblocks <= maxrootrecs)
104 maxblocks = 1;
105 else
106 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
107 }
108 mp->m_bm_maxlevels[whichfork] = level;
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100111STATIC int /* error */
112xfs_bmbt_lookup_eq(
113 struct xfs_btree_cur *cur,
114 xfs_fileoff_t off,
115 xfs_fsblock_t bno,
116 xfs_filblks_t len,
117 int *stat) /* success/failure */
118{
119 cur->bc_rec.b.br_startoff = off;
120 cur->bc_rec.b.br_startblock = bno;
121 cur->bc_rec.b.br_blockcount = len;
122 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
123}
124
125STATIC int /* error */
126xfs_bmbt_lookup_ge(
127 struct xfs_btree_cur *cur,
128 xfs_fileoff_t off,
129 xfs_fsblock_t bno,
130 xfs_filblks_t len,
131 int *stat) /* success/failure */
132{
133 cur->bc_rec.b.br_startoff = off;
134 cur->bc_rec.b.br_startblock = bno;
135 cur->bc_rec.b.br_blockcount = len;
136 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
137}
138
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100139/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000140 * Check if the inode needs to be converted to btree format.
141 */
142static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
143{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700144 return whichfork != XFS_COW_FORK &&
145 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000146 XFS_IFORK_NEXTENTS(ip, whichfork) >
147 XFS_IFORK_MAXEXT(ip, whichfork);
148}
149
150/*
151 * Check if the inode should be converted to extent format.
152 */
153static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
154{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700155 return whichfork != XFS_COW_FORK &&
156 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000157 XFS_IFORK_NEXTENTS(ip, whichfork) <=
158 XFS_IFORK_MAXEXT(ip, whichfork);
159}
160
161/*
162 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100163 * by [off, bno, len, state].
164 * This either works (return 0) or gets an EFSCORRUPTED error.
165 */
166STATIC int
167xfs_bmbt_update(
168 struct xfs_btree_cur *cur,
169 xfs_fileoff_t off,
170 xfs_fsblock_t bno,
171 xfs_filblks_t len,
172 xfs_exntst_t state)
173{
174 union xfs_btree_rec rec;
175
176 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
177 return xfs_btree_update(cur, &rec);
178}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
Dave Chinner9e5987a72013-02-25 12:31:26 +1100181 * Compute the worst-case number of indirect blocks that will be used
182 * for ip's delayed extent of length "len".
183 */
184STATIC xfs_filblks_t
185xfs_bmap_worst_indlen(
186 xfs_inode_t *ip, /* incore inode pointer */
187 xfs_filblks_t len) /* delayed extent length */
188{
189 int level; /* btree level number */
190 int maxrecs; /* maximum record count at this level */
191 xfs_mount_t *mp; /* mount structure */
192 xfs_filblks_t rval; /* return value */
193
194 mp = ip->i_mount;
195 maxrecs = mp->m_bmap_dmxr[0];
196 for (level = 0, rval = 0;
197 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
198 level++) {
199 len += maxrecs - 1;
200 do_div(len, maxrecs);
201 rval += len;
202 if (len == 1)
203 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
204 level - 1;
205 if (level == 0)
206 maxrecs = mp->m_bmap_dmxr[1];
207 }
208 return rval;
209}
210
211/*
212 * Calculate the default attribute fork offset for newly created inodes.
213 */
214uint
215xfs_default_attroffset(
216 struct xfs_inode *ip)
217{
218 struct xfs_mount *mp = ip->i_mount;
219 uint offset;
220
221 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100222 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a72013-02-25 12:31:26 +1100223 XFS_BMDR_SPACE_CALC(MINABTPTRS);
224 } else {
225 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
226 }
227
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100228 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100229 return offset;
230}
231
232/*
233 * Helper routine to reset inode di_forkoff field when switching
234 * attribute fork from local to extent format - we reset it where
235 * possible to make space available for inline data fork extents.
236 */
237STATIC void
238xfs_bmap_forkoff_reset(
Dave Chinner9e5987a72013-02-25 12:31:26 +1100239 xfs_inode_t *ip,
240 int whichfork)
241{
242 if (whichfork == XFS_ATTR_FORK &&
243 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
244 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
245 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
246 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
247
248 if (dfl_forkoff > ip->i_d.di_forkoff)
249 ip->i_d.di_forkoff = dfl_forkoff;
250 }
251}
252
Dave Chinner9e5987a72013-02-25 12:31:26 +1100253#ifdef DEBUG
254STATIC struct xfs_buf *
255xfs_bmap_get_bp(
256 struct xfs_btree_cur *cur,
257 xfs_fsblock_t bno)
258{
259 struct xfs_log_item_desc *lidp;
260 int i;
261
262 if (!cur)
263 return NULL;
264
265 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
266 if (!cur->bc_bufs[i])
267 break;
268 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
269 return cur->bc_bufs[i];
270 }
271
272 /* Chase down all the log items to see if the bp is there */
273 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
274 struct xfs_buf_log_item *bip;
275 bip = (struct xfs_buf_log_item *)lidp->lid_item;
276 if (bip->bli_item.li_type == XFS_LI_BUF &&
277 XFS_BUF_ADDR(bip->bli_buf) == bno)
278 return bip->bli_buf;
279 }
280
281 return NULL;
282}
283
284STATIC void
285xfs_check_block(
286 struct xfs_btree_block *block,
287 xfs_mount_t *mp,
288 int root,
289 short sz)
290{
291 int i, j, dmxr;
292 __be64 *pp, *thispa; /* pointer to block address */
293 xfs_bmbt_key_t *prevp, *keyp;
294
295 ASSERT(be16_to_cpu(block->bb_level) > 0);
296
297 prevp = NULL;
298 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
299 dmxr = mp->m_bmap_dmxr[0];
300 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
301
302 if (prevp) {
303 ASSERT(be64_to_cpu(prevp->br_startoff) <
304 be64_to_cpu(keyp->br_startoff));
305 }
306 prevp = keyp;
307
308 /*
309 * Compare the block numbers to see if there are dups.
310 */
311 if (root)
312 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
313 else
314 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
315
316 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
317 if (root)
318 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
319 else
320 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
321 if (*thispa == *pp) {
322 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
323 __func__, j, i,
324 (unsigned long long)be64_to_cpu(*thispa));
325 panic("%s: ptrs are equal in node\n",
326 __func__);
327 }
328 }
329 }
330}
331
332/*
333 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100334 * btree leaves. THis becomes prohibitively expensive for large extent count
335 * files, so don't bother with inodes that have more than 10,000 extents in
336 * them. The btree record ordering checks will still be done, so for such large
337 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a72013-02-25 12:31:26 +1100338 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100339STATIC void
340xfs_bmap_check_leaf_extents(
341 xfs_btree_cur_t *cur, /* btree cursor or null */
342 xfs_inode_t *ip, /* incore inode pointer */
343 int whichfork) /* data or attr fork */
344{
345 struct xfs_btree_block *block; /* current btree block */
346 xfs_fsblock_t bno; /* block # of "block" */
347 xfs_buf_t *bp; /* buffer for "block" */
348 int error; /* error return value */
349 xfs_extnum_t i=0, j; /* index into the extents list */
350 xfs_ifork_t *ifp; /* fork structure */
351 int level; /* btree level, for checking */
352 xfs_mount_t *mp; /* file system mount structure */
353 __be64 *pp; /* pointer to block address */
354 xfs_bmbt_rec_t *ep; /* pointer to current extent */
355 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
356 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
357 int bp_release = 0;
358
359 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
360 return;
361 }
362
Dave Chinnere3543812016-01-08 11:28:49 +1100363 /* skip large extent count inodes */
364 if (ip->i_d.di_nextents > 10000)
365 return;
366
Dave Chinner9e5987a72013-02-25 12:31:26 +1100367 bno = NULLFSBLOCK;
368 mp = ip->i_mount;
369 ifp = XFS_IFORK_PTR(ip, whichfork);
370 block = ifp->if_broot;
371 /*
372 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
373 */
374 level = be16_to_cpu(block->bb_level);
375 ASSERT(level > 0);
376 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
377 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
378 bno = be64_to_cpu(*pp);
379
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000380 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100381 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
382 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
383
384 /*
385 * Go down the tree until leaf level is reached, following the first
386 * pointer (leftmost) at each level.
387 */
388 while (level-- > 0) {
389 /* See if buf is in cur first */
390 bp_release = 0;
391 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
392 if (!bp) {
393 bp_release = 1;
394 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
395 XFS_BMAP_BTREE_REF,
396 &xfs_bmbt_buf_ops);
397 if (error)
398 goto error_norelse;
399 }
400 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100401 if (level == 0)
402 break;
403
404 /*
405 * Check this block for basic sanity (increasing keys and
406 * no duplicate blocks).
407 */
408
409 xfs_check_block(block, mp, 0, 0);
410 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
411 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100412 XFS_WANT_CORRUPTED_GOTO(mp,
413 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100414 if (bp_release) {
415 bp_release = 0;
416 xfs_trans_brelse(NULL, bp);
417 }
418 }
419
420 /*
421 * Here with bp and block set to the leftmost leaf node in the tree.
422 */
423 i = 0;
424
425 /*
426 * Loop over all leaf nodes checking that all extents are in the right order.
427 */
428 for (;;) {
429 xfs_fsblock_t nextbno;
430 xfs_extnum_t num_recs;
431
432
433 num_recs = xfs_btree_get_numrecs(block);
434
435 /*
436 * Read-ahead the next leaf block, if any.
437 */
438
439 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
440
441 /*
442 * Check all the extents to make sure they are OK.
443 * If we had a previous block, the last entry should
444 * conform with the first entry in this one.
445 */
446
447 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
448 if (i) {
449 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
450 xfs_bmbt_disk_get_blockcount(&last) <=
451 xfs_bmbt_disk_get_startoff(ep));
452 }
453 for (j = 1; j < num_recs; j++) {
454 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
455 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
456 xfs_bmbt_disk_get_blockcount(ep) <=
457 xfs_bmbt_disk_get_startoff(nextp));
458 ep = nextp;
459 }
460
461 last = *ep;
462 i += num_recs;
463 if (bp_release) {
464 bp_release = 0;
465 xfs_trans_brelse(NULL, bp);
466 }
467 bno = nextbno;
468 /*
469 * If we've reached the end, stop.
470 */
471 if (bno == NULLFSBLOCK)
472 break;
473
474 bp_release = 0;
475 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
476 if (!bp) {
477 bp_release = 1;
478 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
479 XFS_BMAP_BTREE_REF,
480 &xfs_bmbt_buf_ops);
481 if (error)
482 goto error_norelse;
483 }
484 block = XFS_BUF_TO_BLOCK(bp);
485 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100486
Dave Chinner9e5987a72013-02-25 12:31:26 +1100487 return;
488
489error0:
490 xfs_warn(mp, "%s: at error0", __func__);
491 if (bp_release)
492 xfs_trans_brelse(NULL, bp);
493error_norelse:
494 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
495 __func__, i);
496 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
497 return;
498}
499
500/*
501 * Add bmap trace insert entries for all the contents of the extent records.
502 */
503void
504xfs_bmap_trace_exlist(
505 xfs_inode_t *ip, /* incore inode pointer */
506 xfs_extnum_t cnt, /* count of entries in the list */
507 int whichfork, /* data or attr fork */
508 unsigned long caller_ip)
509{
510 xfs_extnum_t idx; /* extent record index */
511 xfs_ifork_t *ifp; /* inode fork pointer */
512 int state = 0;
513
514 if (whichfork == XFS_ATTR_FORK)
515 state |= BMAP_ATTRFORK;
516
517 ifp = XFS_IFORK_PTR(ip, whichfork);
518 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
519 for (idx = 0; idx < cnt; idx++)
520 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
521}
522
523/*
524 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000525 * given the caller's original parameters. Specifically check the
526 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a72013-02-25 12:31:26 +1100527 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
528 */
529STATIC void
530xfs_bmap_validate_ret(
531 xfs_fileoff_t bno,
532 xfs_filblks_t len,
533 int flags,
534 xfs_bmbt_irec_t *mval,
535 int nmap,
536 int ret_nmap)
537{
538 int i; /* index to map values */
539
540 ASSERT(ret_nmap <= nmap);
541
542 for (i = 0; i < ret_nmap; i++) {
543 ASSERT(mval[i].br_blockcount > 0);
544 if (!(flags & XFS_BMAPI_ENTIRE)) {
545 ASSERT(mval[i].br_startoff >= bno);
546 ASSERT(mval[i].br_blockcount <= len);
547 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
548 bno + len);
549 } else {
550 ASSERT(mval[i].br_startoff < bno + len);
551 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
552 bno);
553 }
554 ASSERT(i == 0 ||
555 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
556 mval[i].br_startoff);
557 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
558 mval[i].br_startblock != HOLESTARTBLOCK);
559 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
560 mval[i].br_state == XFS_EXT_UNWRITTEN);
561 }
562}
563
564#else
565#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
566#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
567#endif /* DEBUG */
568
569/*
570 * bmap free list manipulation functions
571 */
572
573/*
574 * Add the extent to the list of extents to be free at transaction end.
575 * The list is maintained sorted (by block number).
576 */
577void
578xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000579 struct xfs_mount *mp,
580 struct xfs_defer_ops *dfops,
581 xfs_fsblock_t bno,
582 xfs_filblks_t len,
583 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a72013-02-25 12:31:26 +1100584{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000585 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100586#ifdef DEBUG
587 xfs_agnumber_t agno;
588 xfs_agblock_t agbno;
589
590 ASSERT(bno != NULLFSBLOCK);
591 ASSERT(len > 0);
592 ASSERT(len <= MAXEXTLEN);
593 ASSERT(!isnullstartblock(bno));
594 agno = XFS_FSB_TO_AGNO(mp, bno);
595 agbno = XFS_FSB_TO_AGBNO(mp, bno);
596 ASSERT(agno < mp->m_sb.sb_agcount);
597 ASSERT(agbno < mp->m_sb.sb_agblocks);
598 ASSERT(len < mp->m_sb.sb_agblocks);
599 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
600#endif
601 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000602
Dave Chinner9e5987a72013-02-25 12:31:26 +1100603 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000604 new->xefi_startblock = bno;
605 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000606 if (oinfo)
607 new->xefi_oinfo = *oinfo;
608 else
609 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000610 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
611 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000612 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100613}
614
615/*
616 * Inode fork format manipulation functions
617 */
618
619/*
620 * Transform a btree format file with only one leaf node, where the
621 * extents list will fit in the inode, into an extents format file.
622 * Since the file extents are already in-core, all we have to do is
623 * give up the space for the btree root and pitch the leaf block.
624 */
625STATIC int /* error */
626xfs_bmap_btree_to_extents(
627 xfs_trans_t *tp, /* transaction pointer */
628 xfs_inode_t *ip, /* incore inode pointer */
629 xfs_btree_cur_t *cur, /* btree cursor */
630 int *logflagsp, /* inode logging flags */
631 int whichfork) /* data or attr fork */
632{
633 /* REFERENCED */
634 struct xfs_btree_block *cblock;/* child btree block */
635 xfs_fsblock_t cbno; /* child block number */
636 xfs_buf_t *cbp; /* child block's buffer */
637 int error; /* error return value */
638 xfs_ifork_t *ifp; /* inode fork data */
639 xfs_mount_t *mp; /* mount point structure */
640 __be64 *pp; /* ptr to block address */
641 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000642 struct xfs_owner_info oinfo;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100643
644 mp = ip->i_mount;
645 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700646 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100647 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
648 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
649 rblock = ifp->if_broot;
650 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
651 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
652 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
653 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
654 cbno = be64_to_cpu(*pp);
655 *logflagsp = 0;
656#ifdef DEBUG
657 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
658 return error;
659#endif
660 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
661 &xfs_bmbt_buf_ops);
662 if (error)
663 return error;
664 cblock = XFS_BUF_TO_BLOCK(cbp);
665 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
666 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000667 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
668 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100669 ip->i_d.di_nblocks--;
670 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
671 xfs_trans_binval(tp, cbp);
672 if (cur->bc_bufs[0] == cbp)
673 cur->bc_bufs[0] = NULL;
674 xfs_iroot_realloc(ip, -1, whichfork);
675 ASSERT(ifp->if_broot == NULL);
676 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
677 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
678 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
679 return 0;
680}
681
682/*
683 * Convert an extents-format file into a btree-format file.
684 * The new file will have a root block (in the inode) and a single child block.
685 */
686STATIC int /* error */
687xfs_bmap_extents_to_btree(
688 xfs_trans_t *tp, /* transaction pointer */
689 xfs_inode_t *ip, /* incore inode pointer */
690 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000691 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100692 xfs_btree_cur_t **curp, /* cursor returned to caller */
693 int wasdel, /* converting a delayed alloc */
694 int *logflagsp, /* inode logging flags */
695 int whichfork) /* data or attr fork */
696{
697 struct xfs_btree_block *ablock; /* allocated (child) bt block */
698 xfs_buf_t *abp; /* buffer for ablock */
699 xfs_alloc_arg_t args; /* allocation arguments */
700 xfs_bmbt_rec_t *arp; /* child record pointer */
701 struct xfs_btree_block *block; /* btree root block */
702 xfs_btree_cur_t *cur; /* bmap btree cursor */
703 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
704 int error; /* error return value */
705 xfs_extnum_t i, cnt; /* extent record index */
706 xfs_ifork_t *ifp; /* inode fork pointer */
707 xfs_bmbt_key_t *kp; /* root block key pointer */
708 xfs_mount_t *mp; /* mount structure */
709 xfs_extnum_t nextents; /* number of file extents */
710 xfs_bmbt_ptr_t *pp; /* root block address pointer */
711
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500712 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700713 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100714 ifp = XFS_IFORK_PTR(ip, whichfork);
715 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
716
717 /*
718 * Make space in the inode incore.
719 */
720 xfs_iroot_realloc(ip, 1, whichfork);
721 ifp->if_flags |= XFS_IFBROOT;
722
723 /*
724 * Fill in the root.
725 */
726 block = ifp->if_broot;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500727 if (xfs_sb_version_hascrc(&mp->m_sb))
728 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
729 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
730 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
731 else
732 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
733 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
734 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100735
736 /*
737 * Need a cursor. Can't allocate until bb_level is filled in.
738 */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100739 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
740 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000741 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100742 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
743 /*
744 * Convert to a btree with two levels, one record in root.
745 */
746 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
747 memset(&args, 0, sizeof(args));
748 args.tp = tp;
749 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000750 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100751 args.firstblock = *firstblock;
752 if (*firstblock == NULLFSBLOCK) {
753 args.type = XFS_ALLOCTYPE_START_BNO;
754 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000755 } else if (dfops->dop_low) {
Dave Chinner9e5987a72013-02-25 12:31:26 +1100756 args.type = XFS_ALLOCTYPE_START_BNO;
757 args.fsbno = *firstblock;
758 } else {
759 args.type = XFS_ALLOCTYPE_NEAR_BNO;
760 args.fsbno = *firstblock;
761 }
762 args.minlen = args.maxlen = args.prod = 1;
763 args.wasdel = wasdel;
764 *logflagsp = 0;
765 if ((error = xfs_alloc_vextent(&args))) {
766 xfs_iroot_realloc(ip, -1, whichfork);
767 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
768 return error;
769 }
770 /*
771 * Allocation can't fail, the space was reserved.
772 */
773 ASSERT(args.fsbno != NULLFSBLOCK);
774 ASSERT(*firstblock == NULLFSBLOCK ||
775 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000776 (dfops->dop_low &&
Dave Chinner9e5987a72013-02-25 12:31:26 +1100777 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
778 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
779 cur->bc_private.b.allocated++;
780 ip->i_d.di_nblocks++;
781 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
782 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
783 /*
784 * Fill in the child block.
785 */
786 abp->b_ops = &xfs_bmbt_buf_ops;
787 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500788 if (xfs_sb_version_hascrc(&mp->m_sb))
789 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
790 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
791 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
792 else
793 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
794 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
795 XFS_BTREE_LONG_PTRS);
796
Dave Chinner9e5987a72013-02-25 12:31:26 +1100797 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
798 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
799 for (cnt = i = 0; i < nextents; i++) {
800 ep = xfs_iext_get_ext(ifp, i);
801 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
802 arp->l0 = cpu_to_be64(ep->l0);
803 arp->l1 = cpu_to_be64(ep->l1);
804 arp++; cnt++;
805 }
806 }
807 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
808 xfs_btree_set_numrecs(ablock, cnt);
809
810 /*
811 * Fill in the root key and pointer.
812 */
813 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
814 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
815 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
816 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
817 be16_to_cpu(block->bb_level)));
818 *pp = cpu_to_be64(args.fsbno);
819
820 /*
821 * Do all this logging at the end so that
822 * the root is at the right level.
823 */
824 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
825 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
826 ASSERT(*curp == NULL);
827 *curp = cur;
828 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
829 return 0;
830}
831
832/*
833 * Convert a local file to an extents file.
834 * This code is out of bounds for data forks of regular files,
835 * since the file data needs to get logged so things will stay consistent.
836 * (The bmap-level manipulations are ok, though).
837 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000838void
839xfs_bmap_local_to_extents_empty(
840 struct xfs_inode *ip,
841 int whichfork)
842{
843 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
844
Darrick J. Wong60b49842016-10-03 09:11:34 -0700845 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000846 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
847 ASSERT(ifp->if_bytes == 0);
848 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
849
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000850 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000851 ifp->if_flags &= ~XFS_IFINLINE;
852 ifp->if_flags |= XFS_IFEXTENTS;
853 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
854}
855
856
Dave Chinner9e5987a72013-02-25 12:31:26 +1100857STATIC int /* error */
858xfs_bmap_local_to_extents(
859 xfs_trans_t *tp, /* transaction pointer */
860 xfs_inode_t *ip, /* incore inode pointer */
861 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
862 xfs_extlen_t total, /* total blocks needed by transaction */
863 int *logflagsp, /* inode logging flags */
864 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500865 void (*init_fn)(struct xfs_trans *tp,
866 struct xfs_buf *bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100867 struct xfs_inode *ip,
868 struct xfs_ifork *ifp))
869{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000870 int error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100871 int flags; /* logging flags returned */
872 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000873 xfs_alloc_arg_t args; /* allocation arguments */
874 xfs_buf_t *bp; /* buffer for extent block */
875 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100876
877 /*
878 * We don't want to deal with the case of keeping inode data inline yet.
879 * So sending the data fork of a regular inode is invalid.
880 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100881 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100882 ifp = XFS_IFORK_PTR(ip, whichfork);
883 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000884
885 if (!ifp->if_bytes) {
886 xfs_bmap_local_to_extents_empty(ip, whichfork);
887 flags = XFS_ILOG_CORE;
888 goto done;
889 }
890
Dave Chinner9e5987a72013-02-25 12:31:26 +1100891 flags = 0;
892 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000893 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
894 XFS_IFINLINE);
895 memset(&args, 0, sizeof(args));
896 args.tp = tp;
897 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000898 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000899 args.firstblock = *firstblock;
900 /*
901 * Allocate a block. We know we need only one, since the
902 * file currently fits in an inode.
903 */
904 if (*firstblock == NULLFSBLOCK) {
905 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
906 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100907 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000908 args.fsbno = *firstblock;
909 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100910 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000911 args.total = total;
912 args.minlen = args.maxlen = args.prod = 1;
913 error = xfs_alloc_vextent(&args);
914 if (error)
915 goto done;
916
917 /* Can't fail, the space was reserved. */
918 ASSERT(args.fsbno != NULLFSBLOCK);
919 ASSERT(args.len == 1);
920 *firstblock = args.fsbno;
921 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
922
Dave Chinnerfe22d552015-01-22 09:30:06 +1100923 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100924 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100925 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100926 * The callout is responsible for logging because the remote format
927 * might differ from the local format and thus we don't know how much to
928 * log here. Note that init_fn must also set the buffer log item type
929 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100930 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000931 init_fn(tp, bp, ip, ifp);
932
Brian Fosterb7cdc662015-10-12 15:40:24 +1100933 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000934 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
935 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100936 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000937
938 xfs_iext_add(ifp, 0, 1);
939 ep = xfs_iext_get_ext(ifp, 0);
940 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
941 trace_xfs_bmap_post_update(ip, 0,
942 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
943 _THIS_IP_);
944 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
945 ip->i_d.di_nblocks = 1;
946 xfs_trans_mod_dquot_byino(tp, ip,
947 XFS_TRANS_DQ_BCOUNT, 1L);
948 flags |= xfs_ilog_fext(whichfork);
949
Dave Chinner9e5987a72013-02-25 12:31:26 +1100950done:
951 *logflagsp = flags;
952 return error;
953}
954
955/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 * Called from xfs_bmap_add_attrfork to handle btree format files.
957 */
958STATIC int /* error */
959xfs_bmap_add_attrfork_btree(
960 xfs_trans_t *tp, /* transaction pointer */
961 xfs_inode_t *ip, /* incore inode pointer */
962 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000963 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 int *flags) /* inode logging flags */
965{
966 xfs_btree_cur_t *cur; /* btree cursor */
967 int error; /* error return value */
968 xfs_mount_t *mp; /* file system mount struct */
969 int stat; /* newroot status */
970
971 mp = ip->i_mount;
972 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
973 *flags |= XFS_ILOG_DBROOT;
974 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100975 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000976 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 cur->bc_private.b.firstblock = *firstblock;
978 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
979 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000980 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100981 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100982 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 goto error0;
984 if (stat == 0) {
985 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000986 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 }
988 *firstblock = cur->bc_private.b.firstblock;
989 cur->bc_private.b.allocated = 0;
990 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
991 }
992 return 0;
993error0:
994 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
995 return error;
996}
997
998/*
999 * Called from xfs_bmap_add_attrfork to handle extents format files.
1000 */
1001STATIC int /* error */
1002xfs_bmap_add_attrfork_extents(
1003 xfs_trans_t *tp, /* transaction pointer */
1004 xfs_inode_t *ip, /* incore inode pointer */
1005 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001006 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 int *flags) /* inode logging flags */
1008{
1009 xfs_btree_cur_t *cur; /* bmap btree cursor */
1010 int error; /* error return value */
1011
1012 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1013 return 0;
1014 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001015 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 flags, XFS_DATA_FORK);
1017 if (cur) {
1018 cur->bc_private.b.allocated = 0;
1019 xfs_btree_del_cursor(cur,
1020 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1021 }
1022 return error;
1023}
1024
1025/*
Dave Chinner1e823792013-02-11 15:58:13 +11001026 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1027 * different data fork content type needs a different callout to do the
1028 * conversion. Some are basic and only require special block initialisation
1029 * callouts for the data formating, others (directories) are so specialised they
1030 * handle everything themselves.
1031 *
1032 * XXX (dgc): investigate whether directory conversion can use the generic
1033 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001034 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 */
1036STATIC int /* error */
1037xfs_bmap_add_attrfork_local(
1038 xfs_trans_t *tp, /* transaction pointer */
1039 xfs_inode_t *ip, /* incore inode pointer */
1040 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001041 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 int *flags) /* inode logging flags */
1043{
1044 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
1046 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1047 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001048
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001049 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001051 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 dargs.dp = ip;
1053 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001054 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001055 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 dargs.whichfork = XFS_DATA_FORK;
1057 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001058 return xfs_dir2_sf_to_block(&dargs);
1059 }
1060
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001061 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001062 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1063 flags, XFS_DATA_FORK,
1064 xfs_symlink_local_to_remote);
1065
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001066 /* should only be called for types that support local format data */
1067 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001068 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
1071/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001072 * Convert inode from non-attributed to attributed.
1073 * Must not be in a transaction, ip must not be locked.
1074 */
1075int /* error code */
1076xfs_bmap_add_attrfork(
1077 xfs_inode_t *ip, /* incore inode pointer */
1078 int size, /* space new attribute needs */
1079 int rsvd) /* xact may use reserved blks */
1080{
1081 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001082 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001083 xfs_mount_t *mp; /* mount structure */
1084 xfs_trans_t *tp; /* transaction pointer */
1085 int blks; /* space reservation */
1086 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001087 int logflags; /* logging flags */
1088 int error; /* error return value */
1089
1090 ASSERT(XFS_IFORK_Q(ip) == 0);
1091
1092 mp = ip->i_mount;
1093 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001094
Dave Chinner9e5987a72013-02-25 12:31:26 +11001095 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001096
1097 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1098 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1099 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001100 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001101
Dave Chinner9e5987a72013-02-25 12:31:26 +11001102 xfs_ilock(ip, XFS_ILOCK_EXCL);
1103 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1104 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1105 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001106 if (error)
1107 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001108 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001109 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001110 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1111 /*
1112 * For inodes coming from pre-6.2 filesystems.
1113 */
1114 ASSERT(ip->i_d.di_aformat == 0);
1115 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1116 }
1117 ASSERT(ip->i_d.di_anextents == 0);
1118
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001119 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001120 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1121
1122 switch (ip->i_d.di_format) {
1123 case XFS_DINODE_FMT_DEV:
1124 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1125 break;
1126 case XFS_DINODE_FMT_UUID:
1127 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1128 break;
1129 case XFS_DINODE_FMT_LOCAL:
1130 case XFS_DINODE_FMT_EXTENTS:
1131 case XFS_DINODE_FMT_BTREE:
1132 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1133 if (!ip->i_d.di_forkoff)
1134 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1135 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1136 version = 2;
1137 break;
1138 default:
1139 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001140 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001141 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001142 }
1143
1144 ASSERT(ip->i_afp == NULL);
1145 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1146 ip->i_afp->if_flags = XFS_IFEXTENTS;
1147 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001148 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001149 switch (ip->i_d.di_format) {
1150 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001151 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001152 &logflags);
1153 break;
1154 case XFS_DINODE_FMT_EXTENTS:
1155 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001156 &dfops, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001157 break;
1158 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001159 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001160 &logflags);
1161 break;
1162 default:
1163 error = 0;
1164 break;
1165 }
1166 if (logflags)
1167 xfs_trans_log_inode(tp, ip, logflags);
1168 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001169 goto bmap_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001170 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1171 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001172 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001173
1174 spin_lock(&mp->m_sb_lock);
1175 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1176 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001177 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001178 }
1179 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1180 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001181 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001182 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001183 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001184 if (log_sb)
1185 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001186 }
1187
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001188 error = xfs_defer_finish(&tp, &dfops, NULL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001189 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001190 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001191 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001192 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001193 return error;
1194
1195bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001196 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001197trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001198 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001199 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001200 return error;
1201}
1202
1203/*
1204 * Internal and external extent tree search functions.
1205 */
1206
1207/*
1208 * Read in the extents to if_extents.
1209 * All inode fields are set up by caller, we just traverse the btree
1210 * and copy the records in. If the file system cannot contain unwritten
1211 * extents, the records are checked for no "state" flags.
1212 */
1213int /* error */
1214xfs_bmap_read_extents(
1215 xfs_trans_t *tp, /* transaction pointer */
1216 xfs_inode_t *ip, /* incore inode */
1217 int whichfork) /* data or attr fork */
1218{
1219 struct xfs_btree_block *block; /* current btree block */
1220 xfs_fsblock_t bno; /* block # of "block" */
1221 xfs_buf_t *bp; /* buffer for "block" */
1222 int error; /* error return value */
1223 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1224 xfs_extnum_t i, j; /* index into the extents list */
1225 xfs_ifork_t *ifp; /* fork structure */
1226 int level; /* btree level, for checking */
1227 xfs_mount_t *mp; /* file system mount structure */
1228 __be64 *pp; /* pointer to block address */
1229 /* REFERENCED */
1230 xfs_extnum_t room; /* number of entries there's room for */
1231
1232 bno = NULLFSBLOCK;
1233 mp = ip->i_mount;
1234 ifp = XFS_IFORK_PTR(ip, whichfork);
1235 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1236 XFS_EXTFMT_INODE(ip);
1237 block = ifp->if_broot;
1238 /*
1239 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1240 */
1241 level = be16_to_cpu(block->bb_level);
1242 ASSERT(level > 0);
1243 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1244 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001245 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001246 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1247 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1248 /*
1249 * Go down the tree until leaf level is reached, following the first
1250 * pointer (leftmost) at each level.
1251 */
1252 while (level-- > 0) {
1253 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1254 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1255 if (error)
1256 return error;
1257 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001258 if (level == 0)
1259 break;
1260 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1261 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001262 XFS_WANT_CORRUPTED_GOTO(mp,
1263 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001264 xfs_trans_brelse(tp, bp);
1265 }
1266 /*
1267 * Here with bp and block set to the leftmost leaf node in the tree.
1268 */
1269 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1270 i = 0;
1271 /*
1272 * Loop over all leaf nodes. Copy information to the extent records.
1273 */
1274 for (;;) {
1275 xfs_bmbt_rec_t *frp;
1276 xfs_fsblock_t nextbno;
1277 xfs_extnum_t num_recs;
1278 xfs_extnum_t start;
1279
1280 num_recs = xfs_btree_get_numrecs(block);
1281 if (unlikely(i + num_recs > room)) {
1282 ASSERT(i + num_recs <= room);
1283 xfs_warn(ip->i_mount,
1284 "corrupt dinode %Lu, (btree extents).",
1285 (unsigned long long) ip->i_ino);
1286 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1287 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1288 goto error0;
1289 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001290 /*
1291 * Read-ahead the next leaf block, if any.
1292 */
1293 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1294 if (nextbno != NULLFSBLOCK)
1295 xfs_btree_reada_bufl(mp, nextbno, 1,
1296 &xfs_bmbt_buf_ops);
1297 /*
1298 * Copy records into the extent records.
1299 */
1300 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1301 start = i;
1302 for (j = 0; j < num_recs; j++, i++, frp++) {
1303 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1304 trp->l0 = be64_to_cpu(frp->l0);
1305 trp->l1 = be64_to_cpu(frp->l1);
1306 }
1307 if (exntf == XFS_EXTFMT_NOSTATE) {
1308 /*
1309 * Check all attribute bmap btree records and
1310 * any "older" data bmap btree records for a
1311 * set bit in the "extent flag" position.
1312 */
1313 if (unlikely(xfs_check_nostate_extents(ifp,
1314 start, num_recs))) {
1315 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1316 XFS_ERRLEVEL_LOW,
1317 ip->i_mount);
1318 goto error0;
1319 }
1320 }
1321 xfs_trans_brelse(tp, bp);
1322 bno = nextbno;
1323 /*
1324 * If we've reached the end, stop.
1325 */
1326 if (bno == NULLFSBLOCK)
1327 break;
1328 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1329 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1330 if (error)
1331 return error;
1332 block = XFS_BUF_TO_BLOCK(bp);
1333 }
1334 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1335 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1336 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1337 return 0;
1338error0:
1339 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001340 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001341}
1342
1343
1344/*
1345 * Search the extent records for the entry containing block bno.
1346 * If bno lies in a hole, point to the next entry. If bno lies
1347 * past eof, *eofp will be set, and *prevp will contain the last
1348 * entry (null if none). Else, *lastxp will be set to the index
1349 * of the found entry; *gotp will contain the entry.
1350 */
1351STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1352xfs_bmap_search_multi_extents(
1353 xfs_ifork_t *ifp, /* inode fork pointer */
1354 xfs_fileoff_t bno, /* block number searched for */
1355 int *eofp, /* out: end of file found */
1356 xfs_extnum_t *lastxp, /* out: last extent index */
1357 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1358 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1359{
1360 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1361 xfs_extnum_t lastx; /* last extent index */
1362
1363 /*
1364 * Initialize the extent entry structure to catch access to
1365 * uninitialized br_startblock field.
1366 */
1367 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1368 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1369 gotp->br_state = XFS_EXT_INVALID;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001370 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001371 prevp->br_startoff = NULLFILEOFF;
1372
1373 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1374 if (lastx > 0) {
1375 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1376 }
1377 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1378 xfs_bmbt_get_all(ep, gotp);
1379 *eofp = 0;
1380 } else {
1381 if (lastx > 0) {
1382 *gotp = *prevp;
1383 }
1384 *eofp = 1;
1385 ep = NULL;
1386 }
1387 *lastxp = lastx;
1388 return ep;
1389}
1390
1391/*
1392 * Search the extents list for the inode, for the extent containing bno.
1393 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1394 * *eofp will be set, and *prevp will contain the last entry (null if none).
1395 * Else, *lastxp will be set to the index of the found
1396 * entry; *gotp will contain the entry.
1397 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10001398xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001399xfs_bmap_search_extents(
1400 xfs_inode_t *ip, /* incore inode pointer */
1401 xfs_fileoff_t bno, /* block number searched for */
1402 int fork, /* data or attr fork */
1403 int *eofp, /* out: end of file found */
1404 xfs_extnum_t *lastxp, /* out: last extent index */
1405 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1406 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1407{
1408 xfs_ifork_t *ifp; /* inode fork pointer */
1409 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1410
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001411 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001412 ifp = XFS_IFORK_PTR(ip, fork);
1413
1414 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1415
1416 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1417 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1418 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1419 "Access to block zero in inode %llu "
1420 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -05001421 "blkcnt: %llx extent-state: %x lastx: %x",
Dave Chinner9e5987a72013-02-25 12:31:26 +11001422 (unsigned long long)ip->i_ino,
1423 (unsigned long long)gotp->br_startblock,
1424 (unsigned long long)gotp->br_startoff,
1425 (unsigned long long)gotp->br_blockcount,
1426 gotp->br_state, *lastxp);
1427 *lastxp = NULLEXTNUM;
1428 *eofp = 1;
1429 return NULL;
1430 }
1431 return ep;
1432}
1433
1434/*
1435 * Returns the file-relative block number of the first unused block(s)
1436 * in the file with at least "len" logically contiguous blocks free.
1437 * This is the lowest-address hole if the file has holes, else the first block
1438 * past the end of file.
1439 * Return 0 if the file is currently local (in-inode).
1440 */
1441int /* error */
1442xfs_bmap_first_unused(
1443 xfs_trans_t *tp, /* transaction pointer */
1444 xfs_inode_t *ip, /* incore inode */
1445 xfs_extlen_t len, /* size of hole to find */
1446 xfs_fileoff_t *first_unused, /* unused block */
1447 int whichfork) /* data or attr fork */
1448{
1449 int error; /* error return value */
1450 int idx; /* extent record index */
1451 xfs_ifork_t *ifp; /* inode fork pointer */
1452 xfs_fileoff_t lastaddr; /* last block number seen */
1453 xfs_fileoff_t lowest; /* lowest useful block */
1454 xfs_fileoff_t max; /* starting useful block */
1455 xfs_fileoff_t off; /* offset for this block */
1456 xfs_extnum_t nextents; /* number of extent entries */
1457
1458 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1459 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1460 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1461 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1462 *first_unused = 0;
1463 return 0;
1464 }
1465 ifp = XFS_IFORK_PTR(ip, whichfork);
1466 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1467 (error = xfs_iread_extents(tp, ip, whichfork)))
1468 return error;
1469 lowest = *first_unused;
1470 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1471 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1472 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1473 off = xfs_bmbt_get_startoff(ep);
1474 /*
1475 * See if the hole before this extent will work.
1476 */
1477 if (off >= lowest + len && off - max >= len) {
1478 *first_unused = max;
1479 return 0;
1480 }
1481 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1482 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1483 }
1484 *first_unused = max;
1485 return 0;
1486}
1487
1488/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001489 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001490 * last_block (input value) in the file.
1491 * This is not based on i_size, it is based on the extent records.
1492 * Returns 0 for local files, as they do not have extent records.
1493 */
1494int /* error */
1495xfs_bmap_last_before(
1496 xfs_trans_t *tp, /* transaction pointer */
1497 xfs_inode_t *ip, /* incore inode */
1498 xfs_fileoff_t *last_block, /* last block */
1499 int whichfork) /* data or attr fork */
1500{
1501 xfs_fileoff_t bno; /* input file offset */
1502 int eof; /* hit end of file */
1503 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1504 int error; /* error return value */
1505 xfs_bmbt_irec_t got; /* current extent value */
1506 xfs_ifork_t *ifp; /* inode fork pointer */
1507 xfs_extnum_t lastx; /* last extent used */
1508 xfs_bmbt_irec_t prev; /* previous extent value */
1509
1510 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1511 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1512 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +10001513 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001514 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1515 *last_block = 0;
1516 return 0;
1517 }
1518 ifp = XFS_IFORK_PTR(ip, whichfork);
1519 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1520 (error = xfs_iread_extents(tp, ip, whichfork)))
1521 return error;
1522 bno = *last_block - 1;
1523 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1524 &prev);
1525 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1526 if (prev.br_startoff == NULLFILEOFF)
1527 *last_block = 0;
1528 else
1529 *last_block = prev.br_startoff + prev.br_blockcount;
1530 }
1531 /*
1532 * Otherwise *last_block is already the right answer.
1533 */
1534 return 0;
1535}
1536
Dave Chinner68988112013-08-12 20:49:42 +10001537int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001538xfs_bmap_last_extent(
1539 struct xfs_trans *tp,
1540 struct xfs_inode *ip,
1541 int whichfork,
1542 struct xfs_bmbt_irec *rec,
1543 int *is_empty)
1544{
1545 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1546 int error;
1547 int nextents;
1548
1549 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1550 error = xfs_iread_extents(tp, ip, whichfork);
1551 if (error)
1552 return error;
1553 }
1554
1555 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1556 if (nextents == 0) {
1557 *is_empty = 1;
1558 return 0;
1559 }
1560
1561 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1562 *is_empty = 0;
1563 return 0;
1564}
1565
1566/*
1567 * Check the last inode extent to determine whether this allocation will result
1568 * in blocks being allocated at the end of the file. When we allocate new data
1569 * blocks at the end of the file which do not start at the previous data block,
1570 * we will try to align the new blocks at stripe unit boundaries.
1571 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001572 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001573 * at, or past the EOF.
1574 */
1575STATIC int
1576xfs_bmap_isaeof(
1577 struct xfs_bmalloca *bma,
1578 int whichfork)
1579{
1580 struct xfs_bmbt_irec rec;
1581 int is_empty;
1582 int error;
1583
1584 bma->aeof = 0;
1585 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1586 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001587 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001588 return error;
1589
Dave Chinner6e708bc2013-11-22 10:41:16 +11001590 if (is_empty) {
1591 bma->aeof = 1;
1592 return 0;
1593 }
1594
Dave Chinner9e5987a72013-02-25 12:31:26 +11001595 /*
1596 * Check if we are allocation or past the last extent, or at least into
1597 * the last delayed allocated extent.
1598 */
1599 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1600 (bma->offset >= rec.br_startoff &&
1601 isnullstartblock(rec.br_startblock));
1602 return 0;
1603}
1604
1605/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001606 * Returns the file-relative block number of the first block past eof in
1607 * the file. This is not based on i_size, it is based on the extent records.
1608 * Returns 0 for local files, as they do not have extent records.
1609 */
1610int
1611xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001612 struct xfs_inode *ip,
1613 xfs_fileoff_t *last_block,
1614 int whichfork)
1615{
1616 struct xfs_bmbt_irec rec;
1617 int is_empty;
1618 int error;
1619
1620 *last_block = 0;
1621
1622 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1623 return 0;
1624
1625 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1626 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001627 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001628
1629 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1630 if (error || is_empty)
1631 return error;
1632
1633 *last_block = rec.br_startoff + rec.br_blockcount;
1634 return 0;
1635}
1636
1637/*
1638 * Returns whether the selected fork of the inode has exactly one
1639 * block or not. For the data fork we check this matches di_size,
1640 * implying the file's range is 0..bsize-1.
1641 */
1642int /* 1=>1 block, 0=>otherwise */
1643xfs_bmap_one_block(
1644 xfs_inode_t *ip, /* incore inode */
1645 int whichfork) /* data or attr fork */
1646{
1647 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1648 xfs_ifork_t *ifp; /* inode fork pointer */
1649 int rval; /* return value */
1650 xfs_bmbt_irec_t s; /* internal version of extent */
1651
1652#ifndef DEBUG
1653 if (whichfork == XFS_DATA_FORK)
1654 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1655#endif /* !DEBUG */
1656 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1657 return 0;
1658 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1659 return 0;
1660 ifp = XFS_IFORK_PTR(ip, whichfork);
1661 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1662 ep = xfs_iext_get_ext(ifp, 0);
1663 xfs_bmbt_get_all(ep, &s);
1664 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1665 if (rval && whichfork == XFS_DATA_FORK)
1666 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1667 return rval;
1668}
1669
1670/*
1671 * Extent tree manipulation functions used during allocation.
1672 */
1673
1674/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001675 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 */
1677STATIC int /* error */
1678xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001679 struct xfs_bmalloca *bma,
1680 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001682 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001684 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001687 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 xfs_fileoff_t new_endoff; /* end offset of new entry */
1689 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1690 /* left is 0, right is 1, prev is 2 */
1691 int rval=0; /* return value (logging flags) */
1692 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001693 xfs_filblks_t da_new; /* new count del alloc blocks used */
1694 xfs_filblks_t da_old; /* old count del alloc blocks used */
1695 xfs_filblks_t temp=0; /* value for da_new calculations */
1696 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001698 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001699 xfs_extnum_t *nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001701 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001702 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001703 ASSERT(whichfork != XFS_ATTR_FORK);
1704 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1705 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001706
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001707 ASSERT(bma->idx >= 0);
1708 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001709 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001710 ASSERT(!bma->cur ||
1711 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001712
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001713 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715#define LEFT r[0]
1716#define RIGHT r[1]
1717#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Darrick J. Wong60b49842016-10-03 09:11:34 -07001719 if (whichfork == XFS_COW_FORK)
1720 state |= BMAP_COWFORK;
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 /*
1723 * Set up a bunch of variables to make the tests simpler.
1724 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001725 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 xfs_bmbt_get_all(ep, &PREV);
1727 new_endoff = new->br_startoff + new->br_blockcount;
1728 ASSERT(PREV.br_startoff <= new->br_startoff);
1729 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001730
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001731 da_old = startblockval(PREV.br_startblock);
1732 da_new = 0;
1733
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /*
1735 * Set flags determining what part of the previous delayed allocation
1736 * extent is being replaced by a real allocation.
1737 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001738 if (PREV.br_startoff == new->br_startoff)
1739 state |= BMAP_LEFT_FILLING;
1740 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1741 state |= BMAP_RIGHT_FILLING;
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 /*
1744 * Check and set flags if this segment has a left neighbor.
1745 * Don't set contiguous if the combined extent would be too large.
1746 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001747 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001748 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001749 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001750
1751 if (isnullstartblock(LEFT.br_startblock))
1752 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001754
1755 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1756 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1757 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1758 LEFT.br_state == new->br_state &&
1759 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1760 state |= BMAP_LEFT_CONTIG;
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 /*
1763 * Check and set flags if this segment has a right neighbor.
1764 * Don't set contiguous if the combined extent would be too large.
1765 * Also check for all-three-contiguous being too large.
1766 */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001767 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001768 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001769 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001770
1771 if (isnullstartblock(RIGHT.br_startblock))
1772 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001774
1775 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1776 new_endoff == RIGHT.br_startoff &&
1777 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1778 new->br_state == RIGHT.br_state &&
1779 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1780 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1781 BMAP_RIGHT_FILLING)) !=
1782 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1783 BMAP_RIGHT_FILLING) ||
1784 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1785 <= MAXEXTLEN))
1786 state |= BMAP_RIGHT_CONTIG;
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 error = 0;
1789 /*
1790 * Switch out based on the FILLING and CONTIG state bits.
1791 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001792 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1793 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1794 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1795 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 /*
1797 * Filling in all of a previously delayed allocation extent.
1798 * The left and right neighbors are both contiguous with new.
1799 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001800 bma->idx--;
1801 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1802 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 LEFT.br_blockcount + PREV.br_blockcount +
1804 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001805 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001806
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001807 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001808 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001809 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1811 else {
1812 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001813 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001815 RIGHT.br_blockcount, &i);
1816 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001818 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 error = xfs_btree_delete(bma->cur, &i);
1820 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001822 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001823 error = xfs_btree_decrement(bma->cur, 0, &i);
1824 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001826 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001827 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 LEFT.br_startblock,
1829 LEFT.br_blockcount +
1830 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001831 RIGHT.br_blockcount, LEFT.br_state);
1832 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 goto done;
1834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 break;
1836
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001837 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 /*
1839 * Filling in all of a previously delayed allocation extent.
1840 * The left neighbor is contiguous, the right is not.
1841 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001842 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001843
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001844 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1845 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001846 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001848
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001849 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1850 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 rval = XFS_ILOG_DEXT;
1852 else {
1853 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001854 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001856 &i);
1857 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001859 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001860 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 LEFT.br_startblock,
1862 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001863 PREV.br_blockcount, LEFT.br_state);
1864 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 goto done;
1866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 break;
1868
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001869 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /*
1871 * Filling in all of a previously delayed allocation extent.
1872 * The right neighbor is contiguous, the left is not.
1873 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001874 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 xfs_bmbt_set_startblock(ep, new->br_startblock);
1876 xfs_bmbt_set_blockcount(ep,
1877 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001878 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001879
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001880 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1881 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 rval = XFS_ILOG_DEXT;
1883 else {
1884 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001885 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001887 RIGHT.br_blockcount, &i);
1888 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001890 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001891 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 new->br_startblock,
1893 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001894 RIGHT.br_blockcount, PREV.br_state);
1895 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 goto done;
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 break;
1899
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001900 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 /*
1902 * Filling in all of a previously delayed allocation extent.
1903 * Neither the left nor right neighbors are contiguous with
1904 * the new one.
1905 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001906 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001908 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001909
Darrick J. Wong60b49842016-10-03 09:11:34 -07001910 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001911 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1913 else {
1914 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001915 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001917 &i);
1918 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001920 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001921 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1922 error = xfs_btree_insert(bma->cur, &i);
1923 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001925 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 break;
1928
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001929 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 /*
1931 * Filling in the first part of a previous delayed allocation.
1932 * The left neighbor is contiguous.
1933 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001934 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1935 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 LEFT.br_blockcount + new->br_blockcount);
1937 xfs_bmbt_set_startoff(ep,
1938 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001939 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001942 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001944 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 rval = XFS_ILOG_DEXT;
1946 else {
1947 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001948 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001950 &i);
1951 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001953 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001954 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 LEFT.br_startblock,
1956 LEFT.br_blockcount +
1957 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 LEFT.br_state);
1959 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 goto done;
1961 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001962 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001963 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001964 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001965 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001966
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001967 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 break;
1969
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001970 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /*
1972 * Filling in the first part of a previous delayed allocation.
1973 * The left neighbor is not contiguous.
1974 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001975 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 xfs_bmbt_set_startoff(ep, new_endoff);
1977 temp = PREV.br_blockcount - new->br_blockcount;
1978 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001979 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001980 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001981 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1983 else {
1984 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001985 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001987 &i);
1988 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001990 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001991 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1992 error = xfs_btree_insert(bma->cur, &i);
1993 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001995 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001997
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001998 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001999 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002000 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002001 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 rval |= tmp_rval;
2003 if (error)
2004 goto done;
2005 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002006 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002007 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002008 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2009 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002010 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002011 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 break;
2013
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002014 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 /*
2016 * Filling in the last part of a previous delayed allocation.
2017 * The right neighbor is contiguous with the new allocation.
2018 */
2019 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002020 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002022 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002023 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 new->br_blockcount + RIGHT.br_blockcount,
2025 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002026 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2027 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 rval = XFS_ILOG_DEXT;
2029 else {
2030 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002031 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002033 RIGHT.br_blockcount, &i);
2034 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002036 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002037 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 new->br_startblock,
2039 new->br_blockcount +
2040 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002041 RIGHT.br_state);
2042 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 goto done;
2044 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002045
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002046 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002047 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002048 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002049 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002050 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002051
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002052 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 break;
2054
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002055 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /*
2057 * Filling in the last part of a previous delayed allocation.
2058 * The right neighbor is not contiguous.
2059 */
2060 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002061 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002063 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002064 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002065 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2067 else {
2068 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002069 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002071 &i);
2072 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002074 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002075 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2076 error = xfs_btree_insert(bma->cur, &i);
2077 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002079 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002081
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002082 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002083 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002084 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002085 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 rval |= tmp_rval;
2087 if (error)
2088 goto done;
2089 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002090 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002091 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002092 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2093 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002094 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002095 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002096
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002097 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 break;
2099
2100 case 0:
2101 /*
2102 * Filling in the middle part of a previous delayed allocation.
2103 * Contiguity is impossible here.
2104 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002105 *
2106 * We start with a delayed allocation:
2107 *
2108 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2109 * PREV @ idx
2110 *
2111 * and we are allocating:
2112 * +rrrrrrrrrrrrrrrrr+
2113 * new
2114 *
2115 * and we set it up for insertion as:
2116 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2117 * new
2118 * PREV @ idx LEFT RIGHT
2119 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 */
2121 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002123 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002124 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2125 LEFT = *new;
2126 RIGHT.br_state = PREV.br_state;
2127 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002128 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002129 RIGHT.br_startoff = new_endoff;
2130 RIGHT.br_blockcount = temp2;
2131 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002132 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002133 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002134 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2136 else {
2137 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002138 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002140 &i);
2141 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002143 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002144 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2145 error = xfs_btree_insert(bma->cur, &i);
2146 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002148 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002150
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002151 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002152 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002153 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002154 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 rval |= tmp_rval;
2156 if (error)
2157 goto done;
2158 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002159 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2160 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002161 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002162 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002163 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002164 error = xfs_mod_fdblocks(bma->ip->i_mount,
2165 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002166 ASSERT(!error);
2167 if (error)
2168 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002170
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002171 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002172 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002173 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2174 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2175 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002176 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002177 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002178
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002179 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002180 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 break;
2182
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002183 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2184 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2185 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2186 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2187 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2188 case BMAP_LEFT_CONTIG:
2189 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 /*
2191 * These cases are all impossible.
2192 */
2193 ASSERT(0);
2194 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002195
Darrick J. Wong9c194642016-08-03 12:16:05 +10002196 /* add reverse mapping */
2197 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2198 if (error)
2199 goto done;
2200
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002201 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002202 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002203 int tmp_logflags; /* partial log flag return val */
2204
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002205 ASSERT(bma->cur == NULL);
2206 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002207 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002208 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002209 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002210 if (error)
2211 goto done;
2212 }
2213
2214 /* adjust for changes in reserved delayed indirect blocks */
2215 if (da_old || da_new) {
2216 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002217 if (bma->cur)
2218 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002219 ASSERT(temp <= da_old);
2220 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002221 xfs_mod_fdblocks(bma->ip->i_mount,
2222 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002223 }
2224
2225 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002226 if (bma->cur)
2227 bma->cur->bc_private.b.allocated = 0;
2228
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002229 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002231 if (whichfork != XFS_COW_FORK)
2232 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return error;
2234#undef LEFT
2235#undef RIGHT
2236#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237}
2238
2239/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002240 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 */
2242STATIC int /* error */
2243xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002244 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002246 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002248 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002249 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002250 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002251 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002254 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002257 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 xfs_fileoff_t new_endoff; /* end offset of new entry */
2259 xfs_exntst_t newext; /* new extent state */
2260 xfs_exntst_t oldext; /* old extent state */
2261 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2262 /* left is 0, right is 1, prev is 2 */
2263 int rval=0; /* return value (logging flags) */
2264 int state = 0;/* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002265 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002267 *logflagsp = 0;
2268
2269 cur = *curp;
2270 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2271
2272 ASSERT(*idx >= 0);
2273 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2274 ASSERT(!isnullstartblock(new->br_startblock));
2275
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002276 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278#define LEFT r[0]
2279#define RIGHT r[1]
2280#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 /*
2283 * Set up a bunch of variables to make the tests simpler.
2284 */
2285 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002286 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 xfs_bmbt_get_all(ep, &PREV);
2288 newext = new->br_state;
2289 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2290 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2291 ASSERT(PREV.br_state == oldext);
2292 new_endoff = new->br_startoff + new->br_blockcount;
2293 ASSERT(PREV.br_startoff <= new->br_startoff);
2294 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 /*
2297 * Set flags determining what part of the previous oldext allocation
2298 * extent is being replaced by a newext allocation.
2299 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002300 if (PREV.br_startoff == new->br_startoff)
2301 state |= BMAP_LEFT_FILLING;
2302 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2303 state |= BMAP_RIGHT_FILLING;
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 /*
2306 * Check and set flags if this segment has a left neighbor.
2307 * Don't set contiguous if the combined extent would be too large.
2308 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002309 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002310 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002311 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002312
2313 if (isnullstartblock(LEFT.br_startblock))
2314 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002316
2317 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2318 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2319 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2320 LEFT.br_state == newext &&
2321 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2322 state |= BMAP_LEFT_CONTIG;
2323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 /*
2325 * Check and set flags if this segment has a right neighbor.
2326 * Don't set contiguous if the combined extent would be too large.
2327 * Also check for all-three-contiguous being too large.
2328 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002329 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002330 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002331 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002332 if (isnullstartblock(RIGHT.br_startblock))
2333 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002335
2336 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2337 new_endoff == RIGHT.br_startoff &&
2338 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2339 newext == RIGHT.br_state &&
2340 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2341 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2342 BMAP_RIGHT_FILLING)) !=
2343 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2344 BMAP_RIGHT_FILLING) ||
2345 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2346 <= MAXEXTLEN))
2347 state |= BMAP_RIGHT_CONTIG;
2348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 /*
2350 * Switch out based on the FILLING and CONTIG state bits.
2351 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002352 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2353 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2354 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2355 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 /*
2357 * Setting all of a previous oldext extent to newext.
2358 * The left and right neighbors are both contiguous with new.
2359 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002360 --*idx;
2361
2362 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2363 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 LEFT.br_blockcount + PREV.br_blockcount +
2365 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002366 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002367
Christoph Hellwigec90c552011-05-23 08:52:53 +00002368 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 ip->i_d.di_nextents -= 2;
2370 if (cur == NULL)
2371 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2372 else {
2373 rval = XFS_ILOG_CORE;
2374 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2375 RIGHT.br_startblock,
2376 RIGHT.br_blockcount, &i)))
2377 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002378 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002379 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002381 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002382 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002384 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002385 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002387 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002388 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002390 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2392 LEFT.br_startblock,
2393 LEFT.br_blockcount + PREV.br_blockcount +
2394 RIGHT.br_blockcount, LEFT.br_state)))
2395 goto done;
2396 }
2397 break;
2398
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002399 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 /*
2401 * Setting all of a previous oldext extent to newext.
2402 * The left neighbor is contiguous, the right is not.
2403 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002404 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002405
Christoph Hellwigec90c552011-05-23 08:52:53 +00002406 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2407 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2408 LEFT.br_blockcount + PREV.br_blockcount);
2409 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2410
2411 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 ip->i_d.di_nextents--;
2413 if (cur == NULL)
2414 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2415 else {
2416 rval = XFS_ILOG_CORE;
2417 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2418 PREV.br_startblock, PREV.br_blockcount,
2419 &i)))
2420 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002421 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002422 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002424 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002425 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002427 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2429 LEFT.br_startblock,
2430 LEFT.br_blockcount + PREV.br_blockcount,
2431 LEFT.br_state)))
2432 goto done;
2433 }
2434 break;
2435
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002436 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 /*
2438 * Setting all of a previous oldext extent to newext.
2439 * The right neighbor is contiguous, the left is not.
2440 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002441 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 xfs_bmbt_set_blockcount(ep,
2443 PREV.br_blockcount + RIGHT.br_blockcount);
2444 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002445 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2446 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 ip->i_d.di_nextents--;
2448 if (cur == NULL)
2449 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2450 else {
2451 rval = XFS_ILOG_CORE;
2452 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2453 RIGHT.br_startblock,
2454 RIGHT.br_blockcount, &i)))
2455 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002456 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002457 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002459 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002460 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002462 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2464 new->br_startblock,
2465 new->br_blockcount + RIGHT.br_blockcount,
2466 newext)))
2467 goto done;
2468 }
2469 break;
2470
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002471 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 /*
2473 * Setting all of a previous oldext extent to newext.
2474 * Neither the left nor right neighbors are contiguous with
2475 * the new one.
2476 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002477 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002479 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if (cur == NULL)
2482 rval = XFS_ILOG_DEXT;
2483 else {
2484 rval = 0;
2485 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2486 new->br_startblock, new->br_blockcount,
2487 &i)))
2488 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002489 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2491 new->br_startblock, new->br_blockcount,
2492 newext)))
2493 goto done;
2494 }
2495 break;
2496
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002497 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 /*
2499 * Setting the first part of a previous oldext extent to newext.
2500 * The left neighbor is contiguous.
2501 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002502 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2503 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 LEFT.br_blockcount + new->br_blockcount);
2505 xfs_bmbt_set_startoff(ep,
2506 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002507 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002508
Christoph Hellwigec90c552011-05-23 08:52:53 +00002509 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 xfs_bmbt_set_startblock(ep,
2511 new->br_startblock + new->br_blockcount);
2512 xfs_bmbt_set_blockcount(ep,
2513 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002514 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002515
Christoph Hellwigec90c552011-05-23 08:52:53 +00002516 --*idx;
2517
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, PREV.br_blockcount,
2524 &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,
2528 PREV.br_startoff + new->br_blockcount,
2529 PREV.br_startblock + new->br_blockcount,
2530 PREV.br_blockcount - new->br_blockcount,
2531 oldext)))
2532 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002533 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002535 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 LEFT.br_startblock,
2537 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002538 LEFT.br_state);
2539 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 goto done;
2541 }
2542 break;
2543
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002544 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 /*
2546 * Setting the first part of a previous oldext extent to newext.
2547 * The left neighbor is not contiguous.
2548 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002549 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2551 xfs_bmbt_set_startoff(ep, new_endoff);
2552 xfs_bmbt_set_blockcount(ep,
2553 PREV.br_blockcount - new->br_blockcount);
2554 xfs_bmbt_set_startblock(ep,
2555 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002556 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002557
Christoph Hellwigec90c552011-05-23 08:52:53 +00002558 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 ip->i_d.di_nextents++;
2560 if (cur == NULL)
2561 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2562 else {
2563 rval = XFS_ILOG_CORE;
2564 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2565 PREV.br_startblock, PREV.br_blockcount,
2566 &i)))
2567 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002568 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if ((error = xfs_bmbt_update(cur,
2570 PREV.br_startoff + new->br_blockcount,
2571 PREV.br_startblock + new->br_blockcount,
2572 PREV.br_blockcount - new->br_blockcount,
2573 oldext)))
2574 goto done;
2575 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002576 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002578 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 }
2580 break;
2581
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002582 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 /*
2584 * Setting the last part of a previous oldext extent to newext.
2585 * The right neighbor is contiguous with the new allocation.
2586 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002587 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 xfs_bmbt_set_blockcount(ep,
2589 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002590 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2591
2592 ++*idx;
2593
2594 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2595 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002596 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002598 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002599
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 if (cur == NULL)
2601 rval = XFS_ILOG_DEXT;
2602 else {
2603 rval = 0;
2604 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2605 PREV.br_startblock,
2606 PREV.br_blockcount, &i)))
2607 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002608 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2610 PREV.br_startblock,
2611 PREV.br_blockcount - new->br_blockcount,
2612 oldext)))
2613 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002614 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 goto done;
2616 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2617 new->br_startblock,
2618 new->br_blockcount + RIGHT.br_blockcount,
2619 newext)))
2620 goto done;
2621 }
2622 break;
2623
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002624 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 /*
2626 * Setting the last part of a previous oldext extent to newext.
2627 * The right neighbor is not contiguous.
2628 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002629 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 xfs_bmbt_set_blockcount(ep,
2631 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002632 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002633
Christoph Hellwigec90c552011-05-23 08:52:53 +00002634 ++*idx;
2635 xfs_iext_insert(ip, *idx, 1, new, state);
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 ip->i_d.di_nextents++;
2638 if (cur == NULL)
2639 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2640 else {
2641 rval = XFS_ILOG_CORE;
2642 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2643 PREV.br_startblock, PREV.br_blockcount,
2644 &i)))
2645 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002646 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2648 PREV.br_startblock,
2649 PREV.br_blockcount - new->br_blockcount,
2650 oldext)))
2651 goto done;
2652 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2653 new->br_startblock, new->br_blockcount,
2654 &i)))
2655 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002656 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002658 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002660 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
2662 break;
2663
2664 case 0:
2665 /*
2666 * Setting the middle part of a previous oldext extent to
2667 * newext. Contiguity is impossible here.
2668 * One extent becomes three extents.
2669 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002670 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 xfs_bmbt_set_blockcount(ep,
2672 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002673 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002674
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 r[0] = *new;
2676 r[1].br_startoff = new_endoff;
2677 r[1].br_blockcount =
2678 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2679 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2680 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002681
2682 ++*idx;
2683 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 ip->i_d.di_nextents += 2;
2686 if (cur == NULL)
2687 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2688 else {
2689 rval = XFS_ILOG_CORE;
2690 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2691 PREV.br_startblock, PREV.br_blockcount,
2692 &i)))
2693 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002694 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 /* new right extent - oldext */
2696 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2697 r[1].br_startblock, r[1].br_blockcount,
2698 r[1].br_state)))
2699 goto done;
2700 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002702 cur->bc_rec.b.br_blockcount =
2703 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002704 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002706 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002707 /*
2708 * Reset the cursor to the position of the new extent
2709 * we are about to insert as we can't trust it after
2710 * the previous insert.
2711 */
2712 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2713 new->br_startblock, new->br_blockcount,
2714 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002716 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002718 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002719 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002721 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
2723 break;
2724
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002725 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2726 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2727 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2728 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2729 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2730 case BMAP_LEFT_CONTIG:
2731 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 /*
2733 * These cases are all impossible.
2734 */
2735 ASSERT(0);
2736 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002737
Darrick J. Wong9c194642016-08-03 12:16:05 +10002738 /* update reverse mappings */
2739 error = xfs_rmap_convert_extent(mp, dfops, ip, XFS_DATA_FORK, new);
2740 if (error)
2741 goto done;
2742
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002743 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002744 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002745 int tmp_logflags; /* partial log flag return val */
2746
2747 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002748 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002749 0, &tmp_logflags, XFS_DATA_FORK);
2750 *logflagsp |= tmp_logflags;
2751 if (error)
2752 goto done;
2753 }
2754
2755 /* clear out the allocated field, done with it now in any case. */
2756 if (cur) {
2757 cur->bc_private.b.allocated = 0;
2758 *curp = cur;
2759 }
2760
2761 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002763 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return error;
2765#undef LEFT
2766#undef RIGHT
2767#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
2769
2770/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002771 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002773STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774xfs_bmap_add_extent_hole_delay(
2775 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002776 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002777 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002778 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002780 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2782 xfs_filblks_t newlen=0; /* new indirect size */
2783 xfs_filblks_t oldlen=0; /* old indirect size */
2784 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2785 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002786 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002788 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002790 if (whichfork == XFS_COW_FORK)
2791 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002792 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002793
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 /*
2795 * Check and set flags if this segment has a left neighbor
2796 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002797 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002798 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002799 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002800
2801 if (isnullstartblock(left.br_startblock))
2802 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002804
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 /*
2806 * Check and set flags if the current (right) segment exists.
2807 * If it doesn't exist, we're converting the hole at end-of-file.
2808 */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002809 if (*idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002810 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002811 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002812
2813 if (isnullstartblock(right.br_startblock))
2814 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 /*
2818 * Set contiguity flags on the left and right neighbors.
2819 * Don't let extents get too large, even if the pieces are contiguous.
2820 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002821 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2822 left.br_startoff + left.br_blockcount == new->br_startoff &&
2823 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2824 state |= BMAP_LEFT_CONTIG;
2825
2826 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2827 new->br_startoff + new->br_blockcount == right.br_startoff &&
2828 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2829 (!(state & BMAP_LEFT_CONTIG) ||
2830 (left.br_blockcount + new->br_blockcount +
2831 right.br_blockcount <= MAXEXTLEN)))
2832 state |= BMAP_RIGHT_CONTIG;
2833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 /*
2835 * Switch out based on the contiguity flags.
2836 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002837 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2838 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 /*
2840 * New allocation is contiguous with delayed allocations
2841 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002842 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002844 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 temp = left.br_blockcount + new->br_blockcount +
2846 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002847
Christoph Hellwigec90c552011-05-23 08:52:53 +00002848 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2849 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002850 oldlen = startblockval(left.br_startblock) +
2851 startblockval(new->br_startblock) +
2852 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002854 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002855 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002856 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002857
Christoph Hellwigec90c552011-05-23 08:52:53 +00002858 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 break;
2860
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002861 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 /*
2863 * New allocation is contiguous with a delayed allocation
2864 * on the left.
2865 * Merge the new allocation with the left neighbor.
2866 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002867 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002869
2870 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2871 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002872 oldlen = startblockval(left.br_startblock) +
2873 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002875 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002876 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002877 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 break;
2879
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002880 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 /*
2882 * New allocation is contiguous with a delayed allocation
2883 * on the right.
2884 * Merge the new allocation with the right neighbor.
2885 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002886 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002888 oldlen = startblockval(new->br_startblock) +
2889 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002891 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2892 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002893 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002894 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 break;
2896
2897 case 0:
2898 /*
2899 * New allocation is not contiguous with another
2900 * delayed allocation.
2901 * Insert a new entry.
2902 */
2903 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002904 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 break;
2906 }
2907 if (oldlen != newlen) {
2908 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002909 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2910 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 /*
2912 * Nothing to do for disk quota accounting here.
2913 */
2914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
2916
2917/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002918 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 */
2920STATIC int /* error */
2921xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002922 struct xfs_bmalloca *bma,
2923 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002925 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 int i; /* temp state */
2928 xfs_ifork_t *ifp; /* inode fork pointer */
2929 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2930 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002931 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002933 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Eric Sandeenf1f96c42016-01-04 16:10:42 +11002935 mp = bma->ip->i_mount;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002936 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002937
Christoph Hellwigc6534242011-09-18 20:41:05 +00002938 ASSERT(bma->idx >= 0);
2939 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002940 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002941 ASSERT(!bma->cur ||
2942 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Darrick J. Wong3993bae2016-10-03 09:11:32 -07002943 ASSERT(whichfork != XFS_COW_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002944
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002945 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002946
2947 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002948 if (whichfork == XFS_ATTR_FORK)
2949 state |= BMAP_ATTRFORK;
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 /*
2952 * Check and set flags if this segment has a left neighbor.
2953 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002954 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002955 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002956 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002957 if (isnullstartblock(left.br_startblock))
2958 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002960
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 /*
2962 * Check and set flags if this segment has a current value.
2963 * Not true if we're inserting into the "hole" at eof.
2964 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002965 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002966 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002967 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002968 if (isnullstartblock(right.br_startblock))
2969 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 /*
2973 * We're inserting a real allocation between "left" and "right".
2974 * Set the contiguity flags. Don't let extents get too large.
2975 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002976 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2977 left.br_startoff + left.br_blockcount == new->br_startoff &&
2978 left.br_startblock + left.br_blockcount == new->br_startblock &&
2979 left.br_state == new->br_state &&
2980 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2981 state |= BMAP_LEFT_CONTIG;
2982
2983 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2984 new->br_startoff + new->br_blockcount == right.br_startoff &&
2985 new->br_startblock + new->br_blockcount == right.br_startblock &&
2986 new->br_state == right.br_state &&
2987 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2988 (!(state & BMAP_LEFT_CONTIG) ||
2989 left.br_blockcount + new->br_blockcount +
2990 right.br_blockcount <= MAXEXTLEN))
2991 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002993 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 /*
2995 * Select which case we're in here, and implement it.
2996 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002997 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2998 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 /*
3000 * New allocation is contiguous with real allocations on the
3001 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003002 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003004 --bma->idx;
3005 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3006 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 left.br_blockcount + new->br_blockcount +
3008 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003009 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003010
Christoph Hellwigc6534242011-09-18 20:41:05 +00003011 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003012
Christoph Hellwigc6534242011-09-18 20:41:05 +00003013 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3014 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3015 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003016 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003017 } else {
3018 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003019 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3020 right.br_startblock, right.br_blockcount,
3021 &i);
3022 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003023 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003024 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003025 error = xfs_btree_delete(bma->cur, &i);
3026 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003027 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003028 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003029 error = xfs_btree_decrement(bma->cur, 0, &i);
3030 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003031 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003032 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003033 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003034 left.br_startblock,
3035 left.br_blockcount +
3036 new->br_blockcount +
3037 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003038 left.br_state);
3039 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003040 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003042 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003044 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 /*
3046 * New allocation is contiguous with a real allocation
3047 * on the left.
3048 * Merge the new allocation with the left neighbor.
3049 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003050 --bma->idx;
3051 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3052 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003054 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003055
Christoph Hellwigc6534242011-09-18 20:41:05 +00003056 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003057 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003058 } else {
3059 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003060 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3061 left.br_startblock, left.br_blockcount,
3062 &i);
3063 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003064 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003065 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003066 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003067 left.br_startblock,
3068 left.br_blockcount +
3069 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003070 left.br_state);
3071 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003072 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003074 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003076 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 /*
3078 * New allocation is contiguous with a real allocation
3079 * on the right.
3080 * Merge the new allocation with the right neighbor.
3081 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003082 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3083 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003084 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 new->br_blockcount + right.br_blockcount,
3086 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003087 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003088
Christoph Hellwigc6534242011-09-18 20:41:05 +00003089 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003090 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003091 } else {
3092 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003093 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003094 right.br_startoff,
3095 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003096 right.br_blockcount, &i);
3097 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003098 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003099 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003100 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003101 new->br_startblock,
3102 new->br_blockcount +
3103 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003104 right.br_state);
3105 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003106 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003108 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
3110 case 0:
3111 /*
3112 * New allocation is not contiguous with another
3113 * real allocation.
3114 * Insert a new entry.
3115 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003116 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3117 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3118 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3119 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003120 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003121 } else {
3122 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003123 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003124 new->br_startoff,
3125 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003126 new->br_blockcount, &i);
3127 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003128 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003129 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003130 bma->cur->bc_rec.b.br_state = new->br_state;
3131 error = xfs_btree_insert(bma->cur, &i);
3132 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003133 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003134 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003138
Darrick J. Wong9c194642016-08-03 12:16:05 +10003139 /* add reverse mapping */
3140 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
3141 if (error)
3142 goto done;
3143
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003144 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003145 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003146 int tmp_logflags; /* partial log flag return val */
3147
Christoph Hellwigc6534242011-09-18 20:41:05 +00003148 ASSERT(bma->cur == NULL);
3149 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003150 bma->firstblock, bma->dfops, &bma->cur,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003151 0, &tmp_logflags, whichfork);
3152 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003153 if (error)
3154 goto done;
3155 }
3156
3157 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003158 if (bma->cur)
3159 bma->cur->bc_private.b.allocated = 0;
3160
3161 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003162done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003163 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003164 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
3166
Nathan Scottdd9f4382006-01-11 15:28:28 +11003167/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11003168 * Functions used in the extent read, allocate and remove paths
3169 */
3170
3171/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003172 * Adjust the size of the new extent based on di_extsize and rt extsize.
3173 */
Dave Chinner68988112013-08-12 20:49:42 +10003174int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003175xfs_bmap_extsize_align(
3176 xfs_mount_t *mp,
3177 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3178 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3179 xfs_extlen_t extsz, /* align to this extent size */
3180 int rt, /* is this a realtime inode? */
3181 int eof, /* is extent at end-of-file? */
3182 int delay, /* creating delalloc extent? */
3183 int convert, /* overwriting unwritten extent? */
3184 xfs_fileoff_t *offp, /* in/out: aligned offset */
3185 xfs_extlen_t *lenp) /* in/out: aligned length */
3186{
3187 xfs_fileoff_t orig_off; /* original offset */
3188 xfs_extlen_t orig_alen; /* original length */
3189 xfs_fileoff_t orig_end; /* original off+len */
3190 xfs_fileoff_t nexto; /* next file offset */
3191 xfs_fileoff_t prevo; /* previous file offset */
3192 xfs_fileoff_t align_off; /* temp for offset */
3193 xfs_extlen_t align_alen; /* temp for length */
3194 xfs_extlen_t temp; /* temp for calculations */
3195
3196 if (convert)
3197 return 0;
3198
3199 orig_off = align_off = *offp;
3200 orig_alen = align_alen = *lenp;
3201 orig_end = orig_off + orig_alen;
3202
3203 /*
3204 * If this request overlaps an existing extent, then don't
3205 * attempt to perform any additional alignment.
3206 */
3207 if (!delay && !eof &&
3208 (orig_off >= gotp->br_startoff) &&
3209 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3210 return 0;
3211 }
3212
3213 /*
3214 * If the file offset is unaligned vs. the extent size
3215 * we need to align it. This will be possible unless
3216 * the file was previously written with a kernel that didn't
3217 * perform this alignment, or if a truncate shot us in the
3218 * foot.
3219 */
3220 temp = do_mod(orig_off, extsz);
3221 if (temp) {
3222 align_alen += temp;
3223 align_off -= temp;
3224 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003225
3226 /* Same adjustment for the end of the requested area. */
3227 temp = (align_alen % extsz);
3228 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003229 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003230
3231 /*
3232 * For large extent hint sizes, the aligned extent might be larger than
3233 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3234 * the length back under MAXEXTLEN. The outer allocation loops handle
3235 * short allocation just fine, so it is safe to do this. We only want to
3236 * do it when we are forced to, though, because it means more allocation
3237 * operations are required.
3238 */
3239 while (align_alen > MAXEXTLEN)
3240 align_alen -= extsz;
3241 ASSERT(align_alen <= MAXEXTLEN);
3242
Nathan Scottdd9f4382006-01-11 15:28:28 +11003243 /*
3244 * If the previous block overlaps with this proposed allocation
3245 * then move the start forward without adjusting the length.
3246 */
3247 if (prevp->br_startoff != NULLFILEOFF) {
3248 if (prevp->br_startblock == HOLESTARTBLOCK)
3249 prevo = prevp->br_startoff;
3250 else
3251 prevo = prevp->br_startoff + prevp->br_blockcount;
3252 } else
3253 prevo = 0;
3254 if (align_off != orig_off && align_off < prevo)
3255 align_off = prevo;
3256 /*
3257 * If the next block overlaps with this proposed allocation
3258 * then move the start back without adjusting the length,
3259 * but not before offset 0.
3260 * This may of course make the start overlap previous block,
3261 * and if we hit the offset 0 limit then the next block
3262 * can still overlap too.
3263 */
3264 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3265 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3266 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3267 nexto = gotp->br_startoff + gotp->br_blockcount;
3268 else
3269 nexto = gotp->br_startoff;
3270 } else
3271 nexto = NULLFILEOFF;
3272 if (!eof &&
3273 align_off + align_alen != orig_end &&
3274 align_off + align_alen > nexto)
3275 align_off = nexto > align_alen ? nexto - align_alen : 0;
3276 /*
3277 * If we're now overlapping the next or previous extent that
3278 * means we can't fit an extsz piece in this hole. Just move
3279 * the start forward to the first valid spot and set
3280 * the length so we hit the end.
3281 */
3282 if (align_off != orig_off && align_off < prevo)
3283 align_off = prevo;
3284 if (align_off + align_alen != orig_end &&
3285 align_off + align_alen > nexto &&
3286 nexto != NULLFILEOFF) {
3287 ASSERT(nexto > prevo);
3288 align_alen = nexto - align_off;
3289 }
3290
3291 /*
3292 * If realtime, and the result isn't a multiple of the realtime
3293 * extent size we need to remove blocks until it is.
3294 */
3295 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3296 /*
3297 * We're not covering the original request, or
3298 * we won't be able to once we fix the length.
3299 */
3300 if (orig_off < align_off ||
3301 orig_end > align_off + align_alen ||
3302 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003303 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003304 /*
3305 * Try to fix it by moving the start up.
3306 */
3307 if (align_off + temp <= orig_off) {
3308 align_alen -= temp;
3309 align_off += temp;
3310 }
3311 /*
3312 * Try to fix it by moving the end in.
3313 */
3314 else if (align_off + align_alen - temp >= orig_end)
3315 align_alen -= temp;
3316 /*
3317 * Set the start to the minimum then trim the length.
3318 */
3319 else {
3320 align_alen -= orig_off - align_off;
3321 align_off = orig_off;
3322 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3323 }
3324 /*
3325 * Result doesn't cover the request, fail it.
3326 */
3327 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003328 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003329 } else {
3330 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003331 /* see MAXEXTLEN handling above */
3332 ASSERT(orig_end <= align_off + align_alen ||
3333 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003334 }
3335
3336#ifdef DEBUG
3337 if (!eof && gotp->br_startoff != NULLFILEOFF)
3338 ASSERT(align_off + align_alen <= gotp->br_startoff);
3339 if (prevp->br_startoff != NULLFILEOFF)
3340 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3341#endif
3342
3343 *lenp = align_alen;
3344 *offp = align_off;
3345 return 0;
3346}
3347
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348#define XFS_ALLOC_GAP_UNITS 4
3349
Dave Chinner68988112013-08-12 20:49:42 +10003350void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003351xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003352 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353{
3354 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3356 xfs_mount_t *mp; /* mount point structure */
3357 int nullfb; /* true if ap->firstblock isn't set */
3358 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
3360#define ISVALID(x,y) \
3361 (rt ? \
3362 (x) < mp->m_sb.sb_rblocks : \
3363 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3364 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3365 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003368 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003369 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3370 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003371 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 /*
3373 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003374 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003376 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3377 !isnullstartblock(ap->prev.br_startblock) &&
3378 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3379 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003380 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 /*
3382 * Adjust for the gap between prevp and us.
3383 */
Dave Chinner3a756672011-09-18 20:40:58 +00003384 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003385 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003387 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3388 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 }
3390 /*
3391 * If not at eof, then compare the two neighbor blocks.
3392 * Figure out whether either one gives us a good starting point,
3393 * and pick the better one.
3394 */
3395 else if (!ap->eof) {
3396 xfs_fsblock_t gotbno; /* right side block number */
3397 xfs_fsblock_t gotdiff=0; /* right side difference */
3398 xfs_fsblock_t prevbno; /* left side block number */
3399 xfs_fsblock_t prevdiff=0; /* left side difference */
3400
3401 /*
3402 * If there's a previous (left) block, select a requested
3403 * start block based on it.
3404 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003405 if (ap->prev.br_startoff != NULLFILEOFF &&
3406 !isnullstartblock(ap->prev.br_startblock) &&
3407 (prevbno = ap->prev.br_startblock +
3408 ap->prev.br_blockcount) &&
3409 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 /*
3411 * Calculate gap to end of previous block.
3412 */
Dave Chinner3a756672011-09-18 20:40:58 +00003413 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003414 (ap->prev.br_startoff +
3415 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 /*
3417 * Figure the startblock based on the previous block's
3418 * end and the gap size.
3419 * Heuristic!
3420 * If the gap is large relative to the piece we're
3421 * allocating, or using it gives us an invalid block
3422 * number, then just use the end of the previous block.
3423 */
Dave Chinner3a756672011-09-18 20:40:58 +00003424 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003426 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 prevbno += adjust;
3428 else
3429 prevdiff += adjust;
3430 /*
3431 * If the firstblock forbids it, can't use it,
3432 * must use default.
3433 */
3434 if (!rt && !nullfb &&
3435 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3436 prevbno = NULLFSBLOCK;
3437 }
3438 /*
3439 * No previous block or can't follow it, just default.
3440 */
3441 else
3442 prevbno = NULLFSBLOCK;
3443 /*
3444 * If there's a following (right) block, select a requested
3445 * start block based on it.
3446 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003447 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 /*
3449 * Calculate gap to start of next block.
3450 */
Dave Chinner3a756672011-09-18 20:40:58 +00003451 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 /*
3453 * Figure the startblock based on the next block's
3454 * start and the gap size.
3455 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003456 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 /*
3458 * Heuristic!
3459 * If the gap is large relative to the piece we're
3460 * allocating, or using it gives us an invalid block
3461 * number, then just use the start of the next block
3462 * offset by our length.
3463 */
Dave Chinner3a756672011-09-18 20:40:58 +00003464 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 ISVALID(gotbno - gotdiff, gotbno))
3466 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003467 else if (ISVALID(gotbno - ap->length, gotbno)) {
3468 gotbno -= ap->length;
3469 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 } else
3471 gotdiff += adjust;
3472 /*
3473 * If the firstblock forbids it, can't use it,
3474 * must use default.
3475 */
3476 if (!rt && !nullfb &&
3477 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3478 gotbno = NULLFSBLOCK;
3479 }
3480 /*
3481 * No next block, just default.
3482 */
3483 else
3484 gotbno = NULLFSBLOCK;
3485 /*
3486 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003487 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 */
3489 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003490 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003492 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003494 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003496#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003497}
3498
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003499static int
3500xfs_bmap_longest_free_extent(
3501 struct xfs_trans *tp,
3502 xfs_agnumber_t ag,
3503 xfs_extlen_t *blen,
3504 int *notinit)
3505{
3506 struct xfs_mount *mp = tp->t_mountp;
3507 struct xfs_perag *pag;
3508 xfs_extlen_t longest;
3509 int error = 0;
3510
3511 pag = xfs_perag_get(mp, ag);
3512 if (!pag->pagf_init) {
3513 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3514 if (error)
3515 goto out;
3516
3517 if (!pag->pagf_init) {
3518 *notinit = 1;
3519 goto out;
3520 }
3521 }
3522
Dave Chinner50adbcb2015-06-22 10:04:31 +10003523 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003524 xfs_alloc_min_freelist(mp, pag),
3525 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003526 if (*blen < longest)
3527 *blen = longest;
3528
3529out:
3530 xfs_perag_put(pag);
3531 return error;
3532}
3533
3534static void
3535xfs_bmap_select_minlen(
3536 struct xfs_bmalloca *ap,
3537 struct xfs_alloc_arg *args,
3538 xfs_extlen_t *blen,
3539 int notinit)
3540{
3541 if (notinit || *blen < ap->minlen) {
3542 /*
3543 * Since we did a BUF_TRYLOCK above, it is possible that
3544 * there is space for this request.
3545 */
3546 args->minlen = ap->minlen;
3547 } else if (*blen < args->maxlen) {
3548 /*
3549 * If the best seen length is less than the request length,
3550 * use the best as the minimum.
3551 */
3552 args->minlen = *blen;
3553 } else {
3554 /*
3555 * Otherwise we've seen an extent as big as maxlen, use that
3556 * as the minimum.
3557 */
3558 args->minlen = args->maxlen;
3559 }
3560}
3561
Nathan Scotta365bdd2006-03-14 13:34:16 +11003562STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003563xfs_bmap_btalloc_nullfb(
3564 struct xfs_bmalloca *ap,
3565 struct xfs_alloc_arg *args,
3566 xfs_extlen_t *blen)
3567{
3568 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003569 xfs_agnumber_t ag, startag;
3570 int notinit = 0;
3571 int error;
3572
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003573 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003574 args->total = ap->total;
3575
Christoph Hellwigc467c042010-02-15 23:34:42 +00003576 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3577 if (startag == NULLAGNUMBER)
3578 startag = ag = 0;
3579
Dave Chinner14b064c2011-01-27 12:16:28 +11003580 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003581 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3582 &notinit);
3583 if (error)
3584 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003585
Christoph Hellwigc467c042010-02-15 23:34:42 +00003586 if (++ag == mp->m_sb.sb_agcount)
3587 ag = 0;
3588 if (ag == startag)
3589 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003590 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003591
3592 xfs_bmap_select_minlen(ap, args, blen, notinit);
3593 return 0;
3594}
3595
3596STATIC int
3597xfs_bmap_btalloc_filestreams(
3598 struct xfs_bmalloca *ap,
3599 struct xfs_alloc_arg *args,
3600 xfs_extlen_t *blen)
3601{
3602 struct xfs_mount *mp = ap->ip->i_mount;
3603 xfs_agnumber_t ag;
3604 int notinit = 0;
3605 int error;
3606
3607 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3608 args->total = ap->total;
3609
3610 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3611 if (ag == NULLAGNUMBER)
3612 ag = 0;
3613
3614 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3615 if (error)
3616 return error;
3617
3618 if (*blen < args->maxlen) {
3619 error = xfs_filestream_new_ag(ap, &ag);
3620 if (error)
3621 return error;
3622
3623 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3624 &notinit);
3625 if (error)
3626 return error;
3627
3628 }
3629
3630 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003631
3632 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003633 * Set the failure fallback case to look in the selected AG as stream
3634 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003635 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003636 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003637 return 0;
3638}
3639
3640STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003641xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003642 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003643{
3644 xfs_mount_t *mp; /* mount point structure */
3645 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003646 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003647 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003648 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003649 xfs_alloc_arg_t args;
3650 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003651 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003652 int nullfb; /* true if ap->firstblock isn't set */
3653 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003654 int tryagain;
3655 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003656 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003657
Dave Chinnera99ebf42011-12-01 11:24:20 +00003658 ASSERT(ap->length);
3659
Nathan Scotta365bdd2006-03-14 13:34:16 +11003660 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003661
3662 /* stripe alignment for allocation is determined by mount parameters */
3663 stripe_align = 0;
3664 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3665 stripe_align = mp->m_swidth;
3666 else if (mp->m_dalign)
3667 stripe_align = mp->m_dalign;
3668
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003669 if (ap->flags & XFS_BMAPI_COWFORK)
3670 align = xfs_get_cowextsz_hint(ap->ip);
3671 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003672 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003673 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003674 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003675 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003676 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003677 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003678 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003679 }
Dave Chinner33177f052013-12-12 16:34:36 +11003680
3681
Dave Chinner0937e0f2011-09-18 20:40:57 +00003682 nullfb = *ap->firstblock == NULLFSBLOCK;
3683 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003684 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003685 if (xfs_alloc_is_userdata(ap->datatype) &&
3686 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003687 ag = xfs_filestream_lookup_ag(ap->ip);
3688 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003689 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003690 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003691 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003692 }
3693 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003694 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003695
3696 xfs_bmap_adjacent(ap);
3697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003699 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 * it's in the right allocation group.
3701 */
Dave Chinner3a756672011-09-18 20:40:58 +00003702 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 ;
3704 else
Dave Chinner3a756672011-09-18 20:40:58 +00003705 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 * Normal allocation, done through xfs_alloc_vextent.
3708 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003709 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003710 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003711 args.tp = ap->tp;
3712 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003713 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003714 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003715
3716 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003717 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003718 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003719 blen = 0;
3720 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003721 /*
3722 * Search for an allocation group with a single extent large
3723 * enough for the request. If one isn't found, then adjust
3724 * the minimum allocation size to the largest space found.
3725 */
Dave Chinner292378e2016-09-26 08:21:28 +10003726 if (xfs_alloc_is_userdata(ap->datatype) &&
3727 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003728 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3729 else
3730 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003731 if (error)
3732 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003733 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003734 if (xfs_inode_is_filestream(ap->ip))
3735 args.type = XFS_ALLOCTYPE_FIRST_AG;
3736 else
3737 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003738 args.total = args.minlen = ap->minlen;
3739 } else {
3740 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3741 args.total = ap->total;
3742 args.minlen = ap->minlen;
3743 }
David Chinner957d0eb2007-06-18 16:50:37 +10003744 /* apply extent size hints if obtained earlier */
3745 if (unlikely(align)) {
3746 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003747 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003748 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003749 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003750 args.prod = 1;
3751 args.mod = 0;
3752 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003753 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003754 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003755 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3756 }
3757 /*
3758 * If we are not low on available data blocks, and the
3759 * underlying logical volume manager is a stripe, and
3760 * the file offset is zero then try to allocate data
3761 * blocks on stripe unit boundary.
3762 * NOTE: ap->aeof is only set if the allocation length
3763 * is >= the stripe unit and the allocation offset is
3764 * at the end of file.
3765 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003766 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003767 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003768 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003769 atype = args.type;
3770 isaligned = 1;
3771 /*
3772 * Adjust for alignment
3773 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003774 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003775 args.minlen = blen - args.alignment;
3776 args.minalignslop = 0;
3777 } else {
3778 /*
3779 * First try an exact bno allocation.
3780 * If it fails then do a near or start bno
3781 * allocation with alignment turned on.
3782 */
3783 atype = args.type;
3784 tryagain = 1;
3785 args.type = XFS_ALLOCTYPE_THIS_BNO;
3786 args.alignment = 1;
3787 /*
3788 * Compute the minlen+alignment for the
3789 * next case. Set slop so that the value
3790 * of minlen+alignment+slop doesn't go up
3791 * between the calls.
3792 */
Dave Chinner33177f052013-12-12 16:34:36 +11003793 if (blen > stripe_align && blen <= args.maxlen)
3794 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003795 else
3796 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003797 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003798 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003799 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003800 args.minlen - 1;
3801 else
3802 args.minalignslop = 0;
3803 }
3804 } else {
3805 args.alignment = 1;
3806 args.minalignslop = 0;
3807 }
3808 args.minleft = ap->minleft;
3809 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003810 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003811 args.datatype = ap->datatype;
3812 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003813 args.ip = ap->ip;
3814
3815 error = xfs_alloc_vextent(&args);
3816 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003817 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003818
Nathan Scotta365bdd2006-03-14 13:34:16 +11003819 if (tryagain && args.fsbno == NULLFSBLOCK) {
3820 /*
3821 * Exact allocation failed. Now try with alignment
3822 * turned on.
3823 */
3824 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003825 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003826 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003827 args.minlen = nextminlen;
3828 args.minalignslop = 0;
3829 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 if ((error = xfs_alloc_vextent(&args)))
3831 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003832 }
3833 if (isaligned && args.fsbno == NULLFSBLOCK) {
3834 /*
3835 * allocation failed, so turn off alignment and
3836 * try again.
3837 */
3838 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003839 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003840 args.alignment = 0;
3841 if ((error = xfs_alloc_vextent(&args)))
3842 return error;
3843 }
3844 if (args.fsbno == NULLFSBLOCK && nullfb &&
3845 args.minlen > ap->minlen) {
3846 args.minlen = ap->minlen;
3847 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003848 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003849 if ((error = xfs_alloc_vextent(&args)))
3850 return error;
3851 }
3852 if (args.fsbno == NULLFSBLOCK && nullfb) {
3853 args.fsbno = 0;
3854 args.type = XFS_ALLOCTYPE_FIRST_AG;
3855 args.total = ap->minlen;
3856 args.minleft = 0;
3857 if ((error = xfs_alloc_vextent(&args)))
3858 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003859 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003860 }
3861 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003862 /*
3863 * check the allocation happened at the same or higher AG than
3864 * the first block that was allocated.
3865 */
3866 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3867 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3868 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003869 (ap->dfops->dop_low &&
Dave Chinner0937e0f2011-09-18 20:40:57 +00003870 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3871 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3872
Dave Chinner3a756672011-09-18 20:40:58 +00003873 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003874 if (*ap->firstblock == NULLFSBLOCK)
3875 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003876 ASSERT(nullfb || fb_agno == args.agno ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003877 (ap->dfops->dop_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003878 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003879 if (!(ap->flags & XFS_BMAPI_COWFORK))
3880 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003881 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3882 if (ap->wasdel)
3883 ap->ip->i_delayed_blks -= args.len;
3884 /*
3885 * Adjust the disk quota also. This was reserved
3886 * earlier.
3887 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003888 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003889 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3890 XFS_TRANS_DQ_BCOUNT,
3891 (long) args.len);
3892 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003893 ap->blkno = NULLFSBLOCK;
3894 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 }
3896 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003897}
3898
3899/*
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003900 * For a remap operation, just "allocate" an extent at the address that the
3901 * caller passed in, and ensure that the AGFL is the right size. The caller
3902 * will then map the "allocated" extent into the file somewhere.
3903 */
3904STATIC int
3905xfs_bmap_remap_alloc(
3906 struct xfs_bmalloca *ap)
3907{
3908 struct xfs_trans *tp = ap->tp;
3909 struct xfs_mount *mp = tp->t_mountp;
3910 xfs_agblock_t bno;
3911 struct xfs_alloc_arg args;
3912 int error;
3913
3914 /*
3915 * validate that the block number is legal - the enables us to detect
3916 * and handle a silent filesystem corruption rather than crashing.
3917 */
3918 memset(&args, 0, sizeof(struct xfs_alloc_arg));
3919 args.tp = ap->tp;
3920 args.mp = ap->tp->t_mountp;
3921 bno = *ap->firstblock;
3922 args.agno = XFS_FSB_TO_AGNO(mp, bno);
3923 args.agbno = XFS_FSB_TO_AGBNO(mp, bno);
3924 if (args.agno >= mp->m_sb.sb_agcount ||
3925 args.agbno >= mp->m_sb.sb_agblocks)
3926 return -EFSCORRUPTED;
3927
3928 /* "Allocate" the extent from the range we passed in. */
3929 trace_xfs_bmap_remap_alloc(ap->ip, *ap->firstblock, ap->length);
3930 ap->blkno = bno;
3931 ap->ip->i_d.di_nblocks += ap->length;
3932 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3933
3934 /* Fix the freelist, like a real allocator does. */
3935 args.datatype = ap->datatype;
3936 args.pag = xfs_perag_get(args.mp, args.agno);
3937 ASSERT(args.pag);
3938
3939 /*
3940 * The freelist fixing code will decline the allocation if
3941 * the size and shape of the free space doesn't allow for
3942 * allocating the extent and updating all the metadata that
3943 * happens during an allocation. We're remapping, not
3944 * allocating, so skip that check by pretending to be freeing.
3945 */
3946 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
3947 if (error)
3948 goto error0;
3949error0:
3950 xfs_perag_put(args.pag);
3951 if (error)
3952 trace_xfs_bmap_remap_alloc_error(ap->ip, error, _RET_IP_);
3953 return error;
3954}
3955
3956/*
Nathan Scotta365bdd2006-03-14 13:34:16 +11003957 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3958 * It figures out where to ask the underlying allocator to put the new extent.
3959 */
3960STATIC int
3961xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003962 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003963{
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003964 if (ap->flags & XFS_BMAPI_REMAP)
3965 return xfs_bmap_remap_alloc(ap);
Dave Chinner292378e2016-09-26 08:21:28 +10003966 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3967 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003968 return xfs_bmap_rtalloc(ap);
3969 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970}
3971
3972/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003973 * Trim the returned map to the required bounds
3974 */
3975STATIC void
3976xfs_bmapi_trim_map(
3977 struct xfs_bmbt_irec *mval,
3978 struct xfs_bmbt_irec *got,
3979 xfs_fileoff_t *bno,
3980 xfs_filblks_t len,
3981 xfs_fileoff_t obno,
3982 xfs_fileoff_t end,
3983 int n,
3984 int flags)
3985{
3986 if ((flags & XFS_BMAPI_ENTIRE) ||
3987 got->br_startoff + got->br_blockcount <= obno) {
3988 *mval = *got;
3989 if (isnullstartblock(got->br_startblock))
3990 mval->br_startblock = DELAYSTARTBLOCK;
3991 return;
3992 }
3993
3994 if (obno > *bno)
3995 *bno = obno;
3996 ASSERT((*bno >= obno) || (n == 0));
3997 ASSERT(*bno < end);
3998 mval->br_startoff = *bno;
3999 if (isnullstartblock(got->br_startblock))
4000 mval->br_startblock = DELAYSTARTBLOCK;
4001 else
4002 mval->br_startblock = got->br_startblock +
4003 (*bno - got->br_startoff);
4004 /*
4005 * Return the minimum of what we got and what we asked for for
4006 * the length. We can use the len variable here because it is
4007 * modified below and we could have been there before coming
4008 * here if the first part of the allocation didn't overlap what
4009 * was asked for.
4010 */
4011 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4012 got->br_blockcount - (*bno - got->br_startoff));
4013 mval->br_state = got->br_state;
4014 ASSERT(mval->br_blockcount <= len);
4015 return;
4016}
4017
4018/*
4019 * Update and validate the extent map to return
4020 */
4021STATIC void
4022xfs_bmapi_update_map(
4023 struct xfs_bmbt_irec **map,
4024 xfs_fileoff_t *bno,
4025 xfs_filblks_t *len,
4026 xfs_fileoff_t obno,
4027 xfs_fileoff_t end,
4028 int *n,
4029 int flags)
4030{
4031 xfs_bmbt_irec_t *mval = *map;
4032
4033 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4034 ((mval->br_startoff + mval->br_blockcount) <= end));
4035 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4036 (mval->br_startoff < obno));
4037
4038 *bno = mval->br_startoff + mval->br_blockcount;
4039 *len = end - *bno;
4040 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4041 /* update previous map with new information */
4042 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4043 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4044 ASSERT(mval->br_state == mval[-1].br_state);
4045 mval[-1].br_blockcount = mval->br_blockcount;
4046 mval[-1].br_state = mval->br_state;
4047 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4048 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4049 mval[-1].br_startblock != HOLESTARTBLOCK &&
4050 mval->br_startblock == mval[-1].br_startblock +
4051 mval[-1].br_blockcount &&
4052 ((flags & XFS_BMAPI_IGSTATE) ||
4053 mval[-1].br_state == mval->br_state)) {
4054 ASSERT(mval->br_startoff ==
4055 mval[-1].br_startoff + mval[-1].br_blockcount);
4056 mval[-1].br_blockcount += mval->br_blockcount;
4057 } else if (*n > 0 &&
4058 mval->br_startblock == DELAYSTARTBLOCK &&
4059 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4060 mval->br_startoff ==
4061 mval[-1].br_startoff + mval[-1].br_blockcount) {
4062 mval[-1].br_blockcount += mval->br_blockcount;
4063 mval[-1].br_state = mval->br_state;
4064 } else if (!((*n == 0) &&
4065 ((mval->br_startoff + mval->br_blockcount) <=
4066 obno))) {
4067 mval++;
4068 (*n)++;
4069 }
4070 *map = mval;
4071}
4072
4073/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004074 * Map file blocks to filesystem blocks without allocation.
4075 */
4076int
4077xfs_bmapi_read(
4078 struct xfs_inode *ip,
4079 xfs_fileoff_t bno,
4080 xfs_filblks_t len,
4081 struct xfs_bmbt_irec *mval,
4082 int *nmap,
4083 int flags)
4084{
4085 struct xfs_mount *mp = ip->i_mount;
4086 struct xfs_ifork *ifp;
4087 struct xfs_bmbt_irec got;
4088 struct xfs_bmbt_irec prev;
4089 xfs_fileoff_t obno;
4090 xfs_fileoff_t end;
4091 xfs_extnum_t lastx;
4092 int error;
4093 int eof;
4094 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004095 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004096
4097 ASSERT(*nmap >= 1);
4098 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004099 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004100 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004101
4102 if (unlikely(XFS_TEST_ERROR(
4103 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4104 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4105 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4106 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004107 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004108 }
4109
4110 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004111 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004112
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004113 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004114
4115 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004116
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004117 /* No CoW fork? Return a hole. */
4118 if (whichfork == XFS_COW_FORK && !ifp) {
4119 mval->br_startoff = bno;
4120 mval->br_startblock = HOLESTARTBLOCK;
4121 mval->br_blockcount = len;
4122 mval->br_state = XFS_EXT_NORM;
4123 *nmap = 1;
4124 return 0;
4125 }
4126
Dave Chinner5c8ed202011-09-18 20:40:45 +00004127 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4128 error = xfs_iread_extents(NULL, ip, whichfork);
4129 if (error)
4130 return error;
4131 }
4132
4133 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4134 end = bno + len;
4135 obno = bno;
4136
4137 while (bno < end && n < *nmap) {
4138 /* Reading past eof, act as though there's a hole up to end. */
4139 if (eof)
4140 got.br_startoff = end;
4141 if (got.br_startoff > bno) {
4142 /* Reading in a hole. */
4143 mval->br_startoff = bno;
4144 mval->br_startblock = HOLESTARTBLOCK;
4145 mval->br_blockcount =
4146 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4147 mval->br_state = XFS_EXT_NORM;
4148 bno += mval->br_blockcount;
4149 len -= mval->br_blockcount;
4150 mval++;
4151 n++;
4152 continue;
4153 }
4154
4155 /* set up the extent map to return. */
4156 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4157 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4158
4159 /* If we're done, stop now. */
4160 if (bno >= end || n >= *nmap)
4161 break;
4162
4163 /* Else go on to the next record. */
4164 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4165 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4166 else
4167 eof = 1;
4168 }
4169 *nmap = n;
4170 return 0;
4171}
4172
Christoph Hellwig51446f52016-09-19 11:10:21 +10004173int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004174xfs_bmapi_reserve_delalloc(
4175 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004176 int whichfork,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004177 xfs_fileoff_t aoff,
4178 xfs_filblks_t len,
4179 struct xfs_bmbt_irec *got,
4180 struct xfs_bmbt_irec *prev,
4181 xfs_extnum_t *lastx,
4182 int eof)
4183{
4184 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004185 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004186 xfs_extlen_t alen;
4187 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004188 char rt = XFS_IS_REALTIME_INODE(ip);
4189 xfs_extlen_t extsz;
4190 int error;
4191
4192 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4193 if (!eof)
4194 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4195
4196 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004197 if (whichfork == XFS_COW_FORK)
4198 extsz = xfs_get_cowextsz_hint(ip);
4199 else
4200 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004201 if (extsz) {
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004202 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4203 1, 0, &aoff, &alen);
4204 ASSERT(!error);
4205 }
4206
4207 if (rt)
4208 extsz = alen / mp->m_sb.sb_rextsize;
4209
4210 /*
4211 * Make a transaction-less quota reservation for delayed allocation
4212 * blocks. This number gets adjusted later. We return if we haven't
4213 * allocated blocks already inside this loop.
4214 */
4215 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4216 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4217 if (error)
4218 return error;
4219
4220 /*
4221 * Split changing sb for alen and indlen since they could be coming
4222 * from different places.
4223 */
4224 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4225 ASSERT(indlen > 0);
4226
4227 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004228 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004229 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004230 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004231 }
4232
4233 if (error)
4234 goto out_unreserve_quota;
4235
Dave Chinner0d485ad2015-02-23 21:22:03 +11004236 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004237 if (error)
4238 goto out_unreserve_blocks;
4239
4240
4241 ip->i_delayed_blks += alen;
4242
4243 got->br_startoff = aoff;
4244 got->br_startblock = nullstartblock(indlen);
4245 got->br_blockcount = alen;
4246 got->br_state = XFS_EXT_NORM;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004247 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004248
4249 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004250 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4251 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004252 */
4253 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4254
4255 ASSERT(got->br_startoff <= aoff);
4256 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4257 ASSERT(isnullstartblock(got->br_startblock));
4258 ASSERT(got->br_state == XFS_EXT_NORM);
4259 return 0;
4260
4261out_unreserve_blocks:
4262 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004263 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004264 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004265 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004266out_unreserve_quota:
4267 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004268 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004269 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4270 return error;
4271}
4272
Dave Chinnercf11da92014-07-15 07:08:24 +10004273static int
4274xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004275 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004276{
4277 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004278 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004279 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004280 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004281 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004282
Dave Chinnera99ebf42011-12-01 11:24:20 +00004283 ASSERT(bma->length > 0);
4284
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004285 /*
4286 * For the wasdelay case, we could also just allocate the stuff asked
4287 * for in this bmap call but that wouldn't be as good.
4288 */
4289 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004290 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4291 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004292 if (bma->idx != NULLEXTNUM && bma->idx) {
4293 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004294 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004295 }
4296 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004297 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004298 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004299 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004300 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004301 }
4302
4303 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004304 * Set the data type being allocated. For the data fork, the first data
4305 * in the file is treated differently to all other allocations. For the
4306 * attribute fork, we only need to ensure the allocated range is not on
4307 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004308 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004309 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004310 bma->datatype = XFS_ALLOC_NOBUSY;
4311 if (whichfork == XFS_DATA_FORK) {
4312 if (bma->offset == 0)
4313 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4314 else
4315 bma->datatype |= XFS_ALLOC_USERDATA;
4316 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004317 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004318 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004319 }
4320
Dave Chinnere04426b2012-10-05 11:06:59 +10004321 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004322
4323 /*
4324 * Only want to do the alignment at the eof if it is userdata and
4325 * allocation length is larger than a stripe unit.
4326 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004327 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004328 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004329 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004330 if (error)
4331 return error;
4332 }
4333
4334 error = xfs_bmap_alloc(bma);
4335 if (error)
4336 return error;
4337
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004338 if (bma->dfops->dop_low)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004339 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004340 if (bma->cur)
4341 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004342 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004343 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004344 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4345 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4346 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004347 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004348 }
4349 /*
4350 * Bump the number of extents we've allocated
4351 * in this call.
4352 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004353 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004354
Dave Chinner29c8d172011-09-18 20:41:00 +00004355 if (bma->cur)
4356 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004357 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4358
Dave Chinner963c30c2011-09-18 20:40:59 +00004359 bma->got.br_startoff = bma->offset;
4360 bma->got.br_startblock = bma->blkno;
4361 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004362 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004363
4364 /*
4365 * A wasdelay extent has been initialized, so shouldn't be flagged
4366 * as unwritten.
4367 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004368 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004369 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004370 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004371
Christoph Hellwigc6534242011-09-18 20:41:05 +00004372 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004373 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004374 else
4375 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004376
Christoph Hellwigc315c902011-09-18 20:41:02 +00004377 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004378 if (error)
4379 return error;
4380
4381 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004382 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4383 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4384 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004385 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004386 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004387
Dave Chinner963c30c2011-09-18 20:40:59 +00004388 ASSERT(bma->got.br_startoff <= bma->offset);
4389 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4390 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004391 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4392 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004393 return 0;
4394}
4395
Dave Chinnerb447fe52011-09-18 20:40:51 +00004396STATIC int
4397xfs_bmapi_convert_unwritten(
4398 struct xfs_bmalloca *bma,
4399 struct xfs_bmbt_irec *mval,
4400 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004401 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004402{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004403 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004404 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004405 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004406 int error;
4407
Dave Chinnerb447fe52011-09-18 20:40:51 +00004408 /* check if we need to do unwritten->real conversion */
4409 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4410 (flags & XFS_BMAPI_PREALLOC))
4411 return 0;
4412
4413 /* check if we need to do real->unwritten conversion */
4414 if (mval->br_state == XFS_EXT_NORM &&
4415 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4416 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4417 return 0;
4418
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004419 ASSERT(whichfork != XFS_COW_FORK);
4420
Dave Chinnerb447fe52011-09-18 20:40:51 +00004421 /*
4422 * Modify (by adding) the state flag, if writing.
4423 */
4424 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004425 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4426 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004427 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004428 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004429 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004430 }
4431 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4432 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4433
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004434 /*
4435 * Before insertion into the bmbt, zero the range being converted
4436 * if required.
4437 */
4438 if (flags & XFS_BMAPI_ZERO) {
4439 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4440 mval->br_blockcount);
4441 if (error)
4442 return error;
4443 }
4444
Dave Chinnere0c3da52011-09-18 20:41:01 +00004445 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004446 &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004447 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004448 /*
4449 * Log the inode core unconditionally in the unwritten extent conversion
4450 * path because the conversion might not have done so (e.g., if the
4451 * extent count hasn't changed). We need to make sure the inode is dirty
4452 * in the transaction for the sake of fsync(), even if nothing has
4453 * changed, because fsync() will not force the log for this transaction
4454 * unless it sees the inode pinned.
4455 */
4456 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004457 if (error)
4458 return error;
4459
4460 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004461 * Update our extent pointer, given that
4462 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4463 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004464 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004465 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004466
4467 /*
4468 * We may have combined previously unwritten space with written space,
4469 * so generate another request.
4470 */
4471 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004472 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004473 return 0;
4474}
4475
Christoph Hellwig44032802011-09-18 20:40:48 +00004476/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004477 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4478 * extent state if necessary. Details behaviour is controlled by the flags
4479 * parameter. Only allocates blocks from a single allocation group, to avoid
4480 * locking problems.
4481 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 * The returned value in "firstblock" from the first call in a transaction
4483 * must be remembered and presented to subsequent calls in "firstblock".
4484 * An upper bound for the number of blocks to be allocated is supplied to
4485 * the first call in "total"; if no allocation group has that many free
4486 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4487 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004488int
4489xfs_bmapi_write(
4490 struct xfs_trans *tp, /* transaction pointer */
4491 struct xfs_inode *ip, /* incore inode */
4492 xfs_fileoff_t bno, /* starting file offs. mapped */
4493 xfs_filblks_t len, /* length to map in file */
4494 int flags, /* XFS_BMAPI_... */
4495 xfs_fsblock_t *firstblock, /* first allocated block
4496 controls a.g. for allocs */
4497 xfs_extlen_t total, /* total blocks needed */
4498 struct xfs_bmbt_irec *mval, /* output: map values */
4499 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004500 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004502 struct xfs_mount *mp = ip->i_mount;
4503 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004504 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004505 xfs_fileoff_t end; /* end of mapped file region */
4506 int eof; /* after the end of extents */
4507 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004508 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004509 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004510 int whichfork; /* data or attr fork */
4511 char inhole; /* current location is hole in file */
4512 char wasdelay; /* old extent was delayed */
4513
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004515 xfs_fileoff_t orig_bno; /* original block number value */
4516 int orig_flags; /* original flags arg value */
4517 xfs_filblks_t orig_len; /* original value of len arg */
4518 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4519 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
4521 orig_bno = bno;
4522 orig_len = len;
4523 orig_flags = flags;
4524 orig_mval = mval;
4525 orig_nmap = *nmap;
4526#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004527 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004530 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4531 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4532 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004533 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004534 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004535 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004536 ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK);
4537 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP));
4538 ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP));
Darrick J. Wong60b49842016-10-03 09:11:34 -07004539 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || whichfork != XFS_COW_FORK);
4540 ASSERT(!(flags & XFS_BMAPI_CONVERT) || whichfork != XFS_COW_FORK);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004541
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004542 /* zeroing is for currently only for data extents, not metadata */
4543 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4544 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4545 /*
4546 * we can allocate unwritten extents or pre-zero allocated blocks,
4547 * but it makes no sense to do both at once. This would result in
4548 * zeroing the unwritten extent twice, but it still being an
4549 * unwritten extent....
4550 */
4551 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4552 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4553
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 if (unlikely(XFS_TEST_ERROR(
4555 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004556 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004558 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004559 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004563 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004564
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004566
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004567 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004568
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004569 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004571 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004573 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004574 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004575 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004576 }
4577
4578 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4579 error = xfs_iread_extents(tp, ip, whichfork);
4580 if (error)
4581 goto error0;
4582 }
4583
Dave Chinnere0c3da52011-09-18 20:41:01 +00004584 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004585 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 n = 0;
4587 end = bno + len;
4588 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004589
4590 bma.tp = tp;
4591 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004592 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004593 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004594 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004595 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004596
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004598 inhole = eof || bma.got.br_startoff > bno;
4599 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 /*
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004602 * Make sure we only reflink into a hole.
4603 */
4604 if (flags & XFS_BMAPI_REMAP)
4605 ASSERT(inhole);
Darrick J. Wong60b49842016-10-03 09:11:34 -07004606 if (flags & XFS_BMAPI_COWFORK)
4607 ASSERT(!inhole);
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004608
4609 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 * First, deal with the hole before the allocated space
4611 * that we found, if any.
4612 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004613 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004614 bma.eof = eof;
4615 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4616 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004617 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004618 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004619
Dave Chinnera99ebf42011-12-01 11:24:20 +00004620 /*
4621 * There's a 32/64 bit type mismatch between the
4622 * allocation length request (which can be 64 bits in
4623 * length) and the bma length request, which is
4624 * xfs_extlen_t and therefore 32 bits. Hence we have to
4625 * check for 32-bit overflows and handle them here.
4626 */
4627 if (len > (xfs_filblks_t)MAXEXTLEN)
4628 bma.length = MAXEXTLEN;
4629 else
4630 bma.length = len;
4631
4632 ASSERT(len > 0);
4633 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004634 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 if (error)
4636 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004637 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004638 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004639
4640 /*
4641 * If this is a CoW allocation, record the data in
4642 * the refcount btree for orphan recovery.
4643 */
4644 if (whichfork == XFS_COW_FORK) {
4645 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4646 bma.blkno, bma.length);
4647 if (error)
4648 goto error0;
4649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004651
Dave Chinneraef9a892011-09-18 20:40:44 +00004652 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004653 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4654 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655
Dave Chinnerb447fe52011-09-18 20:40:51 +00004656 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004657 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004658 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004659 continue;
4660 if (error)
4661 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
Dave Chinneraef9a892011-09-18 20:40:44 +00004663 /* update the extent map to return */
4664 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4665
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 /*
4667 * If we're done, stop now. Stop when we've allocated
4668 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4669 * the transaction may get too big.
4670 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004671 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004673
4674 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004675 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004676 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4677 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4678 &bma.got);
4679 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00004680 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004683
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 /*
4685 * Transform from btree to extents, give it cur.
4686 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004687 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004688 int tmp_logflags = 0;
4689
Dave Chinner29c8d172011-09-18 20:41:00 +00004690 ASSERT(bma.cur);
4691 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004693 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 if (error)
4695 goto error0;
4696 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004697
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004699 XFS_IFORK_NEXTENTS(ip, whichfork) >
4700 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702error0:
4703 /*
4704 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004705 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004707 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004709 bma.logflags &= ~xfs_ilog_fext(whichfork);
4710 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004712 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 /*
4714 * Log whatever the flags say, even if error. Otherwise we might miss
4715 * detecting a case where the data is changed, there's an error,
4716 * and it's not logged so we don't shutdown when we should.
4717 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004718 if (bma.logflags)
4719 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004720
Dave Chinner29c8d172011-09-18 20:41:00 +00004721 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 if (!error) {
4723 ASSERT(*firstblock == NULLFSBLOCK ||
4724 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4725 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004726 bma.cur->bc_private.b.firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004727 (dfops->dop_low &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 XFS_FSB_TO_AGNO(mp, *firstblock) <
4729 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004730 bma.cur->bc_private.b.firstblock)));
4731 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004733 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4735 }
4736 if (!error)
4737 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4738 orig_nmap, *nmap);
4739 return error;
4740}
4741
4742/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004743 * When a delalloc extent is split (e.g., due to a hole punch), the original
4744 * indlen reservation must be shared across the two new extents that are left
4745 * behind.
4746 *
4747 * Given the original reservation and the worst case indlen for the two new
4748 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004749 * reservation fairly across the two new extents. If necessary, steal available
4750 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4751 * ores == 1). The number of stolen blocks is returned. The availability and
4752 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004753 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004754static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004755xfs_bmap_split_indlen(
4756 xfs_filblks_t ores, /* original res. */
4757 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004758 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4759 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004760{
4761 xfs_filblks_t len1 = *indlen1;
4762 xfs_filblks_t len2 = *indlen2;
4763 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004764 xfs_filblks_t stolen = 0;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004765
4766 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004767 * Steal as many blocks as we can to try and satisfy the worst case
4768 * indlen for both new extents.
4769 */
4770 while (nres > ores && avail) {
4771 nres--;
4772 avail--;
4773 stolen++;
4774 }
4775
4776 /*
4777 * The only blocks available are those reserved for the original
4778 * extent and what we can steal from the extent being removed.
4779 * If this still isn't enough to satisfy the combined
4780 * requirements for the two new extents, skim blocks off of each
4781 * of the new reservations until they match what is available.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004782 */
4783 while (nres > ores) {
4784 if (len1) {
4785 len1--;
4786 nres--;
4787 }
4788 if (nres == ores)
4789 break;
4790 if (len2) {
4791 len2--;
4792 nres--;
4793 }
4794 }
4795
4796 *indlen1 = len1;
4797 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004798
4799 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004800}
4801
4802/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11004803 * Called by xfs_bmapi to update file extent records and the btree
4804 * after removing space (or undoing a delayed allocation).
4805 */
4806STATIC int /* error */
4807xfs_bmap_del_extent(
4808 xfs_inode_t *ip, /* incore inode pointer */
4809 xfs_trans_t *tp, /* current transaction pointer */
4810 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004811 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004812 xfs_btree_cur_t *cur, /* if null, not a btree */
4813 xfs_bmbt_irec_t *del, /* data to remove from extents */
4814 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004815 int whichfork, /* data or attr fork */
4816 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11004817{
4818 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
4819 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
4820 xfs_fsblock_t del_endblock=0; /* first block past del */
4821 xfs_fileoff_t del_endoff; /* first offset past del */
4822 int delay; /* current block is delayed allocated */
4823 int do_fx; /* free extent at end of routine */
4824 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
4825 int error; /* error return value */
4826 int flags; /* inode logging flags */
4827 xfs_bmbt_irec_t got; /* current extent entry */
4828 xfs_fileoff_t got_endoff; /* first offset past got */
4829 int i; /* temp state */
4830 xfs_ifork_t *ifp; /* inode fork pointer */
4831 xfs_mount_t *mp; /* mount structure */
4832 xfs_filblks_t nblks; /* quota/sb block count */
4833 xfs_bmbt_irec_t new; /* new record to be inserted */
4834 /* REFERENCED */
4835 uint qfield; /* quota field to update */
4836 xfs_filblks_t temp; /* for indirect length calculations */
4837 xfs_filblks_t temp2; /* for indirect length calculations */
4838 int state = 0;
4839
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004840 mp = ip->i_mount;
4841 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004842
4843 if (whichfork == XFS_ATTR_FORK)
4844 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004845 else if (whichfork == XFS_COW_FORK)
4846 state |= BMAP_COWFORK;
Dave Chinner9e5987a72013-02-25 12:31:26 +11004847
Dave Chinner9e5987a72013-02-25 12:31:26 +11004848 ifp = XFS_IFORK_PTR(ip, whichfork);
4849 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4850 (uint)sizeof(xfs_bmbt_rec_t)));
4851 ASSERT(del->br_blockcount > 0);
4852 ep = xfs_iext_get_ext(ifp, *idx);
4853 xfs_bmbt_get_all(ep, &got);
4854 ASSERT(got.br_startoff <= del->br_startoff);
4855 del_endoff = del->br_startoff + del->br_blockcount;
4856 got_endoff = got.br_startoff + got.br_blockcount;
4857 ASSERT(got_endoff >= del_endoff);
4858 delay = isnullstartblock(got.br_startblock);
4859 ASSERT(isnullstartblock(del->br_startblock) == delay);
4860 flags = 0;
4861 qfield = 0;
4862 error = 0;
4863 /*
4864 * If deleting a real allocation, must free up the disk space.
4865 */
4866 if (!delay) {
4867 flags = XFS_ILOG_CORE;
4868 /*
4869 * Realtime allocation. Free it and record di_nblocks update.
4870 */
4871 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4872 xfs_fsblock_t bno;
4873 xfs_filblks_t len;
4874
4875 ASSERT(do_mod(del->br_blockcount,
4876 mp->m_sb.sb_rextsize) == 0);
4877 ASSERT(do_mod(del->br_startblock,
4878 mp->m_sb.sb_rextsize) == 0);
4879 bno = del->br_startblock;
4880 len = del->br_blockcount;
4881 do_div(bno, mp->m_sb.sb_rextsize);
4882 do_div(len, mp->m_sb.sb_rextsize);
4883 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4884 if (error)
4885 goto done;
4886 do_fx = 0;
4887 nblks = len * mp->m_sb.sb_rextsize;
4888 qfield = XFS_TRANS_DQ_RTBCOUNT;
4889 }
4890 /*
4891 * Ordinary allocation.
4892 */
4893 else {
4894 do_fx = 1;
4895 nblks = del->br_blockcount;
4896 qfield = XFS_TRANS_DQ_BCOUNT;
4897 }
4898 /*
4899 * Set up del_endblock and cur for later.
4900 */
4901 del_endblock = del->br_startblock + del->br_blockcount;
4902 if (cur) {
4903 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
4904 got.br_startblock, got.br_blockcount,
4905 &i)))
4906 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004907 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004908 }
4909 da_old = da_new = 0;
4910 } else {
4911 da_old = startblockval(got.br_startblock);
4912 da_new = 0;
4913 nblks = 0;
4914 do_fx = 0;
4915 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004916
Dave Chinner9e5987a72013-02-25 12:31:26 +11004917 /*
4918 * Set flag value to use in switch statement.
4919 * Left-contig is 2, right-contig is 1.
4920 */
4921 switch (((got.br_startoff == del->br_startoff) << 1) |
4922 (got_endoff == del_endoff)) {
4923 case 3:
4924 /*
4925 * Matches the whole extent. Delete the entry.
4926 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07004927 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004928 xfs_iext_remove(ip, *idx, 1,
4929 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
4930 --*idx;
4931 if (delay)
4932 break;
4933
4934 XFS_IFORK_NEXT_SET(ip, whichfork,
4935 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4936 flags |= XFS_ILOG_CORE;
4937 if (!cur) {
4938 flags |= xfs_ilog_fext(whichfork);
4939 break;
4940 }
4941 if ((error = xfs_btree_delete(cur, &i)))
4942 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004943 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11004944 break;
4945
4946 case 2:
4947 /*
4948 * Deleting the first part of the extent.
4949 */
4950 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4951 xfs_bmbt_set_startoff(ep, del_endoff);
4952 temp = got.br_blockcount - del->br_blockcount;
4953 xfs_bmbt_set_blockcount(ep, temp);
4954 if (delay) {
4955 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4956 da_old);
4957 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4958 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4959 da_new = temp;
4960 break;
4961 }
4962 xfs_bmbt_set_startblock(ep, del_endblock);
4963 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4964 if (!cur) {
4965 flags |= xfs_ilog_fext(whichfork);
4966 break;
4967 }
4968 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
4969 got.br_blockcount - del->br_blockcount,
4970 got.br_state)))
4971 goto done;
4972 break;
4973
4974 case 1:
4975 /*
4976 * Deleting the last part of the extent.
4977 */
4978 temp = got.br_blockcount - del->br_blockcount;
4979 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4980 xfs_bmbt_set_blockcount(ep, temp);
4981 if (delay) {
4982 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4983 da_old);
4984 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4985 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4986 da_new = temp;
4987 break;
4988 }
4989 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4990 if (!cur) {
4991 flags |= xfs_ilog_fext(whichfork);
4992 break;
4993 }
4994 if ((error = xfs_bmbt_update(cur, got.br_startoff,
4995 got.br_startblock,
4996 got.br_blockcount - del->br_blockcount,
4997 got.br_state)))
4998 goto done;
4999 break;
5000
5001 case 0:
5002 /*
5003 * Deleting the middle of the extent.
5004 */
5005 temp = del->br_startoff - got.br_startoff;
5006 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5007 xfs_bmbt_set_blockcount(ep, temp);
5008 new.br_startoff = del_endoff;
5009 temp2 = got_endoff - del_endoff;
5010 new.br_blockcount = temp2;
5011 new.br_state = got.br_state;
5012 if (!delay) {
5013 new.br_startblock = del_endblock;
5014 flags |= XFS_ILOG_CORE;
5015 if (cur) {
5016 if ((error = xfs_bmbt_update(cur,
5017 got.br_startoff,
5018 got.br_startblock, temp,
5019 got.br_state)))
5020 goto done;
5021 if ((error = xfs_btree_increment(cur, 0, &i)))
5022 goto done;
5023 cur->bc_rec.b = new;
5024 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10005025 if (error && error != -ENOSPC)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005026 goto done;
5027 /*
5028 * If get no-space back from btree insert,
5029 * it tried a split, and we have a zero
5030 * block reservation.
5031 * Fix up our state and return the error.
5032 */
Dave Chinner24513372014-06-25 14:58:08 +10005033 if (error == -ENOSPC) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11005034 /*
5035 * Reset the cursor, don't trust
5036 * it after any insert operation.
5037 */
5038 if ((error = xfs_bmbt_lookup_eq(cur,
5039 got.br_startoff,
5040 got.br_startblock,
5041 temp, &i)))
5042 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005043 XFS_WANT_CORRUPTED_GOTO(mp,
5044 i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005045 /*
5046 * Update the btree record back
5047 * to the original value.
5048 */
5049 if ((error = xfs_bmbt_update(cur,
5050 got.br_startoff,
5051 got.br_startblock,
5052 got.br_blockcount,
5053 got.br_state)))
5054 goto done;
5055 /*
5056 * Reset the extent record back
5057 * to the original value.
5058 */
5059 xfs_bmbt_set_blockcount(ep,
5060 got.br_blockcount);
5061 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005062 error = -ENOSPC;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005063 goto done;
5064 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005065 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005066 } else
5067 flags |= xfs_ilog_fext(whichfork);
5068 XFS_IFORK_NEXT_SET(ip, whichfork,
5069 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5070 } else {
Brian Fosterd34999c2016-03-15 11:42:47 +11005071 xfs_filblks_t stolen;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005072 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005073
5074 /*
5075 * Distribute the original indlen reservation across the
Brian Fosterd34999c2016-03-15 11:42:47 +11005076 * two new extents. Steal blocks from the deleted extent
5077 * if necessary. Stealing blocks simply fudges the
5078 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005079 */
5080 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5081 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c2016-03-15 11:42:47 +11005082 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5083 del->br_blockcount);
5084 da_new = temp + temp2 - stolen;
5085 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005086
5087 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11005088 * Set the reservation for each extent. Warn if either
5089 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005090 */
Brian Fosterd34999c2016-03-15 11:42:47 +11005091 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005092 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005093 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005094 }
5095 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5096 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5097 ++*idx;
5098 break;
5099 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005100
5101 /* remove reverse mapping */
5102 if (!delay) {
5103 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5104 if (error)
5105 goto done;
5106 }
5107
Dave Chinner9e5987a72013-02-25 12:31:26 +11005108 /*
5109 * If we need to, add to list of extents to delete.
5110 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005111 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005112 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5113 error = xfs_refcount_decrease_extent(mp, dfops, del);
5114 if (error)
5115 goto done;
5116 } else
5117 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5118 del->br_blockcount, NULL);
5119 }
5120
Dave Chinner9e5987a72013-02-25 12:31:26 +11005121 /*
5122 * Adjust inode # blocks in the file.
5123 */
5124 if (nblks)
5125 ip->i_d.di_nblocks -= nblks;
5126 /*
5127 * Adjust quota data.
5128 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005129 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005130 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5131
5132 /*
5133 * Account for change in delayed indirect blocks.
5134 * Nothing to do for disk quota accounting here.
5135 */
5136 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005137 if (da_old > da_new)
5138 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005139done:
5140 *logflagsp = flags;
5141 return error;
5142}
5143
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005144/* Remove an extent from the CoW fork. Similar to xfs_bmap_del_extent. */
5145int
5146xfs_bunmapi_cow(
5147 struct xfs_inode *ip,
5148 struct xfs_bmbt_irec *del)
5149{
5150 xfs_filblks_t da_new;
5151 xfs_filblks_t da_old;
5152 xfs_fsblock_t del_endblock = 0;
5153 xfs_fileoff_t del_endoff;
5154 int delay;
5155 struct xfs_bmbt_rec_host *ep;
5156 int error;
5157 struct xfs_bmbt_irec got;
5158 xfs_fileoff_t got_endoff;
5159 struct xfs_ifork *ifp;
5160 struct xfs_mount *mp;
5161 xfs_filblks_t nblks;
5162 struct xfs_bmbt_irec new;
5163 /* REFERENCED */
5164 uint qfield;
5165 xfs_filblks_t temp;
5166 xfs_filblks_t temp2;
5167 int state = BMAP_COWFORK;
5168 int eof;
5169 xfs_extnum_t eidx;
5170
5171 mp = ip->i_mount;
5172 XFS_STATS_INC(mp, xs_del_exlist);
5173
5174 ep = xfs_bmap_search_extents(ip, del->br_startoff, XFS_COW_FORK, &eof,
5175 &eidx, &got, &new);
5176
5177 ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK); ifp = ifp;
5178 ASSERT((eidx >= 0) && (eidx < ifp->if_bytes /
5179 (uint)sizeof(xfs_bmbt_rec_t)));
5180 ASSERT(del->br_blockcount > 0);
5181 ASSERT(got.br_startoff <= del->br_startoff);
5182 del_endoff = del->br_startoff + del->br_blockcount;
5183 got_endoff = got.br_startoff + got.br_blockcount;
5184 ASSERT(got_endoff >= del_endoff);
5185 delay = isnullstartblock(got.br_startblock);
5186 ASSERT(isnullstartblock(del->br_startblock) == delay);
5187 qfield = 0;
5188 error = 0;
5189 /*
5190 * If deleting a real allocation, must free up the disk space.
5191 */
5192 if (!delay) {
5193 nblks = del->br_blockcount;
5194 qfield = XFS_TRANS_DQ_BCOUNT;
5195 /*
5196 * Set up del_endblock and cur for later.
5197 */
5198 del_endblock = del->br_startblock + del->br_blockcount;
5199 da_old = da_new = 0;
5200 } else {
5201 da_old = startblockval(got.br_startblock);
5202 da_new = 0;
5203 nblks = 0;
5204 }
5205 qfield = qfield;
5206 nblks = nblks;
5207
5208 /*
5209 * Set flag value to use in switch statement.
5210 * Left-contig is 2, right-contig is 1.
5211 */
5212 switch (((got.br_startoff == del->br_startoff) << 1) |
5213 (got_endoff == del_endoff)) {
5214 case 3:
5215 /*
5216 * Matches the whole extent. Delete the entry.
5217 */
5218 xfs_iext_remove(ip, eidx, 1, BMAP_COWFORK);
5219 --eidx;
5220 break;
5221
5222 case 2:
5223 /*
5224 * Deleting the first part of the extent.
5225 */
5226 trace_xfs_bmap_pre_update(ip, eidx, state, _THIS_IP_);
5227 xfs_bmbt_set_startoff(ep, del_endoff);
5228 temp = got.br_blockcount - del->br_blockcount;
5229 xfs_bmbt_set_blockcount(ep, temp);
5230 if (delay) {
5231 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5232 da_old);
5233 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5234 trace_xfs_bmap_post_update(ip, eidx, state, _THIS_IP_);
5235 da_new = temp;
5236 break;
5237 }
5238 xfs_bmbt_set_startblock(ep, del_endblock);
5239 trace_xfs_bmap_post_update(ip, eidx, state, _THIS_IP_);
5240 break;
5241
5242 case 1:
5243 /*
5244 * Deleting the last part of the extent.
5245 */
5246 temp = got.br_blockcount - del->br_blockcount;
5247 trace_xfs_bmap_pre_update(ip, eidx, state, _THIS_IP_);
5248 xfs_bmbt_set_blockcount(ep, temp);
5249 if (delay) {
5250 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5251 da_old);
5252 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5253 trace_xfs_bmap_post_update(ip, eidx, state, _THIS_IP_);
5254 da_new = temp;
5255 break;
5256 }
5257 trace_xfs_bmap_post_update(ip, eidx, state, _THIS_IP_);
5258 break;
5259
5260 case 0:
5261 /*
5262 * Deleting the middle of the extent.
5263 */
5264 temp = del->br_startoff - got.br_startoff;
5265 trace_xfs_bmap_pre_update(ip, eidx, state, _THIS_IP_);
5266 xfs_bmbt_set_blockcount(ep, temp);
5267 new.br_startoff = del_endoff;
5268 temp2 = got_endoff - del_endoff;
5269 new.br_blockcount = temp2;
5270 new.br_state = got.br_state;
5271 if (!delay) {
5272 new.br_startblock = del_endblock;
5273 } else {
5274 temp = xfs_bmap_worst_indlen(ip, temp);
5275 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5276 temp2 = xfs_bmap_worst_indlen(ip, temp2);
5277 new.br_startblock = nullstartblock((int)temp2);
5278 da_new = temp + temp2;
5279 while (da_new > da_old) {
5280 if (temp) {
5281 temp--;
5282 da_new--;
5283 xfs_bmbt_set_startblock(ep,
5284 nullstartblock((int)temp));
5285 }
5286 if (da_new == da_old)
5287 break;
5288 if (temp2) {
5289 temp2--;
5290 da_new--;
5291 new.br_startblock =
5292 nullstartblock((int)temp2);
5293 }
5294 }
5295 }
5296 trace_xfs_bmap_post_update(ip, eidx, state, _THIS_IP_);
5297 xfs_iext_insert(ip, eidx + 1, 1, &new, state);
5298 ++eidx;
5299 break;
5300 }
5301
5302 /*
5303 * Account for change in delayed indirect blocks.
5304 * Nothing to do for disk quota accounting here.
5305 */
5306 ASSERT(da_old >= da_new);
5307 if (da_old > da_new)
5308 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
5309
5310 return error;
5311}
5312
Dave Chinner9e5987a72013-02-25 12:31:26 +11005313/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 * Unmap (remove) blocks from a file.
5315 * If nexts is nonzero then the number of extents to remove is limited to
5316 * that value. If not all extents in the block range can be removed then
5317 * *done is set.
5318 */
5319int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005320__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 xfs_trans_t *tp, /* transaction pointer */
5322 struct xfs_inode *ip, /* incore inode */
5323 xfs_fileoff_t bno, /* starting offset to unmap */
Darrick J. Wong44535932016-10-03 09:11:29 -07005324 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 int flags, /* misc flags */
5326 xfs_extnum_t nexts, /* number of extents max */
5327 xfs_fsblock_t *firstblock, /* first allocated block
5328 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005329 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330{
5331 xfs_btree_cur_t *cur; /* bmap btree cursor */
5332 xfs_bmbt_irec_t del; /* extent being deleted */
5333 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005334 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 int error; /* error return value */
5336 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005337 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 xfs_ifork_t *ifp; /* inode fork pointer */
5339 int isrt; /* freeing in rt area */
5340 xfs_extnum_t lastx; /* last extent index used */
5341 int logflags; /* transaction logging flags */
5342 xfs_extlen_t mod; /* rt extent offset */
5343 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005344 xfs_extnum_t nextents; /* number of file extents */
5345 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 xfs_fileoff_t start; /* first file offset deleted */
5347 int tmp_logflags; /* partial logging flags */
5348 int wasdel; /* was a delayed alloc extent */
5349 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005351 xfs_filblks_t len = *rlen; /* length to unmap in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005353 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5354
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005355 whichfork = xfs_bmapi_whichfork(flags);
5356 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 ifp = XFS_IFORK_PTR(ip, whichfork);
5358 if (unlikely(
5359 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5360 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5361 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5362 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005363 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 }
5365 mp = ip->i_mount;
5366 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005367 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005368
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005369 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 ASSERT(len > 0);
5371 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005372
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5374 (error = xfs_iread_extents(tp, ip, whichfork)))
5375 return error;
5376 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5377 if (nextents == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005378 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 return 0;
5380 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005381 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005382 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 start = bno;
5384 bno = start + len - 1;
5385 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5386 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005387
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 /*
5389 * Check to see if the given block number is past the end of the
5390 * file, back up to the last block if so...
5391 */
5392 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005393 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 xfs_bmbt_get_all(ep, &got);
5395 bno = got.br_startoff + got.br_blockcount - 1;
5396 }
5397 logflags = 0;
5398 if (ifp->if_flags & XFS_IFBROOT) {
5399 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005400 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005402 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005403 cur->bc_private.b.flags = 0;
5404 } else
5405 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005406
5407 if (isrt) {
5408 /*
5409 * Synchronize by locking the bitmap inode.
5410 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005411 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005412 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005413 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5414 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005415 }
5416
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 extno = 0;
5418 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5419 (nexts == 0 || extno < nexts)) {
5420 /*
5421 * Is the found extent after a hole in which bno lives?
5422 * Just back up to the previous extent, if so.
5423 */
5424 if (got.br_startoff > bno) {
5425 if (--lastx < 0)
5426 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005427 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 xfs_bmbt_get_all(ep, &got);
5429 }
5430 /*
5431 * Is the last block of this extent before the range
5432 * we're supposed to delete? If so, we're done.
5433 */
5434 bno = XFS_FILEOFF_MIN(bno,
5435 got.br_startoff + got.br_blockcount - 1);
5436 if (bno < start)
5437 break;
5438 /*
5439 * Then deal with the (possibly delayed) allocated space
5440 * we found.
5441 */
5442 ASSERT(ep != NULL);
5443 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005444 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005445 if (got.br_startoff < start) {
5446 del.br_startoff = start;
5447 del.br_blockcount -= start - got.br_startoff;
5448 if (!wasdel)
5449 del.br_startblock += start - got.br_startoff;
5450 }
5451 if (del.br_startoff + del.br_blockcount > bno + 1)
5452 del.br_blockcount = bno + 1 - del.br_startoff;
5453 sum = del.br_startblock + del.br_blockcount;
5454 if (isrt &&
5455 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5456 /*
5457 * Realtime extent not lined up at the end.
5458 * The extent could have been split into written
5459 * and unwritten pieces, or we could just be
5460 * unmapping part of it. But we can't really
5461 * get rid of part of a realtime extent.
5462 */
5463 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005464 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 /*
5466 * This piece is unwritten, or we're not
5467 * using unwritten extents. Skip over it.
5468 */
5469 ASSERT(bno >= mod);
5470 bno -= mod > del.br_blockcount ?
5471 del.br_blockcount : mod;
5472 if (bno < got.br_startoff) {
5473 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005474 xfs_bmbt_get_all(xfs_iext_get_ext(
5475 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005476 }
5477 continue;
5478 }
5479 /*
5480 * It's written, turn it unwritten.
5481 * This is better than zeroing it.
5482 */
5483 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005484 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 /*
5486 * If this spans a realtime extent boundary,
5487 * chop it back to the start of the one we end at.
5488 */
5489 if (del.br_blockcount > mod) {
5490 del.br_startoff += del.br_blockcount - mod;
5491 del.br_startblock += del.br_blockcount - mod;
5492 del.br_blockcount = mod;
5493 }
5494 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005495 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005496 &lastx, &cur, &del, firstblock, dfops,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005497 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 if (error)
5499 goto error0;
5500 goto nodelete;
5501 }
5502 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5503 /*
5504 * Realtime extent is lined up at the end but not
5505 * at the front. We'll get rid of full extents if
5506 * we can.
5507 */
5508 mod = mp->m_sb.sb_rextsize - mod;
5509 if (del.br_blockcount > mod) {
5510 del.br_blockcount -= mod;
5511 del.br_startoff += mod;
5512 del.br_startblock += mod;
5513 } else if ((del.br_startoff == start &&
5514 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005515 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005516 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 /*
5518 * Can't make it unwritten. There isn't
5519 * a full extent here so just skip it.
5520 */
5521 ASSERT(bno >= del.br_blockcount);
5522 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005523 if (got.br_startoff > bno) {
5524 if (--lastx >= 0) {
5525 ep = xfs_iext_get_ext(ifp,
5526 lastx);
5527 xfs_bmbt_get_all(ep, &got);
5528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 }
5530 continue;
5531 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5532 /*
5533 * This one is already unwritten.
5534 * It must have a written left neighbor.
5535 * Unwrite the killed part of that one and
5536 * try again.
5537 */
5538 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005539 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5540 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005542 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 ASSERT(del.br_startblock ==
5544 prev.br_startblock + prev.br_blockcount);
5545 if (prev.br_startoff < start) {
5546 mod = start - prev.br_startoff;
5547 prev.br_blockcount -= mod;
5548 prev.br_startblock += mod;
5549 prev.br_startoff = start;
5550 }
5551 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005552 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005553 error = xfs_bmap_add_extent_unwritten_real(tp,
5554 ip, &lastx, &cur, &prev,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005555 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 if (error)
5557 goto error0;
5558 goto nodelete;
5559 } else {
5560 ASSERT(del.br_state == XFS_EXT_NORM);
5561 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005562 error = xfs_bmap_add_extent_unwritten_real(tp,
5563 ip, &lastx, &cur, &del,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005564 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 if (error)
5566 goto error0;
5567 goto nodelete;
5568 }
5569 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005570
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 /*
5572 * If it's the case where the directory code is running
5573 * with no block reservation, and the deleted block is in
5574 * the middle of its extent, and the resulting insert
5575 * of an extent would cause transformation to btree format,
5576 * then reject it. The calling code will then swap
5577 * blocks around instead.
5578 * We have to do this now, rather than waiting for the
5579 * conversion to btree format, since the transaction
5580 * will be dirty.
5581 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005582 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005584 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5585 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005586 del.br_startoff > got.br_startoff &&
5587 del.br_startoff + del.br_blockcount <
5588 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005589 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 goto error0;
5591 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005592
5593 /*
5594 * Unreserve quota and update realtime free space, if
5595 * appropriate. If delayed allocation, update the inode delalloc
5596 * counter now and wait to update the sb counters as
5597 * xfs_bmap_del_extent() might need to borrow some blocks.
5598 */
5599 if (wasdel) {
5600 ASSERT(startblockval(del.br_startblock) > 0);
5601 if (isrt) {
5602 xfs_filblks_t rtexts;
5603
5604 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5605 do_div(rtexts, mp->m_sb.sb_rextsize);
5606 xfs_mod_frextents(mp, (int64_t)rtexts);
5607 (void)xfs_trans_reserve_quota_nblks(NULL,
5608 ip, -((long)del.br_blockcount), 0,
5609 XFS_QMOPT_RES_RTBLKS);
5610 } else {
5611 (void)xfs_trans_reserve_quota_nblks(NULL,
5612 ip, -((long)del.br_blockcount), 0,
5613 XFS_QMOPT_RES_REGBLKS);
5614 }
5615 ip->i_delayed_blks -= del.br_blockcount;
5616 if (cur)
5617 cur->bc_private.b.flags |=
5618 XFS_BTCUR_BPRV_WASDEL;
5619 } else if (cur)
5620 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5621
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005622 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005623 &tmp_logflags, whichfork, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 logflags |= tmp_logflags;
5625 if (error)
5626 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005627
5628 if (!isrt && wasdel)
5629 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5630
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 bno = del.br_startoff - 1;
5632nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 /*
5634 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005637 if (lastx >= 0) {
5638 ep = xfs_iext_get_ext(ifp, lastx);
5639 if (xfs_bmbt_get_startoff(ep) > bno) {
5640 if (--lastx >= 0)
5641 ep = xfs_iext_get_ext(ifp,
5642 lastx);
5643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646 extno++;
5647 }
5648 }
Darrick J. Wong44535932016-10-03 09:11:29 -07005649 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5650 *rlen = 0;
5651 else
5652 *rlen = bno - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005653
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 /*
5655 * Convert to a btree if necessary.
5656 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005657 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005659 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 &cur, 0, &tmp_logflags, whichfork);
5661 logflags |= tmp_logflags;
5662 if (error)
5663 goto error0;
5664 }
5665 /*
5666 * transform from btree to extents, give it cur
5667 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005668 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 ASSERT(cur != NULL);
5670 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5671 whichfork);
5672 logflags |= tmp_logflags;
5673 if (error)
5674 goto error0;
5675 }
5676 /*
5677 * transform from extents to local?
5678 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 error = 0;
5680error0:
5681 /*
5682 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005683 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005685 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005687 logflags &= ~xfs_ilog_fext(whichfork);
5688 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005689 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005690 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 /*
5692 * Log inode even in the error case, if the transaction
5693 * is dirty we'll need to shut down the filesystem.
5694 */
5695 if (logflags)
5696 xfs_trans_log_inode(tp, ip, logflags);
5697 if (cur) {
5698 if (!error) {
5699 *firstblock = cur->bc_private.b.firstblock;
5700 cur->bc_private.b.allocated = 0;
5701 }
5702 xfs_btree_del_cursor(cur,
5703 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5704 }
5705 return error;
5706}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005707
Darrick J. Wong44535932016-10-03 09:11:29 -07005708/* Unmap a range of a file. */
5709int
5710xfs_bunmapi(
5711 xfs_trans_t *tp,
5712 struct xfs_inode *ip,
5713 xfs_fileoff_t bno,
5714 xfs_filblks_t len,
5715 int flags,
5716 xfs_extnum_t nexts,
5717 xfs_fsblock_t *firstblock,
5718 struct xfs_defer_ops *dfops,
5719 int *done)
5720{
5721 int error;
5722
5723 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5724 dfops);
5725 *done = (len == 0);
5726 return error;
5727}
5728
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005729/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005730 * Determine whether an extent shift can be accomplished by a merge with the
5731 * extent that precedes the target hole of the shift.
5732 */
5733STATIC bool
5734xfs_bmse_can_merge(
5735 struct xfs_bmbt_irec *left, /* preceding extent */
5736 struct xfs_bmbt_irec *got, /* current extent to shift */
5737 xfs_fileoff_t shift) /* shift fsb */
5738{
5739 xfs_fileoff_t startoff;
5740
5741 startoff = got->br_startoff - shift;
5742
5743 /*
5744 * The extent, once shifted, must be adjacent in-file and on-disk with
5745 * the preceding extent.
5746 */
5747 if ((left->br_startoff + left->br_blockcount != startoff) ||
5748 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5749 (left->br_state != got->br_state) ||
5750 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5751 return false;
5752
5753 return true;
5754}
5755
5756/*
5757 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5758 * hole in the file. If an extent shift would result in the extent being fully
5759 * adjacent to the extent that currently precedes the hole, we can merge with
5760 * the preceding extent rather than do the shift.
5761 *
5762 * This function assumes the caller has verified a shift-by-merge is possible
5763 * with the provided extents via xfs_bmse_can_merge().
5764 */
5765STATIC int
5766xfs_bmse_merge(
5767 struct xfs_inode *ip,
5768 int whichfork,
5769 xfs_fileoff_t shift, /* shift fsb */
5770 int current_ext, /* idx of gotp */
5771 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5772 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5773 struct xfs_btree_cur *cur,
5774 int *logflags) /* output */
5775{
Brian Fosterddb19e32014-09-23 15:38:09 +10005776 struct xfs_bmbt_irec got;
5777 struct xfs_bmbt_irec left;
5778 xfs_filblks_t blockcount;
5779 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005780 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005781
Brian Fosterddb19e32014-09-23 15:38:09 +10005782 xfs_bmbt_get_all(gotp, &got);
5783 xfs_bmbt_get_all(leftp, &left);
5784 blockcount = left.br_blockcount + got.br_blockcount;
5785
5786 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5787 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5788 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5789
5790 /*
5791 * Merge the in-core extents. Note that the host record pointers and
5792 * current_ext index are invalid once the extent has been removed via
5793 * xfs_iext_remove().
5794 */
5795 xfs_bmbt_set_blockcount(leftp, blockcount);
5796 xfs_iext_remove(ip, current_ext, 1, 0);
5797
5798 /*
5799 * Update the on-disk extent count, the btree if necessary and log the
5800 * inode.
5801 */
5802 XFS_IFORK_NEXT_SET(ip, whichfork,
5803 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5804 *logflags |= XFS_ILOG_CORE;
5805 if (!cur) {
5806 *logflags |= XFS_ILOG_DEXT;
5807 return 0;
5808 }
5809
5810 /* lookup and remove the extent to merge */
5811 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5812 got.br_blockcount, &i);
5813 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005814 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005815 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005816
5817 error = xfs_btree_delete(cur, &i);
5818 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005819 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005820 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005821
5822 /* lookup and update size of the previous extent */
5823 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5824 left.br_blockcount, &i);
5825 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005826 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005827 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005828
5829 left.br_blockcount = blockcount;
5830
Dave Chinner4db431f2014-12-04 09:42:40 +11005831 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5832 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005833}
5834
5835/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005836 * Shift a single extent.
5837 */
5838STATIC int
5839xfs_bmse_shift_one(
5840 struct xfs_inode *ip,
5841 int whichfork,
5842 xfs_fileoff_t offset_shift_fsb,
5843 int *current_ext,
5844 struct xfs_bmbt_rec_host *gotp,
5845 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005846 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005847 enum shift_direction direction,
5848 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005849{
5850 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005851 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005852 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005853 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005854 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005855 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005856 int error;
5857 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005858 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005859
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005860 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005861 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005862 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Brian Fostera979bdf2014-09-23 15:39:04 +10005863
5864 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005865
Brian Fosterf71721d2014-09-23 15:39:05 +10005866 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005867 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005868
Namjae Jeona904b1c2015-03-25 15:08:56 +11005869 if (direction == SHIFT_LEFT) {
5870 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005871
Namjae Jeona904b1c2015-03-25 15:08:56 +11005872 /*
5873 * Check for merge if we've got an extent to the left,
5874 * otherwise make sure there's enough room at the start
5875 * of the file for the shift.
5876 */
5877 if (!*current_ext) {
5878 if (got.br_startoff < offset_shift_fsb)
5879 return -EINVAL;
5880 goto update_current_ext;
5881 }
5882 /*
5883 * grab the left extent and check for a large
5884 * enough hole.
5885 */
5886 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5887 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5888
5889 if (startoff <
5890 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005891 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005892
Dave Chinnerb11bd672014-12-04 09:42:24 +11005893 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005894 if (xfs_bmse_can_merge(&adj_irec, &got,
5895 offset_shift_fsb)) {
Darrick J. Wong9c194642016-08-03 12:16:05 +10005896 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5897 *current_ext, gotp, adj_irecp,
5898 cur, logflags);
5899 if (error)
5900 return error;
5901 adj_irec = got;
5902 goto update_rmap;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005903 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005904 } else {
5905 startoff = got.br_startoff + offset_shift_fsb;
5906 /* nothing to move if this is the last extent */
5907 if (*current_ext >= (total_extents - 1))
5908 goto update_current_ext;
5909 /*
5910 * If this is not the last extent in the file, make sure there
5911 * is enough room between current extent and next extent for
5912 * accommodating the shift.
5913 */
5914 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5915 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5916 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5917 return -EINVAL;
5918 /*
5919 * Unlike a left shift (which involves a hole punch),
5920 * a right shift does not modify extent neighbors
5921 * in any way. We should never find mergeable extents
5922 * in this scenario. Check anyways and warn if we
5923 * encounter two extents that could be one.
5924 */
5925 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5926 WARN_ON_ONCE(1);
5927 }
Brian Fostera979bdf2014-09-23 15:39:04 +10005928 /*
5929 * Increment the extent index for the next iteration, update the start
5930 * offset of the in-core extent and update the btree if applicable.
5931 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005932update_current_ext:
5933 if (direction == SHIFT_LEFT)
5934 (*current_ext)++;
5935 else
5936 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005937 xfs_bmbt_set_startoff(gotp, startoff);
5938 *logflags |= XFS_ILOG_CORE;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005939 adj_irec = got;
Brian Fostera979bdf2014-09-23 15:39:04 +10005940 if (!cur) {
5941 *logflags |= XFS_ILOG_DEXT;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005942 goto update_rmap;
Brian Fostera979bdf2014-09-23 15:39:04 +10005943 }
5944
5945 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5946 got.br_blockcount, &i);
5947 if (error)
5948 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005949 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10005950
5951 got.br_startoff = startoff;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005952 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5953 got.br_blockcount, got.br_state);
5954 if (error)
5955 return error;
5956
5957update_rmap:
5958 /* update reverse mapping */
5959 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
5960 if (error)
5961 return error;
5962 adj_irec.br_startoff = startoff;
5963 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
Brian Fostera979bdf2014-09-23 15:39:04 +10005964}
5965
5966/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005967 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005968 *
Brian Foster2c845f52014-09-23 15:37:09 +10005969 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005970 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10005971 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5972 * is the length by which each extent is shifted. If there is no hole to shift
5973 * the extents into, this will be considered invalid operation and we abort
5974 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005975 */
5976int
5977xfs_bmap_shift_extents(
5978 struct xfs_trans *tp,
5979 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005980 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005981 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005982 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005983 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005984 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005985 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005986 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005987 int num_exts)
5988{
Brian Fosterca446d82014-09-02 12:12:53 +10005989 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005990 struct xfs_bmbt_rec_host *gotp;
5991 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005992 struct xfs_mount *mp = ip->i_mount;
5993 struct xfs_ifork *ifp;
5994 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005995 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005996 xfs_extnum_t total_extents;
5997 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005998 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005999 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10006000 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006001
6002 if (unlikely(XFS_TEST_ERROR(
6003 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6004 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6005 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6006 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6007 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10006008 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006009 }
6010
6011 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10006012 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006013
Brian Foster2c845f52014-09-23 15:37:09 +10006014 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6015 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006016 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6017 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006018
6019 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006020 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6021 /* Read in all the extents */
6022 error = xfs_iread_extents(tp, ip, whichfork);
6023 if (error)
6024 return error;
6025 }
6026
Brian Fosterddb19e32014-09-23 15:38:09 +10006027 if (ifp->if_flags & XFS_IFBROOT) {
6028 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6029 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006030 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006031 cur->bc_private.b.flags = 0;
6032 }
6033
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006034 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006035 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006036 * are collapsing out, so we cannot use the count of real extents here.
6037 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006038 */
6039 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006040 if (total_extents == 0) {
6041 *done = 1;
6042 goto del_cursor;
6043 }
6044
6045 /*
6046 * In case of first right shift, we need to initialize next_fsb
6047 */
6048 if (*next_fsb == NULLFSBLOCK) {
6049 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6050 xfs_bmbt_get_all(gotp, &got);
6051 *next_fsb = got.br_startoff;
6052 if (stop_fsb > *next_fsb) {
6053 *done = 1;
6054 goto del_cursor;
6055 }
6056 }
6057
6058 /* Lookup the extent index at which we have to stop */
6059 if (direction == SHIFT_RIGHT) {
6060 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6061 /* Make stop_extent exclusive of shift range */
6062 stop_extent--;
6063 } else
6064 stop_extent = total_extents;
6065
6066 /*
6067 * Look up the extent index for the fsb where we start shifting. We can
6068 * henceforth iterate with current_ext as extent list changes are locked
6069 * out via ilock.
6070 *
6071 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6072 * *next_fsb lies in a hole beyond which there are no extents. Either
6073 * way, we are done.
6074 */
6075 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
6076 if (!gotp) {
6077 *done = 1;
6078 goto del_cursor;
6079 }
6080
6081 /* some sanity checking before we finally start shifting extents */
6082 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6083 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6084 error = -EIO;
6085 goto del_cursor;
6086 }
6087
6088 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006089 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006090 &current_ext, gotp, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006091 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006092 if (error)
6093 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006094 /*
6095 * If there was an extent merge during the shift, the extent
6096 * count can change. Update the total and grade the next record.
6097 */
6098 if (direction == SHIFT_LEFT) {
6099 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
6100 stop_extent = total_extents;
6101 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006102
Namjae Jeona904b1c2015-03-25 15:08:56 +11006103 if (current_ext == stop_extent) {
6104 *done = 1;
6105 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006106 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006107 }
Brian Fosterddb19e32014-09-23 15:38:09 +10006108 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006109 }
6110
Namjae Jeona904b1c2015-03-25 15:08:56 +11006111 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006112 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10006113 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10006114 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006115
6116del_cursor:
6117 if (cur)
6118 xfs_btree_del_cursor(cur,
6119 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6120
Brian Fosterca446d82014-09-02 12:12:53 +10006121 if (logflags)
6122 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006123
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006124 return error;
6125}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006126
6127/*
6128 * Splits an extent into two extents at split_fsb block such that it is
6129 * the first block of the current_ext. @current_ext is a target extent
6130 * to be split. @split_fsb is a block where the extents is split.
6131 * If split_fsb lies in a hole or the first block of extents, just return 0.
6132 */
6133STATIC int
6134xfs_bmap_split_extent_at(
6135 struct xfs_trans *tp,
6136 struct xfs_inode *ip,
6137 xfs_fileoff_t split_fsb,
6138 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006139 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006140{
6141 int whichfork = XFS_DATA_FORK;
6142 struct xfs_btree_cur *cur = NULL;
6143 struct xfs_bmbt_rec_host *gotp;
6144 struct xfs_bmbt_irec got;
6145 struct xfs_bmbt_irec new; /* split extent */
6146 struct xfs_mount *mp = ip->i_mount;
6147 struct xfs_ifork *ifp;
6148 xfs_fsblock_t gotblkcnt; /* new block count for got */
6149 xfs_extnum_t current_ext;
6150 int error = 0;
6151 int logflags = 0;
6152 int i = 0;
6153
6154 if (unlikely(XFS_TEST_ERROR(
6155 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6156 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6157 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6158 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6159 XFS_ERRLEVEL_LOW, mp);
6160 return -EFSCORRUPTED;
6161 }
6162
6163 if (XFS_FORCED_SHUTDOWN(mp))
6164 return -EIO;
6165
6166 ifp = XFS_IFORK_PTR(ip, whichfork);
6167 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6168 /* Read in all the extents */
6169 error = xfs_iread_extents(tp, ip, whichfork);
6170 if (error)
6171 return error;
6172 }
6173
6174 /*
6175 * gotp can be null in 2 cases: 1) if there are no extents
6176 * or 2) split_fsb lies in a hole beyond which there are
6177 * no extents. Either way, we are done.
6178 */
6179 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
6180 if (!gotp)
6181 return 0;
6182
6183 xfs_bmbt_get_all(gotp, &got);
6184
6185 /*
6186 * Check split_fsb lies in a hole or the start boundary offset
6187 * of the extent.
6188 */
6189 if (got.br_startoff >= split_fsb)
6190 return 0;
6191
6192 gotblkcnt = split_fsb - got.br_startoff;
6193 new.br_startoff = split_fsb;
6194 new.br_startblock = got.br_startblock + gotblkcnt;
6195 new.br_blockcount = got.br_blockcount - gotblkcnt;
6196 new.br_state = got.br_state;
6197
6198 if (ifp->if_flags & XFS_IFBROOT) {
6199 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6200 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006201 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006202 cur->bc_private.b.flags = 0;
6203 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6204 got.br_startblock,
6205 got.br_blockcount,
6206 &i);
6207 if (error)
6208 goto del_cursor;
6209 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6210 }
6211
6212 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6213 got.br_blockcount = gotblkcnt;
6214
6215 logflags = XFS_ILOG_CORE;
6216 if (cur) {
6217 error = xfs_bmbt_update(cur, got.br_startoff,
6218 got.br_startblock,
6219 got.br_blockcount,
6220 got.br_state);
6221 if (error)
6222 goto del_cursor;
6223 } else
6224 logflags |= XFS_ILOG_DEXT;
6225
6226 /* Add new extent */
6227 current_ext++;
6228 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6229 XFS_IFORK_NEXT_SET(ip, whichfork,
6230 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6231
6232 if (cur) {
6233 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6234 new.br_startblock, new.br_blockcount,
6235 &i);
6236 if (error)
6237 goto del_cursor;
6238 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6239 cur->bc_rec.b.br_state = new.br_state;
6240
6241 error = xfs_btree_insert(cur, &i);
6242 if (error)
6243 goto del_cursor;
6244 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6245 }
6246
6247 /*
6248 * Convert to a btree if necessary.
6249 */
6250 if (xfs_bmap_needs_btree(ip, whichfork)) {
6251 int tmp_logflags; /* partial log flag return val */
6252
6253 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006254 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006255 &cur, 0, &tmp_logflags, whichfork);
6256 logflags |= tmp_logflags;
6257 }
6258
6259del_cursor:
6260 if (cur) {
6261 cur->bc_private.b.allocated = 0;
6262 xfs_btree_del_cursor(cur,
6263 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6264 }
6265
6266 if (logflags)
6267 xfs_trans_log_inode(tp, ip, logflags);
6268 return error;
6269}
6270
6271int
6272xfs_bmap_split_extent(
6273 struct xfs_inode *ip,
6274 xfs_fileoff_t split_fsb)
6275{
6276 struct xfs_mount *mp = ip->i_mount;
6277 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006278 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006279 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006280 int error;
6281
Christoph Hellwig253f4912016-04-06 09:19:55 +10006282 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6283 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6284 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006285 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006286
6287 xfs_ilock(ip, XFS_ILOCK_EXCL);
6288 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6289
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006290 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006291
6292 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006293 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006294 if (error)
6295 goto out;
6296
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006297 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006298 if (error)
6299 goto out;
6300
Christoph Hellwig70393312015-06-04 13:48:08 +10006301 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006302
6303out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006304 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006305 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006306 return error;
6307}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006308
6309/* Deferred mapping is only for real extents in the data fork. */
6310static bool
6311xfs_bmap_is_update_needed(
6312 struct xfs_bmbt_irec *bmap)
6313{
6314 return bmap->br_startblock != HOLESTARTBLOCK &&
6315 bmap->br_startblock != DELAYSTARTBLOCK;
6316}
6317
6318/* Record a bmap intent. */
6319static int
6320__xfs_bmap_add(
6321 struct xfs_mount *mp,
6322 struct xfs_defer_ops *dfops,
6323 enum xfs_bmap_intent_type type,
6324 struct xfs_inode *ip,
6325 int whichfork,
6326 struct xfs_bmbt_irec *bmap)
6327{
6328 int error;
6329 struct xfs_bmap_intent *bi;
6330
6331 trace_xfs_bmap_defer(mp,
6332 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6333 type,
6334 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6335 ip->i_ino, whichfork,
6336 bmap->br_startoff,
6337 bmap->br_blockcount,
6338 bmap->br_state);
6339
6340 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6341 INIT_LIST_HEAD(&bi->bi_list);
6342 bi->bi_type = type;
6343 bi->bi_owner = ip;
6344 bi->bi_whichfork = whichfork;
6345 bi->bi_bmap = *bmap;
6346
6347 error = xfs_defer_join(dfops, bi->bi_owner);
6348 if (error) {
6349 kmem_free(bi);
6350 return error;
6351 }
6352
6353 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6354 return 0;
6355}
6356
6357/* Map an extent into a file. */
6358int
6359xfs_bmap_map_extent(
6360 struct xfs_mount *mp,
6361 struct xfs_defer_ops *dfops,
6362 struct xfs_inode *ip,
6363 struct xfs_bmbt_irec *PREV)
6364{
6365 if (!xfs_bmap_is_update_needed(PREV))
6366 return 0;
6367
6368 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6369 XFS_DATA_FORK, PREV);
6370}
6371
6372/* Unmap an extent out of a file. */
6373int
6374xfs_bmap_unmap_extent(
6375 struct xfs_mount *mp,
6376 struct xfs_defer_ops *dfops,
6377 struct xfs_inode *ip,
6378 struct xfs_bmbt_irec *PREV)
6379{
6380 if (!xfs_bmap_is_update_needed(PREV))
6381 return 0;
6382
6383 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6384 XFS_DATA_FORK, PREV);
6385}
6386
6387/*
6388 * Process one of the deferred bmap operations. We pass back the
6389 * btree cursor to maintain our lock on the bmapbt between calls.
6390 */
6391int
6392xfs_bmap_finish_one(
6393 struct xfs_trans *tp,
6394 struct xfs_defer_ops *dfops,
6395 struct xfs_inode *ip,
6396 enum xfs_bmap_intent_type type,
6397 int whichfork,
6398 xfs_fileoff_t startoff,
6399 xfs_fsblock_t startblock,
6400 xfs_filblks_t blockcount,
6401 xfs_exntst_t state)
6402{
6403 struct xfs_bmbt_irec bmap;
6404 int nimaps = 1;
6405 xfs_fsblock_t firstfsb;
6406 int flags = XFS_BMAPI_REMAP;
6407 int done;
6408 int error = 0;
6409
6410 bmap.br_startblock = startblock;
6411 bmap.br_startoff = startoff;
6412 bmap.br_blockcount = blockcount;
6413 bmap.br_state = state;
6414
6415 trace_xfs_bmap_deferred(tp->t_mountp,
6416 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6417 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6418 ip->i_ino, whichfork, startoff, blockcount, state);
6419
6420 if (whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK)
6421 return -EFSCORRUPTED;
6422 if (whichfork == XFS_ATTR_FORK)
6423 flags |= XFS_BMAPI_ATTRFORK;
6424
6425 if (XFS_TEST_ERROR(false, tp->t_mountp,
6426 XFS_ERRTAG_BMAP_FINISH_ONE,
6427 XFS_RANDOM_BMAP_FINISH_ONE))
6428 return -EIO;
6429
6430 switch (type) {
6431 case XFS_BMAP_MAP:
6432 firstfsb = bmap.br_startblock;
6433 error = xfs_bmapi_write(tp, ip, bmap.br_startoff,
6434 bmap.br_blockcount, flags, &firstfsb,
6435 bmap.br_blockcount, &bmap, &nimaps,
6436 dfops);
6437 break;
6438 case XFS_BMAP_UNMAP:
6439 error = xfs_bunmapi(tp, ip, bmap.br_startoff,
6440 bmap.br_blockcount, flags, 1, &firstfsb,
6441 dfops, &done);
6442 ASSERT(done);
6443 break;
6444 default:
6445 ASSERT(0);
6446 error = -EFSCORRUPTED;
6447 }
6448
6449 return error;
6450}