blob: d490fe8fd19f39e6da4ac1e32e24aed393d3ca85 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110030#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#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"
Nathan Scottd8cc8902005-11-02 10:34:53 +110043#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_quota.h"
45#include "xfs_trans_space.h"
46#include "xfs_buf_item.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100047#include "xfs_filestream.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100048#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000049#include "xfs_trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052kmem_zone_t *xfs_bmap_free_item_zone;
53
54/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110055 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 */
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110059 * Compute and fill in the value of the maximum depth of a bmap btree
60 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110062void
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 */
74
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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100110 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100114xfs_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}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100122STATIC 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
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100150/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000151 * 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
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100172 * 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}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100190 * Compute the worst-case number of indirect blocks that will be used
191 * for ip's delayed extent of length "len".
192 */
193STATIC xfs_filblks_t
194xfs_bmap_worst_indlen(
195 xfs_inode_t *ip, /* incore inode pointer */
196 xfs_filblks_t len) /* delayed extent length */
197{
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 */
202
203 mp = ip->i_mount;
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];
216 }
217 return rval;
218}
219
220/*
221 * Calculate the default attribute fork offset for newly created inodes.
222 */
223uint
224xfs_default_attroffset(
225 struct xfs_inode *ip)
226{
227 struct xfs_mount *mp = ip->i_mount;
228 uint offset;
229
230 if (mp->m_sb.sb_inodesize == 256) {
231 offset = XFS_LITINO(mp) -
232 XFS_BMDR_SPACE_CALC(MINABTPTRS);
233 } else {
234 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
235 }
236
237 ASSERT(offset < XFS_LITINO(mp));
238 return offset;
239}
240
241/*
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.
245 */
246STATIC void
247xfs_bmap_forkoff_reset(
248 xfs_mount_t *mp,
249 xfs_inode_t *ip,
250 int whichfork)
251{
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 }
261}
262
263/*
264 * Extent tree block counting routines.
265 */
266
267/*
268 * Count leaf blocks given a range of extent records.
269 */
270STATIC void
271xfs_bmap_count_leaves(
272 xfs_ifork_t *ifp,
273 xfs_extnum_t idx,
274 int numrecs,
275 int *count)
276{
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 }
283}
284
285/*
286 * Count leaf blocks given a range of extent records originally
287 * in btree format.
288 */
289STATIC void
290xfs_bmap_disk_count_leaves(
291 struct xfs_mount *mp,
292 struct xfs_btree_block *block,
293 int numrecs,
294 int *count)
295{
296 int b;
297 xfs_bmbt_rec_t *frp;
298
299 for (b = 1; b <= numrecs; b++) {
300 frp = XFS_BMBT_REC_ADDR(mp, block, b);
301 *count += xfs_bmbt_disk_get_blockcount(frp);
302 }
303}
304
305/*
306 * Recursively walks each level of a btree
307 * to count total fsblocks is use.
308 */
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 */
317{
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;
326
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);
333
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 }
348
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}
381
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 }
408
409 /*
410 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
411 */
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);
420
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 }
426
427 return 0;
428}
429
430/*
431 * Debug/sanity checking code
432 */
433
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);
441
442 if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) ||
443 be16_to_cpu(block->bb_level) != level ||
444 be16_to_cpu(block->bb_numrecs) == 0 ||
445 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
446 return 0;
447 return 1;
448}
449
450#ifdef DEBUG
451STATIC struct xfs_buf *
452xfs_bmap_get_bp(
453 struct xfs_btree_cur *cur,
454 xfs_fsblock_t bno)
455{
456 struct xfs_log_item_desc *lidp;
457 int i;
458
459 if (!cur)
460 return NULL;
461
462 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
463 if (!cur->bc_bufs[i])
464 break;
465 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
466 return cur->bc_bufs[i];
467 }
468
469 /* Chase down all the log items to see if the bp is there */
470 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
471 struct xfs_buf_log_item *bip;
472 bip = (struct xfs_buf_log_item *)lidp->lid_item;
473 if (bip->bli_item.li_type == XFS_LI_BUF &&
474 XFS_BUF_ADDR(bip->bli_buf) == bno)
475 return bip->bli_buf;
476 }
477
478 return NULL;
479}
480
481STATIC void
482xfs_check_block(
483 struct xfs_btree_block *block,
484 xfs_mount_t *mp,
485 int root,
486 short sz)
487{
488 int i, j, dmxr;
489 __be64 *pp, *thispa; /* pointer to block address */
490 xfs_bmbt_key_t *prevp, *keyp;
491
492 ASSERT(be16_to_cpu(block->bb_level) > 0);
493
494 prevp = NULL;
495 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
496 dmxr = mp->m_bmap_dmxr[0];
497 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
498
499 if (prevp) {
500 ASSERT(be64_to_cpu(prevp->br_startoff) <
501 be64_to_cpu(keyp->br_startoff));
502 }
503 prevp = keyp;
504
505 /*
506 * Compare the block numbers to see if there are dups.
507 */
508 if (root)
509 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
510 else
511 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
512
513 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
514 if (root)
515 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
516 else
517 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
518 if (*thispa == *pp) {
519 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
520 __func__, j, i,
521 (unsigned long long)be64_to_cpu(*thispa));
522 panic("%s: ptrs are equal in node\n",
523 __func__);
524 }
525 }
526 }
527}
528
529/*
530 * Check that the extents for the inode ip are in the right order in all
531 * btree leaves.
532 */
533
534STATIC void
535xfs_bmap_check_leaf_extents(
536 xfs_btree_cur_t *cur, /* btree cursor or null */
537 xfs_inode_t *ip, /* incore inode pointer */
538 int whichfork) /* data or attr fork */
539{
540 struct xfs_btree_block *block; /* current btree block */
541 xfs_fsblock_t bno; /* block # of "block" */
542 xfs_buf_t *bp; /* buffer for "block" */
543 int error; /* error return value */
544 xfs_extnum_t i=0, j; /* index into the extents list */
545 xfs_ifork_t *ifp; /* fork structure */
546 int level; /* btree level, for checking */
547 xfs_mount_t *mp; /* file system mount structure */
548 __be64 *pp; /* pointer to block address */
549 xfs_bmbt_rec_t *ep; /* pointer to current extent */
550 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
551 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
552 int bp_release = 0;
553
554 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
555 return;
556 }
557
558 bno = NULLFSBLOCK;
559 mp = ip->i_mount;
560 ifp = XFS_IFORK_PTR(ip, whichfork);
561 block = ifp->if_broot;
562 /*
563 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
564 */
565 level = be16_to_cpu(block->bb_level);
566 ASSERT(level > 0);
567 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
568 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
569 bno = be64_to_cpu(*pp);
570
571 ASSERT(bno != NULLDFSBNO);
572 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
573 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
574
575 /*
576 * Go down the tree until leaf level is reached, following the first
577 * pointer (leftmost) at each level.
578 */
579 while (level-- > 0) {
580 /* See if buf is in cur first */
581 bp_release = 0;
582 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
583 if (!bp) {
584 bp_release = 1;
585 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
586 XFS_BMAP_BTREE_REF,
587 &xfs_bmbt_buf_ops);
588 if (error)
589 goto error_norelse;
590 }
591 block = XFS_BUF_TO_BLOCK(bp);
592 XFS_WANT_CORRUPTED_GOTO(
593 xfs_bmap_sanity_check(mp, bp, level),
594 error0);
595 if (level == 0)
596 break;
597
598 /*
599 * Check this block for basic sanity (increasing keys and
600 * no duplicate blocks).
601 */
602
603 xfs_check_block(block, mp, 0, 0);
604 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
605 bno = be64_to_cpu(*pp);
606 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
607 if (bp_release) {
608 bp_release = 0;
609 xfs_trans_brelse(NULL, bp);
610 }
611 }
612
613 /*
614 * Here with bp and block set to the leftmost leaf node in the tree.
615 */
616 i = 0;
617
618 /*
619 * Loop over all leaf nodes checking that all extents are in the right order.
620 */
621 for (;;) {
622 xfs_fsblock_t nextbno;
623 xfs_extnum_t num_recs;
624
625
626 num_recs = xfs_btree_get_numrecs(block);
627
628 /*
629 * Read-ahead the next leaf block, if any.
630 */
631
632 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
633
634 /*
635 * Check all the extents to make sure they are OK.
636 * If we had a previous block, the last entry should
637 * conform with the first entry in this one.
638 */
639
640 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
641 if (i) {
642 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
643 xfs_bmbt_disk_get_blockcount(&last) <=
644 xfs_bmbt_disk_get_startoff(ep));
645 }
646 for (j = 1; j < num_recs; j++) {
647 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
648 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
649 xfs_bmbt_disk_get_blockcount(ep) <=
650 xfs_bmbt_disk_get_startoff(nextp));
651 ep = nextp;
652 }
653
654 last = *ep;
655 i += num_recs;
656 if (bp_release) {
657 bp_release = 0;
658 xfs_trans_brelse(NULL, bp);
659 }
660 bno = nextbno;
661 /*
662 * If we've reached the end, stop.
663 */
664 if (bno == NULLFSBLOCK)
665 break;
666
667 bp_release = 0;
668 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
669 if (!bp) {
670 bp_release = 1;
671 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
672 XFS_BMAP_BTREE_REF,
673 &xfs_bmbt_buf_ops);
674 if (error)
675 goto error_norelse;
676 }
677 block = XFS_BUF_TO_BLOCK(bp);
678 }
679 if (bp_release) {
680 bp_release = 0;
681 xfs_trans_brelse(NULL, bp);
682 }
683 return;
684
685error0:
686 xfs_warn(mp, "%s: at error0", __func__);
687 if (bp_release)
688 xfs_trans_brelse(NULL, bp);
689error_norelse:
690 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
691 __func__, i);
692 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
693 return;
694}
695
696/*
697 * Add bmap trace insert entries for all the contents of the extent records.
698 */
699void
700xfs_bmap_trace_exlist(
701 xfs_inode_t *ip, /* incore inode pointer */
702 xfs_extnum_t cnt, /* count of entries in the list */
703 int whichfork, /* data or attr fork */
704 unsigned long caller_ip)
705{
706 xfs_extnum_t idx; /* extent record index */
707 xfs_ifork_t *ifp; /* inode fork pointer */
708 int state = 0;
709
710 if (whichfork == XFS_ATTR_FORK)
711 state |= BMAP_ATTRFORK;
712
713 ifp = XFS_IFORK_PTR(ip, whichfork);
714 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
715 for (idx = 0; idx < cnt; idx++)
716 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
717}
718
719/*
720 * Validate that the bmbt_irecs being returned from bmapi are valid
721 * given the callers original parameters. Specifically check the
722 * ranges of the returned irecs to ensure that they only extent beyond
723 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
724 */
725STATIC void
726xfs_bmap_validate_ret(
727 xfs_fileoff_t bno,
728 xfs_filblks_t len,
729 int flags,
730 xfs_bmbt_irec_t *mval,
731 int nmap,
732 int ret_nmap)
733{
734 int i; /* index to map values */
735
736 ASSERT(ret_nmap <= nmap);
737
738 for (i = 0; i < ret_nmap; i++) {
739 ASSERT(mval[i].br_blockcount > 0);
740 if (!(flags & XFS_BMAPI_ENTIRE)) {
741 ASSERT(mval[i].br_startoff >= bno);
742 ASSERT(mval[i].br_blockcount <= len);
743 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
744 bno + len);
745 } else {
746 ASSERT(mval[i].br_startoff < bno + len);
747 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
748 bno);
749 }
750 ASSERT(i == 0 ||
751 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
752 mval[i].br_startoff);
753 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
754 mval[i].br_startblock != HOLESTARTBLOCK);
755 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
756 mval[i].br_state == XFS_EXT_UNWRITTEN);
757 }
758}
759
760#else
761#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
762#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
763#endif /* DEBUG */
764
765/*
766 * bmap free list manipulation functions
767 */
768
769/*
770 * Add the extent to the list of extents to be free at transaction end.
771 * The list is maintained sorted (by block number).
772 */
773void
774xfs_bmap_add_free(
775 xfs_fsblock_t bno, /* fs block number of extent */
776 xfs_filblks_t len, /* length of extent */
777 xfs_bmap_free_t *flist, /* list of extents */
778 xfs_mount_t *mp) /* mount point structure */
779{
780 xfs_bmap_free_item_t *cur; /* current (next) element */
781 xfs_bmap_free_item_t *new; /* new element */
782 xfs_bmap_free_item_t *prev; /* previous element */
783#ifdef DEBUG
784 xfs_agnumber_t agno;
785 xfs_agblock_t agbno;
786
787 ASSERT(bno != NULLFSBLOCK);
788 ASSERT(len > 0);
789 ASSERT(len <= MAXEXTLEN);
790 ASSERT(!isnullstartblock(bno));
791 agno = XFS_FSB_TO_AGNO(mp, bno);
792 agbno = XFS_FSB_TO_AGBNO(mp, bno);
793 ASSERT(agno < mp->m_sb.sb_agcount);
794 ASSERT(agbno < mp->m_sb.sb_agblocks);
795 ASSERT(len < mp->m_sb.sb_agblocks);
796 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
797#endif
798 ASSERT(xfs_bmap_free_item_zone != NULL);
799 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
800 new->xbfi_startblock = bno;
801 new->xbfi_blockcount = (xfs_extlen_t)len;
802 for (prev = NULL, cur = flist->xbf_first;
803 cur != NULL;
804 prev = cur, cur = cur->xbfi_next) {
805 if (cur->xbfi_startblock >= bno)
806 break;
807 }
808 if (prev)
809 prev->xbfi_next = new;
810 else
811 flist->xbf_first = new;
812 new->xbfi_next = cur;
813 flist->xbf_count++;
814}
815
816/*
817 * Remove the entry "free" from the free item list. Prev points to the
818 * previous entry, unless "free" is the head of the list.
819 */
820STATIC void
821xfs_bmap_del_free(
822 xfs_bmap_free_t *flist, /* free item list header */
823 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
824 xfs_bmap_free_item_t *free) /* list item to be freed */
825{
826 if (prev)
827 prev->xbfi_next = free->xbfi_next;
828 else
829 flist->xbf_first = free->xbfi_next;
830 flist->xbf_count--;
831 kmem_zone_free(xfs_bmap_free_item_zone, free);
832}
833
834
835/*
836 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
837 * caller. Frees all the extents that need freeing, which must be done
838 * last due to locking considerations. We never free any extents in
839 * the first transaction.
840 *
841 * Return 1 if the given transaction was committed and a new one
842 * started, and 0 otherwise in the committed parameter.
843 */
844int /* error */
845xfs_bmap_finish(
846 xfs_trans_t **tp, /* transaction pointer addr */
847 xfs_bmap_free_t *flist, /* i/o: list extents to free */
848 int *committed) /* xact committed or not */
849{
850 xfs_efd_log_item_t *efd; /* extent free data */
851 xfs_efi_log_item_t *efi; /* extent free intention */
852 int error; /* error return value */
853 xfs_bmap_free_item_t *free; /* free extent item */
854 unsigned int logres; /* new log reservation */
855 unsigned int logcount; /* new log count */
856 xfs_mount_t *mp; /* filesystem mount structure */
857 xfs_bmap_free_item_t *next; /* next item on free list */
858 xfs_trans_t *ntp; /* new transaction pointer */
859
860 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
861 if (flist->xbf_count == 0) {
862 *committed = 0;
863 return 0;
864 }
865 ntp = *tp;
866 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
867 for (free = flist->xbf_first; free; free = free->xbfi_next)
868 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
869 free->xbfi_blockcount);
870 logres = ntp->t_log_res;
871 logcount = ntp->t_log_count;
872 ntp = xfs_trans_dup(*tp);
873 error = xfs_trans_commit(*tp, 0);
874 *tp = ntp;
875 *committed = 1;
876 /*
877 * We have a new transaction, so we should return committed=1,
878 * even though we're returning an error.
879 */
880 if (error)
881 return error;
882
883 /*
884 * transaction commit worked ok so we can drop the extra ticket
885 * reference that we gained in xfs_trans_dup()
886 */
887 xfs_log_ticket_put(ntp->t_ticket);
888
889 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
890 logcount)))
891 return error;
892 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
893 for (free = flist->xbf_first; free != NULL; free = next) {
894 next = free->xbfi_next;
895 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
896 free->xbfi_blockcount))) {
897 /*
898 * The bmap free list will be cleaned up at a
899 * higher level. The EFI will be canceled when
900 * this transaction is aborted.
901 * Need to force shutdown here to make sure it
902 * happens, since this transaction may not be
903 * dirty yet.
904 */
905 mp = ntp->t_mountp;
906 if (!XFS_FORCED_SHUTDOWN(mp))
907 xfs_force_shutdown(mp,
908 (error == EFSCORRUPTED) ?
909 SHUTDOWN_CORRUPT_INCORE :
910 SHUTDOWN_META_IO_ERROR);
911 return error;
912 }
913 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
914 free->xbfi_blockcount);
915 xfs_bmap_del_free(flist, NULL, free);
916 }
917 return 0;
918}
919
920/*
921 * Free up any items left in the list.
922 */
923void
924xfs_bmap_cancel(
925 xfs_bmap_free_t *flist) /* list of bmap_free_items */
926{
927 xfs_bmap_free_item_t *free; /* free list item */
928 xfs_bmap_free_item_t *next;
929
930 if (flist->xbf_count == 0)
931 return;
932 ASSERT(flist->xbf_first != NULL);
933 for (free = flist->xbf_first; free; free = next) {
934 next = free->xbfi_next;
935 xfs_bmap_del_free(flist, NULL, free);
936 }
937 ASSERT(flist->xbf_count == 0);
938}
939
940/*
941 * Inode fork format manipulation functions
942 */
943
944/*
945 * Transform a btree format file with only one leaf node, where the
946 * extents list will fit in the inode, into an extents format file.
947 * Since the file extents are already in-core, all we have to do is
948 * give up the space for the btree root and pitch the leaf block.
949 */
950STATIC int /* error */
951xfs_bmap_btree_to_extents(
952 xfs_trans_t *tp, /* transaction pointer */
953 xfs_inode_t *ip, /* incore inode pointer */
954 xfs_btree_cur_t *cur, /* btree cursor */
955 int *logflagsp, /* inode logging flags */
956 int whichfork) /* data or attr fork */
957{
958 /* REFERENCED */
959 struct xfs_btree_block *cblock;/* child btree block */
960 xfs_fsblock_t cbno; /* child block number */
961 xfs_buf_t *cbp; /* child block's buffer */
962 int error; /* error return value */
963 xfs_ifork_t *ifp; /* inode fork data */
964 xfs_mount_t *mp; /* mount point structure */
965 __be64 *pp; /* ptr to block address */
966 struct xfs_btree_block *rblock;/* root btree block */
967
968 mp = ip->i_mount;
969 ifp = XFS_IFORK_PTR(ip, whichfork);
970 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
971 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
972 rblock = ifp->if_broot;
973 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
974 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
975 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
976 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
977 cbno = be64_to_cpu(*pp);
978 *logflagsp = 0;
979#ifdef DEBUG
980 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
981 return error;
982#endif
983 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
984 &xfs_bmbt_buf_ops);
985 if (error)
986 return error;
987 cblock = XFS_BUF_TO_BLOCK(cbp);
988 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
989 return error;
990 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
991 ip->i_d.di_nblocks--;
992 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
993 xfs_trans_binval(tp, cbp);
994 if (cur->bc_bufs[0] == cbp)
995 cur->bc_bufs[0] = NULL;
996 xfs_iroot_realloc(ip, -1, whichfork);
997 ASSERT(ifp->if_broot == NULL);
998 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
999 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
1000 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1001 return 0;
1002}
1003
1004/*
1005 * Convert an extents-format file into a btree-format file.
1006 * The new file will have a root block (in the inode) and a single child block.
1007 */
1008STATIC int /* error */
1009xfs_bmap_extents_to_btree(
1010 xfs_trans_t *tp, /* transaction pointer */
1011 xfs_inode_t *ip, /* incore inode pointer */
1012 xfs_fsblock_t *firstblock, /* first-block-allocated */
1013 xfs_bmap_free_t *flist, /* blocks freed in xaction */
1014 xfs_btree_cur_t **curp, /* cursor returned to caller */
1015 int wasdel, /* converting a delayed alloc */
1016 int *logflagsp, /* inode logging flags */
1017 int whichfork) /* data or attr fork */
1018{
1019 struct xfs_btree_block *ablock; /* allocated (child) bt block */
1020 xfs_buf_t *abp; /* buffer for ablock */
1021 xfs_alloc_arg_t args; /* allocation arguments */
1022 xfs_bmbt_rec_t *arp; /* child record pointer */
1023 struct xfs_btree_block *block; /* btree root block */
1024 xfs_btree_cur_t *cur; /* bmap btree cursor */
1025 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1026 int error; /* error return value */
1027 xfs_extnum_t i, cnt; /* extent record index */
1028 xfs_ifork_t *ifp; /* inode fork pointer */
1029 xfs_bmbt_key_t *kp; /* root block key pointer */
1030 xfs_mount_t *mp; /* mount structure */
1031 xfs_extnum_t nextents; /* number of file extents */
1032 xfs_bmbt_ptr_t *pp; /* root block address pointer */
1033
1034 ifp = XFS_IFORK_PTR(ip, whichfork);
1035 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
1036
1037 /*
1038 * Make space in the inode incore.
1039 */
1040 xfs_iroot_realloc(ip, 1, whichfork);
1041 ifp->if_flags |= XFS_IFBROOT;
1042
1043 /*
1044 * Fill in the root.
1045 */
1046 block = ifp->if_broot;
1047 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
1048 block->bb_level = cpu_to_be16(1);
1049 block->bb_numrecs = cpu_to_be16(1);
1050 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
1051 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
1052
1053 /*
1054 * Need a cursor. Can't allocate until bb_level is filled in.
1055 */
1056 mp = ip->i_mount;
1057 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1058 cur->bc_private.b.firstblock = *firstblock;
1059 cur->bc_private.b.flist = flist;
1060 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
1061 /*
1062 * Convert to a btree with two levels, one record in root.
1063 */
1064 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
1065 memset(&args, 0, sizeof(args));
1066 args.tp = tp;
1067 args.mp = mp;
1068 args.firstblock = *firstblock;
1069 if (*firstblock == NULLFSBLOCK) {
1070 args.type = XFS_ALLOCTYPE_START_BNO;
1071 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
1072 } else if (flist->xbf_low) {
1073 args.type = XFS_ALLOCTYPE_START_BNO;
1074 args.fsbno = *firstblock;
1075 } else {
1076 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1077 args.fsbno = *firstblock;
1078 }
1079 args.minlen = args.maxlen = args.prod = 1;
1080 args.wasdel = wasdel;
1081 *logflagsp = 0;
1082 if ((error = xfs_alloc_vextent(&args))) {
1083 xfs_iroot_realloc(ip, -1, whichfork);
1084 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1085 return error;
1086 }
1087 /*
1088 * Allocation can't fail, the space was reserved.
1089 */
1090 ASSERT(args.fsbno != NULLFSBLOCK);
1091 ASSERT(*firstblock == NULLFSBLOCK ||
1092 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
1093 (flist->xbf_low &&
1094 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
1095 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
1096 cur->bc_private.b.allocated++;
1097 ip->i_d.di_nblocks++;
1098 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
1099 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
1100 /*
1101 * Fill in the child block.
1102 */
1103 abp->b_ops = &xfs_bmbt_buf_ops;
1104 ablock = XFS_BUF_TO_BLOCK(abp);
1105 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
1106 ablock->bb_level = 0;
1107 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
1108 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
1109 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
1110 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1111 for (cnt = i = 0; i < nextents; i++) {
1112 ep = xfs_iext_get_ext(ifp, i);
1113 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
1114 arp->l0 = cpu_to_be64(ep->l0);
1115 arp->l1 = cpu_to_be64(ep->l1);
1116 arp++; cnt++;
1117 }
1118 }
1119 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
1120 xfs_btree_set_numrecs(ablock, cnt);
1121
1122 /*
1123 * Fill in the root key and pointer.
1124 */
1125 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
1126 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
1127 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
1128 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
1129 be16_to_cpu(block->bb_level)));
1130 *pp = cpu_to_be64(args.fsbno);
1131
1132 /*
1133 * Do all this logging at the end so that
1134 * the root is at the right level.
1135 */
1136 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
1137 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
1138 ASSERT(*curp == NULL);
1139 *curp = cur;
1140 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
1141 return 0;
1142}
1143
1144/*
1145 * Convert a local file to an extents file.
1146 * This code is out of bounds for data forks of regular files,
1147 * since the file data needs to get logged so things will stay consistent.
1148 * (The bmap-level manipulations are ok, though).
1149 */
1150STATIC int /* error */
1151xfs_bmap_local_to_extents(
1152 xfs_trans_t *tp, /* transaction pointer */
1153 xfs_inode_t *ip, /* incore inode pointer */
1154 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
1155 xfs_extlen_t total, /* total blocks needed by transaction */
1156 int *logflagsp, /* inode logging flags */
1157 int whichfork,
1158 void (*init_fn)(struct xfs_buf *bp,
1159 struct xfs_inode *ip,
1160 struct xfs_ifork *ifp))
1161{
1162 int error; /* error return value */
1163 int flags; /* logging flags returned */
1164 xfs_ifork_t *ifp; /* inode fork pointer */
1165
1166 /*
1167 * We don't want to deal with the case of keeping inode data inline yet.
1168 * So sending the data fork of a regular inode is invalid.
1169 */
1170 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
1171 ifp = XFS_IFORK_PTR(ip, whichfork);
1172 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1173 flags = 0;
1174 error = 0;
1175 if (ifp->if_bytes) {
1176 xfs_alloc_arg_t args; /* allocation arguments */
1177 xfs_buf_t *bp; /* buffer for extent block */
1178 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
1179
1180 ASSERT((ifp->if_flags &
1181 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
1182 memset(&args, 0, sizeof(args));
1183 args.tp = tp;
1184 args.mp = ip->i_mount;
1185 args.firstblock = *firstblock;
1186 /*
1187 * Allocate a block. We know we need only one, since the
1188 * file currently fits in an inode.
1189 */
1190 if (*firstblock == NULLFSBLOCK) {
1191 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
1192 args.type = XFS_ALLOCTYPE_START_BNO;
1193 } else {
1194 args.fsbno = *firstblock;
1195 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1196 }
1197 args.total = total;
1198 args.minlen = args.maxlen = args.prod = 1;
1199 error = xfs_alloc_vextent(&args);
1200 if (error)
1201 goto done;
1202
1203 /* Can't fail, the space was reserved. */
1204 ASSERT(args.fsbno != NULLFSBLOCK);
1205 ASSERT(args.len == 1);
1206 *firstblock = args.fsbno;
1207 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
1208
1209 /* initialise the block and copy the data */
1210 init_fn(bp, ip, ifp);
1211
1212 /* account for the change in fork size and log everything */
1213 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
1214 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
1215 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
1216 xfs_iext_add(ifp, 0, 1);
1217 ep = xfs_iext_get_ext(ifp, 0);
1218 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
1219 trace_xfs_bmap_post_update(ip, 0,
1220 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
1221 _THIS_IP_);
1222 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
1223 ip->i_d.di_nblocks = 1;
1224 xfs_trans_mod_dquot_byino(tp, ip,
1225 XFS_TRANS_DQ_BCOUNT, 1L);
1226 flags |= xfs_ilog_fext(whichfork);
1227 } else {
1228 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
1229 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
1230 }
1231 ifp->if_flags &= ~XFS_IFINLINE;
1232 ifp->if_flags |= XFS_IFEXTENTS;
1233 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
1234 flags |= XFS_ILOG_CORE;
1235done:
1236 *logflagsp = flags;
1237 return error;
1238}
1239
1240/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 * Called from xfs_bmap_add_attrfork to handle btree format files.
1242 */
1243STATIC int /* error */
1244xfs_bmap_add_attrfork_btree(
1245 xfs_trans_t *tp, /* transaction pointer */
1246 xfs_inode_t *ip, /* incore inode pointer */
1247 xfs_fsblock_t *firstblock, /* first block allocated */
1248 xfs_bmap_free_t *flist, /* blocks to free at commit */
1249 int *flags) /* inode logging flags */
1250{
1251 xfs_btree_cur_t *cur; /* btree cursor */
1252 int error; /* error return value */
1253 xfs_mount_t *mp; /* file system mount struct */
1254 int stat; /* newroot status */
1255
1256 mp = ip->i_mount;
1257 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1258 *flags |= XFS_ILOG_DBROOT;
1259 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001260 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 cur->bc_private.b.flist = flist;
1262 cur->bc_private.b.firstblock = *firstblock;
1263 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1264 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001265 /* must be at least one entry */
1266 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001267 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 goto error0;
1269 if (stat == 0) {
1270 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1271 return XFS_ERROR(ENOSPC);
1272 }
1273 *firstblock = cur->bc_private.b.firstblock;
1274 cur->bc_private.b.allocated = 0;
1275 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1276 }
1277 return 0;
1278error0:
1279 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1280 return error;
1281}
1282
1283/*
1284 * Called from xfs_bmap_add_attrfork to handle extents format files.
1285 */
1286STATIC int /* error */
1287xfs_bmap_add_attrfork_extents(
1288 xfs_trans_t *tp, /* transaction pointer */
1289 xfs_inode_t *ip, /* incore inode pointer */
1290 xfs_fsblock_t *firstblock, /* first block allocated */
1291 xfs_bmap_free_t *flist, /* blocks to free at commit */
1292 int *flags) /* inode logging flags */
1293{
1294 xfs_btree_cur_t *cur; /* bmap btree cursor */
1295 int error; /* error return value */
1296
1297 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1298 return 0;
1299 cur = NULL;
1300 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
1301 flags, XFS_DATA_FORK);
1302 if (cur) {
1303 cur->bc_private.b.allocated = 0;
1304 xfs_btree_del_cursor(cur,
1305 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1306 }
1307 return error;
1308}
1309
1310/*
Dave Chinner1e823792013-02-11 15:58:13 +11001311 * Block initialisation functions for local to extent format conversion.
1312 * As these get more complex, they will be moved to the relevant files,
1313 * but for now they are too simple to worry about.
1314 */
1315STATIC void
1316xfs_bmap_local_to_extents_init_fn(
1317 struct xfs_buf *bp,
1318 struct xfs_inode *ip,
1319 struct xfs_ifork *ifp)
1320{
1321 bp->b_ops = &xfs_bmbt_buf_ops;
1322 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
1323}
1324
1325STATIC void
1326xfs_symlink_local_to_remote(
1327 struct xfs_buf *bp,
1328 struct xfs_inode *ip,
1329 struct xfs_ifork *ifp)
1330{
1331 /* remote symlink blocks are not verifiable until CRCs come along */
1332 bp->b_ops = NULL;
1333 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
1334}
1335
1336/*
1337 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1338 * different data fork content type needs a different callout to do the
1339 * conversion. Some are basic and only require special block initialisation
1340 * callouts for the data formating, others (directories) are so specialised they
1341 * handle everything themselves.
1342 *
1343 * XXX (dgc): investigate whether directory conversion can use the generic
1344 * formatting callout. It should be possible - it's just a very complex
1345 * formatter. it would also require passing the transaction through to the init
1346 * function.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 */
1348STATIC int /* error */
1349xfs_bmap_add_attrfork_local(
1350 xfs_trans_t *tp, /* transaction pointer */
1351 xfs_inode_t *ip, /* incore inode pointer */
1352 xfs_fsblock_t *firstblock, /* first block allocated */
1353 xfs_bmap_free_t *flist, /* blocks to free at commit */
1354 int *flags) /* inode logging flags */
1355{
1356 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1359 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001360
Al Viroabbede12011-07-26 02:31:30 -04001361 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 memset(&dargs, 0, sizeof(dargs));
1363 dargs.dp = ip;
1364 dargs.firstblock = firstblock;
1365 dargs.flist = flist;
Dave Chinner1e823792013-02-11 15:58:13 +11001366 dargs.total = ip->i_mount->m_dirblkfsbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 dargs.whichfork = XFS_DATA_FORK;
1368 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001369 return xfs_dir2_sf_to_block(&dargs);
1370 }
1371
1372 if (S_ISLNK(ip->i_d.di_mode))
1373 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1374 flags, XFS_DATA_FORK,
1375 xfs_symlink_local_to_remote);
1376
1377 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
1378 XFS_DATA_FORK,
1379 xfs_bmap_local_to_extents_init_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380}
1381
1382/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001383 * Convert inode from non-attributed to attributed.
1384 * Must not be in a transaction, ip must not be locked.
1385 */
1386int /* error code */
1387xfs_bmap_add_attrfork(
1388 xfs_inode_t *ip, /* incore inode pointer */
1389 int size, /* space new attribute needs */
1390 int rsvd) /* xact may use reserved blks */
1391{
1392 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1393 xfs_bmap_free_t flist; /* freed extent records */
1394 xfs_mount_t *mp; /* mount structure */
1395 xfs_trans_t *tp; /* transaction pointer */
1396 int blks; /* space reservation */
1397 int version = 1; /* superblock attr version */
1398 int committed; /* xaction was committed */
1399 int logflags; /* logging flags */
1400 int error; /* error return value */
1401
1402 ASSERT(XFS_IFORK_Q(ip) == 0);
1403
1404 mp = ip->i_mount;
1405 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1406 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
1407 blks = XFS_ADDAFORK_SPACE_RES(mp);
1408 if (rsvd)
1409 tp->t_flags |= XFS_TRANS_RESERVE;
1410 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
1411 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
1412 goto error0;
1413 xfs_ilock(ip, XFS_ILOCK_EXCL);
1414 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1415 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1416 XFS_QMOPT_RES_REGBLKS);
1417 if (error) {
1418 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1419 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
1420 return error;
1421 }
1422 if (XFS_IFORK_Q(ip))
1423 goto error1;
1424 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1425 /*
1426 * For inodes coming from pre-6.2 filesystems.
1427 */
1428 ASSERT(ip->i_d.di_aformat == 0);
1429 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1430 }
1431 ASSERT(ip->i_d.di_anextents == 0);
1432
1433 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1434 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1435
1436 switch (ip->i_d.di_format) {
1437 case XFS_DINODE_FMT_DEV:
1438 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1439 break;
1440 case XFS_DINODE_FMT_UUID:
1441 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1442 break;
1443 case XFS_DINODE_FMT_LOCAL:
1444 case XFS_DINODE_FMT_EXTENTS:
1445 case XFS_DINODE_FMT_BTREE:
1446 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1447 if (!ip->i_d.di_forkoff)
1448 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1449 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1450 version = 2;
1451 break;
1452 default:
1453 ASSERT(0);
1454 error = XFS_ERROR(EINVAL);
1455 goto error1;
1456 }
1457
1458 ASSERT(ip->i_afp == NULL);
1459 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1460 ip->i_afp->if_flags = XFS_IFEXTENTS;
1461 logflags = 0;
1462 xfs_bmap_init(&flist, &firstblock);
1463 switch (ip->i_d.di_format) {
1464 case XFS_DINODE_FMT_LOCAL:
1465 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
1466 &logflags);
1467 break;
1468 case XFS_DINODE_FMT_EXTENTS:
1469 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1470 &flist, &logflags);
1471 break;
1472 case XFS_DINODE_FMT_BTREE:
1473 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
1474 &logflags);
1475 break;
1476 default:
1477 error = 0;
1478 break;
1479 }
1480 if (logflags)
1481 xfs_trans_log_inode(tp, ip, logflags);
1482 if (error)
1483 goto error2;
1484 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1485 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1486 __int64_t sbfields = 0;
1487
1488 spin_lock(&mp->m_sb_lock);
1489 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1490 xfs_sb_version_addattr(&mp->m_sb);
1491 sbfields |= XFS_SB_VERSIONNUM;
1492 }
1493 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1494 xfs_sb_version_addattr2(&mp->m_sb);
1495 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
1496 }
1497 if (sbfields) {
1498 spin_unlock(&mp->m_sb_lock);
1499 xfs_mod_sb(tp, sbfields);
1500 } else
1501 spin_unlock(&mp->m_sb_lock);
1502 }
1503
1504 error = xfs_bmap_finish(&tp, &flist, &committed);
1505 if (error)
1506 goto error2;
1507 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1508error2:
1509 xfs_bmap_cancel(&flist);
1510error1:
1511 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1512error0:
1513 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1514 return error;
1515}
1516
1517/*
1518 * Internal and external extent tree search functions.
1519 */
1520
1521/*
1522 * Read in the extents to if_extents.
1523 * All inode fields are set up by caller, we just traverse the btree
1524 * and copy the records in. If the file system cannot contain unwritten
1525 * extents, the records are checked for no "state" flags.
1526 */
1527int /* error */
1528xfs_bmap_read_extents(
1529 xfs_trans_t *tp, /* transaction pointer */
1530 xfs_inode_t *ip, /* incore inode */
1531 int whichfork) /* data or attr fork */
1532{
1533 struct xfs_btree_block *block; /* current btree block */
1534 xfs_fsblock_t bno; /* block # of "block" */
1535 xfs_buf_t *bp; /* buffer for "block" */
1536 int error; /* error return value */
1537 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1538 xfs_extnum_t i, j; /* index into the extents list */
1539 xfs_ifork_t *ifp; /* fork structure */
1540 int level; /* btree level, for checking */
1541 xfs_mount_t *mp; /* file system mount structure */
1542 __be64 *pp; /* pointer to block address */
1543 /* REFERENCED */
1544 xfs_extnum_t room; /* number of entries there's room for */
1545
1546 bno = NULLFSBLOCK;
1547 mp = ip->i_mount;
1548 ifp = XFS_IFORK_PTR(ip, whichfork);
1549 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1550 XFS_EXTFMT_INODE(ip);
1551 block = ifp->if_broot;
1552 /*
1553 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1554 */
1555 level = be16_to_cpu(block->bb_level);
1556 ASSERT(level > 0);
1557 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1558 bno = be64_to_cpu(*pp);
1559 ASSERT(bno != NULLDFSBNO);
1560 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1561 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1562 /*
1563 * Go down the tree until leaf level is reached, following the first
1564 * pointer (leftmost) at each level.
1565 */
1566 while (level-- > 0) {
1567 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1568 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1569 if (error)
1570 return error;
1571 block = XFS_BUF_TO_BLOCK(bp);
1572 XFS_WANT_CORRUPTED_GOTO(
1573 xfs_bmap_sanity_check(mp, bp, level),
1574 error0);
1575 if (level == 0)
1576 break;
1577 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1578 bno = be64_to_cpu(*pp);
1579 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
1580 xfs_trans_brelse(tp, bp);
1581 }
1582 /*
1583 * Here with bp and block set to the leftmost leaf node in the tree.
1584 */
1585 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1586 i = 0;
1587 /*
1588 * Loop over all leaf nodes. Copy information to the extent records.
1589 */
1590 for (;;) {
1591 xfs_bmbt_rec_t *frp;
1592 xfs_fsblock_t nextbno;
1593 xfs_extnum_t num_recs;
1594 xfs_extnum_t start;
1595
1596 num_recs = xfs_btree_get_numrecs(block);
1597 if (unlikely(i + num_recs > room)) {
1598 ASSERT(i + num_recs <= room);
1599 xfs_warn(ip->i_mount,
1600 "corrupt dinode %Lu, (btree extents).",
1601 (unsigned long long) ip->i_ino);
1602 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1603 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1604 goto error0;
1605 }
1606 XFS_WANT_CORRUPTED_GOTO(
1607 xfs_bmap_sanity_check(mp, bp, 0),
1608 error0);
1609 /*
1610 * Read-ahead the next leaf block, if any.
1611 */
1612 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1613 if (nextbno != NULLFSBLOCK)
1614 xfs_btree_reada_bufl(mp, nextbno, 1,
1615 &xfs_bmbt_buf_ops);
1616 /*
1617 * Copy records into the extent records.
1618 */
1619 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1620 start = i;
1621 for (j = 0; j < num_recs; j++, i++, frp++) {
1622 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1623 trp->l0 = be64_to_cpu(frp->l0);
1624 trp->l1 = be64_to_cpu(frp->l1);
1625 }
1626 if (exntf == XFS_EXTFMT_NOSTATE) {
1627 /*
1628 * Check all attribute bmap btree records and
1629 * any "older" data bmap btree records for a
1630 * set bit in the "extent flag" position.
1631 */
1632 if (unlikely(xfs_check_nostate_extents(ifp,
1633 start, num_recs))) {
1634 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1635 XFS_ERRLEVEL_LOW,
1636 ip->i_mount);
1637 goto error0;
1638 }
1639 }
1640 xfs_trans_brelse(tp, bp);
1641 bno = nextbno;
1642 /*
1643 * If we've reached the end, stop.
1644 */
1645 if (bno == NULLFSBLOCK)
1646 break;
1647 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1648 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1649 if (error)
1650 return error;
1651 block = XFS_BUF_TO_BLOCK(bp);
1652 }
1653 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1654 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1655 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1656 return 0;
1657error0:
1658 xfs_trans_brelse(tp, bp);
1659 return XFS_ERROR(EFSCORRUPTED);
1660}
1661
1662
1663/*
1664 * Search the extent records for the entry containing block bno.
1665 * If bno lies in a hole, point to the next entry. If bno lies
1666 * past eof, *eofp will be set, and *prevp will contain the last
1667 * entry (null if none). Else, *lastxp will be set to the index
1668 * of the found entry; *gotp will contain the entry.
1669 */
1670STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1671xfs_bmap_search_multi_extents(
1672 xfs_ifork_t *ifp, /* inode fork pointer */
1673 xfs_fileoff_t bno, /* block number searched for */
1674 int *eofp, /* out: end of file found */
1675 xfs_extnum_t *lastxp, /* out: last extent index */
1676 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1677 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1678{
1679 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1680 xfs_extnum_t lastx; /* last extent index */
1681
1682 /*
1683 * Initialize the extent entry structure to catch access to
1684 * uninitialized br_startblock field.
1685 */
1686 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1687 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1688 gotp->br_state = XFS_EXT_INVALID;
1689#if XFS_BIG_BLKNOS
1690 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
1691#else
1692 gotp->br_startblock = 0xffffa5a5;
1693#endif
1694 prevp->br_startoff = NULLFILEOFF;
1695
1696 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1697 if (lastx > 0) {
1698 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1699 }
1700 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1701 xfs_bmbt_get_all(ep, gotp);
1702 *eofp = 0;
1703 } else {
1704 if (lastx > 0) {
1705 *gotp = *prevp;
1706 }
1707 *eofp = 1;
1708 ep = NULL;
1709 }
1710 *lastxp = lastx;
1711 return ep;
1712}
1713
1714/*
1715 * Search the extents list for the inode, for the extent containing bno.
1716 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1717 * *eofp will be set, and *prevp will contain the last entry (null if none).
1718 * Else, *lastxp will be set to the index of the found
1719 * entry; *gotp will contain the entry.
1720 */
1721STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1722xfs_bmap_search_extents(
1723 xfs_inode_t *ip, /* incore inode pointer */
1724 xfs_fileoff_t bno, /* block number searched for */
1725 int fork, /* data or attr fork */
1726 int *eofp, /* out: end of file found */
1727 xfs_extnum_t *lastxp, /* out: last extent index */
1728 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1729 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1730{
1731 xfs_ifork_t *ifp; /* inode fork pointer */
1732 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1733
1734 XFS_STATS_INC(xs_look_exlist);
1735 ifp = XFS_IFORK_PTR(ip, fork);
1736
1737 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1738
1739 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1740 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1741 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1742 "Access to block zero in inode %llu "
1743 "start_block: %llx start_off: %llx "
1744 "blkcnt: %llx extent-state: %x lastx: %x\n",
1745 (unsigned long long)ip->i_ino,
1746 (unsigned long long)gotp->br_startblock,
1747 (unsigned long long)gotp->br_startoff,
1748 (unsigned long long)gotp->br_blockcount,
1749 gotp->br_state, *lastxp);
1750 *lastxp = NULLEXTNUM;
1751 *eofp = 1;
1752 return NULL;
1753 }
1754 return ep;
1755}
1756
1757/*
1758 * Returns the file-relative block number of the first unused block(s)
1759 * in the file with at least "len" logically contiguous blocks free.
1760 * This is the lowest-address hole if the file has holes, else the first block
1761 * past the end of file.
1762 * Return 0 if the file is currently local (in-inode).
1763 */
1764int /* error */
1765xfs_bmap_first_unused(
1766 xfs_trans_t *tp, /* transaction pointer */
1767 xfs_inode_t *ip, /* incore inode */
1768 xfs_extlen_t len, /* size of hole to find */
1769 xfs_fileoff_t *first_unused, /* unused block */
1770 int whichfork) /* data or attr fork */
1771{
1772 int error; /* error return value */
1773 int idx; /* extent record index */
1774 xfs_ifork_t *ifp; /* inode fork pointer */
1775 xfs_fileoff_t lastaddr; /* last block number seen */
1776 xfs_fileoff_t lowest; /* lowest useful block */
1777 xfs_fileoff_t max; /* starting useful block */
1778 xfs_fileoff_t off; /* offset for this block */
1779 xfs_extnum_t nextents; /* number of extent entries */
1780
1781 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1782 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1783 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1784 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1785 *first_unused = 0;
1786 return 0;
1787 }
1788 ifp = XFS_IFORK_PTR(ip, whichfork);
1789 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1790 (error = xfs_iread_extents(tp, ip, whichfork)))
1791 return error;
1792 lowest = *first_unused;
1793 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1794 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1795 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1796 off = xfs_bmbt_get_startoff(ep);
1797 /*
1798 * See if the hole before this extent will work.
1799 */
1800 if (off >= lowest + len && off - max >= len) {
1801 *first_unused = max;
1802 return 0;
1803 }
1804 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1805 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1806 }
1807 *first_unused = max;
1808 return 0;
1809}
1810
1811/*
1812 * Returns the file-relative block number of the last block + 1 before
1813 * last_block (input value) in the file.
1814 * This is not based on i_size, it is based on the extent records.
1815 * Returns 0 for local files, as they do not have extent records.
1816 */
1817int /* error */
1818xfs_bmap_last_before(
1819 xfs_trans_t *tp, /* transaction pointer */
1820 xfs_inode_t *ip, /* incore inode */
1821 xfs_fileoff_t *last_block, /* last block */
1822 int whichfork) /* data or attr fork */
1823{
1824 xfs_fileoff_t bno; /* input file offset */
1825 int eof; /* hit end of file */
1826 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1827 int error; /* error return value */
1828 xfs_bmbt_irec_t got; /* current extent value */
1829 xfs_ifork_t *ifp; /* inode fork pointer */
1830 xfs_extnum_t lastx; /* last extent used */
1831 xfs_bmbt_irec_t prev; /* previous extent value */
1832
1833 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1834 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1835 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
1836 return XFS_ERROR(EIO);
1837 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1838 *last_block = 0;
1839 return 0;
1840 }
1841 ifp = XFS_IFORK_PTR(ip, whichfork);
1842 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1843 (error = xfs_iread_extents(tp, ip, whichfork)))
1844 return error;
1845 bno = *last_block - 1;
1846 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1847 &prev);
1848 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1849 if (prev.br_startoff == NULLFILEOFF)
1850 *last_block = 0;
1851 else
1852 *last_block = prev.br_startoff + prev.br_blockcount;
1853 }
1854 /*
1855 * Otherwise *last_block is already the right answer.
1856 */
1857 return 0;
1858}
1859
1860STATIC int
1861xfs_bmap_last_extent(
1862 struct xfs_trans *tp,
1863 struct xfs_inode *ip,
1864 int whichfork,
1865 struct xfs_bmbt_irec *rec,
1866 int *is_empty)
1867{
1868 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1869 int error;
1870 int nextents;
1871
1872 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1873 error = xfs_iread_extents(tp, ip, whichfork);
1874 if (error)
1875 return error;
1876 }
1877
1878 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1879 if (nextents == 0) {
1880 *is_empty = 1;
1881 return 0;
1882 }
1883
1884 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1885 *is_empty = 0;
1886 return 0;
1887}
1888
1889/*
1890 * Check the last inode extent to determine whether this allocation will result
1891 * in blocks being allocated at the end of the file. When we allocate new data
1892 * blocks at the end of the file which do not start at the previous data block,
1893 * we will try to align the new blocks at stripe unit boundaries.
1894 *
1895 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
1896 * at, or past the EOF.
1897 */
1898STATIC int
1899xfs_bmap_isaeof(
1900 struct xfs_bmalloca *bma,
1901 int whichfork)
1902{
1903 struct xfs_bmbt_irec rec;
1904 int is_empty;
1905 int error;
1906
1907 bma->aeof = 0;
1908 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1909 &is_empty);
1910 if (error || is_empty)
1911 return error;
1912
1913 /*
1914 * Check if we are allocation or past the last extent, or at least into
1915 * the last delayed allocated extent.
1916 */
1917 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1918 (bma->offset >= rec.br_startoff &&
1919 isnullstartblock(rec.br_startblock));
1920 return 0;
1921}
1922
1923/*
1924 * Check if the endoff is outside the last extent. If so the caller will grow
1925 * the allocation to a stripe unit boundary. All offsets are considered outside
1926 * the end of file for an empty fork, so 1 is returned in *eof in that case.
1927 */
1928int
1929xfs_bmap_eof(
1930 struct xfs_inode *ip,
1931 xfs_fileoff_t endoff,
1932 int whichfork,
1933 int *eof)
1934{
1935 struct xfs_bmbt_irec rec;
1936 int error;
1937
1938 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
1939 if (error || *eof)
1940 return error;
1941
1942 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
1943 return 0;
1944}
1945
1946/*
1947 * Returns the file-relative block number of the first block past eof in
1948 * the file. This is not based on i_size, it is based on the extent records.
1949 * Returns 0 for local files, as they do not have extent records.
1950 */
1951int
1952xfs_bmap_last_offset(
1953 struct xfs_trans *tp,
1954 struct xfs_inode *ip,
1955 xfs_fileoff_t *last_block,
1956 int whichfork)
1957{
1958 struct xfs_bmbt_irec rec;
1959 int is_empty;
1960 int error;
1961
1962 *last_block = 0;
1963
1964 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1965 return 0;
1966
1967 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1968 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1969 return XFS_ERROR(EIO);
1970
1971 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1972 if (error || is_empty)
1973 return error;
1974
1975 *last_block = rec.br_startoff + rec.br_blockcount;
1976 return 0;
1977}
1978
1979/*
1980 * Returns whether the selected fork of the inode has exactly one
1981 * block or not. For the data fork we check this matches di_size,
1982 * implying the file's range is 0..bsize-1.
1983 */
1984int /* 1=>1 block, 0=>otherwise */
1985xfs_bmap_one_block(
1986 xfs_inode_t *ip, /* incore inode */
1987 int whichfork) /* data or attr fork */
1988{
1989 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1990 xfs_ifork_t *ifp; /* inode fork pointer */
1991 int rval; /* return value */
1992 xfs_bmbt_irec_t s; /* internal version of extent */
1993
1994#ifndef DEBUG
1995 if (whichfork == XFS_DATA_FORK)
1996 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1997#endif /* !DEBUG */
1998 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1999 return 0;
2000 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
2001 return 0;
2002 ifp = XFS_IFORK_PTR(ip, whichfork);
2003 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2004 ep = xfs_iext_get_ext(ifp, 0);
2005 xfs_bmbt_get_all(ep, &s);
2006 rval = s.br_startoff == 0 && s.br_blockcount == 1;
2007 if (rval && whichfork == XFS_DATA_FORK)
2008 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
2009 return rval;
2010}
2011
2012/*
2013 * Extent tree manipulation functions used during allocation.
2014 */
2015
2016/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002017 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 */
2019STATIC int /* error */
2020xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002021 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002023 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002025 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002028 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 xfs_fileoff_t new_endoff; /* end offset of new entry */
2030 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2031 /* left is 0, right is 1, prev is 2 */
2032 int rval=0; /* return value (logging flags) */
2033 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002034 xfs_filblks_t da_new; /* new count del alloc blocks used */
2035 xfs_filblks_t da_old; /* old count del alloc blocks used */
2036 xfs_filblks_t temp=0; /* value for da_new calculations */
2037 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002040 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002041
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002042 ASSERT(bma->idx >= 0);
2043 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002044 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002045 ASSERT(!bma->cur ||
2046 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002047
2048 XFS_STATS_INC(xs_add_exlist);
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050#define LEFT r[0]
2051#define RIGHT r[1]
2052#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
2054 /*
2055 * Set up a bunch of variables to make the tests simpler.
2056 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002057 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 xfs_bmbt_get_all(ep, &PREV);
2059 new_endoff = new->br_startoff + new->br_blockcount;
2060 ASSERT(PREV.br_startoff <= new->br_startoff);
2061 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002062
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002063 da_old = startblockval(PREV.br_startblock);
2064 da_new = 0;
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 /*
2067 * Set flags determining what part of the previous delayed allocation
2068 * extent is being replaced by a real allocation.
2069 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002070 if (PREV.br_startoff == new->br_startoff)
2071 state |= BMAP_LEFT_FILLING;
2072 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2073 state |= BMAP_RIGHT_FILLING;
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /*
2076 * Check and set flags if this segment has a left neighbor.
2077 * Don't set contiguous if the combined extent would be too large.
2078 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002079 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002080 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002081 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002082
2083 if (isnullstartblock(LEFT.br_startblock))
2084 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002086
2087 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2088 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2089 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2090 LEFT.br_state == new->br_state &&
2091 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2092 state |= BMAP_LEFT_CONTIG;
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 /*
2095 * Check and set flags if this segment has a right neighbor.
2096 * Don't set contiguous if the combined extent would be too large.
2097 * Also check for all-three-contiguous being too large.
2098 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002099 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002100 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002101 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002102
2103 if (isnullstartblock(RIGHT.br_startblock))
2104 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002106
2107 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2108 new_endoff == RIGHT.br_startoff &&
2109 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2110 new->br_state == RIGHT.br_state &&
2111 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2112 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2113 BMAP_RIGHT_FILLING)) !=
2114 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2115 BMAP_RIGHT_FILLING) ||
2116 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2117 <= MAXEXTLEN))
2118 state |= BMAP_RIGHT_CONTIG;
2119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 error = 0;
2121 /*
2122 * Switch out based on the FILLING and CONTIG state bits.
2123 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002124 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2125 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2126 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2127 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 /*
2129 * Filling in all of a previously delayed allocation extent.
2130 * The left and right neighbors are both contiguous with new.
2131 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002132 bma->idx--;
2133 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2134 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 LEFT.br_blockcount + PREV.br_blockcount +
2136 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002137 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002138
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002139 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
2140 bma->ip->i_d.di_nextents--;
2141 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2143 else {
2144 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002145 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002147 RIGHT.br_blockcount, &i);
2148 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002150 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002151 error = xfs_btree_delete(bma->cur, &i);
2152 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002154 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002155 error = xfs_btree_decrement(bma->cur, 0, &i);
2156 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002158 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002159 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 LEFT.br_startblock,
2161 LEFT.br_blockcount +
2162 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002163 RIGHT.br_blockcount, LEFT.br_state);
2164 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 goto done;
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 break;
2168
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002169 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /*
2171 * Filling in all of a previously delayed allocation extent.
2172 * The left neighbor is contiguous, the right is not.
2173 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002174 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002175
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002176 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2177 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00002178 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002179 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002180
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002181 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
2182 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 rval = XFS_ILOG_DEXT;
2184 else {
2185 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002186 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002188 &i);
2189 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002191 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002192 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 LEFT.br_startblock,
2194 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002195 PREV.br_blockcount, LEFT.br_state);
2196 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 goto done;
2198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 break;
2200
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002201 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 /*
2203 * Filling in all of a previously delayed allocation extent.
2204 * The right neighbor is contiguous, the left is not.
2205 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002206 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 xfs_bmbt_set_startblock(ep, new->br_startblock);
2208 xfs_bmbt_set_blockcount(ep,
2209 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002210 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002211
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002212 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
2213 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 rval = XFS_ILOG_DEXT;
2215 else {
2216 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002217 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002219 RIGHT.br_blockcount, &i);
2220 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002222 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002223 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 new->br_startblock,
2225 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002226 RIGHT.br_blockcount, PREV.br_state);
2227 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 goto done;
2229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 break;
2231
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002232 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 /*
2234 * Filling in all of a previously delayed allocation extent.
2235 * Neither the left nor right neighbors are contiguous with
2236 * the new one.
2237 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002238 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002240 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002241
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002242 bma->ip->i_d.di_nextents++;
2243 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2245 else {
2246 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002247 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002249 &i);
2250 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002252 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002253 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2254 error = xfs_btree_insert(bma->cur, &i);
2255 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002257 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 break;
2260
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002261 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 /*
2263 * Filling in the first part of a previous delayed allocation.
2264 * The left neighbor is contiguous.
2265 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002266 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
2267 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 LEFT.br_blockcount + new->br_blockcount);
2269 xfs_bmbt_set_startoff(ep,
2270 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002271 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002274 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002276 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 rval = XFS_ILOG_DEXT;
2278 else {
2279 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002280 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002282 &i);
2283 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002285 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002286 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 LEFT.br_startblock,
2288 LEFT.br_blockcount +
2289 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002290 LEFT.br_state);
2291 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 goto done;
2293 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002294 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002295 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002296 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002297 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002298
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002299 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 break;
2301
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002302 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 /*
2304 * Filling in the first part of a previous delayed allocation.
2305 * The left neighbor is not contiguous.
2306 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002307 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 xfs_bmbt_set_startoff(ep, new_endoff);
2309 temp = PREV.br_blockcount - new->br_blockcount;
2310 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002311 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
2312 bma->ip->i_d.di_nextents++;
2313 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2315 else {
2316 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002317 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002319 &i);
2320 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002322 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002323 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2324 error = xfs_btree_insert(bma->cur, &i);
2325 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002327 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002329
2330 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002331 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2332 bma->firstblock, bma->flist,
2333 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 rval |= tmp_rval;
2335 if (error)
2336 goto done;
2337 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002338 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002339 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002340 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2341 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002342 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002343 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 break;
2345
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002346 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 /*
2348 * Filling in the last part of a previous delayed allocation.
2349 * The right neighbor is contiguous with the new allocation.
2350 */
2351 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002352 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002354 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002355 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 new->br_blockcount + RIGHT.br_blockcount,
2357 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002358 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2359 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 rval = XFS_ILOG_DEXT;
2361 else {
2362 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002363 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002365 RIGHT.br_blockcount, &i);
2366 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002368 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002369 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 new->br_startblock,
2371 new->br_blockcount +
2372 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002373 RIGHT.br_state);
2374 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 goto done;
2376 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002377
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002378 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002379 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002380 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002381 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002382 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002383
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002384 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 break;
2386
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002387 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 /*
2389 * Filling in the last part of a previous delayed allocation.
2390 * The right neighbor is not contiguous.
2391 */
2392 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002393 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002395 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2396 bma->ip->i_d.di_nextents++;
2397 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2399 else {
2400 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002401 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002403 &i);
2404 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002406 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002407 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2408 error = xfs_btree_insert(bma->cur, &i);
2409 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002411 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002413
2414 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002415 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2416 bma->firstblock, bma->flist, &bma->cur, 1,
2417 &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 rval |= tmp_rval;
2419 if (error)
2420 goto done;
2421 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002422 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002423 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002424 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2425 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002426 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002427 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002428
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002429 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 break;
2431
2432 case 0:
2433 /*
2434 * Filling in the middle part of a previous delayed allocation.
2435 * Contiguity is impossible here.
2436 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002437 *
2438 * We start with a delayed allocation:
2439 *
2440 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2441 * PREV @ idx
2442 *
2443 * and we are allocating:
2444 * +rrrrrrrrrrrrrrrrr+
2445 * new
2446 *
2447 * and we set it up for insertion as:
2448 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2449 * new
2450 * PREV @ idx LEFT RIGHT
2451 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 */
2453 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002455 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002456 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2457 LEFT = *new;
2458 RIGHT.br_state = PREV.br_state;
2459 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002460 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002461 RIGHT.br_startoff = new_endoff;
2462 RIGHT.br_blockcount = temp2;
2463 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002464 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2465 bma->ip->i_d.di_nextents++;
2466 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2468 else {
2469 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002470 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002472 &i);
2473 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002475 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002476 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2477 error = xfs_btree_insert(bma->cur, &i);
2478 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002480 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002482
2483 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002484 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2485 bma->firstblock, bma->flist, &bma->cur,
2486 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 rval |= tmp_rval;
2488 if (error)
2489 goto done;
2490 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002491 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2492 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002493 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002494 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002495 if (diff > 0) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002496 error = xfs_icsb_modify_counters(bma->ip->i_mount,
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002497 XFS_SBS_FDBLOCKS,
2498 -((int64_t)diff), 0);
2499 ASSERT(!error);
2500 if (error)
2501 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002503
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002504 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002505 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002506 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2507 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2508 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002509 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002510 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002511
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002512 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002513 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 break;
2515
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002516 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2517 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2518 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2519 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2520 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2521 case BMAP_LEFT_CONTIG:
2522 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 /*
2524 * These cases are all impossible.
2525 */
2526 ASSERT(0);
2527 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002528
2529 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002530 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002531 int tmp_logflags; /* partial log flag return val */
2532
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002533 ASSERT(bma->cur == NULL);
2534 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2535 bma->firstblock, bma->flist, &bma->cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002536 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002537 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002538 if (error)
2539 goto done;
2540 }
2541
2542 /* adjust for changes in reserved delayed indirect blocks */
2543 if (da_old || da_new) {
2544 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002545 if (bma->cur)
2546 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002547 ASSERT(temp <= da_old);
2548 if (temp < da_old)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002549 xfs_icsb_modify_counters(bma->ip->i_mount,
2550 XFS_SBS_FDBLOCKS,
2551 (int64_t)(da_old - temp), 0);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002552 }
2553
2554 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002555 if (bma->cur)
2556 bma->cur->bc_private.b.allocated = 0;
2557
2558 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002560 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return error;
2562#undef LEFT
2563#undef RIGHT
2564#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
2566
2567/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002568 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 */
2570STATIC int /* error */
2571xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002572 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002574 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002576 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002577 xfs_fsblock_t *first, /* pointer to firstblock variable */
2578 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002579 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002582 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002585 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 xfs_fileoff_t new_endoff; /* end offset of new entry */
2587 xfs_exntst_t newext; /* new extent state */
2588 xfs_exntst_t oldext; /* old extent state */
2589 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2590 /* left is 0, right is 1, prev is 2 */
2591 int rval=0; /* return value (logging flags) */
2592 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002594 *logflagsp = 0;
2595
2596 cur = *curp;
2597 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2598
2599 ASSERT(*idx >= 0);
2600 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2601 ASSERT(!isnullstartblock(new->br_startblock));
2602
2603 XFS_STATS_INC(xs_add_exlist);
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605#define LEFT r[0]
2606#define RIGHT r[1]
2607#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 /*
2610 * Set up a bunch of variables to make the tests simpler.
2611 */
2612 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002613 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 xfs_bmbt_get_all(ep, &PREV);
2615 newext = new->br_state;
2616 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2617 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2618 ASSERT(PREV.br_state == oldext);
2619 new_endoff = new->br_startoff + new->br_blockcount;
2620 ASSERT(PREV.br_startoff <= new->br_startoff);
2621 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 /*
2624 * Set flags determining what part of the previous oldext allocation
2625 * extent is being replaced by a newext allocation.
2626 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002627 if (PREV.br_startoff == new->br_startoff)
2628 state |= BMAP_LEFT_FILLING;
2629 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2630 state |= BMAP_RIGHT_FILLING;
2631
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 /*
2633 * Check and set flags if this segment has a left neighbor.
2634 * Don't set contiguous if the combined extent would be too large.
2635 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002636 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002637 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002638 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002639
2640 if (isnullstartblock(LEFT.br_startblock))
2641 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002643
2644 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2645 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2646 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2647 LEFT.br_state == newext &&
2648 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2649 state |= BMAP_LEFT_CONTIG;
2650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 /*
2652 * Check and set flags if this segment has a right neighbor.
2653 * Don't set contiguous if the combined extent would be too large.
2654 * Also check for all-three-contiguous being too large.
2655 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002656 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002657 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002658 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002659 if (isnullstartblock(RIGHT.br_startblock))
2660 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002662
2663 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2664 new_endoff == RIGHT.br_startoff &&
2665 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2666 newext == RIGHT.br_state &&
2667 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2668 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2669 BMAP_RIGHT_FILLING)) !=
2670 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2671 BMAP_RIGHT_FILLING) ||
2672 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2673 <= MAXEXTLEN))
2674 state |= BMAP_RIGHT_CONTIG;
2675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 /*
2677 * Switch out based on the FILLING and CONTIG state bits.
2678 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002679 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2680 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2681 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2682 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 /*
2684 * Setting all of a previous oldext extent to newext.
2685 * The left and right neighbors are both contiguous with new.
2686 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002687 --*idx;
2688
2689 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2690 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 LEFT.br_blockcount + PREV.br_blockcount +
2692 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002693 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002694
Christoph Hellwigec90c552011-05-23 08:52:53 +00002695 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 ip->i_d.di_nextents -= 2;
2697 if (cur == NULL)
2698 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2699 else {
2700 rval = XFS_ILOG_CORE;
2701 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2702 RIGHT.br_startblock,
2703 RIGHT.br_blockcount, &i)))
2704 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002705 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002706 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002708 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002709 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002711 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002712 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002714 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002715 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002717 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2719 LEFT.br_startblock,
2720 LEFT.br_blockcount + PREV.br_blockcount +
2721 RIGHT.br_blockcount, LEFT.br_state)))
2722 goto done;
2723 }
2724 break;
2725
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002726 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /*
2728 * Setting all of a previous oldext extent to newext.
2729 * The left neighbor is contiguous, the right is not.
2730 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002731 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002732
Christoph Hellwigec90c552011-05-23 08:52:53 +00002733 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2734 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2735 LEFT.br_blockcount + PREV.br_blockcount);
2736 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2737
2738 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 ip->i_d.di_nextents--;
2740 if (cur == NULL)
2741 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2742 else {
2743 rval = XFS_ILOG_CORE;
2744 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2745 PREV.br_startblock, PREV.br_blockcount,
2746 &i)))
2747 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002748 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002749 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002751 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002752 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002754 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2756 LEFT.br_startblock,
2757 LEFT.br_blockcount + PREV.br_blockcount,
2758 LEFT.br_state)))
2759 goto done;
2760 }
2761 break;
2762
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002763 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 /*
2765 * Setting all of a previous oldext extent to newext.
2766 * The right neighbor is contiguous, the left is not.
2767 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002768 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 xfs_bmbt_set_blockcount(ep,
2770 PREV.br_blockcount + RIGHT.br_blockcount);
2771 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002772 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2773 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 ip->i_d.di_nextents--;
2775 if (cur == NULL)
2776 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2777 else {
2778 rval = XFS_ILOG_CORE;
2779 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2780 RIGHT.br_startblock,
2781 RIGHT.br_blockcount, &i)))
2782 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002783 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002784 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002786 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002787 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002789 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2791 new->br_startblock,
2792 new->br_blockcount + RIGHT.br_blockcount,
2793 newext)))
2794 goto done;
2795 }
2796 break;
2797
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002798 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 /*
2800 * Setting all of a previous oldext extent to newext.
2801 * Neither the left nor right neighbors are contiguous with
2802 * the new one.
2803 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002804 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002806 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002807
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (cur == NULL)
2809 rval = XFS_ILOG_DEXT;
2810 else {
2811 rval = 0;
2812 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2813 new->br_startblock, new->br_blockcount,
2814 &i)))
2815 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002816 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2818 new->br_startblock, new->br_blockcount,
2819 newext)))
2820 goto done;
2821 }
2822 break;
2823
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002824 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 /*
2826 * Setting the first part of a previous oldext extent to newext.
2827 * The left neighbor is contiguous.
2828 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002829 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2830 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 LEFT.br_blockcount + new->br_blockcount);
2832 xfs_bmbt_set_startoff(ep,
2833 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002834 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002835
Christoph Hellwigec90c552011-05-23 08:52:53 +00002836 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 xfs_bmbt_set_startblock(ep,
2838 new->br_startblock + new->br_blockcount);
2839 xfs_bmbt_set_blockcount(ep,
2840 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002841 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002842
Christoph Hellwigec90c552011-05-23 08:52:53 +00002843 --*idx;
2844
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 if (cur == NULL)
2846 rval = XFS_ILOG_DEXT;
2847 else {
2848 rval = 0;
2849 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2850 PREV.br_startblock, PREV.br_blockcount,
2851 &i)))
2852 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002853 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 if ((error = xfs_bmbt_update(cur,
2855 PREV.br_startoff + new->br_blockcount,
2856 PREV.br_startblock + new->br_blockcount,
2857 PREV.br_blockcount - new->br_blockcount,
2858 oldext)))
2859 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002860 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002862 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 LEFT.br_startblock,
2864 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002865 LEFT.br_state);
2866 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 goto done;
2868 }
2869 break;
2870
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002871 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 /*
2873 * Setting the first part of a previous oldext extent to newext.
2874 * The left neighbor is not contiguous.
2875 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002876 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2878 xfs_bmbt_set_startoff(ep, new_endoff);
2879 xfs_bmbt_set_blockcount(ep,
2880 PREV.br_blockcount - new->br_blockcount);
2881 xfs_bmbt_set_startblock(ep,
2882 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002883 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002884
Christoph Hellwigec90c552011-05-23 08:52:53 +00002885 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 ip->i_d.di_nextents++;
2887 if (cur == NULL)
2888 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2889 else {
2890 rval = XFS_ILOG_CORE;
2891 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2892 PREV.br_startblock, PREV.br_blockcount,
2893 &i)))
2894 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002895 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 if ((error = xfs_bmbt_update(cur,
2897 PREV.br_startoff + new->br_blockcount,
2898 PREV.br_startblock + new->br_blockcount,
2899 PREV.br_blockcount - new->br_blockcount,
2900 oldext)))
2901 goto done;
2902 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002903 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002905 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 }
2907 break;
2908
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002909 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 /*
2911 * Setting the last part of a previous oldext extent to newext.
2912 * The right neighbor is contiguous with the new allocation.
2913 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002914 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 xfs_bmbt_set_blockcount(ep,
2916 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002917 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2918
2919 ++*idx;
2920
2921 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2922 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002923 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002925 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 if (cur == NULL)
2928 rval = XFS_ILOG_DEXT;
2929 else {
2930 rval = 0;
2931 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2932 PREV.br_startblock,
2933 PREV.br_blockcount, &i)))
2934 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002935 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2937 PREV.br_startblock,
2938 PREV.br_blockcount - new->br_blockcount,
2939 oldext)))
2940 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002941 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 goto done;
2943 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2944 new->br_startblock,
2945 new->br_blockcount + RIGHT.br_blockcount,
2946 newext)))
2947 goto done;
2948 }
2949 break;
2950
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002951 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 /*
2953 * Setting the last part of a previous oldext extent to newext.
2954 * The right neighbor is not contiguous.
2955 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002956 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 xfs_bmbt_set_blockcount(ep,
2958 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002959 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002960
Christoph Hellwigec90c552011-05-23 08:52:53 +00002961 ++*idx;
2962 xfs_iext_insert(ip, *idx, 1, new, state);
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 ip->i_d.di_nextents++;
2965 if (cur == NULL)
2966 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2967 else {
2968 rval = XFS_ILOG_CORE;
2969 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2970 PREV.br_startblock, PREV.br_blockcount,
2971 &i)))
2972 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002973 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2975 PREV.br_startblock,
2976 PREV.br_blockcount - new->br_blockcount,
2977 oldext)))
2978 goto done;
2979 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2980 new->br_startblock, new->br_blockcount,
2981 &i)))
2982 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002983 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002985 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002987 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
2989 break;
2990
2991 case 0:
2992 /*
2993 * Setting the middle part of a previous oldext extent to
2994 * newext. Contiguity is impossible here.
2995 * One extent becomes three extents.
2996 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002997 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 xfs_bmbt_set_blockcount(ep,
2999 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003000 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003001
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 r[0] = *new;
3003 r[1].br_startoff = new_endoff;
3004 r[1].br_blockcount =
3005 PREV.br_startoff + PREV.br_blockcount - new_endoff;
3006 r[1].br_startblock = new->br_startblock + new->br_blockcount;
3007 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003008
3009 ++*idx;
3010 xfs_iext_insert(ip, *idx, 2, &r[0], state);
3011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 ip->i_d.di_nextents += 2;
3013 if (cur == NULL)
3014 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
3015 else {
3016 rval = XFS_ILOG_CORE;
3017 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
3018 PREV.br_startblock, PREV.br_blockcount,
3019 &i)))
3020 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003021 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 /* new right extent - oldext */
3023 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
3024 r[1].br_startblock, r[1].br_blockcount,
3025 r[1].br_state)))
3026 goto done;
3027 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10003029 cur->bc_rec.b.br_blockcount =
3030 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003031 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003033 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10003034 /*
3035 * Reset the cursor to the position of the new extent
3036 * we are about to insert as we can't trust it after
3037 * the previous insert.
3038 */
3039 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
3040 new->br_startblock, new->br_blockcount,
3041 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10003043 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10003045 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003046 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003048 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
3050 break;
3051
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003052 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3053 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3054 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
3055 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
3056 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3057 case BMAP_LEFT_CONTIG:
3058 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 /*
3060 * These cases are all impossible.
3061 */
3062 ASSERT(0);
3063 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003064
3065 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003066 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003067 int tmp_logflags; /* partial log flag return val */
3068
3069 ASSERT(cur == NULL);
3070 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
3071 0, &tmp_logflags, XFS_DATA_FORK);
3072 *logflagsp |= tmp_logflags;
3073 if (error)
3074 goto done;
3075 }
3076
3077 /* clear out the allocated field, done with it now in any case. */
3078 if (cur) {
3079 cur->bc_private.b.allocated = 0;
3080 *curp = cur;
3081 }
3082
3083 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003085 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 return error;
3087#undef LEFT
3088#undef RIGHT
3089#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090}
3091
3092/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00003093 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00003095STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096xfs_bmap_add_extent_hole_delay(
3097 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003098 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00003099 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003101 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 xfs_bmbt_irec_t left; /* left neighbor extent entry */
3103 xfs_filblks_t newlen=0; /* new indirect size */
3104 xfs_filblks_t oldlen=0; /* old indirect size */
3105 xfs_bmbt_irec_t right; /* right neighbor extent entry */
3106 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003107 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003109 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003111 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003112
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 /*
3114 * Check and set flags if this segment has a left neighbor
3115 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003116 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003117 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003118 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003119
3120 if (isnullstartblock(left.br_startblock))
3121 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003123
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 /*
3125 * Check and set flags if the current (right) segment exists.
3126 * If it doesn't exist, we're converting the hole at end-of-file.
3127 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003128 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003129 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003130 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003131
3132 if (isnullstartblock(right.br_startblock))
3133 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 /*
3137 * Set contiguity flags on the left and right neighbors.
3138 * Don't let extents get too large, even if the pieces are contiguous.
3139 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003140 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
3141 left.br_startoff + left.br_blockcount == new->br_startoff &&
3142 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
3143 state |= BMAP_LEFT_CONTIG;
3144
3145 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
3146 new->br_startoff + new->br_blockcount == right.br_startoff &&
3147 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3148 (!(state & BMAP_LEFT_CONTIG) ||
3149 (left.br_blockcount + new->br_blockcount +
3150 right.br_blockcount <= MAXEXTLEN)))
3151 state |= BMAP_RIGHT_CONTIG;
3152
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 /*
3154 * Switch out based on the contiguity flags.
3155 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003156 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3157 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 /*
3159 * New allocation is contiguous with delayed allocations
3160 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003161 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003163 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 temp = left.br_blockcount + new->br_blockcount +
3165 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003166
Christoph Hellwigec90c552011-05-23 08:52:53 +00003167 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
3168 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003169 oldlen = startblockval(left.br_startblock) +
3170 startblockval(new->br_startblock) +
3171 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003173 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06003174 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00003175 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003176
Christoph Hellwigec90c552011-05-23 08:52:53 +00003177 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 break;
3179
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003180 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 /*
3182 * New allocation is contiguous with a delayed allocation
3183 * on the left.
3184 * Merge the new allocation with the left neighbor.
3185 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003186 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003188
3189 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
3190 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003191 oldlen = startblockval(left.br_startblock) +
3192 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003194 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06003195 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00003196 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 break;
3198
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003199 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 /*
3201 * New allocation is contiguous with a delayed allocation
3202 * on the right.
3203 * Merge the new allocation with the right neighbor.
3204 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003205 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003207 oldlen = startblockval(new->br_startblock) +
3208 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003210 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
3211 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003212 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003213 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 break;
3215
3216 case 0:
3217 /*
3218 * New allocation is not contiguous with another
3219 * delayed allocation.
3220 * Insert a new entry.
3221 */
3222 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003223 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 break;
3225 }
3226 if (oldlen != newlen) {
3227 ASSERT(oldlen > newlen);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003228 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00003229 (int64_t)(oldlen - newlen), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 /*
3231 * Nothing to do for disk quota accounting here.
3232 */
3233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234}
3235
3236/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003237 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 */
3239STATIC int /* error */
3240xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00003241 struct xfs_bmalloca *bma,
3242 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243{
Christoph Hellwigc6534242011-09-18 20:41:05 +00003244 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 int i; /* temp state */
3247 xfs_ifork_t *ifp; /* inode fork pointer */
3248 xfs_bmbt_irec_t left; /* left neighbor extent entry */
3249 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003250 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
Christoph Hellwigc6534242011-09-18 20:41:05 +00003253 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003254
Christoph Hellwigc6534242011-09-18 20:41:05 +00003255 ASSERT(bma->idx >= 0);
3256 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003257 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00003258 ASSERT(!bma->cur ||
3259 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003260
3261 XFS_STATS_INC(xs_add_exlist);
3262
3263 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003264 if (whichfork == XFS_ATTR_FORK)
3265 state |= BMAP_ATTRFORK;
3266
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 /*
3268 * Check and set flags if this segment has a left neighbor.
3269 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003270 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003271 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003272 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003273 if (isnullstartblock(left.br_startblock))
3274 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003276
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 /*
3278 * Check and set flags if this segment has a current value.
3279 * Not true if we're inserting into the "hole" at eof.
3280 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003281 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003282 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003283 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003284 if (isnullstartblock(right.br_startblock))
3285 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003287
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 /*
3289 * We're inserting a real allocation between "left" and "right".
3290 * Set the contiguity flags. Don't let extents get too large.
3291 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003292 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
3293 left.br_startoff + left.br_blockcount == new->br_startoff &&
3294 left.br_startblock + left.br_blockcount == new->br_startblock &&
3295 left.br_state == new->br_state &&
3296 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
3297 state |= BMAP_LEFT_CONTIG;
3298
3299 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
3300 new->br_startoff + new->br_blockcount == right.br_startoff &&
3301 new->br_startblock + new->br_blockcount == right.br_startblock &&
3302 new->br_state == right.br_state &&
3303 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3304 (!(state & BMAP_LEFT_CONTIG) ||
3305 left.br_blockcount + new->br_blockcount +
3306 right.br_blockcount <= MAXEXTLEN))
3307 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003309 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 /*
3311 * Select which case we're in here, and implement it.
3312 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003313 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3314 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 /*
3316 * New allocation is contiguous with real allocations on the
3317 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003318 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003320 --bma->idx;
3321 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3322 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 left.br_blockcount + new->br_blockcount +
3324 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003325 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003326
Christoph Hellwigc6534242011-09-18 20:41:05 +00003327 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003328
Christoph Hellwigc6534242011-09-18 20:41:05 +00003329 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3330 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3331 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003332 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003333 } else {
3334 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003335 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3336 right.br_startblock, right.br_blockcount,
3337 &i);
3338 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003339 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003340 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003341 error = xfs_btree_delete(bma->cur, &i);
3342 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003343 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003344 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003345 error = xfs_btree_decrement(bma->cur, 0, &i);
3346 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003347 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003348 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003349 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003350 left.br_startblock,
3351 left.br_blockcount +
3352 new->br_blockcount +
3353 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003354 left.br_state);
3355 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003356 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003358 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003360 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 /*
3362 * New allocation is contiguous with a real allocation
3363 * on the left.
3364 * Merge the new allocation with the left neighbor.
3365 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003366 --bma->idx;
3367 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3368 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003370 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003371
Christoph Hellwigc6534242011-09-18 20:41:05 +00003372 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003373 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003374 } else {
3375 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003376 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3377 left.br_startblock, left.br_blockcount,
3378 &i);
3379 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003380 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003381 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003382 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003383 left.br_startblock,
3384 left.br_blockcount +
3385 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003386 left.br_state);
3387 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003388 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003390 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003392 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 /*
3394 * New allocation is contiguous with a real allocation
3395 * on the right.
3396 * Merge the new allocation with the right neighbor.
3397 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003398 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3399 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003400 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 new->br_blockcount + right.br_blockcount,
3402 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003403 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003404
Christoph Hellwigc6534242011-09-18 20:41:05 +00003405 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003406 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003407 } else {
3408 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003409 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003410 right.br_startoff,
3411 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003412 right.br_blockcount, &i);
3413 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003414 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003415 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003416 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003417 new->br_startblock,
3418 new->br_blockcount +
3419 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003420 right.br_state);
3421 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003422 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003424 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
3426 case 0:
3427 /*
3428 * New allocation is not contiguous with another
3429 * real allocation.
3430 * Insert a new entry.
3431 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003432 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3433 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3434 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3435 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003436 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003437 } else {
3438 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003439 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003440 new->br_startoff,
3441 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003442 new->br_blockcount, &i);
3443 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003444 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003445 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003446 bma->cur->bc_rec.b.br_state = new->br_state;
3447 error = xfs_btree_insert(bma->cur, &i);
3448 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003449 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003450 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003454
3455 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003456 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003457 int tmp_logflags; /* partial log flag return val */
3458
Christoph Hellwigc6534242011-09-18 20:41:05 +00003459 ASSERT(bma->cur == NULL);
3460 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3461 bma->firstblock, bma->flist, &bma->cur,
3462 0, &tmp_logflags, whichfork);
3463 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003464 if (error)
3465 goto done;
3466 }
3467
3468 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003469 if (bma->cur)
3470 bma->cur->bc_private.b.allocated = 0;
3471
3472 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003473done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003474 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003475 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
Nathan Scottdd9f4382006-01-11 15:28:28 +11003478/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003479 * Functions used in the extent read, allocate and remove paths
3480 */
3481
3482/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003483 * Adjust the size of the new extent based on di_extsize and rt extsize.
3484 */
3485STATIC int
3486xfs_bmap_extsize_align(
3487 xfs_mount_t *mp,
3488 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3489 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3490 xfs_extlen_t extsz, /* align to this extent size */
3491 int rt, /* is this a realtime inode? */
3492 int eof, /* is extent at end-of-file? */
3493 int delay, /* creating delalloc extent? */
3494 int convert, /* overwriting unwritten extent? */
3495 xfs_fileoff_t *offp, /* in/out: aligned offset */
3496 xfs_extlen_t *lenp) /* in/out: aligned length */
3497{
3498 xfs_fileoff_t orig_off; /* original offset */
3499 xfs_extlen_t orig_alen; /* original length */
3500 xfs_fileoff_t orig_end; /* original off+len */
3501 xfs_fileoff_t nexto; /* next file offset */
3502 xfs_fileoff_t prevo; /* previous file offset */
3503 xfs_fileoff_t align_off; /* temp for offset */
3504 xfs_extlen_t align_alen; /* temp for length */
3505 xfs_extlen_t temp; /* temp for calculations */
3506
3507 if (convert)
3508 return 0;
3509
3510 orig_off = align_off = *offp;
3511 orig_alen = align_alen = *lenp;
3512 orig_end = orig_off + orig_alen;
3513
3514 /*
3515 * If this request overlaps an existing extent, then don't
3516 * attempt to perform any additional alignment.
3517 */
3518 if (!delay && !eof &&
3519 (orig_off >= gotp->br_startoff) &&
3520 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3521 return 0;
3522 }
3523
3524 /*
3525 * If the file offset is unaligned vs. the extent size
3526 * we need to align it. This will be possible unless
3527 * the file was previously written with a kernel that didn't
3528 * perform this alignment, or if a truncate shot us in the
3529 * foot.
3530 */
3531 temp = do_mod(orig_off, extsz);
3532 if (temp) {
3533 align_alen += temp;
3534 align_off -= temp;
3535 }
3536 /*
3537 * Same adjustment for the end of the requested area.
3538 */
3539 if ((temp = (align_alen % extsz))) {
3540 align_alen += extsz - temp;
3541 }
3542 /*
3543 * If the previous block overlaps with this proposed allocation
3544 * then move the start forward without adjusting the length.
3545 */
3546 if (prevp->br_startoff != NULLFILEOFF) {
3547 if (prevp->br_startblock == HOLESTARTBLOCK)
3548 prevo = prevp->br_startoff;
3549 else
3550 prevo = prevp->br_startoff + prevp->br_blockcount;
3551 } else
3552 prevo = 0;
3553 if (align_off != orig_off && align_off < prevo)
3554 align_off = prevo;
3555 /*
3556 * If the next block overlaps with this proposed allocation
3557 * then move the start back without adjusting the length,
3558 * but not before offset 0.
3559 * This may of course make the start overlap previous block,
3560 * and if we hit the offset 0 limit then the next block
3561 * can still overlap too.
3562 */
3563 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3564 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3565 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3566 nexto = gotp->br_startoff + gotp->br_blockcount;
3567 else
3568 nexto = gotp->br_startoff;
3569 } else
3570 nexto = NULLFILEOFF;
3571 if (!eof &&
3572 align_off + align_alen != orig_end &&
3573 align_off + align_alen > nexto)
3574 align_off = nexto > align_alen ? nexto - align_alen : 0;
3575 /*
3576 * If we're now overlapping the next or previous extent that
3577 * means we can't fit an extsz piece in this hole. Just move
3578 * the start forward to the first valid spot and set
3579 * the length so we hit the end.
3580 */
3581 if (align_off != orig_off && align_off < prevo)
3582 align_off = prevo;
3583 if (align_off + align_alen != orig_end &&
3584 align_off + align_alen > nexto &&
3585 nexto != NULLFILEOFF) {
3586 ASSERT(nexto > prevo);
3587 align_alen = nexto - align_off;
3588 }
3589
3590 /*
3591 * If realtime, and the result isn't a multiple of the realtime
3592 * extent size we need to remove blocks until it is.
3593 */
3594 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3595 /*
3596 * We're not covering the original request, or
3597 * we won't be able to once we fix the length.
3598 */
3599 if (orig_off < align_off ||
3600 orig_end > align_off + align_alen ||
3601 align_alen - temp < orig_alen)
3602 return XFS_ERROR(EINVAL);
3603 /*
3604 * Try to fix it by moving the start up.
3605 */
3606 if (align_off + temp <= orig_off) {
3607 align_alen -= temp;
3608 align_off += temp;
3609 }
3610 /*
3611 * Try to fix it by moving the end in.
3612 */
3613 else if (align_off + align_alen - temp >= orig_end)
3614 align_alen -= temp;
3615 /*
3616 * Set the start to the minimum then trim the length.
3617 */
3618 else {
3619 align_alen -= orig_off - align_off;
3620 align_off = orig_off;
3621 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3622 }
3623 /*
3624 * Result doesn't cover the request, fail it.
3625 */
3626 if (orig_off < align_off || orig_end > align_off + align_alen)
3627 return XFS_ERROR(EINVAL);
3628 } else {
3629 ASSERT(orig_off >= align_off);
3630 ASSERT(orig_end <= align_off + align_alen);
3631 }
3632
3633#ifdef DEBUG
3634 if (!eof && gotp->br_startoff != NULLFILEOFF)
3635 ASSERT(align_off + align_alen <= gotp->br_startoff);
3636 if (prevp->br_startoff != NULLFILEOFF)
3637 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3638#endif
3639
3640 *lenp = align_alen;
3641 *offp = align_off;
3642 return 0;
3643}
3644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645#define XFS_ALLOC_GAP_UNITS 4
3646
David Chinnerc2b1cba2008-04-10 12:21:40 +10003647STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003648xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
3650{
3651 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3653 xfs_mount_t *mp; /* mount point structure */
3654 int nullfb; /* true if ap->firstblock isn't set */
3655 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657#define ISVALID(x,y) \
3658 (rt ? \
3659 (x) < mp->m_sb.sb_rblocks : \
3660 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3661 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3662 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3663
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003665 nullfb = *ap->firstblock == NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003667 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 /*
3669 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003670 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003672 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3673 !isnullstartblock(ap->prev.br_startblock) &&
3674 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3675 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003676 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 /*
3678 * Adjust for the gap between prevp and us.
3679 */
Dave Chinner3a756672011-09-18 20:40:58 +00003680 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003681 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003683 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3684 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 }
3686 /*
3687 * If not at eof, then compare the two neighbor blocks.
3688 * Figure out whether either one gives us a good starting point,
3689 * and pick the better one.
3690 */
3691 else if (!ap->eof) {
3692 xfs_fsblock_t gotbno; /* right side block number */
3693 xfs_fsblock_t gotdiff=0; /* right side difference */
3694 xfs_fsblock_t prevbno; /* left side block number */
3695 xfs_fsblock_t prevdiff=0; /* left side difference */
3696
3697 /*
3698 * If there's a previous (left) block, select a requested
3699 * start block based on it.
3700 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003701 if (ap->prev.br_startoff != NULLFILEOFF &&
3702 !isnullstartblock(ap->prev.br_startblock) &&
3703 (prevbno = ap->prev.br_startblock +
3704 ap->prev.br_blockcount) &&
3705 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 /*
3707 * Calculate gap to end of previous block.
3708 */
Dave Chinner3a756672011-09-18 20:40:58 +00003709 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003710 (ap->prev.br_startoff +
3711 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 /*
3713 * Figure the startblock based on the previous block's
3714 * end and the gap size.
3715 * Heuristic!
3716 * If the gap is large relative to the piece we're
3717 * allocating, or using it gives us an invalid block
3718 * number, then just use the end of the previous block.
3719 */
Dave Chinner3a756672011-09-18 20:40:58 +00003720 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003722 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 prevbno += adjust;
3724 else
3725 prevdiff += adjust;
3726 /*
3727 * If the firstblock forbids it, can't use it,
3728 * must use default.
3729 */
3730 if (!rt && !nullfb &&
3731 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3732 prevbno = NULLFSBLOCK;
3733 }
3734 /*
3735 * No previous block or can't follow it, just default.
3736 */
3737 else
3738 prevbno = NULLFSBLOCK;
3739 /*
3740 * If there's a following (right) block, select a requested
3741 * start block based on it.
3742 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003743 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 /*
3745 * Calculate gap to start of next block.
3746 */
Dave Chinner3a756672011-09-18 20:40:58 +00003747 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 /*
3749 * Figure the startblock based on the next block's
3750 * start and the gap size.
3751 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003752 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 /*
3754 * Heuristic!
3755 * If the gap is large relative to the piece we're
3756 * allocating, or using it gives us an invalid block
3757 * number, then just use the start of the next block
3758 * offset by our length.
3759 */
Dave Chinner3a756672011-09-18 20:40:58 +00003760 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 ISVALID(gotbno - gotdiff, gotbno))
3762 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003763 else if (ISVALID(gotbno - ap->length, gotbno)) {
3764 gotbno -= ap->length;
3765 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 } else
3767 gotdiff += adjust;
3768 /*
3769 * If the firstblock forbids it, can't use it,
3770 * must use default.
3771 */
3772 if (!rt && !nullfb &&
3773 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3774 gotbno = NULLFSBLOCK;
3775 }
3776 /*
3777 * No next block, just default.
3778 */
3779 else
3780 gotbno = NULLFSBLOCK;
3781 /*
3782 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003783 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 */
3785 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003786 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003788 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003790 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003792#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003793}
3794
3795STATIC int
3796xfs_bmap_rtalloc(
3797 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
3798{
3799 xfs_alloctype_t atype = 0; /* type for allocation routines */
3800 int error; /* error return value */
3801 xfs_mount_t *mp; /* mount point structure */
3802 xfs_extlen_t prod = 0; /* product factor for allocators */
3803 xfs_extlen_t ralen = 0; /* realtime allocation length */
3804 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003805 xfs_rtblock_t rtb;
3806
3807 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10003808 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003809 prod = align / mp->m_sb.sb_rextsize;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003810 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003811 align, 1, ap->eof, 0,
Dave Chinner3a756672011-09-18 20:40:58 +00003812 ap->conv, &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003813 if (error)
3814 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00003815 ASSERT(ap->length);
3816 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003817
3818 /*
3819 * If the offset & length are not perfectly aligned
3820 * then kill prod, it will just get us in trouble.
3821 */
Dave Chinner3a756672011-09-18 20:40:58 +00003822 if (do_mod(ap->offset, align) || ap->length % align)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003823 prod = 1;
3824 /*
3825 * Set ralen to be the actual requested length in rtextents.
3826 */
Dave Chinner3a756672011-09-18 20:40:58 +00003827 ralen = ap->length / mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003828 /*
3829 * If the old value was close enough to MAXEXTLEN that
3830 * we rounded up to it, cut it back so it's valid again.
3831 * Note that if it's a really large request (bigger than
3832 * MAXEXTLEN), we don't hear about that number, and can't
3833 * adjust the starting point to match it.
3834 */
3835 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
3836 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
Christoph Hellwig04e99452011-01-25 09:06:19 +00003837
3838 /*
3839 * Lock out other modifications to the RT bitmap inode.
3840 */
Christoph Hellwig1050c712011-02-13 13:25:31 +00003841 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00003842 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwig04e99452011-01-25 09:06:19 +00003843
Nathan Scotta365bdd2006-03-14 13:34:16 +11003844 /*
3845 * If it's an allocation to an empty file at offset 0,
3846 * pick an extent that will space things out in the rt area.
3847 */
Dave Chinner3a756672011-09-18 20:40:58 +00003848 if (ap->eof && ap->offset == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10003849 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
3850
Nathan Scotta365bdd2006-03-14 13:34:16 +11003851 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
3852 if (error)
3853 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00003854 ap->blkno = rtx * mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003855 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003856 ap->blkno = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003857 }
3858
3859 xfs_bmap_adjacent(ap);
3860
3861 /*
3862 * Realtime allocation, done through xfs_rtallocate_extent.
3863 */
Dave Chinner3a756672011-09-18 20:40:58 +00003864 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
3865 do_div(ap->blkno, mp->m_sb.sb_rextsize);
3866 rtb = ap->blkno;
3867 ap->length = ralen;
3868 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003869 &ralen, atype, ap->wasdel, prod, &rtb)))
3870 return error;
3871 if (rtb == NULLFSBLOCK && prod > 1 &&
Dave Chinner3a756672011-09-18 20:40:58 +00003872 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
3873 ap->length, &ralen, atype,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003874 ap->wasdel, 1, &rtb)))
3875 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00003876 ap->blkno = rtb;
3877 if (ap->blkno != NULLFSBLOCK) {
3878 ap->blkno *= mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003879 ralen *= mp->m_sb.sb_rextsize;
Dave Chinner3a756672011-09-18 20:40:58 +00003880 ap->length = ralen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003881 ap->ip->i_d.di_nblocks += ralen;
3882 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3883 if (ap->wasdel)
3884 ap->ip->i_delayed_blks -= ralen;
3885 /*
3886 * Adjust the disk quota also. This was reserved
3887 * earlier.
3888 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003889 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003890 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
3891 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
3892 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003893 ap->length = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003894 }
3895 return 0;
3896}
3897
3898STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003899xfs_bmap_btalloc_nullfb(
3900 struct xfs_bmalloca *ap,
3901 struct xfs_alloc_arg *args,
3902 xfs_extlen_t *blen)
3903{
3904 struct xfs_mount *mp = ap->ip->i_mount;
3905 struct xfs_perag *pag;
3906 xfs_agnumber_t ag, startag;
3907 int notinit = 0;
3908 int error;
3909
3910 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3911 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3912 else
3913 args->type = XFS_ALLOCTYPE_START_BNO;
3914 args->total = ap->total;
3915
3916 /*
3917 * Search for an allocation group with a single extent large enough
3918 * for the request. If one isn't found, then adjust the minimum
3919 * allocation size to the largest space found.
3920 */
3921 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3922 if (startag == NULLAGNUMBER)
3923 startag = ag = 0;
3924
3925 pag = xfs_perag_get(mp, ag);
Dave Chinner14b064c2011-01-27 12:16:28 +11003926 while (*blen < args->maxlen) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00003927 if (!pag->pagf_init) {
3928 error = xfs_alloc_pagf_init(mp, args->tp, ag,
3929 XFS_ALLOC_FLAG_TRYLOCK);
3930 if (error) {
3931 xfs_perag_put(pag);
3932 return error;
3933 }
3934 }
3935
3936 /*
3937 * See xfs_alloc_fix_freelist...
3938 */
3939 if (pag->pagf_init) {
3940 xfs_extlen_t longest;
3941 longest = xfs_alloc_longest_free_extent(mp, pag);
3942 if (*blen < longest)
3943 *blen = longest;
3944 } else
3945 notinit = 1;
3946
3947 if (xfs_inode_is_filestream(ap->ip)) {
Dave Chinner14b064c2011-01-27 12:16:28 +11003948 if (*blen >= args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00003949 break;
3950
3951 if (ap->userdata) {
3952 /*
3953 * If startag is an invalid AG, we've
3954 * come here once before and
3955 * xfs_filestream_new_ag picked the
3956 * best currently available.
3957 *
3958 * Don't continue looping, since we
3959 * could loop forever.
3960 */
3961 if (startag == NULLAGNUMBER)
3962 break;
3963
3964 error = xfs_filestream_new_ag(ap, &ag);
3965 xfs_perag_put(pag);
3966 if (error)
3967 return error;
3968
3969 /* loop again to set 'blen'*/
3970 startag = NULLAGNUMBER;
3971 pag = xfs_perag_get(mp, ag);
3972 continue;
3973 }
3974 }
3975 if (++ag == mp->m_sb.sb_agcount)
3976 ag = 0;
3977 if (ag == startag)
3978 break;
3979 xfs_perag_put(pag);
3980 pag = xfs_perag_get(mp, ag);
3981 }
3982 xfs_perag_put(pag);
3983
3984 /*
3985 * Since the above loop did a BUF_TRYLOCK, it is
3986 * possible that there is space for this request.
3987 */
3988 if (notinit || *blen < ap->minlen)
3989 args->minlen = ap->minlen;
3990 /*
3991 * If the best seen length is less than the request
3992 * length, use the best as the minimum.
3993 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003994 else if (*blen < args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00003995 args->minlen = *blen;
3996 /*
Dave Chinner14b064c2011-01-27 12:16:28 +11003997 * Otherwise we've seen an extent as big as maxlen,
Christoph Hellwigc467c042010-02-15 23:34:42 +00003998 * use that as the minimum.
3999 */
4000 else
Dave Chinner14b064c2011-01-27 12:16:28 +11004001 args->minlen = args->maxlen;
Christoph Hellwigc467c042010-02-15 23:34:42 +00004002
4003 /*
4004 * set the failure fallback case to look in the selected
4005 * AG as the stream may have moved.
4006 */
4007 if (xfs_inode_is_filestream(ap->ip))
Dave Chinner3a756672011-09-18 20:40:58 +00004008 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00004009
4010 return 0;
4011}
4012
4013STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11004014xfs_bmap_btalloc(
4015 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
4016{
4017 xfs_mount_t *mp; /* mount point structure */
4018 xfs_alloctype_t atype = 0; /* type for allocation routines */
4019 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11004020 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00004021 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004022 xfs_alloc_arg_t args;
4023 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004024 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004025 int nullfb; /* true if ap->firstblock isn't set */
4026 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004027 int tryagain;
4028 int error;
4029
Dave Chinnera99ebf42011-12-01 11:24:20 +00004030 ASSERT(ap->length);
4031
Nathan Scotta365bdd2006-03-14 13:34:16 +11004032 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10004033 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004034 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004035 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11004036 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00004037 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11004038 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00004039 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11004040 }
Dave Chinner0937e0f2011-09-18 20:40:57 +00004041 nullfb = *ap->firstblock == NULLFSBLOCK;
4042 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10004043 if (nullfb) {
4044 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
4045 ag = xfs_filestream_lookup_ag(ap->ip);
4046 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00004047 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10004048 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00004049 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10004050 }
4051 } else
Dave Chinner3a756672011-09-18 20:40:58 +00004052 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004053
4054 xfs_bmap_adjacent(ap);
4055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 /*
Dave Chinner3a756672011-09-18 20:40:58 +00004057 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 * it's in the right allocation group.
4059 */
Dave Chinner3a756672011-09-18 20:40:58 +00004060 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 ;
4062 else
Dave Chinner3a756672011-09-18 20:40:58 +00004063 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 * Normal allocation, done through xfs_alloc_vextent.
4066 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11004067 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05004068 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11004069 args.tp = ap->tp;
4070 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00004071 args.fsbno = ap->blkno;
Dave Chinner14b064c2011-01-27 12:16:28 +11004072
4073 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner3a756672011-09-18 20:40:58 +00004074 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
Dave Chinner0937e0f2011-09-18 20:40:57 +00004075 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004076 blen = 0;
4077 if (nullfb) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00004078 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
4079 if (error)
4080 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004081 } else if (ap->flist->xbf_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10004082 if (xfs_inode_is_filestream(ap->ip))
4083 args.type = XFS_ALLOCTYPE_FIRST_AG;
4084 else
4085 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004086 args.total = args.minlen = ap->minlen;
4087 } else {
4088 args.type = XFS_ALLOCTYPE_NEAR_BNO;
4089 args.total = ap->total;
4090 args.minlen = ap->minlen;
4091 }
David Chinner957d0eb2007-06-18 16:50:37 +10004092 /* apply extent size hints if obtained earlier */
4093 if (unlikely(align)) {
4094 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00004095 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11004096 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11004097 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11004098 args.prod = 1;
4099 args.mod = 0;
4100 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11004101 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00004102 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11004103 args.mod = (xfs_extlen_t)(args.prod - args.mod);
4104 }
4105 /*
4106 * If we are not low on available data blocks, and the
4107 * underlying logical volume manager is a stripe, and
4108 * the file offset is zero then try to allocate data
4109 * blocks on stripe unit boundary.
4110 * NOTE: ap->aeof is only set if the allocation length
4111 * is >= the stripe unit and the allocation offset is
4112 * at the end of file.
4113 */
Dave Chinner0937e0f2011-09-18 20:40:57 +00004114 if (!ap->flist->xbf_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00004115 if (!ap->offset) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11004116 args.alignment = mp->m_dalign;
4117 atype = args.type;
4118 isaligned = 1;
4119 /*
4120 * Adjust for alignment
4121 */
Dave Chinner14b064c2011-01-27 12:16:28 +11004122 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11004123 args.minlen = blen - args.alignment;
4124 args.minalignslop = 0;
4125 } else {
4126 /*
4127 * First try an exact bno allocation.
4128 * If it fails then do a near or start bno
4129 * allocation with alignment turned on.
4130 */
4131 atype = args.type;
4132 tryagain = 1;
4133 args.type = XFS_ALLOCTYPE_THIS_BNO;
4134 args.alignment = 1;
4135 /*
4136 * Compute the minlen+alignment for the
4137 * next case. Set slop so that the value
4138 * of minlen+alignment+slop doesn't go up
4139 * between the calls.
4140 */
Dave Chinner14b064c2011-01-27 12:16:28 +11004141 if (blen > mp->m_dalign && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11004142 nextminlen = blen - mp->m_dalign;
4143 else
4144 nextminlen = args.minlen;
4145 if (nextminlen + mp->m_dalign > args.minlen + 1)
4146 args.minalignslop =
4147 nextminlen + mp->m_dalign -
4148 args.minlen - 1;
4149 else
4150 args.minalignslop = 0;
4151 }
4152 } else {
4153 args.alignment = 1;
4154 args.minalignslop = 0;
4155 }
4156 args.minleft = ap->minleft;
4157 args.wasdel = ap->wasdel;
4158 args.isfl = 0;
4159 args.userdata = ap->userdata;
4160 if ((error = xfs_alloc_vextent(&args)))
4161 return error;
4162 if (tryagain && args.fsbno == NULLFSBLOCK) {
4163 /*
4164 * Exact allocation failed. Now try with alignment
4165 * turned on.
4166 */
4167 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00004168 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004169 args.alignment = mp->m_dalign;
4170 args.minlen = nextminlen;
4171 args.minalignslop = 0;
4172 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 if ((error = xfs_alloc_vextent(&args)))
4174 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004175 }
4176 if (isaligned && args.fsbno == NULLFSBLOCK) {
4177 /*
4178 * allocation failed, so turn off alignment and
4179 * try again.
4180 */
4181 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00004182 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004183 args.alignment = 0;
4184 if ((error = xfs_alloc_vextent(&args)))
4185 return error;
4186 }
4187 if (args.fsbno == NULLFSBLOCK && nullfb &&
4188 args.minlen > ap->minlen) {
4189 args.minlen = ap->minlen;
4190 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00004191 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004192 if ((error = xfs_alloc_vextent(&args)))
4193 return error;
4194 }
4195 if (args.fsbno == NULLFSBLOCK && nullfb) {
4196 args.fsbno = 0;
4197 args.type = XFS_ALLOCTYPE_FIRST_AG;
4198 args.total = ap->minlen;
4199 args.minleft = 0;
4200 if ((error = xfs_alloc_vextent(&args)))
4201 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004202 ap->flist->xbf_low = 1;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004203 }
4204 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004205 /*
4206 * check the allocation happened at the same or higher AG than
4207 * the first block that was allocated.
4208 */
4209 ASSERT(*ap->firstblock == NULLFSBLOCK ||
4210 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
4211 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
4212 (ap->flist->xbf_low &&
4213 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
4214 XFS_FSB_TO_AGNO(mp, args.fsbno)));
4215
Dave Chinner3a756672011-09-18 20:40:58 +00004216 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004217 if (*ap->firstblock == NULLFSBLOCK)
4218 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004219 ASSERT(nullfb || fb_agno == args.agno ||
Dave Chinner0937e0f2011-09-18 20:40:57 +00004220 (ap->flist->xbf_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00004221 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004222 ap->ip->i_d.di_nblocks += args.len;
4223 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
4224 if (ap->wasdel)
4225 ap->ip->i_delayed_blks -= args.len;
4226 /*
4227 * Adjust the disk quota also. This was reserved
4228 * earlier.
4229 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02004230 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11004231 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
4232 XFS_TRANS_DQ_BCOUNT,
4233 (long) args.len);
4234 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00004235 ap->blkno = NULLFSBLOCK;
4236 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 }
4238 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004239}
4240
4241/*
4242 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
4243 * It figures out where to ask the underlying allocator to put the new extent.
4244 */
4245STATIC int
4246xfs_bmap_alloc(
4247 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
4248{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11004249 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11004250 return xfs_bmap_rtalloc(ap);
4251 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252}
4253
4254/*
Dave Chinneraef9a892011-09-18 20:40:44 +00004255 * Trim the returned map to the required bounds
4256 */
4257STATIC void
4258xfs_bmapi_trim_map(
4259 struct xfs_bmbt_irec *mval,
4260 struct xfs_bmbt_irec *got,
4261 xfs_fileoff_t *bno,
4262 xfs_filblks_t len,
4263 xfs_fileoff_t obno,
4264 xfs_fileoff_t end,
4265 int n,
4266 int flags)
4267{
4268 if ((flags & XFS_BMAPI_ENTIRE) ||
4269 got->br_startoff + got->br_blockcount <= obno) {
4270 *mval = *got;
4271 if (isnullstartblock(got->br_startblock))
4272 mval->br_startblock = DELAYSTARTBLOCK;
4273 return;
4274 }
4275
4276 if (obno > *bno)
4277 *bno = obno;
4278 ASSERT((*bno >= obno) || (n == 0));
4279 ASSERT(*bno < end);
4280 mval->br_startoff = *bno;
4281 if (isnullstartblock(got->br_startblock))
4282 mval->br_startblock = DELAYSTARTBLOCK;
4283 else
4284 mval->br_startblock = got->br_startblock +
4285 (*bno - got->br_startoff);
4286 /*
4287 * Return the minimum of what we got and what we asked for for
4288 * the length. We can use the len variable here because it is
4289 * modified below and we could have been there before coming
4290 * here if the first part of the allocation didn't overlap what
4291 * was asked for.
4292 */
4293 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4294 got->br_blockcount - (*bno - got->br_startoff));
4295 mval->br_state = got->br_state;
4296 ASSERT(mval->br_blockcount <= len);
4297 return;
4298}
4299
4300/*
4301 * Update and validate the extent map to return
4302 */
4303STATIC void
4304xfs_bmapi_update_map(
4305 struct xfs_bmbt_irec **map,
4306 xfs_fileoff_t *bno,
4307 xfs_filblks_t *len,
4308 xfs_fileoff_t obno,
4309 xfs_fileoff_t end,
4310 int *n,
4311 int flags)
4312{
4313 xfs_bmbt_irec_t *mval = *map;
4314
4315 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4316 ((mval->br_startoff + mval->br_blockcount) <= end));
4317 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4318 (mval->br_startoff < obno));
4319
4320 *bno = mval->br_startoff + mval->br_blockcount;
4321 *len = end - *bno;
4322 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4323 /* update previous map with new information */
4324 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4325 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4326 ASSERT(mval->br_state == mval[-1].br_state);
4327 mval[-1].br_blockcount = mval->br_blockcount;
4328 mval[-1].br_state = mval->br_state;
4329 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4330 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4331 mval[-1].br_startblock != HOLESTARTBLOCK &&
4332 mval->br_startblock == mval[-1].br_startblock +
4333 mval[-1].br_blockcount &&
4334 ((flags & XFS_BMAPI_IGSTATE) ||
4335 mval[-1].br_state == mval->br_state)) {
4336 ASSERT(mval->br_startoff ==
4337 mval[-1].br_startoff + mval[-1].br_blockcount);
4338 mval[-1].br_blockcount += mval->br_blockcount;
4339 } else if (*n > 0 &&
4340 mval->br_startblock == DELAYSTARTBLOCK &&
4341 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4342 mval->br_startoff ==
4343 mval[-1].br_startoff + mval[-1].br_blockcount) {
4344 mval[-1].br_blockcount += mval->br_blockcount;
4345 mval[-1].br_state = mval->br_state;
4346 } else if (!((*n == 0) &&
4347 ((mval->br_startoff + mval->br_blockcount) <=
4348 obno))) {
4349 mval++;
4350 (*n)++;
4351 }
4352 *map = mval;
4353}
4354
4355/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004356 * Map file blocks to filesystem blocks without allocation.
4357 */
4358int
4359xfs_bmapi_read(
4360 struct xfs_inode *ip,
4361 xfs_fileoff_t bno,
4362 xfs_filblks_t len,
4363 struct xfs_bmbt_irec *mval,
4364 int *nmap,
4365 int flags)
4366{
4367 struct xfs_mount *mp = ip->i_mount;
4368 struct xfs_ifork *ifp;
4369 struct xfs_bmbt_irec got;
4370 struct xfs_bmbt_irec prev;
4371 xfs_fileoff_t obno;
4372 xfs_fileoff_t end;
4373 xfs_extnum_t lastx;
4374 int error;
4375 int eof;
4376 int n = 0;
4377 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4378 XFS_ATTR_FORK : XFS_DATA_FORK;
4379
4380 ASSERT(*nmap >= 1);
4381 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4382 XFS_BMAPI_IGSTATE)));
4383
4384 if (unlikely(XFS_TEST_ERROR(
4385 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4386 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4387 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4388 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4389 return XFS_ERROR(EFSCORRUPTED);
4390 }
4391
4392 if (XFS_FORCED_SHUTDOWN(mp))
4393 return XFS_ERROR(EIO);
4394
4395 XFS_STATS_INC(xs_blk_mapr);
4396
4397 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004398
4399 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4400 error = xfs_iread_extents(NULL, ip, whichfork);
4401 if (error)
4402 return error;
4403 }
4404
4405 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4406 end = bno + len;
4407 obno = bno;
4408
4409 while (bno < end && n < *nmap) {
4410 /* Reading past eof, act as though there's a hole up to end. */
4411 if (eof)
4412 got.br_startoff = end;
4413 if (got.br_startoff > bno) {
4414 /* Reading in a hole. */
4415 mval->br_startoff = bno;
4416 mval->br_startblock = HOLESTARTBLOCK;
4417 mval->br_blockcount =
4418 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4419 mval->br_state = XFS_EXT_NORM;
4420 bno += mval->br_blockcount;
4421 len -= mval->br_blockcount;
4422 mval++;
4423 n++;
4424 continue;
4425 }
4426
4427 /* set up the extent map to return. */
4428 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4429 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4430
4431 /* If we're done, stop now. */
4432 if (bno >= end || n >= *nmap)
4433 break;
4434
4435 /* Else go on to the next record. */
4436 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4437 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4438 else
4439 eof = 1;
4440 }
4441 *nmap = n;
4442 return 0;
4443}
4444
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004445STATIC int
4446xfs_bmapi_reserve_delalloc(
4447 struct xfs_inode *ip,
4448 xfs_fileoff_t aoff,
4449 xfs_filblks_t len,
4450 struct xfs_bmbt_irec *got,
4451 struct xfs_bmbt_irec *prev,
4452 xfs_extnum_t *lastx,
4453 int eof)
4454{
4455 struct xfs_mount *mp = ip->i_mount;
4456 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4457 xfs_extlen_t alen;
4458 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004459 char rt = XFS_IS_REALTIME_INODE(ip);
4460 xfs_extlen_t extsz;
4461 int error;
4462
4463 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4464 if (!eof)
4465 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4466
4467 /* Figure out the extent size, adjust alen */
4468 extsz = xfs_get_extsz_hint(ip);
4469 if (extsz) {
4470 /*
4471 * Make sure we don't exceed a single extent length when we
4472 * align the extent by reducing length we are going to
4473 * allocate by the maximum amount extent size aligment may
4474 * require.
4475 */
4476 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4477 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4478 1, 0, &aoff, &alen);
4479 ASSERT(!error);
4480 }
4481
4482 if (rt)
4483 extsz = alen / mp->m_sb.sb_rextsize;
4484
4485 /*
4486 * Make a transaction-less quota reservation for delayed allocation
4487 * blocks. This number gets adjusted later. We return if we haven't
4488 * allocated blocks already inside this loop.
4489 */
4490 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4491 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4492 if (error)
4493 return error;
4494
4495 /*
4496 * Split changing sb for alen and indlen since they could be coming
4497 * from different places.
4498 */
4499 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4500 ASSERT(indlen > 0);
4501
4502 if (rt) {
4503 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4504 -((int64_t)extsz), 0);
4505 } else {
4506 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4507 -((int64_t)alen), 0);
4508 }
4509
4510 if (error)
4511 goto out_unreserve_quota;
4512
4513 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4514 -((int64_t)indlen), 0);
4515 if (error)
4516 goto out_unreserve_blocks;
4517
4518
4519 ip->i_delayed_blks += alen;
4520
4521 got->br_startoff = aoff;
4522 got->br_startblock = nullstartblock(indlen);
4523 got->br_blockcount = alen;
4524 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004525 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004526
4527 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004528 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4529 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004530 */
4531 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4532
4533 ASSERT(got->br_startoff <= aoff);
4534 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4535 ASSERT(isnullstartblock(got->br_startblock));
4536 ASSERT(got->br_state == XFS_EXT_NORM);
4537 return 0;
4538
4539out_unreserve_blocks:
4540 if (rt)
4541 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4542 else
4543 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4544out_unreserve_quota:
4545 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004546 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004547 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4548 return error;
4549}
4550
Dave Chinner5c8ed202011-09-18 20:40:45 +00004551/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004552 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4553 */
4554int
4555xfs_bmapi_delay(
4556 struct xfs_inode *ip, /* incore inode */
4557 xfs_fileoff_t bno, /* starting file offs. mapped */
4558 xfs_filblks_t len, /* length to map in file */
4559 struct xfs_bmbt_irec *mval, /* output: map values */
4560 int *nmap, /* i/o: mval size/count */
4561 int flags) /* XFS_BMAPI_... */
4562{
4563 struct xfs_mount *mp = ip->i_mount;
4564 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4565 struct xfs_bmbt_irec got; /* current file extent record */
4566 struct xfs_bmbt_irec prev; /* previous file extent record */
4567 xfs_fileoff_t obno; /* old block number (offset) */
4568 xfs_fileoff_t end; /* end of mapped file region */
4569 xfs_extnum_t lastx; /* last useful extent number */
4570 int eof; /* we've hit the end of extents */
4571 int n = 0; /* current extent index */
4572 int error = 0;
4573
4574 ASSERT(*nmap >= 1);
4575 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4576 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4577
4578 if (unlikely(XFS_TEST_ERROR(
4579 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4580 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4581 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4582 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4583 return XFS_ERROR(EFSCORRUPTED);
4584 }
4585
4586 if (XFS_FORCED_SHUTDOWN(mp))
4587 return XFS_ERROR(EIO);
4588
4589 XFS_STATS_INC(xs_blk_mapw);
4590
4591 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4592 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4593 if (error)
4594 return error;
4595 }
4596
4597 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4598 end = bno + len;
4599 obno = bno;
4600
4601 while (bno < end && n < *nmap) {
4602 if (eof || got.br_startoff > bno) {
4603 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4604 &prev, &lastx, eof);
4605 if (error) {
4606 if (n == 0) {
4607 *nmap = 0;
4608 return error;
4609 }
4610 break;
4611 }
4612 }
4613
4614 /* set up the extent map to return. */
4615 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4616 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4617
4618 /* If we're done, stop now. */
4619 if (bno >= end || n >= *nmap)
4620 break;
4621
4622 /* Else go on to the next record. */
4623 prev = got;
4624 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4625 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4626 else
4627 eof = 1;
4628 }
4629
4630 *nmap = n;
4631 return 0;
4632}
4633
4634
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004635STATIC int
Dave Chinnere04426b2012-10-05 11:06:59 +10004636__xfs_bmapi_allocate(
4637 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004638{
4639 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004640 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004641 XFS_ATTR_FORK : XFS_DATA_FORK;
4642 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004643 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004644 int error;
4645 int rt;
4646
Dave Chinnera99ebf42011-12-01 11:24:20 +00004647 ASSERT(bma->length > 0);
4648
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004649 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip);
4650
4651 /*
4652 * For the wasdelay case, we could also just allocate the stuff asked
4653 * for in this bmap call but that wouldn't be as good.
4654 */
4655 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004656 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4657 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004658 if (bma->idx != NULLEXTNUM && bma->idx) {
4659 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004660 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004661 }
4662 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004663 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004664 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004665 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004666 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004667 }
4668
4669 /*
4670 * Indicate if this is the first user data in the file, or just any
4671 * user data.
4672 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004673 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004674 bma->userdata = (bma->offset == 0) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004675 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4676 }
4677
Dave Chinnere04426b2012-10-05 11:06:59 +10004678 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004679
4680 /*
4681 * Only want to do the alignment at the eof if it is userdata and
4682 * allocation length is larger than a stripe unit.
4683 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004684 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004685 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004686 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004687 if (error)
4688 return error;
4689 }
4690
4691 error = xfs_bmap_alloc(bma);
4692 if (error)
4693 return error;
4694
Dave Chinner0937e0f2011-09-18 20:40:57 +00004695 if (bma->flist->xbf_low)
4696 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004697 if (bma->cur)
4698 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004699 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004700 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004701 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4702 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4703 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4704 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004705 }
4706 /*
4707 * Bump the number of extents we've allocated
4708 * in this call.
4709 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004710 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004711
Dave Chinner29c8d172011-09-18 20:41:00 +00004712 if (bma->cur)
4713 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004714 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4715
Dave Chinner963c30c2011-09-18 20:40:59 +00004716 bma->got.br_startoff = bma->offset;
4717 bma->got.br_startblock = bma->blkno;
4718 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004719 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004720
4721 /*
4722 * A wasdelay extent has been initialized, so shouldn't be flagged
4723 * as unwritten.
4724 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004725 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004726 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004727 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004728
Christoph Hellwigc6534242011-09-18 20:41:05 +00004729 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004730 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004731 else
4732 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004733
Christoph Hellwigc315c902011-09-18 20:41:02 +00004734 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004735 if (error)
4736 return error;
4737
4738 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004739 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4740 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4741 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004742 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004743 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004744
Dave Chinner963c30c2011-09-18 20:40:59 +00004745 ASSERT(bma->got.br_startoff <= bma->offset);
4746 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4747 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004748 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4749 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004750 return 0;
4751}
4752
Dave Chinnere04426b2012-10-05 11:06:59 +10004753static void
4754xfs_bmapi_allocate_worker(
4755 struct work_struct *work)
4756{
4757 struct xfs_bmalloca *args = container_of(work,
4758 struct xfs_bmalloca, work);
4759 unsigned long pflags;
4760
4761 /* we are in a transaction context here */
4762 current_set_flags_nested(&pflags, PF_FSTRANS);
4763
4764 args->result = __xfs_bmapi_allocate(args);
4765 complete(args->done);
4766
4767 current_restore_flags_nested(&pflags, PF_FSTRANS);
4768}
4769
4770/*
4771 * Some allocation requests often come in with little stack to work on. Push
4772 * them off to a worker thread so there is lots of stack to use. Otherwise just
4773 * call directly to avoid the context switch overhead here.
4774 */
4775int
4776xfs_bmapi_allocate(
4777 struct xfs_bmalloca *args)
4778{
4779 DECLARE_COMPLETION_ONSTACK(done);
4780
4781 if (!args->stack_switch)
4782 return __xfs_bmapi_allocate(args);
4783
4784
4785 args->done = &done;
4786 INIT_WORK_ONSTACK(&args->work, xfs_bmapi_allocate_worker);
4787 queue_work(xfs_alloc_wq, &args->work);
4788 wait_for_completion(&done);
4789 return args->result;
4790}
4791
Dave Chinnerb447fe52011-09-18 20:40:51 +00004792STATIC int
4793xfs_bmapi_convert_unwritten(
4794 struct xfs_bmalloca *bma,
4795 struct xfs_bmbt_irec *mval,
4796 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004797 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004798{
4799 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4800 XFS_ATTR_FORK : XFS_DATA_FORK;
4801 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004802 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004803 int error;
4804
Dave Chinnerb447fe52011-09-18 20:40:51 +00004805 /* check if we need to do unwritten->real conversion */
4806 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4807 (flags & XFS_BMAPI_PREALLOC))
4808 return 0;
4809
4810 /* check if we need to do real->unwritten conversion */
4811 if (mval->br_state == XFS_EXT_NORM &&
4812 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4813 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4814 return 0;
4815
4816 /*
4817 * Modify (by adding) the state flag, if writing.
4818 */
4819 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004820 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4821 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004822 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004823 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4824 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004825 }
4826 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4827 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4828
Dave Chinnere0c3da52011-09-18 20:41:01 +00004829 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004830 &bma->cur, mval, bma->firstblock, bma->flist,
4831 &tmp_logflags);
4832 bma->logflags |= tmp_logflags;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004833 if (error)
4834 return error;
4835
4836 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004837 * Update our extent pointer, given that
4838 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4839 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004840 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004841 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004842
4843 /*
4844 * We may have combined previously unwritten space with written space,
4845 * so generate another request.
4846 */
4847 if (mval->br_blockcount < len)
4848 return EAGAIN;
4849 return 0;
4850}
4851
Christoph Hellwig44032802011-09-18 20:40:48 +00004852/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004853 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4854 * extent state if necessary. Details behaviour is controlled by the flags
4855 * parameter. Only allocates blocks from a single allocation group, to avoid
4856 * locking problems.
4857 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 * The returned value in "firstblock" from the first call in a transaction
4859 * must be remembered and presented to subsequent calls in "firstblock".
4860 * An upper bound for the number of blocks to be allocated is supplied to
4861 * the first call in "total"; if no allocation group has that many free
4862 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4863 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004864int
4865xfs_bmapi_write(
4866 struct xfs_trans *tp, /* transaction pointer */
4867 struct xfs_inode *ip, /* incore inode */
4868 xfs_fileoff_t bno, /* starting file offs. mapped */
4869 xfs_filblks_t len, /* length to map in file */
4870 int flags, /* XFS_BMAPI_... */
4871 xfs_fsblock_t *firstblock, /* first allocated block
4872 controls a.g. for allocs */
4873 xfs_extlen_t total, /* total blocks needed */
4874 struct xfs_bmbt_irec *mval, /* output: map values */
4875 int *nmap, /* i/o: mval size/count */
4876 struct xfs_bmap_free *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004878 struct xfs_mount *mp = ip->i_mount;
4879 struct xfs_ifork *ifp;
4880 struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004881 xfs_fileoff_t end; /* end of mapped file region */
4882 int eof; /* after the end of extents */
4883 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004884 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004885 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004886 int whichfork; /* data or attr fork */
4887 char inhole; /* current location is hole in file */
4888 char wasdelay; /* old extent was delayed */
4889
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004891 xfs_fileoff_t orig_bno; /* original block number value */
4892 int orig_flags; /* original flags arg value */
4893 xfs_filblks_t orig_len; /* original value of len arg */
4894 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4895 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896
4897 orig_bno = bno;
4898 orig_len = len;
4899 orig_flags = flags;
4900 orig_mval = mval;
4901 orig_nmap = *nmap;
4902#endif
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004903
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004905 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4906 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4907 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004908 ASSERT(len > 0);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004909
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4911 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004912
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 if (unlikely(XFS_TEST_ERROR(
4914 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4915 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4916 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4917 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004918 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 return XFS_ERROR(EFSCORRUPTED);
4920 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004921
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 if (XFS_FORCED_SHUTDOWN(mp))
4923 return XFS_ERROR(EIO);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004924
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004926
4927 XFS_STATS_INC(xs_blk_mapw);
4928
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
Dave Chinner1e823792013-02-11 15:58:13 +11004930 /*
4931 * XXX (dgc): This assumes we are only called for inodes that
4932 * contain content neutral data in local format. Anything that
4933 * contains caller-specific data in local format that needs
4934 * transformation to move to a block format needs to do the
4935 * conversion to extent format itself.
4936 *
4937 * Directory data forks and attribute forks handle this
4938 * themselves, but with the addition of metadata verifiers every
4939 * data fork in local format now contains caller specific data
4940 * and as such conversion through this function is likely to be
4941 * broken.
4942 *
4943 * The only likely user of this branch is for remote symlinks,
4944 * but we cannot overwrite the data fork contents of the symlink
4945 * (EEXIST occurs higher up the stack) and so it will never go
4946 * from local format to extent format here. Hence I don't think
4947 * this branch is ever executed intentionally and we should
4948 * consider removing it and asserting that xfs_bmapi_write()
4949 * cannot be called directly on local format forks. i.e. callers
4950 * are completely responsible for local to extent format
4951 * conversion, not xfs_bmapi_write().
4952 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004953 error = xfs_bmap_local_to_extents(tp, ip, firstblock, total,
Dave Chinner1e823792013-02-11 15:58:13 +11004954 &bma.logflags, whichfork,
4955 xfs_bmap_local_to_extents_init_fn);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004956 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957 goto error0;
4958 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004959
4960 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004962 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004964 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004965 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004966 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004967 }
4968
4969 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4970 error = xfs_iread_extents(tp, ip, whichfork);
4971 if (error)
4972 goto error0;
4973 }
4974
Dave Chinnere0c3da52011-09-18 20:41:01 +00004975 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004976 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 n = 0;
4978 end = bno + len;
4979 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004980
4981 bma.tp = tp;
4982 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004983 bma.total = total;
4984 bma.userdata = 0;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004985 bma.flist = flist;
4986 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004987
Brian Foster9e96fe62013-01-17 13:11:29 -05004988 if (flags & XFS_BMAPI_STACK_SWITCH)
4989 bma.stack_switch = 1;
4990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004992 inhole = eof || bma.got.br_startoff > bno;
4993 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004994
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 /*
4996 * First, deal with the hole before the allocated space
4997 * that we found, if any.
4998 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004999 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00005000 bma.eof = eof;
5001 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
5002 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00005003 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10005004 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00005005
Dave Chinnera99ebf42011-12-01 11:24:20 +00005006 /*
5007 * There's a 32/64 bit type mismatch between the
5008 * allocation length request (which can be 64 bits in
5009 * length) and the bma length request, which is
5010 * xfs_extlen_t and therefore 32 bits. Hence we have to
5011 * check for 32-bit overflows and handle them here.
5012 */
5013 if (len > (xfs_filblks_t)MAXEXTLEN)
5014 bma.length = MAXEXTLEN;
5015 else
5016 bma.length = len;
5017
5018 ASSERT(len > 0);
5019 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10005020 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 if (error)
5022 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00005023 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00005024 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 }
Christoph Hellwig44032802011-09-18 20:40:48 +00005026
Dave Chinneraef9a892011-09-18 20:40:44 +00005027 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00005028 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
5029 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030
Dave Chinnerb447fe52011-09-18 20:40:51 +00005031 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005032 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005033 if (error == EAGAIN)
5034 continue;
5035 if (error)
5036 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
Dave Chinneraef9a892011-09-18 20:40:44 +00005038 /* update the extent map to return */
5039 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
5040
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 /*
5042 * If we're done, stop now. Stop when we've allocated
5043 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5044 * the transaction may get too big.
5045 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00005046 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005048
5049 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00005050 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00005051 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
5052 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
5053 &bma.got);
5054 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00005055 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005058
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 /*
5060 * Transform from btree to extents, give it cur.
5061 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005062 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00005063 int tmp_logflags = 0;
5064
Dave Chinner29c8d172011-09-18 20:41:00 +00005065 ASSERT(bma.cur);
5066 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00005068 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 if (error)
5070 goto error0;
5071 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005072
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005074 XFS_IFORK_NEXTENTS(ip, whichfork) >
5075 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077error0:
5078 /*
5079 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005080 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005082 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00005084 bma.logflags &= ~xfs_ilog_fext(whichfork);
5085 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00005087 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 /*
5089 * Log whatever the flags say, even if error. Otherwise we might miss
5090 * detecting a case where the data is changed, there's an error,
5091 * and it's not logged so we don't shutdown when we should.
5092 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005093 if (bma.logflags)
5094 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005095
Dave Chinner29c8d172011-09-18 20:41:00 +00005096 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 if (!error) {
5098 ASSERT(*firstblock == NULLFSBLOCK ||
5099 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5100 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00005101 bma.cur->bc_private.b.firstblock) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 (flist->xbf_low &&
5103 XFS_FSB_TO_AGNO(mp, *firstblock) <
5104 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00005105 bma.cur->bc_private.b.firstblock)));
5106 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 }
Dave Chinner29c8d172011-09-18 20:41:00 +00005108 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5110 }
5111 if (!error)
5112 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5113 orig_nmap, *nmap);
5114 return error;
5115}
5116
5117/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005118 * Called by xfs_bmapi to update file extent records and the btree
5119 * after removing space (or undoing a delayed allocation).
5120 */
5121STATIC int /* error */
5122xfs_bmap_del_extent(
5123 xfs_inode_t *ip, /* incore inode pointer */
5124 xfs_trans_t *tp, /* current transaction pointer */
5125 xfs_extnum_t *idx, /* extent number to update/delete */
5126 xfs_bmap_free_t *flist, /* list of extents to be freed */
5127 xfs_btree_cur_t *cur, /* if null, not a btree */
5128 xfs_bmbt_irec_t *del, /* data to remove from extents */
5129 int *logflagsp, /* inode logging flags */
5130 int whichfork) /* data or attr fork */
5131{
5132 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5133 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5134 xfs_fsblock_t del_endblock=0; /* first block past del */
5135 xfs_fileoff_t del_endoff; /* first offset past del */
5136 int delay; /* current block is delayed allocated */
5137 int do_fx; /* free extent at end of routine */
5138 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5139 int error; /* error return value */
5140 int flags; /* inode logging flags */
5141 xfs_bmbt_irec_t got; /* current extent entry */
5142 xfs_fileoff_t got_endoff; /* first offset past got */
5143 int i; /* temp state */
5144 xfs_ifork_t *ifp; /* inode fork pointer */
5145 xfs_mount_t *mp; /* mount structure */
5146 xfs_filblks_t nblks; /* quota/sb block count */
5147 xfs_bmbt_irec_t new; /* new record to be inserted */
5148 /* REFERENCED */
5149 uint qfield; /* quota field to update */
5150 xfs_filblks_t temp; /* for indirect length calculations */
5151 xfs_filblks_t temp2; /* for indirect length calculations */
5152 int state = 0;
5153
5154 XFS_STATS_INC(xs_del_exlist);
5155
5156 if (whichfork == XFS_ATTR_FORK)
5157 state |= BMAP_ATTRFORK;
5158
5159 mp = ip->i_mount;
5160 ifp = XFS_IFORK_PTR(ip, whichfork);
5161 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
5162 (uint)sizeof(xfs_bmbt_rec_t)));
5163 ASSERT(del->br_blockcount > 0);
5164 ep = xfs_iext_get_ext(ifp, *idx);
5165 xfs_bmbt_get_all(ep, &got);
5166 ASSERT(got.br_startoff <= del->br_startoff);
5167 del_endoff = del->br_startoff + del->br_blockcount;
5168 got_endoff = got.br_startoff + got.br_blockcount;
5169 ASSERT(got_endoff >= del_endoff);
5170 delay = isnullstartblock(got.br_startblock);
5171 ASSERT(isnullstartblock(del->br_startblock) == delay);
5172 flags = 0;
5173 qfield = 0;
5174 error = 0;
5175 /*
5176 * If deleting a real allocation, must free up the disk space.
5177 */
5178 if (!delay) {
5179 flags = XFS_ILOG_CORE;
5180 /*
5181 * Realtime allocation. Free it and record di_nblocks update.
5182 */
5183 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5184 xfs_fsblock_t bno;
5185 xfs_filblks_t len;
5186
5187 ASSERT(do_mod(del->br_blockcount,
5188 mp->m_sb.sb_rextsize) == 0);
5189 ASSERT(do_mod(del->br_startblock,
5190 mp->m_sb.sb_rextsize) == 0);
5191 bno = del->br_startblock;
5192 len = del->br_blockcount;
5193 do_div(bno, mp->m_sb.sb_rextsize);
5194 do_div(len, mp->m_sb.sb_rextsize);
5195 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5196 if (error)
5197 goto done;
5198 do_fx = 0;
5199 nblks = len * mp->m_sb.sb_rextsize;
5200 qfield = XFS_TRANS_DQ_RTBCOUNT;
5201 }
5202 /*
5203 * Ordinary allocation.
5204 */
5205 else {
5206 do_fx = 1;
5207 nblks = del->br_blockcount;
5208 qfield = XFS_TRANS_DQ_BCOUNT;
5209 }
5210 /*
5211 * Set up del_endblock and cur for later.
5212 */
5213 del_endblock = del->br_startblock + del->br_blockcount;
5214 if (cur) {
5215 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5216 got.br_startblock, got.br_blockcount,
5217 &i)))
5218 goto done;
5219 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5220 }
5221 da_old = da_new = 0;
5222 } else {
5223 da_old = startblockval(got.br_startblock);
5224 da_new = 0;
5225 nblks = 0;
5226 do_fx = 0;
5227 }
5228 /*
5229 * Set flag value to use in switch statement.
5230 * Left-contig is 2, right-contig is 1.
5231 */
5232 switch (((got.br_startoff == del->br_startoff) << 1) |
5233 (got_endoff == del_endoff)) {
5234 case 3:
5235 /*
5236 * Matches the whole extent. Delete the entry.
5237 */
5238 xfs_iext_remove(ip, *idx, 1,
5239 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5240 --*idx;
5241 if (delay)
5242 break;
5243
5244 XFS_IFORK_NEXT_SET(ip, whichfork,
5245 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5246 flags |= XFS_ILOG_CORE;
5247 if (!cur) {
5248 flags |= xfs_ilog_fext(whichfork);
5249 break;
5250 }
5251 if ((error = xfs_btree_delete(cur, &i)))
5252 goto done;
5253 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5254 break;
5255
5256 case 2:
5257 /*
5258 * Deleting the first part of the extent.
5259 */
5260 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5261 xfs_bmbt_set_startoff(ep, del_endoff);
5262 temp = got.br_blockcount - del->br_blockcount;
5263 xfs_bmbt_set_blockcount(ep, temp);
5264 if (delay) {
5265 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5266 da_old);
5267 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5268 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5269 da_new = temp;
5270 break;
5271 }
5272 xfs_bmbt_set_startblock(ep, del_endblock);
5273 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5274 if (!cur) {
5275 flags |= xfs_ilog_fext(whichfork);
5276 break;
5277 }
5278 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5279 got.br_blockcount - del->br_blockcount,
5280 got.br_state)))
5281 goto done;
5282 break;
5283
5284 case 1:
5285 /*
5286 * Deleting the last part of the extent.
5287 */
5288 temp = got.br_blockcount - del->br_blockcount;
5289 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5290 xfs_bmbt_set_blockcount(ep, temp);
5291 if (delay) {
5292 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5293 da_old);
5294 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5295 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5296 da_new = temp;
5297 break;
5298 }
5299 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5300 if (!cur) {
5301 flags |= xfs_ilog_fext(whichfork);
5302 break;
5303 }
5304 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5305 got.br_startblock,
5306 got.br_blockcount - del->br_blockcount,
5307 got.br_state)))
5308 goto done;
5309 break;
5310
5311 case 0:
5312 /*
5313 * Deleting the middle of the extent.
5314 */
5315 temp = del->br_startoff - got.br_startoff;
5316 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5317 xfs_bmbt_set_blockcount(ep, temp);
5318 new.br_startoff = del_endoff;
5319 temp2 = got_endoff - del_endoff;
5320 new.br_blockcount = temp2;
5321 new.br_state = got.br_state;
5322 if (!delay) {
5323 new.br_startblock = del_endblock;
5324 flags |= XFS_ILOG_CORE;
5325 if (cur) {
5326 if ((error = xfs_bmbt_update(cur,
5327 got.br_startoff,
5328 got.br_startblock, temp,
5329 got.br_state)))
5330 goto done;
5331 if ((error = xfs_btree_increment(cur, 0, &i)))
5332 goto done;
5333 cur->bc_rec.b = new;
5334 error = xfs_btree_insert(cur, &i);
5335 if (error && error != ENOSPC)
5336 goto done;
5337 /*
5338 * If get no-space back from btree insert,
5339 * it tried a split, and we have a zero
5340 * block reservation.
5341 * Fix up our state and return the error.
5342 */
5343 if (error == ENOSPC) {
5344 /*
5345 * Reset the cursor, don't trust
5346 * it after any insert operation.
5347 */
5348 if ((error = xfs_bmbt_lookup_eq(cur,
5349 got.br_startoff,
5350 got.br_startblock,
5351 temp, &i)))
5352 goto done;
5353 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5354 /*
5355 * Update the btree record back
5356 * to the original value.
5357 */
5358 if ((error = xfs_bmbt_update(cur,
5359 got.br_startoff,
5360 got.br_startblock,
5361 got.br_blockcount,
5362 got.br_state)))
5363 goto done;
5364 /*
5365 * Reset the extent record back
5366 * to the original value.
5367 */
5368 xfs_bmbt_set_blockcount(ep,
5369 got.br_blockcount);
5370 flags = 0;
5371 error = XFS_ERROR(ENOSPC);
5372 goto done;
5373 }
5374 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5375 } else
5376 flags |= xfs_ilog_fext(whichfork);
5377 XFS_IFORK_NEXT_SET(ip, whichfork,
5378 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5379 } else {
5380 ASSERT(whichfork == XFS_DATA_FORK);
5381 temp = xfs_bmap_worst_indlen(ip, temp);
5382 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5383 temp2 = xfs_bmap_worst_indlen(ip, temp2);
5384 new.br_startblock = nullstartblock((int)temp2);
5385 da_new = temp + temp2;
5386 while (da_new > da_old) {
5387 if (temp) {
5388 temp--;
5389 da_new--;
5390 xfs_bmbt_set_startblock(ep,
5391 nullstartblock((int)temp));
5392 }
5393 if (da_new == da_old)
5394 break;
5395 if (temp2) {
5396 temp2--;
5397 da_new--;
5398 new.br_startblock =
5399 nullstartblock((int)temp2);
5400 }
5401 }
5402 }
5403 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5404 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5405 ++*idx;
5406 break;
5407 }
5408 /*
5409 * If we need to, add to list of extents to delete.
5410 */
5411 if (do_fx)
5412 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
5413 mp);
5414 /*
5415 * Adjust inode # blocks in the file.
5416 */
5417 if (nblks)
5418 ip->i_d.di_nblocks -= nblks;
5419 /*
5420 * Adjust quota data.
5421 */
5422 if (qfield)
5423 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5424
5425 /*
5426 * Account for change in delayed indirect blocks.
5427 * Nothing to do for disk quota accounting here.
5428 */
5429 ASSERT(da_old >= da_new);
5430 if (da_old > da_new) {
5431 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5432 (int64_t)(da_old - da_new), 0);
5433 }
5434done:
5435 *logflagsp = flags;
5436 return error;
5437}
5438
5439/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 * Unmap (remove) blocks from a file.
5441 * If nexts is nonzero then the number of extents to remove is limited to
5442 * that value. If not all extents in the block range can be removed then
5443 * *done is set.
5444 */
5445int /* error */
5446xfs_bunmapi(
5447 xfs_trans_t *tp, /* transaction pointer */
5448 struct xfs_inode *ip, /* incore inode */
5449 xfs_fileoff_t bno, /* starting offset to unmap */
5450 xfs_filblks_t len, /* length to unmap in file */
5451 int flags, /* misc flags */
5452 xfs_extnum_t nexts, /* number of extents max */
5453 xfs_fsblock_t *firstblock, /* first allocated block
5454 controls a.g. for allocs */
5455 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5456 int *done) /* set if not done yet */
5457{
5458 xfs_btree_cur_t *cur; /* bmap btree cursor */
5459 xfs_bmbt_irec_t del; /* extent being deleted */
5460 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005461 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005462 int error; /* error return value */
5463 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005464 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 xfs_ifork_t *ifp; /* inode fork pointer */
5466 int isrt; /* freeing in rt area */
5467 xfs_extnum_t lastx; /* last extent index used */
5468 int logflags; /* transaction logging flags */
5469 xfs_extlen_t mod; /* rt extent offset */
5470 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005471 xfs_extnum_t nextents; /* number of file extents */
5472 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 xfs_fileoff_t start; /* first file offset deleted */
5474 int tmp_logflags; /* partial logging flags */
5475 int wasdel; /* was a delayed alloc extent */
5476 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005477 xfs_fsblock_t sum;
5478
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005479 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5480
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5482 XFS_ATTR_FORK : XFS_DATA_FORK;
5483 ifp = XFS_IFORK_PTR(ip, whichfork);
5484 if (unlikely(
5485 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5486 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5487 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5488 ip->i_mount);
5489 return XFS_ERROR(EFSCORRUPTED);
5490 }
5491 mp = ip->i_mount;
5492 if (XFS_FORCED_SHUTDOWN(mp))
5493 return XFS_ERROR(EIO);
Christoph Hellwig54893272011-05-11 15:04:03 +00005494
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 ASSERT(len > 0);
5496 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005497
Linus Torvalds1da177e2005-04-16 15:20:36 -07005498 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5499 (error = xfs_iread_extents(tp, ip, whichfork)))
5500 return error;
5501 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5502 if (nextents == 0) {
5503 *done = 1;
5504 return 0;
5505 }
5506 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005507 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 start = bno;
5509 bno = start + len - 1;
5510 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5511 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005512
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 /*
5514 * Check to see if the given block number is past the end of the
5515 * file, back up to the last block if so...
5516 */
5517 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005518 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 xfs_bmbt_get_all(ep, &got);
5520 bno = got.br_startoff + got.br_blockcount - 1;
5521 }
5522 logflags = 0;
5523 if (ifp->if_flags & XFS_IFBROOT) {
5524 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005525 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 cur->bc_private.b.firstblock = *firstblock;
5527 cur->bc_private.b.flist = flist;
5528 cur->bc_private.b.flags = 0;
5529 } else
5530 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005531
5532 if (isrt) {
5533 /*
5534 * Synchronize by locking the bitmap inode.
5535 */
5536 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5537 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5538 }
5539
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 extno = 0;
5541 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5542 (nexts == 0 || extno < nexts)) {
5543 /*
5544 * Is the found extent after a hole in which bno lives?
5545 * Just back up to the previous extent, if so.
5546 */
5547 if (got.br_startoff > bno) {
5548 if (--lastx < 0)
5549 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005550 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005551 xfs_bmbt_get_all(ep, &got);
5552 }
5553 /*
5554 * Is the last block of this extent before the range
5555 * we're supposed to delete? If so, we're done.
5556 */
5557 bno = XFS_FILEOFF_MIN(bno,
5558 got.br_startoff + got.br_blockcount - 1);
5559 if (bno < start)
5560 break;
5561 /*
5562 * Then deal with the (possibly delayed) allocated space
5563 * we found.
5564 */
5565 ASSERT(ep != NULL);
5566 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005567 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 if (got.br_startoff < start) {
5569 del.br_startoff = start;
5570 del.br_blockcount -= start - got.br_startoff;
5571 if (!wasdel)
5572 del.br_startblock += start - got.br_startoff;
5573 }
5574 if (del.br_startoff + del.br_blockcount > bno + 1)
5575 del.br_blockcount = bno + 1 - del.br_startoff;
5576 sum = del.br_startblock + del.br_blockcount;
5577 if (isrt &&
5578 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5579 /*
5580 * Realtime extent not lined up at the end.
5581 * The extent could have been split into written
5582 * and unwritten pieces, or we could just be
5583 * unmapping part of it. But we can't really
5584 * get rid of part of a realtime extent.
5585 */
5586 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005587 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 /*
5589 * This piece is unwritten, or we're not
5590 * using unwritten extents. Skip over it.
5591 */
5592 ASSERT(bno >= mod);
5593 bno -= mod > del.br_blockcount ?
5594 del.br_blockcount : mod;
5595 if (bno < got.br_startoff) {
5596 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005597 xfs_bmbt_get_all(xfs_iext_get_ext(
5598 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 }
5600 continue;
5601 }
5602 /*
5603 * It's written, turn it unwritten.
5604 * This is better than zeroing it.
5605 */
5606 ASSERT(del.br_state == XFS_EXT_NORM);
5607 ASSERT(xfs_trans_get_block_res(tp) > 0);
5608 /*
5609 * If this spans a realtime extent boundary,
5610 * chop it back to the start of the one we end at.
5611 */
5612 if (del.br_blockcount > mod) {
5613 del.br_startoff += del.br_blockcount - mod;
5614 del.br_startblock += del.br_blockcount - mod;
5615 del.br_blockcount = mod;
5616 }
5617 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005618 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5619 &lastx, &cur, &del, firstblock, flist,
5620 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 if (error)
5622 goto error0;
5623 goto nodelete;
5624 }
5625 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5626 /*
5627 * Realtime extent is lined up at the end but not
5628 * at the front. We'll get rid of full extents if
5629 * we can.
5630 */
5631 mod = mp->m_sb.sb_rextsize - mod;
5632 if (del.br_blockcount > mod) {
5633 del.br_blockcount -= mod;
5634 del.br_startoff += mod;
5635 del.br_startblock += mod;
5636 } else if ((del.br_startoff == start &&
5637 (del.br_state == XFS_EXT_UNWRITTEN ||
5638 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005639 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 /*
5641 * Can't make it unwritten. There isn't
5642 * a full extent here so just skip it.
5643 */
5644 ASSERT(bno >= del.br_blockcount);
5645 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005646 if (got.br_startoff > bno) {
5647 if (--lastx >= 0) {
5648 ep = xfs_iext_get_ext(ifp,
5649 lastx);
5650 xfs_bmbt_get_all(ep, &got);
5651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652 }
5653 continue;
5654 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5655 /*
5656 * This one is already unwritten.
5657 * It must have a written left neighbor.
5658 * Unwrite the killed part of that one and
5659 * try again.
5660 */
5661 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005662 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5663 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005664 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005665 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 ASSERT(del.br_startblock ==
5667 prev.br_startblock + prev.br_blockcount);
5668 if (prev.br_startoff < start) {
5669 mod = start - prev.br_startoff;
5670 prev.br_blockcount -= mod;
5671 prev.br_startblock += mod;
5672 prev.br_startoff = start;
5673 }
5674 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005675 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005676 error = xfs_bmap_add_extent_unwritten_real(tp,
5677 ip, &lastx, &cur, &prev,
5678 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 if (error)
5680 goto error0;
5681 goto nodelete;
5682 } else {
5683 ASSERT(del.br_state == XFS_EXT_NORM);
5684 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005685 error = xfs_bmap_add_extent_unwritten_real(tp,
5686 ip, &lastx, &cur, &del,
5687 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 if (error)
5689 goto error0;
5690 goto nodelete;
5691 }
5692 }
5693 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005694 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005695 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005696 if (isrt) {
5697 xfs_filblks_t rtexts;
5698
5699 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5700 do_div(rtexts, mp->m_sb.sb_rextsize);
5701 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005702 (int64_t)rtexts, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005703 (void)xfs_trans_reserve_quota_nblks(NULL,
5704 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005705 XFS_QMOPT_RES_RTBLKS);
5706 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00005707 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005708 (int64_t)del.br_blockcount, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005709 (void)xfs_trans_reserve_quota_nblks(NULL,
5710 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005711 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 ip->i_delayed_blks -= del.br_blockcount;
5714 if (cur)
5715 cur->bc_private.b.flags |=
5716 XFS_BTCUR_BPRV_WASDEL;
5717 } else if (cur)
5718 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5719 /*
5720 * If it's the case where the directory code is running
5721 * with no block reservation, and the deleted block is in
5722 * the middle of its extent, and the resulting insert
5723 * of an extent would cause transformation to btree format,
5724 * then reject it. The calling code will then swap
5725 * blocks around instead.
5726 * We have to do this now, rather than waiting for the
5727 * conversion to btree format, since the transaction
5728 * will be dirty.
5729 */
5730 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5731 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005732 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5733 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 del.br_startoff > got.br_startoff &&
5735 del.br_startoff + del.br_blockcount <
5736 got.br_startoff + got.br_blockcount) {
5737 error = XFS_ERROR(ENOSPC);
5738 goto error0;
5739 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00005740 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005741 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 logflags |= tmp_logflags;
5743 if (error)
5744 goto error0;
5745 bno = del.br_startoff - 1;
5746nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005747 /*
5748 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005751 if (lastx >= 0) {
5752 ep = xfs_iext_get_ext(ifp, lastx);
5753 if (xfs_bmbt_get_startoff(ep) > bno) {
5754 if (--lastx >= 0)
5755 ep = xfs_iext_get_ext(ifp,
5756 lastx);
5757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005759 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 extno++;
5761 }
5762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005764
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 /*
5766 * Convert to a btree if necessary.
5767 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005768 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 ASSERT(cur == NULL);
5770 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5771 &cur, 0, &tmp_logflags, whichfork);
5772 logflags |= tmp_logflags;
5773 if (error)
5774 goto error0;
5775 }
5776 /*
5777 * transform from btree to extents, give it cur
5778 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005779 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 ASSERT(cur != NULL);
5781 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5782 whichfork);
5783 logflags |= tmp_logflags;
5784 if (error)
5785 goto error0;
5786 }
5787 /*
5788 * transform from extents to local?
5789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 error = 0;
5791error0:
5792 /*
5793 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005794 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005796 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005798 logflags &= ~xfs_ilog_fext(whichfork);
5799 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005801 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 /*
5803 * Log inode even in the error case, if the transaction
5804 * is dirty we'll need to shut down the filesystem.
5805 */
5806 if (logflags)
5807 xfs_trans_log_inode(tp, ip, logflags);
5808 if (cur) {
5809 if (!error) {
5810 *firstblock = cur->bc_private.b.firstblock;
5811 cur->bc_private.b.allocated = 0;
5812 }
5813 xfs_btree_del_cursor(cur,
5814 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5815 }
5816 return error;
5817}
5818
5819/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005820 * returns 1 for success, 0 if we failed to map the extent.
5821 */
5822STATIC int
5823xfs_getbmapx_fix_eof_hole(
5824 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005825 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005826 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005827 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005828 __int64_t end, /* last block requested */
5829 xfs_fsblock_t startblock)
5830{
5831 __int64_t fixlen;
5832 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005833 xfs_ifork_t *ifp; /* inode fork pointer */
5834 xfs_extnum_t lastx; /* last extent pointer */
5835 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005836
5837 if (startblock == HOLESTARTBLOCK) {
5838 mp = ip->i_mount;
5839 out->bmv_block = -1;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005840 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005841 fixlen -= out->bmv_offset;
5842 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5843 /* Came to hole at EOF. Trim it. */
5844 if (fixlen <= 0)
5845 return 0;
5846 out->bmv_length = fixlen;
5847 }
5848 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005849 if (startblock == DELAYSTARTBLOCK)
5850 out->bmv_block = -2;
5851 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005852 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005853 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5854 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5855 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5856 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5857 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005858 }
5859
5860 return 1;
5861}
5862
5863/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005864 * Get inode's extents as described in bmv, and format for output.
5865 * Calls formatter to fill the user's buffer until all extents
5866 * are mapped, until the passed-in bmv->bmv_count slots have
5867 * been filled, or until the formatter short-circuits the loop,
5868 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869 */
5870int /* error code */
5871xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005872 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005873 struct getbmapx *bmv, /* user bmap structure */
5874 xfs_bmap_format_t formatter, /* format to user */
5875 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876{
5877 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005878 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 __int64_t fixlen; /* length for -1 case */
5880 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005881 int lock; /* lock state */
5882 xfs_bmbt_irec_t *map; /* buffer for user's data */
5883 xfs_mount_t *mp; /* file system mount point */
5884 int nex; /* # of user extents can do */
5885 int nexleft; /* # of user extents left */
5886 int subnex; /* # of bmapi's can do */
5887 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005888 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005889 int whichfork; /* data or attr fork */
5890 int prealloced; /* this is a file with
5891 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005892 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005894 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005897 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005898 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 if (whichfork == XFS_ATTR_FORK) {
5901 if (XFS_IFORK_Q(ip)) {
5902 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5903 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5904 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5905 return XFS_ERROR(EINVAL);
5906 } else if (unlikely(
5907 ip->i_d.di_aformat != 0 &&
5908 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5909 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5910 ip->i_mount);
5911 return XFS_ERROR(EFSCORRUPTED);
5912 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005913
5914 prealloced = 0;
5915 fixlen = 1LL << 32;
5916 } else {
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005917 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5918 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5919 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5920 return XFS_ERROR(EINVAL);
5921
David Chinner957d0eb2007-06-18 16:50:37 +10005922 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005923 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005924 prealloced = 1;
Dave Chinner32972382012-06-08 15:44:54 +10005925 fixlen = mp->m_super->s_maxbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 } else {
5927 prealloced = 0;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005928 fixlen = XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005930 }
5931
5932 if (bmv->bmv_length == -1) {
5933 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005934 bmv->bmv_length =
5935 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5936 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 bmv->bmv_entries = 0;
5938 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005939 } else if (bmv->bmv_length < 0) {
5940 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005941 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005942
Linus Torvalds1da177e2005-04-16 15:20:36 -07005943 nex = bmv->bmv_count - 1;
5944 if (nex <= 0)
5945 return XFS_ERROR(EINVAL);
5946 bmvend = bmv->bmv_offset + bmv->bmv_length;
5947
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005948
5949 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5950 return XFS_ERROR(ENOMEM);
5951 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
Dave Chinnerf0742112012-03-07 04:50:22 +00005952 if (!out) {
5953 out = kmem_zalloc_large(bmv->bmv_count *
5954 sizeof(struct getbmapx));
5955 if (!out)
5956 return XFS_ERROR(ENOMEM);
5957 }
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005958
Linus Torvalds1da177e2005-04-16 15:20:36 -07005959 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005960 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005961 if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) {
Dave Chinner4bc1ea62012-11-12 22:53:56 +11005962 error = -filemap_write_and_wait(VFS_I(ip)->i_mapping);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005963 if (error)
5964 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005965 }
Dave Chinner309c8482010-11-30 15:16:02 +11005966 /*
5967 * even after flushing the inode, there can still be delalloc
5968 * blocks on the inode beyond EOF due to speculative
5969 * preallocation. These are not removed until the release
5970 * function is called or the inode is inactivated. Hence we
5971 * cannot assert here that ip->i_delayed_blks == 0.
5972 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005974
5975 lock = xfs_ilock_map_shared(ip);
5976
5977 /*
5978 * Don't let nex be bigger than the number of extents
5979 * we can have assuming alternating holes and real extents.
5980 */
5981 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5982 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5983
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005984 bmapi_flags = xfs_bmapi_aflag(whichfork);
5985 if (!(iflags & BMV_IF_PREALLOC))
5986 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987
5988 /*
5989 * Allocate enough space to handle "subnex" maps at a time.
5990 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005991 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04005993 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005994 if (!map)
5995 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996
5997 bmv->bmv_entries = 0;
5998
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005999 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
6000 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
6001 error = 0;
6002 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003 }
6004
6005 nexleft = nex;
6006
6007 do {
6008 nmap = (nexleft > subnex) ? subnex : nexleft;
Dave Chinner5c8ed202011-09-18 20:40:45 +00006009 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
6010 XFS_BB_TO_FSB(mp, bmv->bmv_length),
6011 map, &nmap, bmapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006013 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 ASSERT(nmap <= subnex);
6015
6016 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006017 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11006018 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006019 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11006020 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006021 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
6022 out[cur_ext].bmv_offset =
6023 XFS_FSB_TO_BB(mp, map[i].br_startoff);
6024 out[cur_ext].bmv_length =
6025 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
6026 out[cur_ext].bmv_unused1 = 0;
6027 out[cur_ext].bmv_unused2 = 0;
Dave Chinner58e20772012-04-29 21:16:17 +10006028
6029 /*
6030 * delayed allocation extents that start beyond EOF can
6031 * occur due to speculative EOF allocation when the
6032 * delalloc extent is larger than the largest freespace
6033 * extent at conversion time. These extents cannot be
6034 * converted by data writeback, so can exist here even
6035 * if we are not supposed to be finding delalloc
6036 * extents.
6037 */
6038 if (map[i].br_startblock == DELAYSTARTBLOCK &&
6039 map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
6040 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
6041
Yingping Lu9af0a702005-11-02 15:09:54 +11006042 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10006043 whichfork == XFS_ATTR_FORK) {
6044 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006045 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006046 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006047 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006048
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006049 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
6050 prealloced, bmvend,
6051 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006052 goto out_free_map;
6053
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006054 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006055 out[cur_ext].bmv_offset +
6056 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006057 bmv->bmv_length =
6058 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
Tao Ma9af25462010-08-30 02:44:03 +00006059
6060 /*
6061 * In case we don't want to return the hole,
6062 * don't increase cur_ext so that we can reuse
6063 * it in the next loop.
6064 */
6065 if ((iflags & BMV_IF_NO_HOLES) &&
6066 map[i].br_startblock == HOLESTARTBLOCK) {
6067 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
6068 continue;
6069 }
6070
6071 nexleft--;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006072 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006073 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006074 }
6075 } while (nmap && nexleft && bmv->bmv_length);
6076
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006077 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10006078 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006079 out_unlock_ilock:
6080 xfs_iunlock_map_shared(ip, lock);
6081 out_unlock_iolock:
6082 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006083
6084 for (i = 0; i < cur_ext; i++) {
6085 int full = 0; /* user array is full */
6086
6087 /* format results & advance arg */
6088 error = formatter(&arg, &out[i], &full);
6089 if (error || full)
6090 break;
6091 }
6092
Dave Chinnerf0742112012-03-07 04:50:22 +00006093 if (is_vmalloc_addr(out))
6094 kmem_free_large(out);
6095 else
6096 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 return error;
6098}
6099
Dave Chinnerc726de42010-11-30 15:14:39 +11006100/*
6101 * dead simple method of punching delalyed allocation blocks from a range in
6102 * the inode. Walks a block at a time so will be slow, but is only executed in
6103 * rare error cases so the overhead is not critical. This will alays punch out
6104 * both the start and end blocks, even if the ranges only partially overlap
6105 * them, so it is up to the caller to ensure that partial blocks are not
6106 * passed in.
6107 */
6108int
6109xfs_bmap_punch_delalloc_range(
6110 struct xfs_inode *ip,
6111 xfs_fileoff_t start_fsb,
6112 xfs_fileoff_t length)
6113{
6114 xfs_fileoff_t remaining = length;
6115 int error = 0;
6116
6117 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6118
6119 do {
6120 int done;
6121 xfs_bmbt_irec_t imap;
6122 int nimaps = 1;
6123 xfs_fsblock_t firstblock;
6124 xfs_bmap_free_t flist;
6125
6126 /*
6127 * Map the range first and check that it is a delalloc extent
6128 * before trying to unmap the range. Otherwise we will be
6129 * trying to remove a real extent (which requires a
6130 * transaction) or a hole, which is probably a bad idea...
6131 */
Dave Chinner5c8ed202011-09-18 20:40:45 +00006132 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
6133 XFS_BMAPI_ENTIRE);
Dave Chinnerc726de42010-11-30 15:14:39 +11006134
6135 if (error) {
6136 /* something screwed, just bail */
6137 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner53487782011-03-07 10:05:35 +11006138 xfs_alert(ip->i_mount,
Dave Chinnerc726de42010-11-30 15:14:39 +11006139 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6140 ip->i_ino, start_fsb);
6141 }
6142 break;
6143 }
6144 if (!nimaps) {
6145 /* nothing there */
6146 goto next_block;
6147 }
6148 if (imap.br_startblock != DELAYSTARTBLOCK) {
6149 /* been converted, ignore */
6150 goto next_block;
6151 }
6152 WARN_ON(imap.br_blockcount == 0);
6153
6154 /*
6155 * Note: while we initialise the firstblock/flist pair, they
6156 * should never be used because blocks should never be
6157 * allocated or freed for a delalloc extent and hence we need
6158 * don't cancel or finish them after the xfs_bunmapi() call.
6159 */
6160 xfs_bmap_init(&flist, &firstblock);
6161 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6162 &flist, &done);
6163 if (error)
6164 break;
6165
6166 ASSERT(!flist.xbf_count && !flist.xbf_first);
6167next_block:
6168 start_fsb++;
6169 remaining--;
6170 } while(remaining > 0);
6171
6172 return error;
6173}