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