blob: 042d7bf9fb60460998afbb8f207e01130d1a9d11 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053kmem_zone_t *xfs_bmap_free_item_zone;
54
55/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110056 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 */
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110060 * Compute and fill in the value of the maximum depth of a bmap btree
61 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110063void
64xfs_bmap_compute_maxlevels(
65 xfs_mount_t *mp, /* file system mount structure */
66 int whichfork) /* data or attr fork */
67{
68 int level; /* btree level */
69 uint maxblocks; /* max blocks at this level */
70 uint maxleafents; /* max leaf entries possible */
71 int maxrootrecs; /* max records in root block */
72 int minleafrecs; /* min records in leaf block */
73 int minnoderecs; /* min records in node block */
74 int sz; /* root block size */
75
76 /*
77 * The maximum number of extents in a file, hence the maximum
78 * number of leaf entries, is controlled by the type of di_nextents
79 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
80 * (a signed 16-bit number, xfs_aextnum_t).
81 *
82 * Note that we can no longer assume that if we are in ATTR1 that
83 * the fork offset of all the inodes will be
84 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
85 * with ATTR2 and then mounted back with ATTR1, keeping the
86 * di_forkoff's fixed but probably at various positions. Therefore,
87 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
88 * of a minimum size available.
89 */
90 if (whichfork == XFS_DATA_FORK) {
91 maxleafents = MAXEXTNUM;
92 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
93 } else {
94 maxleafents = MAXAEXTNUM;
95 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
96 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100097 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +110098 minleafrecs = mp->m_bmap_dmnr[0];
99 minnoderecs = mp->m_bmap_dmnr[1];
100 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
101 for (level = 1; maxblocks > 1; level++) {
102 if (maxblocks <= maxrootrecs)
103 maxblocks = 1;
104 else
105 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
106 }
107 mp->m_bm_maxlevels[whichfork] = level;
108}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100110STATIC int /* error */
111xfs_bmbt_lookup_eq(
112 struct xfs_btree_cur *cur,
113 xfs_fileoff_t off,
114 xfs_fsblock_t bno,
115 xfs_filblks_t len,
116 int *stat) /* success/failure */
117{
118 cur->bc_rec.b.br_startoff = off;
119 cur->bc_rec.b.br_startblock = bno;
120 cur->bc_rec.b.br_blockcount = len;
121 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
122}
123
124STATIC int /* error */
125xfs_bmbt_lookup_ge(
126 struct xfs_btree_cur *cur,
127 xfs_fileoff_t off,
128 xfs_fsblock_t bno,
129 xfs_filblks_t len,
130 int *stat) /* success/failure */
131{
132 cur->bc_rec.b.br_startoff = off;
133 cur->bc_rec.b.br_startblock = bno;
134 cur->bc_rec.b.br_blockcount = len;
135 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
136}
137
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100138/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000139 * Check if the inode needs to be converted to btree format.
140 */
141static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
142{
143 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
144 XFS_IFORK_NEXTENTS(ip, whichfork) >
145 XFS_IFORK_MAXEXT(ip, whichfork);
146}
147
148/*
149 * Check if the inode should be converted to extent format.
150 */
151static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
152{
153 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
154 XFS_IFORK_NEXTENTS(ip, whichfork) <=
155 XFS_IFORK_MAXEXT(ip, whichfork);
156}
157
158/*
159 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100160 * by [off, bno, len, state].
161 * This either works (return 0) or gets an EFSCORRUPTED error.
162 */
163STATIC int
164xfs_bmbt_update(
165 struct xfs_btree_cur *cur,
166 xfs_fileoff_t off,
167 xfs_fsblock_t bno,
168 xfs_filblks_t len,
169 xfs_exntst_t state)
170{
171 union xfs_btree_rec rec;
172
173 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
174 return xfs_btree_update(cur, &rec);
175}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100178 * Compute the worst-case number of indirect blocks that will be used
179 * for ip's delayed extent of length "len".
180 */
181STATIC xfs_filblks_t
182xfs_bmap_worst_indlen(
183 xfs_inode_t *ip, /* incore inode pointer */
184 xfs_filblks_t len) /* delayed extent length */
185{
186 int level; /* btree level number */
187 int maxrecs; /* maximum record count at this level */
188 xfs_mount_t *mp; /* mount structure */
189 xfs_filblks_t rval; /* return value */
190
191 mp = ip->i_mount;
192 maxrecs = mp->m_bmap_dmxr[0];
193 for (level = 0, rval = 0;
194 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
195 level++) {
196 len += maxrecs - 1;
197 do_div(len, maxrecs);
198 rval += len;
199 if (len == 1)
200 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
201 level - 1;
202 if (level == 0)
203 maxrecs = mp->m_bmap_dmxr[1];
204 }
205 return rval;
206}
207
208/*
209 * Calculate the default attribute fork offset for newly created inodes.
210 */
211uint
212xfs_default_attroffset(
213 struct xfs_inode *ip)
214{
215 struct xfs_mount *mp = ip->i_mount;
216 uint offset;
217
218 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100219 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100220 XFS_BMDR_SPACE_CALC(MINABTPTRS);
221 } else {
222 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
223 }
224
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100225 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100226 return offset;
227}
228
229/*
230 * Helper routine to reset inode di_forkoff field when switching
231 * attribute fork from local to extent format - we reset it where
232 * possible to make space available for inline data fork extents.
233 */
234STATIC void
235xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100236 xfs_inode_t *ip,
237 int whichfork)
238{
239 if (whichfork == XFS_ATTR_FORK &&
240 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
241 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
242 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
243 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
244
245 if (dfl_forkoff > ip->i_d.di_forkoff)
246 ip->i_d.di_forkoff = dfl_forkoff;
247 }
248}
249
Dave Chinner9e5987a2013-02-25 12:31:26 +1100250#ifdef DEBUG
251STATIC struct xfs_buf *
252xfs_bmap_get_bp(
253 struct xfs_btree_cur *cur,
254 xfs_fsblock_t bno)
255{
256 struct xfs_log_item_desc *lidp;
257 int i;
258
259 if (!cur)
260 return NULL;
261
262 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
263 if (!cur->bc_bufs[i])
264 break;
265 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
266 return cur->bc_bufs[i];
267 }
268
269 /* Chase down all the log items to see if the bp is there */
270 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
271 struct xfs_buf_log_item *bip;
272 bip = (struct xfs_buf_log_item *)lidp->lid_item;
273 if (bip->bli_item.li_type == XFS_LI_BUF &&
274 XFS_BUF_ADDR(bip->bli_buf) == bno)
275 return bip->bli_buf;
276 }
277
278 return NULL;
279}
280
281STATIC void
282xfs_check_block(
283 struct xfs_btree_block *block,
284 xfs_mount_t *mp,
285 int root,
286 short sz)
287{
288 int i, j, dmxr;
289 __be64 *pp, *thispa; /* pointer to block address */
290 xfs_bmbt_key_t *prevp, *keyp;
291
292 ASSERT(be16_to_cpu(block->bb_level) > 0);
293
294 prevp = NULL;
295 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
296 dmxr = mp->m_bmap_dmxr[0];
297 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
298
299 if (prevp) {
300 ASSERT(be64_to_cpu(prevp->br_startoff) <
301 be64_to_cpu(keyp->br_startoff));
302 }
303 prevp = keyp;
304
305 /*
306 * Compare the block numbers to see if there are dups.
307 */
308 if (root)
309 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
310 else
311 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
312
313 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
314 if (root)
315 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
316 else
317 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
318 if (*thispa == *pp) {
319 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
320 __func__, j, i,
321 (unsigned long long)be64_to_cpu(*thispa));
322 panic("%s: ptrs are equal in node\n",
323 __func__);
324 }
325 }
326 }
327}
328
329/*
330 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100331 * btree leaves. THis becomes prohibitively expensive for large extent count
332 * files, so don't bother with inodes that have more than 10,000 extents in
333 * them. The btree record ordering checks will still be done, so for such large
334 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100335 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100336STATIC void
337xfs_bmap_check_leaf_extents(
338 xfs_btree_cur_t *cur, /* btree cursor or null */
339 xfs_inode_t *ip, /* incore inode pointer */
340 int whichfork) /* data or attr fork */
341{
342 struct xfs_btree_block *block; /* current btree block */
343 xfs_fsblock_t bno; /* block # of "block" */
344 xfs_buf_t *bp; /* buffer for "block" */
345 int error; /* error return value */
346 xfs_extnum_t i=0, j; /* index into the extents list */
347 xfs_ifork_t *ifp; /* fork structure */
348 int level; /* btree level, for checking */
349 xfs_mount_t *mp; /* file system mount structure */
350 __be64 *pp; /* pointer to block address */
351 xfs_bmbt_rec_t *ep; /* pointer to current extent */
352 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
353 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
354 int bp_release = 0;
355
356 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
357 return;
358 }
359
Dave Chinnere3543812016-01-08 11:28:49 +1100360 /* skip large extent count inodes */
361 if (ip->i_d.di_nextents > 10000)
362 return;
363
Dave Chinner9e5987a2013-02-25 12:31:26 +1100364 bno = NULLFSBLOCK;
365 mp = ip->i_mount;
366 ifp = XFS_IFORK_PTR(ip, whichfork);
367 block = ifp->if_broot;
368 /*
369 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
370 */
371 level = be16_to_cpu(block->bb_level);
372 ASSERT(level > 0);
373 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
374 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
375 bno = be64_to_cpu(*pp);
376
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000377 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100378 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
379 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
380
381 /*
382 * Go down the tree until leaf level is reached, following the first
383 * pointer (leftmost) at each level.
384 */
385 while (level-- > 0) {
386 /* See if buf is in cur first */
387 bp_release = 0;
388 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
389 if (!bp) {
390 bp_release = 1;
391 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
392 XFS_BMAP_BTREE_REF,
393 &xfs_bmbt_buf_ops);
394 if (error)
395 goto error_norelse;
396 }
397 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100398 if (level == 0)
399 break;
400
401 /*
402 * Check this block for basic sanity (increasing keys and
403 * no duplicate blocks).
404 */
405
406 xfs_check_block(block, mp, 0, 0);
407 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
408 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100409 XFS_WANT_CORRUPTED_GOTO(mp,
410 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100411 if (bp_release) {
412 bp_release = 0;
413 xfs_trans_brelse(NULL, bp);
414 }
415 }
416
417 /*
418 * Here with bp and block set to the leftmost leaf node in the tree.
419 */
420 i = 0;
421
422 /*
423 * Loop over all leaf nodes checking that all extents are in the right order.
424 */
425 for (;;) {
426 xfs_fsblock_t nextbno;
427 xfs_extnum_t num_recs;
428
429
430 num_recs = xfs_btree_get_numrecs(block);
431
432 /*
433 * Read-ahead the next leaf block, if any.
434 */
435
436 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
437
438 /*
439 * Check all the extents to make sure they are OK.
440 * If we had a previous block, the last entry should
441 * conform with the first entry in this one.
442 */
443
444 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
445 if (i) {
446 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
447 xfs_bmbt_disk_get_blockcount(&last) <=
448 xfs_bmbt_disk_get_startoff(ep));
449 }
450 for (j = 1; j < num_recs; j++) {
451 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
452 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
453 xfs_bmbt_disk_get_blockcount(ep) <=
454 xfs_bmbt_disk_get_startoff(nextp));
455 ep = nextp;
456 }
457
458 last = *ep;
459 i += num_recs;
460 if (bp_release) {
461 bp_release = 0;
462 xfs_trans_brelse(NULL, bp);
463 }
464 bno = nextbno;
465 /*
466 * If we've reached the end, stop.
467 */
468 if (bno == NULLFSBLOCK)
469 break;
470
471 bp_release = 0;
472 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
473 if (!bp) {
474 bp_release = 1;
475 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
476 XFS_BMAP_BTREE_REF,
477 &xfs_bmbt_buf_ops);
478 if (error)
479 goto error_norelse;
480 }
481 block = XFS_BUF_TO_BLOCK(bp);
482 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100483
Dave Chinner9e5987a2013-02-25 12:31:26 +1100484 return;
485
486error0:
487 xfs_warn(mp, "%s: at error0", __func__);
488 if (bp_release)
489 xfs_trans_brelse(NULL, bp);
490error_norelse:
491 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
492 __func__, i);
493 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
494 return;
495}
496
497/*
498 * Add bmap trace insert entries for all the contents of the extent records.
499 */
500void
501xfs_bmap_trace_exlist(
502 xfs_inode_t *ip, /* incore inode pointer */
503 xfs_extnum_t cnt, /* count of entries in the list */
504 int whichfork, /* data or attr fork */
505 unsigned long caller_ip)
506{
507 xfs_extnum_t idx; /* extent record index */
508 xfs_ifork_t *ifp; /* inode fork pointer */
509 int state = 0;
510
511 if (whichfork == XFS_ATTR_FORK)
512 state |= BMAP_ATTRFORK;
513
514 ifp = XFS_IFORK_PTR(ip, whichfork);
515 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
516 for (idx = 0; idx < cnt; idx++)
517 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
518}
519
520/*
521 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000522 * given the caller's original parameters. Specifically check the
523 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100524 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
525 */
526STATIC void
527xfs_bmap_validate_ret(
528 xfs_fileoff_t bno,
529 xfs_filblks_t len,
530 int flags,
531 xfs_bmbt_irec_t *mval,
532 int nmap,
533 int ret_nmap)
534{
535 int i; /* index to map values */
536
537 ASSERT(ret_nmap <= nmap);
538
539 for (i = 0; i < ret_nmap; i++) {
540 ASSERT(mval[i].br_blockcount > 0);
541 if (!(flags & XFS_BMAPI_ENTIRE)) {
542 ASSERT(mval[i].br_startoff >= bno);
543 ASSERT(mval[i].br_blockcount <= len);
544 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
545 bno + len);
546 } else {
547 ASSERT(mval[i].br_startoff < bno + len);
548 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
549 bno);
550 }
551 ASSERT(i == 0 ||
552 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
553 mval[i].br_startoff);
554 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
555 mval[i].br_startblock != HOLESTARTBLOCK);
556 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
557 mval[i].br_state == XFS_EXT_UNWRITTEN);
558 }
559}
560
561#else
562#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
563#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
564#endif /* DEBUG */
565
566/*
567 * bmap free list manipulation functions
568 */
569
570/*
571 * Add the extent to the list of extents to be free at transaction end.
572 * The list is maintained sorted (by block number).
573 */
574void
575xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000576 struct xfs_mount *mp,
577 struct xfs_defer_ops *dfops,
578 xfs_fsblock_t bno,
579 xfs_filblks_t len,
580 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100581{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000582 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100583#ifdef DEBUG
584 xfs_agnumber_t agno;
585 xfs_agblock_t agbno;
586
587 ASSERT(bno != NULLFSBLOCK);
588 ASSERT(len > 0);
589 ASSERT(len <= MAXEXTLEN);
590 ASSERT(!isnullstartblock(bno));
591 agno = XFS_FSB_TO_AGNO(mp, bno);
592 agbno = XFS_FSB_TO_AGBNO(mp, bno);
593 ASSERT(agno < mp->m_sb.sb_agcount);
594 ASSERT(agbno < mp->m_sb.sb_agblocks);
595 ASSERT(len < mp->m_sb.sb_agblocks);
596 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
597#endif
598 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000599
Dave Chinner9e5987a2013-02-25 12:31:26 +1100600 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000601 new->xefi_startblock = bno;
602 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000603 if (oinfo)
604 new->xefi_oinfo = *oinfo;
605 else
606 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000607 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
608 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000609 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100610}
611
612/*
613 * Inode fork format manipulation functions
614 */
615
616/*
617 * Transform a btree format file with only one leaf node, where the
618 * extents list will fit in the inode, into an extents format file.
619 * Since the file extents are already in-core, all we have to do is
620 * give up the space for the btree root and pitch the leaf block.
621 */
622STATIC int /* error */
623xfs_bmap_btree_to_extents(
624 xfs_trans_t *tp, /* transaction pointer */
625 xfs_inode_t *ip, /* incore inode pointer */
626 xfs_btree_cur_t *cur, /* btree cursor */
627 int *logflagsp, /* inode logging flags */
628 int whichfork) /* data or attr fork */
629{
630 /* REFERENCED */
631 struct xfs_btree_block *cblock;/* child btree block */
632 xfs_fsblock_t cbno; /* child block number */
633 xfs_buf_t *cbp; /* child block's buffer */
634 int error; /* error return value */
635 xfs_ifork_t *ifp; /* inode fork data */
636 xfs_mount_t *mp; /* mount point structure */
637 __be64 *pp; /* ptr to block address */
638 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000639 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100640
641 mp = ip->i_mount;
642 ifp = XFS_IFORK_PTR(ip, whichfork);
643 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
644 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
645 rblock = ifp->if_broot;
646 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
647 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
648 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
649 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
650 cbno = be64_to_cpu(*pp);
651 *logflagsp = 0;
652#ifdef DEBUG
653 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
654 return error;
655#endif
656 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
657 &xfs_bmbt_buf_ops);
658 if (error)
659 return error;
660 cblock = XFS_BUF_TO_BLOCK(cbp);
661 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
662 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000663 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
664 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100665 ip->i_d.di_nblocks--;
666 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
667 xfs_trans_binval(tp, cbp);
668 if (cur->bc_bufs[0] == cbp)
669 cur->bc_bufs[0] = NULL;
670 xfs_iroot_realloc(ip, -1, whichfork);
671 ASSERT(ifp->if_broot == NULL);
672 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
673 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
674 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
675 return 0;
676}
677
678/*
679 * Convert an extents-format file into a btree-format file.
680 * The new file will have a root block (in the inode) and a single child block.
681 */
682STATIC int /* error */
683xfs_bmap_extents_to_btree(
684 xfs_trans_t *tp, /* transaction pointer */
685 xfs_inode_t *ip, /* incore inode pointer */
686 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000687 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100688 xfs_btree_cur_t **curp, /* cursor returned to caller */
689 int wasdel, /* converting a delayed alloc */
690 int *logflagsp, /* inode logging flags */
691 int whichfork) /* data or attr fork */
692{
693 struct xfs_btree_block *ablock; /* allocated (child) bt block */
694 xfs_buf_t *abp; /* buffer for ablock */
695 xfs_alloc_arg_t args; /* allocation arguments */
696 xfs_bmbt_rec_t *arp; /* child record pointer */
697 struct xfs_btree_block *block; /* btree root block */
698 xfs_btree_cur_t *cur; /* bmap btree cursor */
699 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
700 int error; /* error return value */
701 xfs_extnum_t i, cnt; /* extent record index */
702 xfs_ifork_t *ifp; /* inode fork pointer */
703 xfs_bmbt_key_t *kp; /* root block key pointer */
704 xfs_mount_t *mp; /* mount structure */
705 xfs_extnum_t nextents; /* number of file extents */
706 xfs_bmbt_ptr_t *pp; /* root block address pointer */
707
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500708 mp = ip->i_mount;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100709 ifp = XFS_IFORK_PTR(ip, whichfork);
710 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
711
712 /*
713 * Make space in the inode incore.
714 */
715 xfs_iroot_realloc(ip, 1, whichfork);
716 ifp->if_flags |= XFS_IFBROOT;
717
718 /*
719 * Fill in the root.
720 */
721 block = ifp->if_broot;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500722 if (xfs_sb_version_hascrc(&mp->m_sb))
723 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
724 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
725 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
726 else
727 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
728 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
729 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100730
731 /*
732 * Need a cursor. Can't allocate until bb_level is filled in.
733 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100734 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
735 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000736 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100737 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
738 /*
739 * Convert to a btree with two levels, one record in root.
740 */
741 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
742 memset(&args, 0, sizeof(args));
743 args.tp = tp;
744 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000745 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100746 args.firstblock = *firstblock;
747 if (*firstblock == NULLFSBLOCK) {
748 args.type = XFS_ALLOCTYPE_START_BNO;
749 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000750 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100751 args.type = XFS_ALLOCTYPE_START_BNO;
752 args.fsbno = *firstblock;
753 } else {
754 args.type = XFS_ALLOCTYPE_NEAR_BNO;
755 args.fsbno = *firstblock;
756 }
757 args.minlen = args.maxlen = args.prod = 1;
758 args.wasdel = wasdel;
759 *logflagsp = 0;
760 if ((error = xfs_alloc_vextent(&args))) {
761 xfs_iroot_realloc(ip, -1, whichfork);
762 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
763 return error;
764 }
765 /*
766 * Allocation can't fail, the space was reserved.
767 */
768 ASSERT(args.fsbno != NULLFSBLOCK);
769 ASSERT(*firstblock == NULLFSBLOCK ||
770 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000771 (dfops->dop_low &&
Dave Chinner9e5987a2013-02-25 12:31:26 +1100772 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
773 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
774 cur->bc_private.b.allocated++;
775 ip->i_d.di_nblocks++;
776 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
777 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
778 /*
779 * Fill in the child block.
780 */
781 abp->b_ops = &xfs_bmbt_buf_ops;
782 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500783 if (xfs_sb_version_hascrc(&mp->m_sb))
784 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
785 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
786 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
787 else
788 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
789 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
790 XFS_BTREE_LONG_PTRS);
791
Dave Chinner9e5987a2013-02-25 12:31:26 +1100792 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
793 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
794 for (cnt = i = 0; i < nextents; i++) {
795 ep = xfs_iext_get_ext(ifp, i);
796 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
797 arp->l0 = cpu_to_be64(ep->l0);
798 arp->l1 = cpu_to_be64(ep->l1);
799 arp++; cnt++;
800 }
801 }
802 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
803 xfs_btree_set_numrecs(ablock, cnt);
804
805 /*
806 * Fill in the root key and pointer.
807 */
808 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
809 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
810 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
811 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
812 be16_to_cpu(block->bb_level)));
813 *pp = cpu_to_be64(args.fsbno);
814
815 /*
816 * Do all this logging at the end so that
817 * the root is at the right level.
818 */
819 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
820 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
821 ASSERT(*curp == NULL);
822 *curp = cur;
823 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
824 return 0;
825}
826
827/*
828 * Convert a local file to an extents file.
829 * This code is out of bounds for data forks of regular files,
830 * since the file data needs to get logged so things will stay consistent.
831 * (The bmap-level manipulations are ok, though).
832 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000833void
834xfs_bmap_local_to_extents_empty(
835 struct xfs_inode *ip,
836 int whichfork)
837{
838 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
839
840 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
841 ASSERT(ifp->if_bytes == 0);
842 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
843
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000844 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000845 ifp->if_flags &= ~XFS_IFINLINE;
846 ifp->if_flags |= XFS_IFEXTENTS;
847 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
848}
849
850
Dave Chinner9e5987a2013-02-25 12:31:26 +1100851STATIC int /* error */
852xfs_bmap_local_to_extents(
853 xfs_trans_t *tp, /* transaction pointer */
854 xfs_inode_t *ip, /* incore inode pointer */
855 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
856 xfs_extlen_t total, /* total blocks needed by transaction */
857 int *logflagsp, /* inode logging flags */
858 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500859 void (*init_fn)(struct xfs_trans *tp,
860 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100861 struct xfs_inode *ip,
862 struct xfs_ifork *ifp))
863{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000864 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100865 int flags; /* logging flags returned */
866 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000867 xfs_alloc_arg_t args; /* allocation arguments */
868 xfs_buf_t *bp; /* buffer for extent block */
869 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100870
871 /*
872 * We don't want to deal with the case of keeping inode data inline yet.
873 * So sending the data fork of a regular inode is invalid.
874 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100875 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100876 ifp = XFS_IFORK_PTR(ip, whichfork);
877 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000878
879 if (!ifp->if_bytes) {
880 xfs_bmap_local_to_extents_empty(ip, whichfork);
881 flags = XFS_ILOG_CORE;
882 goto done;
883 }
884
Dave Chinner9e5987a2013-02-25 12:31:26 +1100885 flags = 0;
886 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000887 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
888 XFS_IFINLINE);
889 memset(&args, 0, sizeof(args));
890 args.tp = tp;
891 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000892 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000893 args.firstblock = *firstblock;
894 /*
895 * Allocate a block. We know we need only one, since the
896 * file currently fits in an inode.
897 */
898 if (*firstblock == NULLFSBLOCK) {
899 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
900 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100901 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000902 args.fsbno = *firstblock;
903 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100904 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000905 args.total = total;
906 args.minlen = args.maxlen = args.prod = 1;
907 error = xfs_alloc_vextent(&args);
908 if (error)
909 goto done;
910
911 /* Can't fail, the space was reserved. */
912 ASSERT(args.fsbno != NULLFSBLOCK);
913 ASSERT(args.len == 1);
914 *firstblock = args.fsbno;
915 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
916
Dave Chinnerfe22d552015-01-22 09:30:06 +1100917 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100918 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100919 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100920 * The callout is responsible for logging because the remote format
921 * might differ from the local format and thus we don't know how much to
922 * log here. Note that init_fn must also set the buffer log item type
923 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100924 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000925 init_fn(tp, bp, ip, ifp);
926
Brian Fosterb7cdc662015-10-12 15:40:24 +1100927 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000928 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
929 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100930 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000931
932 xfs_iext_add(ifp, 0, 1);
933 ep = xfs_iext_get_ext(ifp, 0);
934 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
935 trace_xfs_bmap_post_update(ip, 0,
936 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
937 _THIS_IP_);
938 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
939 ip->i_d.di_nblocks = 1;
940 xfs_trans_mod_dquot_byino(tp, ip,
941 XFS_TRANS_DQ_BCOUNT, 1L);
942 flags |= xfs_ilog_fext(whichfork);
943
Dave Chinner9e5987a2013-02-25 12:31:26 +1100944done:
945 *logflagsp = flags;
946 return error;
947}
948
949/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * Called from xfs_bmap_add_attrfork to handle btree format files.
951 */
952STATIC int /* error */
953xfs_bmap_add_attrfork_btree(
954 xfs_trans_t *tp, /* transaction pointer */
955 xfs_inode_t *ip, /* incore inode pointer */
956 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000957 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 int *flags) /* inode logging flags */
959{
960 xfs_btree_cur_t *cur; /* btree cursor */
961 int error; /* error return value */
962 xfs_mount_t *mp; /* file system mount struct */
963 int stat; /* newroot status */
964
965 mp = ip->i_mount;
966 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
967 *flags |= XFS_ILOG_DBROOT;
968 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100969 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000970 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 cur->bc_private.b.firstblock = *firstblock;
972 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
973 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000974 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100975 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100976 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 goto error0;
978 if (stat == 0) {
979 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000980 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982 *firstblock = cur->bc_private.b.firstblock;
983 cur->bc_private.b.allocated = 0;
984 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
985 }
986 return 0;
987error0:
988 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
989 return error;
990}
991
992/*
993 * Called from xfs_bmap_add_attrfork to handle extents format files.
994 */
995STATIC int /* error */
996xfs_bmap_add_attrfork_extents(
997 xfs_trans_t *tp, /* transaction pointer */
998 xfs_inode_t *ip, /* incore inode pointer */
999 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001000 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 int *flags) /* inode logging flags */
1002{
1003 xfs_btree_cur_t *cur; /* bmap btree cursor */
1004 int error; /* error return value */
1005
1006 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1007 return 0;
1008 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001009 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 flags, XFS_DATA_FORK);
1011 if (cur) {
1012 cur->bc_private.b.allocated = 0;
1013 xfs_btree_del_cursor(cur,
1014 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1015 }
1016 return error;
1017}
1018
1019/*
Dave Chinner1e823792013-02-11 15:58:13 +11001020 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1021 * different data fork content type needs a different callout to do the
1022 * conversion. Some are basic and only require special block initialisation
1023 * callouts for the data formating, others (directories) are so specialised they
1024 * handle everything themselves.
1025 *
1026 * XXX (dgc): investigate whether directory conversion can use the generic
1027 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001028 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
1030STATIC int /* error */
1031xfs_bmap_add_attrfork_local(
1032 xfs_trans_t *tp, /* transaction pointer */
1033 xfs_inode_t *ip, /* incore inode pointer */
1034 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001035 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 int *flags) /* inode logging flags */
1037{
1038 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1041 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001042
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001043 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001045 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 dargs.dp = ip;
1047 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001048 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001049 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 dargs.whichfork = XFS_DATA_FORK;
1051 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001052 return xfs_dir2_sf_to_block(&dargs);
1053 }
1054
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001055 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001056 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1057 flags, XFS_DATA_FORK,
1058 xfs_symlink_local_to_remote);
1059
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001060 /* should only be called for types that support local format data */
1061 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001062 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001066 * Convert inode from non-attributed to attributed.
1067 * Must not be in a transaction, ip must not be locked.
1068 */
1069int /* error code */
1070xfs_bmap_add_attrfork(
1071 xfs_inode_t *ip, /* incore inode pointer */
1072 int size, /* space new attribute needs */
1073 int rsvd) /* xact may use reserved blks */
1074{
1075 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001076 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001077 xfs_mount_t *mp; /* mount structure */
1078 xfs_trans_t *tp; /* transaction pointer */
1079 int blks; /* space reservation */
1080 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001081 int logflags; /* logging flags */
1082 int error; /* error return value */
1083
1084 ASSERT(XFS_IFORK_Q(ip) == 0);
1085
1086 mp = ip->i_mount;
1087 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001088
Dave Chinner9e5987a2013-02-25 12:31:26 +11001089 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001090
1091 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1092 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1093 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001094 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001095
Dave Chinner9e5987a2013-02-25 12:31:26 +11001096 xfs_ilock(ip, XFS_ILOCK_EXCL);
1097 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1098 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1099 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001100 if (error)
1101 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001102 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001103 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001104 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1105 /*
1106 * For inodes coming from pre-6.2 filesystems.
1107 */
1108 ASSERT(ip->i_d.di_aformat == 0);
1109 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1110 }
1111 ASSERT(ip->i_d.di_anextents == 0);
1112
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001113 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001114 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1115
1116 switch (ip->i_d.di_format) {
1117 case XFS_DINODE_FMT_DEV:
1118 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1119 break;
1120 case XFS_DINODE_FMT_UUID:
1121 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1122 break;
1123 case XFS_DINODE_FMT_LOCAL:
1124 case XFS_DINODE_FMT_EXTENTS:
1125 case XFS_DINODE_FMT_BTREE:
1126 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1127 if (!ip->i_d.di_forkoff)
1128 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1129 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1130 version = 2;
1131 break;
1132 default:
1133 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001134 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001135 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001136 }
1137
1138 ASSERT(ip->i_afp == NULL);
1139 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1140 ip->i_afp->if_flags = XFS_IFEXTENTS;
1141 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001142 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001143 switch (ip->i_d.di_format) {
1144 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001145 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001146 &logflags);
1147 break;
1148 case XFS_DINODE_FMT_EXTENTS:
1149 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001150 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001151 break;
1152 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001153 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001154 &logflags);
1155 break;
1156 default:
1157 error = 0;
1158 break;
1159 }
1160 if (logflags)
1161 xfs_trans_log_inode(tp, ip, logflags);
1162 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001163 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001164 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1165 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001166 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001167
1168 spin_lock(&mp->m_sb_lock);
1169 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1170 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001171 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001172 }
1173 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1174 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001175 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001176 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001177 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001178 if (log_sb)
1179 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001180 }
1181
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001182 error = xfs_defer_finish(&tp, &dfops, NULL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001183 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001184 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001185 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001186 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001187 return error;
1188
1189bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001190 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001191trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001192 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001193 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001194 return error;
1195}
1196
1197/*
1198 * Internal and external extent tree search functions.
1199 */
1200
1201/*
1202 * Read in the extents to if_extents.
1203 * All inode fields are set up by caller, we just traverse the btree
1204 * and copy the records in. If the file system cannot contain unwritten
1205 * extents, the records are checked for no "state" flags.
1206 */
1207int /* error */
1208xfs_bmap_read_extents(
1209 xfs_trans_t *tp, /* transaction pointer */
1210 xfs_inode_t *ip, /* incore inode */
1211 int whichfork) /* data or attr fork */
1212{
1213 struct xfs_btree_block *block; /* current btree block */
1214 xfs_fsblock_t bno; /* block # of "block" */
1215 xfs_buf_t *bp; /* buffer for "block" */
1216 int error; /* error return value */
1217 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1218 xfs_extnum_t i, j; /* index into the extents list */
1219 xfs_ifork_t *ifp; /* fork structure */
1220 int level; /* btree level, for checking */
1221 xfs_mount_t *mp; /* file system mount structure */
1222 __be64 *pp; /* pointer to block address */
1223 /* REFERENCED */
1224 xfs_extnum_t room; /* number of entries there's room for */
1225
1226 bno = NULLFSBLOCK;
1227 mp = ip->i_mount;
1228 ifp = XFS_IFORK_PTR(ip, whichfork);
1229 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1230 XFS_EXTFMT_INODE(ip);
1231 block = ifp->if_broot;
1232 /*
1233 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1234 */
1235 level = be16_to_cpu(block->bb_level);
1236 ASSERT(level > 0);
1237 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1238 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001239 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001240 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1241 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1242 /*
1243 * Go down the tree until leaf level is reached, following the first
1244 * pointer (leftmost) at each level.
1245 */
1246 while (level-- > 0) {
1247 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1248 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1249 if (error)
1250 return error;
1251 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001252 if (level == 0)
1253 break;
1254 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1255 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001256 XFS_WANT_CORRUPTED_GOTO(mp,
1257 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001258 xfs_trans_brelse(tp, bp);
1259 }
1260 /*
1261 * Here with bp and block set to the leftmost leaf node in the tree.
1262 */
1263 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1264 i = 0;
1265 /*
1266 * Loop over all leaf nodes. Copy information to the extent records.
1267 */
1268 for (;;) {
1269 xfs_bmbt_rec_t *frp;
1270 xfs_fsblock_t nextbno;
1271 xfs_extnum_t num_recs;
1272 xfs_extnum_t start;
1273
1274 num_recs = xfs_btree_get_numrecs(block);
1275 if (unlikely(i + num_recs > room)) {
1276 ASSERT(i + num_recs <= room);
1277 xfs_warn(ip->i_mount,
1278 "corrupt dinode %Lu, (btree extents).",
1279 (unsigned long long) ip->i_ino);
1280 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1281 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1282 goto error0;
1283 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001284 /*
1285 * Read-ahead the next leaf block, if any.
1286 */
1287 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1288 if (nextbno != NULLFSBLOCK)
1289 xfs_btree_reada_bufl(mp, nextbno, 1,
1290 &xfs_bmbt_buf_ops);
1291 /*
1292 * Copy records into the extent records.
1293 */
1294 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1295 start = i;
1296 for (j = 0; j < num_recs; j++, i++, frp++) {
1297 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1298 trp->l0 = be64_to_cpu(frp->l0);
1299 trp->l1 = be64_to_cpu(frp->l1);
1300 }
1301 if (exntf == XFS_EXTFMT_NOSTATE) {
1302 /*
1303 * Check all attribute bmap btree records and
1304 * any "older" data bmap btree records for a
1305 * set bit in the "extent flag" position.
1306 */
1307 if (unlikely(xfs_check_nostate_extents(ifp,
1308 start, num_recs))) {
1309 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1310 XFS_ERRLEVEL_LOW,
1311 ip->i_mount);
1312 goto error0;
1313 }
1314 }
1315 xfs_trans_brelse(tp, bp);
1316 bno = nextbno;
1317 /*
1318 * If we've reached the end, stop.
1319 */
1320 if (bno == NULLFSBLOCK)
1321 break;
1322 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1323 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1324 if (error)
1325 return error;
1326 block = XFS_BUF_TO_BLOCK(bp);
1327 }
1328 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1329 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1330 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1331 return 0;
1332error0:
1333 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001334 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001335}
1336
1337
1338/*
1339 * Search the extent records for the entry containing block bno.
1340 * If bno lies in a hole, point to the next entry. If bno lies
1341 * past eof, *eofp will be set, and *prevp will contain the last
1342 * entry (null if none). Else, *lastxp will be set to the index
1343 * of the found entry; *gotp will contain the entry.
1344 */
1345STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1346xfs_bmap_search_multi_extents(
1347 xfs_ifork_t *ifp, /* inode fork pointer */
1348 xfs_fileoff_t bno, /* block number searched for */
1349 int *eofp, /* out: end of file found */
1350 xfs_extnum_t *lastxp, /* out: last extent index */
1351 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1352 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1353{
1354 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1355 xfs_extnum_t lastx; /* last extent index */
1356
1357 /*
1358 * Initialize the extent entry structure to catch access to
1359 * uninitialized br_startblock field.
1360 */
1361 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1362 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1363 gotp->br_state = XFS_EXT_INVALID;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001364 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001365 prevp->br_startoff = NULLFILEOFF;
1366
1367 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1368 if (lastx > 0) {
1369 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1370 }
1371 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1372 xfs_bmbt_get_all(ep, gotp);
1373 *eofp = 0;
1374 } else {
1375 if (lastx > 0) {
1376 *gotp = *prevp;
1377 }
1378 *eofp = 1;
1379 ep = NULL;
1380 }
1381 *lastxp = lastx;
1382 return ep;
1383}
1384
1385/*
1386 * Search the extents list for the inode, for the extent containing bno.
1387 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1388 * *eofp will be set, and *prevp will contain the last entry (null if none).
1389 * Else, *lastxp will be set to the index of the found
1390 * entry; *gotp will contain the entry.
1391 */
1392STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1393xfs_bmap_search_extents(
1394 xfs_inode_t *ip, /* incore inode pointer */
1395 xfs_fileoff_t bno, /* block number searched for */
1396 int fork, /* data or attr fork */
1397 int *eofp, /* out: end of file found */
1398 xfs_extnum_t *lastxp, /* out: last extent index */
1399 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1400 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1401{
1402 xfs_ifork_t *ifp; /* inode fork pointer */
1403 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1404
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001405 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001406 ifp = XFS_IFORK_PTR(ip, fork);
1407
1408 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1409
1410 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1411 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1412 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1413 "Access to block zero in inode %llu "
1414 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -05001415 "blkcnt: %llx extent-state: %x lastx: %x",
Dave Chinner9e5987a2013-02-25 12:31:26 +11001416 (unsigned long long)ip->i_ino,
1417 (unsigned long long)gotp->br_startblock,
1418 (unsigned long long)gotp->br_startoff,
1419 (unsigned long long)gotp->br_blockcount,
1420 gotp->br_state, *lastxp);
1421 *lastxp = NULLEXTNUM;
1422 *eofp = 1;
1423 return NULL;
1424 }
1425 return ep;
1426}
1427
1428/*
1429 * Returns the file-relative block number of the first unused block(s)
1430 * in the file with at least "len" logically contiguous blocks free.
1431 * This is the lowest-address hole if the file has holes, else the first block
1432 * past the end of file.
1433 * Return 0 if the file is currently local (in-inode).
1434 */
1435int /* error */
1436xfs_bmap_first_unused(
1437 xfs_trans_t *tp, /* transaction pointer */
1438 xfs_inode_t *ip, /* incore inode */
1439 xfs_extlen_t len, /* size of hole to find */
1440 xfs_fileoff_t *first_unused, /* unused block */
1441 int whichfork) /* data or attr fork */
1442{
1443 int error; /* error return value */
1444 int idx; /* extent record index */
1445 xfs_ifork_t *ifp; /* inode fork pointer */
1446 xfs_fileoff_t lastaddr; /* last block number seen */
1447 xfs_fileoff_t lowest; /* lowest useful block */
1448 xfs_fileoff_t max; /* starting useful block */
1449 xfs_fileoff_t off; /* offset for this block */
1450 xfs_extnum_t nextents; /* number of extent entries */
1451
1452 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1453 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1454 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1455 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1456 *first_unused = 0;
1457 return 0;
1458 }
1459 ifp = XFS_IFORK_PTR(ip, whichfork);
1460 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1461 (error = xfs_iread_extents(tp, ip, whichfork)))
1462 return error;
1463 lowest = *first_unused;
1464 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1465 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1466 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1467 off = xfs_bmbt_get_startoff(ep);
1468 /*
1469 * See if the hole before this extent will work.
1470 */
1471 if (off >= lowest + len && off - max >= len) {
1472 *first_unused = max;
1473 return 0;
1474 }
1475 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1476 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1477 }
1478 *first_unused = max;
1479 return 0;
1480}
1481
1482/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001483 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001484 * last_block (input value) in the file.
1485 * This is not based on i_size, it is based on the extent records.
1486 * Returns 0 for local files, as they do not have extent records.
1487 */
1488int /* error */
1489xfs_bmap_last_before(
1490 xfs_trans_t *tp, /* transaction pointer */
1491 xfs_inode_t *ip, /* incore inode */
1492 xfs_fileoff_t *last_block, /* last block */
1493 int whichfork) /* data or attr fork */
1494{
1495 xfs_fileoff_t bno; /* input file offset */
1496 int eof; /* hit end of file */
1497 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1498 int error; /* error return value */
1499 xfs_bmbt_irec_t got; /* current extent value */
1500 xfs_ifork_t *ifp; /* inode fork pointer */
1501 xfs_extnum_t lastx; /* last extent used */
1502 xfs_bmbt_irec_t prev; /* previous extent value */
1503
1504 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1505 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1506 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +10001507 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001508 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1509 *last_block = 0;
1510 return 0;
1511 }
1512 ifp = XFS_IFORK_PTR(ip, whichfork);
1513 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1514 (error = xfs_iread_extents(tp, ip, whichfork)))
1515 return error;
1516 bno = *last_block - 1;
1517 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1518 &prev);
1519 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1520 if (prev.br_startoff == NULLFILEOFF)
1521 *last_block = 0;
1522 else
1523 *last_block = prev.br_startoff + prev.br_blockcount;
1524 }
1525 /*
1526 * Otherwise *last_block is already the right answer.
1527 */
1528 return 0;
1529}
1530
Dave Chinner68988112013-08-12 20:49:42 +10001531int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001532xfs_bmap_last_extent(
1533 struct xfs_trans *tp,
1534 struct xfs_inode *ip,
1535 int whichfork,
1536 struct xfs_bmbt_irec *rec,
1537 int *is_empty)
1538{
1539 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1540 int error;
1541 int nextents;
1542
1543 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1544 error = xfs_iread_extents(tp, ip, whichfork);
1545 if (error)
1546 return error;
1547 }
1548
1549 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1550 if (nextents == 0) {
1551 *is_empty = 1;
1552 return 0;
1553 }
1554
1555 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1556 *is_empty = 0;
1557 return 0;
1558}
1559
1560/*
1561 * Check the last inode extent to determine whether this allocation will result
1562 * in blocks being allocated at the end of the file. When we allocate new data
1563 * blocks at the end of the file which do not start at the previous data block,
1564 * we will try to align the new blocks at stripe unit boundaries.
1565 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001566 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001567 * at, or past the EOF.
1568 */
1569STATIC int
1570xfs_bmap_isaeof(
1571 struct xfs_bmalloca *bma,
1572 int whichfork)
1573{
1574 struct xfs_bmbt_irec rec;
1575 int is_empty;
1576 int error;
1577
1578 bma->aeof = 0;
1579 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1580 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001581 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001582 return error;
1583
Dave Chinner6e708bc2013-11-22 10:41:16 +11001584 if (is_empty) {
1585 bma->aeof = 1;
1586 return 0;
1587 }
1588
Dave Chinner9e5987a2013-02-25 12:31:26 +11001589 /*
1590 * Check if we are allocation or past the last extent, or at least into
1591 * the last delayed allocated extent.
1592 */
1593 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1594 (bma->offset >= rec.br_startoff &&
1595 isnullstartblock(rec.br_startblock));
1596 return 0;
1597}
1598
1599/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001600 * Returns the file-relative block number of the first block past eof in
1601 * the file. This is not based on i_size, it is based on the extent records.
1602 * Returns 0 for local files, as they do not have extent records.
1603 */
1604int
1605xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001606 struct xfs_inode *ip,
1607 xfs_fileoff_t *last_block,
1608 int whichfork)
1609{
1610 struct xfs_bmbt_irec rec;
1611 int is_empty;
1612 int error;
1613
1614 *last_block = 0;
1615
1616 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1617 return 0;
1618
1619 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1620 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001621 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001622
1623 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1624 if (error || is_empty)
1625 return error;
1626
1627 *last_block = rec.br_startoff + rec.br_blockcount;
1628 return 0;
1629}
1630
1631/*
1632 * Returns whether the selected fork of the inode has exactly one
1633 * block or not. For the data fork we check this matches di_size,
1634 * implying the file's range is 0..bsize-1.
1635 */
1636int /* 1=>1 block, 0=>otherwise */
1637xfs_bmap_one_block(
1638 xfs_inode_t *ip, /* incore inode */
1639 int whichfork) /* data or attr fork */
1640{
1641 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1642 xfs_ifork_t *ifp; /* inode fork pointer */
1643 int rval; /* return value */
1644 xfs_bmbt_irec_t s; /* internal version of extent */
1645
1646#ifndef DEBUG
1647 if (whichfork == XFS_DATA_FORK)
1648 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1649#endif /* !DEBUG */
1650 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1651 return 0;
1652 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1653 return 0;
1654 ifp = XFS_IFORK_PTR(ip, whichfork);
1655 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1656 ep = xfs_iext_get_ext(ifp, 0);
1657 xfs_bmbt_get_all(ep, &s);
1658 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1659 if (rval && whichfork == XFS_DATA_FORK)
1660 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1661 return rval;
1662}
1663
1664/*
1665 * Extent tree manipulation functions used during allocation.
1666 */
1667
1668/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001669 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 */
1671STATIC int /* error */
1672xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001673 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001675 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001677 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001680 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 xfs_fileoff_t new_endoff; /* end offset of new entry */
1682 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1683 /* left is 0, right is 1, prev is 2 */
1684 int rval=0; /* return value (logging flags) */
1685 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001686 xfs_filblks_t da_new; /* new count del alloc blocks used */
1687 xfs_filblks_t da_old; /* old count del alloc blocks used */
1688 xfs_filblks_t temp=0; /* value for da_new calculations */
1689 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 int tmp_rval; /* partial logging flags */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001691 int whichfork = XFS_DATA_FORK;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001692 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001694 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001695 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001696
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001697 ASSERT(bma->idx >= 0);
1698 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001699 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001700 ASSERT(!bma->cur ||
1701 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001702
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001703 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705#define LEFT r[0]
1706#define RIGHT r[1]
1707#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 /*
1710 * Set up a bunch of variables to make the tests simpler.
1711 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001712 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 xfs_bmbt_get_all(ep, &PREV);
1714 new_endoff = new->br_startoff + new->br_blockcount;
1715 ASSERT(PREV.br_startoff <= new->br_startoff);
1716 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001717
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001718 da_old = startblockval(PREV.br_startblock);
1719 da_new = 0;
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /*
1722 * Set flags determining what part of the previous delayed allocation
1723 * extent is being replaced by a real allocation.
1724 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001725 if (PREV.br_startoff == new->br_startoff)
1726 state |= BMAP_LEFT_FILLING;
1727 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1728 state |= BMAP_RIGHT_FILLING;
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 /*
1731 * Check and set flags if this segment has a left neighbor.
1732 * Don't set contiguous if the combined extent would be too large.
1733 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001734 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001735 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001736 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001737
1738 if (isnullstartblock(LEFT.br_startblock))
1739 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001741
1742 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1743 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1744 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1745 LEFT.br_state == new->br_state &&
1746 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1747 state |= BMAP_LEFT_CONTIG;
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 /*
1750 * Check and set flags if this segment has a right neighbor.
1751 * Don't set contiguous if the combined extent would be too large.
1752 * Also check for all-three-contiguous being too large.
1753 */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001754 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001755 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001756 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001757
1758 if (isnullstartblock(RIGHT.br_startblock))
1759 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001761
1762 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1763 new_endoff == RIGHT.br_startoff &&
1764 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1765 new->br_state == RIGHT.br_state &&
1766 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1767 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1768 BMAP_RIGHT_FILLING)) !=
1769 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1770 BMAP_RIGHT_FILLING) ||
1771 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1772 <= MAXEXTLEN))
1773 state |= BMAP_RIGHT_CONTIG;
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 error = 0;
1776 /*
1777 * Switch out based on the FILLING and CONTIG state bits.
1778 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001779 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1780 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1781 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1782 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /*
1784 * Filling in all of a previously delayed allocation extent.
1785 * The left and right neighbors are both contiguous with new.
1786 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001787 bma->idx--;
1788 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1789 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 LEFT.br_blockcount + PREV.br_blockcount +
1791 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001792 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001793
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001794 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1795 bma->ip->i_d.di_nextents--;
1796 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1798 else {
1799 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001800 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001802 RIGHT.br_blockcount, &i);
1803 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001805 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001806 error = xfs_btree_delete(bma->cur, &i);
1807 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001809 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001810 error = xfs_btree_decrement(bma->cur, 0, &i);
1811 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001813 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001814 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 LEFT.br_startblock,
1816 LEFT.br_blockcount +
1817 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001818 RIGHT.br_blockcount, LEFT.br_state);
1819 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 goto done;
1821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 break;
1823
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001824 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 /*
1826 * Filling in all of a previously delayed allocation extent.
1827 * The left neighbor is contiguous, the right is not.
1828 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001829 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001830
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001831 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1832 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001833 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001834 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001835
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001836 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1837 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 rval = XFS_ILOG_DEXT;
1839 else {
1840 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001841 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001843 &i);
1844 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001846 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 LEFT.br_startblock,
1849 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001850 PREV.br_blockcount, LEFT.br_state);
1851 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto done;
1853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 break;
1855
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001856 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /*
1858 * Filling in all of a previously delayed allocation extent.
1859 * The right neighbor is contiguous, the left is not.
1860 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001861 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 xfs_bmbt_set_startblock(ep, new->br_startblock);
1863 xfs_bmbt_set_blockcount(ep,
1864 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001865 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001866
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001867 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1868 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 rval = XFS_ILOG_DEXT;
1870 else {
1871 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001872 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001874 RIGHT.br_blockcount, &i);
1875 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001877 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001878 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 new->br_startblock,
1880 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001881 RIGHT.br_blockcount, PREV.br_state);
1882 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 goto done;
1884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 break;
1886
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001887 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /*
1889 * Filling in all of a previously delayed allocation extent.
1890 * Neither the left nor right neighbors are contiguous with
1891 * the new one.
1892 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001893 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001895 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001896
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001897 bma->ip->i_d.di_nextents++;
1898 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1900 else {
1901 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001902 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001904 &i);
1905 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001907 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001908 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1909 error = xfs_btree_insert(bma->cur, &i);
1910 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001912 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 break;
1915
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001916 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /*
1918 * Filling in the first part of a previous delayed allocation.
1919 * The left neighbor is contiguous.
1920 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001921 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1922 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 LEFT.br_blockcount + new->br_blockcount);
1924 xfs_bmbt_set_startoff(ep,
1925 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001926 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001927
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001929 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001931 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 rval = XFS_ILOG_DEXT;
1933 else {
1934 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001935 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001937 &i);
1938 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001940 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001941 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 LEFT.br_startblock,
1943 LEFT.br_blockcount +
1944 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001945 LEFT.br_state);
1946 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 goto done;
1948 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001949 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001950 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001951 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001952 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001953
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001954 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 break;
1956
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001957 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 /*
1959 * Filling in the first part of a previous delayed allocation.
1960 * The left neighbor is not contiguous.
1961 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001962 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 xfs_bmbt_set_startoff(ep, new_endoff);
1964 temp = PREV.br_blockcount - new->br_blockcount;
1965 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001966 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
1967 bma->ip->i_d.di_nextents++;
1968 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1970 else {
1971 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001972 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001974 &i);
1975 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001977 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001978 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1979 error = xfs_btree_insert(bma->cur, &i);
1980 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001982 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001984
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001985 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001986 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001987 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001988 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 rval |= tmp_rval;
1990 if (error)
1991 goto done;
1992 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001993 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001994 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001995 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1996 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001997 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001998 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 break;
2000
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002001 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 /*
2003 * Filling in the last part of a previous delayed allocation.
2004 * The right neighbor is contiguous with the new allocation.
2005 */
2006 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002007 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002009 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002010 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 new->br_blockcount + RIGHT.br_blockcount,
2012 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002013 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2014 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 rval = XFS_ILOG_DEXT;
2016 else {
2017 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002018 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002020 RIGHT.br_blockcount, &i);
2021 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002023 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002024 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 new->br_startblock,
2026 new->br_blockcount +
2027 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002028 RIGHT.br_state);
2029 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 goto done;
2031 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002032
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002033 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002034 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002035 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002036 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002037 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002038
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002039 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 break;
2041
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002042 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 /*
2044 * Filling in the last part of a previous delayed allocation.
2045 * The right neighbor is not contiguous.
2046 */
2047 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002048 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002050 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2051 bma->ip->i_d.di_nextents++;
2052 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2054 else {
2055 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002056 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002058 &i);
2059 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002061 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002062 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2063 error = xfs_btree_insert(bma->cur, &i);
2064 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002066 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002068
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002069 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002070 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002071 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002072 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 rval |= tmp_rval;
2074 if (error)
2075 goto done;
2076 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002077 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002078 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002079 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2080 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002081 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002082 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002083
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002084 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 break;
2086
2087 case 0:
2088 /*
2089 * Filling in the middle part of a previous delayed allocation.
2090 * Contiguity is impossible here.
2091 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002092 *
2093 * We start with a delayed allocation:
2094 *
2095 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2096 * PREV @ idx
2097 *
2098 * and we are allocating:
2099 * +rrrrrrrrrrrrrrrrr+
2100 * new
2101 *
2102 * and we set it up for insertion as:
2103 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2104 * new
2105 * PREV @ idx LEFT RIGHT
2106 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 */
2108 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002110 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002111 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2112 LEFT = *new;
2113 RIGHT.br_state = PREV.br_state;
2114 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002115 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002116 RIGHT.br_startoff = new_endoff;
2117 RIGHT.br_blockcount = temp2;
2118 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002119 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2120 bma->ip->i_d.di_nextents++;
2121 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2123 else {
2124 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002125 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002127 &i);
2128 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002130 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002131 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2132 error = xfs_btree_insert(bma->cur, &i);
2133 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002135 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002137
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002138 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002139 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002140 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002141 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 rval |= tmp_rval;
2143 if (error)
2144 goto done;
2145 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002146 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2147 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002148 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002149 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002150 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002151 error = xfs_mod_fdblocks(bma->ip->i_mount,
2152 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002153 ASSERT(!error);
2154 if (error)
2155 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002157
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002158 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002159 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002160 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2161 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2162 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002163 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002164 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002165
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002166 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002167 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 break;
2169
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002170 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2171 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2172 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2173 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2174 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2175 case BMAP_LEFT_CONTIG:
2176 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 /*
2178 * These cases are all impossible.
2179 */
2180 ASSERT(0);
2181 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002182
Darrick J. Wong9c194642016-08-03 12:16:05 +10002183 /* add reverse mapping */
2184 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2185 if (error)
2186 goto done;
2187
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002188 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002189 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002190 int tmp_logflags; /* partial log flag return val */
2191
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002192 ASSERT(bma->cur == NULL);
2193 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002194 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002195 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002196 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002197 if (error)
2198 goto done;
2199 }
2200
2201 /* adjust for changes in reserved delayed indirect blocks */
2202 if (da_old || da_new) {
2203 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002204 if (bma->cur)
2205 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002206 ASSERT(temp <= da_old);
2207 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002208 xfs_mod_fdblocks(bma->ip->i_mount,
2209 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002210 }
2211
2212 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002213 if (bma->cur)
2214 bma->cur->bc_private.b.allocated = 0;
2215
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002216 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002218 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return error;
2220#undef LEFT
2221#undef RIGHT
2222#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223}
2224
2225/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002226 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 */
2228STATIC int /* error */
2229xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002230 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002232 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002234 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002235 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002236 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002237 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002240 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002243 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 xfs_fileoff_t new_endoff; /* end offset of new entry */
2245 xfs_exntst_t newext; /* new extent state */
2246 xfs_exntst_t oldext; /* old extent state */
2247 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2248 /* left is 0, right is 1, prev is 2 */
2249 int rval=0; /* return value (logging flags) */
2250 int state = 0;/* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002251 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002253 *logflagsp = 0;
2254
2255 cur = *curp;
2256 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2257
2258 ASSERT(*idx >= 0);
2259 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2260 ASSERT(!isnullstartblock(new->br_startblock));
2261
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002262 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264#define LEFT r[0]
2265#define RIGHT r[1]
2266#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 /*
2269 * Set up a bunch of variables to make the tests simpler.
2270 */
2271 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002272 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 xfs_bmbt_get_all(ep, &PREV);
2274 newext = new->br_state;
2275 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2276 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2277 ASSERT(PREV.br_state == oldext);
2278 new_endoff = new->br_startoff + new->br_blockcount;
2279 ASSERT(PREV.br_startoff <= new->br_startoff);
2280 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 /*
2283 * Set flags determining what part of the previous oldext allocation
2284 * extent is being replaced by a newext allocation.
2285 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002286 if (PREV.br_startoff == new->br_startoff)
2287 state |= BMAP_LEFT_FILLING;
2288 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2289 state |= BMAP_RIGHT_FILLING;
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 /*
2292 * Check and set flags if this segment has a left neighbor.
2293 * Don't set contiguous if the combined extent would be too large.
2294 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002295 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002296 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002297 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002298
2299 if (isnullstartblock(LEFT.br_startblock))
2300 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002302
2303 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2304 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2305 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2306 LEFT.br_state == newext &&
2307 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2308 state |= BMAP_LEFT_CONTIG;
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 /*
2311 * Check and set flags if this segment has a right neighbor.
2312 * Don't set contiguous if the combined extent would be too large.
2313 * Also check for all-three-contiguous being too large.
2314 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002315 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002316 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002317 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002318 if (isnullstartblock(RIGHT.br_startblock))
2319 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002321
2322 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2323 new_endoff == RIGHT.br_startoff &&
2324 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2325 newext == RIGHT.br_state &&
2326 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2327 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2328 BMAP_RIGHT_FILLING)) !=
2329 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2330 BMAP_RIGHT_FILLING) ||
2331 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2332 <= MAXEXTLEN))
2333 state |= BMAP_RIGHT_CONTIG;
2334
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 /*
2336 * Switch out based on the FILLING and CONTIG state bits.
2337 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002338 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2339 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2340 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2341 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 /*
2343 * Setting all of a previous oldext extent to newext.
2344 * The left and right neighbors are both contiguous with new.
2345 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002346 --*idx;
2347
2348 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2349 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 LEFT.br_blockcount + PREV.br_blockcount +
2351 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002352 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002353
Christoph Hellwigec90c552011-05-23 08:52:53 +00002354 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 ip->i_d.di_nextents -= 2;
2356 if (cur == NULL)
2357 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2358 else {
2359 rval = XFS_ILOG_CORE;
2360 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2361 RIGHT.br_startblock,
2362 RIGHT.br_blockcount, &i)))
2363 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002364 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002365 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002367 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002368 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002370 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002371 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002373 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002374 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002376 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2378 LEFT.br_startblock,
2379 LEFT.br_blockcount + PREV.br_blockcount +
2380 RIGHT.br_blockcount, LEFT.br_state)))
2381 goto done;
2382 }
2383 break;
2384
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002385 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 /*
2387 * Setting all of a previous oldext extent to newext.
2388 * The left neighbor is contiguous, the right is not.
2389 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002390 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002391
Christoph Hellwigec90c552011-05-23 08:52:53 +00002392 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2393 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2394 LEFT.br_blockcount + PREV.br_blockcount);
2395 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2396
2397 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 ip->i_d.di_nextents--;
2399 if (cur == NULL)
2400 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2401 else {
2402 rval = XFS_ILOG_CORE;
2403 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2404 PREV.br_startblock, PREV.br_blockcount,
2405 &i)))
2406 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002407 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002408 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002410 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002411 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002413 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2415 LEFT.br_startblock,
2416 LEFT.br_blockcount + PREV.br_blockcount,
2417 LEFT.br_state)))
2418 goto done;
2419 }
2420 break;
2421
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002422 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 /*
2424 * Setting all of a previous oldext extent to newext.
2425 * The right neighbor is contiguous, the left is not.
2426 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002427 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 xfs_bmbt_set_blockcount(ep,
2429 PREV.br_blockcount + RIGHT.br_blockcount);
2430 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002431 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2432 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 ip->i_d.di_nextents--;
2434 if (cur == NULL)
2435 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2436 else {
2437 rval = XFS_ILOG_CORE;
2438 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2439 RIGHT.br_startblock,
2440 RIGHT.br_blockcount, &i)))
2441 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002442 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002443 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002445 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002446 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002448 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2450 new->br_startblock,
2451 new->br_blockcount + RIGHT.br_blockcount,
2452 newext)))
2453 goto done;
2454 }
2455 break;
2456
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002457 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 /*
2459 * Setting all of a previous oldext extent to newext.
2460 * Neither the left nor right neighbors are contiguous with
2461 * the new one.
2462 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002463 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002465 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (cur == NULL)
2468 rval = XFS_ILOG_DEXT;
2469 else {
2470 rval = 0;
2471 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2472 new->br_startblock, new->br_blockcount,
2473 &i)))
2474 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002475 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2477 new->br_startblock, new->br_blockcount,
2478 newext)))
2479 goto done;
2480 }
2481 break;
2482
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002483 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 /*
2485 * Setting the first part of a previous oldext extent to newext.
2486 * The left neighbor is contiguous.
2487 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002488 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2489 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 LEFT.br_blockcount + new->br_blockcount);
2491 xfs_bmbt_set_startoff(ep,
2492 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002493 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002494
Christoph Hellwigec90c552011-05-23 08:52:53 +00002495 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 xfs_bmbt_set_startblock(ep,
2497 new->br_startblock + new->br_blockcount);
2498 xfs_bmbt_set_blockcount(ep,
2499 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002500 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002501
Christoph Hellwigec90c552011-05-23 08:52:53 +00002502 --*idx;
2503
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (cur == NULL)
2505 rval = XFS_ILOG_DEXT;
2506 else {
2507 rval = 0;
2508 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2509 PREV.br_startblock, PREV.br_blockcount,
2510 &i)))
2511 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002512 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 if ((error = xfs_bmbt_update(cur,
2514 PREV.br_startoff + new->br_blockcount,
2515 PREV.br_startblock + new->br_blockcount,
2516 PREV.br_blockcount - new->br_blockcount,
2517 oldext)))
2518 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002519 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002521 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 LEFT.br_startblock,
2523 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002524 LEFT.br_state);
2525 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 goto done;
2527 }
2528 break;
2529
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002530 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 /*
2532 * Setting the first part of a previous oldext extent to newext.
2533 * The left neighbor is not contiguous.
2534 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002535 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2537 xfs_bmbt_set_startoff(ep, new_endoff);
2538 xfs_bmbt_set_blockcount(ep,
2539 PREV.br_blockcount - new->br_blockcount);
2540 xfs_bmbt_set_startblock(ep,
2541 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002542 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002543
Christoph Hellwigec90c552011-05-23 08:52:53 +00002544 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 ip->i_d.di_nextents++;
2546 if (cur == NULL)
2547 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2548 else {
2549 rval = XFS_ILOG_CORE;
2550 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2551 PREV.br_startblock, PREV.br_blockcount,
2552 &i)))
2553 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002554 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 if ((error = xfs_bmbt_update(cur,
2556 PREV.br_startoff + new->br_blockcount,
2557 PREV.br_startblock + new->br_blockcount,
2558 PREV.br_blockcount - new->br_blockcount,
2559 oldext)))
2560 goto done;
2561 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002562 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002564 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
2566 break;
2567
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002568 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 /*
2570 * Setting the last part of a previous oldext extent to newext.
2571 * The right neighbor is contiguous with the new allocation.
2572 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002573 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 xfs_bmbt_set_blockcount(ep,
2575 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002576 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2577
2578 ++*idx;
2579
2580 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2581 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002582 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002584 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 if (cur == NULL)
2587 rval = XFS_ILOG_DEXT;
2588 else {
2589 rval = 0;
2590 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2591 PREV.br_startblock,
2592 PREV.br_blockcount, &i)))
2593 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002594 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2596 PREV.br_startblock,
2597 PREV.br_blockcount - new->br_blockcount,
2598 oldext)))
2599 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002600 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 goto done;
2602 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2603 new->br_startblock,
2604 new->br_blockcount + RIGHT.br_blockcount,
2605 newext)))
2606 goto done;
2607 }
2608 break;
2609
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002610 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 /*
2612 * Setting the last part of a previous oldext extent to newext.
2613 * The right neighbor is not contiguous.
2614 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002615 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 xfs_bmbt_set_blockcount(ep,
2617 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002618 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002619
Christoph Hellwigec90c552011-05-23 08:52:53 +00002620 ++*idx;
2621 xfs_iext_insert(ip, *idx, 1, new, state);
2622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 ip->i_d.di_nextents++;
2624 if (cur == NULL)
2625 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2626 else {
2627 rval = XFS_ILOG_CORE;
2628 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2629 PREV.br_startblock, PREV.br_blockcount,
2630 &i)))
2631 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002632 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2634 PREV.br_startblock,
2635 PREV.br_blockcount - new->br_blockcount,
2636 oldext)))
2637 goto done;
2638 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2639 new->br_startblock, new->br_blockcount,
2640 &i)))
2641 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002642 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002644 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002646 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
2648 break;
2649
2650 case 0:
2651 /*
2652 * Setting the middle part of a previous oldext extent to
2653 * newext. Contiguity is impossible here.
2654 * One extent becomes three extents.
2655 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002656 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 xfs_bmbt_set_blockcount(ep,
2658 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002659 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002660
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 r[0] = *new;
2662 r[1].br_startoff = new_endoff;
2663 r[1].br_blockcount =
2664 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2665 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2666 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002667
2668 ++*idx;
2669 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 ip->i_d.di_nextents += 2;
2672 if (cur == NULL)
2673 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2674 else {
2675 rval = XFS_ILOG_CORE;
2676 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2677 PREV.br_startblock, PREV.br_blockcount,
2678 &i)))
2679 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002680 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 /* new right extent - oldext */
2682 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2683 r[1].br_startblock, r[1].br_blockcount,
2684 r[1].br_state)))
2685 goto done;
2686 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002688 cur->bc_rec.b.br_blockcount =
2689 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002690 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002692 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002693 /*
2694 * Reset the cursor to the position of the new extent
2695 * we are about to insert as we can't trust it after
2696 * the previous insert.
2697 */
2698 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2699 new->br_startblock, new->br_blockcount,
2700 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002702 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002704 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002705 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002707 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
2709 break;
2710
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002711 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2712 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2713 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2714 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2715 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2716 case BMAP_LEFT_CONTIG:
2717 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 /*
2719 * These cases are all impossible.
2720 */
2721 ASSERT(0);
2722 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002723
Darrick J. Wong9c194642016-08-03 12:16:05 +10002724 /* update reverse mappings */
2725 error = xfs_rmap_convert_extent(mp, dfops, ip, XFS_DATA_FORK, new);
2726 if (error)
2727 goto done;
2728
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002729 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002730 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002731 int tmp_logflags; /* partial log flag return val */
2732
2733 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002734 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002735 0, &tmp_logflags, XFS_DATA_FORK);
2736 *logflagsp |= tmp_logflags;
2737 if (error)
2738 goto done;
2739 }
2740
2741 /* clear out the allocated field, done with it now in any case. */
2742 if (cur) {
2743 cur->bc_private.b.allocated = 0;
2744 *curp = cur;
2745 }
2746
2747 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002749 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 return error;
2751#undef LEFT
2752#undef RIGHT
2753#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754}
2755
2756/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002757 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002759STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760xfs_bmap_add_extent_hole_delay(
2761 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002762 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002763 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002765 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2767 xfs_filblks_t newlen=0; /* new indirect size */
2768 xfs_filblks_t oldlen=0; /* old indirect size */
2769 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2770 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002771 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002773 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002775 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 /*
2778 * Check and set flags if this segment has a left neighbor
2779 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002780 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002781 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002782 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002783
2784 if (isnullstartblock(left.br_startblock))
2785 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002787
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 /*
2789 * Check and set flags if the current (right) segment exists.
2790 * If it doesn't exist, we're converting the hole at end-of-file.
2791 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002792 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002793 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002794 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002795
2796 if (isnullstartblock(right.br_startblock))
2797 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002799
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 /*
2801 * Set contiguity flags on the left and right neighbors.
2802 * Don't let extents get too large, even if the pieces are contiguous.
2803 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002804 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2805 left.br_startoff + left.br_blockcount == new->br_startoff &&
2806 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2807 state |= BMAP_LEFT_CONTIG;
2808
2809 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2810 new->br_startoff + new->br_blockcount == right.br_startoff &&
2811 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2812 (!(state & BMAP_LEFT_CONTIG) ||
2813 (left.br_blockcount + new->br_blockcount +
2814 right.br_blockcount <= MAXEXTLEN)))
2815 state |= BMAP_RIGHT_CONTIG;
2816
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 /*
2818 * Switch out based on the contiguity flags.
2819 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002820 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2821 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 /*
2823 * New allocation is contiguous with delayed allocations
2824 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002825 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002827 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 temp = left.br_blockcount + new->br_blockcount +
2829 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002830
Christoph Hellwigec90c552011-05-23 08:52:53 +00002831 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2832 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002833 oldlen = startblockval(left.br_startblock) +
2834 startblockval(new->br_startblock) +
2835 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002837 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002838 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002839 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002840
Christoph Hellwigec90c552011-05-23 08:52:53 +00002841 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 break;
2843
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002844 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 /*
2846 * New allocation is contiguous with a delayed allocation
2847 * on the left.
2848 * Merge the new allocation with the left neighbor.
2849 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002850 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002852
2853 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2854 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002855 oldlen = startblockval(left.br_startblock) +
2856 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002858 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002859 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002860 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 break;
2862
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002863 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 /*
2865 * New allocation is contiguous with a delayed allocation
2866 * on the right.
2867 * Merge the new allocation with the right neighbor.
2868 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002869 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002871 oldlen = startblockval(new->br_startblock) +
2872 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002874 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2875 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002876 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002877 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 break;
2879
2880 case 0:
2881 /*
2882 * New allocation is not contiguous with another
2883 * delayed allocation.
2884 * Insert a new entry.
2885 */
2886 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002887 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 break;
2889 }
2890 if (oldlen != newlen) {
2891 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002892 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2893 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 /*
2895 * Nothing to do for disk quota accounting here.
2896 */
2897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
2899
2900/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002901 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 */
2903STATIC int /* error */
2904xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002905 struct xfs_bmalloca *bma,
2906 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002908 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 int i; /* temp state */
2911 xfs_ifork_t *ifp; /* inode fork pointer */
2912 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2913 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002914 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002916 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
Eric Sandeenf1f96c42016-01-04 16:10:42 +11002918 mp = bma->ip->i_mount;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002919 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002920
Christoph Hellwigc6534242011-09-18 20:41:05 +00002921 ASSERT(bma->idx >= 0);
2922 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002923 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002924 ASSERT(!bma->cur ||
2925 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002926
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002927 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002928
2929 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002930 if (whichfork == XFS_ATTR_FORK)
2931 state |= BMAP_ATTRFORK;
2932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 /*
2934 * Check and set flags if this segment has a left neighbor.
2935 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002936 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002937 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002938 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002939 if (isnullstartblock(left.br_startblock))
2940 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 /*
2944 * Check and set flags if this segment has a current value.
2945 * Not true if we're inserting into the "hole" at eof.
2946 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002947 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002948 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002949 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002950 if (isnullstartblock(right.br_startblock))
2951 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002953
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 /*
2955 * We're inserting a real allocation between "left" and "right".
2956 * Set the contiguity flags. Don't let extents get too large.
2957 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002958 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2959 left.br_startoff + left.br_blockcount == new->br_startoff &&
2960 left.br_startblock + left.br_blockcount == new->br_startblock &&
2961 left.br_state == new->br_state &&
2962 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2963 state |= BMAP_LEFT_CONTIG;
2964
2965 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2966 new->br_startoff + new->br_blockcount == right.br_startoff &&
2967 new->br_startblock + new->br_blockcount == right.br_startblock &&
2968 new->br_state == right.br_state &&
2969 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2970 (!(state & BMAP_LEFT_CONTIG) ||
2971 left.br_blockcount + new->br_blockcount +
2972 right.br_blockcount <= MAXEXTLEN))
2973 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002975 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 /*
2977 * Select which case we're in here, and implement it.
2978 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002979 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2980 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 /*
2982 * New allocation is contiguous with real allocations on the
2983 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002984 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002986 --bma->idx;
2987 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2988 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 left.br_blockcount + new->br_blockcount +
2990 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002991 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002992
Christoph Hellwigc6534242011-09-18 20:41:05 +00002993 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002994
Christoph Hellwigc6534242011-09-18 20:41:05 +00002995 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
2996 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
2997 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002998 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002999 } else {
3000 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003001 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3002 right.br_startblock, right.br_blockcount,
3003 &i);
3004 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003005 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003006 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003007 error = xfs_btree_delete(bma->cur, &i);
3008 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003009 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003010 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003011 error = xfs_btree_decrement(bma->cur, 0, &i);
3012 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003013 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003014 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003015 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003016 left.br_startblock,
3017 left.br_blockcount +
3018 new->br_blockcount +
3019 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003020 left.br_state);
3021 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003022 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003024 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003026 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 /*
3028 * New allocation is contiguous with a real allocation
3029 * on the left.
3030 * Merge the new allocation with the left neighbor.
3031 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003032 --bma->idx;
3033 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3034 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003036 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003037
Christoph Hellwigc6534242011-09-18 20:41:05 +00003038 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003039 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003040 } else {
3041 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003042 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3043 left.br_startblock, left.br_blockcount,
3044 &i);
3045 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003046 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003047 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003048 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003049 left.br_startblock,
3050 left.br_blockcount +
3051 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003052 left.br_state);
3053 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003054 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003056 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003058 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 /*
3060 * New allocation is contiguous with a real allocation
3061 * on the right.
3062 * Merge the new allocation with the right neighbor.
3063 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003064 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3065 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003066 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 new->br_blockcount + right.br_blockcount,
3068 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003069 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003070
Christoph Hellwigc6534242011-09-18 20:41:05 +00003071 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003072 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003073 } else {
3074 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003075 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003076 right.br_startoff,
3077 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003078 right.br_blockcount, &i);
3079 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003080 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003081 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003082 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003083 new->br_startblock,
3084 new->br_blockcount +
3085 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003086 right.br_state);
3087 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003088 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003090 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
3092 case 0:
3093 /*
3094 * New allocation is not contiguous with another
3095 * real allocation.
3096 * Insert a new entry.
3097 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003098 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3099 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3100 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3101 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003102 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003103 } else {
3104 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003105 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003106 new->br_startoff,
3107 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003108 new->br_blockcount, &i);
3109 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003110 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003111 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003112 bma->cur->bc_rec.b.br_state = new->br_state;
3113 error = xfs_btree_insert(bma->cur, &i);
3114 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003115 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003116 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003118 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003120
Darrick J. Wong9c194642016-08-03 12:16:05 +10003121 /* add reverse mapping */
3122 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
3123 if (error)
3124 goto done;
3125
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003126 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003127 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003128 int tmp_logflags; /* partial log flag return val */
3129
Christoph Hellwigc6534242011-09-18 20:41:05 +00003130 ASSERT(bma->cur == NULL);
3131 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003132 bma->firstblock, bma->dfops, &bma->cur,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003133 0, &tmp_logflags, whichfork);
3134 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003135 if (error)
3136 goto done;
3137 }
3138
3139 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003140 if (bma->cur)
3141 bma->cur->bc_private.b.allocated = 0;
3142
3143 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003144done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003145 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003146 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147}
3148
Nathan Scottdd9f4382006-01-11 15:28:28 +11003149/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003150 * Functions used in the extent read, allocate and remove paths
3151 */
3152
3153/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003154 * Adjust the size of the new extent based on di_extsize and rt extsize.
3155 */
Dave Chinner68988112013-08-12 20:49:42 +10003156int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003157xfs_bmap_extsize_align(
3158 xfs_mount_t *mp,
3159 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3160 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3161 xfs_extlen_t extsz, /* align to this extent size */
3162 int rt, /* is this a realtime inode? */
3163 int eof, /* is extent at end-of-file? */
3164 int delay, /* creating delalloc extent? */
3165 int convert, /* overwriting unwritten extent? */
3166 xfs_fileoff_t *offp, /* in/out: aligned offset */
3167 xfs_extlen_t *lenp) /* in/out: aligned length */
3168{
3169 xfs_fileoff_t orig_off; /* original offset */
3170 xfs_extlen_t orig_alen; /* original length */
3171 xfs_fileoff_t orig_end; /* original off+len */
3172 xfs_fileoff_t nexto; /* next file offset */
3173 xfs_fileoff_t prevo; /* previous file offset */
3174 xfs_fileoff_t align_off; /* temp for offset */
3175 xfs_extlen_t align_alen; /* temp for length */
3176 xfs_extlen_t temp; /* temp for calculations */
3177
3178 if (convert)
3179 return 0;
3180
3181 orig_off = align_off = *offp;
3182 orig_alen = align_alen = *lenp;
3183 orig_end = orig_off + orig_alen;
3184
3185 /*
3186 * If this request overlaps an existing extent, then don't
3187 * attempt to perform any additional alignment.
3188 */
3189 if (!delay && !eof &&
3190 (orig_off >= gotp->br_startoff) &&
3191 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3192 return 0;
3193 }
3194
3195 /*
3196 * If the file offset is unaligned vs. the extent size
3197 * we need to align it. This will be possible unless
3198 * the file was previously written with a kernel that didn't
3199 * perform this alignment, or if a truncate shot us in the
3200 * foot.
3201 */
3202 temp = do_mod(orig_off, extsz);
3203 if (temp) {
3204 align_alen += temp;
3205 align_off -= temp;
3206 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003207
3208 /* Same adjustment for the end of the requested area. */
3209 temp = (align_alen % extsz);
3210 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003211 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003212
3213 /*
3214 * For large extent hint sizes, the aligned extent might be larger than
3215 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3216 * the length back under MAXEXTLEN. The outer allocation loops handle
3217 * short allocation just fine, so it is safe to do this. We only want to
3218 * do it when we are forced to, though, because it means more allocation
3219 * operations are required.
3220 */
3221 while (align_alen > MAXEXTLEN)
3222 align_alen -= extsz;
3223 ASSERT(align_alen <= MAXEXTLEN);
3224
Nathan Scottdd9f4382006-01-11 15:28:28 +11003225 /*
3226 * If the previous block overlaps with this proposed allocation
3227 * then move the start forward without adjusting the length.
3228 */
3229 if (prevp->br_startoff != NULLFILEOFF) {
3230 if (prevp->br_startblock == HOLESTARTBLOCK)
3231 prevo = prevp->br_startoff;
3232 else
3233 prevo = prevp->br_startoff + prevp->br_blockcount;
3234 } else
3235 prevo = 0;
3236 if (align_off != orig_off && align_off < prevo)
3237 align_off = prevo;
3238 /*
3239 * If the next block overlaps with this proposed allocation
3240 * then move the start back without adjusting the length,
3241 * but not before offset 0.
3242 * This may of course make the start overlap previous block,
3243 * and if we hit the offset 0 limit then the next block
3244 * can still overlap too.
3245 */
3246 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3247 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3248 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3249 nexto = gotp->br_startoff + gotp->br_blockcount;
3250 else
3251 nexto = gotp->br_startoff;
3252 } else
3253 nexto = NULLFILEOFF;
3254 if (!eof &&
3255 align_off + align_alen != orig_end &&
3256 align_off + align_alen > nexto)
3257 align_off = nexto > align_alen ? nexto - align_alen : 0;
3258 /*
3259 * If we're now overlapping the next or previous extent that
3260 * means we can't fit an extsz piece in this hole. Just move
3261 * the start forward to the first valid spot and set
3262 * the length so we hit the end.
3263 */
3264 if (align_off != orig_off && align_off < prevo)
3265 align_off = prevo;
3266 if (align_off + align_alen != orig_end &&
3267 align_off + align_alen > nexto &&
3268 nexto != NULLFILEOFF) {
3269 ASSERT(nexto > prevo);
3270 align_alen = nexto - align_off;
3271 }
3272
3273 /*
3274 * If realtime, and the result isn't a multiple of the realtime
3275 * extent size we need to remove blocks until it is.
3276 */
3277 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3278 /*
3279 * We're not covering the original request, or
3280 * we won't be able to once we fix the length.
3281 */
3282 if (orig_off < align_off ||
3283 orig_end > align_off + align_alen ||
3284 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003285 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003286 /*
3287 * Try to fix it by moving the start up.
3288 */
3289 if (align_off + temp <= orig_off) {
3290 align_alen -= temp;
3291 align_off += temp;
3292 }
3293 /*
3294 * Try to fix it by moving the end in.
3295 */
3296 else if (align_off + align_alen - temp >= orig_end)
3297 align_alen -= temp;
3298 /*
3299 * Set the start to the minimum then trim the length.
3300 */
3301 else {
3302 align_alen -= orig_off - align_off;
3303 align_off = orig_off;
3304 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3305 }
3306 /*
3307 * Result doesn't cover the request, fail it.
3308 */
3309 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003310 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003311 } else {
3312 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003313 /* see MAXEXTLEN handling above */
3314 ASSERT(orig_end <= align_off + align_alen ||
3315 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003316 }
3317
3318#ifdef DEBUG
3319 if (!eof && gotp->br_startoff != NULLFILEOFF)
3320 ASSERT(align_off + align_alen <= gotp->br_startoff);
3321 if (prevp->br_startoff != NULLFILEOFF)
3322 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3323#endif
3324
3325 *lenp = align_alen;
3326 *offp = align_off;
3327 return 0;
3328}
3329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330#define XFS_ALLOC_GAP_UNITS 4
3331
Dave Chinner68988112013-08-12 20:49:42 +10003332void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003333xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003334 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
3336 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3338 xfs_mount_t *mp; /* mount point structure */
3339 int nullfb; /* true if ap->firstblock isn't set */
3340 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342#define ISVALID(x,y) \
3343 (rt ? \
3344 (x) < mp->m_sb.sb_rblocks : \
3345 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3346 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3347 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003350 nullfb = *ap->firstblock == NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003352 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 /*
3354 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003355 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003357 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3358 !isnullstartblock(ap->prev.br_startblock) &&
3359 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3360 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003361 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 /*
3363 * Adjust for the gap between prevp and us.
3364 */
Dave Chinner3a756672011-09-18 20:40:58 +00003365 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003366 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003368 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3369 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 }
3371 /*
3372 * If not at eof, then compare the two neighbor blocks.
3373 * Figure out whether either one gives us a good starting point,
3374 * and pick the better one.
3375 */
3376 else if (!ap->eof) {
3377 xfs_fsblock_t gotbno; /* right side block number */
3378 xfs_fsblock_t gotdiff=0; /* right side difference */
3379 xfs_fsblock_t prevbno; /* left side block number */
3380 xfs_fsblock_t prevdiff=0; /* left side difference */
3381
3382 /*
3383 * If there's a previous (left) block, select a requested
3384 * start block based on it.
3385 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003386 if (ap->prev.br_startoff != NULLFILEOFF &&
3387 !isnullstartblock(ap->prev.br_startblock) &&
3388 (prevbno = ap->prev.br_startblock +
3389 ap->prev.br_blockcount) &&
3390 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 /*
3392 * Calculate gap to end of previous block.
3393 */
Dave Chinner3a756672011-09-18 20:40:58 +00003394 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003395 (ap->prev.br_startoff +
3396 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 /*
3398 * Figure the startblock based on the previous block's
3399 * end and the gap size.
3400 * Heuristic!
3401 * If the gap is large relative to the piece we're
3402 * allocating, or using it gives us an invalid block
3403 * number, then just use the end of the previous block.
3404 */
Dave Chinner3a756672011-09-18 20:40:58 +00003405 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003407 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 prevbno += adjust;
3409 else
3410 prevdiff += adjust;
3411 /*
3412 * If the firstblock forbids it, can't use it,
3413 * must use default.
3414 */
3415 if (!rt && !nullfb &&
3416 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3417 prevbno = NULLFSBLOCK;
3418 }
3419 /*
3420 * No previous block or can't follow it, just default.
3421 */
3422 else
3423 prevbno = NULLFSBLOCK;
3424 /*
3425 * If there's a following (right) block, select a requested
3426 * start block based on it.
3427 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003428 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 /*
3430 * Calculate gap to start of next block.
3431 */
Dave Chinner3a756672011-09-18 20:40:58 +00003432 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 /*
3434 * Figure the startblock based on the next block's
3435 * start and the gap size.
3436 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003437 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 /*
3439 * Heuristic!
3440 * If the gap is large relative to the piece we're
3441 * allocating, or using it gives us an invalid block
3442 * number, then just use the start of the next block
3443 * offset by our length.
3444 */
Dave Chinner3a756672011-09-18 20:40:58 +00003445 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 ISVALID(gotbno - gotdiff, gotbno))
3447 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003448 else if (ISVALID(gotbno - ap->length, gotbno)) {
3449 gotbno -= ap->length;
3450 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 } else
3452 gotdiff += adjust;
3453 /*
3454 * If the firstblock forbids it, can't use it,
3455 * must use default.
3456 */
3457 if (!rt && !nullfb &&
3458 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3459 gotbno = NULLFSBLOCK;
3460 }
3461 /*
3462 * No next block, just default.
3463 */
3464 else
3465 gotbno = NULLFSBLOCK;
3466 /*
3467 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003468 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 */
3470 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003471 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003473 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003475 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003477#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003478}
3479
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003480static int
3481xfs_bmap_longest_free_extent(
3482 struct xfs_trans *tp,
3483 xfs_agnumber_t ag,
3484 xfs_extlen_t *blen,
3485 int *notinit)
3486{
3487 struct xfs_mount *mp = tp->t_mountp;
3488 struct xfs_perag *pag;
3489 xfs_extlen_t longest;
3490 int error = 0;
3491
3492 pag = xfs_perag_get(mp, ag);
3493 if (!pag->pagf_init) {
3494 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3495 if (error)
3496 goto out;
3497
3498 if (!pag->pagf_init) {
3499 *notinit = 1;
3500 goto out;
3501 }
3502 }
3503
Dave Chinner50adbcb2015-06-22 10:04:31 +10003504 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003505 xfs_alloc_min_freelist(mp, pag),
3506 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003507 if (*blen < longest)
3508 *blen = longest;
3509
3510out:
3511 xfs_perag_put(pag);
3512 return error;
3513}
3514
3515static void
3516xfs_bmap_select_minlen(
3517 struct xfs_bmalloca *ap,
3518 struct xfs_alloc_arg *args,
3519 xfs_extlen_t *blen,
3520 int notinit)
3521{
3522 if (notinit || *blen < ap->minlen) {
3523 /*
3524 * Since we did a BUF_TRYLOCK above, it is possible that
3525 * there is space for this request.
3526 */
3527 args->minlen = ap->minlen;
3528 } else if (*blen < args->maxlen) {
3529 /*
3530 * If the best seen length is less than the request length,
3531 * use the best as the minimum.
3532 */
3533 args->minlen = *blen;
3534 } else {
3535 /*
3536 * Otherwise we've seen an extent as big as maxlen, use that
3537 * as the minimum.
3538 */
3539 args->minlen = args->maxlen;
3540 }
3541}
3542
Nathan Scotta365bdd2006-03-14 13:34:16 +11003543STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003544xfs_bmap_btalloc_nullfb(
3545 struct xfs_bmalloca *ap,
3546 struct xfs_alloc_arg *args,
3547 xfs_extlen_t *blen)
3548{
3549 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003550 xfs_agnumber_t ag, startag;
3551 int notinit = 0;
3552 int error;
3553
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003554 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003555 args->total = ap->total;
3556
Christoph Hellwigc467c042010-02-15 23:34:42 +00003557 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3558 if (startag == NULLAGNUMBER)
3559 startag = ag = 0;
3560
Dave Chinner14b064c2011-01-27 12:16:28 +11003561 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003562 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3563 &notinit);
3564 if (error)
3565 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003566
Christoph Hellwigc467c042010-02-15 23:34:42 +00003567 if (++ag == mp->m_sb.sb_agcount)
3568 ag = 0;
3569 if (ag == startag)
3570 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003571 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003572
3573 xfs_bmap_select_minlen(ap, args, blen, notinit);
3574 return 0;
3575}
3576
3577STATIC int
3578xfs_bmap_btalloc_filestreams(
3579 struct xfs_bmalloca *ap,
3580 struct xfs_alloc_arg *args,
3581 xfs_extlen_t *blen)
3582{
3583 struct xfs_mount *mp = ap->ip->i_mount;
3584 xfs_agnumber_t ag;
3585 int notinit = 0;
3586 int error;
3587
3588 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3589 args->total = ap->total;
3590
3591 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3592 if (ag == NULLAGNUMBER)
3593 ag = 0;
3594
3595 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3596 if (error)
3597 return error;
3598
3599 if (*blen < args->maxlen) {
3600 error = xfs_filestream_new_ag(ap, &ag);
3601 if (error)
3602 return error;
3603
3604 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3605 &notinit);
3606 if (error)
3607 return error;
3608
3609 }
3610
3611 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003612
3613 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003614 * Set the failure fallback case to look in the selected AG as stream
3615 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003616 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003617 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003618 return 0;
3619}
3620
3621STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003622xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003623 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003624{
3625 xfs_mount_t *mp; /* mount point structure */
3626 xfs_alloctype_t atype = 0; /* type for allocation routines */
3627 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003628 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003629 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003630 xfs_alloc_arg_t args;
3631 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003632 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003633 int nullfb; /* true if ap->firstblock isn't set */
3634 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003635 int tryagain;
3636 int error;
Dave Chinner33177f02013-12-12 16:34:36 +11003637 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003638
Dave Chinnera99ebf42011-12-01 11:24:20 +00003639 ASSERT(ap->length);
3640
Nathan Scotta365bdd2006-03-14 13:34:16 +11003641 mp = ap->ip->i_mount;
Dave Chinner33177f02013-12-12 16:34:36 +11003642
3643 /* stripe alignment for allocation is determined by mount parameters */
3644 stripe_align = 0;
3645 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3646 stripe_align = mp->m_swidth;
3647 else if (mp->m_dalign)
3648 stripe_align = mp->m_dalign;
3649
David Chinner957d0eb2007-06-18 16:50:37 +10003650 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003651 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003652 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003653 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003654 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003655 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003656 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003657 }
Dave Chinner33177f02013-12-12 16:34:36 +11003658
3659
Dave Chinner0937e0f2011-09-18 20:40:57 +00003660 nullfb = *ap->firstblock == NULLFSBLOCK;
3661 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003662 if (nullfb) {
3663 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
3664 ag = xfs_filestream_lookup_ag(ap->ip);
3665 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003666 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003667 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003668 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003669 }
3670 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003671 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003672
3673 xfs_bmap_adjacent(ap);
3674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003676 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 * it's in the right allocation group.
3678 */
Dave Chinner3a756672011-09-18 20:40:58 +00003679 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 ;
3681 else
Dave Chinner3a756672011-09-18 20:40:58 +00003682 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 * Normal allocation, done through xfs_alloc_vextent.
3685 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003686 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003687 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003688 args.tp = ap->tp;
3689 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003690 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003691 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003692
3693 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003694 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003695 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003696 blen = 0;
3697 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003698 /*
3699 * Search for an allocation group with a single extent large
3700 * enough for the request. If one isn't found, then adjust
3701 * the minimum allocation size to the largest space found.
3702 */
3703 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3704 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3705 else
3706 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003707 if (error)
3708 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003709 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003710 if (xfs_inode_is_filestream(ap->ip))
3711 args.type = XFS_ALLOCTYPE_FIRST_AG;
3712 else
3713 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003714 args.total = args.minlen = ap->minlen;
3715 } else {
3716 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3717 args.total = ap->total;
3718 args.minlen = ap->minlen;
3719 }
David Chinner957d0eb2007-06-18 16:50:37 +10003720 /* apply extent size hints if obtained earlier */
3721 if (unlikely(align)) {
3722 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003723 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003724 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003725 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003726 args.prod = 1;
3727 args.mod = 0;
3728 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003729 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003730 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003731 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3732 }
3733 /*
3734 * If we are not low on available data blocks, and the
3735 * underlying logical volume manager is a stripe, and
3736 * the file offset is zero then try to allocate data
3737 * blocks on stripe unit boundary.
3738 * NOTE: ap->aeof is only set if the allocation length
3739 * is >= the stripe unit and the allocation offset is
3740 * at the end of file.
3741 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003742 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003743 if (!ap->offset) {
Dave Chinner33177f02013-12-12 16:34:36 +11003744 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003745 atype = args.type;
3746 isaligned = 1;
3747 /*
3748 * Adjust for alignment
3749 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003750 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003751 args.minlen = blen - args.alignment;
3752 args.minalignslop = 0;
3753 } else {
3754 /*
3755 * First try an exact bno allocation.
3756 * If it fails then do a near or start bno
3757 * allocation with alignment turned on.
3758 */
3759 atype = args.type;
3760 tryagain = 1;
3761 args.type = XFS_ALLOCTYPE_THIS_BNO;
3762 args.alignment = 1;
3763 /*
3764 * Compute the minlen+alignment for the
3765 * next case. Set slop so that the value
3766 * of minlen+alignment+slop doesn't go up
3767 * between the calls.
3768 */
Dave Chinner33177f02013-12-12 16:34:36 +11003769 if (blen > stripe_align && blen <= args.maxlen)
3770 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003771 else
3772 nextminlen = args.minlen;
Dave Chinner33177f02013-12-12 16:34:36 +11003773 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003774 args.minalignslop =
Dave Chinner33177f02013-12-12 16:34:36 +11003775 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003776 args.minlen - 1;
3777 else
3778 args.minalignslop = 0;
3779 }
3780 } else {
3781 args.alignment = 1;
3782 args.minalignslop = 0;
3783 }
3784 args.minleft = ap->minleft;
3785 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003786 args.resv = XFS_AG_RESV_NONE;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003787 args.userdata = ap->userdata;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003788 if (ap->userdata & XFS_ALLOC_USERDATA_ZERO)
3789 args.ip = ap->ip;
3790
3791 error = xfs_alloc_vextent(&args);
3792 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003793 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003794
Nathan Scotta365bdd2006-03-14 13:34:16 +11003795 if (tryagain && args.fsbno == NULLFSBLOCK) {
3796 /*
3797 * Exact allocation failed. Now try with alignment
3798 * turned on.
3799 */
3800 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003801 args.fsbno = ap->blkno;
Dave Chinner33177f02013-12-12 16:34:36 +11003802 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003803 args.minlen = nextminlen;
3804 args.minalignslop = 0;
3805 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 if ((error = xfs_alloc_vextent(&args)))
3807 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003808 }
3809 if (isaligned && args.fsbno == NULLFSBLOCK) {
3810 /*
3811 * allocation failed, so turn off alignment and
3812 * try again.
3813 */
3814 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003815 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003816 args.alignment = 0;
3817 if ((error = xfs_alloc_vextent(&args)))
3818 return error;
3819 }
3820 if (args.fsbno == NULLFSBLOCK && nullfb &&
3821 args.minlen > ap->minlen) {
3822 args.minlen = ap->minlen;
3823 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003824 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003825 if ((error = xfs_alloc_vextent(&args)))
3826 return error;
3827 }
3828 if (args.fsbno == NULLFSBLOCK && nullfb) {
3829 args.fsbno = 0;
3830 args.type = XFS_ALLOCTYPE_FIRST_AG;
3831 args.total = ap->minlen;
3832 args.minleft = 0;
3833 if ((error = xfs_alloc_vextent(&args)))
3834 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003835 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003836 }
3837 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003838 /*
3839 * check the allocation happened at the same or higher AG than
3840 * the first block that was allocated.
3841 */
3842 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3843 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3844 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003845 (ap->dfops->dop_low &&
Dave Chinner0937e0f2011-09-18 20:40:57 +00003846 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3847 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3848
Dave Chinner3a756672011-09-18 20:40:58 +00003849 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003850 if (*ap->firstblock == NULLFSBLOCK)
3851 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003852 ASSERT(nullfb || fb_agno == args.agno ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003853 (ap->dfops->dop_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003854 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003855 ap->ip->i_d.di_nblocks += args.len;
3856 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3857 if (ap->wasdel)
3858 ap->ip->i_delayed_blks -= args.len;
3859 /*
3860 * Adjust the disk quota also. This was reserved
3861 * earlier.
3862 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003863 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003864 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3865 XFS_TRANS_DQ_BCOUNT,
3866 (long) args.len);
3867 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003868 ap->blkno = NULLFSBLOCK;
3869 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 }
3871 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003872}
3873
3874/*
3875 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3876 * It figures out where to ask the underlying allocator to put the new extent.
3877 */
3878STATIC int
3879xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003880 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003881{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003882 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003883 return xfs_bmap_rtalloc(ap);
3884 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885}
3886
3887/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003888 * Trim the returned map to the required bounds
3889 */
3890STATIC void
3891xfs_bmapi_trim_map(
3892 struct xfs_bmbt_irec *mval,
3893 struct xfs_bmbt_irec *got,
3894 xfs_fileoff_t *bno,
3895 xfs_filblks_t len,
3896 xfs_fileoff_t obno,
3897 xfs_fileoff_t end,
3898 int n,
3899 int flags)
3900{
3901 if ((flags & XFS_BMAPI_ENTIRE) ||
3902 got->br_startoff + got->br_blockcount <= obno) {
3903 *mval = *got;
3904 if (isnullstartblock(got->br_startblock))
3905 mval->br_startblock = DELAYSTARTBLOCK;
3906 return;
3907 }
3908
3909 if (obno > *bno)
3910 *bno = obno;
3911 ASSERT((*bno >= obno) || (n == 0));
3912 ASSERT(*bno < end);
3913 mval->br_startoff = *bno;
3914 if (isnullstartblock(got->br_startblock))
3915 mval->br_startblock = DELAYSTARTBLOCK;
3916 else
3917 mval->br_startblock = got->br_startblock +
3918 (*bno - got->br_startoff);
3919 /*
3920 * Return the minimum of what we got and what we asked for for
3921 * the length. We can use the len variable here because it is
3922 * modified below and we could have been there before coming
3923 * here if the first part of the allocation didn't overlap what
3924 * was asked for.
3925 */
3926 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3927 got->br_blockcount - (*bno - got->br_startoff));
3928 mval->br_state = got->br_state;
3929 ASSERT(mval->br_blockcount <= len);
3930 return;
3931}
3932
3933/*
3934 * Update and validate the extent map to return
3935 */
3936STATIC void
3937xfs_bmapi_update_map(
3938 struct xfs_bmbt_irec **map,
3939 xfs_fileoff_t *bno,
3940 xfs_filblks_t *len,
3941 xfs_fileoff_t obno,
3942 xfs_fileoff_t end,
3943 int *n,
3944 int flags)
3945{
3946 xfs_bmbt_irec_t *mval = *map;
3947
3948 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3949 ((mval->br_startoff + mval->br_blockcount) <= end));
3950 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3951 (mval->br_startoff < obno));
3952
3953 *bno = mval->br_startoff + mval->br_blockcount;
3954 *len = end - *bno;
3955 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3956 /* update previous map with new information */
3957 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3958 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3959 ASSERT(mval->br_state == mval[-1].br_state);
3960 mval[-1].br_blockcount = mval->br_blockcount;
3961 mval[-1].br_state = mval->br_state;
3962 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3963 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3964 mval[-1].br_startblock != HOLESTARTBLOCK &&
3965 mval->br_startblock == mval[-1].br_startblock +
3966 mval[-1].br_blockcount &&
3967 ((flags & XFS_BMAPI_IGSTATE) ||
3968 mval[-1].br_state == mval->br_state)) {
3969 ASSERT(mval->br_startoff ==
3970 mval[-1].br_startoff + mval[-1].br_blockcount);
3971 mval[-1].br_blockcount += mval->br_blockcount;
3972 } else if (*n > 0 &&
3973 mval->br_startblock == DELAYSTARTBLOCK &&
3974 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3975 mval->br_startoff ==
3976 mval[-1].br_startoff + mval[-1].br_blockcount) {
3977 mval[-1].br_blockcount += mval->br_blockcount;
3978 mval[-1].br_state = mval->br_state;
3979 } else if (!((*n == 0) &&
3980 ((mval->br_startoff + mval->br_blockcount) <=
3981 obno))) {
3982 mval++;
3983 (*n)++;
3984 }
3985 *map = mval;
3986}
3987
3988/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003989 * Map file blocks to filesystem blocks without allocation.
3990 */
3991int
3992xfs_bmapi_read(
3993 struct xfs_inode *ip,
3994 xfs_fileoff_t bno,
3995 xfs_filblks_t len,
3996 struct xfs_bmbt_irec *mval,
3997 int *nmap,
3998 int flags)
3999{
4000 struct xfs_mount *mp = ip->i_mount;
4001 struct xfs_ifork *ifp;
4002 struct xfs_bmbt_irec got;
4003 struct xfs_bmbt_irec prev;
4004 xfs_fileoff_t obno;
4005 xfs_fileoff_t end;
4006 xfs_extnum_t lastx;
4007 int error;
4008 int eof;
4009 int n = 0;
4010 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4011 XFS_ATTR_FORK : XFS_DATA_FORK;
4012
4013 ASSERT(*nmap >= 1);
4014 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4015 XFS_BMAPI_IGSTATE)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004016 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004017
4018 if (unlikely(XFS_TEST_ERROR(
4019 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4020 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4021 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4022 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004023 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004024 }
4025
4026 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004027 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004028
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004029 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004030
4031 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004032
4033 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4034 error = xfs_iread_extents(NULL, ip, whichfork);
4035 if (error)
4036 return error;
4037 }
4038
4039 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4040 end = bno + len;
4041 obno = bno;
4042
4043 while (bno < end && n < *nmap) {
4044 /* Reading past eof, act as though there's a hole up to end. */
4045 if (eof)
4046 got.br_startoff = end;
4047 if (got.br_startoff > bno) {
4048 /* Reading in a hole. */
4049 mval->br_startoff = bno;
4050 mval->br_startblock = HOLESTARTBLOCK;
4051 mval->br_blockcount =
4052 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4053 mval->br_state = XFS_EXT_NORM;
4054 bno += mval->br_blockcount;
4055 len -= mval->br_blockcount;
4056 mval++;
4057 n++;
4058 continue;
4059 }
4060
4061 /* set up the extent map to return. */
4062 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4063 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4064
4065 /* If we're done, stop now. */
4066 if (bno >= end || n >= *nmap)
4067 break;
4068
4069 /* Else go on to the next record. */
4070 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4071 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4072 else
4073 eof = 1;
4074 }
4075 *nmap = n;
4076 return 0;
4077}
4078
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004079STATIC int
4080xfs_bmapi_reserve_delalloc(
4081 struct xfs_inode *ip,
4082 xfs_fileoff_t aoff,
4083 xfs_filblks_t len,
4084 struct xfs_bmbt_irec *got,
4085 struct xfs_bmbt_irec *prev,
4086 xfs_extnum_t *lastx,
4087 int eof)
4088{
4089 struct xfs_mount *mp = ip->i_mount;
4090 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4091 xfs_extlen_t alen;
4092 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004093 char rt = XFS_IS_REALTIME_INODE(ip);
4094 xfs_extlen_t extsz;
4095 int error;
4096
4097 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4098 if (!eof)
4099 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4100
4101 /* Figure out the extent size, adjust alen */
4102 extsz = xfs_get_extsz_hint(ip);
4103 if (extsz) {
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004104 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4105 1, 0, &aoff, &alen);
4106 ASSERT(!error);
4107 }
4108
4109 if (rt)
4110 extsz = alen / mp->m_sb.sb_rextsize;
4111
4112 /*
4113 * Make a transaction-less quota reservation for delayed allocation
4114 * blocks. This number gets adjusted later. We return if we haven't
4115 * allocated blocks already inside this loop.
4116 */
4117 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4118 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4119 if (error)
4120 return error;
4121
4122 /*
4123 * Split changing sb for alen and indlen since they could be coming
4124 * from different places.
4125 */
4126 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4127 ASSERT(indlen > 0);
4128
4129 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004130 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004131 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004132 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004133 }
4134
4135 if (error)
4136 goto out_unreserve_quota;
4137
Dave Chinner0d485ad2015-02-23 21:22:03 +11004138 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004139 if (error)
4140 goto out_unreserve_blocks;
4141
4142
4143 ip->i_delayed_blks += alen;
4144
4145 got->br_startoff = aoff;
4146 got->br_startblock = nullstartblock(indlen);
4147 got->br_blockcount = alen;
4148 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004149 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004150
4151 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004152 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4153 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004154 */
4155 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4156
4157 ASSERT(got->br_startoff <= aoff);
4158 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4159 ASSERT(isnullstartblock(got->br_startblock));
4160 ASSERT(got->br_state == XFS_EXT_NORM);
4161 return 0;
4162
4163out_unreserve_blocks:
4164 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004165 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004166 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004167 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004168out_unreserve_quota:
4169 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004170 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004171 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4172 return error;
4173}
4174
Dave Chinner5c8ed202011-09-18 20:40:45 +00004175/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004176 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4177 */
4178int
4179xfs_bmapi_delay(
4180 struct xfs_inode *ip, /* incore inode */
4181 xfs_fileoff_t bno, /* starting file offs. mapped */
4182 xfs_filblks_t len, /* length to map in file */
4183 struct xfs_bmbt_irec *mval, /* output: map values */
4184 int *nmap, /* i/o: mval size/count */
4185 int flags) /* XFS_BMAPI_... */
4186{
4187 struct xfs_mount *mp = ip->i_mount;
4188 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4189 struct xfs_bmbt_irec got; /* current file extent record */
4190 struct xfs_bmbt_irec prev; /* previous file extent record */
4191 xfs_fileoff_t obno; /* old block number (offset) */
4192 xfs_fileoff_t end; /* end of mapped file region */
4193 xfs_extnum_t lastx; /* last useful extent number */
4194 int eof; /* we've hit the end of extents */
4195 int n = 0; /* current extent index */
4196 int error = 0;
4197
4198 ASSERT(*nmap >= 1);
4199 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4200 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004201 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig44032802011-09-18 20:40:48 +00004202
4203 if (unlikely(XFS_TEST_ERROR(
4204 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4205 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4206 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4207 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004208 return -EFSCORRUPTED;
Christoph Hellwig44032802011-09-18 20:40:48 +00004209 }
4210
4211 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004212 return -EIO;
Christoph Hellwig44032802011-09-18 20:40:48 +00004213
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004214 XFS_STATS_INC(mp, xs_blk_mapw);
Christoph Hellwig44032802011-09-18 20:40:48 +00004215
4216 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4217 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4218 if (error)
4219 return error;
4220 }
4221
4222 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4223 end = bno + len;
4224 obno = bno;
4225
4226 while (bno < end && n < *nmap) {
4227 if (eof || got.br_startoff > bno) {
4228 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4229 &prev, &lastx, eof);
4230 if (error) {
4231 if (n == 0) {
4232 *nmap = 0;
4233 return error;
4234 }
4235 break;
4236 }
4237 }
4238
4239 /* set up the extent map to return. */
4240 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4241 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4242
4243 /* If we're done, stop now. */
4244 if (bno >= end || n >= *nmap)
4245 break;
4246
4247 /* Else go on to the next record. */
4248 prev = got;
4249 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4250 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4251 else
4252 eof = 1;
4253 }
4254
4255 *nmap = n;
4256 return 0;
4257}
4258
4259
Dave Chinnercf11da92014-07-15 07:08:24 +10004260static int
4261xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004262 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004263{
4264 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004265 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004266 XFS_ATTR_FORK : XFS_DATA_FORK;
4267 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004268 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004269 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004270
Dave Chinnera99ebf42011-12-01 11:24:20 +00004271 ASSERT(bma->length > 0);
4272
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004273 /*
4274 * For the wasdelay case, we could also just allocate the stuff asked
4275 * for in this bmap call but that wouldn't be as good.
4276 */
4277 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004278 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4279 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004280 if (bma->idx != NULLEXTNUM && bma->idx) {
4281 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004282 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004283 }
4284 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004285 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004286 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004287 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004288 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004289 }
4290
4291 /*
4292 * Indicate if this is the first user data in the file, or just any
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004293 * user data. And if it is userdata, indicate whether it needs to
4294 * be initialised to zero during allocation.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004295 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004296 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004297 bma->userdata = (bma->offset == 0) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004298 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004299 if (bma->flags & XFS_BMAPI_ZERO)
4300 bma->userdata |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004301 }
4302
Dave Chinnere04426b2012-10-05 11:06:59 +10004303 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004304
4305 /*
4306 * Only want to do the alignment at the eof if it is userdata and
4307 * allocation length is larger than a stripe unit.
4308 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004309 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004310 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004311 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004312 if (error)
4313 return error;
4314 }
4315
4316 error = xfs_bmap_alloc(bma);
4317 if (error)
4318 return error;
4319
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004320 if (bma->dfops->dop_low)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004321 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004322 if (bma->cur)
4323 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004324 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004325 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004326 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4327 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4328 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004329 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004330 }
4331 /*
4332 * Bump the number of extents we've allocated
4333 * in this call.
4334 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004335 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004336
Dave Chinner29c8d172011-09-18 20:41:00 +00004337 if (bma->cur)
4338 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004339 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4340
Dave Chinner963c30c2011-09-18 20:40:59 +00004341 bma->got.br_startoff = bma->offset;
4342 bma->got.br_startblock = bma->blkno;
4343 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004344 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004345
4346 /*
4347 * A wasdelay extent has been initialized, so shouldn't be flagged
4348 * as unwritten.
4349 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004350 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004351 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004352 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004353
Christoph Hellwigc6534242011-09-18 20:41:05 +00004354 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004355 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004356 else
4357 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004358
Christoph Hellwigc315c902011-09-18 20:41:02 +00004359 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004360 if (error)
4361 return error;
4362
4363 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004364 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4365 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4366 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004367 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004368 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004369
Dave Chinner963c30c2011-09-18 20:40:59 +00004370 ASSERT(bma->got.br_startoff <= bma->offset);
4371 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4372 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004373 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4374 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004375 return 0;
4376}
4377
Dave Chinnerb447fe52011-09-18 20:40:51 +00004378STATIC int
4379xfs_bmapi_convert_unwritten(
4380 struct xfs_bmalloca *bma,
4381 struct xfs_bmbt_irec *mval,
4382 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004383 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004384{
4385 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4386 XFS_ATTR_FORK : XFS_DATA_FORK;
4387 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004388 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004389 int error;
4390
Dave Chinnerb447fe52011-09-18 20:40:51 +00004391 /* check if we need to do unwritten->real conversion */
4392 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4393 (flags & XFS_BMAPI_PREALLOC))
4394 return 0;
4395
4396 /* check if we need to do real->unwritten conversion */
4397 if (mval->br_state == XFS_EXT_NORM &&
4398 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4399 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4400 return 0;
4401
4402 /*
4403 * Modify (by adding) the state flag, if writing.
4404 */
4405 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004406 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4407 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004408 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004409 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004410 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004411 }
4412 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4413 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4414
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004415 /*
4416 * Before insertion into the bmbt, zero the range being converted
4417 * if required.
4418 */
4419 if (flags & XFS_BMAPI_ZERO) {
4420 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4421 mval->br_blockcount);
4422 if (error)
4423 return error;
4424 }
4425
Dave Chinnere0c3da52011-09-18 20:41:01 +00004426 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004427 &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004428 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004429 /*
4430 * Log the inode core unconditionally in the unwritten extent conversion
4431 * path because the conversion might not have done so (e.g., if the
4432 * extent count hasn't changed). We need to make sure the inode is dirty
4433 * in the transaction for the sake of fsync(), even if nothing has
4434 * changed, because fsync() will not force the log for this transaction
4435 * unless it sees the inode pinned.
4436 */
4437 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004438 if (error)
4439 return error;
4440
4441 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004442 * Update our extent pointer, given that
4443 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4444 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004445 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004446 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004447
4448 /*
4449 * We may have combined previously unwritten space with written space,
4450 * so generate another request.
4451 */
4452 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004453 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004454 return 0;
4455}
4456
Christoph Hellwig44032802011-09-18 20:40:48 +00004457/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004458 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4459 * extent state if necessary. Details behaviour is controlled by the flags
4460 * parameter. Only allocates blocks from a single allocation group, to avoid
4461 * locking problems.
4462 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 * The returned value in "firstblock" from the first call in a transaction
4464 * must be remembered and presented to subsequent calls in "firstblock".
4465 * An upper bound for the number of blocks to be allocated is supplied to
4466 * the first call in "total"; if no allocation group has that many free
4467 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4468 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004469int
4470xfs_bmapi_write(
4471 struct xfs_trans *tp, /* transaction pointer */
4472 struct xfs_inode *ip, /* incore inode */
4473 xfs_fileoff_t bno, /* starting file offs. mapped */
4474 xfs_filblks_t len, /* length to map in file */
4475 int flags, /* XFS_BMAPI_... */
4476 xfs_fsblock_t *firstblock, /* first allocated block
4477 controls a.g. for allocs */
4478 xfs_extlen_t total, /* total blocks needed */
4479 struct xfs_bmbt_irec *mval, /* output: map values */
4480 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004481 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004483 struct xfs_mount *mp = ip->i_mount;
4484 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004485 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004486 xfs_fileoff_t end; /* end of mapped file region */
4487 int eof; /* after the end of extents */
4488 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004489 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004490 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004491 int whichfork; /* data or attr fork */
4492 char inhole; /* current location is hole in file */
4493 char wasdelay; /* old extent was delayed */
4494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004496 xfs_fileoff_t orig_bno; /* original block number value */
4497 int orig_flags; /* original flags arg value */
4498 xfs_filblks_t orig_len; /* original value of len arg */
4499 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4500 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
4502 orig_bno = bno;
4503 orig_len = len;
4504 orig_flags = flags;
4505 orig_mval = mval;
4506 orig_nmap = *nmap;
4507#endif
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004508 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4509 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004510
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004512 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4513 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4514 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004515 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004516 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004517 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004518
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004519 /* zeroing is for currently only for data extents, not metadata */
4520 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4521 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4522 /*
4523 * we can allocate unwritten extents or pre-zero allocated blocks,
4524 * but it makes no sense to do both at once. This would result in
4525 * zeroing the unwritten extent twice, but it still being an
4526 * unwritten extent....
4527 */
4528 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4529 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4530
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 if (unlikely(XFS_TEST_ERROR(
4532 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004533 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004535 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004536 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004538
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004540 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004541
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004543
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004544 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004545
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004546 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004548 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004550 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004551 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004552 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004553 }
4554
4555 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4556 error = xfs_iread_extents(tp, ip, whichfork);
4557 if (error)
4558 goto error0;
4559 }
4560
Dave Chinnere0c3da52011-09-18 20:41:01 +00004561 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004562 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 n = 0;
4564 end = bno + len;
4565 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004566
4567 bma.tp = tp;
4568 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004569 bma.total = total;
4570 bma.userdata = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004571 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004572 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004573
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004575 inhole = eof || bma.got.br_startoff > bno;
4576 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004577
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 /*
4579 * First, deal with the hole before the allocated space
4580 * that we found, if any.
4581 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004582 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004583 bma.eof = eof;
4584 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4585 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004586 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004587 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004588
Dave Chinnera99ebf42011-12-01 11:24:20 +00004589 /*
4590 * There's a 32/64 bit type mismatch between the
4591 * allocation length request (which can be 64 bits in
4592 * length) and the bma length request, which is
4593 * xfs_extlen_t and therefore 32 bits. Hence we have to
4594 * check for 32-bit overflows and handle them here.
4595 */
4596 if (len > (xfs_filblks_t)MAXEXTLEN)
4597 bma.length = MAXEXTLEN;
4598 else
4599 bma.length = len;
4600
4601 ASSERT(len > 0);
4602 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004603 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 if (error)
4605 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004606 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004607 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004609
Dave Chinneraef9a892011-09-18 20:40:44 +00004610 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004611 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4612 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613
Dave Chinnerb447fe52011-09-18 20:40:51 +00004614 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004615 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004616 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004617 continue;
4618 if (error)
4619 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620
Dave Chinneraef9a892011-09-18 20:40:44 +00004621 /* update the extent map to return */
4622 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4623
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 /*
4625 * If we're done, stop now. Stop when we've allocated
4626 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4627 * the transaction may get too big.
4628 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004629 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004631
4632 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004633 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004634 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4635 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4636 &bma.got);
4637 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00004638 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 /*
4643 * Transform from btree to extents, give it cur.
4644 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004645 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004646 int tmp_logflags = 0;
4647
Dave Chinner29c8d172011-09-18 20:41:00 +00004648 ASSERT(bma.cur);
4649 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004651 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 if (error)
4653 goto error0;
4654 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004655
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004657 XFS_IFORK_NEXTENTS(ip, whichfork) >
4658 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660error0:
4661 /*
4662 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004663 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004665 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004667 bma.logflags &= ~xfs_ilog_fext(whichfork);
4668 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004670 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 /*
4672 * Log whatever the flags say, even if error. Otherwise we might miss
4673 * detecting a case where the data is changed, there's an error,
4674 * and it's not logged so we don't shutdown when we should.
4675 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004676 if (bma.logflags)
4677 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004678
Dave Chinner29c8d172011-09-18 20:41:00 +00004679 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 if (!error) {
4681 ASSERT(*firstblock == NULLFSBLOCK ||
4682 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4683 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004684 bma.cur->bc_private.b.firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004685 (dfops->dop_low &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 XFS_FSB_TO_AGNO(mp, *firstblock) <
4687 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004688 bma.cur->bc_private.b.firstblock)));
4689 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004691 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4693 }
4694 if (!error)
4695 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4696 orig_nmap, *nmap);
4697 return error;
4698}
4699
4700/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004701 * When a delalloc extent is split (e.g., due to a hole punch), the original
4702 * indlen reservation must be shared across the two new extents that are left
4703 * behind.
4704 *
4705 * Given the original reservation and the worst case indlen for the two new
4706 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004707 * reservation fairly across the two new extents. If necessary, steal available
4708 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4709 * ores == 1). The number of stolen blocks is returned. The availability and
4710 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004711 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004712static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004713xfs_bmap_split_indlen(
4714 xfs_filblks_t ores, /* original res. */
4715 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004716 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4717 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004718{
4719 xfs_filblks_t len1 = *indlen1;
4720 xfs_filblks_t len2 = *indlen2;
4721 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004722 xfs_filblks_t stolen = 0;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004723
4724 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004725 * Steal as many blocks as we can to try and satisfy the worst case
4726 * indlen for both new extents.
4727 */
4728 while (nres > ores && avail) {
4729 nres--;
4730 avail--;
4731 stolen++;
4732 }
4733
4734 /*
4735 * The only blocks available are those reserved for the original
4736 * extent and what we can steal from the extent being removed.
4737 * If this still isn't enough to satisfy the combined
4738 * requirements for the two new extents, skim blocks off of each
4739 * of the new reservations until they match what is available.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004740 */
4741 while (nres > ores) {
4742 if (len1) {
4743 len1--;
4744 nres--;
4745 }
4746 if (nres == ores)
4747 break;
4748 if (len2) {
4749 len2--;
4750 nres--;
4751 }
4752 }
4753
4754 *indlen1 = len1;
4755 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004756
4757 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004758}
4759
4760/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004761 * Called by xfs_bmapi to update file extent records and the btree
4762 * after removing space (or undoing a delayed allocation).
4763 */
4764STATIC int /* error */
4765xfs_bmap_del_extent(
4766 xfs_inode_t *ip, /* incore inode pointer */
4767 xfs_trans_t *tp, /* current transaction pointer */
4768 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004769 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004770 xfs_btree_cur_t *cur, /* if null, not a btree */
4771 xfs_bmbt_irec_t *del, /* data to remove from extents */
4772 int *logflagsp, /* inode logging flags */
4773 int whichfork) /* data or attr fork */
4774{
4775 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
4776 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
4777 xfs_fsblock_t del_endblock=0; /* first block past del */
4778 xfs_fileoff_t del_endoff; /* first offset past del */
4779 int delay; /* current block is delayed allocated */
4780 int do_fx; /* free extent at end of routine */
4781 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
4782 int error; /* error return value */
4783 int flags; /* inode logging flags */
4784 xfs_bmbt_irec_t got; /* current extent entry */
4785 xfs_fileoff_t got_endoff; /* first offset past got */
4786 int i; /* temp state */
4787 xfs_ifork_t *ifp; /* inode fork pointer */
4788 xfs_mount_t *mp; /* mount structure */
4789 xfs_filblks_t nblks; /* quota/sb block count */
4790 xfs_bmbt_irec_t new; /* new record to be inserted */
4791 /* REFERENCED */
4792 uint qfield; /* quota field to update */
4793 xfs_filblks_t temp; /* for indirect length calculations */
4794 xfs_filblks_t temp2; /* for indirect length calculations */
4795 int state = 0;
4796
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004797 mp = ip->i_mount;
4798 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004799
4800 if (whichfork == XFS_ATTR_FORK)
4801 state |= BMAP_ATTRFORK;
4802
Dave Chinner9e5987a2013-02-25 12:31:26 +11004803 ifp = XFS_IFORK_PTR(ip, whichfork);
4804 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4805 (uint)sizeof(xfs_bmbt_rec_t)));
4806 ASSERT(del->br_blockcount > 0);
4807 ep = xfs_iext_get_ext(ifp, *idx);
4808 xfs_bmbt_get_all(ep, &got);
4809 ASSERT(got.br_startoff <= del->br_startoff);
4810 del_endoff = del->br_startoff + del->br_blockcount;
4811 got_endoff = got.br_startoff + got.br_blockcount;
4812 ASSERT(got_endoff >= del_endoff);
4813 delay = isnullstartblock(got.br_startblock);
4814 ASSERT(isnullstartblock(del->br_startblock) == delay);
4815 flags = 0;
4816 qfield = 0;
4817 error = 0;
4818 /*
4819 * If deleting a real allocation, must free up the disk space.
4820 */
4821 if (!delay) {
4822 flags = XFS_ILOG_CORE;
4823 /*
4824 * Realtime allocation. Free it and record di_nblocks update.
4825 */
4826 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4827 xfs_fsblock_t bno;
4828 xfs_filblks_t len;
4829
4830 ASSERT(do_mod(del->br_blockcount,
4831 mp->m_sb.sb_rextsize) == 0);
4832 ASSERT(do_mod(del->br_startblock,
4833 mp->m_sb.sb_rextsize) == 0);
4834 bno = del->br_startblock;
4835 len = del->br_blockcount;
4836 do_div(bno, mp->m_sb.sb_rextsize);
4837 do_div(len, mp->m_sb.sb_rextsize);
4838 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4839 if (error)
4840 goto done;
4841 do_fx = 0;
4842 nblks = len * mp->m_sb.sb_rextsize;
4843 qfield = XFS_TRANS_DQ_RTBCOUNT;
4844 }
4845 /*
4846 * Ordinary allocation.
4847 */
4848 else {
4849 do_fx = 1;
4850 nblks = del->br_blockcount;
4851 qfield = XFS_TRANS_DQ_BCOUNT;
4852 }
4853 /*
4854 * Set up del_endblock and cur for later.
4855 */
4856 del_endblock = del->br_startblock + del->br_blockcount;
4857 if (cur) {
4858 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
4859 got.br_startblock, got.br_blockcount,
4860 &i)))
4861 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004862 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004863 }
4864 da_old = da_new = 0;
4865 } else {
4866 da_old = startblockval(got.br_startblock);
4867 da_new = 0;
4868 nblks = 0;
4869 do_fx = 0;
4870 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004871
Dave Chinner9e5987a2013-02-25 12:31:26 +11004872 /*
4873 * Set flag value to use in switch statement.
4874 * Left-contig is 2, right-contig is 1.
4875 */
4876 switch (((got.br_startoff == del->br_startoff) << 1) |
4877 (got_endoff == del_endoff)) {
4878 case 3:
4879 /*
4880 * Matches the whole extent. Delete the entry.
4881 */
4882 xfs_iext_remove(ip, *idx, 1,
4883 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
4884 --*idx;
4885 if (delay)
4886 break;
4887
4888 XFS_IFORK_NEXT_SET(ip, whichfork,
4889 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4890 flags |= XFS_ILOG_CORE;
4891 if (!cur) {
4892 flags |= xfs_ilog_fext(whichfork);
4893 break;
4894 }
4895 if ((error = xfs_btree_delete(cur, &i)))
4896 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004897 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004898 break;
4899
4900 case 2:
4901 /*
4902 * Deleting the first part of the extent.
4903 */
4904 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4905 xfs_bmbt_set_startoff(ep, del_endoff);
4906 temp = got.br_blockcount - del->br_blockcount;
4907 xfs_bmbt_set_blockcount(ep, temp);
4908 if (delay) {
4909 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4910 da_old);
4911 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4912 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4913 da_new = temp;
4914 break;
4915 }
4916 xfs_bmbt_set_startblock(ep, del_endblock);
4917 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4918 if (!cur) {
4919 flags |= xfs_ilog_fext(whichfork);
4920 break;
4921 }
4922 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
4923 got.br_blockcount - del->br_blockcount,
4924 got.br_state)))
4925 goto done;
4926 break;
4927
4928 case 1:
4929 /*
4930 * Deleting the last part of the extent.
4931 */
4932 temp = got.br_blockcount - del->br_blockcount;
4933 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4934 xfs_bmbt_set_blockcount(ep, temp);
4935 if (delay) {
4936 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4937 da_old);
4938 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4939 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4940 da_new = temp;
4941 break;
4942 }
4943 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4944 if (!cur) {
4945 flags |= xfs_ilog_fext(whichfork);
4946 break;
4947 }
4948 if ((error = xfs_bmbt_update(cur, got.br_startoff,
4949 got.br_startblock,
4950 got.br_blockcount - del->br_blockcount,
4951 got.br_state)))
4952 goto done;
4953 break;
4954
4955 case 0:
4956 /*
4957 * Deleting the middle of the extent.
4958 */
4959 temp = del->br_startoff - got.br_startoff;
4960 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4961 xfs_bmbt_set_blockcount(ep, temp);
4962 new.br_startoff = del_endoff;
4963 temp2 = got_endoff - del_endoff;
4964 new.br_blockcount = temp2;
4965 new.br_state = got.br_state;
4966 if (!delay) {
4967 new.br_startblock = del_endblock;
4968 flags |= XFS_ILOG_CORE;
4969 if (cur) {
4970 if ((error = xfs_bmbt_update(cur,
4971 got.br_startoff,
4972 got.br_startblock, temp,
4973 got.br_state)))
4974 goto done;
4975 if ((error = xfs_btree_increment(cur, 0, &i)))
4976 goto done;
4977 cur->bc_rec.b = new;
4978 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10004979 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004980 goto done;
4981 /*
4982 * If get no-space back from btree insert,
4983 * it tried a split, and we have a zero
4984 * block reservation.
4985 * Fix up our state and return the error.
4986 */
Dave Chinner24513372014-06-25 14:58:08 +10004987 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11004988 /*
4989 * Reset the cursor, don't trust
4990 * it after any insert operation.
4991 */
4992 if ((error = xfs_bmbt_lookup_eq(cur,
4993 got.br_startoff,
4994 got.br_startblock,
4995 temp, &i)))
4996 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004997 XFS_WANT_CORRUPTED_GOTO(mp,
4998 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004999 /*
5000 * Update the btree record back
5001 * to the original value.
5002 */
5003 if ((error = xfs_bmbt_update(cur,
5004 got.br_startoff,
5005 got.br_startblock,
5006 got.br_blockcount,
5007 got.br_state)))
5008 goto done;
5009 /*
5010 * Reset the extent record back
5011 * to the original value.
5012 */
5013 xfs_bmbt_set_blockcount(ep,
5014 got.br_blockcount);
5015 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005016 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005017 goto done;
5018 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005019 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005020 } else
5021 flags |= xfs_ilog_fext(whichfork);
5022 XFS_IFORK_NEXT_SET(ip, whichfork,
5023 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5024 } else {
Brian Fosterd34999c2016-03-15 11:42:47 +11005025 xfs_filblks_t stolen;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005026 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005027
5028 /*
5029 * Distribute the original indlen reservation across the
Brian Fosterd34999c2016-03-15 11:42:47 +11005030 * two new extents. Steal blocks from the deleted extent
5031 * if necessary. Stealing blocks simply fudges the
5032 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005033 */
5034 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5035 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c2016-03-15 11:42:47 +11005036 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5037 del->br_blockcount);
5038 da_new = temp + temp2 - stolen;
5039 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005040
5041 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11005042 * Set the reservation for each extent. Warn if either
5043 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005044 */
Brian Fosterd34999c2016-03-15 11:42:47 +11005045 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005046 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005047 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005048 }
5049 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5050 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5051 ++*idx;
5052 break;
5053 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005054
5055 /* remove reverse mapping */
5056 if (!delay) {
5057 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5058 if (error)
5059 goto done;
5060 }
5061
Dave Chinner9e5987a2013-02-25 12:31:26 +11005062 /*
5063 * If we need to, add to list of extents to delete.
5064 */
5065 if (do_fx)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005066 xfs_bmap_add_free(mp, dfops, del->br_startblock,
Darrick J. Wong340785c2016-08-03 11:33:42 +10005067 del->br_blockcount, NULL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005068 /*
5069 * Adjust inode # blocks in the file.
5070 */
5071 if (nblks)
5072 ip->i_d.di_nblocks -= nblks;
5073 /*
5074 * Adjust quota data.
5075 */
5076 if (qfield)
5077 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5078
5079 /*
5080 * Account for change in delayed indirect blocks.
5081 * Nothing to do for disk quota accounting here.
5082 */
5083 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005084 if (da_old > da_new)
5085 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005086done:
5087 *logflagsp = flags;
5088 return error;
5089}
5090
5091/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 * Unmap (remove) blocks from a file.
5093 * If nexts is nonzero then the number of extents to remove is limited to
5094 * that value. If not all extents in the block range can be removed then
5095 * *done is set.
5096 */
5097int /* error */
5098xfs_bunmapi(
5099 xfs_trans_t *tp, /* transaction pointer */
5100 struct xfs_inode *ip, /* incore inode */
5101 xfs_fileoff_t bno, /* starting offset to unmap */
5102 xfs_filblks_t len, /* length to unmap in file */
5103 int flags, /* misc flags */
5104 xfs_extnum_t nexts, /* number of extents max */
5105 xfs_fsblock_t *firstblock, /* first allocated block
5106 controls a.g. for allocs */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005107 struct xfs_defer_ops *dfops, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 int *done) /* set if not done yet */
5109{
5110 xfs_btree_cur_t *cur; /* bmap btree cursor */
5111 xfs_bmbt_irec_t del; /* extent being deleted */
5112 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005113 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 int error; /* error return value */
5115 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005116 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 xfs_ifork_t *ifp; /* inode fork pointer */
5118 int isrt; /* freeing in rt area */
5119 xfs_extnum_t lastx; /* last extent index used */
5120 int logflags; /* transaction logging flags */
5121 xfs_extlen_t mod; /* rt extent offset */
5122 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005123 xfs_extnum_t nextents; /* number of file extents */
5124 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 xfs_fileoff_t start; /* first file offset deleted */
5126 int tmp_logflags; /* partial logging flags */
5127 int wasdel; /* was a delayed alloc extent */
5128 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 xfs_fsblock_t sum;
5130
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005131 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5132
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5134 XFS_ATTR_FORK : XFS_DATA_FORK;
5135 ifp = XFS_IFORK_PTR(ip, whichfork);
5136 if (unlikely(
5137 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5138 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5139 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5140 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005141 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 }
5143 mp = ip->i_mount;
5144 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005145 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005146
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005147 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005148 ASSERT(len > 0);
5149 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005150
Linus Torvalds1da177e2005-04-16 15:20:36 -07005151 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5152 (error = xfs_iread_extents(tp, ip, whichfork)))
5153 return error;
5154 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5155 if (nextents == 0) {
5156 *done = 1;
5157 return 0;
5158 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005159 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005160 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 start = bno;
5162 bno = start + len - 1;
5163 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5164 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005165
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 /*
5167 * Check to see if the given block number is past the end of the
5168 * file, back up to the last block if so...
5169 */
5170 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005171 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 xfs_bmbt_get_all(ep, &got);
5173 bno = got.br_startoff + got.br_blockcount - 1;
5174 }
5175 logflags = 0;
5176 if (ifp->if_flags & XFS_IFBROOT) {
5177 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005178 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005180 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 cur->bc_private.b.flags = 0;
5182 } else
5183 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005184
5185 if (isrt) {
5186 /*
5187 * Synchronize by locking the bitmap inode.
5188 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005189 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005190 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005191 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5192 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005193 }
5194
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 extno = 0;
5196 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5197 (nexts == 0 || extno < nexts)) {
5198 /*
5199 * Is the found extent after a hole in which bno lives?
5200 * Just back up to the previous extent, if so.
5201 */
5202 if (got.br_startoff > bno) {
5203 if (--lastx < 0)
5204 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005205 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 xfs_bmbt_get_all(ep, &got);
5207 }
5208 /*
5209 * Is the last block of this extent before the range
5210 * we're supposed to delete? If so, we're done.
5211 */
5212 bno = XFS_FILEOFF_MIN(bno,
5213 got.br_startoff + got.br_blockcount - 1);
5214 if (bno < start)
5215 break;
5216 /*
5217 * Then deal with the (possibly delayed) allocated space
5218 * we found.
5219 */
5220 ASSERT(ep != NULL);
5221 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005222 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 if (got.br_startoff < start) {
5224 del.br_startoff = start;
5225 del.br_blockcount -= start - got.br_startoff;
5226 if (!wasdel)
5227 del.br_startblock += start - got.br_startoff;
5228 }
5229 if (del.br_startoff + del.br_blockcount > bno + 1)
5230 del.br_blockcount = bno + 1 - del.br_startoff;
5231 sum = del.br_startblock + del.br_blockcount;
5232 if (isrt &&
5233 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5234 /*
5235 * Realtime extent not lined up at the end.
5236 * The extent could have been split into written
5237 * and unwritten pieces, or we could just be
5238 * unmapping part of it. But we can't really
5239 * get rid of part of a realtime extent.
5240 */
5241 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005242 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243 /*
5244 * This piece is unwritten, or we're not
5245 * using unwritten extents. Skip over it.
5246 */
5247 ASSERT(bno >= mod);
5248 bno -= mod > del.br_blockcount ?
5249 del.br_blockcount : mod;
5250 if (bno < got.br_startoff) {
5251 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005252 xfs_bmbt_get_all(xfs_iext_get_ext(
5253 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 }
5255 continue;
5256 }
5257 /*
5258 * It's written, turn it unwritten.
5259 * This is better than zeroing it.
5260 */
5261 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005262 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 /*
5264 * If this spans a realtime extent boundary,
5265 * chop it back to the start of the one we end at.
5266 */
5267 if (del.br_blockcount > mod) {
5268 del.br_startoff += del.br_blockcount - mod;
5269 del.br_startblock += del.br_blockcount - mod;
5270 del.br_blockcount = mod;
5271 }
5272 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005273 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005274 &lastx, &cur, &del, firstblock, dfops,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005275 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276 if (error)
5277 goto error0;
5278 goto nodelete;
5279 }
5280 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5281 /*
5282 * Realtime extent is lined up at the end but not
5283 * at the front. We'll get rid of full extents if
5284 * we can.
5285 */
5286 mod = mp->m_sb.sb_rextsize - mod;
5287 if (del.br_blockcount > mod) {
5288 del.br_blockcount -= mod;
5289 del.br_startoff += mod;
5290 del.br_startblock += mod;
5291 } else if ((del.br_startoff == start &&
5292 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005293 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005294 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 /*
5296 * Can't make it unwritten. There isn't
5297 * a full extent here so just skip it.
5298 */
5299 ASSERT(bno >= del.br_blockcount);
5300 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005301 if (got.br_startoff > bno) {
5302 if (--lastx >= 0) {
5303 ep = xfs_iext_get_ext(ifp,
5304 lastx);
5305 xfs_bmbt_get_all(ep, &got);
5306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 }
5308 continue;
5309 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5310 /*
5311 * This one is already unwritten.
5312 * It must have a written left neighbor.
5313 * Unwrite the killed part of that one and
5314 * try again.
5315 */
5316 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005317 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5318 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005320 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 ASSERT(del.br_startblock ==
5322 prev.br_startblock + prev.br_blockcount);
5323 if (prev.br_startoff < start) {
5324 mod = start - prev.br_startoff;
5325 prev.br_blockcount -= mod;
5326 prev.br_startblock += mod;
5327 prev.br_startoff = start;
5328 }
5329 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005330 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005331 error = xfs_bmap_add_extent_unwritten_real(tp,
5332 ip, &lastx, &cur, &prev,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005333 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 if (error)
5335 goto error0;
5336 goto nodelete;
5337 } else {
5338 ASSERT(del.br_state == XFS_EXT_NORM);
5339 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005340 error = xfs_bmap_add_extent_unwritten_real(tp,
5341 ip, &lastx, &cur, &del,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005342 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 if (error)
5344 goto error0;
5345 goto nodelete;
5346 }
5347 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005348
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 /*
5350 * If it's the case where the directory code is running
5351 * with no block reservation, and the deleted block is in
5352 * the middle of its extent, and the resulting insert
5353 * of an extent would cause transformation to btree format,
5354 * then reject it. The calling code will then swap
5355 * blocks around instead.
5356 * We have to do this now, rather than waiting for the
5357 * conversion to btree format, since the transaction
5358 * will be dirty.
5359 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005360 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005362 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5363 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 del.br_startoff > got.br_startoff &&
5365 del.br_startoff + del.br_blockcount <
5366 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005367 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 goto error0;
5369 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005370
5371 /*
5372 * Unreserve quota and update realtime free space, if
5373 * appropriate. If delayed allocation, update the inode delalloc
5374 * counter now and wait to update the sb counters as
5375 * xfs_bmap_del_extent() might need to borrow some blocks.
5376 */
5377 if (wasdel) {
5378 ASSERT(startblockval(del.br_startblock) > 0);
5379 if (isrt) {
5380 xfs_filblks_t rtexts;
5381
5382 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5383 do_div(rtexts, mp->m_sb.sb_rextsize);
5384 xfs_mod_frextents(mp, (int64_t)rtexts);
5385 (void)xfs_trans_reserve_quota_nblks(NULL,
5386 ip, -((long)del.br_blockcount), 0,
5387 XFS_QMOPT_RES_RTBLKS);
5388 } else {
5389 (void)xfs_trans_reserve_quota_nblks(NULL,
5390 ip, -((long)del.br_blockcount), 0,
5391 XFS_QMOPT_RES_REGBLKS);
5392 }
5393 ip->i_delayed_blks -= del.br_blockcount;
5394 if (cur)
5395 cur->bc_private.b.flags |=
5396 XFS_BTCUR_BPRV_WASDEL;
5397 } else if (cur)
5398 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5399
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005400 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005401 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 logflags |= tmp_logflags;
5403 if (error)
5404 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005405
5406 if (!isrt && wasdel)
5407 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5408
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 bno = del.br_startoff - 1;
5410nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 /*
5412 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005415 if (lastx >= 0) {
5416 ep = xfs_iext_get_ext(ifp, lastx);
5417 if (xfs_bmbt_get_startoff(ep) > bno) {
5418 if (--lastx >= 0)
5419 ep = xfs_iext_get_ext(ifp,
5420 lastx);
5421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 extno++;
5425 }
5426 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005428
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 /*
5430 * Convert to a btree if necessary.
5431 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005432 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005434 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 &cur, 0, &tmp_logflags, whichfork);
5436 logflags |= tmp_logflags;
5437 if (error)
5438 goto error0;
5439 }
5440 /*
5441 * transform from btree to extents, give it cur
5442 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005443 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 ASSERT(cur != NULL);
5445 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5446 whichfork);
5447 logflags |= tmp_logflags;
5448 if (error)
5449 goto error0;
5450 }
5451 /*
5452 * transform from extents to local?
5453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 error = 0;
5455error0:
5456 /*
5457 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005458 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005460 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005461 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005462 logflags &= ~xfs_ilog_fext(whichfork);
5463 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005464 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005465 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005466 /*
5467 * Log inode even in the error case, if the transaction
5468 * is dirty we'll need to shut down the filesystem.
5469 */
5470 if (logflags)
5471 xfs_trans_log_inode(tp, ip, logflags);
5472 if (cur) {
5473 if (!error) {
5474 *firstblock = cur->bc_private.b.firstblock;
5475 cur->bc_private.b.allocated = 0;
5476 }
5477 xfs_btree_del_cursor(cur,
5478 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5479 }
5480 return error;
5481}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005482
5483/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005484 * Determine whether an extent shift can be accomplished by a merge with the
5485 * extent that precedes the target hole of the shift.
5486 */
5487STATIC bool
5488xfs_bmse_can_merge(
5489 struct xfs_bmbt_irec *left, /* preceding extent */
5490 struct xfs_bmbt_irec *got, /* current extent to shift */
5491 xfs_fileoff_t shift) /* shift fsb */
5492{
5493 xfs_fileoff_t startoff;
5494
5495 startoff = got->br_startoff - shift;
5496
5497 /*
5498 * The extent, once shifted, must be adjacent in-file and on-disk with
5499 * the preceding extent.
5500 */
5501 if ((left->br_startoff + left->br_blockcount != startoff) ||
5502 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5503 (left->br_state != got->br_state) ||
5504 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5505 return false;
5506
5507 return true;
5508}
5509
5510/*
5511 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5512 * hole in the file. If an extent shift would result in the extent being fully
5513 * adjacent to the extent that currently precedes the hole, we can merge with
5514 * the preceding extent rather than do the shift.
5515 *
5516 * This function assumes the caller has verified a shift-by-merge is possible
5517 * with the provided extents via xfs_bmse_can_merge().
5518 */
5519STATIC int
5520xfs_bmse_merge(
5521 struct xfs_inode *ip,
5522 int whichfork,
5523 xfs_fileoff_t shift, /* shift fsb */
5524 int current_ext, /* idx of gotp */
5525 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5526 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5527 struct xfs_btree_cur *cur,
5528 int *logflags) /* output */
5529{
Brian Fosterddb19e32014-09-23 15:38:09 +10005530 struct xfs_bmbt_irec got;
5531 struct xfs_bmbt_irec left;
5532 xfs_filblks_t blockcount;
5533 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005534 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005535
Brian Fosterddb19e32014-09-23 15:38:09 +10005536 xfs_bmbt_get_all(gotp, &got);
5537 xfs_bmbt_get_all(leftp, &left);
5538 blockcount = left.br_blockcount + got.br_blockcount;
5539
5540 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5541 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5542 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5543
5544 /*
5545 * Merge the in-core extents. Note that the host record pointers and
5546 * current_ext index are invalid once the extent has been removed via
5547 * xfs_iext_remove().
5548 */
5549 xfs_bmbt_set_blockcount(leftp, blockcount);
5550 xfs_iext_remove(ip, current_ext, 1, 0);
5551
5552 /*
5553 * Update the on-disk extent count, the btree if necessary and log the
5554 * inode.
5555 */
5556 XFS_IFORK_NEXT_SET(ip, whichfork,
5557 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5558 *logflags |= XFS_ILOG_CORE;
5559 if (!cur) {
5560 *logflags |= XFS_ILOG_DEXT;
5561 return 0;
5562 }
5563
5564 /* lookup and remove the extent to merge */
5565 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5566 got.br_blockcount, &i);
5567 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005568 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005569 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005570
5571 error = xfs_btree_delete(cur, &i);
5572 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005573 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005574 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005575
5576 /* lookup and update size of the previous extent */
5577 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5578 left.br_blockcount, &i);
5579 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005580 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005581 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005582
5583 left.br_blockcount = blockcount;
5584
Dave Chinner4db431f2014-12-04 09:42:40 +11005585 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5586 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005587}
5588
5589/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005590 * Shift a single extent.
5591 */
5592STATIC int
5593xfs_bmse_shift_one(
5594 struct xfs_inode *ip,
5595 int whichfork,
5596 xfs_fileoff_t offset_shift_fsb,
5597 int *current_ext,
5598 struct xfs_bmbt_rec_host *gotp,
5599 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005600 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005601 enum shift_direction direction,
5602 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005603{
5604 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005605 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005606 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005607 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005608 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005609 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005610 int error;
5611 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005612 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005613
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005614 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005615 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005616 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Brian Fostera979bdf2014-09-23 15:39:04 +10005617
5618 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005619
Brian Fosterf71721d2014-09-23 15:39:05 +10005620 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005621 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005622
Namjae Jeona904b1c2015-03-25 15:08:56 +11005623 if (direction == SHIFT_LEFT) {
5624 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005625
Namjae Jeona904b1c2015-03-25 15:08:56 +11005626 /*
5627 * Check for merge if we've got an extent to the left,
5628 * otherwise make sure there's enough room at the start
5629 * of the file for the shift.
5630 */
5631 if (!*current_ext) {
5632 if (got.br_startoff < offset_shift_fsb)
5633 return -EINVAL;
5634 goto update_current_ext;
5635 }
5636 /*
5637 * grab the left extent and check for a large
5638 * enough hole.
5639 */
5640 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5641 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5642
5643 if (startoff <
5644 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005645 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005646
Dave Chinnerb11bd672014-12-04 09:42:24 +11005647 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005648 if (xfs_bmse_can_merge(&adj_irec, &got,
5649 offset_shift_fsb)) {
Darrick J. Wong9c194642016-08-03 12:16:05 +10005650 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5651 *current_ext, gotp, adj_irecp,
5652 cur, logflags);
5653 if (error)
5654 return error;
5655 adj_irec = got;
5656 goto update_rmap;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005657 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005658 } else {
5659 startoff = got.br_startoff + offset_shift_fsb;
5660 /* nothing to move if this is the last extent */
5661 if (*current_ext >= (total_extents - 1))
5662 goto update_current_ext;
5663 /*
5664 * If this is not the last extent in the file, make sure there
5665 * is enough room between current extent and next extent for
5666 * accommodating the shift.
5667 */
5668 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5669 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5670 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5671 return -EINVAL;
5672 /*
5673 * Unlike a left shift (which involves a hole punch),
5674 * a right shift does not modify extent neighbors
5675 * in any way. We should never find mergeable extents
5676 * in this scenario. Check anyways and warn if we
5677 * encounter two extents that could be one.
5678 */
5679 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5680 WARN_ON_ONCE(1);
5681 }
Brian Fostera979bdf2014-09-23 15:39:04 +10005682 /*
5683 * Increment the extent index for the next iteration, update the start
5684 * offset of the in-core extent and update the btree if applicable.
5685 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005686update_current_ext:
5687 if (direction == SHIFT_LEFT)
5688 (*current_ext)++;
5689 else
5690 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005691 xfs_bmbt_set_startoff(gotp, startoff);
5692 *logflags |= XFS_ILOG_CORE;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005693 adj_irec = got;
Brian Fostera979bdf2014-09-23 15:39:04 +10005694 if (!cur) {
5695 *logflags |= XFS_ILOG_DEXT;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005696 goto update_rmap;
Brian Fostera979bdf2014-09-23 15:39:04 +10005697 }
5698
5699 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5700 got.br_blockcount, &i);
5701 if (error)
5702 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005703 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10005704
5705 got.br_startoff = startoff;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005706 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5707 got.br_blockcount, got.br_state);
5708 if (error)
5709 return error;
5710
5711update_rmap:
5712 /* update reverse mapping */
5713 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
5714 if (error)
5715 return error;
5716 adj_irec.br_startoff = startoff;
5717 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
Brian Fostera979bdf2014-09-23 15:39:04 +10005718}
5719
5720/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005721 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005722 *
Brian Foster2c845f52014-09-23 15:37:09 +10005723 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005724 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10005725 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5726 * is the length by which each extent is shifted. If there is no hole to shift
5727 * the extents into, this will be considered invalid operation and we abort
5728 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005729 */
5730int
5731xfs_bmap_shift_extents(
5732 struct xfs_trans *tp,
5733 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005734 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005735 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005736 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005737 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005738 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005739 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005740 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005741 int num_exts)
5742{
Brian Fosterca446d82014-09-02 12:12:53 +10005743 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005744 struct xfs_bmbt_rec_host *gotp;
5745 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005746 struct xfs_mount *mp = ip->i_mount;
5747 struct xfs_ifork *ifp;
5748 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005749 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005750 xfs_extnum_t total_extents;
5751 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005752 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005753 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10005754 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005755
5756 if (unlikely(XFS_TEST_ERROR(
5757 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5758 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5759 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5760 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5761 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005762 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005763 }
5764
5765 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005766 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005767
Brian Foster2c845f52014-09-23 15:37:09 +10005768 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5769 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005770 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
5771 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005772
5773 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005774 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5775 /* Read in all the extents */
5776 error = xfs_iread_extents(tp, ip, whichfork);
5777 if (error)
5778 return error;
5779 }
5780
Brian Fosterddb19e32014-09-23 15:38:09 +10005781 if (ifp->if_flags & XFS_IFBROOT) {
5782 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5783 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005784 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005785 cur->bc_private.b.flags = 0;
5786 }
5787
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005788 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005789 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10005790 * are collapsing out, so we cannot use the count of real extents here.
5791 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005792 */
5793 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005794 if (total_extents == 0) {
5795 *done = 1;
5796 goto del_cursor;
5797 }
5798
5799 /*
5800 * In case of first right shift, we need to initialize next_fsb
5801 */
5802 if (*next_fsb == NULLFSBLOCK) {
5803 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
5804 xfs_bmbt_get_all(gotp, &got);
5805 *next_fsb = got.br_startoff;
5806 if (stop_fsb > *next_fsb) {
5807 *done = 1;
5808 goto del_cursor;
5809 }
5810 }
5811
5812 /* Lookup the extent index at which we have to stop */
5813 if (direction == SHIFT_RIGHT) {
5814 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
5815 /* Make stop_extent exclusive of shift range */
5816 stop_extent--;
5817 } else
5818 stop_extent = total_extents;
5819
5820 /*
5821 * Look up the extent index for the fsb where we start shifting. We can
5822 * henceforth iterate with current_ext as extent list changes are locked
5823 * out via ilock.
5824 *
5825 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5826 * *next_fsb lies in a hole beyond which there are no extents. Either
5827 * way, we are done.
5828 */
5829 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
5830 if (!gotp) {
5831 *done = 1;
5832 goto del_cursor;
5833 }
5834
5835 /* some sanity checking before we finally start shifting extents */
5836 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
5837 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
5838 error = -EIO;
5839 goto del_cursor;
5840 }
5841
5842 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005843 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005844 &current_ext, gotp, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005845 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10005846 if (error)
5847 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005848 /*
5849 * If there was an extent merge during the shift, the extent
5850 * count can change. Update the total and grade the next record.
5851 */
5852 if (direction == SHIFT_LEFT) {
5853 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5854 stop_extent = total_extents;
5855 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005856
Namjae Jeona904b1c2015-03-25 15:08:56 +11005857 if (current_ext == stop_extent) {
5858 *done = 1;
5859 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10005860 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005861 }
Brian Fosterddb19e32014-09-23 15:38:09 +10005862 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005863 }
5864
Namjae Jeona904b1c2015-03-25 15:08:56 +11005865 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005866 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10005867 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10005868 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005869
5870del_cursor:
5871 if (cur)
5872 xfs_btree_del_cursor(cur,
5873 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5874
Brian Fosterca446d82014-09-02 12:12:53 +10005875 if (logflags)
5876 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10005877
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005878 return error;
5879}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005880
5881/*
5882 * Splits an extent into two extents at split_fsb block such that it is
5883 * the first block of the current_ext. @current_ext is a target extent
5884 * to be split. @split_fsb is a block where the extents is split.
5885 * If split_fsb lies in a hole or the first block of extents, just return 0.
5886 */
5887STATIC int
5888xfs_bmap_split_extent_at(
5889 struct xfs_trans *tp,
5890 struct xfs_inode *ip,
5891 xfs_fileoff_t split_fsb,
5892 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005893 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005894{
5895 int whichfork = XFS_DATA_FORK;
5896 struct xfs_btree_cur *cur = NULL;
5897 struct xfs_bmbt_rec_host *gotp;
5898 struct xfs_bmbt_irec got;
5899 struct xfs_bmbt_irec new; /* split extent */
5900 struct xfs_mount *mp = ip->i_mount;
5901 struct xfs_ifork *ifp;
5902 xfs_fsblock_t gotblkcnt; /* new block count for got */
5903 xfs_extnum_t current_ext;
5904 int error = 0;
5905 int logflags = 0;
5906 int i = 0;
5907
5908 if (unlikely(XFS_TEST_ERROR(
5909 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5910 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5911 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5912 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5913 XFS_ERRLEVEL_LOW, mp);
5914 return -EFSCORRUPTED;
5915 }
5916
5917 if (XFS_FORCED_SHUTDOWN(mp))
5918 return -EIO;
5919
5920 ifp = XFS_IFORK_PTR(ip, whichfork);
5921 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5922 /* Read in all the extents */
5923 error = xfs_iread_extents(tp, ip, whichfork);
5924 if (error)
5925 return error;
5926 }
5927
5928 /*
5929 * gotp can be null in 2 cases: 1) if there are no extents
5930 * or 2) split_fsb lies in a hole beyond which there are
5931 * no extents. Either way, we are done.
5932 */
5933 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
5934 if (!gotp)
5935 return 0;
5936
5937 xfs_bmbt_get_all(gotp, &got);
5938
5939 /*
5940 * Check split_fsb lies in a hole or the start boundary offset
5941 * of the extent.
5942 */
5943 if (got.br_startoff >= split_fsb)
5944 return 0;
5945
5946 gotblkcnt = split_fsb - got.br_startoff;
5947 new.br_startoff = split_fsb;
5948 new.br_startblock = got.br_startblock + gotblkcnt;
5949 new.br_blockcount = got.br_blockcount - gotblkcnt;
5950 new.br_state = got.br_state;
5951
5952 if (ifp->if_flags & XFS_IFBROOT) {
5953 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5954 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005955 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005956 cur->bc_private.b.flags = 0;
5957 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5958 got.br_startblock,
5959 got.br_blockcount,
5960 &i);
5961 if (error)
5962 goto del_cursor;
5963 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5964 }
5965
5966 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
5967 got.br_blockcount = gotblkcnt;
5968
5969 logflags = XFS_ILOG_CORE;
5970 if (cur) {
5971 error = xfs_bmbt_update(cur, got.br_startoff,
5972 got.br_startblock,
5973 got.br_blockcount,
5974 got.br_state);
5975 if (error)
5976 goto del_cursor;
5977 } else
5978 logflags |= XFS_ILOG_DEXT;
5979
5980 /* Add new extent */
5981 current_ext++;
5982 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5983 XFS_IFORK_NEXT_SET(ip, whichfork,
5984 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5985
5986 if (cur) {
5987 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
5988 new.br_startblock, new.br_blockcount,
5989 &i);
5990 if (error)
5991 goto del_cursor;
5992 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
5993 cur->bc_rec.b.br_state = new.br_state;
5994
5995 error = xfs_btree_insert(cur, &i);
5996 if (error)
5997 goto del_cursor;
5998 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5999 }
6000
6001 /*
6002 * Convert to a btree if necessary.
6003 */
6004 if (xfs_bmap_needs_btree(ip, whichfork)) {
6005 int tmp_logflags; /* partial log flag return val */
6006
6007 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006008 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006009 &cur, 0, &tmp_logflags, whichfork);
6010 logflags |= tmp_logflags;
6011 }
6012
6013del_cursor:
6014 if (cur) {
6015 cur->bc_private.b.allocated = 0;
6016 xfs_btree_del_cursor(cur,
6017 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6018 }
6019
6020 if (logflags)
6021 xfs_trans_log_inode(tp, ip, logflags);
6022 return error;
6023}
6024
6025int
6026xfs_bmap_split_extent(
6027 struct xfs_inode *ip,
6028 xfs_fileoff_t split_fsb)
6029{
6030 struct xfs_mount *mp = ip->i_mount;
6031 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006032 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006033 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006034 int error;
6035
Christoph Hellwig253f4912016-04-06 09:19:55 +10006036 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6037 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6038 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006039 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006040
6041 xfs_ilock(ip, XFS_ILOCK_EXCL);
6042 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6043
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006044 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006045
6046 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006047 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006048 if (error)
6049 goto out;
6050
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006051 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006052 if (error)
6053 goto out;
6054
Christoph Hellwig70393312015-06-04 13:48:08 +10006055 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006056
6057out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006058 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006059 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006060 return error;
6061}