Merge tag 'arc-4.10-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/vgupta/arc
[linux-block.git] / fs / xfs / libxfs / xfs_bmap.c
CommitLineData
1da177e4 1/*
3e57ecf6 2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
7b718769 3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
70a9883c 20#include "xfs_shared.h"
239880ef
DC
21#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
a844f451 24#include "xfs_bit.h"
1da177e4 25#include "xfs_sb.h"
f5ea1100 26#include "xfs_mount.h"
3ab78df2 27#include "xfs_defer.h"
57062787 28#include "xfs_da_format.h"
a844f451 29#include "xfs_da_btree.h"
2b9ab5ab 30#include "xfs_dir2.h"
1da177e4 31#include "xfs_inode.h"
a844f451 32#include "xfs_btree.h"
239880ef 33#include "xfs_trans.h"
a844f451 34#include "xfs_inode_item.h"
1da177e4
LT
35#include "xfs_extfree_item.h"
36#include "xfs_alloc.h"
37#include "xfs_bmap.h"
68988114 38#include "xfs_bmap_util.h"
a4fbe6ab 39#include "xfs_bmap_btree.h"
1da177e4
LT
40#include "xfs_rtalloc.h"
41#include "xfs_error.h"
1da177e4
LT
42#include "xfs_quota.h"
43#include "xfs_trans_space.h"
44#include "xfs_buf_item.h"
0b1b213f 45#include "xfs_trace.h"
19de7351 46#include "xfs_symlink.h"
a4fbe6ab 47#include "xfs_attr_leaf.h"
a4fbe6ab 48#include "xfs_filestream.h"
340785cc 49#include "xfs_rmap.h"
3fd129b6 50#include "xfs_ag_resv.h"
62aab20f 51#include "xfs_refcount.h"
fd26a880 52#include "xfs_rmap_btree.h"
974ae922 53#include "xfs_icache.h"
1da177e4
LT
54
55
1da177e4
LT
56kmem_zone_t *xfs_bmap_free_item_zone;
57
58/*
9e5987a7 59 * Miscellaneous helper functions
1da177e4
LT
60 */
61
1da177e4 62/*
9e5987a7
DC
63 * Compute and fill in the value of the maximum depth of a bmap btree
64 * in this filesystem. Done once, during mount.
1da177e4 65 */
9e5987a7
DC
66void
67xfs_bmap_compute_maxlevels(
68 xfs_mount_t *mp, /* file system mount structure */
69 int whichfork) /* data or attr fork */
70{
71 int level; /* btree level */
72 uint maxblocks; /* max blocks at this level */
73 uint maxleafents; /* max leaf entries possible */
74 int maxrootrecs; /* max records in root block */
75 int minleafrecs; /* min records in leaf block */
76 int minnoderecs; /* min records in node block */
77 int sz; /* root block size */
1da177e4 78
9e5987a7
DC
79 /*
80 * The maximum number of extents in a file, hence the maximum
81 * number of leaf entries, is controlled by the type of di_nextents
82 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
83 * (a signed 16-bit number, xfs_aextnum_t).
84 *
85 * Note that we can no longer assume that if we are in ATTR1 that
86 * the fork offset of all the inodes will be
87 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
88 * with ATTR2 and then mounted back with ATTR1, keeping the
89 * di_forkoff's fixed but probably at various positions. Therefore,
90 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
91 * of a minimum size available.
92 */
93 if (whichfork == XFS_DATA_FORK) {
94 maxleafents = MAXEXTNUM;
95 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
96 } else {
97 maxleafents = MAXAEXTNUM;
98 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
99 }
152d93b7 100 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
9e5987a7
DC
101 minleafrecs = mp->m_bmap_dmnr[0];
102 minnoderecs = mp->m_bmap_dmnr[1];
103 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
104 for (level = 1; maxblocks > 1; level++) {
105 if (maxblocks <= maxrootrecs)
106 maxblocks = 1;
107 else
108 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
109 }
110 mp->m_bm_maxlevels[whichfork] = level;
111}
91e11088 112
fe033cc8
CH
113STATIC int /* error */
114xfs_bmbt_lookup_eq(
115 struct xfs_btree_cur *cur,
116 xfs_fileoff_t off,
117 xfs_fsblock_t bno,
118 xfs_filblks_t len,
119 int *stat) /* success/failure */
120{
121 cur->bc_rec.b.br_startoff = off;
122 cur->bc_rec.b.br_startblock = bno;
123 cur->bc_rec.b.br_blockcount = len;
124 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
125}
126
127STATIC int /* error */
128xfs_bmbt_lookup_ge(
129 struct xfs_btree_cur *cur,
130 xfs_fileoff_t off,
131 xfs_fsblock_t bno,
132 xfs_filblks_t len,
133 int *stat) /* success/failure */
134{
135 cur->bc_rec.b.br_startoff = off;
136 cur->bc_rec.b.br_startblock = bno;
137 cur->bc_rec.b.br_blockcount = len;
138 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
139}
140
278d0ca1 141/*
8096b1eb
CH
142 * Check if the inode needs to be converted to btree format.
143 */
144static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
145{
60b4984f
DW
146 return whichfork != XFS_COW_FORK &&
147 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
8096b1eb
CH
148 XFS_IFORK_NEXTENTS(ip, whichfork) >
149 XFS_IFORK_MAXEXT(ip, whichfork);
150}
151
152/*
153 * Check if the inode should be converted to extent format.
154 */
155static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
156{
60b4984f
DW
157 return whichfork != XFS_COW_FORK &&
158 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
8096b1eb
CH
159 XFS_IFORK_NEXTENTS(ip, whichfork) <=
160 XFS_IFORK_MAXEXT(ip, whichfork);
161}
162
163/*
164 * Update the record referred to by cur to the value given
278d0ca1
CH
165 * by [off, bno, len, state].
166 * This either works (return 0) or gets an EFSCORRUPTED error.
167 */
168STATIC int
169xfs_bmbt_update(
170 struct xfs_btree_cur *cur,
171 xfs_fileoff_t off,
172 xfs_fsblock_t bno,
173 xfs_filblks_t len,
174 xfs_exntst_t state)
175{
176 union xfs_btree_rec rec;
177
178 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
179 return xfs_btree_update(cur, &rec);
180}
fe033cc8 181
1da177e4 182/*
9e5987a7
DC
183 * Compute the worst-case number of indirect blocks that will be used
184 * for ip's delayed extent of length "len".
1da177e4 185 */
9e5987a7
DC
186STATIC xfs_filblks_t
187xfs_bmap_worst_indlen(
188 xfs_inode_t *ip, /* incore inode pointer */
189 xfs_filblks_t len) /* delayed extent length */
1da177e4 190{
9e5987a7
DC
191 int level; /* btree level number */
192 int maxrecs; /* maximum record count at this level */
193 xfs_mount_t *mp; /* mount structure */
194 xfs_filblks_t rval; /* return value */
fd26a880 195 xfs_filblks_t orig_len;
1da177e4
LT
196
197 mp = ip->i_mount;
fd26a880
DW
198
199 /* Calculate the worst-case size of the bmbt. */
200 orig_len = len;
9e5987a7
DC
201 maxrecs = mp->m_bmap_dmxr[0];
202 for (level = 0, rval = 0;
203 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
204 level++) {
205 len += maxrecs - 1;
206 do_div(len, maxrecs);
207 rval += len;
fd26a880
DW
208 if (len == 1) {
209 rval += XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
9e5987a7 210 level - 1;
fd26a880
DW
211 break;
212 }
9e5987a7
DC
213 if (level == 0)
214 maxrecs = mp->m_bmap_dmxr[1];
1da177e4 215 }
fd26a880
DW
216
217 /* Calculate the worst-case size of the rmapbt. */
218 if (xfs_sb_version_hasrmapbt(&mp->m_sb))
219 rval += 1 + xfs_rmapbt_calc_size(mp, orig_len) +
220 mp->m_rmap_maxlevels;
221
9e5987a7 222 return rval;
1da177e4
LT
223}
224
225/*
9e5987a7 226 * Calculate the default attribute fork offset for newly created inodes.
1da177e4 227 */
9e5987a7
DC
228uint
229xfs_default_attroffset(
230 struct xfs_inode *ip)
1da177e4 231{
9e5987a7
DC
232 struct xfs_mount *mp = ip->i_mount;
233 uint offset;
1da177e4 234
9e5987a7 235 if (mp->m_sb.sb_inodesize == 256) {
56cea2d0 236 offset = XFS_LITINO(mp, ip->i_d.di_version) -
9e5987a7
DC
237 XFS_BMDR_SPACE_CALC(MINABTPTRS);
238 } else {
239 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
1da177e4 240 }
9e5987a7 241
56cea2d0 242 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
9e5987a7 243 return offset;
1da177e4
LT
244}
245
246/*
9e5987a7
DC
247 * Helper routine to reset inode di_forkoff field when switching
248 * attribute fork from local to extent format - we reset it where
249 * possible to make space available for inline data fork extents.
1e82379b
DC
250 */
251STATIC void
9e5987a7 252xfs_bmap_forkoff_reset(
9e5987a7
DC
253 xfs_inode_t *ip,
254 int whichfork)
1e82379b 255{
9e5987a7
DC
256 if (whichfork == XFS_ATTR_FORK &&
257 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
258 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
259 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
260 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
261
262 if (dfl_forkoff > ip->i_d.di_forkoff)
263 ip->i_d.di_forkoff = dfl_forkoff;
264 }
1e82379b
DC
265}
266
9e5987a7
DC
267#ifdef DEBUG
268STATIC struct xfs_buf *
269xfs_bmap_get_bp(
270 struct xfs_btree_cur *cur,
271 xfs_fsblock_t bno)
272{
273 struct xfs_log_item_desc *lidp;
274 int i;
7574aa92 275
9e5987a7
DC
276 if (!cur)
277 return NULL;
278
279 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
280 if (!cur->bc_bufs[i])
281 break;
282 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
283 return cur->bc_bufs[i];
1da177e4 284 }
7574aa92 285
9e5987a7
DC
286 /* Chase down all the log items to see if the bp is there */
287 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
288 struct xfs_buf_log_item *bip;
289 bip = (struct xfs_buf_log_item *)lidp->lid_item;
290 if (bip->bli_item.li_type == XFS_LI_BUF &&
291 XFS_BUF_ADDR(bip->bli_buf) == bno)
292 return bip->bli_buf;
293 }
7574aa92 294
9e5987a7
DC
295 return NULL;
296}
0b1b213f 297
9e5987a7
DC
298STATIC void
299xfs_check_block(
300 struct xfs_btree_block *block,
301 xfs_mount_t *mp,
302 int root,
303 short sz)
304{
305 int i, j, dmxr;
306 __be64 *pp, *thispa; /* pointer to block address */
307 xfs_bmbt_key_t *prevp, *keyp;
1da177e4 308
9e5987a7 309 ASSERT(be16_to_cpu(block->bb_level) > 0);
ec90c556 310
9e5987a7
DC
311 prevp = NULL;
312 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
313 dmxr = mp->m_bmap_dmxr[0];
314 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
0b1b213f 315
9e5987a7
DC
316 if (prevp) {
317 ASSERT(be64_to_cpu(prevp->br_startoff) <
318 be64_to_cpu(keyp->br_startoff));
1da177e4 319 }
9e5987a7 320 prevp = keyp;
1da177e4 321
1da177e4 322 /*
9e5987a7 323 * Compare the block numbers to see if there are dups.
1da177e4 324 */
9e5987a7
DC
325 if (root)
326 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
327 else
328 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
0b1b213f 329
9e5987a7
DC
330 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
331 if (root)
332 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
333 else
334 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
335 if (*thispa == *pp) {
336 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
337 __func__, j, i,
338 (unsigned long long)be64_to_cpu(*thispa));
339 panic("%s: ptrs are equal in node\n",
340 __func__);
341 }
1da177e4 342 }
9e5987a7
DC
343 }
344}
1da177e4 345
9e5987a7
DC
346/*
347 * Check that the extents for the inode ip are in the right order in all
e3543819
DC
348 * btree leaves. THis becomes prohibitively expensive for large extent count
349 * files, so don't bother with inodes that have more than 10,000 extents in
350 * them. The btree record ordering checks will still be done, so for such large
351 * bmapbt constructs that is going to catch most corruptions.
9e5987a7 352 */
9e5987a7
DC
353STATIC void
354xfs_bmap_check_leaf_extents(
355 xfs_btree_cur_t *cur, /* btree cursor or null */
356 xfs_inode_t *ip, /* incore inode pointer */
357 int whichfork) /* data or attr fork */
358{
359 struct xfs_btree_block *block; /* current btree block */
360 xfs_fsblock_t bno; /* block # of "block" */
361 xfs_buf_t *bp; /* buffer for "block" */
362 int error; /* error return value */
363 xfs_extnum_t i=0, j; /* index into the extents list */
364 xfs_ifork_t *ifp; /* fork structure */
365 int level; /* btree level, for checking */
366 xfs_mount_t *mp; /* file system mount structure */
367 __be64 *pp; /* pointer to block address */
368 xfs_bmbt_rec_t *ep; /* pointer to current extent */
369 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
370 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
371 int bp_release = 0;
372
373 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
374 return;
375 }
376
e3543819
DC
377 /* skip large extent count inodes */
378 if (ip->i_d.di_nextents > 10000)
379 return;
380
9e5987a7
DC
381 bno = NULLFSBLOCK;
382 mp = ip->i_mount;
383 ifp = XFS_IFORK_PTR(ip, whichfork);
384 block = ifp->if_broot;
385 /*
386 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
387 */
388 level = be16_to_cpu(block->bb_level);
389 ASSERT(level > 0);
390 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
391 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
392 bno = be64_to_cpu(*pp);
393
d5cf09ba 394 ASSERT(bno != NULLFSBLOCK);
9e5987a7
DC
395 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
396 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
397
398 /*
399 * Go down the tree until leaf level is reached, following the first
400 * pointer (leftmost) at each level.
401 */
402 while (level-- > 0) {
403 /* See if buf is in cur first */
404 bp_release = 0;
405 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
406 if (!bp) {
407 bp_release = 1;
408 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
409 XFS_BMAP_BTREE_REF,
410 &xfs_bmbt_buf_ops);
572a4cf0 411 if (error)
9e5987a7 412 goto error_norelse;
1da177e4 413 }
9e5987a7 414 block = XFS_BUF_TO_BLOCK(bp);
9e5987a7
DC
415 if (level == 0)
416 break;
1da177e4 417
1da177e4 418 /*
9e5987a7
DC
419 * Check this block for basic sanity (increasing keys and
420 * no duplicate blocks).
1da177e4 421 */
0b1b213f 422
9e5987a7
DC
423 xfs_check_block(block, mp, 0, 0);
424 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
425 bno = be64_to_cpu(*pp);
c29aad41
ES
426 XFS_WANT_CORRUPTED_GOTO(mp,
427 XFS_FSB_SANITY_CHECK(mp, bno), error0);
9e5987a7
DC
428 if (bp_release) {
429 bp_release = 0;
430 xfs_trans_brelse(NULL, bp);
1da177e4 431 }
9e5987a7 432 }
ec90c556 433
9e5987a7
DC
434 /*
435 * Here with bp and block set to the leftmost leaf node in the tree.
436 */
437 i = 0;
438
439 /*
440 * Loop over all leaf nodes checking that all extents are in the right order.
441 */
442 for (;;) {
443 xfs_fsblock_t nextbno;
444 xfs_extnum_t num_recs;
445
446
447 num_recs = xfs_btree_get_numrecs(block);
1da177e4 448
1da177e4 449 /*
9e5987a7 450 * Read-ahead the next leaf block, if any.
1da177e4 451 */
8096b1eb 452
9e5987a7 453 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1da177e4 454
1da177e4 455 /*
9e5987a7
DC
456 * Check all the extents to make sure they are OK.
457 * If we had a previous block, the last entry should
458 * conform with the first entry in this one.
1da177e4 459 */
ec90c556 460
9e5987a7
DC
461 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
462 if (i) {
463 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
464 xfs_bmbt_disk_get_blockcount(&last) <=
465 xfs_bmbt_disk_get_startoff(ep));
466 }
467 for (j = 1; j < num_recs; j++) {
468 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
469 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
470 xfs_bmbt_disk_get_blockcount(ep) <=
471 xfs_bmbt_disk_get_startoff(nextp));
472 ep = nextp;
473 }
1da177e4 474
9e5987a7
DC
475 last = *ep;
476 i += num_recs;
477 if (bp_release) {
478 bp_release = 0;
479 xfs_trans_brelse(NULL, bp);
480 }
481 bno = nextbno;
1da177e4 482 /*
9e5987a7 483 * If we've reached the end, stop.
1da177e4 484 */
9e5987a7
DC
485 if (bno == NULLFSBLOCK)
486 break;
8096b1eb 487
9e5987a7
DC
488 bp_release = 0;
489 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
490 if (!bp) {
491 bp_release = 1;
492 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
493 XFS_BMAP_BTREE_REF,
494 &xfs_bmbt_buf_ops);
b9b984d7 495 if (error)
9e5987a7 496 goto error_norelse;
1da177e4 497 }
9e5987a7 498 block = XFS_BUF_TO_BLOCK(bp);
a5bd606b 499 }
a5fd276b 500
9e5987a7 501 return;
a5bd606b 502
9e5987a7
DC
503error0:
504 xfs_warn(mp, "%s: at error0", __func__);
505 if (bp_release)
506 xfs_trans_brelse(NULL, bp);
507error_norelse:
508 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
509 __func__, i);
510 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
511 return;
1da177e4
LT
512}
513
514/*
9e5987a7 515 * Add bmap trace insert entries for all the contents of the extent records.
1da177e4 516 */
9e5987a7
DC
517void
518xfs_bmap_trace_exlist(
519 xfs_inode_t *ip, /* incore inode pointer */
520 xfs_extnum_t cnt, /* count of entries in the list */
c44a1f22 521 int whichfork, /* data or attr or cow fork */
9e5987a7 522 unsigned long caller_ip)
1da177e4 523{
9e5987a7
DC
524 xfs_extnum_t idx; /* extent record index */
525 xfs_ifork_t *ifp; /* inode fork pointer */
526 int state = 0;
a5bd606b 527
9e5987a7
DC
528 if (whichfork == XFS_ATTR_FORK)
529 state |= BMAP_ATTRFORK;
c44a1f22
ES
530 else if (whichfork == XFS_COW_FORK)
531 state |= BMAP_COWFORK;
a5bd606b 532
9e5987a7 533 ifp = XFS_IFORK_PTR(ip, whichfork);
5d829300 534 ASSERT(cnt == xfs_iext_count(ifp));
9e5987a7 535 for (idx = 0; idx < cnt; idx++)
7710517f 536 trace_xfs_extlist(ip, idx, state, caller_ip);
9e5987a7 537}
a5bd606b 538
9e5987a7
DC
539/*
540 * Validate that the bmbt_irecs being returned from bmapi are valid
a97f4df7
ZYW
541 * given the caller's original parameters. Specifically check the
542 * ranges of the returned irecs to ensure that they only extend beyond
9e5987a7
DC
543 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
544 */
545STATIC void
546xfs_bmap_validate_ret(
547 xfs_fileoff_t bno,
548 xfs_filblks_t len,
549 int flags,
550 xfs_bmbt_irec_t *mval,
551 int nmap,
552 int ret_nmap)
553{
554 int i; /* index to map values */
a5bd606b 555
9e5987a7 556 ASSERT(ret_nmap <= nmap);
a5bd606b 557
9e5987a7
DC
558 for (i = 0; i < ret_nmap; i++) {
559 ASSERT(mval[i].br_blockcount > 0);
560 if (!(flags & XFS_BMAPI_ENTIRE)) {
561 ASSERT(mval[i].br_startoff >= bno);
562 ASSERT(mval[i].br_blockcount <= len);
563 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
564 bno + len);
565 } else {
566 ASSERT(mval[i].br_startoff < bno + len);
567 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
568 bno);
569 }
570 ASSERT(i == 0 ||
571 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
572 mval[i].br_startoff);
573 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
574 mval[i].br_startblock != HOLESTARTBLOCK);
575 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
576 mval[i].br_state == XFS_EXT_UNWRITTEN);
577 }
578}
7574aa92 579
9e5987a7
DC
580#else
581#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
582#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
583#endif /* DEBUG */
7574aa92 584
9e5987a7
DC
585/*
586 * bmap free list manipulation functions
587 */
7574aa92 588
9e5987a7
DC
589/*
590 * Add the extent to the list of extents to be free at transaction end.
591 * The list is maintained sorted (by block number).
592 */
593void
594xfs_bmap_add_free(
340785cc
DW
595 struct xfs_mount *mp,
596 struct xfs_defer_ops *dfops,
597 xfs_fsblock_t bno,
598 xfs_filblks_t len,
599 struct xfs_owner_info *oinfo)
9e5987a7 600{
310a75a3 601 struct xfs_extent_free_item *new; /* new element */
9e5987a7
DC
602#ifdef DEBUG
603 xfs_agnumber_t agno;
604 xfs_agblock_t agbno;
605
606 ASSERT(bno != NULLFSBLOCK);
607 ASSERT(len > 0);
608 ASSERT(len <= MAXEXTLEN);
609 ASSERT(!isnullstartblock(bno));
610 agno = XFS_FSB_TO_AGNO(mp, bno);
611 agbno = XFS_FSB_TO_AGBNO(mp, bno);
612 ASSERT(agno < mp->m_sb.sb_agcount);
613 ASSERT(agbno < mp->m_sb.sb_agblocks);
614 ASSERT(len < mp->m_sb.sb_agblocks);
615 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
616#endif
617 ASSERT(xfs_bmap_free_item_zone != NULL);
340785cc 618
9e5987a7 619 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
310a75a3
DW
620 new->xefi_startblock = bno;
621 new->xefi_blockcount = (xfs_extlen_t)len;
340785cc
DW
622 if (oinfo)
623 new->xefi_oinfo = *oinfo;
624 else
625 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
ba9e7802
DW
626 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
627 XFS_FSB_TO_AGBNO(mp, bno), len);
2c3234d1 628 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
9e5987a7 629}
0b1b213f 630
9e5987a7
DC
631/*
632 * Inode fork format manipulation functions
633 */
1da177e4 634
9e5987a7
DC
635/*
636 * Transform a btree format file with only one leaf node, where the
637 * extents list will fit in the inode, into an extents format file.
638 * Since the file extents are already in-core, all we have to do is
639 * give up the space for the btree root and pitch the leaf block.
640 */
641STATIC int /* error */
642xfs_bmap_btree_to_extents(
643 xfs_trans_t *tp, /* transaction pointer */
644 xfs_inode_t *ip, /* incore inode pointer */
645 xfs_btree_cur_t *cur, /* btree cursor */
646 int *logflagsp, /* inode logging flags */
647 int whichfork) /* data or attr fork */
648{
649 /* REFERENCED */
650 struct xfs_btree_block *cblock;/* child btree block */
651 xfs_fsblock_t cbno; /* child block number */
652 xfs_buf_t *cbp; /* child block's buffer */
653 int error; /* error return value */
654 xfs_ifork_t *ifp; /* inode fork data */
655 xfs_mount_t *mp; /* mount point structure */
656 __be64 *pp; /* ptr to block address */
657 struct xfs_btree_block *rblock;/* root btree block */
340785cc 658 struct xfs_owner_info oinfo;
1da177e4 659
9e5987a7
DC
660 mp = ip->i_mount;
661 ifp = XFS_IFORK_PTR(ip, whichfork);
60b4984f 662 ASSERT(whichfork != XFS_COW_FORK);
9e5987a7
DC
663 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
664 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
665 rblock = ifp->if_broot;
666 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
667 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
668 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
669 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
670 cbno = be64_to_cpu(*pp);
671 *logflagsp = 0;
672#ifdef DEBUG
673 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
674 return error;
675#endif
676 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
677 &xfs_bmbt_buf_ops);
678 if (error)
679 return error;
680 cblock = XFS_BUF_TO_BLOCK(cbp);
681 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
682 return error;
340785cc
DW
683 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
684 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
9e5987a7
DC
685 ip->i_d.di_nblocks--;
686 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
687 xfs_trans_binval(tp, cbp);
688 if (cur->bc_bufs[0] == cbp)
689 cur->bc_bufs[0] = NULL;
690 xfs_iroot_realloc(ip, -1, whichfork);
691 ASSERT(ifp->if_broot == NULL);
692 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
693 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
694 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
695 return 0;
696}
0b1b213f 697
9e5987a7
DC
698/*
699 * Convert an extents-format file into a btree-format file.
700 * The new file will have a root block (in the inode) and a single child block.
701 */
702STATIC int /* error */
703xfs_bmap_extents_to_btree(
704 xfs_trans_t *tp, /* transaction pointer */
705 xfs_inode_t *ip, /* incore inode pointer */
706 xfs_fsblock_t *firstblock, /* first-block-allocated */
2c3234d1 707 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
9e5987a7
DC
708 xfs_btree_cur_t **curp, /* cursor returned to caller */
709 int wasdel, /* converting a delayed alloc */
710 int *logflagsp, /* inode logging flags */
711 int whichfork) /* data or attr fork */
712{
713 struct xfs_btree_block *ablock; /* allocated (child) bt block */
714 xfs_buf_t *abp; /* buffer for ablock */
715 xfs_alloc_arg_t args; /* allocation arguments */
716 xfs_bmbt_rec_t *arp; /* child record pointer */
717 struct xfs_btree_block *block; /* btree root block */
718 xfs_btree_cur_t *cur; /* bmap btree cursor */
719 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
720 int error; /* error return value */
721 xfs_extnum_t i, cnt; /* extent record index */
722 xfs_ifork_t *ifp; /* inode fork pointer */
723 xfs_bmbt_key_t *kp; /* root block key pointer */
724 xfs_mount_t *mp; /* mount structure */
725 xfs_extnum_t nextents; /* number of file extents */
726 xfs_bmbt_ptr_t *pp; /* root block address pointer */
1da177e4 727
ee1a47ab 728 mp = ip->i_mount;
60b4984f 729 ASSERT(whichfork != XFS_COW_FORK);
9e5987a7
DC
730 ifp = XFS_IFORK_PTR(ip, whichfork);
731 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
0b1b213f 732
9e5987a7
DC
733 /*
734 * Make space in the inode incore.
735 */
736 xfs_iroot_realloc(ip, 1, whichfork);
737 ifp->if_flags |= XFS_IFBROOT;
ec90c556 738
9e5987a7
DC
739 /*
740 * Fill in the root.
741 */
742 block = ifp->if_broot;
ee1a47ab
CH
743 if (xfs_sb_version_hascrc(&mp->m_sb))
744 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
745 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
746 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
747 else
748 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
749 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
750 XFS_BTREE_LONG_PTRS);
0b1b213f 751
9e5987a7
DC
752 /*
753 * Need a cursor. Can't allocate until bb_level is filled in.
754 */
9e5987a7
DC
755 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
756 cur->bc_private.b.firstblock = *firstblock;
2c3234d1 757 cur->bc_private.b.dfops = dfops;
9e5987a7
DC
758 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
759 /*
760 * Convert to a btree with two levels, one record in root.
761 */
762 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
763 memset(&args, 0, sizeof(args));
764 args.tp = tp;
765 args.mp = mp;
340785cc 766 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
9e5987a7
DC
767 args.firstblock = *firstblock;
768 if (*firstblock == NULLFSBLOCK) {
769 args.type = XFS_ALLOCTYPE_START_BNO;
770 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
2c3234d1 771 } else if (dfops->dop_low) {
90e2056d 772try_another_ag:
9e5987a7
DC
773 args.type = XFS_ALLOCTYPE_START_BNO;
774 args.fsbno = *firstblock;
775 } else {
776 args.type = XFS_ALLOCTYPE_NEAR_BNO;
777 args.fsbno = *firstblock;
778 }
779 args.minlen = args.maxlen = args.prod = 1;
780 args.wasdel = wasdel;
781 *logflagsp = 0;
782 if ((error = xfs_alloc_vextent(&args))) {
783 xfs_iroot_realloc(ip, -1, whichfork);
784 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
785 return error;
786 }
90e2056d
DW
787
788 /*
789 * During a CoW operation, the allocation and bmbt updates occur in
790 * different transactions. The mapping code tries to put new bmbt
791 * blocks near extents being mapped, but the only way to guarantee this
792 * is if the alloc and the mapping happen in a single transaction that
793 * has a block reservation. That isn't the case here, so if we run out
794 * of space we'll try again with another AG.
795 */
796 if (xfs_sb_version_hasreflink(&cur->bc_mp->m_sb) &&
797 args.fsbno == NULLFSBLOCK &&
798 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
799 dfops->dop_low = true;
800 goto try_another_ag;
801 }
9e5987a7
DC
802 /*
803 * Allocation can't fail, the space was reserved.
804 */
805 ASSERT(args.fsbno != NULLFSBLOCK);
806 ASSERT(*firstblock == NULLFSBLOCK ||
807 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
2c3234d1 808 (dfops->dop_low &&
9e5987a7
DC
809 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
810 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
811 cur->bc_private.b.allocated++;
812 ip->i_d.di_nblocks++;
813 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
814 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
815 /*
816 * Fill in the child block.
817 */
818 abp->b_ops = &xfs_bmbt_buf_ops;
819 ablock = XFS_BUF_TO_BLOCK(abp);
ee1a47ab
CH
820 if (xfs_sb_version_hascrc(&mp->m_sb))
821 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
822 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
823 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
824 else
825 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
826 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
827 XFS_BTREE_LONG_PTRS);
828
9e5987a7 829 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
5d829300 830 nextents = xfs_iext_count(ifp);
9e5987a7
DC
831 for (cnt = i = 0; i < nextents; i++) {
832 ep = xfs_iext_get_ext(ifp, i);
833 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
834 arp->l0 = cpu_to_be64(ep->l0);
835 arp->l1 = cpu_to_be64(ep->l1);
836 arp++; cnt++;
1da177e4 837 }
9e5987a7
DC
838 }
839 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
840 xfs_btree_set_numrecs(ablock, cnt);
1da177e4 841
9e5987a7
DC
842 /*
843 * Fill in the root key and pointer.
844 */
845 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
846 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
847 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
848 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
849 be16_to_cpu(block->bb_level)));
850 *pp = cpu_to_be64(args.fsbno);
ec90c556 851
9e5987a7
DC
852 /*
853 * Do all this logging at the end so that
854 * the root is at the right level.
855 */
856 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
857 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
858 ASSERT(*curp == NULL);
859 *curp = cur;
860 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
861 return 0;
862}
ec90c556 863
9e5987a7
DC
864/*
865 * Convert a local file to an extents file.
866 * This code is out of bounds for data forks of regular files,
867 * since the file data needs to get logged so things will stay consistent.
868 * (The bmap-level manipulations are ok, though).
869 */
f3508bcd
DC
870void
871xfs_bmap_local_to_extents_empty(
872 struct xfs_inode *ip,
873 int whichfork)
874{
875 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
876
60b4984f 877 ASSERT(whichfork != XFS_COW_FORK);
f3508bcd
DC
878 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
879 ASSERT(ifp->if_bytes == 0);
880 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
881
6a9edd3d 882 xfs_bmap_forkoff_reset(ip, whichfork);
f3508bcd
DC
883 ifp->if_flags &= ~XFS_IFINLINE;
884 ifp->if_flags |= XFS_IFEXTENTS;
885 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
886}
887
888
9e5987a7
DC
889STATIC int /* error */
890xfs_bmap_local_to_extents(
891 xfs_trans_t *tp, /* transaction pointer */
892 xfs_inode_t *ip, /* incore inode pointer */
893 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
894 xfs_extlen_t total, /* total blocks needed by transaction */
895 int *logflagsp, /* inode logging flags */
896 int whichfork,
ee1a47ab
CH
897 void (*init_fn)(struct xfs_trans *tp,
898 struct xfs_buf *bp,
9e5987a7
DC
899 struct xfs_inode *ip,
900 struct xfs_ifork *ifp))
901{
f3508bcd 902 int error = 0;
9e5987a7
DC
903 int flags; /* logging flags returned */
904 xfs_ifork_t *ifp; /* inode fork pointer */
f3508bcd
DC
905 xfs_alloc_arg_t args; /* allocation arguments */
906 xfs_buf_t *bp; /* buffer for extent block */
907 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
0b1b213f 908
9e5987a7
DC
909 /*
910 * We don't want to deal with the case of keeping inode data inline yet.
911 * So sending the data fork of a regular inode is invalid.
912 */
c19b3b05 913 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
9e5987a7
DC
914 ifp = XFS_IFORK_PTR(ip, whichfork);
915 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
f3508bcd
DC
916
917 if (!ifp->if_bytes) {
918 xfs_bmap_local_to_extents_empty(ip, whichfork);
919 flags = XFS_ILOG_CORE;
920 goto done;
921 }
922
9e5987a7
DC
923 flags = 0;
924 error = 0;
f3508bcd
DC
925 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
926 XFS_IFINLINE);
927 memset(&args, 0, sizeof(args));
928 args.tp = tp;
929 args.mp = ip->i_mount;
340785cc 930 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
f3508bcd
DC
931 args.firstblock = *firstblock;
932 /*
933 * Allocate a block. We know we need only one, since the
934 * file currently fits in an inode.
935 */
936 if (*firstblock == NULLFSBLOCK) {
90e2056d 937try_another_ag:
f3508bcd
DC
938 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
939 args.type = XFS_ALLOCTYPE_START_BNO;
9e5987a7 940 } else {
f3508bcd
DC
941 args.fsbno = *firstblock;
942 args.type = XFS_ALLOCTYPE_NEAR_BNO;
9e5987a7 943 }
f3508bcd
DC
944 args.total = total;
945 args.minlen = args.maxlen = args.prod = 1;
946 error = xfs_alloc_vextent(&args);
947 if (error)
948 goto done;
949
90e2056d
DW
950 /*
951 * During a CoW operation, the allocation and bmbt updates occur in
952 * different transactions. The mapping code tries to put new bmbt
953 * blocks near extents being mapped, but the only way to guarantee this
954 * is if the alloc and the mapping happen in a single transaction that
955 * has a block reservation. That isn't the case here, so if we run out
956 * of space we'll try again with another AG.
957 */
958 if (xfs_sb_version_hasreflink(&ip->i_mount->m_sb) &&
959 args.fsbno == NULLFSBLOCK &&
960 args.type == XFS_ALLOCTYPE_NEAR_BNO) {
961 goto try_another_ag;
962 }
f3508bcd
DC
963 /* Can't fail, the space was reserved. */
964 ASSERT(args.fsbno != NULLFSBLOCK);
965 ASSERT(args.len == 1);
966 *firstblock = args.fsbno;
967 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
968
fe22d552 969 /*
b7cdc66b 970 * Initialize the block, copy the data and log the remote buffer.
fe22d552 971 *
b7cdc66b
BF
972 * The callout is responsible for logging because the remote format
973 * might differ from the local format and thus we don't know how much to
974 * log here. Note that init_fn must also set the buffer log item type
975 * correctly.
fe22d552 976 */
f3508bcd
DC
977 init_fn(tp, bp, ip, ifp);
978
b7cdc66b 979 /* account for the change in fork size */
f3508bcd
DC
980 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
981 xfs_bmap_local_to_extents_empty(ip, whichfork);
9e5987a7 982 flags |= XFS_ILOG_CORE;
f3508bcd
DC
983
984 xfs_iext_add(ifp, 0, 1);
985 ep = xfs_iext_get_ext(ifp, 0);
986 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
987 trace_xfs_bmap_post_update(ip, 0,
988 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
989 _THIS_IP_);
990 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
991 ip->i_d.di_nblocks = 1;
992 xfs_trans_mod_dquot_byino(tp, ip,
993 XFS_TRANS_DQ_BCOUNT, 1L);
994 flags |= xfs_ilog_fext(whichfork);
995
9e5987a7
DC
996done:
997 *logflagsp = flags;
998 return error;
999}
ec90c556 1000
9e5987a7
DC
1001/*
1002 * Called from xfs_bmap_add_attrfork to handle btree format files.
1003 */
1004STATIC int /* error */
1005xfs_bmap_add_attrfork_btree(
1006 xfs_trans_t *tp, /* transaction pointer */
1007 xfs_inode_t *ip, /* incore inode pointer */
1008 xfs_fsblock_t *firstblock, /* first block allocated */
2c3234d1 1009 struct xfs_defer_ops *dfops, /* blocks to free at commit */
9e5987a7
DC
1010 int *flags) /* inode logging flags */
1011{
1012 xfs_btree_cur_t *cur; /* btree cursor */
1013 int error; /* error return value */
1014 xfs_mount_t *mp; /* file system mount struct */
1015 int stat; /* newroot status */
ec90c556 1016
9e5987a7
DC
1017 mp = ip->i_mount;
1018 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1019 *flags |= XFS_ILOG_DBROOT;
1020 else {
1021 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
2c3234d1 1022 cur->bc_private.b.dfops = dfops;
9e5987a7
DC
1023 cur->bc_private.b.firstblock = *firstblock;
1024 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1025 goto error0;
1026 /* must be at least one entry */
c29aad41 1027 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
9e5987a7
DC
1028 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
1029 goto error0;
1030 if (stat == 0) {
1031 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
2451337d 1032 return -ENOSPC;
1da177e4 1033 }
9e5987a7
DC
1034 *firstblock = cur->bc_private.b.firstblock;
1035 cur->bc_private.b.allocated = 0;
1036 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1da177e4 1037 }
9e5987a7
DC
1038 return 0;
1039error0:
1040 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1041 return error;
1042}
a5bd606b 1043
9e5987a7
DC
1044/*
1045 * Called from xfs_bmap_add_attrfork to handle extents format files.
1046 */
1047STATIC int /* error */
1048xfs_bmap_add_attrfork_extents(
1049 xfs_trans_t *tp, /* transaction pointer */
1050 xfs_inode_t *ip, /* incore inode pointer */
1051 xfs_fsblock_t *firstblock, /* first block allocated */
2c3234d1 1052 struct xfs_defer_ops *dfops, /* blocks to free at commit */
9e5987a7
DC
1053 int *flags) /* inode logging flags */
1054{
1055 xfs_btree_cur_t *cur; /* bmap btree cursor */
1056 int error; /* error return value */
a5bd606b 1057
9e5987a7
DC
1058 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1059 return 0;
1060 cur = NULL;
2c3234d1 1061 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
9e5987a7 1062 flags, XFS_DATA_FORK);
a5bd606b
CH
1063 if (cur) {
1064 cur->bc_private.b.allocated = 0;
9e5987a7
DC
1065 xfs_btree_del_cursor(cur,
1066 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
a5bd606b 1067 }
1da177e4 1068 return error;
1da177e4
LT
1069}
1070
9e5987a7
DC
1071/*
1072 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1073 * different data fork content type needs a different callout to do the
1074 * conversion. Some are basic and only require special block initialisation
1075 * callouts for the data formating, others (directories) are so specialised they
1076 * handle everything themselves.
1077 *
1078 * XXX (dgc): investigate whether directory conversion can use the generic
1079 * formatting callout. It should be possible - it's just a very complex
ee1a47ab 1080 * formatter.
9e5987a7
DC
1081 */
1082STATIC int /* error */
1083xfs_bmap_add_attrfork_local(
1084 xfs_trans_t *tp, /* transaction pointer */
1085 xfs_inode_t *ip, /* incore inode pointer */
1086 xfs_fsblock_t *firstblock, /* first block allocated */
2c3234d1 1087 struct xfs_defer_ops *dfops, /* blocks to free at commit */
9e5987a7
DC
1088 int *flags) /* inode logging flags */
1089{
1090 xfs_da_args_t dargs; /* args for dir/attr code */
7574aa92 1091
9e5987a7
DC
1092 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1093 return 0;
7574aa92 1094
c19b3b05 1095 if (S_ISDIR(VFS_I(ip)->i_mode)) {
9e5987a7 1096 memset(&dargs, 0, sizeof(dargs));
d6cf1305 1097 dargs.geo = ip->i_mount->m_dir_geo;
9e5987a7
DC
1098 dargs.dp = ip;
1099 dargs.firstblock = firstblock;
2c3234d1 1100 dargs.dfops = dfops;
d6cf1305 1101 dargs.total = dargs.geo->fsbcount;
9e5987a7
DC
1102 dargs.whichfork = XFS_DATA_FORK;
1103 dargs.trans = tp;
1104 return xfs_dir2_sf_to_block(&dargs);
1da177e4 1105 }
7574aa92 1106
c19b3b05 1107 if (S_ISLNK(VFS_I(ip)->i_mode))
9e5987a7
DC
1108 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1109 flags, XFS_DATA_FORK,
1110 xfs_symlink_local_to_remote);
7574aa92 1111
f3508bcd
DC
1112 /* should only be called for types that support local format data */
1113 ASSERT(0);
2451337d 1114 return -EFSCORRUPTED;
9e5987a7 1115}
0b1b213f 1116
9e5987a7
DC
1117/*
1118 * Convert inode from non-attributed to attributed.
1119 * Must not be in a transaction, ip must not be locked.
1120 */
1121int /* error code */
1122xfs_bmap_add_attrfork(
1123 xfs_inode_t *ip, /* incore inode pointer */
1124 int size, /* space new attribute needs */
1125 int rsvd) /* xact may use reserved blks */
1126{
1127 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
2c3234d1 1128 struct xfs_defer_ops dfops; /* freed extent records */
9e5987a7
DC
1129 xfs_mount_t *mp; /* mount structure */
1130 xfs_trans_t *tp; /* transaction pointer */
1131 int blks; /* space reservation */
1132 int version = 1; /* superblock attr version */
9e5987a7
DC
1133 int logflags; /* logging flags */
1134 int error; /* error return value */
0b1b213f 1135
9e5987a7 1136 ASSERT(XFS_IFORK_Q(ip) == 0);
1da177e4 1137
9e5987a7
DC
1138 mp = ip->i_mount;
1139 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
253f4911 1140
9e5987a7 1141 blks = XFS_ADDAFORK_SPACE_RES(mp);
253f4911
CH
1142
1143 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1144 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1145 if (error)
9e3908e3 1146 return error;
253f4911 1147
9e5987a7
DC
1148 xfs_ilock(ip, XFS_ILOCK_EXCL);
1149 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1150 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1151 XFS_QMOPT_RES_REGBLKS);
9e3908e3
MT
1152 if (error)
1153 goto trans_cancel;
9e5987a7 1154 if (XFS_IFORK_Q(ip))
9e3908e3 1155 goto trans_cancel;
0f352f8e
DW
1156 if (ip->i_d.di_anextents != 0) {
1157 error = -EFSCORRUPTED;
1158 goto trans_cancel;
1159 }
9e5987a7 1160 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1da177e4 1161 /*
9e5987a7 1162 * For inodes coming from pre-6.2 filesystems.
1da177e4 1163 */
9e5987a7
DC
1164 ASSERT(ip->i_d.di_aformat == 0);
1165 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1166 }
ec90c556 1167
9e3908e3 1168 xfs_trans_ijoin(tp, ip, 0);
9e5987a7 1169 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1da177e4 1170
9e5987a7
DC
1171 switch (ip->i_d.di_format) {
1172 case XFS_DINODE_FMT_DEV:
1173 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1174 break;
1175 case XFS_DINODE_FMT_UUID:
1176 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1177 break;
1178 case XFS_DINODE_FMT_LOCAL:
1179 case XFS_DINODE_FMT_EXTENTS:
1180 case XFS_DINODE_FMT_BTREE:
1181 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1182 if (!ip->i_d.di_forkoff)
1183 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1184 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1185 version = 2;
1da177e4 1186 break;
9e5987a7
DC
1187 default:
1188 ASSERT(0);
2451337d 1189 error = -EINVAL;
9e3908e3 1190 goto trans_cancel;
9e5987a7 1191 }
1da177e4 1192
9e5987a7
DC
1193 ASSERT(ip->i_afp == NULL);
1194 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1195 ip->i_afp->if_flags = XFS_IFEXTENTS;
1196 logflags = 0;
2c3234d1 1197 xfs_defer_init(&dfops, &firstblock);
9e5987a7
DC
1198 switch (ip->i_d.di_format) {
1199 case XFS_DINODE_FMT_LOCAL:
2c3234d1 1200 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
9e5987a7
DC
1201 &logflags);
1202 break;
1203 case XFS_DINODE_FMT_EXTENTS:
1204 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
2c3234d1 1205 &dfops, &logflags);
9e5987a7
DC
1206 break;
1207 case XFS_DINODE_FMT_BTREE:
2c3234d1 1208 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
9e5987a7
DC
1209 &logflags);
1210 break;
1211 default:
1212 error = 0;
1da177e4
LT
1213 break;
1214 }
9e5987a7
DC
1215 if (logflags)
1216 xfs_trans_log_inode(tp, ip, logflags);
1217 if (error)
9e3908e3 1218 goto bmap_cancel;
9e5987a7
DC
1219 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1220 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
61e63ecb 1221 bool log_sb = false;
9e5987a7
DC
1222
1223 spin_lock(&mp->m_sb_lock);
1224 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1225 xfs_sb_version_addattr(&mp->m_sb);
61e63ecb 1226 log_sb = true;
9e5987a7
DC
1227 }
1228 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1229 xfs_sb_version_addattr2(&mp->m_sb);
61e63ecb 1230 log_sb = true;
9e5987a7 1231 }
4d11a402 1232 spin_unlock(&mp->m_sb_lock);
61e63ecb
DC
1233 if (log_sb)
1234 xfs_log_sb(tp);
1da177e4 1235 }
9e5987a7 1236
2c3234d1 1237 error = xfs_defer_finish(&tp, &dfops, NULL);
9e5987a7 1238 if (error)
9e3908e3 1239 goto bmap_cancel;
70393313 1240 error = xfs_trans_commit(tp);
9e3908e3
MT
1241 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1242 return error;
1243
1244bmap_cancel:
2c3234d1 1245 xfs_defer_cancel(&dfops);
9e3908e3 1246trans_cancel:
4906e215 1247 xfs_trans_cancel(tp);
9e5987a7 1248 xfs_iunlock(ip, XFS_ILOCK_EXCL);
9e5987a7 1249 return error;
1da177e4
LT
1250}
1251
1252/*
9e5987a7 1253 * Internal and external extent tree search functions.
1da177e4 1254 */
a5bd606b 1255
9e5987a7
DC
1256/*
1257 * Read in the extents to if_extents.
1258 * All inode fields are set up by caller, we just traverse the btree
1259 * and copy the records in. If the file system cannot contain unwritten
1260 * extents, the records are checked for no "state" flags.
1261 */
1262int /* error */
1263xfs_bmap_read_extents(
1264 xfs_trans_t *tp, /* transaction pointer */
1265 xfs_inode_t *ip, /* incore inode */
1266 int whichfork) /* data or attr fork */
1267{
1268 struct xfs_btree_block *block; /* current btree block */
1269 xfs_fsblock_t bno; /* block # of "block" */
1270 xfs_buf_t *bp; /* buffer for "block" */
1271 int error; /* error return value */
1272 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1273 xfs_extnum_t i, j; /* index into the extents list */
1274 xfs_ifork_t *ifp; /* fork structure */
1275 int level; /* btree level, for checking */
1276 xfs_mount_t *mp; /* file system mount structure */
1277 __be64 *pp; /* pointer to block address */
1278 /* REFERENCED */
1279 xfs_extnum_t room; /* number of entries there's room for */
6ef35544 1280
9e5987a7
DC
1281 bno = NULLFSBLOCK;
1282 mp = ip->i_mount;
1283 ifp = XFS_IFORK_PTR(ip, whichfork);
1284 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1285 XFS_EXTFMT_INODE(ip);
1286 block = ifp->if_broot;
1da177e4 1287 /*
9e5987a7 1288 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1da177e4 1289 */
9e5987a7
DC
1290 level = be16_to_cpu(block->bb_level);
1291 ASSERT(level > 0);
1292 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1293 bno = be64_to_cpu(*pp);
d5cf09ba 1294 ASSERT(bno != NULLFSBLOCK);
9e5987a7
DC
1295 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1296 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1da177e4 1297 /*
9e5987a7
DC
1298 * Go down the tree until leaf level is reached, following the first
1299 * pointer (leftmost) at each level.
1da177e4 1300 */
9e5987a7
DC
1301 while (level-- > 0) {
1302 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1303 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1304 if (error)
1305 return error;
1306 block = XFS_BUF_TO_BLOCK(bp);
9e5987a7
DC
1307 if (level == 0)
1308 break;
1309 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1310 bno = be64_to_cpu(*pp);
c29aad41
ES
1311 XFS_WANT_CORRUPTED_GOTO(mp,
1312 XFS_FSB_SANITY_CHECK(mp, bno), error0);
9e5987a7 1313 xfs_trans_brelse(tp, bp);
1da177e4
LT
1314 }
1315 /*
9e5987a7 1316 * Here with bp and block set to the leftmost leaf node in the tree.
1da177e4 1317 */
5d829300 1318 room = xfs_iext_count(ifp);
9e5987a7 1319 i = 0;
1da177e4 1320 /*
9e5987a7 1321 * Loop over all leaf nodes. Copy information to the extent records.
1da177e4 1322 */
9e5987a7
DC
1323 for (;;) {
1324 xfs_bmbt_rec_t *frp;
1325 xfs_fsblock_t nextbno;
1326 xfs_extnum_t num_recs;
1327 xfs_extnum_t start;
0b1b213f 1328
9e5987a7
DC
1329 num_recs = xfs_btree_get_numrecs(block);
1330 if (unlikely(i + num_recs > room)) {
1331 ASSERT(i + num_recs <= room);
1332 xfs_warn(ip->i_mount,
1333 "corrupt dinode %Lu, (btree extents).",
1334 (unsigned long long) ip->i_ino);
1335 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1336 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1337 goto error0;
1da177e4 1338 }
1da177e4 1339 /*
9e5987a7 1340 * Read-ahead the next leaf block, if any.
1da177e4 1341 */
9e5987a7
DC
1342 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1343 if (nextbno != NULLFSBLOCK)
1344 xfs_btree_reada_bufl(mp, nextbno, 1,
1345 &xfs_bmbt_buf_ops);
1da177e4 1346 /*
9e5987a7 1347 * Copy records into the extent records.
1da177e4 1348 */
9e5987a7
DC
1349 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1350 start = i;
1351 for (j = 0; j < num_recs; j++, i++, frp++) {
1352 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1353 trp->l0 = be64_to_cpu(frp->l0);
1354 trp->l1 = be64_to_cpu(frp->l1);
1da177e4 1355 }
9e5987a7
DC
1356 if (exntf == XFS_EXTFMT_NOSTATE) {
1357 /*
1358 * Check all attribute bmap btree records and
1359 * any "older" data bmap btree records for a
1360 * set bit in the "extent flag" position.
1361 */
1362 if (unlikely(xfs_check_nostate_extents(ifp,
1363 start, num_recs))) {
1364 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1365 XFS_ERRLEVEL_LOW,
1366 ip->i_mount);
1367 goto error0;
1368 }
1369 }
1370 xfs_trans_brelse(tp, bp);
1371 bno = nextbno;
1da177e4 1372 /*
9e5987a7 1373 * If we've reached the end, stop.
1da177e4 1374 */
9e5987a7
DC
1375 if (bno == NULLFSBLOCK)
1376 break;
1377 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1378 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1379 if (error)
1380 return error;
1381 block = XFS_BUF_TO_BLOCK(bp);
1da177e4 1382 }
356a3225
DW
1383 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1384 return -EFSCORRUPTED;
5d829300 1385 ASSERT(i == xfs_iext_count(ifp));
9e5987a7
DC
1386 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1387 return 0;
1388error0:
1389 xfs_trans_brelse(tp, bp);
2451337d 1390 return -EFSCORRUPTED;
9e5987a7 1391}
a5bd606b 1392
9e5987a7
DC
1393/*
1394 * Returns the file-relative block number of the first unused block(s)
1395 * in the file with at least "len" logically contiguous blocks free.
1396 * This is the lowest-address hole if the file has holes, else the first block
1397 * past the end of file.
1398 * Return 0 if the file is currently local (in-inode).
1399 */
1400int /* error */
1401xfs_bmap_first_unused(
1402 xfs_trans_t *tp, /* transaction pointer */
1403 xfs_inode_t *ip, /* incore inode */
1404 xfs_extlen_t len, /* size of hole to find */
1405 xfs_fileoff_t *first_unused, /* unused block */
1406 int whichfork) /* data or attr fork */
1407{
1408 int error; /* error return value */
1409 int idx; /* extent record index */
1410 xfs_ifork_t *ifp; /* inode fork pointer */
1411 xfs_fileoff_t lastaddr; /* last block number seen */
1412 xfs_fileoff_t lowest; /* lowest useful block */
1413 xfs_fileoff_t max; /* starting useful block */
1414 xfs_fileoff_t off; /* offset for this block */
1415 xfs_extnum_t nextents; /* number of extent entries */
1416
1417 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1418 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1419 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1420 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1421 *first_unused = 0;
1422 return 0;
dd9f438e 1423 }
9e5987a7
DC
1424 ifp = XFS_IFORK_PTR(ip, whichfork);
1425 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1426 (error = xfs_iread_extents(tp, ip, whichfork)))
1427 return error;
1428 lowest = *first_unused;
5d829300 1429 nextents = xfs_iext_count(ifp);
9e5987a7
DC
1430 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1431 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1432 off = xfs_bmbt_get_startoff(ep);
1433 /*
1434 * See if the hole before this extent will work.
1435 */
1436 if (off >= lowest + len && off - max >= len) {
1437 *first_unused = max;
1438 return 0;
1439 }
1440 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1441 max = XFS_FILEOFF_MAX(lastaddr, lowest);
dd9f438e 1442 }
9e5987a7
DC
1443 *first_unused = max;
1444 return 0;
1445}
1446
1447/*
02bb4873 1448 * Returns the file-relative block number of the last block - 1 before
9e5987a7
DC
1449 * last_block (input value) in the file.
1450 * This is not based on i_size, it is based on the extent records.
1451 * Returns 0 for local files, as they do not have extent records.
1452 */
1453int /* error */
1454xfs_bmap_last_before(
86685f7b
CH
1455 struct xfs_trans *tp, /* transaction pointer */
1456 struct xfs_inode *ip, /* incore inode */
1457 xfs_fileoff_t *last_block, /* last block */
1458 int whichfork) /* data or attr fork */
9e5987a7 1459{
86685f7b
CH
1460 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1461 struct xfs_bmbt_irec got;
1462 xfs_extnum_t idx;
1463 int error;
9e5987a7 1464
86685f7b
CH
1465 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1466 case XFS_DINODE_FMT_LOCAL:
9e5987a7
DC
1467 *last_block = 0;
1468 return 0;
86685f7b
CH
1469 case XFS_DINODE_FMT_BTREE:
1470 case XFS_DINODE_FMT_EXTENTS:
1471 break;
1472 default:
1473 return -EIO;
9e5987a7 1474 }
86685f7b
CH
1475
1476 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1477 error = xfs_iread_extents(tp, ip, whichfork);
1478 if (error)
1479 return error;
dd9f438e 1480 }
86685f7b
CH
1481
1482 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1483 if (got.br_startoff <= *last_block - 1)
1484 return 0;
1485 }
1486
1487 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1488 *last_block = got.br_startoff + got.br_blockcount;
1489 return 0;
1490 }
1491
1492 *last_block = 0;
9e5987a7
DC
1493 return 0;
1494}
1495
68988114 1496int
9e5987a7
DC
1497xfs_bmap_last_extent(
1498 struct xfs_trans *tp,
1499 struct xfs_inode *ip,
1500 int whichfork,
1501 struct xfs_bmbt_irec *rec,
1502 int *is_empty)
1503{
1504 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1505 int error;
1506 int nextents;
1507
1508 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1509 error = xfs_iread_extents(tp, ip, whichfork);
1510 if (error)
1511 return error;
dd9f438e
NS
1512 }
1513
5d829300 1514 nextents = xfs_iext_count(ifp);
9e5987a7
DC
1515 if (nextents == 0) {
1516 *is_empty = 1;
1517 return 0;
1518 }
dd9f438e 1519
9e5987a7
DC
1520 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1521 *is_empty = 0;
dd9f438e
NS
1522 return 0;
1523}
1524
9e5987a7
DC
1525/*
1526 * Check the last inode extent to determine whether this allocation will result
1527 * in blocks being allocated at the end of the file. When we allocate new data
1528 * blocks at the end of the file which do not start at the previous data block,
1529 * we will try to align the new blocks at stripe unit boundaries.
1530 *
6e708bcf 1531 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
9e5987a7
DC
1532 * at, or past the EOF.
1533 */
1534STATIC int
1535xfs_bmap_isaeof(
1536 struct xfs_bmalloca *bma,
1537 int whichfork)
1da177e4 1538{
9e5987a7
DC
1539 struct xfs_bmbt_irec rec;
1540 int is_empty;
1541 int error;
1da177e4 1542
9e5987a7
DC
1543 bma->aeof = 0;
1544 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1545 &is_empty);
6e708bcf 1546 if (error)
9e5987a7 1547 return error;
1da177e4 1548
6e708bcf
DC
1549 if (is_empty) {
1550 bma->aeof = 1;
1551 return 0;
1552 }
1553
1da177e4 1554 /*
9e5987a7
DC
1555 * Check if we are allocation or past the last extent, or at least into
1556 * the last delayed allocated extent.
1da177e4 1557 */
9e5987a7
DC
1558 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1559 (bma->offset >= rec.br_startoff &&
1560 isnullstartblock(rec.br_startblock));
1561 return 0;
1562}
1da177e4 1563
9e5987a7
DC
1564/*
1565 * Returns the file-relative block number of the first block past eof in
1566 * the file. This is not based on i_size, it is based on the extent records.
1567 * Returns 0 for local files, as they do not have extent records.
1568 */
1569int
1570xfs_bmap_last_offset(
9e5987a7
DC
1571 struct xfs_inode *ip,
1572 xfs_fileoff_t *last_block,
1573 int whichfork)
1574{
1575 struct xfs_bmbt_irec rec;
1576 int is_empty;
1577 int error;
04e99455 1578
9e5987a7 1579 *last_block = 0;
0892ccd6 1580
9e5987a7
DC
1581 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1582 return 0;
a365bdd5 1583
9e5987a7
DC
1584 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1585 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
2451337d 1586 return -EIO;
a365bdd5 1587
9e5987a7
DC
1588 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1589 if (error || is_empty)
a365bdd5 1590 return error;
9e5987a7
DC
1591
1592 *last_block = rec.br_startoff + rec.br_blockcount;
a365bdd5
NS
1593 return 0;
1594}
1595
9e5987a7
DC
1596/*
1597 * Returns whether the selected fork of the inode has exactly one
1598 * block or not. For the data fork we check this matches di_size,
1599 * implying the file's range is 0..bsize-1.
1600 */
1601int /* 1=>1 block, 0=>otherwise */
1602xfs_bmap_one_block(
1603 xfs_inode_t *ip, /* incore inode */
1604 int whichfork) /* data or attr fork */
c467c049 1605{
9e5987a7
DC
1606 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1607 xfs_ifork_t *ifp; /* inode fork pointer */
1608 int rval; /* return value */
1609 xfs_bmbt_irec_t s; /* internal version of extent */
c467c049 1610
9e5987a7
DC
1611#ifndef DEBUG
1612 if (whichfork == XFS_DATA_FORK)
1613 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1614#endif /* !DEBUG */
1615 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1616 return 0;
1617 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1618 return 0;
1619 ifp = XFS_IFORK_PTR(ip, whichfork);
1620 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1621 ep = xfs_iext_get_ext(ifp, 0);
1622 xfs_bmbt_get_all(ep, &s);
1623 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1624 if (rval && whichfork == XFS_DATA_FORK)
1625 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1626 return rval;
1627}
c467c049 1628
9e5987a7
DC
1629/*
1630 * Extent tree manipulation functions used during allocation.
1631 */
c467c049 1632
9e5987a7
DC
1633/*
1634 * Convert a delayed allocation to a real allocation.
1635 */
1636STATIC int /* error */
1637xfs_bmap_add_extent_delay_real(
60b4984f
DW
1638 struct xfs_bmalloca *bma,
1639 int whichfork)
9e5987a7
DC
1640{
1641 struct xfs_bmbt_irec *new = &bma->got;
1642 int diff; /* temp value */
1643 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
1644 int error; /* error return value */
1645 int i; /* temp state */
1646 xfs_ifork_t *ifp; /* inode fork pointer */
1647 xfs_fileoff_t new_endoff; /* end offset of new entry */
1648 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1649 /* left is 0, right is 1, prev is 2 */
1650 int rval=0; /* return value (logging flags) */
1651 int state = 0;/* state bits, accessed thru macros */
1652 xfs_filblks_t da_new; /* new count del alloc blocks used */
1653 xfs_filblks_t da_old; /* old count del alloc blocks used */
1654 xfs_filblks_t temp=0; /* value for da_new calculations */
1655 xfs_filblks_t temp2=0;/* value for da_new calculations */
1656 int tmp_rval; /* partial logging flags */
c29aad41 1657 struct xfs_mount *mp;
60b4984f 1658 xfs_extnum_t *nextents;
c467c049 1659
f1f96c49 1660 mp = bma->ip->i_mount;
6d3eb1ec 1661 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
60b4984f
DW
1662 ASSERT(whichfork != XFS_ATTR_FORK);
1663 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1664 &bma->ip->i_d.di_nextents);
c467c049 1665
9e5987a7 1666 ASSERT(bma->idx >= 0);
5d829300 1667 ASSERT(bma->idx <= xfs_iext_count(ifp));
9e5987a7
DC
1668 ASSERT(!isnullstartblock(new->br_startblock));
1669 ASSERT(!bma->cur ||
1670 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
c467c049 1671
ff6d6af2 1672 XFS_STATS_INC(mp, xs_add_exlist);
c467c049 1673
9e5987a7
DC
1674#define LEFT r[0]
1675#define RIGHT r[1]
1676#define PREV r[2]
c467c049 1677
60b4984f
DW
1678 if (whichfork == XFS_COW_FORK)
1679 state |= BMAP_COWFORK;
1680
c467c049 1681 /*
9e5987a7 1682 * Set up a bunch of variables to make the tests simpler.
c467c049 1683 */
9e5987a7
DC
1684 ep = xfs_iext_get_ext(ifp, bma->idx);
1685 xfs_bmbt_get_all(ep, &PREV);
1686 new_endoff = new->br_startoff + new->br_blockcount;
1687 ASSERT(PREV.br_startoff <= new->br_startoff);
1688 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1689
1690 da_old = startblockval(PREV.br_startblock);
1691 da_new = 0;
1692
c467c049 1693 /*
9e5987a7
DC
1694 * Set flags determining what part of the previous delayed allocation
1695 * extent is being replaced by a real allocation.
c467c049 1696 */
9e5987a7
DC
1697 if (PREV.br_startoff == new->br_startoff)
1698 state |= BMAP_LEFT_FILLING;
1699 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1700 state |= BMAP_RIGHT_FILLING;
c467c049
CH
1701
1702 /*
9e5987a7
DC
1703 * Check and set flags if this segment has a left neighbor.
1704 * Don't set contiguous if the combined extent would be too large.
c467c049 1705 */
9e5987a7
DC
1706 if (bma->idx > 0) {
1707 state |= BMAP_LEFT_VALID;
1708 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
a99ebf43 1709
9e5987a7
DC
1710 if (isnullstartblock(LEFT.br_startblock))
1711 state |= BMAP_LEFT_DELAY;
a365bdd5 1712 }
a365bdd5 1713
9e5987a7
DC
1714 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1715 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1716 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1717 LEFT.br_state == new->br_state &&
1718 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1719 state |= BMAP_LEFT_CONTIG;
a365bdd5 1720
1da177e4 1721 /*
9e5987a7
DC
1722 * Check and set flags if this segment has a right neighbor.
1723 * Don't set contiguous if the combined extent would be too large.
1724 * Also check for all-three-contiguous being too large.
1da177e4 1725 */
5d829300 1726 if (bma->idx < xfs_iext_count(ifp) - 1) {
9e5987a7
DC
1727 state |= BMAP_RIGHT_VALID;
1728 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
14b064ce 1729
9e5987a7
DC
1730 if (isnullstartblock(RIGHT.br_startblock))
1731 state |= BMAP_RIGHT_DELAY;
1da177e4 1732 }
9e5987a7
DC
1733
1734 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1735 new_endoff == RIGHT.br_startoff &&
1736 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1737 new->br_state == RIGHT.br_state &&
1738 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1739 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1740 BMAP_RIGHT_FILLING)) !=
1741 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1742 BMAP_RIGHT_FILLING) ||
1743 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1744 <= MAXEXTLEN))
1745 state |= BMAP_RIGHT_CONTIG;
1746
1747 error = 0;
1da177e4 1748 /*
9e5987a7 1749 * Switch out based on the FILLING and CONTIG state bits.
1da177e4 1750 */
9e5987a7
DC
1751 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1752 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1753 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1754 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4 1755 /*
9e5987a7
DC
1756 * Filling in all of a previously delayed allocation extent.
1757 * The left and right neighbors are both contiguous with new.
1da177e4 1758 */
9e5987a7
DC
1759 bma->idx--;
1760 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1761 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1762 LEFT.br_blockcount + PREV.br_blockcount +
1763 RIGHT.br_blockcount);
1764 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1765
1766 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
60b4984f 1767 (*nextents)--;
9e5987a7
DC
1768 if (bma->cur == NULL)
1769 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1770 else {
1771 rval = XFS_ILOG_CORE;
1772 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1773 RIGHT.br_startblock,
1774 RIGHT.br_blockcount, &i);
1775 if (error)
1776 goto done;
c29aad41 1777 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
1778 error = xfs_btree_delete(bma->cur, &i);
1779 if (error)
1780 goto done;
c29aad41 1781 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
1782 error = xfs_btree_decrement(bma->cur, 0, &i);
1783 if (error)
1784 goto done;
c29aad41 1785 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
1786 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1787 LEFT.br_startblock,
1788 LEFT.br_blockcount +
1789 PREV.br_blockcount +
1790 RIGHT.br_blockcount, LEFT.br_state);
1791 if (error)
1792 goto done;
1793 }
1794 break;
1795
1796 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
a365bdd5 1797 /*
9e5987a7
DC
1798 * Filling in all of a previously delayed allocation extent.
1799 * The left neighbor is contiguous, the right is not.
a365bdd5 1800 */
9e5987a7
DC
1801 bma->idx--;
1802
1803 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1804 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1805 LEFT.br_blockcount + PREV.br_blockcount);
1806 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1807
1808 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1809 if (bma->cur == NULL)
1810 rval = XFS_ILOG_DEXT;
1811 else {
1812 rval = 0;
1813 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1814 LEFT.br_startblock, LEFT.br_blockcount,
1815 &i);
1816 if (error)
1817 goto done;
c29aad41 1818 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
1819 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1820 LEFT.br_startblock,
1821 LEFT.br_blockcount +
1822 PREV.br_blockcount, LEFT.br_state);
1823 if (error)
1824 goto done;
1825 }
1826 break;
1827
1828 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
0937e0fd 1829 /*
9e5987a7
DC
1830 * Filling in all of a previously delayed allocation extent.
1831 * The right neighbor is contiguous, the left is not.
0937e0fd 1832 */
9e5987a7
DC
1833 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1834 xfs_bmbt_set_startblock(ep, new->br_startblock);
1835 xfs_bmbt_set_blockcount(ep,
1836 PREV.br_blockcount + RIGHT.br_blockcount);
1837 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
0937e0fd 1838
9e5987a7
DC
1839 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1840 if (bma->cur == NULL)
1841 rval = XFS_ILOG_DEXT;
1842 else {
1843 rval = 0;
1844 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1845 RIGHT.br_startblock,
1846 RIGHT.br_blockcount, &i);
1847 if (error)
1848 goto done;
c29aad41 1849 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
1850 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
1851 new->br_startblock,
1852 PREV.br_blockcount +
1853 RIGHT.br_blockcount, PREV.br_state);
1854 if (error)
1855 goto done;
1856 }
1857 break;
1858
1859 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
a365bdd5 1860 /*
9e5987a7
DC
1861 * Filling in all of a previously delayed allocation extent.
1862 * Neither the left nor right neighbors are contiguous with
1863 * the new one.
a365bdd5 1864 */
9e5987a7
DC
1865 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1866 xfs_bmbt_set_startblock(ep, new->br_startblock);
1867 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
a365bdd5 1868
60b4984f 1869 (*nextents)++;
9e5987a7
DC
1870 if (bma->cur == NULL)
1871 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1872 else {
1873 rval = XFS_ILOG_CORE;
1874 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1875 new->br_startblock, new->br_blockcount,
1876 &i);
1877 if (error)
1878 goto done;
c29aad41 1879 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
9e5987a7
DC
1880 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1881 error = xfs_btree_insert(bma->cur, &i);
1882 if (error)
1883 goto done;
c29aad41 1884 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
1885 }
1886 break;
1da177e4 1887
9e5987a7 1888 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1da177e4 1889 /*
9e5987a7
DC
1890 * Filling in the first part of a previous delayed allocation.
1891 * The left neighbor is contiguous.
1da177e4 1892 */
9e5987a7
DC
1893 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1894 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
1895 LEFT.br_blockcount + new->br_blockcount);
1896 xfs_bmbt_set_startoff(ep,
1897 PREV.br_startoff + new->br_blockcount);
1898 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1da177e4 1899
9e5987a7
DC
1900 temp = PREV.br_blockcount - new->br_blockcount;
1901 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1902 xfs_bmbt_set_blockcount(ep, temp);
1903 if (bma->cur == NULL)
1904 rval = XFS_ILOG_DEXT;
1905 else {
1906 rval = 0;
1907 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
1908 LEFT.br_startblock, LEFT.br_blockcount,
1909 &i);
1910 if (error)
1911 goto done;
c29aad41 1912 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
1913 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
1914 LEFT.br_startblock,
1915 LEFT.br_blockcount +
1916 new->br_blockcount,
1917 LEFT.br_state);
f3ca8738 1918 if (error)
1da177e4 1919 goto done;
1da177e4 1920 }
9e5987a7
DC
1921 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1922 startblockval(PREV.br_startblock));
1923 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
1924 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1925
1926 bma->idx--;
1927 break;
1928
1929 case BMAP_LEFT_FILLING:
1da177e4 1930 /*
9e5987a7
DC
1931 * Filling in the first part of a previous delayed allocation.
1932 * The left neighbor is not contiguous.
1da177e4 1933 */
9e5987a7
DC
1934 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1935 xfs_bmbt_set_startoff(ep, new_endoff);
1936 temp = PREV.br_blockcount - new->br_blockcount;
1937 xfs_bmbt_set_blockcount(ep, temp);
1938 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
60b4984f 1939 (*nextents)++;
9e5987a7
DC
1940 if (bma->cur == NULL)
1941 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1da177e4 1942 else {
9e5987a7
DC
1943 rval = XFS_ILOG_CORE;
1944 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
1945 new->br_startblock, new->br_blockcount,
1946 &i);
1947 if (error)
1948 goto done;
c29aad41 1949 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
9e5987a7
DC
1950 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1951 error = xfs_btree_insert(bma->cur, &i);
1952 if (error)
1da177e4 1953 goto done;
c29aad41 1954 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1da177e4 1955 }
233eebb9 1956
6d3eb1ec 1957 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
9e5987a7 1958 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2c3234d1 1959 bma->firstblock, bma->dfops,
6d3eb1ec 1960 &bma->cur, 1, &tmp_rval, whichfork);
9e5987a7
DC
1961 rval |= tmp_rval;
1962 if (error)
1963 goto done;
1da177e4 1964 }
9e5987a7
DC
1965 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1966 startblockval(PREV.br_startblock) -
1967 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1968 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
1969 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
1970 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1da177e4
LT
1971 break;
1972
9e5987a7 1973 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4 1974 /*
9e5987a7
DC
1975 * Filling in the last part of a previous delayed allocation.
1976 * The right neighbor is contiguous with the new allocation.
1da177e4 1977 */
9e5987a7
DC
1978 temp = PREV.br_blockcount - new->br_blockcount;
1979 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1da177e4 1980 xfs_bmbt_set_blockcount(ep, temp);
9e5987a7
DC
1981 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
1982 new->br_startoff, new->br_startblock,
1983 new->br_blockcount + RIGHT.br_blockcount,
1984 RIGHT.br_state);
1985 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1986 if (bma->cur == NULL)
1987 rval = XFS_ILOG_DEXT;
1988 else {
1989 rval = 0;
1990 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
1991 RIGHT.br_startblock,
1992 RIGHT.br_blockcount, &i);
1993 if (error)
1994 goto done;
c29aad41 1995 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
1996 error = xfs_bmbt_update(bma->cur, new->br_startoff,
1997 new->br_startblock,
1998 new->br_blockcount +
1999 RIGHT.br_blockcount,
2000 RIGHT.br_state);
2001 if (error)
2002 goto done;
1da177e4 2003 }
9e5987a7
DC
2004
2005 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2006 startblockval(PREV.br_startblock));
2007 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2008 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2009 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2010
2011 bma->idx++;
1da177e4
LT
2012 break;
2013
9e5987a7 2014 case BMAP_RIGHT_FILLING:
1da177e4 2015 /*
9e5987a7
DC
2016 * Filling in the last part of a previous delayed allocation.
2017 * The right neighbor is not contiguous.
1da177e4 2018 */
9e5987a7
DC
2019 temp = PREV.br_blockcount - new->br_blockcount;
2020 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1da177e4 2021 xfs_bmbt_set_blockcount(ep, temp);
9e5987a7 2022 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
60b4984f 2023 (*nextents)++;
9e5987a7
DC
2024 if (bma->cur == NULL)
2025 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2026 else {
2027 rval = XFS_ILOG_CORE;
2028 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2029 new->br_startblock, new->br_blockcount,
2030 &i);
2031 if (error)
2032 goto done;
c29aad41 2033 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
9e5987a7
DC
2034 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2035 error = xfs_btree_insert(bma->cur, &i);
2036 if (error)
2037 goto done;
c29aad41 2038 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1da177e4 2039 }
9e5987a7 2040
6d3eb1ec 2041 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
9e5987a7 2042 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2c3234d1 2043 bma->firstblock, bma->dfops, &bma->cur, 1,
6d3eb1ec 2044 &tmp_rval, whichfork);
9e5987a7
DC
2045 rval |= tmp_rval;
2046 if (error)
2047 goto done;
1da177e4 2048 }
9e5987a7
DC
2049 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
2050 startblockval(PREV.br_startblock) -
2051 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2052 ep = xfs_iext_get_ext(ifp, bma->idx);
2053 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
2054 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2055
2056 bma->idx++;
1da177e4
LT
2057 break;
2058
2059 case 0:
2060 /*
9e5987a7
DC
2061 * Filling in the middle part of a previous delayed allocation.
2062 * Contiguity is impossible here.
2063 * This case is avoided almost all the time.
2064 *
2065 * We start with a delayed allocation:
2066 *
2067 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2068 * PREV @ idx
2069 *
2070 * and we are allocating:
2071 * +rrrrrrrrrrrrrrrrr+
2072 * new
2073 *
2074 * and we set it up for insertion as:
2075 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2076 * new
2077 * PREV @ idx LEFT RIGHT
2078 * inserted at idx + 1
1da177e4 2079 */
9e5987a7
DC
2080 temp = new->br_startoff - PREV.br_startoff;
2081 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
2082 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
2083 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2084 LEFT = *new;
2085 RIGHT.br_state = PREV.br_state;
2086 RIGHT.br_startblock = nullstartblock(
2087 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
2088 RIGHT.br_startoff = new_endoff;
2089 RIGHT.br_blockcount = temp2;
2090 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
2091 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
60b4984f 2092 (*nextents)++;
9e5987a7
DC
2093 if (bma->cur == NULL)
2094 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2095 else {
2096 rval = XFS_ILOG_CORE;
2097 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
2098 new->br_startblock, new->br_blockcount,
2099 &i);
2100 if (error)
2101 goto done;
c29aad41 2102 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
9e5987a7
DC
2103 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2104 error = xfs_btree_insert(bma->cur, &i);
2105 if (error)
2106 goto done;
c29aad41 2107 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1da177e4 2108 }
1da177e4 2109
6d3eb1ec 2110 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
9e5987a7 2111 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2c3234d1 2112 bma->firstblock, bma->dfops, &bma->cur,
6d3eb1ec 2113 1, &tmp_rval, whichfork);
9e5987a7
DC
2114 rval |= tmp_rval;
2115 if (error)
2116 goto done;
2117 }
2118 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2119 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
2120 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
2121 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2122 if (diff > 0) {
0d485ada
DC
2123 error = xfs_mod_fdblocks(bma->ip->i_mount,
2124 -((int64_t)diff), false);
9e5987a7
DC
2125 ASSERT(!error);
2126 if (error)
2127 goto done;
2128 }
2129
2130 ep = xfs_iext_get_ext(ifp, bma->idx);
2131 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2132 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2133 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2134 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
2135 nullstartblock((int)temp2));
2136 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2137
2138 bma->idx++;
2139 da_new = temp + temp2;
2140 break;
2141
2142 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2143 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2144 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2145 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2146 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2147 case BMAP_LEFT_CONTIG:
2148 case BMAP_RIGHT_CONTIG:
2149 /*
2150 * These cases are all impossible.
2151 */
2152 ASSERT(0);
2153 }
2154
9c194644
DW
2155 /* add reverse mapping */
2156 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2157 if (error)
2158 goto done;
2159
9e5987a7 2160 /* convert to a btree if necessary */
6d3eb1ec 2161 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
9e5987a7
DC
2162 int tmp_logflags; /* partial log flag return val */
2163
2164 ASSERT(bma->cur == NULL);
2165 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2c3234d1 2166 bma->firstblock, bma->dfops, &bma->cur,
6d3eb1ec 2167 da_old > 0, &tmp_logflags, whichfork);
9e5987a7
DC
2168 bma->logflags |= tmp_logflags;
2169 if (error)
2170 goto done;
2171 }
2172
2173 /* adjust for changes in reserved delayed indirect blocks */
2174 if (da_old || da_new) {
2175 temp = da_new;
2176 if (bma->cur)
2177 temp += bma->cur->bc_private.b.allocated;
2178 ASSERT(temp <= da_old);
2179 if (temp < da_old)
0d485ada
DC
2180 xfs_mod_fdblocks(bma->ip->i_mount,
2181 (int64_t)(da_old - temp), false);
96540c78 2182 }
9e5987a7
DC
2183
2184 /* clear out the allocated field, done with it now in any case. */
2185 if (bma->cur)
2186 bma->cur->bc_private.b.allocated = 0;
2187
6d3eb1ec 2188 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
1da177e4 2189done:
60b4984f
DW
2190 if (whichfork != XFS_COW_FORK)
2191 bma->logflags |= rval;
1da177e4 2192 return error;
9e5987a7
DC
2193#undef LEFT
2194#undef RIGHT
2195#undef PREV
1da177e4
LT
2196}
2197
2198/*
9e5987a7 2199 * Convert an unwritten allocation to a real allocation or vice versa.
1da177e4 2200 */
9e5987a7
DC
2201STATIC int /* error */
2202xfs_bmap_add_extent_unwritten_real(
2203 struct xfs_trans *tp,
2204 xfs_inode_t *ip, /* incore inode pointer */
2205 xfs_extnum_t *idx, /* extent number to update/insert */
2206 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2207 xfs_bmbt_irec_t *new, /* new data to add to file extents */
2208 xfs_fsblock_t *first, /* pointer to firstblock variable */
2c3234d1 2209 struct xfs_defer_ops *dfops, /* list of extents to be freed */
9e5987a7 2210 int *logflagsp) /* inode logging flags */
1da177e4 2211{
9e5987a7
DC
2212 xfs_btree_cur_t *cur; /* btree cursor */
2213 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
2214 int error; /* error return value */
2215 int i; /* temp state */
2216 xfs_ifork_t *ifp; /* inode fork pointer */
2217 xfs_fileoff_t new_endoff; /* end offset of new entry */
2218 xfs_exntst_t newext; /* new extent state */
2219 xfs_exntst_t oldext; /* old extent state */
2220 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2221 /* left is 0, right is 1, prev is 2 */
2222 int rval=0; /* return value (logging flags) */
2223 int state = 0;/* state bits, accessed thru macros */
c29aad41 2224 struct xfs_mount *mp = tp->t_mountp;
1da177e4 2225
9e5987a7 2226 *logflagsp = 0;
1da177e4 2227
9e5987a7
DC
2228 cur = *curp;
2229 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
8096b1eb 2230
9e5987a7 2231 ASSERT(*idx >= 0);
5d829300 2232 ASSERT(*idx <= xfs_iext_count(ifp));
9e5987a7
DC
2233 ASSERT(!isnullstartblock(new->br_startblock));
2234
ff6d6af2 2235 XFS_STATS_INC(mp, xs_add_exlist);
9e5987a7
DC
2236
2237#define LEFT r[0]
2238#define RIGHT r[1]
2239#define PREV r[2]
7cc95a82 2240
1da177e4 2241 /*
9e5987a7 2242 * Set up a bunch of variables to make the tests simpler.
1da177e4 2243 */
9e5987a7
DC
2244 error = 0;
2245 ep = xfs_iext_get_ext(ifp, *idx);
2246 xfs_bmbt_get_all(ep, &PREV);
2247 newext = new->br_state;
2248 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2249 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2250 ASSERT(PREV.br_state == oldext);
2251 new_endoff = new->br_startoff + new->br_blockcount;
2252 ASSERT(PREV.br_startoff <= new->br_startoff);
2253 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
7cc95a82 2254
1da177e4 2255 /*
9e5987a7
DC
2256 * Set flags determining what part of the previous oldext allocation
2257 * extent is being replaced by a newext allocation.
1da177e4 2258 */
9e5987a7
DC
2259 if (PREV.br_startoff == new->br_startoff)
2260 state |= BMAP_LEFT_FILLING;
2261 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2262 state |= BMAP_RIGHT_FILLING;
2263
1da177e4 2264 /*
9e5987a7
DC
2265 * Check and set flags if this segment has a left neighbor.
2266 * Don't set contiguous if the combined extent would be too large.
1da177e4 2267 */
9e5987a7
DC
2268 if (*idx > 0) {
2269 state |= BMAP_LEFT_VALID;
2270 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
2271
2272 if (isnullstartblock(LEFT.br_startblock))
2273 state |= BMAP_LEFT_DELAY;
1da177e4 2274 }
9e5987a7
DC
2275
2276 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2277 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2278 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2279 LEFT.br_state == newext &&
2280 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2281 state |= BMAP_LEFT_CONTIG;
2282
1da177e4 2283 /*
9e5987a7
DC
2284 * Check and set flags if this segment has a right neighbor.
2285 * Don't set contiguous if the combined extent would be too large.
2286 * Also check for all-three-contiguous being too large.
1da177e4 2287 */
5d829300 2288 if (*idx < xfs_iext_count(&ip->i_df) - 1) {
9e5987a7
DC
2289 state |= BMAP_RIGHT_VALID;
2290 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
2291 if (isnullstartblock(RIGHT.br_startblock))
2292 state |= BMAP_RIGHT_DELAY;
1da177e4 2293 }
7cc95a82 2294
9e5987a7
DC
2295 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2296 new_endoff == RIGHT.br_startoff &&
2297 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2298 newext == RIGHT.br_state &&
2299 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2300 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2301 BMAP_RIGHT_FILLING)) !=
2302 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2303 BMAP_RIGHT_FILLING) ||
2304 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2305 <= MAXEXTLEN))
2306 state |= BMAP_RIGHT_CONTIG;
136341b4 2307
1da177e4 2308 /*
9e5987a7 2309 * Switch out based on the FILLING and CONTIG state bits.
1da177e4 2310 */
9e5987a7
DC
2311 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2312 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2313 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2314 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2315 /*
2316 * Setting all of a previous oldext extent to newext.
2317 * The left and right neighbors are both contiguous with new.
2318 */
2319 --*idx;
1a5902c5 2320
9e5987a7
DC
2321 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2322 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2323 LEFT.br_blockcount + PREV.br_blockcount +
2324 RIGHT.br_blockcount);
2325 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1a5902c5 2326
9e5987a7
DC
2327 xfs_iext_remove(ip, *idx + 1, 2, state);
2328 ip->i_d.di_nextents -= 2;
2329 if (cur == NULL)
2330 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2331 else {
2332 rval = XFS_ILOG_CORE;
2333 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2334 RIGHT.br_startblock,
2335 RIGHT.br_blockcount, &i)))
2336 goto done;
c29aad41 2337 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2338 if ((error = xfs_btree_delete(cur, &i)))
2339 goto done;
c29aad41 2340 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2341 if ((error = xfs_btree_decrement(cur, 0, &i)))
2342 goto done;
c29aad41 2343 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2344 if ((error = xfs_btree_delete(cur, &i)))
2345 goto done;
c29aad41 2346 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2347 if ((error = xfs_btree_decrement(cur, 0, &i)))
2348 goto done;
c29aad41 2349 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2350 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2351 LEFT.br_startblock,
2352 LEFT.br_blockcount + PREV.br_blockcount +
2353 RIGHT.br_blockcount, LEFT.br_state)))
2354 goto done;
2355 }
2356 break;
1a5902c5 2357
9e5987a7
DC
2358 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2359 /*
2360 * Setting all of a previous oldext extent to newext.
2361 * The left neighbor is contiguous, the right is not.
2362 */
2363 --*idx;
d8cc890d 2364
9e5987a7
DC
2365 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2366 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2367 LEFT.br_blockcount + PREV.br_blockcount);
2368 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1da177e4 2369
9e5987a7
DC
2370 xfs_iext_remove(ip, *idx + 1, 1, state);
2371 ip->i_d.di_nextents--;
2372 if (cur == NULL)
2373 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2374 else {
2375 rval = XFS_ILOG_CORE;
2376 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2377 PREV.br_startblock, PREV.br_blockcount,
2378 &i)))
2379 goto done;
c29aad41 2380 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2381 if ((error = xfs_btree_delete(cur, &i)))
2382 goto done;
c29aad41 2383 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2384 if ((error = xfs_btree_decrement(cur, 0, &i)))
2385 goto done;
c29aad41 2386 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2387 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2388 LEFT.br_startblock,
2389 LEFT.br_blockcount + PREV.br_blockcount,
2390 LEFT.br_state)))
2391 goto done;
2392 }
2393 break;
1da177e4 2394
9e5987a7 2395 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1da177e4 2396 /*
9e5987a7
DC
2397 * Setting all of a previous oldext extent to newext.
2398 * The right neighbor is contiguous, the left is not.
1da177e4 2399 */
9e5987a7
DC
2400 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2401 xfs_bmbt_set_blockcount(ep,
2402 PREV.br_blockcount + RIGHT.br_blockcount);
2403 xfs_bmbt_set_state(ep, newext);
2404 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2405 xfs_iext_remove(ip, *idx + 1, 1, state);
2406 ip->i_d.di_nextents--;
2407 if (cur == NULL)
2408 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2409 else {
2410 rval = XFS_ILOG_CORE;
2411 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2412 RIGHT.br_startblock,
2413 RIGHT.br_blockcount, &i)))
2414 goto done;
c29aad41 2415 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2416 if ((error = xfs_btree_delete(cur, &i)))
2417 goto done;
c29aad41 2418 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2419 if ((error = xfs_btree_decrement(cur, 0, &i)))
2420 goto done;
c29aad41 2421 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2422 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2423 new->br_startblock,
2424 new->br_blockcount + RIGHT.br_blockcount,
2425 newext)))
2426 goto done;
1da177e4 2427 }
9e5987a7 2428 break;
1e82379b 2429
9e5987a7
DC
2430 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
2431 /*
2432 * Setting all of a previous oldext extent to newext.
2433 * Neither the left nor right neighbors are contiguous with
2434 * the new one.
2435 */
2436 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2437 xfs_bmbt_set_state(ep, newext);
2438 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1e82379b 2439
9e5987a7
DC
2440 if (cur == NULL)
2441 rval = XFS_ILOG_DEXT;
2442 else {
2443 rval = 0;
2444 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2445 new->br_startblock, new->br_blockcount,
2446 &i)))
2447 goto done;
c29aad41 2448 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2449 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2450 new->br_startblock, new->br_blockcount,
2451 newext)))
2452 goto done;
2453 }
2454 break;
1e82379b 2455
9e5987a7
DC
2456 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
2457 /*
2458 * Setting the first part of a previous oldext extent to newext.
2459 * The left neighbor is contiguous.
2460 */
2461 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2462 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
2463 LEFT.br_blockcount + new->br_blockcount);
2464 xfs_bmbt_set_startoff(ep,
2465 PREV.br_startoff + new->br_blockcount);
2466 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
1da177e4 2467
9e5987a7
DC
2468 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2469 xfs_bmbt_set_startblock(ep,
2470 new->br_startblock + new->br_blockcount);
2471 xfs_bmbt_set_blockcount(ep,
2472 PREV.br_blockcount - new->br_blockcount);
2473 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
0293ce3a 2474
9e5987a7 2475 --*idx;
8867bc9b 2476
9e5987a7
DC
2477 if (cur == NULL)
2478 rval = XFS_ILOG_DEXT;
2479 else {
2480 rval = 0;
2481 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2482 PREV.br_startblock, PREV.br_blockcount,
2483 &i)))
2484 goto done;
c29aad41 2485 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2486 if ((error = xfs_bmbt_update(cur,
2487 PREV.br_startoff + new->br_blockcount,
2488 PREV.br_startblock + new->br_blockcount,
2489 PREV.br_blockcount - new->br_blockcount,
2490 oldext)))
2491 goto done;
2492 if ((error = xfs_btree_decrement(cur, 0, &i)))
2493 goto done;
2494 error = xfs_bmbt_update(cur, LEFT.br_startoff,
2495 LEFT.br_startblock,
2496 LEFT.br_blockcount + new->br_blockcount,
2497 LEFT.br_state);
2498 if (error)
2499 goto done;
8867bc9b 2500 }
9e5987a7 2501 break;
0293ce3a 2502
9e5987a7
DC
2503 case BMAP_LEFT_FILLING:
2504 /*
2505 * Setting the first part of a previous oldext extent to newext.
2506 * The left neighbor is not contiguous.
2507 */
2508 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2509 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2510 xfs_bmbt_set_startoff(ep, new_endoff);
2511 xfs_bmbt_set_blockcount(ep,
2512 PREV.br_blockcount - new->br_blockcount);
2513 xfs_bmbt_set_startblock(ep,
2514 new->br_startblock + new->br_blockcount);
2515 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1da177e4 2516
9e5987a7
DC
2517 xfs_iext_insert(ip, *idx, 1, new, state);
2518 ip->i_d.di_nextents++;
2519 if (cur == NULL)
2520 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2521 else {
2522 rval = XFS_ILOG_CORE;
2523 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2524 PREV.br_startblock, PREV.br_blockcount,
2525 &i)))
2526 goto done;
c29aad41 2527 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2528 if ((error = xfs_bmbt_update(cur,
2529 PREV.br_startoff + new->br_blockcount,
2530 PREV.br_startblock + new->br_blockcount,
2531 PREV.br_blockcount - new->br_blockcount,
2532 oldext)))
2533 goto done;
2534 cur->bc_rec.b = *new;
2535 if ((error = xfs_btree_insert(cur, &i)))
2536 goto done;
c29aad41 2537 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2538 }
2539 break;
1da177e4 2540
9e5987a7
DC
2541 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
2542 /*
2543 * Setting the last part of a previous oldext extent to newext.
2544 * The right neighbor is contiguous with the new allocation.
2545 */
2546 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2547 xfs_bmbt_set_blockcount(ep,
2548 PREV.br_blockcount - new->br_blockcount);
2549 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
0293ce3a 2550
9e5987a7 2551 ++*idx;
1da177e4 2552
9e5987a7
DC
2553 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2554 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2555 new->br_startoff, new->br_startblock,
2556 new->br_blockcount + RIGHT.br_blockcount, newext);
2557 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1da177e4 2558
9e5987a7
DC
2559 if (cur == NULL)
2560 rval = XFS_ILOG_DEXT;
2561 else {
2562 rval = 0;
2563 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2564 PREV.br_startblock,
2565 PREV.br_blockcount, &i)))
2566 goto done;
c29aad41 2567 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2568 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2569 PREV.br_startblock,
2570 PREV.br_blockcount - new->br_blockcount,
2571 oldext)))
2572 goto done;
2573 if ((error = xfs_btree_increment(cur, 0, &i)))
2574 goto done;
2575 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2576 new->br_startblock,
2577 new->br_blockcount + RIGHT.br_blockcount,
2578 newext)))
2579 goto done;
2580 }
2581 break;
1da177e4 2582
9e5987a7
DC
2583 case BMAP_RIGHT_FILLING:
2584 /*
2585 * Setting the last part of a previous oldext extent to newext.
2586 * The right neighbor is not contiguous.
2587 */
2588 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2589 xfs_bmbt_set_blockcount(ep,
2590 PREV.br_blockcount - new->br_blockcount);
2591 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1da177e4 2592
9e5987a7
DC
2593 ++*idx;
2594 xfs_iext_insert(ip, *idx, 1, new, state);
d8cc890d 2595
9e5987a7
DC
2596 ip->i_d.di_nextents++;
2597 if (cur == NULL)
2598 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2599 else {
2600 rval = XFS_ILOG_CORE;
2601 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2602 PREV.br_startblock, PREV.br_blockcount,
2603 &i)))
2604 goto done;
c29aad41 2605 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2606 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2607 PREV.br_startblock,
2608 PREV.br_blockcount - new->br_blockcount,
2609 oldext)))
2610 goto done;
2611 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2612 new->br_startblock, new->br_blockcount,
2613 &i)))
2614 goto done;
c29aad41 2615 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
9e5987a7
DC
2616 cur->bc_rec.b.br_state = XFS_EXT_NORM;
2617 if ((error = xfs_btree_insert(cur, &i)))
2618 goto done;
c29aad41 2619 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2620 }
2621 break;
2622
2623 case 0:
1da177e4 2624 /*
9e5987a7
DC
2625 * Setting the middle part of a previous oldext extent to
2626 * newext. Contiguity is impossible here.
2627 * One extent becomes three extents.
1da177e4 2628 */
9e5987a7
DC
2629 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2630 xfs_bmbt_set_blockcount(ep,
2631 new->br_startoff - PREV.br_startoff);
2632 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
898621d5 2633
9e5987a7
DC
2634 r[0] = *new;
2635 r[1].br_startoff = new_endoff;
2636 r[1].br_blockcount =
2637 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2638 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2639 r[1].br_state = oldext;
898621d5 2640
9e5987a7
DC
2641 ++*idx;
2642 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2643
2644 ip->i_d.di_nextents += 2;
2645 if (cur == NULL)
2646 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2647 else {
2648 rval = XFS_ILOG_CORE;
2649 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2650 PREV.br_startblock, PREV.br_blockcount,
2651 &i)))
2652 goto done;
c29aad41 2653 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2654 /* new right extent - oldext */
2655 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2656 r[1].br_startblock, r[1].br_blockcount,
2657 r[1].br_state)))
2658 goto done;
2659 /* new left extent - oldext */
2660 cur->bc_rec.b = PREV;
2661 cur->bc_rec.b.br_blockcount =
2662 new->br_startoff - PREV.br_startoff;
2663 if ((error = xfs_btree_insert(cur, &i)))
2664 goto done;
c29aad41 2665 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2666 /*
2667 * Reset the cursor to the position of the new extent
2668 * we are about to insert as we can't trust it after
2669 * the previous insert.
2670 */
2671 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2672 new->br_startblock, new->br_blockcount,
2673 &i)))
2674 goto done;
c29aad41 2675 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
9e5987a7
DC
2676 /* new middle extent - newext */
2677 cur->bc_rec.b.br_state = new->br_state;
2678 if ((error = xfs_btree_insert(cur, &i)))
2679 goto done;
c29aad41 2680 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7 2681 }
1da177e4 2682 break;
9e5987a7
DC
2683
2684 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2685 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2686 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2687 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2688 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2689 case BMAP_LEFT_CONTIG:
2690 case BMAP_RIGHT_CONTIG:
2691 /*
2692 * These cases are all impossible.
2693 */
1da177e4 2694 ASSERT(0);
1da177e4 2695 }
8096b1eb 2696
9c194644
DW
2697 /* update reverse mappings */
2698 error = xfs_rmap_convert_extent(mp, dfops, ip, XFS_DATA_FORK, new);
2699 if (error)
2700 goto done;
2701
9e5987a7
DC
2702 /* convert to a btree if necessary */
2703 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
2704 int tmp_logflags; /* partial log flag return val */
2705
2706 ASSERT(cur == NULL);
2c3234d1 2707 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
9e5987a7
DC
2708 0, &tmp_logflags, XFS_DATA_FORK);
2709 *logflagsp |= tmp_logflags;
2710 if (error)
2711 goto done;
1da177e4 2712 }
da087bad 2713
9e5987a7
DC
2714 /* clear out the allocated field, done with it now in any case. */
2715 if (cur) {
2716 cur->bc_private.b.allocated = 0;
2717 *curp = cur;
1da177e4 2718 }
8096b1eb 2719
9e5987a7
DC
2720 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
2721done:
2722 *logflagsp |= rval;
1da177e4 2723 return error;
9e5987a7
DC
2724#undef LEFT
2725#undef RIGHT
2726#undef PREV
1da177e4
LT
2727}
2728
2729/*
9e5987a7 2730 * Convert a hole to a delayed allocation.
1da177e4 2731 */
9e5987a7
DC
2732STATIC void
2733xfs_bmap_add_extent_hole_delay(
2734 xfs_inode_t *ip, /* incore inode pointer */
be51f811 2735 int whichfork,
9e5987a7
DC
2736 xfs_extnum_t *idx, /* extent number to update/insert */
2737 xfs_bmbt_irec_t *new) /* new data to add to file extents */
1da177e4 2738{
9e5987a7
DC
2739 xfs_ifork_t *ifp; /* inode fork pointer */
2740 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2741 xfs_filblks_t newlen=0; /* new indirect size */
2742 xfs_filblks_t oldlen=0; /* old indirect size */
2743 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2744 int state; /* state bits, accessed thru macros */
2745 xfs_filblks_t temp=0; /* temp for indirect calculations */
1da177e4 2746
be51f811 2747 ifp = XFS_IFORK_PTR(ip, whichfork);
9e5987a7 2748 state = 0;
be51f811
DW
2749 if (whichfork == XFS_COW_FORK)
2750 state |= BMAP_COWFORK;
9e5987a7 2751 ASSERT(isnullstartblock(new->br_startblock));
1da177e4
LT
2752
2753 /*
9e5987a7 2754 * Check and set flags if this segment has a left neighbor
1da177e4 2755 */
9e5987a7
DC
2756 if (*idx > 0) {
2757 state |= BMAP_LEFT_VALID;
2758 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
2759
2760 if (isnullstartblock(left.br_startblock))
2761 state |= BMAP_LEFT_DELAY;
1da177e4 2762 }
1da177e4 2763
9e5987a7
DC
2764 /*
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 */
5d829300 2768 if (*idx < xfs_iext_count(ifp)) {
9e5987a7
DC
2769 state |= BMAP_RIGHT_VALID;
2770 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
1da177e4 2771
9e5987a7
DC
2772 if (isnullstartblock(right.br_startblock))
2773 state |= BMAP_RIGHT_DELAY;
1da177e4 2774 }
9e5987a7 2775
1da177e4 2776 /*
9e5987a7
DC
2777 * Set contiguity flags on the left and right neighbors.
2778 * Don't let extents get too large, even if the pieces are contiguous.
1da177e4 2779 */
9e5987a7
DC
2780 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;
cc09c0dc
DC
2792
2793 /*
9e5987a7 2794 * Switch out based on the contiguity flags.
cc09c0dc 2795 */
9e5987a7
DC
2796 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2797 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2798 /*
2799 * New allocation is contiguous with delayed allocations
2800 * on the left and on the right.
2801 * Merge all three into a single extent record.
2802 */
2803 --*idx;
2804 temp = left.br_blockcount + new->br_blockcount +
2805 right.br_blockcount;
cc09c0dc 2806
9e5987a7
DC
2807 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2808 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2809 oldlen = startblockval(left.br_startblock) +
2810 startblockval(new->br_startblock) +
2811 startblockval(right.br_startblock);
2812 newlen = xfs_bmap_worst_indlen(ip, temp);
2813 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2814 nullstartblock((int)newlen));
2815 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1da177e4 2816
9e5987a7
DC
2817 xfs_iext_remove(ip, *idx + 1, 1, state);
2818 break;
1da177e4 2819
9e5987a7
DC
2820 case BMAP_LEFT_CONTIG:
2821 /*
2822 * New allocation is contiguous with a delayed allocation
2823 * on the left.
2824 * Merge the new allocation with the left neighbor.
2825 */
2826 --*idx;
2827 temp = left.br_blockcount + new->br_blockcount;
1da177e4 2828
9e5987a7
DC
2829 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2830 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
2831 oldlen = startblockval(left.br_startblock) +
2832 startblockval(new->br_startblock);
2833 newlen = xfs_bmap_worst_indlen(ip, temp);
2834 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
2835 nullstartblock((int)newlen));
2836 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2837 break;
1da177e4 2838
9e5987a7
DC
2839 case BMAP_RIGHT_CONTIG:
2840 /*
2841 * New allocation is contiguous with a delayed allocation
2842 * on the right.
2843 * Merge the new allocation with the right neighbor.
2844 */
2845 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2846 temp = new->br_blockcount + right.br_blockcount;
2847 oldlen = startblockval(new->br_startblock) +
2848 startblockval(right.br_startblock);
2849 newlen = xfs_bmap_worst_indlen(ip, temp);
2850 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2851 new->br_startoff,
2852 nullstartblock((int)newlen), temp, right.br_state);
2853 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2854 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;
2863 xfs_iext_insert(ip, *idx, 1, new, state);
2864 break;
1da177e4 2865 }
9e5987a7
DC
2866 if (oldlen != newlen) {
2867 ASSERT(oldlen > newlen);
0d485ada
DC
2868 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2869 false);
1da177e4 2870 /*
9e5987a7 2871 * Nothing to do for disk quota accounting here.
1da177e4 2872 */
1da177e4 2873 }
1da177e4
LT
2874}
2875
2876/*
9e5987a7 2877 * Convert a hole to a real allocation.
1da177e4 2878 */
9e5987a7
DC
2879STATIC int /* error */
2880xfs_bmap_add_extent_hole_real(
2881 struct xfs_bmalloca *bma,
2882 int whichfork)
27a3f8f2 2883{
9e5987a7
DC
2884 struct xfs_bmbt_irec *new = &bma->got;
2885 int error; /* error return value */
2886 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 */
2890 int rval=0; /* return value (logging flags) */
2891 int state; /* state bits, accessed thru macros */
c29aad41 2892 struct xfs_mount *mp;
27a3f8f2 2893
f1f96c49 2894 mp = bma->ip->i_mount;
9e5987a7 2895 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
27a3f8f2 2896
9e5987a7 2897 ASSERT(bma->idx >= 0);
5d829300 2898 ASSERT(bma->idx <= xfs_iext_count(ifp));
9e5987a7
DC
2899 ASSERT(!isnullstartblock(new->br_startblock));
2900 ASSERT(!bma->cur ||
2901 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
3993baeb 2902 ASSERT(whichfork != XFS_COW_FORK);
27a3f8f2 2903
ff6d6af2 2904 XFS_STATS_INC(mp, xs_add_exlist);
27a3f8f2 2905
9e5987a7
DC
2906 state = 0;
2907 if (whichfork == XFS_ATTR_FORK)
2908 state |= BMAP_ATTRFORK;
27a3f8f2 2909
9e5987a7
DC
2910 /*
2911 * Check and set flags if this segment has a left neighbor.
2912 */
2913 if (bma->idx > 0) {
2914 state |= BMAP_LEFT_VALID;
2915 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
2916 if (isnullstartblock(left.br_startblock))
2917 state |= BMAP_LEFT_DELAY;
2918 }
27a3f8f2
CH
2919
2920 /*
9e5987a7
DC
2921 * Check and set flags if this segment has a current value.
2922 * Not true if we're inserting into the "hole" at eof.
27a3f8f2 2923 */
5d829300 2924 if (bma->idx < xfs_iext_count(ifp)) {
9e5987a7
DC
2925 state |= BMAP_RIGHT_VALID;
2926 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
2927 if (isnullstartblock(right.br_startblock))
2928 state |= BMAP_RIGHT_DELAY;
2929 }
27a3f8f2 2930
9e5987a7
DC
2931 /*
2932 * We're inserting a real allocation between "left" and "right".
2933 * Set the contiguity flags. Don't let extents get too large.
2934 */
2935 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2936 left.br_startoff + left.br_blockcount == new->br_startoff &&
2937 left.br_startblock + left.br_blockcount == new->br_startblock &&
2938 left.br_state == new->br_state &&
2939 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2940 state |= BMAP_LEFT_CONTIG;
27a3f8f2 2941
9e5987a7
DC
2942 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2943 new->br_startoff + new->br_blockcount == right.br_startoff &&
2944 new->br_startblock + new->br_blockcount == right.br_startblock &&
2945 new->br_state == right.br_state &&
2946 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2947 (!(state & BMAP_LEFT_CONTIG) ||
2948 left.br_blockcount + new->br_blockcount +
2949 right.br_blockcount <= MAXEXTLEN))
2950 state |= BMAP_RIGHT_CONTIG;
27a3f8f2 2951
9e5987a7
DC
2952 error = 0;
2953 /*
2954 * Select which case we're in here, and implement it.
2955 */
2956 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2957 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2958 /*
2959 * New allocation is contiguous with real allocations on the
2960 * left and on the right.
2961 * Merge all three into a single extent record.
2962 */
2963 --bma->idx;
2964 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2965 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
2966 left.br_blockcount + new->br_blockcount +
2967 right.br_blockcount);
2968 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
27a3f8f2 2969
9e5987a7 2970 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
27a3f8f2 2971
9e5987a7
DC
2972 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
2973 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
2974 if (bma->cur == NULL) {
2975 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2976 } else {
2977 rval = XFS_ILOG_CORE;
2978 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
2979 right.br_startblock, right.br_blockcount,
2980 &i);
2981 if (error)
2982 goto done;
c29aad41 2983 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2984 error = xfs_btree_delete(bma->cur, &i);
2985 if (error)
2986 goto done;
c29aad41 2987 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2988 error = xfs_btree_decrement(bma->cur, 0, &i);
2989 if (error)
2990 goto done;
c29aad41 2991 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
2992 error = xfs_bmbt_update(bma->cur, left.br_startoff,
2993 left.br_startblock,
2994 left.br_blockcount +
2995 new->br_blockcount +
2996 right.br_blockcount,
2997 left.br_state);
2998 if (error)
2999 goto done;
3000 }
3001 break;
27a3f8f2 3002
9e5987a7
DC
3003 case BMAP_LEFT_CONTIG:
3004 /*
3005 * New allocation is contiguous with a real allocation
3006 * on the left.
3007 * Merge the new allocation with the left neighbor.
3008 */
3009 --bma->idx;
3010 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3011 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
3012 left.br_blockcount + new->br_blockcount);
3013 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1da177e4 3014
9e5987a7
DC
3015 if (bma->cur == NULL) {
3016 rval = xfs_ilog_fext(whichfork);
3017 } else {
3018 rval = 0;
3019 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3020 left.br_startblock, left.br_blockcount,
3021 &i);
3022 if (error)
3023 goto done;
c29aad41 3024 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
3025 error = xfs_bmbt_update(bma->cur, left.br_startoff,
3026 left.br_startblock,
3027 left.br_blockcount +
3028 new->br_blockcount,
3029 left.br_state);
3030 if (error)
3031 goto done;
3032 }
3033 break;
27a3f8f2 3034
9e5987a7
DC
3035 case BMAP_RIGHT_CONTIG:
3036 /*
3037 * New allocation is contiguous with a real allocation
3038 * on the right.
3039 * Merge the new allocation with the right neighbor.
3040 */
3041 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3042 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
3043 new->br_startoff, new->br_startblock,
3044 new->br_blockcount + right.br_blockcount,
3045 right.br_state);
3046 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
27a3f8f2 3047
9e5987a7
DC
3048 if (bma->cur == NULL) {
3049 rval = xfs_ilog_fext(whichfork);
3050 } else {
3051 rval = 0;
3052 error = xfs_bmbt_lookup_eq(bma->cur,
3053 right.br_startoff,
3054 right.br_startblock,
3055 right.br_blockcount, &i);
3056 if (error)
3057 goto done;
c29aad41 3058 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
3059 error = xfs_bmbt_update(bma->cur, new->br_startoff,
3060 new->br_startblock,
3061 new->br_blockcount +
3062 right.br_blockcount,
3063 right.br_state);
3064 if (error)
3065 goto done;
3066 }
3067 break;
3068
3069 case 0:
3070 /*
3071 * New allocation is not contiguous with another
3072 * real allocation.
3073 * Insert a new entry.
3074 */
3075 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3076 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3077 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3078 if (bma->cur == NULL) {
3079 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
3080 } else {
3081 rval = XFS_ILOG_CORE;
3082 error = xfs_bmbt_lookup_eq(bma->cur,
3083 new->br_startoff,
3084 new->br_startblock,
3085 new->br_blockcount, &i);
3086 if (error)
3087 goto done;
c29aad41 3088 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
9e5987a7
DC
3089 bma->cur->bc_rec.b.br_state = new->br_state;
3090 error = xfs_btree_insert(bma->cur, &i);
3091 if (error)
3092 goto done;
c29aad41 3093 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
3094 }
3095 break;
3096 }
3097
9c194644
DW
3098 /* add reverse mapping */
3099 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
3100 if (error)
3101 goto done;
3102
9e5987a7
DC
3103 /* convert to a btree if necessary */
3104 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
3105 int tmp_logflags; /* partial log flag return val */
3106
3107 ASSERT(bma->cur == NULL);
3108 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2c3234d1 3109 bma->firstblock, bma->dfops, &bma->cur,
9e5987a7
DC
3110 0, &tmp_logflags, whichfork);
3111 bma->logflags |= tmp_logflags;
3112 if (error)
3113 goto done;
3114 }
3115
3116 /* clear out the allocated field, done with it now in any case. */
3117 if (bma->cur)
3118 bma->cur->bc_private.b.allocated = 0;
3119
3120 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
3121done:
3122 bma->logflags |= rval;
3123 return error;
1da177e4
LT
3124}
3125
3126/*
9e5987a7 3127 * Functions used in the extent read, allocate and remove paths
1da177e4 3128 */
1da177e4 3129
9e5987a7
DC
3130/*
3131 * Adjust the size of the new extent based on di_extsize and rt extsize.
3132 */
68988114 3133int
9e5987a7
DC
3134xfs_bmap_extsize_align(
3135 xfs_mount_t *mp,
3136 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3137 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3138 xfs_extlen_t extsz, /* align to this extent size */
3139 int rt, /* is this a realtime inode? */
3140 int eof, /* is extent at end-of-file? */
3141 int delay, /* creating delalloc extent? */
3142 int convert, /* overwriting unwritten extent? */
3143 xfs_fileoff_t *offp, /* in/out: aligned offset */
3144 xfs_extlen_t *lenp) /* in/out: aligned length */
4e8938fe 3145{
9e5987a7
DC
3146 xfs_fileoff_t orig_off; /* original offset */
3147 xfs_extlen_t orig_alen; /* original length */
3148 xfs_fileoff_t orig_end; /* original off+len */
3149 xfs_fileoff_t nexto; /* next file offset */
3150 xfs_fileoff_t prevo; /* previous file offset */
3151 xfs_fileoff_t align_off; /* temp for offset */
3152 xfs_extlen_t align_alen; /* temp for length */
3153 xfs_extlen_t temp; /* temp for calculations */
4e8938fe 3154
9e5987a7 3155 if (convert)
4e8938fe 3156 return 0;
4e8938fe 3157
9e5987a7
DC
3158 orig_off = align_off = *offp;
3159 orig_alen = align_alen = *lenp;
3160 orig_end = orig_off + orig_alen;
1da177e4 3161
1da177e4 3162 /*
9e5987a7
DC
3163 * If this request overlaps an existing extent, then don't
3164 * attempt to perform any additional alignment.
1da177e4 3165 */
9e5987a7
DC
3166 if (!delay && !eof &&
3167 (orig_off >= gotp->br_startoff) &&
3168 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3169 return 0;
3170 }
3171
1da177e4 3172 /*
9e5987a7
DC
3173 * If the file offset is unaligned vs. the extent size
3174 * we need to align it. This will be possible unless
3175 * the file was previously written with a kernel that didn't
3176 * perform this alignment, or if a truncate shot us in the
3177 * foot.
1da177e4 3178 */
9e5987a7
DC
3179 temp = do_mod(orig_off, extsz);
3180 if (temp) {
3181 align_alen += temp;
3182 align_off -= temp;
1da177e4 3183 }
6dea405e
DC
3184
3185 /* Same adjustment for the end of the requested area. */
3186 temp = (align_alen % extsz);
3187 if (temp)
3188 align_alen += extsz - temp;
3189
1da177e4 3190 /*
6dea405e
DC
3191 * For large extent hint sizes, the aligned extent might be larger than
3192 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3193 * the length back under MAXEXTLEN. The outer allocation loops handle
3194 * short allocation just fine, so it is safe to do this. We only want to
3195 * do it when we are forced to, though, because it means more allocation
3196 * operations are required.
1da177e4 3197 */
6dea405e
DC
3198 while (align_alen > MAXEXTLEN)
3199 align_alen -= extsz;
3200 ASSERT(align_alen <= MAXEXTLEN);
3201
1da177e4 3202 /*
9e5987a7
DC
3203 * If the previous block overlaps with this proposed allocation
3204 * then move the start forward without adjusting the length.
1da177e4 3205 */
9e5987a7
DC
3206 if (prevp->br_startoff != NULLFILEOFF) {
3207 if (prevp->br_startblock == HOLESTARTBLOCK)
3208 prevo = prevp->br_startoff;
3209 else
3210 prevo = prevp->br_startoff + prevp->br_blockcount;
3211 } else
3212 prevo = 0;
3213 if (align_off != orig_off && align_off < prevo)
3214 align_off = prevo;
3215 /*
3216 * If the next block overlaps with this proposed allocation
3217 * then move the start back without adjusting the length,
3218 * but not before offset 0.
3219 * This may of course make the start overlap previous block,
3220 * and if we hit the offset 0 limit then the next block
3221 * can still overlap too.
3222 */
3223 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3224 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3225 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3226 nexto = gotp->br_startoff + gotp->br_blockcount;
3227 else
3228 nexto = gotp->br_startoff;
3229 } else
3230 nexto = NULLFILEOFF;
3231 if (!eof &&
3232 align_off + align_alen != orig_end &&
3233 align_off + align_alen > nexto)
3234 align_off = nexto > align_alen ? nexto - align_alen : 0;
3235 /*
3236 * If we're now overlapping the next or previous extent that
3237 * means we can't fit an extsz piece in this hole. Just move
3238 * the start forward to the first valid spot and set
3239 * the length so we hit the end.
3240 */
3241 if (align_off != orig_off && align_off < prevo)
3242 align_off = prevo;
3243 if (align_off + align_alen != orig_end &&
3244 align_off + align_alen > nexto &&
3245 nexto != NULLFILEOFF) {
3246 ASSERT(nexto > prevo);
3247 align_alen = nexto - align_off;
3248 }
1da177e4 3249
9e5987a7
DC
3250 /*
3251 * If realtime, and the result isn't a multiple of the realtime
3252 * extent size we need to remove blocks until it is.
3253 */
3254 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
1da177e4 3255 /*
9e5987a7
DC
3256 * We're not covering the original request, or
3257 * we won't be able to once we fix the length.
1da177e4 3258 */
9e5987a7
DC
3259 if (orig_off < align_off ||
3260 orig_end > align_off + align_alen ||
3261 align_alen - temp < orig_alen)
2451337d 3262 return -EINVAL;
1da177e4 3263 /*
9e5987a7 3264 * Try to fix it by moving the start up.
1da177e4 3265 */
9e5987a7
DC
3266 if (align_off + temp <= orig_off) {
3267 align_alen -= temp;
3268 align_off += temp;
1da177e4 3269 }
9e5987a7
DC
3270 /*
3271 * Try to fix it by moving the end in.
3272 */
3273 else if (align_off + align_alen - temp >= orig_end)
3274 align_alen -= temp;
3275 /*
3276 * Set the start to the minimum then trim the length.
3277 */
3278 else {
3279 align_alen -= orig_off - align_off;
3280 align_off = orig_off;
3281 align_alen -= align_alen % mp->m_sb.sb_rextsize;
1da177e4 3282 }
1da177e4 3283 /*
9e5987a7 3284 * Result doesn't cover the request, fail it.
1da177e4 3285 */
9e5987a7 3286 if (orig_off < align_off || orig_end > align_off + align_alen)
2451337d 3287 return -EINVAL;
9e5987a7
DC
3288 } else {
3289 ASSERT(orig_off >= align_off);
6dea405e
DC
3290 /* see MAXEXTLEN handling above */
3291 ASSERT(orig_end <= align_off + align_alen ||
3292 align_alen + extsz > MAXEXTLEN);
1da177e4 3293 }
1da177e4 3294
0b1b213f 3295#ifdef DEBUG
9e5987a7
DC
3296 if (!eof && gotp->br_startoff != NULLFILEOFF)
3297 ASSERT(align_off + align_alen <= gotp->br_startoff);
3298 if (prevp->br_startoff != NULLFILEOFF)
3299 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3300#endif
1da177e4 3301
9e5987a7
DC
3302 *lenp = align_alen;
3303 *offp = align_off;
3304 return 0;
1da177e4 3305}
1da177e4 3306
9e5987a7
DC
3307#define XFS_ALLOC_GAP_UNITS 4
3308
68988114 3309void
9e5987a7 3310xfs_bmap_adjacent(
68988114 3311 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
1da177e4 3312{
9e5987a7
DC
3313 xfs_fsblock_t adjust; /* adjustment to block numbers */
3314 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3315 xfs_mount_t *mp; /* mount point structure */
3316 int nullfb; /* true if ap->firstblock isn't set */
3317 int rt; /* true if inode is realtime */
1da177e4 3318
9e5987a7
DC
3319#define ISVALID(x,y) \
3320 (rt ? \
3321 (x) < mp->m_sb.sb_rblocks : \
3322 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3323 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3324 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
1da177e4 3325
9e5987a7
DC
3326 mp = ap->ip->i_mount;
3327 nullfb = *ap->firstblock == NULLFSBLOCK;
292378ed
DC
3328 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3329 xfs_alloc_is_userdata(ap->datatype);
9e5987a7
DC
3330 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3331 /*
3332 * If allocating at eof, and there's a previous real block,
3333 * try to use its last block as our starting point.
3334 */
3335 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3336 !isnullstartblock(ap->prev.br_startblock) &&
3337 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3338 ap->prev.br_startblock)) {
3339 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
3340 /*
3341 * Adjust for the gap between prevp and us.
3342 */
3343 adjust = ap->offset -
3344 (ap->prev.br_startoff + ap->prev.br_blockcount);
3345 if (adjust &&
3346 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3347 ap->blkno += adjust;
3348 }
3349 /*
3350 * If not at eof, then compare the two neighbor blocks.
3351 * Figure out whether either one gives us a good starting point,
3352 * and pick the better one.
3353 */
3354 else if (!ap->eof) {
3355 xfs_fsblock_t gotbno; /* right side block number */
3356 xfs_fsblock_t gotdiff=0; /* right side difference */
3357 xfs_fsblock_t prevbno; /* left side block number */
3358 xfs_fsblock_t prevdiff=0; /* left side difference */
3359
3360 /*
3361 * If there's a previous (left) block, select a requested
3362 * start block based on it.
3363 */
3364 if (ap->prev.br_startoff != NULLFILEOFF &&
3365 !isnullstartblock(ap->prev.br_startblock) &&
3366 (prevbno = ap->prev.br_startblock +
3367 ap->prev.br_blockcount) &&
3368 ISVALID(prevbno, ap->prev.br_startblock)) {
3369 /*
3370 * Calculate gap to end of previous block.
3371 */
3372 adjust = prevdiff = ap->offset -
3373 (ap->prev.br_startoff +
3374 ap->prev.br_blockcount);
3375 /*
3376 * Figure the startblock based on the previous block's
3377 * end and the gap size.
3378 * Heuristic!
3379 * If the gap is large relative to the piece we're
3380 * allocating, or using it gives us an invalid block
3381 * number, then just use the end of the previous block.
3382 */
3383 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3384 ISVALID(prevbno + prevdiff,
3385 ap->prev.br_startblock))
3386 prevbno += adjust;
3387 else
3388 prevdiff += adjust;
3389 /*
3390 * If the firstblock forbids it, can't use it,
3391 * must use default.
3392 */
3393 if (!rt && !nullfb &&
3394 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3395 prevbno = NULLFSBLOCK;
1da177e4 3396 }
9e5987a7
DC
3397 /*
3398 * No previous block or can't follow it, just default.
3399 */
3400 else
3401 prevbno = NULLFSBLOCK;
3402 /*
3403 * If there's a following (right) block, select a requested
3404 * start block based on it.
3405 */
3406 if (!isnullstartblock(ap->got.br_startblock)) {
3407 /*
3408 * Calculate gap to start of next block.
3409 */
3410 adjust = gotdiff = ap->got.br_startoff - ap->offset;
3411 /*
3412 * Figure the startblock based on the next block's
3413 * start and the gap size.
3414 */
3415 gotbno = ap->got.br_startblock;
3416 /*
3417 * Heuristic!
3418 * If the gap is large relative to the piece we're
3419 * allocating, or using it gives us an invalid block
3420 * number, then just use the start of the next block
3421 * offset by our length.
3422 */
3423 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
3424 ISVALID(gotbno - gotdiff, gotbno))
3425 gotbno -= adjust;
3426 else if (ISVALID(gotbno - ap->length, gotbno)) {
3427 gotbno -= ap->length;
3428 gotdiff += adjust - ap->length;
3429 } else
3430 gotdiff += adjust;
3431 /*
3432 * If the firstblock forbids it, can't use it,
3433 * must use default.
3434 */
3435 if (!rt && !nullfb &&
3436 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3437 gotbno = NULLFSBLOCK;
3438 }
3439 /*
3440 * No next block, just default.
3441 */
3442 else
3443 gotbno = NULLFSBLOCK;
3444 /*
3445 * If both valid, pick the better one, else the only good
3446 * one, else ap->blkno is already set (to 0 or the inode block).
3447 */
3448 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
3449 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
3450 else if (prevbno != NULLFSBLOCK)
3451 ap->blkno = prevbno;
3452 else if (gotbno != NULLFSBLOCK)
3453 ap->blkno = gotbno;
1da177e4 3454 }
9e5987a7 3455#undef ISVALID
1da177e4 3456}
1da177e4 3457
c977eb10
CH
3458static int
3459xfs_bmap_longest_free_extent(
3460 struct xfs_trans *tp,
3461 xfs_agnumber_t ag,
3462 xfs_extlen_t *blen,
3463 int *notinit)
3464{
3465 struct xfs_mount *mp = tp->t_mountp;
3466 struct xfs_perag *pag;
3467 xfs_extlen_t longest;
3468 int error = 0;
3469
3470 pag = xfs_perag_get(mp, ag);
3471 if (!pag->pagf_init) {
3472 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3473 if (error)
3474 goto out;
3475
3476 if (!pag->pagf_init) {
3477 *notinit = 1;
3478 goto out;
3479 }
3480 }
3481
50adbcb4 3482 longest = xfs_alloc_longest_free_extent(mp, pag,
3fd129b6
DW
3483 xfs_alloc_min_freelist(mp, pag),
3484 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
c977eb10
CH
3485 if (*blen < longest)
3486 *blen = longest;
3487
3488out:
3489 xfs_perag_put(pag);
3490 return error;
3491}
3492
3493static void
3494xfs_bmap_select_minlen(
3495 struct xfs_bmalloca *ap,
3496 struct xfs_alloc_arg *args,
3497 xfs_extlen_t *blen,
3498 int notinit)
3499{
3500 if (notinit || *blen < ap->minlen) {
3501 /*
3502 * Since we did a BUF_TRYLOCK above, it is possible that
3503 * there is space for this request.
3504 */
3505 args->minlen = ap->minlen;
3506 } else if (*blen < args->maxlen) {
3507 /*
3508 * If the best seen length is less than the request length,
3509 * use the best as the minimum.
3510 */
3511 args->minlen = *blen;
3512 } else {
3513 /*
3514 * Otherwise we've seen an extent as big as maxlen, use that
3515 * as the minimum.
3516 */
3517 args->minlen = args->maxlen;
3518 }
3519}
3520
b64dfe4e 3521STATIC int
9e5987a7
DC
3522xfs_bmap_btalloc_nullfb(
3523 struct xfs_bmalloca *ap,
3524 struct xfs_alloc_arg *args,
3525 xfs_extlen_t *blen)
b64dfe4e 3526{
9e5987a7 3527 struct xfs_mount *mp = ap->ip->i_mount;
9e5987a7
DC
3528 xfs_agnumber_t ag, startag;
3529 int notinit = 0;
b64dfe4e
CH
3530 int error;
3531
c977eb10 3532 args->type = XFS_ALLOCTYPE_START_BNO;
9e5987a7 3533 args->total = ap->total;
b64dfe4e 3534
9e5987a7
DC
3535 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3536 if (startag == NULLAGNUMBER)
3537 startag = ag = 0;
b64dfe4e 3538
9e5987a7 3539 while (*blen < args->maxlen) {
c977eb10
CH
3540 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3541 &notinit);
3542 if (error)
3543 return error;
b64dfe4e 3544
9e5987a7
DC
3545 if (++ag == mp->m_sb.sb_agcount)
3546 ag = 0;
3547 if (ag == startag)
3548 break;
9e5987a7 3549 }
b64dfe4e 3550
c977eb10
CH
3551 xfs_bmap_select_minlen(ap, args, blen, notinit);
3552 return 0;
3553}
3554
3555STATIC int
3556xfs_bmap_btalloc_filestreams(
3557 struct xfs_bmalloca *ap,
3558 struct xfs_alloc_arg *args,
3559 xfs_extlen_t *blen)
3560{
3561 struct xfs_mount *mp = ap->ip->i_mount;
3562 xfs_agnumber_t ag;
3563 int notinit = 0;
3564 int error;
3565
3566 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3567 args->total = ap->total;
3568
3569 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3570 if (ag == NULLAGNUMBER)
3571 ag = 0;
3572
3573 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3574 if (error)
3575 return error;
3576
3577 if (*blen < args->maxlen) {
3578 error = xfs_filestream_new_ag(ap, &ag);
3579 if (error)
3580 return error;
3581
3582 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3583 &notinit);
3584 if (error)
3585 return error;
3586
3587 }
3588
3589 xfs_bmap_select_minlen(ap, args, blen, notinit);
b64dfe4e
CH
3590
3591 /*
c977eb10
CH
3592 * Set the failure fallback case to look in the selected AG as stream
3593 * may have moved.
b64dfe4e 3594 */
c977eb10 3595 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
b64dfe4e 3596 return 0;
b64dfe4e
CH
3597}
3598
9e5987a7
DC
3599STATIC int
3600xfs_bmap_btalloc(
68988114 3601 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
4403280a 3602{
9e5987a7
DC
3603 xfs_mount_t *mp; /* mount point structure */
3604 xfs_alloctype_t atype = 0; /* type for allocation routines */
292378ed 3605 xfs_extlen_t align = 0; /* minimum allocation alignment */
9e5987a7
DC
3606 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3607 xfs_agnumber_t ag;
3608 xfs_alloc_arg_t args;
3609 xfs_extlen_t blen;
3610 xfs_extlen_t nextminlen = 0;
3611 int nullfb; /* true if ap->firstblock isn't set */
3612 int isaligned;
3613 int tryagain;
3614 int error;
33177f05 3615 int stripe_align;
4403280a 3616
9e5987a7 3617 ASSERT(ap->length);
4403280a 3618
9e5987a7 3619 mp = ap->ip->i_mount;
33177f05
DC
3620
3621 /* stripe alignment for allocation is determined by mount parameters */
3622 stripe_align = 0;
3623 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3624 stripe_align = mp->m_swidth;
3625 else if (mp->m_dalign)
3626 stripe_align = mp->m_dalign;
3627
f7ca3522
DW
3628 if (ap->flags & XFS_BMAPI_COWFORK)
3629 align = xfs_get_cowextsz_hint(ap->ip);
3630 else if (xfs_alloc_is_userdata(ap->datatype))
292378ed 3631 align = xfs_get_extsz_hint(ap->ip);
9e5987a7
DC
3632 if (unlikely(align)) {
3633 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
3634 align, 0, ap->eof, 0, ap->conv,
3635 &ap->offset, &ap->length);
3636 ASSERT(!error);
3637 ASSERT(ap->length);
4403280a 3638 }
33177f05
DC
3639
3640
9e5987a7
DC
3641 nullfb = *ap->firstblock == NULLFSBLOCK;
3642 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
3643 if (nullfb) {
292378ed
DC
3644 if (xfs_alloc_is_userdata(ap->datatype) &&
3645 xfs_inode_is_filestream(ap->ip)) {
9e5987a7
DC
3646 ag = xfs_filestream_lookup_ag(ap->ip);
3647 ag = (ag != NULLAGNUMBER) ? ag : 0;
3648 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
3649 } else {
3650 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
3651 }
3652 } else
3653 ap->blkno = *ap->firstblock;
4403280a 3654
9e5987a7 3655 xfs_bmap_adjacent(ap);
4403280a 3656
9e5987a7
DC
3657 /*
3658 * If allowed, use ap->blkno; otherwise must use firstblock since
3659 * it's in the right allocation group.
3660 */
3661 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
3662 ;
3663 else
3664 ap->blkno = *ap->firstblock;
3665 /*
3666 * Normal allocation, done through xfs_alloc_vextent.
3667 */
3668 tryagain = isaligned = 0;
3669 memset(&args, 0, sizeof(args));
3670 args.tp = ap->tp;
3671 args.mp = mp;
3672 args.fsbno = ap->blkno;
340785cc 3673 xfs_rmap_skip_owner_update(&args.oinfo);
4403280a 3674
9e5987a7 3675 /* Trim the allocation back to the maximum an AG can fit. */
52548852 3676 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
9e5987a7
DC
3677 args.firstblock = *ap->firstblock;
3678 blen = 0;
3679 if (nullfb) {
c977eb10
CH
3680 /*
3681 * Search for an allocation group with a single extent large
3682 * enough for the request. If one isn't found, then adjust
3683 * the minimum allocation size to the largest space found.
3684 */
292378ed
DC
3685 if (xfs_alloc_is_userdata(ap->datatype) &&
3686 xfs_inode_is_filestream(ap->ip))
c977eb10
CH
3687 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3688 else
3689 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
4403280a
CH
3690 if (error)
3691 return error;
2c3234d1 3692 } else if (ap->dfops->dop_low) {
9e5987a7
DC
3693 if (xfs_inode_is_filestream(ap->ip))
3694 args.type = XFS_ALLOCTYPE_FIRST_AG;
3695 else
3696 args.type = XFS_ALLOCTYPE_START_BNO;
3697 args.total = args.minlen = ap->minlen;
3698 } else {
3699 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3700 args.total = ap->total;
3701 args.minlen = ap->minlen;
4403280a 3702 }
9e5987a7
DC
3703 /* apply extent size hints if obtained earlier */
3704 if (unlikely(align)) {
3705 args.prod = align;
3706 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
3707 args.mod = (xfs_extlen_t)(args.prod - args.mod);
09cbfeaf 3708 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
9e5987a7
DC
3709 args.prod = 1;
3710 args.mod = 0;
3711 } else {
09cbfeaf 3712 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
9e5987a7
DC
3713 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
3714 args.mod = (xfs_extlen_t)(args.prod - args.mod);
4403280a 3715 }
7e47a4ef 3716 /*
9e5987a7
DC
3717 * If we are not low on available data blocks, and the
3718 * underlying logical volume manager is a stripe, and
3719 * the file offset is zero then try to allocate data
3720 * blocks on stripe unit boundary.
3721 * NOTE: ap->aeof is only set if the allocation length
3722 * is >= the stripe unit and the allocation offset is
3723 * at the end of file.
7e47a4ef 3724 */
2c3234d1 3725 if (!ap->dfops->dop_low && ap->aeof) {
9e5987a7 3726 if (!ap->offset) {
33177f05 3727 args.alignment = stripe_align;
9e5987a7
DC
3728 atype = args.type;
3729 isaligned = 1;
3730 /*
3731 * Adjust for alignment
3732 */
3733 if (blen > args.alignment && blen <= args.maxlen)
3734 args.minlen = blen - args.alignment;
3735 args.minalignslop = 0;
3736 } else {
3737 /*
3738 * First try an exact bno allocation.
3739 * If it fails then do a near or start bno
3740 * allocation with alignment turned on.
3741 */
3742 atype = args.type;
3743 tryagain = 1;
3744 args.type = XFS_ALLOCTYPE_THIS_BNO;
3745 args.alignment = 1;
3746 /*
3747 * Compute the minlen+alignment for the
3748 * next case. Set slop so that the value
3749 * of minlen+alignment+slop doesn't go up
3750 * between the calls.
3751 */
33177f05
DC
3752 if (blen > stripe_align && blen <= args.maxlen)
3753 nextminlen = blen - stripe_align;
9e5987a7
DC
3754 else
3755 nextminlen = args.minlen;
33177f05 3756 if (nextminlen + stripe_align > args.minlen + 1)
9e5987a7 3757 args.minalignslop =
33177f05 3758 nextminlen + stripe_align -
9e5987a7
DC
3759 args.minlen - 1;
3760 else
3761 args.minalignslop = 0;
7e47a4ef
DC
3762 }
3763 } else {
9e5987a7
DC
3764 args.alignment = 1;
3765 args.minalignslop = 0;
7e47a4ef 3766 }
9e5987a7
DC
3767 args.minleft = ap->minleft;
3768 args.wasdel = ap->wasdel;
3fd129b6 3769 args.resv = XFS_AG_RESV_NONE;
292378ed
DC
3770 args.datatype = ap->datatype;
3771 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
3fbbbea3
DC
3772 args.ip = ap->ip;
3773
3774 error = xfs_alloc_vextent(&args);
3775 if (error)
9e5987a7 3776 return error;
3fbbbea3 3777
9e5987a7
DC
3778 if (tryagain && args.fsbno == NULLFSBLOCK) {
3779 /*
3780 * Exact allocation failed. Now try with alignment
3781 * turned on.
3782 */
3783 args.type = atype;
3784 args.fsbno = ap->blkno;
33177f05 3785 args.alignment = stripe_align;
9e5987a7
DC
3786 args.minlen = nextminlen;
3787 args.minalignslop = 0;
3788 isaligned = 1;
3789 if ((error = xfs_alloc_vextent(&args)))
3790 return error;
7e47a4ef 3791 }
9e5987a7
DC
3792 if (isaligned && args.fsbno == NULLFSBLOCK) {
3793 /*
3794 * allocation failed, so turn off alignment and
3795 * try again.
3796 */
3797 args.type = atype;
3798 args.fsbno = ap->blkno;
3799 args.alignment = 0;
3800 if ((error = xfs_alloc_vextent(&args)))
7e47a4ef
DC
3801 return error;
3802 }
9e5987a7
DC
3803 if (args.fsbno == NULLFSBLOCK && nullfb &&
3804 args.minlen > ap->minlen) {
3805 args.minlen = ap->minlen;
3806 args.type = XFS_ALLOCTYPE_START_BNO;
3807 args.fsbno = ap->blkno;
3808 if ((error = xfs_alloc_vextent(&args)))
3809 return error;
7e47a4ef 3810 }
9e5987a7
DC
3811 if (args.fsbno == NULLFSBLOCK && nullfb) {
3812 args.fsbno = 0;
3813 args.type = XFS_ALLOCTYPE_FIRST_AG;
3814 args.total = ap->minlen;
9e5987a7
DC
3815 if ((error = xfs_alloc_vextent(&args)))
3816 return error;
2c3234d1 3817 ap->dfops->dop_low = true;
9e5987a7
DC
3818 }
3819 if (args.fsbno != NULLFSBLOCK) {
3820 /*
3821 * check the allocation happened at the same or higher AG than
3822 * the first block that was allocated.
3823 */
3824 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3825 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3826 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
2c3234d1 3827 (ap->dfops->dop_low &&
9e5987a7
DC
3828 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3829 XFS_FSB_TO_AGNO(mp, args.fsbno)));
7e47a4ef 3830
9e5987a7
DC
3831 ap->blkno = args.fsbno;
3832 if (*ap->firstblock == NULLFSBLOCK)
3833 *ap->firstblock = args.fsbno;
3834 ASSERT(nullfb || fb_agno == args.agno ||
2c3234d1 3835 (ap->dfops->dop_low && fb_agno < args.agno));
9e5987a7 3836 ap->length = args.len;
60b4984f
DW
3837 if (!(ap->flags & XFS_BMAPI_COWFORK))
3838 ap->ip->i_d.di_nblocks += args.len;
9e5987a7
DC
3839 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3840 if (ap->wasdel)
3841 ap->ip->i_delayed_blks -= args.len;
3842 /*
3843 * Adjust the disk quota also. This was reserved
3844 * earlier.
3845 */
3846 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
3847 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3848 XFS_TRANS_DQ_BCOUNT,
3849 (long) args.len);
3850 } else {
3851 ap->blkno = NULLFSBLOCK;
3852 ap->length = 0;
3853 }
3854 return 0;
3855}
7e47a4ef 3856
f65306ea
DW
3857/*
3858 * For a remap operation, just "allocate" an extent at the address that the
3859 * caller passed in, and ensure that the AGFL is the right size. The caller
3860 * will then map the "allocated" extent into the file somewhere.
3861 */
3862STATIC int
3863xfs_bmap_remap_alloc(
3864 struct xfs_bmalloca *ap)
3865{
3866 struct xfs_trans *tp = ap->tp;
3867 struct xfs_mount *mp = tp->t_mountp;
3868 xfs_agblock_t bno;
3869 struct xfs_alloc_arg args;
3870 int error;
3871
3872 /*
3873 * validate that the block number is legal - the enables us to detect
3874 * and handle a silent filesystem corruption rather than crashing.
3875 */
3876 memset(&args, 0, sizeof(struct xfs_alloc_arg));
3877 args.tp = ap->tp;
3878 args.mp = ap->tp->t_mountp;
3879 bno = *ap->firstblock;
3880 args.agno = XFS_FSB_TO_AGNO(mp, bno);
3881 args.agbno = XFS_FSB_TO_AGBNO(mp, bno);
3882 if (args.agno >= mp->m_sb.sb_agcount ||
3883 args.agbno >= mp->m_sb.sb_agblocks)
3884 return -EFSCORRUPTED;
3885
3886 /* "Allocate" the extent from the range we passed in. */
3887 trace_xfs_bmap_remap_alloc(ap->ip, *ap->firstblock, ap->length);
3888 ap->blkno = bno;
3889 ap->ip->i_d.di_nblocks += ap->length;
3890 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3891
3892 /* Fix the freelist, like a real allocator does. */
3893 args.datatype = ap->datatype;
3894 args.pag = xfs_perag_get(args.mp, args.agno);
3895 ASSERT(args.pag);
3896
3897 /*
3898 * The freelist fixing code will decline the allocation if
3899 * the size and shape of the free space doesn't allow for
3900 * allocating the extent and updating all the metadata that
3901 * happens during an allocation. We're remapping, not
3902 * allocating, so skip that check by pretending to be freeing.
3903 */
3904 error = xfs_alloc_fix_freelist(&args, XFS_ALLOC_FLAG_FREEING);
f65306ea
DW
3905 xfs_perag_put(args.pag);
3906 if (error)
3907 trace_xfs_bmap_remap_alloc_error(ap->ip, error, _RET_IP_);
3908 return error;
3909}
3910
9e5987a7
DC
3911/*
3912 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3913 * It figures out where to ask the underlying allocator to put the new extent.
3914 */
3915STATIC int
3916xfs_bmap_alloc(
68988114 3917 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
9e5987a7 3918{
f65306ea
DW
3919 if (ap->flags & XFS_BMAPI_REMAP)
3920 return xfs_bmap_remap_alloc(ap);
292378ed
DC
3921 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3922 xfs_alloc_is_userdata(ap->datatype))
9e5987a7
DC
3923 return xfs_bmap_rtalloc(ap);
3924 return xfs_bmap_btalloc(ap);
3925}
a5bd606b 3926
0a0af28c
DW
3927/* Trim extent to fit a logical block range. */
3928void
3929xfs_trim_extent(
3930 struct xfs_bmbt_irec *irec,
3931 xfs_fileoff_t bno,
3932 xfs_filblks_t len)
3933{
3934 xfs_fileoff_t distance;
3935 xfs_fileoff_t end = bno + len;
3936
3937 if (irec->br_startoff + irec->br_blockcount <= bno ||
3938 irec->br_startoff >= end) {
3939 irec->br_blockcount = 0;
3940 return;
3941 }
3942
3943 if (irec->br_startoff < bno) {
3944 distance = bno - irec->br_startoff;
3945 if (isnullstartblock(irec->br_startblock))
3946 irec->br_startblock = DELAYSTARTBLOCK;
3947 if (irec->br_startblock != DELAYSTARTBLOCK &&
3948 irec->br_startblock != HOLESTARTBLOCK)
3949 irec->br_startblock += distance;
3950 irec->br_startoff += distance;
3951 irec->br_blockcount -= distance;
3952 }
3953
3954 if (end < irec->br_startoff + irec->br_blockcount) {
3955 distance = irec->br_startoff + irec->br_blockcount - end;
3956 irec->br_blockcount -= distance;
3957 }
3958}
3959
9e5987a7
DC
3960/*
3961 * Trim the returned map to the required bounds
3962 */
3963STATIC void
3964xfs_bmapi_trim_map(
3965 struct xfs_bmbt_irec *mval,
3966 struct xfs_bmbt_irec *got,
3967 xfs_fileoff_t *bno,
3968 xfs_filblks_t len,
3969 xfs_fileoff_t obno,
3970 xfs_fileoff_t end,
3971 int n,
3972 int flags)
3973{
3974 if ((flags & XFS_BMAPI_ENTIRE) ||
3975 got->br_startoff + got->br_blockcount <= obno) {
3976 *mval = *got;
3977 if (isnullstartblock(got->br_startblock))
3978 mval->br_startblock = DELAYSTARTBLOCK;
3979 return;
3980 }
7e47a4ef 3981
9e5987a7
DC
3982 if (obno > *bno)
3983 *bno = obno;
3984 ASSERT((*bno >= obno) || (n == 0));
3985 ASSERT(*bno < end);
3986 mval->br_startoff = *bno;
3987 if (isnullstartblock(got->br_startblock))
3988 mval->br_startblock = DELAYSTARTBLOCK;
3989 else
3990 mval->br_startblock = got->br_startblock +
3991 (*bno - got->br_startoff);
7e47a4ef 3992 /*
9e5987a7
DC
3993 * Return the minimum of what we got and what we asked for for
3994 * the length. We can use the len variable here because it is
3995 * modified below and we could have been there before coming
3996 * here if the first part of the allocation didn't overlap what
3997 * was asked for.
7e47a4ef 3998 */
9e5987a7
DC
3999 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4000 got->br_blockcount - (*bno - got->br_startoff));
4001 mval->br_state = got->br_state;
4002 ASSERT(mval->br_blockcount <= len);
4003 return;
7e47a4ef
DC
4004}
4005
9e5987a7
DC
4006/*
4007 * Update and validate the extent map to return
4008 */
4009STATIC void
4010xfs_bmapi_update_map(
4011 struct xfs_bmbt_irec **map,
4012 xfs_fileoff_t *bno,
4013 xfs_filblks_t *len,
4014 xfs_fileoff_t obno,
4015 xfs_fileoff_t end,
4016 int *n,
4017 int flags)
e04426b9 4018{
9e5987a7 4019 xfs_bmbt_irec_t *mval = *map;
e04426b9 4020
9e5987a7
DC
4021 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4022 ((mval->br_startoff + mval->br_blockcount) <= end));
4023 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4024 (mval->br_startoff < obno));
e04426b9 4025
9e5987a7
DC
4026 *bno = mval->br_startoff + mval->br_blockcount;
4027 *len = end - *bno;
4028 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4029 /* update previous map with new information */
4030 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4031 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4032 ASSERT(mval->br_state == mval[-1].br_state);
4033 mval[-1].br_blockcount = mval->br_blockcount;
4034 mval[-1].br_state = mval->br_state;
4035 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4036 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4037 mval[-1].br_startblock != HOLESTARTBLOCK &&
4038 mval->br_startblock == mval[-1].br_startblock +
4039 mval[-1].br_blockcount &&
4040 ((flags & XFS_BMAPI_IGSTATE) ||
4041 mval[-1].br_state == mval->br_state)) {
4042 ASSERT(mval->br_startoff ==
4043 mval[-1].br_startoff + mval[-1].br_blockcount);
4044 mval[-1].br_blockcount += mval->br_blockcount;
4045 } else if (*n > 0 &&
4046 mval->br_startblock == DELAYSTARTBLOCK &&
4047 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4048 mval->br_startoff ==
4049 mval[-1].br_startoff + mval[-1].br_blockcount) {
4050 mval[-1].br_blockcount += mval->br_blockcount;
4051 mval[-1].br_state = mval->br_state;
4052 } else if (!((*n == 0) &&
4053 ((mval->br_startoff + mval->br_blockcount) <=
4054 obno))) {
4055 mval++;
4056 (*n)++;
4057 }
4058 *map = mval;
e04426b9
DC
4059}
4060
4061/*
9e5987a7 4062 * Map file blocks to filesystem blocks without allocation.
e04426b9
DC
4063 */
4064int
9e5987a7
DC
4065xfs_bmapi_read(
4066 struct xfs_inode *ip,
4067 xfs_fileoff_t bno,
b447fe5a 4068 xfs_filblks_t len,
9e5987a7
DC
4069 struct xfs_bmbt_irec *mval,
4070 int *nmap,
c315c90b 4071 int flags)
b447fe5a 4072{
9e5987a7
DC
4073 struct xfs_mount *mp = ip->i_mount;
4074 struct xfs_ifork *ifp;
4075 struct xfs_bmbt_irec got;
9e5987a7
DC
4076 xfs_fileoff_t obno;
4077 xfs_fileoff_t end;
334f3423 4078 xfs_extnum_t idx;
9e5987a7 4079 int error;
334f3423 4080 bool eof = false;
9e5987a7 4081 int n = 0;
3993baeb 4082 int whichfork = xfs_bmapi_whichfork(flags);
b447fe5a 4083
9e5987a7
DC
4084 ASSERT(*nmap >= 1);
4085 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
3993baeb 4086 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
eef334e5 4087 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
b447fe5a 4088
9e5987a7
DC
4089 if (unlikely(XFS_TEST_ERROR(
4090 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4091 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4092 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4093 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
2451337d 4094 return -EFSCORRUPTED;
9e5987a7 4095 }
b447fe5a 4096
9e5987a7 4097 if (XFS_FORCED_SHUTDOWN(mp))
2451337d 4098 return -EIO;
9e5987a7 4099
ff6d6af2 4100 XFS_STATS_INC(mp, xs_blk_mapr);
9e5987a7
DC
4101
4102 ifp = XFS_IFORK_PTR(ip, whichfork);
4103
3993baeb
DW
4104 /* No CoW fork? Return a hole. */
4105 if (whichfork == XFS_COW_FORK && !ifp) {
4106 mval->br_startoff = bno;
4107 mval->br_startblock = HOLESTARTBLOCK;
4108 mval->br_blockcount = len;
4109 mval->br_state = XFS_EXT_NORM;
4110 *nmap = 1;
4111 return 0;
4112 }
4113
9e5987a7
DC
4114 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4115 error = xfs_iread_extents(NULL, ip, whichfork);
4116 if (error)
4117 return error;
b447fe5a 4118 }
b447fe5a 4119
334f3423
CH
4120 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4121 eof = true;
9e5987a7
DC
4122 end = bno + len;
4123 obno = bno;
b447fe5a 4124
9e5987a7
DC
4125 while (bno < end && n < *nmap) {
4126 /* Reading past eof, act as though there's a hole up to end. */
4127 if (eof)
4128 got.br_startoff = end;
4129 if (got.br_startoff > bno) {
4130 /* Reading in a hole. */
4131 mval->br_startoff = bno;
4132 mval->br_startblock = HOLESTARTBLOCK;
4133 mval->br_blockcount =
4134 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4135 mval->br_state = XFS_EXT_NORM;
4136 bno += mval->br_blockcount;
4137 len -= mval->br_blockcount;
4138 mval++;
4139 n++;
4140 continue;
4141 }
b447fe5a 4142
9e5987a7
DC
4143 /* set up the extent map to return. */
4144 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4145 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4146
4147 /* If we're done, stop now. */
4148 if (bno >= end || n >= *nmap)
4149 break;
4150
4151 /* Else go on to the next record. */
334f3423
CH
4152 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4153 eof = true;
9e5987a7
DC
4154 }
4155 *nmap = n;
b447fe5a
DC
4156 return 0;
4157}
4158
51446f5b 4159int
9e5987a7
DC
4160xfs_bmapi_reserve_delalloc(
4161 struct xfs_inode *ip,
be51f811 4162 int whichfork,
974ae922 4163 xfs_fileoff_t off,
9e5987a7 4164 xfs_filblks_t len,
974ae922 4165 xfs_filblks_t prealloc,
9e5987a7 4166 struct xfs_bmbt_irec *got,
9e5987a7
DC
4167 xfs_extnum_t *lastx,
4168 int eof)
1da177e4 4169{
c0dc7828 4170 struct xfs_mount *mp = ip->i_mount;
be51f811 4171 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
9e5987a7
DC
4172 xfs_extlen_t alen;
4173 xfs_extlen_t indlen;
4174 char rt = XFS_IS_REALTIME_INODE(ip);
4175 xfs_extlen_t extsz;
4176 int error;
974ae922 4177 xfs_fileoff_t aoff = off;
c0dc7828 4178
974ae922
BF
4179 /*
4180 * Cap the alloc length. Keep track of prealloc so we know whether to
4181 * tag the inode before we return.
4182 */
4183 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
9e5987a7
DC
4184 if (!eof)
4185 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
974ae922
BF
4186 if (prealloc && alen >= len)
4187 prealloc = alen - len;
1da177e4 4188
9e5987a7 4189 /* Figure out the extent size, adjust alen */
f7ca3522
DW
4190 if (whichfork == XFS_COW_FORK)
4191 extsz = xfs_get_cowextsz_hint(ip);
4192 else
4193 extsz = xfs_get_extsz_hint(ip);
9e5987a7 4194 if (extsz) {
65c5f419
CH
4195 struct xfs_bmbt_irec prev;
4196
4197 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4198 prev.br_startoff = NULLFILEOFF;
4199
4200 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
9e5987a7
DC
4201 1, 0, &aoff, &alen);
4202 ASSERT(!error);
4203 }
4204
4205 if (rt)
4206 extsz = alen / mp->m_sb.sb_rextsize;
4207
4208 /*
4209 * Make a transaction-less quota reservation for delayed allocation
4210 * blocks. This number gets adjusted later. We return if we haven't
4211 * allocated blocks already inside this loop.
4212 */
4213 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4214 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4215 if (error)
4216 return error;
4217
4218 /*
4219 * Split changing sb for alen and indlen since they could be coming
4220 * from different places.
4221 */
4222 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4223 ASSERT(indlen > 0);
4224
4225 if (rt) {
bab98bbe 4226 error = xfs_mod_frextents(mp, -((int64_t)extsz));
9e5987a7 4227 } else {
0d485ada 4228 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
9e5987a7
DC
4229 }
4230
4231 if (error)
4232 goto out_unreserve_quota;
4233
0d485ada 4234 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
9e5987a7
DC
4235 if (error)
4236 goto out_unreserve_blocks;
4237
4238
4239 ip->i_delayed_blks += alen;
4240
4241 got->br_startoff = aoff;
4242 got->br_startblock = nullstartblock(indlen);
4243 got->br_blockcount = alen;
4244 got->br_state = XFS_EXT_NORM;
be51f811 4245 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
9e5987a7
DC
4246
4247 /*
4248 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4249 * might have merged it into one of the neighbouring ones.
4250 */
4251 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4252
974ae922
BF
4253 /*
4254 * Tag the inode if blocks were preallocated. Note that COW fork
4255 * preallocation can occur at the start or end of the extent, even when
4256 * prealloc == 0, so we must also check the aligned offset and length.
4257 */
4258 if (whichfork == XFS_DATA_FORK && prealloc)
4259 xfs_inode_set_eofblocks_tag(ip);
4260 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4261 xfs_inode_set_cowblocks_tag(ip);
4262
9e5987a7
DC
4263 ASSERT(got->br_startoff <= aoff);
4264 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4265 ASSERT(isnullstartblock(got->br_startblock));
4266 ASSERT(got->br_state == XFS_EXT_NORM);
4267 return 0;
4268
4269out_unreserve_blocks:
4270 if (rt)
bab98bbe 4271 xfs_mod_frextents(mp, extsz);
9e5987a7 4272 else
0d485ada 4273 xfs_mod_fdblocks(mp, alen, false);
9e5987a7
DC
4274out_unreserve_quota:
4275 if (XFS_IS_QUOTA_ON(mp))
4276 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
4277 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4278 return error;
4279}
4280
cf11da9c
DC
4281static int
4282xfs_bmapi_allocate(
9e5987a7
DC
4283 struct xfs_bmalloca *bma)
4284{
4285 struct xfs_mount *mp = bma->ip->i_mount;
60b4984f 4286 int whichfork = xfs_bmapi_whichfork(bma->flags);
9e5987a7
DC
4287 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4288 int tmp_logflags = 0;
4289 int error;
9e5987a7
DC
4290
4291 ASSERT(bma->length > 0);
4292
1da177e4 4293 /*
9e5987a7
DC
4294 * For the wasdelay case, we could also just allocate the stuff asked
4295 * for in this bmap call but that wouldn't be as good.
1da177e4 4296 */
9e5987a7
DC
4297 if (bma->wasdel) {
4298 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4299 bma->offset = bma->got.br_startoff;
0e8d630b 4300 if (bma->idx) {
9e5987a7
DC
4301 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
4302 &bma->prev);
1da177e4 4303 }
9e5987a7
DC
4304 } else {
4305 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4306 if (!bma->eof)
4307 bma->length = XFS_FILBLKS_MIN(bma->length,
4308 bma->got.br_startoff - bma->offset);
1da177e4 4309 }
1da177e4 4310
9e5987a7 4311 /*
292378ed
DC
4312 * Set the data type being allocated. For the data fork, the first data
4313 * in the file is treated differently to all other allocations. For the
4314 * attribute fork, we only need to ensure the allocated range is not on
4315 * the busy list.
9e5987a7
DC
4316 */
4317 if (!(bma->flags & XFS_BMAPI_METADATA)) {
292378ed
DC
4318 bma->datatype = XFS_ALLOC_NOBUSY;
4319 if (whichfork == XFS_DATA_FORK) {
4320 if (bma->offset == 0)
4321 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4322 else
4323 bma->datatype |= XFS_ALLOC_USERDATA;
4324 }
3fbbbea3 4325 if (bma->flags & XFS_BMAPI_ZERO)
292378ed 4326 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
9e5987a7 4327 }
1da177e4 4328
9e5987a7 4329 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
0b1b213f 4330
9e5987a7
DC
4331 /*
4332 * Only want to do the alignment at the eof if it is userdata and
4333 * allocation length is larger than a stripe unit.
4334 */
4335 if (mp->m_dalign && bma->length >= mp->m_dalign &&
4336 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4337 error = xfs_bmap_isaeof(bma, whichfork);
4338 if (error)
4339 return error;
1da177e4 4340 }
8096b1eb 4341
9e5987a7
DC
4342 error = xfs_bmap_alloc(bma);
4343 if (error)
1da177e4 4344 return error;
9e5987a7 4345
9e5987a7
DC
4346 if (bma->cur)
4347 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4348 if (bma->blkno == NULLFSBLOCK)
1da177e4 4349 return 0;
9e5987a7
DC
4350 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4351 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4352 bma->cur->bc_private.b.firstblock = *bma->firstblock;
2c3234d1 4353 bma->cur->bc_private.b.dfops = bma->dfops;
1da177e4 4354 }
9e5987a7
DC
4355 /*
4356 * Bump the number of extents we've allocated
4357 * in this call.
4358 */
4359 bma->nallocs++;
4360
4361 if (bma->cur)
4362 bma->cur->bc_private.b.flags =
4363 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4364
4365 bma->got.br_startoff = bma->offset;
4366 bma->got.br_startblock = bma->blkno;
4367 bma->got.br_blockcount = bma->length;
4368 bma->got.br_state = XFS_EXT_NORM;
b4e9181e 4369
1da177e4 4370 /*
9e5987a7
DC
4371 * A wasdelay extent has been initialized, so shouldn't be flagged
4372 * as unwritten.
1da177e4 4373 */
9e5987a7
DC
4374 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
4375 xfs_sb_version_hasextflgbit(&mp->m_sb))
4376 bma->got.br_state = XFS_EXT_UNWRITTEN;
4377
4378 if (bma->wasdel)
60b4984f 4379 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
9e5987a7
DC
4380 else
4381 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
4382
4383 bma->logflags |= tmp_logflags;
4384 if (error)
4385 return error;
4386
4387 /*
4388 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4389 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4390 * the neighbouring ones.
4391 */
4392 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4393
4394 ASSERT(bma->got.br_startoff <= bma->offset);
4395 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4396 bma->offset + bma->length);
4397 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4398 bma->got.br_state == XFS_EXT_UNWRITTEN);
4399 return 0;
4400}
4401
9e5987a7
DC
4402STATIC int
4403xfs_bmapi_convert_unwritten(
4404 struct xfs_bmalloca *bma,
4405 struct xfs_bmbt_irec *mval,
4406 xfs_filblks_t len,
4407 int flags)
4408{
3993baeb 4409 int whichfork = xfs_bmapi_whichfork(flags);
9e5987a7
DC
4410 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4411 int tmp_logflags = 0;
4412 int error;
4413
4414 /* check if we need to do unwritten->real conversion */
4415 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4416 (flags & XFS_BMAPI_PREALLOC))
4417 return 0;
4418
4419 /* check if we need to do real->unwritten conversion */
4420 if (mval->br_state == XFS_EXT_NORM &&
4421 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4422 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4423 return 0;
4424
3993baeb
DW
4425 ASSERT(whichfork != XFS_COW_FORK);
4426
9e5987a7
DC
4427 /*
4428 * Modify (by adding) the state flag, if writing.
4429 */
4430 ASSERT(mval->br_blockcount <= len);
4431 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4432 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4433 bma->ip, whichfork);
4434 bma->cur->bc_private.b.firstblock = *bma->firstblock;
2c3234d1 4435 bma->cur->bc_private.b.dfops = bma->dfops;
1da177e4 4436 }
9e5987a7
DC
4437 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4438 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
5575acc7 4439
3fbbbea3
DC
4440 /*
4441 * Before insertion into the bmbt, zero the range being converted
4442 * if required.
4443 */
4444 if (flags & XFS_BMAPI_ZERO) {
4445 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4446 mval->br_blockcount);
4447 if (error)
4448 return error;
4449 }
4450
9e5987a7 4451 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
2c3234d1 4452 &bma->cur, mval, bma->firstblock, bma->dfops,
9e5987a7 4453 &tmp_logflags);
2e588a46
BF
4454 /*
4455 * Log the inode core unconditionally in the unwritten extent conversion
4456 * path because the conversion might not have done so (e.g., if the
4457 * extent count hasn't changed). We need to make sure the inode is dirty
4458 * in the transaction for the sake of fsync(), even if nothing has
4459 * changed, because fsync() will not force the log for this transaction
4460 * unless it sees the inode pinned.
4461 */
4462 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
9e5987a7
DC
4463 if (error)
4464 return error;
4465
4466 /*
4467 * Update our extent pointer, given that
4468 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4469 * of the neighbouring ones.
4470 */
4471 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4472
4473 /*
4474 * We may have combined previously unwritten space with written space,
4475 * so generate another request.
4476 */
4477 if (mval->br_blockcount < len)
2451337d 4478 return -EAGAIN;
9e5987a7
DC
4479 return 0;
4480}
4481
4482/*
4483 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4484 * extent state if necessary. Details behaviour is controlled by the flags
4485 * parameter. Only allocates blocks from a single allocation group, to avoid
4486 * locking problems.
4487 *
4488 * The returned value in "firstblock" from the first call in a transaction
4489 * must be remembered and presented to subsequent calls in "firstblock".
4490 * An upper bound for the number of blocks to be allocated is supplied to
4491 * the first call in "total"; if no allocation group has that many free
4492 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4493 */
4494int
4495xfs_bmapi_write(
4496 struct xfs_trans *tp, /* transaction pointer */
4497 struct xfs_inode *ip, /* incore inode */
4498 xfs_fileoff_t bno, /* starting file offs. mapped */
4499 xfs_filblks_t len, /* length to map in file */
4500 int flags, /* XFS_BMAPI_... */
4501 xfs_fsblock_t *firstblock, /* first allocated block
4502 controls a.g. for allocs */
4503 xfs_extlen_t total, /* total blocks needed */
4504 struct xfs_bmbt_irec *mval, /* output: map values */
4505 int *nmap, /* i/o: mval size/count */
2c3234d1 4506 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
9e5987a7
DC
4507{
4508 struct xfs_mount *mp = ip->i_mount;
4509 struct xfs_ifork *ifp;
a30b0367 4510 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
9e5987a7 4511 xfs_fileoff_t end; /* end of mapped file region */
2d58f6ef 4512 bool eof = false; /* after the end of extents */
9e5987a7
DC
4513 int error; /* error return */
4514 int n; /* current extent index */
4515 xfs_fileoff_t obno; /* old block number (offset) */
4516 int whichfork; /* data or attr fork */
4517 char inhole; /* current location is hole in file */
4518 char wasdelay; /* old extent was delayed */
4519
4520#ifdef DEBUG
4521 xfs_fileoff_t orig_bno; /* original block number value */
4522 int orig_flags; /* original flags arg value */
4523 xfs_filblks_t orig_len; /* original value of len arg */
4524 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4525 int orig_nmap; /* original value of *nmap */
4526
4527 orig_bno = bno;
4528 orig_len = len;
4529 orig_flags = flags;
4530 orig_mval = mval;
4531 orig_nmap = *nmap;
4532#endif
60b4984f 4533 whichfork = xfs_bmapi_whichfork(flags);
9e5987a7
DC
4534
4535 ASSERT(*nmap >= 1);
4536 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4537 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4538 ASSERT(tp != NULL);
4539 ASSERT(len > 0);
f3508bcd 4540 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
eef334e5 4541 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
f65306ea
DW
4542 ASSERT(!(flags & XFS_BMAPI_REMAP) || whichfork == XFS_DATA_FORK);
4543 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || !(flags & XFS_BMAPI_REMAP));
4544 ASSERT(!(flags & XFS_BMAPI_CONVERT) || !(flags & XFS_BMAPI_REMAP));
60b4984f
DW
4545 ASSERT(!(flags & XFS_BMAPI_PREALLOC) || whichfork != XFS_COW_FORK);
4546 ASSERT(!(flags & XFS_BMAPI_CONVERT) || whichfork != XFS_COW_FORK);
9e5987a7 4547
3fbbbea3
DC
4548 /* zeroing is for currently only for data extents, not metadata */
4549 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4550 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4551 /*
4552 * we can allocate unwritten extents or pre-zero allocated blocks,
4553 * but it makes no sense to do both at once. This would result in
4554 * zeroing the unwritten extent twice, but it still being an
4555 * unwritten extent....
4556 */
4557 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4558 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4559
9e5987a7
DC
4560 if (unlikely(XFS_TEST_ERROR(
4561 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
f3508bcd 4562 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
9e5987a7
DC
4563 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4564 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
2451337d 4565 return -EFSCORRUPTED;
5575acc7
KD
4566 }
4567
9e5987a7 4568 if (XFS_FORCED_SHUTDOWN(mp))
2451337d 4569 return -EIO;
9e5987a7
DC
4570
4571 ifp = XFS_IFORK_PTR(ip, whichfork);
4572
ff6d6af2 4573 XFS_STATS_INC(mp, xs_blk_mapw);
9e5987a7 4574
9e5987a7
DC
4575 if (*firstblock == NULLFSBLOCK) {
4576 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4577 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4578 else
4579 bma.minleft = 1;
4580 } else {
4581 bma.minleft = 0;
4582 }
4583
4584 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4585 error = xfs_iread_extents(tp, ip, whichfork);
4586 if (error)
4587 goto error0;
4588 }
4589
9e5987a7
DC
4590 n = 0;
4591 end = bno + len;
4592 obno = bno;
4593
2d58f6ef
CH
4594 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4595 eof = true;
4596 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4597 bma.prev.br_startoff = NULLFILEOFF;
9e5987a7
DC
4598 bma.tp = tp;
4599 bma.ip = ip;
4600 bma.total = total;
292378ed 4601 bma.datatype = 0;
2c3234d1 4602 bma.dfops = dfops;
9e5987a7
DC
4603 bma.firstblock = firstblock;
4604
9e5987a7
DC
4605 while (bno < end && n < *nmap) {
4606 inhole = eof || bma.got.br_startoff > bno;
4607 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
4608
f65306ea
DW
4609 /*
4610 * Make sure we only reflink into a hole.
4611 */
4612 if (flags & XFS_BMAPI_REMAP)
4613 ASSERT(inhole);
60b4984f
DW
4614 if (flags & XFS_BMAPI_COWFORK)
4615 ASSERT(!inhole);
f65306ea 4616
1da177e4 4617 /*
9e5987a7
DC
4618 * First, deal with the hole before the allocated space
4619 * that we found, if any.
1da177e4 4620 */
9e5987a7
DC
4621 if (inhole || wasdelay) {
4622 bma.eof = eof;
4623 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4624 bma.wasdel = wasdelay;
4625 bma.offset = bno;
4626 bma.flags = flags;
4627
1da177e4 4628 /*
9e5987a7
DC
4629 * There's a 32/64 bit type mismatch between the
4630 * allocation length request (which can be 64 bits in
4631 * length) and the bma length request, which is
4632 * xfs_extlen_t and therefore 32 bits. Hence we have to
4633 * check for 32-bit overflows and handle them here.
1da177e4 4634 */
9e5987a7
DC
4635 if (len > (xfs_filblks_t)MAXEXTLEN)
4636 bma.length = MAXEXTLEN;
4637 else
4638 bma.length = len;
4639
4640 ASSERT(len > 0);
4641 ASSERT(bma.length > 0);
4642 error = xfs_bmapi_allocate(&bma);
1da177e4
LT
4643 if (error)
4644 goto error0;
9e5987a7
DC
4645 if (bma.blkno == NULLFSBLOCK)
4646 break;
174edb0e
DW
4647
4648 /*
4649 * If this is a CoW allocation, record the data in
4650 * the refcount btree for orphan recovery.
4651 */
4652 if (whichfork == XFS_COW_FORK) {
4653 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4654 bma.blkno, bma.length);
4655 if (error)
4656 goto error0;
4657 }
1da177e4 4658 }
9e5987a7
DC
4659
4660 /* Deal with the allocated space we found. */
4661 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4662 end, n, flags);
4663
4664 /* Execute unwritten extent conversion if necessary */
4665 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
2451337d 4666 if (error == -EAGAIN)
9e5987a7
DC
4667 continue;
4668 if (error)
4669 goto error0;
4670
4671 /* update the extent map to return */
4672 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4673
4674 /*
4675 * If we're done, stop now. Stop when we've allocated
4676 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4677 * the transaction may get too big.
4678 */
4679 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
4680 break;
4681
4682 /* Else go on to the next record. */
4683 bma.prev = bma.got;
2d58f6ef
CH
4684 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4685 eof = true;
9e5987a7
DC
4686 }
4687 *nmap = n;
4688
4689 /*
4690 * Transform from btree to extents, give it cur.
4691 */
4692 if (xfs_bmap_wants_extents(ip, whichfork)) {
4693 int tmp_logflags = 0;
4694
4695 ASSERT(bma.cur);
4696 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
4697 &tmp_logflags, whichfork);
4698 bma.logflags |= tmp_logflags;
4699 if (error)
4700 goto error0;
4701 }
4702
4703 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4704 XFS_IFORK_NEXTENTS(ip, whichfork) >
4705 XFS_IFORK_MAXEXT(ip, whichfork));
4706 error = 0;
4707error0:
4708 /*
4709 * Log everything. Do this after conversion, there's no point in
4710 * logging the extent records if we've converted to btree format.
4711 */
4712 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
4713 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4714 bma.logflags &= ~xfs_ilog_fext(whichfork);
4715 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
4716 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4717 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
4718 /*
4719 * Log whatever the flags say, even if error. Otherwise we might miss
4720 * detecting a case where the data is changed, there's an error,
4721 * and it's not logged so we don't shutdown when we should.
4722 */
4723 if (bma.logflags)
4724 xfs_trans_log_inode(tp, ip, bma.logflags);
4725
4726 if (bma.cur) {
4727 if (!error) {
4728 ASSERT(*firstblock == NULLFSBLOCK ||
4729 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4730 XFS_FSB_TO_AGNO(mp,
4731 bma.cur->bc_private.b.firstblock) ||
2c3234d1 4732 (dfops->dop_low &&
9e5987a7
DC
4733 XFS_FSB_TO_AGNO(mp, *firstblock) <
4734 XFS_FSB_TO_AGNO(mp,
4735 bma.cur->bc_private.b.firstblock)));
4736 *firstblock = bma.cur->bc_private.b.firstblock;
1da177e4 4737 }
9e5987a7
DC
4738 xfs_btree_del_cursor(bma.cur,
4739 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4740 }
4741 if (!error)
4742 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4743 orig_nmap, *nmap);
4744 return error;
4745}
06d10dd9 4746
a9bd24ac
BF
4747/*
4748 * When a delalloc extent is split (e.g., due to a hole punch), the original
4749 * indlen reservation must be shared across the two new extents that are left
4750 * behind.
4751 *
4752 * Given the original reservation and the worst case indlen for the two new
4753 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
d34999c9
BF
4754 * reservation fairly across the two new extents. If necessary, steal available
4755 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4756 * ores == 1). The number of stolen blocks is returned. The availability and
4757 * subsequent accounting of stolen blocks is the responsibility of the caller.
a9bd24ac 4758 */
d34999c9 4759static xfs_filblks_t
a9bd24ac
BF
4760xfs_bmap_split_indlen(
4761 xfs_filblks_t ores, /* original res. */
4762 xfs_filblks_t *indlen1, /* ext1 worst indlen */
d34999c9
BF
4763 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4764 xfs_filblks_t avail) /* stealable blocks */
a9bd24ac
BF
4765{
4766 xfs_filblks_t len1 = *indlen1;
4767 xfs_filblks_t len2 = *indlen2;
4768 xfs_filblks_t nres = len1 + len2; /* new total res. */
d34999c9
BF
4769 xfs_filblks_t stolen = 0;
4770
4771 /*
4772 * Steal as many blocks as we can to try and satisfy the worst case
4773 * indlen for both new extents.
4774 */
4775 while (nres > ores && avail) {
4776 nres--;
4777 avail--;
4778 stolen++;
4779 }
a9bd24ac
BF
4780
4781 /*
d34999c9
BF
4782 * The only blocks available are those reserved for the original
4783 * extent and what we can steal from the extent being removed.
4784 * If this still isn't enough to satisfy the combined
4785 * requirements for the two new extents, skim blocks off of each
4786 * of the new reservations until they match what is available.
a9bd24ac
BF
4787 */
4788 while (nres > ores) {
4789 if (len1) {
4790 len1--;
4791 nres--;
4792 }
4793 if (nres == ores)
4794 break;
4795 if (len2) {
4796 len2--;
4797 nres--;
4798 }
4799 }
4800
4801 *indlen1 = len1;
4802 *indlen2 = len2;
d34999c9
BF
4803
4804 return stolen;
a9bd24ac
BF
4805}
4806
fa5c836c
CH
4807int
4808xfs_bmap_del_extent_delay(
4809 struct xfs_inode *ip,
4810 int whichfork,
4811 xfs_extnum_t *idx,
4812 struct xfs_bmbt_irec *got,
4813 struct xfs_bmbt_irec *del)
4814{
4815 struct xfs_mount *mp = ip->i_mount;
4816 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4817 struct xfs_bmbt_irec new;
4818 int64_t da_old, da_new, da_diff = 0;
4819 xfs_fileoff_t del_endoff, got_endoff;
4820 xfs_filblks_t got_indlen, new_indlen, stolen;
4821 int error = 0, state = 0;
4822 bool isrt;
4823
4824 XFS_STATS_INC(mp, xs_del_exlist);
4825
4826 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4827 del_endoff = del->br_startoff + del->br_blockcount;
4828 got_endoff = got->br_startoff + got->br_blockcount;
4829 da_old = startblockval(got->br_startblock);
4830 da_new = 0;
4831
4832 ASSERT(*idx >= 0);
5d829300 4833 ASSERT(*idx <= xfs_iext_count(ifp));
fa5c836c
CH
4834 ASSERT(del->br_blockcount > 0);
4835 ASSERT(got->br_startoff <= del->br_startoff);
4836 ASSERT(got_endoff >= del_endoff);
4837
4838 if (isrt) {
4839 int64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
4840
4841 do_div(rtexts, mp->m_sb.sb_rextsize);
4842 xfs_mod_frextents(mp, rtexts);
4843 }
4844
4845 /*
4846 * Update the inode delalloc counter now and wait to update the
4847 * sb counters as we might have to borrow some blocks for the
4848 * indirect block accounting.
4849 */
4fd29ec4
DW
4850 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4851 -((long)del->br_blockcount), 0,
fa5c836c 4852 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4fd29ec4
DW
4853 if (error)
4854 return error;
fa5c836c
CH
4855 ip->i_delayed_blks -= del->br_blockcount;
4856
4857 if (whichfork == XFS_COW_FORK)
4858 state |= BMAP_COWFORK;
4859
4860 if (got->br_startoff == del->br_startoff)
4861 state |= BMAP_LEFT_CONTIG;
4862 if (got_endoff == del_endoff)
4863 state |= BMAP_RIGHT_CONTIG;
4864
4865 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4866 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4867 /*
4868 * Matches the whole extent. Delete the entry.
4869 */
4870 xfs_iext_remove(ip, *idx, 1, state);
4871 --*idx;
4872 break;
4873 case BMAP_LEFT_CONTIG:
4874 /*
4875 * Deleting the first part of the extent.
4876 */
4877 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4878 got->br_startoff = del_endoff;
4879 got->br_blockcount -= del->br_blockcount;
4880 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4881 got->br_blockcount), da_old);
4882 got->br_startblock = nullstartblock((int)da_new);
4883 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4884 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4885 break;
4886 case BMAP_RIGHT_CONTIG:
4887 /*
4888 * Deleting the last part of the extent.
4889 */
4890 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4891 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4892 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4893 got->br_blockcount), da_old);
4894 got->br_startblock = nullstartblock((int)da_new);
4895 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4896 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4897 break;
4898 case 0:
4899 /*
4900 * Deleting the middle of the extent.
4901 *
4902 * Distribute the original indlen reservation across the two new
4903 * extents. Steal blocks from the deleted extent if necessary.
4904 * Stealing blocks simply fudges the fdblocks accounting below.
4905 * Warn if either of the new indlen reservations is zero as this
4906 * can lead to delalloc problems.
4907 */
4908 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4909
4910 got->br_blockcount = del->br_startoff - got->br_startoff;
4911 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4912
4913 new.br_blockcount = got_endoff - del_endoff;
4914 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4915
4916 WARN_ON_ONCE(!got_indlen || !new_indlen);
4917 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4918 del->br_blockcount);
4919
4920 got->br_startblock = nullstartblock((int)got_indlen);
4921 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4922 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4923
4924 new.br_startoff = del_endoff;
4925 new.br_state = got->br_state;
4926 new.br_startblock = nullstartblock((int)new_indlen);
4927
4928 ++*idx;
4929 xfs_iext_insert(ip, *idx, 1, &new, state);
4930
4931 da_new = got_indlen + new_indlen - stolen;
4932 del->br_blockcount -= stolen;
4933 break;
4934 }
4935
4936 ASSERT(da_old >= da_new);
4937 da_diff = da_old - da_new;
4938 if (!isrt)
4939 da_diff += del->br_blockcount;
4940 if (da_diff)
4941 xfs_mod_fdblocks(mp, da_diff, false);
4942 return error;
4943}
4944
4945void
4946xfs_bmap_del_extent_cow(
4947 struct xfs_inode *ip,
4948 xfs_extnum_t *idx,
4949 struct xfs_bmbt_irec *got,
4950 struct xfs_bmbt_irec *del)
4951{
4952 struct xfs_mount *mp = ip->i_mount;
4953 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4954 struct xfs_bmbt_irec new;
4955 xfs_fileoff_t del_endoff, got_endoff;
4956 int state = BMAP_COWFORK;
4957
4958 XFS_STATS_INC(mp, xs_del_exlist);
4959
4960 del_endoff = del->br_startoff + del->br_blockcount;
4961 got_endoff = got->br_startoff + got->br_blockcount;
4962
4963 ASSERT(*idx >= 0);
5d829300 4964 ASSERT(*idx <= xfs_iext_count(ifp));
fa5c836c
CH
4965 ASSERT(del->br_blockcount > 0);
4966 ASSERT(got->br_startoff <= del->br_startoff);
4967 ASSERT(got_endoff >= del_endoff);
4968 ASSERT(!isnullstartblock(got->br_startblock));
4969
4970 if (got->br_startoff == del->br_startoff)
4971 state |= BMAP_LEFT_CONTIG;
4972 if (got_endoff == del_endoff)
4973 state |= BMAP_RIGHT_CONTIG;
4974
4975 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4976 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4977 /*
4978 * Matches the whole extent. Delete the entry.
4979 */
4980 xfs_iext_remove(ip, *idx, 1, state);
4981 --*idx;
4982 break;
4983 case BMAP_LEFT_CONTIG:
4984 /*
4985 * Deleting the first part of the extent.
4986 */
4987 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4988 got->br_startoff = del_endoff;
4989 got->br_blockcount -= del->br_blockcount;
4990 got->br_startblock = del->br_startblock + del->br_blockcount;
4991 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
4992 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4993 break;
4994 case BMAP_RIGHT_CONTIG:
4995 /*
4996 * Deleting the last part of the extent.
4997 */
4998 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4999 got->br_blockcount -= del->br_blockcount;
5000 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5001 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5002 break;
5003 case 0:
5004 /*
5005 * Deleting the middle of the extent.
5006 */
5007 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5008 got->br_blockcount = del->br_startoff - got->br_startoff;
5009 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, *idx), got);
5010 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5011
5012 new.br_startoff = del_endoff;
5013 new.br_blockcount = got_endoff - del_endoff;
5014 new.br_state = got->br_state;
5015 new.br_startblock = del->br_startblock + del->br_blockcount;
5016
5017 ++*idx;
5018 xfs_iext_insert(ip, *idx, 1, &new, state);
5019 break;
5020 }
5021}
5022
9e5987a7
DC
5023/*
5024 * Called by xfs_bmapi to update file extent records and the btree
5025 * after removing space (or undoing a delayed allocation).
5026 */
5027STATIC int /* error */
5028xfs_bmap_del_extent(
5029 xfs_inode_t *ip, /* incore inode pointer */
5030 xfs_trans_t *tp, /* current transaction pointer */
5031 xfs_extnum_t *idx, /* extent number to update/delete */
2c3234d1 5032 struct xfs_defer_ops *dfops, /* list of extents to be freed */
9e5987a7
DC
5033 xfs_btree_cur_t *cur, /* if null, not a btree */
5034 xfs_bmbt_irec_t *del, /* data to remove from extents */
5035 int *logflagsp, /* inode logging flags */
4847acf8
DW
5036 int whichfork, /* data or attr fork */
5037 int bflags) /* bmapi flags */
9e5987a7
DC
5038{
5039 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5040 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5041 xfs_fsblock_t del_endblock=0; /* first block past del */
5042 xfs_fileoff_t del_endoff; /* first offset past del */
5043 int delay; /* current block is delayed allocated */
5044 int do_fx; /* free extent at end of routine */
5045 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5046 int error; /* error return value */
5047 int flags; /* inode logging flags */
5048 xfs_bmbt_irec_t got; /* current extent entry */
5049 xfs_fileoff_t got_endoff; /* first offset past got */
5050 int i; /* temp state */
5051 xfs_ifork_t *ifp; /* inode fork pointer */
5052 xfs_mount_t *mp; /* mount structure */
5053 xfs_filblks_t nblks; /* quota/sb block count */
5054 xfs_bmbt_irec_t new; /* new record to be inserted */
5055 /* REFERENCED */
5056 uint qfield; /* quota field to update */
5057 xfs_filblks_t temp; /* for indirect length calculations */
5058 xfs_filblks_t temp2; /* for indirect length calculations */
5059 int state = 0;
5060
ff6d6af2
BD
5061 mp = ip->i_mount;
5062 XFS_STATS_INC(mp, xs_del_exlist);
9e5987a7
DC
5063
5064 if (whichfork == XFS_ATTR_FORK)
5065 state |= BMAP_ATTRFORK;
3993baeb
DW
5066 else if (whichfork == XFS_COW_FORK)
5067 state |= BMAP_COWFORK;
9e5987a7 5068
9e5987a7 5069 ifp = XFS_IFORK_PTR(ip, whichfork);
5d829300 5070 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
9e5987a7
DC
5071 ASSERT(del->br_blockcount > 0);
5072 ep = xfs_iext_get_ext(ifp, *idx);
5073 xfs_bmbt_get_all(ep, &got);
5074 ASSERT(got.br_startoff <= del->br_startoff);
5075 del_endoff = del->br_startoff + del->br_blockcount;
5076 got_endoff = got.br_startoff + got.br_blockcount;
5077 ASSERT(got_endoff >= del_endoff);
5078 delay = isnullstartblock(got.br_startblock);
5079 ASSERT(isnullstartblock(del->br_startblock) == delay);
5080 flags = 0;
5081 qfield = 0;
5082 error = 0;
5083 /*
5084 * If deleting a real allocation, must free up the disk space.
5085 */
5086 if (!delay) {
5087 flags = XFS_ILOG_CORE;
5088 /*
5089 * Realtime allocation. Free it and record di_nblocks update.
5090 */
5091 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5092 xfs_fsblock_t bno;
5093 xfs_filblks_t len;
5094
5095 ASSERT(do_mod(del->br_blockcount,
5096 mp->m_sb.sb_rextsize) == 0);
5097 ASSERT(do_mod(del->br_startblock,
5098 mp->m_sb.sb_rextsize) == 0);
5099 bno = del->br_startblock;
5100 len = del->br_blockcount;
5101 do_div(bno, mp->m_sb.sb_rextsize);
5102 do_div(len, mp->m_sb.sb_rextsize);
5103 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5104 if (error)
5105 goto done;
5106 do_fx = 0;
5107 nblks = len * mp->m_sb.sb_rextsize;
5108 qfield = XFS_TRANS_DQ_RTBCOUNT;
5109 }
1da177e4 5110 /*
9e5987a7 5111 * Ordinary allocation.
1da177e4 5112 */
9e5987a7
DC
5113 else {
5114 do_fx = 1;
5115 nblks = del->br_blockcount;
5116 qfield = XFS_TRANS_DQ_BCOUNT;
1da177e4 5117 }
1da177e4 5118 /*
9e5987a7 5119 * Set up del_endblock and cur for later.
1da177e4 5120 */
9e5987a7
DC
5121 del_endblock = del->br_startblock + del->br_blockcount;
5122 if (cur) {
5123 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5124 got.br_startblock, got.br_blockcount,
5125 &i)))
5126 goto done;
c29aad41 5127 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
1da177e4 5128 }
9e5987a7
DC
5129 da_old = da_new = 0;
5130 } else {
5131 da_old = startblockval(got.br_startblock);
5132 da_new = 0;
5133 nblks = 0;
5134 do_fx = 0;
1da177e4 5135 }
340785cc 5136
1da177e4 5137 /*
9e5987a7
DC
5138 * Set flag value to use in switch statement.
5139 * Left-contig is 2, right-contig is 1.
1da177e4 5140 */
9e5987a7
DC
5141 switch (((got.br_startoff == del->br_startoff) << 1) |
5142 (got_endoff == del_endoff)) {
5143 case 3:
5144 /*
5145 * Matches the whole extent. Delete the entry.
5146 */
4862cfe8 5147 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
9e5987a7
DC
5148 xfs_iext_remove(ip, *idx, 1,
5149 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5150 --*idx;
5151 if (delay)
5152 break;
5153
5154 XFS_IFORK_NEXT_SET(ip, whichfork,
5155 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5156 flags |= XFS_ILOG_CORE;
5157 if (!cur) {
5158 flags |= xfs_ilog_fext(whichfork);
5159 break;
5160 }
5161 if ((error = xfs_btree_delete(cur, &i)))
5162 goto done;
c29aad41 5163 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
5164 break;
5165
5166 case 2:
5167 /*
5168 * Deleting the first part of the extent.
5169 */
5170 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5171 xfs_bmbt_set_startoff(ep, del_endoff);
5172 temp = got.br_blockcount - del->br_blockcount;
5173 xfs_bmbt_set_blockcount(ep, temp);
5174 if (delay) {
5175 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5176 da_old);
5177 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5178 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5179 da_new = temp;
5180 break;
5181 }
5182 xfs_bmbt_set_startblock(ep, del_endblock);
5183 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5184 if (!cur) {
5185 flags |= xfs_ilog_fext(whichfork);
5186 break;
5187 }
5188 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5189 got.br_blockcount - del->br_blockcount,
5190 got.br_state)))
5191 goto done;
5192 break;
5193
5194 case 1:
5195 /*
5196 * Deleting the last part of the extent.
5197 */
5198 temp = got.br_blockcount - del->br_blockcount;
5199 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5200 xfs_bmbt_set_blockcount(ep, temp);
5201 if (delay) {
5202 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5203 da_old);
5204 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5205 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5206 da_new = temp;
5207 break;
5208 }
5209 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5210 if (!cur) {
5211 flags |= xfs_ilog_fext(whichfork);
5212 break;
5213 }
5214 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5215 got.br_startblock,
5216 got.br_blockcount - del->br_blockcount,
5217 got.br_state)))
5218 goto done;
5219 break;
5220
5221 case 0:
5222 /*
5223 * Deleting the middle of the extent.
5224 */
5225 temp = del->br_startoff - got.br_startoff;
5226 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5227 xfs_bmbt_set_blockcount(ep, temp);
5228 new.br_startoff = del_endoff;
5229 temp2 = got_endoff - del_endoff;
5230 new.br_blockcount = temp2;
5231 new.br_state = got.br_state;
5232 if (!delay) {
5233 new.br_startblock = del_endblock;
5234 flags |= XFS_ILOG_CORE;
5235 if (cur) {
5236 if ((error = xfs_bmbt_update(cur,
5237 got.br_startoff,
5238 got.br_startblock, temp,
5239 got.br_state)))
5240 goto done;
5241 if ((error = xfs_btree_increment(cur, 0, &i)))
5242 goto done;
5243 cur->bc_rec.b = new;
5244 error = xfs_btree_insert(cur, &i);
2451337d 5245 if (error && error != -ENOSPC)
9e5987a7
DC
5246 goto done;
5247 /*
5248 * If get no-space back from btree insert,
5249 * it tried a split, and we have a zero
5250 * block reservation.
5251 * Fix up our state and return the error.
5252 */
2451337d 5253 if (error == -ENOSPC) {
9e5987a7
DC
5254 /*
5255 * Reset the cursor, don't trust
5256 * it after any insert operation.
5257 */
5258 if ((error = xfs_bmbt_lookup_eq(cur,
5259 got.br_startoff,
5260 got.br_startblock,
5261 temp, &i)))
5262 goto done;
c29aad41
ES
5263 XFS_WANT_CORRUPTED_GOTO(mp,
5264 i == 1, done);
9e5987a7
DC
5265 /*
5266 * Update the btree record back
5267 * to the original value.
5268 */
5269 if ((error = xfs_bmbt_update(cur,
5270 got.br_startoff,
5271 got.br_startblock,
5272 got.br_blockcount,
5273 got.br_state)))
5274 goto done;
5275 /*
5276 * Reset the extent record back
5277 * to the original value.
5278 */
5279 xfs_bmbt_set_blockcount(ep,
5280 got.br_blockcount);
5281 flags = 0;
2451337d 5282 error = -ENOSPC;
9e5987a7
DC
5283 goto done;
5284 }
c29aad41 5285 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
9e5987a7
DC
5286 } else
5287 flags |= xfs_ilog_fext(whichfork);
5288 XFS_IFORK_NEXT_SET(ip, whichfork,
5289 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5290 } else {
d34999c9 5291 xfs_filblks_t stolen;
9e5987a7 5292 ASSERT(whichfork == XFS_DATA_FORK);
a9bd24ac
BF
5293
5294 /*
5295 * Distribute the original indlen reservation across the
d34999c9
BF
5296 * two new extents. Steal blocks from the deleted extent
5297 * if necessary. Stealing blocks simply fudges the
5298 * fdblocks accounting in xfs_bunmapi().
a9bd24ac
BF
5299 */
5300 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5301 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
d34999c9
BF
5302 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5303 del->br_blockcount);
5304 da_new = temp + temp2 - stolen;
5305 del->br_blockcount -= stolen;
a9bd24ac
BF
5306
5307 /*
d34999c9
BF
5308 * Set the reservation for each extent. Warn if either
5309 * is zero as this can lead to delalloc problems.
a9bd24ac 5310 */
d34999c9 5311 WARN_ON_ONCE(!temp || !temp2);
9e5987a7 5312 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
9e5987a7 5313 new.br_startblock = nullstartblock((int)temp2);
9e5987a7
DC
5314 }
5315 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5316 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5317 ++*idx;
5318 break;
1da177e4 5319 }
9c194644
DW
5320
5321 /* remove reverse mapping */
5322 if (!delay) {
5323 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5324 if (error)
5325 goto done;
5326 }
5327
1da177e4 5328 /*
9e5987a7 5329 * If we need to, add to list of extents to delete.
1da177e4 5330 */
4847acf8 5331 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
62aab20f
DW
5332 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5333 error = xfs_refcount_decrease_extent(mp, dfops, del);
5334 if (error)
5335 goto done;
5336 } else
5337 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5338 del->br_blockcount, NULL);
5339 }
5340
1da177e4 5341 /*
9e5987a7 5342 * Adjust inode # blocks in the file.
1da177e4 5343 */
9e5987a7
DC
5344 if (nblks)
5345 ip->i_d.di_nblocks -= nblks;
1da177e4 5346 /*
9e5987a7 5347 * Adjust quota data.
1da177e4 5348 */
4847acf8 5349 if (qfield && !(bflags & XFS_BMAPI_REMAP))
9e5987a7
DC
5350 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5351
5352 /*
5353 * Account for change in delayed indirect blocks.
5354 * Nothing to do for disk quota accounting here.
5355 */
5356 ASSERT(da_old >= da_new);
0d485ada
DC
5357 if (da_old > da_new)
5358 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
9e5987a7
DC
5359done:
5360 *logflagsp = flags;
1da177e4
LT
5361 return error;
5362}
5363
3bacbcd8 5364/*
9e5987a7
DC
5365 * Unmap (remove) blocks from a file.
5366 * If nexts is nonzero then the number of extents to remove is limited to
5367 * that value. If not all extents in the block range can be removed then
5368 * *done is set.
3bacbcd8 5369 */
9e5987a7 5370int /* error */
4453593b 5371__xfs_bunmapi(
9e5987a7
DC
5372 xfs_trans_t *tp, /* transaction pointer */
5373 struct xfs_inode *ip, /* incore inode */
5374 xfs_fileoff_t bno, /* starting offset to unmap */
4453593b 5375 xfs_filblks_t *rlen, /* i/o: amount remaining */
9e5987a7
DC
5376 int flags, /* misc flags */
5377 xfs_extnum_t nexts, /* number of extents max */
5378 xfs_fsblock_t *firstblock, /* first allocated block
5379 controls a.g. for allocs */
4453593b 5380 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
3bacbcd8 5381{
9e5987a7
DC
5382 xfs_btree_cur_t *cur; /* bmap btree cursor */
5383 xfs_bmbt_irec_t del; /* extent being deleted */
9e5987a7
DC
5384 int error; /* error return value */
5385 xfs_extnum_t extno; /* extent number in list */
5386 xfs_bmbt_irec_t got; /* current extent record */
5af317c9 5387 xfs_ifork_t *ifp; /* inode fork pointer */
9e5987a7
DC
5388 int isrt; /* freeing in rt area */
5389 xfs_extnum_t lastx; /* last extent index used */
5390 int logflags; /* transaction logging flags */
5391 xfs_extlen_t mod; /* rt extent offset */
5392 xfs_mount_t *mp; /* mount structure */
9e5987a7
DC
5393 xfs_fileoff_t start; /* first file offset deleted */
5394 int tmp_logflags; /* partial logging flags */
5395 int wasdel; /* was a delayed alloc extent */
5396 int whichfork; /* data or attribute fork */
5397 xfs_fsblock_t sum;
4453593b 5398 xfs_filblks_t len = *rlen; /* length to unmap in file */
1da177e4 5399
9e5987a7 5400 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
1da177e4 5401
3993baeb
DW
5402 whichfork = xfs_bmapi_whichfork(flags);
5403 ASSERT(whichfork != XFS_COW_FORK);
9e5987a7
DC
5404 ifp = XFS_IFORK_PTR(ip, whichfork);
5405 if (unlikely(
5406 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5407 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5408 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5409 ip->i_mount);
2451337d 5410 return -EFSCORRUPTED;
1da177e4 5411 }
9e5987a7
DC
5412 mp = ip->i_mount;
5413 if (XFS_FORCED_SHUTDOWN(mp))
2451337d 5414 return -EIO;
1da177e4 5415
eef334e5 5416 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
9e5987a7
DC
5417 ASSERT(len > 0);
5418 ASSERT(nexts >= 0);
1da177e4 5419
9e5987a7
DC
5420 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5421 (error = xfs_iread_extents(tp, ip, whichfork)))
5422 return error;
5d829300 5423 if (xfs_iext_count(ifp) == 0) {
4453593b 5424 *rlen = 0;
9e5987a7
DC
5425 return 0;
5426 }
ff6d6af2 5427 XFS_STATS_INC(mp, xs_blk_unmap);
9e5987a7
DC
5428 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5429 start = bno;
5430 bno = start + len - 1;
1da177e4 5431
9e5987a7
DC
5432 /*
5433 * Check to see if the given block number is past the end of the
5434 * file, back up to the last block if so...
5435 */
7efc7945
CH
5436 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5437 ASSERT(lastx > 0);
5438 xfs_iext_get_extent(ifp, --lastx, &got);
9e5987a7
DC
5439 bno = got.br_startoff + got.br_blockcount - 1;
5440 }
7efc7945 5441
9e5987a7
DC
5442 logflags = 0;
5443 if (ifp->if_flags & XFS_IFBROOT) {
5444 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5445 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5446 cur->bc_private.b.firstblock = *firstblock;
2c3234d1 5447 cur->bc_private.b.dfops = dfops;
9e5987a7
DC
5448 cur->bc_private.b.flags = 0;
5449 } else
5450 cur = NULL;
5451
5452 if (isrt) {
5453 /*
5454 * Synchronize by locking the bitmap inode.
5455 */
f4a0660d 5456 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
9e5987a7 5457 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
f4a0660d
DW
5458 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5459 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
9e5987a7 5460 }
58e20770 5461
9e5987a7
DC
5462 extno = 0;
5463 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5464 (nexts == 0 || extno < nexts)) {
5465 /*
5466 * Is the found extent after a hole in which bno lives?
5467 * Just back up to the previous extent, if so.
5468 */
5469 if (got.br_startoff > bno) {
5470 if (--lastx < 0)
5471 break;
7efc7945 5472 xfs_iext_get_extent(ifp, lastx, &got);
9e5987a7
DC
5473 }
5474 /*
5475 * Is the last block of this extent before the range
5476 * we're supposed to delete? If so, we're done.
5477 */
5478 bno = XFS_FILEOFF_MIN(bno,
5479 got.br_startoff + got.br_blockcount - 1);
5480 if (bno < start)
5481 break;
5482 /*
5483 * Then deal with the (possibly delayed) allocated space
5484 * we found.
5485 */
9e5987a7
DC
5486 del = got;
5487 wasdel = isnullstartblock(del.br_startblock);
5488 if (got.br_startoff < start) {
5489 del.br_startoff = start;
5490 del.br_blockcount -= start - got.br_startoff;
5491 if (!wasdel)
5492 del.br_startblock += start - got.br_startoff;
5493 }
5494 if (del.br_startoff + del.br_blockcount > bno + 1)
5495 del.br_blockcount = bno + 1 - del.br_startoff;
5496 sum = del.br_startblock + del.br_blockcount;
5497 if (isrt &&
5498 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
58e20770 5499 /*
9e5987a7
DC
5500 * Realtime extent not lined up at the end.
5501 * The extent could have been split into written
5502 * and unwritten pieces, or we could just be
5503 * unmapping part of it. But we can't really
5504 * get rid of part of a realtime extent.
58e20770 5505 */
9e5987a7
DC
5506 if (del.br_state == XFS_EXT_UNWRITTEN ||
5507 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5508 /*
5509 * This piece is unwritten, or we're not
5510 * using unwritten extents. Skip over it.
5511 */
5512 ASSERT(bno >= mod);
5513 bno -= mod > del.br_blockcount ?
5514 del.br_blockcount : mod;
5515 if (bno < got.br_startoff) {
5516 if (--lastx >= 0)
5517 xfs_bmbt_get_all(xfs_iext_get_ext(
5518 ifp, lastx), &got);
5519 }
5520 continue;
1da177e4 5521 }
9af25465 5522 /*
9e5987a7
DC
5523 * It's written, turn it unwritten.
5524 * This is better than zeroing it.
9af25465 5525 */
9e5987a7 5526 ASSERT(del.br_state == XFS_EXT_NORM);
a7e5d03b 5527 ASSERT(tp->t_blk_res > 0);
9e5987a7
DC
5528 /*
5529 * If this spans a realtime extent boundary,
5530 * chop it back to the start of the one we end at.
5531 */
5532 if (del.br_blockcount > mod) {
5533 del.br_startoff += del.br_blockcount - mod;
5534 del.br_startblock += del.br_blockcount - mod;
5535 del.br_blockcount = mod;
5536 }
5537 del.br_state = XFS_EXT_UNWRITTEN;
5538 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
2c3234d1 5539 &lastx, &cur, &del, firstblock, dfops,
9e5987a7
DC
5540 &logflags);
5541 if (error)
5542 goto error0;
5543 goto nodelete;
5544 }
5545 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5546 /*
5547 * Realtime extent is lined up at the end but not
5548 * at the front. We'll get rid of full extents if
5549 * we can.
5550 */
5551 mod = mp->m_sb.sb_rextsize - mod;
5552 if (del.br_blockcount > mod) {
5553 del.br_blockcount -= mod;
5554 del.br_startoff += mod;
5555 del.br_startblock += mod;
5556 } else if ((del.br_startoff == start &&
5557 (del.br_state == XFS_EXT_UNWRITTEN ||
a7e5d03b 5558 tp->t_blk_res == 0)) ||
9e5987a7
DC
5559 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5560 /*
5561 * Can't make it unwritten. There isn't
5562 * a full extent here so just skip it.
5563 */
5564 ASSERT(bno >= del.br_blockcount);
5565 bno -= del.br_blockcount;
7efc7945
CH
5566 if (got.br_startoff > bno && --lastx >= 0)
5567 xfs_iext_get_extent(ifp, lastx, &got);
9af25465 5568 continue;
9e5987a7 5569 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
7efc7945
CH
5570 struct xfs_bmbt_irec prev;
5571
9e5987a7
DC
5572 /*
5573 * This one is already unwritten.
5574 * It must have a written left neighbor.
5575 * Unwrite the killed part of that one and
5576 * try again.
5577 */
5578 ASSERT(lastx > 0);
7efc7945 5579 xfs_iext_get_extent(ifp, lastx - 1, &prev);
9e5987a7
DC
5580 ASSERT(prev.br_state == XFS_EXT_NORM);
5581 ASSERT(!isnullstartblock(prev.br_startblock));
5582 ASSERT(del.br_startblock ==
5583 prev.br_startblock + prev.br_blockcount);
5584 if (prev.br_startoff < start) {
5585 mod = start - prev.br_startoff;
5586 prev.br_blockcount -= mod;
5587 prev.br_startblock += mod;
5588 prev.br_startoff = start;
5589 }
5590 prev.br_state = XFS_EXT_UNWRITTEN;
5591 lastx--;
5592 error = xfs_bmap_add_extent_unwritten_real(tp,
5593 ip, &lastx, &cur, &prev,
2c3234d1 5594 firstblock, dfops, &logflags);
9e5987a7
DC
5595 if (error)
5596 goto error0;
5597 goto nodelete;
5598 } else {
5599 ASSERT(del.br_state == XFS_EXT_NORM);
5600 del.br_state = XFS_EXT_UNWRITTEN;
5601 error = xfs_bmap_add_extent_unwritten_real(tp,
5602 ip, &lastx, &cur, &del,
2c3234d1 5603 firstblock, dfops, &logflags);
9e5987a7
DC
5604 if (error)
5605 goto error0;
5606 goto nodelete;
9af25465 5607 }
1da177e4 5608 }
1da177e4 5609
9e5987a7
DC
5610 /*
5611 * If it's the case where the directory code is running
5612 * with no block reservation, and the deleted block is in
5613 * the middle of its extent, and the resulting insert
5614 * of an extent would cause transformation to btree format,
5615 * then reject it. The calling code will then swap
5616 * blocks around instead.
5617 * We have to do this now, rather than waiting for the
5618 * conversion to btree format, since the transaction
5619 * will be dirty.
5620 */
a7e5d03b 5621 if (!wasdel && tp->t_blk_res == 0 &&
9e5987a7
DC
5622 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5623 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5624 XFS_IFORK_MAXEXT(ip, whichfork) &&
5625 del.br_startoff > got.br_startoff &&
5626 del.br_startoff + del.br_blockcount <
5627 got.br_startoff + got.br_blockcount) {
2451337d 5628 error = -ENOSPC;
9e5987a7 5629 goto error0;
1da177e4 5630 }
b2706a05
BF
5631
5632 /*
5633 * Unreserve quota and update realtime free space, if
5634 * appropriate. If delayed allocation, update the inode delalloc
5635 * counter now and wait to update the sb counters as
5636 * xfs_bmap_del_extent() might need to borrow some blocks.
5637 */
5638 if (wasdel) {
5639 ASSERT(startblockval(del.br_startblock) > 0);
5640 if (isrt) {
5641 xfs_filblks_t rtexts;
5642
5643 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5644 do_div(rtexts, mp->m_sb.sb_rextsize);
5645 xfs_mod_frextents(mp, (int64_t)rtexts);
5646 (void)xfs_trans_reserve_quota_nblks(NULL,
5647 ip, -((long)del.br_blockcount), 0,
5648 XFS_QMOPT_RES_RTBLKS);
5649 } else {
5650 (void)xfs_trans_reserve_quota_nblks(NULL,
5651 ip, -((long)del.br_blockcount), 0,
5652 XFS_QMOPT_RES_REGBLKS);
5653 }
5654 ip->i_delayed_blks -= del.br_blockcount;
5655 if (cur)
5656 cur->bc_private.b.flags |=
5657 XFS_BTCUR_BPRV_WASDEL;
5658 } else if (cur)
5659 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5660
2c3234d1 5661 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
4847acf8 5662 &tmp_logflags, whichfork, flags);
9e5987a7
DC
5663 logflags |= tmp_logflags;
5664 if (error)
5665 goto error0;
b2706a05
BF
5666
5667 if (!isrt && wasdel)
5668 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5669
9e5987a7
DC
5670 bno = del.br_startoff - 1;
5671nodelete:
1da177e4 5672 /*
9e5987a7 5673 * If not done go on to the next (previous) record.
1da177e4 5674 */
9e5987a7
DC
5675 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5676 if (lastx >= 0) {
7efc7945
CH
5677 xfs_iext_get_extent(ifp, lastx, &got);
5678 if (got.br_startoff > bno && --lastx >= 0)
5679 xfs_iext_get_extent(ifp, lastx, &got);
1da177e4 5680 }
9e5987a7 5681 extno++;
1da177e4
LT
5682 }
5683 }
4453593b
DW
5684 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5685 *rlen = 0;
5686 else
5687 *rlen = bno - start + 1;
576039cf 5688
1da177e4 5689 /*
9e5987a7 5690 * Convert to a btree if necessary.
1da177e4 5691 */
9e5987a7
DC
5692 if (xfs_bmap_needs_btree(ip, whichfork)) {
5693 ASSERT(cur == NULL);
2c3234d1 5694 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
9e5987a7
DC
5695 &cur, 0, &tmp_logflags, whichfork);
5696 logflags |= tmp_logflags;
5697 if (error)
5698 goto error0;
1da177e4 5699 }
1da177e4 5700 /*
9e5987a7 5701 * transform from btree to extents, give it cur
1da177e4 5702 */
9e5987a7
DC
5703 else if (xfs_bmap_wants_extents(ip, whichfork)) {
5704 ASSERT(cur != NULL);
5705 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5706 whichfork);
5707 logflags |= tmp_logflags;
5708 if (error)
5709 goto error0;
5710 }
1da177e4 5711 /*
9e5987a7 5712 * transform from extents to local?
1da177e4 5713 */
9e5987a7
DC
5714 error = 0;
5715error0:
5716 /*
5717 * Log everything. Do this after conversion, there's no point in
5718 * logging the extent records if we've converted to btree format.
5719 */
5720 if ((logflags & xfs_ilog_fext(whichfork)) &&
5721 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5722 logflags &= ~xfs_ilog_fext(whichfork);
5723 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5724 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5725 logflags &= ~xfs_ilog_fbroot(whichfork);
5726 /*
5727 * Log inode even in the error case, if the transaction
5728 * is dirty we'll need to shut down the filesystem.
5729 */
5730 if (logflags)
5731 xfs_trans_log_inode(tp, ip, logflags);
5732 if (cur) {
5733 if (!error) {
5734 *firstblock = cur->bc_private.b.firstblock;
5735 cur->bc_private.b.allocated = 0;
5736 }
5737 xfs_btree_del_cursor(cur,
5738 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5739 }
5740 return error;
5741}
e1d8fb88 5742
4453593b
DW
5743/* Unmap a range of a file. */
5744int
5745xfs_bunmapi(
5746 xfs_trans_t *tp,
5747 struct xfs_inode *ip,
5748 xfs_fileoff_t bno,
5749 xfs_filblks_t len,
5750 int flags,
5751 xfs_extnum_t nexts,
5752 xfs_fsblock_t *firstblock,
5753 struct xfs_defer_ops *dfops,
5754 int *done)
5755{
5756 int error;
5757
5758 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5759 dfops);
5760 *done = (len == 0);
5761 return error;
5762}
5763
ddb19e31
BF
5764/*
5765 * Determine whether an extent shift can be accomplished by a merge with the
5766 * extent that precedes the target hole of the shift.
5767 */
5768STATIC bool
5769xfs_bmse_can_merge(
5770 struct xfs_bmbt_irec *left, /* preceding extent */
5771 struct xfs_bmbt_irec *got, /* current extent to shift */
5772 xfs_fileoff_t shift) /* shift fsb */
5773{
5774 xfs_fileoff_t startoff;
5775
5776 startoff = got->br_startoff - shift;
5777
5778 /*
5779 * The extent, once shifted, must be adjacent in-file and on-disk with
5780 * the preceding extent.
5781 */
5782 if ((left->br_startoff + left->br_blockcount != startoff) ||
5783 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5784 (left->br_state != got->br_state) ||
5785 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5786 return false;
5787
5788 return true;
5789}
5790
5791/*
5792 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5793 * hole in the file. If an extent shift would result in the extent being fully
5794 * adjacent to the extent that currently precedes the hole, we can merge with
5795 * the preceding extent rather than do the shift.
5796 *
5797 * This function assumes the caller has verified a shift-by-merge is possible
5798 * with the provided extents via xfs_bmse_can_merge().
5799 */
5800STATIC int
5801xfs_bmse_merge(
5802 struct xfs_inode *ip,
5803 int whichfork,
5804 xfs_fileoff_t shift, /* shift fsb */
5805 int current_ext, /* idx of gotp */
5806 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5807 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5808 struct xfs_btree_cur *cur,
5809 int *logflags) /* output */
5810{
ddb19e31
BF
5811 struct xfs_bmbt_irec got;
5812 struct xfs_bmbt_irec left;
5813 xfs_filblks_t blockcount;
5814 int error, i;
5fb5aeee 5815 struct xfs_mount *mp = ip->i_mount;
ddb19e31 5816
ddb19e31
BF
5817 xfs_bmbt_get_all(gotp, &got);
5818 xfs_bmbt_get_all(leftp, &left);
5819 blockcount = left.br_blockcount + got.br_blockcount;
5820
5821 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5822 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5823 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5824
5825 /*
5826 * Merge the in-core extents. Note that the host record pointers and
5827 * current_ext index are invalid once the extent has been removed via
5828 * xfs_iext_remove().
5829 */
5830 xfs_bmbt_set_blockcount(leftp, blockcount);
5831 xfs_iext_remove(ip, current_ext, 1, 0);
5832
5833 /*
5834 * Update the on-disk extent count, the btree if necessary and log the
5835 * inode.
5836 */
5837 XFS_IFORK_NEXT_SET(ip, whichfork,
5838 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5839 *logflags |= XFS_ILOG_CORE;
5840 if (!cur) {
5841 *logflags |= XFS_ILOG_DEXT;
5842 return 0;
5843 }
5844
5845 /* lookup and remove the extent to merge */
5846 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5847 got.br_blockcount, &i);
5848 if (error)
4db431f5 5849 return error;
5fb5aeee 5850 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
ddb19e31
BF
5851
5852 error = xfs_btree_delete(cur, &i);
5853 if (error)
4db431f5 5854 return error;
5fb5aeee 5855 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
ddb19e31
BF
5856
5857 /* lookup and update size of the previous extent */
5858 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5859 left.br_blockcount, &i);
5860 if (error)
4db431f5 5861 return error;
5fb5aeee 5862 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
ddb19e31
BF
5863
5864 left.br_blockcount = blockcount;
5865
4db431f5
DC
5866 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5867 left.br_blockcount, left.br_state);
ddb19e31
BF
5868}
5869
a979bdfe
BF
5870/*
5871 * Shift a single extent.
5872 */
5873STATIC int
5874xfs_bmse_shift_one(
5875 struct xfs_inode *ip,
5876 int whichfork,
5877 xfs_fileoff_t offset_shift_fsb,
5878 int *current_ext,
5879 struct xfs_bmbt_rec_host *gotp,
5880 struct xfs_btree_cur *cur,
a904b1ca 5881 int *logflags,
9c194644
DW
5882 enum shift_direction direction,
5883 struct xfs_defer_ops *dfops)
a979bdfe
BF
5884{
5885 struct xfs_ifork *ifp;
5fb5aeee 5886 struct xfs_mount *mp;
a979bdfe 5887 xfs_fileoff_t startoff;
a904b1ca 5888 struct xfs_bmbt_rec_host *adj_irecp;
a979bdfe 5889 struct xfs_bmbt_irec got;
a904b1ca 5890 struct xfs_bmbt_irec adj_irec;
a979bdfe
BF
5891 int error;
5892 int i;
a904b1ca 5893 int total_extents;
a979bdfe 5894
5fb5aeee 5895 mp = ip->i_mount;
a979bdfe 5896 ifp = XFS_IFORK_PTR(ip, whichfork);
5d829300 5897 total_extents = xfs_iext_count(ifp);
a979bdfe
BF
5898
5899 xfs_bmbt_get_all(gotp, &got);
a979bdfe 5900
f71721d0 5901 /* delalloc extents should be prevented by caller */
5fb5aeee 5902 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
f71721d0 5903
a904b1ca
NJ
5904 if (direction == SHIFT_LEFT) {
5905 startoff = got.br_startoff - offset_shift_fsb;
5906
5907 /*
5908 * Check for merge if we've got an extent to the left,
5909 * otherwise make sure there's enough room at the start
5910 * of the file for the shift.
5911 */
5912 if (!*current_ext) {
5913 if (got.br_startoff < offset_shift_fsb)
5914 return -EINVAL;
5915 goto update_current_ext;
5916 }
5917 /*
5918 * grab the left extent and check for a large
5919 * enough hole.
5920 */
5921 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5922 xfs_bmbt_get_all(adj_irecp, &adj_irec);
a979bdfe 5923
a904b1ca
NJ
5924 if (startoff <
5925 adj_irec.br_startoff + adj_irec.br_blockcount)
b11bd671 5926 return -EINVAL;
a979bdfe 5927
b11bd671 5928 /* check whether to merge the extent or shift it down */
a904b1ca
NJ
5929 if (xfs_bmse_can_merge(&adj_irec, &got,
5930 offset_shift_fsb)) {
9c194644
DW
5931 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5932 *current_ext, gotp, adj_irecp,
5933 cur, logflags);
5934 if (error)
5935 return error;
5936 adj_irec = got;
5937 goto update_rmap;
b11bd671 5938 }
a904b1ca
NJ
5939 } else {
5940 startoff = got.br_startoff + offset_shift_fsb;
5941 /* nothing to move if this is the last extent */
5942 if (*current_ext >= (total_extents - 1))
5943 goto update_current_ext;
5944 /*
5945 * If this is not the last extent in the file, make sure there
5946 * is enough room between current extent and next extent for
5947 * accommodating the shift.
5948 */
5949 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5950 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5951 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5952 return -EINVAL;
5953 /*
5954 * Unlike a left shift (which involves a hole punch),
5955 * a right shift does not modify extent neighbors
5956 * in any way. We should never find mergeable extents
5957 * in this scenario. Check anyways and warn if we
5958 * encounter two extents that could be one.
5959 */
5960 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5961 WARN_ON_ONCE(1);
5962 }
a979bdfe
BF
5963 /*
5964 * Increment the extent index for the next iteration, update the start
5965 * offset of the in-core extent and update the btree if applicable.
5966 */
a904b1ca
NJ
5967update_current_ext:
5968 if (direction == SHIFT_LEFT)
5969 (*current_ext)++;
5970 else
5971 (*current_ext)--;
a979bdfe
BF
5972 xfs_bmbt_set_startoff(gotp, startoff);
5973 *logflags |= XFS_ILOG_CORE;
9c194644 5974 adj_irec = got;
a979bdfe
BF
5975 if (!cur) {
5976 *logflags |= XFS_ILOG_DEXT;
9c194644 5977 goto update_rmap;
a979bdfe
BF
5978 }
5979
5980 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5981 got.br_blockcount, &i);
5982 if (error)
5983 return error;
5fb5aeee 5984 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
a979bdfe
BF
5985
5986 got.br_startoff = startoff;
9c194644
DW
5987 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5988 got.br_blockcount, got.br_state);
5989 if (error)
5990 return error;
5991
5992update_rmap:
5993 /* update reverse mapping */
5994 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &adj_irec);
5995 if (error)
5996 return error;
5997 adj_irec.br_startoff = startoff;
5998 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &adj_irec);
a979bdfe
BF
5999}
6000
e1d8fb88 6001/*
a904b1ca 6002 * Shift extent records to the left/right to cover/create a hole.
e1d8fb88 6003 *
2c845f5a 6004 * The maximum number of extents to be shifted in a single operation is
a904b1ca 6005 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
2c845f5a
BF
6006 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6007 * is the length by which each extent is shifted. If there is no hole to shift
6008 * the extents into, this will be considered invalid operation and we abort
6009 * immediately.
e1d8fb88
NJ
6010 */
6011int
6012xfs_bmap_shift_extents(
6013 struct xfs_trans *tp,
6014 struct xfs_inode *ip,
a904b1ca 6015 xfs_fileoff_t *next_fsb,
e1d8fb88 6016 xfs_fileoff_t offset_shift_fsb,
2c845f5a 6017 int *done,
a904b1ca 6018 xfs_fileoff_t stop_fsb,
e1d8fb88 6019 xfs_fsblock_t *firstblock,
2c3234d1 6020 struct xfs_defer_ops *dfops,
a904b1ca 6021 enum shift_direction direction,
e1d8fb88
NJ
6022 int num_exts)
6023{
ca446d88 6024 struct xfs_btree_cur *cur = NULL;
e1d8fb88
NJ
6025 struct xfs_bmbt_rec_host *gotp;
6026 struct xfs_bmbt_irec got;
e1d8fb88
NJ
6027 struct xfs_mount *mp = ip->i_mount;
6028 struct xfs_ifork *ifp;
6029 xfs_extnum_t nexts = 0;
2c845f5a 6030 xfs_extnum_t current_ext;
a904b1ca
NJ
6031 xfs_extnum_t total_extents;
6032 xfs_extnum_t stop_extent;
e1d8fb88 6033 int error = 0;
e1d8fb88 6034 int whichfork = XFS_DATA_FORK;
ca446d88 6035 int logflags = 0;
e1d8fb88
NJ
6036
6037 if (unlikely(XFS_TEST_ERROR(
6038 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6039 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6040 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6041 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6042 XFS_ERRLEVEL_LOW, mp);
2451337d 6043 return -EFSCORRUPTED;
e1d8fb88
NJ
6044 }
6045
6046 if (XFS_FORCED_SHUTDOWN(mp))
2451337d 6047 return -EIO;
e1d8fb88 6048
2c845f5a
BF
6049 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6050 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
a904b1ca
NJ
6051 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
6052 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
e1d8fb88
NJ
6053
6054 ifp = XFS_IFORK_PTR(ip, whichfork);
e1d8fb88
NJ
6055 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6056 /* Read in all the extents */
6057 error = xfs_iread_extents(tp, ip, whichfork);
6058 if (error)
6059 return error;
6060 }
6061
ddb19e31
BF
6062 if (ifp->if_flags & XFS_IFBROOT) {
6063 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6064 cur->bc_private.b.firstblock = *firstblock;
2c3234d1 6065 cur->bc_private.b.dfops = dfops;
ddb19e31
BF
6066 cur->bc_private.b.flags = 0;
6067 }
6068
a904b1ca
NJ
6069 /*
6070 * There may be delalloc extents in the data fork before the range we
6071 * are collapsing out, so we cannot use the count of real extents here.
6072 * Instead we have to calculate it from the incore fork.
6073 */
5d829300 6074 total_extents = xfs_iext_count(ifp);
a904b1ca
NJ
6075 if (total_extents == 0) {
6076 *done = 1;
6077 goto del_cursor;
6078 }
6079
6080 /*
6081 * In case of first right shift, we need to initialize next_fsb
6082 */
6083 if (*next_fsb == NULLFSBLOCK) {
6084 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
6085 xfs_bmbt_get_all(gotp, &got);
6086 *next_fsb = got.br_startoff;
6087 if (stop_fsb > *next_fsb) {
6088 *done = 1;
6089 goto del_cursor;
6090 }
6091 }
6092
6093 /* Lookup the extent index at which we have to stop */
6094 if (direction == SHIFT_RIGHT) {
6095 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
6096 /* Make stop_extent exclusive of shift range */
6097 stop_extent--;
6098 } else
6099 stop_extent = total_extents;
6100
e1d8fb88 6101 /*
2c845f5a
BF
6102 * Look up the extent index for the fsb where we start shifting. We can
6103 * henceforth iterate with current_ext as extent list changes are locked
6104 * out via ilock.
6105 *
6106 * gotp can be null in 2 cases: 1) if there are no extents or 2)
a904b1ca 6107 * *next_fsb lies in a hole beyond which there are no extents. Either
2c845f5a 6108 * way, we are done.
e1d8fb88 6109 */
a904b1ca 6110 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
2c845f5a
BF
6111 if (!gotp) {
6112 *done = 1;
ddb19e31 6113 goto del_cursor;
e1d8fb88 6114 }
e1d8fb88 6115
a904b1ca
NJ
6116 /* some sanity checking before we finally start shifting extents */
6117 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
6118 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
6119 error = -EIO;
6120 goto del_cursor;
6121 }
6122
6123 while (nexts++ < num_exts) {
a979bdfe 6124 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
a904b1ca 6125 &current_ext, gotp, cur, &logflags,
9c194644 6126 direction, dfops);
a979bdfe
BF
6127 if (error)
6128 goto del_cursor;
a904b1ca
NJ
6129 /*
6130 * If there was an extent merge during the shift, the extent
6131 * count can change. Update the total and grade the next record.
6132 */
6133 if (direction == SHIFT_LEFT) {
5d829300 6134 total_extents = xfs_iext_count(ifp);
a904b1ca
NJ
6135 stop_extent = total_extents;
6136 }
e1d8fb88 6137
a904b1ca
NJ
6138 if (current_ext == stop_extent) {
6139 *done = 1;
6140 *next_fsb = NULLFSBLOCK;
ddb19e31 6141 break;
a904b1ca 6142 }
ddb19e31 6143 gotp = xfs_iext_get_ext(ifp, current_ext);
e1d8fb88
NJ
6144 }
6145
a904b1ca 6146 if (!*done) {
a979bdfe 6147 xfs_bmbt_get_all(gotp, &got);
2c845f5a 6148 *next_fsb = got.br_startoff;
a979bdfe 6149 }
e1d8fb88
NJ
6150
6151del_cursor:
6152 if (cur)
6153 xfs_btree_del_cursor(cur,
6154 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6155
ca446d88
BF
6156 if (logflags)
6157 xfs_trans_log_inode(tp, ip, logflags);
2c845f5a 6158
e1d8fb88
NJ
6159 return error;
6160}
a904b1ca
NJ
6161
6162/*
6163 * Splits an extent into two extents at split_fsb block such that it is
6164 * the first block of the current_ext. @current_ext is a target extent
6165 * to be split. @split_fsb is a block where the extents is split.
6166 * If split_fsb lies in a hole or the first block of extents, just return 0.
6167 */
6168STATIC int
6169xfs_bmap_split_extent_at(
6170 struct xfs_trans *tp,
6171 struct xfs_inode *ip,
6172 xfs_fileoff_t split_fsb,
6173 xfs_fsblock_t *firstfsb,
2c3234d1 6174 struct xfs_defer_ops *dfops)
a904b1ca
NJ
6175{
6176 int whichfork = XFS_DATA_FORK;
6177 struct xfs_btree_cur *cur = NULL;
6178 struct xfs_bmbt_rec_host *gotp;
6179 struct xfs_bmbt_irec got;
6180 struct xfs_bmbt_irec new; /* split extent */
6181 struct xfs_mount *mp = ip->i_mount;
6182 struct xfs_ifork *ifp;
6183 xfs_fsblock_t gotblkcnt; /* new block count for got */
6184 xfs_extnum_t current_ext;
6185 int error = 0;
6186 int logflags = 0;
6187 int i = 0;
6188
6189 if (unlikely(XFS_TEST_ERROR(
6190 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6191 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
6192 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
6193 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6194 XFS_ERRLEVEL_LOW, mp);
6195 return -EFSCORRUPTED;
6196 }
6197
6198 if (XFS_FORCED_SHUTDOWN(mp))
6199 return -EIO;
6200
6201 ifp = XFS_IFORK_PTR(ip, whichfork);
6202 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6203 /* Read in all the extents */
6204 error = xfs_iread_extents(tp, ip, whichfork);
6205 if (error)
6206 return error;
6207 }
6208
6209 /*
6210 * gotp can be null in 2 cases: 1) if there are no extents
6211 * or 2) split_fsb lies in a hole beyond which there are
6212 * no extents. Either way, we are done.
6213 */
6214 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
6215 if (!gotp)
6216 return 0;
6217
6218 xfs_bmbt_get_all(gotp, &got);
6219
6220 /*
6221 * Check split_fsb lies in a hole or the start boundary offset
6222 * of the extent.
6223 */
6224 if (got.br_startoff >= split_fsb)
6225 return 0;
6226
6227 gotblkcnt = split_fsb - got.br_startoff;
6228 new.br_startoff = split_fsb;
6229 new.br_startblock = got.br_startblock + gotblkcnt;
6230 new.br_blockcount = got.br_blockcount - gotblkcnt;
6231 new.br_state = got.br_state;
6232
6233 if (ifp->if_flags & XFS_IFBROOT) {
6234 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6235 cur->bc_private.b.firstblock = *firstfsb;
2c3234d1 6236 cur->bc_private.b.dfops = dfops;
a904b1ca
NJ
6237 cur->bc_private.b.flags = 0;
6238 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6239 got.br_startblock,
6240 got.br_blockcount,
6241 &i);
6242 if (error)
6243 goto del_cursor;
6244 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6245 }
6246
6247 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
6248 got.br_blockcount = gotblkcnt;
6249
6250 logflags = XFS_ILOG_CORE;
6251 if (cur) {
6252 error = xfs_bmbt_update(cur, got.br_startoff,
6253 got.br_startblock,
6254 got.br_blockcount,
6255 got.br_state);
6256 if (error)
6257 goto del_cursor;
6258 } else
6259 logflags |= XFS_ILOG_DEXT;
6260
6261 /* Add new extent */
6262 current_ext++;
6263 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6264 XFS_IFORK_NEXT_SET(ip, whichfork,
6265 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6266
6267 if (cur) {
6268 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6269 new.br_startblock, new.br_blockcount,
6270 &i);
6271 if (error)
6272 goto del_cursor;
6273 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6274 cur->bc_rec.b.br_state = new.br_state;
6275
6276 error = xfs_btree_insert(cur, &i);
6277 if (error)
6278 goto del_cursor;
6279 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6280 }
6281
6282 /*
6283 * Convert to a btree if necessary.
6284 */
6285 if (xfs_bmap_needs_btree(ip, whichfork)) {
6286 int tmp_logflags; /* partial log flag return val */
6287
6288 ASSERT(cur == NULL);
2c3234d1 6289 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
a904b1ca
NJ
6290 &cur, 0, &tmp_logflags, whichfork);
6291 logflags |= tmp_logflags;
6292 }
6293
6294del_cursor:
6295 if (cur) {
6296 cur->bc_private.b.allocated = 0;
6297 xfs_btree_del_cursor(cur,
6298 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6299 }
6300
6301 if (logflags)
6302 xfs_trans_log_inode(tp, ip, logflags);
6303 return error;
6304}
6305
6306int
6307xfs_bmap_split_extent(
6308 struct xfs_inode *ip,
6309 xfs_fileoff_t split_fsb)
6310{
6311 struct xfs_mount *mp = ip->i_mount;
6312 struct xfs_trans *tp;
2c3234d1 6313 struct xfs_defer_ops dfops;
a904b1ca 6314 xfs_fsblock_t firstfsb;
a904b1ca
NJ
6315 int error;
6316
253f4911
CH
6317 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6318 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6319 if (error)
a904b1ca 6320 return error;
a904b1ca
NJ
6321
6322 xfs_ilock(ip, XFS_ILOCK_EXCL);
6323 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6324
2c3234d1 6325 xfs_defer_init(&dfops, &firstfsb);
a904b1ca
NJ
6326
6327 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
2c3234d1 6328 &firstfsb, &dfops);
a904b1ca
NJ
6329 if (error)
6330 goto out;
6331
2c3234d1 6332 error = xfs_defer_finish(&tp, &dfops, NULL);
a904b1ca
NJ
6333 if (error)
6334 goto out;
6335
70393313 6336 return xfs_trans_commit(tp);
a904b1ca
NJ
6337
6338out:
2c3234d1 6339 xfs_defer_cancel(&dfops);
4906e215 6340 xfs_trans_cancel(tp);
a904b1ca
NJ
6341 return error;
6342}
9f3afb57
DW
6343
6344/* Deferred mapping is only for real extents in the data fork. */
6345static bool
6346xfs_bmap_is_update_needed(
6347 struct xfs_bmbt_irec *bmap)
6348{
6349 return bmap->br_startblock != HOLESTARTBLOCK &&
6350 bmap->br_startblock != DELAYSTARTBLOCK;
6351}
6352
6353/* Record a bmap intent. */
6354static int
6355__xfs_bmap_add(
6356 struct xfs_mount *mp,
6357 struct xfs_defer_ops *dfops,
6358 enum xfs_bmap_intent_type type,
6359 struct xfs_inode *ip,
6360 int whichfork,
6361 struct xfs_bmbt_irec *bmap)
6362{
6363 int error;
6364 struct xfs_bmap_intent *bi;
6365
6366 trace_xfs_bmap_defer(mp,
6367 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6368 type,
6369 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6370 ip->i_ino, whichfork,
6371 bmap->br_startoff,
6372 bmap->br_blockcount,
6373 bmap->br_state);
6374
6375 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6376 INIT_LIST_HEAD(&bi->bi_list);
6377 bi->bi_type = type;
6378 bi->bi_owner = ip;
6379 bi->bi_whichfork = whichfork;
6380 bi->bi_bmap = *bmap;
6381
6382 error = xfs_defer_join(dfops, bi->bi_owner);
6383 if (error) {
6384 kmem_free(bi);
6385 return error;
6386 }
6387
6388 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6389 return 0;
6390}
6391
6392/* Map an extent into a file. */
6393int
6394xfs_bmap_map_extent(
6395 struct xfs_mount *mp,
6396 struct xfs_defer_ops *dfops,
6397 struct xfs_inode *ip,
6398 struct xfs_bmbt_irec *PREV)
6399{
6400 if (!xfs_bmap_is_update_needed(PREV))
6401 return 0;
6402
6403 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6404 XFS_DATA_FORK, PREV);
6405}
6406
6407/* Unmap an extent out of a file. */
6408int
6409xfs_bmap_unmap_extent(
6410 struct xfs_mount *mp,
6411 struct xfs_defer_ops *dfops,
6412 struct xfs_inode *ip,
6413 struct xfs_bmbt_irec *PREV)
6414{
6415 if (!xfs_bmap_is_update_needed(PREV))
6416 return 0;
6417
6418 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6419 XFS_DATA_FORK, PREV);
6420}
6421
6422/*
6423 * Process one of the deferred bmap operations. We pass back the
6424 * btree cursor to maintain our lock on the bmapbt between calls.
6425 */
6426int
6427xfs_bmap_finish_one(
6428 struct xfs_trans *tp,
6429 struct xfs_defer_ops *dfops,
6430 struct xfs_inode *ip,
6431 enum xfs_bmap_intent_type type,
6432 int whichfork,
6433 xfs_fileoff_t startoff,
6434 xfs_fsblock_t startblock,
6435 xfs_filblks_t blockcount,
6436 xfs_exntst_t state)
6437{
6438 struct xfs_bmbt_irec bmap;
6439 int nimaps = 1;
6440 xfs_fsblock_t firstfsb;
6441 int flags = XFS_BMAPI_REMAP;
6442 int done;
6443 int error = 0;
6444
6445 bmap.br_startblock = startblock;
6446 bmap.br_startoff = startoff;
6447 bmap.br_blockcount = blockcount;
6448 bmap.br_state = state;
6449
6450 trace_xfs_bmap_deferred(tp->t_mountp,
6451 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6452 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
6453 ip->i_ino, whichfork, startoff, blockcount, state);
6454
6455 if (whichfork != XFS_DATA_FORK && whichfork != XFS_ATTR_FORK)
6456 return -EFSCORRUPTED;
6457 if (whichfork == XFS_ATTR_FORK)
6458 flags |= XFS_BMAPI_ATTRFORK;
6459
6460 if (XFS_TEST_ERROR(false, tp->t_mountp,
6461 XFS_ERRTAG_BMAP_FINISH_ONE,
6462 XFS_RANDOM_BMAP_FINISH_ONE))
6463 return -EIO;
6464
6465 switch (type) {
6466 case XFS_BMAP_MAP:
6467 firstfsb = bmap.br_startblock;
6468 error = xfs_bmapi_write(tp, ip, bmap.br_startoff,
6469 bmap.br_blockcount, flags, &firstfsb,
6470 bmap.br_blockcount, &bmap, &nimaps,
6471 dfops);
6472 break;
6473 case XFS_BMAP_UNMAP:
6474 error = xfs_bunmapi(tp, ip, bmap.br_startoff,
6475 bmap.br_blockcount, flags, 1, &firstfsb,
6476 dfops, &done);
6477 ASSERT(done);
6478 break;
6479 default:
6480 ASSERT(0);
6481 error = -EFSCORRUPTED;
6482 }
6483
6484 return error;
6485}