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