blob: 1ec3087bbb975206ea73fc3999de6f51942b5f4e [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 Chinner70a9883c2013-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 Chinner9e5987a2013-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 Chinner9e5987a2013-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 Chinner9e5987a2013-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 Chinner9e5987a2013-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{
144 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
145 XFS_IFORK_NEXTENTS(ip, whichfork) >
146 XFS_IFORK_MAXEXT(ip, whichfork);
147}
148
149/*
150 * Check if the inode should be converted to extent format.
151 */
152static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
153{
154 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
155 XFS_IFORK_NEXTENTS(ip, whichfork) <=
156 XFS_IFORK_MAXEXT(ip, whichfork);
157}
158
159/*
160 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100161 * by [off, bno, len, state].
162 * This either works (return 0) or gets an EFSCORRUPTED error.
163 */
164STATIC int
165xfs_bmbt_update(
166 struct xfs_btree_cur *cur,
167 xfs_fileoff_t off,
168 xfs_fsblock_t bno,
169 xfs_filblks_t len,
170 xfs_exntst_t state)
171{
172 union xfs_btree_rec rec;
173
174 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
175 return xfs_btree_update(cur, &rec);
176}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100179 * Compute the worst-case number of indirect blocks that will be used
180 * for ip's delayed extent of length "len".
181 */
182STATIC xfs_filblks_t
183xfs_bmap_worst_indlen(
184 xfs_inode_t *ip, /* incore inode pointer */
185 xfs_filblks_t len) /* delayed extent length */
186{
187 int level; /* btree level number */
188 int maxrecs; /* maximum record count at this level */
189 xfs_mount_t *mp; /* mount structure */
190 xfs_filblks_t rval; /* return value */
191
192 mp = ip->i_mount;
193 maxrecs = mp->m_bmap_dmxr[0];
194 for (level = 0, rval = 0;
195 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
196 level++) {
197 len += maxrecs - 1;
198 do_div(len, maxrecs);
199 rval += len;
200 if (len == 1)
201 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
202 level - 1;
203 if (level == 0)
204 maxrecs = mp->m_bmap_dmxr[1];
205 }
206 return rval;
207}
208
209/*
210 * Calculate the default attribute fork offset for newly created inodes.
211 */
212uint
213xfs_default_attroffset(
214 struct xfs_inode *ip)
215{
216 struct xfs_mount *mp = ip->i_mount;
217 uint offset;
218
219 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100220 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100221 XFS_BMDR_SPACE_CALC(MINABTPTRS);
222 } else {
223 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
224 }
225
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100226 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100227 return offset;
228}
229
230/*
231 * Helper routine to reset inode di_forkoff field when switching
232 * attribute fork from local to extent format - we reset it where
233 * possible to make space available for inline data fork extents.
234 */
235STATIC void
236xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100237 xfs_inode_t *ip,
238 int whichfork)
239{
240 if (whichfork == XFS_ATTR_FORK &&
241 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
242 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
243 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
244 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
245
246 if (dfl_forkoff > ip->i_d.di_forkoff)
247 ip->i_d.di_forkoff = dfl_forkoff;
248 }
249}
250
Dave Chinner9e5987a2013-02-25 12:31:26 +1100251#ifdef DEBUG
252STATIC struct xfs_buf *
253xfs_bmap_get_bp(
254 struct xfs_btree_cur *cur,
255 xfs_fsblock_t bno)
256{
257 struct xfs_log_item_desc *lidp;
258 int i;
259
260 if (!cur)
261 return NULL;
262
263 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
264 if (!cur->bc_bufs[i])
265 break;
266 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
267 return cur->bc_bufs[i];
268 }
269
270 /* Chase down all the log items to see if the bp is there */
271 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
272 struct xfs_buf_log_item *bip;
273 bip = (struct xfs_buf_log_item *)lidp->lid_item;
274 if (bip->bli_item.li_type == XFS_LI_BUF &&
275 XFS_BUF_ADDR(bip->bli_buf) == bno)
276 return bip->bli_buf;
277 }
278
279 return NULL;
280}
281
282STATIC void
283xfs_check_block(
284 struct xfs_btree_block *block,
285 xfs_mount_t *mp,
286 int root,
287 short sz)
288{
289 int i, j, dmxr;
290 __be64 *pp, *thispa; /* pointer to block address */
291 xfs_bmbt_key_t *prevp, *keyp;
292
293 ASSERT(be16_to_cpu(block->bb_level) > 0);
294
295 prevp = NULL;
296 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
297 dmxr = mp->m_bmap_dmxr[0];
298 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
299
300 if (prevp) {
301 ASSERT(be64_to_cpu(prevp->br_startoff) <
302 be64_to_cpu(keyp->br_startoff));
303 }
304 prevp = keyp;
305
306 /*
307 * Compare the block numbers to see if there are dups.
308 */
309 if (root)
310 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
311 else
312 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
313
314 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
315 if (root)
316 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
317 else
318 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
319 if (*thispa == *pp) {
320 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
321 __func__, j, i,
322 (unsigned long long)be64_to_cpu(*thispa));
323 panic("%s: ptrs are equal in node\n",
324 __func__);
325 }
326 }
327 }
328}
329
330/*
331 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100332 * btree leaves. THis becomes prohibitively expensive for large extent count
333 * files, so don't bother with inodes that have more than 10,000 extents in
334 * them. The btree record ordering checks will still be done, so for such large
335 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100336 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100337STATIC void
338xfs_bmap_check_leaf_extents(
339 xfs_btree_cur_t *cur, /* btree cursor or null */
340 xfs_inode_t *ip, /* incore inode pointer */
341 int whichfork) /* data or attr fork */
342{
343 struct xfs_btree_block *block; /* current btree block */
344 xfs_fsblock_t bno; /* block # of "block" */
345 xfs_buf_t *bp; /* buffer for "block" */
346 int error; /* error return value */
347 xfs_extnum_t i=0, j; /* index into the extents list */
348 xfs_ifork_t *ifp; /* fork structure */
349 int level; /* btree level, for checking */
350 xfs_mount_t *mp; /* file system mount structure */
351 __be64 *pp; /* pointer to block address */
352 xfs_bmbt_rec_t *ep; /* pointer to current extent */
353 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
354 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
355 int bp_release = 0;
356
357 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
358 return;
359 }
360
Dave Chinnere3543812016-01-08 11:28:49 +1100361 /* skip large extent count inodes */
362 if (ip->i_d.di_nextents > 10000)
363 return;
364
Dave Chinner9e5987a2013-02-25 12:31:26 +1100365 bno = NULLFSBLOCK;
366 mp = ip->i_mount;
367 ifp = XFS_IFORK_PTR(ip, whichfork);
368 block = ifp->if_broot;
369 /*
370 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
371 */
372 level = be16_to_cpu(block->bb_level);
373 ASSERT(level > 0);
374 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
375 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
376 bno = be64_to_cpu(*pp);
377
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000378 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100379 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
380 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
381
382 /*
383 * Go down the tree until leaf level is reached, following the first
384 * pointer (leftmost) at each level.
385 */
386 while (level-- > 0) {
387 /* See if buf is in cur first */
388 bp_release = 0;
389 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
390 if (!bp) {
391 bp_release = 1;
392 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
393 XFS_BMAP_BTREE_REF,
394 &xfs_bmbt_buf_ops);
395 if (error)
396 goto error_norelse;
397 }
398 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100399 if (level == 0)
400 break;
401
402 /*
403 * Check this block for basic sanity (increasing keys and
404 * no duplicate blocks).
405 */
406
407 xfs_check_block(block, mp, 0, 0);
408 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
409 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100410 XFS_WANT_CORRUPTED_GOTO(mp,
411 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100412 if (bp_release) {
413 bp_release = 0;
414 xfs_trans_brelse(NULL, bp);
415 }
416 }
417
418 /*
419 * Here with bp and block set to the leftmost leaf node in the tree.
420 */
421 i = 0;
422
423 /*
424 * Loop over all leaf nodes checking that all extents are in the right order.
425 */
426 for (;;) {
427 xfs_fsblock_t nextbno;
428 xfs_extnum_t num_recs;
429
430
431 num_recs = xfs_btree_get_numrecs(block);
432
433 /*
434 * Read-ahead the next leaf block, if any.
435 */
436
437 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
438
439 /*
440 * Check all the extents to make sure they are OK.
441 * If we had a previous block, the last entry should
442 * conform with the first entry in this one.
443 */
444
445 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
446 if (i) {
447 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
448 xfs_bmbt_disk_get_blockcount(&last) <=
449 xfs_bmbt_disk_get_startoff(ep));
450 }
451 for (j = 1; j < num_recs; j++) {
452 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
453 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
454 xfs_bmbt_disk_get_blockcount(ep) <=
455 xfs_bmbt_disk_get_startoff(nextp));
456 ep = nextp;
457 }
458
459 last = *ep;
460 i += num_recs;
461 if (bp_release) {
462 bp_release = 0;
463 xfs_trans_brelse(NULL, bp);
464 }
465 bno = nextbno;
466 /*
467 * If we've reached the end, stop.
468 */
469 if (bno == NULLFSBLOCK)
470 break;
471
472 bp_release = 0;
473 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
474 if (!bp) {
475 bp_release = 1;
476 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
477 XFS_BMAP_BTREE_REF,
478 &xfs_bmbt_buf_ops);
479 if (error)
480 goto error_norelse;
481 }
482 block = XFS_BUF_TO_BLOCK(bp);
483 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100484
Dave Chinner9e5987a2013-02-25 12:31:26 +1100485 return;
486
487error0:
488 xfs_warn(mp, "%s: at error0", __func__);
489 if (bp_release)
490 xfs_trans_brelse(NULL, bp);
491error_norelse:
492 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
493 __func__, i);
494 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
495 return;
496}
497
498/*
499 * Add bmap trace insert entries for all the contents of the extent records.
500 */
501void
502xfs_bmap_trace_exlist(
503 xfs_inode_t *ip, /* incore inode pointer */
504 xfs_extnum_t cnt, /* count of entries in the list */
505 int whichfork, /* data or attr fork */
506 unsigned long caller_ip)
507{
508 xfs_extnum_t idx; /* extent record index */
509 xfs_ifork_t *ifp; /* inode fork pointer */
510 int state = 0;
511
512 if (whichfork == XFS_ATTR_FORK)
513 state |= BMAP_ATTRFORK;
514
515 ifp = XFS_IFORK_PTR(ip, whichfork);
516 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
517 for (idx = 0; idx < cnt; idx++)
518 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
519}
520
521/*
522 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000523 * given the caller's original parameters. Specifically check the
524 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100525 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
526 */
527STATIC void
528xfs_bmap_validate_ret(
529 xfs_fileoff_t bno,
530 xfs_filblks_t len,
531 int flags,
532 xfs_bmbt_irec_t *mval,
533 int nmap,
534 int ret_nmap)
535{
536 int i; /* index to map values */
537
538 ASSERT(ret_nmap <= nmap);
539
540 for (i = 0; i < ret_nmap; i++) {
541 ASSERT(mval[i].br_blockcount > 0);
542 if (!(flags & XFS_BMAPI_ENTIRE)) {
543 ASSERT(mval[i].br_startoff >= bno);
544 ASSERT(mval[i].br_blockcount <= len);
545 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
546 bno + len);
547 } else {
548 ASSERT(mval[i].br_startoff < bno + len);
549 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
550 bno);
551 }
552 ASSERT(i == 0 ||
553 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
554 mval[i].br_startoff);
555 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
556 mval[i].br_startblock != HOLESTARTBLOCK);
557 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
558 mval[i].br_state == XFS_EXT_UNWRITTEN);
559 }
560}
561
562#else
563#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
564#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
565#endif /* DEBUG */
566
567/*
568 * bmap free list manipulation functions
569 */
570
571/*
572 * Add the extent to the list of extents to be free at transaction end.
573 * The list is maintained sorted (by block number).
574 */
575void
576xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000577 struct xfs_mount *mp,
578 struct xfs_defer_ops *dfops,
579 xfs_fsblock_t bno,
580 xfs_filblks_t len,
581 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100582{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000583 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100584#ifdef DEBUG
585 xfs_agnumber_t agno;
586 xfs_agblock_t agbno;
587
588 ASSERT(bno != NULLFSBLOCK);
589 ASSERT(len > 0);
590 ASSERT(len <= MAXEXTLEN);
591 ASSERT(!isnullstartblock(bno));
592 agno = XFS_FSB_TO_AGNO(mp, bno);
593 agbno = XFS_FSB_TO_AGBNO(mp, bno);
594 ASSERT(agno < mp->m_sb.sb_agcount);
595 ASSERT(agbno < mp->m_sb.sb_agblocks);
596 ASSERT(len < mp->m_sb.sb_agblocks);
597 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
598#endif
599 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000600
Dave Chinner9e5987a2013-02-25 12:31:26 +1100601 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000602 new->xefi_startblock = bno;
603 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000604 if (oinfo)
605 new->xefi_oinfo = *oinfo;
606 else
607 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000608 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
609 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000610 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100611}
612
613/*
614 * Inode fork format manipulation functions
615 */
616
617/*
618 * Transform a btree format file with only one leaf node, where the
619 * extents list will fit in the inode, into an extents format file.
620 * Since the file extents are already in-core, all we have to do is
621 * give up the space for the btree root and pitch the leaf block.
622 */
623STATIC int /* error */
624xfs_bmap_btree_to_extents(
625 xfs_trans_t *tp, /* transaction pointer */
626 xfs_inode_t *ip, /* incore inode pointer */
627 xfs_btree_cur_t *cur, /* btree cursor */
628 int *logflagsp, /* inode logging flags */
629 int whichfork) /* data or attr fork */
630{
631 /* REFERENCED */
632 struct xfs_btree_block *cblock;/* child btree block */
633 xfs_fsblock_t cbno; /* child block number */
634 xfs_buf_t *cbp; /* child block's buffer */
635 int error; /* error return value */
636 xfs_ifork_t *ifp; /* inode fork data */
637 xfs_mount_t *mp; /* mount point structure */
638 __be64 *pp; /* ptr to block address */
639 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000640 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100641
642 mp = ip->i_mount;
643 ifp = XFS_IFORK_PTR(ip, whichfork);
644 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
645 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
646 rblock = ifp->if_broot;
647 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
648 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
649 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
650 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
651 cbno = be64_to_cpu(*pp);
652 *logflagsp = 0;
653#ifdef DEBUG
654 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
655 return error;
656#endif
657 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
658 &xfs_bmbt_buf_ops);
659 if (error)
660 return error;
661 cblock = XFS_BUF_TO_BLOCK(cbp);
662 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
663 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000664 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
665 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100666 ip->i_d.di_nblocks--;
667 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
668 xfs_trans_binval(tp, cbp);
669 if (cur->bc_bufs[0] == cbp)
670 cur->bc_bufs[0] = NULL;
671 xfs_iroot_realloc(ip, -1, whichfork);
672 ASSERT(ifp->if_broot == NULL);
673 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
674 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
675 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
676 return 0;
677}
678
679/*
680 * Convert an extents-format file into a btree-format file.
681 * The new file will have a root block (in the inode) and a single child block.
682 */
683STATIC int /* error */
684xfs_bmap_extents_to_btree(
685 xfs_trans_t *tp, /* transaction pointer */
686 xfs_inode_t *ip, /* incore inode pointer */
687 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000688 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100689 xfs_btree_cur_t **curp, /* cursor returned to caller */
690 int wasdel, /* converting a delayed alloc */
691 int *logflagsp, /* inode logging flags */
692 int whichfork) /* data or attr fork */
693{
694 struct xfs_btree_block *ablock; /* allocated (child) bt block */
695 xfs_buf_t *abp; /* buffer for ablock */
696 xfs_alloc_arg_t args; /* allocation arguments */
697 xfs_bmbt_rec_t *arp; /* child record pointer */
698 struct xfs_btree_block *block; /* btree root block */
699 xfs_btree_cur_t *cur; /* bmap btree cursor */
700 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
701 int error; /* error return value */
702 xfs_extnum_t i, cnt; /* extent record index */
703 xfs_ifork_t *ifp; /* inode fork pointer */
704 xfs_bmbt_key_t *kp; /* root block key pointer */
705 xfs_mount_t *mp; /* mount structure */
706 xfs_extnum_t nextents; /* number of file extents */
707 xfs_bmbt_ptr_t *pp; /* root block address pointer */
708
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500709 mp = ip->i_mount;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100710 ifp = XFS_IFORK_PTR(ip, whichfork);
711 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
712
713 /*
714 * Make space in the inode incore.
715 */
716 xfs_iroot_realloc(ip, 1, whichfork);
717 ifp->if_flags |= XFS_IFBROOT;
718
719 /*
720 * Fill in the root.
721 */
722 block = ifp->if_broot;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500723 if (xfs_sb_version_hascrc(&mp->m_sb))
724 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
725 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
726 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
727 else
728 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
729 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
730 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100731
732 /*
733 * Need a cursor. Can't allocate until bb_level is filled in.
734 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100735 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
736 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000737 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100738 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
739 /*
740 * Convert to a btree with two levels, one record in root.
741 */
742 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
743 memset(&args, 0, sizeof(args));
744 args.tp = tp;
745 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000746 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100747 args.firstblock = *firstblock;
748 if (*firstblock == NULLFSBLOCK) {
749 args.type = XFS_ALLOCTYPE_START_BNO;
750 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000751 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100752 args.type = XFS_ALLOCTYPE_START_BNO;
753 args.fsbno = *firstblock;
754 } else {
755 args.type = XFS_ALLOCTYPE_NEAR_BNO;
756 args.fsbno = *firstblock;
757 }
758 args.minlen = args.maxlen = args.prod = 1;
759 args.wasdel = wasdel;
760 *logflagsp = 0;
761 if ((error = xfs_alloc_vextent(&args))) {
762 xfs_iroot_realloc(ip, -1, whichfork);
763 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
764 return error;
765 }
766 /*
767 * Allocation can't fail, the space was reserved.
768 */
769 ASSERT(args.fsbno != NULLFSBLOCK);
770 ASSERT(*firstblock == NULLFSBLOCK ||
771 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000772 (dfops->dop_low &&
Dave Chinner9e5987a2013-02-25 12:31:26 +1100773 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
774 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
775 cur->bc_private.b.allocated++;
776 ip->i_d.di_nblocks++;
777 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
778 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
779 /*
780 * Fill in the child block.
781 */
782 abp->b_ops = &xfs_bmbt_buf_ops;
783 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500784 if (xfs_sb_version_hascrc(&mp->m_sb))
785 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
786 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
787 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
788 else
789 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
790 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
791 XFS_BTREE_LONG_PTRS);
792
Dave Chinner9e5987a2013-02-25 12:31:26 +1100793 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
794 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
795 for (cnt = i = 0; i < nextents; i++) {
796 ep = xfs_iext_get_ext(ifp, i);
797 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
798 arp->l0 = cpu_to_be64(ep->l0);
799 arp->l1 = cpu_to_be64(ep->l1);
800 arp++; cnt++;
801 }
802 }
803 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
804 xfs_btree_set_numrecs(ablock, cnt);
805
806 /*
807 * Fill in the root key and pointer.
808 */
809 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
810 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
811 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
812 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
813 be16_to_cpu(block->bb_level)));
814 *pp = cpu_to_be64(args.fsbno);
815
816 /*
817 * Do all this logging at the end so that
818 * the root is at the right level.
819 */
820 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
821 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
822 ASSERT(*curp == NULL);
823 *curp = cur;
824 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
825 return 0;
826}
827
828/*
829 * Convert a local file to an extents file.
830 * This code is out of bounds for data forks of regular files,
831 * since the file data needs to get logged so things will stay consistent.
832 * (The bmap-level manipulations are ok, though).
833 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000834void
835xfs_bmap_local_to_extents_empty(
836 struct xfs_inode *ip,
837 int whichfork)
838{
839 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
840
841 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
842 ASSERT(ifp->if_bytes == 0);
843 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
844
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000845 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000846 ifp->if_flags &= ~XFS_IFINLINE;
847 ifp->if_flags |= XFS_IFEXTENTS;
848 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
849}
850
851
Dave Chinner9e5987a2013-02-25 12:31:26 +1100852STATIC int /* error */
853xfs_bmap_local_to_extents(
854 xfs_trans_t *tp, /* transaction pointer */
855 xfs_inode_t *ip, /* incore inode pointer */
856 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
857 xfs_extlen_t total, /* total blocks needed by transaction */
858 int *logflagsp, /* inode logging flags */
859 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500860 void (*init_fn)(struct xfs_trans *tp,
861 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100862 struct xfs_inode *ip,
863 struct xfs_ifork *ifp))
864{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000865 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100866 int flags; /* logging flags returned */
867 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000868 xfs_alloc_arg_t args; /* allocation arguments */
869 xfs_buf_t *bp; /* buffer for extent block */
870 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100871
872 /*
873 * We don't want to deal with the case of keeping inode data inline yet.
874 * So sending the data fork of a regular inode is invalid.
875 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100876 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100877 ifp = XFS_IFORK_PTR(ip, whichfork);
878 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000879
880 if (!ifp->if_bytes) {
881 xfs_bmap_local_to_extents_empty(ip, whichfork);
882 flags = XFS_ILOG_CORE;
883 goto done;
884 }
885
Dave Chinner9e5987a2013-02-25 12:31:26 +1100886 flags = 0;
887 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000888 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
889 XFS_IFINLINE);
890 memset(&args, 0, sizeof(args));
891 args.tp = tp;
892 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000893 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000894 args.firstblock = *firstblock;
895 /*
896 * Allocate a block. We know we need only one, since the
897 * file currently fits in an inode.
898 */
899 if (*firstblock == NULLFSBLOCK) {
900 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
901 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100902 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000903 args.fsbno = *firstblock;
904 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100905 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000906 args.total = total;
907 args.minlen = args.maxlen = args.prod = 1;
908 error = xfs_alloc_vextent(&args);
909 if (error)
910 goto done;
911
912 /* Can't fail, the space was reserved. */
913 ASSERT(args.fsbno != NULLFSBLOCK);
914 ASSERT(args.len == 1);
915 *firstblock = args.fsbno;
916 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
917
Dave Chinnerfe22d552015-01-22 09:30:06 +1100918 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100919 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100920 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100921 * The callout is responsible for logging because the remote format
922 * might differ from the local format and thus we don't know how much to
923 * log here. Note that init_fn must also set the buffer log item type
924 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100925 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000926 init_fn(tp, bp, ip, ifp);
927
Brian Fosterb7cdc662015-10-12 15:40:24 +1100928 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000929 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
930 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100931 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000932
933 xfs_iext_add(ifp, 0, 1);
934 ep = xfs_iext_get_ext(ifp, 0);
935 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
936 trace_xfs_bmap_post_update(ip, 0,
937 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
938 _THIS_IP_);
939 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
940 ip->i_d.di_nblocks = 1;
941 xfs_trans_mod_dquot_byino(tp, ip,
942 XFS_TRANS_DQ_BCOUNT, 1L);
943 flags |= xfs_ilog_fext(whichfork);
944
Dave Chinner9e5987a2013-02-25 12:31:26 +1100945done:
946 *logflagsp = flags;
947 return error;
948}
949
950/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 * Called from xfs_bmap_add_attrfork to handle btree format files.
952 */
953STATIC int /* error */
954xfs_bmap_add_attrfork_btree(
955 xfs_trans_t *tp, /* transaction pointer */
956 xfs_inode_t *ip, /* incore inode pointer */
957 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000958 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 int *flags) /* inode logging flags */
960{
961 xfs_btree_cur_t *cur; /* btree cursor */
962 int error; /* error return value */
963 xfs_mount_t *mp; /* file system mount struct */
964 int stat; /* newroot status */
965
966 mp = ip->i_mount;
967 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
968 *flags |= XFS_ILOG_DBROOT;
969 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100970 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000971 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 cur->bc_private.b.firstblock = *firstblock;
973 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
974 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000975 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100976 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100977 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 goto error0;
979 if (stat == 0) {
980 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000981 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
983 *firstblock = cur->bc_private.b.firstblock;
984 cur->bc_private.b.allocated = 0;
985 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
986 }
987 return 0;
988error0:
989 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
990 return error;
991}
992
993/*
994 * Called from xfs_bmap_add_attrfork to handle extents format files.
995 */
996STATIC int /* error */
997xfs_bmap_add_attrfork_extents(
998 xfs_trans_t *tp, /* transaction pointer */
999 xfs_inode_t *ip, /* incore inode pointer */
1000 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001001 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 int *flags) /* inode logging flags */
1003{
1004 xfs_btree_cur_t *cur; /* bmap btree cursor */
1005 int error; /* error return value */
1006
1007 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1008 return 0;
1009 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001010 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 flags, XFS_DATA_FORK);
1012 if (cur) {
1013 cur->bc_private.b.allocated = 0;
1014 xfs_btree_del_cursor(cur,
1015 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1016 }
1017 return error;
1018}
1019
1020/*
Dave Chinner1e823792013-02-11 15:58:13 +11001021 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1022 * different data fork content type needs a different callout to do the
1023 * conversion. Some are basic and only require special block initialisation
1024 * callouts for the data formating, others (directories) are so specialised they
1025 * handle everything themselves.
1026 *
1027 * XXX (dgc): investigate whether directory conversion can use the generic
1028 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001029 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 */
1031STATIC int /* error */
1032xfs_bmap_add_attrfork_local(
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_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1042 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001043
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001044 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001046 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 dargs.dp = ip;
1048 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001049 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001050 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 dargs.whichfork = XFS_DATA_FORK;
1052 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001053 return xfs_dir2_sf_to_block(&dargs);
1054 }
1055
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001056 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001057 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1058 flags, XFS_DATA_FORK,
1059 xfs_symlink_local_to_remote);
1060
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001061 /* should only be called for types that support local format data */
1062 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001063 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
1066/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001067 * Convert inode from non-attributed to attributed.
1068 * Must not be in a transaction, ip must not be locked.
1069 */
1070int /* error code */
1071xfs_bmap_add_attrfork(
1072 xfs_inode_t *ip, /* incore inode pointer */
1073 int size, /* space new attribute needs */
1074 int rsvd) /* xact may use reserved blks */
1075{
1076 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001077 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001078 xfs_mount_t *mp; /* mount structure */
1079 xfs_trans_t *tp; /* transaction pointer */
1080 int blks; /* space reservation */
1081 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001082 int logflags; /* logging flags */
1083 int error; /* error return value */
1084
1085 ASSERT(XFS_IFORK_Q(ip) == 0);
1086
1087 mp = ip->i_mount;
1088 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001089
Dave Chinner9e5987a2013-02-25 12:31:26 +11001090 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001091
1092 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1093 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1094 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001095 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001096
Dave Chinner9e5987a2013-02-25 12:31:26 +11001097 xfs_ilock(ip, XFS_ILOCK_EXCL);
1098 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1099 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1100 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001101 if (error)
1102 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001103 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001104 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001105 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1106 /*
1107 * For inodes coming from pre-6.2 filesystems.
1108 */
1109 ASSERT(ip->i_d.di_aformat == 0);
1110 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1111 }
1112 ASSERT(ip->i_d.di_anextents == 0);
1113
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001114 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001115 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1116
1117 switch (ip->i_d.di_format) {
1118 case XFS_DINODE_FMT_DEV:
1119 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1120 break;
1121 case XFS_DINODE_FMT_UUID:
1122 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1123 break;
1124 case XFS_DINODE_FMT_LOCAL:
1125 case XFS_DINODE_FMT_EXTENTS:
1126 case XFS_DINODE_FMT_BTREE:
1127 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1128 if (!ip->i_d.di_forkoff)
1129 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1130 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1131 version = 2;
1132 break;
1133 default:
1134 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001135 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001136 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001137 }
1138
1139 ASSERT(ip->i_afp == NULL);
1140 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1141 ip->i_afp->if_flags = XFS_IFEXTENTS;
1142 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001143 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001144 switch (ip->i_d.di_format) {
1145 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001146 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001147 &logflags);
1148 break;
1149 case XFS_DINODE_FMT_EXTENTS:
1150 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001151 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001152 break;
1153 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001154 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001155 &logflags);
1156 break;
1157 default:
1158 error = 0;
1159 break;
1160 }
1161 if (logflags)
1162 xfs_trans_log_inode(tp, ip, logflags);
1163 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001164 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001165 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1166 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001167 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001168
1169 spin_lock(&mp->m_sb_lock);
1170 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1171 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001172 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001173 }
1174 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1175 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001176 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001177 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001178 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001179 if (log_sb)
1180 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001181 }
1182
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001183 error = xfs_defer_finish(&tp, &dfops, NULL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001184 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001185 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001186 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001187 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001188 return error;
1189
1190bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001191 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001192trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001193 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001194 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001195 return error;
1196}
1197
1198/*
1199 * Internal and external extent tree search functions.
1200 */
1201
1202/*
1203 * Read in the extents to if_extents.
1204 * All inode fields are set up by caller, we just traverse the btree
1205 * and copy the records in. If the file system cannot contain unwritten
1206 * extents, the records are checked for no "state" flags.
1207 */
1208int /* error */
1209xfs_bmap_read_extents(
1210 xfs_trans_t *tp, /* transaction pointer */
1211 xfs_inode_t *ip, /* incore inode */
1212 int whichfork) /* data or attr fork */
1213{
1214 struct xfs_btree_block *block; /* current btree block */
1215 xfs_fsblock_t bno; /* block # of "block" */
1216 xfs_buf_t *bp; /* buffer for "block" */
1217 int error; /* error return value */
1218 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1219 xfs_extnum_t i, j; /* index into the extents list */
1220 xfs_ifork_t *ifp; /* fork structure */
1221 int level; /* btree level, for checking */
1222 xfs_mount_t *mp; /* file system mount structure */
1223 __be64 *pp; /* pointer to block address */
1224 /* REFERENCED */
1225 xfs_extnum_t room; /* number of entries there's room for */
1226
1227 bno = NULLFSBLOCK;
1228 mp = ip->i_mount;
1229 ifp = XFS_IFORK_PTR(ip, whichfork);
1230 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1231 XFS_EXTFMT_INODE(ip);
1232 block = ifp->if_broot;
1233 /*
1234 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1235 */
1236 level = be16_to_cpu(block->bb_level);
1237 ASSERT(level > 0);
1238 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1239 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001240 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001241 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1242 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1243 /*
1244 * Go down the tree until leaf level is reached, following the first
1245 * pointer (leftmost) at each level.
1246 */
1247 while (level-- > 0) {
1248 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1249 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1250 if (error)
1251 return error;
1252 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001253 if (level == 0)
1254 break;
1255 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1256 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001257 XFS_WANT_CORRUPTED_GOTO(mp,
1258 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001259 xfs_trans_brelse(tp, bp);
1260 }
1261 /*
1262 * Here with bp and block set to the leftmost leaf node in the tree.
1263 */
1264 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1265 i = 0;
1266 /*
1267 * Loop over all leaf nodes. Copy information to the extent records.
1268 */
1269 for (;;) {
1270 xfs_bmbt_rec_t *frp;
1271 xfs_fsblock_t nextbno;
1272 xfs_extnum_t num_recs;
1273 xfs_extnum_t start;
1274
1275 num_recs = xfs_btree_get_numrecs(block);
1276 if (unlikely(i + num_recs > room)) {
1277 ASSERT(i + num_recs <= room);
1278 xfs_warn(ip->i_mount,
1279 "corrupt dinode %Lu, (btree extents).",
1280 (unsigned long long) ip->i_ino);
1281 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1282 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1283 goto error0;
1284 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001285 /*
1286 * Read-ahead the next leaf block, if any.
1287 */
1288 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1289 if (nextbno != NULLFSBLOCK)
1290 xfs_btree_reada_bufl(mp, nextbno, 1,
1291 &xfs_bmbt_buf_ops);
1292 /*
1293 * Copy records into the extent records.
1294 */
1295 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1296 start = i;
1297 for (j = 0; j < num_recs; j++, i++, frp++) {
1298 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1299 trp->l0 = be64_to_cpu(frp->l0);
1300 trp->l1 = be64_to_cpu(frp->l1);
1301 }
1302 if (exntf == XFS_EXTFMT_NOSTATE) {
1303 /*
1304 * Check all attribute bmap btree records and
1305 * any "older" data bmap btree records for a
1306 * set bit in the "extent flag" position.
1307 */
1308 if (unlikely(xfs_check_nostate_extents(ifp,
1309 start, num_recs))) {
1310 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1311 XFS_ERRLEVEL_LOW,
1312 ip->i_mount);
1313 goto error0;
1314 }
1315 }
1316 xfs_trans_brelse(tp, bp);
1317 bno = nextbno;
1318 /*
1319 * If we've reached the end, stop.
1320 */
1321 if (bno == NULLFSBLOCK)
1322 break;
1323 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1324 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1325 if (error)
1326 return error;
1327 block = XFS_BUF_TO_BLOCK(bp);
1328 }
1329 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1330 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1331 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1332 return 0;
1333error0:
1334 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001335 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001336}
1337
1338
1339/*
1340 * Search the extent records for the entry containing block bno.
1341 * If bno lies in a hole, point to the next entry. If bno lies
1342 * past eof, *eofp will be set, and *prevp will contain the last
1343 * entry (null if none). Else, *lastxp will be set to the index
1344 * of the found entry; *gotp will contain the entry.
1345 */
1346STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1347xfs_bmap_search_multi_extents(
1348 xfs_ifork_t *ifp, /* inode fork pointer */
1349 xfs_fileoff_t bno, /* block number searched for */
1350 int *eofp, /* out: end of file found */
1351 xfs_extnum_t *lastxp, /* out: last extent index */
1352 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1353 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1354{
1355 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1356 xfs_extnum_t lastx; /* last extent index */
1357
1358 /*
1359 * Initialize the extent entry structure to catch access to
1360 * uninitialized br_startblock field.
1361 */
1362 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1363 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1364 gotp->br_state = XFS_EXT_INVALID;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001365 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001366 prevp->br_startoff = NULLFILEOFF;
1367
1368 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1369 if (lastx > 0) {
1370 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1371 }
1372 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1373 xfs_bmbt_get_all(ep, gotp);
1374 *eofp = 0;
1375 } else {
1376 if (lastx > 0) {
1377 *gotp = *prevp;
1378 }
1379 *eofp = 1;
1380 ep = NULL;
1381 }
1382 *lastxp = lastx;
1383 return ep;
1384}
1385
1386/*
1387 * Search the extents list for the inode, for the extent containing bno.
1388 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1389 * *eofp will be set, and *prevp will contain the last entry (null if none).
1390 * Else, *lastxp will be set to the index of the found
1391 * entry; *gotp will contain the entry.
1392 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10001393xfs_bmbt_rec_host_t * /* pointer to found extent entry */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001394xfs_bmap_search_extents(
1395 xfs_inode_t *ip, /* incore inode pointer */
1396 xfs_fileoff_t bno, /* block number searched for */
1397 int fork, /* data or attr fork */
1398 int *eofp, /* out: end of file found */
1399 xfs_extnum_t *lastxp, /* out: last extent index */
1400 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1401 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1402{
1403 xfs_ifork_t *ifp; /* inode fork pointer */
1404 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1405
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001406 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001407 ifp = XFS_IFORK_PTR(ip, fork);
1408
1409 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1410
1411 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1412 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1413 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1414 "Access to block zero in inode %llu "
1415 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -05001416 "blkcnt: %llx extent-state: %x lastx: %x",
Dave Chinner9e5987a2013-02-25 12:31:26 +11001417 (unsigned long long)ip->i_ino,
1418 (unsigned long long)gotp->br_startblock,
1419 (unsigned long long)gotp->br_startoff,
1420 (unsigned long long)gotp->br_blockcount,
1421 gotp->br_state, *lastxp);
1422 *lastxp = NULLEXTNUM;
1423 *eofp = 1;
1424 return NULL;
1425 }
1426 return ep;
1427}
1428
1429/*
1430 * Returns the file-relative block number of the first unused block(s)
1431 * in the file with at least "len" logically contiguous blocks free.
1432 * This is the lowest-address hole if the file has holes, else the first block
1433 * past the end of file.
1434 * Return 0 if the file is currently local (in-inode).
1435 */
1436int /* error */
1437xfs_bmap_first_unused(
1438 xfs_trans_t *tp, /* transaction pointer */
1439 xfs_inode_t *ip, /* incore inode */
1440 xfs_extlen_t len, /* size of hole to find */
1441 xfs_fileoff_t *first_unused, /* unused block */
1442 int whichfork) /* data or attr fork */
1443{
1444 int error; /* error return value */
1445 int idx; /* extent record index */
1446 xfs_ifork_t *ifp; /* inode fork pointer */
1447 xfs_fileoff_t lastaddr; /* last block number seen */
1448 xfs_fileoff_t lowest; /* lowest useful block */
1449 xfs_fileoff_t max; /* starting useful block */
1450 xfs_fileoff_t off; /* offset for this block */
1451 xfs_extnum_t nextents; /* number of extent entries */
1452
1453 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1454 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1455 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1456 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1457 *first_unused = 0;
1458 return 0;
1459 }
1460 ifp = XFS_IFORK_PTR(ip, whichfork);
1461 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1462 (error = xfs_iread_extents(tp, ip, whichfork)))
1463 return error;
1464 lowest = *first_unused;
1465 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1466 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1467 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1468 off = xfs_bmbt_get_startoff(ep);
1469 /*
1470 * See if the hole before this extent will work.
1471 */
1472 if (off >= lowest + len && off - max >= len) {
1473 *first_unused = max;
1474 return 0;
1475 }
1476 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1477 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1478 }
1479 *first_unused = max;
1480 return 0;
1481}
1482
1483/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001484 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001485 * last_block (input value) in the file.
1486 * This is not based on i_size, it is based on the extent records.
1487 * Returns 0 for local files, as they do not have extent records.
1488 */
1489int /* error */
1490xfs_bmap_last_before(
1491 xfs_trans_t *tp, /* transaction pointer */
1492 xfs_inode_t *ip, /* incore inode */
1493 xfs_fileoff_t *last_block, /* last block */
1494 int whichfork) /* data or attr fork */
1495{
1496 xfs_fileoff_t bno; /* input file offset */
1497 int eof; /* hit end of file */
1498 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1499 int error; /* error return value */
1500 xfs_bmbt_irec_t got; /* current extent value */
1501 xfs_ifork_t *ifp; /* inode fork pointer */
1502 xfs_extnum_t lastx; /* last extent used */
1503 xfs_bmbt_irec_t prev; /* previous extent value */
1504
1505 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1506 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1507 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +10001508 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001509 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1510 *last_block = 0;
1511 return 0;
1512 }
1513 ifp = XFS_IFORK_PTR(ip, whichfork);
1514 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1515 (error = xfs_iread_extents(tp, ip, whichfork)))
1516 return error;
1517 bno = *last_block - 1;
1518 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1519 &prev);
1520 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1521 if (prev.br_startoff == NULLFILEOFF)
1522 *last_block = 0;
1523 else
1524 *last_block = prev.br_startoff + prev.br_blockcount;
1525 }
1526 /*
1527 * Otherwise *last_block is already the right answer.
1528 */
1529 return 0;
1530}
1531
Dave Chinner68988112013-08-12 20:49:42 +10001532int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001533xfs_bmap_last_extent(
1534 struct xfs_trans *tp,
1535 struct xfs_inode *ip,
1536 int whichfork,
1537 struct xfs_bmbt_irec *rec,
1538 int *is_empty)
1539{
1540 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1541 int error;
1542 int nextents;
1543
1544 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1545 error = xfs_iread_extents(tp, ip, whichfork);
1546 if (error)
1547 return error;
1548 }
1549
1550 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1551 if (nextents == 0) {
1552 *is_empty = 1;
1553 return 0;
1554 }
1555
1556 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1557 *is_empty = 0;
1558 return 0;
1559}
1560
1561/*
1562 * Check the last inode extent to determine whether this allocation will result
1563 * in blocks being allocated at the end of the file. When we allocate new data
1564 * blocks at the end of the file which do not start at the previous data block,
1565 * we will try to align the new blocks at stripe unit boundaries.
1566 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001567 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001568 * at, or past the EOF.
1569 */
1570STATIC int
1571xfs_bmap_isaeof(
1572 struct xfs_bmalloca *bma,
1573 int whichfork)
1574{
1575 struct xfs_bmbt_irec rec;
1576 int is_empty;
1577 int error;
1578
1579 bma->aeof = 0;
1580 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1581 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001582 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001583 return error;
1584
Dave Chinner6e708bc2013-11-22 10:41:16 +11001585 if (is_empty) {
1586 bma->aeof = 1;
1587 return 0;
1588 }
1589
Dave Chinner9e5987a2013-02-25 12:31:26 +11001590 /*
1591 * Check if we are allocation or past the last extent, or at least into
1592 * the last delayed allocated extent.
1593 */
1594 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1595 (bma->offset >= rec.br_startoff &&
1596 isnullstartblock(rec.br_startblock));
1597 return 0;
1598}
1599
1600/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001601 * Returns the file-relative block number of the first block past eof in
1602 * the file. This is not based on i_size, it is based on the extent records.
1603 * Returns 0 for local files, as they do not have extent records.
1604 */
1605int
1606xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001607 struct xfs_inode *ip,
1608 xfs_fileoff_t *last_block,
1609 int whichfork)
1610{
1611 struct xfs_bmbt_irec rec;
1612 int is_empty;
1613 int error;
1614
1615 *last_block = 0;
1616
1617 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1618 return 0;
1619
1620 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1621 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001622 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001623
1624 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1625 if (error || is_empty)
1626 return error;
1627
1628 *last_block = rec.br_startoff + rec.br_blockcount;
1629 return 0;
1630}
1631
1632/*
1633 * Returns whether the selected fork of the inode has exactly one
1634 * block or not. For the data fork we check this matches di_size,
1635 * implying the file's range is 0..bsize-1.
1636 */
1637int /* 1=>1 block, 0=>otherwise */
1638xfs_bmap_one_block(
1639 xfs_inode_t *ip, /* incore inode */
1640 int whichfork) /* data or attr fork */
1641{
1642 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1643 xfs_ifork_t *ifp; /* inode fork pointer */
1644 int rval; /* return value */
1645 xfs_bmbt_irec_t s; /* internal version of extent */
1646
1647#ifndef DEBUG
1648 if (whichfork == XFS_DATA_FORK)
1649 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1650#endif /* !DEBUG */
1651 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1652 return 0;
1653 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1654 return 0;
1655 ifp = XFS_IFORK_PTR(ip, whichfork);
1656 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1657 ep = xfs_iext_get_ext(ifp, 0);
1658 xfs_bmbt_get_all(ep, &s);
1659 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1660 if (rval && whichfork == XFS_DATA_FORK)
1661 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1662 return rval;
1663}
1664
1665/*
1666 * Extent tree manipulation functions used during allocation.
1667 */
1668
1669/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001670 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 */
1672STATIC int /* error */
1673xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001674 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001676 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001678 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001681 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 xfs_fileoff_t new_endoff; /* end offset of new entry */
1683 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1684 /* left is 0, right is 1, prev is 2 */
1685 int rval=0; /* return value (logging flags) */
1686 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001687 xfs_filblks_t da_new; /* new count del alloc blocks used */
1688 xfs_filblks_t da_old; /* old count del alloc blocks used */
1689 xfs_filblks_t temp=0; /* value for da_new calculations */
1690 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 int tmp_rval; /* partial logging flags */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001692 int whichfork = XFS_DATA_FORK;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001693 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001695 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001696 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001697
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001698 ASSERT(bma->idx >= 0);
1699 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001700 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001701 ASSERT(!bma->cur ||
1702 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001703
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001704 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706#define LEFT r[0]
1707#define RIGHT r[1]
1708#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709
1710 /*
1711 * Set up a bunch of variables to make the tests simpler.
1712 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001713 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 xfs_bmbt_get_all(ep, &PREV);
1715 new_endoff = new->br_startoff + new->br_blockcount;
1716 ASSERT(PREV.br_startoff <= new->br_startoff);
1717 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001718
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001719 da_old = startblockval(PREV.br_startblock);
1720 da_new = 0;
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 /*
1723 * Set flags determining what part of the previous delayed allocation
1724 * extent is being replaced by a real allocation.
1725 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001726 if (PREV.br_startoff == new->br_startoff)
1727 state |= BMAP_LEFT_FILLING;
1728 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1729 state |= BMAP_RIGHT_FILLING;
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /*
1732 * Check and set flags if this segment has a left neighbor.
1733 * Don't set contiguous if the combined extent would be too large.
1734 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001735 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001736 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001737 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001738
1739 if (isnullstartblock(LEFT.br_startblock))
1740 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001742
1743 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1744 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1745 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1746 LEFT.br_state == new->br_state &&
1747 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1748 state |= BMAP_LEFT_CONTIG;
1749
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 /*
1751 * Check and set flags if this segment has a right neighbor.
1752 * Don't set contiguous if the combined extent would be too large.
1753 * Also check for all-three-contiguous being too large.
1754 */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001755 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001756 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001757 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001758
1759 if (isnullstartblock(RIGHT.br_startblock))
1760 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001762
1763 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1764 new_endoff == RIGHT.br_startoff &&
1765 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1766 new->br_state == RIGHT.br_state &&
1767 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1768 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1769 BMAP_RIGHT_FILLING)) !=
1770 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1771 BMAP_RIGHT_FILLING) ||
1772 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1773 <= MAXEXTLEN))
1774 state |= BMAP_RIGHT_CONTIG;
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 error = 0;
1777 /*
1778 * Switch out based on the FILLING and CONTIG state bits.
1779 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001780 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1781 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1782 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1783 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 /*
1785 * Filling in all of a previously delayed allocation extent.
1786 * The left and right neighbors are both contiguous with new.
1787 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001788 bma->idx--;
1789 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1790 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 LEFT.br_blockcount + PREV.br_blockcount +
1792 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001793 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001794
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001795 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1796 bma->ip->i_d.di_nextents--;
1797 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1799 else {
1800 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001801 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001803 RIGHT.br_blockcount, &i);
1804 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001806 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001807 error = xfs_btree_delete(bma->cur, &i);
1808 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001810 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001811 error = xfs_btree_decrement(bma->cur, 0, &i);
1812 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001814 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001815 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 LEFT.br_startblock,
1817 LEFT.br_blockcount +
1818 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 RIGHT.br_blockcount, LEFT.br_state);
1820 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 goto done;
1822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 break;
1824
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001825 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /*
1827 * Filling in all of a previously delayed allocation extent.
1828 * The left neighbor is contiguous, the right is not.
1829 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001830 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001831
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001832 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1833 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001834 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001835 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001836
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001837 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1838 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 rval = XFS_ILOG_DEXT;
1840 else {
1841 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001842 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001844 &i);
1845 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001847 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001848 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 LEFT.br_startblock,
1850 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001851 PREV.br_blockcount, LEFT.br_state);
1852 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 goto done;
1854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 break;
1856
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001857 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 /*
1859 * Filling in all of a previously delayed allocation extent.
1860 * The right neighbor is contiguous, the left is not.
1861 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001862 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 xfs_bmbt_set_startblock(ep, new->br_startblock);
1864 xfs_bmbt_set_blockcount(ep,
1865 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001866 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001867
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001868 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1869 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 rval = XFS_ILOG_DEXT;
1871 else {
1872 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001873 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001875 RIGHT.br_blockcount, &i);
1876 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001878 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001879 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 new->br_startblock,
1881 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001882 RIGHT.br_blockcount, PREV.br_state);
1883 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 goto done;
1885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 break;
1887
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001888 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 /*
1890 * Filling in all of a previously delayed allocation extent.
1891 * Neither the left nor right neighbors are contiguous with
1892 * the new one.
1893 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001894 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001896 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001897
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001898 bma->ip->i_d.di_nextents++;
1899 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1901 else {
1902 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001903 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001905 &i);
1906 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001908 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001909 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1910 error = xfs_btree_insert(bma->cur, &i);
1911 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001913 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 break;
1916
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001917 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 /*
1919 * Filling in the first part of a previous delayed allocation.
1920 * The left neighbor is contiguous.
1921 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001922 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1923 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 LEFT.br_blockcount + new->br_blockcount);
1925 xfs_bmbt_set_startoff(ep,
1926 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001927 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001930 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001932 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 rval = XFS_ILOG_DEXT;
1934 else {
1935 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001936 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001938 &i);
1939 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001941 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001942 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 LEFT.br_startblock,
1944 LEFT.br_blockcount +
1945 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001946 LEFT.br_state);
1947 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 goto done;
1949 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001950 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001951 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001952 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001953 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001954
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001955 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 break;
1957
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001958 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 /*
1960 * Filling in the first part of a previous delayed allocation.
1961 * The left neighbor is not contiguous.
1962 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001963 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 xfs_bmbt_set_startoff(ep, new_endoff);
1965 temp = PREV.br_blockcount - new->br_blockcount;
1966 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001967 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
1968 bma->ip->i_d.di_nextents++;
1969 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1971 else {
1972 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001973 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001975 &i);
1976 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001978 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001979 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1980 error = xfs_btree_insert(bma->cur, &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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001985
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001986 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001987 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001988 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001989 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 rval |= tmp_rval;
1991 if (error)
1992 goto done;
1993 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001994 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001995 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001996 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1997 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001998 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001999 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 break;
2001
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002002 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 /*
2004 * Filling in the last part of a previous delayed allocation.
2005 * The right neighbor is contiguous with the new allocation.
2006 */
2007 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002008 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002010 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002011 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 new->br_blockcount + RIGHT.br_blockcount,
2013 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002014 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2015 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 rval = XFS_ILOG_DEXT;
2017 else {
2018 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002019 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002021 RIGHT.br_blockcount, &i);
2022 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002024 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002025 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 new->br_startblock,
2027 new->br_blockcount +
2028 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002029 RIGHT.br_state);
2030 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 goto done;
2032 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002033
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002034 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002035 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002036 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002037 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002038 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002039
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002040 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 break;
2042
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002043 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 /*
2045 * Filling in the last part of a previous delayed allocation.
2046 * The right neighbor is not contiguous.
2047 */
2048 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002049 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002051 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2052 bma->ip->i_d.di_nextents++;
2053 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2055 else {
2056 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002057 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002059 &i);
2060 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002062 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002063 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2064 error = xfs_btree_insert(bma->cur, &i);
2065 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002067 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002069
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002070 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002071 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002072 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002073 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 rval |= tmp_rval;
2075 if (error)
2076 goto done;
2077 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002078 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002079 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002080 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2081 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002082 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002083 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002084
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002085 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 break;
2087
2088 case 0:
2089 /*
2090 * Filling in the middle part of a previous delayed allocation.
2091 * Contiguity is impossible here.
2092 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002093 *
2094 * We start with a delayed allocation:
2095 *
2096 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2097 * PREV @ idx
2098 *
2099 * and we are allocating:
2100 * +rrrrrrrrrrrrrrrrr+
2101 * new
2102 *
2103 * and we set it up for insertion as:
2104 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2105 * new
2106 * PREV @ idx LEFT RIGHT
2107 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 */
2109 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002111 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002112 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2113 LEFT = *new;
2114 RIGHT.br_state = PREV.br_state;
2115 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002116 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002117 RIGHT.br_startoff = new_endoff;
2118 RIGHT.br_blockcount = temp2;
2119 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002120 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2121 bma->ip->i_d.di_nextents++;
2122 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2124 else {
2125 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002126 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002128 &i);
2129 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002131 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002132 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2133 error = xfs_btree_insert(bma->cur, &i);
2134 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002136 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002138
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002139 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002140 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002141 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002142 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 rval |= tmp_rval;
2144 if (error)
2145 goto done;
2146 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002147 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2148 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002149 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002150 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002151 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002152 error = xfs_mod_fdblocks(bma->ip->i_mount,
2153 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002154 ASSERT(!error);
2155 if (error)
2156 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002158
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002159 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002160 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002161 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2162 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2163 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002164 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002165 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002166
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002167 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002168 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 break;
2170
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002171 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2172 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2173 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2174 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2175 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2176 case BMAP_LEFT_CONTIG:
2177 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 /*
2179 * These cases are all impossible.
2180 */
2181 ASSERT(0);
2182 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002183
Darrick J. Wong9c194642016-08-03 12:16:05 +10002184 /* add reverse mapping */
2185 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2186 if (error)
2187 goto done;
2188
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002189 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002190 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002191 int tmp_logflags; /* partial log flag return val */
2192
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002193 ASSERT(bma->cur == NULL);
2194 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002195 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002196 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002197 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002198 if (error)
2199 goto done;
2200 }
2201
2202 /* adjust for changes in reserved delayed indirect blocks */
2203 if (da_old || da_new) {
2204 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002205 if (bma->cur)
2206 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002207 ASSERT(temp <= da_old);
2208 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002209 xfs_mod_fdblocks(bma->ip->i_mount,
2210 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002211 }
2212
2213 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002214 if (bma->cur)
2215 bma->cur->bc_private.b.allocated = 0;
2216
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002217 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002219 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return error;
2221#undef LEFT
2222#undef RIGHT
2223#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224}
2225
2226/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002227 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 */
2229STATIC int /* error */
2230xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002231 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002233 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002235 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002236 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002237 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002238 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002241 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002244 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 xfs_fileoff_t new_endoff; /* end offset of new entry */
2246 xfs_exntst_t newext; /* new extent state */
2247 xfs_exntst_t oldext; /* old extent state */
2248 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2249 /* left is 0, right is 1, prev is 2 */
2250 int rval=0; /* return value (logging flags) */
2251 int state = 0;/* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002252 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002254 *logflagsp = 0;
2255
2256 cur = *curp;
2257 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2258
2259 ASSERT(*idx >= 0);
2260 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2261 ASSERT(!isnullstartblock(new->br_startblock));
2262
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002263 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265#define LEFT r[0]
2266#define RIGHT r[1]
2267#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002268
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 /*
2270 * Set up a bunch of variables to make the tests simpler.
2271 */
2272 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002273 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 xfs_bmbt_get_all(ep, &PREV);
2275 newext = new->br_state;
2276 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2277 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2278 ASSERT(PREV.br_state == oldext);
2279 new_endoff = new->br_startoff + new->br_blockcount;
2280 ASSERT(PREV.br_startoff <= new->br_startoff);
2281 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 /*
2284 * Set flags determining what part of the previous oldext allocation
2285 * extent is being replaced by a newext allocation.
2286 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002287 if (PREV.br_startoff == new->br_startoff)
2288 state |= BMAP_LEFT_FILLING;
2289 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2290 state |= BMAP_RIGHT_FILLING;
2291
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 /*
2293 * Check and set flags if this segment has a left neighbor.
2294 * Don't set contiguous if the combined extent would be too large.
2295 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002296 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002297 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002298 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002299
2300 if (isnullstartblock(LEFT.br_startblock))
2301 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002303
2304 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2305 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2306 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2307 LEFT.br_state == newext &&
2308 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2309 state |= BMAP_LEFT_CONTIG;
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 /*
2312 * Check and set flags if this segment has a right neighbor.
2313 * Don't set contiguous if the combined extent would be too large.
2314 * Also check for all-three-contiguous being too large.
2315 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002316 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002317 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002318 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002319 if (isnullstartblock(RIGHT.br_startblock))
2320 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002322
2323 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2324 new_endoff == RIGHT.br_startoff &&
2325 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2326 newext == RIGHT.br_state &&
2327 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2328 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2329 BMAP_RIGHT_FILLING)) !=
2330 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2331 BMAP_RIGHT_FILLING) ||
2332 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2333 <= MAXEXTLEN))
2334 state |= BMAP_RIGHT_CONTIG;
2335
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 /*
2337 * Switch out based on the FILLING and CONTIG state bits.
2338 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002339 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2340 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2341 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2342 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 /*
2344 * Setting all of a previous oldext extent to newext.
2345 * The left and right neighbors are both contiguous with new.
2346 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002347 --*idx;
2348
2349 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2350 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 LEFT.br_blockcount + PREV.br_blockcount +
2352 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002353 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002354
Christoph Hellwigec90c552011-05-23 08:52:53 +00002355 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 ip->i_d.di_nextents -= 2;
2357 if (cur == NULL)
2358 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2359 else {
2360 rval = XFS_ILOG_CORE;
2361 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2362 RIGHT.br_startblock,
2363 RIGHT.br_blockcount, &i)))
2364 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002365 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002366 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002368 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002369 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002371 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002372 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002374 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002375 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002377 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2379 LEFT.br_startblock,
2380 LEFT.br_blockcount + PREV.br_blockcount +
2381 RIGHT.br_blockcount, LEFT.br_state)))
2382 goto done;
2383 }
2384 break;
2385
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002386 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 /*
2388 * Setting all of a previous oldext extent to newext.
2389 * The left neighbor is contiguous, the right is not.
2390 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002391 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002392
Christoph Hellwigec90c552011-05-23 08:52:53 +00002393 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2394 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2395 LEFT.br_blockcount + PREV.br_blockcount);
2396 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2397
2398 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 ip->i_d.di_nextents--;
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, PREV.br_startoff,
2405 PREV.br_startblock, PREV.br_blockcount,
2406 &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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2416 LEFT.br_startblock,
2417 LEFT.br_blockcount + PREV.br_blockcount,
2418 LEFT.br_state)))
2419 goto done;
2420 }
2421 break;
2422
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002423 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 /*
2425 * Setting all of a previous oldext extent to newext.
2426 * The right neighbor is contiguous, the left is not.
2427 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002428 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 xfs_bmbt_set_blockcount(ep,
2430 PREV.br_blockcount + RIGHT.br_blockcount);
2431 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002432 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2433 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 ip->i_d.di_nextents--;
2435 if (cur == NULL)
2436 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2437 else {
2438 rval = XFS_ILOG_CORE;
2439 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2440 RIGHT.br_startblock,
2441 RIGHT.br_blockcount, &i)))
2442 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002443 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002444 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002446 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002447 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002449 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2451 new->br_startblock,
2452 new->br_blockcount + RIGHT.br_blockcount,
2453 newext)))
2454 goto done;
2455 }
2456 break;
2457
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002458 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 /*
2460 * Setting all of a previous oldext extent to newext.
2461 * Neither the left nor right neighbors are contiguous with
2462 * the new one.
2463 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002464 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002466 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002467
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 if (cur == NULL)
2469 rval = XFS_ILOG_DEXT;
2470 else {
2471 rval = 0;
2472 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2473 new->br_startblock, new->br_blockcount,
2474 &i)))
2475 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002476 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2478 new->br_startblock, new->br_blockcount,
2479 newext)))
2480 goto done;
2481 }
2482 break;
2483
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002484 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 /*
2486 * Setting the first part of a previous oldext extent to newext.
2487 * The left neighbor is contiguous.
2488 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002489 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2490 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 LEFT.br_blockcount + new->br_blockcount);
2492 xfs_bmbt_set_startoff(ep,
2493 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002494 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002495
Christoph Hellwigec90c552011-05-23 08:52:53 +00002496 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 xfs_bmbt_set_startblock(ep,
2498 new->br_startblock + new->br_blockcount);
2499 xfs_bmbt_set_blockcount(ep,
2500 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002501 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002502
Christoph Hellwigec90c552011-05-23 08:52:53 +00002503 --*idx;
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (cur == NULL)
2506 rval = XFS_ILOG_DEXT;
2507 else {
2508 rval = 0;
2509 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2510 PREV.br_startblock, PREV.br_blockcount,
2511 &i)))
2512 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002513 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if ((error = xfs_bmbt_update(cur,
2515 PREV.br_startoff + new->br_blockcount,
2516 PREV.br_startblock + new->br_blockcount,
2517 PREV.br_blockcount - new->br_blockcount,
2518 oldext)))
2519 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002520 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002522 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 LEFT.br_startblock,
2524 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002525 LEFT.br_state);
2526 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 goto done;
2528 }
2529 break;
2530
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002531 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 /*
2533 * Setting the first part of a previous oldext extent to newext.
2534 * The left neighbor is not contiguous.
2535 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002536 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2538 xfs_bmbt_set_startoff(ep, new_endoff);
2539 xfs_bmbt_set_blockcount(ep,
2540 PREV.br_blockcount - new->br_blockcount);
2541 xfs_bmbt_set_startblock(ep,
2542 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002543 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002544
Christoph Hellwigec90c552011-05-23 08:52:53 +00002545 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 ip->i_d.di_nextents++;
2547 if (cur == NULL)
2548 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2549 else {
2550 rval = XFS_ILOG_CORE;
2551 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2552 PREV.br_startblock, PREV.br_blockcount,
2553 &i)))
2554 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002555 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if ((error = xfs_bmbt_update(cur,
2557 PREV.br_startoff + new->br_blockcount,
2558 PREV.br_startblock + new->br_blockcount,
2559 PREV.br_blockcount - new->br_blockcount,
2560 oldext)))
2561 goto done;
2562 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002563 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002565 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
2567 break;
2568
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002569 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 /*
2571 * Setting the last part of a previous oldext extent to newext.
2572 * The right neighbor is contiguous with the new allocation.
2573 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002574 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 xfs_bmbt_set_blockcount(ep,
2576 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002577 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2578
2579 ++*idx;
2580
2581 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2582 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002583 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002585 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if (cur == NULL)
2588 rval = XFS_ILOG_DEXT;
2589 else {
2590 rval = 0;
2591 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2592 PREV.br_startblock,
2593 PREV.br_blockcount, &i)))
2594 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002595 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2597 PREV.br_startblock,
2598 PREV.br_blockcount - new->br_blockcount,
2599 oldext)))
2600 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002601 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 goto done;
2603 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2604 new->br_startblock,
2605 new->br_blockcount + RIGHT.br_blockcount,
2606 newext)))
2607 goto done;
2608 }
2609 break;
2610
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002611 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 /*
2613 * Setting the last part of a previous oldext extent to newext.
2614 * The right neighbor is not contiguous.
2615 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002616 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 xfs_bmbt_set_blockcount(ep,
2618 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002619 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002620
Christoph Hellwigec90c552011-05-23 08:52:53 +00002621 ++*idx;
2622 xfs_iext_insert(ip, *idx, 1, new, state);
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 ip->i_d.di_nextents++;
2625 if (cur == NULL)
2626 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2627 else {
2628 rval = XFS_ILOG_CORE;
2629 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2630 PREV.br_startblock, PREV.br_blockcount,
2631 &i)))
2632 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002633 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2635 PREV.br_startblock,
2636 PREV.br_blockcount - new->br_blockcount,
2637 oldext)))
2638 goto done;
2639 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2640 new->br_startblock, new->br_blockcount,
2641 &i)))
2642 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002643 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002645 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002647 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
2649 break;
2650
2651 case 0:
2652 /*
2653 * Setting the middle part of a previous oldext extent to
2654 * newext. Contiguity is impossible here.
2655 * One extent becomes three extents.
2656 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002657 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 xfs_bmbt_set_blockcount(ep,
2659 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002660 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 r[0] = *new;
2663 r[1].br_startoff = new_endoff;
2664 r[1].br_blockcount =
2665 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2666 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2667 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002668
2669 ++*idx;
2670 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 ip->i_d.di_nextents += 2;
2673 if (cur == NULL)
2674 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2675 else {
2676 rval = XFS_ILOG_CORE;
2677 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2678 PREV.br_startblock, PREV.br_blockcount,
2679 &i)))
2680 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002681 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 /* new right extent - oldext */
2683 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2684 r[1].br_startblock, r[1].br_blockcount,
2685 r[1].br_state)))
2686 goto done;
2687 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002689 cur->bc_rec.b.br_blockcount =
2690 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002691 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002693 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002694 /*
2695 * Reset the cursor to the position of the new extent
2696 * we are about to insert as we can't trust it after
2697 * the previous insert.
2698 */
2699 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2700 new->br_startblock, new->br_blockcount,
2701 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002703 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002705 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002706 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002708 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 }
2710 break;
2711
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002712 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2713 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2714 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2715 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2716 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2717 case BMAP_LEFT_CONTIG:
2718 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 /*
2720 * These cases are all impossible.
2721 */
2722 ASSERT(0);
2723 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002724
Darrick J. Wong9c194642016-08-03 12:16:05 +10002725 /* update reverse mappings */
2726 error = xfs_rmap_convert_extent(mp, dfops, ip, XFS_DATA_FORK, new);
2727 if (error)
2728 goto done;
2729
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002730 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002731 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002732 int tmp_logflags; /* partial log flag return val */
2733
2734 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002735 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002736 0, &tmp_logflags, XFS_DATA_FORK);
2737 *logflagsp |= tmp_logflags;
2738 if (error)
2739 goto done;
2740 }
2741
2742 /* clear out the allocated field, done with it now in any case. */
2743 if (cur) {
2744 cur->bc_private.b.allocated = 0;
2745 *curp = cur;
2746 }
2747
2748 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002750 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 return error;
2752#undef LEFT
2753#undef RIGHT
2754#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755}
2756
2757/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002758 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002760STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761xfs_bmap_add_extent_hole_delay(
2762 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002763 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002764 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002766 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2768 xfs_filblks_t newlen=0; /* new indirect size */
2769 xfs_filblks_t oldlen=0; /* old indirect size */
2770 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2771 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002772 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002774 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002776 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002777
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 /*
2779 * Check and set flags if this segment has a left neighbor
2780 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002781 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002782 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002783 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002784
2785 if (isnullstartblock(left.br_startblock))
2786 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002788
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 /*
2790 * Check and set flags if the current (right) segment exists.
2791 * If it doesn't exist, we're converting the hole at end-of-file.
2792 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002793 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002794 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002795 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002796
2797 if (isnullstartblock(right.br_startblock))
2798 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 /*
2802 * Set contiguity flags on the left and right neighbors.
2803 * Don't let extents get too large, even if the pieces are contiguous.
2804 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002805 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2806 left.br_startoff + left.br_blockcount == new->br_startoff &&
2807 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2808 state |= BMAP_LEFT_CONTIG;
2809
2810 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2811 new->br_startoff + new->br_blockcount == right.br_startoff &&
2812 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2813 (!(state & BMAP_LEFT_CONTIG) ||
2814 (left.br_blockcount + new->br_blockcount +
2815 right.br_blockcount <= MAXEXTLEN)))
2816 state |= BMAP_RIGHT_CONTIG;
2817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 /*
2819 * Switch out based on the contiguity flags.
2820 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002821 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2822 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 /*
2824 * New allocation is contiguous with delayed allocations
2825 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002826 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002828 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 temp = left.br_blockcount + new->br_blockcount +
2830 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002831
Christoph Hellwigec90c552011-05-23 08:52:53 +00002832 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2833 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002834 oldlen = startblockval(left.br_startblock) +
2835 startblockval(new->br_startblock) +
2836 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002838 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002839 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002840 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002841
Christoph Hellwigec90c552011-05-23 08:52:53 +00002842 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 break;
2844
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002845 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 /*
2847 * New allocation is contiguous with a delayed allocation
2848 * on the left.
2849 * Merge the new allocation with the left neighbor.
2850 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002851 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002853
2854 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2855 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002856 oldlen = startblockval(left.br_startblock) +
2857 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002859 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002860 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002861 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 break;
2863
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002864 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 /*
2866 * New allocation is contiguous with a delayed allocation
2867 * on the right.
2868 * Merge the new allocation with the right neighbor.
2869 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002870 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002872 oldlen = startblockval(new->br_startblock) +
2873 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002875 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2876 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002877 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002878 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 break;
2880
2881 case 0:
2882 /*
2883 * New allocation is not contiguous with another
2884 * delayed allocation.
2885 * Insert a new entry.
2886 */
2887 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002888 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 break;
2890 }
2891 if (oldlen != newlen) {
2892 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002893 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2894 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 /*
2896 * Nothing to do for disk quota accounting here.
2897 */
2898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899}
2900
2901/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002902 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 */
2904STATIC int /* error */
2905xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002906 struct xfs_bmalloca *bma,
2907 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002909 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 int i; /* temp state */
2912 xfs_ifork_t *ifp; /* inode fork pointer */
2913 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2914 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002915 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002917 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Eric Sandeenf1f96c42016-01-04 16:10:42 +11002919 mp = bma->ip->i_mount;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002920 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002921
Christoph Hellwigc6534242011-09-18 20:41:05 +00002922 ASSERT(bma->idx >= 0);
2923 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002924 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002925 ASSERT(!bma->cur ||
2926 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002927
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002928 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002929
2930 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002931 if (whichfork == XFS_ATTR_FORK)
2932 state |= BMAP_ATTRFORK;
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 /*
2935 * Check and set flags if this segment has a left neighbor.
2936 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002937 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002938 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002939 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002940 if (isnullstartblock(left.br_startblock))
2941 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 /*
2945 * Check and set flags if this segment has a current value.
2946 * Not true if we're inserting into the "hole" at eof.
2947 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002948 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002949 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002950 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002951 if (isnullstartblock(right.br_startblock))
2952 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 /*
2956 * We're inserting a real allocation between "left" and "right".
2957 * Set the contiguity flags. Don't let extents get too large.
2958 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002959 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2960 left.br_startoff + left.br_blockcount == new->br_startoff &&
2961 left.br_startblock + left.br_blockcount == new->br_startblock &&
2962 left.br_state == new->br_state &&
2963 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2964 state |= BMAP_LEFT_CONTIG;
2965
2966 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2967 new->br_startoff + new->br_blockcount == right.br_startoff &&
2968 new->br_startblock + new->br_blockcount == right.br_startblock &&
2969 new->br_state == right.br_state &&
2970 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2971 (!(state & BMAP_LEFT_CONTIG) ||
2972 left.br_blockcount + new->br_blockcount +
2973 right.br_blockcount <= MAXEXTLEN))
2974 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002976 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 /*
2978 * Select which case we're in here, and implement it.
2979 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002980 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2981 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 /*
2983 * New allocation is contiguous with real allocations on the
2984 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002985 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002987 --bma->idx;
2988 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2989 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 left.br_blockcount + new->br_blockcount +
2991 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002992 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002993
Christoph Hellwigc6534242011-09-18 20:41:05 +00002994 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002995
Christoph Hellwigc6534242011-09-18 20:41:05 +00002996 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
2997 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
2998 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002999 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003000 } else {
3001 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003002 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3003 right.br_startblock, right.br_blockcount,
3004 &i);
3005 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003006 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003007 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003008 error = xfs_btree_delete(bma->cur, &i);
3009 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003010 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003011 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003012 error = xfs_btree_decrement(bma->cur, 0, &i);
3013 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003014 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003015 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003016 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003017 left.br_startblock,
3018 left.br_blockcount +
3019 new->br_blockcount +
3020 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003021 left.br_state);
3022 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003023 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003025 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003027 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 /*
3029 * New allocation is contiguous with a real allocation
3030 * on the left.
3031 * Merge the new allocation with the left neighbor.
3032 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003033 --bma->idx;
3034 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3035 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003037 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003038
Christoph Hellwigc6534242011-09-18 20:41:05 +00003039 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003040 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003041 } else {
3042 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003043 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3044 left.br_startblock, left.br_blockcount,
3045 &i);
3046 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003047 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003048 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003049 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003050 left.br_startblock,
3051 left.br_blockcount +
3052 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003053 left.br_state);
3054 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003055 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003057 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003059 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 /*
3061 * New allocation is contiguous with a real allocation
3062 * on the right.
3063 * Merge the new allocation with the right neighbor.
3064 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003065 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3066 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003067 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 new->br_blockcount + right.br_blockcount,
3069 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003070 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003071
Christoph Hellwigc6534242011-09-18 20:41:05 +00003072 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003073 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003074 } else {
3075 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003076 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003077 right.br_startoff,
3078 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003079 right.br_blockcount, &i);
3080 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003081 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003082 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003083 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003084 new->br_startblock,
3085 new->br_blockcount +
3086 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003087 right.br_state);
3088 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003089 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003091 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093 case 0:
3094 /*
3095 * New allocation is not contiguous with another
3096 * real allocation.
3097 * Insert a new entry.
3098 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003099 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3100 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3101 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3102 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003103 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003104 } else {
3105 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003106 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003107 new->br_startoff,
3108 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003109 new->br_blockcount, &i);
3110 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003111 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003112 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003113 bma->cur->bc_rec.b.br_state = new->br_state;
3114 error = xfs_btree_insert(bma->cur, &i);
3115 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003116 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003117 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003119 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003121
Darrick J. Wong9c194642016-08-03 12:16:05 +10003122 /* add reverse mapping */
3123 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
3124 if (error)
3125 goto done;
3126
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003127 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003128 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003129 int tmp_logflags; /* partial log flag return val */
3130
Christoph Hellwigc6534242011-09-18 20:41:05 +00003131 ASSERT(bma->cur == NULL);
3132 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003133 bma->firstblock, bma->dfops, &bma->cur,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003134 0, &tmp_logflags, whichfork);
3135 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003136 if (error)
3137 goto done;
3138 }
3139
3140 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003141 if (bma->cur)
3142 bma->cur->bc_private.b.allocated = 0;
3143
3144 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003145done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003146 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003147 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148}
3149
Nathan Scottdd9f4382006-01-11 15:28:28 +11003150/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003151 * Functions used in the extent read, allocate and remove paths
3152 */
3153
3154/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003155 * Adjust the size of the new extent based on di_extsize and rt extsize.
3156 */
Dave Chinner68988112013-08-12 20:49:42 +10003157int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003158xfs_bmap_extsize_align(
3159 xfs_mount_t *mp,
3160 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3161 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3162 xfs_extlen_t extsz, /* align to this extent size */
3163 int rt, /* is this a realtime inode? */
3164 int eof, /* is extent at end-of-file? */
3165 int delay, /* creating delalloc extent? */
3166 int convert, /* overwriting unwritten extent? */
3167 xfs_fileoff_t *offp, /* in/out: aligned offset */
3168 xfs_extlen_t *lenp) /* in/out: aligned length */
3169{
3170 xfs_fileoff_t orig_off; /* original offset */
3171 xfs_extlen_t orig_alen; /* original length */
3172 xfs_fileoff_t orig_end; /* original off+len */
3173 xfs_fileoff_t nexto; /* next file offset */
3174 xfs_fileoff_t prevo; /* previous file offset */
3175 xfs_fileoff_t align_off; /* temp for offset */
3176 xfs_extlen_t align_alen; /* temp for length */
3177 xfs_extlen_t temp; /* temp for calculations */
3178
3179 if (convert)
3180 return 0;
3181
3182 orig_off = align_off = *offp;
3183 orig_alen = align_alen = *lenp;
3184 orig_end = orig_off + orig_alen;
3185
3186 /*
3187 * If this request overlaps an existing extent, then don't
3188 * attempt to perform any additional alignment.
3189 */
3190 if (!delay && !eof &&
3191 (orig_off >= gotp->br_startoff) &&
3192 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3193 return 0;
3194 }
3195
3196 /*
3197 * If the file offset is unaligned vs. the extent size
3198 * we need to align it. This will be possible unless
3199 * the file was previously written with a kernel that didn't
3200 * perform this alignment, or if a truncate shot us in the
3201 * foot.
3202 */
3203 temp = do_mod(orig_off, extsz);
3204 if (temp) {
3205 align_alen += temp;
3206 align_off -= temp;
3207 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003208
3209 /* Same adjustment for the end of the requested area. */
3210 temp = (align_alen % extsz);
3211 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003212 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003213
3214 /*
3215 * For large extent hint sizes, the aligned extent might be larger than
3216 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3217 * the length back under MAXEXTLEN. The outer allocation loops handle
3218 * short allocation just fine, so it is safe to do this. We only want to
3219 * do it when we are forced to, though, because it means more allocation
3220 * operations are required.
3221 */
3222 while (align_alen > MAXEXTLEN)
3223 align_alen -= extsz;
3224 ASSERT(align_alen <= MAXEXTLEN);
3225
Nathan Scottdd9f4382006-01-11 15:28:28 +11003226 /*
3227 * If the previous block overlaps with this proposed allocation
3228 * then move the start forward without adjusting the length.
3229 */
3230 if (prevp->br_startoff != NULLFILEOFF) {
3231 if (prevp->br_startblock == HOLESTARTBLOCK)
3232 prevo = prevp->br_startoff;
3233 else
3234 prevo = prevp->br_startoff + prevp->br_blockcount;
3235 } else
3236 prevo = 0;
3237 if (align_off != orig_off && align_off < prevo)
3238 align_off = prevo;
3239 /*
3240 * If the next block overlaps with this proposed allocation
3241 * then move the start back without adjusting the length,
3242 * but not before offset 0.
3243 * This may of course make the start overlap previous block,
3244 * and if we hit the offset 0 limit then the next block
3245 * can still overlap too.
3246 */
3247 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3248 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3249 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3250 nexto = gotp->br_startoff + gotp->br_blockcount;
3251 else
3252 nexto = gotp->br_startoff;
3253 } else
3254 nexto = NULLFILEOFF;
3255 if (!eof &&
3256 align_off + align_alen != orig_end &&
3257 align_off + align_alen > nexto)
3258 align_off = nexto > align_alen ? nexto - align_alen : 0;
3259 /*
3260 * If we're now overlapping the next or previous extent that
3261 * means we can't fit an extsz piece in this hole. Just move
3262 * the start forward to the first valid spot and set
3263 * the length so we hit the end.
3264 */
3265 if (align_off != orig_off && align_off < prevo)
3266 align_off = prevo;
3267 if (align_off + align_alen != orig_end &&
3268 align_off + align_alen > nexto &&
3269 nexto != NULLFILEOFF) {
3270 ASSERT(nexto > prevo);
3271 align_alen = nexto - align_off;
3272 }
3273
3274 /*
3275 * If realtime, and the result isn't a multiple of the realtime
3276 * extent size we need to remove blocks until it is.
3277 */
3278 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3279 /*
3280 * We're not covering the original request, or
3281 * we won't be able to once we fix the length.
3282 */
3283 if (orig_off < align_off ||
3284 orig_end > align_off + align_alen ||
3285 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003286 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003287 /*
3288 * Try to fix it by moving the start up.
3289 */
3290 if (align_off + temp <= orig_off) {
3291 align_alen -= temp;
3292 align_off += temp;
3293 }
3294 /*
3295 * Try to fix it by moving the end in.
3296 */
3297 else if (align_off + align_alen - temp >= orig_end)
3298 align_alen -= temp;
3299 /*
3300 * Set the start to the minimum then trim the length.
3301 */
3302 else {
3303 align_alen -= orig_off - align_off;
3304 align_off = orig_off;
3305 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3306 }
3307 /*
3308 * Result doesn't cover the request, fail it.
3309 */
3310 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003311 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003312 } else {
3313 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003314 /* see MAXEXTLEN handling above */
3315 ASSERT(orig_end <= align_off + align_alen ||
3316 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003317 }
3318
3319#ifdef DEBUG
3320 if (!eof && gotp->br_startoff != NULLFILEOFF)
3321 ASSERT(align_off + align_alen <= gotp->br_startoff);
3322 if (prevp->br_startoff != NULLFILEOFF)
3323 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3324#endif
3325
3326 *lenp = align_alen;
3327 *offp = align_off;
3328 return 0;
3329}
3330
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331#define XFS_ALLOC_GAP_UNITS 4
3332
Dave Chinner68988112013-08-12 20:49:42 +10003333void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003334xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003335 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
3337 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3339 xfs_mount_t *mp; /* mount point structure */
3340 int nullfb; /* true if ap->firstblock isn't set */
3341 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
3343#define ISVALID(x,y) \
3344 (rt ? \
3345 (x) < mp->m_sb.sb_rblocks : \
3346 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3347 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3348 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003351 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003352 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3353 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003354 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 /*
3356 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003357 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003359 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3360 !isnullstartblock(ap->prev.br_startblock) &&
3361 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3362 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003363 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 /*
3365 * Adjust for the gap between prevp and us.
3366 */
Dave Chinner3a756672011-09-18 20:40:58 +00003367 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003368 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003370 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3371 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 }
3373 /*
3374 * If not at eof, then compare the two neighbor blocks.
3375 * Figure out whether either one gives us a good starting point,
3376 * and pick the better one.
3377 */
3378 else if (!ap->eof) {
3379 xfs_fsblock_t gotbno; /* right side block number */
3380 xfs_fsblock_t gotdiff=0; /* right side difference */
3381 xfs_fsblock_t prevbno; /* left side block number */
3382 xfs_fsblock_t prevdiff=0; /* left side difference */
3383
3384 /*
3385 * If there's a previous (left) block, select a requested
3386 * start block based on it.
3387 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003388 if (ap->prev.br_startoff != NULLFILEOFF &&
3389 !isnullstartblock(ap->prev.br_startblock) &&
3390 (prevbno = ap->prev.br_startblock +
3391 ap->prev.br_blockcount) &&
3392 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 /*
3394 * Calculate gap to end of previous block.
3395 */
Dave Chinner3a756672011-09-18 20:40:58 +00003396 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003397 (ap->prev.br_startoff +
3398 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 /*
3400 * Figure the startblock based on the previous block's
3401 * end and the gap size.
3402 * Heuristic!
3403 * If the gap is large relative to the piece we're
3404 * allocating, or using it gives us an invalid block
3405 * number, then just use the end of the previous block.
3406 */
Dave Chinner3a756672011-09-18 20:40:58 +00003407 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003409 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 prevbno += adjust;
3411 else
3412 prevdiff += adjust;
3413 /*
3414 * If the firstblock forbids it, can't use it,
3415 * must use default.
3416 */
3417 if (!rt && !nullfb &&
3418 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3419 prevbno = NULLFSBLOCK;
3420 }
3421 /*
3422 * No previous block or can't follow it, just default.
3423 */
3424 else
3425 prevbno = NULLFSBLOCK;
3426 /*
3427 * If there's a following (right) block, select a requested
3428 * start block based on it.
3429 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003430 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 /*
3432 * Calculate gap to start of next block.
3433 */
Dave Chinner3a756672011-09-18 20:40:58 +00003434 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 /*
3436 * Figure the startblock based on the next block's
3437 * start and the gap size.
3438 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003439 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 /*
3441 * Heuristic!
3442 * If the gap is large relative to the piece we're
3443 * allocating, or using it gives us an invalid block
3444 * number, then just use the start of the next block
3445 * offset by our length.
3446 */
Dave Chinner3a756672011-09-18 20:40:58 +00003447 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 ISVALID(gotbno - gotdiff, gotbno))
3449 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003450 else if (ISVALID(gotbno - ap->length, gotbno)) {
3451 gotbno -= ap->length;
3452 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 } else
3454 gotdiff += adjust;
3455 /*
3456 * If the firstblock forbids it, can't use it,
3457 * must use default.
3458 */
3459 if (!rt && !nullfb &&
3460 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3461 gotbno = NULLFSBLOCK;
3462 }
3463 /*
3464 * No next block, just default.
3465 */
3466 else
3467 gotbno = NULLFSBLOCK;
3468 /*
3469 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003470 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 */
3472 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003473 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003475 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003477 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003479#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003480}
3481
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003482static int
3483xfs_bmap_longest_free_extent(
3484 struct xfs_trans *tp,
3485 xfs_agnumber_t ag,
3486 xfs_extlen_t *blen,
3487 int *notinit)
3488{
3489 struct xfs_mount *mp = tp->t_mountp;
3490 struct xfs_perag *pag;
3491 xfs_extlen_t longest;
3492 int error = 0;
3493
3494 pag = xfs_perag_get(mp, ag);
3495 if (!pag->pagf_init) {
3496 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3497 if (error)
3498 goto out;
3499
3500 if (!pag->pagf_init) {
3501 *notinit = 1;
3502 goto out;
3503 }
3504 }
3505
Dave Chinner50adbcb2015-06-22 10:04:31 +10003506 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003507 xfs_alloc_min_freelist(mp, pag),
3508 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003509 if (*blen < longest)
3510 *blen = longest;
3511
3512out:
3513 xfs_perag_put(pag);
3514 return error;
3515}
3516
3517static void
3518xfs_bmap_select_minlen(
3519 struct xfs_bmalloca *ap,
3520 struct xfs_alloc_arg *args,
3521 xfs_extlen_t *blen,
3522 int notinit)
3523{
3524 if (notinit || *blen < ap->minlen) {
3525 /*
3526 * Since we did a BUF_TRYLOCK above, it is possible that
3527 * there is space for this request.
3528 */
3529 args->minlen = ap->minlen;
3530 } else if (*blen < args->maxlen) {
3531 /*
3532 * If the best seen length is less than the request length,
3533 * use the best as the minimum.
3534 */
3535 args->minlen = *blen;
3536 } else {
3537 /*
3538 * Otherwise we've seen an extent as big as maxlen, use that
3539 * as the minimum.
3540 */
3541 args->minlen = args->maxlen;
3542 }
3543}
3544
Nathan Scotta365bdd2006-03-14 13:34:16 +11003545STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003546xfs_bmap_btalloc_nullfb(
3547 struct xfs_bmalloca *ap,
3548 struct xfs_alloc_arg *args,
3549 xfs_extlen_t *blen)
3550{
3551 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003552 xfs_agnumber_t ag, startag;
3553 int notinit = 0;
3554 int error;
3555
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003556 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003557 args->total = ap->total;
3558
Christoph Hellwigc467c042010-02-15 23:34:42 +00003559 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3560 if (startag == NULLAGNUMBER)
3561 startag = ag = 0;
3562
Dave Chinner14b064c2011-01-27 12:16:28 +11003563 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003564 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3565 &notinit);
3566 if (error)
3567 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003568
Christoph Hellwigc467c042010-02-15 23:34:42 +00003569 if (++ag == mp->m_sb.sb_agcount)
3570 ag = 0;
3571 if (ag == startag)
3572 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003573 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003574
3575 xfs_bmap_select_minlen(ap, args, blen, notinit);
3576 return 0;
3577}
3578
3579STATIC int
3580xfs_bmap_btalloc_filestreams(
3581 struct xfs_bmalloca *ap,
3582 struct xfs_alloc_arg *args,
3583 xfs_extlen_t *blen)
3584{
3585 struct xfs_mount *mp = ap->ip->i_mount;
3586 xfs_agnumber_t ag;
3587 int notinit = 0;
3588 int error;
3589
3590 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3591 args->total = ap->total;
3592
3593 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3594 if (ag == NULLAGNUMBER)
3595 ag = 0;
3596
3597 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3598 if (error)
3599 return error;
3600
3601 if (*blen < args->maxlen) {
3602 error = xfs_filestream_new_ag(ap, &ag);
3603 if (error)
3604 return error;
3605
3606 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3607 &notinit);
3608 if (error)
3609 return error;
3610
3611 }
3612
3613 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003614
3615 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003616 * Set the failure fallback case to look in the selected AG as stream
3617 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003618 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003619 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003620 return 0;
3621}
3622
3623STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003624xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003625 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003626{
3627 xfs_mount_t *mp; /* mount point structure */
3628 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003629 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003630 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003631 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003632 xfs_alloc_arg_t args;
3633 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003634 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003635 int nullfb; /* true if ap->firstblock isn't set */
3636 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003637 int tryagain;
3638 int error;
Dave Chinner33177f02013-12-12 16:34:36 +11003639 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003640
Dave Chinnera99ebf42011-12-01 11:24:20 +00003641 ASSERT(ap->length);
3642
Nathan Scotta365bdd2006-03-14 13:34:16 +11003643 mp = ap->ip->i_mount;
Dave Chinner33177f02013-12-12 16:34:36 +11003644
3645 /* stripe alignment for allocation is determined by mount parameters */
3646 stripe_align = 0;
3647 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3648 stripe_align = mp->m_swidth;
3649 else if (mp->m_dalign)
3650 stripe_align = mp->m_dalign;
3651
Dave Chinner292378e2016-09-26 08:21:28 +10003652 if (xfs_alloc_is_userdata(ap->datatype))
3653 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003654 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003655 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003656 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003657 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003658 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003659 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003660 }
Dave Chinner33177f02013-12-12 16:34:36 +11003661
3662
Dave Chinner0937e0f2011-09-18 20:40:57 +00003663 nullfb = *ap->firstblock == NULLFSBLOCK;
3664 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003665 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003666 if (xfs_alloc_is_userdata(ap->datatype) &&
3667 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003668 ag = xfs_filestream_lookup_ag(ap->ip);
3669 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003670 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003671 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003672 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003673 }
3674 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003675 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003676
3677 xfs_bmap_adjacent(ap);
3678
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003680 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 * it's in the right allocation group.
3682 */
Dave Chinner3a756672011-09-18 20:40:58 +00003683 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 ;
3685 else
Dave Chinner3a756672011-09-18 20:40:58 +00003686 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 * Normal allocation, done through xfs_alloc_vextent.
3689 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003690 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003691 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003692 args.tp = ap->tp;
3693 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003694 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003695 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003696
3697 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003698 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003699 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003700 blen = 0;
3701 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003702 /*
3703 * Search for an allocation group with a single extent large
3704 * enough for the request. If one isn't found, then adjust
3705 * the minimum allocation size to the largest space found.
3706 */
Dave Chinner292378e2016-09-26 08:21:28 +10003707 if (xfs_alloc_is_userdata(ap->datatype) &&
3708 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003709 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3710 else
3711 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003712 if (error)
3713 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003714 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003715 if (xfs_inode_is_filestream(ap->ip))
3716 args.type = XFS_ALLOCTYPE_FIRST_AG;
3717 else
3718 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003719 args.total = args.minlen = ap->minlen;
3720 } else {
3721 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3722 args.total = ap->total;
3723 args.minlen = ap->minlen;
3724 }
David Chinner957d0eb2007-06-18 16:50:37 +10003725 /* apply extent size hints if obtained earlier */
3726 if (unlikely(align)) {
3727 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003728 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003729 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003730 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003731 args.prod = 1;
3732 args.mod = 0;
3733 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003734 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003735 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003736 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3737 }
3738 /*
3739 * If we are not low on available data blocks, and the
3740 * underlying logical volume manager is a stripe, and
3741 * the file offset is zero then try to allocate data
3742 * blocks on stripe unit boundary.
3743 * NOTE: ap->aeof is only set if the allocation length
3744 * is >= the stripe unit and the allocation offset is
3745 * at the end of file.
3746 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003747 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003748 if (!ap->offset) {
Dave Chinner33177f02013-12-12 16:34:36 +11003749 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003750 atype = args.type;
3751 isaligned = 1;
3752 /*
3753 * Adjust for alignment
3754 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003755 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003756 args.minlen = blen - args.alignment;
3757 args.minalignslop = 0;
3758 } else {
3759 /*
3760 * First try an exact bno allocation.
3761 * If it fails then do a near or start bno
3762 * allocation with alignment turned on.
3763 */
3764 atype = args.type;
3765 tryagain = 1;
3766 args.type = XFS_ALLOCTYPE_THIS_BNO;
3767 args.alignment = 1;
3768 /*
3769 * Compute the minlen+alignment for the
3770 * next case. Set slop so that the value
3771 * of minlen+alignment+slop doesn't go up
3772 * between the calls.
3773 */
Dave Chinner33177f02013-12-12 16:34:36 +11003774 if (blen > stripe_align && blen <= args.maxlen)
3775 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003776 else
3777 nextminlen = args.minlen;
Dave Chinner33177f02013-12-12 16:34:36 +11003778 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003779 args.minalignslop =
Dave Chinner33177f02013-12-12 16:34:36 +11003780 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003781 args.minlen - 1;
3782 else
3783 args.minalignslop = 0;
3784 }
3785 } else {
3786 args.alignment = 1;
3787 args.minalignslop = 0;
3788 }
3789 args.minleft = ap->minleft;
3790 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003791 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003792 args.datatype = ap->datatype;
3793 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003794 args.ip = ap->ip;
3795
3796 error = xfs_alloc_vextent(&args);
3797 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003798 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003799
Nathan Scotta365bdd2006-03-14 13:34:16 +11003800 if (tryagain && args.fsbno == NULLFSBLOCK) {
3801 /*
3802 * Exact allocation failed. Now try with alignment
3803 * turned on.
3804 */
3805 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003806 args.fsbno = ap->blkno;
Dave Chinner33177f02013-12-12 16:34:36 +11003807 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003808 args.minlen = nextminlen;
3809 args.minalignslop = 0;
3810 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 if ((error = xfs_alloc_vextent(&args)))
3812 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003813 }
3814 if (isaligned && args.fsbno == NULLFSBLOCK) {
3815 /*
3816 * allocation failed, so turn off alignment and
3817 * try again.
3818 */
3819 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003820 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003821 args.alignment = 0;
3822 if ((error = xfs_alloc_vextent(&args)))
3823 return error;
3824 }
3825 if (args.fsbno == NULLFSBLOCK && nullfb &&
3826 args.minlen > ap->minlen) {
3827 args.minlen = ap->minlen;
3828 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003829 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003830 if ((error = xfs_alloc_vextent(&args)))
3831 return error;
3832 }
3833 if (args.fsbno == NULLFSBLOCK && nullfb) {
3834 args.fsbno = 0;
3835 args.type = XFS_ALLOCTYPE_FIRST_AG;
3836 args.total = ap->minlen;
3837 args.minleft = 0;
3838 if ((error = xfs_alloc_vextent(&args)))
3839 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003840 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003841 }
3842 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003843 /*
3844 * check the allocation happened at the same or higher AG than
3845 * the first block that was allocated.
3846 */
3847 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3848 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3849 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003850 (ap->dfops->dop_low &&
Dave Chinner0937e0f2011-09-18 20:40:57 +00003851 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3852 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3853
Dave Chinner3a756672011-09-18 20:40:58 +00003854 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003855 if (*ap->firstblock == NULLFSBLOCK)
3856 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003857 ASSERT(nullfb || fb_agno == args.agno ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003858 (ap->dfops->dop_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003859 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003860 ap->ip->i_d.di_nblocks += args.len;
3861 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3862 if (ap->wasdel)
3863 ap->ip->i_delayed_blks -= args.len;
3864 /*
3865 * Adjust the disk quota also. This was reserved
3866 * earlier.
3867 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003868 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003869 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3870 XFS_TRANS_DQ_BCOUNT,
3871 (long) args.len);
3872 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003873 ap->blkno = NULLFSBLOCK;
3874 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 }
3876 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003877}
3878
3879/*
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003880 * For a remap operation, just "allocate" an extent at the address that the
3881 * caller passed in, and ensure that the AGFL is the right size. The caller
3882 * will then map the "allocated" extent into the file somewhere.
3883 */
3884STATIC int
3885xfs_bmap_remap_alloc(
3886 struct xfs_bmalloca *ap)
3887{
3888 struct xfs_trans *tp = ap->tp;
3889 struct xfs_mount *mp = tp->t_mountp;
3890 xfs_agblock_t bno;
3891 struct xfs_alloc_arg args;
3892 int error;
3893
3894 /*
3895 * validate that the block number is legal - the enables us to detect
3896 * and handle a silent filesystem corruption rather than crashing.
3897 */
3898 memset(&args, 0, sizeof(struct xfs_alloc_arg));
3899 args.tp = ap->tp;
3900 args.mp = ap->tp->t_mountp;
3901 bno = *ap->firstblock;
3902 args.agno = XFS_FSB_TO_AGNO(mp, bno);
3903 args.agbno = XFS_FSB_TO_AGBNO(mp, bno);
3904 if (args.agno >= mp->m_sb.sb_agcount ||
3905 args.agbno >= mp->m_sb.sb_agblocks)
3906 return -EFSCORRUPTED;
3907
3908 /* "Allocate" the extent from the range we passed in. */
3909 trace_xfs_bmap_remap_alloc(ap->ip, *ap->firstblock, ap->length);
3910 ap->blkno = bno;
3911 ap->ip->i_d.di_nblocks += ap->length;
3912 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3913
3914 /* Fix the freelist, like a real allocator does. */
3915 args.datatype = ap->datatype;
3916 args.pag = xfs_perag_get(args.mp, args.agno);
3917 ASSERT(args.pag);
3918
3919 /*
3920 * The freelist fixing code will decline the allocation if
3921 * the size and shape of the free space doesn't allow for
3922 * allocating the extent and updating all the metadata that
3923 * happens during an allocation. We're remapping, not
3924 * allocating, so skip that check by pretending to be freeing.
3925 */
3926 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
3927 if (error)
3928 goto error0;
3929error0:
3930 xfs_perag_put(args.pag);
3931 if (error)
3932 trace_xfs_bmap_remap_alloc_error(ap->ip, error, _RET_IP_);
3933 return error;
3934}
3935
3936/*
Nathan Scotta365bdd2006-03-14 13:34:16 +11003937 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3938 * It figures out where to ask the underlying allocator to put the new extent.
3939 */
3940STATIC int
3941xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003942 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003943{
Darrick J. Wongf65306e2016-10-03 09:11:27 -07003944 if (ap->flags & XFS_BMAPI_REMAP)
3945 return xfs_bmap_remap_alloc(ap);
Dave Chinner292378e2016-09-26 08:21:28 +10003946 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3947 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003948 return xfs_bmap_rtalloc(ap);
3949 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950}
3951
3952/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003953 * Trim the returned map to the required bounds
3954 */
3955STATIC void
3956xfs_bmapi_trim_map(
3957 struct xfs_bmbt_irec *mval,
3958 struct xfs_bmbt_irec *got,
3959 xfs_fileoff_t *bno,
3960 xfs_filblks_t len,
3961 xfs_fileoff_t obno,
3962 xfs_fileoff_t end,
3963 int n,
3964 int flags)
3965{
3966 if ((flags & XFS_BMAPI_ENTIRE) ||
3967 got->br_startoff + got->br_blockcount <= obno) {
3968 *mval = *got;
3969 if (isnullstartblock(got->br_startblock))
3970 mval->br_startblock = DELAYSTARTBLOCK;
3971 return;
3972 }
3973
3974 if (obno > *bno)
3975 *bno = obno;
3976 ASSERT((*bno >= obno) || (n == 0));
3977 ASSERT(*bno < end);
3978 mval->br_startoff = *bno;
3979 if (isnullstartblock(got->br_startblock))
3980 mval->br_startblock = DELAYSTARTBLOCK;
3981 else
3982 mval->br_startblock = got->br_startblock +
3983 (*bno - got->br_startoff);
3984 /*
3985 * Return the minimum of what we got and what we asked for for
3986 * the length. We can use the len variable here because it is
3987 * modified below and we could have been there before coming
3988 * here if the first part of the allocation didn't overlap what
3989 * was asked for.
3990 */
3991 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3992 got->br_blockcount - (*bno - got->br_startoff));
3993 mval->br_state = got->br_state;
3994 ASSERT(mval->br_blockcount <= len);
3995 return;
3996}
3997
3998/*
3999 * Update and validate the extent map to return
4000 */
4001STATIC void
4002xfs_bmapi_update_map(
4003 struct xfs_bmbt_irec **map,
4004 xfs_fileoff_t *bno,
4005 xfs_filblks_t *len,
4006 xfs_fileoff_t obno,
4007 xfs_fileoff_t end,
4008 int *n,
4009 int flags)
4010{
4011 xfs_bmbt_irec_t *mval = *map;
4012
4013 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4014 ((mval->br_startoff + mval->br_blockcount) <= end));
4015 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4016 (mval->br_startoff < obno));
4017
4018 *bno = mval->br_startoff + mval->br_blockcount;
4019 *len = end - *bno;
4020 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4021 /* update previous map with new information */
4022 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4023 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4024 ASSERT(mval->br_state == mval[-1].br_state);
4025 mval[-1].br_blockcount = mval->br_blockcount;
4026 mval[-1].br_state = mval->br_state;
4027 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4028 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4029 mval[-1].br_startblock != HOLESTARTBLOCK &&
4030 mval->br_startblock == mval[-1].br_startblock +
4031 mval[-1].br_blockcount &&
4032 ((flags & XFS_BMAPI_IGSTATE) ||
4033 mval[-1].br_state == mval->br_state)) {
4034 ASSERT(mval->br_startoff ==
4035 mval[-1].br_startoff + mval[-1].br_blockcount);
4036 mval[-1].br_blockcount += mval->br_blockcount;
4037 } else if (*n > 0 &&
4038 mval->br_startblock == DELAYSTARTBLOCK &&
4039 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4040 mval->br_startoff ==
4041 mval[-1].br_startoff + mval[-1].br_blockcount) {
4042 mval[-1].br_blockcount += mval->br_blockcount;
4043 mval[-1].br_state = mval->br_state;
4044 } else if (!((*n == 0) &&
4045 ((mval->br_startoff + mval->br_blockcount) <=
4046 obno))) {
4047 mval++;
4048 (*n)++;
4049 }
4050 *map = mval;
4051}
4052
4053/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004054 * Map file blocks to filesystem blocks without allocation.
4055 */
4056int
4057xfs_bmapi_read(
4058 struct xfs_inode *ip,
4059 xfs_fileoff_t bno,
4060 xfs_filblks_t len,
4061 struct xfs_bmbt_irec *mval,
4062 int *nmap,
4063 int flags)
4064{
4065 struct xfs_mount *mp = ip->i_mount;
4066 struct xfs_ifork *ifp;
4067 struct xfs_bmbt_irec got;
4068 struct xfs_bmbt_irec prev;
4069 xfs_fileoff_t obno;
4070 xfs_fileoff_t end;
4071 xfs_extnum_t lastx;
4072 int error;
4073 int eof;
4074 int n = 0;
4075 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4076 XFS_ATTR_FORK : XFS_DATA_FORK;
4077
4078 ASSERT(*nmap >= 1);
4079 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4080 XFS_BMAPI_IGSTATE)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004081 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004082
4083 if (unlikely(XFS_TEST_ERROR(
4084 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4085 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4086 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4087 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004088 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004089 }
4090
4091 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004092 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004093
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004094 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004095
4096 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004097
4098 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4099 error = xfs_iread_extents(NULL, ip, whichfork);
4100 if (error)
4101 return error;
4102 }
4103
4104 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4105 end = bno + len;
4106 obno = bno;
4107
4108 while (bno < end && n < *nmap) {
4109 /* Reading past eof, act as though there's a hole up to end. */
4110 if (eof)
4111 got.br_startoff = end;
4112 if (got.br_startoff > bno) {
4113 /* Reading in a hole. */
4114 mval->br_startoff = bno;
4115 mval->br_startblock = HOLESTARTBLOCK;
4116 mval->br_blockcount =
4117 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4118 mval->br_state = XFS_EXT_NORM;
4119 bno += mval->br_blockcount;
4120 len -= mval->br_blockcount;
4121 mval++;
4122 n++;
4123 continue;
4124 }
4125
4126 /* set up the extent map to return. */
4127 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4128 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4129
4130 /* If we're done, stop now. */
4131 if (bno >= end || n >= *nmap)
4132 break;
4133
4134 /* Else go on to the next record. */
4135 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4136 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4137 else
4138 eof = 1;
4139 }
4140 *nmap = n;
4141 return 0;
4142}
4143
Christoph Hellwig51446f52016-09-19 11:10:21 +10004144int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004145xfs_bmapi_reserve_delalloc(
4146 struct xfs_inode *ip,
4147 xfs_fileoff_t aoff,
4148 xfs_filblks_t len,
4149 struct xfs_bmbt_irec *got,
4150 struct xfs_bmbt_irec *prev,
4151 xfs_extnum_t *lastx,
4152 int eof)
4153{
4154 struct xfs_mount *mp = ip->i_mount;
4155 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4156 xfs_extlen_t alen;
4157 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004158 char rt = XFS_IS_REALTIME_INODE(ip);
4159 xfs_extlen_t extsz;
4160 int error;
4161
4162 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4163 if (!eof)
4164 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4165
4166 /* Figure out the extent size, adjust alen */
4167 extsz = xfs_get_extsz_hint(ip);
4168 if (extsz) {
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004169 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4170 1, 0, &aoff, &alen);
4171 ASSERT(!error);
4172 }
4173
4174 if (rt)
4175 extsz = alen / mp->m_sb.sb_rextsize;
4176
4177 /*
4178 * Make a transaction-less quota reservation for delayed allocation
4179 * blocks. This number gets adjusted later. We return if we haven't
4180 * allocated blocks already inside this loop.
4181 */
4182 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4183 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4184 if (error)
4185 return error;
4186
4187 /*
4188 * Split changing sb for alen and indlen since they could be coming
4189 * from different places.
4190 */
4191 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4192 ASSERT(indlen > 0);
4193
4194 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004195 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004196 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004197 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004198 }
4199
4200 if (error)
4201 goto out_unreserve_quota;
4202
Dave Chinner0d485ad2015-02-23 21:22:03 +11004203 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004204 if (error)
4205 goto out_unreserve_blocks;
4206
4207
4208 ip->i_delayed_blks += alen;
4209
4210 got->br_startoff = aoff;
4211 got->br_startblock = nullstartblock(indlen);
4212 got->br_blockcount = alen;
4213 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004214 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004215
4216 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004217 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4218 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004219 */
4220 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4221
4222 ASSERT(got->br_startoff <= aoff);
4223 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4224 ASSERT(isnullstartblock(got->br_startblock));
4225 ASSERT(got->br_state == XFS_EXT_NORM);
4226 return 0;
4227
4228out_unreserve_blocks:
4229 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004230 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004231 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004232 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004233out_unreserve_quota:
4234 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004235 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004236 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4237 return error;
4238}
4239
Dave Chinnercf11da92014-07-15 07:08:24 +10004240static int
4241xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004242 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004243{
4244 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004245 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004246 XFS_ATTR_FORK : XFS_DATA_FORK;
4247 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004248 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004249 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004250
Dave Chinnera99ebf42011-12-01 11:24:20 +00004251 ASSERT(bma->length > 0);
4252
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004253 /*
4254 * For the wasdelay case, we could also just allocate the stuff asked
4255 * for in this bmap call but that wouldn't be as good.
4256 */
4257 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004258 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4259 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004260 if (bma->idx != NULLEXTNUM && bma->idx) {
4261 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004262 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004263 }
4264 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004265 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004266 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004267 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004268 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004269 }
4270
4271 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004272 * Set the data type being allocated. For the data fork, the first data
4273 * in the file is treated differently to all other allocations. For the
4274 * attribute fork, we only need to ensure the allocated range is not on
4275 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004276 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004277 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004278 bma->datatype = XFS_ALLOC_NOBUSY;
4279 if (whichfork == XFS_DATA_FORK) {
4280 if (bma->offset == 0)
4281 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4282 else
4283 bma->datatype |= XFS_ALLOC_USERDATA;
4284 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004285 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004286 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004287 }
4288
Dave Chinnere04426b2012-10-05 11:06:59 +10004289 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004290
4291 /*
4292 * Only want to do the alignment at the eof if it is userdata and
4293 * allocation length is larger than a stripe unit.
4294 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004295 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004296 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004297 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004298 if (error)
4299 return error;
4300 }
4301
4302 error = xfs_bmap_alloc(bma);
4303 if (error)
4304 return error;
4305
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004306 if (bma->dfops->dop_low)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004307 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004308 if (bma->cur)
4309 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004310 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004311 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004312 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4313 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4314 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004315 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004316 }
4317 /*
4318 * Bump the number of extents we've allocated
4319 * in this call.
4320 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004321 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004322
Dave Chinner29c8d172011-09-18 20:41:00 +00004323 if (bma->cur)
4324 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004325 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4326
Dave Chinner963c30c2011-09-18 20:40:59 +00004327 bma->got.br_startoff = bma->offset;
4328 bma->got.br_startblock = bma->blkno;
4329 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004330 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004331
4332 /*
4333 * A wasdelay extent has been initialized, so shouldn't be flagged
4334 * as unwritten.
4335 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004336 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004337 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004338 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004339
Christoph Hellwigc6534242011-09-18 20:41:05 +00004340 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004341 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004342 else
4343 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004344
Christoph Hellwigc315c902011-09-18 20:41:02 +00004345 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004346 if (error)
4347 return error;
4348
4349 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004350 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4351 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4352 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004353 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004354 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004355
Dave Chinner963c30c2011-09-18 20:40:59 +00004356 ASSERT(bma->got.br_startoff <= bma->offset);
4357 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4358 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004359 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4360 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004361 return 0;
4362}
4363
Dave Chinnerb447fe52011-09-18 20:40:51 +00004364STATIC int
4365xfs_bmapi_convert_unwritten(
4366 struct xfs_bmalloca *bma,
4367 struct xfs_bmbt_irec *mval,
4368 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004369 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004370{
4371 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4372 XFS_ATTR_FORK : XFS_DATA_FORK;
4373 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004374 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004375 int error;
4376
Dave Chinnerb447fe52011-09-18 20:40:51 +00004377 /* check if we need to do unwritten->real conversion */
4378 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4379 (flags & XFS_BMAPI_PREALLOC))
4380 return 0;
4381
4382 /* check if we need to do real->unwritten conversion */
4383 if (mval->br_state == XFS_EXT_NORM &&
4384 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4385 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4386 return 0;
4387
4388 /*
4389 * Modify (by adding) the state flag, if writing.
4390 */
4391 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004392 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4393 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004394 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004395 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004396 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004397 }
4398 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4399 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4400
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004401 /*
4402 * Before insertion into the bmbt, zero the range being converted
4403 * if required.
4404 */
4405 if (flags & XFS_BMAPI_ZERO) {
4406 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4407 mval->br_blockcount);
4408 if (error)
4409 return error;
4410 }
4411
Dave Chinnere0c3da52011-09-18 20:41:01 +00004412 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004413 &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004414 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004415 /*
4416 * Log the inode core unconditionally in the unwritten extent conversion
4417 * path because the conversion might not have done so (e.g., if the
4418 * extent count hasn't changed). We need to make sure the inode is dirty
4419 * in the transaction for the sake of fsync(), even if nothing has
4420 * changed, because fsync() will not force the log for this transaction
4421 * unless it sees the inode pinned.
4422 */
4423 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004424 if (error)
4425 return error;
4426
4427 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004428 * Update our extent pointer, given that
4429 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4430 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004431 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004432 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004433
4434 /*
4435 * We may have combined previously unwritten space with written space,
4436 * so generate another request.
4437 */
4438 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004439 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004440 return 0;
4441}
4442
Christoph Hellwig44032802011-09-18 20:40:48 +00004443/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004444 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4445 * extent state if necessary. Details behaviour is controlled by the flags
4446 * parameter. Only allocates blocks from a single allocation group, to avoid
4447 * locking problems.
4448 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 * The returned value in "firstblock" from the first call in a transaction
4450 * must be remembered and presented to subsequent calls in "firstblock".
4451 * An upper bound for the number of blocks to be allocated is supplied to
4452 * the first call in "total"; if no allocation group has that many free
4453 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4454 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004455int
4456xfs_bmapi_write(
4457 struct xfs_trans *tp, /* transaction pointer */
4458 struct xfs_inode *ip, /* incore inode */
4459 xfs_fileoff_t bno, /* starting file offs. mapped */
4460 xfs_filblks_t len, /* length to map in file */
4461 int flags, /* XFS_BMAPI_... */
4462 xfs_fsblock_t *firstblock, /* first allocated block
4463 controls a.g. for allocs */
4464 xfs_extlen_t total, /* total blocks needed */
4465 struct xfs_bmbt_irec *mval, /* output: map values */
4466 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004467 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004469 struct xfs_mount *mp = ip->i_mount;
4470 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004471 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004472 xfs_fileoff_t end; /* end of mapped file region */
4473 int eof; /* after the end of extents */
4474 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004475 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004476 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004477 int whichfork; /* data or attr fork */
4478 char inhole; /* current location is hole in file */
4479 char wasdelay; /* old extent was delayed */
4480
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004482 xfs_fileoff_t orig_bno; /* original block number value */
4483 int orig_flags; /* original flags arg value */
4484 xfs_filblks_t orig_len; /* original value of len arg */
4485 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4486 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
4488 orig_bno = bno;
4489 orig_len = len;
4490 orig_flags = flags;
4491 orig_mval = mval;
4492 orig_nmap = *nmap;
4493#endif
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004494 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4495 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004496
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004498 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4499 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4500 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004501 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004502 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004503 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004504 ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK);
4505 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP));
4506 ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004507
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004508 /* zeroing is for currently only for data extents, not metadata */
4509 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4510 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4511 /*
4512 * we can allocate unwritten extents or pre-zero allocated blocks,
4513 * but it makes no sense to do both at once. This would result in
4514 * zeroing the unwritten extent twice, but it still being an
4515 * unwritten extent....
4516 */
4517 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4518 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4519
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 if (unlikely(XFS_TEST_ERROR(
4521 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004522 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004524 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004525 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004529 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004530
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004532
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004533 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004534
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004535 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004537 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004539 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004540 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004541 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004542 }
4543
4544 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4545 error = xfs_iread_extents(tp, ip, whichfork);
4546 if (error)
4547 goto error0;
4548 }
4549
Dave Chinnere0c3da52011-09-18 20:41:01 +00004550 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004551 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 n = 0;
4553 end = bno + len;
4554 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004555
4556 bma.tp = tp;
4557 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004558 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004559 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004560 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004561 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004564 inhole = eof || bma.got.br_startoff > bno;
4565 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 /*
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004568 * Make sure we only reflink into a hole.
4569 */
4570 if (flags & XFS_BMAPI_REMAP)
4571 ASSERT(inhole);
4572
4573 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 * First, deal with the hole before the allocated space
4575 * that we found, if any.
4576 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004577 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004578 bma.eof = eof;
4579 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4580 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004581 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004582 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004583
Dave Chinnera99ebf42011-12-01 11:24:20 +00004584 /*
4585 * There's a 32/64 bit type mismatch between the
4586 * allocation length request (which can be 64 bits in
4587 * length) and the bma length request, which is
4588 * xfs_extlen_t and therefore 32 bits. Hence we have to
4589 * check for 32-bit overflows and handle them here.
4590 */
4591 if (len > (xfs_filblks_t)MAXEXTLEN)
4592 bma.length = MAXEXTLEN;
4593 else
4594 bma.length = len;
4595
4596 ASSERT(len > 0);
4597 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004598 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 if (error)
4600 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004601 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004602 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004604
Dave Chinneraef9a892011-09-18 20:40:44 +00004605 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004606 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4607 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
Dave Chinnerb447fe52011-09-18 20:40:51 +00004609 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004610 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004611 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004612 continue;
4613 if (error)
4614 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
Dave Chinneraef9a892011-09-18 20:40:44 +00004616 /* update the extent map to return */
4617 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4618
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 /*
4620 * If we're done, stop now. Stop when we've allocated
4621 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4622 * the transaction may get too big.
4623 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004624 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004626
4627 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004628 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004629 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4630 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4631 &bma.got);
4632 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00004633 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004636
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 /*
4638 * Transform from btree to extents, give it cur.
4639 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004640 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004641 int tmp_logflags = 0;
4642
Dave Chinner29c8d172011-09-18 20:41:00 +00004643 ASSERT(bma.cur);
4644 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004646 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 if (error)
4648 goto error0;
4649 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004650
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004652 XFS_IFORK_NEXTENTS(ip, whichfork) >
4653 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655error0:
4656 /*
4657 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004658 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004660 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004662 bma.logflags &= ~xfs_ilog_fext(whichfork);
4663 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004665 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 /*
4667 * Log whatever the flags say, even if error. Otherwise we might miss
4668 * detecting a case where the data is changed, there's an error,
4669 * and it's not logged so we don't shutdown when we should.
4670 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004671 if (bma.logflags)
4672 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004673
Dave Chinner29c8d172011-09-18 20:41:00 +00004674 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 if (!error) {
4676 ASSERT(*firstblock == NULLFSBLOCK ||
4677 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4678 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004679 bma.cur->bc_private.b.firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004680 (dfops->dop_low &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 XFS_FSB_TO_AGNO(mp, *firstblock) <
4682 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004683 bma.cur->bc_private.b.firstblock)));
4684 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004686 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4688 }
4689 if (!error)
4690 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4691 orig_nmap, *nmap);
4692 return error;
4693}
4694
4695/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004696 * When a delalloc extent is split (e.g., due to a hole punch), the original
4697 * indlen reservation must be shared across the two new extents that are left
4698 * behind.
4699 *
4700 * Given the original reservation and the worst case indlen for the two new
4701 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004702 * reservation fairly across the two new extents. If necessary, steal available
4703 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4704 * ores == 1). The number of stolen blocks is returned. The availability and
4705 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004706 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004707static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004708xfs_bmap_split_indlen(
4709 xfs_filblks_t ores, /* original res. */
4710 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004711 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4712 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004713{
4714 xfs_filblks_t len1 = *indlen1;
4715 xfs_filblks_t len2 = *indlen2;
4716 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004717 xfs_filblks_t stolen = 0;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004718
4719 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004720 * Steal as many blocks as we can to try and satisfy the worst case
4721 * indlen for both new extents.
4722 */
4723 while (nres > ores && avail) {
4724 nres--;
4725 avail--;
4726 stolen++;
4727 }
4728
4729 /*
4730 * The only blocks available are those reserved for the original
4731 * extent and what we can steal from the extent being removed.
4732 * If this still isn't enough to satisfy the combined
4733 * requirements for the two new extents, skim blocks off of each
4734 * of the new reservations until they match what is available.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004735 */
4736 while (nres > ores) {
4737 if (len1) {
4738 len1--;
4739 nres--;
4740 }
4741 if (nres == ores)
4742 break;
4743 if (len2) {
4744 len2--;
4745 nres--;
4746 }
4747 }
4748
4749 *indlen1 = len1;
4750 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004751
4752 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004753}
4754
4755/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004756 * Called by xfs_bmapi to update file extent records and the btree
4757 * after removing space (or undoing a delayed allocation).
4758 */
4759STATIC int /* error */
4760xfs_bmap_del_extent(
4761 xfs_inode_t *ip, /* incore inode pointer */
4762 xfs_trans_t *tp, /* current transaction pointer */
4763 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004764 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004765 xfs_btree_cur_t *cur, /* if null, not a btree */
4766 xfs_bmbt_irec_t *del, /* data to remove from extents */
4767 int *logflagsp, /* inode logging flags */
4768 int whichfork) /* data or attr fork */
4769{
4770 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
4771 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
4772 xfs_fsblock_t del_endblock=0; /* first block past del */
4773 xfs_fileoff_t del_endoff; /* first offset past del */
4774 int delay; /* current block is delayed allocated */
4775 int do_fx; /* free extent at end of routine */
4776 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
4777 int error; /* error return value */
4778 int flags; /* inode logging flags */
4779 xfs_bmbt_irec_t got; /* current extent entry */
4780 xfs_fileoff_t got_endoff; /* first offset past got */
4781 int i; /* temp state */
4782 xfs_ifork_t *ifp; /* inode fork pointer */
4783 xfs_mount_t *mp; /* mount structure */
4784 xfs_filblks_t nblks; /* quota/sb block count */
4785 xfs_bmbt_irec_t new; /* new record to be inserted */
4786 /* REFERENCED */
4787 uint qfield; /* quota field to update */
4788 xfs_filblks_t temp; /* for indirect length calculations */
4789 xfs_filblks_t temp2; /* for indirect length calculations */
4790 int state = 0;
4791
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004792 mp = ip->i_mount;
4793 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004794
4795 if (whichfork == XFS_ATTR_FORK)
4796 state |= BMAP_ATTRFORK;
4797
Dave Chinner9e5987a2013-02-25 12:31:26 +11004798 ifp = XFS_IFORK_PTR(ip, whichfork);
4799 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4800 (uint)sizeof(xfs_bmbt_rec_t)));
4801 ASSERT(del->br_blockcount > 0);
4802 ep = xfs_iext_get_ext(ifp, *idx);
4803 xfs_bmbt_get_all(ep, &got);
4804 ASSERT(got.br_startoff <= del->br_startoff);
4805 del_endoff = del->br_startoff + del->br_blockcount;
4806 got_endoff = got.br_startoff + got.br_blockcount;
4807 ASSERT(got_endoff >= del_endoff);
4808 delay = isnullstartblock(got.br_startblock);
4809 ASSERT(isnullstartblock(del->br_startblock) == delay);
4810 flags = 0;
4811 qfield = 0;
4812 error = 0;
4813 /*
4814 * If deleting a real allocation, must free up the disk space.
4815 */
4816 if (!delay) {
4817 flags = XFS_ILOG_CORE;
4818 /*
4819 * Realtime allocation. Free it and record di_nblocks update.
4820 */
4821 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4822 xfs_fsblock_t bno;
4823 xfs_filblks_t len;
4824
4825 ASSERT(do_mod(del->br_blockcount,
4826 mp->m_sb.sb_rextsize) == 0);
4827 ASSERT(do_mod(del->br_startblock,
4828 mp->m_sb.sb_rextsize) == 0);
4829 bno = del->br_startblock;
4830 len = del->br_blockcount;
4831 do_div(bno, mp->m_sb.sb_rextsize);
4832 do_div(len, mp->m_sb.sb_rextsize);
4833 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4834 if (error)
4835 goto done;
4836 do_fx = 0;
4837 nblks = len * mp->m_sb.sb_rextsize;
4838 qfield = XFS_TRANS_DQ_RTBCOUNT;
4839 }
4840 /*
4841 * Ordinary allocation.
4842 */
4843 else {
4844 do_fx = 1;
4845 nblks = del->br_blockcount;
4846 qfield = XFS_TRANS_DQ_BCOUNT;
4847 }
4848 /*
4849 * Set up del_endblock and cur for later.
4850 */
4851 del_endblock = del->br_startblock + del->br_blockcount;
4852 if (cur) {
4853 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
4854 got.br_startblock, got.br_blockcount,
4855 &i)))
4856 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004857 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004858 }
4859 da_old = da_new = 0;
4860 } else {
4861 da_old = startblockval(got.br_startblock);
4862 da_new = 0;
4863 nblks = 0;
4864 do_fx = 0;
4865 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004866
Dave Chinner9e5987a2013-02-25 12:31:26 +11004867 /*
4868 * Set flag value to use in switch statement.
4869 * Left-contig is 2, right-contig is 1.
4870 */
4871 switch (((got.br_startoff == del->br_startoff) << 1) |
4872 (got_endoff == del_endoff)) {
4873 case 3:
4874 /*
4875 * Matches the whole extent. Delete the entry.
4876 */
4877 xfs_iext_remove(ip, *idx, 1,
4878 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
4879 --*idx;
4880 if (delay)
4881 break;
4882
4883 XFS_IFORK_NEXT_SET(ip, whichfork,
4884 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4885 flags |= XFS_ILOG_CORE;
4886 if (!cur) {
4887 flags |= xfs_ilog_fext(whichfork);
4888 break;
4889 }
4890 if ((error = xfs_btree_delete(cur, &i)))
4891 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004892 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004893 break;
4894
4895 case 2:
4896 /*
4897 * Deleting the first part of the extent.
4898 */
4899 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4900 xfs_bmbt_set_startoff(ep, del_endoff);
4901 temp = got.br_blockcount - del->br_blockcount;
4902 xfs_bmbt_set_blockcount(ep, temp);
4903 if (delay) {
4904 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4905 da_old);
4906 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4907 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4908 da_new = temp;
4909 break;
4910 }
4911 xfs_bmbt_set_startblock(ep, del_endblock);
4912 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4913 if (!cur) {
4914 flags |= xfs_ilog_fext(whichfork);
4915 break;
4916 }
4917 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
4918 got.br_blockcount - del->br_blockcount,
4919 got.br_state)))
4920 goto done;
4921 break;
4922
4923 case 1:
4924 /*
4925 * Deleting the last part of the extent.
4926 */
4927 temp = got.br_blockcount - del->br_blockcount;
4928 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4929 xfs_bmbt_set_blockcount(ep, temp);
4930 if (delay) {
4931 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4932 da_old);
4933 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4934 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4935 da_new = temp;
4936 break;
4937 }
4938 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4939 if (!cur) {
4940 flags |= xfs_ilog_fext(whichfork);
4941 break;
4942 }
4943 if ((error = xfs_bmbt_update(cur, got.br_startoff,
4944 got.br_startblock,
4945 got.br_blockcount - del->br_blockcount,
4946 got.br_state)))
4947 goto done;
4948 break;
4949
4950 case 0:
4951 /*
4952 * Deleting the middle of the extent.
4953 */
4954 temp = del->br_startoff - got.br_startoff;
4955 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4956 xfs_bmbt_set_blockcount(ep, temp);
4957 new.br_startoff = del_endoff;
4958 temp2 = got_endoff - del_endoff;
4959 new.br_blockcount = temp2;
4960 new.br_state = got.br_state;
4961 if (!delay) {
4962 new.br_startblock = del_endblock;
4963 flags |= XFS_ILOG_CORE;
4964 if (cur) {
4965 if ((error = xfs_bmbt_update(cur,
4966 got.br_startoff,
4967 got.br_startblock, temp,
4968 got.br_state)))
4969 goto done;
4970 if ((error = xfs_btree_increment(cur, 0, &i)))
4971 goto done;
4972 cur->bc_rec.b = new;
4973 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10004974 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004975 goto done;
4976 /*
4977 * If get no-space back from btree insert,
4978 * it tried a split, and we have a zero
4979 * block reservation.
4980 * Fix up our state and return the error.
4981 */
Dave Chinner24513372014-06-25 14:58:08 +10004982 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11004983 /*
4984 * Reset the cursor, don't trust
4985 * it after any insert operation.
4986 */
4987 if ((error = xfs_bmbt_lookup_eq(cur,
4988 got.br_startoff,
4989 got.br_startblock,
4990 temp, &i)))
4991 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004992 XFS_WANT_CORRUPTED_GOTO(mp,
4993 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004994 /*
4995 * Update the btree record back
4996 * to the original value.
4997 */
4998 if ((error = xfs_bmbt_update(cur,
4999 got.br_startoff,
5000 got.br_startblock,
5001 got.br_blockcount,
5002 got.br_state)))
5003 goto done;
5004 /*
5005 * Reset the extent record back
5006 * to the original value.
5007 */
5008 xfs_bmbt_set_blockcount(ep,
5009 got.br_blockcount);
5010 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005011 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005012 goto done;
5013 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005014 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005015 } else
5016 flags |= xfs_ilog_fext(whichfork);
5017 XFS_IFORK_NEXT_SET(ip, whichfork,
5018 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5019 } else {
Brian Fosterd34999c2016-03-15 11:42:47 +11005020 xfs_filblks_t stolen;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005021 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005022
5023 /*
5024 * Distribute the original indlen reservation across the
Brian Fosterd34999c2016-03-15 11:42:47 +11005025 * two new extents. Steal blocks from the deleted extent
5026 * if necessary. Stealing blocks simply fudges the
5027 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005028 */
5029 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5030 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c2016-03-15 11:42:47 +11005031 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5032 del->br_blockcount);
5033 da_new = temp + temp2 - stolen;
5034 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005035
5036 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11005037 * Set the reservation for each extent. Warn if either
5038 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005039 */
Brian Fosterd34999c2016-03-15 11:42:47 +11005040 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005041 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005042 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005043 }
5044 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5045 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5046 ++*idx;
5047 break;
5048 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005049
5050 /* remove reverse mapping */
5051 if (!delay) {
5052 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5053 if (error)
5054 goto done;
5055 }
5056
Dave Chinner9e5987a2013-02-25 12:31:26 +11005057 /*
5058 * If we need to, add to list of extents to delete.
5059 */
Darrick J. Wong62aab202016-10-03 09:11:23 -07005060 if (do_fx) {
5061 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5062 error = xfs_refcount_decrease_extent(mp, dfops, del);
5063 if (error)
5064 goto done;
5065 } else
5066 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5067 del->br_blockcount, NULL);
5068 }
5069
Dave Chinner9e5987a2013-02-25 12:31:26 +11005070 /*
5071 * Adjust inode # blocks in the file.
5072 */
5073 if (nblks)
5074 ip->i_d.di_nblocks -= nblks;
5075 /*
5076 * Adjust quota data.
5077 */
5078 if (qfield)
5079 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5080
5081 /*
5082 * Account for change in delayed indirect blocks.
5083 * Nothing to do for disk quota accounting here.
5084 */
5085 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005086 if (da_old > da_new)
5087 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005088done:
5089 *logflagsp = flags;
5090 return error;
5091}
5092
5093/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 * Unmap (remove) blocks from a file.
5095 * If nexts is nonzero then the number of extents to remove is limited to
5096 * that value. If not all extents in the block range can be removed then
5097 * *done is set.
5098 */
5099int /* error */
5100xfs_bunmapi(
5101 xfs_trans_t *tp, /* transaction pointer */
5102 struct xfs_inode *ip, /* incore inode */
5103 xfs_fileoff_t bno, /* starting offset to unmap */
5104 xfs_filblks_t len, /* length to unmap in file */
5105 int flags, /* misc flags */
5106 xfs_extnum_t nexts, /* number of extents max */
5107 xfs_fsblock_t *firstblock, /* first allocated block
5108 controls a.g. for allocs */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005109 struct xfs_defer_ops *dfops, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 int *done) /* set if not done yet */
5111{
5112 xfs_btree_cur_t *cur; /* bmap btree cursor */
5113 xfs_bmbt_irec_t del; /* extent being deleted */
5114 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005115 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 int error; /* error return value */
5117 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005118 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 xfs_ifork_t *ifp; /* inode fork pointer */
5120 int isrt; /* freeing in rt area */
5121 xfs_extnum_t lastx; /* last extent index used */
5122 int logflags; /* transaction logging flags */
5123 xfs_extlen_t mod; /* rt extent offset */
5124 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005125 xfs_extnum_t nextents; /* number of file extents */
5126 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 xfs_fileoff_t start; /* first file offset deleted */
5128 int tmp_logflags; /* partial logging flags */
5129 int wasdel; /* was a delayed alloc extent */
5130 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 xfs_fsblock_t sum;
5132
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005133 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5134
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5136 XFS_ATTR_FORK : XFS_DATA_FORK;
5137 ifp = XFS_IFORK_PTR(ip, whichfork);
5138 if (unlikely(
5139 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5140 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5141 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5142 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005143 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 }
5145 mp = ip->i_mount;
5146 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005147 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005148
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005149 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 ASSERT(len > 0);
5151 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005152
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5154 (error = xfs_iread_extents(tp, ip, whichfork)))
5155 return error;
5156 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5157 if (nextents == 0) {
5158 *done = 1;
5159 return 0;
5160 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005161 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005162 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163 start = bno;
5164 bno = start + len - 1;
5165 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5166 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005167
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168 /*
5169 * Check to see if the given block number is past the end of the
5170 * file, back up to the last block if so...
5171 */
5172 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005173 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 xfs_bmbt_get_all(ep, &got);
5175 bno = got.br_startoff + got.br_blockcount - 1;
5176 }
5177 logflags = 0;
5178 if (ifp->if_flags & XFS_IFBROOT) {
5179 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005180 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005182 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 cur->bc_private.b.flags = 0;
5184 } else
5185 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005186
5187 if (isrt) {
5188 /*
5189 * Synchronize by locking the bitmap inode.
5190 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005191 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005192 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005193 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5194 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005195 }
5196
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197 extno = 0;
5198 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5199 (nexts == 0 || extno < nexts)) {
5200 /*
5201 * Is the found extent after a hole in which bno lives?
5202 * Just back up to the previous extent, if so.
5203 */
5204 if (got.br_startoff > bno) {
5205 if (--lastx < 0)
5206 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005207 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 xfs_bmbt_get_all(ep, &got);
5209 }
5210 /*
5211 * Is the last block of this extent before the range
5212 * we're supposed to delete? If so, we're done.
5213 */
5214 bno = XFS_FILEOFF_MIN(bno,
5215 got.br_startoff + got.br_blockcount - 1);
5216 if (bno < start)
5217 break;
5218 /*
5219 * Then deal with the (possibly delayed) allocated space
5220 * we found.
5221 */
5222 ASSERT(ep != NULL);
5223 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005224 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 if (got.br_startoff < start) {
5226 del.br_startoff = start;
5227 del.br_blockcount -= start - got.br_startoff;
5228 if (!wasdel)
5229 del.br_startblock += start - got.br_startoff;
5230 }
5231 if (del.br_startoff + del.br_blockcount > bno + 1)
5232 del.br_blockcount = bno + 1 - del.br_startoff;
5233 sum = del.br_startblock + del.br_blockcount;
5234 if (isrt &&
5235 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5236 /*
5237 * Realtime extent not lined up at the end.
5238 * The extent could have been split into written
5239 * and unwritten pieces, or we could just be
5240 * unmapping part of it. But we can't really
5241 * get rid of part of a realtime extent.
5242 */
5243 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005244 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 /*
5246 * This piece is unwritten, or we're not
5247 * using unwritten extents. Skip over it.
5248 */
5249 ASSERT(bno >= mod);
5250 bno -= mod > del.br_blockcount ?
5251 del.br_blockcount : mod;
5252 if (bno < got.br_startoff) {
5253 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005254 xfs_bmbt_get_all(xfs_iext_get_ext(
5255 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256 }
5257 continue;
5258 }
5259 /*
5260 * It's written, turn it unwritten.
5261 * This is better than zeroing it.
5262 */
5263 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005264 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 /*
5266 * If this spans a realtime extent boundary,
5267 * chop it back to the start of the one we end at.
5268 */
5269 if (del.br_blockcount > mod) {
5270 del.br_startoff += del.br_blockcount - mod;
5271 del.br_startblock += del.br_blockcount - mod;
5272 del.br_blockcount = mod;
5273 }
5274 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005275 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005276 &lastx, &cur, &del, firstblock, dfops,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005277 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278 if (error)
5279 goto error0;
5280 goto nodelete;
5281 }
5282 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5283 /*
5284 * Realtime extent is lined up at the end but not
5285 * at the front. We'll get rid of full extents if
5286 * we can.
5287 */
5288 mod = mp->m_sb.sb_rextsize - mod;
5289 if (del.br_blockcount > mod) {
5290 del.br_blockcount -= mod;
5291 del.br_startoff += mod;
5292 del.br_startblock += mod;
5293 } else if ((del.br_startoff == start &&
5294 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005295 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005296 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297 /*
5298 * Can't make it unwritten. There isn't
5299 * a full extent here so just skip it.
5300 */
5301 ASSERT(bno >= del.br_blockcount);
5302 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005303 if (got.br_startoff > bno) {
5304 if (--lastx >= 0) {
5305 ep = xfs_iext_get_ext(ifp,
5306 lastx);
5307 xfs_bmbt_get_all(ep, &got);
5308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 }
5310 continue;
5311 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5312 /*
5313 * This one is already unwritten.
5314 * It must have a written left neighbor.
5315 * Unwrite the killed part of that one and
5316 * try again.
5317 */
5318 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005319 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5320 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005322 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 ASSERT(del.br_startblock ==
5324 prev.br_startblock + prev.br_blockcount);
5325 if (prev.br_startoff < start) {
5326 mod = start - prev.br_startoff;
5327 prev.br_blockcount -= mod;
5328 prev.br_startblock += mod;
5329 prev.br_startoff = start;
5330 }
5331 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005332 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005333 error = xfs_bmap_add_extent_unwritten_real(tp,
5334 ip, &lastx, &cur, &prev,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005335 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 if (error)
5337 goto error0;
5338 goto nodelete;
5339 } else {
5340 ASSERT(del.br_state == XFS_EXT_NORM);
5341 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005342 error = xfs_bmap_add_extent_unwritten_real(tp,
5343 ip, &lastx, &cur, &del,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005344 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 if (error)
5346 goto error0;
5347 goto nodelete;
5348 }
5349 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005350
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 /*
5352 * If it's the case where the directory code is running
5353 * with no block reservation, and the deleted block is in
5354 * the middle of its extent, and the resulting insert
5355 * of an extent would cause transformation to btree format,
5356 * then reject it. The calling code will then swap
5357 * blocks around instead.
5358 * We have to do this now, rather than waiting for the
5359 * conversion to btree format, since the transaction
5360 * will be dirty.
5361 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005362 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005364 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5365 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 del.br_startoff > got.br_startoff &&
5367 del.br_startoff + del.br_blockcount <
5368 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005369 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 goto error0;
5371 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005372
5373 /*
5374 * Unreserve quota and update realtime free space, if
5375 * appropriate. If delayed allocation, update the inode delalloc
5376 * counter now and wait to update the sb counters as
5377 * xfs_bmap_del_extent() might need to borrow some blocks.
5378 */
5379 if (wasdel) {
5380 ASSERT(startblockval(del.br_startblock) > 0);
5381 if (isrt) {
5382 xfs_filblks_t rtexts;
5383
5384 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5385 do_div(rtexts, mp->m_sb.sb_rextsize);
5386 xfs_mod_frextents(mp, (int64_t)rtexts);
5387 (void)xfs_trans_reserve_quota_nblks(NULL,
5388 ip, -((long)del.br_blockcount), 0,
5389 XFS_QMOPT_RES_RTBLKS);
5390 } else {
5391 (void)xfs_trans_reserve_quota_nblks(NULL,
5392 ip, -((long)del.br_blockcount), 0,
5393 XFS_QMOPT_RES_REGBLKS);
5394 }
5395 ip->i_delayed_blks -= del.br_blockcount;
5396 if (cur)
5397 cur->bc_private.b.flags |=
5398 XFS_BTCUR_BPRV_WASDEL;
5399 } else if (cur)
5400 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5401
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005402 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005403 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 logflags |= tmp_logflags;
5405 if (error)
5406 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005407
5408 if (!isrt && wasdel)
5409 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 bno = del.br_startoff - 1;
5412nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 /*
5414 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005417 if (lastx >= 0) {
5418 ep = xfs_iext_get_ext(ifp, lastx);
5419 if (xfs_bmbt_get_startoff(ep) > bno) {
5420 if (--lastx >= 0)
5421 ep = xfs_iext_get_ext(ifp,
5422 lastx);
5423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 extno++;
5427 }
5428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005430
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 /*
5432 * Convert to a btree if necessary.
5433 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005434 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005436 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 &cur, 0, &tmp_logflags, whichfork);
5438 logflags |= tmp_logflags;
5439 if (error)
5440 goto error0;
5441 }
5442 /*
5443 * transform from btree to extents, give it cur
5444 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005445 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 ASSERT(cur != NULL);
5447 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5448 whichfork);
5449 logflags |= tmp_logflags;
5450 if (error)
5451 goto error0;
5452 }
5453 /*
5454 * transform from extents to local?
5455 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 error = 0;
5457error0:
5458 /*
5459 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005460 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005462 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005464 logflags &= ~xfs_ilog_fext(whichfork);
5465 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005467 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 /*
5469 * Log inode even in the error case, if the transaction
5470 * is dirty we'll need to shut down the filesystem.
5471 */
5472 if (logflags)
5473 xfs_trans_log_inode(tp, ip, logflags);
5474 if (cur) {
5475 if (!error) {
5476 *firstblock = cur->bc_private.b.firstblock;
5477 cur->bc_private.b.allocated = 0;
5478 }
5479 xfs_btree_del_cursor(cur,
5480 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5481 }
5482 return error;
5483}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005484
5485/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005486 * Determine whether an extent shift can be accomplished by a merge with the
5487 * extent that precedes the target hole of the shift.
5488 */
5489STATIC bool
5490xfs_bmse_can_merge(
5491 struct xfs_bmbt_irec *left, /* preceding extent */
5492 struct xfs_bmbt_irec *got, /* current extent to shift */
5493 xfs_fileoff_t shift) /* shift fsb */
5494{
5495 xfs_fileoff_t startoff;
5496
5497 startoff = got->br_startoff - shift;
5498
5499 /*
5500 * The extent, once shifted, must be adjacent in-file and on-disk with
5501 * the preceding extent.
5502 */
5503 if ((left->br_startoff + left->br_blockcount != startoff) ||
5504 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5505 (left->br_state != got->br_state) ||
5506 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5507 return false;
5508
5509 return true;
5510}
5511
5512/*
5513 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5514 * hole in the file. If an extent shift would result in the extent being fully
5515 * adjacent to the extent that currently precedes the hole, we can merge with
5516 * the preceding extent rather than do the shift.
5517 *
5518 * This function assumes the caller has verified a shift-by-merge is possible
5519 * with the provided extents via xfs_bmse_can_merge().
5520 */
5521STATIC int
5522xfs_bmse_merge(
5523 struct xfs_inode *ip,
5524 int whichfork,
5525 xfs_fileoff_t shift, /* shift fsb */
5526 int current_ext, /* idx of gotp */
5527 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5528 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5529 struct xfs_btree_cur *cur,
5530 int *logflags) /* output */
5531{
Brian Fosterddb19e32014-09-23 15:38:09 +10005532 struct xfs_bmbt_irec got;
5533 struct xfs_bmbt_irec left;
5534 xfs_filblks_t blockcount;
5535 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005536 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005537
Brian Fosterddb19e32014-09-23 15:38:09 +10005538 xfs_bmbt_get_all(gotp, &got);
5539 xfs_bmbt_get_all(leftp, &left);
5540 blockcount = left.br_blockcount + got.br_blockcount;
5541
5542 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5543 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5544 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5545
5546 /*
5547 * Merge the in-core extents. Note that the host record pointers and
5548 * current_ext index are invalid once the extent has been removed via
5549 * xfs_iext_remove().
5550 */
5551 xfs_bmbt_set_blockcount(leftp, blockcount);
5552 xfs_iext_remove(ip, current_ext, 1, 0);
5553
5554 /*
5555 * Update the on-disk extent count, the btree if necessary and log the
5556 * inode.
5557 */
5558 XFS_IFORK_NEXT_SET(ip, whichfork,
5559 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5560 *logflags |= XFS_ILOG_CORE;
5561 if (!cur) {
5562 *logflags |= XFS_ILOG_DEXT;
5563 return 0;
5564 }
5565
5566 /* lookup and remove the extent to merge */
5567 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5568 got.br_blockcount, &i);
5569 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005570 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005571 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005572
5573 error = xfs_btree_delete(cur, &i);
5574 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005575 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005576 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005577
5578 /* lookup and update size of the previous extent */
5579 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5580 left.br_blockcount, &i);
5581 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005582 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005583 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005584
5585 left.br_blockcount = blockcount;
5586
Dave Chinner4db431f2014-12-04 09:42:40 +11005587 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5588 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005589}
5590
5591/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005592 * Shift a single extent.
5593 */
5594STATIC int
5595xfs_bmse_shift_one(
5596 struct xfs_inode *ip,
5597 int whichfork,
5598 xfs_fileoff_t offset_shift_fsb,
5599 int *current_ext,
5600 struct xfs_bmbt_rec_host *gotp,
5601 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005602 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005603 enum shift_direction direction,
5604 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005605{
5606 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005607 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005608 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005609 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005610 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005611 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005612 int error;
5613 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005614 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005615
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005616 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005617 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005618 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Brian Fostera979bdf2014-09-23 15:39:04 +10005619
5620 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005621
Brian Fosterf71721d2014-09-23 15:39:05 +10005622 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005623 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005624
Namjae Jeona904b1c2015-03-25 15:08:56 +11005625 if (direction == SHIFT_LEFT) {
5626 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005627
Namjae Jeona904b1c2015-03-25 15:08:56 +11005628 /*
5629 * Check for merge if we've got an extent to the left,
5630 * otherwise make sure there's enough room at the start
5631 * of the file for the shift.
5632 */
5633 if (!*current_ext) {
5634 if (got.br_startoff < offset_shift_fsb)
5635 return -EINVAL;
5636 goto update_current_ext;
5637 }
5638 /*
5639 * grab the left extent and check for a large
5640 * enough hole.
5641 */
5642 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5643 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5644
5645 if (startoff <
5646 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005647 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005648
Dave Chinnerb11bd672014-12-04 09:42:24 +11005649 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005650 if (xfs_bmse_can_merge(&adj_irec, &got,
5651 offset_shift_fsb)) {
Darrick J. Wong9c194642016-08-03 12:16:05 +10005652 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5653 *current_ext, gotp, adj_irecp,
5654 cur, logflags);
5655 if (error)
5656 return error;
5657 adj_irec = got;
5658 goto update_rmap;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005659 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005660 } else {
5661 startoff = got.br_startoff + offset_shift_fsb;
5662 /* nothing to move if this is the last extent */
5663 if (*current_ext >= (total_extents - 1))
5664 goto update_current_ext;
5665 /*
5666 * If this is not the last extent in the file, make sure there
5667 * is enough room between current extent and next extent for
5668 * accommodating the shift.
5669 */
5670 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5671 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5672 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5673 return -EINVAL;
5674 /*
5675 * Unlike a left shift (which involves a hole punch),
5676 * a right shift does not modify extent neighbors
5677 * in any way. We should never find mergeable extents
5678 * in this scenario. Check anyways and warn if we
5679 * encounter two extents that could be one.
5680 */
5681 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5682 WARN_ON_ONCE(1);
5683 }
Brian Fostera979bdf2014-09-23 15:39:04 +10005684 /*
5685 * Increment the extent index for the next iteration, update the start
5686 * offset of the in-core extent and update the btree if applicable.
5687 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005688update_current_ext:
5689 if (direction == SHIFT_LEFT)
5690 (*current_ext)++;
5691 else
5692 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005693 xfs_bmbt_set_startoff(gotp, startoff);
5694 *logflags |= XFS_ILOG_CORE;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005695 adj_irec = got;
Brian Fostera979bdf2014-09-23 15:39:04 +10005696 if (!cur) {
5697 *logflags |= XFS_ILOG_DEXT;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005698 goto update_rmap;
Brian Fostera979bdf2014-09-23 15:39:04 +10005699 }
5700
5701 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5702 got.br_blockcount, &i);
5703 if (error)
5704 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005705 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10005706
5707 got.br_startoff = startoff;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005708 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5709 got.br_blockcount, got.br_state);
5710 if (error)
5711 return error;
5712
5713update_rmap:
5714 /* update reverse mapping */
5715 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
5716 if (error)
5717 return error;
5718 adj_irec.br_startoff = startoff;
5719 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
Brian Fostera979bdf2014-09-23 15:39:04 +10005720}
5721
5722/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005723 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005724 *
Brian Foster2c845f52014-09-23 15:37:09 +10005725 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005726 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10005727 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5728 * is the length by which each extent is shifted. If there is no hole to shift
5729 * the extents into, this will be considered invalid operation and we abort
5730 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005731 */
5732int
5733xfs_bmap_shift_extents(
5734 struct xfs_trans *tp,
5735 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005736 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005737 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005738 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005739 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005740 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005741 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005742 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005743 int num_exts)
5744{
Brian Fosterca446d82014-09-02 12:12:53 +10005745 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005746 struct xfs_bmbt_rec_host *gotp;
5747 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005748 struct xfs_mount *mp = ip->i_mount;
5749 struct xfs_ifork *ifp;
5750 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005751 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005752 xfs_extnum_t total_extents;
5753 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005754 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005755 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10005756 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005757
5758 if (unlikely(XFS_TEST_ERROR(
5759 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5760 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5761 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5762 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5763 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005764 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005765 }
5766
5767 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005768 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005769
Brian Foster2c845f52014-09-23 15:37:09 +10005770 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5771 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005772 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
5773 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005774
5775 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005776 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5777 /* Read in all the extents */
5778 error = xfs_iread_extents(tp, ip, whichfork);
5779 if (error)
5780 return error;
5781 }
5782
Brian Fosterddb19e32014-09-23 15:38:09 +10005783 if (ifp->if_flags & XFS_IFBROOT) {
5784 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5785 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005786 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005787 cur->bc_private.b.flags = 0;
5788 }
5789
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005790 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005791 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10005792 * are collapsing out, so we cannot use the count of real extents here.
5793 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005794 */
5795 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005796 if (total_extents == 0) {
5797 *done = 1;
5798 goto del_cursor;
5799 }
5800
5801 /*
5802 * In case of first right shift, we need to initialize next_fsb
5803 */
5804 if (*next_fsb == NULLFSBLOCK) {
5805 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
5806 xfs_bmbt_get_all(gotp, &got);
5807 *next_fsb = got.br_startoff;
5808 if (stop_fsb > *next_fsb) {
5809 *done = 1;
5810 goto del_cursor;
5811 }
5812 }
5813
5814 /* Lookup the extent index at which we have to stop */
5815 if (direction == SHIFT_RIGHT) {
5816 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
5817 /* Make stop_extent exclusive of shift range */
5818 stop_extent--;
5819 } else
5820 stop_extent = total_extents;
5821
5822 /*
5823 * Look up the extent index for the fsb where we start shifting. We can
5824 * henceforth iterate with current_ext as extent list changes are locked
5825 * out via ilock.
5826 *
5827 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5828 * *next_fsb lies in a hole beyond which there are no extents. Either
5829 * way, we are done.
5830 */
5831 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
5832 if (!gotp) {
5833 *done = 1;
5834 goto del_cursor;
5835 }
5836
5837 /* some sanity checking before we finally start shifting extents */
5838 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
5839 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
5840 error = -EIO;
5841 goto del_cursor;
5842 }
5843
5844 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005845 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005846 &current_ext, gotp, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005847 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10005848 if (error)
5849 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005850 /*
5851 * If there was an extent merge during the shift, the extent
5852 * count can change. Update the total and grade the next record.
5853 */
5854 if (direction == SHIFT_LEFT) {
5855 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5856 stop_extent = total_extents;
5857 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005858
Namjae Jeona904b1c2015-03-25 15:08:56 +11005859 if (current_ext == stop_extent) {
5860 *done = 1;
5861 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10005862 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005863 }
Brian Fosterddb19e32014-09-23 15:38:09 +10005864 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005865 }
5866
Namjae Jeona904b1c2015-03-25 15:08:56 +11005867 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005868 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10005869 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10005870 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005871
5872del_cursor:
5873 if (cur)
5874 xfs_btree_del_cursor(cur,
5875 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5876
Brian Fosterca446d82014-09-02 12:12:53 +10005877 if (logflags)
5878 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10005879
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005880 return error;
5881}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005882
5883/*
5884 * Splits an extent into two extents at split_fsb block such that it is
5885 * the first block of the current_ext. @current_ext is a target extent
5886 * to be split. @split_fsb is a block where the extents is split.
5887 * If split_fsb lies in a hole or the first block of extents, just return 0.
5888 */
5889STATIC int
5890xfs_bmap_split_extent_at(
5891 struct xfs_trans *tp,
5892 struct xfs_inode *ip,
5893 xfs_fileoff_t split_fsb,
5894 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005895 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005896{
5897 int whichfork = XFS_DATA_FORK;
5898 struct xfs_btree_cur *cur = NULL;
5899 struct xfs_bmbt_rec_host *gotp;
5900 struct xfs_bmbt_irec got;
5901 struct xfs_bmbt_irec new; /* split extent */
5902 struct xfs_mount *mp = ip->i_mount;
5903 struct xfs_ifork *ifp;
5904 xfs_fsblock_t gotblkcnt; /* new block count for got */
5905 xfs_extnum_t current_ext;
5906 int error = 0;
5907 int logflags = 0;
5908 int i = 0;
5909
5910 if (unlikely(XFS_TEST_ERROR(
5911 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5912 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5913 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5914 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5915 XFS_ERRLEVEL_LOW, mp);
5916 return -EFSCORRUPTED;
5917 }
5918
5919 if (XFS_FORCED_SHUTDOWN(mp))
5920 return -EIO;
5921
5922 ifp = XFS_IFORK_PTR(ip, whichfork);
5923 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5924 /* Read in all the extents */
5925 error = xfs_iread_extents(tp, ip, whichfork);
5926 if (error)
5927 return error;
5928 }
5929
5930 /*
5931 * gotp can be null in 2 cases: 1) if there are no extents
5932 * or 2) split_fsb lies in a hole beyond which there are
5933 * no extents. Either way, we are done.
5934 */
5935 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
5936 if (!gotp)
5937 return 0;
5938
5939 xfs_bmbt_get_all(gotp, &got);
5940
5941 /*
5942 * Check split_fsb lies in a hole or the start boundary offset
5943 * of the extent.
5944 */
5945 if (got.br_startoff >= split_fsb)
5946 return 0;
5947
5948 gotblkcnt = split_fsb - got.br_startoff;
5949 new.br_startoff = split_fsb;
5950 new.br_startblock = got.br_startblock + gotblkcnt;
5951 new.br_blockcount = got.br_blockcount - gotblkcnt;
5952 new.br_state = got.br_state;
5953
5954 if (ifp->if_flags & XFS_IFBROOT) {
5955 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5956 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005957 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005958 cur->bc_private.b.flags = 0;
5959 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5960 got.br_startblock,
5961 got.br_blockcount,
5962 &i);
5963 if (error)
5964 goto del_cursor;
5965 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5966 }
5967
5968 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
5969 got.br_blockcount = gotblkcnt;
5970
5971 logflags = XFS_ILOG_CORE;
5972 if (cur) {
5973 error = xfs_bmbt_update(cur, got.br_startoff,
5974 got.br_startblock,
5975 got.br_blockcount,
5976 got.br_state);
5977 if (error)
5978 goto del_cursor;
5979 } else
5980 logflags |= XFS_ILOG_DEXT;
5981
5982 /* Add new extent */
5983 current_ext++;
5984 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5985 XFS_IFORK_NEXT_SET(ip, whichfork,
5986 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5987
5988 if (cur) {
5989 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
5990 new.br_startblock, new.br_blockcount,
5991 &i);
5992 if (error)
5993 goto del_cursor;
5994 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
5995 cur->bc_rec.b.br_state = new.br_state;
5996
5997 error = xfs_btree_insert(cur, &i);
5998 if (error)
5999 goto del_cursor;
6000 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6001 }
6002
6003 /*
6004 * Convert to a btree if necessary.
6005 */
6006 if (xfs_bmap_needs_btree(ip, whichfork)) {
6007 int tmp_logflags; /* partial log flag return val */
6008
6009 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006010 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006011 &cur, 0, &tmp_logflags, whichfork);
6012 logflags |= tmp_logflags;
6013 }
6014
6015del_cursor:
6016 if (cur) {
6017 cur->bc_private.b.allocated = 0;
6018 xfs_btree_del_cursor(cur,
6019 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6020 }
6021
6022 if (logflags)
6023 xfs_trans_log_inode(tp, ip, logflags);
6024 return error;
6025}
6026
6027int
6028xfs_bmap_split_extent(
6029 struct xfs_inode *ip,
6030 xfs_fileoff_t split_fsb)
6031{
6032 struct xfs_mount *mp = ip->i_mount;
6033 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006034 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006035 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006036 int error;
6037
Christoph Hellwig253f4912016-04-06 09:19:55 +10006038 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6039 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6040 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006041 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006042
6043 xfs_ilock(ip, XFS_ILOCK_EXCL);
6044 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6045
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006046 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006047
6048 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006049 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006050 if (error)
6051 goto out;
6052
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006053 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006054 if (error)
6055 goto out;
6056
Christoph Hellwig70393312015-06-04 13:48:08 +10006057 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006058
6059out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006060 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006061 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006062 return error;
6063}