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