blob: 540a6b7928af22bafb6d063875d64c0f32e1022a [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051kmem_zone_t *xfs_bmap_free_item_zone;
52
53/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110054 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110058 * Compute and fill in the value of the maximum depth of a bmap btree
59 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110061void
62xfs_bmap_compute_maxlevels(
63 xfs_mount_t *mp, /* file system mount structure */
64 int whichfork) /* data or attr fork */
65{
66 int level; /* btree level */
67 uint maxblocks; /* max blocks at this level */
68 uint maxleafents; /* max leaf entries possible */
69 int maxrootrecs; /* max records in root block */
70 int minleafrecs; /* min records in leaf block */
71 int minnoderecs; /* min records in node block */
72 int sz; /* root block size */
73
74 /*
75 * The maximum number of extents in a file, hence the maximum
76 * number of leaf entries, is controlled by the type of di_nextents
77 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
78 * (a signed 16-bit number, xfs_aextnum_t).
79 *
80 * Note that we can no longer assume that if we are in ATTR1 that
81 * the fork offset of all the inodes will be
82 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
83 * with ATTR2 and then mounted back with ATTR1, keeping the
84 * di_forkoff's fixed but probably at various positions. Therefore,
85 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
86 * of a minimum size available.
87 */
88 if (whichfork == XFS_DATA_FORK) {
89 maxleafents = MAXEXTNUM;
90 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
91 } else {
92 maxleafents = MAXAEXTNUM;
93 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
94 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100095 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +110096 minleafrecs = mp->m_bmap_dmnr[0];
97 minnoderecs = mp->m_bmap_dmnr[1];
98 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
99 for (level = 1; maxblocks > 1; level++) {
100 if (maxblocks <= maxrootrecs)
101 maxblocks = 1;
102 else
103 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
104 }
105 mp->m_bm_maxlevels[whichfork] = level;
106}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100108STATIC int /* error */
109xfs_bmbt_lookup_eq(
110 struct xfs_btree_cur *cur,
111 xfs_fileoff_t off,
112 xfs_fsblock_t bno,
113 xfs_filblks_t len,
114 int *stat) /* success/failure */
115{
116 cur->bc_rec.b.br_startoff = off;
117 cur->bc_rec.b.br_startblock = bno;
118 cur->bc_rec.b.br_blockcount = len;
119 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
120}
121
122STATIC int /* error */
123xfs_bmbt_lookup_ge(
124 struct xfs_btree_cur *cur,
125 xfs_fileoff_t off,
126 xfs_fsblock_t bno,
127 xfs_filblks_t len,
128 int *stat) /* success/failure */
129{
130 cur->bc_rec.b.br_startoff = off;
131 cur->bc_rec.b.br_startblock = bno;
132 cur->bc_rec.b.br_blockcount = len;
133 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
134}
135
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100136/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000137 * Check if the inode needs to be converted to btree format.
138 */
139static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
140{
141 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
142 XFS_IFORK_NEXTENTS(ip, whichfork) >
143 XFS_IFORK_MAXEXT(ip, whichfork);
144}
145
146/*
147 * Check if the inode should be converted to extent format.
148 */
149static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
150{
151 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
152 XFS_IFORK_NEXTENTS(ip, whichfork) <=
153 XFS_IFORK_MAXEXT(ip, whichfork);
154}
155
156/*
157 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100158 * by [off, bno, len, state].
159 * This either works (return 0) or gets an EFSCORRUPTED error.
160 */
161STATIC int
162xfs_bmbt_update(
163 struct xfs_btree_cur *cur,
164 xfs_fileoff_t off,
165 xfs_fsblock_t bno,
166 xfs_filblks_t len,
167 xfs_exntst_t state)
168{
169 union xfs_btree_rec rec;
170
171 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
172 return xfs_btree_update(cur, &rec);
173}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100176 * Compute the worst-case number of indirect blocks that will be used
177 * for ip's delayed extent of length "len".
178 */
179STATIC xfs_filblks_t
180xfs_bmap_worst_indlen(
181 xfs_inode_t *ip, /* incore inode pointer */
182 xfs_filblks_t len) /* delayed extent length */
183{
184 int level; /* btree level number */
185 int maxrecs; /* maximum record count at this level */
186 xfs_mount_t *mp; /* mount structure */
187 xfs_filblks_t rval; /* return value */
188
189 mp = ip->i_mount;
190 maxrecs = mp->m_bmap_dmxr[0];
191 for (level = 0, rval = 0;
192 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
193 level++) {
194 len += maxrecs - 1;
195 do_div(len, maxrecs);
196 rval += len;
197 if (len == 1)
198 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
199 level - 1;
200 if (level == 0)
201 maxrecs = mp->m_bmap_dmxr[1];
202 }
203 return rval;
204}
205
206/*
207 * Calculate the default attribute fork offset for newly created inodes.
208 */
209uint
210xfs_default_attroffset(
211 struct xfs_inode *ip)
212{
213 struct xfs_mount *mp = ip->i_mount;
214 uint offset;
215
216 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100217 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100218 XFS_BMDR_SPACE_CALC(MINABTPTRS);
219 } else {
220 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
221 }
222
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100223 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100224 return offset;
225}
226
227/*
228 * Helper routine to reset inode di_forkoff field when switching
229 * attribute fork from local to extent format - we reset it where
230 * possible to make space available for inline data fork extents.
231 */
232STATIC void
233xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100234 xfs_inode_t *ip,
235 int whichfork)
236{
237 if (whichfork == XFS_ATTR_FORK &&
238 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
239 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
240 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
241 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
242
243 if (dfl_forkoff > ip->i_d.di_forkoff)
244 ip->i_d.di_forkoff = dfl_forkoff;
245 }
246}
247
Dave Chinner9e5987a2013-02-25 12:31:26 +1100248#ifdef DEBUG
249STATIC struct xfs_buf *
250xfs_bmap_get_bp(
251 struct xfs_btree_cur *cur,
252 xfs_fsblock_t bno)
253{
254 struct xfs_log_item_desc *lidp;
255 int i;
256
257 if (!cur)
258 return NULL;
259
260 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
261 if (!cur->bc_bufs[i])
262 break;
263 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
264 return cur->bc_bufs[i];
265 }
266
267 /* Chase down all the log items to see if the bp is there */
268 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
269 struct xfs_buf_log_item *bip;
270 bip = (struct xfs_buf_log_item *)lidp->lid_item;
271 if (bip->bli_item.li_type == XFS_LI_BUF &&
272 XFS_BUF_ADDR(bip->bli_buf) == bno)
273 return bip->bli_buf;
274 }
275
276 return NULL;
277}
278
279STATIC void
280xfs_check_block(
281 struct xfs_btree_block *block,
282 xfs_mount_t *mp,
283 int root,
284 short sz)
285{
286 int i, j, dmxr;
287 __be64 *pp, *thispa; /* pointer to block address */
288 xfs_bmbt_key_t *prevp, *keyp;
289
290 ASSERT(be16_to_cpu(block->bb_level) > 0);
291
292 prevp = NULL;
293 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
294 dmxr = mp->m_bmap_dmxr[0];
295 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
296
297 if (prevp) {
298 ASSERT(be64_to_cpu(prevp->br_startoff) <
299 be64_to_cpu(keyp->br_startoff));
300 }
301 prevp = keyp;
302
303 /*
304 * Compare the block numbers to see if there are dups.
305 */
306 if (root)
307 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
308 else
309 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
310
311 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
312 if (root)
313 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
314 else
315 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
316 if (*thispa == *pp) {
317 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
318 __func__, j, i,
319 (unsigned long long)be64_to_cpu(*thispa));
320 panic("%s: ptrs are equal in node\n",
321 __func__);
322 }
323 }
324 }
325}
326
327/*
328 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100329 * btree leaves. THis becomes prohibitively expensive for large extent count
330 * files, so don't bother with inodes that have more than 10,000 extents in
331 * them. The btree record ordering checks will still be done, so for such large
332 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100333 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100334STATIC void
335xfs_bmap_check_leaf_extents(
336 xfs_btree_cur_t *cur, /* btree cursor or null */
337 xfs_inode_t *ip, /* incore inode pointer */
338 int whichfork) /* data or attr fork */
339{
340 struct xfs_btree_block *block; /* current btree block */
341 xfs_fsblock_t bno; /* block # of "block" */
342 xfs_buf_t *bp; /* buffer for "block" */
343 int error; /* error return value */
344 xfs_extnum_t i=0, j; /* index into the extents list */
345 xfs_ifork_t *ifp; /* fork structure */
346 int level; /* btree level, for checking */
347 xfs_mount_t *mp; /* file system mount structure */
348 __be64 *pp; /* pointer to block address */
349 xfs_bmbt_rec_t *ep; /* pointer to current extent */
350 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
351 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
352 int bp_release = 0;
353
354 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
355 return;
356 }
357
Dave Chinnere3543812016-01-08 11:28:49 +1100358 /* skip large extent count inodes */
359 if (ip->i_d.di_nextents > 10000)
360 return;
361
Dave Chinner9e5987a2013-02-25 12:31:26 +1100362 bno = NULLFSBLOCK;
363 mp = ip->i_mount;
364 ifp = XFS_IFORK_PTR(ip, whichfork);
365 block = ifp->if_broot;
366 /*
367 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
368 */
369 level = be16_to_cpu(block->bb_level);
370 ASSERT(level > 0);
371 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
372 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
373 bno = be64_to_cpu(*pp);
374
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000375 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100376 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
377 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
378
379 /*
380 * Go down the tree until leaf level is reached, following the first
381 * pointer (leftmost) at each level.
382 */
383 while (level-- > 0) {
384 /* See if buf is in cur first */
385 bp_release = 0;
386 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
387 if (!bp) {
388 bp_release = 1;
389 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
390 XFS_BMAP_BTREE_REF,
391 &xfs_bmbt_buf_ops);
392 if (error)
393 goto error_norelse;
394 }
395 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100396 if (level == 0)
397 break;
398
399 /*
400 * Check this block for basic sanity (increasing keys and
401 * no duplicate blocks).
402 */
403
404 xfs_check_block(block, mp, 0, 0);
405 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
406 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100407 XFS_WANT_CORRUPTED_GOTO(mp,
408 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100409 if (bp_release) {
410 bp_release = 0;
411 xfs_trans_brelse(NULL, bp);
412 }
413 }
414
415 /*
416 * Here with bp and block set to the leftmost leaf node in the tree.
417 */
418 i = 0;
419
420 /*
421 * Loop over all leaf nodes checking that all extents are in the right order.
422 */
423 for (;;) {
424 xfs_fsblock_t nextbno;
425 xfs_extnum_t num_recs;
426
427
428 num_recs = xfs_btree_get_numrecs(block);
429
430 /*
431 * Read-ahead the next leaf block, if any.
432 */
433
434 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
435
436 /*
437 * Check all the extents to make sure they are OK.
438 * If we had a previous block, the last entry should
439 * conform with the first entry in this one.
440 */
441
442 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
443 if (i) {
444 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
445 xfs_bmbt_disk_get_blockcount(&last) <=
446 xfs_bmbt_disk_get_startoff(ep));
447 }
448 for (j = 1; j < num_recs; j++) {
449 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
450 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
451 xfs_bmbt_disk_get_blockcount(ep) <=
452 xfs_bmbt_disk_get_startoff(nextp));
453 ep = nextp;
454 }
455
456 last = *ep;
457 i += num_recs;
458 if (bp_release) {
459 bp_release = 0;
460 xfs_trans_brelse(NULL, bp);
461 }
462 bno = nextbno;
463 /*
464 * If we've reached the end, stop.
465 */
466 if (bno == NULLFSBLOCK)
467 break;
468
469 bp_release = 0;
470 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
471 if (!bp) {
472 bp_release = 1;
473 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
474 XFS_BMAP_BTREE_REF,
475 &xfs_bmbt_buf_ops);
476 if (error)
477 goto error_norelse;
478 }
479 block = XFS_BUF_TO_BLOCK(bp);
480 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100481
Dave Chinner9e5987a2013-02-25 12:31:26 +1100482 return;
483
484error0:
485 xfs_warn(mp, "%s: at error0", __func__);
486 if (bp_release)
487 xfs_trans_brelse(NULL, bp);
488error_norelse:
489 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
490 __func__, i);
491 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
492 return;
493}
494
495/*
496 * Add bmap trace insert entries for all the contents of the extent records.
497 */
498void
499xfs_bmap_trace_exlist(
500 xfs_inode_t *ip, /* incore inode pointer */
501 xfs_extnum_t cnt, /* count of entries in the list */
502 int whichfork, /* data or attr fork */
503 unsigned long caller_ip)
504{
505 xfs_extnum_t idx; /* extent record index */
506 xfs_ifork_t *ifp; /* inode fork pointer */
507 int state = 0;
508
509 if (whichfork == XFS_ATTR_FORK)
510 state |= BMAP_ATTRFORK;
511
512 ifp = XFS_IFORK_PTR(ip, whichfork);
513 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
514 for (idx = 0; idx < cnt; idx++)
515 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
516}
517
518/*
519 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000520 * given the caller's original parameters. Specifically check the
521 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100522 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
523 */
524STATIC void
525xfs_bmap_validate_ret(
526 xfs_fileoff_t bno,
527 xfs_filblks_t len,
528 int flags,
529 xfs_bmbt_irec_t *mval,
530 int nmap,
531 int ret_nmap)
532{
533 int i; /* index to map values */
534
535 ASSERT(ret_nmap <= nmap);
536
537 for (i = 0; i < ret_nmap; i++) {
538 ASSERT(mval[i].br_blockcount > 0);
539 if (!(flags & XFS_BMAPI_ENTIRE)) {
540 ASSERT(mval[i].br_startoff >= bno);
541 ASSERT(mval[i].br_blockcount <= len);
542 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
543 bno + len);
544 } else {
545 ASSERT(mval[i].br_startoff < bno + len);
546 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
547 bno);
548 }
549 ASSERT(i == 0 ||
550 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
551 mval[i].br_startoff);
552 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
553 mval[i].br_startblock != HOLESTARTBLOCK);
554 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
555 mval[i].br_state == XFS_EXT_UNWRITTEN);
556 }
557}
558
559#else
560#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
561#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
562#endif /* DEBUG */
563
564/*
565 * bmap free list manipulation functions
566 */
567
568/*
569 * Add the extent to the list of extents to be free at transaction end.
570 * The list is maintained sorted (by block number).
571 */
572void
573xfs_bmap_add_free(
Darrick J. Wong59bad072016-06-21 11:53:28 +1000574 struct xfs_mount *mp, /* mount point structure */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000575 struct xfs_defer_ops *dfops, /* list of extents */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100576 xfs_fsblock_t bno, /* fs block number of extent */
Darrick J. Wong59bad072016-06-21 11:53:28 +1000577 xfs_filblks_t len) /* length of extent */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100578{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000579 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100580#ifdef DEBUG
581 xfs_agnumber_t agno;
582 xfs_agblock_t agbno;
583
584 ASSERT(bno != NULLFSBLOCK);
585 ASSERT(len > 0);
586 ASSERT(len <= MAXEXTLEN);
587 ASSERT(!isnullstartblock(bno));
588 agno = XFS_FSB_TO_AGNO(mp, bno);
589 agbno = XFS_FSB_TO_AGBNO(mp, bno);
590 ASSERT(agno < mp->m_sb.sb_agcount);
591 ASSERT(agbno < mp->m_sb.sb_agblocks);
592 ASSERT(len < mp->m_sb.sb_agblocks);
593 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
594#endif
595 ASSERT(xfs_bmap_free_item_zone != NULL);
596 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000597 new->xefi_startblock = bno;
598 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000599 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100600}
601
602/*
603 * Inode fork format manipulation functions
604 */
605
606/*
607 * Transform a btree format file with only one leaf node, where the
608 * extents list will fit in the inode, into an extents format file.
609 * Since the file extents are already in-core, all we have to do is
610 * give up the space for the btree root and pitch the leaf block.
611 */
612STATIC int /* error */
613xfs_bmap_btree_to_extents(
614 xfs_trans_t *tp, /* transaction pointer */
615 xfs_inode_t *ip, /* incore inode pointer */
616 xfs_btree_cur_t *cur, /* btree cursor */
617 int *logflagsp, /* inode logging flags */
618 int whichfork) /* data or attr fork */
619{
620 /* REFERENCED */
621 struct xfs_btree_block *cblock;/* child btree block */
622 xfs_fsblock_t cbno; /* child block number */
623 xfs_buf_t *cbp; /* child block's buffer */
624 int error; /* error return value */
625 xfs_ifork_t *ifp; /* inode fork data */
626 xfs_mount_t *mp; /* mount point structure */
627 __be64 *pp; /* ptr to block address */
628 struct xfs_btree_block *rblock;/* root btree block */
629
630 mp = ip->i_mount;
631 ifp = XFS_IFORK_PTR(ip, whichfork);
632 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
633 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
634 rblock = ifp->if_broot;
635 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
636 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
637 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
638 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
639 cbno = be64_to_cpu(*pp);
640 *logflagsp = 0;
641#ifdef DEBUG
642 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
643 return error;
644#endif
645 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
646 &xfs_bmbt_buf_ops);
647 if (error)
648 return error;
649 cblock = XFS_BUF_TO_BLOCK(cbp);
650 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
651 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000652 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100653 ip->i_d.di_nblocks--;
654 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
655 xfs_trans_binval(tp, cbp);
656 if (cur->bc_bufs[0] == cbp)
657 cur->bc_bufs[0] = NULL;
658 xfs_iroot_realloc(ip, -1, whichfork);
659 ASSERT(ifp->if_broot == NULL);
660 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
661 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
662 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
663 return 0;
664}
665
666/*
667 * Convert an extents-format file into a btree-format file.
668 * The new file will have a root block (in the inode) and a single child block.
669 */
670STATIC int /* error */
671xfs_bmap_extents_to_btree(
672 xfs_trans_t *tp, /* transaction pointer */
673 xfs_inode_t *ip, /* incore inode pointer */
674 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000675 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100676 xfs_btree_cur_t **curp, /* cursor returned to caller */
677 int wasdel, /* converting a delayed alloc */
678 int *logflagsp, /* inode logging flags */
679 int whichfork) /* data or attr fork */
680{
681 struct xfs_btree_block *ablock; /* allocated (child) bt block */
682 xfs_buf_t *abp; /* buffer for ablock */
683 xfs_alloc_arg_t args; /* allocation arguments */
684 xfs_bmbt_rec_t *arp; /* child record pointer */
685 struct xfs_btree_block *block; /* btree root block */
686 xfs_btree_cur_t *cur; /* bmap btree cursor */
687 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
688 int error; /* error return value */
689 xfs_extnum_t i, cnt; /* extent record index */
690 xfs_ifork_t *ifp; /* inode fork pointer */
691 xfs_bmbt_key_t *kp; /* root block key pointer */
692 xfs_mount_t *mp; /* mount structure */
693 xfs_extnum_t nextents; /* number of file extents */
694 xfs_bmbt_ptr_t *pp; /* root block address pointer */
695
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500696 mp = ip->i_mount;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100697 ifp = XFS_IFORK_PTR(ip, whichfork);
698 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
699
700 /*
701 * Make space in the inode incore.
702 */
703 xfs_iroot_realloc(ip, 1, whichfork);
704 ifp->if_flags |= XFS_IFBROOT;
705
706 /*
707 * Fill in the root.
708 */
709 block = ifp->if_broot;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500710 if (xfs_sb_version_hascrc(&mp->m_sb))
711 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
712 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
713 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
714 else
715 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
716 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
717 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100718
719 /*
720 * Need a cursor. Can't allocate until bb_level is filled in.
721 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100722 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
723 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000724 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100725 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
726 /*
727 * Convert to a btree with two levels, one record in root.
728 */
729 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
730 memset(&args, 0, sizeof(args));
731 args.tp = tp;
732 args.mp = mp;
733 args.firstblock = *firstblock;
734 if (*firstblock == NULLFSBLOCK) {
735 args.type = XFS_ALLOCTYPE_START_BNO;
736 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000737 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100738 args.type = XFS_ALLOCTYPE_START_BNO;
739 args.fsbno = *firstblock;
740 } else {
741 args.type = XFS_ALLOCTYPE_NEAR_BNO;
742 args.fsbno = *firstblock;
743 }
744 args.minlen = args.maxlen = args.prod = 1;
745 args.wasdel = wasdel;
746 *logflagsp = 0;
747 if ((error = xfs_alloc_vextent(&args))) {
748 xfs_iroot_realloc(ip, -1, whichfork);
749 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
750 return error;
751 }
752 /*
753 * Allocation can't fail, the space was reserved.
754 */
755 ASSERT(args.fsbno != NULLFSBLOCK);
756 ASSERT(*firstblock == NULLFSBLOCK ||
757 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000758 (dfops->dop_low &&
Dave Chinner9e5987a2013-02-25 12:31:26 +1100759 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
760 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
761 cur->bc_private.b.allocated++;
762 ip->i_d.di_nblocks++;
763 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
764 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
765 /*
766 * Fill in the child block.
767 */
768 abp->b_ops = &xfs_bmbt_buf_ops;
769 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500770 if (xfs_sb_version_hascrc(&mp->m_sb))
771 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
772 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
773 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
774 else
775 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
776 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
777 XFS_BTREE_LONG_PTRS);
778
Dave Chinner9e5987a2013-02-25 12:31:26 +1100779 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
780 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
781 for (cnt = i = 0; i < nextents; i++) {
782 ep = xfs_iext_get_ext(ifp, i);
783 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
784 arp->l0 = cpu_to_be64(ep->l0);
785 arp->l1 = cpu_to_be64(ep->l1);
786 arp++; cnt++;
787 }
788 }
789 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
790 xfs_btree_set_numrecs(ablock, cnt);
791
792 /*
793 * Fill in the root key and pointer.
794 */
795 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
796 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
797 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
798 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
799 be16_to_cpu(block->bb_level)));
800 *pp = cpu_to_be64(args.fsbno);
801
802 /*
803 * Do all this logging at the end so that
804 * the root is at the right level.
805 */
806 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
807 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
808 ASSERT(*curp == NULL);
809 *curp = cur;
810 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
811 return 0;
812}
813
814/*
815 * Convert a local file to an extents file.
816 * This code is out of bounds for data forks of regular files,
817 * since the file data needs to get logged so things will stay consistent.
818 * (The bmap-level manipulations are ok, though).
819 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000820void
821xfs_bmap_local_to_extents_empty(
822 struct xfs_inode *ip,
823 int whichfork)
824{
825 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
826
827 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
828 ASSERT(ifp->if_bytes == 0);
829 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
830
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000831 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000832 ifp->if_flags &= ~XFS_IFINLINE;
833 ifp->if_flags |= XFS_IFEXTENTS;
834 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
835}
836
837
Dave Chinner9e5987a2013-02-25 12:31:26 +1100838STATIC int /* error */
839xfs_bmap_local_to_extents(
840 xfs_trans_t *tp, /* transaction pointer */
841 xfs_inode_t *ip, /* incore inode pointer */
842 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
843 xfs_extlen_t total, /* total blocks needed by transaction */
844 int *logflagsp, /* inode logging flags */
845 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500846 void (*init_fn)(struct xfs_trans *tp,
847 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100848 struct xfs_inode *ip,
849 struct xfs_ifork *ifp))
850{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000851 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100852 int flags; /* logging flags returned */
853 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000854 xfs_alloc_arg_t args; /* allocation arguments */
855 xfs_buf_t *bp; /* buffer for extent block */
856 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100857
858 /*
859 * We don't want to deal with the case of keeping inode data inline yet.
860 * So sending the data fork of a regular inode is invalid.
861 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100862 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100863 ifp = XFS_IFORK_PTR(ip, whichfork);
864 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000865
866 if (!ifp->if_bytes) {
867 xfs_bmap_local_to_extents_empty(ip, whichfork);
868 flags = XFS_ILOG_CORE;
869 goto done;
870 }
871
Dave Chinner9e5987a2013-02-25 12:31:26 +1100872 flags = 0;
873 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000874 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
875 XFS_IFINLINE);
876 memset(&args, 0, sizeof(args));
877 args.tp = tp;
878 args.mp = ip->i_mount;
879 args.firstblock = *firstblock;
880 /*
881 * Allocate a block. We know we need only one, since the
882 * file currently fits in an inode.
883 */
884 if (*firstblock == NULLFSBLOCK) {
885 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
886 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100887 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000888 args.fsbno = *firstblock;
889 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100890 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000891 args.total = total;
892 args.minlen = args.maxlen = args.prod = 1;
893 error = xfs_alloc_vextent(&args);
894 if (error)
895 goto done;
896
897 /* Can't fail, the space was reserved. */
898 ASSERT(args.fsbno != NULLFSBLOCK);
899 ASSERT(args.len == 1);
900 *firstblock = args.fsbno;
901 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
902
Dave Chinnerfe22d552015-01-22 09:30:06 +1100903 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100904 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100905 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100906 * The callout is responsible for logging because the remote format
907 * might differ from the local format and thus we don't know how much to
908 * log here. Note that init_fn must also set the buffer log item type
909 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100910 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000911 init_fn(tp, bp, ip, ifp);
912
Brian Fosterb7cdc662015-10-12 15:40:24 +1100913 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000914 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
915 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100916 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000917
918 xfs_iext_add(ifp, 0, 1);
919 ep = xfs_iext_get_ext(ifp, 0);
920 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
921 trace_xfs_bmap_post_update(ip, 0,
922 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
923 _THIS_IP_);
924 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
925 ip->i_d.di_nblocks = 1;
926 xfs_trans_mod_dquot_byino(tp, ip,
927 XFS_TRANS_DQ_BCOUNT, 1L);
928 flags |= xfs_ilog_fext(whichfork);
929
Dave Chinner9e5987a2013-02-25 12:31:26 +1100930done:
931 *logflagsp = flags;
932 return error;
933}
934
935/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * Called from xfs_bmap_add_attrfork to handle btree format files.
937 */
938STATIC int /* error */
939xfs_bmap_add_attrfork_btree(
940 xfs_trans_t *tp, /* transaction pointer */
941 xfs_inode_t *ip, /* incore inode pointer */
942 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000943 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 int *flags) /* inode logging flags */
945{
946 xfs_btree_cur_t *cur; /* btree cursor */
947 int error; /* error return value */
948 xfs_mount_t *mp; /* file system mount struct */
949 int stat; /* newroot status */
950
951 mp = ip->i_mount;
952 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
953 *flags |= XFS_ILOG_DBROOT;
954 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100955 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000956 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 cur->bc_private.b.firstblock = *firstblock;
958 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
959 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000960 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100961 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100962 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 goto error0;
964 if (stat == 0) {
965 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000966 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968 *firstblock = cur->bc_private.b.firstblock;
969 cur->bc_private.b.allocated = 0;
970 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
971 }
972 return 0;
973error0:
974 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
975 return error;
976}
977
978/*
979 * Called from xfs_bmap_add_attrfork to handle extents format files.
980 */
981STATIC int /* error */
982xfs_bmap_add_attrfork_extents(
983 xfs_trans_t *tp, /* transaction pointer */
984 xfs_inode_t *ip, /* incore inode pointer */
985 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000986 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 int *flags) /* inode logging flags */
988{
989 xfs_btree_cur_t *cur; /* bmap btree cursor */
990 int error; /* error return value */
991
992 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
993 return 0;
994 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000995 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 flags, XFS_DATA_FORK);
997 if (cur) {
998 cur->bc_private.b.allocated = 0;
999 xfs_btree_del_cursor(cur,
1000 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1001 }
1002 return error;
1003}
1004
1005/*
Dave Chinner1e823792013-02-11 15:58:13 +11001006 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1007 * different data fork content type needs a different callout to do the
1008 * conversion. Some are basic and only require special block initialisation
1009 * callouts for the data formating, others (directories) are so specialised they
1010 * handle everything themselves.
1011 *
1012 * XXX (dgc): investigate whether directory conversion can use the generic
1013 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001014 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 */
1016STATIC int /* error */
1017xfs_bmap_add_attrfork_local(
1018 xfs_trans_t *tp, /* transaction pointer */
1019 xfs_inode_t *ip, /* incore inode pointer */
1020 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001021 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 int *flags) /* inode logging flags */
1023{
1024 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1027 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001028
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001029 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001031 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 dargs.dp = ip;
1033 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001034 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001035 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 dargs.whichfork = XFS_DATA_FORK;
1037 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001038 return xfs_dir2_sf_to_block(&dargs);
1039 }
1040
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001041 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001042 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1043 flags, XFS_DATA_FORK,
1044 xfs_symlink_local_to_remote);
1045
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001046 /* should only be called for types that support local format data */
1047 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001048 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001052 * Convert inode from non-attributed to attributed.
1053 * Must not be in a transaction, ip must not be locked.
1054 */
1055int /* error code */
1056xfs_bmap_add_attrfork(
1057 xfs_inode_t *ip, /* incore inode pointer */
1058 int size, /* space new attribute needs */
1059 int rsvd) /* xact may use reserved blks */
1060{
1061 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001062 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001063 xfs_mount_t *mp; /* mount structure */
1064 xfs_trans_t *tp; /* transaction pointer */
1065 int blks; /* space reservation */
1066 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001067 int logflags; /* logging flags */
1068 int error; /* error return value */
1069
1070 ASSERT(XFS_IFORK_Q(ip) == 0);
1071
1072 mp = ip->i_mount;
1073 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001074
Dave Chinner9e5987a2013-02-25 12:31:26 +11001075 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001076
1077 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1078 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1079 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001080 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001081
Dave Chinner9e5987a2013-02-25 12:31:26 +11001082 xfs_ilock(ip, XFS_ILOCK_EXCL);
1083 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1084 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1085 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001086 if (error)
1087 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001088 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001089 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001090 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1091 /*
1092 * For inodes coming from pre-6.2 filesystems.
1093 */
1094 ASSERT(ip->i_d.di_aformat == 0);
1095 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1096 }
1097 ASSERT(ip->i_d.di_anextents == 0);
1098
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001099 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001100 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1101
1102 switch (ip->i_d.di_format) {
1103 case XFS_DINODE_FMT_DEV:
1104 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1105 break;
1106 case XFS_DINODE_FMT_UUID:
1107 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1108 break;
1109 case XFS_DINODE_FMT_LOCAL:
1110 case XFS_DINODE_FMT_EXTENTS:
1111 case XFS_DINODE_FMT_BTREE:
1112 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1113 if (!ip->i_d.di_forkoff)
1114 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1115 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1116 version = 2;
1117 break;
1118 default:
1119 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001120 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001121 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001122 }
1123
1124 ASSERT(ip->i_afp == NULL);
1125 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1126 ip->i_afp->if_flags = XFS_IFEXTENTS;
1127 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001128 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001129 switch (ip->i_d.di_format) {
1130 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001131 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001132 &logflags);
1133 break;
1134 case XFS_DINODE_FMT_EXTENTS:
1135 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001136 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001137 break;
1138 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001139 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001140 &logflags);
1141 break;
1142 default:
1143 error = 0;
1144 break;
1145 }
1146 if (logflags)
1147 xfs_trans_log_inode(tp, ip, logflags);
1148 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001149 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001150 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1151 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001152 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001153
1154 spin_lock(&mp->m_sb_lock);
1155 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1156 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001157 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001158 }
1159 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1160 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001161 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001162 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001163 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001164 if (log_sb)
1165 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001166 }
1167
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001168 error = xfs_defer_finish(&tp, &dfops, NULL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001169 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001170 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001171 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001172 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001173 return error;
1174
1175bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001176 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001177trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001178 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001179 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001180 return error;
1181}
1182
1183/*
1184 * Internal and external extent tree search functions.
1185 */
1186
1187/*
1188 * Read in the extents to if_extents.
1189 * All inode fields are set up by caller, we just traverse the btree
1190 * and copy the records in. If the file system cannot contain unwritten
1191 * extents, the records are checked for no "state" flags.
1192 */
1193int /* error */
1194xfs_bmap_read_extents(
1195 xfs_trans_t *tp, /* transaction pointer */
1196 xfs_inode_t *ip, /* incore inode */
1197 int whichfork) /* data or attr fork */
1198{
1199 struct xfs_btree_block *block; /* current btree block */
1200 xfs_fsblock_t bno; /* block # of "block" */
1201 xfs_buf_t *bp; /* buffer for "block" */
1202 int error; /* error return value */
1203 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1204 xfs_extnum_t i, j; /* index into the extents list */
1205 xfs_ifork_t *ifp; /* fork structure */
1206 int level; /* btree level, for checking */
1207 xfs_mount_t *mp; /* file system mount structure */
1208 __be64 *pp; /* pointer to block address */
1209 /* REFERENCED */
1210 xfs_extnum_t room; /* number of entries there's room for */
1211
1212 bno = NULLFSBLOCK;
1213 mp = ip->i_mount;
1214 ifp = XFS_IFORK_PTR(ip, whichfork);
1215 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1216 XFS_EXTFMT_INODE(ip);
1217 block = ifp->if_broot;
1218 /*
1219 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1220 */
1221 level = be16_to_cpu(block->bb_level);
1222 ASSERT(level > 0);
1223 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1224 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001225 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001226 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1227 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1228 /*
1229 * Go down the tree until leaf level is reached, following the first
1230 * pointer (leftmost) at each level.
1231 */
1232 while (level-- > 0) {
1233 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1234 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1235 if (error)
1236 return error;
1237 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001238 if (level == 0)
1239 break;
1240 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1241 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001242 XFS_WANT_CORRUPTED_GOTO(mp,
1243 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001244 xfs_trans_brelse(tp, bp);
1245 }
1246 /*
1247 * Here with bp and block set to the leftmost leaf node in the tree.
1248 */
1249 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1250 i = 0;
1251 /*
1252 * Loop over all leaf nodes. Copy information to the extent records.
1253 */
1254 for (;;) {
1255 xfs_bmbt_rec_t *frp;
1256 xfs_fsblock_t nextbno;
1257 xfs_extnum_t num_recs;
1258 xfs_extnum_t start;
1259
1260 num_recs = xfs_btree_get_numrecs(block);
1261 if (unlikely(i + num_recs > room)) {
1262 ASSERT(i + num_recs <= room);
1263 xfs_warn(ip->i_mount,
1264 "corrupt dinode %Lu, (btree extents).",
1265 (unsigned long long) ip->i_ino);
1266 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1267 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1268 goto error0;
1269 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001270 /*
1271 * Read-ahead the next leaf block, if any.
1272 */
1273 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1274 if (nextbno != NULLFSBLOCK)
1275 xfs_btree_reada_bufl(mp, nextbno, 1,
1276 &xfs_bmbt_buf_ops);
1277 /*
1278 * Copy records into the extent records.
1279 */
1280 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1281 start = i;
1282 for (j = 0; j < num_recs; j++, i++, frp++) {
1283 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1284 trp->l0 = be64_to_cpu(frp->l0);
1285 trp->l1 = be64_to_cpu(frp->l1);
1286 }
1287 if (exntf == XFS_EXTFMT_NOSTATE) {
1288 /*
1289 * Check all attribute bmap btree records and
1290 * any "older" data bmap btree records for a
1291 * set bit in the "extent flag" position.
1292 */
1293 if (unlikely(xfs_check_nostate_extents(ifp,
1294 start, num_recs))) {
1295 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1296 XFS_ERRLEVEL_LOW,
1297 ip->i_mount);
1298 goto error0;
1299 }
1300 }
1301 xfs_trans_brelse(tp, bp);
1302 bno = nextbno;
1303 /*
1304 * If we've reached the end, stop.
1305 */
1306 if (bno == NULLFSBLOCK)
1307 break;
1308 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1309 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1310 if (error)
1311 return error;
1312 block = XFS_BUF_TO_BLOCK(bp);
1313 }
1314 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1315 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1316 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1317 return 0;
1318error0:
1319 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001320 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001321}
1322
1323
1324/*
1325 * Search the extent records for the entry containing block bno.
1326 * If bno lies in a hole, point to the next entry. If bno lies
1327 * past eof, *eofp will be set, and *prevp will contain the last
1328 * entry (null if none). Else, *lastxp will be set to the index
1329 * of the found entry; *gotp will contain the entry.
1330 */
1331STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1332xfs_bmap_search_multi_extents(
1333 xfs_ifork_t *ifp, /* inode fork pointer */
1334 xfs_fileoff_t bno, /* block number searched for */
1335 int *eofp, /* out: end of file found */
1336 xfs_extnum_t *lastxp, /* out: last extent index */
1337 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1338 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1339{
1340 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1341 xfs_extnum_t lastx; /* last extent index */
1342
1343 /*
1344 * Initialize the extent entry structure to catch access to
1345 * uninitialized br_startblock field.
1346 */
1347 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1348 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1349 gotp->br_state = XFS_EXT_INVALID;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001350 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001351 prevp->br_startoff = NULLFILEOFF;
1352
1353 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1354 if (lastx > 0) {
1355 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1356 }
1357 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1358 xfs_bmbt_get_all(ep, gotp);
1359 *eofp = 0;
1360 } else {
1361 if (lastx > 0) {
1362 *gotp = *prevp;
1363 }
1364 *eofp = 1;
1365 ep = NULL;
1366 }
1367 *lastxp = lastx;
1368 return ep;
1369}
1370
1371/*
1372 * Search the extents list for the inode, for the extent containing bno.
1373 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1374 * *eofp will be set, and *prevp will contain the last entry (null if none).
1375 * Else, *lastxp will be set to the index of the found
1376 * entry; *gotp will contain the entry.
1377 */
1378STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1379xfs_bmap_search_extents(
1380 xfs_inode_t *ip, /* incore inode pointer */
1381 xfs_fileoff_t bno, /* block number searched for */
1382 int fork, /* data or attr fork */
1383 int *eofp, /* out: end of file found */
1384 xfs_extnum_t *lastxp, /* out: last extent index */
1385 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1386 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1387{
1388 xfs_ifork_t *ifp; /* inode fork pointer */
1389 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1390
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001391 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001392 ifp = XFS_IFORK_PTR(ip, fork);
1393
1394 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1395
1396 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1397 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1398 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1399 "Access to block zero in inode %llu "
1400 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -05001401 "blkcnt: %llx extent-state: %x lastx: %x",
Dave Chinner9e5987a2013-02-25 12:31:26 +11001402 (unsigned long long)ip->i_ino,
1403 (unsigned long long)gotp->br_startblock,
1404 (unsigned long long)gotp->br_startoff,
1405 (unsigned long long)gotp->br_blockcount,
1406 gotp->br_state, *lastxp);
1407 *lastxp = NULLEXTNUM;
1408 *eofp = 1;
1409 return NULL;
1410 }
1411 return ep;
1412}
1413
1414/*
1415 * Returns the file-relative block number of the first unused block(s)
1416 * in the file with at least "len" logically contiguous blocks free.
1417 * This is the lowest-address hole if the file has holes, else the first block
1418 * past the end of file.
1419 * Return 0 if the file is currently local (in-inode).
1420 */
1421int /* error */
1422xfs_bmap_first_unused(
1423 xfs_trans_t *tp, /* transaction pointer */
1424 xfs_inode_t *ip, /* incore inode */
1425 xfs_extlen_t len, /* size of hole to find */
1426 xfs_fileoff_t *first_unused, /* unused block */
1427 int whichfork) /* data or attr fork */
1428{
1429 int error; /* error return value */
1430 int idx; /* extent record index */
1431 xfs_ifork_t *ifp; /* inode fork pointer */
1432 xfs_fileoff_t lastaddr; /* last block number seen */
1433 xfs_fileoff_t lowest; /* lowest useful block */
1434 xfs_fileoff_t max; /* starting useful block */
1435 xfs_fileoff_t off; /* offset for this block */
1436 xfs_extnum_t nextents; /* number of extent entries */
1437
1438 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1439 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1440 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1441 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1442 *first_unused = 0;
1443 return 0;
1444 }
1445 ifp = XFS_IFORK_PTR(ip, whichfork);
1446 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1447 (error = xfs_iread_extents(tp, ip, whichfork)))
1448 return error;
1449 lowest = *first_unused;
1450 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1451 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1452 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1453 off = xfs_bmbt_get_startoff(ep);
1454 /*
1455 * See if the hole before this extent will work.
1456 */
1457 if (off >= lowest + len && off - max >= len) {
1458 *first_unused = max;
1459 return 0;
1460 }
1461 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1462 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1463 }
1464 *first_unused = max;
1465 return 0;
1466}
1467
1468/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001469 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001470 * last_block (input value) in the file.
1471 * This is not based on i_size, it is based on the extent records.
1472 * Returns 0 for local files, as they do not have extent records.
1473 */
1474int /* error */
1475xfs_bmap_last_before(
1476 xfs_trans_t *tp, /* transaction pointer */
1477 xfs_inode_t *ip, /* incore inode */
1478 xfs_fileoff_t *last_block, /* last block */
1479 int whichfork) /* data or attr fork */
1480{
1481 xfs_fileoff_t bno; /* input file offset */
1482 int eof; /* hit end of file */
1483 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1484 int error; /* error return value */
1485 xfs_bmbt_irec_t got; /* current extent value */
1486 xfs_ifork_t *ifp; /* inode fork pointer */
1487 xfs_extnum_t lastx; /* last extent used */
1488 xfs_bmbt_irec_t prev; /* previous extent value */
1489
1490 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1491 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1492 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +10001493 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001494 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1495 *last_block = 0;
1496 return 0;
1497 }
1498 ifp = XFS_IFORK_PTR(ip, whichfork);
1499 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1500 (error = xfs_iread_extents(tp, ip, whichfork)))
1501 return error;
1502 bno = *last_block - 1;
1503 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1504 &prev);
1505 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1506 if (prev.br_startoff == NULLFILEOFF)
1507 *last_block = 0;
1508 else
1509 *last_block = prev.br_startoff + prev.br_blockcount;
1510 }
1511 /*
1512 * Otherwise *last_block is already the right answer.
1513 */
1514 return 0;
1515}
1516
Dave Chinner68988112013-08-12 20:49:42 +10001517int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001518xfs_bmap_last_extent(
1519 struct xfs_trans *tp,
1520 struct xfs_inode *ip,
1521 int whichfork,
1522 struct xfs_bmbt_irec *rec,
1523 int *is_empty)
1524{
1525 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1526 int error;
1527 int nextents;
1528
1529 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1530 error = xfs_iread_extents(tp, ip, whichfork);
1531 if (error)
1532 return error;
1533 }
1534
1535 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1536 if (nextents == 0) {
1537 *is_empty = 1;
1538 return 0;
1539 }
1540
1541 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1542 *is_empty = 0;
1543 return 0;
1544}
1545
1546/*
1547 * Check the last inode extent to determine whether this allocation will result
1548 * in blocks being allocated at the end of the file. When we allocate new data
1549 * blocks at the end of the file which do not start at the previous data block,
1550 * we will try to align the new blocks at stripe unit boundaries.
1551 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001552 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001553 * at, or past the EOF.
1554 */
1555STATIC int
1556xfs_bmap_isaeof(
1557 struct xfs_bmalloca *bma,
1558 int whichfork)
1559{
1560 struct xfs_bmbt_irec rec;
1561 int is_empty;
1562 int error;
1563
1564 bma->aeof = 0;
1565 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1566 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001567 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001568 return error;
1569
Dave Chinner6e708bc2013-11-22 10:41:16 +11001570 if (is_empty) {
1571 bma->aeof = 1;
1572 return 0;
1573 }
1574
Dave Chinner9e5987a2013-02-25 12:31:26 +11001575 /*
1576 * Check if we are allocation or past the last extent, or at least into
1577 * the last delayed allocated extent.
1578 */
1579 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1580 (bma->offset >= rec.br_startoff &&
1581 isnullstartblock(rec.br_startblock));
1582 return 0;
1583}
1584
1585/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001586 * Returns the file-relative block number of the first block past eof in
1587 * the file. This is not based on i_size, it is based on the extent records.
1588 * Returns 0 for local files, as they do not have extent records.
1589 */
1590int
1591xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001592 struct xfs_inode *ip,
1593 xfs_fileoff_t *last_block,
1594 int whichfork)
1595{
1596 struct xfs_bmbt_irec rec;
1597 int is_empty;
1598 int error;
1599
1600 *last_block = 0;
1601
1602 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1603 return 0;
1604
1605 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1606 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001607 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001608
1609 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1610 if (error || is_empty)
1611 return error;
1612
1613 *last_block = rec.br_startoff + rec.br_blockcount;
1614 return 0;
1615}
1616
1617/*
1618 * Returns whether the selected fork of the inode has exactly one
1619 * block or not. For the data fork we check this matches di_size,
1620 * implying the file's range is 0..bsize-1.
1621 */
1622int /* 1=>1 block, 0=>otherwise */
1623xfs_bmap_one_block(
1624 xfs_inode_t *ip, /* incore inode */
1625 int whichfork) /* data or attr fork */
1626{
1627 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1628 xfs_ifork_t *ifp; /* inode fork pointer */
1629 int rval; /* return value */
1630 xfs_bmbt_irec_t s; /* internal version of extent */
1631
1632#ifndef DEBUG
1633 if (whichfork == XFS_DATA_FORK)
1634 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1635#endif /* !DEBUG */
1636 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1637 return 0;
1638 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1639 return 0;
1640 ifp = XFS_IFORK_PTR(ip, whichfork);
1641 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1642 ep = xfs_iext_get_ext(ifp, 0);
1643 xfs_bmbt_get_all(ep, &s);
1644 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1645 if (rval && whichfork == XFS_DATA_FORK)
1646 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1647 return rval;
1648}
1649
1650/*
1651 * Extent tree manipulation functions used during allocation.
1652 */
1653
1654/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001655 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 */
1657STATIC int /* error */
1658xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001659 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001661 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001663 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001666 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 xfs_fileoff_t new_endoff; /* end offset of new entry */
1668 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1669 /* left is 0, right is 1, prev is 2 */
1670 int rval=0; /* return value (logging flags) */
1671 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001672 xfs_filblks_t da_new; /* new count del alloc blocks used */
1673 xfs_filblks_t da_old; /* old count del alloc blocks used */
1674 xfs_filblks_t temp=0; /* value for da_new calculations */
1675 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 int tmp_rval; /* partial logging flags */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001677 int whichfork = XFS_DATA_FORK;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001678 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001680 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001681 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001682
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001683 ASSERT(bma->idx >= 0);
1684 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001685 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001686 ASSERT(!bma->cur ||
1687 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001688
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001689 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001690
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691#define LEFT r[0]
1692#define RIGHT r[1]
1693#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 /*
1696 * Set up a bunch of variables to make the tests simpler.
1697 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001698 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 xfs_bmbt_get_all(ep, &PREV);
1700 new_endoff = new->br_startoff + new->br_blockcount;
1701 ASSERT(PREV.br_startoff <= new->br_startoff);
1702 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001703
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001704 da_old = startblockval(PREV.br_startblock);
1705 da_new = 0;
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 /*
1708 * Set flags determining what part of the previous delayed allocation
1709 * extent is being replaced by a real allocation.
1710 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001711 if (PREV.br_startoff == new->br_startoff)
1712 state |= BMAP_LEFT_FILLING;
1713 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1714 state |= BMAP_RIGHT_FILLING;
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 /*
1717 * Check and set flags if this segment has a left neighbor.
1718 * Don't set contiguous if the combined extent would be too large.
1719 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001720 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001721 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001722 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001723
1724 if (isnullstartblock(LEFT.br_startblock))
1725 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001727
1728 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1729 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1730 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1731 LEFT.br_state == new->br_state &&
1732 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1733 state |= BMAP_LEFT_CONTIG;
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 /*
1736 * Check and set flags if this segment has a right neighbor.
1737 * Don't set contiguous if the combined extent would be too large.
1738 * Also check for all-three-contiguous being too large.
1739 */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001740 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001741 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001742 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001743
1744 if (isnullstartblock(RIGHT.br_startblock))
1745 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001747
1748 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1749 new_endoff == RIGHT.br_startoff &&
1750 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1751 new->br_state == RIGHT.br_state &&
1752 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1753 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1754 BMAP_RIGHT_FILLING)) !=
1755 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1756 BMAP_RIGHT_FILLING) ||
1757 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1758 <= MAXEXTLEN))
1759 state |= BMAP_RIGHT_CONTIG;
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 error = 0;
1762 /*
1763 * Switch out based on the FILLING and CONTIG state bits.
1764 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001765 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1766 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1767 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1768 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 /*
1770 * Filling in all of a previously delayed allocation extent.
1771 * The left and right neighbors are both contiguous with new.
1772 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001773 bma->idx--;
1774 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1775 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 LEFT.br_blockcount + PREV.br_blockcount +
1777 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001778 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001779
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001780 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1781 bma->ip->i_d.di_nextents--;
1782 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1784 else {
1785 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001786 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001788 RIGHT.br_blockcount, &i);
1789 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001791 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001792 error = xfs_btree_delete(bma->cur, &i);
1793 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001795 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001796 error = xfs_btree_decrement(bma->cur, 0, &i);
1797 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001799 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001800 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 LEFT.br_startblock,
1802 LEFT.br_blockcount +
1803 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001804 RIGHT.br_blockcount, LEFT.br_state);
1805 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 goto done;
1807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 break;
1809
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001810 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 /*
1812 * Filling in all of a previously delayed allocation extent.
1813 * The left neighbor is contiguous, the right is not.
1814 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001815 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001816
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001817 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1818 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001819 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001820 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001821
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001822 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1823 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 rval = XFS_ILOG_DEXT;
1825 else {
1826 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001827 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001829 &i);
1830 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001832 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001833 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 LEFT.br_startblock,
1835 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001836 PREV.br_blockcount, LEFT.br_state);
1837 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 goto done;
1839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 break;
1841
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001842 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /*
1844 * Filling in all of a previously delayed allocation extent.
1845 * The right neighbor is contiguous, the left is not.
1846 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 xfs_bmbt_set_startblock(ep, new->br_startblock);
1849 xfs_bmbt_set_blockcount(ep,
1850 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001851 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001852
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001853 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1854 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 rval = XFS_ILOG_DEXT;
1856 else {
1857 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001858 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001860 RIGHT.br_blockcount, &i);
1861 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001863 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001864 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 new->br_startblock,
1866 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001867 RIGHT.br_blockcount, PREV.br_state);
1868 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 goto done;
1870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 break;
1872
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001873 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 /*
1875 * Filling in all of a previously delayed allocation extent.
1876 * Neither the left nor right neighbors are contiguous with
1877 * the new one.
1878 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001879 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001881 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001882
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001883 bma->ip->i_d.di_nextents++;
1884 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1886 else {
1887 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001888 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001890 &i);
1891 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001893 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001894 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1895 error = xfs_btree_insert(bma->cur, &i);
1896 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001898 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 break;
1901
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001902 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /*
1904 * Filling in the first part of a previous delayed allocation.
1905 * The left neighbor is contiguous.
1906 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001907 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1908 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 LEFT.br_blockcount + new->br_blockcount);
1910 xfs_bmbt_set_startoff(ep,
1911 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001912 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001915 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001917 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 rval = XFS_ILOG_DEXT;
1919 else {
1920 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001921 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001923 &i);
1924 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001926 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001927 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 LEFT.br_startblock,
1929 LEFT.br_blockcount +
1930 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001931 LEFT.br_state);
1932 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 goto done;
1934 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001935 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001936 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001937 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001938 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001939
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001940 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 break;
1942
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001943 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 /*
1945 * Filling in the first part of a previous delayed allocation.
1946 * The left neighbor is not contiguous.
1947 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001948 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 xfs_bmbt_set_startoff(ep, new_endoff);
1950 temp = PREV.br_blockcount - new->br_blockcount;
1951 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001952 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
1953 bma->ip->i_d.di_nextents++;
1954 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1956 else {
1957 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001960 &i);
1961 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001963 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001964 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1965 error = xfs_btree_insert(bma->cur, &i);
1966 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001968 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001970
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001971 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001972 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001973 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001974 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 rval |= tmp_rval;
1976 if (error)
1977 goto done;
1978 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001979 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001980 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001981 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1982 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001983 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001984 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 break;
1986
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001987 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /*
1989 * Filling in the last part of a previous delayed allocation.
1990 * The right neighbor is contiguous with the new allocation.
1991 */
1992 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001993 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001995 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001996 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 new->br_blockcount + RIGHT.br_blockcount,
1998 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001999 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2000 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 rval = XFS_ILOG_DEXT;
2002 else {
2003 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002004 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002006 RIGHT.br_blockcount, &i);
2007 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002009 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002010 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 new->br_startblock,
2012 new->br_blockcount +
2013 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002014 RIGHT.br_state);
2015 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 goto done;
2017 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002018
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002019 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002020 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002021 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002022 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002023 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002024
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002025 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 break;
2027
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002028 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 /*
2030 * Filling in the last part of a previous delayed allocation.
2031 * The right neighbor is not contiguous.
2032 */
2033 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002034 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002036 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2037 bma->ip->i_d.di_nextents++;
2038 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2040 else {
2041 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002042 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002044 &i);
2045 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002047 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002048 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2049 error = xfs_btree_insert(bma->cur, &i);
2050 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002052 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002054
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002055 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002056 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002057 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002058 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 rval |= tmp_rval;
2060 if (error)
2061 goto done;
2062 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002063 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002064 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002065 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2066 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002067 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002068 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002069
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002070 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 break;
2072
2073 case 0:
2074 /*
2075 * Filling in the middle part of a previous delayed allocation.
2076 * Contiguity is impossible here.
2077 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002078 *
2079 * We start with a delayed allocation:
2080 *
2081 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2082 * PREV @ idx
2083 *
2084 * and we are allocating:
2085 * +rrrrrrrrrrrrrrrrr+
2086 * new
2087 *
2088 * and we set it up for insertion as:
2089 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2090 * new
2091 * PREV @ idx LEFT RIGHT
2092 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 */
2094 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002096 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002097 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2098 LEFT = *new;
2099 RIGHT.br_state = PREV.br_state;
2100 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002101 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002102 RIGHT.br_startoff = new_endoff;
2103 RIGHT.br_blockcount = temp2;
2104 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002105 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2106 bma->ip->i_d.di_nextents++;
2107 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2109 else {
2110 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002111 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002113 &i);
2114 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002116 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002117 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2118 error = xfs_btree_insert(bma->cur, &i);
2119 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002121 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002123
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002124 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002125 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002126 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002127 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 rval |= tmp_rval;
2129 if (error)
2130 goto done;
2131 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002132 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2133 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002134 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002135 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002136 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002137 error = xfs_mod_fdblocks(bma->ip->i_mount,
2138 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002139 ASSERT(!error);
2140 if (error)
2141 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002143
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002144 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002145 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002146 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2147 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2148 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002149 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002150 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002151
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002152 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002153 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 break;
2155
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002156 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2157 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2158 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2159 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2160 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2161 case BMAP_LEFT_CONTIG:
2162 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 /*
2164 * These cases are all impossible.
2165 */
2166 ASSERT(0);
2167 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002168
2169 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002170 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002171 int tmp_logflags; /* partial log flag return val */
2172
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002173 ASSERT(bma->cur == NULL);
2174 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002175 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002176 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002177 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002178 if (error)
2179 goto done;
2180 }
2181
2182 /* adjust for changes in reserved delayed indirect blocks */
2183 if (da_old || da_new) {
2184 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002185 if (bma->cur)
2186 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002187 ASSERT(temp <= da_old);
2188 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002189 xfs_mod_fdblocks(bma->ip->i_mount,
2190 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002191 }
2192
2193 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002194 if (bma->cur)
2195 bma->cur->bc_private.b.allocated = 0;
2196
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002197 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002199 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return error;
2201#undef LEFT
2202#undef RIGHT
2203#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
2205
2206/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002207 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 */
2209STATIC int /* error */
2210xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002211 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002213 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002215 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002216 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002217 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002218 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002221 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002224 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 xfs_fileoff_t new_endoff; /* end offset of new entry */
2226 xfs_exntst_t newext; /* new extent state */
2227 xfs_exntst_t oldext; /* old extent state */
2228 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2229 /* left is 0, right is 1, prev is 2 */
2230 int rval=0; /* return value (logging flags) */
2231 int state = 0;/* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002232 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002234 *logflagsp = 0;
2235
2236 cur = *curp;
2237 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2238
2239 ASSERT(*idx >= 0);
2240 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2241 ASSERT(!isnullstartblock(new->br_startblock));
2242
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002243 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245#define LEFT r[0]
2246#define RIGHT r[1]
2247#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 /*
2250 * Set up a bunch of variables to make the tests simpler.
2251 */
2252 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002253 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 xfs_bmbt_get_all(ep, &PREV);
2255 newext = new->br_state;
2256 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2257 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2258 ASSERT(PREV.br_state == oldext);
2259 new_endoff = new->br_startoff + new->br_blockcount;
2260 ASSERT(PREV.br_startoff <= new->br_startoff);
2261 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 /*
2264 * Set flags determining what part of the previous oldext allocation
2265 * extent is being replaced by a newext allocation.
2266 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002267 if (PREV.br_startoff == new->br_startoff)
2268 state |= BMAP_LEFT_FILLING;
2269 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2270 state |= BMAP_RIGHT_FILLING;
2271
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 /*
2273 * Check and set flags if this segment has a left neighbor.
2274 * Don't set contiguous if the combined extent would be too large.
2275 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002276 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002277 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002278 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002279
2280 if (isnullstartblock(LEFT.br_startblock))
2281 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002283
2284 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2285 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2286 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2287 LEFT.br_state == newext &&
2288 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2289 state |= BMAP_LEFT_CONTIG;
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 /*
2292 * Check and set flags if this segment has a right neighbor.
2293 * Don't set contiguous if the combined extent would be too large.
2294 * Also check for all-three-contiguous being too large.
2295 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002296 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002297 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002298 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002299 if (isnullstartblock(RIGHT.br_startblock))
2300 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002302
2303 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2304 new_endoff == RIGHT.br_startoff &&
2305 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2306 newext == RIGHT.br_state &&
2307 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2308 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2309 BMAP_RIGHT_FILLING)) !=
2310 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2311 BMAP_RIGHT_FILLING) ||
2312 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2313 <= MAXEXTLEN))
2314 state |= BMAP_RIGHT_CONTIG;
2315
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 /*
2317 * Switch out based on the FILLING and CONTIG state bits.
2318 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002319 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2320 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2321 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2322 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 /*
2324 * Setting all of a previous oldext extent to newext.
2325 * The left and right neighbors are both contiguous with new.
2326 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002327 --*idx;
2328
2329 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2330 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 LEFT.br_blockcount + PREV.br_blockcount +
2332 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002333 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002334
Christoph Hellwigec90c552011-05-23 08:52:53 +00002335 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 ip->i_d.di_nextents -= 2;
2337 if (cur == NULL)
2338 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2339 else {
2340 rval = XFS_ILOG_CORE;
2341 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2342 RIGHT.br_startblock,
2343 RIGHT.br_blockcount, &i)))
2344 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002345 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002346 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002348 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002349 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002351 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002352 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002354 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002355 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002357 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2359 LEFT.br_startblock,
2360 LEFT.br_blockcount + PREV.br_blockcount +
2361 RIGHT.br_blockcount, LEFT.br_state)))
2362 goto done;
2363 }
2364 break;
2365
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002366 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 /*
2368 * Setting all of a previous oldext extent to newext.
2369 * The left neighbor is contiguous, the right is not.
2370 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002371 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002372
Christoph Hellwigec90c552011-05-23 08:52:53 +00002373 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2374 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2375 LEFT.br_blockcount + PREV.br_blockcount);
2376 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2377
2378 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 ip->i_d.di_nextents--;
2380 if (cur == NULL)
2381 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2382 else {
2383 rval = XFS_ILOG_CORE;
2384 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2385 PREV.br_startblock, PREV.br_blockcount,
2386 &i)))
2387 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002388 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002389 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002391 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002392 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002394 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2396 LEFT.br_startblock,
2397 LEFT.br_blockcount + PREV.br_blockcount,
2398 LEFT.br_state)))
2399 goto done;
2400 }
2401 break;
2402
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002403 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 /*
2405 * Setting all of a previous oldext extent to newext.
2406 * The right neighbor is contiguous, the left is not.
2407 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002408 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 xfs_bmbt_set_blockcount(ep,
2410 PREV.br_blockcount + RIGHT.br_blockcount);
2411 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002412 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2413 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 ip->i_d.di_nextents--;
2415 if (cur == NULL)
2416 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2417 else {
2418 rval = XFS_ILOG_CORE;
2419 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2420 RIGHT.br_startblock,
2421 RIGHT.br_blockcount, &i)))
2422 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002423 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002424 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002426 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002427 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002429 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2431 new->br_startblock,
2432 new->br_blockcount + RIGHT.br_blockcount,
2433 newext)))
2434 goto done;
2435 }
2436 break;
2437
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002438 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 /*
2440 * Setting all of a previous oldext extent to newext.
2441 * Neither the left nor right neighbors are contiguous with
2442 * the new one.
2443 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002444 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002446 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 if (cur == NULL)
2449 rval = XFS_ILOG_DEXT;
2450 else {
2451 rval = 0;
2452 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2453 new->br_startblock, new->br_blockcount,
2454 &i)))
2455 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002456 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2458 new->br_startblock, new->br_blockcount,
2459 newext)))
2460 goto done;
2461 }
2462 break;
2463
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002464 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 /*
2466 * Setting the first part of a previous oldext extent to newext.
2467 * The left neighbor is contiguous.
2468 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002469 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2470 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 LEFT.br_blockcount + new->br_blockcount);
2472 xfs_bmbt_set_startoff(ep,
2473 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002474 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002475
Christoph Hellwigec90c552011-05-23 08:52:53 +00002476 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 xfs_bmbt_set_startblock(ep,
2478 new->br_startblock + new->br_blockcount);
2479 xfs_bmbt_set_blockcount(ep,
2480 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002481 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002482
Christoph Hellwigec90c552011-05-23 08:52:53 +00002483 --*idx;
2484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 if (cur == NULL)
2486 rval = XFS_ILOG_DEXT;
2487 else {
2488 rval = 0;
2489 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2490 PREV.br_startblock, PREV.br_blockcount,
2491 &i)))
2492 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002493 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if ((error = xfs_bmbt_update(cur,
2495 PREV.br_startoff + new->br_blockcount,
2496 PREV.br_startblock + new->br_blockcount,
2497 PREV.br_blockcount - new->br_blockcount,
2498 oldext)))
2499 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002500 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002502 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 LEFT.br_startblock,
2504 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002505 LEFT.br_state);
2506 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 goto done;
2508 }
2509 break;
2510
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002511 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 /*
2513 * Setting the first part of a previous oldext extent to newext.
2514 * The left neighbor is not contiguous.
2515 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002516 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2518 xfs_bmbt_set_startoff(ep, new_endoff);
2519 xfs_bmbt_set_blockcount(ep,
2520 PREV.br_blockcount - new->br_blockcount);
2521 xfs_bmbt_set_startblock(ep,
2522 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002523 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002524
Christoph Hellwigec90c552011-05-23 08:52:53 +00002525 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 ip->i_d.di_nextents++;
2527 if (cur == NULL)
2528 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2529 else {
2530 rval = XFS_ILOG_CORE;
2531 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2532 PREV.br_startblock, PREV.br_blockcount,
2533 &i)))
2534 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002535 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if ((error = xfs_bmbt_update(cur,
2537 PREV.br_startoff + new->br_blockcount,
2538 PREV.br_startblock + new->br_blockcount,
2539 PREV.br_blockcount - new->br_blockcount,
2540 oldext)))
2541 goto done;
2542 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002543 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002545 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
2547 break;
2548
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002549 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 /*
2551 * Setting the last part of a previous oldext extent to newext.
2552 * The right neighbor is contiguous with the new allocation.
2553 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002554 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 xfs_bmbt_set_blockcount(ep,
2556 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002557 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2558
2559 ++*idx;
2560
2561 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2562 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002563 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002565 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 if (cur == NULL)
2568 rval = XFS_ILOG_DEXT;
2569 else {
2570 rval = 0;
2571 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2572 PREV.br_startblock,
2573 PREV.br_blockcount, &i)))
2574 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002575 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2577 PREV.br_startblock,
2578 PREV.br_blockcount - new->br_blockcount,
2579 oldext)))
2580 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002581 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 goto done;
2583 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2584 new->br_startblock,
2585 new->br_blockcount + RIGHT.br_blockcount,
2586 newext)))
2587 goto done;
2588 }
2589 break;
2590
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002591 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 /*
2593 * Setting the last part of a previous oldext extent to newext.
2594 * The right neighbor is not contiguous.
2595 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002596 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 xfs_bmbt_set_blockcount(ep,
2598 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002599 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002600
Christoph Hellwigec90c552011-05-23 08:52:53 +00002601 ++*idx;
2602 xfs_iext_insert(ip, *idx, 1, new, state);
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 ip->i_d.di_nextents++;
2605 if (cur == NULL)
2606 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2607 else {
2608 rval = XFS_ILOG_CORE;
2609 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2610 PREV.br_startblock, PREV.br_blockcount,
2611 &i)))
2612 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002613 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2615 PREV.br_startblock,
2616 PREV.br_blockcount - new->br_blockcount,
2617 oldext)))
2618 goto done;
2619 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2620 new->br_startblock, new->br_blockcount,
2621 &i)))
2622 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002623 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002625 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002627 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 }
2629 break;
2630
2631 case 0:
2632 /*
2633 * Setting the middle part of a previous oldext extent to
2634 * newext. Contiguity is impossible here.
2635 * One extent becomes three extents.
2636 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002637 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 xfs_bmbt_set_blockcount(ep,
2639 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002640 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 r[0] = *new;
2643 r[1].br_startoff = new_endoff;
2644 r[1].br_blockcount =
2645 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2646 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2647 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002648
2649 ++*idx;
2650 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 ip->i_d.di_nextents += 2;
2653 if (cur == NULL)
2654 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2655 else {
2656 rval = XFS_ILOG_CORE;
2657 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2658 PREV.br_startblock, PREV.br_blockcount,
2659 &i)))
2660 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002661 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 /* new right extent - oldext */
2663 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2664 r[1].br_startblock, r[1].br_blockcount,
2665 r[1].br_state)))
2666 goto done;
2667 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002669 cur->bc_rec.b.br_blockcount =
2670 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002671 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002673 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002674 /*
2675 * Reset the cursor to the position of the new extent
2676 * we are about to insert as we can't trust it after
2677 * the previous insert.
2678 */
2679 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2680 new->br_startblock, new->br_blockcount,
2681 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002683 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002685 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002686 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002688 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
2690 break;
2691
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002692 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2693 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2694 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2695 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2696 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2697 case BMAP_LEFT_CONTIG:
2698 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 /*
2700 * These cases are all impossible.
2701 */
2702 ASSERT(0);
2703 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002704
2705 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002706 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002707 int tmp_logflags; /* partial log flag return val */
2708
2709 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002710 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002711 0, &tmp_logflags, XFS_DATA_FORK);
2712 *logflagsp |= tmp_logflags;
2713 if (error)
2714 goto done;
2715 }
2716
2717 /* clear out the allocated field, done with it now in any case. */
2718 if (cur) {
2719 cur->bc_private.b.allocated = 0;
2720 *curp = cur;
2721 }
2722
2723 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002725 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 return error;
2727#undef LEFT
2728#undef RIGHT
2729#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
2732/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002733 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002735STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736xfs_bmap_add_extent_hole_delay(
2737 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002738 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002739 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002741 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2743 xfs_filblks_t newlen=0; /* new indirect size */
2744 xfs_filblks_t oldlen=0; /* old indirect size */
2745 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2746 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002747 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002749 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002751 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002752
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 /*
2754 * Check and set flags if this segment has a left neighbor
2755 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002756 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002757 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002758 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002759
2760 if (isnullstartblock(left.br_startblock))
2761 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 /*
2765 * Check and set flags if the current (right) segment exists.
2766 * If it doesn't exist, we're converting the hole at end-of-file.
2767 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002768 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002769 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002770 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002771
2772 if (isnullstartblock(right.br_startblock))
2773 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 /*
2777 * Set contiguity flags on the left and right neighbors.
2778 * Don't let extents get too large, even if the pieces are contiguous.
2779 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002780 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2781 left.br_startoff + left.br_blockcount == new->br_startoff &&
2782 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2783 state |= BMAP_LEFT_CONTIG;
2784
2785 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2786 new->br_startoff + new->br_blockcount == right.br_startoff &&
2787 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2788 (!(state & BMAP_LEFT_CONTIG) ||
2789 (left.br_blockcount + new->br_blockcount +
2790 right.br_blockcount <= MAXEXTLEN)))
2791 state |= BMAP_RIGHT_CONTIG;
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 /*
2794 * Switch out based on the contiguity flags.
2795 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002796 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2797 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 /*
2799 * New allocation is contiguous with delayed allocations
2800 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002801 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002803 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 temp = left.br_blockcount + new->br_blockcount +
2805 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002806
Christoph Hellwigec90c552011-05-23 08:52:53 +00002807 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2808 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002809 oldlen = startblockval(left.br_startblock) +
2810 startblockval(new->br_startblock) +
2811 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002813 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002814 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002815 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002816
Christoph Hellwigec90c552011-05-23 08:52:53 +00002817 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 break;
2819
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002820 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 /*
2822 * New allocation is contiguous with a delayed allocation
2823 * on the left.
2824 * Merge the new allocation with the left neighbor.
2825 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002826 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002828
2829 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2830 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002831 oldlen = startblockval(left.br_startblock) +
2832 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002834 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002835 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002836 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 break;
2838
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002839 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 /*
2841 * New allocation is contiguous with a delayed allocation
2842 * on the right.
2843 * Merge the new allocation with the right neighbor.
2844 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002845 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002847 oldlen = startblockval(new->br_startblock) +
2848 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002850 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2851 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002852 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002853 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 break;
2855
2856 case 0:
2857 /*
2858 * New allocation is not contiguous with another
2859 * delayed allocation.
2860 * Insert a new entry.
2861 */
2862 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002863 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 break;
2865 }
2866 if (oldlen != newlen) {
2867 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002868 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2869 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 /*
2871 * Nothing to do for disk quota accounting here.
2872 */
2873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874}
2875
2876/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002877 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 */
2879STATIC int /* error */
2880xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002881 struct xfs_bmalloca *bma,
2882 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002884 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 int i; /* temp state */
2887 xfs_ifork_t *ifp; /* inode fork pointer */
2888 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2889 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002890 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002892 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Eric Sandeenf1f96c42016-01-04 16:10:42 +11002894 mp = bma->ip->i_mount;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002895 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002896
Christoph Hellwigc6534242011-09-18 20:41:05 +00002897 ASSERT(bma->idx >= 0);
2898 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002899 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002900 ASSERT(!bma->cur ||
2901 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002902
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002903 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002904
2905 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002906 if (whichfork == XFS_ATTR_FORK)
2907 state |= BMAP_ATTRFORK;
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 /*
2910 * Check and set flags if this segment has a left neighbor.
2911 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002912 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002913 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002914 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002915 if (isnullstartblock(left.br_startblock))
2916 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 /*
2920 * Check and set flags if this segment has a current value.
2921 * Not true if we're inserting into the "hole" at eof.
2922 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002923 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002924 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002925 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002926 if (isnullstartblock(right.br_startblock))
2927 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 /*
2931 * We're inserting a real allocation between "left" and "right".
2932 * Set the contiguity flags. Don't let extents get too large.
2933 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002934 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2935 left.br_startoff + left.br_blockcount == new->br_startoff &&
2936 left.br_startblock + left.br_blockcount == new->br_startblock &&
2937 left.br_state == new->br_state &&
2938 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2939 state |= BMAP_LEFT_CONTIG;
2940
2941 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2942 new->br_startoff + new->br_blockcount == right.br_startoff &&
2943 new->br_startblock + new->br_blockcount == right.br_startblock &&
2944 new->br_state == right.br_state &&
2945 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2946 (!(state & BMAP_LEFT_CONTIG) ||
2947 left.br_blockcount + new->br_blockcount +
2948 right.br_blockcount <= MAXEXTLEN))
2949 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002951 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 /*
2953 * Select which case we're in here, and implement it.
2954 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002955 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2956 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 /*
2958 * New allocation is contiguous with real allocations on the
2959 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002960 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002962 --bma->idx;
2963 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2964 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 left.br_blockcount + new->br_blockcount +
2966 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002967 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002968
Christoph Hellwigc6534242011-09-18 20:41:05 +00002969 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002970
Christoph Hellwigc6534242011-09-18 20:41:05 +00002971 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
2972 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
2973 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002974 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002975 } else {
2976 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002977 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
2978 right.br_startblock, right.br_blockcount,
2979 &i);
2980 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002981 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002982 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002983 error = xfs_btree_delete(bma->cur, &i);
2984 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002985 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002986 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002987 error = xfs_btree_decrement(bma->cur, 0, &i);
2988 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002989 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002990 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002991 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002992 left.br_startblock,
2993 left.br_blockcount +
2994 new->br_blockcount +
2995 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002996 left.br_state);
2997 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002998 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003000 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003002 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 /*
3004 * New allocation is contiguous with a real allocation
3005 * on the left.
3006 * Merge the new allocation with the left neighbor.
3007 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003008 --bma->idx;
3009 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3010 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003012 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003013
Christoph Hellwigc6534242011-09-18 20:41:05 +00003014 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003015 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003016 } else {
3017 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003018 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3019 left.br_startblock, left.br_blockcount,
3020 &i);
3021 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003022 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003023 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003024 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003025 left.br_startblock,
3026 left.br_blockcount +
3027 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003028 left.br_state);
3029 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003030 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003032 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003034 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 /*
3036 * New allocation is contiguous with a real allocation
3037 * on the right.
3038 * Merge the new allocation with the right neighbor.
3039 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003040 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3041 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003042 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 new->br_blockcount + right.br_blockcount,
3044 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003045 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003046
Christoph Hellwigc6534242011-09-18 20:41:05 +00003047 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003048 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003049 } else {
3050 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003051 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003052 right.br_startoff,
3053 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003054 right.br_blockcount, &i);
3055 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003056 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003057 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003058 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003059 new->br_startblock,
3060 new->br_blockcount +
3061 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003062 right.br_state);
3063 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003064 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003066 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
3068 case 0:
3069 /*
3070 * New allocation is not contiguous with another
3071 * real allocation.
3072 * Insert a new entry.
3073 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003074 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3075 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3076 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3077 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003078 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003079 } else {
3080 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003081 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003082 new->br_startoff,
3083 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003084 new->br_blockcount, &i);
3085 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003086 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003087 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003088 bma->cur->bc_rec.b.br_state = new->br_state;
3089 error = xfs_btree_insert(bma->cur, &i);
3090 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003091 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003092 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003094 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003096
3097 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003098 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003099 int tmp_logflags; /* partial log flag return val */
3100
Christoph Hellwigc6534242011-09-18 20:41:05 +00003101 ASSERT(bma->cur == NULL);
3102 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003103 bma->firstblock, bma->dfops, &bma->cur,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003104 0, &tmp_logflags, whichfork);
3105 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003106 if (error)
3107 goto done;
3108 }
3109
3110 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003111 if (bma->cur)
3112 bma->cur->bc_private.b.allocated = 0;
3113
3114 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003115done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003116 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003117 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118}
3119
Nathan Scottdd9f4382006-01-11 15:28:28 +11003120/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003121 * Functions used in the extent read, allocate and remove paths
3122 */
3123
3124/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003125 * Adjust the size of the new extent based on di_extsize and rt extsize.
3126 */
Dave Chinner68988112013-08-12 20:49:42 +10003127int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003128xfs_bmap_extsize_align(
3129 xfs_mount_t *mp,
3130 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3131 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3132 xfs_extlen_t extsz, /* align to this extent size */
3133 int rt, /* is this a realtime inode? */
3134 int eof, /* is extent at end-of-file? */
3135 int delay, /* creating delalloc extent? */
3136 int convert, /* overwriting unwritten extent? */
3137 xfs_fileoff_t *offp, /* in/out: aligned offset */
3138 xfs_extlen_t *lenp) /* in/out: aligned length */
3139{
3140 xfs_fileoff_t orig_off; /* original offset */
3141 xfs_extlen_t orig_alen; /* original length */
3142 xfs_fileoff_t orig_end; /* original off+len */
3143 xfs_fileoff_t nexto; /* next file offset */
3144 xfs_fileoff_t prevo; /* previous file offset */
3145 xfs_fileoff_t align_off; /* temp for offset */
3146 xfs_extlen_t align_alen; /* temp for length */
3147 xfs_extlen_t temp; /* temp for calculations */
3148
3149 if (convert)
3150 return 0;
3151
3152 orig_off = align_off = *offp;
3153 orig_alen = align_alen = *lenp;
3154 orig_end = orig_off + orig_alen;
3155
3156 /*
3157 * If this request overlaps an existing extent, then don't
3158 * attempt to perform any additional alignment.
3159 */
3160 if (!delay && !eof &&
3161 (orig_off >= gotp->br_startoff) &&
3162 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3163 return 0;
3164 }
3165
3166 /*
3167 * If the file offset is unaligned vs. the extent size
3168 * we need to align it. This will be possible unless
3169 * the file was previously written with a kernel that didn't
3170 * perform this alignment, or if a truncate shot us in the
3171 * foot.
3172 */
3173 temp = do_mod(orig_off, extsz);
3174 if (temp) {
3175 align_alen += temp;
3176 align_off -= temp;
3177 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003178
3179 /* Same adjustment for the end of the requested area. */
3180 temp = (align_alen % extsz);
3181 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003182 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003183
3184 /*
3185 * For large extent hint sizes, the aligned extent might be larger than
3186 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3187 * the length back under MAXEXTLEN. The outer allocation loops handle
3188 * short allocation just fine, so it is safe to do this. We only want to
3189 * do it when we are forced to, though, because it means more allocation
3190 * operations are required.
3191 */
3192 while (align_alen > MAXEXTLEN)
3193 align_alen -= extsz;
3194 ASSERT(align_alen <= MAXEXTLEN);
3195
Nathan Scottdd9f4382006-01-11 15:28:28 +11003196 /*
3197 * If the previous block overlaps with this proposed allocation
3198 * then move the start forward without adjusting the length.
3199 */
3200 if (prevp->br_startoff != NULLFILEOFF) {
3201 if (prevp->br_startblock == HOLESTARTBLOCK)
3202 prevo = prevp->br_startoff;
3203 else
3204 prevo = prevp->br_startoff + prevp->br_blockcount;
3205 } else
3206 prevo = 0;
3207 if (align_off != orig_off && align_off < prevo)
3208 align_off = prevo;
3209 /*
3210 * If the next block overlaps with this proposed allocation
3211 * then move the start back without adjusting the length,
3212 * but not before offset 0.
3213 * This may of course make the start overlap previous block,
3214 * and if we hit the offset 0 limit then the next block
3215 * can still overlap too.
3216 */
3217 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3218 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3219 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3220 nexto = gotp->br_startoff + gotp->br_blockcount;
3221 else
3222 nexto = gotp->br_startoff;
3223 } else
3224 nexto = NULLFILEOFF;
3225 if (!eof &&
3226 align_off + align_alen != orig_end &&
3227 align_off + align_alen > nexto)
3228 align_off = nexto > align_alen ? nexto - align_alen : 0;
3229 /*
3230 * If we're now overlapping the next or previous extent that
3231 * means we can't fit an extsz piece in this hole. Just move
3232 * the start forward to the first valid spot and set
3233 * the length so we hit the end.
3234 */
3235 if (align_off != orig_off && align_off < prevo)
3236 align_off = prevo;
3237 if (align_off + align_alen != orig_end &&
3238 align_off + align_alen > nexto &&
3239 nexto != NULLFILEOFF) {
3240 ASSERT(nexto > prevo);
3241 align_alen = nexto - align_off;
3242 }
3243
3244 /*
3245 * If realtime, and the result isn't a multiple of the realtime
3246 * extent size we need to remove blocks until it is.
3247 */
3248 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3249 /*
3250 * We're not covering the original request, or
3251 * we won't be able to once we fix the length.
3252 */
3253 if (orig_off < align_off ||
3254 orig_end > align_off + align_alen ||
3255 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003256 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003257 /*
3258 * Try to fix it by moving the start up.
3259 */
3260 if (align_off + temp <= orig_off) {
3261 align_alen -= temp;
3262 align_off += temp;
3263 }
3264 /*
3265 * Try to fix it by moving the end in.
3266 */
3267 else if (align_off + align_alen - temp >= orig_end)
3268 align_alen -= temp;
3269 /*
3270 * Set the start to the minimum then trim the length.
3271 */
3272 else {
3273 align_alen -= orig_off - align_off;
3274 align_off = orig_off;
3275 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3276 }
3277 /*
3278 * Result doesn't cover the request, fail it.
3279 */
3280 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003281 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003282 } else {
3283 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003284 /* see MAXEXTLEN handling above */
3285 ASSERT(orig_end <= align_off + align_alen ||
3286 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003287 }
3288
3289#ifdef DEBUG
3290 if (!eof && gotp->br_startoff != NULLFILEOFF)
3291 ASSERT(align_off + align_alen <= gotp->br_startoff);
3292 if (prevp->br_startoff != NULLFILEOFF)
3293 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3294#endif
3295
3296 *lenp = align_alen;
3297 *offp = align_off;
3298 return 0;
3299}
3300
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301#define XFS_ALLOC_GAP_UNITS 4
3302
Dave Chinner68988112013-08-12 20:49:42 +10003303void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003304xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003305 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306{
3307 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3309 xfs_mount_t *mp; /* mount point structure */
3310 int nullfb; /* true if ap->firstblock isn't set */
3311 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312
3313#define ISVALID(x,y) \
3314 (rt ? \
3315 (x) < mp->m_sb.sb_rblocks : \
3316 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3317 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3318 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3319
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003321 nullfb = *ap->firstblock == NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003323 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 /*
3325 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003326 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003328 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3329 !isnullstartblock(ap->prev.br_startblock) &&
3330 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3331 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003332 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 /*
3334 * Adjust for the gap between prevp and us.
3335 */
Dave Chinner3a756672011-09-18 20:40:58 +00003336 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003337 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003339 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3340 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 }
3342 /*
3343 * If not at eof, then compare the two neighbor blocks.
3344 * Figure out whether either one gives us a good starting point,
3345 * and pick the better one.
3346 */
3347 else if (!ap->eof) {
3348 xfs_fsblock_t gotbno; /* right side block number */
3349 xfs_fsblock_t gotdiff=0; /* right side difference */
3350 xfs_fsblock_t prevbno; /* left side block number */
3351 xfs_fsblock_t prevdiff=0; /* left side difference */
3352
3353 /*
3354 * If there's a previous (left) block, select a requested
3355 * start block based on it.
3356 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003357 if (ap->prev.br_startoff != NULLFILEOFF &&
3358 !isnullstartblock(ap->prev.br_startblock) &&
3359 (prevbno = ap->prev.br_startblock +
3360 ap->prev.br_blockcount) &&
3361 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 /*
3363 * Calculate gap to end of previous block.
3364 */
Dave Chinner3a756672011-09-18 20:40:58 +00003365 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003366 (ap->prev.br_startoff +
3367 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 /*
3369 * Figure the startblock based on the previous block's
3370 * end and the gap size.
3371 * Heuristic!
3372 * If the gap is large relative to the piece we're
3373 * allocating, or using it gives us an invalid block
3374 * number, then just use the end of the previous block.
3375 */
Dave Chinner3a756672011-09-18 20:40:58 +00003376 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003378 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 prevbno += adjust;
3380 else
3381 prevdiff += adjust;
3382 /*
3383 * If the firstblock forbids it, can't use it,
3384 * must use default.
3385 */
3386 if (!rt && !nullfb &&
3387 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3388 prevbno = NULLFSBLOCK;
3389 }
3390 /*
3391 * No previous block or can't follow it, just default.
3392 */
3393 else
3394 prevbno = NULLFSBLOCK;
3395 /*
3396 * If there's a following (right) block, select a requested
3397 * start block based on it.
3398 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003399 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 /*
3401 * Calculate gap to start of next block.
3402 */
Dave Chinner3a756672011-09-18 20:40:58 +00003403 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 /*
3405 * Figure the startblock based on the next block's
3406 * start and the gap size.
3407 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003408 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 /*
3410 * Heuristic!
3411 * If the gap is large relative to the piece we're
3412 * allocating, or using it gives us an invalid block
3413 * number, then just use the start of the next block
3414 * offset by our length.
3415 */
Dave Chinner3a756672011-09-18 20:40:58 +00003416 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 ISVALID(gotbno - gotdiff, gotbno))
3418 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003419 else if (ISVALID(gotbno - ap->length, gotbno)) {
3420 gotbno -= ap->length;
3421 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 } else
3423 gotdiff += adjust;
3424 /*
3425 * If the firstblock forbids it, can't use it,
3426 * must use default.
3427 */
3428 if (!rt && !nullfb &&
3429 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3430 gotbno = NULLFSBLOCK;
3431 }
3432 /*
3433 * No next block, just default.
3434 */
3435 else
3436 gotbno = NULLFSBLOCK;
3437 /*
3438 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003439 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 */
3441 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003442 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003444 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003446 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003448#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003449}
3450
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003451static int
3452xfs_bmap_longest_free_extent(
3453 struct xfs_trans *tp,
3454 xfs_agnumber_t ag,
3455 xfs_extlen_t *blen,
3456 int *notinit)
3457{
3458 struct xfs_mount *mp = tp->t_mountp;
3459 struct xfs_perag *pag;
3460 xfs_extlen_t longest;
3461 int error = 0;
3462
3463 pag = xfs_perag_get(mp, ag);
3464 if (!pag->pagf_init) {
3465 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3466 if (error)
3467 goto out;
3468
3469 if (!pag->pagf_init) {
3470 *notinit = 1;
3471 goto out;
3472 }
3473 }
3474
Dave Chinner50adbcb2015-06-22 10:04:31 +10003475 longest = xfs_alloc_longest_free_extent(mp, pag,
Dave Chinner496817b2015-06-22 10:13:30 +10003476 xfs_alloc_min_freelist(mp, pag));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003477 if (*blen < longest)
3478 *blen = longest;
3479
3480out:
3481 xfs_perag_put(pag);
3482 return error;
3483}
3484
3485static void
3486xfs_bmap_select_minlen(
3487 struct xfs_bmalloca *ap,
3488 struct xfs_alloc_arg *args,
3489 xfs_extlen_t *blen,
3490 int notinit)
3491{
3492 if (notinit || *blen < ap->minlen) {
3493 /*
3494 * Since we did a BUF_TRYLOCK above, it is possible that
3495 * there is space for this request.
3496 */
3497 args->minlen = ap->minlen;
3498 } else if (*blen < args->maxlen) {
3499 /*
3500 * If the best seen length is less than the request length,
3501 * use the best as the minimum.
3502 */
3503 args->minlen = *blen;
3504 } else {
3505 /*
3506 * Otherwise we've seen an extent as big as maxlen, use that
3507 * as the minimum.
3508 */
3509 args->minlen = args->maxlen;
3510 }
3511}
3512
Nathan Scotta365bdd2006-03-14 13:34:16 +11003513STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003514xfs_bmap_btalloc_nullfb(
3515 struct xfs_bmalloca *ap,
3516 struct xfs_alloc_arg *args,
3517 xfs_extlen_t *blen)
3518{
3519 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003520 xfs_agnumber_t ag, startag;
3521 int notinit = 0;
3522 int error;
3523
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003524 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003525 args->total = ap->total;
3526
Christoph Hellwigc467c042010-02-15 23:34:42 +00003527 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3528 if (startag == NULLAGNUMBER)
3529 startag = ag = 0;
3530
Dave Chinner14b064c2011-01-27 12:16:28 +11003531 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003532 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3533 &notinit);
3534 if (error)
3535 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003536
Christoph Hellwigc467c042010-02-15 23:34:42 +00003537 if (++ag == mp->m_sb.sb_agcount)
3538 ag = 0;
3539 if (ag == startag)
3540 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003541 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003542
3543 xfs_bmap_select_minlen(ap, args, blen, notinit);
3544 return 0;
3545}
3546
3547STATIC int
3548xfs_bmap_btalloc_filestreams(
3549 struct xfs_bmalloca *ap,
3550 struct xfs_alloc_arg *args,
3551 xfs_extlen_t *blen)
3552{
3553 struct xfs_mount *mp = ap->ip->i_mount;
3554 xfs_agnumber_t ag;
3555 int notinit = 0;
3556 int error;
3557
3558 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3559 args->total = ap->total;
3560
3561 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3562 if (ag == NULLAGNUMBER)
3563 ag = 0;
3564
3565 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3566 if (error)
3567 return error;
3568
3569 if (*blen < args->maxlen) {
3570 error = xfs_filestream_new_ag(ap, &ag);
3571 if (error)
3572 return error;
3573
3574 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3575 &notinit);
3576 if (error)
3577 return error;
3578
3579 }
3580
3581 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003582
3583 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003584 * Set the failure fallback case to look in the selected AG as stream
3585 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003586 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003587 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003588 return 0;
3589}
3590
3591STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003592xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003593 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003594{
3595 xfs_mount_t *mp; /* mount point structure */
3596 xfs_alloctype_t atype = 0; /* type for allocation routines */
3597 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003598 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003599 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003600 xfs_alloc_arg_t args;
3601 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003602 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003603 int nullfb; /* true if ap->firstblock isn't set */
3604 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003605 int tryagain;
3606 int error;
Dave Chinner33177f02013-12-12 16:34:36 +11003607 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003608
Dave Chinnera99ebf42011-12-01 11:24:20 +00003609 ASSERT(ap->length);
3610
Nathan Scotta365bdd2006-03-14 13:34:16 +11003611 mp = ap->ip->i_mount;
Dave Chinner33177f02013-12-12 16:34:36 +11003612
3613 /* stripe alignment for allocation is determined by mount parameters */
3614 stripe_align = 0;
3615 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3616 stripe_align = mp->m_swidth;
3617 else if (mp->m_dalign)
3618 stripe_align = mp->m_dalign;
3619
David Chinner957d0eb2007-06-18 16:50:37 +10003620 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003621 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003622 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003623 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003624 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003625 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003626 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003627 }
Dave Chinner33177f02013-12-12 16:34:36 +11003628
3629
Dave Chinner0937e0f2011-09-18 20:40:57 +00003630 nullfb = *ap->firstblock == NULLFSBLOCK;
3631 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003632 if (nullfb) {
3633 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
3634 ag = xfs_filestream_lookup_ag(ap->ip);
3635 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003636 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003637 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003638 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003639 }
3640 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003641 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003642
3643 xfs_bmap_adjacent(ap);
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003646 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 * it's in the right allocation group.
3648 */
Dave Chinner3a756672011-09-18 20:40:58 +00003649 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 ;
3651 else
Dave Chinner3a756672011-09-18 20:40:58 +00003652 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 * Normal allocation, done through xfs_alloc_vextent.
3655 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003656 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003657 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003658 args.tp = ap->tp;
3659 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003660 args.fsbno = ap->blkno;
Dave Chinner14b064c2011-01-27 12:16:28 +11003661
3662 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner3a756672011-09-18 20:40:58 +00003663 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003664 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003665 blen = 0;
3666 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003667 /*
3668 * Search for an allocation group with a single extent large
3669 * enough for the request. If one isn't found, then adjust
3670 * the minimum allocation size to the largest space found.
3671 */
3672 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3673 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3674 else
3675 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003676 if (error)
3677 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003678 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003679 if (xfs_inode_is_filestream(ap->ip))
3680 args.type = XFS_ALLOCTYPE_FIRST_AG;
3681 else
3682 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003683 args.total = args.minlen = ap->minlen;
3684 } else {
3685 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3686 args.total = ap->total;
3687 args.minlen = ap->minlen;
3688 }
David Chinner957d0eb2007-06-18 16:50:37 +10003689 /* apply extent size hints if obtained earlier */
3690 if (unlikely(align)) {
3691 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003692 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003693 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003694 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003695 args.prod = 1;
3696 args.mod = 0;
3697 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003698 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003699 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003700 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3701 }
3702 /*
3703 * If we are not low on available data blocks, and the
3704 * underlying logical volume manager is a stripe, and
3705 * the file offset is zero then try to allocate data
3706 * blocks on stripe unit boundary.
3707 * NOTE: ap->aeof is only set if the allocation length
3708 * is >= the stripe unit and the allocation offset is
3709 * at the end of file.
3710 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003711 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003712 if (!ap->offset) {
Dave Chinner33177f02013-12-12 16:34:36 +11003713 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003714 atype = args.type;
3715 isaligned = 1;
3716 /*
3717 * Adjust for alignment
3718 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003719 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003720 args.minlen = blen - args.alignment;
3721 args.minalignslop = 0;
3722 } else {
3723 /*
3724 * First try an exact bno allocation.
3725 * If it fails then do a near or start bno
3726 * allocation with alignment turned on.
3727 */
3728 atype = args.type;
3729 tryagain = 1;
3730 args.type = XFS_ALLOCTYPE_THIS_BNO;
3731 args.alignment = 1;
3732 /*
3733 * Compute the minlen+alignment for the
3734 * next case. Set slop so that the value
3735 * of minlen+alignment+slop doesn't go up
3736 * between the calls.
3737 */
Dave Chinner33177f02013-12-12 16:34:36 +11003738 if (blen > stripe_align && blen <= args.maxlen)
3739 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003740 else
3741 nextminlen = args.minlen;
Dave Chinner33177f02013-12-12 16:34:36 +11003742 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003743 args.minalignslop =
Dave Chinner33177f02013-12-12 16:34:36 +11003744 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003745 args.minlen - 1;
3746 else
3747 args.minalignslop = 0;
3748 }
3749 } else {
3750 args.alignment = 1;
3751 args.minalignslop = 0;
3752 }
3753 args.minleft = ap->minleft;
3754 args.wasdel = ap->wasdel;
3755 args.isfl = 0;
3756 args.userdata = ap->userdata;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003757 if (ap->userdata & XFS_ALLOC_USERDATA_ZERO)
3758 args.ip = ap->ip;
3759
3760 error = xfs_alloc_vextent(&args);
3761 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003762 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003763
Nathan Scotta365bdd2006-03-14 13:34:16 +11003764 if (tryagain && args.fsbno == NULLFSBLOCK) {
3765 /*
3766 * Exact allocation failed. Now try with alignment
3767 * turned on.
3768 */
3769 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003770 args.fsbno = ap->blkno;
Dave Chinner33177f02013-12-12 16:34:36 +11003771 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003772 args.minlen = nextminlen;
3773 args.minalignslop = 0;
3774 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 if ((error = xfs_alloc_vextent(&args)))
3776 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003777 }
3778 if (isaligned && args.fsbno == NULLFSBLOCK) {
3779 /*
3780 * allocation failed, so turn off alignment and
3781 * try again.
3782 */
3783 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003784 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003785 args.alignment = 0;
3786 if ((error = xfs_alloc_vextent(&args)))
3787 return error;
3788 }
3789 if (args.fsbno == NULLFSBLOCK && nullfb &&
3790 args.minlen > ap->minlen) {
3791 args.minlen = ap->minlen;
3792 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003793 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003794 if ((error = xfs_alloc_vextent(&args)))
3795 return error;
3796 }
3797 if (args.fsbno == NULLFSBLOCK && nullfb) {
3798 args.fsbno = 0;
3799 args.type = XFS_ALLOCTYPE_FIRST_AG;
3800 args.total = ap->minlen;
3801 args.minleft = 0;
3802 if ((error = xfs_alloc_vextent(&args)))
3803 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003804 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003805 }
3806 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003807 /*
3808 * check the allocation happened at the same or higher AG than
3809 * the first block that was allocated.
3810 */
3811 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3812 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3813 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003814 (ap->dfops->dop_low &&
Dave Chinner0937e0f2011-09-18 20:40:57 +00003815 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3816 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3817
Dave Chinner3a756672011-09-18 20:40:58 +00003818 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003819 if (*ap->firstblock == NULLFSBLOCK)
3820 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003821 ASSERT(nullfb || fb_agno == args.agno ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003822 (ap->dfops->dop_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003823 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003824 ap->ip->i_d.di_nblocks += args.len;
3825 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3826 if (ap->wasdel)
3827 ap->ip->i_delayed_blks -= args.len;
3828 /*
3829 * Adjust the disk quota also. This was reserved
3830 * earlier.
3831 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003832 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003833 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3834 XFS_TRANS_DQ_BCOUNT,
3835 (long) args.len);
3836 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003837 ap->blkno = NULLFSBLOCK;
3838 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 }
3840 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003841}
3842
3843/*
3844 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3845 * It figures out where to ask the underlying allocator to put the new extent.
3846 */
3847STATIC int
3848xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003849 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003850{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003851 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003852 return xfs_bmap_rtalloc(ap);
3853 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854}
3855
3856/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003857 * Trim the returned map to the required bounds
3858 */
3859STATIC void
3860xfs_bmapi_trim_map(
3861 struct xfs_bmbt_irec *mval,
3862 struct xfs_bmbt_irec *got,
3863 xfs_fileoff_t *bno,
3864 xfs_filblks_t len,
3865 xfs_fileoff_t obno,
3866 xfs_fileoff_t end,
3867 int n,
3868 int flags)
3869{
3870 if ((flags & XFS_BMAPI_ENTIRE) ||
3871 got->br_startoff + got->br_blockcount <= obno) {
3872 *mval = *got;
3873 if (isnullstartblock(got->br_startblock))
3874 mval->br_startblock = DELAYSTARTBLOCK;
3875 return;
3876 }
3877
3878 if (obno > *bno)
3879 *bno = obno;
3880 ASSERT((*bno >= obno) || (n == 0));
3881 ASSERT(*bno < end);
3882 mval->br_startoff = *bno;
3883 if (isnullstartblock(got->br_startblock))
3884 mval->br_startblock = DELAYSTARTBLOCK;
3885 else
3886 mval->br_startblock = got->br_startblock +
3887 (*bno - got->br_startoff);
3888 /*
3889 * Return the minimum of what we got and what we asked for for
3890 * the length. We can use the len variable here because it is
3891 * modified below and we could have been there before coming
3892 * here if the first part of the allocation didn't overlap what
3893 * was asked for.
3894 */
3895 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3896 got->br_blockcount - (*bno - got->br_startoff));
3897 mval->br_state = got->br_state;
3898 ASSERT(mval->br_blockcount <= len);
3899 return;
3900}
3901
3902/*
3903 * Update and validate the extent map to return
3904 */
3905STATIC void
3906xfs_bmapi_update_map(
3907 struct xfs_bmbt_irec **map,
3908 xfs_fileoff_t *bno,
3909 xfs_filblks_t *len,
3910 xfs_fileoff_t obno,
3911 xfs_fileoff_t end,
3912 int *n,
3913 int flags)
3914{
3915 xfs_bmbt_irec_t *mval = *map;
3916
3917 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3918 ((mval->br_startoff + mval->br_blockcount) <= end));
3919 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3920 (mval->br_startoff < obno));
3921
3922 *bno = mval->br_startoff + mval->br_blockcount;
3923 *len = end - *bno;
3924 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3925 /* update previous map with new information */
3926 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3927 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3928 ASSERT(mval->br_state == mval[-1].br_state);
3929 mval[-1].br_blockcount = mval->br_blockcount;
3930 mval[-1].br_state = mval->br_state;
3931 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3932 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3933 mval[-1].br_startblock != HOLESTARTBLOCK &&
3934 mval->br_startblock == mval[-1].br_startblock +
3935 mval[-1].br_blockcount &&
3936 ((flags & XFS_BMAPI_IGSTATE) ||
3937 mval[-1].br_state == mval->br_state)) {
3938 ASSERT(mval->br_startoff ==
3939 mval[-1].br_startoff + mval[-1].br_blockcount);
3940 mval[-1].br_blockcount += mval->br_blockcount;
3941 } else if (*n > 0 &&
3942 mval->br_startblock == DELAYSTARTBLOCK &&
3943 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3944 mval->br_startoff ==
3945 mval[-1].br_startoff + mval[-1].br_blockcount) {
3946 mval[-1].br_blockcount += mval->br_blockcount;
3947 mval[-1].br_state = mval->br_state;
3948 } else if (!((*n == 0) &&
3949 ((mval->br_startoff + mval->br_blockcount) <=
3950 obno))) {
3951 mval++;
3952 (*n)++;
3953 }
3954 *map = mval;
3955}
3956
3957/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003958 * Map file blocks to filesystem blocks without allocation.
3959 */
3960int
3961xfs_bmapi_read(
3962 struct xfs_inode *ip,
3963 xfs_fileoff_t bno,
3964 xfs_filblks_t len,
3965 struct xfs_bmbt_irec *mval,
3966 int *nmap,
3967 int flags)
3968{
3969 struct xfs_mount *mp = ip->i_mount;
3970 struct xfs_ifork *ifp;
3971 struct xfs_bmbt_irec got;
3972 struct xfs_bmbt_irec prev;
3973 xfs_fileoff_t obno;
3974 xfs_fileoff_t end;
3975 xfs_extnum_t lastx;
3976 int error;
3977 int eof;
3978 int n = 0;
3979 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
3980 XFS_ATTR_FORK : XFS_DATA_FORK;
3981
3982 ASSERT(*nmap >= 1);
3983 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
3984 XFS_BMAPI_IGSTATE)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003985 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003986
3987 if (unlikely(XFS_TEST_ERROR(
3988 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3989 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
3990 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
3991 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003992 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003993 }
3994
3995 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003996 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003997
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003998 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003999
4000 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004001
4002 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4003 error = xfs_iread_extents(NULL, ip, whichfork);
4004 if (error)
4005 return error;
4006 }
4007
4008 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4009 end = bno + len;
4010 obno = bno;
4011
4012 while (bno < end && n < *nmap) {
4013 /* Reading past eof, act as though there's a hole up to end. */
4014 if (eof)
4015 got.br_startoff = end;
4016 if (got.br_startoff > bno) {
4017 /* Reading in a hole. */
4018 mval->br_startoff = bno;
4019 mval->br_startblock = HOLESTARTBLOCK;
4020 mval->br_blockcount =
4021 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4022 mval->br_state = XFS_EXT_NORM;
4023 bno += mval->br_blockcount;
4024 len -= mval->br_blockcount;
4025 mval++;
4026 n++;
4027 continue;
4028 }
4029
4030 /* set up the extent map to return. */
4031 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4032 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4033
4034 /* If we're done, stop now. */
4035 if (bno >= end || n >= *nmap)
4036 break;
4037
4038 /* Else go on to the next record. */
4039 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4040 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4041 else
4042 eof = 1;
4043 }
4044 *nmap = n;
4045 return 0;
4046}
4047
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004048STATIC int
4049xfs_bmapi_reserve_delalloc(
4050 struct xfs_inode *ip,
4051 xfs_fileoff_t aoff,
4052 xfs_filblks_t len,
4053 struct xfs_bmbt_irec *got,
4054 struct xfs_bmbt_irec *prev,
4055 xfs_extnum_t *lastx,
4056 int eof)
4057{
4058 struct xfs_mount *mp = ip->i_mount;
4059 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4060 xfs_extlen_t alen;
4061 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004062 char rt = XFS_IS_REALTIME_INODE(ip);
4063 xfs_extlen_t extsz;
4064 int error;
4065
4066 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4067 if (!eof)
4068 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4069
4070 /* Figure out the extent size, adjust alen */
4071 extsz = xfs_get_extsz_hint(ip);
4072 if (extsz) {
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004073 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4074 1, 0, &aoff, &alen);
4075 ASSERT(!error);
4076 }
4077
4078 if (rt)
4079 extsz = alen / mp->m_sb.sb_rextsize;
4080
4081 /*
4082 * Make a transaction-less quota reservation for delayed allocation
4083 * blocks. This number gets adjusted later. We return if we haven't
4084 * allocated blocks already inside this loop.
4085 */
4086 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4087 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4088 if (error)
4089 return error;
4090
4091 /*
4092 * Split changing sb for alen and indlen since they could be coming
4093 * from different places.
4094 */
4095 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4096 ASSERT(indlen > 0);
4097
4098 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004099 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004100 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004101 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004102 }
4103
4104 if (error)
4105 goto out_unreserve_quota;
4106
Dave Chinner0d485ad2015-02-23 21:22:03 +11004107 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004108 if (error)
4109 goto out_unreserve_blocks;
4110
4111
4112 ip->i_delayed_blks += alen;
4113
4114 got->br_startoff = aoff;
4115 got->br_startblock = nullstartblock(indlen);
4116 got->br_blockcount = alen;
4117 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004118 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004119
4120 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004121 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4122 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004123 */
4124 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4125
4126 ASSERT(got->br_startoff <= aoff);
4127 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4128 ASSERT(isnullstartblock(got->br_startblock));
4129 ASSERT(got->br_state == XFS_EXT_NORM);
4130 return 0;
4131
4132out_unreserve_blocks:
4133 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004134 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004135 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004136 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004137out_unreserve_quota:
4138 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004139 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004140 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4141 return error;
4142}
4143
Dave Chinner5c8ed202011-09-18 20:40:45 +00004144/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004145 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4146 */
4147int
4148xfs_bmapi_delay(
4149 struct xfs_inode *ip, /* incore inode */
4150 xfs_fileoff_t bno, /* starting file offs. mapped */
4151 xfs_filblks_t len, /* length to map in file */
4152 struct xfs_bmbt_irec *mval, /* output: map values */
4153 int *nmap, /* i/o: mval size/count */
4154 int flags) /* XFS_BMAPI_... */
4155{
4156 struct xfs_mount *mp = ip->i_mount;
4157 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4158 struct xfs_bmbt_irec got; /* current file extent record */
4159 struct xfs_bmbt_irec prev; /* previous file extent record */
4160 xfs_fileoff_t obno; /* old block number (offset) */
4161 xfs_fileoff_t end; /* end of mapped file region */
4162 xfs_extnum_t lastx; /* last useful extent number */
4163 int eof; /* we've hit the end of extents */
4164 int n = 0; /* current extent index */
4165 int error = 0;
4166
4167 ASSERT(*nmap >= 1);
4168 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4169 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004170 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig44032802011-09-18 20:40:48 +00004171
4172 if (unlikely(XFS_TEST_ERROR(
4173 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4174 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4175 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4176 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004177 return -EFSCORRUPTED;
Christoph Hellwig44032802011-09-18 20:40:48 +00004178 }
4179
4180 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004181 return -EIO;
Christoph Hellwig44032802011-09-18 20:40:48 +00004182
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004183 XFS_STATS_INC(mp, xs_blk_mapw);
Christoph Hellwig44032802011-09-18 20:40:48 +00004184
4185 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4186 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4187 if (error)
4188 return error;
4189 }
4190
4191 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4192 end = bno + len;
4193 obno = bno;
4194
4195 while (bno < end && n < *nmap) {
4196 if (eof || got.br_startoff > bno) {
4197 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4198 &prev, &lastx, eof);
4199 if (error) {
4200 if (n == 0) {
4201 *nmap = 0;
4202 return error;
4203 }
4204 break;
4205 }
4206 }
4207
4208 /* set up the extent map to return. */
4209 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4210 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4211
4212 /* If we're done, stop now. */
4213 if (bno >= end || n >= *nmap)
4214 break;
4215
4216 /* Else go on to the next record. */
4217 prev = got;
4218 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4219 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4220 else
4221 eof = 1;
4222 }
4223
4224 *nmap = n;
4225 return 0;
4226}
4227
4228
Dave Chinnercf11da92014-07-15 07:08:24 +10004229static int
4230xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004231 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004232{
4233 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004234 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004235 XFS_ATTR_FORK : XFS_DATA_FORK;
4236 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004237 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004238 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004239
Dave Chinnera99ebf42011-12-01 11:24:20 +00004240 ASSERT(bma->length > 0);
4241
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004242 /*
4243 * For the wasdelay case, we could also just allocate the stuff asked
4244 * for in this bmap call but that wouldn't be as good.
4245 */
4246 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004247 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4248 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004249 if (bma->idx != NULLEXTNUM && bma->idx) {
4250 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004251 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004252 }
4253 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004254 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004255 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004256 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004257 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004258 }
4259
4260 /*
4261 * Indicate if this is the first user data in the file, or just any
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004262 * user data. And if it is userdata, indicate whether it needs to
4263 * be initialised to zero during allocation.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004264 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004265 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004266 bma->userdata = (bma->offset == 0) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004267 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004268 if (bma->flags & XFS_BMAPI_ZERO)
4269 bma->userdata |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004270 }
4271
Dave Chinnere04426b2012-10-05 11:06:59 +10004272 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004273
4274 /*
4275 * Only want to do the alignment at the eof if it is userdata and
4276 * allocation length is larger than a stripe unit.
4277 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004278 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004279 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004280 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004281 if (error)
4282 return error;
4283 }
4284
4285 error = xfs_bmap_alloc(bma);
4286 if (error)
4287 return error;
4288
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004289 if (bma->dfops->dop_low)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004290 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004291 if (bma->cur)
4292 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004293 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004294 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004295 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4296 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4297 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004298 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004299 }
4300 /*
4301 * Bump the number of extents we've allocated
4302 * in this call.
4303 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004304 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004305
Dave Chinner29c8d172011-09-18 20:41:00 +00004306 if (bma->cur)
4307 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004308 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4309
Dave Chinner963c30c2011-09-18 20:40:59 +00004310 bma->got.br_startoff = bma->offset;
4311 bma->got.br_startblock = bma->blkno;
4312 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004313 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004314
4315 /*
4316 * A wasdelay extent has been initialized, so shouldn't be flagged
4317 * as unwritten.
4318 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004319 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004320 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004321 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004322
Christoph Hellwigc6534242011-09-18 20:41:05 +00004323 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004324 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004325 else
4326 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004327
Christoph Hellwigc315c902011-09-18 20:41:02 +00004328 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004329 if (error)
4330 return error;
4331
4332 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004333 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4334 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4335 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004336 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004337 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004338
Dave Chinner963c30c2011-09-18 20:40:59 +00004339 ASSERT(bma->got.br_startoff <= bma->offset);
4340 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4341 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004342 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4343 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004344 return 0;
4345}
4346
Dave Chinnerb447fe52011-09-18 20:40:51 +00004347STATIC int
4348xfs_bmapi_convert_unwritten(
4349 struct xfs_bmalloca *bma,
4350 struct xfs_bmbt_irec *mval,
4351 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004352 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004353{
4354 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4355 XFS_ATTR_FORK : XFS_DATA_FORK;
4356 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004357 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004358 int error;
4359
Dave Chinnerb447fe52011-09-18 20:40:51 +00004360 /* check if we need to do unwritten->real conversion */
4361 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4362 (flags & XFS_BMAPI_PREALLOC))
4363 return 0;
4364
4365 /* check if we need to do real->unwritten conversion */
4366 if (mval->br_state == XFS_EXT_NORM &&
4367 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4368 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4369 return 0;
4370
4371 /*
4372 * Modify (by adding) the state flag, if writing.
4373 */
4374 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004375 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4376 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004377 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004378 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004379 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004380 }
4381 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4382 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4383
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004384 /*
4385 * Before insertion into the bmbt, zero the range being converted
4386 * if required.
4387 */
4388 if (flags & XFS_BMAPI_ZERO) {
4389 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4390 mval->br_blockcount);
4391 if (error)
4392 return error;
4393 }
4394
Dave Chinnere0c3da52011-09-18 20:41:01 +00004395 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004396 &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004397 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004398 /*
4399 * Log the inode core unconditionally in the unwritten extent conversion
4400 * path because the conversion might not have done so (e.g., if the
4401 * extent count hasn't changed). We need to make sure the inode is dirty
4402 * in the transaction for the sake of fsync(), even if nothing has
4403 * changed, because fsync() will not force the log for this transaction
4404 * unless it sees the inode pinned.
4405 */
4406 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004407 if (error)
4408 return error;
4409
4410 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004411 * Update our extent pointer, given that
4412 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4413 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004414 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004415 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004416
4417 /*
4418 * We may have combined previously unwritten space with written space,
4419 * so generate another request.
4420 */
4421 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004422 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004423 return 0;
4424}
4425
Christoph Hellwig44032802011-09-18 20:40:48 +00004426/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004427 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4428 * extent state if necessary. Details behaviour is controlled by the flags
4429 * parameter. Only allocates blocks from a single allocation group, to avoid
4430 * locking problems.
4431 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 * The returned value in "firstblock" from the first call in a transaction
4433 * must be remembered and presented to subsequent calls in "firstblock".
4434 * An upper bound for the number of blocks to be allocated is supplied to
4435 * the first call in "total"; if no allocation group has that many free
4436 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4437 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004438int
4439xfs_bmapi_write(
4440 struct xfs_trans *tp, /* transaction pointer */
4441 struct xfs_inode *ip, /* incore inode */
4442 xfs_fileoff_t bno, /* starting file offs. mapped */
4443 xfs_filblks_t len, /* length to map in file */
4444 int flags, /* XFS_BMAPI_... */
4445 xfs_fsblock_t *firstblock, /* first allocated block
4446 controls a.g. for allocs */
4447 xfs_extlen_t total, /* total blocks needed */
4448 struct xfs_bmbt_irec *mval, /* output: map values */
4449 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004450 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004452 struct xfs_mount *mp = ip->i_mount;
4453 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004454 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004455 xfs_fileoff_t end; /* end of mapped file region */
4456 int eof; /* after the end of extents */
4457 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004458 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004459 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004460 int whichfork; /* data or attr fork */
4461 char inhole; /* current location is hole in file */
4462 char wasdelay; /* old extent was delayed */
4463
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004465 xfs_fileoff_t orig_bno; /* original block number value */
4466 int orig_flags; /* original flags arg value */
4467 xfs_filblks_t orig_len; /* original value of len arg */
4468 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4469 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
4471 orig_bno = bno;
4472 orig_len = len;
4473 orig_flags = flags;
4474 orig_mval = mval;
4475 orig_nmap = *nmap;
4476#endif
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004477 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4478 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004479
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004481 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4482 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4483 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004484 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004485 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004486 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004487
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004488 /* zeroing is for currently only for data extents, not metadata */
4489 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4490 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4491 /*
4492 * we can allocate unwritten extents or pre-zero allocated blocks,
4493 * but it makes no sense to do both at once. This would result in
4494 * zeroing the unwritten extent twice, but it still being an
4495 * unwritten extent....
4496 */
4497 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4498 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 if (unlikely(XFS_TEST_ERROR(
4501 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004502 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004504 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004505 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004507
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004509 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004510
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004512
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004513 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004514
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004515 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004517 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004519 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004520 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004521 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004522 }
4523
4524 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4525 error = xfs_iread_extents(tp, ip, whichfork);
4526 if (error)
4527 goto error0;
4528 }
4529
Dave Chinnere0c3da52011-09-18 20:41:01 +00004530 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004531 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 n = 0;
4533 end = bno + len;
4534 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004535
4536 bma.tp = tp;
4537 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004538 bma.total = total;
4539 bma.userdata = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004540 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004541 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004542
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004544 inhole = eof || bma.got.br_startoff > bno;
4545 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004546
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 /*
4548 * First, deal with the hole before the allocated space
4549 * that we found, if any.
4550 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004551 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004552 bma.eof = eof;
4553 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4554 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004555 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004556 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004557
Dave Chinnera99ebf42011-12-01 11:24:20 +00004558 /*
4559 * There's a 32/64 bit type mismatch between the
4560 * allocation length request (which can be 64 bits in
4561 * length) and the bma length request, which is
4562 * xfs_extlen_t and therefore 32 bits. Hence we have to
4563 * check for 32-bit overflows and handle them here.
4564 */
4565 if (len > (xfs_filblks_t)MAXEXTLEN)
4566 bma.length = MAXEXTLEN;
4567 else
4568 bma.length = len;
4569
4570 ASSERT(len > 0);
4571 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004572 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 if (error)
4574 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004575 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004576 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004578
Dave Chinneraef9a892011-09-18 20:40:44 +00004579 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004580 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4581 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
Dave Chinnerb447fe52011-09-18 20:40:51 +00004583 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004584 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004585 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004586 continue;
4587 if (error)
4588 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
Dave Chinneraef9a892011-09-18 20:40:44 +00004590 /* update the extent map to return */
4591 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 /*
4594 * If we're done, stop now. Stop when we've allocated
4595 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4596 * the transaction may get too big.
4597 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004598 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004600
4601 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004602 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004603 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4604 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4605 &bma.got);
4606 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00004607 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 /*
4612 * Transform from btree to extents, give it cur.
4613 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004614 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004615 int tmp_logflags = 0;
4616
Dave Chinner29c8d172011-09-18 20:41:00 +00004617 ASSERT(bma.cur);
4618 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004620 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 if (error)
4622 goto error0;
4623 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004624
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004626 XFS_IFORK_NEXTENTS(ip, whichfork) >
4627 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629error0:
4630 /*
4631 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004632 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004634 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004636 bma.logflags &= ~xfs_ilog_fext(whichfork);
4637 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004639 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 /*
4641 * Log whatever the flags say, even if error. Otherwise we might miss
4642 * detecting a case where the data is changed, there's an error,
4643 * and it's not logged so we don't shutdown when we should.
4644 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004645 if (bma.logflags)
4646 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004647
Dave Chinner29c8d172011-09-18 20:41:00 +00004648 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 if (!error) {
4650 ASSERT(*firstblock == NULLFSBLOCK ||
4651 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4652 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004653 bma.cur->bc_private.b.firstblock) ||
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004654 (dfops->dop_low &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 XFS_FSB_TO_AGNO(mp, *firstblock) <
4656 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004657 bma.cur->bc_private.b.firstblock)));
4658 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004660 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4662 }
4663 if (!error)
4664 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4665 orig_nmap, *nmap);
4666 return error;
4667}
4668
4669/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004670 * When a delalloc extent is split (e.g., due to a hole punch), the original
4671 * indlen reservation must be shared across the two new extents that are left
4672 * behind.
4673 *
4674 * Given the original reservation and the worst case indlen for the two new
4675 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004676 * reservation fairly across the two new extents. If necessary, steal available
4677 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4678 * ores == 1). The number of stolen blocks is returned. The availability and
4679 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004680 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004681static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004682xfs_bmap_split_indlen(
4683 xfs_filblks_t ores, /* original res. */
4684 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004685 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4686 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004687{
4688 xfs_filblks_t len1 = *indlen1;
4689 xfs_filblks_t len2 = *indlen2;
4690 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004691 xfs_filblks_t stolen = 0;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004692
4693 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004694 * Steal as many blocks as we can to try and satisfy the worst case
4695 * indlen for both new extents.
4696 */
4697 while (nres > ores && avail) {
4698 nres--;
4699 avail--;
4700 stolen++;
4701 }
4702
4703 /*
4704 * The only blocks available are those reserved for the original
4705 * extent and what we can steal from the extent being removed.
4706 * If this still isn't enough to satisfy the combined
4707 * requirements for the two new extents, skim blocks off of each
4708 * of the new reservations until they match what is available.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004709 */
4710 while (nres > ores) {
4711 if (len1) {
4712 len1--;
4713 nres--;
4714 }
4715 if (nres == ores)
4716 break;
4717 if (len2) {
4718 len2--;
4719 nres--;
4720 }
4721 }
4722
4723 *indlen1 = len1;
4724 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004725
4726 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004727}
4728
4729/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004730 * Called by xfs_bmapi to update file extent records and the btree
4731 * after removing space (or undoing a delayed allocation).
4732 */
4733STATIC int /* error */
4734xfs_bmap_del_extent(
4735 xfs_inode_t *ip, /* incore inode pointer */
4736 xfs_trans_t *tp, /* current transaction pointer */
4737 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004738 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004739 xfs_btree_cur_t *cur, /* if null, not a btree */
4740 xfs_bmbt_irec_t *del, /* data to remove from extents */
4741 int *logflagsp, /* inode logging flags */
4742 int whichfork) /* data or attr fork */
4743{
4744 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
4745 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
4746 xfs_fsblock_t del_endblock=0; /* first block past del */
4747 xfs_fileoff_t del_endoff; /* first offset past del */
4748 int delay; /* current block is delayed allocated */
4749 int do_fx; /* free extent at end of routine */
4750 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
4751 int error; /* error return value */
4752 int flags; /* inode logging flags */
4753 xfs_bmbt_irec_t got; /* current extent entry */
4754 xfs_fileoff_t got_endoff; /* first offset past got */
4755 int i; /* temp state */
4756 xfs_ifork_t *ifp; /* inode fork pointer */
4757 xfs_mount_t *mp; /* mount structure */
4758 xfs_filblks_t nblks; /* quota/sb block count */
4759 xfs_bmbt_irec_t new; /* new record to be inserted */
4760 /* REFERENCED */
4761 uint qfield; /* quota field to update */
4762 xfs_filblks_t temp; /* for indirect length calculations */
4763 xfs_filblks_t temp2; /* for indirect length calculations */
4764 int state = 0;
4765
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004766 mp = ip->i_mount;
4767 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004768
4769 if (whichfork == XFS_ATTR_FORK)
4770 state |= BMAP_ATTRFORK;
4771
Dave Chinner9e5987a2013-02-25 12:31:26 +11004772 ifp = XFS_IFORK_PTR(ip, whichfork);
4773 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4774 (uint)sizeof(xfs_bmbt_rec_t)));
4775 ASSERT(del->br_blockcount > 0);
4776 ep = xfs_iext_get_ext(ifp, *idx);
4777 xfs_bmbt_get_all(ep, &got);
4778 ASSERT(got.br_startoff <= del->br_startoff);
4779 del_endoff = del->br_startoff + del->br_blockcount;
4780 got_endoff = got.br_startoff + got.br_blockcount;
4781 ASSERT(got_endoff >= del_endoff);
4782 delay = isnullstartblock(got.br_startblock);
4783 ASSERT(isnullstartblock(del->br_startblock) == delay);
4784 flags = 0;
4785 qfield = 0;
4786 error = 0;
4787 /*
4788 * If deleting a real allocation, must free up the disk space.
4789 */
4790 if (!delay) {
4791 flags = XFS_ILOG_CORE;
4792 /*
4793 * Realtime allocation. Free it and record di_nblocks update.
4794 */
4795 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4796 xfs_fsblock_t bno;
4797 xfs_filblks_t len;
4798
4799 ASSERT(do_mod(del->br_blockcount,
4800 mp->m_sb.sb_rextsize) == 0);
4801 ASSERT(do_mod(del->br_startblock,
4802 mp->m_sb.sb_rextsize) == 0);
4803 bno = del->br_startblock;
4804 len = del->br_blockcount;
4805 do_div(bno, mp->m_sb.sb_rextsize);
4806 do_div(len, mp->m_sb.sb_rextsize);
4807 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4808 if (error)
4809 goto done;
4810 do_fx = 0;
4811 nblks = len * mp->m_sb.sb_rextsize;
4812 qfield = XFS_TRANS_DQ_RTBCOUNT;
4813 }
4814 /*
4815 * Ordinary allocation.
4816 */
4817 else {
4818 do_fx = 1;
4819 nblks = del->br_blockcount;
4820 qfield = XFS_TRANS_DQ_BCOUNT;
4821 }
4822 /*
4823 * Set up del_endblock and cur for later.
4824 */
4825 del_endblock = del->br_startblock + del->br_blockcount;
4826 if (cur) {
4827 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
4828 got.br_startblock, got.br_blockcount,
4829 &i)))
4830 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004831 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004832 }
4833 da_old = da_new = 0;
4834 } else {
4835 da_old = startblockval(got.br_startblock);
4836 da_new = 0;
4837 nblks = 0;
4838 do_fx = 0;
4839 }
4840 /*
4841 * Set flag value to use in switch statement.
4842 * Left-contig is 2, right-contig is 1.
4843 */
4844 switch (((got.br_startoff == del->br_startoff) << 1) |
4845 (got_endoff == del_endoff)) {
4846 case 3:
4847 /*
4848 * Matches the whole extent. Delete the entry.
4849 */
4850 xfs_iext_remove(ip, *idx, 1,
4851 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
4852 --*idx;
4853 if (delay)
4854 break;
4855
4856 XFS_IFORK_NEXT_SET(ip, whichfork,
4857 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4858 flags |= XFS_ILOG_CORE;
4859 if (!cur) {
4860 flags |= xfs_ilog_fext(whichfork);
4861 break;
4862 }
4863 if ((error = xfs_btree_delete(cur, &i)))
4864 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004865 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004866 break;
4867
4868 case 2:
4869 /*
4870 * Deleting the first part of the extent.
4871 */
4872 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4873 xfs_bmbt_set_startoff(ep, del_endoff);
4874 temp = got.br_blockcount - del->br_blockcount;
4875 xfs_bmbt_set_blockcount(ep, temp);
4876 if (delay) {
4877 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4878 da_old);
4879 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4880 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4881 da_new = temp;
4882 break;
4883 }
4884 xfs_bmbt_set_startblock(ep, del_endblock);
4885 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4886 if (!cur) {
4887 flags |= xfs_ilog_fext(whichfork);
4888 break;
4889 }
4890 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
4891 got.br_blockcount - del->br_blockcount,
4892 got.br_state)))
4893 goto done;
4894 break;
4895
4896 case 1:
4897 /*
4898 * Deleting the last part of the extent.
4899 */
4900 temp = got.br_blockcount - del->br_blockcount;
4901 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4902 xfs_bmbt_set_blockcount(ep, temp);
4903 if (delay) {
4904 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4905 da_old);
4906 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4907 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4908 da_new = temp;
4909 break;
4910 }
4911 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4912 if (!cur) {
4913 flags |= xfs_ilog_fext(whichfork);
4914 break;
4915 }
4916 if ((error = xfs_bmbt_update(cur, got.br_startoff,
4917 got.br_startblock,
4918 got.br_blockcount - del->br_blockcount,
4919 got.br_state)))
4920 goto done;
4921 break;
4922
4923 case 0:
4924 /*
4925 * Deleting the middle of the extent.
4926 */
4927 temp = del->br_startoff - got.br_startoff;
4928 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4929 xfs_bmbt_set_blockcount(ep, temp);
4930 new.br_startoff = del_endoff;
4931 temp2 = got_endoff - del_endoff;
4932 new.br_blockcount = temp2;
4933 new.br_state = got.br_state;
4934 if (!delay) {
4935 new.br_startblock = del_endblock;
4936 flags |= XFS_ILOG_CORE;
4937 if (cur) {
4938 if ((error = xfs_bmbt_update(cur,
4939 got.br_startoff,
4940 got.br_startblock, temp,
4941 got.br_state)))
4942 goto done;
4943 if ((error = xfs_btree_increment(cur, 0, &i)))
4944 goto done;
4945 cur->bc_rec.b = new;
4946 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10004947 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004948 goto done;
4949 /*
4950 * If get no-space back from btree insert,
4951 * it tried a split, and we have a zero
4952 * block reservation.
4953 * Fix up our state and return the error.
4954 */
Dave Chinner24513372014-06-25 14:58:08 +10004955 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11004956 /*
4957 * Reset the cursor, don't trust
4958 * it after any insert operation.
4959 */
4960 if ((error = xfs_bmbt_lookup_eq(cur,
4961 got.br_startoff,
4962 got.br_startblock,
4963 temp, &i)))
4964 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004965 XFS_WANT_CORRUPTED_GOTO(mp,
4966 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004967 /*
4968 * Update the btree record back
4969 * to the original value.
4970 */
4971 if ((error = xfs_bmbt_update(cur,
4972 got.br_startoff,
4973 got.br_startblock,
4974 got.br_blockcount,
4975 got.br_state)))
4976 goto done;
4977 /*
4978 * Reset the extent record back
4979 * to the original value.
4980 */
4981 xfs_bmbt_set_blockcount(ep,
4982 got.br_blockcount);
4983 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10004984 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004985 goto done;
4986 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11004987 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004988 } else
4989 flags |= xfs_ilog_fext(whichfork);
4990 XFS_IFORK_NEXT_SET(ip, whichfork,
4991 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
4992 } else {
Brian Fosterd34999c2016-03-15 11:42:47 +11004993 xfs_filblks_t stolen;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004994 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11004995
4996 /*
4997 * Distribute the original indlen reservation across the
Brian Fosterd34999c2016-03-15 11:42:47 +11004998 * two new extents. Steal blocks from the deleted extent
4999 * if necessary. Stealing blocks simply fudges the
5000 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005001 */
5002 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5003 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c2016-03-15 11:42:47 +11005004 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5005 del->br_blockcount);
5006 da_new = temp + temp2 - stolen;
5007 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005008
5009 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11005010 * Set the reservation for each extent. Warn if either
5011 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005012 */
Brian Fosterd34999c2016-03-15 11:42:47 +11005013 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005014 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005015 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005016 }
5017 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5018 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5019 ++*idx;
5020 break;
5021 }
5022 /*
5023 * If we need to, add to list of extents to delete.
5024 */
5025 if (do_fx)
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005026 xfs_bmap_add_free(mp, dfops, del->br_startblock,
Darrick J. Wong59bad072016-06-21 11:53:28 +10005027 del->br_blockcount);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005028 /*
5029 * Adjust inode # blocks in the file.
5030 */
5031 if (nblks)
5032 ip->i_d.di_nblocks -= nblks;
5033 /*
5034 * Adjust quota data.
5035 */
5036 if (qfield)
5037 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5038
5039 /*
5040 * Account for change in delayed indirect blocks.
5041 * Nothing to do for disk quota accounting here.
5042 */
5043 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005044 if (da_old > da_new)
5045 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005046done:
5047 *logflagsp = flags;
5048 return error;
5049}
5050
5051/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 * Unmap (remove) blocks from a file.
5053 * If nexts is nonzero then the number of extents to remove is limited to
5054 * that value. If not all extents in the block range can be removed then
5055 * *done is set.
5056 */
5057int /* error */
5058xfs_bunmapi(
5059 xfs_trans_t *tp, /* transaction pointer */
5060 struct xfs_inode *ip, /* incore inode */
5061 xfs_fileoff_t bno, /* starting offset to unmap */
5062 xfs_filblks_t len, /* length to unmap in file */
5063 int flags, /* misc flags */
5064 xfs_extnum_t nexts, /* number of extents max */
5065 xfs_fsblock_t *firstblock, /* first allocated block
5066 controls a.g. for allocs */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005067 struct xfs_defer_ops *dfops, /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068 int *done) /* set if not done yet */
5069{
5070 xfs_btree_cur_t *cur; /* bmap btree cursor */
5071 xfs_bmbt_irec_t del; /* extent being deleted */
5072 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005073 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 int error; /* error return value */
5075 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005076 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 xfs_ifork_t *ifp; /* inode fork pointer */
5078 int isrt; /* freeing in rt area */
5079 xfs_extnum_t lastx; /* last extent index used */
5080 int logflags; /* transaction logging flags */
5081 xfs_extlen_t mod; /* rt extent offset */
5082 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005083 xfs_extnum_t nextents; /* number of file extents */
5084 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085 xfs_fileoff_t start; /* first file offset deleted */
5086 int tmp_logflags; /* partial logging flags */
5087 int wasdel; /* was a delayed alloc extent */
5088 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089 xfs_fsblock_t sum;
5090
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005091 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5092
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5094 XFS_ATTR_FORK : XFS_DATA_FORK;
5095 ifp = XFS_IFORK_PTR(ip, whichfork);
5096 if (unlikely(
5097 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5098 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5099 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5100 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005101 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 }
5103 mp = ip->i_mount;
5104 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005105 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005106
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005107 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 ASSERT(len > 0);
5109 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005110
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5112 (error = xfs_iread_extents(tp, ip, whichfork)))
5113 return error;
5114 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5115 if (nextents == 0) {
5116 *done = 1;
5117 return 0;
5118 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005119 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005120 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121 start = bno;
5122 bno = start + len - 1;
5123 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5124 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005125
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 /*
5127 * Check to see if the given block number is past the end of the
5128 * file, back up to the last block if so...
5129 */
5130 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005131 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 xfs_bmbt_get_all(ep, &got);
5133 bno = got.br_startoff + got.br_blockcount - 1;
5134 }
5135 logflags = 0;
5136 if (ifp->if_flags & XFS_IFBROOT) {
5137 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005138 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005140 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 cur->bc_private.b.flags = 0;
5142 } else
5143 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005144
5145 if (isrt) {
5146 /*
5147 * Synchronize by locking the bitmap inode.
5148 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005149 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005150 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005151 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5152 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005153 }
5154
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 extno = 0;
5156 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5157 (nexts == 0 || extno < nexts)) {
5158 /*
5159 * Is the found extent after a hole in which bno lives?
5160 * Just back up to the previous extent, if so.
5161 */
5162 if (got.br_startoff > bno) {
5163 if (--lastx < 0)
5164 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005165 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 xfs_bmbt_get_all(ep, &got);
5167 }
5168 /*
5169 * Is the last block of this extent before the range
5170 * we're supposed to delete? If so, we're done.
5171 */
5172 bno = XFS_FILEOFF_MIN(bno,
5173 got.br_startoff + got.br_blockcount - 1);
5174 if (bno < start)
5175 break;
5176 /*
5177 * Then deal with the (possibly delayed) allocated space
5178 * we found.
5179 */
5180 ASSERT(ep != NULL);
5181 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005182 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 if (got.br_startoff < start) {
5184 del.br_startoff = start;
5185 del.br_blockcount -= start - got.br_startoff;
5186 if (!wasdel)
5187 del.br_startblock += start - got.br_startoff;
5188 }
5189 if (del.br_startoff + del.br_blockcount > bno + 1)
5190 del.br_blockcount = bno + 1 - del.br_startoff;
5191 sum = del.br_startblock + del.br_blockcount;
5192 if (isrt &&
5193 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5194 /*
5195 * Realtime extent not lined up at the end.
5196 * The extent could have been split into written
5197 * and unwritten pieces, or we could just be
5198 * unmapping part of it. But we can't really
5199 * get rid of part of a realtime extent.
5200 */
5201 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005202 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 /*
5204 * This piece is unwritten, or we're not
5205 * using unwritten extents. Skip over it.
5206 */
5207 ASSERT(bno >= mod);
5208 bno -= mod > del.br_blockcount ?
5209 del.br_blockcount : mod;
5210 if (bno < got.br_startoff) {
5211 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005212 xfs_bmbt_get_all(xfs_iext_get_ext(
5213 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 }
5215 continue;
5216 }
5217 /*
5218 * It's written, turn it unwritten.
5219 * This is better than zeroing it.
5220 */
5221 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005222 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005223 /*
5224 * If this spans a realtime extent boundary,
5225 * chop it back to the start of the one we end at.
5226 */
5227 if (del.br_blockcount > mod) {
5228 del.br_startoff += del.br_blockcount - mod;
5229 del.br_startblock += del.br_blockcount - mod;
5230 del.br_blockcount = mod;
5231 }
5232 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005233 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005234 &lastx, &cur, &del, firstblock, dfops,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005235 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236 if (error)
5237 goto error0;
5238 goto nodelete;
5239 }
5240 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5241 /*
5242 * Realtime extent is lined up at the end but not
5243 * at the front. We'll get rid of full extents if
5244 * we can.
5245 */
5246 mod = mp->m_sb.sb_rextsize - mod;
5247 if (del.br_blockcount > mod) {
5248 del.br_blockcount -= mod;
5249 del.br_startoff += mod;
5250 del.br_startblock += mod;
5251 } else if ((del.br_startoff == start &&
5252 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005253 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005254 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 /*
5256 * Can't make it unwritten. There isn't
5257 * a full extent here so just skip it.
5258 */
5259 ASSERT(bno >= del.br_blockcount);
5260 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005261 if (got.br_startoff > bno) {
5262 if (--lastx >= 0) {
5263 ep = xfs_iext_get_ext(ifp,
5264 lastx);
5265 xfs_bmbt_get_all(ep, &got);
5266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 }
5268 continue;
5269 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5270 /*
5271 * This one is already unwritten.
5272 * It must have a written left neighbor.
5273 * Unwrite the killed part of that one and
5274 * try again.
5275 */
5276 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005277 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5278 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005280 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 ASSERT(del.br_startblock ==
5282 prev.br_startblock + prev.br_blockcount);
5283 if (prev.br_startoff < start) {
5284 mod = start - prev.br_startoff;
5285 prev.br_blockcount -= mod;
5286 prev.br_startblock += mod;
5287 prev.br_startoff = start;
5288 }
5289 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005290 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005291 error = xfs_bmap_add_extent_unwritten_real(tp,
5292 ip, &lastx, &cur, &prev,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005293 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 if (error)
5295 goto error0;
5296 goto nodelete;
5297 } else {
5298 ASSERT(del.br_state == XFS_EXT_NORM);
5299 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005300 error = xfs_bmap_add_extent_unwritten_real(tp,
5301 ip, &lastx, &cur, &del,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005302 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 if (error)
5304 goto error0;
5305 goto nodelete;
5306 }
5307 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005308
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 /*
5310 * If it's the case where the directory code is running
5311 * with no block reservation, and the deleted block is in
5312 * the middle of its extent, and the resulting insert
5313 * of an extent would cause transformation to btree format,
5314 * then reject it. The calling code will then swap
5315 * blocks around instead.
5316 * We have to do this now, rather than waiting for the
5317 * conversion to btree format, since the transaction
5318 * will be dirty.
5319 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005320 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005322 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5323 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 del.br_startoff > got.br_startoff &&
5325 del.br_startoff + del.br_blockcount <
5326 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005327 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 goto error0;
5329 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005330
5331 /*
5332 * Unreserve quota and update realtime free space, if
5333 * appropriate. If delayed allocation, update the inode delalloc
5334 * counter now and wait to update the sb counters as
5335 * xfs_bmap_del_extent() might need to borrow some blocks.
5336 */
5337 if (wasdel) {
5338 ASSERT(startblockval(del.br_startblock) > 0);
5339 if (isrt) {
5340 xfs_filblks_t rtexts;
5341
5342 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5343 do_div(rtexts, mp->m_sb.sb_rextsize);
5344 xfs_mod_frextents(mp, (int64_t)rtexts);
5345 (void)xfs_trans_reserve_quota_nblks(NULL,
5346 ip, -((long)del.br_blockcount), 0,
5347 XFS_QMOPT_RES_RTBLKS);
5348 } else {
5349 (void)xfs_trans_reserve_quota_nblks(NULL,
5350 ip, -((long)del.br_blockcount), 0,
5351 XFS_QMOPT_RES_REGBLKS);
5352 }
5353 ip->i_delayed_blks -= del.br_blockcount;
5354 if (cur)
5355 cur->bc_private.b.flags |=
5356 XFS_BTCUR_BPRV_WASDEL;
5357 } else if (cur)
5358 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5359
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005360 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005361 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 logflags |= tmp_logflags;
5363 if (error)
5364 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005365
5366 if (!isrt && wasdel)
5367 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5368
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 bno = del.br_startoff - 1;
5370nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 /*
5372 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005375 if (lastx >= 0) {
5376 ep = xfs_iext_get_ext(ifp, lastx);
5377 if (xfs_bmbt_get_startoff(ep) > bno) {
5378 if (--lastx >= 0)
5379 ep = xfs_iext_get_ext(ifp,
5380 lastx);
5381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 extno++;
5385 }
5386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005388
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 /*
5390 * Convert to a btree if necessary.
5391 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005392 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005394 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 &cur, 0, &tmp_logflags, whichfork);
5396 logflags |= tmp_logflags;
5397 if (error)
5398 goto error0;
5399 }
5400 /*
5401 * transform from btree to extents, give it cur
5402 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005403 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 ASSERT(cur != NULL);
5405 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5406 whichfork);
5407 logflags |= tmp_logflags;
5408 if (error)
5409 goto error0;
5410 }
5411 /*
5412 * transform from extents to local?
5413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 error = 0;
5415error0:
5416 /*
5417 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005418 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005420 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005422 logflags &= ~xfs_ilog_fext(whichfork);
5423 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005425 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 /*
5427 * Log inode even in the error case, if the transaction
5428 * is dirty we'll need to shut down the filesystem.
5429 */
5430 if (logflags)
5431 xfs_trans_log_inode(tp, ip, logflags);
5432 if (cur) {
5433 if (!error) {
5434 *firstblock = cur->bc_private.b.firstblock;
5435 cur->bc_private.b.allocated = 0;
5436 }
5437 xfs_btree_del_cursor(cur,
5438 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5439 }
5440 return error;
5441}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005442
5443/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005444 * Determine whether an extent shift can be accomplished by a merge with the
5445 * extent that precedes the target hole of the shift.
5446 */
5447STATIC bool
5448xfs_bmse_can_merge(
5449 struct xfs_bmbt_irec *left, /* preceding extent */
5450 struct xfs_bmbt_irec *got, /* current extent to shift */
5451 xfs_fileoff_t shift) /* shift fsb */
5452{
5453 xfs_fileoff_t startoff;
5454
5455 startoff = got->br_startoff - shift;
5456
5457 /*
5458 * The extent, once shifted, must be adjacent in-file and on-disk with
5459 * the preceding extent.
5460 */
5461 if ((left->br_startoff + left->br_blockcount != startoff) ||
5462 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5463 (left->br_state != got->br_state) ||
5464 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5465 return false;
5466
5467 return true;
5468}
5469
5470/*
5471 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5472 * hole in the file. If an extent shift would result in the extent being fully
5473 * adjacent to the extent that currently precedes the hole, we can merge with
5474 * the preceding extent rather than do the shift.
5475 *
5476 * This function assumes the caller has verified a shift-by-merge is possible
5477 * with the provided extents via xfs_bmse_can_merge().
5478 */
5479STATIC int
5480xfs_bmse_merge(
5481 struct xfs_inode *ip,
5482 int whichfork,
5483 xfs_fileoff_t shift, /* shift fsb */
5484 int current_ext, /* idx of gotp */
5485 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5486 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5487 struct xfs_btree_cur *cur,
5488 int *logflags) /* output */
5489{
Brian Fosterddb19e32014-09-23 15:38:09 +10005490 struct xfs_bmbt_irec got;
5491 struct xfs_bmbt_irec left;
5492 xfs_filblks_t blockcount;
5493 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005494 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005495
Brian Fosterddb19e32014-09-23 15:38:09 +10005496 xfs_bmbt_get_all(gotp, &got);
5497 xfs_bmbt_get_all(leftp, &left);
5498 blockcount = left.br_blockcount + got.br_blockcount;
5499
5500 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5501 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5502 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5503
5504 /*
5505 * Merge the in-core extents. Note that the host record pointers and
5506 * current_ext index are invalid once the extent has been removed via
5507 * xfs_iext_remove().
5508 */
5509 xfs_bmbt_set_blockcount(leftp, blockcount);
5510 xfs_iext_remove(ip, current_ext, 1, 0);
5511
5512 /*
5513 * Update the on-disk extent count, the btree if necessary and log the
5514 * inode.
5515 */
5516 XFS_IFORK_NEXT_SET(ip, whichfork,
5517 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5518 *logflags |= XFS_ILOG_CORE;
5519 if (!cur) {
5520 *logflags |= XFS_ILOG_DEXT;
5521 return 0;
5522 }
5523
5524 /* lookup and remove the extent to merge */
5525 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5526 got.br_blockcount, &i);
5527 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005528 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005529 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005530
5531 error = xfs_btree_delete(cur, &i);
5532 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005533 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005534 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005535
5536 /* lookup and update size of the previous extent */
5537 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5538 left.br_blockcount, &i);
5539 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005540 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005541 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005542
5543 left.br_blockcount = blockcount;
5544
Dave Chinner4db431f2014-12-04 09:42:40 +11005545 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5546 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005547}
5548
5549/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005550 * Shift a single extent.
5551 */
5552STATIC int
5553xfs_bmse_shift_one(
5554 struct xfs_inode *ip,
5555 int whichfork,
5556 xfs_fileoff_t offset_shift_fsb,
5557 int *current_ext,
5558 struct xfs_bmbt_rec_host *gotp,
5559 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005560 int *logflags,
5561 enum shift_direction direction)
Brian Fostera979bdf2014-09-23 15:39:04 +10005562{
5563 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005564 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005565 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005566 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005567 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005568 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005569 int error;
5570 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005571 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005572
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005573 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005574 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005575 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Brian Fostera979bdf2014-09-23 15:39:04 +10005576
5577 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005578
Brian Fosterf71721d2014-09-23 15:39:05 +10005579 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005580 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005581
Namjae Jeona904b1c2015-03-25 15:08:56 +11005582 if (direction == SHIFT_LEFT) {
5583 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005584
Namjae Jeona904b1c2015-03-25 15:08:56 +11005585 /*
5586 * Check for merge if we've got an extent to the left,
5587 * otherwise make sure there's enough room at the start
5588 * of the file for the shift.
5589 */
5590 if (!*current_ext) {
5591 if (got.br_startoff < offset_shift_fsb)
5592 return -EINVAL;
5593 goto update_current_ext;
5594 }
5595 /*
5596 * grab the left extent and check for a large
5597 * enough hole.
5598 */
5599 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5600 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5601
5602 if (startoff <
5603 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005604 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005605
Dave Chinnerb11bd672014-12-04 09:42:24 +11005606 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005607 if (xfs_bmse_can_merge(&adj_irec, &got,
5608 offset_shift_fsb)) {
Dave Chinnerb11bd672014-12-04 09:42:24 +11005609 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005610 *current_ext, gotp, adj_irecp,
5611 cur, logflags);
Dave Chinnerb11bd672014-12-04 09:42:24 +11005612 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005613 } else {
5614 startoff = got.br_startoff + offset_shift_fsb;
5615 /* nothing to move if this is the last extent */
5616 if (*current_ext >= (total_extents - 1))
5617 goto update_current_ext;
5618 /*
5619 * If this is not the last extent in the file, make sure there
5620 * is enough room between current extent and next extent for
5621 * accommodating the shift.
5622 */
5623 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5624 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5625 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5626 return -EINVAL;
5627 /*
5628 * Unlike a left shift (which involves a hole punch),
5629 * a right shift does not modify extent neighbors
5630 * in any way. We should never find mergeable extents
5631 * in this scenario. Check anyways and warn if we
5632 * encounter two extents that could be one.
5633 */
5634 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5635 WARN_ON_ONCE(1);
5636 }
Brian Fostera979bdf2014-09-23 15:39:04 +10005637 /*
5638 * Increment the extent index for the next iteration, update the start
5639 * offset of the in-core extent and update the btree if applicable.
5640 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005641update_current_ext:
5642 if (direction == SHIFT_LEFT)
5643 (*current_ext)++;
5644 else
5645 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005646 xfs_bmbt_set_startoff(gotp, startoff);
5647 *logflags |= XFS_ILOG_CORE;
5648 if (!cur) {
5649 *logflags |= XFS_ILOG_DEXT;
5650 return 0;
5651 }
5652
5653 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5654 got.br_blockcount, &i);
5655 if (error)
5656 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005657 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10005658
5659 got.br_startoff = startoff;
kbuild test robotd254aae2014-12-01 08:42:52 +11005660 return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005661 got.br_blockcount, got.br_state);
Brian Fostera979bdf2014-09-23 15:39:04 +10005662}
5663
5664/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005665 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005666 *
Brian Foster2c845f52014-09-23 15:37:09 +10005667 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005668 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10005669 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5670 * is the length by which each extent is shifted. If there is no hole to shift
5671 * the extents into, this will be considered invalid operation and we abort
5672 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005673 */
5674int
5675xfs_bmap_shift_extents(
5676 struct xfs_trans *tp,
5677 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005678 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005679 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005680 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005681 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005682 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005683 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005684 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005685 int num_exts)
5686{
Brian Fosterca446d82014-09-02 12:12:53 +10005687 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005688 struct xfs_bmbt_rec_host *gotp;
5689 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005690 struct xfs_mount *mp = ip->i_mount;
5691 struct xfs_ifork *ifp;
5692 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005693 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005694 xfs_extnum_t total_extents;
5695 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005696 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005697 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10005698 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005699
5700 if (unlikely(XFS_TEST_ERROR(
5701 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5702 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5703 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5704 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5705 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005706 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005707 }
5708
5709 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005710 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005711
Brian Foster2c845f52014-09-23 15:37:09 +10005712 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5713 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005714 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
5715 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005716
5717 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005718 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5719 /* Read in all the extents */
5720 error = xfs_iread_extents(tp, ip, whichfork);
5721 if (error)
5722 return error;
5723 }
5724
Brian Fosterddb19e32014-09-23 15:38:09 +10005725 if (ifp->if_flags & XFS_IFBROOT) {
5726 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5727 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005728 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005729 cur->bc_private.b.flags = 0;
5730 }
5731
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005732 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005733 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10005734 * are collapsing out, so we cannot use the count of real extents here.
5735 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005736 */
5737 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005738 if (total_extents == 0) {
5739 *done = 1;
5740 goto del_cursor;
5741 }
5742
5743 /*
5744 * In case of first right shift, we need to initialize next_fsb
5745 */
5746 if (*next_fsb == NULLFSBLOCK) {
5747 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
5748 xfs_bmbt_get_all(gotp, &got);
5749 *next_fsb = got.br_startoff;
5750 if (stop_fsb > *next_fsb) {
5751 *done = 1;
5752 goto del_cursor;
5753 }
5754 }
5755
5756 /* Lookup the extent index at which we have to stop */
5757 if (direction == SHIFT_RIGHT) {
5758 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
5759 /* Make stop_extent exclusive of shift range */
5760 stop_extent--;
5761 } else
5762 stop_extent = total_extents;
5763
5764 /*
5765 * Look up the extent index for the fsb where we start shifting. We can
5766 * henceforth iterate with current_ext as extent list changes are locked
5767 * out via ilock.
5768 *
5769 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5770 * *next_fsb lies in a hole beyond which there are no extents. Either
5771 * way, we are done.
5772 */
5773 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
5774 if (!gotp) {
5775 *done = 1;
5776 goto del_cursor;
5777 }
5778
5779 /* some sanity checking before we finally start shifting extents */
5780 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
5781 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
5782 error = -EIO;
5783 goto del_cursor;
5784 }
5785
5786 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005787 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005788 &current_ext, gotp, cur, &logflags,
5789 direction);
Brian Fostera979bdf2014-09-23 15:39:04 +10005790 if (error)
5791 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005792 /*
5793 * If there was an extent merge during the shift, the extent
5794 * count can change. Update the total and grade the next record.
5795 */
5796 if (direction == SHIFT_LEFT) {
5797 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5798 stop_extent = total_extents;
5799 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005800
Namjae Jeona904b1c2015-03-25 15:08:56 +11005801 if (current_ext == stop_extent) {
5802 *done = 1;
5803 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10005804 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005805 }
Brian Fosterddb19e32014-09-23 15:38:09 +10005806 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005807 }
5808
Namjae Jeona904b1c2015-03-25 15:08:56 +11005809 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005810 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10005811 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10005812 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005813
5814del_cursor:
5815 if (cur)
5816 xfs_btree_del_cursor(cur,
5817 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5818
Brian Fosterca446d82014-09-02 12:12:53 +10005819 if (logflags)
5820 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10005821
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005822 return error;
5823}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005824
5825/*
5826 * Splits an extent into two extents at split_fsb block such that it is
5827 * the first block of the current_ext. @current_ext is a target extent
5828 * to be split. @split_fsb is a block where the extents is split.
5829 * If split_fsb lies in a hole or the first block of extents, just return 0.
5830 */
5831STATIC int
5832xfs_bmap_split_extent_at(
5833 struct xfs_trans *tp,
5834 struct xfs_inode *ip,
5835 xfs_fileoff_t split_fsb,
5836 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005837 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005838{
5839 int whichfork = XFS_DATA_FORK;
5840 struct xfs_btree_cur *cur = NULL;
5841 struct xfs_bmbt_rec_host *gotp;
5842 struct xfs_bmbt_irec got;
5843 struct xfs_bmbt_irec new; /* split extent */
5844 struct xfs_mount *mp = ip->i_mount;
5845 struct xfs_ifork *ifp;
5846 xfs_fsblock_t gotblkcnt; /* new block count for got */
5847 xfs_extnum_t current_ext;
5848 int error = 0;
5849 int logflags = 0;
5850 int i = 0;
5851
5852 if (unlikely(XFS_TEST_ERROR(
5853 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5854 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5855 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5856 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5857 XFS_ERRLEVEL_LOW, mp);
5858 return -EFSCORRUPTED;
5859 }
5860
5861 if (XFS_FORCED_SHUTDOWN(mp))
5862 return -EIO;
5863
5864 ifp = XFS_IFORK_PTR(ip, whichfork);
5865 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5866 /* Read in all the extents */
5867 error = xfs_iread_extents(tp, ip, whichfork);
5868 if (error)
5869 return error;
5870 }
5871
5872 /*
5873 * gotp can be null in 2 cases: 1) if there are no extents
5874 * or 2) split_fsb lies in a hole beyond which there are
5875 * no extents. Either way, we are done.
5876 */
5877 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
5878 if (!gotp)
5879 return 0;
5880
5881 xfs_bmbt_get_all(gotp, &got);
5882
5883 /*
5884 * Check split_fsb lies in a hole or the start boundary offset
5885 * of the extent.
5886 */
5887 if (got.br_startoff >= split_fsb)
5888 return 0;
5889
5890 gotblkcnt = split_fsb - got.br_startoff;
5891 new.br_startoff = split_fsb;
5892 new.br_startblock = got.br_startblock + gotblkcnt;
5893 new.br_blockcount = got.br_blockcount - gotblkcnt;
5894 new.br_state = got.br_state;
5895
5896 if (ifp->if_flags & XFS_IFBROOT) {
5897 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5898 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005899 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005900 cur->bc_private.b.flags = 0;
5901 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5902 got.br_startblock,
5903 got.br_blockcount,
5904 &i);
5905 if (error)
5906 goto del_cursor;
5907 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5908 }
5909
5910 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
5911 got.br_blockcount = gotblkcnt;
5912
5913 logflags = XFS_ILOG_CORE;
5914 if (cur) {
5915 error = xfs_bmbt_update(cur, got.br_startoff,
5916 got.br_startblock,
5917 got.br_blockcount,
5918 got.br_state);
5919 if (error)
5920 goto del_cursor;
5921 } else
5922 logflags |= XFS_ILOG_DEXT;
5923
5924 /* Add new extent */
5925 current_ext++;
5926 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5927 XFS_IFORK_NEXT_SET(ip, whichfork,
5928 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5929
5930 if (cur) {
5931 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
5932 new.br_startblock, new.br_blockcount,
5933 &i);
5934 if (error)
5935 goto del_cursor;
5936 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
5937 cur->bc_rec.b.br_state = new.br_state;
5938
5939 error = xfs_btree_insert(cur, &i);
5940 if (error)
5941 goto del_cursor;
5942 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5943 }
5944
5945 /*
5946 * Convert to a btree if necessary.
5947 */
5948 if (xfs_bmap_needs_btree(ip, whichfork)) {
5949 int tmp_logflags; /* partial log flag return val */
5950
5951 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005952 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005953 &cur, 0, &tmp_logflags, whichfork);
5954 logflags |= tmp_logflags;
5955 }
5956
5957del_cursor:
5958 if (cur) {
5959 cur->bc_private.b.allocated = 0;
5960 xfs_btree_del_cursor(cur,
5961 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5962 }
5963
5964 if (logflags)
5965 xfs_trans_log_inode(tp, ip, logflags);
5966 return error;
5967}
5968
5969int
5970xfs_bmap_split_extent(
5971 struct xfs_inode *ip,
5972 xfs_fileoff_t split_fsb)
5973{
5974 struct xfs_mount *mp = ip->i_mount;
5975 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005976 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005977 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005978 int error;
5979
Christoph Hellwig253f4912016-04-06 09:19:55 +10005980 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
5981 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
5982 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005983 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005984
5985 xfs_ilock(ip, XFS_ILOCK_EXCL);
5986 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
5987
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005988 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005989
5990 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005991 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005992 if (error)
5993 goto out;
5994
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005995 error = xfs_defer_finish(&tp, &dfops, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005996 if (error)
5997 goto out;
5998
Christoph Hellwig70393312015-06-04 13:48:08 +10005999 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006000
6001out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006002 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006003 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006004 return error;
6005}