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