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