blob: 72a2eea597e990030d065067205e8702e6e24c72 [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"
Dave Chinner6ca1c902013-08-12 20:49:26 +100020#include "xfs_format.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"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100027#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100029#include "xfs_dir2_format.h"
30#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_extfree_item.h"
41#include "xfs_alloc.h"
42#include "xfs_bmap.h"
43#include "xfs_rtalloc.h"
44#include "xfs_error.h"
Nathan Scottd8cc8902005-11-02 10:34:53 +110045#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include "xfs_quota.h"
47#include "xfs_trans_space.h"
48#include "xfs_buf_item.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100049#include "xfs_filestream.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100050#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000051#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110052#include "xfs_symlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055kmem_zone_t *xfs_bmap_free_item_zone;
56
57/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110058 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110062 * Compute and fill in the value of the maximum depth of a bmap btree
63 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110065void
66xfs_bmap_compute_maxlevels(
67 xfs_mount_t *mp, /* file system mount structure */
68 int whichfork) /* data or attr fork */
69{
70 int level; /* btree level */
71 uint maxblocks; /* max blocks at this level */
72 uint maxleafents; /* max leaf entries possible */
73 int maxrootrecs; /* max records in root block */
74 int minleafrecs; /* min records in leaf block */
75 int minnoderecs; /* min records in node block */
76 int sz; /* root block size */
77
78 /*
79 * The maximum number of extents in a file, hence the maximum
80 * number of leaf entries, is controlled by the type of di_nextents
81 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
82 * (a signed 16-bit number, xfs_aextnum_t).
83 *
84 * Note that we can no longer assume that if we are in ATTR1 that
85 * the fork offset of all the inodes will be
86 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
87 * with ATTR2 and then mounted back with ATTR1, keeping the
88 * di_forkoff's fixed but probably at various positions. Therefore,
89 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
90 * of a minimum size available.
91 */
92 if (whichfork == XFS_DATA_FORK) {
93 maxleafents = MAXEXTNUM;
94 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
95 } else {
96 maxleafents = MAXAEXTNUM;
97 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
98 }
99 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
100 minleafrecs = mp->m_bmap_dmnr[0];
101 minnoderecs = mp->m_bmap_dmnr[1];
102 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
103 for (level = 1; maxblocks > 1; level++) {
104 if (maxblocks <= maxrootrecs)
105 maxblocks = 1;
106 else
107 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
108 }
109 mp->m_bm_maxlevels[whichfork] = level;
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100113 * Convert the given file system block to a disk block. We have to treat it
114 * differently based on whether the file is a real time file or not, because the
115 * bmap code does.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100117xfs_daddr_t
118xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
119{
120 return (XFS_IS_REALTIME_INODE(ip) ? \
121 (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
122 XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
123}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100125STATIC int /* error */
126xfs_bmbt_lookup_eq(
127 struct xfs_btree_cur *cur,
128 xfs_fileoff_t off,
129 xfs_fsblock_t bno,
130 xfs_filblks_t len,
131 int *stat) /* success/failure */
132{
133 cur->bc_rec.b.br_startoff = off;
134 cur->bc_rec.b.br_startblock = bno;
135 cur->bc_rec.b.br_blockcount = len;
136 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
137}
138
139STATIC int /* error */
140xfs_bmbt_lookup_ge(
141 struct xfs_btree_cur *cur,
142 xfs_fileoff_t off,
143 xfs_fsblock_t bno,
144 xfs_filblks_t len,
145 int *stat) /* success/failure */
146{
147 cur->bc_rec.b.br_startoff = off;
148 cur->bc_rec.b.br_startblock = bno;
149 cur->bc_rec.b.br_blockcount = len;
150 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
151}
152
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100153/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000154 * Check if the inode needs to be converted to btree format.
155 */
156static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
157{
158 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
159 XFS_IFORK_NEXTENTS(ip, whichfork) >
160 XFS_IFORK_MAXEXT(ip, whichfork);
161}
162
163/*
164 * Check if the inode should be converted to extent format.
165 */
166static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
167{
168 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
169 XFS_IFORK_NEXTENTS(ip, whichfork) <=
170 XFS_IFORK_MAXEXT(ip, whichfork);
171}
172
173/*
174 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100175 * by [off, bno, len, state].
176 * This either works (return 0) or gets an EFSCORRUPTED error.
177 */
178STATIC int
179xfs_bmbt_update(
180 struct xfs_btree_cur *cur,
181 xfs_fileoff_t off,
182 xfs_fsblock_t bno,
183 xfs_filblks_t len,
184 xfs_exntst_t state)
185{
186 union xfs_btree_rec rec;
187
188 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
189 return xfs_btree_update(cur, &rec);
190}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100193 * Compute the worst-case number of indirect blocks that will be used
194 * for ip's delayed extent of length "len".
195 */
196STATIC xfs_filblks_t
197xfs_bmap_worst_indlen(
198 xfs_inode_t *ip, /* incore inode pointer */
199 xfs_filblks_t len) /* delayed extent length */
200{
201 int level; /* btree level number */
202 int maxrecs; /* maximum record count at this level */
203 xfs_mount_t *mp; /* mount structure */
204 xfs_filblks_t rval; /* return value */
205
206 mp = ip->i_mount;
207 maxrecs = mp->m_bmap_dmxr[0];
208 for (level = 0, rval = 0;
209 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
210 level++) {
211 len += maxrecs - 1;
212 do_div(len, maxrecs);
213 rval += len;
214 if (len == 1)
215 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
216 level - 1;
217 if (level == 0)
218 maxrecs = mp->m_bmap_dmxr[1];
219 }
220 return rval;
221}
222
223/*
224 * Calculate the default attribute fork offset for newly created inodes.
225 */
226uint
227xfs_default_attroffset(
228 struct xfs_inode *ip)
229{
230 struct xfs_mount *mp = ip->i_mount;
231 uint offset;
232
233 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100234 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100235 XFS_BMDR_SPACE_CALC(MINABTPTRS);
236 } else {
237 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
238 }
239
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100240 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100241 return offset;
242}
243
244/*
245 * Helper routine to reset inode di_forkoff field when switching
246 * attribute fork from local to extent format - we reset it where
247 * possible to make space available for inline data fork extents.
248 */
249STATIC void
250xfs_bmap_forkoff_reset(
251 xfs_mount_t *mp,
252 xfs_inode_t *ip,
253 int whichfork)
254{
255 if (whichfork == XFS_ATTR_FORK &&
256 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
257 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
258 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
259 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
260
261 if (dfl_forkoff > ip->i_d.di_forkoff)
262 ip->i_d.di_forkoff = dfl_forkoff;
263 }
264}
265
266/*
267 * Extent tree block counting routines.
268 */
269
270/*
271 * Count leaf blocks given a range of extent records.
272 */
273STATIC void
274xfs_bmap_count_leaves(
275 xfs_ifork_t *ifp,
276 xfs_extnum_t idx,
277 int numrecs,
278 int *count)
279{
280 int b;
281
282 for (b = 0; b < numrecs; b++) {
283 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
284 *count += xfs_bmbt_get_blockcount(frp);
285 }
286}
287
288/*
289 * Count leaf blocks given a range of extent records originally
290 * in btree format.
291 */
292STATIC void
293xfs_bmap_disk_count_leaves(
294 struct xfs_mount *mp,
295 struct xfs_btree_block *block,
296 int numrecs,
297 int *count)
298{
299 int b;
300 xfs_bmbt_rec_t *frp;
301
302 for (b = 1; b <= numrecs; b++) {
303 frp = XFS_BMBT_REC_ADDR(mp, block, b);
304 *count += xfs_bmbt_disk_get_blockcount(frp);
305 }
306}
307
308/*
309 * Recursively walks each level of a btree
310 * to count total fsblocks is use.
311 */
312STATIC int /* error */
313xfs_bmap_count_tree(
314 xfs_mount_t *mp, /* file system mount point */
315 xfs_trans_t *tp, /* transaction pointer */
316 xfs_ifork_t *ifp, /* inode fork pointer */
317 xfs_fsblock_t blockno, /* file system block number */
318 int levelin, /* level in btree */
319 int *count) /* Count of blocks */
320{
321 int error;
322 xfs_buf_t *bp, *nbp;
323 int level = levelin;
324 __be64 *pp;
325 xfs_fsblock_t bno = blockno;
326 xfs_fsblock_t nextbno;
327 struct xfs_btree_block *block, *nextblock;
328 int numrecs;
329
330 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
331 &xfs_bmbt_buf_ops);
332 if (error)
333 return error;
334 *count += 1;
335 block = XFS_BUF_TO_BLOCK(bp);
336
337 if (--level) {
338 /* Not at node above leaves, count this level of nodes */
339 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
340 while (nextbno != NULLFSBLOCK) {
341 error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
342 XFS_BMAP_BTREE_REF,
343 &xfs_bmbt_buf_ops);
344 if (error)
345 return error;
346 *count += 1;
347 nextblock = XFS_BUF_TO_BLOCK(nbp);
348 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
349 xfs_trans_brelse(tp, nbp);
350 }
351
352 /* Dive to the next level */
353 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
354 bno = be64_to_cpu(*pp);
355 if (unlikely((error =
356 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
357 xfs_trans_brelse(tp, bp);
358 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
359 XFS_ERRLEVEL_LOW, mp);
360 return XFS_ERROR(EFSCORRUPTED);
361 }
362 xfs_trans_brelse(tp, bp);
363 } else {
364 /* count all level 1 nodes and their leaves */
365 for (;;) {
366 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
367 numrecs = be16_to_cpu(block->bb_numrecs);
368 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
369 xfs_trans_brelse(tp, bp);
370 if (nextbno == NULLFSBLOCK)
371 break;
372 bno = nextbno;
373 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
374 XFS_BMAP_BTREE_REF,
375 &xfs_bmbt_buf_ops);
376 if (error)
377 return error;
378 *count += 1;
379 block = XFS_BUF_TO_BLOCK(bp);
380 }
381 }
382 return 0;
383}
384
385/*
386 * Count fsblocks of the given fork.
387 */
388int /* error */
389xfs_bmap_count_blocks(
390 xfs_trans_t *tp, /* transaction pointer */
391 xfs_inode_t *ip, /* incore inode */
392 int whichfork, /* data or attr fork */
393 int *count) /* out: count of blocks */
394{
395 struct xfs_btree_block *block; /* current btree block */
396 xfs_fsblock_t bno; /* block # of "block" */
397 xfs_ifork_t *ifp; /* fork structure */
398 int level; /* btree level, for checking */
399 xfs_mount_t *mp; /* file system mount structure */
400 __be64 *pp; /* pointer to block address */
401
402 bno = NULLFSBLOCK;
403 mp = ip->i_mount;
404 ifp = XFS_IFORK_PTR(ip, whichfork);
405 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
406 xfs_bmap_count_leaves(ifp, 0,
407 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
408 count);
409 return 0;
410 }
411
412 /*
413 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
414 */
415 block = ifp->if_broot;
416 level = be16_to_cpu(block->bb_level);
417 ASSERT(level > 0);
418 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
419 bno = be64_to_cpu(*pp);
420 ASSERT(bno != NULLDFSBNO);
421 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
422 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
423
424 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
425 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
426 mp);
427 return XFS_ERROR(EFSCORRUPTED);
428 }
429
430 return 0;
431}
432
433/*
434 * Debug/sanity checking code
435 */
436
437STATIC int
438xfs_bmap_sanity_check(
439 struct xfs_mount *mp,
440 struct xfs_buf *bp,
441 int level)
442{
443 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
444
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500445 if (block->bb_magic != cpu_to_be32(XFS_BMAP_CRC_MAGIC) &&
446 block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC))
447 return 0;
448
449 if (be16_to_cpu(block->bb_level) != level ||
Dave Chinner9e5987a2013-02-25 12:31:26 +1100450 be16_to_cpu(block->bb_numrecs) == 0 ||
451 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
452 return 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500453
Dave Chinner9e5987a2013-02-25 12:31:26 +1100454 return 1;
455}
456
457#ifdef DEBUG
458STATIC struct xfs_buf *
459xfs_bmap_get_bp(
460 struct xfs_btree_cur *cur,
461 xfs_fsblock_t bno)
462{
463 struct xfs_log_item_desc *lidp;
464 int i;
465
466 if (!cur)
467 return NULL;
468
469 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
470 if (!cur->bc_bufs[i])
471 break;
472 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
473 return cur->bc_bufs[i];
474 }
475
476 /* Chase down all the log items to see if the bp is there */
477 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
478 struct xfs_buf_log_item *bip;
479 bip = (struct xfs_buf_log_item *)lidp->lid_item;
480 if (bip->bli_item.li_type == XFS_LI_BUF &&
481 XFS_BUF_ADDR(bip->bli_buf) == bno)
482 return bip->bli_buf;
483 }
484
485 return NULL;
486}
487
488STATIC void
489xfs_check_block(
490 struct xfs_btree_block *block,
491 xfs_mount_t *mp,
492 int root,
493 short sz)
494{
495 int i, j, dmxr;
496 __be64 *pp, *thispa; /* pointer to block address */
497 xfs_bmbt_key_t *prevp, *keyp;
498
499 ASSERT(be16_to_cpu(block->bb_level) > 0);
500
501 prevp = NULL;
502 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
503 dmxr = mp->m_bmap_dmxr[0];
504 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
505
506 if (prevp) {
507 ASSERT(be64_to_cpu(prevp->br_startoff) <
508 be64_to_cpu(keyp->br_startoff));
509 }
510 prevp = keyp;
511
512 /*
513 * Compare the block numbers to see if there are dups.
514 */
515 if (root)
516 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
517 else
518 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
519
520 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
521 if (root)
522 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
523 else
524 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
525 if (*thispa == *pp) {
526 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
527 __func__, j, i,
528 (unsigned long long)be64_to_cpu(*thispa));
529 panic("%s: ptrs are equal in node\n",
530 __func__);
531 }
532 }
533 }
534}
535
536/*
537 * Check that the extents for the inode ip are in the right order in all
538 * btree leaves.
539 */
540
541STATIC void
542xfs_bmap_check_leaf_extents(
543 xfs_btree_cur_t *cur, /* btree cursor or null */
544 xfs_inode_t *ip, /* incore inode pointer */
545 int whichfork) /* data or attr fork */
546{
547 struct xfs_btree_block *block; /* current btree block */
548 xfs_fsblock_t bno; /* block # of "block" */
549 xfs_buf_t *bp; /* buffer for "block" */
550 int error; /* error return value */
551 xfs_extnum_t i=0, j; /* index into the extents list */
552 xfs_ifork_t *ifp; /* fork structure */
553 int level; /* btree level, for checking */
554 xfs_mount_t *mp; /* file system mount structure */
555 __be64 *pp; /* pointer to block address */
556 xfs_bmbt_rec_t *ep; /* pointer to current extent */
557 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
558 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
559 int bp_release = 0;
560
561 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
562 return;
563 }
564
565 bno = NULLFSBLOCK;
566 mp = ip->i_mount;
567 ifp = XFS_IFORK_PTR(ip, whichfork);
568 block = ifp->if_broot;
569 /*
570 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
571 */
572 level = be16_to_cpu(block->bb_level);
573 ASSERT(level > 0);
574 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
575 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
576 bno = be64_to_cpu(*pp);
577
578 ASSERT(bno != NULLDFSBNO);
579 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
580 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
581
582 /*
583 * Go down the tree until leaf level is reached, following the first
584 * pointer (leftmost) at each level.
585 */
586 while (level-- > 0) {
587 /* See if buf is in cur first */
588 bp_release = 0;
589 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
590 if (!bp) {
591 bp_release = 1;
592 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
593 XFS_BMAP_BTREE_REF,
594 &xfs_bmbt_buf_ops);
595 if (error)
596 goto error_norelse;
597 }
598 block = XFS_BUF_TO_BLOCK(bp);
599 XFS_WANT_CORRUPTED_GOTO(
600 xfs_bmap_sanity_check(mp, bp, level),
601 error0);
602 if (level == 0)
603 break;
604
605 /*
606 * Check this block for basic sanity (increasing keys and
607 * no duplicate blocks).
608 */
609
610 xfs_check_block(block, mp, 0, 0);
611 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
612 bno = be64_to_cpu(*pp);
613 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
614 if (bp_release) {
615 bp_release = 0;
616 xfs_trans_brelse(NULL, bp);
617 }
618 }
619
620 /*
621 * Here with bp and block set to the leftmost leaf node in the tree.
622 */
623 i = 0;
624
625 /*
626 * Loop over all leaf nodes checking that all extents are in the right order.
627 */
628 for (;;) {
629 xfs_fsblock_t nextbno;
630 xfs_extnum_t num_recs;
631
632
633 num_recs = xfs_btree_get_numrecs(block);
634
635 /*
636 * Read-ahead the next leaf block, if any.
637 */
638
639 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
640
641 /*
642 * Check all the extents to make sure they are OK.
643 * If we had a previous block, the last entry should
644 * conform with the first entry in this one.
645 */
646
647 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
648 if (i) {
649 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
650 xfs_bmbt_disk_get_blockcount(&last) <=
651 xfs_bmbt_disk_get_startoff(ep));
652 }
653 for (j = 1; j < num_recs; j++) {
654 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
655 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
656 xfs_bmbt_disk_get_blockcount(ep) <=
657 xfs_bmbt_disk_get_startoff(nextp));
658 ep = nextp;
659 }
660
661 last = *ep;
662 i += num_recs;
663 if (bp_release) {
664 bp_release = 0;
665 xfs_trans_brelse(NULL, bp);
666 }
667 bno = nextbno;
668 /*
669 * If we've reached the end, stop.
670 */
671 if (bno == NULLFSBLOCK)
672 break;
673
674 bp_release = 0;
675 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
676 if (!bp) {
677 bp_release = 1;
678 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
679 XFS_BMAP_BTREE_REF,
680 &xfs_bmbt_buf_ops);
681 if (error)
682 goto error_norelse;
683 }
684 block = XFS_BUF_TO_BLOCK(bp);
685 }
686 if (bp_release) {
687 bp_release = 0;
688 xfs_trans_brelse(NULL, bp);
689 }
690 return;
691
692error0:
693 xfs_warn(mp, "%s: at error0", __func__);
694 if (bp_release)
695 xfs_trans_brelse(NULL, bp);
696error_norelse:
697 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
698 __func__, i);
699 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
700 return;
701}
702
703/*
704 * Add bmap trace insert entries for all the contents of the extent records.
705 */
706void
707xfs_bmap_trace_exlist(
708 xfs_inode_t *ip, /* incore inode pointer */
709 xfs_extnum_t cnt, /* count of entries in the list */
710 int whichfork, /* data or attr fork */
711 unsigned long caller_ip)
712{
713 xfs_extnum_t idx; /* extent record index */
714 xfs_ifork_t *ifp; /* inode fork pointer */
715 int state = 0;
716
717 if (whichfork == XFS_ATTR_FORK)
718 state |= BMAP_ATTRFORK;
719
720 ifp = XFS_IFORK_PTR(ip, whichfork);
721 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
722 for (idx = 0; idx < cnt; idx++)
723 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
724}
725
726/*
727 * Validate that the bmbt_irecs being returned from bmapi are valid
728 * given the callers original parameters. Specifically check the
729 * ranges of the returned irecs to ensure that they only extent beyond
730 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
731 */
732STATIC void
733xfs_bmap_validate_ret(
734 xfs_fileoff_t bno,
735 xfs_filblks_t len,
736 int flags,
737 xfs_bmbt_irec_t *mval,
738 int nmap,
739 int ret_nmap)
740{
741 int i; /* index to map values */
742
743 ASSERT(ret_nmap <= nmap);
744
745 for (i = 0; i < ret_nmap; i++) {
746 ASSERT(mval[i].br_blockcount > 0);
747 if (!(flags & XFS_BMAPI_ENTIRE)) {
748 ASSERT(mval[i].br_startoff >= bno);
749 ASSERT(mval[i].br_blockcount <= len);
750 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
751 bno + len);
752 } else {
753 ASSERT(mval[i].br_startoff < bno + len);
754 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
755 bno);
756 }
757 ASSERT(i == 0 ||
758 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
759 mval[i].br_startoff);
760 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
761 mval[i].br_startblock != HOLESTARTBLOCK);
762 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
763 mval[i].br_state == XFS_EXT_UNWRITTEN);
764 }
765}
766
767#else
768#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
769#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
770#endif /* DEBUG */
771
772/*
773 * bmap free list manipulation functions
774 */
775
776/*
777 * Add the extent to the list of extents to be free at transaction end.
778 * The list is maintained sorted (by block number).
779 */
780void
781xfs_bmap_add_free(
782 xfs_fsblock_t bno, /* fs block number of extent */
783 xfs_filblks_t len, /* length of extent */
784 xfs_bmap_free_t *flist, /* list of extents */
785 xfs_mount_t *mp) /* mount point structure */
786{
787 xfs_bmap_free_item_t *cur; /* current (next) element */
788 xfs_bmap_free_item_t *new; /* new element */
789 xfs_bmap_free_item_t *prev; /* previous element */
790#ifdef DEBUG
791 xfs_agnumber_t agno;
792 xfs_agblock_t agbno;
793
794 ASSERT(bno != NULLFSBLOCK);
795 ASSERT(len > 0);
796 ASSERT(len <= MAXEXTLEN);
797 ASSERT(!isnullstartblock(bno));
798 agno = XFS_FSB_TO_AGNO(mp, bno);
799 agbno = XFS_FSB_TO_AGBNO(mp, bno);
800 ASSERT(agno < mp->m_sb.sb_agcount);
801 ASSERT(agbno < mp->m_sb.sb_agblocks);
802 ASSERT(len < mp->m_sb.sb_agblocks);
803 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
804#endif
805 ASSERT(xfs_bmap_free_item_zone != NULL);
806 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
807 new->xbfi_startblock = bno;
808 new->xbfi_blockcount = (xfs_extlen_t)len;
809 for (prev = NULL, cur = flist->xbf_first;
810 cur != NULL;
811 prev = cur, cur = cur->xbfi_next) {
812 if (cur->xbfi_startblock >= bno)
813 break;
814 }
815 if (prev)
816 prev->xbfi_next = new;
817 else
818 flist->xbf_first = new;
819 new->xbfi_next = cur;
820 flist->xbf_count++;
821}
822
823/*
824 * Remove the entry "free" from the free item list. Prev points to the
825 * previous entry, unless "free" is the head of the list.
826 */
827STATIC void
828xfs_bmap_del_free(
829 xfs_bmap_free_t *flist, /* free item list header */
830 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
831 xfs_bmap_free_item_t *free) /* list item to be freed */
832{
833 if (prev)
834 prev->xbfi_next = free->xbfi_next;
835 else
836 flist->xbf_first = free->xbfi_next;
837 flist->xbf_count--;
838 kmem_zone_free(xfs_bmap_free_item_zone, free);
839}
840
841
842/*
843 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
844 * caller. Frees all the extents that need freeing, which must be done
845 * last due to locking considerations. We never free any extents in
846 * the first transaction.
847 *
848 * Return 1 if the given transaction was committed and a new one
849 * started, and 0 otherwise in the committed parameter.
850 */
851int /* error */
852xfs_bmap_finish(
853 xfs_trans_t **tp, /* transaction pointer addr */
854 xfs_bmap_free_t *flist, /* i/o: list extents to free */
855 int *committed) /* xact committed or not */
856{
857 xfs_efd_log_item_t *efd; /* extent free data */
858 xfs_efi_log_item_t *efi; /* extent free intention */
859 int error; /* error return value */
860 xfs_bmap_free_item_t *free; /* free extent item */
861 unsigned int logres; /* new log reservation */
862 unsigned int logcount; /* new log count */
863 xfs_mount_t *mp; /* filesystem mount structure */
864 xfs_bmap_free_item_t *next; /* next item on free list */
865 xfs_trans_t *ntp; /* new transaction pointer */
866
867 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
868 if (flist->xbf_count == 0) {
869 *committed = 0;
870 return 0;
871 }
872 ntp = *tp;
873 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
874 for (free = flist->xbf_first; free; free = free->xbfi_next)
875 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
876 free->xbfi_blockcount);
877 logres = ntp->t_log_res;
878 logcount = ntp->t_log_count;
879 ntp = xfs_trans_dup(*tp);
880 error = xfs_trans_commit(*tp, 0);
881 *tp = ntp;
882 *committed = 1;
883 /*
884 * We have a new transaction, so we should return committed=1,
885 * even though we're returning an error.
886 */
887 if (error)
888 return error;
889
890 /*
891 * transaction commit worked ok so we can drop the extra ticket
892 * reference that we gained in xfs_trans_dup()
893 */
894 xfs_log_ticket_put(ntp->t_ticket);
895
896 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
897 logcount)))
898 return error;
899 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
900 for (free = flist->xbf_first; free != NULL; free = next) {
901 next = free->xbfi_next;
902 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
903 free->xbfi_blockcount))) {
904 /*
905 * The bmap free list will be cleaned up at a
906 * higher level. The EFI will be canceled when
907 * this transaction is aborted.
908 * Need to force shutdown here to make sure it
909 * happens, since this transaction may not be
910 * dirty yet.
911 */
912 mp = ntp->t_mountp;
913 if (!XFS_FORCED_SHUTDOWN(mp))
914 xfs_force_shutdown(mp,
915 (error == EFSCORRUPTED) ?
916 SHUTDOWN_CORRUPT_INCORE :
917 SHUTDOWN_META_IO_ERROR);
918 return error;
919 }
920 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
921 free->xbfi_blockcount);
922 xfs_bmap_del_free(flist, NULL, free);
923 }
924 return 0;
925}
926
927/*
928 * Free up any items left in the list.
929 */
930void
931xfs_bmap_cancel(
932 xfs_bmap_free_t *flist) /* list of bmap_free_items */
933{
934 xfs_bmap_free_item_t *free; /* free list item */
935 xfs_bmap_free_item_t *next;
936
937 if (flist->xbf_count == 0)
938 return;
939 ASSERT(flist->xbf_first != NULL);
940 for (free = flist->xbf_first; free; free = next) {
941 next = free->xbfi_next;
942 xfs_bmap_del_free(flist, NULL, free);
943 }
944 ASSERT(flist->xbf_count == 0);
945}
946
947/*
948 * Inode fork format manipulation functions
949 */
950
951/*
952 * Transform a btree format file with only one leaf node, where the
953 * extents list will fit in the inode, into an extents format file.
954 * Since the file extents are already in-core, all we have to do is
955 * give up the space for the btree root and pitch the leaf block.
956 */
957STATIC int /* error */
958xfs_bmap_btree_to_extents(
959 xfs_trans_t *tp, /* transaction pointer */
960 xfs_inode_t *ip, /* incore inode pointer */
961 xfs_btree_cur_t *cur, /* btree cursor */
962 int *logflagsp, /* inode logging flags */
963 int whichfork) /* data or attr fork */
964{
965 /* REFERENCED */
966 struct xfs_btree_block *cblock;/* child btree block */
967 xfs_fsblock_t cbno; /* child block number */
968 xfs_buf_t *cbp; /* child block's buffer */
969 int error; /* error return value */
970 xfs_ifork_t *ifp; /* inode fork data */
971 xfs_mount_t *mp; /* mount point structure */
972 __be64 *pp; /* ptr to block address */
973 struct xfs_btree_block *rblock;/* root btree block */
974
975 mp = ip->i_mount;
976 ifp = XFS_IFORK_PTR(ip, whichfork);
977 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
978 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
979 rblock = ifp->if_broot;
980 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
981 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
982 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
983 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
984 cbno = be64_to_cpu(*pp);
985 *logflagsp = 0;
986#ifdef DEBUG
987 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
988 return error;
989#endif
990 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
991 &xfs_bmbt_buf_ops);
992 if (error)
993 return error;
994 cblock = XFS_BUF_TO_BLOCK(cbp);
995 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
996 return error;
997 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
998 ip->i_d.di_nblocks--;
999 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
1000 xfs_trans_binval(tp, cbp);
1001 if (cur->bc_bufs[0] == cbp)
1002 cur->bc_bufs[0] = NULL;
1003 xfs_iroot_realloc(ip, -1, whichfork);
1004 ASSERT(ifp->if_broot == NULL);
1005 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
1006 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
1007 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1008 return 0;
1009}
1010
1011/*
1012 * Convert an extents-format file into a btree-format file.
1013 * The new file will have a root block (in the inode) and a single child block.
1014 */
1015STATIC int /* error */
1016xfs_bmap_extents_to_btree(
1017 xfs_trans_t *tp, /* transaction pointer */
1018 xfs_inode_t *ip, /* incore inode pointer */
1019 xfs_fsblock_t *firstblock, /* first-block-allocated */
1020 xfs_bmap_free_t *flist, /* blocks freed in xaction */
1021 xfs_btree_cur_t **curp, /* cursor returned to caller */
1022 int wasdel, /* converting a delayed alloc */
1023 int *logflagsp, /* inode logging flags */
1024 int whichfork) /* data or attr fork */
1025{
1026 struct xfs_btree_block *ablock; /* allocated (child) bt block */
1027 xfs_buf_t *abp; /* buffer for ablock */
1028 xfs_alloc_arg_t args; /* allocation arguments */
1029 xfs_bmbt_rec_t *arp; /* child record pointer */
1030 struct xfs_btree_block *block; /* btree root block */
1031 xfs_btree_cur_t *cur; /* bmap btree cursor */
1032 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1033 int error; /* error return value */
1034 xfs_extnum_t i, cnt; /* extent record index */
1035 xfs_ifork_t *ifp; /* inode fork pointer */
1036 xfs_bmbt_key_t *kp; /* root block key pointer */
1037 xfs_mount_t *mp; /* mount structure */
1038 xfs_extnum_t nextents; /* number of file extents */
1039 xfs_bmbt_ptr_t *pp; /* root block address pointer */
1040
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001041 mp = ip->i_mount;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001042 ifp = XFS_IFORK_PTR(ip, whichfork);
1043 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
1044
1045 /*
1046 * Make space in the inode incore.
1047 */
1048 xfs_iroot_realloc(ip, 1, whichfork);
1049 ifp->if_flags |= XFS_IFBROOT;
1050
1051 /*
1052 * Fill in the root.
1053 */
1054 block = ifp->if_broot;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001055 if (xfs_sb_version_hascrc(&mp->m_sb))
1056 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
1057 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
1058 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
1059 else
1060 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
1061 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
1062 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001063
1064 /*
1065 * Need a cursor. Can't allocate until bb_level is filled in.
1066 */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001067 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1068 cur->bc_private.b.firstblock = *firstblock;
1069 cur->bc_private.b.flist = flist;
1070 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
1071 /*
1072 * Convert to a btree with two levels, one record in root.
1073 */
1074 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
1075 memset(&args, 0, sizeof(args));
1076 args.tp = tp;
1077 args.mp = mp;
1078 args.firstblock = *firstblock;
1079 if (*firstblock == NULLFSBLOCK) {
1080 args.type = XFS_ALLOCTYPE_START_BNO;
1081 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
1082 } else if (flist->xbf_low) {
1083 args.type = XFS_ALLOCTYPE_START_BNO;
1084 args.fsbno = *firstblock;
1085 } else {
1086 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1087 args.fsbno = *firstblock;
1088 }
1089 args.minlen = args.maxlen = args.prod = 1;
1090 args.wasdel = wasdel;
1091 *logflagsp = 0;
1092 if ((error = xfs_alloc_vextent(&args))) {
1093 xfs_iroot_realloc(ip, -1, whichfork);
1094 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1095 return error;
1096 }
1097 /*
1098 * Allocation can't fail, the space was reserved.
1099 */
1100 ASSERT(args.fsbno != NULLFSBLOCK);
1101 ASSERT(*firstblock == NULLFSBLOCK ||
1102 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
1103 (flist->xbf_low &&
1104 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
1105 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
1106 cur->bc_private.b.allocated++;
1107 ip->i_d.di_nblocks++;
1108 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
1109 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
1110 /*
1111 * Fill in the child block.
1112 */
1113 abp->b_ops = &xfs_bmbt_buf_ops;
1114 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001115 if (xfs_sb_version_hascrc(&mp->m_sb))
1116 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
1117 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
1118 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
1119 else
1120 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
1121 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
1122 XFS_BTREE_LONG_PTRS);
1123
Dave Chinner9e5987a2013-02-25 12:31:26 +11001124 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
1125 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1126 for (cnt = i = 0; i < nextents; i++) {
1127 ep = xfs_iext_get_ext(ifp, i);
1128 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
1129 arp->l0 = cpu_to_be64(ep->l0);
1130 arp->l1 = cpu_to_be64(ep->l1);
1131 arp++; cnt++;
1132 }
1133 }
1134 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
1135 xfs_btree_set_numrecs(ablock, cnt);
1136
1137 /*
1138 * Fill in the root key and pointer.
1139 */
1140 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
1141 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
1142 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
1143 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
1144 be16_to_cpu(block->bb_level)));
1145 *pp = cpu_to_be64(args.fsbno);
1146
1147 /*
1148 * Do all this logging at the end so that
1149 * the root is at the right level.
1150 */
1151 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
1152 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
1153 ASSERT(*curp == NULL);
1154 *curp = cur;
1155 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
1156 return 0;
1157}
1158
1159/*
1160 * Convert a local file to an extents file.
1161 * This code is out of bounds for data forks of regular files,
1162 * since the file data needs to get logged so things will stay consistent.
1163 * (The bmap-level manipulations are ok, though).
1164 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001165void
1166xfs_bmap_local_to_extents_empty(
1167 struct xfs_inode *ip,
1168 int whichfork)
1169{
1170 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1171
1172 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1173 ASSERT(ifp->if_bytes == 0);
1174 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
1175
1176 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
1177 ifp->if_flags &= ~XFS_IFINLINE;
1178 ifp->if_flags |= XFS_IFEXTENTS;
1179 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
1180}
1181
1182
Dave Chinner9e5987a2013-02-25 12:31:26 +11001183STATIC int /* error */
1184xfs_bmap_local_to_extents(
1185 xfs_trans_t *tp, /* transaction pointer */
1186 xfs_inode_t *ip, /* incore inode pointer */
1187 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
1188 xfs_extlen_t total, /* total blocks needed by transaction */
1189 int *logflagsp, /* inode logging flags */
1190 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001191 void (*init_fn)(struct xfs_trans *tp,
1192 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001193 struct xfs_inode *ip,
1194 struct xfs_ifork *ifp))
1195{
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001196 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001197 int flags; /* logging flags returned */
1198 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001199 xfs_alloc_arg_t args; /* allocation arguments */
1200 xfs_buf_t *bp; /* buffer for extent block */
1201 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001202
1203 /*
1204 * We don't want to deal with the case of keeping inode data inline yet.
1205 * So sending the data fork of a regular inode is invalid.
1206 */
1207 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
1208 ifp = XFS_IFORK_PTR(ip, whichfork);
1209 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001210
1211 if (!ifp->if_bytes) {
1212 xfs_bmap_local_to_extents_empty(ip, whichfork);
1213 flags = XFS_ILOG_CORE;
1214 goto done;
1215 }
1216
Dave Chinner9e5987a2013-02-25 12:31:26 +11001217 flags = 0;
1218 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001219 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
1220 XFS_IFINLINE);
1221 memset(&args, 0, sizeof(args));
1222 args.tp = tp;
1223 args.mp = ip->i_mount;
1224 args.firstblock = *firstblock;
1225 /*
1226 * Allocate a block. We know we need only one, since the
1227 * file currently fits in an inode.
1228 */
1229 if (*firstblock == NULLFSBLOCK) {
1230 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
1231 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001232 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001233 args.fsbno = *firstblock;
1234 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001235 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001236 args.total = total;
1237 args.minlen = args.maxlen = args.prod = 1;
1238 error = xfs_alloc_vextent(&args);
1239 if (error)
1240 goto done;
1241
1242 /* Can't fail, the space was reserved. */
1243 ASSERT(args.fsbno != NULLFSBLOCK);
1244 ASSERT(args.len == 1);
1245 *firstblock = args.fsbno;
1246 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
1247
1248 /* initialise the block and copy the data */
1249 init_fn(tp, bp, ip, ifp);
1250
1251 /* account for the change in fork size and log everything */
1252 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
1253 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
1254 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001255 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001256
1257 xfs_iext_add(ifp, 0, 1);
1258 ep = xfs_iext_get_ext(ifp, 0);
1259 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
1260 trace_xfs_bmap_post_update(ip, 0,
1261 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
1262 _THIS_IP_);
1263 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
1264 ip->i_d.di_nblocks = 1;
1265 xfs_trans_mod_dquot_byino(tp, ip,
1266 XFS_TRANS_DQ_BCOUNT, 1L);
1267 flags |= xfs_ilog_fext(whichfork);
1268
Dave Chinner9e5987a2013-02-25 12:31:26 +11001269done:
1270 *logflagsp = flags;
1271 return error;
1272}
1273
1274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 * Called from xfs_bmap_add_attrfork to handle btree format files.
1276 */
1277STATIC int /* error */
1278xfs_bmap_add_attrfork_btree(
1279 xfs_trans_t *tp, /* transaction pointer */
1280 xfs_inode_t *ip, /* incore inode pointer */
1281 xfs_fsblock_t *firstblock, /* first block allocated */
1282 xfs_bmap_free_t *flist, /* blocks to free at commit */
1283 int *flags) /* inode logging flags */
1284{
1285 xfs_btree_cur_t *cur; /* btree cursor */
1286 int error; /* error return value */
1287 xfs_mount_t *mp; /* file system mount struct */
1288 int stat; /* newroot status */
1289
1290 mp = ip->i_mount;
1291 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1292 *flags |= XFS_ILOG_DBROOT;
1293 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001294 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 cur->bc_private.b.flist = flist;
1296 cur->bc_private.b.firstblock = *firstblock;
1297 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1298 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001299 /* must be at least one entry */
1300 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001301 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 goto error0;
1303 if (stat == 0) {
1304 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1305 return XFS_ERROR(ENOSPC);
1306 }
1307 *firstblock = cur->bc_private.b.firstblock;
1308 cur->bc_private.b.allocated = 0;
1309 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1310 }
1311 return 0;
1312error0:
1313 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1314 return error;
1315}
1316
1317/*
1318 * Called from xfs_bmap_add_attrfork to handle extents format files.
1319 */
1320STATIC int /* error */
1321xfs_bmap_add_attrfork_extents(
1322 xfs_trans_t *tp, /* transaction pointer */
1323 xfs_inode_t *ip, /* incore inode pointer */
1324 xfs_fsblock_t *firstblock, /* first block allocated */
1325 xfs_bmap_free_t *flist, /* blocks to free at commit */
1326 int *flags) /* inode logging flags */
1327{
1328 xfs_btree_cur_t *cur; /* bmap btree cursor */
1329 int error; /* error return value */
1330
1331 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1332 return 0;
1333 cur = NULL;
1334 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
1335 flags, XFS_DATA_FORK);
1336 if (cur) {
1337 cur->bc_private.b.allocated = 0;
1338 xfs_btree_del_cursor(cur,
1339 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1340 }
1341 return error;
1342}
1343
1344/*
Dave Chinner1e823792013-02-11 15:58:13 +11001345 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1346 * different data fork content type needs a different callout to do the
1347 * conversion. Some are basic and only require special block initialisation
1348 * callouts for the data formating, others (directories) are so specialised they
1349 * handle everything themselves.
1350 *
1351 * XXX (dgc): investigate whether directory conversion can use the generic
1352 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001353 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
1355STATIC int /* error */
1356xfs_bmap_add_attrfork_local(
1357 xfs_trans_t *tp, /* transaction pointer */
1358 xfs_inode_t *ip, /* incore inode pointer */
1359 xfs_fsblock_t *firstblock, /* first block allocated */
1360 xfs_bmap_free_t *flist, /* blocks to free at commit */
1361 int *flags) /* inode logging flags */
1362{
1363 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1366 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001367
Al Viroabbede12011-07-26 02:31:30 -04001368 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 memset(&dargs, 0, sizeof(dargs));
1370 dargs.dp = ip;
1371 dargs.firstblock = firstblock;
1372 dargs.flist = flist;
Dave Chinner1e823792013-02-11 15:58:13 +11001373 dargs.total = ip->i_mount->m_dirblkfsbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 dargs.whichfork = XFS_DATA_FORK;
1375 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001376 return xfs_dir2_sf_to_block(&dargs);
1377 }
1378
1379 if (S_ISLNK(ip->i_d.di_mode))
1380 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1381 flags, XFS_DATA_FORK,
1382 xfs_symlink_local_to_remote);
1383
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001384 /* should only be called for types that support local format data */
1385 ASSERT(0);
1386 return EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
1389/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001390 * Convert inode from non-attributed to attributed.
1391 * Must not be in a transaction, ip must not be locked.
1392 */
1393int /* error code */
1394xfs_bmap_add_attrfork(
1395 xfs_inode_t *ip, /* incore inode pointer */
1396 int size, /* space new attribute needs */
1397 int rsvd) /* xact may use reserved blks */
1398{
1399 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1400 xfs_bmap_free_t flist; /* freed extent records */
1401 xfs_mount_t *mp; /* mount structure */
1402 xfs_trans_t *tp; /* transaction pointer */
1403 int blks; /* space reservation */
1404 int version = 1; /* superblock attr version */
1405 int committed; /* xaction was committed */
1406 int logflags; /* logging flags */
1407 int error; /* error return value */
1408
1409 ASSERT(XFS_IFORK_Q(ip) == 0);
1410
1411 mp = ip->i_mount;
1412 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1413 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
1414 blks = XFS_ADDAFORK_SPACE_RES(mp);
1415 if (rsvd)
1416 tp->t_flags |= XFS_TRANS_RESERVE;
1417 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
1418 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
1419 goto error0;
1420 xfs_ilock(ip, XFS_ILOCK_EXCL);
1421 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1422 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1423 XFS_QMOPT_RES_REGBLKS);
1424 if (error) {
1425 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1426 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
1427 return error;
1428 }
1429 if (XFS_IFORK_Q(ip))
1430 goto error1;
1431 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1432 /*
1433 * For inodes coming from pre-6.2 filesystems.
1434 */
1435 ASSERT(ip->i_d.di_aformat == 0);
1436 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1437 }
1438 ASSERT(ip->i_d.di_anextents == 0);
1439
1440 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1441 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1442
1443 switch (ip->i_d.di_format) {
1444 case XFS_DINODE_FMT_DEV:
1445 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1446 break;
1447 case XFS_DINODE_FMT_UUID:
1448 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1449 break;
1450 case XFS_DINODE_FMT_LOCAL:
1451 case XFS_DINODE_FMT_EXTENTS:
1452 case XFS_DINODE_FMT_BTREE:
1453 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1454 if (!ip->i_d.di_forkoff)
1455 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1456 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1457 version = 2;
1458 break;
1459 default:
1460 ASSERT(0);
1461 error = XFS_ERROR(EINVAL);
1462 goto error1;
1463 }
1464
1465 ASSERT(ip->i_afp == NULL);
1466 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1467 ip->i_afp->if_flags = XFS_IFEXTENTS;
1468 logflags = 0;
1469 xfs_bmap_init(&flist, &firstblock);
1470 switch (ip->i_d.di_format) {
1471 case XFS_DINODE_FMT_LOCAL:
1472 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
1473 &logflags);
1474 break;
1475 case XFS_DINODE_FMT_EXTENTS:
1476 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1477 &flist, &logflags);
1478 break;
1479 case XFS_DINODE_FMT_BTREE:
1480 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
1481 &logflags);
1482 break;
1483 default:
1484 error = 0;
1485 break;
1486 }
1487 if (logflags)
1488 xfs_trans_log_inode(tp, ip, logflags);
1489 if (error)
1490 goto error2;
1491 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1492 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1493 __int64_t sbfields = 0;
1494
1495 spin_lock(&mp->m_sb_lock);
1496 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1497 xfs_sb_version_addattr(&mp->m_sb);
1498 sbfields |= XFS_SB_VERSIONNUM;
1499 }
1500 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1501 xfs_sb_version_addattr2(&mp->m_sb);
1502 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
1503 }
1504 if (sbfields) {
1505 spin_unlock(&mp->m_sb_lock);
1506 xfs_mod_sb(tp, sbfields);
1507 } else
1508 spin_unlock(&mp->m_sb_lock);
1509 }
1510
1511 error = xfs_bmap_finish(&tp, &flist, &committed);
1512 if (error)
1513 goto error2;
1514 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1515error2:
1516 xfs_bmap_cancel(&flist);
1517error1:
1518 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1519error0:
1520 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1521 return error;
1522}
1523
1524/*
1525 * Internal and external extent tree search functions.
1526 */
1527
1528/*
1529 * Read in the extents to if_extents.
1530 * All inode fields are set up by caller, we just traverse the btree
1531 * and copy the records in. If the file system cannot contain unwritten
1532 * extents, the records are checked for no "state" flags.
1533 */
1534int /* error */
1535xfs_bmap_read_extents(
1536 xfs_trans_t *tp, /* transaction pointer */
1537 xfs_inode_t *ip, /* incore inode */
1538 int whichfork) /* data or attr fork */
1539{
1540 struct xfs_btree_block *block; /* current btree block */
1541 xfs_fsblock_t bno; /* block # of "block" */
1542 xfs_buf_t *bp; /* buffer for "block" */
1543 int error; /* error return value */
1544 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1545 xfs_extnum_t i, j; /* index into the extents list */
1546 xfs_ifork_t *ifp; /* fork structure */
1547 int level; /* btree level, for checking */
1548 xfs_mount_t *mp; /* file system mount structure */
1549 __be64 *pp; /* pointer to block address */
1550 /* REFERENCED */
1551 xfs_extnum_t room; /* number of entries there's room for */
1552
1553 bno = NULLFSBLOCK;
1554 mp = ip->i_mount;
1555 ifp = XFS_IFORK_PTR(ip, whichfork);
1556 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1557 XFS_EXTFMT_INODE(ip);
1558 block = ifp->if_broot;
1559 /*
1560 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1561 */
1562 level = be16_to_cpu(block->bb_level);
1563 ASSERT(level > 0);
1564 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1565 bno = be64_to_cpu(*pp);
1566 ASSERT(bno != NULLDFSBNO);
1567 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1568 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1569 /*
1570 * Go down the tree until leaf level is reached, following the first
1571 * pointer (leftmost) at each level.
1572 */
1573 while (level-- > 0) {
1574 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1575 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1576 if (error)
1577 return error;
1578 block = XFS_BUF_TO_BLOCK(bp);
1579 XFS_WANT_CORRUPTED_GOTO(
1580 xfs_bmap_sanity_check(mp, bp, level),
1581 error0);
1582 if (level == 0)
1583 break;
1584 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1585 bno = be64_to_cpu(*pp);
1586 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
1587 xfs_trans_brelse(tp, bp);
1588 }
1589 /*
1590 * Here with bp and block set to the leftmost leaf node in the tree.
1591 */
1592 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1593 i = 0;
1594 /*
1595 * Loop over all leaf nodes. Copy information to the extent records.
1596 */
1597 for (;;) {
1598 xfs_bmbt_rec_t *frp;
1599 xfs_fsblock_t nextbno;
1600 xfs_extnum_t num_recs;
1601 xfs_extnum_t start;
1602
1603 num_recs = xfs_btree_get_numrecs(block);
1604 if (unlikely(i + num_recs > room)) {
1605 ASSERT(i + num_recs <= room);
1606 xfs_warn(ip->i_mount,
1607 "corrupt dinode %Lu, (btree extents).",
1608 (unsigned long long) ip->i_ino);
1609 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1610 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1611 goto error0;
1612 }
1613 XFS_WANT_CORRUPTED_GOTO(
1614 xfs_bmap_sanity_check(mp, bp, 0),
1615 error0);
1616 /*
1617 * Read-ahead the next leaf block, if any.
1618 */
1619 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1620 if (nextbno != NULLFSBLOCK)
1621 xfs_btree_reada_bufl(mp, nextbno, 1,
1622 &xfs_bmbt_buf_ops);
1623 /*
1624 * Copy records into the extent records.
1625 */
1626 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1627 start = i;
1628 for (j = 0; j < num_recs; j++, i++, frp++) {
1629 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1630 trp->l0 = be64_to_cpu(frp->l0);
1631 trp->l1 = be64_to_cpu(frp->l1);
1632 }
1633 if (exntf == XFS_EXTFMT_NOSTATE) {
1634 /*
1635 * Check all attribute bmap btree records and
1636 * any "older" data bmap btree records for a
1637 * set bit in the "extent flag" position.
1638 */
1639 if (unlikely(xfs_check_nostate_extents(ifp,
1640 start, num_recs))) {
1641 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1642 XFS_ERRLEVEL_LOW,
1643 ip->i_mount);
1644 goto error0;
1645 }
1646 }
1647 xfs_trans_brelse(tp, bp);
1648 bno = nextbno;
1649 /*
1650 * If we've reached the end, stop.
1651 */
1652 if (bno == NULLFSBLOCK)
1653 break;
1654 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1655 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1656 if (error)
1657 return error;
1658 block = XFS_BUF_TO_BLOCK(bp);
1659 }
1660 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1661 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1662 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1663 return 0;
1664error0:
1665 xfs_trans_brelse(tp, bp);
1666 return XFS_ERROR(EFSCORRUPTED);
1667}
1668
1669
1670/*
1671 * Search the extent records for the entry containing block bno.
1672 * If bno lies in a hole, point to the next entry. If bno lies
1673 * past eof, *eofp will be set, and *prevp will contain the last
1674 * entry (null if none). Else, *lastxp will be set to the index
1675 * of the found entry; *gotp will contain the entry.
1676 */
1677STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1678xfs_bmap_search_multi_extents(
1679 xfs_ifork_t *ifp, /* inode fork pointer */
1680 xfs_fileoff_t bno, /* block number searched for */
1681 int *eofp, /* out: end of file found */
1682 xfs_extnum_t *lastxp, /* out: last extent index */
1683 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1684 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1685{
1686 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1687 xfs_extnum_t lastx; /* last extent index */
1688
1689 /*
1690 * Initialize the extent entry structure to catch access to
1691 * uninitialized br_startblock field.
1692 */
1693 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1694 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1695 gotp->br_state = XFS_EXT_INVALID;
1696#if XFS_BIG_BLKNOS
1697 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
1698#else
1699 gotp->br_startblock = 0xffffa5a5;
1700#endif
1701 prevp->br_startoff = NULLFILEOFF;
1702
1703 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1704 if (lastx > 0) {
1705 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1706 }
1707 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1708 xfs_bmbt_get_all(ep, gotp);
1709 *eofp = 0;
1710 } else {
1711 if (lastx > 0) {
1712 *gotp = *prevp;
1713 }
1714 *eofp = 1;
1715 ep = NULL;
1716 }
1717 *lastxp = lastx;
1718 return ep;
1719}
1720
1721/*
1722 * Search the extents list for the inode, for the extent containing bno.
1723 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1724 * *eofp will be set, and *prevp will contain the last entry (null if none).
1725 * Else, *lastxp will be set to the index of the found
1726 * entry; *gotp will contain the entry.
1727 */
1728STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1729xfs_bmap_search_extents(
1730 xfs_inode_t *ip, /* incore inode pointer */
1731 xfs_fileoff_t bno, /* block number searched for */
1732 int fork, /* data or attr fork */
1733 int *eofp, /* out: end of file found */
1734 xfs_extnum_t *lastxp, /* out: last extent index */
1735 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1736 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1737{
1738 xfs_ifork_t *ifp; /* inode fork pointer */
1739 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1740
1741 XFS_STATS_INC(xs_look_exlist);
1742 ifp = XFS_IFORK_PTR(ip, fork);
1743
1744 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1745
1746 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1747 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1748 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1749 "Access to block zero in inode %llu "
1750 "start_block: %llx start_off: %llx "
1751 "blkcnt: %llx extent-state: %x lastx: %x\n",
1752 (unsigned long long)ip->i_ino,
1753 (unsigned long long)gotp->br_startblock,
1754 (unsigned long long)gotp->br_startoff,
1755 (unsigned long long)gotp->br_blockcount,
1756 gotp->br_state, *lastxp);
1757 *lastxp = NULLEXTNUM;
1758 *eofp = 1;
1759 return NULL;
1760 }
1761 return ep;
1762}
1763
1764/*
1765 * Returns the file-relative block number of the first unused block(s)
1766 * in the file with at least "len" logically contiguous blocks free.
1767 * This is the lowest-address hole if the file has holes, else the first block
1768 * past the end of file.
1769 * Return 0 if the file is currently local (in-inode).
1770 */
1771int /* error */
1772xfs_bmap_first_unused(
1773 xfs_trans_t *tp, /* transaction pointer */
1774 xfs_inode_t *ip, /* incore inode */
1775 xfs_extlen_t len, /* size of hole to find */
1776 xfs_fileoff_t *first_unused, /* unused block */
1777 int whichfork) /* data or attr fork */
1778{
1779 int error; /* error return value */
1780 int idx; /* extent record index */
1781 xfs_ifork_t *ifp; /* inode fork pointer */
1782 xfs_fileoff_t lastaddr; /* last block number seen */
1783 xfs_fileoff_t lowest; /* lowest useful block */
1784 xfs_fileoff_t max; /* starting useful block */
1785 xfs_fileoff_t off; /* offset for this block */
1786 xfs_extnum_t nextents; /* number of extent entries */
1787
1788 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1789 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1790 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1791 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1792 *first_unused = 0;
1793 return 0;
1794 }
1795 ifp = XFS_IFORK_PTR(ip, whichfork);
1796 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1797 (error = xfs_iread_extents(tp, ip, whichfork)))
1798 return error;
1799 lowest = *first_unused;
1800 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1801 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1802 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1803 off = xfs_bmbt_get_startoff(ep);
1804 /*
1805 * See if the hole before this extent will work.
1806 */
1807 if (off >= lowest + len && off - max >= len) {
1808 *first_unused = max;
1809 return 0;
1810 }
1811 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1812 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1813 }
1814 *first_unused = max;
1815 return 0;
1816}
1817
1818/*
1819 * Returns the file-relative block number of the last block + 1 before
1820 * last_block (input value) in the file.
1821 * This is not based on i_size, it is based on the extent records.
1822 * Returns 0 for local files, as they do not have extent records.
1823 */
1824int /* error */
1825xfs_bmap_last_before(
1826 xfs_trans_t *tp, /* transaction pointer */
1827 xfs_inode_t *ip, /* incore inode */
1828 xfs_fileoff_t *last_block, /* last block */
1829 int whichfork) /* data or attr fork */
1830{
1831 xfs_fileoff_t bno; /* input file offset */
1832 int eof; /* hit end of file */
1833 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1834 int error; /* error return value */
1835 xfs_bmbt_irec_t got; /* current extent value */
1836 xfs_ifork_t *ifp; /* inode fork pointer */
1837 xfs_extnum_t lastx; /* last extent used */
1838 xfs_bmbt_irec_t prev; /* previous extent value */
1839
1840 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1841 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1842 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
1843 return XFS_ERROR(EIO);
1844 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1845 *last_block = 0;
1846 return 0;
1847 }
1848 ifp = XFS_IFORK_PTR(ip, whichfork);
1849 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1850 (error = xfs_iread_extents(tp, ip, whichfork)))
1851 return error;
1852 bno = *last_block - 1;
1853 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1854 &prev);
1855 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1856 if (prev.br_startoff == NULLFILEOFF)
1857 *last_block = 0;
1858 else
1859 *last_block = prev.br_startoff + prev.br_blockcount;
1860 }
1861 /*
1862 * Otherwise *last_block is already the right answer.
1863 */
1864 return 0;
1865}
1866
1867STATIC int
1868xfs_bmap_last_extent(
1869 struct xfs_trans *tp,
1870 struct xfs_inode *ip,
1871 int whichfork,
1872 struct xfs_bmbt_irec *rec,
1873 int *is_empty)
1874{
1875 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1876 int error;
1877 int nextents;
1878
1879 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1880 error = xfs_iread_extents(tp, ip, whichfork);
1881 if (error)
1882 return error;
1883 }
1884
1885 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1886 if (nextents == 0) {
1887 *is_empty = 1;
1888 return 0;
1889 }
1890
1891 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1892 *is_empty = 0;
1893 return 0;
1894}
1895
1896/*
1897 * Check the last inode extent to determine whether this allocation will result
1898 * in blocks being allocated at the end of the file. When we allocate new data
1899 * blocks at the end of the file which do not start at the previous data block,
1900 * we will try to align the new blocks at stripe unit boundaries.
1901 *
1902 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
1903 * at, or past the EOF.
1904 */
1905STATIC int
1906xfs_bmap_isaeof(
1907 struct xfs_bmalloca *bma,
1908 int whichfork)
1909{
1910 struct xfs_bmbt_irec rec;
1911 int is_empty;
1912 int error;
1913
1914 bma->aeof = 0;
1915 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1916 &is_empty);
1917 if (error || is_empty)
1918 return error;
1919
1920 /*
1921 * Check if we are allocation or past the last extent, or at least into
1922 * the last delayed allocated extent.
1923 */
1924 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1925 (bma->offset >= rec.br_startoff &&
1926 isnullstartblock(rec.br_startblock));
1927 return 0;
1928}
1929
1930/*
1931 * Check if the endoff is outside the last extent. If so the caller will grow
1932 * the allocation to a stripe unit boundary. All offsets are considered outside
1933 * the end of file for an empty fork, so 1 is returned in *eof in that case.
1934 */
1935int
1936xfs_bmap_eof(
1937 struct xfs_inode *ip,
1938 xfs_fileoff_t endoff,
1939 int whichfork,
1940 int *eof)
1941{
1942 struct xfs_bmbt_irec rec;
1943 int error;
1944
1945 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
1946 if (error || *eof)
1947 return error;
1948
1949 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
1950 return 0;
1951}
1952
1953/*
1954 * Returns the file-relative block number of the first block past eof in
1955 * the file. This is not based on i_size, it is based on the extent records.
1956 * Returns 0 for local files, as they do not have extent records.
1957 */
1958int
1959xfs_bmap_last_offset(
1960 struct xfs_trans *tp,
1961 struct xfs_inode *ip,
1962 xfs_fileoff_t *last_block,
1963 int whichfork)
1964{
1965 struct xfs_bmbt_irec rec;
1966 int is_empty;
1967 int error;
1968
1969 *last_block = 0;
1970
1971 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1972 return 0;
1973
1974 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1975 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1976 return XFS_ERROR(EIO);
1977
1978 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1979 if (error || is_empty)
1980 return error;
1981
1982 *last_block = rec.br_startoff + rec.br_blockcount;
1983 return 0;
1984}
1985
1986/*
1987 * Returns whether the selected fork of the inode has exactly one
1988 * block or not. For the data fork we check this matches di_size,
1989 * implying the file's range is 0..bsize-1.
1990 */
1991int /* 1=>1 block, 0=>otherwise */
1992xfs_bmap_one_block(
1993 xfs_inode_t *ip, /* incore inode */
1994 int whichfork) /* data or attr fork */
1995{
1996 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1997 xfs_ifork_t *ifp; /* inode fork pointer */
1998 int rval; /* return value */
1999 xfs_bmbt_irec_t s; /* internal version of extent */
2000
2001#ifndef DEBUG
2002 if (whichfork == XFS_DATA_FORK)
2003 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
2004#endif /* !DEBUG */
2005 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
2006 return 0;
2007 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
2008 return 0;
2009 ifp = XFS_IFORK_PTR(ip, whichfork);
2010 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2011 ep = xfs_iext_get_ext(ifp, 0);
2012 xfs_bmbt_get_all(ep, &s);
2013 rval = s.br_startoff == 0 && s.br_blockcount == 1;
2014 if (rval && whichfork == XFS_DATA_FORK)
2015 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
2016 return rval;
2017}
2018
2019/*
2020 * Extent tree manipulation functions used during allocation.
2021 */
2022
2023/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002024 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 */
2026STATIC int /* error */
2027xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002028 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002030 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002032 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002035 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 xfs_fileoff_t new_endoff; /* end offset of new entry */
2037 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2038 /* left is 0, right is 1, prev is 2 */
2039 int rval=0; /* return value (logging flags) */
2040 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002041 xfs_filblks_t da_new; /* new count del alloc blocks used */
2042 xfs_filblks_t da_old; /* old count del alloc blocks used */
2043 xfs_filblks_t temp=0; /* value for da_new calculations */
2044 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002047 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002048
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002049 ASSERT(bma->idx >= 0);
2050 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002051 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002052 ASSERT(!bma->cur ||
2053 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002054
2055 XFS_STATS_INC(xs_add_exlist);
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057#define LEFT r[0]
2058#define RIGHT r[1]
2059#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 /*
2062 * Set up a bunch of variables to make the tests simpler.
2063 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002064 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 xfs_bmbt_get_all(ep, &PREV);
2066 new_endoff = new->br_startoff + new->br_blockcount;
2067 ASSERT(PREV.br_startoff <= new->br_startoff);
2068 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002069
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002070 da_old = startblockval(PREV.br_startblock);
2071 da_new = 0;
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 /*
2074 * Set flags determining what part of the previous delayed allocation
2075 * extent is being replaced by a real allocation.
2076 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002077 if (PREV.br_startoff == new->br_startoff)
2078 state |= BMAP_LEFT_FILLING;
2079 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2080 state |= BMAP_RIGHT_FILLING;
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 /*
2083 * Check and set flags if this segment has a left neighbor.
2084 * Don't set contiguous if the combined extent would be too large.
2085 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002086 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002087 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002088 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002089
2090 if (isnullstartblock(LEFT.br_startblock))
2091 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002093
2094 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2095 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2096 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2097 LEFT.br_state == new->br_state &&
2098 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2099 state |= BMAP_LEFT_CONTIG;
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /*
2102 * Check and set flags if this segment has a right neighbor.
2103 * Don't set contiguous if the combined extent would be too large.
2104 * Also check for all-three-contiguous being too large.
2105 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002106 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002107 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002108 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002109
2110 if (isnullstartblock(RIGHT.br_startblock))
2111 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002113
2114 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2115 new_endoff == RIGHT.br_startoff &&
2116 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2117 new->br_state == RIGHT.br_state &&
2118 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2119 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2120 BMAP_RIGHT_FILLING)) !=
2121 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2122 BMAP_RIGHT_FILLING) ||
2123 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2124 <= MAXEXTLEN))
2125 state |= BMAP_RIGHT_CONTIG;
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 error = 0;
2128 /*
2129 * Switch out based on the FILLING and CONTIG state bits.
2130 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002131 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2132 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2133 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2134 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /*
2136 * Filling in all of a previously delayed allocation extent.
2137 * The left and right neighbors are both contiguous with new.
2138 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002139 bma->idx--;
2140 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2141 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 LEFT.br_blockcount + PREV.br_blockcount +
2143 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002144 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002145
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002146 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
2147 bma->ip->i_d.di_nextents--;
2148 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2150 else {
2151 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002152 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002154 RIGHT.br_blockcount, &i);
2155 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002157 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002158 error = xfs_btree_delete(bma->cur, &i);
2159 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002161 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002162 error = xfs_btree_decrement(bma->cur, 0, &i);
2163 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002165 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002166 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 LEFT.br_startblock,
2168 LEFT.br_blockcount +
2169 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002170 RIGHT.br_blockcount, LEFT.br_state);
2171 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto done;
2173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 break;
2175
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002176 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 /*
2178 * Filling in all of a previously delayed allocation extent.
2179 * The left neighbor is contiguous, the right is not.
2180 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002181 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002182
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002183 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2184 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00002185 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002186 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002187
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002188 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
2189 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 rval = XFS_ILOG_DEXT;
2191 else {
2192 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002193 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002195 &i);
2196 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002198 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002199 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 LEFT.br_startblock,
2201 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002202 PREV.br_blockcount, LEFT.br_state);
2203 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 goto done;
2205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 break;
2207
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002208 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /*
2210 * Filling in all of a previously delayed allocation extent.
2211 * The right neighbor is contiguous, the left is not.
2212 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002213 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 xfs_bmbt_set_startblock(ep, new->br_startblock);
2215 xfs_bmbt_set_blockcount(ep,
2216 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002217 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002218
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002219 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
2220 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 rval = XFS_ILOG_DEXT;
2222 else {
2223 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002224 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002226 RIGHT.br_blockcount, &i);
2227 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002229 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002230 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 new->br_startblock,
2232 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002233 RIGHT.br_blockcount, PREV.br_state);
2234 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 goto done;
2236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 break;
2238
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002239 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 /*
2241 * Filling in all of a previously delayed allocation extent.
2242 * Neither the left nor right neighbors are contiguous with
2243 * the new one.
2244 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002245 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002247 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002248
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002249 bma->ip->i_d.di_nextents++;
2250 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2252 else {
2253 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002254 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002256 &i);
2257 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002259 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002260 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2261 error = xfs_btree_insert(bma->cur, &i);
2262 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002264 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 break;
2267
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002268 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 /*
2270 * Filling in the first part of a previous delayed allocation.
2271 * The left neighbor is contiguous.
2272 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002273 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
2274 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 LEFT.br_blockcount + new->br_blockcount);
2276 xfs_bmbt_set_startoff(ep,
2277 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002278 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002281 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002283 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 rval = XFS_ILOG_DEXT;
2285 else {
2286 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002287 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002289 &i);
2290 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002292 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002293 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 LEFT.br_startblock,
2295 LEFT.br_blockcount +
2296 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002297 LEFT.br_state);
2298 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 goto done;
2300 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002301 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002302 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002303 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002304 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002305
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002306 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 break;
2308
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002309 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 /*
2311 * Filling in the first part of a previous delayed allocation.
2312 * The left neighbor is not contiguous.
2313 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002314 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 xfs_bmbt_set_startoff(ep, new_endoff);
2316 temp = PREV.br_blockcount - new->br_blockcount;
2317 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002318 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
2319 bma->ip->i_d.di_nextents++;
2320 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2322 else {
2323 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002324 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002326 &i);
2327 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002329 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002330 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2331 error = xfs_btree_insert(bma->cur, &i);
2332 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002334 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002336
2337 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002338 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2339 bma->firstblock, bma->flist,
2340 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 rval |= tmp_rval;
2342 if (error)
2343 goto done;
2344 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002345 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002346 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002347 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2348 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002349 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002350 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 break;
2352
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002353 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 /*
2355 * Filling in the last part of a previous delayed allocation.
2356 * The right neighbor is contiguous with the new allocation.
2357 */
2358 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002359 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002361 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002362 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 new->br_blockcount + RIGHT.br_blockcount,
2364 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002365 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2366 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 rval = XFS_ILOG_DEXT;
2368 else {
2369 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002370 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002372 RIGHT.br_blockcount, &i);
2373 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002375 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002376 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 new->br_startblock,
2378 new->br_blockcount +
2379 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002380 RIGHT.br_state);
2381 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 goto done;
2383 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002384
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002385 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002386 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002387 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002388 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002389 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002390
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002391 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 break;
2393
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002394 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 /*
2396 * Filling in the last part of a previous delayed allocation.
2397 * The right neighbor is not contiguous.
2398 */
2399 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002400 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002402 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2403 bma->ip->i_d.di_nextents++;
2404 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2406 else {
2407 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002408 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002410 &i);
2411 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002413 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002414 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2415 error = xfs_btree_insert(bma->cur, &i);
2416 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002418 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002420
2421 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002422 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2423 bma->firstblock, bma->flist, &bma->cur, 1,
2424 &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 rval |= tmp_rval;
2426 if (error)
2427 goto done;
2428 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002429 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002430 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002431 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2432 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002433 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002434 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002435
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002436 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 break;
2438
2439 case 0:
2440 /*
2441 * Filling in the middle part of a previous delayed allocation.
2442 * Contiguity is impossible here.
2443 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002444 *
2445 * We start with a delayed allocation:
2446 *
2447 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2448 * PREV @ idx
2449 *
2450 * and we are allocating:
2451 * +rrrrrrrrrrrrrrrrr+
2452 * new
2453 *
2454 * and we set it up for insertion as:
2455 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2456 * new
2457 * PREV @ idx LEFT RIGHT
2458 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 */
2460 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002462 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002463 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2464 LEFT = *new;
2465 RIGHT.br_state = PREV.br_state;
2466 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002467 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002468 RIGHT.br_startoff = new_endoff;
2469 RIGHT.br_blockcount = temp2;
2470 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002471 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2472 bma->ip->i_d.di_nextents++;
2473 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2475 else {
2476 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002477 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002479 &i);
2480 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002482 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002483 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2484 error = xfs_btree_insert(bma->cur, &i);
2485 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002487 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002489
2490 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002491 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2492 bma->firstblock, bma->flist, &bma->cur,
2493 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 rval |= tmp_rval;
2495 if (error)
2496 goto done;
2497 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002498 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2499 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002500 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002501 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002502 if (diff > 0) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002503 error = xfs_icsb_modify_counters(bma->ip->i_mount,
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002504 XFS_SBS_FDBLOCKS,
2505 -((int64_t)diff), 0);
2506 ASSERT(!error);
2507 if (error)
2508 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002510
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002511 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002512 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002513 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2514 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2515 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002516 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002517 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002518
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002519 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002520 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 break;
2522
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002523 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2524 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2525 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2526 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2527 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2528 case BMAP_LEFT_CONTIG:
2529 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 /*
2531 * These cases are all impossible.
2532 */
2533 ASSERT(0);
2534 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002535
2536 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002537 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002538 int tmp_logflags; /* partial log flag return val */
2539
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002540 ASSERT(bma->cur == NULL);
2541 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2542 bma->firstblock, bma->flist, &bma->cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002543 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002544 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002545 if (error)
2546 goto done;
2547 }
2548
2549 /* adjust for changes in reserved delayed indirect blocks */
2550 if (da_old || da_new) {
2551 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002552 if (bma->cur)
2553 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002554 ASSERT(temp <= da_old);
2555 if (temp < da_old)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002556 xfs_icsb_modify_counters(bma->ip->i_mount,
2557 XFS_SBS_FDBLOCKS,
2558 (int64_t)(da_old - temp), 0);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002559 }
2560
2561 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002562 if (bma->cur)
2563 bma->cur->bc_private.b.allocated = 0;
2564
2565 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002567 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return error;
2569#undef LEFT
2570#undef RIGHT
2571#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
2574/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002575 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 */
2577STATIC int /* error */
2578xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002579 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002581 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002583 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002584 xfs_fsblock_t *first, /* pointer to firstblock variable */
2585 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002586 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002589 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002592 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 xfs_fileoff_t new_endoff; /* end offset of new entry */
2594 xfs_exntst_t newext; /* new extent state */
2595 xfs_exntst_t oldext; /* old extent state */
2596 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2597 /* left is 0, right is 1, prev is 2 */
2598 int rval=0; /* return value (logging flags) */
2599 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002601 *logflagsp = 0;
2602
2603 cur = *curp;
2604 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2605
2606 ASSERT(*idx >= 0);
2607 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2608 ASSERT(!isnullstartblock(new->br_startblock));
2609
2610 XFS_STATS_INC(xs_add_exlist);
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612#define LEFT r[0]
2613#define RIGHT r[1]
2614#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 /*
2617 * Set up a bunch of variables to make the tests simpler.
2618 */
2619 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002620 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 xfs_bmbt_get_all(ep, &PREV);
2622 newext = new->br_state;
2623 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2624 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2625 ASSERT(PREV.br_state == oldext);
2626 new_endoff = new->br_startoff + new->br_blockcount;
2627 ASSERT(PREV.br_startoff <= new->br_startoff);
2628 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 /*
2631 * Set flags determining what part of the previous oldext allocation
2632 * extent is being replaced by a newext allocation.
2633 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002634 if (PREV.br_startoff == new->br_startoff)
2635 state |= BMAP_LEFT_FILLING;
2636 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2637 state |= BMAP_RIGHT_FILLING;
2638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 /*
2640 * Check and set flags if this segment has a left neighbor.
2641 * Don't set contiguous if the combined extent would be too large.
2642 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002643 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002644 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002645 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002646
2647 if (isnullstartblock(LEFT.br_startblock))
2648 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002650
2651 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2652 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2653 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2654 LEFT.br_state == newext &&
2655 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2656 state |= BMAP_LEFT_CONTIG;
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 /*
2659 * Check and set flags if this segment has a right neighbor.
2660 * Don't set contiguous if the combined extent would be too large.
2661 * Also check for all-three-contiguous being too large.
2662 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002663 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002664 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002665 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002666 if (isnullstartblock(RIGHT.br_startblock))
2667 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002669
2670 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2671 new_endoff == RIGHT.br_startoff &&
2672 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2673 newext == RIGHT.br_state &&
2674 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2675 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2676 BMAP_RIGHT_FILLING)) !=
2677 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2678 BMAP_RIGHT_FILLING) ||
2679 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2680 <= MAXEXTLEN))
2681 state |= BMAP_RIGHT_CONTIG;
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 /*
2684 * Switch out based on the FILLING and CONTIG state bits.
2685 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002686 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2687 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2688 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2689 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 /*
2691 * Setting all of a previous oldext extent to newext.
2692 * The left and right neighbors are both contiguous with new.
2693 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002694 --*idx;
2695
2696 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2697 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 LEFT.br_blockcount + PREV.br_blockcount +
2699 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002700 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002701
Christoph Hellwigec90c552011-05-23 08:52:53 +00002702 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 ip->i_d.di_nextents -= 2;
2704 if (cur == NULL)
2705 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2706 else {
2707 rval = XFS_ILOG_CORE;
2708 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2709 RIGHT.br_startblock,
2710 RIGHT.br_blockcount, &i)))
2711 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002712 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11002713 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002715 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002716 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002718 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11002719 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002721 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002722 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002724 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2726 LEFT.br_startblock,
2727 LEFT.br_blockcount + PREV.br_blockcount +
2728 RIGHT.br_blockcount, LEFT.br_state)))
2729 goto done;
2730 }
2731 break;
2732
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002733 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /*
2735 * Setting all of a previous oldext extent to newext.
2736 * The left neighbor is contiguous, the right is not.
2737 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002738 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002739
Christoph Hellwigec90c552011-05-23 08:52:53 +00002740 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2741 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2742 LEFT.br_blockcount + PREV.br_blockcount);
2743 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2744
2745 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 ip->i_d.di_nextents--;
2747 if (cur == NULL)
2748 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2749 else {
2750 rval = XFS_ILOG_CORE;
2751 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2752 PREV.br_startblock, PREV.br_blockcount,
2753 &i)))
2754 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002755 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11002756 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002758 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002759 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002761 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2763 LEFT.br_startblock,
2764 LEFT.br_blockcount + PREV.br_blockcount,
2765 LEFT.br_state)))
2766 goto done;
2767 }
2768 break;
2769
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002770 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 /*
2772 * Setting all of a previous oldext extent to newext.
2773 * The right neighbor is contiguous, the left is not.
2774 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002775 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 xfs_bmbt_set_blockcount(ep,
2777 PREV.br_blockcount + RIGHT.br_blockcount);
2778 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002779 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2780 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 ip->i_d.di_nextents--;
2782 if (cur == NULL)
2783 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2784 else {
2785 rval = XFS_ILOG_CORE;
2786 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2787 RIGHT.br_startblock,
2788 RIGHT.br_blockcount, &i)))
2789 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002790 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5d2008-10-30 16:58:01 +11002791 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002793 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002794 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002796 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2798 new->br_startblock,
2799 new->br_blockcount + RIGHT.br_blockcount,
2800 newext)))
2801 goto done;
2802 }
2803 break;
2804
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002805 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 /*
2807 * Setting all of a previous oldext extent to newext.
2808 * Neither the left nor right neighbors are contiguous with
2809 * the new one.
2810 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002811 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002813 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (cur == NULL)
2816 rval = XFS_ILOG_DEXT;
2817 else {
2818 rval = 0;
2819 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2820 new->br_startblock, new->br_blockcount,
2821 &i)))
2822 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002823 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2825 new->br_startblock, new->br_blockcount,
2826 newext)))
2827 goto done;
2828 }
2829 break;
2830
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002831 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 /*
2833 * Setting the first part of a previous oldext extent to newext.
2834 * The left neighbor is contiguous.
2835 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002836 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2837 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 LEFT.br_blockcount + new->br_blockcount);
2839 xfs_bmbt_set_startoff(ep,
2840 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002841 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002842
Christoph Hellwigec90c552011-05-23 08:52:53 +00002843 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 xfs_bmbt_set_startblock(ep,
2845 new->br_startblock + new->br_blockcount);
2846 xfs_bmbt_set_blockcount(ep,
2847 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002848 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002849
Christoph Hellwigec90c552011-05-23 08:52:53 +00002850 --*idx;
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (cur == NULL)
2853 rval = XFS_ILOG_DEXT;
2854 else {
2855 rval = 0;
2856 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2857 PREV.br_startblock, PREV.br_blockcount,
2858 &i)))
2859 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002860 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if ((error = xfs_bmbt_update(cur,
2862 PREV.br_startoff + new->br_blockcount,
2863 PREV.br_startblock + new->br_blockcount,
2864 PREV.br_blockcount - new->br_blockcount,
2865 oldext)))
2866 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002867 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002869 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 LEFT.br_startblock,
2871 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002872 LEFT.br_state);
2873 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 goto done;
2875 }
2876 break;
2877
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002878 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 /*
2880 * Setting the first part of a previous oldext extent to newext.
2881 * The left neighbor is not contiguous.
2882 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002883 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2885 xfs_bmbt_set_startoff(ep, new_endoff);
2886 xfs_bmbt_set_blockcount(ep,
2887 PREV.br_blockcount - new->br_blockcount);
2888 xfs_bmbt_set_startblock(ep,
2889 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002890 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002891
Christoph Hellwigec90c552011-05-23 08:52:53 +00002892 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 ip->i_d.di_nextents++;
2894 if (cur == NULL)
2895 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2896 else {
2897 rval = XFS_ILOG_CORE;
2898 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2899 PREV.br_startblock, PREV.br_blockcount,
2900 &i)))
2901 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002902 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 if ((error = xfs_bmbt_update(cur,
2904 PREV.br_startoff + new->br_blockcount,
2905 PREV.br_startblock + new->br_blockcount,
2906 PREV.br_blockcount - new->br_blockcount,
2907 oldext)))
2908 goto done;
2909 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002910 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002912 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
2914 break;
2915
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002916 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 /*
2918 * Setting the last part of a previous oldext extent to newext.
2919 * The right neighbor is contiguous with the new allocation.
2920 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002921 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 xfs_bmbt_set_blockcount(ep,
2923 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002924 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2925
2926 ++*idx;
2927
2928 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2929 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002930 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002932 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 if (cur == NULL)
2935 rval = XFS_ILOG_DEXT;
2936 else {
2937 rval = 0;
2938 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2939 PREV.br_startblock,
2940 PREV.br_blockcount, &i)))
2941 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002942 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2944 PREV.br_startblock,
2945 PREV.br_blockcount - new->br_blockcount,
2946 oldext)))
2947 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002948 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 goto done;
2950 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2951 new->br_startblock,
2952 new->br_blockcount + RIGHT.br_blockcount,
2953 newext)))
2954 goto done;
2955 }
2956 break;
2957
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002958 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 /*
2960 * Setting the last part of a previous oldext extent to newext.
2961 * The right neighbor is not contiguous.
2962 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002963 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 xfs_bmbt_set_blockcount(ep,
2965 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002966 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002967
Christoph Hellwigec90c552011-05-23 08:52:53 +00002968 ++*idx;
2969 xfs_iext_insert(ip, *idx, 1, new, state);
2970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 ip->i_d.di_nextents++;
2972 if (cur == NULL)
2973 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2974 else {
2975 rval = XFS_ILOG_CORE;
2976 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2977 PREV.br_startblock, PREV.br_blockcount,
2978 &i)))
2979 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002980 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2982 PREV.br_startblock,
2983 PREV.br_blockcount - new->br_blockcount,
2984 oldext)))
2985 goto done;
2986 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2987 new->br_startblock, new->br_blockcount,
2988 &i)))
2989 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002990 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002992 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002994 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 }
2996 break;
2997
2998 case 0:
2999 /*
3000 * Setting the middle part of a previous oldext extent to
3001 * newext. Contiguity is impossible here.
3002 * One extent becomes three extents.
3003 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003004 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 xfs_bmbt_set_blockcount(ep,
3006 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003007 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 r[0] = *new;
3010 r[1].br_startoff = new_endoff;
3011 r[1].br_blockcount =
3012 PREV.br_startoff + PREV.br_blockcount - new_endoff;
3013 r[1].br_startblock = new->br_startblock + new->br_blockcount;
3014 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003015
3016 ++*idx;
3017 xfs_iext_insert(ip, *idx, 2, &r[0], state);
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 ip->i_d.di_nextents += 2;
3020 if (cur == NULL)
3021 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
3022 else {
3023 rval = XFS_ILOG_CORE;
3024 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
3025 PREV.br_startblock, PREV.br_blockcount,
3026 &i)))
3027 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003028 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 /* new right extent - oldext */
3030 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
3031 r[1].br_startblock, r[1].br_blockcount,
3032 r[1].br_state)))
3033 goto done;
3034 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10003036 cur->bc_rec.b.br_blockcount =
3037 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003038 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003040 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10003041 /*
3042 * Reset the cursor to the position of the new extent
3043 * we are about to insert as we can't trust it after
3044 * the previous insert.
3045 */
3046 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
3047 new->br_startblock, new->br_blockcount,
3048 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10003050 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10003052 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003053 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003055 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 }
3057 break;
3058
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003059 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3060 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3061 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
3062 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
3063 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3064 case BMAP_LEFT_CONTIG:
3065 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 /*
3067 * These cases are all impossible.
3068 */
3069 ASSERT(0);
3070 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003071
3072 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003073 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003074 int tmp_logflags; /* partial log flag return val */
3075
3076 ASSERT(cur == NULL);
3077 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
3078 0, &tmp_logflags, XFS_DATA_FORK);
3079 *logflagsp |= tmp_logflags;
3080 if (error)
3081 goto done;
3082 }
3083
3084 /* clear out the allocated field, done with it now in any case. */
3085 if (cur) {
3086 cur->bc_private.b.allocated = 0;
3087 *curp = cur;
3088 }
3089
3090 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003092 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return error;
3094#undef LEFT
3095#undef RIGHT
3096#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
3099/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00003100 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00003102STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103xfs_bmap_add_extent_hole_delay(
3104 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003105 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00003106 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003108 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 xfs_bmbt_irec_t left; /* left neighbor extent entry */
3110 xfs_filblks_t newlen=0; /* new indirect size */
3111 xfs_filblks_t oldlen=0; /* old indirect size */
3112 xfs_bmbt_irec_t right; /* right neighbor extent entry */
3113 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003114 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003116 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003118 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 /*
3121 * Check and set flags if this segment has a left neighbor
3122 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003123 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003124 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003125 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003126
3127 if (isnullstartblock(left.br_startblock))
3128 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003130
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 /*
3132 * Check and set flags if the current (right) segment exists.
3133 * If it doesn't exist, we're converting the hole at end-of-file.
3134 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003135 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003136 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003137 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003138
3139 if (isnullstartblock(right.br_startblock))
3140 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 /*
3144 * Set contiguity flags on the left and right neighbors.
3145 * Don't let extents get too large, even if the pieces are contiguous.
3146 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003147 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
3148 left.br_startoff + left.br_blockcount == new->br_startoff &&
3149 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
3150 state |= BMAP_LEFT_CONTIG;
3151
3152 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
3153 new->br_startoff + new->br_blockcount == right.br_startoff &&
3154 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3155 (!(state & BMAP_LEFT_CONTIG) ||
3156 (left.br_blockcount + new->br_blockcount +
3157 right.br_blockcount <= MAXEXTLEN)))
3158 state |= BMAP_RIGHT_CONTIG;
3159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 /*
3161 * Switch out based on the contiguity flags.
3162 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003163 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3164 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 /*
3166 * New allocation is contiguous with delayed allocations
3167 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003168 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003170 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 temp = left.br_blockcount + new->br_blockcount +
3172 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003173
Christoph Hellwigec90c552011-05-23 08:52:53 +00003174 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
3175 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003176 oldlen = startblockval(left.br_startblock) +
3177 startblockval(new->br_startblock) +
3178 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003180 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06003181 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00003182 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003183
Christoph Hellwigec90c552011-05-23 08:52:53 +00003184 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 break;
3186
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003187 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 /*
3189 * New allocation is contiguous with a delayed allocation
3190 * on the left.
3191 * Merge the new allocation with the left neighbor.
3192 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003193 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003195
3196 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
3197 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003198 oldlen = startblockval(left.br_startblock) +
3199 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003201 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06003202 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00003203 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 break;
3205
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003206 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 /*
3208 * New allocation is contiguous with a delayed allocation
3209 * on the right.
3210 * Merge the new allocation with the right neighbor.
3211 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003212 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003214 oldlen = startblockval(new->br_startblock) +
3215 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003217 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
3218 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003219 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003220 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 break;
3222
3223 case 0:
3224 /*
3225 * New allocation is not contiguous with another
3226 * delayed allocation.
3227 * Insert a new entry.
3228 */
3229 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003230 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 break;
3232 }
3233 if (oldlen != newlen) {
3234 ASSERT(oldlen > newlen);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003235 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00003236 (int64_t)(oldlen - newlen), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 /*
3238 * Nothing to do for disk quota accounting here.
3239 */
3240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
3243/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003244 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 */
3246STATIC int /* error */
3247xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00003248 struct xfs_bmalloca *bma,
3249 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Christoph Hellwigc6534242011-09-18 20:41:05 +00003251 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 int i; /* temp state */
3254 xfs_ifork_t *ifp; /* inode fork pointer */
3255 xfs_bmbt_irec_t left; /* left neighbor extent entry */
3256 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003257 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Christoph Hellwigc6534242011-09-18 20:41:05 +00003260 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003261
Christoph Hellwigc6534242011-09-18 20:41:05 +00003262 ASSERT(bma->idx >= 0);
3263 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003264 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00003265 ASSERT(!bma->cur ||
3266 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003267
3268 XFS_STATS_INC(xs_add_exlist);
3269
3270 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003271 if (whichfork == XFS_ATTR_FORK)
3272 state |= BMAP_ATTRFORK;
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 /*
3275 * Check and set flags if this segment has a left neighbor.
3276 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003277 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003278 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003279 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003280 if (isnullstartblock(left.br_startblock))
3281 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 /*
3285 * Check and set flags if this segment has a current value.
3286 * Not true if we're inserting into the "hole" at eof.
3287 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003288 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003289 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003290 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003291 if (isnullstartblock(right.br_startblock))
3292 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 /*
3296 * We're inserting a real allocation between "left" and "right".
3297 * Set the contiguity flags. Don't let extents get too large.
3298 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003299 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
3300 left.br_startoff + left.br_blockcount == new->br_startoff &&
3301 left.br_startblock + left.br_blockcount == new->br_startblock &&
3302 left.br_state == new->br_state &&
3303 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
3304 state |= BMAP_LEFT_CONTIG;
3305
3306 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
3307 new->br_startoff + new->br_blockcount == right.br_startoff &&
3308 new->br_startblock + new->br_blockcount == right.br_startblock &&
3309 new->br_state == right.br_state &&
3310 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3311 (!(state & BMAP_LEFT_CONTIG) ||
3312 left.br_blockcount + new->br_blockcount +
3313 right.br_blockcount <= MAXEXTLEN))
3314 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003316 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 /*
3318 * Select which case we're in here, and implement it.
3319 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003320 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3321 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 /*
3323 * New allocation is contiguous with real allocations on the
3324 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003325 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003327 --bma->idx;
3328 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3329 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 left.br_blockcount + new->br_blockcount +
3331 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003332 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003333
Christoph Hellwigc6534242011-09-18 20:41:05 +00003334 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003335
Christoph Hellwigc6534242011-09-18 20:41:05 +00003336 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3337 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3338 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003339 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003340 } else {
3341 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003342 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3343 right.br_startblock, right.br_blockcount,
3344 &i);
3345 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003346 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003347 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003348 error = xfs_btree_delete(bma->cur, &i);
3349 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003350 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003351 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003352 error = xfs_btree_decrement(bma->cur, 0, &i);
3353 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003354 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003355 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003356 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003357 left.br_startblock,
3358 left.br_blockcount +
3359 new->br_blockcount +
3360 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003361 left.br_state);
3362 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003363 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003365 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003367 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 /*
3369 * New allocation is contiguous with a real allocation
3370 * on the left.
3371 * Merge the new allocation with the left neighbor.
3372 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003373 --bma->idx;
3374 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3375 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003377 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003378
Christoph Hellwigc6534242011-09-18 20:41:05 +00003379 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003380 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003381 } else {
3382 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003383 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3384 left.br_startblock, left.br_blockcount,
3385 &i);
3386 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003387 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003388 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003389 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003390 left.br_startblock,
3391 left.br_blockcount +
3392 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003393 left.br_state);
3394 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003395 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003397 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003399 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 /*
3401 * New allocation is contiguous with a real allocation
3402 * on the right.
3403 * Merge the new allocation with the right neighbor.
3404 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003405 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3406 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003407 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 new->br_blockcount + right.br_blockcount,
3409 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003410 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003411
Christoph Hellwigc6534242011-09-18 20:41:05 +00003412 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003413 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003414 } else {
3415 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003416 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003417 right.br_startoff,
3418 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003419 right.br_blockcount, &i);
3420 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003421 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003422 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003423 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003424 new->br_startblock,
3425 new->br_blockcount +
3426 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003427 right.br_state);
3428 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003429 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
3433 case 0:
3434 /*
3435 * New allocation is not contiguous with another
3436 * real allocation.
3437 * Insert a new entry.
3438 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003439 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3440 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3441 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3442 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003443 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003444 } else {
3445 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003446 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003447 new->br_startoff,
3448 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003449 new->br_blockcount, &i);
3450 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003451 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003452 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003453 bma->cur->bc_rec.b.br_state = new->br_state;
3454 error = xfs_btree_insert(bma->cur, &i);
3455 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003456 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003457 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003461
3462 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003463 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003464 int tmp_logflags; /* partial log flag return val */
3465
Christoph Hellwigc6534242011-09-18 20:41:05 +00003466 ASSERT(bma->cur == NULL);
3467 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3468 bma->firstblock, bma->flist, &bma->cur,
3469 0, &tmp_logflags, whichfork);
3470 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003471 if (error)
3472 goto done;
3473 }
3474
3475 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003476 if (bma->cur)
3477 bma->cur->bc_private.b.allocated = 0;
3478
3479 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003480done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003481 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003482 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
Nathan Scottdd9f4382006-01-11 15:28:28 +11003485/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003486 * Functions used in the extent read, allocate and remove paths
3487 */
3488
3489/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003490 * Adjust the size of the new extent based on di_extsize and rt extsize.
3491 */
3492STATIC int
3493xfs_bmap_extsize_align(
3494 xfs_mount_t *mp,
3495 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3496 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3497 xfs_extlen_t extsz, /* align to this extent size */
3498 int rt, /* is this a realtime inode? */
3499 int eof, /* is extent at end-of-file? */
3500 int delay, /* creating delalloc extent? */
3501 int convert, /* overwriting unwritten extent? */
3502 xfs_fileoff_t *offp, /* in/out: aligned offset */
3503 xfs_extlen_t *lenp) /* in/out: aligned length */
3504{
3505 xfs_fileoff_t orig_off; /* original offset */
3506 xfs_extlen_t orig_alen; /* original length */
3507 xfs_fileoff_t orig_end; /* original off+len */
3508 xfs_fileoff_t nexto; /* next file offset */
3509 xfs_fileoff_t prevo; /* previous file offset */
3510 xfs_fileoff_t align_off; /* temp for offset */
3511 xfs_extlen_t align_alen; /* temp for length */
3512 xfs_extlen_t temp; /* temp for calculations */
3513
3514 if (convert)
3515 return 0;
3516
3517 orig_off = align_off = *offp;
3518 orig_alen = align_alen = *lenp;
3519 orig_end = orig_off + orig_alen;
3520
3521 /*
3522 * If this request overlaps an existing extent, then don't
3523 * attempt to perform any additional alignment.
3524 */
3525 if (!delay && !eof &&
3526 (orig_off >= gotp->br_startoff) &&
3527 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3528 return 0;
3529 }
3530
3531 /*
3532 * If the file offset is unaligned vs. the extent size
3533 * we need to align it. This will be possible unless
3534 * the file was previously written with a kernel that didn't
3535 * perform this alignment, or if a truncate shot us in the
3536 * foot.
3537 */
3538 temp = do_mod(orig_off, extsz);
3539 if (temp) {
3540 align_alen += temp;
3541 align_off -= temp;
3542 }
3543 /*
3544 * Same adjustment for the end of the requested area.
3545 */
3546 if ((temp = (align_alen % extsz))) {
3547 align_alen += extsz - temp;
3548 }
3549 /*
3550 * If the previous block overlaps with this proposed allocation
3551 * then move the start forward without adjusting the length.
3552 */
3553 if (prevp->br_startoff != NULLFILEOFF) {
3554 if (prevp->br_startblock == HOLESTARTBLOCK)
3555 prevo = prevp->br_startoff;
3556 else
3557 prevo = prevp->br_startoff + prevp->br_blockcount;
3558 } else
3559 prevo = 0;
3560 if (align_off != orig_off && align_off < prevo)
3561 align_off = prevo;
3562 /*
3563 * If the next block overlaps with this proposed allocation
3564 * then move the start back without adjusting the length,
3565 * but not before offset 0.
3566 * This may of course make the start overlap previous block,
3567 * and if we hit the offset 0 limit then the next block
3568 * can still overlap too.
3569 */
3570 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3571 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3572 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3573 nexto = gotp->br_startoff + gotp->br_blockcount;
3574 else
3575 nexto = gotp->br_startoff;
3576 } else
3577 nexto = NULLFILEOFF;
3578 if (!eof &&
3579 align_off + align_alen != orig_end &&
3580 align_off + align_alen > nexto)
3581 align_off = nexto > align_alen ? nexto - align_alen : 0;
3582 /*
3583 * If we're now overlapping the next or previous extent that
3584 * means we can't fit an extsz piece in this hole. Just move
3585 * the start forward to the first valid spot and set
3586 * the length so we hit the end.
3587 */
3588 if (align_off != orig_off && align_off < prevo)
3589 align_off = prevo;
3590 if (align_off + align_alen != orig_end &&
3591 align_off + align_alen > nexto &&
3592 nexto != NULLFILEOFF) {
3593 ASSERT(nexto > prevo);
3594 align_alen = nexto - align_off;
3595 }
3596
3597 /*
3598 * If realtime, and the result isn't a multiple of the realtime
3599 * extent size we need to remove blocks until it is.
3600 */
3601 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3602 /*
3603 * We're not covering the original request, or
3604 * we won't be able to once we fix the length.
3605 */
3606 if (orig_off < align_off ||
3607 orig_end > align_off + align_alen ||
3608 align_alen - temp < orig_alen)
3609 return XFS_ERROR(EINVAL);
3610 /*
3611 * Try to fix it by moving the start up.
3612 */
3613 if (align_off + temp <= orig_off) {
3614 align_alen -= temp;
3615 align_off += temp;
3616 }
3617 /*
3618 * Try to fix it by moving the end in.
3619 */
3620 else if (align_off + align_alen - temp >= orig_end)
3621 align_alen -= temp;
3622 /*
3623 * Set the start to the minimum then trim the length.
3624 */
3625 else {
3626 align_alen -= orig_off - align_off;
3627 align_off = orig_off;
3628 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3629 }
3630 /*
3631 * Result doesn't cover the request, fail it.
3632 */
3633 if (orig_off < align_off || orig_end > align_off + align_alen)
3634 return XFS_ERROR(EINVAL);
3635 } else {
3636 ASSERT(orig_off >= align_off);
3637 ASSERT(orig_end <= align_off + align_alen);
3638 }
3639
3640#ifdef DEBUG
3641 if (!eof && gotp->br_startoff != NULLFILEOFF)
3642 ASSERT(align_off + align_alen <= gotp->br_startoff);
3643 if (prevp->br_startoff != NULLFILEOFF)
3644 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3645#endif
3646
3647 *lenp = align_alen;
3648 *offp = align_off;
3649 return 0;
3650}
3651
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652#define XFS_ALLOC_GAP_UNITS 4
3653
David Chinnerc2b1cba2008-04-10 12:21:40 +10003654STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003655xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
3657{
3658 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3660 xfs_mount_t *mp; /* mount point structure */
3661 int nullfb; /* true if ap->firstblock isn't set */
3662 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
3664#define ISVALID(x,y) \
3665 (rt ? \
3666 (x) < mp->m_sb.sb_rblocks : \
3667 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3668 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3669 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003672 nullfb = *ap->firstblock == NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003674 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 /*
3676 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003677 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003679 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3680 !isnullstartblock(ap->prev.br_startblock) &&
3681 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3682 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003683 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 /*
3685 * Adjust for the gap between prevp and us.
3686 */
Dave Chinner3a756672011-09-18 20:40:58 +00003687 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003688 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003690 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3691 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
3693 /*
3694 * If not at eof, then compare the two neighbor blocks.
3695 * Figure out whether either one gives us a good starting point,
3696 * and pick the better one.
3697 */
3698 else if (!ap->eof) {
3699 xfs_fsblock_t gotbno; /* right side block number */
3700 xfs_fsblock_t gotdiff=0; /* right side difference */
3701 xfs_fsblock_t prevbno; /* left side block number */
3702 xfs_fsblock_t prevdiff=0; /* left side difference */
3703
3704 /*
3705 * If there's a previous (left) block, select a requested
3706 * start block based on it.
3707 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003708 if (ap->prev.br_startoff != NULLFILEOFF &&
3709 !isnullstartblock(ap->prev.br_startblock) &&
3710 (prevbno = ap->prev.br_startblock +
3711 ap->prev.br_blockcount) &&
3712 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 /*
3714 * Calculate gap to end of previous block.
3715 */
Dave Chinner3a756672011-09-18 20:40:58 +00003716 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003717 (ap->prev.br_startoff +
3718 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 /*
3720 * Figure the startblock based on the previous block's
3721 * end and the gap size.
3722 * Heuristic!
3723 * If the gap is large relative to the piece we're
3724 * allocating, or using it gives us an invalid block
3725 * number, then just use the end of the previous block.
3726 */
Dave Chinner3a756672011-09-18 20:40:58 +00003727 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003729 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 prevbno += adjust;
3731 else
3732 prevdiff += adjust;
3733 /*
3734 * If the firstblock forbids it, can't use it,
3735 * must use default.
3736 */
3737 if (!rt && !nullfb &&
3738 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3739 prevbno = NULLFSBLOCK;
3740 }
3741 /*
3742 * No previous block or can't follow it, just default.
3743 */
3744 else
3745 prevbno = NULLFSBLOCK;
3746 /*
3747 * If there's a following (right) block, select a requested
3748 * start block based on it.
3749 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003750 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 /*
3752 * Calculate gap to start of next block.
3753 */
Dave Chinner3a756672011-09-18 20:40:58 +00003754 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 /*
3756 * Figure the startblock based on the next block's
3757 * start and the gap size.
3758 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003759 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 /*
3761 * Heuristic!
3762 * If the gap is large relative to the piece we're
3763 * allocating, or using it gives us an invalid block
3764 * number, then just use the start of the next block
3765 * offset by our length.
3766 */
Dave Chinner3a756672011-09-18 20:40:58 +00003767 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 ISVALID(gotbno - gotdiff, gotbno))
3769 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003770 else if (ISVALID(gotbno - ap->length, gotbno)) {
3771 gotbno -= ap->length;
3772 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 } else
3774 gotdiff += adjust;
3775 /*
3776 * If the firstblock forbids it, can't use it,
3777 * must use default.
3778 */
3779 if (!rt && !nullfb &&
3780 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3781 gotbno = NULLFSBLOCK;
3782 }
3783 /*
3784 * No next block, just default.
3785 */
3786 else
3787 gotbno = NULLFSBLOCK;
3788 /*
3789 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003790 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 */
3792 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003793 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003795 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003797 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003799#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003800}
3801
3802STATIC int
3803xfs_bmap_rtalloc(
3804 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
3805{
3806 xfs_alloctype_t atype = 0; /* type for allocation routines */
3807 int error; /* error return value */
3808 xfs_mount_t *mp; /* mount point structure */
3809 xfs_extlen_t prod = 0; /* product factor for allocators */
3810 xfs_extlen_t ralen = 0; /* realtime allocation length */
3811 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003812 xfs_rtblock_t rtb;
3813
3814 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10003815 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003816 prod = align / mp->m_sb.sb_rextsize;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003817 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003818 align, 1, ap->eof, 0,
Dave Chinner3a756672011-09-18 20:40:58 +00003819 ap->conv, &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003820 if (error)
3821 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00003822 ASSERT(ap->length);
3823 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003824
3825 /*
3826 * If the offset & length are not perfectly aligned
3827 * then kill prod, it will just get us in trouble.
3828 */
Dave Chinner3a756672011-09-18 20:40:58 +00003829 if (do_mod(ap->offset, align) || ap->length % align)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003830 prod = 1;
3831 /*
3832 * Set ralen to be the actual requested length in rtextents.
3833 */
Dave Chinner3a756672011-09-18 20:40:58 +00003834 ralen = ap->length / mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003835 /*
3836 * If the old value was close enough to MAXEXTLEN that
3837 * we rounded up to it, cut it back so it's valid again.
3838 * Note that if it's a really large request (bigger than
3839 * MAXEXTLEN), we don't hear about that number, and can't
3840 * adjust the starting point to match it.
3841 */
3842 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
3843 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
Christoph Hellwig04e99452011-01-25 09:06:19 +00003844
3845 /*
3846 * Lock out other modifications to the RT bitmap inode.
3847 */
Christoph Hellwig1050c712011-02-13 13:25:31 +00003848 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00003849 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwig04e99452011-01-25 09:06:19 +00003850
Nathan Scotta365bdd2006-03-14 13:34:16 +11003851 /*
3852 * If it's an allocation to an empty file at offset 0,
3853 * pick an extent that will space things out in the rt area.
3854 */
Dave Chinner3a756672011-09-18 20:40:58 +00003855 if (ap->eof && ap->offset == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10003856 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
3857
Nathan Scotta365bdd2006-03-14 13:34:16 +11003858 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
3859 if (error)
3860 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00003861 ap->blkno = rtx * mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003862 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003863 ap->blkno = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003864 }
3865
3866 xfs_bmap_adjacent(ap);
3867
3868 /*
3869 * Realtime allocation, done through xfs_rtallocate_extent.
3870 */
Dave Chinner3a756672011-09-18 20:40:58 +00003871 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
3872 do_div(ap->blkno, mp->m_sb.sb_rextsize);
3873 rtb = ap->blkno;
3874 ap->length = ralen;
3875 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003876 &ralen, atype, ap->wasdel, prod, &rtb)))
3877 return error;
3878 if (rtb == NULLFSBLOCK && prod > 1 &&
Dave Chinner3a756672011-09-18 20:40:58 +00003879 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
3880 ap->length, &ralen, atype,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003881 ap->wasdel, 1, &rtb)))
3882 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00003883 ap->blkno = rtb;
3884 if (ap->blkno != NULLFSBLOCK) {
3885 ap->blkno *= mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003886 ralen *= mp->m_sb.sb_rextsize;
Dave Chinner3a756672011-09-18 20:40:58 +00003887 ap->length = ralen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003888 ap->ip->i_d.di_nblocks += ralen;
3889 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3890 if (ap->wasdel)
3891 ap->ip->i_delayed_blks -= ralen;
3892 /*
3893 * Adjust the disk quota also. This was reserved
3894 * earlier.
3895 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003896 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003897 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
3898 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
3899 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003900 ap->length = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003901 }
3902 return 0;
3903}
3904
3905STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003906xfs_bmap_btalloc_nullfb(
3907 struct xfs_bmalloca *ap,
3908 struct xfs_alloc_arg *args,
3909 xfs_extlen_t *blen)
3910{
3911 struct xfs_mount *mp = ap->ip->i_mount;
3912 struct xfs_perag *pag;
3913 xfs_agnumber_t ag, startag;
3914 int notinit = 0;
3915 int error;
3916
3917 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3918 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3919 else
3920 args->type = XFS_ALLOCTYPE_START_BNO;
3921 args->total = ap->total;
3922
3923 /*
3924 * Search for an allocation group with a single extent large enough
3925 * for the request. If one isn't found, then adjust the minimum
3926 * allocation size to the largest space found.
3927 */
3928 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3929 if (startag == NULLAGNUMBER)
3930 startag = ag = 0;
3931
3932 pag = xfs_perag_get(mp, ag);
Dave Chinner14b064c2011-01-27 12:16:28 +11003933 while (*blen < args->maxlen) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00003934 if (!pag->pagf_init) {
3935 error = xfs_alloc_pagf_init(mp, args->tp, ag,
3936 XFS_ALLOC_FLAG_TRYLOCK);
3937 if (error) {
3938 xfs_perag_put(pag);
3939 return error;
3940 }
3941 }
3942
3943 /*
3944 * See xfs_alloc_fix_freelist...
3945 */
3946 if (pag->pagf_init) {
3947 xfs_extlen_t longest;
3948 longest = xfs_alloc_longest_free_extent(mp, pag);
3949 if (*blen < longest)
3950 *blen = longest;
3951 } else
3952 notinit = 1;
3953
3954 if (xfs_inode_is_filestream(ap->ip)) {
Dave Chinner14b064c2011-01-27 12:16:28 +11003955 if (*blen >= args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00003956 break;
3957
3958 if (ap->userdata) {
3959 /*
3960 * If startag is an invalid AG, we've
3961 * come here once before and
3962 * xfs_filestream_new_ag picked the
3963 * best currently available.
3964 *
3965 * Don't continue looping, since we
3966 * could loop forever.
3967 */
3968 if (startag == NULLAGNUMBER)
3969 break;
3970
3971 error = xfs_filestream_new_ag(ap, &ag);
3972 xfs_perag_put(pag);
3973 if (error)
3974 return error;
3975
3976 /* loop again to set 'blen'*/
3977 startag = NULLAGNUMBER;
3978 pag = xfs_perag_get(mp, ag);
3979 continue;
3980 }
3981 }
3982 if (++ag == mp->m_sb.sb_agcount)
3983 ag = 0;
3984 if (ag == startag)
3985 break;
3986 xfs_perag_put(pag);
3987 pag = xfs_perag_get(mp, ag);
3988 }
3989 xfs_perag_put(pag);
3990
3991 /*
3992 * Since the above loop did a BUF_TRYLOCK, it is
3993 * possible that there is space for this request.
3994 */
3995 if (notinit || *blen < ap->minlen)
3996 args->minlen = ap->minlen;
3997 /*
3998 * If the best seen length is less than the request
3999 * length, use the best as the minimum.
4000 */
Dave Chinner14b064c2011-01-27 12:16:28 +11004001 else if (*blen < args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00004002 args->minlen = *blen;
4003 /*
Dave Chinner14b064c2011-01-27 12:16:28 +11004004 * Otherwise we've seen an extent as big as maxlen,
Christoph Hellwigc467c042010-02-15 23:34:42 +00004005 * use that as the minimum.
4006 */
4007 else
Dave Chinner14b064c2011-01-27 12:16:28 +11004008 args->minlen = args->maxlen;
Christoph Hellwigc467c042010-02-15 23:34:42 +00004009
4010 /*
4011 * set the failure fallback case to look in the selected
4012 * AG as the stream may have moved.
4013 */
4014 if (xfs_inode_is_filestream(ap->ip))
Dave Chinner3a756672011-09-18 20:40:58 +00004015 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00004016
4017 return 0;
4018}
4019
4020STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11004021xfs_bmap_btalloc(
4022 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
4023{
4024 xfs_mount_t *mp; /* mount point structure */
4025 xfs_alloctype_t atype = 0; /* type for allocation routines */
4026 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11004027 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00004028 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004029 xfs_alloc_arg_t args;
4030 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004031 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004032 int nullfb; /* true if ap->firstblock isn't set */
4033 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004034 int tryagain;
4035 int error;
4036
Dave Chinnera99ebf42011-12-01 11:24:20 +00004037 ASSERT(ap->length);
4038
Nathan Scotta365bdd2006-03-14 13:34:16 +11004039 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10004040 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004041 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004042 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11004043 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00004044 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11004045 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00004046 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11004047 }
Dave Chinner0937e0f2011-09-18 20:40:57 +00004048 nullfb = *ap->firstblock == NULLFSBLOCK;
4049 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10004050 if (nullfb) {
4051 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
4052 ag = xfs_filestream_lookup_ag(ap->ip);
4053 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00004054 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10004055 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00004056 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10004057 }
4058 } else
Dave Chinner3a756672011-09-18 20:40:58 +00004059 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004060
4061 xfs_bmap_adjacent(ap);
4062
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 /*
Dave Chinner3a756672011-09-18 20:40:58 +00004064 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 * it's in the right allocation group.
4066 */
Dave Chinner3a756672011-09-18 20:40:58 +00004067 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 ;
4069 else
Dave Chinner3a756672011-09-18 20:40:58 +00004070 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 * Normal allocation, done through xfs_alloc_vextent.
4073 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11004074 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05004075 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11004076 args.tp = ap->tp;
4077 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00004078 args.fsbno = ap->blkno;
Dave Chinner14b064c2011-01-27 12:16:28 +11004079
4080 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner3a756672011-09-18 20:40:58 +00004081 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
Dave Chinner0937e0f2011-09-18 20:40:57 +00004082 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004083 blen = 0;
4084 if (nullfb) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00004085 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
4086 if (error)
4087 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004088 } else if (ap->flist->xbf_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10004089 if (xfs_inode_is_filestream(ap->ip))
4090 args.type = XFS_ALLOCTYPE_FIRST_AG;
4091 else
4092 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004093 args.total = args.minlen = ap->minlen;
4094 } else {
4095 args.type = XFS_ALLOCTYPE_NEAR_BNO;
4096 args.total = ap->total;
4097 args.minlen = ap->minlen;
4098 }
David Chinner957d0eb2007-06-18 16:50:37 +10004099 /* apply extent size hints if obtained earlier */
4100 if (unlikely(align)) {
4101 args.prod = align;
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);
Tim Shimmine6a4b372007-11-23 16:30:42 +11004104 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11004105 args.prod = 1;
4106 args.mod = 0;
4107 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11004108 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00004109 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11004110 args.mod = (xfs_extlen_t)(args.prod - args.mod);
4111 }
4112 /*
4113 * If we are not low on available data blocks, and the
4114 * underlying logical volume manager is a stripe, and
4115 * the file offset is zero then try to allocate data
4116 * blocks on stripe unit boundary.
4117 * NOTE: ap->aeof is only set if the allocation length
4118 * is >= the stripe unit and the allocation offset is
4119 * at the end of file.
4120 */
Dave Chinner0937e0f2011-09-18 20:40:57 +00004121 if (!ap->flist->xbf_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00004122 if (!ap->offset) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11004123 args.alignment = mp->m_dalign;
4124 atype = args.type;
4125 isaligned = 1;
4126 /*
4127 * Adjust for alignment
4128 */
Dave Chinner14b064c2011-01-27 12:16:28 +11004129 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11004130 args.minlen = blen - args.alignment;
4131 args.minalignslop = 0;
4132 } else {
4133 /*
4134 * First try an exact bno allocation.
4135 * If it fails then do a near or start bno
4136 * allocation with alignment turned on.
4137 */
4138 atype = args.type;
4139 tryagain = 1;
4140 args.type = XFS_ALLOCTYPE_THIS_BNO;
4141 args.alignment = 1;
4142 /*
4143 * Compute the minlen+alignment for the
4144 * next case. Set slop so that the value
4145 * of minlen+alignment+slop doesn't go up
4146 * between the calls.
4147 */
Dave Chinner14b064c2011-01-27 12:16:28 +11004148 if (blen > mp->m_dalign && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11004149 nextminlen = blen - mp->m_dalign;
4150 else
4151 nextminlen = args.minlen;
4152 if (nextminlen + mp->m_dalign > args.minlen + 1)
4153 args.minalignslop =
4154 nextminlen + mp->m_dalign -
4155 args.minlen - 1;
4156 else
4157 args.minalignslop = 0;
4158 }
4159 } else {
4160 args.alignment = 1;
4161 args.minalignslop = 0;
4162 }
4163 args.minleft = ap->minleft;
4164 args.wasdel = ap->wasdel;
4165 args.isfl = 0;
4166 args.userdata = ap->userdata;
4167 if ((error = xfs_alloc_vextent(&args)))
4168 return error;
4169 if (tryagain && args.fsbno == NULLFSBLOCK) {
4170 /*
4171 * Exact allocation failed. Now try with alignment
4172 * turned on.
4173 */
4174 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00004175 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004176 args.alignment = mp->m_dalign;
4177 args.minlen = nextminlen;
4178 args.minalignslop = 0;
4179 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 if ((error = xfs_alloc_vextent(&args)))
4181 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004182 }
4183 if (isaligned && args.fsbno == NULLFSBLOCK) {
4184 /*
4185 * allocation failed, so turn off alignment and
4186 * try again.
4187 */
4188 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00004189 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004190 args.alignment = 0;
4191 if ((error = xfs_alloc_vextent(&args)))
4192 return error;
4193 }
4194 if (args.fsbno == NULLFSBLOCK && nullfb &&
4195 args.minlen > ap->minlen) {
4196 args.minlen = ap->minlen;
4197 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00004198 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004199 if ((error = xfs_alloc_vextent(&args)))
4200 return error;
4201 }
4202 if (args.fsbno == NULLFSBLOCK && nullfb) {
4203 args.fsbno = 0;
4204 args.type = XFS_ALLOCTYPE_FIRST_AG;
4205 args.total = ap->minlen;
4206 args.minleft = 0;
4207 if ((error = xfs_alloc_vextent(&args)))
4208 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004209 ap->flist->xbf_low = 1;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004210 }
4211 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004212 /*
4213 * check the allocation happened at the same or higher AG than
4214 * the first block that was allocated.
4215 */
4216 ASSERT(*ap->firstblock == NULLFSBLOCK ||
4217 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
4218 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
4219 (ap->flist->xbf_low &&
4220 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
4221 XFS_FSB_TO_AGNO(mp, args.fsbno)));
4222
Dave Chinner3a756672011-09-18 20:40:58 +00004223 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004224 if (*ap->firstblock == NULLFSBLOCK)
4225 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004226 ASSERT(nullfb || fb_agno == args.agno ||
Dave Chinner0937e0f2011-09-18 20:40:57 +00004227 (ap->flist->xbf_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00004228 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004229 ap->ip->i_d.di_nblocks += args.len;
4230 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
4231 if (ap->wasdel)
4232 ap->ip->i_delayed_blks -= args.len;
4233 /*
4234 * Adjust the disk quota also. This was reserved
4235 * earlier.
4236 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02004237 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11004238 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
4239 XFS_TRANS_DQ_BCOUNT,
4240 (long) args.len);
4241 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00004242 ap->blkno = NULLFSBLOCK;
4243 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 }
4245 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004246}
4247
4248/*
4249 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
4250 * It figures out where to ask the underlying allocator to put the new extent.
4251 */
4252STATIC int
4253xfs_bmap_alloc(
4254 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
4255{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11004256 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11004257 return xfs_bmap_rtalloc(ap);
4258 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259}
4260
4261/*
Dave Chinneraef9a892011-09-18 20:40:44 +00004262 * Trim the returned map to the required bounds
4263 */
4264STATIC void
4265xfs_bmapi_trim_map(
4266 struct xfs_bmbt_irec *mval,
4267 struct xfs_bmbt_irec *got,
4268 xfs_fileoff_t *bno,
4269 xfs_filblks_t len,
4270 xfs_fileoff_t obno,
4271 xfs_fileoff_t end,
4272 int n,
4273 int flags)
4274{
4275 if ((flags & XFS_BMAPI_ENTIRE) ||
4276 got->br_startoff + got->br_blockcount <= obno) {
4277 *mval = *got;
4278 if (isnullstartblock(got->br_startblock))
4279 mval->br_startblock = DELAYSTARTBLOCK;
4280 return;
4281 }
4282
4283 if (obno > *bno)
4284 *bno = obno;
4285 ASSERT((*bno >= obno) || (n == 0));
4286 ASSERT(*bno < end);
4287 mval->br_startoff = *bno;
4288 if (isnullstartblock(got->br_startblock))
4289 mval->br_startblock = DELAYSTARTBLOCK;
4290 else
4291 mval->br_startblock = got->br_startblock +
4292 (*bno - got->br_startoff);
4293 /*
4294 * Return the minimum of what we got and what we asked for for
4295 * the length. We can use the len variable here because it is
4296 * modified below and we could have been there before coming
4297 * here if the first part of the allocation didn't overlap what
4298 * was asked for.
4299 */
4300 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4301 got->br_blockcount - (*bno - got->br_startoff));
4302 mval->br_state = got->br_state;
4303 ASSERT(mval->br_blockcount <= len);
4304 return;
4305}
4306
4307/*
4308 * Update and validate the extent map to return
4309 */
4310STATIC void
4311xfs_bmapi_update_map(
4312 struct xfs_bmbt_irec **map,
4313 xfs_fileoff_t *bno,
4314 xfs_filblks_t *len,
4315 xfs_fileoff_t obno,
4316 xfs_fileoff_t end,
4317 int *n,
4318 int flags)
4319{
4320 xfs_bmbt_irec_t *mval = *map;
4321
4322 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4323 ((mval->br_startoff + mval->br_blockcount) <= end));
4324 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4325 (mval->br_startoff < obno));
4326
4327 *bno = mval->br_startoff + mval->br_blockcount;
4328 *len = end - *bno;
4329 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4330 /* update previous map with new information */
4331 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4332 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4333 ASSERT(mval->br_state == mval[-1].br_state);
4334 mval[-1].br_blockcount = mval->br_blockcount;
4335 mval[-1].br_state = mval->br_state;
4336 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4337 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4338 mval[-1].br_startblock != HOLESTARTBLOCK &&
4339 mval->br_startblock == mval[-1].br_startblock +
4340 mval[-1].br_blockcount &&
4341 ((flags & XFS_BMAPI_IGSTATE) ||
4342 mval[-1].br_state == mval->br_state)) {
4343 ASSERT(mval->br_startoff ==
4344 mval[-1].br_startoff + mval[-1].br_blockcount);
4345 mval[-1].br_blockcount += mval->br_blockcount;
4346 } else if (*n > 0 &&
4347 mval->br_startblock == DELAYSTARTBLOCK &&
4348 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4349 mval->br_startoff ==
4350 mval[-1].br_startoff + mval[-1].br_blockcount) {
4351 mval[-1].br_blockcount += mval->br_blockcount;
4352 mval[-1].br_state = mval->br_state;
4353 } else if (!((*n == 0) &&
4354 ((mval->br_startoff + mval->br_blockcount) <=
4355 obno))) {
4356 mval++;
4357 (*n)++;
4358 }
4359 *map = mval;
4360}
4361
4362/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004363 * Map file blocks to filesystem blocks without allocation.
4364 */
4365int
4366xfs_bmapi_read(
4367 struct xfs_inode *ip,
4368 xfs_fileoff_t bno,
4369 xfs_filblks_t len,
4370 struct xfs_bmbt_irec *mval,
4371 int *nmap,
4372 int flags)
4373{
4374 struct xfs_mount *mp = ip->i_mount;
4375 struct xfs_ifork *ifp;
4376 struct xfs_bmbt_irec got;
4377 struct xfs_bmbt_irec prev;
4378 xfs_fileoff_t obno;
4379 xfs_fileoff_t end;
4380 xfs_extnum_t lastx;
4381 int error;
4382 int eof;
4383 int n = 0;
4384 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4385 XFS_ATTR_FORK : XFS_DATA_FORK;
4386
4387 ASSERT(*nmap >= 1);
4388 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4389 XFS_BMAPI_IGSTATE)));
4390
4391 if (unlikely(XFS_TEST_ERROR(
4392 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4393 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4394 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4395 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4396 return XFS_ERROR(EFSCORRUPTED);
4397 }
4398
4399 if (XFS_FORCED_SHUTDOWN(mp))
4400 return XFS_ERROR(EIO);
4401
4402 XFS_STATS_INC(xs_blk_mapr);
4403
4404 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004405
4406 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4407 error = xfs_iread_extents(NULL, ip, whichfork);
4408 if (error)
4409 return error;
4410 }
4411
4412 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4413 end = bno + len;
4414 obno = bno;
4415
4416 while (bno < end && n < *nmap) {
4417 /* Reading past eof, act as though there's a hole up to end. */
4418 if (eof)
4419 got.br_startoff = end;
4420 if (got.br_startoff > bno) {
4421 /* Reading in a hole. */
4422 mval->br_startoff = bno;
4423 mval->br_startblock = HOLESTARTBLOCK;
4424 mval->br_blockcount =
4425 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4426 mval->br_state = XFS_EXT_NORM;
4427 bno += mval->br_blockcount;
4428 len -= mval->br_blockcount;
4429 mval++;
4430 n++;
4431 continue;
4432 }
4433
4434 /* set up the extent map to return. */
4435 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4436 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4437
4438 /* If we're done, stop now. */
4439 if (bno >= end || n >= *nmap)
4440 break;
4441
4442 /* Else go on to the next record. */
4443 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4444 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4445 else
4446 eof = 1;
4447 }
4448 *nmap = n;
4449 return 0;
4450}
4451
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004452STATIC int
4453xfs_bmapi_reserve_delalloc(
4454 struct xfs_inode *ip,
4455 xfs_fileoff_t aoff,
4456 xfs_filblks_t len,
4457 struct xfs_bmbt_irec *got,
4458 struct xfs_bmbt_irec *prev,
4459 xfs_extnum_t *lastx,
4460 int eof)
4461{
4462 struct xfs_mount *mp = ip->i_mount;
4463 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4464 xfs_extlen_t alen;
4465 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004466 char rt = XFS_IS_REALTIME_INODE(ip);
4467 xfs_extlen_t extsz;
4468 int error;
4469
4470 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4471 if (!eof)
4472 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4473
4474 /* Figure out the extent size, adjust alen */
4475 extsz = xfs_get_extsz_hint(ip);
4476 if (extsz) {
4477 /*
4478 * Make sure we don't exceed a single extent length when we
4479 * align the extent by reducing length we are going to
4480 * allocate by the maximum amount extent size aligment may
4481 * require.
4482 */
4483 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4484 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4485 1, 0, &aoff, &alen);
4486 ASSERT(!error);
4487 }
4488
4489 if (rt)
4490 extsz = alen / mp->m_sb.sb_rextsize;
4491
4492 /*
4493 * Make a transaction-less quota reservation for delayed allocation
4494 * blocks. This number gets adjusted later. We return if we haven't
4495 * allocated blocks already inside this loop.
4496 */
4497 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4498 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4499 if (error)
4500 return error;
4501
4502 /*
4503 * Split changing sb for alen and indlen since they could be coming
4504 * from different places.
4505 */
4506 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4507 ASSERT(indlen > 0);
4508
4509 if (rt) {
4510 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4511 -((int64_t)extsz), 0);
4512 } else {
4513 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4514 -((int64_t)alen), 0);
4515 }
4516
4517 if (error)
4518 goto out_unreserve_quota;
4519
4520 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4521 -((int64_t)indlen), 0);
4522 if (error)
4523 goto out_unreserve_blocks;
4524
4525
4526 ip->i_delayed_blks += alen;
4527
4528 got->br_startoff = aoff;
4529 got->br_startblock = nullstartblock(indlen);
4530 got->br_blockcount = alen;
4531 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004532 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004533
4534 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004535 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4536 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004537 */
4538 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4539
4540 ASSERT(got->br_startoff <= aoff);
4541 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4542 ASSERT(isnullstartblock(got->br_startblock));
4543 ASSERT(got->br_state == XFS_EXT_NORM);
4544 return 0;
4545
4546out_unreserve_blocks:
4547 if (rt)
4548 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4549 else
4550 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4551out_unreserve_quota:
4552 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004553 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004554 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4555 return error;
4556}
4557
Dave Chinner5c8ed202011-09-18 20:40:45 +00004558/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004559 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4560 */
4561int
4562xfs_bmapi_delay(
4563 struct xfs_inode *ip, /* incore inode */
4564 xfs_fileoff_t bno, /* starting file offs. mapped */
4565 xfs_filblks_t len, /* length to map in file */
4566 struct xfs_bmbt_irec *mval, /* output: map values */
4567 int *nmap, /* i/o: mval size/count */
4568 int flags) /* XFS_BMAPI_... */
4569{
4570 struct xfs_mount *mp = ip->i_mount;
4571 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4572 struct xfs_bmbt_irec got; /* current file extent record */
4573 struct xfs_bmbt_irec prev; /* previous file extent record */
4574 xfs_fileoff_t obno; /* old block number (offset) */
4575 xfs_fileoff_t end; /* end of mapped file region */
4576 xfs_extnum_t lastx; /* last useful extent number */
4577 int eof; /* we've hit the end of extents */
4578 int n = 0; /* current extent index */
4579 int error = 0;
4580
4581 ASSERT(*nmap >= 1);
4582 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4583 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4584
4585 if (unlikely(XFS_TEST_ERROR(
4586 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4587 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4588 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4589 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4590 return XFS_ERROR(EFSCORRUPTED);
4591 }
4592
4593 if (XFS_FORCED_SHUTDOWN(mp))
4594 return XFS_ERROR(EIO);
4595
4596 XFS_STATS_INC(xs_blk_mapw);
4597
4598 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4599 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4600 if (error)
4601 return error;
4602 }
4603
4604 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4605 end = bno + len;
4606 obno = bno;
4607
4608 while (bno < end && n < *nmap) {
4609 if (eof || got.br_startoff > bno) {
4610 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4611 &prev, &lastx, eof);
4612 if (error) {
4613 if (n == 0) {
4614 *nmap = 0;
4615 return error;
4616 }
4617 break;
4618 }
4619 }
4620
4621 /* set up the extent map to return. */
4622 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4623 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4624
4625 /* If we're done, stop now. */
4626 if (bno >= end || n >= *nmap)
4627 break;
4628
4629 /* Else go on to the next record. */
4630 prev = got;
4631 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4632 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4633 else
4634 eof = 1;
4635 }
4636
4637 *nmap = n;
4638 return 0;
4639}
4640
4641
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004642STATIC int
Dave Chinnere04426b2012-10-05 11:06:59 +10004643__xfs_bmapi_allocate(
4644 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004645{
4646 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004647 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004648 XFS_ATTR_FORK : XFS_DATA_FORK;
4649 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004650 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004651 int error;
4652 int rt;
4653
Dave Chinnera99ebf42011-12-01 11:24:20 +00004654 ASSERT(bma->length > 0);
4655
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004656 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip);
4657
4658 /*
4659 * For the wasdelay case, we could also just allocate the stuff asked
4660 * for in this bmap call but that wouldn't be as good.
4661 */
4662 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004663 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4664 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004665 if (bma->idx != NULLEXTNUM && bma->idx) {
4666 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004667 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004668 }
4669 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004670 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004671 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004672 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004673 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004674 }
4675
4676 /*
4677 * Indicate if this is the first user data in the file, or just any
4678 * user data.
4679 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004680 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004681 bma->userdata = (bma->offset == 0) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004682 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4683 }
4684
Dave Chinnere04426b2012-10-05 11:06:59 +10004685 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004686
4687 /*
4688 * Only want to do the alignment at the eof if it is userdata and
4689 * allocation length is larger than a stripe unit.
4690 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004691 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004692 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004693 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004694 if (error)
4695 return error;
4696 }
4697
4698 error = xfs_bmap_alloc(bma);
4699 if (error)
4700 return error;
4701
Dave Chinner0937e0f2011-09-18 20:40:57 +00004702 if (bma->flist->xbf_low)
4703 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004704 if (bma->cur)
4705 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004706 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004707 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004708 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4709 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4710 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4711 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004712 }
4713 /*
4714 * Bump the number of extents we've allocated
4715 * in this call.
4716 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004717 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004718
Dave Chinner29c8d172011-09-18 20:41:00 +00004719 if (bma->cur)
4720 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004721 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4722
Dave Chinner963c30c2011-09-18 20:40:59 +00004723 bma->got.br_startoff = bma->offset;
4724 bma->got.br_startblock = bma->blkno;
4725 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004726 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004727
4728 /*
4729 * A wasdelay extent has been initialized, so shouldn't be flagged
4730 * as unwritten.
4731 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004732 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004733 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004734 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004735
Christoph Hellwigc6534242011-09-18 20:41:05 +00004736 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004737 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004738 else
4739 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004740
Christoph Hellwigc315c902011-09-18 20:41:02 +00004741 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004742 if (error)
4743 return error;
4744
4745 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004746 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4747 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4748 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004749 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004750 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004751
Dave Chinner963c30c2011-09-18 20:40:59 +00004752 ASSERT(bma->got.br_startoff <= bma->offset);
4753 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4754 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004755 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4756 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004757 return 0;
4758}
4759
Dave Chinnere04426b2012-10-05 11:06:59 +10004760static void
4761xfs_bmapi_allocate_worker(
4762 struct work_struct *work)
4763{
4764 struct xfs_bmalloca *args = container_of(work,
4765 struct xfs_bmalloca, work);
4766 unsigned long pflags;
4767
4768 /* we are in a transaction context here */
4769 current_set_flags_nested(&pflags, PF_FSTRANS);
4770
4771 args->result = __xfs_bmapi_allocate(args);
4772 complete(args->done);
4773
4774 current_restore_flags_nested(&pflags, PF_FSTRANS);
4775}
4776
4777/*
4778 * Some allocation requests often come in with little stack to work on. Push
4779 * them off to a worker thread so there is lots of stack to use. Otherwise just
4780 * call directly to avoid the context switch overhead here.
4781 */
4782int
4783xfs_bmapi_allocate(
4784 struct xfs_bmalloca *args)
4785{
4786 DECLARE_COMPLETION_ONSTACK(done);
4787
4788 if (!args->stack_switch)
4789 return __xfs_bmapi_allocate(args);
4790
4791
4792 args->done = &done;
4793 INIT_WORK_ONSTACK(&args->work, xfs_bmapi_allocate_worker);
4794 queue_work(xfs_alloc_wq, &args->work);
4795 wait_for_completion(&done);
4796 return args->result;
4797}
4798
Dave Chinnerb447fe52011-09-18 20:40:51 +00004799STATIC int
4800xfs_bmapi_convert_unwritten(
4801 struct xfs_bmalloca *bma,
4802 struct xfs_bmbt_irec *mval,
4803 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004804 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004805{
4806 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4807 XFS_ATTR_FORK : XFS_DATA_FORK;
4808 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004809 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004810 int error;
4811
Dave Chinnerb447fe52011-09-18 20:40:51 +00004812 /* check if we need to do unwritten->real conversion */
4813 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4814 (flags & XFS_BMAPI_PREALLOC))
4815 return 0;
4816
4817 /* check if we need to do real->unwritten conversion */
4818 if (mval->br_state == XFS_EXT_NORM &&
4819 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4820 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4821 return 0;
4822
4823 /*
4824 * Modify (by adding) the state flag, if writing.
4825 */
4826 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004827 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4828 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004829 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004830 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4831 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004832 }
4833 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4834 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4835
Dave Chinnere0c3da52011-09-18 20:41:01 +00004836 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004837 &bma->cur, mval, bma->firstblock, bma->flist,
4838 &tmp_logflags);
4839 bma->logflags |= tmp_logflags;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004840 if (error)
4841 return error;
4842
4843 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004844 * Update our extent pointer, given that
4845 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4846 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004847 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004848 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004849
4850 /*
4851 * We may have combined previously unwritten space with written space,
4852 * so generate another request.
4853 */
4854 if (mval->br_blockcount < len)
4855 return EAGAIN;
4856 return 0;
4857}
4858
Christoph Hellwig44032802011-09-18 20:40:48 +00004859/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004860 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4861 * extent state if necessary. Details behaviour is controlled by the flags
4862 * parameter. Only allocates blocks from a single allocation group, to avoid
4863 * locking problems.
4864 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 * The returned value in "firstblock" from the first call in a transaction
4866 * must be remembered and presented to subsequent calls in "firstblock".
4867 * An upper bound for the number of blocks to be allocated is supplied to
4868 * the first call in "total"; if no allocation group has that many free
4869 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4870 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004871int
4872xfs_bmapi_write(
4873 struct xfs_trans *tp, /* transaction pointer */
4874 struct xfs_inode *ip, /* incore inode */
4875 xfs_fileoff_t bno, /* starting file offs. mapped */
4876 xfs_filblks_t len, /* length to map in file */
4877 int flags, /* XFS_BMAPI_... */
4878 xfs_fsblock_t *firstblock, /* first allocated block
4879 controls a.g. for allocs */
4880 xfs_extlen_t total, /* total blocks needed */
4881 struct xfs_bmbt_irec *mval, /* output: map values */
4882 int *nmap, /* i/o: mval size/count */
4883 struct xfs_bmap_free *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004885 struct xfs_mount *mp = ip->i_mount;
4886 struct xfs_ifork *ifp;
4887 struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004888 xfs_fileoff_t end; /* end of mapped file region */
4889 int eof; /* after the end of extents */
4890 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004891 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004892 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004893 int whichfork; /* data or attr fork */
4894 char inhole; /* current location is hole in file */
4895 char wasdelay; /* old extent was delayed */
4896
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004898 xfs_fileoff_t orig_bno; /* original block number value */
4899 int orig_flags; /* original flags arg value */
4900 xfs_filblks_t orig_len; /* original value of len arg */
4901 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4902 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903
4904 orig_bno = bno;
4905 orig_len = len;
4906 orig_flags = flags;
4907 orig_mval = mval;
4908 orig_nmap = *nmap;
4909#endif
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004910 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 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004914 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4915 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4916 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004917 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004918 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004919
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 if (unlikely(XFS_TEST_ERROR(
4921 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004922 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004924 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 return XFS_ERROR(EFSCORRUPTED);
4926 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004927
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 if (XFS_FORCED_SHUTDOWN(mp))
4929 return XFS_ERROR(EIO);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004930
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004932
4933 XFS_STATS_INC(xs_blk_mapw);
4934
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004935 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004937 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004939 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004940 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004941 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004942 }
4943
4944 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4945 error = xfs_iread_extents(tp, ip, whichfork);
4946 if (error)
4947 goto error0;
4948 }
4949
Dave Chinnere0c3da52011-09-18 20:41:01 +00004950 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004951 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 n = 0;
4953 end = bno + len;
4954 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004955
4956 bma.tp = tp;
4957 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004958 bma.total = total;
4959 bma.userdata = 0;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004960 bma.flist = flist;
4961 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004962
Brian Foster9e96fe62013-01-17 13:11:29 -05004963 if (flags & XFS_BMAPI_STACK_SWITCH)
4964 bma.stack_switch = 1;
4965
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004967 inhole = eof || bma.got.br_startoff > bno;
4968 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004969
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 /*
4971 * First, deal with the hole before the allocated space
4972 * that we found, if any.
4973 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004974 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004975 bma.eof = eof;
4976 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4977 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004978 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004979 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004980
Dave Chinnera99ebf42011-12-01 11:24:20 +00004981 /*
4982 * There's a 32/64 bit type mismatch between the
4983 * allocation length request (which can be 64 bits in
4984 * length) and the bma length request, which is
4985 * xfs_extlen_t and therefore 32 bits. Hence we have to
4986 * check for 32-bit overflows and handle them here.
4987 */
4988 if (len > (xfs_filblks_t)MAXEXTLEN)
4989 bma.length = MAXEXTLEN;
4990 else
4991 bma.length = len;
4992
4993 ASSERT(len > 0);
4994 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004995 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996 if (error)
4997 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004998 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004999 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 }
Christoph Hellwig44032802011-09-18 20:40:48 +00005001
Dave Chinneraef9a892011-09-18 20:40:44 +00005002 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00005003 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
5004 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005
Dave Chinnerb447fe52011-09-18 20:40:51 +00005006 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005007 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005008 if (error == EAGAIN)
5009 continue;
5010 if (error)
5011 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012
Dave Chinneraef9a892011-09-18 20:40:44 +00005013 /* update the extent map to return */
5014 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
5015
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 /*
5017 * If we're done, stop now. Stop when we've allocated
5018 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5019 * the transaction may get too big.
5020 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00005021 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005023
5024 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00005025 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00005026 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
5027 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
5028 &bma.got);
5029 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00005030 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005033
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 /*
5035 * Transform from btree to extents, give it cur.
5036 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005037 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00005038 int tmp_logflags = 0;
5039
Dave Chinner29c8d172011-09-18 20:41:00 +00005040 ASSERT(bma.cur);
5041 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00005043 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 if (error)
5045 goto error0;
5046 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005047
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005049 XFS_IFORK_NEXTENTS(ip, whichfork) >
5050 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052error0:
5053 /*
5054 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005055 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005057 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00005059 bma.logflags &= ~xfs_ilog_fext(whichfork);
5060 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00005062 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 /*
5064 * Log whatever the flags say, even if error. Otherwise we might miss
5065 * detecting a case where the data is changed, there's an error,
5066 * and it's not logged so we don't shutdown when we should.
5067 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005068 if (bma.logflags)
5069 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005070
Dave Chinner29c8d172011-09-18 20:41:00 +00005071 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005072 if (!error) {
5073 ASSERT(*firstblock == NULLFSBLOCK ||
5074 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5075 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00005076 bma.cur->bc_private.b.firstblock) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 (flist->xbf_low &&
5078 XFS_FSB_TO_AGNO(mp, *firstblock) <
5079 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00005080 bma.cur->bc_private.b.firstblock)));
5081 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 }
Dave Chinner29c8d172011-09-18 20:41:00 +00005083 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5085 }
5086 if (!error)
5087 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5088 orig_nmap, *nmap);
5089 return error;
5090}
5091
5092/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005093 * Called by xfs_bmapi to update file extent records and the btree
5094 * after removing space (or undoing a delayed allocation).
5095 */
5096STATIC int /* error */
5097xfs_bmap_del_extent(
5098 xfs_inode_t *ip, /* incore inode pointer */
5099 xfs_trans_t *tp, /* current transaction pointer */
5100 xfs_extnum_t *idx, /* extent number to update/delete */
5101 xfs_bmap_free_t *flist, /* list of extents to be freed */
5102 xfs_btree_cur_t *cur, /* if null, not a btree */
5103 xfs_bmbt_irec_t *del, /* data to remove from extents */
5104 int *logflagsp, /* inode logging flags */
5105 int whichfork) /* data or attr fork */
5106{
5107 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5108 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5109 xfs_fsblock_t del_endblock=0; /* first block past del */
5110 xfs_fileoff_t del_endoff; /* first offset past del */
5111 int delay; /* current block is delayed allocated */
5112 int do_fx; /* free extent at end of routine */
5113 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5114 int error; /* error return value */
5115 int flags; /* inode logging flags */
5116 xfs_bmbt_irec_t got; /* current extent entry */
5117 xfs_fileoff_t got_endoff; /* first offset past got */
5118 int i; /* temp state */
5119 xfs_ifork_t *ifp; /* inode fork pointer */
5120 xfs_mount_t *mp; /* mount structure */
5121 xfs_filblks_t nblks; /* quota/sb block count */
5122 xfs_bmbt_irec_t new; /* new record to be inserted */
5123 /* REFERENCED */
5124 uint qfield; /* quota field to update */
5125 xfs_filblks_t temp; /* for indirect length calculations */
5126 xfs_filblks_t temp2; /* for indirect length calculations */
5127 int state = 0;
5128
5129 XFS_STATS_INC(xs_del_exlist);
5130
5131 if (whichfork == XFS_ATTR_FORK)
5132 state |= BMAP_ATTRFORK;
5133
5134 mp = ip->i_mount;
5135 ifp = XFS_IFORK_PTR(ip, whichfork);
5136 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
5137 (uint)sizeof(xfs_bmbt_rec_t)));
5138 ASSERT(del->br_blockcount > 0);
5139 ep = xfs_iext_get_ext(ifp, *idx);
5140 xfs_bmbt_get_all(ep, &got);
5141 ASSERT(got.br_startoff <= del->br_startoff);
5142 del_endoff = del->br_startoff + del->br_blockcount;
5143 got_endoff = got.br_startoff + got.br_blockcount;
5144 ASSERT(got_endoff >= del_endoff);
5145 delay = isnullstartblock(got.br_startblock);
5146 ASSERT(isnullstartblock(del->br_startblock) == delay);
5147 flags = 0;
5148 qfield = 0;
5149 error = 0;
5150 /*
5151 * If deleting a real allocation, must free up the disk space.
5152 */
5153 if (!delay) {
5154 flags = XFS_ILOG_CORE;
5155 /*
5156 * Realtime allocation. Free it and record di_nblocks update.
5157 */
5158 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5159 xfs_fsblock_t bno;
5160 xfs_filblks_t len;
5161
5162 ASSERT(do_mod(del->br_blockcount,
5163 mp->m_sb.sb_rextsize) == 0);
5164 ASSERT(do_mod(del->br_startblock,
5165 mp->m_sb.sb_rextsize) == 0);
5166 bno = del->br_startblock;
5167 len = del->br_blockcount;
5168 do_div(bno, mp->m_sb.sb_rextsize);
5169 do_div(len, mp->m_sb.sb_rextsize);
5170 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5171 if (error)
5172 goto done;
5173 do_fx = 0;
5174 nblks = len * mp->m_sb.sb_rextsize;
5175 qfield = XFS_TRANS_DQ_RTBCOUNT;
5176 }
5177 /*
5178 * Ordinary allocation.
5179 */
5180 else {
5181 do_fx = 1;
5182 nblks = del->br_blockcount;
5183 qfield = XFS_TRANS_DQ_BCOUNT;
5184 }
5185 /*
5186 * Set up del_endblock and cur for later.
5187 */
5188 del_endblock = del->br_startblock + del->br_blockcount;
5189 if (cur) {
5190 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5191 got.br_startblock, got.br_blockcount,
5192 &i)))
5193 goto done;
5194 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5195 }
5196 da_old = da_new = 0;
5197 } else {
5198 da_old = startblockval(got.br_startblock);
5199 da_new = 0;
5200 nblks = 0;
5201 do_fx = 0;
5202 }
5203 /*
5204 * Set flag value to use in switch statement.
5205 * Left-contig is 2, right-contig is 1.
5206 */
5207 switch (((got.br_startoff == del->br_startoff) << 1) |
5208 (got_endoff == del_endoff)) {
5209 case 3:
5210 /*
5211 * Matches the whole extent. Delete the entry.
5212 */
5213 xfs_iext_remove(ip, *idx, 1,
5214 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5215 --*idx;
5216 if (delay)
5217 break;
5218
5219 XFS_IFORK_NEXT_SET(ip, whichfork,
5220 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5221 flags |= XFS_ILOG_CORE;
5222 if (!cur) {
5223 flags |= xfs_ilog_fext(whichfork);
5224 break;
5225 }
5226 if ((error = xfs_btree_delete(cur, &i)))
5227 goto done;
5228 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5229 break;
5230
5231 case 2:
5232 /*
5233 * Deleting the first part of the extent.
5234 */
5235 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5236 xfs_bmbt_set_startoff(ep, del_endoff);
5237 temp = got.br_blockcount - del->br_blockcount;
5238 xfs_bmbt_set_blockcount(ep, temp);
5239 if (delay) {
5240 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5241 da_old);
5242 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5243 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5244 da_new = temp;
5245 break;
5246 }
5247 xfs_bmbt_set_startblock(ep, del_endblock);
5248 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5249 if (!cur) {
5250 flags |= xfs_ilog_fext(whichfork);
5251 break;
5252 }
5253 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5254 got.br_blockcount - del->br_blockcount,
5255 got.br_state)))
5256 goto done;
5257 break;
5258
5259 case 1:
5260 /*
5261 * Deleting the last part of the extent.
5262 */
5263 temp = got.br_blockcount - del->br_blockcount;
5264 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5265 xfs_bmbt_set_blockcount(ep, temp);
5266 if (delay) {
5267 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5268 da_old);
5269 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5270 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5271 da_new = temp;
5272 break;
5273 }
5274 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5275 if (!cur) {
5276 flags |= xfs_ilog_fext(whichfork);
5277 break;
5278 }
5279 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5280 got.br_startblock,
5281 got.br_blockcount - del->br_blockcount,
5282 got.br_state)))
5283 goto done;
5284 break;
5285
5286 case 0:
5287 /*
5288 * Deleting the middle of the extent.
5289 */
5290 temp = del->br_startoff - got.br_startoff;
5291 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5292 xfs_bmbt_set_blockcount(ep, temp);
5293 new.br_startoff = del_endoff;
5294 temp2 = got_endoff - del_endoff;
5295 new.br_blockcount = temp2;
5296 new.br_state = got.br_state;
5297 if (!delay) {
5298 new.br_startblock = del_endblock;
5299 flags |= XFS_ILOG_CORE;
5300 if (cur) {
5301 if ((error = xfs_bmbt_update(cur,
5302 got.br_startoff,
5303 got.br_startblock, temp,
5304 got.br_state)))
5305 goto done;
5306 if ((error = xfs_btree_increment(cur, 0, &i)))
5307 goto done;
5308 cur->bc_rec.b = new;
5309 error = xfs_btree_insert(cur, &i);
5310 if (error && error != ENOSPC)
5311 goto done;
5312 /*
5313 * If get no-space back from btree insert,
5314 * it tried a split, and we have a zero
5315 * block reservation.
5316 * Fix up our state and return the error.
5317 */
5318 if (error == ENOSPC) {
5319 /*
5320 * Reset the cursor, don't trust
5321 * it after any insert operation.
5322 */
5323 if ((error = xfs_bmbt_lookup_eq(cur,
5324 got.br_startoff,
5325 got.br_startblock,
5326 temp, &i)))
5327 goto done;
5328 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5329 /*
5330 * Update the btree record back
5331 * to the original value.
5332 */
5333 if ((error = xfs_bmbt_update(cur,
5334 got.br_startoff,
5335 got.br_startblock,
5336 got.br_blockcount,
5337 got.br_state)))
5338 goto done;
5339 /*
5340 * Reset the extent record back
5341 * to the original value.
5342 */
5343 xfs_bmbt_set_blockcount(ep,
5344 got.br_blockcount);
5345 flags = 0;
5346 error = XFS_ERROR(ENOSPC);
5347 goto done;
5348 }
5349 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5350 } else
5351 flags |= xfs_ilog_fext(whichfork);
5352 XFS_IFORK_NEXT_SET(ip, whichfork,
5353 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5354 } else {
5355 ASSERT(whichfork == XFS_DATA_FORK);
5356 temp = xfs_bmap_worst_indlen(ip, temp);
5357 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5358 temp2 = xfs_bmap_worst_indlen(ip, temp2);
5359 new.br_startblock = nullstartblock((int)temp2);
5360 da_new = temp + temp2;
5361 while (da_new > da_old) {
5362 if (temp) {
5363 temp--;
5364 da_new--;
5365 xfs_bmbt_set_startblock(ep,
5366 nullstartblock((int)temp));
5367 }
5368 if (da_new == da_old)
5369 break;
5370 if (temp2) {
5371 temp2--;
5372 da_new--;
5373 new.br_startblock =
5374 nullstartblock((int)temp2);
5375 }
5376 }
5377 }
5378 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5379 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5380 ++*idx;
5381 break;
5382 }
5383 /*
5384 * If we need to, add to list of extents to delete.
5385 */
5386 if (do_fx)
5387 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
5388 mp);
5389 /*
5390 * Adjust inode # blocks in the file.
5391 */
5392 if (nblks)
5393 ip->i_d.di_nblocks -= nblks;
5394 /*
5395 * Adjust quota data.
5396 */
5397 if (qfield)
5398 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5399
5400 /*
5401 * Account for change in delayed indirect blocks.
5402 * Nothing to do for disk quota accounting here.
5403 */
5404 ASSERT(da_old >= da_new);
5405 if (da_old > da_new) {
5406 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5407 (int64_t)(da_old - da_new), 0);
5408 }
5409done:
5410 *logflagsp = flags;
5411 return error;
5412}
5413
5414/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005415 * Unmap (remove) blocks from a file.
5416 * If nexts is nonzero then the number of extents to remove is limited to
5417 * that value. If not all extents in the block range can be removed then
5418 * *done is set.
5419 */
5420int /* error */
5421xfs_bunmapi(
5422 xfs_trans_t *tp, /* transaction pointer */
5423 struct xfs_inode *ip, /* incore inode */
5424 xfs_fileoff_t bno, /* starting offset to unmap */
5425 xfs_filblks_t len, /* length to unmap in file */
5426 int flags, /* misc flags */
5427 xfs_extnum_t nexts, /* number of extents max */
5428 xfs_fsblock_t *firstblock, /* first allocated block
5429 controls a.g. for allocs */
5430 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5431 int *done) /* set if not done yet */
5432{
5433 xfs_btree_cur_t *cur; /* bmap btree cursor */
5434 xfs_bmbt_irec_t del; /* extent being deleted */
5435 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005436 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005437 int error; /* error return value */
5438 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005439 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 xfs_ifork_t *ifp; /* inode fork pointer */
5441 int isrt; /* freeing in rt area */
5442 xfs_extnum_t lastx; /* last extent index used */
5443 int logflags; /* transaction logging flags */
5444 xfs_extlen_t mod; /* rt extent offset */
5445 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005446 xfs_extnum_t nextents; /* number of file extents */
5447 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 xfs_fileoff_t start; /* first file offset deleted */
5449 int tmp_logflags; /* partial logging flags */
5450 int wasdel; /* was a delayed alloc extent */
5451 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 xfs_fsblock_t sum;
5453
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005454 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5455
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5457 XFS_ATTR_FORK : XFS_DATA_FORK;
5458 ifp = XFS_IFORK_PTR(ip, whichfork);
5459 if (unlikely(
5460 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5461 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5462 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5463 ip->i_mount);
5464 return XFS_ERROR(EFSCORRUPTED);
5465 }
5466 mp = ip->i_mount;
5467 if (XFS_FORCED_SHUTDOWN(mp))
5468 return XFS_ERROR(EIO);
Christoph Hellwig54893272011-05-11 15:04:03 +00005469
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 ASSERT(len > 0);
5471 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005472
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5474 (error = xfs_iread_extents(tp, ip, whichfork)))
5475 return error;
5476 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5477 if (nextents == 0) {
5478 *done = 1;
5479 return 0;
5480 }
5481 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005482 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483 start = bno;
5484 bno = start + len - 1;
5485 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5486 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005487
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 /*
5489 * Check to see if the given block number is past the end of the
5490 * file, back up to the last block if so...
5491 */
5492 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005493 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005494 xfs_bmbt_get_all(ep, &got);
5495 bno = got.br_startoff + got.br_blockcount - 1;
5496 }
5497 logflags = 0;
5498 if (ifp->if_flags & XFS_IFBROOT) {
5499 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005500 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 cur->bc_private.b.firstblock = *firstblock;
5502 cur->bc_private.b.flist = flist;
5503 cur->bc_private.b.flags = 0;
5504 } else
5505 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005506
5507 if (isrt) {
5508 /*
5509 * Synchronize by locking the bitmap inode.
5510 */
5511 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5512 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5513 }
5514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 extno = 0;
5516 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5517 (nexts == 0 || extno < nexts)) {
5518 /*
5519 * Is the found extent after a hole in which bno lives?
5520 * Just back up to the previous extent, if so.
5521 */
5522 if (got.br_startoff > bno) {
5523 if (--lastx < 0)
5524 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005525 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 xfs_bmbt_get_all(ep, &got);
5527 }
5528 /*
5529 * Is the last block of this extent before the range
5530 * we're supposed to delete? If so, we're done.
5531 */
5532 bno = XFS_FILEOFF_MIN(bno,
5533 got.br_startoff + got.br_blockcount - 1);
5534 if (bno < start)
5535 break;
5536 /*
5537 * Then deal with the (possibly delayed) allocated space
5538 * we found.
5539 */
5540 ASSERT(ep != NULL);
5541 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005542 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 if (got.br_startoff < start) {
5544 del.br_startoff = start;
5545 del.br_blockcount -= start - got.br_startoff;
5546 if (!wasdel)
5547 del.br_startblock += start - got.br_startoff;
5548 }
5549 if (del.br_startoff + del.br_blockcount > bno + 1)
5550 del.br_blockcount = bno + 1 - del.br_startoff;
5551 sum = del.br_startblock + del.br_blockcount;
5552 if (isrt &&
5553 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5554 /*
5555 * Realtime extent not lined up at the end.
5556 * The extent could have been split into written
5557 * and unwritten pieces, or we could just be
5558 * unmapping part of it. But we can't really
5559 * get rid of part of a realtime extent.
5560 */
5561 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005562 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005563 /*
5564 * This piece is unwritten, or we're not
5565 * using unwritten extents. Skip over it.
5566 */
5567 ASSERT(bno >= mod);
5568 bno -= mod > del.br_blockcount ?
5569 del.br_blockcount : mod;
5570 if (bno < got.br_startoff) {
5571 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005572 xfs_bmbt_get_all(xfs_iext_get_ext(
5573 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 }
5575 continue;
5576 }
5577 /*
5578 * It's written, turn it unwritten.
5579 * This is better than zeroing it.
5580 */
5581 ASSERT(del.br_state == XFS_EXT_NORM);
5582 ASSERT(xfs_trans_get_block_res(tp) > 0);
5583 /*
5584 * If this spans a realtime extent boundary,
5585 * chop it back to the start of the one we end at.
5586 */
5587 if (del.br_blockcount > mod) {
5588 del.br_startoff += del.br_blockcount - mod;
5589 del.br_startblock += del.br_blockcount - mod;
5590 del.br_blockcount = mod;
5591 }
5592 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005593 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5594 &lastx, &cur, &del, firstblock, flist,
5595 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 if (error)
5597 goto error0;
5598 goto nodelete;
5599 }
5600 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5601 /*
5602 * Realtime extent is lined up at the end but not
5603 * at the front. We'll get rid of full extents if
5604 * we can.
5605 */
5606 mod = mp->m_sb.sb_rextsize - mod;
5607 if (del.br_blockcount > mod) {
5608 del.br_blockcount -= mod;
5609 del.br_startoff += mod;
5610 del.br_startblock += mod;
5611 } else if ((del.br_startoff == start &&
5612 (del.br_state == XFS_EXT_UNWRITTEN ||
5613 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005614 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 /*
5616 * Can't make it unwritten. There isn't
5617 * a full extent here so just skip it.
5618 */
5619 ASSERT(bno >= del.br_blockcount);
5620 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005621 if (got.br_startoff > bno) {
5622 if (--lastx >= 0) {
5623 ep = xfs_iext_get_ext(ifp,
5624 lastx);
5625 xfs_bmbt_get_all(ep, &got);
5626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 }
5628 continue;
5629 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5630 /*
5631 * This one is already unwritten.
5632 * It must have a written left neighbor.
5633 * Unwrite the killed part of that one and
5634 * try again.
5635 */
5636 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005637 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5638 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005640 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 ASSERT(del.br_startblock ==
5642 prev.br_startblock + prev.br_blockcount);
5643 if (prev.br_startoff < start) {
5644 mod = start - prev.br_startoff;
5645 prev.br_blockcount -= mod;
5646 prev.br_startblock += mod;
5647 prev.br_startoff = start;
5648 }
5649 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005650 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005651 error = xfs_bmap_add_extent_unwritten_real(tp,
5652 ip, &lastx, &cur, &prev,
5653 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 if (error)
5655 goto error0;
5656 goto nodelete;
5657 } else {
5658 ASSERT(del.br_state == XFS_EXT_NORM);
5659 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005660 error = xfs_bmap_add_extent_unwritten_real(tp,
5661 ip, &lastx, &cur, &del,
5662 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 if (error)
5664 goto error0;
5665 goto nodelete;
5666 }
5667 }
5668 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005669 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005670 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005671 if (isrt) {
5672 xfs_filblks_t rtexts;
5673
5674 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5675 do_div(rtexts, mp->m_sb.sb_rextsize);
5676 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005677 (int64_t)rtexts, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005678 (void)xfs_trans_reserve_quota_nblks(NULL,
5679 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005680 XFS_QMOPT_RES_RTBLKS);
5681 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00005682 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005683 (int64_t)del.br_blockcount, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005684 (void)xfs_trans_reserve_quota_nblks(NULL,
5685 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005688 ip->i_delayed_blks -= del.br_blockcount;
5689 if (cur)
5690 cur->bc_private.b.flags |=
5691 XFS_BTCUR_BPRV_WASDEL;
5692 } else if (cur)
5693 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5694 /*
5695 * If it's the case where the directory code is running
5696 * with no block reservation, and the deleted block is in
5697 * the middle of its extent, and the resulting insert
5698 * of an extent would cause transformation to btree format,
5699 * then reject it. The calling code will then swap
5700 * blocks around instead.
5701 * We have to do this now, rather than waiting for the
5702 * conversion to btree format, since the transaction
5703 * will be dirty.
5704 */
5705 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5706 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005707 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5708 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 del.br_startoff > got.br_startoff &&
5710 del.br_startoff + del.br_blockcount <
5711 got.br_startoff + got.br_blockcount) {
5712 error = XFS_ERROR(ENOSPC);
5713 goto error0;
5714 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00005715 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005716 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 logflags |= tmp_logflags;
5718 if (error)
5719 goto error0;
5720 bno = del.br_startoff - 1;
5721nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 /*
5723 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005726 if (lastx >= 0) {
5727 ep = xfs_iext_get_ext(ifp, lastx);
5728 if (xfs_bmbt_get_startoff(ep) > bno) {
5729 if (--lastx >= 0)
5730 ep = xfs_iext_get_ext(ifp,
5731 lastx);
5732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 extno++;
5736 }
5737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005739
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 /*
5741 * Convert to a btree if necessary.
5742 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005743 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 ASSERT(cur == NULL);
5745 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5746 &cur, 0, &tmp_logflags, whichfork);
5747 logflags |= tmp_logflags;
5748 if (error)
5749 goto error0;
5750 }
5751 /*
5752 * transform from btree to extents, give it cur
5753 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005754 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 ASSERT(cur != NULL);
5756 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5757 whichfork);
5758 logflags |= tmp_logflags;
5759 if (error)
5760 goto error0;
5761 }
5762 /*
5763 * transform from extents to local?
5764 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 error = 0;
5766error0:
5767 /*
5768 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005769 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005771 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005773 logflags &= ~xfs_ilog_fext(whichfork);
5774 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005776 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 /*
5778 * Log inode even in the error case, if the transaction
5779 * is dirty we'll need to shut down the filesystem.
5780 */
5781 if (logflags)
5782 xfs_trans_log_inode(tp, ip, logflags);
5783 if (cur) {
5784 if (!error) {
5785 *firstblock = cur->bc_private.b.firstblock;
5786 cur->bc_private.b.allocated = 0;
5787 }
5788 xfs_btree_del_cursor(cur,
5789 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5790 }
5791 return error;
5792}
5793
5794/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005795 * returns 1 for success, 0 if we failed to map the extent.
5796 */
5797STATIC int
5798xfs_getbmapx_fix_eof_hole(
5799 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005800 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005801 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005802 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005803 __int64_t end, /* last block requested */
5804 xfs_fsblock_t startblock)
5805{
5806 __int64_t fixlen;
5807 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005808 xfs_ifork_t *ifp; /* inode fork pointer */
5809 xfs_extnum_t lastx; /* last extent pointer */
5810 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005811
5812 if (startblock == HOLESTARTBLOCK) {
5813 mp = ip->i_mount;
5814 out->bmv_block = -1;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005815 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005816 fixlen -= out->bmv_offset;
5817 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5818 /* Came to hole at EOF. Trim it. */
5819 if (fixlen <= 0)
5820 return 0;
5821 out->bmv_length = fixlen;
5822 }
5823 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005824 if (startblock == DELAYSTARTBLOCK)
5825 out->bmv_block = -2;
5826 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005827 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005828 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5829 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5830 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5831 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5832 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005833 }
5834
5835 return 1;
5836}
5837
5838/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005839 * Get inode's extents as described in bmv, and format for output.
5840 * Calls formatter to fill the user's buffer until all extents
5841 * are mapped, until the passed-in bmv->bmv_count slots have
5842 * been filled, or until the formatter short-circuits the loop,
5843 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005844 */
5845int /* error code */
5846xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005847 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005848 struct getbmapx *bmv, /* user bmap structure */
5849 xfs_bmap_format_t formatter, /* format to user */
5850 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005851{
5852 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005853 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005854 __int64_t fixlen; /* length for -1 case */
5855 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 int lock; /* lock state */
5857 xfs_bmbt_irec_t *map; /* buffer for user's data */
5858 xfs_mount_t *mp; /* file system mount point */
5859 int nex; /* # of user extents can do */
5860 int nexleft; /* # of user extents left */
5861 int subnex; /* # of bmapi's can do */
5862 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005863 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864 int whichfork; /* data or attr fork */
5865 int prealloced; /* this is a file with
5866 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005867 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005869 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870
Linus Torvalds1da177e2005-04-16 15:20:36 -07005871 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005872 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005873 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 if (whichfork == XFS_ATTR_FORK) {
5876 if (XFS_IFORK_Q(ip)) {
5877 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5878 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5879 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5880 return XFS_ERROR(EINVAL);
5881 } else if (unlikely(
5882 ip->i_d.di_aformat != 0 &&
5883 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5884 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5885 ip->i_mount);
5886 return XFS_ERROR(EFSCORRUPTED);
5887 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005888
5889 prealloced = 0;
5890 fixlen = 1LL << 32;
5891 } else {
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005892 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5893 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5894 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5895 return XFS_ERROR(EINVAL);
5896
David Chinner957d0eb2007-06-18 16:50:37 +10005897 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005898 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 prealloced = 1;
Dave Chinner32972382012-06-08 15:44:54 +10005900 fixlen = mp->m_super->s_maxbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005901 } else {
5902 prealloced = 0;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005903 fixlen = XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 }
5906
5907 if (bmv->bmv_length == -1) {
5908 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005909 bmv->bmv_length =
5910 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5911 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005912 bmv->bmv_entries = 0;
5913 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005914 } else if (bmv->bmv_length < 0) {
5915 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005916 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005917
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918 nex = bmv->bmv_count - 1;
5919 if (nex <= 0)
5920 return XFS_ERROR(EINVAL);
5921 bmvend = bmv->bmv_offset + bmv->bmv_length;
5922
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005923
5924 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5925 return XFS_ERROR(ENOMEM);
5926 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
Dave Chinnerf0742112012-03-07 04:50:22 +00005927 if (!out) {
5928 out = kmem_zalloc_large(bmv->bmv_count *
5929 sizeof(struct getbmapx));
5930 if (!out)
5931 return XFS_ERROR(ENOMEM);
5932 }
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005933
Linus Torvalds1da177e2005-04-16 15:20:36 -07005934 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005935 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005936 if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) {
Dave Chinner4bc1ea62012-11-12 22:53:56 +11005937 error = -filemap_write_and_wait(VFS_I(ip)->i_mapping);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005938 if (error)
5939 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005940 }
Dave Chinner309c8482010-11-30 15:16:02 +11005941 /*
5942 * even after flushing the inode, there can still be delalloc
5943 * blocks on the inode beyond EOF due to speculative
5944 * preallocation. These are not removed until the release
5945 * function is called or the inode is inactivated. Hence we
5946 * cannot assert here that ip->i_delayed_blks == 0.
5947 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005949
5950 lock = xfs_ilock_map_shared(ip);
5951
5952 /*
5953 * Don't let nex be bigger than the number of extents
5954 * we can have assuming alternating holes and real extents.
5955 */
5956 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5957 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5958
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005959 bmapi_flags = xfs_bmapi_aflag(whichfork);
5960 if (!(iflags & BMV_IF_PREALLOC))
5961 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005962
5963 /*
5964 * Allocate enough space to handle "subnex" maps at a time.
5965 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005966 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005967 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04005968 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005969 if (!map)
5970 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971
5972 bmv->bmv_entries = 0;
5973
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005974 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5975 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5976 error = 0;
5977 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005978 }
5979
5980 nexleft = nex;
5981
5982 do {
5983 nmap = (nexleft > subnex) ? subnex : nexleft;
Dave Chinner5c8ed202011-09-18 20:40:45 +00005984 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5985 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5986 map, &nmap, bmapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005988 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989 ASSERT(nmap <= subnex);
5990
5991 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005992 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005993 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005994 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005995 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005996 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5997 out[cur_ext].bmv_offset =
5998 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5999 out[cur_ext].bmv_length =
6000 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
6001 out[cur_ext].bmv_unused1 = 0;
6002 out[cur_ext].bmv_unused2 = 0;
Dave Chinner58e20772012-04-29 21:16:17 +10006003
6004 /*
6005 * delayed allocation extents that start beyond EOF can
6006 * occur due to speculative EOF allocation when the
6007 * delalloc extent is larger than the largest freespace
6008 * extent at conversion time. These extents cannot be
6009 * converted by data writeback, so can exist here even
6010 * if we are not supposed to be finding delalloc
6011 * extents.
6012 */
6013 if (map[i].br_startblock == DELAYSTARTBLOCK &&
6014 map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
6015 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
6016
Yingping Lu9af0a702005-11-02 15:09:54 +11006017 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10006018 whichfork == XFS_ATTR_FORK) {
6019 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006020 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006021 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006023
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006024 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
6025 prealloced, bmvend,
6026 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006027 goto out_free_map;
6028
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006029 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006030 out[cur_ext].bmv_offset +
6031 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006032 bmv->bmv_length =
6033 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
Tao Ma9af25462010-08-30 02:44:03 +00006034
6035 /*
6036 * In case we don't want to return the hole,
6037 * don't increase cur_ext so that we can reuse
6038 * it in the next loop.
6039 */
6040 if ((iflags & BMV_IF_NO_HOLES) &&
6041 map[i].br_startblock == HOLESTARTBLOCK) {
6042 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
6043 continue;
6044 }
6045
6046 nexleft--;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006047 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006048 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 }
6050 } while (nmap && nexleft && bmv->bmv_length);
6051
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006052 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10006053 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006054 out_unlock_ilock:
6055 xfs_iunlock_map_shared(ip, lock);
6056 out_unlock_iolock:
6057 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006058
6059 for (i = 0; i < cur_ext; i++) {
6060 int full = 0; /* user array is full */
6061
6062 /* format results & advance arg */
6063 error = formatter(&arg, &out[i], &full);
6064 if (error || full)
6065 break;
6066 }
6067
Dave Chinnerf0742112012-03-07 04:50:22 +00006068 if (is_vmalloc_addr(out))
6069 kmem_free_large(out);
6070 else
6071 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006072 return error;
6073}
6074
Dave Chinnerc726de42010-11-30 15:14:39 +11006075/*
6076 * dead simple method of punching delalyed allocation blocks from a range in
6077 * the inode. Walks a block at a time so will be slow, but is only executed in
6078 * rare error cases so the overhead is not critical. This will alays punch out
6079 * both the start and end blocks, even if the ranges only partially overlap
6080 * them, so it is up to the caller to ensure that partial blocks are not
6081 * passed in.
6082 */
6083int
6084xfs_bmap_punch_delalloc_range(
6085 struct xfs_inode *ip,
6086 xfs_fileoff_t start_fsb,
6087 xfs_fileoff_t length)
6088{
6089 xfs_fileoff_t remaining = length;
6090 int error = 0;
6091
6092 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6093
6094 do {
6095 int done;
6096 xfs_bmbt_irec_t imap;
6097 int nimaps = 1;
6098 xfs_fsblock_t firstblock;
6099 xfs_bmap_free_t flist;
6100
6101 /*
6102 * Map the range first and check that it is a delalloc extent
6103 * before trying to unmap the range. Otherwise we will be
6104 * trying to remove a real extent (which requires a
6105 * transaction) or a hole, which is probably a bad idea...
6106 */
Dave Chinner5c8ed202011-09-18 20:40:45 +00006107 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
6108 XFS_BMAPI_ENTIRE);
Dave Chinnerc726de42010-11-30 15:14:39 +11006109
6110 if (error) {
6111 /* something screwed, just bail */
6112 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner53487782011-03-07 10:05:35 +11006113 xfs_alert(ip->i_mount,
Dave Chinnerc726de42010-11-30 15:14:39 +11006114 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6115 ip->i_ino, start_fsb);
6116 }
6117 break;
6118 }
6119 if (!nimaps) {
6120 /* nothing there */
6121 goto next_block;
6122 }
6123 if (imap.br_startblock != DELAYSTARTBLOCK) {
6124 /* been converted, ignore */
6125 goto next_block;
6126 }
6127 WARN_ON(imap.br_blockcount == 0);
6128
6129 /*
6130 * Note: while we initialise the firstblock/flist pair, they
6131 * should never be used because blocks should never be
6132 * allocated or freed for a delalloc extent and hence we need
6133 * don't cancel or finish them after the xfs_bunmapi() call.
6134 */
6135 xfs_bmap_init(&flist, &firstblock);
6136 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6137 &flist, &done);
6138 if (error)
6139 break;
6140
6141 ASSERT(!flist.xbf_count && !flist.xbf_first);
6142next_block:
6143 start_fsb++;
6144 remaining--;
6145 } while(remaining > 0);
6146
6147 return error;
6148}