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