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