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