blob: 6af34b734ce874d74d2fa48dca119ff903b4c2e3 [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);
Eric Sandeen5d829302016-11-08 12:59:42 +1100518 ASSERT(cnt == xfs_iext_count(ifp));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100519 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) {
Darrick J. Wong90e20562016-10-03 09:11:45 -0700756try_another_ag:
Dave Chinner9e5987a72013-02-25 12:31:26 +1100757 args.type = XFS_ALLOCTYPE_START_BNO;
758 args.fsbno = *firstblock;
759 } else {
760 args.type = XFS_ALLOCTYPE_NEAR_BNO;
761 args.fsbno = *firstblock;
762 }
763 args.minlen = args.maxlen = args.prod = 1;
764 args.wasdel = wasdel;
765 *logflagsp = 0;
766 if ((error = xfs_alloc_vextent(&args))) {
767 xfs_iroot_realloc(ip, -1, whichfork);
768 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
769 return error;
770 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700771
772 /*
773 * During a CoW operation, the allocation and bmbt updates occur in
774 * different transactions. The mapping code tries to put new bmbt
775 * blocks near extents being mapped, but the only way to guarantee this
776 * is if the alloc and the mapping happen in a single transaction that
777 * has a block reservation. That isn't the case here, so if we run out
778 * of space we'll try again with another AG.
779 */
780 if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) &&
781 args.fsbno == NULLFSBLOCK &&
782 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
783 dfops->dop_low = true;
784 goto try_another_ag;
785 }
Dave Chinner9e5987a72013-02-25 12:31:26 +1100786 /*
787 * Allocation can't fail, the space was reserved.
788 */
789 ASSERT(args.fsbno != NULLFSBLOCK);
790 ASSERT(*firstblock == NULLFSBLOCK ||
791 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000792 (dfops->dop_low &&
Dave Chinner9e5987a72013-02-25 12:31:26 +1100793 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
794 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
795 cur->bc_private.b.allocated++;
796 ip->i_d.di_nblocks++;
797 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
798 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
799 /*
800 * Fill in the child block.
801 */
802 abp->b_ops = &xfs_bmbt_buf_ops;
803 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500804 if (xfs_sb_version_hascrc(&mp->m_sb))
805 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
806 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
807 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
808 else
809 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
810 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
811 XFS_BTREE_LONG_PTRS);
812
Dave Chinner9e5987a72013-02-25 12:31:26 +1100813 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100814 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100815 for (cnt = i = 0; i < nextents; i++) {
816 ep = xfs_iext_get_ext(ifp, i);
817 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
818 arp->l0 = cpu_to_be64(ep->l0);
819 arp->l1 = cpu_to_be64(ep->l1);
820 arp++; cnt++;
821 }
822 }
823 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
824 xfs_btree_set_numrecs(ablock, cnt);
825
826 /*
827 * Fill in the root key and pointer.
828 */
829 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
830 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
831 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
832 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
833 be16_to_cpu(block->bb_level)));
834 *pp = cpu_to_be64(args.fsbno);
835
836 /*
837 * Do all this logging at the end so that
838 * the root is at the right level.
839 */
840 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
841 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
842 ASSERT(*curp == NULL);
843 *curp = cur;
844 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
845 return 0;
846}
847
848/*
849 * Convert a local file to an extents file.
850 * This code is out of bounds for data forks of regular files,
851 * since the file data needs to get logged so things will stay consistent.
852 * (The bmap-level manipulations are ok, though).
853 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000854void
855xfs_bmap_local_to_extents_empty(
856 struct xfs_inode *ip,
857 int whichfork)
858{
859 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
860
Darrick J. Wong60b49842016-10-03 09:11:34 -0700861 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000862 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
863 ASSERT(ifp->if_bytes == 0);
864 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
865
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000866 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000867 ifp->if_flags &= ~XFS_IFINLINE;
868 ifp->if_flags |= XFS_IFEXTENTS;
869 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
870}
871
872
Dave Chinner9e5987a72013-02-25 12:31:26 +1100873STATIC int /* error */
874xfs_bmap_local_to_extents(
875 xfs_trans_t *tp, /* transaction pointer */
876 xfs_inode_t *ip, /* incore inode pointer */
877 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
878 xfs_extlen_t total, /* total blocks needed by transaction */
879 int *logflagsp, /* inode logging flags */
880 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500881 void (*init_fn)(struct xfs_trans *tp,
882 struct xfs_buf *bp,
Dave Chinner9e5987a72013-02-25 12:31:26 +1100883 struct xfs_inode *ip,
884 struct xfs_ifork *ifp))
885{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000886 int error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100887 int flags; /* logging flags returned */
888 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000889 xfs_alloc_arg_t args; /* allocation arguments */
890 xfs_buf_t *bp; /* buffer for extent block */
891 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a72013-02-25 12:31:26 +1100892
893 /*
894 * We don't want to deal with the case of keeping inode data inline yet.
895 * So sending the data fork of a regular inode is invalid.
896 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100897 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a72013-02-25 12:31:26 +1100898 ifp = XFS_IFORK_PTR(ip, whichfork);
899 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000900
901 if (!ifp->if_bytes) {
902 xfs_bmap_local_to_extents_empty(ip, whichfork);
903 flags = XFS_ILOG_CORE;
904 goto done;
905 }
906
Dave Chinner9e5987a72013-02-25 12:31:26 +1100907 flags = 0;
908 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000909 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
910 XFS_IFINLINE);
911 memset(&args, 0, sizeof(args));
912 args.tp = tp;
913 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000914 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000915 args.firstblock = *firstblock;
916 /*
917 * Allocate a block. We know we need only one, since the
918 * file currently fits in an inode.
919 */
920 if (*firstblock == NULLFSBLOCK) {
Darrick J. Wong90e20562016-10-03 09:11:45 -0700921try_another_ag:
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000922 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
923 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100924 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000925 args.fsbno = *firstblock;
926 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a72013-02-25 12:31:26 +1100927 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000928 args.total = total;
929 args.minlen = args.maxlen = args.prod = 1;
930 error = xfs_alloc_vextent(&args);
931 if (error)
932 goto done;
933
Darrick J. Wong90e20562016-10-03 09:11:45 -0700934 /*
935 * During a CoW operation, the allocation and bmbt updates occur in
936 * different transactions. The mapping code tries to put new bmbt
937 * blocks near extents being mapped, but the only way to guarantee this
938 * is if the alloc and the mapping happen in a single transaction that
939 * has a block reservation. That isn't the case here, so if we run out
940 * of space we'll try again with another AG.
941 */
942 if (xfs_sb_version_hasreflink(&ip->i_mount->m_sb) &&
943 args.fsbno == NULLFSBLOCK &&
944 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
945 goto try_another_ag;
946 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000947 /* Can't fail, the space was reserved. */
948 ASSERT(args.fsbno != NULLFSBLOCK);
949 ASSERT(args.len == 1);
950 *firstblock = args.fsbno;
951 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
952
Dave Chinnerfe22d552015-01-22 09:30:06 +1100953 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100954 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100955 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100956 * The callout is responsible for logging because the remote format
957 * might differ from the local format and thus we don't know how much to
958 * log here. Note that init_fn must also set the buffer log item type
959 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100960 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000961 init_fn(tp, bp, ip, ifp);
962
Brian Fosterb7cdc662015-10-12 15:40:24 +1100963 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000964 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
965 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a72013-02-25 12:31:26 +1100966 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000967
968 xfs_iext_add(ifp, 0, 1);
969 ep = xfs_iext_get_ext(ifp, 0);
970 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
971 trace_xfs_bmap_post_update(ip, 0,
972 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
973 _THIS_IP_);
974 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
975 ip->i_d.di_nblocks = 1;
976 xfs_trans_mod_dquot_byino(tp, ip,
977 XFS_TRANS_DQ_BCOUNT, 1L);
978 flags |= xfs_ilog_fext(whichfork);
979
Dave Chinner9e5987a72013-02-25 12:31:26 +1100980done:
981 *logflagsp = flags;
982 return error;
983}
984
985/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * Called from xfs_bmap_add_attrfork to handle btree format files.
987 */
988STATIC int /* error */
989xfs_bmap_add_attrfork_btree(
990 xfs_trans_t *tp, /* transaction pointer */
991 xfs_inode_t *ip, /* incore inode pointer */
992 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000993 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 int *flags) /* inode logging flags */
995{
996 xfs_btree_cur_t *cur; /* btree cursor */
997 int error; /* error return value */
998 xfs_mount_t *mp; /* file system mount struct */
999 int stat; /* newroot status */
1000
1001 mp = ip->i_mount;
1002 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1003 *flags |= XFS_ILOG_DBROOT;
1004 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001005 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001006 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 cur->bc_private.b.firstblock = *firstblock;
1008 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1009 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001010 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001011 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001012 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto error0;
1014 if (stat == 0) {
1015 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +10001016 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 *firstblock = cur->bc_private.b.firstblock;
1019 cur->bc_private.b.allocated = 0;
1020 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1021 }
1022 return 0;
1023error0:
1024 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1025 return error;
1026}
1027
1028/*
1029 * Called from xfs_bmap_add_attrfork to handle extents format files.
1030 */
1031STATIC int /* error */
1032xfs_bmap_add_attrfork_extents(
1033 xfs_trans_t *tp, /* transaction pointer */
1034 xfs_inode_t *ip, /* incore inode pointer */
1035 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001036 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 int *flags) /* inode logging flags */
1038{
1039 xfs_btree_cur_t *cur; /* bmap btree cursor */
1040 int error; /* error return value */
1041
1042 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1043 return 0;
1044 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001045 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 flags, XFS_DATA_FORK);
1047 if (cur) {
1048 cur->bc_private.b.allocated = 0;
1049 xfs_btree_del_cursor(cur,
1050 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1051 }
1052 return error;
1053}
1054
1055/*
Dave Chinner1e823792013-02-11 15:58:13 +11001056 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1057 * different data fork content type needs a different callout to do the
1058 * conversion. Some are basic and only require special block initialisation
1059 * callouts for the data formating, others (directories) are so specialised they
1060 * handle everything themselves.
1061 *
1062 * XXX (dgc): investigate whether directory conversion can use the generic
1063 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001064 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 */
1066STATIC int /* error */
1067xfs_bmap_add_attrfork_local(
1068 xfs_trans_t *tp, /* transaction pointer */
1069 xfs_inode_t *ip, /* incore inode pointer */
1070 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001071 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 int *flags) /* inode logging flags */
1073{
1074 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1077 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001078
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001079 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001081 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 dargs.dp = ip;
1083 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001084 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001085 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 dargs.whichfork = XFS_DATA_FORK;
1087 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001088 return xfs_dir2_sf_to_block(&dargs);
1089 }
1090
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001091 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001092 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1093 flags, XFS_DATA_FORK,
1094 xfs_symlink_local_to_remote);
1095
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001096 /* should only be called for types that support local format data */
1097 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001098 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001102 * Convert inode from non-attributed to attributed.
1103 * Must not be in a transaction, ip must not be locked.
1104 */
1105int /* error code */
1106xfs_bmap_add_attrfork(
1107 xfs_inode_t *ip, /* incore inode pointer */
1108 int size, /* space new attribute needs */
1109 int rsvd) /* xact may use reserved blks */
1110{
1111 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001112 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001113 xfs_mount_t *mp; /* mount structure */
1114 xfs_trans_t *tp; /* transaction pointer */
1115 int blks; /* space reservation */
1116 int version = 1; /* superblock attr version */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001117 int logflags; /* logging flags */
1118 int error; /* error return value */
1119
1120 ASSERT(XFS_IFORK_Q(ip) == 0);
1121
1122 mp = ip->i_mount;
1123 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001124
Dave Chinner9e5987a72013-02-25 12:31:26 +11001125 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001126
1127 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1128 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1129 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001130 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001131
Dave Chinner9e5987a72013-02-25 12:31:26 +11001132 xfs_ilock(ip, XFS_ILOCK_EXCL);
1133 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1134 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1135 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001136 if (error)
1137 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001138 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001139 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001140 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1141 /*
1142 * For inodes coming from pre-6.2 filesystems.
1143 */
1144 ASSERT(ip->i_d.di_aformat == 0);
1145 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1146 }
1147 ASSERT(ip->i_d.di_anextents == 0);
1148
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001149 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001150 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1151
1152 switch (ip->i_d.di_format) {
1153 case XFS_DINODE_FMT_DEV:
1154 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1155 break;
1156 case XFS_DINODE_FMT_UUID:
1157 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1158 break;
1159 case XFS_DINODE_FMT_LOCAL:
1160 case XFS_DINODE_FMT_EXTENTS:
1161 case XFS_DINODE_FMT_BTREE:
1162 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1163 if (!ip->i_d.di_forkoff)
1164 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1165 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1166 version = 2;
1167 break;
1168 default:
1169 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001170 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001171 goto trans_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001172 }
1173
1174 ASSERT(ip->i_afp == NULL);
1175 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1176 ip->i_afp->if_flags = XFS_IFEXTENTS;
1177 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001178 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001179 switch (ip->i_d.di_format) {
1180 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001181 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001182 &logflags);
1183 break;
1184 case XFS_DINODE_FMT_EXTENTS:
1185 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001186 &dfops, &logflags);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001187 break;
1188 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001189 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a72013-02-25 12:31:26 +11001190 &logflags);
1191 break;
1192 default:
1193 error = 0;
1194 break;
1195 }
1196 if (logflags)
1197 xfs_trans_log_inode(tp, ip, logflags);
1198 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001199 goto bmap_cancel;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001200 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1201 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001202 bool log_sb = false;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001203
1204 spin_lock(&mp->m_sb_lock);
1205 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1206 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001207 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001208 }
1209 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1210 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001211 log_sb = true;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001212 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001213 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001214 if (log_sb)
1215 xfs_log_sb(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001216 }
1217
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001218 error = xfs_defer_finish(&tp, &dfops, NULL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001219 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001220 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001221 error = xfs_trans_commit(tp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001222 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001223 return error;
1224
1225bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001226 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001227trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001228 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001229 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001230 return error;
1231}
1232
1233/*
1234 * Internal and external extent tree search functions.
1235 */
1236
1237/*
1238 * Read in the extents to if_extents.
1239 * All inode fields are set up by caller, we just traverse the btree
1240 * and copy the records in. If the file system cannot contain unwritten
1241 * extents, the records are checked for no "state" flags.
1242 */
1243int /* error */
1244xfs_bmap_read_extents(
1245 xfs_trans_t *tp, /* transaction pointer */
1246 xfs_inode_t *ip, /* incore inode */
1247 int whichfork) /* data or attr fork */
1248{
1249 struct xfs_btree_block *block; /* current btree block */
1250 xfs_fsblock_t bno; /* block # of "block" */
1251 xfs_buf_t *bp; /* buffer for "block" */
1252 int error; /* error return value */
1253 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1254 xfs_extnum_t i, j; /* index into the extents list */
1255 xfs_ifork_t *ifp; /* fork structure */
1256 int level; /* btree level, for checking */
1257 xfs_mount_t *mp; /* file system mount structure */
1258 __be64 *pp; /* pointer to block address */
1259 /* REFERENCED */
1260 xfs_extnum_t room; /* number of entries there's room for */
1261
1262 bno = NULLFSBLOCK;
1263 mp = ip->i_mount;
1264 ifp = XFS_IFORK_PTR(ip, whichfork);
1265 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1266 XFS_EXTFMT_INODE(ip);
1267 block = ifp->if_broot;
1268 /*
1269 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1270 */
1271 level = be16_to_cpu(block->bb_level);
1272 ASSERT(level > 0);
1273 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1274 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001275 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001276 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1277 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1278 /*
1279 * Go down the tree until leaf level is reached, following the first
1280 * pointer (leftmost) at each level.
1281 */
1282 while (level-- > 0) {
1283 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1284 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1285 if (error)
1286 return error;
1287 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001288 if (level == 0)
1289 break;
1290 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1291 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001292 XFS_WANT_CORRUPTED_GOTO(mp,
1293 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001294 xfs_trans_brelse(tp, bp);
1295 }
1296 /*
1297 * Here with bp and block set to the leftmost leaf node in the tree.
1298 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001299 room = xfs_iext_count(ifp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001300 i = 0;
1301 /*
1302 * Loop over all leaf nodes. Copy information to the extent records.
1303 */
1304 for (;;) {
1305 xfs_bmbt_rec_t *frp;
1306 xfs_fsblock_t nextbno;
1307 xfs_extnum_t num_recs;
1308 xfs_extnum_t start;
1309
1310 num_recs = xfs_btree_get_numrecs(block);
1311 if (unlikely(i + num_recs > room)) {
1312 ASSERT(i + num_recs <= room);
1313 xfs_warn(ip->i_mount,
1314 "corrupt dinode %Lu, (btree extents).",
1315 (unsigned long long) ip->i_ino);
1316 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1317 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1318 goto error0;
1319 }
Dave Chinner9e5987a72013-02-25 12:31:26 +11001320 /*
1321 * Read-ahead the next leaf block, if any.
1322 */
1323 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1324 if (nextbno != NULLFSBLOCK)
1325 xfs_btree_reada_bufl(mp, nextbno, 1,
1326 &xfs_bmbt_buf_ops);
1327 /*
1328 * Copy records into the extent records.
1329 */
1330 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1331 start = i;
1332 for (j = 0; j < num_recs; j++, i++, frp++) {
1333 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1334 trp->l0 = be64_to_cpu(frp->l0);
1335 trp->l1 = be64_to_cpu(frp->l1);
1336 }
1337 if (exntf == XFS_EXTFMT_NOSTATE) {
1338 /*
1339 * Check all attribute bmap btree records and
1340 * any "older" data bmap btree records for a
1341 * set bit in the "extent flag" position.
1342 */
1343 if (unlikely(xfs_check_nostate_extents(ifp,
1344 start, num_recs))) {
1345 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1346 XFS_ERRLEVEL_LOW,
1347 ip->i_mount);
1348 goto error0;
1349 }
1350 }
1351 xfs_trans_brelse(tp, bp);
1352 bno = nextbno;
1353 /*
1354 * If we've reached the end, stop.
1355 */
1356 if (bno == NULLFSBLOCK)
1357 break;
1358 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1359 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1360 if (error)
1361 return error;
1362 block = XFS_BUF_TO_BLOCK(bp);
1363 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001364 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a72013-02-25 12:31:26 +11001365 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1366 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1367 return 0;
1368error0:
1369 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001370 return -EFSCORRUPTED;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001371}
1372
1373
1374/*
1375 * Search the extent records for the entry containing block bno.
1376 * If bno lies in a hole, point to the next entry. If bno lies
1377 * past eof, *eofp will be set, and *prevp will contain the last
1378 * entry (null if none). Else, *lastxp will be set to the index
1379 * of the found entry; *gotp will contain the entry.
1380 */
1381STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1382xfs_bmap_search_multi_extents(
1383 xfs_ifork_t *ifp, /* inode fork pointer */
1384 xfs_fileoff_t bno, /* block number searched for */
1385 int *eofp, /* out: end of file found */
1386 xfs_extnum_t *lastxp, /* out: last extent index */
1387 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1388 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1389{
1390 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1391 xfs_extnum_t lastx; /* last extent index */
1392
1393 /*
1394 * Initialize the extent entry structure to catch access to
1395 * uninitialized br_startblock field.
1396 */
1397 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1398 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1399 gotp->br_state = XFS_EXT_INVALID;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001400 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001401 prevp->br_startoff = NULLFILEOFF;
1402
1403 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1404 if (lastx > 0) {
1405 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1406 }
Eric Sandeen5d829302016-11-08 12:59:42 +11001407 if (lastx < xfs_iext_count(ifp)) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11001408 xfs_bmbt_get_all(ep, gotp);
1409 *eofp = 0;
1410 } else {
1411 if (lastx > 0) {
1412 *gotp = *prevp;
1413 }
1414 *eofp = 1;
1415 ep = NULL;
1416 }
1417 *lastxp = lastx;
1418 return ep;
1419}
1420
1421/*
1422 * Search the extents list for the inode, for the extent containing bno.
1423 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1424 * *eofp will be set, and *prevp will contain the last entry (null if none).
1425 * Else, *lastxp will be set to the index of the found
1426 * entry; *gotp will contain the entry.
1427 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10001428xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001429xfs_bmap_search_extents(
1430 xfs_inode_t *ip, /* incore inode pointer */
1431 xfs_fileoff_t bno, /* block number searched for */
1432 int fork, /* data or attr fork */
1433 int *eofp, /* out: end of file found */
1434 xfs_extnum_t *lastxp, /* out: last extent index */
1435 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1436 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1437{
1438 xfs_ifork_t *ifp; /* inode fork pointer */
1439 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1440
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001441 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001442 ifp = XFS_IFORK_PTR(ip, fork);
1443
1444 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1445
1446 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1447 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1448 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1449 "Access to block zero in inode %llu "
1450 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -05001451 "blkcnt: %llx extent-state: %x lastx: %x",
Dave Chinner9e5987a72013-02-25 12:31:26 +11001452 (unsigned long long)ip->i_ino,
1453 (unsigned long long)gotp->br_startblock,
1454 (unsigned long long)gotp->br_startoff,
1455 (unsigned long long)gotp->br_blockcount,
1456 gotp->br_state, *lastxp);
1457 *lastxp = NULLEXTNUM;
1458 *eofp = 1;
1459 return NULL;
1460 }
1461 return ep;
1462}
1463
1464/*
1465 * Returns the file-relative block number of the first unused block(s)
1466 * in the file with at least "len" logically contiguous blocks free.
1467 * This is the lowest-address hole if the file has holes, else the first block
1468 * past the end of file.
1469 * Return 0 if the file is currently local (in-inode).
1470 */
1471int /* error */
1472xfs_bmap_first_unused(
1473 xfs_trans_t *tp, /* transaction pointer */
1474 xfs_inode_t *ip, /* incore inode */
1475 xfs_extlen_t len, /* size of hole to find */
1476 xfs_fileoff_t *first_unused, /* unused block */
1477 int whichfork) /* data or attr fork */
1478{
1479 int error; /* error return value */
1480 int idx; /* extent record index */
1481 xfs_ifork_t *ifp; /* inode fork pointer */
1482 xfs_fileoff_t lastaddr; /* last block number seen */
1483 xfs_fileoff_t lowest; /* lowest useful block */
1484 xfs_fileoff_t max; /* starting useful block */
1485 xfs_fileoff_t off; /* offset for this block */
1486 xfs_extnum_t nextents; /* number of extent entries */
1487
1488 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1489 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1490 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1491 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1492 *first_unused = 0;
1493 return 0;
1494 }
1495 ifp = XFS_IFORK_PTR(ip, whichfork);
1496 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1497 (error = xfs_iread_extents(tp, ip, whichfork)))
1498 return error;
1499 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001500 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001501 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1502 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1503 off = xfs_bmbt_get_startoff(ep);
1504 /*
1505 * See if the hole before this extent will work.
1506 */
1507 if (off >= lowest + len && off - max >= len) {
1508 *first_unused = max;
1509 return 0;
1510 }
1511 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1512 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1513 }
1514 *first_unused = max;
1515 return 0;
1516}
1517
1518/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001519 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a72013-02-25 12:31:26 +11001520 * last_block (input value) in the file.
1521 * This is not based on i_size, it is based on the extent records.
1522 * Returns 0 for local files, as they do not have extent records.
1523 */
1524int /* error */
1525xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001526 struct xfs_trans *tp, /* transaction pointer */
1527 struct xfs_inode *ip, /* incore inode */
1528 xfs_fileoff_t *last_block, /* last block */
1529 int whichfork) /* data or attr fork */
Dave Chinner9e5987a72013-02-25 12:31:26 +11001530{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001531 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1532 struct xfs_bmbt_irec got;
1533 xfs_extnum_t idx;
1534 int error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001535
Christoph Hellwig86685f72016-11-24 11:39:38 +11001536 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1537 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a72013-02-25 12:31:26 +11001538 *last_block = 0;
1539 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001540 case XFS_DINODE_FMT_BTREE:
1541 case XFS_DINODE_FMT_EXTENTS:
1542 break;
1543 default:
1544 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001545 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001546
1547 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1548 error = xfs_iread_extents(tp, ip, whichfork);
1549 if (error)
1550 return error;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001551 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001552
1553 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1554 if (got.br_startoff <= *last_block - 1)
1555 return 0;
1556 }
1557
1558 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1559 *last_block = got.br_startoff + got.br_blockcount;
1560 return 0;
1561 }
1562
1563 *last_block = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001564 return 0;
1565}
1566
Dave Chinner68988112013-08-12 20:49:42 +10001567int
Dave Chinner9e5987a72013-02-25 12:31:26 +11001568xfs_bmap_last_extent(
1569 struct xfs_trans *tp,
1570 struct xfs_inode *ip,
1571 int whichfork,
1572 struct xfs_bmbt_irec *rec,
1573 int *is_empty)
1574{
1575 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1576 int error;
1577 int nextents;
1578
1579 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1580 error = xfs_iread_extents(tp, ip, whichfork);
1581 if (error)
1582 return error;
1583 }
1584
Eric Sandeen5d829302016-11-08 12:59:42 +11001585 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001586 if (nextents == 0) {
1587 *is_empty = 1;
1588 return 0;
1589 }
1590
1591 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1592 *is_empty = 0;
1593 return 0;
1594}
1595
1596/*
1597 * Check the last inode extent to determine whether this allocation will result
1598 * in blocks being allocated at the end of the file. When we allocate new data
1599 * blocks at the end of the file which do not start at the previous data block,
1600 * we will try to align the new blocks at stripe unit boundaries.
1601 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001602 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a72013-02-25 12:31:26 +11001603 * at, or past the EOF.
1604 */
1605STATIC int
1606xfs_bmap_isaeof(
1607 struct xfs_bmalloca *bma,
1608 int whichfork)
1609{
1610 struct xfs_bmbt_irec rec;
1611 int is_empty;
1612 int error;
1613
1614 bma->aeof = 0;
1615 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1616 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001617 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11001618 return error;
1619
Dave Chinner6e708bc2013-11-22 10:41:16 +11001620 if (is_empty) {
1621 bma->aeof = 1;
1622 return 0;
1623 }
1624
Dave Chinner9e5987a72013-02-25 12:31:26 +11001625 /*
1626 * Check if we are allocation or past the last extent, or at least into
1627 * the last delayed allocated extent.
1628 */
1629 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1630 (bma->offset >= rec.br_startoff &&
1631 isnullstartblock(rec.br_startblock));
1632 return 0;
1633}
1634
1635/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11001636 * Returns the file-relative block number of the first block past eof in
1637 * the file. This is not based on i_size, it is based on the extent records.
1638 * Returns 0 for local files, as they do not have extent records.
1639 */
1640int
1641xfs_bmap_last_offset(
Dave Chinner9e5987a72013-02-25 12:31:26 +11001642 struct xfs_inode *ip,
1643 xfs_fileoff_t *last_block,
1644 int whichfork)
1645{
1646 struct xfs_bmbt_irec rec;
1647 int is_empty;
1648 int error;
1649
1650 *last_block = 0;
1651
1652 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1653 return 0;
1654
1655 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1656 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001657 return -EIO;
Dave Chinner9e5987a72013-02-25 12:31:26 +11001658
1659 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1660 if (error || is_empty)
1661 return error;
1662
1663 *last_block = rec.br_startoff + rec.br_blockcount;
1664 return 0;
1665}
1666
1667/*
1668 * Returns whether the selected fork of the inode has exactly one
1669 * block or not. For the data fork we check this matches di_size,
1670 * implying the file's range is 0..bsize-1.
1671 */
1672int /* 1=>1 block, 0=>otherwise */
1673xfs_bmap_one_block(
1674 xfs_inode_t *ip, /* incore inode */
1675 int whichfork) /* data or attr fork */
1676{
1677 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1678 xfs_ifork_t *ifp; /* inode fork pointer */
1679 int rval; /* return value */
1680 xfs_bmbt_irec_t s; /* internal version of extent */
1681
1682#ifndef DEBUG
1683 if (whichfork == XFS_DATA_FORK)
1684 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1685#endif /* !DEBUG */
1686 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1687 return 0;
1688 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1689 return 0;
1690 ifp = XFS_IFORK_PTR(ip, whichfork);
1691 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1692 ep = xfs_iext_get_ext(ifp, 0);
1693 xfs_bmbt_get_all(ep, &s);
1694 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1695 if (rval && whichfork == XFS_DATA_FORK)
1696 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1697 return rval;
1698}
1699
1700/*
1701 * Extent tree manipulation functions used during allocation.
1702 */
1703
1704/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001705 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 */
1707STATIC int /* error */
1708xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001709 struct xfs_bmalloca *bma,
1710 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001712 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001714 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001717 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 xfs_fileoff_t new_endoff; /* end offset of new entry */
1719 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1720 /* left is 0, right is 1, prev is 2 */
1721 int rval=0; /* return value (logging flags) */
1722 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001723 xfs_filblks_t da_new; /* new count del alloc blocks used */
1724 xfs_filblks_t da_old; /* old count del alloc blocks used */
1725 xfs_filblks_t temp=0; /* value for da_new calculations */
1726 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001728 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001729 xfs_extnum_t *nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001731 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001732 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001733 ASSERT(whichfork != XFS_ATTR_FORK);
1734 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1735 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001736
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001737 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001738 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001739 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001740 ASSERT(!bma->cur ||
1741 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001742
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001743 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745#define LEFT r[0]
1746#define RIGHT r[1]
1747#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Darrick J. Wong60b49842016-10-03 09:11:34 -07001749 if (whichfork == XFS_COW_FORK)
1750 state |= BMAP_COWFORK;
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 /*
1753 * Set up a bunch of variables to make the tests simpler.
1754 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001755 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 xfs_bmbt_get_all(ep, &PREV);
1757 new_endoff = new->br_startoff + new->br_blockcount;
1758 ASSERT(PREV.br_startoff <= new->br_startoff);
1759 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001760
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001761 da_old = startblockval(PREV.br_startblock);
1762 da_new = 0;
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /*
1765 * Set flags determining what part of the previous delayed allocation
1766 * extent is being replaced by a real allocation.
1767 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001768 if (PREV.br_startoff == new->br_startoff)
1769 state |= BMAP_LEFT_FILLING;
1770 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1771 state |= BMAP_RIGHT_FILLING;
1772
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 /*
1774 * Check and set flags if this segment has a left neighbor.
1775 * Don't set contiguous if the combined extent would be too large.
1776 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001777 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001778 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001779 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001780
1781 if (isnullstartblock(LEFT.br_startblock))
1782 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001784
1785 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1786 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1787 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1788 LEFT.br_state == new->br_state &&
1789 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1790 state |= BMAP_LEFT_CONTIG;
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /*
1793 * Check and set flags if this segment has a right neighbor.
1794 * Don't set contiguous if the combined extent would be too large.
1795 * Also check for all-three-contiguous being too large.
1796 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001797 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001798 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001799 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001800
1801 if (isnullstartblock(RIGHT.br_startblock))
1802 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001804
1805 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1806 new_endoff == RIGHT.br_startoff &&
1807 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1808 new->br_state == RIGHT.br_state &&
1809 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1810 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1811 BMAP_RIGHT_FILLING)) !=
1812 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1813 BMAP_RIGHT_FILLING) ||
1814 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1815 <= MAXEXTLEN))
1816 state |= BMAP_RIGHT_CONTIG;
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 error = 0;
1819 /*
1820 * Switch out based on the FILLING and CONTIG state bits.
1821 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001822 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1823 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1824 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1825 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /*
1827 * Filling in all of a previously delayed allocation extent.
1828 * The left and right neighbors are both contiguous with new.
1829 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001830 bma->idx--;
1831 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1832 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 LEFT.br_blockcount + PREV.br_blockcount +
1834 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001835 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001836
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001837 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001838 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001839 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1841 else {
1842 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001843 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001845 RIGHT.br_blockcount, &i);
1846 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001848 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001849 error = xfs_btree_delete(bma->cur, &i);
1850 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001852 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001853 error = xfs_btree_decrement(bma->cur, 0, &i);
1854 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001856 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001857 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 LEFT.br_startblock,
1859 LEFT.br_blockcount +
1860 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001861 RIGHT.br_blockcount, LEFT.br_state);
1862 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 goto done;
1864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 break;
1866
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001867 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /*
1869 * Filling in all of a previously delayed allocation extent.
1870 * The left neighbor is contiguous, the right is not.
1871 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001872 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001873
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001874 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1875 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001876 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001877 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001878
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001879 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1880 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 rval = XFS_ILOG_DEXT;
1882 else {
1883 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001884 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001886 &i);
1887 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001889 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001890 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 LEFT.br_startblock,
1892 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001893 PREV.br_blockcount, LEFT.br_state);
1894 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 goto done;
1896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 break;
1898
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001899 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 /*
1901 * Filling in all of a previously delayed allocation extent.
1902 * The right neighbor is contiguous, the left is not.
1903 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001904 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 xfs_bmbt_set_startblock(ep, new->br_startblock);
1906 xfs_bmbt_set_blockcount(ep,
1907 PREV.br_blockcount + RIGHT.br_blockcount);
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
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001910 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1911 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 rval = XFS_ILOG_DEXT;
1913 else {
1914 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001915 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001917 RIGHT.br_blockcount, &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 == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001921 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 new->br_startblock,
1923 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001924 RIGHT.br_blockcount, PREV.br_state);
1925 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 goto done;
1927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 break;
1929
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001930 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 /*
1932 * Filling in all of a previously delayed allocation extent.
1933 * Neither the left nor right neighbors are contiguous with
1934 * the new one.
1935 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001936 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001938 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001939
Darrick J. Wong60b49842016-10-03 09:11:34 -07001940 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001941 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1943 else {
1944 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001945 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001947 &i);
1948 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001950 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001951 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1952 error = xfs_btree_insert(bma->cur, &i);
1953 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001955 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 break;
1958
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001959 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 /*
1961 * Filling in the first part of a previous delayed allocation.
1962 * The left neighbor is contiguous.
1963 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001964 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1965 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 LEFT.br_blockcount + new->br_blockcount);
1967 xfs_bmbt_set_startoff(ep,
1968 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001969 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001972 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001974 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 rval = XFS_ILOG_DEXT;
1976 else {
1977 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001978 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001980 &i);
1981 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001983 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001984 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 LEFT.br_startblock,
1986 LEFT.br_blockcount +
1987 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001988 LEFT.br_state);
1989 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 goto done;
1991 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001992 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001993 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001994 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001995 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001996
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001997 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 break;
1999
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002000 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 /*
2002 * Filling in the first part of a previous delayed allocation.
2003 * The left neighbor is not contiguous.
2004 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002005 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 xfs_bmbt_set_startoff(ep, new_endoff);
2007 temp = PREV.br_blockcount - new->br_blockcount;
2008 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002009 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002010 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002011 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2013 else {
2014 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002015 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002017 &i);
2018 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002020 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002021 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2022 error = xfs_btree_insert(bma->cur, &i);
2023 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002025 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002027
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002028 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002029 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002030 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002031 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 rval |= tmp_rval;
2033 if (error)
2034 goto done;
2035 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002036 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002037 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002038 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2039 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002040 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002041 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 break;
2043
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002044 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 /*
2046 * Filling in the last part of a previous delayed allocation.
2047 * The right neighbor is contiguous with the new allocation.
2048 */
2049 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002050 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002052 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002053 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 new->br_blockcount + RIGHT.br_blockcount,
2055 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002056 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2057 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 rval = XFS_ILOG_DEXT;
2059 else {
2060 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002061 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002063 RIGHT.br_blockcount, &i);
2064 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002066 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002067 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 new->br_startblock,
2069 new->br_blockcount +
2070 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002071 RIGHT.br_state);
2072 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 goto done;
2074 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002075
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002076 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002077 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002078 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002079 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002080 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002081
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002082 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 break;
2084
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002085 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 /*
2087 * Filling in the last part of a previous delayed allocation.
2088 * The right neighbor is not contiguous.
2089 */
2090 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002091 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002093 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002094 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002095 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2097 else {
2098 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002099 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002101 &i);
2102 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002104 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002105 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2106 error = xfs_btree_insert(bma->cur, &i);
2107 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002109 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002111
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002112 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002113 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002114 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002115 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 rval |= tmp_rval;
2117 if (error)
2118 goto done;
2119 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002120 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002121 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002122 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2123 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002124 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002125 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002126
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002127 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 break;
2129
2130 case 0:
2131 /*
2132 * Filling in the middle part of a previous delayed allocation.
2133 * Contiguity is impossible here.
2134 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002135 *
2136 * We start with a delayed allocation:
2137 *
2138 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2139 * PREV @ idx
2140 *
2141 * and we are allocating:
2142 * +rrrrrrrrrrrrrrrrr+
2143 * new
2144 *
2145 * and we set it up for insertion as:
2146 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2147 * new
2148 * PREV @ idx LEFT RIGHT
2149 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 */
2151 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002153 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002154 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2155 LEFT = *new;
2156 RIGHT.br_state = PREV.br_state;
2157 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002158 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002159 RIGHT.br_startoff = new_endoff;
2160 RIGHT.br_blockcount = temp2;
2161 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002162 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002163 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002164 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2166 else {
2167 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002168 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002170 &i);
2171 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002173 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002174 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2175 error = xfs_btree_insert(bma->cur, &i);
2176 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002178 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002180
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002181 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002182 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002183 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002184 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 rval |= tmp_rval;
2186 if (error)
2187 goto done;
2188 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002189 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2190 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002191 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002192 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002193 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002194 error = xfs_mod_fdblocks(bma->ip->i_mount,
2195 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002196 ASSERT(!error);
2197 if (error)
2198 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002200
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002201 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002202 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002203 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2204 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2205 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002206 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002207 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002208
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002209 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002210 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 break;
2212
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002213 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2214 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2215 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2216 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2217 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2218 case BMAP_LEFT_CONTIG:
2219 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 /*
2221 * These cases are all impossible.
2222 */
2223 ASSERT(0);
2224 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002225
Darrick J. Wong9c194642016-08-03 12:16:05 +10002226 /* add reverse mapping */
2227 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2228 if (error)
2229 goto done;
2230
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002231 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002232 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002233 int tmp_logflags; /* partial log flag return val */
2234
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002235 ASSERT(bma->cur == NULL);
2236 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002237 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002238 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002239 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002240 if (error)
2241 goto done;
2242 }
2243
2244 /* adjust for changes in reserved delayed indirect blocks */
2245 if (da_old || da_new) {
2246 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002247 if (bma->cur)
2248 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002249 ASSERT(temp <= da_old);
2250 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002251 xfs_mod_fdblocks(bma->ip->i_mount,
2252 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002253 }
2254
2255 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002256 if (bma->cur)
2257 bma->cur->bc_private.b.allocated = 0;
2258
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002259 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002261 if (whichfork != XFS_COW_FORK)
2262 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return error;
2264#undef LEFT
2265#undef RIGHT
2266#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267}
2268
2269/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002270 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 */
2272STATIC int /* error */
2273xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002274 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002276 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002278 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002279 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002280 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002281 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002284 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002287 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 xfs_fileoff_t new_endoff; /* end offset of new entry */
2289 xfs_exntst_t newext; /* new extent state */
2290 xfs_exntst_t oldext; /* old extent state */
2291 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2292 /* left is 0, right is 1, prev is 2 */
2293 int rval=0; /* return value (logging flags) */
2294 int state = 0;/* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002295 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002297 *logflagsp = 0;
2298
2299 cur = *curp;
2300 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2301
2302 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002303 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002304 ASSERT(!isnullstartblock(new->br_startblock));
2305
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002306 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002307
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308#define LEFT r[0]
2309#define RIGHT r[1]
2310#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /*
2313 * Set up a bunch of variables to make the tests simpler.
2314 */
2315 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002316 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 xfs_bmbt_get_all(ep, &PREV);
2318 newext = new->br_state;
2319 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2320 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2321 ASSERT(PREV.br_state == oldext);
2322 new_endoff = new->br_startoff + new->br_blockcount;
2323 ASSERT(PREV.br_startoff <= new->br_startoff);
2324 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 /*
2327 * Set flags determining what part of the previous oldext allocation
2328 * extent is being replaced by a newext allocation.
2329 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002330 if (PREV.br_startoff == new->br_startoff)
2331 state |= BMAP_LEFT_FILLING;
2332 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2333 state |= BMAP_RIGHT_FILLING;
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 /*
2336 * Check and set flags if this segment has a left neighbor.
2337 * Don't set contiguous if the combined extent would be too large.
2338 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002339 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002340 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002341 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002342
2343 if (isnullstartblock(LEFT.br_startblock))
2344 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002346
2347 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2348 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2349 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2350 LEFT.br_state == newext &&
2351 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2352 state |= BMAP_LEFT_CONTIG;
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 /*
2355 * Check and set flags if this segment has a right neighbor.
2356 * Don't set contiguous if the combined extent would be too large.
2357 * Also check for all-three-contiguous being too large.
2358 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002359 if (*idx < xfs_iext_count(&ip->i_df) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002360 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002361 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002362 if (isnullstartblock(RIGHT.br_startblock))
2363 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002365
2366 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2367 new_endoff == RIGHT.br_startoff &&
2368 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2369 newext == RIGHT.br_state &&
2370 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2371 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2372 BMAP_RIGHT_FILLING)) !=
2373 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2374 BMAP_RIGHT_FILLING) ||
2375 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2376 <= MAXEXTLEN))
2377 state |= BMAP_RIGHT_CONTIG;
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 /*
2380 * Switch out based on the FILLING and CONTIG state bits.
2381 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002382 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2383 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2384 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2385 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 /*
2387 * Setting all of a previous oldext extent to newext.
2388 * The left and right neighbors are both contiguous with new.
2389 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002390 --*idx;
2391
2392 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2393 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 LEFT.br_blockcount + PREV.br_blockcount +
2395 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002396 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002397
Christoph Hellwigec90c552011-05-23 08:52:53 +00002398 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 ip->i_d.di_nextents -= 2;
2400 if (cur == NULL)
2401 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2402 else {
2403 rval = XFS_ILOG_CORE;
2404 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2405 RIGHT.br_startblock,
2406 RIGHT.br_blockcount, &i)))
2407 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002408 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002409 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002411 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002412 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002414 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002415 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002417 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002418 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002420 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2422 LEFT.br_startblock,
2423 LEFT.br_blockcount + PREV.br_blockcount +
2424 RIGHT.br_blockcount, LEFT.br_state)))
2425 goto done;
2426 }
2427 break;
2428
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002429 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 /*
2431 * Setting all of a previous oldext extent to newext.
2432 * The left neighbor is contiguous, the right is not.
2433 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002434 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002435
Christoph Hellwigec90c552011-05-23 08:52:53 +00002436 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2437 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2438 LEFT.br_blockcount + PREV.br_blockcount);
2439 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2440
2441 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 ip->i_d.di_nextents--;
2443 if (cur == NULL)
2444 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2445 else {
2446 rval = XFS_ILOG_CORE;
2447 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2448 PREV.br_startblock, PREV.br_blockcount,
2449 &i)))
2450 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002451 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002452 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002454 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002455 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002457 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2459 LEFT.br_startblock,
2460 LEFT.br_blockcount + PREV.br_blockcount,
2461 LEFT.br_state)))
2462 goto done;
2463 }
2464 break;
2465
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002466 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 /*
2468 * Setting all of a previous oldext extent to newext.
2469 * The right neighbor is contiguous, the left is not.
2470 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002471 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 xfs_bmbt_set_blockcount(ep,
2473 PREV.br_blockcount + RIGHT.br_blockcount);
2474 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002475 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2476 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 ip->i_d.di_nextents--;
2478 if (cur == NULL)
2479 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2480 else {
2481 rval = XFS_ILOG_CORE;
2482 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2483 RIGHT.br_startblock,
2484 RIGHT.br_blockcount, &i)))
2485 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002486 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002487 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002489 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002490 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002492 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2494 new->br_startblock,
2495 new->br_blockcount + RIGHT.br_blockcount,
2496 newext)))
2497 goto done;
2498 }
2499 break;
2500
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002501 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 /*
2503 * Setting all of a previous oldext extent to newext.
2504 * Neither the left nor right neighbors are contiguous with
2505 * the new one.
2506 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002507 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002509 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if (cur == NULL)
2512 rval = XFS_ILOG_DEXT;
2513 else {
2514 rval = 0;
2515 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2516 new->br_startblock, new->br_blockcount,
2517 &i)))
2518 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002519 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2521 new->br_startblock, new->br_blockcount,
2522 newext)))
2523 goto done;
2524 }
2525 break;
2526
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002527 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 /*
2529 * Setting the first part of a previous oldext extent to newext.
2530 * The left neighbor is contiguous.
2531 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002532 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2533 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 LEFT.br_blockcount + new->br_blockcount);
2535 xfs_bmbt_set_startoff(ep,
2536 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002537 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002538
Christoph Hellwigec90c552011-05-23 08:52:53 +00002539 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 xfs_bmbt_set_startblock(ep,
2541 new->br_startblock + new->br_blockcount);
2542 xfs_bmbt_set_blockcount(ep,
2543 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002544 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002545
Christoph Hellwigec90c552011-05-23 08:52:53 +00002546 --*idx;
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if (cur == NULL)
2549 rval = XFS_ILOG_DEXT;
2550 else {
2551 rval = 0;
2552 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2553 PREV.br_startblock, PREV.br_blockcount,
2554 &i)))
2555 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002556 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if ((error = xfs_bmbt_update(cur,
2558 PREV.br_startoff + new->br_blockcount,
2559 PREV.br_startblock + new->br_blockcount,
2560 PREV.br_blockcount - new->br_blockcount,
2561 oldext)))
2562 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002563 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002565 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 LEFT.br_startblock,
2567 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002568 LEFT.br_state);
2569 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 goto done;
2571 }
2572 break;
2573
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002574 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 /*
2576 * Setting the first part of a previous oldext extent to newext.
2577 * The left neighbor is not contiguous.
2578 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002579 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2581 xfs_bmbt_set_startoff(ep, new_endoff);
2582 xfs_bmbt_set_blockcount(ep,
2583 PREV.br_blockcount - new->br_blockcount);
2584 xfs_bmbt_set_startblock(ep,
2585 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002586 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002587
Christoph Hellwigec90c552011-05-23 08:52:53 +00002588 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 ip->i_d.di_nextents++;
2590 if (cur == NULL)
2591 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2592 else {
2593 rval = XFS_ILOG_CORE;
2594 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2595 PREV.br_startblock, PREV.br_blockcount,
2596 &i)))
2597 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002598 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 if ((error = xfs_bmbt_update(cur,
2600 PREV.br_startoff + new->br_blockcount,
2601 PREV.br_startblock + new->br_blockcount,
2602 PREV.br_blockcount - new->br_blockcount,
2603 oldext)))
2604 goto done;
2605 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002606 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 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 }
2610 break;
2611
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002612 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 /*
2614 * Setting the last part of a previous oldext extent to newext.
2615 * The right neighbor is contiguous with the new allocation.
2616 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002617 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 xfs_bmbt_set_blockcount(ep,
2619 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002620 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2621
2622 ++*idx;
2623
2624 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2625 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002626 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002628 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 if (cur == NULL)
2631 rval = XFS_ILOG_DEXT;
2632 else {
2633 rval = 0;
2634 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2635 PREV.br_startblock,
2636 PREV.br_blockcount, &i)))
2637 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002638 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2640 PREV.br_startblock,
2641 PREV.br_blockcount - new->br_blockcount,
2642 oldext)))
2643 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002644 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 goto done;
2646 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2647 new->br_startblock,
2648 new->br_blockcount + RIGHT.br_blockcount,
2649 newext)))
2650 goto done;
2651 }
2652 break;
2653
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002654 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 /*
2656 * Setting the last part of a previous oldext extent to newext.
2657 * The right neighbor is not contiguous.
2658 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002659 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 xfs_bmbt_set_blockcount(ep,
2661 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002662 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002663
Christoph Hellwigec90c552011-05-23 08:52:53 +00002664 ++*idx;
2665 xfs_iext_insert(ip, *idx, 1, new, state);
2666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 ip->i_d.di_nextents++;
2668 if (cur == NULL)
2669 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2670 else {
2671 rval = XFS_ILOG_CORE;
2672 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2673 PREV.br_startblock, PREV.br_blockcount,
2674 &i)))
2675 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002676 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2678 PREV.br_startblock,
2679 PREV.br_blockcount - new->br_blockcount,
2680 oldext)))
2681 goto done;
2682 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2683 new->br_startblock, new->br_blockcount,
2684 &i)))
2685 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002686 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002688 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002690 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
2692 break;
2693
2694 case 0:
2695 /*
2696 * Setting the middle part of a previous oldext extent to
2697 * newext. Contiguity is impossible here.
2698 * One extent becomes three extents.
2699 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002700 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 xfs_bmbt_set_blockcount(ep,
2702 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002703 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 r[0] = *new;
2706 r[1].br_startoff = new_endoff;
2707 r[1].br_blockcount =
2708 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2709 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2710 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002711
2712 ++*idx;
2713 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 ip->i_d.di_nextents += 2;
2716 if (cur == NULL)
2717 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2718 else {
2719 rval = XFS_ILOG_CORE;
2720 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2721 PREV.br_startblock, PREV.br_blockcount,
2722 &i)))
2723 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002724 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 /* new right extent - oldext */
2726 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2727 r[1].br_startblock, r[1].br_blockcount,
2728 r[1].br_state)))
2729 goto done;
2730 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002732 cur->bc_rec.b.br_blockcount =
2733 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002734 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002736 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002737 /*
2738 * Reset the cursor to the position of the new extent
2739 * we are about to insert as we can't trust it after
2740 * the previous insert.
2741 */
2742 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2743 new->br_startblock, new->br_blockcount,
2744 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002746 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002748 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002749 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002751 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 }
2753 break;
2754
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002755 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2756 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2757 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2758 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2759 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2760 case BMAP_LEFT_CONTIG:
2761 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 /*
2763 * These cases are all impossible.
2764 */
2765 ASSERT(0);
2766 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002767
Darrick J. Wong9c194642016-08-03 12:16:05 +10002768 /* update reverse mappings */
2769 error = xfs_rmap_convert_extent(mp, dfops, ip, XFS_DATA_FORK, new);
2770 if (error)
2771 goto done;
2772
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002773 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002774 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002775 int tmp_logflags; /* partial log flag return val */
2776
2777 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002778 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002779 0, &tmp_logflags, XFS_DATA_FORK);
2780 *logflagsp |= tmp_logflags;
2781 if (error)
2782 goto done;
2783 }
2784
2785 /* clear out the allocated field, done with it now in any case. */
2786 if (cur) {
2787 cur->bc_private.b.allocated = 0;
2788 *curp = cur;
2789 }
2790
2791 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002793 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 return error;
2795#undef LEFT
2796#undef RIGHT
2797#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
2799
2800/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002801 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002803STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804xfs_bmap_add_extent_hole_delay(
2805 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002806 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002807 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002808 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002810 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2812 xfs_filblks_t newlen=0; /* new indirect size */
2813 xfs_filblks_t oldlen=0; /* old indirect size */
2814 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2815 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002816 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002818 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002820 if (whichfork == XFS_COW_FORK)
2821 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002822 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 /*
2825 * Check and set flags if this segment has a left neighbor
2826 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002827 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002828 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002829 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002830
2831 if (isnullstartblock(left.br_startblock))
2832 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 /*
2836 * Check and set flags if the current (right) segment exists.
2837 * If it doesn't exist, we're converting the hole at end-of-file.
2838 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002839 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002840 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002841 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002842
2843 if (isnullstartblock(right.br_startblock))
2844 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 /*
2848 * Set contiguity flags on the left and right neighbors.
2849 * Don't let extents get too large, even if the pieces are contiguous.
2850 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002851 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2852 left.br_startoff + left.br_blockcount == new->br_startoff &&
2853 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2854 state |= BMAP_LEFT_CONTIG;
2855
2856 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2857 new->br_startoff + new->br_blockcount == right.br_startoff &&
2858 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2859 (!(state & BMAP_LEFT_CONTIG) ||
2860 (left.br_blockcount + new->br_blockcount +
2861 right.br_blockcount <= MAXEXTLEN)))
2862 state |= BMAP_RIGHT_CONTIG;
2863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 /*
2865 * Switch out based on the contiguity flags.
2866 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002867 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2868 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 /*
2870 * New allocation is contiguous with delayed allocations
2871 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002872 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002874 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 temp = left.br_blockcount + new->br_blockcount +
2876 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002877
Christoph Hellwigec90c552011-05-23 08:52:53 +00002878 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2879 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002880 oldlen = startblockval(left.br_startblock) +
2881 startblockval(new->br_startblock) +
2882 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002884 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002885 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002886 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002887
Christoph Hellwigec90c552011-05-23 08:52:53 +00002888 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 break;
2890
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002891 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 /*
2893 * New allocation is contiguous with a delayed allocation
2894 * on the left.
2895 * Merge the new allocation with the left neighbor.
2896 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002897 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002899
2900 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2901 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002902 oldlen = startblockval(left.br_startblock) +
2903 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002905 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002906 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002907 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 break;
2909
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002910 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 /*
2912 * New allocation is contiguous with a delayed allocation
2913 * on the right.
2914 * Merge the new allocation with the right neighbor.
2915 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002916 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002918 oldlen = startblockval(new->br_startblock) +
2919 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002921 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2922 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002923 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002924 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 break;
2926
2927 case 0:
2928 /*
2929 * New allocation is not contiguous with another
2930 * delayed allocation.
2931 * Insert a new entry.
2932 */
2933 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002934 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 break;
2936 }
2937 if (oldlen != newlen) {
2938 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002939 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2940 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 /*
2942 * Nothing to do for disk quota accounting here.
2943 */
2944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945}
2946
2947/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002948 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 */
2950STATIC int /* error */
2951xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002952 struct xfs_bmalloca *bma,
2953 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002955 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 int i; /* temp state */
2958 xfs_ifork_t *ifp; /* inode fork pointer */
2959 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2960 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002961 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002963 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Eric Sandeenf1f96c42016-01-04 16:10:42 +11002965 mp = bma->ip->i_mount;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002966 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002967
Christoph Hellwigc6534242011-09-18 20:41:05 +00002968 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002969 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002970 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002971 ASSERT(!bma->cur ||
2972 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Darrick J. Wong3993bae2016-10-03 09:11:32 -07002973 ASSERT(whichfork != XFS_COW_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002974
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002975 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002976
2977 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002978 if (whichfork == XFS_ATTR_FORK)
2979 state |= BMAP_ATTRFORK;
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 /*
2982 * Check and set flags if this segment has a left neighbor.
2983 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002984 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002985 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002986 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002987 if (isnullstartblock(left.br_startblock))
2988 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 /*
2992 * Check and set flags if this segment has a current value.
2993 * Not true if we're inserting into the "hole" at eof.
2994 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002995 if (bma->idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002996 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002997 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002998 if (isnullstartblock(right.br_startblock))
2999 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003001
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 /*
3003 * We're inserting a real allocation between "left" and "right".
3004 * Set the contiguity flags. Don't let extents get too large.
3005 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003006 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
3007 left.br_startoff + left.br_blockcount == new->br_startoff &&
3008 left.br_startblock + left.br_blockcount == new->br_startblock &&
3009 left.br_state == new->br_state &&
3010 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
3011 state |= BMAP_LEFT_CONTIG;
3012
3013 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
3014 new->br_startoff + new->br_blockcount == right.br_startoff &&
3015 new->br_startblock + new->br_blockcount == right.br_startblock &&
3016 new->br_state == right.br_state &&
3017 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3018 (!(state & BMAP_LEFT_CONTIG) ||
3019 left.br_blockcount + new->br_blockcount +
3020 right.br_blockcount <= MAXEXTLEN))
3021 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003023 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 /*
3025 * Select which case we're in here, and implement it.
3026 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003027 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3028 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 /*
3030 * New allocation is contiguous with real allocations on the
3031 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003032 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003034 --bma->idx;
3035 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3036 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 left.br_blockcount + new->br_blockcount +
3038 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003039 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003040
Christoph Hellwigc6534242011-09-18 20:41:05 +00003041 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003042
Christoph Hellwigc6534242011-09-18 20:41:05 +00003043 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3044 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3045 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003046 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003047 } else {
3048 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003049 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3050 right.br_startblock, right.br_blockcount,
3051 &i);
3052 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003053 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003054 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003055 error = xfs_btree_delete(bma->cur, &i);
3056 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003057 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003058 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003059 error = xfs_btree_decrement(bma->cur, 0, &i);
3060 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003061 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003062 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003063 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003064 left.br_startblock,
3065 left.br_blockcount +
3066 new->br_blockcount +
3067 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003068 left.br_state);
3069 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003070 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003072 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003074 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 /*
3076 * New allocation is contiguous with a real allocation
3077 * on the left.
3078 * Merge the new allocation with the left neighbor.
3079 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003080 --bma->idx;
3081 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3082 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003084 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003085
Christoph Hellwigc6534242011-09-18 20:41:05 +00003086 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003087 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003088 } else {
3089 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003090 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3091 left.br_startblock, left.br_blockcount,
3092 &i);
3093 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003094 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003095 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003096 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003097 left.br_startblock,
3098 left.br_blockcount +
3099 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003100 left.br_state);
3101 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003102 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003104 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003106 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 /*
3108 * New allocation is contiguous with a real allocation
3109 * on the right.
3110 * Merge the new allocation with the right neighbor.
3111 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003112 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3113 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003114 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 new->br_blockcount + right.br_blockcount,
3116 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003117 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003118
Christoph Hellwigc6534242011-09-18 20:41:05 +00003119 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003120 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003121 } else {
3122 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003123 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003124 right.br_startoff,
3125 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003126 right.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 == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003130 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003131 new->br_startblock,
3132 new->br_blockcount +
3133 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003134 right.br_state);
3135 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003136 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003138 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139
3140 case 0:
3141 /*
3142 * New allocation is not contiguous with another
3143 * real allocation.
3144 * Insert a new entry.
3145 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003146 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3147 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3148 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3149 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003150 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003151 } else {
3152 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003153 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003154 new->br_startoff,
3155 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003156 new->br_blockcount, &i);
3157 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003158 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003159 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003160 bma->cur->bc_rec.b.br_state = new->br_state;
3161 error = xfs_btree_insert(bma->cur, &i);
3162 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003163 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003164 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003166 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003168
Darrick J. Wong9c194642016-08-03 12:16:05 +10003169 /* add reverse mapping */
3170 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
3171 if (error)
3172 goto done;
3173
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003174 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003175 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003176 int tmp_logflags; /* partial log flag return val */
3177
Christoph Hellwigc6534242011-09-18 20:41:05 +00003178 ASSERT(bma->cur == NULL);
3179 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003180 bma->firstblock, bma->dfops, &bma->cur,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003181 0, &tmp_logflags, whichfork);
3182 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003183 if (error)
3184 goto done;
3185 }
3186
3187 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003188 if (bma->cur)
3189 bma->cur->bc_private.b.allocated = 0;
3190
3191 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003192done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003193 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003194 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195}
3196
Nathan Scottdd9f4382006-01-11 15:28:28 +11003197/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11003198 * Functions used in the extent read, allocate and remove paths
3199 */
3200
3201/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003202 * Adjust the size of the new extent based on di_extsize and rt extsize.
3203 */
Dave Chinner68988112013-08-12 20:49:42 +10003204int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003205xfs_bmap_extsize_align(
3206 xfs_mount_t *mp,
3207 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3208 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3209 xfs_extlen_t extsz, /* align to this extent size */
3210 int rt, /* is this a realtime inode? */
3211 int eof, /* is extent at end-of-file? */
3212 int delay, /* creating delalloc extent? */
3213 int convert, /* overwriting unwritten extent? */
3214 xfs_fileoff_t *offp, /* in/out: aligned offset */
3215 xfs_extlen_t *lenp) /* in/out: aligned length */
3216{
3217 xfs_fileoff_t orig_off; /* original offset */
3218 xfs_extlen_t orig_alen; /* original length */
3219 xfs_fileoff_t orig_end; /* original off+len */
3220 xfs_fileoff_t nexto; /* next file offset */
3221 xfs_fileoff_t prevo; /* previous file offset */
3222 xfs_fileoff_t align_off; /* temp for offset */
3223 xfs_extlen_t align_alen; /* temp for length */
3224 xfs_extlen_t temp; /* temp for calculations */
3225
3226 if (convert)
3227 return 0;
3228
3229 orig_off = align_off = *offp;
3230 orig_alen = align_alen = *lenp;
3231 orig_end = orig_off + orig_alen;
3232
3233 /*
3234 * If this request overlaps an existing extent, then don't
3235 * attempt to perform any additional alignment.
3236 */
3237 if (!delay && !eof &&
3238 (orig_off >= gotp->br_startoff) &&
3239 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3240 return 0;
3241 }
3242
3243 /*
3244 * If the file offset is unaligned vs. the extent size
3245 * we need to align it. This will be possible unless
3246 * the file was previously written with a kernel that didn't
3247 * perform this alignment, or if a truncate shot us in the
3248 * foot.
3249 */
3250 temp = do_mod(orig_off, extsz);
3251 if (temp) {
3252 align_alen += temp;
3253 align_off -= temp;
3254 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003255
3256 /* Same adjustment for the end of the requested area. */
3257 temp = (align_alen % extsz);
3258 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003259 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003260
3261 /*
3262 * For large extent hint sizes, the aligned extent might be larger than
3263 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3264 * the length back under MAXEXTLEN. The outer allocation loops handle
3265 * short allocation just fine, so it is safe to do this. We only want to
3266 * do it when we are forced to, though, because it means more allocation
3267 * operations are required.
3268 */
3269 while (align_alen > MAXEXTLEN)
3270 align_alen -= extsz;
3271 ASSERT(align_alen <= MAXEXTLEN);
3272
Nathan Scottdd9f4382006-01-11 15:28:28 +11003273 /*
3274 * If the previous block overlaps with this proposed allocation
3275 * then move the start forward without adjusting the length.
3276 */
3277 if (prevp->br_startoff != NULLFILEOFF) {
3278 if (prevp->br_startblock == HOLESTARTBLOCK)
3279 prevo = prevp->br_startoff;
3280 else
3281 prevo = prevp->br_startoff + prevp->br_blockcount;
3282 } else
3283 prevo = 0;
3284 if (align_off != orig_off && align_off < prevo)
3285 align_off = prevo;
3286 /*
3287 * If the next block overlaps with this proposed allocation
3288 * then move the start back without adjusting the length,
3289 * but not before offset 0.
3290 * This may of course make the start overlap previous block,
3291 * and if we hit the offset 0 limit then the next block
3292 * can still overlap too.
3293 */
3294 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3295 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3296 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3297 nexto = gotp->br_startoff + gotp->br_blockcount;
3298 else
3299 nexto = gotp->br_startoff;
3300 } else
3301 nexto = NULLFILEOFF;
3302 if (!eof &&
3303 align_off + align_alen != orig_end &&
3304 align_off + align_alen > nexto)
3305 align_off = nexto > align_alen ? nexto - align_alen : 0;
3306 /*
3307 * If we're now overlapping the next or previous extent that
3308 * means we can't fit an extsz piece in this hole. Just move
3309 * the start forward to the first valid spot and set
3310 * the length so we hit the end.
3311 */
3312 if (align_off != orig_off && align_off < prevo)
3313 align_off = prevo;
3314 if (align_off + align_alen != orig_end &&
3315 align_off + align_alen > nexto &&
3316 nexto != NULLFILEOFF) {
3317 ASSERT(nexto > prevo);
3318 align_alen = nexto - align_off;
3319 }
3320
3321 /*
3322 * If realtime, and the result isn't a multiple of the realtime
3323 * extent size we need to remove blocks until it is.
3324 */
3325 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3326 /*
3327 * We're not covering the original request, or
3328 * we won't be able to once we fix the length.
3329 */
3330 if (orig_off < align_off ||
3331 orig_end > align_off + align_alen ||
3332 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003333 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003334 /*
3335 * Try to fix it by moving the start up.
3336 */
3337 if (align_off + temp <= orig_off) {
3338 align_alen -= temp;
3339 align_off += temp;
3340 }
3341 /*
3342 * Try to fix it by moving the end in.
3343 */
3344 else if (align_off + align_alen - temp >= orig_end)
3345 align_alen -= temp;
3346 /*
3347 * Set the start to the minimum then trim the length.
3348 */
3349 else {
3350 align_alen -= orig_off - align_off;
3351 align_off = orig_off;
3352 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3353 }
3354 /*
3355 * Result doesn't cover the request, fail it.
3356 */
3357 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003358 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003359 } else {
3360 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003361 /* see MAXEXTLEN handling above */
3362 ASSERT(orig_end <= align_off + align_alen ||
3363 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003364 }
3365
3366#ifdef DEBUG
3367 if (!eof && gotp->br_startoff != NULLFILEOFF)
3368 ASSERT(align_off + align_alen <= gotp->br_startoff);
3369 if (prevp->br_startoff != NULLFILEOFF)
3370 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3371#endif
3372
3373 *lenp = align_alen;
3374 *offp = align_off;
3375 return 0;
3376}
3377
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378#define XFS_ALLOC_GAP_UNITS 4
3379
Dave Chinner68988112013-08-12 20:49:42 +10003380void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003381xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003382 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383{
3384 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3386 xfs_mount_t *mp; /* mount point structure */
3387 int nullfb; /* true if ap->firstblock isn't set */
3388 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390#define ISVALID(x,y) \
3391 (rt ? \
3392 (x) < mp->m_sb.sb_rblocks : \
3393 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3394 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3395 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003398 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003399 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3400 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003401 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 /*
3403 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003404 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003406 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3407 !isnullstartblock(ap->prev.br_startblock) &&
3408 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3409 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003410 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 /*
3412 * Adjust for the gap between prevp and us.
3413 */
Dave Chinner3a756672011-09-18 20:40:58 +00003414 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003415 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003417 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3418 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 }
3420 /*
3421 * If not at eof, then compare the two neighbor blocks.
3422 * Figure out whether either one gives us a good starting point,
3423 * and pick the better one.
3424 */
3425 else if (!ap->eof) {
3426 xfs_fsblock_t gotbno; /* right side block number */
3427 xfs_fsblock_t gotdiff=0; /* right side difference */
3428 xfs_fsblock_t prevbno; /* left side block number */
3429 xfs_fsblock_t prevdiff=0; /* left side difference */
3430
3431 /*
3432 * If there's a previous (left) block, select a requested
3433 * start block based on it.
3434 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003435 if (ap->prev.br_startoff != NULLFILEOFF &&
3436 !isnullstartblock(ap->prev.br_startblock) &&
3437 (prevbno = ap->prev.br_startblock +
3438 ap->prev.br_blockcount) &&
3439 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 /*
3441 * Calculate gap to end of previous block.
3442 */
Dave Chinner3a756672011-09-18 20:40:58 +00003443 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003444 (ap->prev.br_startoff +
3445 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 /*
3447 * Figure the startblock based on the previous block's
3448 * end and the gap size.
3449 * Heuristic!
3450 * If the gap is large relative to the piece we're
3451 * allocating, or using it gives us an invalid block
3452 * number, then just use the end of the previous block.
3453 */
Dave Chinner3a756672011-09-18 20:40:58 +00003454 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003456 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 prevbno += adjust;
3458 else
3459 prevdiff += adjust;
3460 /*
3461 * If the firstblock forbids it, can't use it,
3462 * must use default.
3463 */
3464 if (!rt && !nullfb &&
3465 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3466 prevbno = NULLFSBLOCK;
3467 }
3468 /*
3469 * No previous block or can't follow it, just default.
3470 */
3471 else
3472 prevbno = NULLFSBLOCK;
3473 /*
3474 * If there's a following (right) block, select a requested
3475 * start block based on it.
3476 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003477 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 /*
3479 * Calculate gap to start of next block.
3480 */
Dave Chinner3a756672011-09-18 20:40:58 +00003481 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 /*
3483 * Figure the startblock based on the next block's
3484 * start and the gap size.
3485 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003486 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 /*
3488 * Heuristic!
3489 * If the gap is large relative to the piece we're
3490 * allocating, or using it gives us an invalid block
3491 * number, then just use the start of the next block
3492 * offset by our length.
3493 */
Dave Chinner3a756672011-09-18 20:40:58 +00003494 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 ISVALID(gotbno - gotdiff, gotbno))
3496 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003497 else if (ISVALID(gotbno - ap->length, gotbno)) {
3498 gotbno -= ap->length;
3499 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 } else
3501 gotdiff += adjust;
3502 /*
3503 * If the firstblock forbids it, can't use it,
3504 * must use default.
3505 */
3506 if (!rt && !nullfb &&
3507 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3508 gotbno = NULLFSBLOCK;
3509 }
3510 /*
3511 * No next block, just default.
3512 */
3513 else
3514 gotbno = NULLFSBLOCK;
3515 /*
3516 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003517 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 */
3519 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003520 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003522 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003524 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003526#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003527}
3528
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003529static int
3530xfs_bmap_longest_free_extent(
3531 struct xfs_trans *tp,
3532 xfs_agnumber_t ag,
3533 xfs_extlen_t *blen,
3534 int *notinit)
3535{
3536 struct xfs_mount *mp = tp->t_mountp;
3537 struct xfs_perag *pag;
3538 xfs_extlen_t longest;
3539 int error = 0;
3540
3541 pag = xfs_perag_get(mp, ag);
3542 if (!pag->pagf_init) {
3543 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3544 if (error)
3545 goto out;
3546
3547 if (!pag->pagf_init) {
3548 *notinit = 1;
3549 goto out;
3550 }
3551 }
3552
Dave Chinner50adbcb2015-06-22 10:04:31 +10003553 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003554 xfs_alloc_min_freelist(mp, pag),
3555 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003556 if (*blen < longest)
3557 *blen = longest;
3558
3559out:
3560 xfs_perag_put(pag);
3561 return error;
3562}
3563
3564static void
3565xfs_bmap_select_minlen(
3566 struct xfs_bmalloca *ap,
3567 struct xfs_alloc_arg *args,
3568 xfs_extlen_t *blen,
3569 int notinit)
3570{
3571 if (notinit || *blen < ap->minlen) {
3572 /*
3573 * Since we did a BUF_TRYLOCK above, it is possible that
3574 * there is space for this request.
3575 */
3576 args->minlen = ap->minlen;
3577 } else if (*blen < args->maxlen) {
3578 /*
3579 * If the best seen length is less than the request length,
3580 * use the best as the minimum.
3581 */
3582 args->minlen = *blen;
3583 } else {
3584 /*
3585 * Otherwise we've seen an extent as big as maxlen, use that
3586 * as the minimum.
3587 */
3588 args->minlen = args->maxlen;
3589 }
3590}
3591
Nathan Scotta365bdd2006-03-14 13:34:16 +11003592STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003593xfs_bmap_btalloc_nullfb(
3594 struct xfs_bmalloca *ap,
3595 struct xfs_alloc_arg *args,
3596 xfs_extlen_t *blen)
3597{
3598 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003599 xfs_agnumber_t ag, startag;
3600 int notinit = 0;
3601 int error;
3602
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003603 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003604 args->total = ap->total;
3605
Christoph Hellwigc467c042010-02-15 23:34:42 +00003606 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3607 if (startag == NULLAGNUMBER)
3608 startag = ag = 0;
3609
Dave Chinner14b064c2011-01-27 12:16:28 +11003610 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003611 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3612 &notinit);
3613 if (error)
3614 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003615
Christoph Hellwigc467c042010-02-15 23:34:42 +00003616 if (++ag == mp->m_sb.sb_agcount)
3617 ag = 0;
3618 if (ag == startag)
3619 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003620 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003621
3622 xfs_bmap_select_minlen(ap, args, blen, notinit);
3623 return 0;
3624}
3625
3626STATIC int
3627xfs_bmap_btalloc_filestreams(
3628 struct xfs_bmalloca *ap,
3629 struct xfs_alloc_arg *args,
3630 xfs_extlen_t *blen)
3631{
3632 struct xfs_mount *mp = ap->ip->i_mount;
3633 xfs_agnumber_t ag;
3634 int notinit = 0;
3635 int error;
3636
3637 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3638 args->total = ap->total;
3639
3640 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3641 if (ag == NULLAGNUMBER)
3642 ag = 0;
3643
3644 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3645 if (error)
3646 return error;
3647
3648 if (*blen < args->maxlen) {
3649 error = xfs_filestream_new_ag(ap, &ag);
3650 if (error)
3651 return error;
3652
3653 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3654 &notinit);
3655 if (error)
3656 return error;
3657
3658 }
3659
3660 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003661
3662 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003663 * Set the failure fallback case to look in the selected AG as stream
3664 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003665 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003666 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003667 return 0;
3668}
3669
3670STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003671xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003672 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003673{
3674 xfs_mount_t *mp; /* mount point structure */
3675 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003676 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003677 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003678 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003679 xfs_alloc_arg_t args;
3680 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003681 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003682 int nullfb; /* true if ap->firstblock isn't set */
3683 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003684 int tryagain;
3685 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003686 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003687
Dave Chinnera99ebf42011-12-01 11:24:20 +00003688 ASSERT(ap->length);
3689
Nathan Scotta365bdd2006-03-14 13:34:16 +11003690 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003691
3692 /* stripe alignment for allocation is determined by mount parameters */
3693 stripe_align = 0;
3694 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3695 stripe_align = mp->m_swidth;
3696 else if (mp->m_dalign)
3697 stripe_align = mp->m_dalign;
3698
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003699 if (ap->flags & XFS_BMAPI_COWFORK)
3700 align = xfs_get_cowextsz_hint(ap->ip);
3701 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003702 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003703 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003704 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003705 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003706 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003707 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003708 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003709 }
Dave Chinner33177f052013-12-12 16:34:36 +11003710
3711
Dave Chinner0937e0f2011-09-18 20:40:57 +00003712 nullfb = *ap->firstblock == NULLFSBLOCK;
3713 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003714 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003715 if (xfs_alloc_is_userdata(ap->datatype) &&
3716 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003717 ag = xfs_filestream_lookup_ag(ap->ip);
3718 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003719 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003720 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003721 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003722 }
3723 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003724 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003725
3726 xfs_bmap_adjacent(ap);
3727
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003729 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 * it's in the right allocation group.
3731 */
Dave Chinner3a756672011-09-18 20:40:58 +00003732 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 ;
3734 else
Dave Chinner3a756672011-09-18 20:40:58 +00003735 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 * Normal allocation, done through xfs_alloc_vextent.
3738 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003739 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003740 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003741 args.tp = ap->tp;
3742 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003743 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003744 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003745
3746 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003747 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003748 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003749 blen = 0;
3750 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003751 /*
3752 * Search for an allocation group with a single extent large
3753 * enough for the request. If one isn't found, then adjust
3754 * the minimum allocation size to the largest space found.
3755 */
Dave Chinner292378e2016-09-26 08:21:28 +10003756 if (xfs_alloc_is_userdata(ap->datatype) &&
3757 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003758 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3759 else
3760 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003761 if (error)
3762 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003763 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003764 if (xfs_inode_is_filestream(ap->ip))
3765 args.type = XFS_ALLOCTYPE_FIRST_AG;
3766 else
3767 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003768 args.total = args.minlen = ap->minlen;
3769 } else {
3770 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3771 args.total = ap->total;
3772 args.minlen = ap->minlen;
3773 }
David Chinner957d0eb2007-06-18 16:50:37 +10003774 /* apply extent size hints if obtained earlier */
3775 if (unlikely(align)) {
3776 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003777 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003778 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003779 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003780 args.prod = 1;
3781 args.mod = 0;
3782 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003783 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003784 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003785 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3786 }
3787 /*
3788 * If we are not low on available data blocks, and the
3789 * underlying logical volume manager is a stripe, and
3790 * the file offset is zero then try to allocate data
3791 * blocks on stripe unit boundary.
3792 * NOTE: ap->aeof is only set if the allocation length
3793 * is >= the stripe unit and the allocation offset is
3794 * at the end of file.
3795 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003796 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003797 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003798 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003799 atype = args.type;
3800 isaligned = 1;
3801 /*
3802 * Adjust for alignment
3803 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003804 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003805 args.minlen = blen - args.alignment;
3806 args.minalignslop = 0;
3807 } else {
3808 /*
3809 * First try an exact bno allocation.
3810 * If it fails then do a near or start bno
3811 * allocation with alignment turned on.
3812 */
3813 atype = args.type;
3814 tryagain = 1;
3815 args.type = XFS_ALLOCTYPE_THIS_BNO;
3816 args.alignment = 1;
3817 /*
3818 * Compute the minlen+alignment for the
3819 * next case. Set slop so that the value
3820 * of minlen+alignment+slop doesn't go up
3821 * between the calls.
3822 */
Dave Chinner33177f052013-12-12 16:34:36 +11003823 if (blen > stripe_align && blen <= args.maxlen)
3824 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003825 else
3826 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003827 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003828 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003829 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003830 args.minlen - 1;
3831 else
3832 args.minalignslop = 0;
3833 }
3834 } else {
3835 args.alignment = 1;
3836 args.minalignslop = 0;
3837 }
3838 args.minleft = ap->minleft;
3839 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003840 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003841 args.datatype = ap->datatype;
3842 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003843 args.ip = ap->ip;
3844
3845 error = xfs_alloc_vextent(&args);
3846 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003847 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003848
Nathan Scotta365bdd2006-03-14 13:34:16 +11003849 if (tryagain && args.fsbno == NULLFSBLOCK) {
3850 /*
3851 * Exact allocation failed. Now try with alignment
3852 * turned on.
3853 */
3854 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003855 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003856 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003857 args.minlen = nextminlen;
3858 args.minalignslop = 0;
3859 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 if ((error = xfs_alloc_vextent(&args)))
3861 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003862 }
3863 if (isaligned && args.fsbno == NULLFSBLOCK) {
3864 /*
3865 * allocation failed, so turn off alignment and
3866 * try again.
3867 */
3868 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003869 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003870 args.alignment = 0;
3871 if ((error = xfs_alloc_vextent(&args)))
3872 return error;
3873 }
3874 if (args.fsbno == NULLFSBLOCK && nullfb &&
3875 args.minlen > ap->minlen) {
3876 args.minlen = ap->minlen;
3877 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003878 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003879 if ((error = xfs_alloc_vextent(&args)))
3880 return error;
3881 }
3882 if (args.fsbno == NULLFSBLOCK && nullfb) {
3883 args.fsbno = 0;
3884 args.type = XFS_ALLOCTYPE_FIRST_AG;
3885 args.total = ap->minlen;
3886 args.minleft = 0;
3887 if ((error = xfs_alloc_vextent(&args)))
3888 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003889 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003890 }
3891 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003892 /*
3893 * check the allocation happened at the same or higher AG than
3894 * the first block that was allocated.
3895 */
3896 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3897 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3898 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003899 (ap->dfops->dop_low &&
Dave Chinner0937e0f2011-09-18 20:40:57 +00003900 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3901 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3902
Dave Chinner3a756672011-09-18 20:40:58 +00003903 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003904 if (*ap->firstblock == NULLFSBLOCK)
3905 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003906 ASSERT(nullfb || fb_agno == args.agno ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003907 (ap->dfops->dop_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003908 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003909 if (!(ap->flags & XFS_BMAPI_COWFORK))
3910 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003911 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3912 if (ap->wasdel)
3913 ap->ip->i_delayed_blks -= args.len;
3914 /*
3915 * Adjust the disk quota also. This was reserved
3916 * earlier.
3917 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003918 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003919 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3920 XFS_TRANS_DQ_BCOUNT,
3921 (long) args.len);
3922 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003923 ap->blkno = NULLFSBLOCK;
3924 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 }
3926 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003927}
3928
3929/*
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003930 * For a remap operation, just "allocate" an extent at the address that the
3931 * caller passed in, and ensure that the AGFL is the right size. The caller
3932 * will then map the "allocated" extent into the file somewhere.
3933 */
3934STATIC int
3935xfs_bmap_remap_alloc(
3936 struct xfs_bmalloca *ap)
3937{
3938 struct xfs_trans *tp = ap->tp;
3939 struct xfs_mount *mp = tp->t_mountp;
3940 xfs_agblock_t bno;
3941 struct xfs_alloc_arg args;
3942 int error;
3943
3944 /*
3945 * validate that the block number is legal - the enables us to detect
3946 * and handle a silent filesystem corruption rather than crashing.
3947 */
3948 memset(&args, 0, sizeof(struct xfs_alloc_arg));
3949 args.tp = ap->tp;
3950 args.mp = ap->tp->t_mountp;
3951 bno = *ap->firstblock;
3952 args.agno = XFS_FSB_TO_AGNO(mp, bno);
3953 args.agbno = XFS_FSB_TO_AGBNO(mp, bno);
3954 if (args.agno >= mp->m_sb.sb_agcount ||
3955 args.agbno >= mp->m_sb.sb_agblocks)
3956 return -EFSCORRUPTED;
3957
3958 /* "Allocate" the extent from the range we passed in. */
3959 trace_xfs_bmap_remap_alloc(ap->ip, *ap->firstblock, ap->length);
3960 ap->blkno = bno;
3961 ap->ip->i_d.di_nblocks += ap->length;
3962 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3963
3964 /* Fix the freelist, like a real allocator does. */
3965 args.datatype = ap->datatype;
3966 args.pag = xfs_perag_get(args.mp, args.agno);
3967 ASSERT(args.pag);
3968
3969 /*
3970 * The freelist fixing code will decline the allocation if
3971 * the size and shape of the free space doesn't allow for
3972 * allocating the extent and updating all the metadata that
3973 * happens during an allocation. We're remapping, not
3974 * allocating, so skip that check by pretending to be freeing.
3975 */
3976 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003977 xfs_perag_put(args.pag);
3978 if (error)
3979 trace_xfs_bmap_remap_alloc_error(ap->ip, error, _RET_IP_);
3980 return error;
3981}
3982
3983/*
Nathan Scotta365bdd2006-03-14 13:34:16 +11003984 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3985 * It figures out where to ask the underlying allocator to put the new extent.
3986 */
3987STATIC int
3988xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003989 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003990{
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003991 if (ap->flags & XFS_BMAPI_REMAP)
3992 return xfs_bmap_remap_alloc(ap);
Dave Chinner292378e2016-09-26 08:21:28 +10003993 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3994 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003995 return xfs_bmap_rtalloc(ap);
3996 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997}
3998
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003999/* Trim extent to fit a logical block range. */
4000void
4001xfs_trim_extent(
4002 struct xfs_bmbt_irec *irec,
4003 xfs_fileoff_t bno,
4004 xfs_filblks_t len)
4005{
4006 xfs_fileoff_t distance;
4007 xfs_fileoff_t end = bno + len;
4008
4009 if (irec->br_startoff + irec->br_blockcount <= bno ||
4010 irec->br_startoff >= end) {
4011 irec->br_blockcount = 0;
4012 return;
4013 }
4014
4015 if (irec->br_startoff < bno) {
4016 distance = bno - irec->br_startoff;
4017 if (isnullstartblock(irec->br_startblock))
4018 irec->br_startblock = DELAYSTARTBLOCK;
4019 if (irec->br_startblock != DELAYSTARTBLOCK &&
4020 irec->br_startblock != HOLESTARTBLOCK)
4021 irec->br_startblock += distance;
4022 irec->br_startoff += distance;
4023 irec->br_blockcount -= distance;
4024 }
4025
4026 if (end < irec->br_startoff + irec->br_blockcount) {
4027 distance = irec->br_startoff + irec->br_blockcount - end;
4028 irec->br_blockcount -= distance;
4029 }
4030}
4031
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032/*
Dave Chinneraef9a892011-09-18 20:40:44 +00004033 * Trim the returned map to the required bounds
4034 */
4035STATIC void
4036xfs_bmapi_trim_map(
4037 struct xfs_bmbt_irec *mval,
4038 struct xfs_bmbt_irec *got,
4039 xfs_fileoff_t *bno,
4040 xfs_filblks_t len,
4041 xfs_fileoff_t obno,
4042 xfs_fileoff_t end,
4043 int n,
4044 int flags)
4045{
4046 if ((flags & XFS_BMAPI_ENTIRE) ||
4047 got->br_startoff + got->br_blockcount <= obno) {
4048 *mval = *got;
4049 if (isnullstartblock(got->br_startblock))
4050 mval->br_startblock = DELAYSTARTBLOCK;
4051 return;
4052 }
4053
4054 if (obno > *bno)
4055 *bno = obno;
4056 ASSERT((*bno >= obno) || (n == 0));
4057 ASSERT(*bno < end);
4058 mval->br_startoff = *bno;
4059 if (isnullstartblock(got->br_startblock))
4060 mval->br_startblock = DELAYSTARTBLOCK;
4061 else
4062 mval->br_startblock = got->br_startblock +
4063 (*bno - got->br_startoff);
4064 /*
4065 * Return the minimum of what we got and what we asked for for
4066 * the length. We can use the len variable here because it is
4067 * modified below and we could have been there before coming
4068 * here if the first part of the allocation didn't overlap what
4069 * was asked for.
4070 */
4071 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4072 got->br_blockcount - (*bno - got->br_startoff));
4073 mval->br_state = got->br_state;
4074 ASSERT(mval->br_blockcount <= len);
4075 return;
4076}
4077
4078/*
4079 * Update and validate the extent map to return
4080 */
4081STATIC void
4082xfs_bmapi_update_map(
4083 struct xfs_bmbt_irec **map,
4084 xfs_fileoff_t *bno,
4085 xfs_filblks_t *len,
4086 xfs_fileoff_t obno,
4087 xfs_fileoff_t end,
4088 int *n,
4089 int flags)
4090{
4091 xfs_bmbt_irec_t *mval = *map;
4092
4093 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4094 ((mval->br_startoff + mval->br_blockcount) <= end));
4095 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4096 (mval->br_startoff < obno));
4097
4098 *bno = mval->br_startoff + mval->br_blockcount;
4099 *len = end - *bno;
4100 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4101 /* update previous map with new information */
4102 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4103 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4104 ASSERT(mval->br_state == mval[-1].br_state);
4105 mval[-1].br_blockcount = mval->br_blockcount;
4106 mval[-1].br_state = mval->br_state;
4107 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4108 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4109 mval[-1].br_startblock != HOLESTARTBLOCK &&
4110 mval->br_startblock == mval[-1].br_startblock +
4111 mval[-1].br_blockcount &&
4112 ((flags & XFS_BMAPI_IGSTATE) ||
4113 mval[-1].br_state == mval->br_state)) {
4114 ASSERT(mval->br_startoff ==
4115 mval[-1].br_startoff + mval[-1].br_blockcount);
4116 mval[-1].br_blockcount += mval->br_blockcount;
4117 } else if (*n > 0 &&
4118 mval->br_startblock == DELAYSTARTBLOCK &&
4119 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4120 mval->br_startoff ==
4121 mval[-1].br_startoff + mval[-1].br_blockcount) {
4122 mval[-1].br_blockcount += mval->br_blockcount;
4123 mval[-1].br_state = mval->br_state;
4124 } else if (!((*n == 0) &&
4125 ((mval->br_startoff + mval->br_blockcount) <=
4126 obno))) {
4127 mval++;
4128 (*n)++;
4129 }
4130 *map = mval;
4131}
4132
4133/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004134 * Map file blocks to filesystem blocks without allocation.
4135 */
4136int
4137xfs_bmapi_read(
4138 struct xfs_inode *ip,
4139 xfs_fileoff_t bno,
4140 xfs_filblks_t len,
4141 struct xfs_bmbt_irec *mval,
4142 int *nmap,
4143 int flags)
4144{
4145 struct xfs_mount *mp = ip->i_mount;
4146 struct xfs_ifork *ifp;
4147 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004148 xfs_fileoff_t obno;
4149 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11004150 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004151 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11004152 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004153 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004154 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004155
4156 ASSERT(*nmap >= 1);
4157 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004158 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004159 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004160
4161 if (unlikely(XFS_TEST_ERROR(
4162 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4163 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4164 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4165 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004166 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004167 }
4168
4169 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004170 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004171
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004172 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004173
4174 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004175
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004176 /* No CoW fork? Return a hole. */
4177 if (whichfork == XFS_COW_FORK && !ifp) {
4178 mval->br_startoff = bno;
4179 mval->br_startblock = HOLESTARTBLOCK;
4180 mval->br_blockcount = len;
4181 mval->br_state = XFS_EXT_NORM;
4182 *nmap = 1;
4183 return 0;
4184 }
4185
Dave Chinner5c8ed202011-09-18 20:40:45 +00004186 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4187 error = xfs_iread_extents(NULL, ip, whichfork);
4188 if (error)
4189 return error;
4190 }
4191
Christoph Hellwig334f3422016-11-24 11:39:43 +11004192 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4193 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004194 end = bno + len;
4195 obno = bno;
4196
4197 while (bno < end && n < *nmap) {
4198 /* Reading past eof, act as though there's a hole up to end. */
4199 if (eof)
4200 got.br_startoff = end;
4201 if (got.br_startoff > bno) {
4202 /* Reading in a hole. */
4203 mval->br_startoff = bno;
4204 mval->br_startblock = HOLESTARTBLOCK;
4205 mval->br_blockcount =
4206 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4207 mval->br_state = XFS_EXT_NORM;
4208 bno += mval->br_blockcount;
4209 len -= mval->br_blockcount;
4210 mval++;
4211 n++;
4212 continue;
4213 }
4214
4215 /* set up the extent map to return. */
4216 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4217 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4218
4219 /* If we're done, stop now. */
4220 if (bno >= end || n >= *nmap)
4221 break;
4222
4223 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004224 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4225 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004226 }
4227 *nmap = n;
4228 return 0;
4229}
4230
Christoph Hellwig51446f52016-09-19 11:10:21 +10004231int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004232xfs_bmapi_reserve_delalloc(
4233 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004234 int whichfork,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004235 xfs_fileoff_t aoff,
4236 xfs_filblks_t len,
4237 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004238 xfs_extnum_t *lastx,
4239 int eof)
4240{
4241 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004242 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004243 xfs_extlen_t alen;
4244 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004245 char rt = XFS_IS_REALTIME_INODE(ip);
4246 xfs_extlen_t extsz;
4247 int error;
4248
4249 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4250 if (!eof)
4251 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4252
4253 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004254 if (whichfork == XFS_COW_FORK)
4255 extsz = xfs_get_cowextsz_hint(ip);
4256 else
4257 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004258 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004259 struct xfs_bmbt_irec prev;
4260
4261 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4262 prev.br_startoff = NULLFILEOFF;
4263
4264 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004265 1, 0, &aoff, &alen);
4266 ASSERT(!error);
4267 }
4268
4269 if (rt)
4270 extsz = alen / mp->m_sb.sb_rextsize;
4271
4272 /*
4273 * Make a transaction-less quota reservation for delayed allocation
4274 * blocks. This number gets adjusted later. We return if we haven't
4275 * allocated blocks already inside this loop.
4276 */
4277 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4278 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4279 if (error)
4280 return error;
4281
4282 /*
4283 * Split changing sb for alen and indlen since they could be coming
4284 * from different places.
4285 */
4286 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4287 ASSERT(indlen > 0);
4288
4289 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004290 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004291 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004292 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004293 }
4294
4295 if (error)
4296 goto out_unreserve_quota;
4297
Dave Chinner0d485ad2015-02-23 21:22:03 +11004298 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004299 if (error)
4300 goto out_unreserve_blocks;
4301
4302
4303 ip->i_delayed_blks += alen;
4304
4305 got->br_startoff = aoff;
4306 got->br_startblock = nullstartblock(indlen);
4307 got->br_blockcount = alen;
4308 got->br_state = XFS_EXT_NORM;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004309 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004310
4311 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004312 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4313 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004314 */
4315 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4316
4317 ASSERT(got->br_startoff <= aoff);
4318 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4319 ASSERT(isnullstartblock(got->br_startblock));
4320 ASSERT(got->br_state == XFS_EXT_NORM);
4321 return 0;
4322
4323out_unreserve_blocks:
4324 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004325 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004326 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004327 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004328out_unreserve_quota:
4329 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004330 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004331 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4332 return error;
4333}
4334
Dave Chinnercf11da92014-07-15 07:08:24 +10004335static int
4336xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004337 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004338{
4339 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004340 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004341 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004342 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004343 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004344
Dave Chinnera99ebf42011-12-01 11:24:20 +00004345 ASSERT(bma->length > 0);
4346
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004347 /*
4348 * For the wasdelay case, we could also just allocate the stuff asked
4349 * for in this bmap call but that wouldn't be as good.
4350 */
4351 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004352 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4353 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004354 if (bma->idx != NULLEXTNUM && bma->idx) {
4355 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004356 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004357 }
4358 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004359 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004360 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004361 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004362 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004363 }
4364
4365 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004366 * Set the data type being allocated. For the data fork, the first data
4367 * in the file is treated differently to all other allocations. For the
4368 * attribute fork, we only need to ensure the allocated range is not on
4369 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004370 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004371 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004372 bma->datatype = XFS_ALLOC_NOBUSY;
4373 if (whichfork == XFS_DATA_FORK) {
4374 if (bma->offset == 0)
4375 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4376 else
4377 bma->datatype |= XFS_ALLOC_USERDATA;
4378 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004379 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004380 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004381 }
4382
Dave Chinnere04426b2012-10-05 11:06:59 +10004383 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004384
4385 /*
4386 * Only want to do the alignment at the eof if it is userdata and
4387 * allocation length is larger than a stripe unit.
4388 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004389 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004390 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004391 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004392 if (error)
4393 return error;
4394 }
4395
4396 error = xfs_bmap_alloc(bma);
4397 if (error)
4398 return error;
4399
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004400 if (bma->dfops->dop_low)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004401 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004402 if (bma->cur)
4403 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004404 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004405 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004406 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4407 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4408 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004409 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004410 }
4411 /*
4412 * Bump the number of extents we've allocated
4413 * in this call.
4414 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004415 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004416
Dave Chinner29c8d172011-09-18 20:41:00 +00004417 if (bma->cur)
4418 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004419 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4420
Dave Chinner963c30c2011-09-18 20:40:59 +00004421 bma->got.br_startoff = bma->offset;
4422 bma->got.br_startblock = bma->blkno;
4423 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004424 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004425
4426 /*
4427 * A wasdelay extent has been initialized, so shouldn't be flagged
4428 * as unwritten.
4429 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004430 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004431 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004432 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004433
Christoph Hellwigc6534242011-09-18 20:41:05 +00004434 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004435 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004436 else
4437 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004438
Christoph Hellwigc315c902011-09-18 20:41:02 +00004439 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004440 if (error)
4441 return error;
4442
4443 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004444 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4445 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4446 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004447 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004448 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004449
Dave Chinner963c30c2011-09-18 20:40:59 +00004450 ASSERT(bma->got.br_startoff <= bma->offset);
4451 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4452 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004453 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4454 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004455 return 0;
4456}
4457
Dave Chinnerb447fe52011-09-18 20:40:51 +00004458STATIC int
4459xfs_bmapi_convert_unwritten(
4460 struct xfs_bmalloca *bma,
4461 struct xfs_bmbt_irec *mval,
4462 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004463 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004464{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004465 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004466 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004467 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004468 int error;
4469
Dave Chinnerb447fe52011-09-18 20:40:51 +00004470 /* check if we need to do unwritten->real conversion */
4471 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4472 (flags & XFS_BMAPI_PREALLOC))
4473 return 0;
4474
4475 /* check if we need to do real->unwritten conversion */
4476 if (mval->br_state == XFS_EXT_NORM &&
4477 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4478 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4479 return 0;
4480
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004481 ASSERT(whichfork != XFS_COW_FORK);
4482
Dave Chinnerb447fe52011-09-18 20:40:51 +00004483 /*
4484 * Modify (by adding) the state flag, if writing.
4485 */
4486 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004487 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4488 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004489 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004490 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004491 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004492 }
4493 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4494 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4495
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004496 /*
4497 * Before insertion into the bmbt, zero the range being converted
4498 * if required.
4499 */
4500 if (flags & XFS_BMAPI_ZERO) {
4501 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4502 mval->br_blockcount);
4503 if (error)
4504 return error;
4505 }
4506
Dave Chinnere0c3da52011-09-18 20:41:01 +00004507 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004508 &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004509 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004510 /*
4511 * Log the inode core unconditionally in the unwritten extent conversion
4512 * path because the conversion might not have done so (e.g., if the
4513 * extent count hasn't changed). We need to make sure the inode is dirty
4514 * in the transaction for the sake of fsync(), even if nothing has
4515 * changed, because fsync() will not force the log for this transaction
4516 * unless it sees the inode pinned.
4517 */
4518 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004519 if (error)
4520 return error;
4521
4522 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004523 * Update our extent pointer, given that
4524 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4525 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004526 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004527 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004528
4529 /*
4530 * We may have combined previously unwritten space with written space,
4531 * so generate another request.
4532 */
4533 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004534 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004535 return 0;
4536}
4537
Christoph Hellwig44032802011-09-18 20:40:48 +00004538/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004539 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4540 * extent state if necessary. Details behaviour is controlled by the flags
4541 * parameter. Only allocates blocks from a single allocation group, to avoid
4542 * locking problems.
4543 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 * The returned value in "firstblock" from the first call in a transaction
4545 * must be remembered and presented to subsequent calls in "firstblock".
4546 * An upper bound for the number of blocks to be allocated is supplied to
4547 * the first call in "total"; if no allocation group has that many free
4548 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4549 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004550int
4551xfs_bmapi_write(
4552 struct xfs_trans *tp, /* transaction pointer */
4553 struct xfs_inode *ip, /* incore inode */
4554 xfs_fileoff_t bno, /* starting file offs. mapped */
4555 xfs_filblks_t len, /* length to map in file */
4556 int flags, /* XFS_BMAPI_... */
4557 xfs_fsblock_t *firstblock, /* first allocated block
4558 controls a.g. for allocs */
4559 xfs_extlen_t total, /* total blocks needed */
4560 struct xfs_bmbt_irec *mval, /* output: map values */
4561 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004562 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004564 struct xfs_mount *mp = ip->i_mount;
4565 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004566 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004567 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004568 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004569 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004570 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004571 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004572 int whichfork; /* data or attr fork */
4573 char inhole; /* current location is hole in file */
4574 char wasdelay; /* old extent was delayed */
4575
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004577 xfs_fileoff_t orig_bno; /* original block number value */
4578 int orig_flags; /* original flags arg value */
4579 xfs_filblks_t orig_len; /* original value of len arg */
4580 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4581 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
4583 orig_bno = bno;
4584 orig_len = len;
4585 orig_flags = flags;
4586 orig_mval = mval;
4587 orig_nmap = *nmap;
4588#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004589 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004590
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004592 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4593 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4594 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004595 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004596 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004597 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004598 ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK);
4599 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP));
4600 ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP));
Darrick J. Wong60b49842016-10-03 09:11:34 -07004601 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || whichfork != XFS_COW_FORK);
4602 ASSERT(!(flags & XFS_BMAPI_CONVERT) || whichfork != XFS_COW_FORK);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004603
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004604 /* zeroing is for currently only for data extents, not metadata */
4605 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4606 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4607 /*
4608 * we can allocate unwritten extents or pre-zero allocated blocks,
4609 * but it makes no sense to do both at once. This would result in
4610 * zeroing the unwritten extent twice, but it still being an
4611 * unwritten extent....
4612 */
4613 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4614 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4615
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 if (unlikely(XFS_TEST_ERROR(
4617 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004618 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004620 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004621 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004625 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004628
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004629 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004630
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004631 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004633 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004635 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004636 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004637 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004638 }
4639
4640 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4641 error = xfs_iread_extents(tp, ip, whichfork);
4642 if (error)
4643 goto error0;
4644 }
4645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 n = 0;
4647 end = bno + len;
4648 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004649
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004650 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4651 eof = true;
4652 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4653 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004654 bma.tp = tp;
4655 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004656 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004657 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004658 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004659 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004660
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004662 inhole = eof || bma.got.br_startoff > bno;
4663 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004664
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 /*
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004666 * Make sure we only reflink into a hole.
4667 */
4668 if (flags & XFS_BMAPI_REMAP)
4669 ASSERT(inhole);
Darrick J. Wong60b49842016-10-03 09:11:34 -07004670 if (flags & XFS_BMAPI_COWFORK)
4671 ASSERT(!inhole);
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004672
4673 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 * First, deal with the hole before the allocated space
4675 * that we found, if any.
4676 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004677 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004678 bma.eof = eof;
4679 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4680 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004681 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004682 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004683
Dave Chinnera99ebf42011-12-01 11:24:20 +00004684 /*
4685 * There's a 32/64 bit type mismatch between the
4686 * allocation length request (which can be 64 bits in
4687 * length) and the bma length request, which is
4688 * xfs_extlen_t and therefore 32 bits. Hence we have to
4689 * check for 32-bit overflows and handle them here.
4690 */
4691 if (len > (xfs_filblks_t)MAXEXTLEN)
4692 bma.length = MAXEXTLEN;
4693 else
4694 bma.length = len;
4695
4696 ASSERT(len > 0);
4697 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004698 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 if (error)
4700 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004701 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004702 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004703
4704 /*
4705 * If this is a CoW allocation, record the data in
4706 * the refcount btree for orphan recovery.
4707 */
4708 if (whichfork == XFS_COW_FORK) {
4709 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4710 bma.blkno, bma.length);
4711 if (error)
4712 goto error0;
4713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004715
Dave Chinneraef9a892011-09-18 20:40:44 +00004716 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004717 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4718 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
Dave Chinnerb447fe52011-09-18 20:40:51 +00004720 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004721 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004722 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004723 continue;
4724 if (error)
4725 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726
Dave Chinneraef9a892011-09-18 20:40:44 +00004727 /* update the extent map to return */
4728 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4729
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 /*
4731 * If we're done, stop now. Stop when we've allocated
4732 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4733 * the transaction may get too big.
4734 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004735 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004737
4738 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004739 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004740 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4741 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004744
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 /*
4746 * Transform from btree to extents, give it cur.
4747 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004748 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004749 int tmp_logflags = 0;
4750
Dave Chinner29c8d172011-09-18 20:41:00 +00004751 ASSERT(bma.cur);
4752 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004754 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 if (error)
4756 goto error0;
4757 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004760 XFS_IFORK_NEXTENTS(ip, whichfork) >
4761 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763error0:
4764 /*
4765 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004766 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004768 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004770 bma.logflags &= ~xfs_ilog_fext(whichfork);
4771 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004773 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774 /*
4775 * Log whatever the flags say, even if error. Otherwise we might miss
4776 * detecting a case where the data is changed, there's an error,
4777 * and it's not logged so we don't shutdown when we should.
4778 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004779 if (bma.logflags)
4780 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004781
Dave Chinner29c8d172011-09-18 20:41:00 +00004782 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 if (!error) {
4784 ASSERT(*firstblock == NULLFSBLOCK ||
4785 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4786 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004787 bma.cur->bc_private.b.firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004788 (dfops->dop_low &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 XFS_FSB_TO_AGNO(mp, *firstblock) <
4790 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004791 bma.cur->bc_private.b.firstblock)));
4792 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004794 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4796 }
4797 if (!error)
4798 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4799 orig_nmap, *nmap);
4800 return error;
4801}
4802
4803/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004804 * When a delalloc extent is split (e.g., due to a hole punch), the original
4805 * indlen reservation must be shared across the two new extents that are left
4806 * behind.
4807 *
4808 * Given the original reservation and the worst case indlen for the two new
4809 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004810 * reservation fairly across the two new extents. If necessary, steal available
4811 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4812 * ores == 1). The number of stolen blocks is returned. The availability and
4813 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004814 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004815static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004816xfs_bmap_split_indlen(
4817 xfs_filblks_t ores, /* original res. */
4818 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004819 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4820 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004821{
4822 xfs_filblks_t len1 = *indlen1;
4823 xfs_filblks_t len2 = *indlen2;
4824 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004825 xfs_filblks_t stolen = 0;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004826
4827 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004828 * Steal as many blocks as we can to try and satisfy the worst case
4829 * indlen for both new extents.
4830 */
4831 while (nres > ores && avail) {
4832 nres--;
4833 avail--;
4834 stolen++;
4835 }
4836
4837 /*
4838 * The only blocks available are those reserved for the original
4839 * extent and what we can steal from the extent being removed.
4840 * If this still isn't enough to satisfy the combined
4841 * requirements for the two new extents, skim blocks off of each
4842 * of the new reservations until they match what is available.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004843 */
4844 while (nres > ores) {
4845 if (len1) {
4846 len1--;
4847 nres--;
4848 }
4849 if (nres == ores)
4850 break;
4851 if (len2) {
4852 len2--;
4853 nres--;
4854 }
4855 }
4856
4857 *indlen1 = len1;
4858 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004859
4860 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004861}
4862
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004863int
4864xfs_bmap_del_extent_delay(
4865 struct xfs_inode *ip,
4866 int whichfork,
4867 xfs_extnum_t *idx,
4868 struct xfs_bmbt_irec *got,
4869 struct xfs_bmbt_irec *del)
4870{
4871 struct xfs_mount *mp = ip->i_mount;
4872 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4873 struct xfs_bmbt_irec new;
4874 int64_t da_old, da_new, da_diff = 0;
4875 xfs_fileoff_t del_endoff, got_endoff;
4876 xfs_filblks_t got_indlen, new_indlen, stolen;
4877 int error = 0, state = 0;
4878 bool isrt;
4879
4880 XFS_STATS_INC(mp, xs_del_exlist);
4881
4882 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4883 del_endoff = del->br_startoff + del->br_blockcount;
4884 got_endoff = got->br_startoff + got->br_blockcount;
4885 da_old = startblockval(got->br_startblock);
4886 da_new = 0;
4887
4888 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004889 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004890 ASSERT(del->br_blockcount > 0);
4891 ASSERT(got->br_startoff <= del->br_startoff);
4892 ASSERT(got_endoff >= del_endoff);
4893
4894 if (isrt) {
4895 int64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4896
4897 do_div(rtexts, mp->m_sb.sb_rextsize);
4898 xfs_mod_frextents(mp, rtexts);
4899 }
4900
4901 /*
4902 * Update the inode delalloc counter now and wait to update the
4903 * sb counters as we might have to borrow some blocks for the
4904 * indirect block accounting.
4905 */
4906 xfs_trans_reserve_quota_nblks(NULL, ip, -((long)del->br_blockcount), 0,
4907 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4908 ip->i_delayed_blks -= del->br_blockcount;
4909
4910 if (whichfork == XFS_COW_FORK)
4911 state |= BMAP_COWFORK;
4912
4913 if (got->br_startoff == del->br_startoff)
4914 state |= BMAP_LEFT_CONTIG;
4915 if (got_endoff == del_endoff)
4916 state |= BMAP_RIGHT_CONTIG;
4917
4918 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4919 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4920 /*
4921 * Matches the whole extent. Delete the entry.
4922 */
4923 xfs_iext_remove(ip, *idx, 1, state);
4924 --*idx;
4925 break;
4926 case BMAP_LEFT_CONTIG:
4927 /*
4928 * Deleting the first part of the extent.
4929 */
4930 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4931 got->br_startoff = del_endoff;
4932 got->br_blockcount -= del->br_blockcount;
4933 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4934 got->br_blockcount), da_old);
4935 got->br_startblock = nullstartblock((int)da_new);
4936 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4937 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4938 break;
4939 case BMAP_RIGHT_CONTIG:
4940 /*
4941 * Deleting the last part of the extent.
4942 */
4943 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4944 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4945 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4946 got->br_blockcount), da_old);
4947 got->br_startblock = nullstartblock((int)da_new);
4948 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4949 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4950 break;
4951 case 0:
4952 /*
4953 * Deleting the middle of the extent.
4954 *
4955 * Distribute the original indlen reservation across the two new
4956 * extents. Steal blocks from the deleted extent if necessary.
4957 * Stealing blocks simply fudges the fdblocks accounting below.
4958 * Warn if either of the new indlen reservations is zero as this
4959 * can lead to delalloc problems.
4960 */
4961 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4962
4963 got->br_blockcount = del->br_startoff - got->br_startoff;
4964 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4965
4966 new.br_blockcount = got_endoff - del_endoff;
4967 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4968
4969 WARN_ON_ONCE(!got_indlen || !new_indlen);
4970 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4971 del->br_blockcount);
4972
4973 got->br_startblock = nullstartblock((int)got_indlen);
4974 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4975 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4976
4977 new.br_startoff = del_endoff;
4978 new.br_state = got->br_state;
4979 new.br_startblock = nullstartblock((int)new_indlen);
4980
4981 ++*idx;
4982 xfs_iext_insert(ip, *idx, 1, &new, state);
4983
4984 da_new = got_indlen + new_indlen - stolen;
4985 del->br_blockcount -= stolen;
4986 break;
4987 }
4988
4989 ASSERT(da_old >= da_new);
4990 da_diff = da_old - da_new;
4991 if (!isrt)
4992 da_diff += del->br_blockcount;
4993 if (da_diff)
4994 xfs_mod_fdblocks(mp, da_diff, false);
4995 return error;
4996}
4997
4998void
4999xfs_bmap_del_extent_cow(
5000 struct xfs_inode *ip,
5001 xfs_extnum_t *idx,
5002 struct xfs_bmbt_irec *got,
5003 struct xfs_bmbt_irec *del)
5004{
5005 struct xfs_mount *mp = ip->i_mount;
5006 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
5007 struct xfs_bmbt_irec new;
5008 xfs_fileoff_t del_endoff, got_endoff;
5009 int state = BMAP_COWFORK;
5010
5011 XFS_STATS_INC(mp, xs_del_exlist);
5012
5013 del_endoff = del->br_startoff + del->br_blockcount;
5014 got_endoff = got->br_startoff + got->br_blockcount;
5015
5016 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11005017 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005018 ASSERT(del->br_blockcount > 0);
5019 ASSERT(got->br_startoff <= del->br_startoff);
5020 ASSERT(got_endoff >= del_endoff);
5021 ASSERT(!isnullstartblock(got->br_startblock));
5022
5023 if (got->br_startoff == del->br_startoff)
5024 state |= BMAP_LEFT_CONTIG;
5025 if (got_endoff == del_endoff)
5026 state |= BMAP_RIGHT_CONTIG;
5027
5028 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
5029 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
5030 /*
5031 * Matches the whole extent. Delete the entry.
5032 */
5033 xfs_iext_remove(ip, *idx, 1, state);
5034 --*idx;
5035 break;
5036 case BMAP_LEFT_CONTIG:
5037 /*
5038 * Deleting the first part of the extent.
5039 */
5040 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5041 got->br_startoff = del_endoff;
5042 got->br_blockcount -= del->br_blockcount;
5043 got->br_startblock = del->br_startblock + del->br_blockcount;
5044 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5045 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5046 break;
5047 case BMAP_RIGHT_CONTIG:
5048 /*
5049 * Deleting the last part of the extent.
5050 */
5051 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5052 got->br_blockcount -= del->br_blockcount;
5053 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5054 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5055 break;
5056 case 0:
5057 /*
5058 * Deleting the middle of the extent.
5059 */
5060 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5061 got->br_blockcount = del->br_startoff - got->br_startoff;
5062 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5063 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5064
5065 new.br_startoff = del_endoff;
5066 new.br_blockcount = got_endoff - del_endoff;
5067 new.br_state = got->br_state;
5068 new.br_startblock = del->br_startblock + del->br_blockcount;
5069
5070 ++*idx;
5071 xfs_iext_insert(ip, *idx, 1, &new, state);
5072 break;
5073 }
5074}
5075
Brian Fostera9bd24a2016-03-15 11:42:46 +11005076/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11005077 * Called by xfs_bmapi to update file extent records and the btree
5078 * after removing space (or undoing a delayed allocation).
5079 */
5080STATIC int /* error */
5081xfs_bmap_del_extent(
5082 xfs_inode_t *ip, /* incore inode pointer */
5083 xfs_trans_t *tp, /* current transaction pointer */
5084 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005085 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005086 xfs_btree_cur_t *cur, /* if null, not a btree */
5087 xfs_bmbt_irec_t *del, /* data to remove from extents */
5088 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005089 int whichfork, /* data or attr fork */
5090 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005091{
5092 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5093 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5094 xfs_fsblock_t del_endblock=0; /* first block past del */
5095 xfs_fileoff_t del_endoff; /* first offset past del */
5096 int delay; /* current block is delayed allocated */
5097 int do_fx; /* free extent at end of routine */
5098 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5099 int error; /* error return value */
5100 int flags; /* inode logging flags */
5101 xfs_bmbt_irec_t got; /* current extent entry */
5102 xfs_fileoff_t got_endoff; /* first offset past got */
5103 int i; /* temp state */
5104 xfs_ifork_t *ifp; /* inode fork pointer */
5105 xfs_mount_t *mp; /* mount structure */
5106 xfs_filblks_t nblks; /* quota/sb block count */
5107 xfs_bmbt_irec_t new; /* new record to be inserted */
5108 /* REFERENCED */
5109 uint qfield; /* quota field to update */
5110 xfs_filblks_t temp; /* for indirect length calculations */
5111 xfs_filblks_t temp2; /* for indirect length calculations */
5112 int state = 0;
5113
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005114 mp = ip->i_mount;
5115 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005116
5117 if (whichfork == XFS_ATTR_FORK)
5118 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005119 else if (whichfork == XFS_COW_FORK)
5120 state |= BMAP_COWFORK;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005121
Dave Chinner9e5987a72013-02-25 12:31:26 +11005122 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005123 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005124 ASSERT(del->br_blockcount > 0);
5125 ep = xfs_iext_get_ext(ifp, *idx);
5126 xfs_bmbt_get_all(ep, &got);
5127 ASSERT(got.br_startoff <= del->br_startoff);
5128 del_endoff = del->br_startoff + del->br_blockcount;
5129 got_endoff = got.br_startoff + got.br_blockcount;
5130 ASSERT(got_endoff >= del_endoff);
5131 delay = isnullstartblock(got.br_startblock);
5132 ASSERT(isnullstartblock(del->br_startblock) == delay);
5133 flags = 0;
5134 qfield = 0;
5135 error = 0;
5136 /*
5137 * If deleting a real allocation, must free up the disk space.
5138 */
5139 if (!delay) {
5140 flags = XFS_ILOG_CORE;
5141 /*
5142 * Realtime allocation. Free it and record di_nblocks update.
5143 */
5144 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5145 xfs_fsblock_t bno;
5146 xfs_filblks_t len;
5147
5148 ASSERT(do_mod(del->br_blockcount,
5149 mp->m_sb.sb_rextsize) == 0);
5150 ASSERT(do_mod(del->br_startblock,
5151 mp->m_sb.sb_rextsize) == 0);
5152 bno = del->br_startblock;
5153 len = del->br_blockcount;
5154 do_div(bno, mp->m_sb.sb_rextsize);
5155 do_div(len, mp->m_sb.sb_rextsize);
5156 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5157 if (error)
5158 goto done;
5159 do_fx = 0;
5160 nblks = len * mp->m_sb.sb_rextsize;
5161 qfield = XFS_TRANS_DQ_RTBCOUNT;
5162 }
5163 /*
5164 * Ordinary allocation.
5165 */
5166 else {
5167 do_fx = 1;
5168 nblks = del->br_blockcount;
5169 qfield = XFS_TRANS_DQ_BCOUNT;
5170 }
5171 /*
5172 * Set up del_endblock and cur for later.
5173 */
5174 del_endblock = del->br_startblock + del->br_blockcount;
5175 if (cur) {
5176 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5177 got.br_startblock, got.br_blockcount,
5178 &i)))
5179 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005180 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005181 }
5182 da_old = da_new = 0;
5183 } else {
5184 da_old = startblockval(got.br_startblock);
5185 da_new = 0;
5186 nblks = 0;
5187 do_fx = 0;
5188 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005189
Dave Chinner9e5987a72013-02-25 12:31:26 +11005190 /*
5191 * Set flag value to use in switch statement.
5192 * Left-contig is 2, right-contig is 1.
5193 */
5194 switch (((got.br_startoff == del->br_startoff) << 1) |
5195 (got_endoff == del_endoff)) {
5196 case 3:
5197 /*
5198 * Matches the whole extent. Delete the entry.
5199 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005200 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005201 xfs_iext_remove(ip, *idx, 1,
5202 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5203 --*idx;
5204 if (delay)
5205 break;
5206
5207 XFS_IFORK_NEXT_SET(ip, whichfork,
5208 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5209 flags |= XFS_ILOG_CORE;
5210 if (!cur) {
5211 flags |= xfs_ilog_fext(whichfork);
5212 break;
5213 }
5214 if ((error = xfs_btree_delete(cur, &i)))
5215 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005216 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005217 break;
5218
5219 case 2:
5220 /*
5221 * Deleting the first part of the extent.
5222 */
5223 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5224 xfs_bmbt_set_startoff(ep, del_endoff);
5225 temp = got.br_blockcount - del->br_blockcount;
5226 xfs_bmbt_set_blockcount(ep, temp);
5227 if (delay) {
5228 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5229 da_old);
5230 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5231 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5232 da_new = temp;
5233 break;
5234 }
5235 xfs_bmbt_set_startblock(ep, del_endblock);
5236 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5237 if (!cur) {
5238 flags |= xfs_ilog_fext(whichfork);
5239 break;
5240 }
5241 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5242 got.br_blockcount - del->br_blockcount,
5243 got.br_state)))
5244 goto done;
5245 break;
5246
5247 case 1:
5248 /*
5249 * Deleting the last part of the extent.
5250 */
5251 temp = got.br_blockcount - del->br_blockcount;
5252 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5253 xfs_bmbt_set_blockcount(ep, temp);
5254 if (delay) {
5255 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5256 da_old);
5257 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5258 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5259 da_new = temp;
5260 break;
5261 }
5262 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5263 if (!cur) {
5264 flags |= xfs_ilog_fext(whichfork);
5265 break;
5266 }
5267 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5268 got.br_startblock,
5269 got.br_blockcount - del->br_blockcount,
5270 got.br_state)))
5271 goto done;
5272 break;
5273
5274 case 0:
5275 /*
5276 * Deleting the middle of the extent.
5277 */
5278 temp = del->br_startoff - got.br_startoff;
5279 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5280 xfs_bmbt_set_blockcount(ep, temp);
5281 new.br_startoff = del_endoff;
5282 temp2 = got_endoff - del_endoff;
5283 new.br_blockcount = temp2;
5284 new.br_state = got.br_state;
5285 if (!delay) {
5286 new.br_startblock = del_endblock;
5287 flags |= XFS_ILOG_CORE;
5288 if (cur) {
5289 if ((error = xfs_bmbt_update(cur,
5290 got.br_startoff,
5291 got.br_startblock, temp,
5292 got.br_state)))
5293 goto done;
5294 if ((error = xfs_btree_increment(cur, 0, &i)))
5295 goto done;
5296 cur->bc_rec.b = new;
5297 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10005298 if (error && error != -ENOSPC)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005299 goto done;
5300 /*
5301 * If get no-space back from btree insert,
5302 * it tried a split, and we have a zero
5303 * block reservation.
5304 * Fix up our state and return the error.
5305 */
Dave Chinner24513372014-06-25 14:58:08 +10005306 if (error == -ENOSPC) {
Dave Chinner9e5987a72013-02-25 12:31:26 +11005307 /*
5308 * Reset the cursor, don't trust
5309 * it after any insert operation.
5310 */
5311 if ((error = xfs_bmbt_lookup_eq(cur,
5312 got.br_startoff,
5313 got.br_startblock,
5314 temp, &i)))
5315 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005316 XFS_WANT_CORRUPTED_GOTO(mp,
5317 i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005318 /*
5319 * Update the btree record back
5320 * to the original value.
5321 */
5322 if ((error = xfs_bmbt_update(cur,
5323 got.br_startoff,
5324 got.br_startblock,
5325 got.br_blockcount,
5326 got.br_state)))
5327 goto done;
5328 /*
5329 * Reset the extent record back
5330 * to the original value.
5331 */
5332 xfs_bmbt_set_blockcount(ep,
5333 got.br_blockcount);
5334 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005335 error = -ENOSPC;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005336 goto done;
5337 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005338 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005339 } else
5340 flags |= xfs_ilog_fext(whichfork);
5341 XFS_IFORK_NEXT_SET(ip, whichfork,
5342 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5343 } else {
Brian Fosterd34999c2016-03-15 11:42:47 +11005344 xfs_filblks_t stolen;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005345 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005346
5347 /*
5348 * Distribute the original indlen reservation across the
Brian Fosterd34999c2016-03-15 11:42:47 +11005349 * two new extents. Steal blocks from the deleted extent
5350 * if necessary. Stealing blocks simply fudges the
5351 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005352 */
5353 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5354 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c2016-03-15 11:42:47 +11005355 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5356 del->br_blockcount);
5357 da_new = temp + temp2 - stolen;
5358 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005359
5360 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11005361 * Set the reservation for each extent. Warn if either
5362 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005363 */
Brian Fosterd34999c2016-03-15 11:42:47 +11005364 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005365 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005366 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005367 }
5368 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5369 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5370 ++*idx;
5371 break;
5372 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005373
5374 /* remove reverse mapping */
5375 if (!delay) {
5376 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5377 if (error)
5378 goto done;
5379 }
5380
Dave Chinner9e5987a72013-02-25 12:31:26 +11005381 /*
5382 * If we need to, add to list of extents to delete.
5383 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005384 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005385 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5386 error = xfs_refcount_decrease_extent(mp, dfops, del);
5387 if (error)
5388 goto done;
5389 } else
5390 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5391 del->br_blockcount, NULL);
5392 }
5393
Dave Chinner9e5987a72013-02-25 12:31:26 +11005394 /*
5395 * Adjust inode # blocks in the file.
5396 */
5397 if (nblks)
5398 ip->i_d.di_nblocks -= nblks;
5399 /*
5400 * Adjust quota data.
5401 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005402 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005403 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5404
5405 /*
5406 * Account for change in delayed indirect blocks.
5407 * Nothing to do for disk quota accounting here.
5408 */
5409 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005410 if (da_old > da_new)
5411 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005412done:
5413 *logflagsp = flags;
5414 return error;
5415}
5416
5417/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 * Unmap (remove) blocks from a file.
5419 * If nexts is nonzero then the number of extents to remove is limited to
5420 * that value. If not all extents in the block range can be removed then
5421 * *done is set.
5422 */
5423int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005424__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 xfs_trans_t *tp, /* transaction pointer */
5426 struct xfs_inode *ip, /* incore inode */
5427 xfs_fileoff_t bno, /* starting offset to unmap */
Darrick J. Wong44535932016-10-03 09:11:29 -07005428 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 int flags, /* misc flags */
5430 xfs_extnum_t nexts, /* number of extents max */
5431 xfs_fsblock_t *firstblock, /* first allocated block
5432 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005433 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434{
5435 xfs_btree_cur_t *cur; /* bmap btree cursor */
5436 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 int error; /* error return value */
5438 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005439 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 xfs_ifork_t *ifp; /* inode fork pointer */
5441 int isrt; /* freeing in rt area */
5442 xfs_extnum_t lastx; /* last extent index used */
5443 int logflags; /* transaction logging flags */
5444 xfs_extlen_t mod; /* rt extent offset */
5445 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 xfs_fileoff_t start; /* first file offset deleted */
5447 int tmp_logflags; /* partial logging flags */
5448 int wasdel; /* was a delayed alloc extent */
5449 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005451 xfs_filblks_t len = *rlen; /* length to unmap in file */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005453 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5454
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005455 whichfork = xfs_bmapi_whichfork(flags);
5456 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005457 ifp = XFS_IFORK_PTR(ip, whichfork);
5458 if (unlikely(
5459 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5460 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5461 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5462 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005463 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 }
5465 mp = ip->i_mount;
5466 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005467 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005468
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005469 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 ASSERT(len > 0);
5471 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005472
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5474 (error = xfs_iread_extents(tp, ip, whichfork)))
5475 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005476 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005477 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 return 0;
5479 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005480 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005481 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 start = bno;
5483 bno = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005484
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 /*
5486 * Check to see if the given block number is past the end of the
5487 * file, back up to the last block if so...
5488 */
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005489 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5490 ASSERT(lastx > 0);
5491 xfs_iext_get_extent(ifp, --lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 bno = got.br_startoff + got.br_blockcount - 1;
5493 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005494
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 logflags = 0;
5496 if (ifp->if_flags & XFS_IFBROOT) {
5497 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005498 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005500 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 cur->bc_private.b.flags = 0;
5502 } else
5503 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005504
5505 if (isrt) {
5506 /*
5507 * Synchronize by locking the bitmap inode.
5508 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005509 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005510 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005511 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5512 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005513 }
5514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 extno = 0;
5516 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5517 (nexts == 0 || extno < nexts)) {
5518 /*
5519 * Is the found extent after a hole in which bno lives?
5520 * Just back up to the previous extent, if so.
5521 */
5522 if (got.br_startoff > bno) {
5523 if (--lastx < 0)
5524 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005525 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 }
5527 /*
5528 * Is the last block of this extent before the range
5529 * we're supposed to delete? If so, we're done.
5530 */
5531 bno = XFS_FILEOFF_MIN(bno,
5532 got.br_startoff + got.br_blockcount - 1);
5533 if (bno < start)
5534 break;
5535 /*
5536 * Then deal with the (possibly delayed) allocated space
5537 * we found.
5538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005540 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005541 if (got.br_startoff < start) {
5542 del.br_startoff = start;
5543 del.br_blockcount -= start - got.br_startoff;
5544 if (!wasdel)
5545 del.br_startblock += start - got.br_startoff;
5546 }
5547 if (del.br_startoff + del.br_blockcount > bno + 1)
5548 del.br_blockcount = bno + 1 - del.br_startoff;
5549 sum = del.br_startblock + del.br_blockcount;
5550 if (isrt &&
5551 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5552 /*
5553 * Realtime extent not lined up at the end.
5554 * The extent could have been split into written
5555 * and unwritten pieces, or we could just be
5556 * unmapping part of it. But we can't really
5557 * get rid of part of a realtime extent.
5558 */
5559 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005560 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561 /*
5562 * This piece is unwritten, or we're not
5563 * using unwritten extents. Skip over it.
5564 */
5565 ASSERT(bno >= mod);
5566 bno -= mod > del.br_blockcount ?
5567 del.br_blockcount : mod;
5568 if (bno < got.br_startoff) {
5569 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005570 xfs_bmbt_get_all(xfs_iext_get_ext(
5571 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 }
5573 continue;
5574 }
5575 /*
5576 * It's written, turn it unwritten.
5577 * This is better than zeroing it.
5578 */
5579 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005580 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 /*
5582 * If this spans a realtime extent boundary,
5583 * chop it back to the start of the one we end at.
5584 */
5585 if (del.br_blockcount > mod) {
5586 del.br_startoff += del.br_blockcount - mod;
5587 del.br_startblock += del.br_blockcount - mod;
5588 del.br_blockcount = mod;
5589 }
5590 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005591 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005592 &lastx, &cur, &del, firstblock, dfops,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005593 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 if (error)
5595 goto error0;
5596 goto nodelete;
5597 }
5598 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5599 /*
5600 * Realtime extent is lined up at the end but not
5601 * at the front. We'll get rid of full extents if
5602 * we can.
5603 */
5604 mod = mp->m_sb.sb_rextsize - mod;
5605 if (del.br_blockcount > mod) {
5606 del.br_blockcount -= mod;
5607 del.br_startoff += mod;
5608 del.br_startblock += mod;
5609 } else if ((del.br_startoff == start &&
5610 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005611 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005612 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 /*
5614 * Can't make it unwritten. There isn't
5615 * a full extent here so just skip it.
5616 */
5617 ASSERT(bno >= del.br_blockcount);
5618 bno -= del.br_blockcount;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005619 if (got.br_startoff > bno && --lastx >= 0)
5620 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 continue;
5622 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005623 struct xfs_bmbt_irec prev;
5624
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 /*
5626 * This one is already unwritten.
5627 * It must have a written left neighbor.
5628 * Unwrite the killed part of that one and
5629 * try again.
5630 */
5631 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005632 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005634 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 ASSERT(del.br_startblock ==
5636 prev.br_startblock + prev.br_blockcount);
5637 if (prev.br_startoff < start) {
5638 mod = start - prev.br_startoff;
5639 prev.br_blockcount -= mod;
5640 prev.br_startblock += mod;
5641 prev.br_startoff = start;
5642 }
5643 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005644 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005645 error = xfs_bmap_add_extent_unwritten_real(tp,
5646 ip, &lastx, &cur, &prev,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005647 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 if (error)
5649 goto error0;
5650 goto nodelete;
5651 } else {
5652 ASSERT(del.br_state == XFS_EXT_NORM);
5653 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005654 error = xfs_bmap_add_extent_unwritten_real(tp,
5655 ip, &lastx, &cur, &del,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005656 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005657 if (error)
5658 goto error0;
5659 goto nodelete;
5660 }
5661 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005662
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 /*
5664 * If it's the case where the directory code is running
5665 * with no block reservation, and the deleted block is in
5666 * the middle of its extent, and the resulting insert
5667 * of an extent would cause transformation to btree format,
5668 * then reject it. The calling code will then swap
5669 * blocks around instead.
5670 * We have to do this now, rather than waiting for the
5671 * conversion to btree format, since the transaction
5672 * will be dirty.
5673 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005674 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005676 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5677 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005678 del.br_startoff > got.br_startoff &&
5679 del.br_startoff + del.br_blockcount <
5680 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005681 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005682 goto error0;
5683 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005684
5685 /*
5686 * Unreserve quota and update realtime free space, if
5687 * appropriate. If delayed allocation, update the inode delalloc
5688 * counter now and wait to update the sb counters as
5689 * xfs_bmap_del_extent() might need to borrow some blocks.
5690 */
5691 if (wasdel) {
5692 ASSERT(startblockval(del.br_startblock) > 0);
5693 if (isrt) {
5694 xfs_filblks_t rtexts;
5695
5696 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5697 do_div(rtexts, mp->m_sb.sb_rextsize);
5698 xfs_mod_frextents(mp, (int64_t)rtexts);
5699 (void)xfs_trans_reserve_quota_nblks(NULL,
5700 ip, -((long)del.br_blockcount), 0,
5701 XFS_QMOPT_RES_RTBLKS);
5702 } else {
5703 (void)xfs_trans_reserve_quota_nblks(NULL,
5704 ip, -((long)del.br_blockcount), 0,
5705 XFS_QMOPT_RES_REGBLKS);
5706 }
5707 ip->i_delayed_blks -= del.br_blockcount;
5708 if (cur)
5709 cur->bc_private.b.flags |=
5710 XFS_BTCUR_BPRV_WASDEL;
5711 } else if (cur)
5712 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5713
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005714 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005715 &tmp_logflags, whichfork, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 logflags |= tmp_logflags;
5717 if (error)
5718 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005719
5720 if (!isrt && wasdel)
5721 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5722
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 bno = del.br_startoff - 1;
5724nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 /*
5726 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005727 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005729 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005730 xfs_iext_get_extent(ifp, lastx, &got);
5731 if (got.br_startoff > bno && --lastx >= 0)
5732 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 extno++;
5735 }
5736 }
Darrick J. Wong44535932016-10-03 09:11:29 -07005737 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5738 *rlen = 0;
5739 else
5740 *rlen = bno - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005741
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 /*
5743 * Convert to a btree if necessary.
5744 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005745 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005747 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 &cur, 0, &tmp_logflags, whichfork);
5749 logflags |= tmp_logflags;
5750 if (error)
5751 goto error0;
5752 }
5753 /*
5754 * transform from btree to extents, give it cur
5755 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005756 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 ASSERT(cur != NULL);
5758 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5759 whichfork);
5760 logflags |= tmp_logflags;
5761 if (error)
5762 goto error0;
5763 }
5764 /*
5765 * transform from extents to local?
5766 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 error = 0;
5768error0:
5769 /*
5770 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005771 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005773 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005775 logflags &= ~xfs_ilog_fext(whichfork);
5776 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005778 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 /*
5780 * Log inode even in the error case, if the transaction
5781 * is dirty we'll need to shut down the filesystem.
5782 */
5783 if (logflags)
5784 xfs_trans_log_inode(tp, ip, logflags);
5785 if (cur) {
5786 if (!error) {
5787 *firstblock = cur->bc_private.b.firstblock;
5788 cur->bc_private.b.allocated = 0;
5789 }
5790 xfs_btree_del_cursor(cur,
5791 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5792 }
5793 return error;
5794}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005795
Darrick J. Wong44535932016-10-03 09:11:29 -07005796/* Unmap a range of a file. */
5797int
5798xfs_bunmapi(
5799 xfs_trans_t *tp,
5800 struct xfs_inode *ip,
5801 xfs_fileoff_t bno,
5802 xfs_filblks_t len,
5803 int flags,
5804 xfs_extnum_t nexts,
5805 xfs_fsblock_t *firstblock,
5806 struct xfs_defer_ops *dfops,
5807 int *done)
5808{
5809 int error;
5810
5811 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5812 dfops);
5813 *done = (len == 0);
5814 return error;
5815}
5816
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005817/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005818 * Determine whether an extent shift can be accomplished by a merge with the
5819 * extent that precedes the target hole of the shift.
5820 */
5821STATIC bool
5822xfs_bmse_can_merge(
5823 struct xfs_bmbt_irec *left, /* preceding extent */
5824 struct xfs_bmbt_irec *got, /* current extent to shift */
5825 xfs_fileoff_t shift) /* shift fsb */
5826{
5827 xfs_fileoff_t startoff;
5828
5829 startoff = got->br_startoff - shift;
5830
5831 /*
5832 * The extent, once shifted, must be adjacent in-file and on-disk with
5833 * the preceding extent.
5834 */
5835 if ((left->br_startoff + left->br_blockcount != startoff) ||
5836 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5837 (left->br_state != got->br_state) ||
5838 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5839 return false;
5840
5841 return true;
5842}
5843
5844/*
5845 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5846 * hole in the file. If an extent shift would result in the extent being fully
5847 * adjacent to the extent that currently precedes the hole, we can merge with
5848 * the preceding extent rather than do the shift.
5849 *
5850 * This function assumes the caller has verified a shift-by-merge is possible
5851 * with the provided extents via xfs_bmse_can_merge().
5852 */
5853STATIC int
5854xfs_bmse_merge(
5855 struct xfs_inode *ip,
5856 int whichfork,
5857 xfs_fileoff_t shift, /* shift fsb */
5858 int current_ext, /* idx of gotp */
5859 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5860 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5861 struct xfs_btree_cur *cur,
5862 int *logflags) /* output */
5863{
Brian Fosterddb19e32014-09-23 15:38:09 +10005864 struct xfs_bmbt_irec got;
5865 struct xfs_bmbt_irec left;
5866 xfs_filblks_t blockcount;
5867 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005868 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005869
Brian Fosterddb19e32014-09-23 15:38:09 +10005870 xfs_bmbt_get_all(gotp, &got);
5871 xfs_bmbt_get_all(leftp, &left);
5872 blockcount = left.br_blockcount + got.br_blockcount;
5873
5874 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5875 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5876 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5877
5878 /*
5879 * Merge the in-core extents. Note that the host record pointers and
5880 * current_ext index are invalid once the extent has been removed via
5881 * xfs_iext_remove().
5882 */
5883 xfs_bmbt_set_blockcount(leftp, blockcount);
5884 xfs_iext_remove(ip, current_ext, 1, 0);
5885
5886 /*
5887 * Update the on-disk extent count, the btree if necessary and log the
5888 * inode.
5889 */
5890 XFS_IFORK_NEXT_SET(ip, whichfork,
5891 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5892 *logflags |= XFS_ILOG_CORE;
5893 if (!cur) {
5894 *logflags |= XFS_ILOG_DEXT;
5895 return 0;
5896 }
5897
5898 /* lookup and remove the extent to merge */
5899 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5900 got.br_blockcount, &i);
5901 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005902 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005903 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005904
5905 error = xfs_btree_delete(cur, &i);
5906 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005907 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005908 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005909
5910 /* lookup and update size of the previous extent */
5911 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5912 left.br_blockcount, &i);
5913 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005914 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005915 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005916
5917 left.br_blockcount = blockcount;
5918
Dave Chinner4db431f2014-12-04 09:42:40 +11005919 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5920 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005921}
5922
5923/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005924 * Shift a single extent.
5925 */
5926STATIC int
5927xfs_bmse_shift_one(
5928 struct xfs_inode *ip,
5929 int whichfork,
5930 xfs_fileoff_t offset_shift_fsb,
5931 int *current_ext,
5932 struct xfs_bmbt_rec_host *gotp,
5933 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005934 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005935 enum shift_direction direction,
5936 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005937{
5938 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005939 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005940 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005941 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005942 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005943 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005944 int error;
5945 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005946 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005947
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005948 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005949 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005950 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005951
5952 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005953
Brian Fosterf71721d2014-09-23 15:39:05 +10005954 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005955 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005956
Namjae Jeona904b1c2015-03-25 15:08:56 +11005957 if (direction == SHIFT_LEFT) {
5958 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005959
Namjae Jeona904b1c2015-03-25 15:08:56 +11005960 /*
5961 * Check for merge if we've got an extent to the left,
5962 * otherwise make sure there's enough room at the start
5963 * of the file for the shift.
5964 */
5965 if (!*current_ext) {
5966 if (got.br_startoff < offset_shift_fsb)
5967 return -EINVAL;
5968 goto update_current_ext;
5969 }
5970 /*
5971 * grab the left extent and check for a large
5972 * enough hole.
5973 */
5974 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5975 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5976
5977 if (startoff <
5978 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005979 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005980
Dave Chinnerb11bd672014-12-04 09:42:24 +11005981 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005982 if (xfs_bmse_can_merge(&adj_irec, &got,
5983 offset_shift_fsb)) {
Darrick J. Wong9c194642016-08-03 12:16:05 +10005984 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5985 *current_ext, gotp, adj_irecp,
5986 cur, logflags);
5987 if (error)
5988 return error;
5989 adj_irec = got;
5990 goto update_rmap;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005991 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005992 } else {
5993 startoff = got.br_startoff + offset_shift_fsb;
5994 /* nothing to move if this is the last extent */
5995 if (*current_ext >= (total_extents - 1))
5996 goto update_current_ext;
5997 /*
5998 * If this is not the last extent in the file, make sure there
5999 * is enough room between current extent and next extent for
6000 * accommodating the shift.
6001 */
6002 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
6003 xfs_bmbt_get_all(adj_irecp, &adj_irec);
6004 if (startoff + got.br_blockcount > adj_irec.br_startoff)
6005 return -EINVAL;
6006 /*
6007 * Unlike a left shift (which involves a hole punch),
6008 * a right shift does not modify extent neighbors
6009 * in any way. We should never find mergeable extents
6010 * in this scenario. Check anyways and warn if we
6011 * encounter two extents that could be one.
6012 */
6013 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
6014 WARN_ON_ONCE(1);
6015 }
Brian Fostera979bdf2014-09-23 15:39:04 +10006016 /*
6017 * Increment the extent index for the next iteration, update the start
6018 * offset of the in-core extent and update the btree if applicable.
6019 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11006020update_current_ext:
6021 if (direction == SHIFT_LEFT)
6022 (*current_ext)++;
6023 else
6024 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10006025 xfs_bmbt_set_startoff(gotp, startoff);
6026 *logflags |= XFS_ILOG_CORE;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006027 adj_irec = got;
Brian Fostera979bdf2014-09-23 15:39:04 +10006028 if (!cur) {
6029 *logflags |= XFS_ILOG_DEXT;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006030 goto update_rmap;
Brian Fostera979bdf2014-09-23 15:39:04 +10006031 }
6032
6033 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
6034 got.br_blockcount, &i);
6035 if (error)
6036 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11006037 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10006038
6039 got.br_startoff = startoff;
Darrick J. Wong9c194642016-08-03 12:16:05 +10006040 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
6041 got.br_blockcount, got.br_state);
6042 if (error)
6043 return error;
6044
6045update_rmap:
6046 /* update reverse mapping */
6047 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
6048 if (error)
6049 return error;
6050 adj_irec.br_startoff = startoff;
6051 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
Brian Fostera979bdf2014-09-23 15:39:04 +10006052}
6053
6054/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11006055 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006056 *
Brian Foster2c845f52014-09-23 15:37:09 +10006057 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11006058 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10006059 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6060 * is the length by which each extent is shifted. If there is no hole to shift
6061 * the extents into, this will be considered invalid operation and we abort
6062 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006063 */
6064int
6065xfs_bmap_shift_extents(
6066 struct xfs_trans *tp,
6067 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006068 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006069 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10006070 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006071 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006072 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006073 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006074 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006075 int num_exts)
6076{
Brian Fosterca446d82014-09-02 12:12:53 +10006077 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006078 struct xfs_bmbt_rec_host *gotp;
6079 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006080 struct xfs_mount *mp = ip->i_mount;
6081 struct xfs_ifork *ifp;
6082 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10006083 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006084 xfs_extnum_t total_extents;
6085 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006086 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006087 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10006088 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006089
6090 if (unlikely(XFS_TEST_ERROR(
6091 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6092 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6093 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6094 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6095 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10006096 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006097 }
6098
6099 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10006100 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006101
Brian Foster2c845f52014-09-23 15:37:09 +10006102 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6103 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006104 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6105 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006106
6107 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006108 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6109 /* Read in all the extents */
6110 error = xfs_iread_extents(tp, ip, whichfork);
6111 if (error)
6112 return error;
6113 }
6114
Brian Fosterddb19e32014-09-23 15:38:09 +10006115 if (ifp->if_flags & XFS_IFBROOT) {
6116 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6117 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006118 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006119 cur->bc_private.b.flags = 0;
6120 }
6121
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006122 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006123 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006124 * are collapsing out, so we cannot use the count of real extents here.
6125 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006126 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006127 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006128 if (total_extents == 0) {
6129 *done = 1;
6130 goto del_cursor;
6131 }
6132
6133 /*
6134 * In case of first right shift, we need to initialize next_fsb
6135 */
6136 if (*next_fsb == NULLFSBLOCK) {
6137 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6138 xfs_bmbt_get_all(gotp, &got);
6139 *next_fsb = got.br_startoff;
6140 if (stop_fsb > *next_fsb) {
6141 *done = 1;
6142 goto del_cursor;
6143 }
6144 }
6145
6146 /* Lookup the extent index at which we have to stop */
6147 if (direction == SHIFT_RIGHT) {
6148 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6149 /* Make stop_extent exclusive of shift range */
6150 stop_extent--;
6151 } else
6152 stop_extent = total_extents;
6153
6154 /*
6155 * Look up the extent index for the fsb where we start shifting. We can
6156 * henceforth iterate with current_ext as extent list changes are locked
6157 * out via ilock.
6158 *
6159 * gotp can be null in 2 cases: 1) if there are no extents or 2)
6160 * *next_fsb lies in a hole beyond which there are no extents. Either
6161 * way, we are done.
6162 */
6163 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
6164 if (!gotp) {
6165 *done = 1;
6166 goto del_cursor;
6167 }
6168
6169 /* some sanity checking before we finally start shifting extents */
6170 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6171 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6172 error = -EIO;
6173 goto del_cursor;
6174 }
6175
6176 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006177 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006178 &current_ext, gotp, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006179 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006180 if (error)
6181 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006182 /*
6183 * If there was an extent merge during the shift, the extent
6184 * count can change. Update the total and grade the next record.
6185 */
6186 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006187 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006188 stop_extent = total_extents;
6189 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006190
Namjae Jeona904b1c2015-03-25 15:08:56 +11006191 if (current_ext == stop_extent) {
6192 *done = 1;
6193 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006194 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006195 }
Brian Fosterddb19e32014-09-23 15:38:09 +10006196 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006197 }
6198
Namjae Jeona904b1c2015-03-25 15:08:56 +11006199 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006200 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10006201 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10006202 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006203
6204del_cursor:
6205 if (cur)
6206 xfs_btree_del_cursor(cur,
6207 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6208
Brian Fosterca446d82014-09-02 12:12:53 +10006209 if (logflags)
6210 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006211
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006212 return error;
6213}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006214
6215/*
6216 * Splits an extent into two extents at split_fsb block such that it is
6217 * the first block of the current_ext. @current_ext is a target extent
6218 * to be split. @split_fsb is a block where the extents is split.
6219 * If split_fsb lies in a hole or the first block of extents, just return 0.
6220 */
6221STATIC int
6222xfs_bmap_split_extent_at(
6223 struct xfs_trans *tp,
6224 struct xfs_inode *ip,
6225 xfs_fileoff_t split_fsb,
6226 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006227 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006228{
6229 int whichfork = XFS_DATA_FORK;
6230 struct xfs_btree_cur *cur = NULL;
6231 struct xfs_bmbt_rec_host *gotp;
6232 struct xfs_bmbt_irec got;
6233 struct xfs_bmbt_irec new; /* split extent */
6234 struct xfs_mount *mp = ip->i_mount;
6235 struct xfs_ifork *ifp;
6236 xfs_fsblock_t gotblkcnt; /* new block count for got */
6237 xfs_extnum_t current_ext;
6238 int error = 0;
6239 int logflags = 0;
6240 int i = 0;
6241
6242 if (unlikely(XFS_TEST_ERROR(
6243 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6244 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6245 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6246 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6247 XFS_ERRLEVEL_LOW, mp);
6248 return -EFSCORRUPTED;
6249 }
6250
6251 if (XFS_FORCED_SHUTDOWN(mp))
6252 return -EIO;
6253
6254 ifp = XFS_IFORK_PTR(ip, whichfork);
6255 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6256 /* Read in all the extents */
6257 error = xfs_iread_extents(tp, ip, whichfork);
6258 if (error)
6259 return error;
6260 }
6261
6262 /*
6263 * gotp can be null in 2 cases: 1) if there are no extents
6264 * or 2) split_fsb lies in a hole beyond which there are
6265 * no extents. Either way, we are done.
6266 */
6267 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
6268 if (!gotp)
6269 return 0;
6270
6271 xfs_bmbt_get_all(gotp, &got);
6272
6273 /*
6274 * Check split_fsb lies in a hole or the start boundary offset
6275 * of the extent.
6276 */
6277 if (got.br_startoff >= split_fsb)
6278 return 0;
6279
6280 gotblkcnt = split_fsb - got.br_startoff;
6281 new.br_startoff = split_fsb;
6282 new.br_startblock = got.br_startblock + gotblkcnt;
6283 new.br_blockcount = got.br_blockcount - gotblkcnt;
6284 new.br_state = got.br_state;
6285
6286 if (ifp->if_flags & XFS_IFBROOT) {
6287 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6288 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006289 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006290 cur->bc_private.b.flags = 0;
6291 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6292 got.br_startblock,
6293 got.br_blockcount,
6294 &i);
6295 if (error)
6296 goto del_cursor;
6297 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6298 }
6299
6300 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6301 got.br_blockcount = gotblkcnt;
6302
6303 logflags = XFS_ILOG_CORE;
6304 if (cur) {
6305 error = xfs_bmbt_update(cur, got.br_startoff,
6306 got.br_startblock,
6307 got.br_blockcount,
6308 got.br_state);
6309 if (error)
6310 goto del_cursor;
6311 } else
6312 logflags |= XFS_ILOG_DEXT;
6313
6314 /* Add new extent */
6315 current_ext++;
6316 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6317 XFS_IFORK_NEXT_SET(ip, whichfork,
6318 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6319
6320 if (cur) {
6321 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6322 new.br_startblock, new.br_blockcount,
6323 &i);
6324 if (error)
6325 goto del_cursor;
6326 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6327 cur->bc_rec.b.br_state = new.br_state;
6328
6329 error = xfs_btree_insert(cur, &i);
6330 if (error)
6331 goto del_cursor;
6332 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6333 }
6334
6335 /*
6336 * Convert to a btree if necessary.
6337 */
6338 if (xfs_bmap_needs_btree(ip, whichfork)) {
6339 int tmp_logflags; /* partial log flag return val */
6340
6341 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006342 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006343 &cur, 0, &tmp_logflags, whichfork);
6344 logflags |= tmp_logflags;
6345 }
6346
6347del_cursor:
6348 if (cur) {
6349 cur->bc_private.b.allocated = 0;
6350 xfs_btree_del_cursor(cur,
6351 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6352 }
6353
6354 if (logflags)
6355 xfs_trans_log_inode(tp, ip, logflags);
6356 return error;
6357}
6358
6359int
6360xfs_bmap_split_extent(
6361 struct xfs_inode *ip,
6362 xfs_fileoff_t split_fsb)
6363{
6364 struct xfs_mount *mp = ip->i_mount;
6365 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006366 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006367 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006368 int error;
6369
Christoph Hellwig253f4912016-04-06 09:19:55 +10006370 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6371 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6372 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006373 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006374
6375 xfs_ilock(ip, XFS_ILOCK_EXCL);
6376 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6377
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006378 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006379
6380 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006381 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006382 if (error)
6383 goto out;
6384
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006385 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006386 if (error)
6387 goto out;
6388
Christoph Hellwig70393312015-06-04 13:48:08 +10006389 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006390
6391out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006392 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006393 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006394 return error;
6395}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006396
6397/* Deferred mapping is only for real extents in the data fork. */
6398static bool
6399xfs_bmap_is_update_needed(
6400 struct xfs_bmbt_irec *bmap)
6401{
6402 return bmap->br_startblock != HOLESTARTBLOCK &&
6403 bmap->br_startblock != DELAYSTARTBLOCK;
6404}
6405
6406/* Record a bmap intent. */
6407static int
6408__xfs_bmap_add(
6409 struct xfs_mount *mp,
6410 struct xfs_defer_ops *dfops,
6411 enum xfs_bmap_intent_type type,
6412 struct xfs_inode *ip,
6413 int whichfork,
6414 struct xfs_bmbt_irec *bmap)
6415{
6416 int error;
6417 struct xfs_bmap_intent *bi;
6418
6419 trace_xfs_bmap_defer(mp,
6420 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6421 type,
6422 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6423 ip->i_ino, whichfork,
6424 bmap->br_startoff,
6425 bmap->br_blockcount,
6426 bmap->br_state);
6427
6428 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6429 INIT_LIST_HEAD(&bi->bi_list);
6430 bi->bi_type = type;
6431 bi->bi_owner = ip;
6432 bi->bi_whichfork = whichfork;
6433 bi->bi_bmap = *bmap;
6434
6435 error = xfs_defer_join(dfops, bi->bi_owner);
6436 if (error) {
6437 kmem_free(bi);
6438 return error;
6439 }
6440
6441 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6442 return 0;
6443}
6444
6445/* Map an extent into a file. */
6446int
6447xfs_bmap_map_extent(
6448 struct xfs_mount *mp,
6449 struct xfs_defer_ops *dfops,
6450 struct xfs_inode *ip,
6451 struct xfs_bmbt_irec *PREV)
6452{
6453 if (!xfs_bmap_is_update_needed(PREV))
6454 return 0;
6455
6456 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6457 XFS_DATA_FORK, PREV);
6458}
6459
6460/* Unmap an extent out of a file. */
6461int
6462xfs_bmap_unmap_extent(
6463 struct xfs_mount *mp,
6464 struct xfs_defer_ops *dfops,
6465 struct xfs_inode *ip,
6466 struct xfs_bmbt_irec *PREV)
6467{
6468 if (!xfs_bmap_is_update_needed(PREV))
6469 return 0;
6470
6471 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6472 XFS_DATA_FORK, PREV);
6473}
6474
6475/*
6476 * Process one of the deferred bmap operations. We pass back the
6477 * btree cursor to maintain our lock on the bmapbt between calls.
6478 */
6479int
6480xfs_bmap_finish_one(
6481 struct xfs_trans *tp,
6482 struct xfs_defer_ops *dfops,
6483 struct xfs_inode *ip,
6484 enum xfs_bmap_intent_type type,
6485 int whichfork,
6486 xfs_fileoff_t startoff,
6487 xfs_fsblock_t startblock,
6488 xfs_filblks_t blockcount,
6489 xfs_exntst_t state)
6490{
6491 struct xfs_bmbt_irec bmap;
6492 int nimaps = 1;
6493 xfs_fsblock_t firstfsb;
6494 int flags = XFS_BMAPI_REMAP;
6495 int done;
6496 int error = 0;
6497
6498 bmap.br_startblock = startblock;
6499 bmap.br_startoff = startoff;
6500 bmap.br_blockcount = blockcount;
6501 bmap.br_state = state;
6502
6503 trace_xfs_bmap_deferred(tp->t_mountp,
6504 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6505 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6506 ip->i_ino, whichfork, startoff, blockcount, state);
6507
6508 if (whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK)
6509 return -EFSCORRUPTED;
6510 if (whichfork == XFS_ATTR_FORK)
6511 flags |= XFS_BMAPI_ATTRFORK;
6512
6513 if (XFS_TEST_ERROR(false, tp->t_mountp,
6514 XFS_ERRTAG_BMAP_FINISH_ONE,
6515 XFS_RANDOM_BMAP_FINISH_ONE))
6516 return -EIO;
6517
6518 switch (type) {
6519 case XFS_BMAP_MAP:
6520 firstfsb = bmap.br_startblock;
6521 error = xfs_bmapi_write(tp, ip, bmap.br_startoff,
6522 bmap.br_blockcount, flags, &firstfsb,
6523 bmap.br_blockcount, &bmap, &nimaps,
6524 dfops);
6525 break;
6526 case XFS_BMAP_UNMAP:
6527 error = xfs_bunmapi(tp, ip, bmap.br_startoff,
6528 bmap.br_blockcount, flags, 1, &firstfsb,
6529 dfops, &done);
6530 ASSERT(done);
6531 break;
6532 default:
6533 ASSERT(0);
6534 error = -EFSCORRUPTED;
6535 }
6536
6537 return error;
6538}