blob: 05c698ccb238f4fa9eef9a1844683fe24f62bd13 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_types.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dir2.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100028#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110038#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_extfree_item.h"
40#include "xfs_alloc.h"
41#include "xfs_bmap.h"
42#include "xfs_rtalloc.h"
43#include "xfs_error.h"
Nathan Scottd8cc8902005-11-02 10:34:53 +110044#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include "xfs_quota.h"
46#include "xfs_trans_space.h"
47#include "xfs_buf_item.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100048#include "xfs_filestream.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100049#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000050#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110051#include "xfs_symlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054kmem_zone_t *xfs_bmap_free_item_zone;
55
56/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110057 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110061 * Compute and fill in the value of the maximum depth of a bmap btree
62 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110064void
65xfs_bmap_compute_maxlevels(
66 xfs_mount_t *mp, /* file system mount structure */
67 int whichfork) /* data or attr fork */
68{
69 int level; /* btree level */
70 uint maxblocks; /* max blocks at this level */
71 uint maxleafents; /* max leaf entries possible */
72 int maxrootrecs; /* max records in root block */
73 int minleafrecs; /* min records in leaf block */
74 int minnoderecs; /* min records in node block */
75 int sz; /* root block size */
76
77 /*
78 * The maximum number of extents in a file, hence the maximum
79 * number of leaf entries, is controlled by the type of di_nextents
80 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
81 * (a signed 16-bit number, xfs_aextnum_t).
82 *
83 * Note that we can no longer assume that if we are in ATTR1 that
84 * the fork offset of all the inodes will be
85 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
86 * with ATTR2 and then mounted back with ATTR1, keeping the
87 * di_forkoff's fixed but probably at various positions. Therefore,
88 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
89 * of a minimum size available.
90 */
91 if (whichfork == XFS_DATA_FORK) {
92 maxleafents = MAXEXTNUM;
93 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
94 } else {
95 maxleafents = MAXAEXTNUM;
96 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
97 }
98 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
99 minleafrecs = mp->m_bmap_dmnr[0];
100 minnoderecs = mp->m_bmap_dmnr[1];
101 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
102 for (level = 1; maxblocks > 1; level++) {
103 if (maxblocks <= maxrootrecs)
104 maxblocks = 1;
105 else
106 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
107 }
108 mp->m_bm_maxlevels[whichfork] = level;
109}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100112 * Convert the given file system block to a disk block. We have to treat it
113 * differently based on whether the file is a real time file or not, because the
114 * bmap code does.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100116xfs_daddr_t
117xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
118{
119 return (XFS_IS_REALTIME_INODE(ip) ? \
120 (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
121 XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
122}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100124STATIC int /* error */
125xfs_bmbt_lookup_eq(
126 struct xfs_btree_cur *cur,
127 xfs_fileoff_t off,
128 xfs_fsblock_t bno,
129 xfs_filblks_t len,
130 int *stat) /* success/failure */
131{
132 cur->bc_rec.b.br_startoff = off;
133 cur->bc_rec.b.br_startblock = bno;
134 cur->bc_rec.b.br_blockcount = len;
135 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
136}
137
138STATIC int /* error */
139xfs_bmbt_lookup_ge(
140 struct xfs_btree_cur *cur,
141 xfs_fileoff_t off,
142 xfs_fsblock_t bno,
143 xfs_filblks_t len,
144 int *stat) /* success/failure */
145{
146 cur->bc_rec.b.br_startoff = off;
147 cur->bc_rec.b.br_startblock = bno;
148 cur->bc_rec.b.br_blockcount = len;
149 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
150}
151
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100152/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000153 * Check if the inode needs to be converted to btree format.
154 */
155static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
156{
157 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
158 XFS_IFORK_NEXTENTS(ip, whichfork) >
159 XFS_IFORK_MAXEXT(ip, whichfork);
160}
161
162/*
163 * Check if the inode should be converted to extent format.
164 */
165static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
166{
167 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
168 XFS_IFORK_NEXTENTS(ip, whichfork) <=
169 XFS_IFORK_MAXEXT(ip, whichfork);
170}
171
172/*
173 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100174 * by [off, bno, len, state].
175 * This either works (return 0) or gets an EFSCORRUPTED error.
176 */
177STATIC int
178xfs_bmbt_update(
179 struct xfs_btree_cur *cur,
180 xfs_fileoff_t off,
181 xfs_fsblock_t bno,
182 xfs_filblks_t len,
183 xfs_exntst_t state)
184{
185 union xfs_btree_rec rec;
186
187 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
188 return xfs_btree_update(cur, &rec);
189}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100192 * Compute the worst-case number of indirect blocks that will be used
193 * for ip's delayed extent of length "len".
194 */
195STATIC xfs_filblks_t
196xfs_bmap_worst_indlen(
197 xfs_inode_t *ip, /* incore inode pointer */
198 xfs_filblks_t len) /* delayed extent length */
199{
200 int level; /* btree level number */
201 int maxrecs; /* maximum record count at this level */
202 xfs_mount_t *mp; /* mount structure */
203 xfs_filblks_t rval; /* return value */
204
205 mp = ip->i_mount;
206 maxrecs = mp->m_bmap_dmxr[0];
207 for (level = 0, rval = 0;
208 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
209 level++) {
210 len += maxrecs - 1;
211 do_div(len, maxrecs);
212 rval += len;
213 if (len == 1)
214 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
215 level - 1;
216 if (level == 0)
217 maxrecs = mp->m_bmap_dmxr[1];
218 }
219 return rval;
220}
221
222/*
223 * Calculate the default attribute fork offset for newly created inodes.
224 */
225uint
226xfs_default_attroffset(
227 struct xfs_inode *ip)
228{
229 struct xfs_mount *mp = ip->i_mount;
230 uint offset;
231
232 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100233 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100234 XFS_BMDR_SPACE_CALC(MINABTPTRS);
235 } else {
236 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
237 }
238
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100239 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100240 return offset;
241}
242
243/*
244 * Helper routine to reset inode di_forkoff field when switching
245 * attribute fork from local to extent format - we reset it where
246 * possible to make space available for inline data fork extents.
247 */
248STATIC void
249xfs_bmap_forkoff_reset(
250 xfs_mount_t *mp,
251 xfs_inode_t *ip,
252 int whichfork)
253{
254 if (whichfork == XFS_ATTR_FORK &&
255 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
256 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
257 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
258 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
259
260 if (dfl_forkoff > ip->i_d.di_forkoff)
261 ip->i_d.di_forkoff = dfl_forkoff;
262 }
263}
264
265/*
266 * Extent tree block counting routines.
267 */
268
269/*
270 * Count leaf blocks given a range of extent records.
271 */
272STATIC void
273xfs_bmap_count_leaves(
274 xfs_ifork_t *ifp,
275 xfs_extnum_t idx,
276 int numrecs,
277 int *count)
278{
279 int b;
280
281 for (b = 0; b < numrecs; b++) {
282 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
283 *count += xfs_bmbt_get_blockcount(frp);
284 }
285}
286
287/*
288 * Count leaf blocks given a range of extent records originally
289 * in btree format.
290 */
291STATIC void
292xfs_bmap_disk_count_leaves(
293 struct xfs_mount *mp,
294 struct xfs_btree_block *block,
295 int numrecs,
296 int *count)
297{
298 int b;
299 xfs_bmbt_rec_t *frp;
300
301 for (b = 1; b <= numrecs; b++) {
302 frp = XFS_BMBT_REC_ADDR(mp, block, b);
303 *count += xfs_bmbt_disk_get_blockcount(frp);
304 }
305}
306
307/*
308 * Recursively walks each level of a btree
309 * to count total fsblocks is use.
310 */
311STATIC int /* error */
312xfs_bmap_count_tree(
313 xfs_mount_t *mp, /* file system mount point */
314 xfs_trans_t *tp, /* transaction pointer */
315 xfs_ifork_t *ifp, /* inode fork pointer */
316 xfs_fsblock_t blockno, /* file system block number */
317 int levelin, /* level in btree */
318 int *count) /* Count of blocks */
319{
320 int error;
321 xfs_buf_t *bp, *nbp;
322 int level = levelin;
323 __be64 *pp;
324 xfs_fsblock_t bno = blockno;
325 xfs_fsblock_t nextbno;
326 struct xfs_btree_block *block, *nextblock;
327 int numrecs;
328
329 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
330 &xfs_bmbt_buf_ops);
331 if (error)
332 return error;
333 *count += 1;
334 block = XFS_BUF_TO_BLOCK(bp);
335
336 if (--level) {
337 /* Not at node above leaves, count this level of nodes */
338 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
339 while (nextbno != NULLFSBLOCK) {
340 error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
341 XFS_BMAP_BTREE_REF,
342 &xfs_bmbt_buf_ops);
343 if (error)
344 return error;
345 *count += 1;
346 nextblock = XFS_BUF_TO_BLOCK(nbp);
347 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
348 xfs_trans_brelse(tp, nbp);
349 }
350
351 /* Dive to the next level */
352 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
353 bno = be64_to_cpu(*pp);
354 if (unlikely((error =
355 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
356 xfs_trans_brelse(tp, bp);
357 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
358 XFS_ERRLEVEL_LOW, mp);
359 return XFS_ERROR(EFSCORRUPTED);
360 }
361 xfs_trans_brelse(tp, bp);
362 } else {
363 /* count all level 1 nodes and their leaves */
364 for (;;) {
365 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
366 numrecs = be16_to_cpu(block->bb_numrecs);
367 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
368 xfs_trans_brelse(tp, bp);
369 if (nextbno == NULLFSBLOCK)
370 break;
371 bno = nextbno;
372 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
373 XFS_BMAP_BTREE_REF,
374 &xfs_bmbt_buf_ops);
375 if (error)
376 return error;
377 *count += 1;
378 block = XFS_BUF_TO_BLOCK(bp);
379 }
380 }
381 return 0;
382}
383
384/*
385 * Count fsblocks of the given fork.
386 */
387int /* error */
388xfs_bmap_count_blocks(
389 xfs_trans_t *tp, /* transaction pointer */
390 xfs_inode_t *ip, /* incore inode */
391 int whichfork, /* data or attr fork */
392 int *count) /* out: count of blocks */
393{
394 struct xfs_btree_block *block; /* current btree block */
395 xfs_fsblock_t bno; /* block # of "block" */
396 xfs_ifork_t *ifp; /* fork structure */
397 int level; /* btree level, for checking */
398 xfs_mount_t *mp; /* file system mount structure */
399 __be64 *pp; /* pointer to block address */
400
401 bno = NULLFSBLOCK;
402 mp = ip->i_mount;
403 ifp = XFS_IFORK_PTR(ip, whichfork);
404 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
405 xfs_bmap_count_leaves(ifp, 0,
406 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
407 count);
408 return 0;
409 }
410
411 /*
412 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
413 */
414 block = ifp->if_broot;
415 level = be16_to_cpu(block->bb_level);
416 ASSERT(level > 0);
417 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
418 bno = be64_to_cpu(*pp);
419 ASSERT(bno != NULLDFSBNO);
420 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
421 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
422
423 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
424 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
425 mp);
426 return XFS_ERROR(EFSCORRUPTED);
427 }
428
429 return 0;
430}
431
432/*
433 * Debug/sanity checking code
434 */
435
436STATIC int
437xfs_bmap_sanity_check(
438 struct xfs_mount *mp,
439 struct xfs_buf *bp,
440 int level)
441{
442 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
443
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500444 if (block->bb_magic != cpu_to_be32(XFS_BMAP_CRC_MAGIC) &&
445 block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC))
446 return 0;
447
448 if (be16_to_cpu(block->bb_level) != level ||
Dave Chinner9e5987a2013-02-25 12:31:26 +1100449 be16_to_cpu(block->bb_numrecs) == 0 ||
450 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
451 return 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500452
Dave Chinner9e5987a2013-02-25 12:31:26 +1100453 return 1;
454}
455
456#ifdef DEBUG
457STATIC struct xfs_buf *
458xfs_bmap_get_bp(
459 struct xfs_btree_cur *cur,
460 xfs_fsblock_t bno)
461{
462 struct xfs_log_item_desc *lidp;
463 int i;
464
465 if (!cur)
466 return NULL;
467
468 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
469 if (!cur->bc_bufs[i])
470 break;
471 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
472 return cur->bc_bufs[i];
473 }
474
475 /* Chase down all the log items to see if the bp is there */
476 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
477 struct xfs_buf_log_item *bip;
478 bip = (struct xfs_buf_log_item *)lidp->lid_item;
479 if (bip->bli_item.li_type == XFS_LI_BUF &&
480 XFS_BUF_ADDR(bip->bli_buf) == bno)
481 return bip->bli_buf;
482 }
483
484 return NULL;
485}
486
487STATIC void
488xfs_check_block(
489 struct xfs_btree_block *block,
490 xfs_mount_t *mp,
491 int root,
492 short sz)
493{
494 int i, j, dmxr;
495 __be64 *pp, *thispa; /* pointer to block address */
496 xfs_bmbt_key_t *prevp, *keyp;
497
498 ASSERT(be16_to_cpu(block->bb_level) > 0);
499
500 prevp = NULL;
501 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
502 dmxr = mp->m_bmap_dmxr[0];
503 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
504
505 if (prevp) {
506 ASSERT(be64_to_cpu(prevp->br_startoff) <
507 be64_to_cpu(keyp->br_startoff));
508 }
509 prevp = keyp;
510
511 /*
512 * Compare the block numbers to see if there are dups.
513 */
514 if (root)
515 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
516 else
517 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
518
519 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
520 if (root)
521 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
522 else
523 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
524 if (*thispa == *pp) {
525 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
526 __func__, j, i,
527 (unsigned long long)be64_to_cpu(*thispa));
528 panic("%s: ptrs are equal in node\n",
529 __func__);
530 }
531 }
532 }
533}
534
535/*
536 * Check that the extents for the inode ip are in the right order in all
537 * btree leaves.
538 */
539
540STATIC void
541xfs_bmap_check_leaf_extents(
542 xfs_btree_cur_t *cur, /* btree cursor or null */
543 xfs_inode_t *ip, /* incore inode pointer */
544 int whichfork) /* data or attr fork */
545{
546 struct xfs_btree_block *block; /* current btree block */
547 xfs_fsblock_t bno; /* block # of "block" */
548 xfs_buf_t *bp; /* buffer for "block" */
549 int error; /* error return value */
550 xfs_extnum_t i=0, j; /* index into the extents list */
551 xfs_ifork_t *ifp; /* fork structure */
552 int level; /* btree level, for checking */
553 xfs_mount_t *mp; /* file system mount structure */
554 __be64 *pp; /* pointer to block address */
555 xfs_bmbt_rec_t *ep; /* pointer to current extent */
556 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
557 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
558 int bp_release = 0;
559
560 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
561 return;
562 }
563
564 bno = NULLFSBLOCK;
565 mp = ip->i_mount;
566 ifp = XFS_IFORK_PTR(ip, whichfork);
567 block = ifp->if_broot;
568 /*
569 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
570 */
571 level = be16_to_cpu(block->bb_level);
572 ASSERT(level > 0);
573 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
574 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
575 bno = be64_to_cpu(*pp);
576
577 ASSERT(bno != NULLDFSBNO);
578 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
579 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
580
581 /*
582 * Go down the tree until leaf level is reached, following the first
583 * pointer (leftmost) at each level.
584 */
585 while (level-- > 0) {
586 /* See if buf is in cur first */
587 bp_release = 0;
588 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
589 if (!bp) {
590 bp_release = 1;
591 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
592 XFS_BMAP_BTREE_REF,
593 &xfs_bmbt_buf_ops);
594 if (error)
595 goto error_norelse;
596 }
597 block = XFS_BUF_TO_BLOCK(bp);
598 XFS_WANT_CORRUPTED_GOTO(
599 xfs_bmap_sanity_check(mp, bp, level),
600 error0);
601 if (level == 0)
602 break;
603
604 /*
605 * Check this block for basic sanity (increasing keys and
606 * no duplicate blocks).
607 */
608
609 xfs_check_block(block, mp, 0, 0);
610 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
611 bno = be64_to_cpu(*pp);
612 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
613 if (bp_release) {
614 bp_release = 0;
615 xfs_trans_brelse(NULL, bp);
616 }
617 }
618
619 /*
620 * Here with bp and block set to the leftmost leaf node in the tree.
621 */
622 i = 0;
623
624 /*
625 * Loop over all leaf nodes checking that all extents are in the right order.
626 */
627 for (;;) {
628 xfs_fsblock_t nextbno;
629 xfs_extnum_t num_recs;
630
631
632 num_recs = xfs_btree_get_numrecs(block);
633
634 /*
635 * Read-ahead the next leaf block, if any.
636 */
637
638 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
639
640 /*
641 * Check all the extents to make sure they are OK.
642 * If we had a previous block, the last entry should
643 * conform with the first entry in this one.
644 */
645
646 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
647 if (i) {
648 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
649 xfs_bmbt_disk_get_blockcount(&last) <=
650 xfs_bmbt_disk_get_startoff(ep));
651 }
652 for (j = 1; j < num_recs; j++) {
653 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
654 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
655 xfs_bmbt_disk_get_blockcount(ep) <=
656 xfs_bmbt_disk_get_startoff(nextp));
657 ep = nextp;
658 }
659
660 last = *ep;
661 i += num_recs;
662 if (bp_release) {
663 bp_release = 0;
664 xfs_trans_brelse(NULL, bp);
665 }
666 bno = nextbno;
667 /*
668 * If we've reached the end, stop.
669 */
670 if (bno == NULLFSBLOCK)
671 break;
672
673 bp_release = 0;
674 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
675 if (!bp) {
676 bp_release = 1;
677 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
678 XFS_BMAP_BTREE_REF,
679 &xfs_bmbt_buf_ops);
680 if (error)
681 goto error_norelse;
682 }
683 block = XFS_BUF_TO_BLOCK(bp);
684 }
685 if (bp_release) {
686 bp_release = 0;
687 xfs_trans_brelse(NULL, bp);
688 }
689 return;
690
691error0:
692 xfs_warn(mp, "%s: at error0", __func__);
693 if (bp_release)
694 xfs_trans_brelse(NULL, bp);
695error_norelse:
696 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
697 __func__, i);
698 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
699 return;
700}
701
702/*
703 * Add bmap trace insert entries for all the contents of the extent records.
704 */
705void
706xfs_bmap_trace_exlist(
707 xfs_inode_t *ip, /* incore inode pointer */
708 xfs_extnum_t cnt, /* count of entries in the list */
709 int whichfork, /* data or attr fork */
710 unsigned long caller_ip)
711{
712 xfs_extnum_t idx; /* extent record index */
713 xfs_ifork_t *ifp; /* inode fork pointer */
714 int state = 0;
715
716 if (whichfork == XFS_ATTR_FORK)
717 state |= BMAP_ATTRFORK;
718
719 ifp = XFS_IFORK_PTR(ip, whichfork);
720 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
721 for (idx = 0; idx < cnt; idx++)
722 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
723}
724
725/*
726 * Validate that the bmbt_irecs being returned from bmapi are valid
727 * given the callers original parameters. Specifically check the
728 * ranges of the returned irecs to ensure that they only extent beyond
729 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
730 */
731STATIC void
732xfs_bmap_validate_ret(
733 xfs_fileoff_t bno,
734 xfs_filblks_t len,
735 int flags,
736 xfs_bmbt_irec_t *mval,
737 int nmap,
738 int ret_nmap)
739{
740 int i; /* index to map values */
741
742 ASSERT(ret_nmap <= nmap);
743
744 for (i = 0; i < ret_nmap; i++) {
745 ASSERT(mval[i].br_blockcount > 0);
746 if (!(flags & XFS_BMAPI_ENTIRE)) {
747 ASSERT(mval[i].br_startoff >= bno);
748 ASSERT(mval[i].br_blockcount <= len);
749 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
750 bno + len);
751 } else {
752 ASSERT(mval[i].br_startoff < bno + len);
753 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
754 bno);
755 }
756 ASSERT(i == 0 ||
757 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
758 mval[i].br_startoff);
759 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
760 mval[i].br_startblock != HOLESTARTBLOCK);
761 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
762 mval[i].br_state == XFS_EXT_UNWRITTEN);
763 }
764}
765
766#else
767#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
768#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
769#endif /* DEBUG */
770
771/*
772 * bmap free list manipulation functions
773 */
774
775/*
776 * Add the extent to the list of extents to be free at transaction end.
777 * The list is maintained sorted (by block number).
778 */
779void
780xfs_bmap_add_free(
781 xfs_fsblock_t bno, /* fs block number of extent */
782 xfs_filblks_t len, /* length of extent */
783 xfs_bmap_free_t *flist, /* list of extents */
784 xfs_mount_t *mp) /* mount point structure */
785{
786 xfs_bmap_free_item_t *cur; /* current (next) element */
787 xfs_bmap_free_item_t *new; /* new element */
788 xfs_bmap_free_item_t *prev; /* previous element */
789#ifdef DEBUG
790 xfs_agnumber_t agno;
791 xfs_agblock_t agbno;
792
793 ASSERT(bno != NULLFSBLOCK);
794 ASSERT(len > 0);
795 ASSERT(len <= MAXEXTLEN);
796 ASSERT(!isnullstartblock(bno));
797 agno = XFS_FSB_TO_AGNO(mp, bno);
798 agbno = XFS_FSB_TO_AGBNO(mp, bno);
799 ASSERT(agno < mp->m_sb.sb_agcount);
800 ASSERT(agbno < mp->m_sb.sb_agblocks);
801 ASSERT(len < mp->m_sb.sb_agblocks);
802 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
803#endif
804 ASSERT(xfs_bmap_free_item_zone != NULL);
805 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
806 new->xbfi_startblock = bno;
807 new->xbfi_blockcount = (xfs_extlen_t)len;
808 for (prev = NULL, cur = flist->xbf_first;
809 cur != NULL;
810 prev = cur, cur = cur->xbfi_next) {
811 if (cur->xbfi_startblock >= bno)
812 break;
813 }
814 if (prev)
815 prev->xbfi_next = new;
816 else
817 flist->xbf_first = new;
818 new->xbfi_next = cur;
819 flist->xbf_count++;
820}
821
822/*
823 * Remove the entry "free" from the free item list. Prev points to the
824 * previous entry, unless "free" is the head of the list.
825 */
826STATIC void
827xfs_bmap_del_free(
828 xfs_bmap_free_t *flist, /* free item list header */
829 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
830 xfs_bmap_free_item_t *free) /* list item to be freed */
831{
832 if (prev)
833 prev->xbfi_next = free->xbfi_next;
834 else
835 flist->xbf_first = free->xbfi_next;
836 flist->xbf_count--;
837 kmem_zone_free(xfs_bmap_free_item_zone, free);
838}
839
840
841/*
842 * Routine to be called at transaction's end by xfs_bmapi, xfs_bunmapi
843 * caller. Frees all the extents that need freeing, which must be done
844 * last due to locking considerations. We never free any extents in
845 * the first transaction.
846 *
847 * Return 1 if the given transaction was committed and a new one
848 * started, and 0 otherwise in the committed parameter.
849 */
850int /* error */
851xfs_bmap_finish(
852 xfs_trans_t **tp, /* transaction pointer addr */
853 xfs_bmap_free_t *flist, /* i/o: list extents to free */
854 int *committed) /* xact committed or not */
855{
856 xfs_efd_log_item_t *efd; /* extent free data */
857 xfs_efi_log_item_t *efi; /* extent free intention */
858 int error; /* error return value */
859 xfs_bmap_free_item_t *free; /* free extent item */
860 unsigned int logres; /* new log reservation */
861 unsigned int logcount; /* new log count */
862 xfs_mount_t *mp; /* filesystem mount structure */
863 xfs_bmap_free_item_t *next; /* next item on free list */
864 xfs_trans_t *ntp; /* new transaction pointer */
865
866 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
867 if (flist->xbf_count == 0) {
868 *committed = 0;
869 return 0;
870 }
871 ntp = *tp;
872 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
873 for (free = flist->xbf_first; free; free = free->xbfi_next)
874 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
875 free->xbfi_blockcount);
876 logres = ntp->t_log_res;
877 logcount = ntp->t_log_count;
878 ntp = xfs_trans_dup(*tp);
879 error = xfs_trans_commit(*tp, 0);
880 *tp = ntp;
881 *committed = 1;
882 /*
883 * We have a new transaction, so we should return committed=1,
884 * even though we're returning an error.
885 */
886 if (error)
887 return error;
888
889 /*
890 * transaction commit worked ok so we can drop the extra ticket
891 * reference that we gained in xfs_trans_dup()
892 */
893 xfs_log_ticket_put(ntp->t_ticket);
894
895 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
896 logcount)))
897 return error;
898 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
899 for (free = flist->xbf_first; free != NULL; free = next) {
900 next = free->xbfi_next;
901 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
902 free->xbfi_blockcount))) {
903 /*
904 * The bmap free list will be cleaned up at a
905 * higher level. The EFI will be canceled when
906 * this transaction is aborted.
907 * Need to force shutdown here to make sure it
908 * happens, since this transaction may not be
909 * dirty yet.
910 */
911 mp = ntp->t_mountp;
912 if (!XFS_FORCED_SHUTDOWN(mp))
913 xfs_force_shutdown(mp,
914 (error == EFSCORRUPTED) ?
915 SHUTDOWN_CORRUPT_INCORE :
916 SHUTDOWN_META_IO_ERROR);
917 return error;
918 }
919 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
920 free->xbfi_blockcount);
921 xfs_bmap_del_free(flist, NULL, free);
922 }
923 return 0;
924}
925
926/*
927 * Free up any items left in the list.
928 */
929void
930xfs_bmap_cancel(
931 xfs_bmap_free_t *flist) /* list of bmap_free_items */
932{
933 xfs_bmap_free_item_t *free; /* free list item */
934 xfs_bmap_free_item_t *next;
935
936 if (flist->xbf_count == 0)
937 return;
938 ASSERT(flist->xbf_first != NULL);
939 for (free = flist->xbf_first; free; free = next) {
940 next = free->xbfi_next;
941 xfs_bmap_del_free(flist, NULL, free);
942 }
943 ASSERT(flist->xbf_count == 0);
944}
945
946/*
947 * Inode fork format manipulation functions
948 */
949
950/*
951 * Transform a btree format file with only one leaf node, where the
952 * extents list will fit in the inode, into an extents format file.
953 * Since the file extents are already in-core, all we have to do is
954 * give up the space for the btree root and pitch the leaf block.
955 */
956STATIC int /* error */
957xfs_bmap_btree_to_extents(
958 xfs_trans_t *tp, /* transaction pointer */
959 xfs_inode_t *ip, /* incore inode pointer */
960 xfs_btree_cur_t *cur, /* btree cursor */
961 int *logflagsp, /* inode logging flags */
962 int whichfork) /* data or attr fork */
963{
964 /* REFERENCED */
965 struct xfs_btree_block *cblock;/* child btree block */
966 xfs_fsblock_t cbno; /* child block number */
967 xfs_buf_t *cbp; /* child block's buffer */
968 int error; /* error return value */
969 xfs_ifork_t *ifp; /* inode fork data */
970 xfs_mount_t *mp; /* mount point structure */
971 __be64 *pp; /* ptr to block address */
972 struct xfs_btree_block *rblock;/* root btree block */
973
974 mp = ip->i_mount;
975 ifp = XFS_IFORK_PTR(ip, whichfork);
976 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
977 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
978 rblock = ifp->if_broot;
979 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
980 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
981 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
982 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
983 cbno = be64_to_cpu(*pp);
984 *logflagsp = 0;
985#ifdef DEBUG
986 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
987 return error;
988#endif
989 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
990 &xfs_bmbt_buf_ops);
991 if (error)
992 return error;
993 cblock = XFS_BUF_TO_BLOCK(cbp);
994 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
995 return error;
996 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
997 ip->i_d.di_nblocks--;
998 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
999 xfs_trans_binval(tp, cbp);
1000 if (cur->bc_bufs[0] == cbp)
1001 cur->bc_bufs[0] = NULL;
1002 xfs_iroot_realloc(ip, -1, whichfork);
1003 ASSERT(ifp->if_broot == NULL);
1004 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
1005 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
1006 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1007 return 0;
1008}
1009
1010/*
1011 * Convert an extents-format file into a btree-format file.
1012 * The new file will have a root block (in the inode) and a single child block.
1013 */
1014STATIC int /* error */
1015xfs_bmap_extents_to_btree(
1016 xfs_trans_t *tp, /* transaction pointer */
1017 xfs_inode_t *ip, /* incore inode pointer */
1018 xfs_fsblock_t *firstblock, /* first-block-allocated */
1019 xfs_bmap_free_t *flist, /* blocks freed in xaction */
1020 xfs_btree_cur_t **curp, /* cursor returned to caller */
1021 int wasdel, /* converting a delayed alloc */
1022 int *logflagsp, /* inode logging flags */
1023 int whichfork) /* data or attr fork */
1024{
1025 struct xfs_btree_block *ablock; /* allocated (child) bt block */
1026 xfs_buf_t *abp; /* buffer for ablock */
1027 xfs_alloc_arg_t args; /* allocation arguments */
1028 xfs_bmbt_rec_t *arp; /* child record pointer */
1029 struct xfs_btree_block *block; /* btree root block */
1030 xfs_btree_cur_t *cur; /* bmap btree cursor */
1031 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1032 int error; /* error return value */
1033 xfs_extnum_t i, cnt; /* extent record index */
1034 xfs_ifork_t *ifp; /* inode fork pointer */
1035 xfs_bmbt_key_t *kp; /* root block key pointer */
1036 xfs_mount_t *mp; /* mount structure */
1037 xfs_extnum_t nextents; /* number of file extents */
1038 xfs_bmbt_ptr_t *pp; /* root block address pointer */
1039
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001040 mp = ip->i_mount;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001041 ifp = XFS_IFORK_PTR(ip, whichfork);
1042 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
1043
1044 /*
1045 * Make space in the inode incore.
1046 */
1047 xfs_iroot_realloc(ip, 1, whichfork);
1048 ifp->if_flags |= XFS_IFBROOT;
1049
1050 /*
1051 * Fill in the root.
1052 */
1053 block = ifp->if_broot;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001054 if (xfs_sb_version_hascrc(&mp->m_sb))
1055 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
1056 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
1057 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
1058 else
1059 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
1060 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
1061 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001062
1063 /*
1064 * Need a cursor. Can't allocate until bb_level is filled in.
1065 */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001066 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
1067 cur->bc_private.b.firstblock = *firstblock;
1068 cur->bc_private.b.flist = flist;
1069 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
1070 /*
1071 * Convert to a btree with two levels, one record in root.
1072 */
1073 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
1074 memset(&args, 0, sizeof(args));
1075 args.tp = tp;
1076 args.mp = mp;
1077 args.firstblock = *firstblock;
1078 if (*firstblock == NULLFSBLOCK) {
1079 args.type = XFS_ALLOCTYPE_START_BNO;
1080 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
1081 } else if (flist->xbf_low) {
1082 args.type = XFS_ALLOCTYPE_START_BNO;
1083 args.fsbno = *firstblock;
1084 } else {
1085 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1086 args.fsbno = *firstblock;
1087 }
1088 args.minlen = args.maxlen = args.prod = 1;
1089 args.wasdel = wasdel;
1090 *logflagsp = 0;
1091 if ((error = xfs_alloc_vextent(&args))) {
1092 xfs_iroot_realloc(ip, -1, whichfork);
1093 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1094 return error;
1095 }
1096 /*
1097 * Allocation can't fail, the space was reserved.
1098 */
1099 ASSERT(args.fsbno != NULLFSBLOCK);
1100 ASSERT(*firstblock == NULLFSBLOCK ||
1101 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
1102 (flist->xbf_low &&
1103 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
1104 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
1105 cur->bc_private.b.allocated++;
1106 ip->i_d.di_nblocks++;
1107 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
1108 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
1109 /*
1110 * Fill in the child block.
1111 */
1112 abp->b_ops = &xfs_bmbt_buf_ops;
1113 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001114 if (xfs_sb_version_hascrc(&mp->m_sb))
1115 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
1116 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
1117 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
1118 else
1119 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
1120 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
1121 XFS_BTREE_LONG_PTRS);
1122
Dave Chinner9e5987a2013-02-25 12:31:26 +11001123 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
1124 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1125 for (cnt = i = 0; i < nextents; i++) {
1126 ep = xfs_iext_get_ext(ifp, i);
1127 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
1128 arp->l0 = cpu_to_be64(ep->l0);
1129 arp->l1 = cpu_to_be64(ep->l1);
1130 arp++; cnt++;
1131 }
1132 }
1133 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
1134 xfs_btree_set_numrecs(ablock, cnt);
1135
1136 /*
1137 * Fill in the root key and pointer.
1138 */
1139 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
1140 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
1141 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
1142 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
1143 be16_to_cpu(block->bb_level)));
1144 *pp = cpu_to_be64(args.fsbno);
1145
1146 /*
1147 * Do all this logging at the end so that
1148 * the root is at the right level.
1149 */
1150 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
1151 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
1152 ASSERT(*curp == NULL);
1153 *curp = cur;
1154 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
1155 return 0;
1156}
1157
1158/*
1159 * Convert a local file to an extents file.
1160 * This code is out of bounds for data forks of regular files,
1161 * since the file data needs to get logged so things will stay consistent.
1162 * (The bmap-level manipulations are ok, though).
1163 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001164void
1165xfs_bmap_local_to_extents_empty(
1166 struct xfs_inode *ip,
1167 int whichfork)
1168{
1169 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1170
1171 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1172 ASSERT(ifp->if_bytes == 0);
1173 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
1174
1175 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
1176 ifp->if_flags &= ~XFS_IFINLINE;
1177 ifp->if_flags |= XFS_IFEXTENTS;
1178 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
1179}
1180
1181
Dave Chinner9e5987a2013-02-25 12:31:26 +11001182STATIC int /* error */
1183xfs_bmap_local_to_extents(
1184 xfs_trans_t *tp, /* transaction pointer */
1185 xfs_inode_t *ip, /* incore inode pointer */
1186 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
1187 xfs_extlen_t total, /* total blocks needed by transaction */
1188 int *logflagsp, /* inode logging flags */
1189 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001190 void (*init_fn)(struct xfs_trans *tp,
1191 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001192 struct xfs_inode *ip,
1193 struct xfs_ifork *ifp))
1194{
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001195 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001196 int flags; /* logging flags returned */
1197 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001198 xfs_alloc_arg_t args; /* allocation arguments */
1199 xfs_buf_t *bp; /* buffer for extent block */
1200 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001201
1202 /*
1203 * We don't want to deal with the case of keeping inode data inline yet.
1204 * So sending the data fork of a regular inode is invalid.
1205 */
1206 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
1207 ifp = XFS_IFORK_PTR(ip, whichfork);
1208 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001209
1210 if (!ifp->if_bytes) {
1211 xfs_bmap_local_to_extents_empty(ip, whichfork);
1212 flags = XFS_ILOG_CORE;
1213 goto done;
1214 }
1215
Dave Chinner9e5987a2013-02-25 12:31:26 +11001216 flags = 0;
1217 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001218 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
1219 XFS_IFINLINE);
1220 memset(&args, 0, sizeof(args));
1221 args.tp = tp;
1222 args.mp = ip->i_mount;
1223 args.firstblock = *firstblock;
1224 /*
1225 * Allocate a block. We know we need only one, since the
1226 * file currently fits in an inode.
1227 */
1228 if (*firstblock == NULLFSBLOCK) {
1229 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
1230 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001231 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001232 args.fsbno = *firstblock;
1233 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001234 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001235 args.total = total;
1236 args.minlen = args.maxlen = args.prod = 1;
1237 error = xfs_alloc_vextent(&args);
1238 if (error)
1239 goto done;
1240
1241 /* Can't fail, the space was reserved. */
1242 ASSERT(args.fsbno != NULLFSBLOCK);
1243 ASSERT(args.len == 1);
1244 *firstblock = args.fsbno;
1245 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
1246
1247 /* initialise the block and copy the data */
1248 init_fn(tp, bp, ip, ifp);
1249
1250 /* account for the change in fork size and log everything */
1251 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
1252 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
1253 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001254 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001255
1256 xfs_iext_add(ifp, 0, 1);
1257 ep = xfs_iext_get_ext(ifp, 0);
1258 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
1259 trace_xfs_bmap_post_update(ip, 0,
1260 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
1261 _THIS_IP_);
1262 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
1263 ip->i_d.di_nblocks = 1;
1264 xfs_trans_mod_dquot_byino(tp, ip,
1265 XFS_TRANS_DQ_BCOUNT, 1L);
1266 flags |= xfs_ilog_fext(whichfork);
1267
Dave Chinner9e5987a2013-02-25 12:31:26 +11001268done:
1269 *logflagsp = flags;
1270 return error;
1271}
1272
1273/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 * Called from xfs_bmap_add_attrfork to handle btree format files.
1275 */
1276STATIC int /* error */
1277xfs_bmap_add_attrfork_btree(
1278 xfs_trans_t *tp, /* transaction pointer */
1279 xfs_inode_t *ip, /* incore inode pointer */
1280 xfs_fsblock_t *firstblock, /* first block allocated */
1281 xfs_bmap_free_t *flist, /* blocks to free at commit */
1282 int *flags) /* inode logging flags */
1283{
1284 xfs_btree_cur_t *cur; /* btree cursor */
1285 int error; /* error return value */
1286 xfs_mount_t *mp; /* file system mount struct */
1287 int stat; /* newroot status */
1288
1289 mp = ip->i_mount;
1290 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1291 *flags |= XFS_ILOG_DBROOT;
1292 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001293 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 cur->bc_private.b.flist = flist;
1295 cur->bc_private.b.firstblock = *firstblock;
1296 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1297 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001298 /* must be at least one entry */
1299 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001300 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 goto error0;
1302 if (stat == 0) {
1303 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1304 return XFS_ERROR(ENOSPC);
1305 }
1306 *firstblock = cur->bc_private.b.firstblock;
1307 cur->bc_private.b.allocated = 0;
1308 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1309 }
1310 return 0;
1311error0:
1312 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1313 return error;
1314}
1315
1316/*
1317 * Called from xfs_bmap_add_attrfork to handle extents format files.
1318 */
1319STATIC int /* error */
1320xfs_bmap_add_attrfork_extents(
1321 xfs_trans_t *tp, /* transaction pointer */
1322 xfs_inode_t *ip, /* incore inode pointer */
1323 xfs_fsblock_t *firstblock, /* first block allocated */
1324 xfs_bmap_free_t *flist, /* blocks to free at commit */
1325 int *flags) /* inode logging flags */
1326{
1327 xfs_btree_cur_t *cur; /* bmap btree cursor */
1328 int error; /* error return value */
1329
1330 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1331 return 0;
1332 cur = NULL;
1333 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
1334 flags, XFS_DATA_FORK);
1335 if (cur) {
1336 cur->bc_private.b.allocated = 0;
1337 xfs_btree_del_cursor(cur,
1338 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1339 }
1340 return error;
1341}
1342
1343/*
Dave Chinner1e823792013-02-11 15:58:13 +11001344 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1345 * different data fork content type needs a different callout to do the
1346 * conversion. Some are basic and only require special block initialisation
1347 * callouts for the data formating, others (directories) are so specialised they
1348 * handle everything themselves.
1349 *
1350 * XXX (dgc): investigate whether directory conversion can use the generic
1351 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001352 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
1354STATIC int /* error */
1355xfs_bmap_add_attrfork_local(
1356 xfs_trans_t *tp, /* transaction pointer */
1357 xfs_inode_t *ip, /* incore inode pointer */
1358 xfs_fsblock_t *firstblock, /* first block allocated */
1359 xfs_bmap_free_t *flist, /* blocks to free at commit */
1360 int *flags) /* inode logging flags */
1361{
1362 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
1364 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1365 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001366
Al Viroabbede12011-07-26 02:31:30 -04001367 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 memset(&dargs, 0, sizeof(dargs));
1369 dargs.dp = ip;
1370 dargs.firstblock = firstblock;
1371 dargs.flist = flist;
Dave Chinner1e823792013-02-11 15:58:13 +11001372 dargs.total = ip->i_mount->m_dirblkfsbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 dargs.whichfork = XFS_DATA_FORK;
1374 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001375 return xfs_dir2_sf_to_block(&dargs);
1376 }
1377
1378 if (S_ISLNK(ip->i_d.di_mode))
1379 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1380 flags, XFS_DATA_FORK,
1381 xfs_symlink_local_to_remote);
1382
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001383 /* should only be called for types that support local format data */
1384 ASSERT(0);
1385 return EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
1388/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001389 * Convert inode from non-attributed to attributed.
1390 * Must not be in a transaction, ip must not be locked.
1391 */
1392int /* error code */
1393xfs_bmap_add_attrfork(
1394 xfs_inode_t *ip, /* incore inode pointer */
1395 int size, /* space new attribute needs */
1396 int rsvd) /* xact may use reserved blks */
1397{
1398 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1399 xfs_bmap_free_t flist; /* freed extent records */
1400 xfs_mount_t *mp; /* mount structure */
1401 xfs_trans_t *tp; /* transaction pointer */
1402 int blks; /* space reservation */
1403 int version = 1; /* superblock attr version */
1404 int committed; /* xaction was committed */
1405 int logflags; /* logging flags */
1406 int error; /* error return value */
1407
1408 ASSERT(XFS_IFORK_Q(ip) == 0);
1409
1410 mp = ip->i_mount;
1411 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1412 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
1413 blks = XFS_ADDAFORK_SPACE_RES(mp);
1414 if (rsvd)
1415 tp->t_flags |= XFS_TRANS_RESERVE;
1416 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
1417 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
1418 goto error0;
1419 xfs_ilock(ip, XFS_ILOCK_EXCL);
1420 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1421 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1422 XFS_QMOPT_RES_REGBLKS);
1423 if (error) {
1424 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1425 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
1426 return error;
1427 }
1428 if (XFS_IFORK_Q(ip))
1429 goto error1;
1430 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1431 /*
1432 * For inodes coming from pre-6.2 filesystems.
1433 */
1434 ASSERT(ip->i_d.di_aformat == 0);
1435 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1436 }
1437 ASSERT(ip->i_d.di_anextents == 0);
1438
1439 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1440 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1441
1442 switch (ip->i_d.di_format) {
1443 case XFS_DINODE_FMT_DEV:
1444 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1445 break;
1446 case XFS_DINODE_FMT_UUID:
1447 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1448 break;
1449 case XFS_DINODE_FMT_LOCAL:
1450 case XFS_DINODE_FMT_EXTENTS:
1451 case XFS_DINODE_FMT_BTREE:
1452 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1453 if (!ip->i_d.di_forkoff)
1454 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1455 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1456 version = 2;
1457 break;
1458 default:
1459 ASSERT(0);
1460 error = XFS_ERROR(EINVAL);
1461 goto error1;
1462 }
1463
1464 ASSERT(ip->i_afp == NULL);
1465 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1466 ip->i_afp->if_flags = XFS_IFEXTENTS;
1467 logflags = 0;
1468 xfs_bmap_init(&flist, &firstblock);
1469 switch (ip->i_d.di_format) {
1470 case XFS_DINODE_FMT_LOCAL:
1471 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
1472 &logflags);
1473 break;
1474 case XFS_DINODE_FMT_EXTENTS:
1475 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1476 &flist, &logflags);
1477 break;
1478 case XFS_DINODE_FMT_BTREE:
1479 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
1480 &logflags);
1481 break;
1482 default:
1483 error = 0;
1484 break;
1485 }
1486 if (logflags)
1487 xfs_trans_log_inode(tp, ip, logflags);
1488 if (error)
1489 goto error2;
1490 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1491 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1492 __int64_t sbfields = 0;
1493
1494 spin_lock(&mp->m_sb_lock);
1495 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1496 xfs_sb_version_addattr(&mp->m_sb);
1497 sbfields |= XFS_SB_VERSIONNUM;
1498 }
1499 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1500 xfs_sb_version_addattr2(&mp->m_sb);
1501 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
1502 }
1503 if (sbfields) {
1504 spin_unlock(&mp->m_sb_lock);
1505 xfs_mod_sb(tp, sbfields);
1506 } else
1507 spin_unlock(&mp->m_sb_lock);
1508 }
1509
1510 error = xfs_bmap_finish(&tp, &flist, &committed);
1511 if (error)
1512 goto error2;
1513 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1514error2:
1515 xfs_bmap_cancel(&flist);
1516error1:
1517 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1518error0:
1519 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1520 return error;
1521}
1522
1523/*
1524 * Internal and external extent tree search functions.
1525 */
1526
1527/*
1528 * Read in the extents to if_extents.
1529 * All inode fields are set up by caller, we just traverse the btree
1530 * and copy the records in. If the file system cannot contain unwritten
1531 * extents, the records are checked for no "state" flags.
1532 */
1533int /* error */
1534xfs_bmap_read_extents(
1535 xfs_trans_t *tp, /* transaction pointer */
1536 xfs_inode_t *ip, /* incore inode */
1537 int whichfork) /* data or attr fork */
1538{
1539 struct xfs_btree_block *block; /* current btree block */
1540 xfs_fsblock_t bno; /* block # of "block" */
1541 xfs_buf_t *bp; /* buffer for "block" */
1542 int error; /* error return value */
1543 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1544 xfs_extnum_t i, j; /* index into the extents list */
1545 xfs_ifork_t *ifp; /* fork structure */
1546 int level; /* btree level, for checking */
1547 xfs_mount_t *mp; /* file system mount structure */
1548 __be64 *pp; /* pointer to block address */
1549 /* REFERENCED */
1550 xfs_extnum_t room; /* number of entries there's room for */
1551
1552 bno = NULLFSBLOCK;
1553 mp = ip->i_mount;
1554 ifp = XFS_IFORK_PTR(ip, whichfork);
1555 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1556 XFS_EXTFMT_INODE(ip);
1557 block = ifp->if_broot;
1558 /*
1559 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1560 */
1561 level = be16_to_cpu(block->bb_level);
1562 ASSERT(level > 0);
1563 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1564 bno = be64_to_cpu(*pp);
1565 ASSERT(bno != NULLDFSBNO);
1566 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1567 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1568 /*
1569 * Go down the tree until leaf level is reached, following the first
1570 * pointer (leftmost) at each level.
1571 */
1572 while (level-- > 0) {
1573 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1574 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1575 if (error)
1576 return error;
1577 block = XFS_BUF_TO_BLOCK(bp);
1578 XFS_WANT_CORRUPTED_GOTO(
1579 xfs_bmap_sanity_check(mp, bp, level),
1580 error0);
1581 if (level == 0)
1582 break;
1583 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1584 bno = be64_to_cpu(*pp);
1585 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
1586 xfs_trans_brelse(tp, bp);
1587 }
1588 /*
1589 * Here with bp and block set to the leftmost leaf node in the tree.
1590 */
1591 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1592 i = 0;
1593 /*
1594 * Loop over all leaf nodes. Copy information to the extent records.
1595 */
1596 for (;;) {
1597 xfs_bmbt_rec_t *frp;
1598 xfs_fsblock_t nextbno;
1599 xfs_extnum_t num_recs;
1600 xfs_extnum_t start;
1601
1602 num_recs = xfs_btree_get_numrecs(block);
1603 if (unlikely(i + num_recs > room)) {
1604 ASSERT(i + num_recs <= room);
1605 xfs_warn(ip->i_mount,
1606 "corrupt dinode %Lu, (btree extents).",
1607 (unsigned long long) ip->i_ino);
1608 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1609 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1610 goto error0;
1611 }
1612 XFS_WANT_CORRUPTED_GOTO(
1613 xfs_bmap_sanity_check(mp, bp, 0),
1614 error0);
1615 /*
1616 * Read-ahead the next leaf block, if any.
1617 */
1618 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1619 if (nextbno != NULLFSBLOCK)
1620 xfs_btree_reada_bufl(mp, nextbno, 1,
1621 &xfs_bmbt_buf_ops);
1622 /*
1623 * Copy records into the extent records.
1624 */
1625 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1626 start = i;
1627 for (j = 0; j < num_recs; j++, i++, frp++) {
1628 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1629 trp->l0 = be64_to_cpu(frp->l0);
1630 trp->l1 = be64_to_cpu(frp->l1);
1631 }
1632 if (exntf == XFS_EXTFMT_NOSTATE) {
1633 /*
1634 * Check all attribute bmap btree records and
1635 * any "older" data bmap btree records for a
1636 * set bit in the "extent flag" position.
1637 */
1638 if (unlikely(xfs_check_nostate_extents(ifp,
1639 start, num_recs))) {
1640 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1641 XFS_ERRLEVEL_LOW,
1642 ip->i_mount);
1643 goto error0;
1644 }
1645 }
1646 xfs_trans_brelse(tp, bp);
1647 bno = nextbno;
1648 /*
1649 * If we've reached the end, stop.
1650 */
1651 if (bno == NULLFSBLOCK)
1652 break;
1653 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1654 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1655 if (error)
1656 return error;
1657 block = XFS_BUF_TO_BLOCK(bp);
1658 }
1659 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1660 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1661 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1662 return 0;
1663error0:
1664 xfs_trans_brelse(tp, bp);
1665 return XFS_ERROR(EFSCORRUPTED);
1666}
1667
1668
1669/*
1670 * Search the extent records for the entry containing block bno.
1671 * If bno lies in a hole, point to the next entry. If bno lies
1672 * past eof, *eofp will be set, and *prevp will contain the last
1673 * entry (null if none). Else, *lastxp will be set to the index
1674 * of the found entry; *gotp will contain the entry.
1675 */
1676STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1677xfs_bmap_search_multi_extents(
1678 xfs_ifork_t *ifp, /* inode fork pointer */
1679 xfs_fileoff_t bno, /* block number searched for */
1680 int *eofp, /* out: end of file found */
1681 xfs_extnum_t *lastxp, /* out: last extent index */
1682 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1683 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1684{
1685 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1686 xfs_extnum_t lastx; /* last extent index */
1687
1688 /*
1689 * Initialize the extent entry structure to catch access to
1690 * uninitialized br_startblock field.
1691 */
1692 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1693 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1694 gotp->br_state = XFS_EXT_INVALID;
1695#if XFS_BIG_BLKNOS
1696 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
1697#else
1698 gotp->br_startblock = 0xffffa5a5;
1699#endif
1700 prevp->br_startoff = NULLFILEOFF;
1701
1702 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1703 if (lastx > 0) {
1704 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1705 }
1706 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1707 xfs_bmbt_get_all(ep, gotp);
1708 *eofp = 0;
1709 } else {
1710 if (lastx > 0) {
1711 *gotp = *prevp;
1712 }
1713 *eofp = 1;
1714 ep = NULL;
1715 }
1716 *lastxp = lastx;
1717 return ep;
1718}
1719
1720/*
1721 * Search the extents list for the inode, for the extent containing bno.
1722 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1723 * *eofp will be set, and *prevp will contain the last entry (null if none).
1724 * Else, *lastxp will be set to the index of the found
1725 * entry; *gotp will contain the entry.
1726 */
1727STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1728xfs_bmap_search_extents(
1729 xfs_inode_t *ip, /* incore inode pointer */
1730 xfs_fileoff_t bno, /* block number searched for */
1731 int fork, /* data or attr fork */
1732 int *eofp, /* out: end of file found */
1733 xfs_extnum_t *lastxp, /* out: last extent index */
1734 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1735 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1736{
1737 xfs_ifork_t *ifp; /* inode fork pointer */
1738 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1739
1740 XFS_STATS_INC(xs_look_exlist);
1741 ifp = XFS_IFORK_PTR(ip, fork);
1742
1743 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1744
1745 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1746 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1747 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1748 "Access to block zero in inode %llu "
1749 "start_block: %llx start_off: %llx "
1750 "blkcnt: %llx extent-state: %x lastx: %x\n",
1751 (unsigned long long)ip->i_ino,
1752 (unsigned long long)gotp->br_startblock,
1753 (unsigned long long)gotp->br_startoff,
1754 (unsigned long long)gotp->br_blockcount,
1755 gotp->br_state, *lastxp);
1756 *lastxp = NULLEXTNUM;
1757 *eofp = 1;
1758 return NULL;
1759 }
1760 return ep;
1761}
1762
1763/*
1764 * Returns the file-relative block number of the first unused block(s)
1765 * in the file with at least "len" logically contiguous blocks free.
1766 * This is the lowest-address hole if the file has holes, else the first block
1767 * past the end of file.
1768 * Return 0 if the file is currently local (in-inode).
1769 */
1770int /* error */
1771xfs_bmap_first_unused(
1772 xfs_trans_t *tp, /* transaction pointer */
1773 xfs_inode_t *ip, /* incore inode */
1774 xfs_extlen_t len, /* size of hole to find */
1775 xfs_fileoff_t *first_unused, /* unused block */
1776 int whichfork) /* data or attr fork */
1777{
1778 int error; /* error return value */
1779 int idx; /* extent record index */
1780 xfs_ifork_t *ifp; /* inode fork pointer */
1781 xfs_fileoff_t lastaddr; /* last block number seen */
1782 xfs_fileoff_t lowest; /* lowest useful block */
1783 xfs_fileoff_t max; /* starting useful block */
1784 xfs_fileoff_t off; /* offset for this block */
1785 xfs_extnum_t nextents; /* number of extent entries */
1786
1787 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1788 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1789 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1790 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1791 *first_unused = 0;
1792 return 0;
1793 }
1794 ifp = XFS_IFORK_PTR(ip, whichfork);
1795 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1796 (error = xfs_iread_extents(tp, ip, whichfork)))
1797 return error;
1798 lowest = *first_unused;
1799 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1800 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1801 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1802 off = xfs_bmbt_get_startoff(ep);
1803 /*
1804 * See if the hole before this extent will work.
1805 */
1806 if (off >= lowest + len && off - max >= len) {
1807 *first_unused = max;
1808 return 0;
1809 }
1810 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1811 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1812 }
1813 *first_unused = max;
1814 return 0;
1815}
1816
1817/*
1818 * Returns the file-relative block number of the last block + 1 before
1819 * last_block (input value) in the file.
1820 * This is not based on i_size, it is based on the extent records.
1821 * Returns 0 for local files, as they do not have extent records.
1822 */
1823int /* error */
1824xfs_bmap_last_before(
1825 xfs_trans_t *tp, /* transaction pointer */
1826 xfs_inode_t *ip, /* incore inode */
1827 xfs_fileoff_t *last_block, /* last block */
1828 int whichfork) /* data or attr fork */
1829{
1830 xfs_fileoff_t bno; /* input file offset */
1831 int eof; /* hit end of file */
1832 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1833 int error; /* error return value */
1834 xfs_bmbt_irec_t got; /* current extent value */
1835 xfs_ifork_t *ifp; /* inode fork pointer */
1836 xfs_extnum_t lastx; /* last extent used */
1837 xfs_bmbt_irec_t prev; /* previous extent value */
1838
1839 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1840 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1841 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
1842 return XFS_ERROR(EIO);
1843 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1844 *last_block = 0;
1845 return 0;
1846 }
1847 ifp = XFS_IFORK_PTR(ip, whichfork);
1848 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1849 (error = xfs_iread_extents(tp, ip, whichfork)))
1850 return error;
1851 bno = *last_block - 1;
1852 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1853 &prev);
1854 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1855 if (prev.br_startoff == NULLFILEOFF)
1856 *last_block = 0;
1857 else
1858 *last_block = prev.br_startoff + prev.br_blockcount;
1859 }
1860 /*
1861 * Otherwise *last_block is already the right answer.
1862 */
1863 return 0;
1864}
1865
1866STATIC int
1867xfs_bmap_last_extent(
1868 struct xfs_trans *tp,
1869 struct xfs_inode *ip,
1870 int whichfork,
1871 struct xfs_bmbt_irec *rec,
1872 int *is_empty)
1873{
1874 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1875 int error;
1876 int nextents;
1877
1878 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1879 error = xfs_iread_extents(tp, ip, whichfork);
1880 if (error)
1881 return error;
1882 }
1883
1884 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1885 if (nextents == 0) {
1886 *is_empty = 1;
1887 return 0;
1888 }
1889
1890 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1891 *is_empty = 0;
1892 return 0;
1893}
1894
1895/*
1896 * Check the last inode extent to determine whether this allocation will result
1897 * in blocks being allocated at the end of the file. When we allocate new data
1898 * blocks at the end of the file which do not start at the previous data block,
1899 * we will try to align the new blocks at stripe unit boundaries.
1900 *
1901 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
1902 * at, or past the EOF.
1903 */
1904STATIC int
1905xfs_bmap_isaeof(
1906 struct xfs_bmalloca *bma,
1907 int whichfork)
1908{
1909 struct xfs_bmbt_irec rec;
1910 int is_empty;
1911 int error;
1912
1913 bma->aeof = 0;
1914 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1915 &is_empty);
1916 if (error || is_empty)
1917 return error;
1918
1919 /*
1920 * Check if we are allocation or past the last extent, or at least into
1921 * the last delayed allocated extent.
1922 */
1923 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1924 (bma->offset >= rec.br_startoff &&
1925 isnullstartblock(rec.br_startblock));
1926 return 0;
1927}
1928
1929/*
1930 * Check if the endoff is outside the last extent. If so the caller will grow
1931 * the allocation to a stripe unit boundary. All offsets are considered outside
1932 * the end of file for an empty fork, so 1 is returned in *eof in that case.
1933 */
1934int
1935xfs_bmap_eof(
1936 struct xfs_inode *ip,
1937 xfs_fileoff_t endoff,
1938 int whichfork,
1939 int *eof)
1940{
1941 struct xfs_bmbt_irec rec;
1942 int error;
1943
1944 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
1945 if (error || *eof)
1946 return error;
1947
1948 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
1949 return 0;
1950}
1951
1952/*
1953 * Returns the file-relative block number of the first block past eof in
1954 * the file. This is not based on i_size, it is based on the extent records.
1955 * Returns 0 for local files, as they do not have extent records.
1956 */
1957int
1958xfs_bmap_last_offset(
1959 struct xfs_trans *tp,
1960 struct xfs_inode *ip,
1961 xfs_fileoff_t *last_block,
1962 int whichfork)
1963{
1964 struct xfs_bmbt_irec rec;
1965 int is_empty;
1966 int error;
1967
1968 *last_block = 0;
1969
1970 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1971 return 0;
1972
1973 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1974 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1975 return XFS_ERROR(EIO);
1976
1977 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1978 if (error || is_empty)
1979 return error;
1980
1981 *last_block = rec.br_startoff + rec.br_blockcount;
1982 return 0;
1983}
1984
1985/*
1986 * Returns whether the selected fork of the inode has exactly one
1987 * block or not. For the data fork we check this matches di_size,
1988 * implying the file's range is 0..bsize-1.
1989 */
1990int /* 1=>1 block, 0=>otherwise */
1991xfs_bmap_one_block(
1992 xfs_inode_t *ip, /* incore inode */
1993 int whichfork) /* data or attr fork */
1994{
1995 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1996 xfs_ifork_t *ifp; /* inode fork pointer */
1997 int rval; /* return value */
1998 xfs_bmbt_irec_t s; /* internal version of extent */
1999
2000#ifndef DEBUG
2001 if (whichfork == XFS_DATA_FORK)
2002 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
2003#endif /* !DEBUG */
2004 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
2005 return 0;
2006 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
2007 return 0;
2008 ifp = XFS_IFORK_PTR(ip, whichfork);
2009 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2010 ep = xfs_iext_get_ext(ifp, 0);
2011 xfs_bmbt_get_all(ep, &s);
2012 rval = s.br_startoff == 0 && s.br_blockcount == 1;
2013 if (rval && whichfork == XFS_DATA_FORK)
2014 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
2015 return rval;
2016}
2017
2018/*
2019 * Extent tree manipulation functions used during allocation.
2020 */
2021
2022/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002023 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 */
2025STATIC int /* error */
2026xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002027 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002029 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002031 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002034 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 xfs_fileoff_t new_endoff; /* end offset of new entry */
2036 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2037 /* left is 0, right is 1, prev is 2 */
2038 int rval=0; /* return value (logging flags) */
2039 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002040 xfs_filblks_t da_new; /* new count del alloc blocks used */
2041 xfs_filblks_t da_old; /* old count del alloc blocks used */
2042 xfs_filblks_t temp=0; /* value for da_new calculations */
2043 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002046 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002047
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002048 ASSERT(bma->idx >= 0);
2049 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002050 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002051 ASSERT(!bma->cur ||
2052 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002053
2054 XFS_STATS_INC(xs_add_exlist);
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056#define LEFT r[0]
2057#define RIGHT r[1]
2058#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 /*
2061 * Set up a bunch of variables to make the tests simpler.
2062 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002063 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 xfs_bmbt_get_all(ep, &PREV);
2065 new_endoff = new->br_startoff + new->br_blockcount;
2066 ASSERT(PREV.br_startoff <= new->br_startoff);
2067 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002068
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002069 da_old = startblockval(PREV.br_startblock);
2070 da_new = 0;
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 /*
2073 * Set flags determining what part of the previous delayed allocation
2074 * extent is being replaced by a real allocation.
2075 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002076 if (PREV.br_startoff == new->br_startoff)
2077 state |= BMAP_LEFT_FILLING;
2078 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2079 state |= BMAP_RIGHT_FILLING;
2080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 /*
2082 * Check and set flags if this segment has a left neighbor.
2083 * Don't set contiguous if the combined extent would be too large.
2084 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002085 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002086 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002087 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002088
2089 if (isnullstartblock(LEFT.br_startblock))
2090 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002092
2093 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2094 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2095 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2096 LEFT.br_state == new->br_state &&
2097 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2098 state |= BMAP_LEFT_CONTIG;
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 /*
2101 * Check and set flags if this segment has a right neighbor.
2102 * Don't set contiguous if the combined extent would be too large.
2103 * Also check for all-three-contiguous being too large.
2104 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002105 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002106 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002107 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002108
2109 if (isnullstartblock(RIGHT.br_startblock))
2110 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002112
2113 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2114 new_endoff == RIGHT.br_startoff &&
2115 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2116 new->br_state == RIGHT.br_state &&
2117 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2118 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2119 BMAP_RIGHT_FILLING)) !=
2120 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2121 BMAP_RIGHT_FILLING) ||
2122 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2123 <= MAXEXTLEN))
2124 state |= BMAP_RIGHT_CONTIG;
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 error = 0;
2127 /*
2128 * Switch out based on the FILLING and CONTIG state bits.
2129 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002130 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2131 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2132 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2133 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 /*
2135 * Filling in all of a previously delayed allocation extent.
2136 * The left and right neighbors are both contiguous with new.
2137 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002138 bma->idx--;
2139 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2140 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 LEFT.br_blockcount + PREV.br_blockcount +
2142 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002143 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002144
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002145 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
2146 bma->ip->i_d.di_nextents--;
2147 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2149 else {
2150 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002151 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002153 RIGHT.br_blockcount, &i);
2154 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002156 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002157 error = xfs_btree_delete(bma->cur, &i);
2158 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002160 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002161 error = xfs_btree_decrement(bma->cur, 0, &i);
2162 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002164 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002165 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 LEFT.br_startblock,
2167 LEFT.br_blockcount +
2168 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002169 RIGHT.br_blockcount, LEFT.br_state);
2170 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 goto done;
2172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 break;
2174
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002175 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /*
2177 * Filling in all of a previously delayed allocation extent.
2178 * The left neighbor is contiguous, the right is not.
2179 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002180 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002181
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002182 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2183 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00002184 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002185 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002186
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002187 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
2188 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 rval = XFS_ILOG_DEXT;
2190 else {
2191 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002192 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002194 &i);
2195 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002197 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002198 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 LEFT.br_startblock,
2200 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002201 PREV.br_blockcount, LEFT.br_state);
2202 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 goto done;
2204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 break;
2206
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002207 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 /*
2209 * Filling in all of a previously delayed allocation extent.
2210 * The right neighbor is contiguous, the left is not.
2211 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002212 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 xfs_bmbt_set_startblock(ep, new->br_startblock);
2214 xfs_bmbt_set_blockcount(ep,
2215 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002216 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002217
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002218 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
2219 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 rval = XFS_ILOG_DEXT;
2221 else {
2222 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002223 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002225 RIGHT.br_blockcount, &i);
2226 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002228 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002229 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 new->br_startblock,
2231 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002232 RIGHT.br_blockcount, PREV.br_state);
2233 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 goto done;
2235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 break;
2237
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002238 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 /*
2240 * Filling in all of a previously delayed allocation extent.
2241 * Neither the left nor right neighbors are contiguous with
2242 * the new one.
2243 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002244 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002246 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002247
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002248 bma->ip->i_d.di_nextents++;
2249 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2251 else {
2252 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002253 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002255 &i);
2256 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002258 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002259 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2260 error = xfs_btree_insert(bma->cur, &i);
2261 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002263 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 break;
2266
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002267 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 /*
2269 * Filling in the first part of a previous delayed allocation.
2270 * The left neighbor is contiguous.
2271 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002272 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
2273 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 LEFT.br_blockcount + new->br_blockcount);
2275 xfs_bmbt_set_startoff(ep,
2276 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002277 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002278
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002280 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002282 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 rval = XFS_ILOG_DEXT;
2284 else {
2285 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002286 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002288 &i);
2289 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002291 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002292 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 LEFT.br_startblock,
2294 LEFT.br_blockcount +
2295 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002296 LEFT.br_state);
2297 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 goto done;
2299 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002300 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002301 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002302 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002303 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002304
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002305 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 break;
2307
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002308 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 /*
2310 * Filling in the first part of a previous delayed allocation.
2311 * The left neighbor is not contiguous.
2312 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002313 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 xfs_bmbt_set_startoff(ep, new_endoff);
2315 temp = PREV.br_blockcount - new->br_blockcount;
2316 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002317 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
2318 bma->ip->i_d.di_nextents++;
2319 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2321 else {
2322 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002323 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002325 &i);
2326 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002328 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002329 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2330 error = xfs_btree_insert(bma->cur, &i);
2331 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002333 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002335
2336 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002337 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2338 bma->firstblock, bma->flist,
2339 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 rval |= tmp_rval;
2341 if (error)
2342 goto done;
2343 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002344 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002345 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002346 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2347 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002348 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002349 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 break;
2351
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002352 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 /*
2354 * Filling in the last part of a previous delayed allocation.
2355 * The right neighbor is contiguous with the new allocation.
2356 */
2357 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002358 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002360 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002361 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 new->br_blockcount + RIGHT.br_blockcount,
2363 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002364 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2365 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 rval = XFS_ILOG_DEXT;
2367 else {
2368 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002369 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002371 RIGHT.br_blockcount, &i);
2372 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002374 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002375 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 new->br_startblock,
2377 new->br_blockcount +
2378 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002379 RIGHT.br_state);
2380 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 goto done;
2382 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002383
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002384 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002385 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002386 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002387 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002388 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002389
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002390 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 break;
2392
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002393 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 /*
2395 * Filling in the last part of a previous delayed allocation.
2396 * The right neighbor is not contiguous.
2397 */
2398 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002399 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002401 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2402 bma->ip->i_d.di_nextents++;
2403 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2405 else {
2406 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002407 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002409 &i);
2410 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002412 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002413 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2414 error = xfs_btree_insert(bma->cur, &i);
2415 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002417 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002419
2420 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002421 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2422 bma->firstblock, bma->flist, &bma->cur, 1,
2423 &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 rval |= tmp_rval;
2425 if (error)
2426 goto done;
2427 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002428 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002429 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002430 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2431 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002432 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002433 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002434
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002435 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 break;
2437
2438 case 0:
2439 /*
2440 * Filling in the middle part of a previous delayed allocation.
2441 * Contiguity is impossible here.
2442 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002443 *
2444 * We start with a delayed allocation:
2445 *
2446 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2447 * PREV @ idx
2448 *
2449 * and we are allocating:
2450 * +rrrrrrrrrrrrrrrrr+
2451 * new
2452 *
2453 * and we set it up for insertion as:
2454 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2455 * new
2456 * PREV @ idx LEFT RIGHT
2457 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 */
2459 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002461 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002462 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2463 LEFT = *new;
2464 RIGHT.br_state = PREV.br_state;
2465 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002466 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002467 RIGHT.br_startoff = new_endoff;
2468 RIGHT.br_blockcount = temp2;
2469 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002470 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2471 bma->ip->i_d.di_nextents++;
2472 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2474 else {
2475 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002476 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002478 &i);
2479 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002481 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002482 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2483 error = xfs_btree_insert(bma->cur, &i);
2484 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002486 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002488
2489 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002490 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2491 bma->firstblock, bma->flist, &bma->cur,
2492 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 rval |= tmp_rval;
2494 if (error)
2495 goto done;
2496 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002497 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2498 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002499 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002500 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002501 if (diff > 0) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002502 error = xfs_icsb_modify_counters(bma->ip->i_mount,
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002503 XFS_SBS_FDBLOCKS,
2504 -((int64_t)diff), 0);
2505 ASSERT(!error);
2506 if (error)
2507 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002509
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002510 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002511 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002512 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2513 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2514 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002515 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002516 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002517
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002518 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002519 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 break;
2521
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002522 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2523 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2524 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2525 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2526 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2527 case BMAP_LEFT_CONTIG:
2528 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 /*
2530 * These cases are all impossible.
2531 */
2532 ASSERT(0);
2533 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002534
2535 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002536 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002537 int tmp_logflags; /* partial log flag return val */
2538
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002539 ASSERT(bma->cur == NULL);
2540 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2541 bma->firstblock, bma->flist, &bma->cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002542 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002543 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002544 if (error)
2545 goto done;
2546 }
2547
2548 /* adjust for changes in reserved delayed indirect blocks */
2549 if (da_old || da_new) {
2550 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002551 if (bma->cur)
2552 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002553 ASSERT(temp <= da_old);
2554 if (temp < da_old)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002555 xfs_icsb_modify_counters(bma->ip->i_mount,
2556 XFS_SBS_FDBLOCKS,
2557 (int64_t)(da_old - temp), 0);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002558 }
2559
2560 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002561 if (bma->cur)
2562 bma->cur->bc_private.b.allocated = 0;
2563
2564 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002566 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 return error;
2568#undef LEFT
2569#undef RIGHT
2570#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
2573/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002574 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 */
2576STATIC int /* error */
2577xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002578 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002580 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002582 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002583 xfs_fsblock_t *first, /* pointer to firstblock variable */
2584 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002585 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002588 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002591 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 xfs_fileoff_t new_endoff; /* end offset of new entry */
2593 xfs_exntst_t newext; /* new extent state */
2594 xfs_exntst_t oldext; /* old extent state */
2595 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2596 /* left is 0, right is 1, prev is 2 */
2597 int rval=0; /* return value (logging flags) */
2598 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002600 *logflagsp = 0;
2601
2602 cur = *curp;
2603 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2604
2605 ASSERT(*idx >= 0);
2606 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2607 ASSERT(!isnullstartblock(new->br_startblock));
2608
2609 XFS_STATS_INC(xs_add_exlist);
2610
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611#define LEFT r[0]
2612#define RIGHT r[1]
2613#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 /*
2616 * Set up a bunch of variables to make the tests simpler.
2617 */
2618 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002619 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 xfs_bmbt_get_all(ep, &PREV);
2621 newext = new->br_state;
2622 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2623 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2624 ASSERT(PREV.br_state == oldext);
2625 new_endoff = new->br_startoff + new->br_blockcount;
2626 ASSERT(PREV.br_startoff <= new->br_startoff);
2627 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 /*
2630 * Set flags determining what part of the previous oldext allocation
2631 * extent is being replaced by a newext allocation.
2632 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002633 if (PREV.br_startoff == new->br_startoff)
2634 state |= BMAP_LEFT_FILLING;
2635 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2636 state |= BMAP_RIGHT_FILLING;
2637
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 /*
2639 * Check and set flags if this segment has a left neighbor.
2640 * Don't set contiguous if the combined extent would be too large.
2641 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002642 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002643 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002644 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002645
2646 if (isnullstartblock(LEFT.br_startblock))
2647 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002649
2650 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2651 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2652 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2653 LEFT.br_state == newext &&
2654 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2655 state |= BMAP_LEFT_CONTIG;
2656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 /*
2658 * Check and set flags if this segment has a right neighbor.
2659 * Don't set contiguous if the combined extent would be too large.
2660 * Also check for all-three-contiguous being too large.
2661 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002662 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002663 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002664 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002665 if (isnullstartblock(RIGHT.br_startblock))
2666 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002668
2669 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2670 new_endoff == RIGHT.br_startoff &&
2671 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2672 newext == RIGHT.br_state &&
2673 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2674 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2675 BMAP_RIGHT_FILLING)) !=
2676 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2677 BMAP_RIGHT_FILLING) ||
2678 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2679 <= MAXEXTLEN))
2680 state |= BMAP_RIGHT_CONTIG;
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 /*
2683 * Switch out based on the FILLING and CONTIG state bits.
2684 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002685 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2686 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2687 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2688 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 /*
2690 * Setting all of a previous oldext extent to newext.
2691 * The left and right neighbors are both contiguous with new.
2692 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002693 --*idx;
2694
2695 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2696 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 LEFT.br_blockcount + PREV.br_blockcount +
2698 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002699 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002700
Christoph Hellwigec90c552011-05-23 08:52:53 +00002701 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 ip->i_d.di_nextents -= 2;
2703 if (cur == NULL)
2704 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2705 else {
2706 rval = XFS_ILOG_CORE;
2707 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2708 RIGHT.br_startblock,
2709 RIGHT.br_blockcount, &i)))
2710 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002711 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002712 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002714 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002715 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002717 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002718 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002720 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002721 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002723 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2725 LEFT.br_startblock,
2726 LEFT.br_blockcount + PREV.br_blockcount +
2727 RIGHT.br_blockcount, LEFT.br_state)))
2728 goto done;
2729 }
2730 break;
2731
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002732 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 /*
2734 * Setting all of a previous oldext extent to newext.
2735 * The left neighbor is contiguous, the right is not.
2736 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002737 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002738
Christoph Hellwigec90c552011-05-23 08:52:53 +00002739 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2740 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2741 LEFT.br_blockcount + PREV.br_blockcount);
2742 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2743
2744 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 ip->i_d.di_nextents--;
2746 if (cur == NULL)
2747 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2748 else {
2749 rval = XFS_ILOG_CORE;
2750 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2751 PREV.br_startblock, PREV.br_blockcount,
2752 &i)))
2753 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002754 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002755 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002757 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002758 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002760 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2762 LEFT.br_startblock,
2763 LEFT.br_blockcount + PREV.br_blockcount,
2764 LEFT.br_state)))
2765 goto done;
2766 }
2767 break;
2768
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002769 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /*
2771 * Setting all of a previous oldext extent to newext.
2772 * The right neighbor is contiguous, the left is not.
2773 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002774 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 xfs_bmbt_set_blockcount(ep,
2776 PREV.br_blockcount + RIGHT.br_blockcount);
2777 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002778 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2779 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 ip->i_d.di_nextents--;
2781 if (cur == NULL)
2782 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2783 else {
2784 rval = XFS_ILOG_CORE;
2785 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2786 RIGHT.br_startblock,
2787 RIGHT.br_blockcount, &i)))
2788 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002789 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002790 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002792 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002793 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002795 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2797 new->br_startblock,
2798 new->br_blockcount + RIGHT.br_blockcount,
2799 newext)))
2800 goto done;
2801 }
2802 break;
2803
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002804 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 /*
2806 * Setting all of a previous oldext extent to newext.
2807 * Neither the left nor right neighbors are contiguous with
2808 * the new one.
2809 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002810 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002812 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (cur == NULL)
2815 rval = XFS_ILOG_DEXT;
2816 else {
2817 rval = 0;
2818 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2819 new->br_startblock, new->br_blockcount,
2820 &i)))
2821 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002822 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2824 new->br_startblock, new->br_blockcount,
2825 newext)))
2826 goto done;
2827 }
2828 break;
2829
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002830 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 /*
2832 * Setting the first part of a previous oldext extent to newext.
2833 * The left neighbor is contiguous.
2834 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002835 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2836 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 LEFT.br_blockcount + new->br_blockcount);
2838 xfs_bmbt_set_startoff(ep,
2839 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002840 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002841
Christoph Hellwigec90c552011-05-23 08:52:53 +00002842 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 xfs_bmbt_set_startblock(ep,
2844 new->br_startblock + new->br_blockcount);
2845 xfs_bmbt_set_blockcount(ep,
2846 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002847 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002848
Christoph Hellwigec90c552011-05-23 08:52:53 +00002849 --*idx;
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 if (cur == NULL)
2852 rval = XFS_ILOG_DEXT;
2853 else {
2854 rval = 0;
2855 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2856 PREV.br_startblock, PREV.br_blockcount,
2857 &i)))
2858 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002859 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 if ((error = xfs_bmbt_update(cur,
2861 PREV.br_startoff + new->br_blockcount,
2862 PREV.br_startblock + new->br_blockcount,
2863 PREV.br_blockcount - new->br_blockcount,
2864 oldext)))
2865 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002866 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002868 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 LEFT.br_startblock,
2870 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002871 LEFT.br_state);
2872 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 goto done;
2874 }
2875 break;
2876
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002877 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 /*
2879 * Setting the first part of a previous oldext extent to newext.
2880 * The left neighbor is not contiguous.
2881 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002882 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2884 xfs_bmbt_set_startoff(ep, new_endoff);
2885 xfs_bmbt_set_blockcount(ep,
2886 PREV.br_blockcount - new->br_blockcount);
2887 xfs_bmbt_set_startblock(ep,
2888 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002889 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002890
Christoph Hellwigec90c552011-05-23 08:52:53 +00002891 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 ip->i_d.di_nextents++;
2893 if (cur == NULL)
2894 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2895 else {
2896 rval = XFS_ILOG_CORE;
2897 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2898 PREV.br_startblock, PREV.br_blockcount,
2899 &i)))
2900 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002901 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 if ((error = xfs_bmbt_update(cur,
2903 PREV.br_startoff + new->br_blockcount,
2904 PREV.br_startblock + new->br_blockcount,
2905 PREV.br_blockcount - new->br_blockcount,
2906 oldext)))
2907 goto done;
2908 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002909 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002911 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
2913 break;
2914
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002915 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 /*
2917 * Setting the last part of a previous oldext extent to newext.
2918 * The right neighbor is contiguous with the new allocation.
2919 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002920 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 xfs_bmbt_set_blockcount(ep,
2922 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002923 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2924
2925 ++*idx;
2926
2927 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2928 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002929 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002931 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (cur == NULL)
2934 rval = XFS_ILOG_DEXT;
2935 else {
2936 rval = 0;
2937 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2938 PREV.br_startblock,
2939 PREV.br_blockcount, &i)))
2940 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002941 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2943 PREV.br_startblock,
2944 PREV.br_blockcount - new->br_blockcount,
2945 oldext)))
2946 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002947 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 goto done;
2949 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2950 new->br_startblock,
2951 new->br_blockcount + RIGHT.br_blockcount,
2952 newext)))
2953 goto done;
2954 }
2955 break;
2956
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002957 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 /*
2959 * Setting the last part of a previous oldext extent to newext.
2960 * The right neighbor is not contiguous.
2961 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002962 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 xfs_bmbt_set_blockcount(ep,
2964 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002965 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002966
Christoph Hellwigec90c552011-05-23 08:52:53 +00002967 ++*idx;
2968 xfs_iext_insert(ip, *idx, 1, new, state);
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 ip->i_d.di_nextents++;
2971 if (cur == NULL)
2972 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2973 else {
2974 rval = XFS_ILOG_CORE;
2975 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2976 PREV.br_startblock, PREV.br_blockcount,
2977 &i)))
2978 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002979 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2981 PREV.br_startblock,
2982 PREV.br_blockcount - new->br_blockcount,
2983 oldext)))
2984 goto done;
2985 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2986 new->br_startblock, new->br_blockcount,
2987 &i)))
2988 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002989 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002991 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002993 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
2995 break;
2996
2997 case 0:
2998 /*
2999 * Setting the middle part of a previous oldext extent to
3000 * newext. Contiguity is impossible here.
3001 * One extent becomes three extents.
3002 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003003 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 xfs_bmbt_set_blockcount(ep,
3005 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003006 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 r[0] = *new;
3009 r[1].br_startoff = new_endoff;
3010 r[1].br_blockcount =
3011 PREV.br_startoff + PREV.br_blockcount - new_endoff;
3012 r[1].br_startblock = new->br_startblock + new->br_blockcount;
3013 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003014
3015 ++*idx;
3016 xfs_iext_insert(ip, *idx, 2, &r[0], state);
3017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 ip->i_d.di_nextents += 2;
3019 if (cur == NULL)
3020 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
3021 else {
3022 rval = XFS_ILOG_CORE;
3023 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
3024 PREV.br_startblock, PREV.br_blockcount,
3025 &i)))
3026 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003027 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 /* new right extent - oldext */
3029 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
3030 r[1].br_startblock, r[1].br_blockcount,
3031 r[1].br_state)))
3032 goto done;
3033 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10003035 cur->bc_rec.b.br_blockcount =
3036 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003037 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003039 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10003040 /*
3041 * Reset the cursor to the position of the new extent
3042 * we are about to insert as we can't trust it after
3043 * the previous insert.
3044 */
3045 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
3046 new->br_startblock, new->br_blockcount,
3047 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10003049 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10003051 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003052 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003054 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 }
3056 break;
3057
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003058 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3059 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3060 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
3061 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
3062 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3063 case BMAP_LEFT_CONTIG:
3064 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 /*
3066 * These cases are all impossible.
3067 */
3068 ASSERT(0);
3069 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003070
3071 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003072 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003073 int tmp_logflags; /* partial log flag return val */
3074
3075 ASSERT(cur == NULL);
3076 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
3077 0, &tmp_logflags, XFS_DATA_FORK);
3078 *logflagsp |= tmp_logflags;
3079 if (error)
3080 goto done;
3081 }
3082
3083 /* clear out the allocated field, done with it now in any case. */
3084 if (cur) {
3085 cur->bc_private.b.allocated = 0;
3086 *curp = cur;
3087 }
3088
3089 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003091 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 return error;
3093#undef LEFT
3094#undef RIGHT
3095#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
3098/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00003099 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00003101STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102xfs_bmap_add_extent_hole_delay(
3103 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003104 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00003105 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003107 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 xfs_bmbt_irec_t left; /* left neighbor extent entry */
3109 xfs_filblks_t newlen=0; /* new indirect size */
3110 xfs_filblks_t oldlen=0; /* old indirect size */
3111 xfs_bmbt_irec_t right; /* right neighbor extent entry */
3112 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003113 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003115 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003117 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 /*
3120 * Check and set flags if this segment has a left neighbor
3121 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003122 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003123 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003124 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003125
3126 if (isnullstartblock(left.br_startblock))
3127 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 /*
3131 * Check and set flags if the current (right) segment exists.
3132 * If it doesn't exist, we're converting the hole at end-of-file.
3133 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003134 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003135 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003136 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003137
3138 if (isnullstartblock(right.br_startblock))
3139 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003141
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 /*
3143 * Set contiguity flags on the left and right neighbors.
3144 * Don't let extents get too large, even if the pieces are contiguous.
3145 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003146 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
3147 left.br_startoff + left.br_blockcount == new->br_startoff &&
3148 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
3149 state |= BMAP_LEFT_CONTIG;
3150
3151 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
3152 new->br_startoff + new->br_blockcount == right.br_startoff &&
3153 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3154 (!(state & BMAP_LEFT_CONTIG) ||
3155 (left.br_blockcount + new->br_blockcount +
3156 right.br_blockcount <= MAXEXTLEN)))
3157 state |= BMAP_RIGHT_CONTIG;
3158
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159 /*
3160 * Switch out based on the contiguity flags.
3161 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003162 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3163 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 /*
3165 * New allocation is contiguous with delayed allocations
3166 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003167 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003169 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 temp = left.br_blockcount + new->br_blockcount +
3171 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003172
Christoph Hellwigec90c552011-05-23 08:52:53 +00003173 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
3174 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003175 oldlen = startblockval(left.br_startblock) +
3176 startblockval(new->br_startblock) +
3177 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003179 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06003180 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00003181 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003182
Christoph Hellwigec90c552011-05-23 08:52:53 +00003183 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 break;
3185
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003186 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 /*
3188 * New allocation is contiguous with a delayed allocation
3189 * on the left.
3190 * Merge the new allocation with the left neighbor.
3191 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003192 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003194
3195 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
3196 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003197 oldlen = startblockval(left.br_startblock) +
3198 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003200 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06003201 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00003202 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 break;
3204
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003205 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 /*
3207 * New allocation is contiguous with a delayed allocation
3208 * on the right.
3209 * Merge the new allocation with the right neighbor.
3210 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003211 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003213 oldlen = startblockval(new->br_startblock) +
3214 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003216 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
3217 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003218 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003219 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 break;
3221
3222 case 0:
3223 /*
3224 * New allocation is not contiguous with another
3225 * delayed allocation.
3226 * Insert a new entry.
3227 */
3228 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003229 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 break;
3231 }
3232 if (oldlen != newlen) {
3233 ASSERT(oldlen > newlen);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003234 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00003235 (int64_t)(oldlen - newlen), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 /*
3237 * Nothing to do for disk quota accounting here.
3238 */
3239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
3242/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003243 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 */
3245STATIC int /* error */
3246xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00003247 struct xfs_bmalloca *bma,
3248 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Christoph Hellwigc6534242011-09-18 20:41:05 +00003250 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 int i; /* temp state */
3253 xfs_ifork_t *ifp; /* inode fork pointer */
3254 xfs_bmbt_irec_t left; /* left neighbor extent entry */
3255 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003256 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258
Christoph Hellwigc6534242011-09-18 20:41:05 +00003259 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003260
Christoph Hellwigc6534242011-09-18 20:41:05 +00003261 ASSERT(bma->idx >= 0);
3262 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003263 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00003264 ASSERT(!bma->cur ||
3265 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003266
3267 XFS_STATS_INC(xs_add_exlist);
3268
3269 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003270 if (whichfork == XFS_ATTR_FORK)
3271 state |= BMAP_ATTRFORK;
3272
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 /*
3274 * Check and set flags if this segment has a left neighbor.
3275 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003276 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003277 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003278 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003279 if (isnullstartblock(left.br_startblock))
3280 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 /*
3284 * Check and set flags if this segment has a current value.
3285 * Not true if we're inserting into the "hole" at eof.
3286 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003287 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003288 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003289 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003290 if (isnullstartblock(right.br_startblock))
3291 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003293
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 /*
3295 * We're inserting a real allocation between "left" and "right".
3296 * Set the contiguity flags. Don't let extents get too large.
3297 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003298 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
3299 left.br_startoff + left.br_blockcount == new->br_startoff &&
3300 left.br_startblock + left.br_blockcount == new->br_startblock &&
3301 left.br_state == new->br_state &&
3302 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
3303 state |= BMAP_LEFT_CONTIG;
3304
3305 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
3306 new->br_startoff + new->br_blockcount == right.br_startoff &&
3307 new->br_startblock + new->br_blockcount == right.br_startblock &&
3308 new->br_state == right.br_state &&
3309 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3310 (!(state & BMAP_LEFT_CONTIG) ||
3311 left.br_blockcount + new->br_blockcount +
3312 right.br_blockcount <= MAXEXTLEN))
3313 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003315 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316 /*
3317 * Select which case we're in here, and implement it.
3318 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003319 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3320 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 /*
3322 * New allocation is contiguous with real allocations on the
3323 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003324 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003326 --bma->idx;
3327 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3328 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 left.br_blockcount + new->br_blockcount +
3330 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003331 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003332
Christoph Hellwigc6534242011-09-18 20:41:05 +00003333 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003334
Christoph Hellwigc6534242011-09-18 20:41:05 +00003335 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3336 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3337 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003338 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003339 } else {
3340 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003341 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3342 right.br_startblock, right.br_blockcount,
3343 &i);
3344 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003345 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003346 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003347 error = xfs_btree_delete(bma->cur, &i);
3348 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003349 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003350 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003351 error = xfs_btree_decrement(bma->cur, 0, &i);
3352 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003353 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003354 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003355 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003356 left.br_startblock,
3357 left.br_blockcount +
3358 new->br_blockcount +
3359 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003360 left.br_state);
3361 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003362 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003364 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003366 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 /*
3368 * New allocation is contiguous with a real allocation
3369 * on the left.
3370 * Merge the new allocation with the left neighbor.
3371 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003372 --bma->idx;
3373 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3374 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003376 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003377
Christoph Hellwigc6534242011-09-18 20:41:05 +00003378 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003379 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003380 } else {
3381 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003382 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3383 left.br_startblock, left.br_blockcount,
3384 &i);
3385 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003386 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003387 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003388 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003389 left.br_startblock,
3390 left.br_blockcount +
3391 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003392 left.br_state);
3393 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003394 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003396 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003398 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 /*
3400 * New allocation is contiguous with a real allocation
3401 * on the right.
3402 * Merge the new allocation with the right neighbor.
3403 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003404 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3405 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003406 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 new->br_blockcount + right.br_blockcount,
3408 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003409 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003410
Christoph Hellwigc6534242011-09-18 20:41:05 +00003411 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003412 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003413 } else {
3414 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003415 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003416 right.br_startoff,
3417 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003418 right.br_blockcount, &i);
3419 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003420 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003421 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003422 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003423 new->br_startblock,
3424 new->br_blockcount +
3425 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003426 right.br_state);
3427 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003428 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003430 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
3432 case 0:
3433 /*
3434 * New allocation is not contiguous with another
3435 * real allocation.
3436 * Insert a new entry.
3437 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003438 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3439 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3440 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3441 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003442 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003443 } else {
3444 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003445 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003446 new->br_startoff,
3447 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003448 new->br_blockcount, &i);
3449 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003450 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003451 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003452 bma->cur->bc_rec.b.br_state = new->br_state;
3453 error = xfs_btree_insert(bma->cur, &i);
3454 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003455 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003456 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003458 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003460
3461 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003462 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003463 int tmp_logflags; /* partial log flag return val */
3464
Christoph Hellwigc6534242011-09-18 20:41:05 +00003465 ASSERT(bma->cur == NULL);
3466 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3467 bma->firstblock, bma->flist, &bma->cur,
3468 0, &tmp_logflags, whichfork);
3469 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003470 if (error)
3471 goto done;
3472 }
3473
3474 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003475 if (bma->cur)
3476 bma->cur->bc_private.b.allocated = 0;
3477
3478 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003479done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003480 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003481 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482}
3483
Nathan Scottdd9f4382006-01-11 15:28:28 +11003484/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003485 * Functions used in the extent read, allocate and remove paths
3486 */
3487
3488/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003489 * Adjust the size of the new extent based on di_extsize and rt extsize.
3490 */
3491STATIC int
3492xfs_bmap_extsize_align(
3493 xfs_mount_t *mp,
3494 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3495 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3496 xfs_extlen_t extsz, /* align to this extent size */
3497 int rt, /* is this a realtime inode? */
3498 int eof, /* is extent at end-of-file? */
3499 int delay, /* creating delalloc extent? */
3500 int convert, /* overwriting unwritten extent? */
3501 xfs_fileoff_t *offp, /* in/out: aligned offset */
3502 xfs_extlen_t *lenp) /* in/out: aligned length */
3503{
3504 xfs_fileoff_t orig_off; /* original offset */
3505 xfs_extlen_t orig_alen; /* original length */
3506 xfs_fileoff_t orig_end; /* original off+len */
3507 xfs_fileoff_t nexto; /* next file offset */
3508 xfs_fileoff_t prevo; /* previous file offset */
3509 xfs_fileoff_t align_off; /* temp for offset */
3510 xfs_extlen_t align_alen; /* temp for length */
3511 xfs_extlen_t temp; /* temp for calculations */
3512
3513 if (convert)
3514 return 0;
3515
3516 orig_off = align_off = *offp;
3517 orig_alen = align_alen = *lenp;
3518 orig_end = orig_off + orig_alen;
3519
3520 /*
3521 * If this request overlaps an existing extent, then don't
3522 * attempt to perform any additional alignment.
3523 */
3524 if (!delay && !eof &&
3525 (orig_off >= gotp->br_startoff) &&
3526 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3527 return 0;
3528 }
3529
3530 /*
3531 * If the file offset is unaligned vs. the extent size
3532 * we need to align it. This will be possible unless
3533 * the file was previously written with a kernel that didn't
3534 * perform this alignment, or if a truncate shot us in the
3535 * foot.
3536 */
3537 temp = do_mod(orig_off, extsz);
3538 if (temp) {
3539 align_alen += temp;
3540 align_off -= temp;
3541 }
3542 /*
3543 * Same adjustment for the end of the requested area.
3544 */
3545 if ((temp = (align_alen % extsz))) {
3546 align_alen += extsz - temp;
3547 }
3548 /*
3549 * If the previous block overlaps with this proposed allocation
3550 * then move the start forward without adjusting the length.
3551 */
3552 if (prevp->br_startoff != NULLFILEOFF) {
3553 if (prevp->br_startblock == HOLESTARTBLOCK)
3554 prevo = prevp->br_startoff;
3555 else
3556 prevo = prevp->br_startoff + prevp->br_blockcount;
3557 } else
3558 prevo = 0;
3559 if (align_off != orig_off && align_off < prevo)
3560 align_off = prevo;
3561 /*
3562 * If the next block overlaps with this proposed allocation
3563 * then move the start back without adjusting the length,
3564 * but not before offset 0.
3565 * This may of course make the start overlap previous block,
3566 * and if we hit the offset 0 limit then the next block
3567 * can still overlap too.
3568 */
3569 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3570 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3571 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3572 nexto = gotp->br_startoff + gotp->br_blockcount;
3573 else
3574 nexto = gotp->br_startoff;
3575 } else
3576 nexto = NULLFILEOFF;
3577 if (!eof &&
3578 align_off + align_alen != orig_end &&
3579 align_off + align_alen > nexto)
3580 align_off = nexto > align_alen ? nexto - align_alen : 0;
3581 /*
3582 * If we're now overlapping the next or previous extent that
3583 * means we can't fit an extsz piece in this hole. Just move
3584 * the start forward to the first valid spot and set
3585 * the length so we hit the end.
3586 */
3587 if (align_off != orig_off && align_off < prevo)
3588 align_off = prevo;
3589 if (align_off + align_alen != orig_end &&
3590 align_off + align_alen > nexto &&
3591 nexto != NULLFILEOFF) {
3592 ASSERT(nexto > prevo);
3593 align_alen = nexto - align_off;
3594 }
3595
3596 /*
3597 * If realtime, and the result isn't a multiple of the realtime
3598 * extent size we need to remove blocks until it is.
3599 */
3600 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3601 /*
3602 * We're not covering the original request, or
3603 * we won't be able to once we fix the length.
3604 */
3605 if (orig_off < align_off ||
3606 orig_end > align_off + align_alen ||
3607 align_alen - temp < orig_alen)
3608 return XFS_ERROR(EINVAL);
3609 /*
3610 * Try to fix it by moving the start up.
3611 */
3612 if (align_off + temp <= orig_off) {
3613 align_alen -= temp;
3614 align_off += temp;
3615 }
3616 /*
3617 * Try to fix it by moving the end in.
3618 */
3619 else if (align_off + align_alen - temp >= orig_end)
3620 align_alen -= temp;
3621 /*
3622 * Set the start to the minimum then trim the length.
3623 */
3624 else {
3625 align_alen -= orig_off - align_off;
3626 align_off = orig_off;
3627 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3628 }
3629 /*
3630 * Result doesn't cover the request, fail it.
3631 */
3632 if (orig_off < align_off || orig_end > align_off + align_alen)
3633 return XFS_ERROR(EINVAL);
3634 } else {
3635 ASSERT(orig_off >= align_off);
3636 ASSERT(orig_end <= align_off + align_alen);
3637 }
3638
3639#ifdef DEBUG
3640 if (!eof && gotp->br_startoff != NULLFILEOFF)
3641 ASSERT(align_off + align_alen <= gotp->br_startoff);
3642 if (prevp->br_startoff != NULLFILEOFF)
3643 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3644#endif
3645
3646 *lenp = align_alen;
3647 *offp = align_off;
3648 return 0;
3649}
3650
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651#define XFS_ALLOC_GAP_UNITS 4
3652
David Chinnerc2b1cba2008-04-10 12:21:40 +10003653STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003654xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
3656{
3657 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3659 xfs_mount_t *mp; /* mount point structure */
3660 int nullfb; /* true if ap->firstblock isn't set */
3661 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662
3663#define ISVALID(x,y) \
3664 (rt ? \
3665 (x) < mp->m_sb.sb_rblocks : \
3666 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3667 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3668 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003671 nullfb = *ap->firstblock == NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003673 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 /*
3675 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003676 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003678 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3679 !isnullstartblock(ap->prev.br_startblock) &&
3680 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3681 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003682 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 /*
3684 * Adjust for the gap between prevp and us.
3685 */
Dave Chinner3a756672011-09-18 20:40:58 +00003686 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003687 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003689 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3690 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 }
3692 /*
3693 * If not at eof, then compare the two neighbor blocks.
3694 * Figure out whether either one gives us a good starting point,
3695 * and pick the better one.
3696 */
3697 else if (!ap->eof) {
3698 xfs_fsblock_t gotbno; /* right side block number */
3699 xfs_fsblock_t gotdiff=0; /* right side difference */
3700 xfs_fsblock_t prevbno; /* left side block number */
3701 xfs_fsblock_t prevdiff=0; /* left side difference */
3702
3703 /*
3704 * If there's a previous (left) block, select a requested
3705 * start block based on it.
3706 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003707 if (ap->prev.br_startoff != NULLFILEOFF &&
3708 !isnullstartblock(ap->prev.br_startblock) &&
3709 (prevbno = ap->prev.br_startblock +
3710 ap->prev.br_blockcount) &&
3711 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 /*
3713 * Calculate gap to end of previous block.
3714 */
Dave Chinner3a756672011-09-18 20:40:58 +00003715 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003716 (ap->prev.br_startoff +
3717 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 /*
3719 * Figure the startblock based on the previous block's
3720 * end and the gap size.
3721 * Heuristic!
3722 * If the gap is large relative to the piece we're
3723 * allocating, or using it gives us an invalid block
3724 * number, then just use the end of the previous block.
3725 */
Dave Chinner3a756672011-09-18 20:40:58 +00003726 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003728 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 prevbno += adjust;
3730 else
3731 prevdiff += adjust;
3732 /*
3733 * If the firstblock forbids it, can't use it,
3734 * must use default.
3735 */
3736 if (!rt && !nullfb &&
3737 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3738 prevbno = NULLFSBLOCK;
3739 }
3740 /*
3741 * No previous block or can't follow it, just default.
3742 */
3743 else
3744 prevbno = NULLFSBLOCK;
3745 /*
3746 * If there's a following (right) block, select a requested
3747 * start block based on it.
3748 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003749 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 /*
3751 * Calculate gap to start of next block.
3752 */
Dave Chinner3a756672011-09-18 20:40:58 +00003753 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 /*
3755 * Figure the startblock based on the next block's
3756 * start and the gap size.
3757 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003758 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 /*
3760 * Heuristic!
3761 * If the gap is large relative to the piece we're
3762 * allocating, or using it gives us an invalid block
3763 * number, then just use the start of the next block
3764 * offset by our length.
3765 */
Dave Chinner3a756672011-09-18 20:40:58 +00003766 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 ISVALID(gotbno - gotdiff, gotbno))
3768 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003769 else if (ISVALID(gotbno - ap->length, gotbno)) {
3770 gotbno -= ap->length;
3771 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 } else
3773 gotdiff += adjust;
3774 /*
3775 * If the firstblock forbids it, can't use it,
3776 * must use default.
3777 */
3778 if (!rt && !nullfb &&
3779 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3780 gotbno = NULLFSBLOCK;
3781 }
3782 /*
3783 * No next block, just default.
3784 */
3785 else
3786 gotbno = NULLFSBLOCK;
3787 /*
3788 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003789 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 */
3791 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003792 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003794 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003796 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003798#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003799}
3800
3801STATIC int
3802xfs_bmap_rtalloc(
3803 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
3804{
3805 xfs_alloctype_t atype = 0; /* type for allocation routines */
3806 int error; /* error return value */
3807 xfs_mount_t *mp; /* mount point structure */
3808 xfs_extlen_t prod = 0; /* product factor for allocators */
3809 xfs_extlen_t ralen = 0; /* realtime allocation length */
3810 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003811 xfs_rtblock_t rtb;
3812
3813 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10003814 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003815 prod = align / mp->m_sb.sb_rextsize;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003816 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003817 align, 1, ap->eof, 0,
Dave Chinner3a756672011-09-18 20:40:58 +00003818 ap->conv, &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003819 if (error)
3820 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00003821 ASSERT(ap->length);
3822 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003823
3824 /*
3825 * If the offset & length are not perfectly aligned
3826 * then kill prod, it will just get us in trouble.
3827 */
Dave Chinner3a756672011-09-18 20:40:58 +00003828 if (do_mod(ap->offset, align) || ap->length % align)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003829 prod = 1;
3830 /*
3831 * Set ralen to be the actual requested length in rtextents.
3832 */
Dave Chinner3a756672011-09-18 20:40:58 +00003833 ralen = ap->length / mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003834 /*
3835 * If the old value was close enough to MAXEXTLEN that
3836 * we rounded up to it, cut it back so it's valid again.
3837 * Note that if it's a really large request (bigger than
3838 * MAXEXTLEN), we don't hear about that number, and can't
3839 * adjust the starting point to match it.
3840 */
3841 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
3842 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
Christoph Hellwig04e99452011-01-25 09:06:19 +00003843
3844 /*
3845 * Lock out other modifications to the RT bitmap inode.
3846 */
Christoph Hellwig1050c712011-02-13 13:25:31 +00003847 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00003848 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwig04e99452011-01-25 09:06:19 +00003849
Nathan Scotta365bdd2006-03-14 13:34:16 +11003850 /*
3851 * If it's an allocation to an empty file at offset 0,
3852 * pick an extent that will space things out in the rt area.
3853 */
Dave Chinner3a756672011-09-18 20:40:58 +00003854 if (ap->eof && ap->offset == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10003855 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
3856
Nathan Scotta365bdd2006-03-14 13:34:16 +11003857 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
3858 if (error)
3859 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00003860 ap->blkno = rtx * mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003861 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003862 ap->blkno = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003863 }
3864
3865 xfs_bmap_adjacent(ap);
3866
3867 /*
3868 * Realtime allocation, done through xfs_rtallocate_extent.
3869 */
Dave Chinner3a756672011-09-18 20:40:58 +00003870 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
3871 do_div(ap->blkno, mp->m_sb.sb_rextsize);
3872 rtb = ap->blkno;
3873 ap->length = ralen;
3874 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003875 &ralen, atype, ap->wasdel, prod, &rtb)))
3876 return error;
3877 if (rtb == NULLFSBLOCK && prod > 1 &&
Dave Chinner3a756672011-09-18 20:40:58 +00003878 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
3879 ap->length, &ralen, atype,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003880 ap->wasdel, 1, &rtb)))
3881 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00003882 ap->blkno = rtb;
3883 if (ap->blkno != NULLFSBLOCK) {
3884 ap->blkno *= mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003885 ralen *= mp->m_sb.sb_rextsize;
Dave Chinner3a756672011-09-18 20:40:58 +00003886 ap->length = ralen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003887 ap->ip->i_d.di_nblocks += ralen;
3888 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3889 if (ap->wasdel)
3890 ap->ip->i_delayed_blks -= ralen;
3891 /*
3892 * Adjust the disk quota also. This was reserved
3893 * earlier.
3894 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003895 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003896 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
3897 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
3898 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003899 ap->length = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003900 }
3901 return 0;
3902}
3903
3904STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003905xfs_bmap_btalloc_nullfb(
3906 struct xfs_bmalloca *ap,
3907 struct xfs_alloc_arg *args,
3908 xfs_extlen_t *blen)
3909{
3910 struct xfs_mount *mp = ap->ip->i_mount;
3911 struct xfs_perag *pag;
3912 xfs_agnumber_t ag, startag;
3913 int notinit = 0;
3914 int error;
3915
3916 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3917 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3918 else
3919 args->type = XFS_ALLOCTYPE_START_BNO;
3920 args->total = ap->total;
3921
3922 /*
3923 * Search for an allocation group with a single extent large enough
3924 * for the request. If one isn't found, then adjust the minimum
3925 * allocation size to the largest space found.
3926 */
3927 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3928 if (startag == NULLAGNUMBER)
3929 startag = ag = 0;
3930
3931 pag = xfs_perag_get(mp, ag);
Dave Chinner14b064c2011-01-27 12:16:28 +11003932 while (*blen < args->maxlen) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00003933 if (!pag->pagf_init) {
3934 error = xfs_alloc_pagf_init(mp, args->tp, ag,
3935 XFS_ALLOC_FLAG_TRYLOCK);
3936 if (error) {
3937 xfs_perag_put(pag);
3938 return error;
3939 }
3940 }
3941
3942 /*
3943 * See xfs_alloc_fix_freelist...
3944 */
3945 if (pag->pagf_init) {
3946 xfs_extlen_t longest;
3947 longest = xfs_alloc_longest_free_extent(mp, pag);
3948 if (*blen < longest)
3949 *blen = longest;
3950 } else
3951 notinit = 1;
3952
3953 if (xfs_inode_is_filestream(ap->ip)) {
Dave Chinner14b064c2011-01-27 12:16:28 +11003954 if (*blen >= args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00003955 break;
3956
3957 if (ap->userdata) {
3958 /*
3959 * If startag is an invalid AG, we've
3960 * come here once before and
3961 * xfs_filestream_new_ag picked the
3962 * best currently available.
3963 *
3964 * Don't continue looping, since we
3965 * could loop forever.
3966 */
3967 if (startag == NULLAGNUMBER)
3968 break;
3969
3970 error = xfs_filestream_new_ag(ap, &ag);
3971 xfs_perag_put(pag);
3972 if (error)
3973 return error;
3974
3975 /* loop again to set 'blen'*/
3976 startag = NULLAGNUMBER;
3977 pag = xfs_perag_get(mp, ag);
3978 continue;
3979 }
3980 }
3981 if (++ag == mp->m_sb.sb_agcount)
3982 ag = 0;
3983 if (ag == startag)
3984 break;
3985 xfs_perag_put(pag);
3986 pag = xfs_perag_get(mp, ag);
3987 }
3988 xfs_perag_put(pag);
3989
3990 /*
3991 * Since the above loop did a BUF_TRYLOCK, it is
3992 * possible that there is space for this request.
3993 */
3994 if (notinit || *blen < ap->minlen)
3995 args->minlen = ap->minlen;
3996 /*
3997 * If the best seen length is less than the request
3998 * length, use the best as the minimum.
3999 */
Dave Chinner14b064c2011-01-27 12:16:28 +11004000 else if (*blen < args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00004001 args->minlen = *blen;
4002 /*
Dave Chinner14b064c2011-01-27 12:16:28 +11004003 * Otherwise we've seen an extent as big as maxlen,
Christoph Hellwigc467c042010-02-15 23:34:42 +00004004 * use that as the minimum.
4005 */
4006 else
Dave Chinner14b064c2011-01-27 12:16:28 +11004007 args->minlen = args->maxlen;
Christoph Hellwigc467c042010-02-15 23:34:42 +00004008
4009 /*
4010 * set the failure fallback case to look in the selected
4011 * AG as the stream may have moved.
4012 */
4013 if (xfs_inode_is_filestream(ap->ip))
Dave Chinner3a756672011-09-18 20:40:58 +00004014 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00004015
4016 return 0;
4017}
4018
4019STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11004020xfs_bmap_btalloc(
4021 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
4022{
4023 xfs_mount_t *mp; /* mount point structure */
4024 xfs_alloctype_t atype = 0; /* type for allocation routines */
4025 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11004026 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00004027 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004028 xfs_alloc_arg_t args;
4029 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004030 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004031 int nullfb; /* true if ap->firstblock isn't set */
4032 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004033 int tryagain;
4034 int error;
4035
Dave Chinnera99ebf42011-12-01 11:24:20 +00004036 ASSERT(ap->length);
4037
Nathan Scotta365bdd2006-03-14 13:34:16 +11004038 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10004039 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004040 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004041 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11004042 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00004043 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11004044 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00004045 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11004046 }
Dave Chinner0937e0f2011-09-18 20:40:57 +00004047 nullfb = *ap->firstblock == NULLFSBLOCK;
4048 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10004049 if (nullfb) {
4050 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
4051 ag = xfs_filestream_lookup_ag(ap->ip);
4052 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00004053 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10004054 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00004055 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10004056 }
4057 } else
Dave Chinner3a756672011-09-18 20:40:58 +00004058 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004059
4060 xfs_bmap_adjacent(ap);
4061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 /*
Dave Chinner3a756672011-09-18 20:40:58 +00004063 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 * it's in the right allocation group.
4065 */
Dave Chinner3a756672011-09-18 20:40:58 +00004066 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 ;
4068 else
Dave Chinner3a756672011-09-18 20:40:58 +00004069 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 * Normal allocation, done through xfs_alloc_vextent.
4072 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11004073 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05004074 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11004075 args.tp = ap->tp;
4076 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00004077 args.fsbno = ap->blkno;
Dave Chinner14b064c2011-01-27 12:16:28 +11004078
4079 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner3a756672011-09-18 20:40:58 +00004080 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
Dave Chinner0937e0f2011-09-18 20:40:57 +00004081 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004082 blen = 0;
4083 if (nullfb) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00004084 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
4085 if (error)
4086 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004087 } else if (ap->flist->xbf_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10004088 if (xfs_inode_is_filestream(ap->ip))
4089 args.type = XFS_ALLOCTYPE_FIRST_AG;
4090 else
4091 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004092 args.total = args.minlen = ap->minlen;
4093 } else {
4094 args.type = XFS_ALLOCTYPE_NEAR_BNO;
4095 args.total = ap->total;
4096 args.minlen = ap->minlen;
4097 }
David Chinner957d0eb2007-06-18 16:50:37 +10004098 /* apply extent size hints if obtained earlier */
4099 if (unlikely(align)) {
4100 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00004101 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11004102 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11004103 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11004104 args.prod = 1;
4105 args.mod = 0;
4106 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11004107 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00004108 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11004109 args.mod = (xfs_extlen_t)(args.prod - args.mod);
4110 }
4111 /*
4112 * If we are not low on available data blocks, and the
4113 * underlying logical volume manager is a stripe, and
4114 * the file offset is zero then try to allocate data
4115 * blocks on stripe unit boundary.
4116 * NOTE: ap->aeof is only set if the allocation length
4117 * is >= the stripe unit and the allocation offset is
4118 * at the end of file.
4119 */
Dave Chinner0937e0f2011-09-18 20:40:57 +00004120 if (!ap->flist->xbf_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00004121 if (!ap->offset) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11004122 args.alignment = mp->m_dalign;
4123 atype = args.type;
4124 isaligned = 1;
4125 /*
4126 * Adjust for alignment
4127 */
Dave Chinner14b064c2011-01-27 12:16:28 +11004128 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11004129 args.minlen = blen - args.alignment;
4130 args.minalignslop = 0;
4131 } else {
4132 /*
4133 * First try an exact bno allocation.
4134 * If it fails then do a near or start bno
4135 * allocation with alignment turned on.
4136 */
4137 atype = args.type;
4138 tryagain = 1;
4139 args.type = XFS_ALLOCTYPE_THIS_BNO;
4140 args.alignment = 1;
4141 /*
4142 * Compute the minlen+alignment for the
4143 * next case. Set slop so that the value
4144 * of minlen+alignment+slop doesn't go up
4145 * between the calls.
4146 */
Dave Chinner14b064c2011-01-27 12:16:28 +11004147 if (blen > mp->m_dalign && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11004148 nextminlen = blen - mp->m_dalign;
4149 else
4150 nextminlen = args.minlen;
4151 if (nextminlen + mp->m_dalign > args.minlen + 1)
4152 args.minalignslop =
4153 nextminlen + mp->m_dalign -
4154 args.minlen - 1;
4155 else
4156 args.minalignslop = 0;
4157 }
4158 } else {
4159 args.alignment = 1;
4160 args.minalignslop = 0;
4161 }
4162 args.minleft = ap->minleft;
4163 args.wasdel = ap->wasdel;
4164 args.isfl = 0;
4165 args.userdata = ap->userdata;
4166 if ((error = xfs_alloc_vextent(&args)))
4167 return error;
4168 if (tryagain && args.fsbno == NULLFSBLOCK) {
4169 /*
4170 * Exact allocation failed. Now try with alignment
4171 * turned on.
4172 */
4173 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00004174 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004175 args.alignment = mp->m_dalign;
4176 args.minlen = nextminlen;
4177 args.minalignslop = 0;
4178 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 if ((error = xfs_alloc_vextent(&args)))
4180 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004181 }
4182 if (isaligned && args.fsbno == NULLFSBLOCK) {
4183 /*
4184 * allocation failed, so turn off alignment and
4185 * try again.
4186 */
4187 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00004188 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004189 args.alignment = 0;
4190 if ((error = xfs_alloc_vextent(&args)))
4191 return error;
4192 }
4193 if (args.fsbno == NULLFSBLOCK && nullfb &&
4194 args.minlen > ap->minlen) {
4195 args.minlen = ap->minlen;
4196 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00004197 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004198 if ((error = xfs_alloc_vextent(&args)))
4199 return error;
4200 }
4201 if (args.fsbno == NULLFSBLOCK && nullfb) {
4202 args.fsbno = 0;
4203 args.type = XFS_ALLOCTYPE_FIRST_AG;
4204 args.total = ap->minlen;
4205 args.minleft = 0;
4206 if ((error = xfs_alloc_vextent(&args)))
4207 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004208 ap->flist->xbf_low = 1;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004209 }
4210 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004211 /*
4212 * check the allocation happened at the same or higher AG than
4213 * the first block that was allocated.
4214 */
4215 ASSERT(*ap->firstblock == NULLFSBLOCK ||
4216 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
4217 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
4218 (ap->flist->xbf_low &&
4219 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
4220 XFS_FSB_TO_AGNO(mp, args.fsbno)));
4221
Dave Chinner3a756672011-09-18 20:40:58 +00004222 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004223 if (*ap->firstblock == NULLFSBLOCK)
4224 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004225 ASSERT(nullfb || fb_agno == args.agno ||
Dave Chinner0937e0f2011-09-18 20:40:57 +00004226 (ap->flist->xbf_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00004227 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004228 ap->ip->i_d.di_nblocks += args.len;
4229 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
4230 if (ap->wasdel)
4231 ap->ip->i_delayed_blks -= args.len;
4232 /*
4233 * Adjust the disk quota also. This was reserved
4234 * earlier.
4235 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02004236 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11004237 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
4238 XFS_TRANS_DQ_BCOUNT,
4239 (long) args.len);
4240 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00004241 ap->blkno = NULLFSBLOCK;
4242 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 }
4244 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004245}
4246
4247/*
4248 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
4249 * It figures out where to ask the underlying allocator to put the new extent.
4250 */
4251STATIC int
4252xfs_bmap_alloc(
4253 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
4254{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11004255 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11004256 return xfs_bmap_rtalloc(ap);
4257 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258}
4259
4260/*
Dave Chinneraef9a892011-09-18 20:40:44 +00004261 * Trim the returned map to the required bounds
4262 */
4263STATIC void
4264xfs_bmapi_trim_map(
4265 struct xfs_bmbt_irec *mval,
4266 struct xfs_bmbt_irec *got,
4267 xfs_fileoff_t *bno,
4268 xfs_filblks_t len,
4269 xfs_fileoff_t obno,
4270 xfs_fileoff_t end,
4271 int n,
4272 int flags)
4273{
4274 if ((flags & XFS_BMAPI_ENTIRE) ||
4275 got->br_startoff + got->br_blockcount <= obno) {
4276 *mval = *got;
4277 if (isnullstartblock(got->br_startblock))
4278 mval->br_startblock = DELAYSTARTBLOCK;
4279 return;
4280 }
4281
4282 if (obno > *bno)
4283 *bno = obno;
4284 ASSERT((*bno >= obno) || (n == 0));
4285 ASSERT(*bno < end);
4286 mval->br_startoff = *bno;
4287 if (isnullstartblock(got->br_startblock))
4288 mval->br_startblock = DELAYSTARTBLOCK;
4289 else
4290 mval->br_startblock = got->br_startblock +
4291 (*bno - got->br_startoff);
4292 /*
4293 * Return the minimum of what we got and what we asked for for
4294 * the length. We can use the len variable here because it is
4295 * modified below and we could have been there before coming
4296 * here if the first part of the allocation didn't overlap what
4297 * was asked for.
4298 */
4299 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4300 got->br_blockcount - (*bno - got->br_startoff));
4301 mval->br_state = got->br_state;
4302 ASSERT(mval->br_blockcount <= len);
4303 return;
4304}
4305
4306/*
4307 * Update and validate the extent map to return
4308 */
4309STATIC void
4310xfs_bmapi_update_map(
4311 struct xfs_bmbt_irec **map,
4312 xfs_fileoff_t *bno,
4313 xfs_filblks_t *len,
4314 xfs_fileoff_t obno,
4315 xfs_fileoff_t end,
4316 int *n,
4317 int flags)
4318{
4319 xfs_bmbt_irec_t *mval = *map;
4320
4321 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4322 ((mval->br_startoff + mval->br_blockcount) <= end));
4323 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4324 (mval->br_startoff < obno));
4325
4326 *bno = mval->br_startoff + mval->br_blockcount;
4327 *len = end - *bno;
4328 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4329 /* update previous map with new information */
4330 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4331 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4332 ASSERT(mval->br_state == mval[-1].br_state);
4333 mval[-1].br_blockcount = mval->br_blockcount;
4334 mval[-1].br_state = mval->br_state;
4335 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4336 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4337 mval[-1].br_startblock != HOLESTARTBLOCK &&
4338 mval->br_startblock == mval[-1].br_startblock +
4339 mval[-1].br_blockcount &&
4340 ((flags & XFS_BMAPI_IGSTATE) ||
4341 mval[-1].br_state == mval->br_state)) {
4342 ASSERT(mval->br_startoff ==
4343 mval[-1].br_startoff + mval[-1].br_blockcount);
4344 mval[-1].br_blockcount += mval->br_blockcount;
4345 } else if (*n > 0 &&
4346 mval->br_startblock == DELAYSTARTBLOCK &&
4347 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4348 mval->br_startoff ==
4349 mval[-1].br_startoff + mval[-1].br_blockcount) {
4350 mval[-1].br_blockcount += mval->br_blockcount;
4351 mval[-1].br_state = mval->br_state;
4352 } else if (!((*n == 0) &&
4353 ((mval->br_startoff + mval->br_blockcount) <=
4354 obno))) {
4355 mval++;
4356 (*n)++;
4357 }
4358 *map = mval;
4359}
4360
4361/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004362 * Map file blocks to filesystem blocks without allocation.
4363 */
4364int
4365xfs_bmapi_read(
4366 struct xfs_inode *ip,
4367 xfs_fileoff_t bno,
4368 xfs_filblks_t len,
4369 struct xfs_bmbt_irec *mval,
4370 int *nmap,
4371 int flags)
4372{
4373 struct xfs_mount *mp = ip->i_mount;
4374 struct xfs_ifork *ifp;
4375 struct xfs_bmbt_irec got;
4376 struct xfs_bmbt_irec prev;
4377 xfs_fileoff_t obno;
4378 xfs_fileoff_t end;
4379 xfs_extnum_t lastx;
4380 int error;
4381 int eof;
4382 int n = 0;
4383 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4384 XFS_ATTR_FORK : XFS_DATA_FORK;
4385
4386 ASSERT(*nmap >= 1);
4387 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4388 XFS_BMAPI_IGSTATE)));
4389
4390 if (unlikely(XFS_TEST_ERROR(
4391 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4392 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4393 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4394 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4395 return XFS_ERROR(EFSCORRUPTED);
4396 }
4397
4398 if (XFS_FORCED_SHUTDOWN(mp))
4399 return XFS_ERROR(EIO);
4400
4401 XFS_STATS_INC(xs_blk_mapr);
4402
4403 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004404
4405 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4406 error = xfs_iread_extents(NULL, ip, whichfork);
4407 if (error)
4408 return error;
4409 }
4410
4411 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4412 end = bno + len;
4413 obno = bno;
4414
4415 while (bno < end && n < *nmap) {
4416 /* Reading past eof, act as though there's a hole up to end. */
4417 if (eof)
4418 got.br_startoff = end;
4419 if (got.br_startoff > bno) {
4420 /* Reading in a hole. */
4421 mval->br_startoff = bno;
4422 mval->br_startblock = HOLESTARTBLOCK;
4423 mval->br_blockcount =
4424 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4425 mval->br_state = XFS_EXT_NORM;
4426 bno += mval->br_blockcount;
4427 len -= mval->br_blockcount;
4428 mval++;
4429 n++;
4430 continue;
4431 }
4432
4433 /* set up the extent map to return. */
4434 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4435 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4436
4437 /* If we're done, stop now. */
4438 if (bno >= end || n >= *nmap)
4439 break;
4440
4441 /* Else go on to the next record. */
4442 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4443 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4444 else
4445 eof = 1;
4446 }
4447 *nmap = n;
4448 return 0;
4449}
4450
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004451STATIC int
4452xfs_bmapi_reserve_delalloc(
4453 struct xfs_inode *ip,
4454 xfs_fileoff_t aoff,
4455 xfs_filblks_t len,
4456 struct xfs_bmbt_irec *got,
4457 struct xfs_bmbt_irec *prev,
4458 xfs_extnum_t *lastx,
4459 int eof)
4460{
4461 struct xfs_mount *mp = ip->i_mount;
4462 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4463 xfs_extlen_t alen;
4464 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004465 char rt = XFS_IS_REALTIME_INODE(ip);
4466 xfs_extlen_t extsz;
4467 int error;
4468
4469 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4470 if (!eof)
4471 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4472
4473 /* Figure out the extent size, adjust alen */
4474 extsz = xfs_get_extsz_hint(ip);
4475 if (extsz) {
4476 /*
4477 * Make sure we don't exceed a single extent length when we
4478 * align the extent by reducing length we are going to
4479 * allocate by the maximum amount extent size aligment may
4480 * require.
4481 */
4482 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4483 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4484 1, 0, &aoff, &alen);
4485 ASSERT(!error);
4486 }
4487
4488 if (rt)
4489 extsz = alen / mp->m_sb.sb_rextsize;
4490
4491 /*
4492 * Make a transaction-less quota reservation for delayed allocation
4493 * blocks. This number gets adjusted later. We return if we haven't
4494 * allocated blocks already inside this loop.
4495 */
4496 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4497 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4498 if (error)
4499 return error;
4500
4501 /*
4502 * Split changing sb for alen and indlen since they could be coming
4503 * from different places.
4504 */
4505 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4506 ASSERT(indlen > 0);
4507
4508 if (rt) {
4509 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4510 -((int64_t)extsz), 0);
4511 } else {
4512 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4513 -((int64_t)alen), 0);
4514 }
4515
4516 if (error)
4517 goto out_unreserve_quota;
4518
4519 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4520 -((int64_t)indlen), 0);
4521 if (error)
4522 goto out_unreserve_blocks;
4523
4524
4525 ip->i_delayed_blks += alen;
4526
4527 got->br_startoff = aoff;
4528 got->br_startblock = nullstartblock(indlen);
4529 got->br_blockcount = alen;
4530 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004531 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004532
4533 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004534 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4535 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004536 */
4537 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4538
4539 ASSERT(got->br_startoff <= aoff);
4540 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4541 ASSERT(isnullstartblock(got->br_startblock));
4542 ASSERT(got->br_state == XFS_EXT_NORM);
4543 return 0;
4544
4545out_unreserve_blocks:
4546 if (rt)
4547 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4548 else
4549 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4550out_unreserve_quota:
4551 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004552 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004553 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4554 return error;
4555}
4556
Dave Chinner5c8ed202011-09-18 20:40:45 +00004557/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004558 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4559 */
4560int
4561xfs_bmapi_delay(
4562 struct xfs_inode *ip, /* incore inode */
4563 xfs_fileoff_t bno, /* starting file offs. mapped */
4564 xfs_filblks_t len, /* length to map in file */
4565 struct xfs_bmbt_irec *mval, /* output: map values */
4566 int *nmap, /* i/o: mval size/count */
4567 int flags) /* XFS_BMAPI_... */
4568{
4569 struct xfs_mount *mp = ip->i_mount;
4570 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4571 struct xfs_bmbt_irec got; /* current file extent record */
4572 struct xfs_bmbt_irec prev; /* previous file extent record */
4573 xfs_fileoff_t obno; /* old block number (offset) */
4574 xfs_fileoff_t end; /* end of mapped file region */
4575 xfs_extnum_t lastx; /* last useful extent number */
4576 int eof; /* we've hit the end of extents */
4577 int n = 0; /* current extent index */
4578 int error = 0;
4579
4580 ASSERT(*nmap >= 1);
4581 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4582 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4583
4584 if (unlikely(XFS_TEST_ERROR(
4585 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4586 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4587 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4588 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4589 return XFS_ERROR(EFSCORRUPTED);
4590 }
4591
4592 if (XFS_FORCED_SHUTDOWN(mp))
4593 return XFS_ERROR(EIO);
4594
4595 XFS_STATS_INC(xs_blk_mapw);
4596
4597 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4598 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4599 if (error)
4600 return error;
4601 }
4602
4603 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4604 end = bno + len;
4605 obno = bno;
4606
4607 while (bno < end && n < *nmap) {
4608 if (eof || got.br_startoff > bno) {
4609 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4610 &prev, &lastx, eof);
4611 if (error) {
4612 if (n == 0) {
4613 *nmap = 0;
4614 return error;
4615 }
4616 break;
4617 }
4618 }
4619
4620 /* set up the extent map to return. */
4621 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4622 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4623
4624 /* If we're done, stop now. */
4625 if (bno >= end || n >= *nmap)
4626 break;
4627
4628 /* Else go on to the next record. */
4629 prev = got;
4630 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4631 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4632 else
4633 eof = 1;
4634 }
4635
4636 *nmap = n;
4637 return 0;
4638}
4639
4640
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004641STATIC int
Dave Chinnere04426b2012-10-05 11:06:59 +10004642__xfs_bmapi_allocate(
4643 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004644{
4645 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004646 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004647 XFS_ATTR_FORK : XFS_DATA_FORK;
4648 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004649 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004650 int error;
4651 int rt;
4652
Dave Chinnera99ebf42011-12-01 11:24:20 +00004653 ASSERT(bma->length > 0);
4654
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004655 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip);
4656
4657 /*
4658 * For the wasdelay case, we could also just allocate the stuff asked
4659 * for in this bmap call but that wouldn't be as good.
4660 */
4661 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004662 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4663 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004664 if (bma->idx != NULLEXTNUM && bma->idx) {
4665 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004666 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004667 }
4668 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004669 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004670 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004671 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004672 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004673 }
4674
4675 /*
4676 * Indicate if this is the first user data in the file, or just any
4677 * user data.
4678 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004679 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004680 bma->userdata = (bma->offset == 0) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004681 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4682 }
4683
Dave Chinnere04426b2012-10-05 11:06:59 +10004684 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004685
4686 /*
4687 * Only want to do the alignment at the eof if it is userdata and
4688 * allocation length is larger than a stripe unit.
4689 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004690 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004691 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004692 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004693 if (error)
4694 return error;
4695 }
4696
4697 error = xfs_bmap_alloc(bma);
4698 if (error)
4699 return error;
4700
Dave Chinner0937e0f2011-09-18 20:40:57 +00004701 if (bma->flist->xbf_low)
4702 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004703 if (bma->cur)
4704 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004705 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004706 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004707 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4708 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4709 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4710 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004711 }
4712 /*
4713 * Bump the number of extents we've allocated
4714 * in this call.
4715 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004716 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004717
Dave Chinner29c8d172011-09-18 20:41:00 +00004718 if (bma->cur)
4719 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004720 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4721
Dave Chinner963c30c2011-09-18 20:40:59 +00004722 bma->got.br_startoff = bma->offset;
4723 bma->got.br_startblock = bma->blkno;
4724 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004725 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004726
4727 /*
4728 * A wasdelay extent has been initialized, so shouldn't be flagged
4729 * as unwritten.
4730 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004731 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004732 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004733 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004734
Christoph Hellwigc6534242011-09-18 20:41:05 +00004735 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004736 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004737 else
4738 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004739
Christoph Hellwigc315c902011-09-18 20:41:02 +00004740 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004741 if (error)
4742 return error;
4743
4744 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004745 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4746 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4747 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004748 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004749 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004750
Dave Chinner963c30c2011-09-18 20:40:59 +00004751 ASSERT(bma->got.br_startoff <= bma->offset);
4752 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4753 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004754 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4755 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004756 return 0;
4757}
4758
Dave Chinnere04426b2012-10-05 11:06:59 +10004759static void
4760xfs_bmapi_allocate_worker(
4761 struct work_struct *work)
4762{
4763 struct xfs_bmalloca *args = container_of(work,
4764 struct xfs_bmalloca, work);
4765 unsigned long pflags;
4766
4767 /* we are in a transaction context here */
4768 current_set_flags_nested(&pflags, PF_FSTRANS);
4769
4770 args->result = __xfs_bmapi_allocate(args);
4771 complete(args->done);
4772
4773 current_restore_flags_nested(&pflags, PF_FSTRANS);
4774}
4775
4776/*
4777 * Some allocation requests often come in with little stack to work on. Push
4778 * them off to a worker thread so there is lots of stack to use. Otherwise just
4779 * call directly to avoid the context switch overhead here.
4780 */
4781int
4782xfs_bmapi_allocate(
4783 struct xfs_bmalloca *args)
4784{
4785 DECLARE_COMPLETION_ONSTACK(done);
4786
4787 if (!args->stack_switch)
4788 return __xfs_bmapi_allocate(args);
4789
4790
4791 args->done = &done;
4792 INIT_WORK_ONSTACK(&args->work, xfs_bmapi_allocate_worker);
4793 queue_work(xfs_alloc_wq, &args->work);
4794 wait_for_completion(&done);
4795 return args->result;
4796}
4797
Dave Chinnerb447fe52011-09-18 20:40:51 +00004798STATIC int
4799xfs_bmapi_convert_unwritten(
4800 struct xfs_bmalloca *bma,
4801 struct xfs_bmbt_irec *mval,
4802 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004803 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004804{
4805 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4806 XFS_ATTR_FORK : XFS_DATA_FORK;
4807 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004808 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004809 int error;
4810
Dave Chinnerb447fe52011-09-18 20:40:51 +00004811 /* check if we need to do unwritten->real conversion */
4812 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4813 (flags & XFS_BMAPI_PREALLOC))
4814 return 0;
4815
4816 /* check if we need to do real->unwritten conversion */
4817 if (mval->br_state == XFS_EXT_NORM &&
4818 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4819 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4820 return 0;
4821
4822 /*
4823 * Modify (by adding) the state flag, if writing.
4824 */
4825 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004826 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4827 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004828 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004829 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4830 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004831 }
4832 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4833 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4834
Dave Chinnere0c3da52011-09-18 20:41:01 +00004835 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004836 &bma->cur, mval, bma->firstblock, bma->flist,
4837 &tmp_logflags);
4838 bma->logflags |= tmp_logflags;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004839 if (error)
4840 return error;
4841
4842 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004843 * Update our extent pointer, given that
4844 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4845 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004846 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004847 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004848
4849 /*
4850 * We may have combined previously unwritten space with written space,
4851 * so generate another request.
4852 */
4853 if (mval->br_blockcount < len)
4854 return EAGAIN;
4855 return 0;
4856}
4857
Christoph Hellwig44032802011-09-18 20:40:48 +00004858/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004859 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4860 * extent state if necessary. Details behaviour is controlled by the flags
4861 * parameter. Only allocates blocks from a single allocation group, to avoid
4862 * locking problems.
4863 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 * The returned value in "firstblock" from the first call in a transaction
4865 * must be remembered and presented to subsequent calls in "firstblock".
4866 * An upper bound for the number of blocks to be allocated is supplied to
4867 * the first call in "total"; if no allocation group has that many free
4868 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4869 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004870int
4871xfs_bmapi_write(
4872 struct xfs_trans *tp, /* transaction pointer */
4873 struct xfs_inode *ip, /* incore inode */
4874 xfs_fileoff_t bno, /* starting file offs. mapped */
4875 xfs_filblks_t len, /* length to map in file */
4876 int flags, /* XFS_BMAPI_... */
4877 xfs_fsblock_t *firstblock, /* first allocated block
4878 controls a.g. for allocs */
4879 xfs_extlen_t total, /* total blocks needed */
4880 struct xfs_bmbt_irec *mval, /* output: map values */
4881 int *nmap, /* i/o: mval size/count */
4882 struct xfs_bmap_free *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004884 struct xfs_mount *mp = ip->i_mount;
4885 struct xfs_ifork *ifp;
4886 struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004887 xfs_fileoff_t end; /* end of mapped file region */
4888 int eof; /* after the end of extents */
4889 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004890 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004891 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004892 int whichfork; /* data or attr fork */
4893 char inhole; /* current location is hole in file */
4894 char wasdelay; /* old extent was delayed */
4895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004897 xfs_fileoff_t orig_bno; /* original block number value */
4898 int orig_flags; /* original flags arg value */
4899 xfs_filblks_t orig_len; /* original value of len arg */
4900 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4901 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902
4903 orig_bno = bno;
4904 orig_len = len;
4905 orig_flags = flags;
4906 orig_mval = mval;
4907 orig_nmap = *nmap;
4908#endif
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004909 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4910 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004911
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004913 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4914 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4915 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004916 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004917 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004918
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 if (unlikely(XFS_TEST_ERROR(
4920 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004921 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004922 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004923 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 return XFS_ERROR(EFSCORRUPTED);
4925 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927 if (XFS_FORCED_SHUTDOWN(mp))
4928 return XFS_ERROR(EIO);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004929
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004931
4932 XFS_STATS_INC(xs_blk_mapw);
4933
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004934 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004936 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004938 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004939 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004940 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004941 }
4942
4943 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4944 error = xfs_iread_extents(tp, ip, whichfork);
4945 if (error)
4946 goto error0;
4947 }
4948
Dave Chinnere0c3da52011-09-18 20:41:01 +00004949 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004950 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951 n = 0;
4952 end = bno + len;
4953 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004954
4955 bma.tp = tp;
4956 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004957 bma.total = total;
4958 bma.userdata = 0;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004959 bma.flist = flist;
4960 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004961
Brian Foster9e96fe62013-01-17 13:11:29 -05004962 if (flags & XFS_BMAPI_STACK_SWITCH)
4963 bma.stack_switch = 1;
4964
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004966 inhole = eof || bma.got.br_startoff > bno;
4967 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004968
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 /*
4970 * First, deal with the hole before the allocated space
4971 * that we found, if any.
4972 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004973 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004974 bma.eof = eof;
4975 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4976 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004977 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004978 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004979
Dave Chinnera99ebf42011-12-01 11:24:20 +00004980 /*
4981 * There's a 32/64 bit type mismatch between the
4982 * allocation length request (which can be 64 bits in
4983 * length) and the bma length request, which is
4984 * xfs_extlen_t and therefore 32 bits. Hence we have to
4985 * check for 32-bit overflows and handle them here.
4986 */
4987 if (len > (xfs_filblks_t)MAXEXTLEN)
4988 bma.length = MAXEXTLEN;
4989 else
4990 bma.length = len;
4991
4992 ASSERT(len > 0);
4993 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004994 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 if (error)
4996 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004997 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004998 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 }
Christoph Hellwig44032802011-09-18 20:40:48 +00005000
Dave Chinneraef9a892011-09-18 20:40:44 +00005001 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00005002 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
5003 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004
Dave Chinnerb447fe52011-09-18 20:40:51 +00005005 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005006 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005007 if (error == EAGAIN)
5008 continue;
5009 if (error)
5010 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011
Dave Chinneraef9a892011-09-18 20:40:44 +00005012 /* update the extent map to return */
5013 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
5014
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 /*
5016 * If we're done, stop now. Stop when we've allocated
5017 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5018 * the transaction may get too big.
5019 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00005020 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005022
5023 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00005024 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00005025 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
5026 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
5027 &bma.got);
5028 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00005029 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005032
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 /*
5034 * Transform from btree to extents, give it cur.
5035 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005036 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00005037 int tmp_logflags = 0;
5038
Dave Chinner29c8d172011-09-18 20:41:00 +00005039 ASSERT(bma.cur);
5040 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00005042 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 if (error)
5044 goto error0;
5045 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005046
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005048 XFS_IFORK_NEXTENTS(ip, whichfork) >
5049 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005051error0:
5052 /*
5053 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005054 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005055 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005056 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00005058 bma.logflags &= ~xfs_ilog_fext(whichfork);
5059 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00005061 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 /*
5063 * Log whatever the flags say, even if error. Otherwise we might miss
5064 * detecting a case where the data is changed, there's an error,
5065 * and it's not logged so we don't shutdown when we should.
5066 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005067 if (bma.logflags)
5068 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005069
Dave Chinner29c8d172011-09-18 20:41:00 +00005070 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 if (!error) {
5072 ASSERT(*firstblock == NULLFSBLOCK ||
5073 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5074 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00005075 bma.cur->bc_private.b.firstblock) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 (flist->xbf_low &&
5077 XFS_FSB_TO_AGNO(mp, *firstblock) <
5078 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00005079 bma.cur->bc_private.b.firstblock)));
5080 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 }
Dave Chinner29c8d172011-09-18 20:41:00 +00005082 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5084 }
5085 if (!error)
5086 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5087 orig_nmap, *nmap);
5088 return error;
5089}
5090
5091/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005092 * Called by xfs_bmapi to update file extent records and the btree
5093 * after removing space (or undoing a delayed allocation).
5094 */
5095STATIC int /* error */
5096xfs_bmap_del_extent(
5097 xfs_inode_t *ip, /* incore inode pointer */
5098 xfs_trans_t *tp, /* current transaction pointer */
5099 xfs_extnum_t *idx, /* extent number to update/delete */
5100 xfs_bmap_free_t *flist, /* list of extents to be freed */
5101 xfs_btree_cur_t *cur, /* if null, not a btree */
5102 xfs_bmbt_irec_t *del, /* data to remove from extents */
5103 int *logflagsp, /* inode logging flags */
5104 int whichfork) /* data or attr fork */
5105{
5106 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5107 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5108 xfs_fsblock_t del_endblock=0; /* first block past del */
5109 xfs_fileoff_t del_endoff; /* first offset past del */
5110 int delay; /* current block is delayed allocated */
5111 int do_fx; /* free extent at end of routine */
5112 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5113 int error; /* error return value */
5114 int flags; /* inode logging flags */
5115 xfs_bmbt_irec_t got; /* current extent entry */
5116 xfs_fileoff_t got_endoff; /* first offset past got */
5117 int i; /* temp state */
5118 xfs_ifork_t *ifp; /* inode fork pointer */
5119 xfs_mount_t *mp; /* mount structure */
5120 xfs_filblks_t nblks; /* quota/sb block count */
5121 xfs_bmbt_irec_t new; /* new record to be inserted */
5122 /* REFERENCED */
5123 uint qfield; /* quota field to update */
5124 xfs_filblks_t temp; /* for indirect length calculations */
5125 xfs_filblks_t temp2; /* for indirect length calculations */
5126 int state = 0;
5127
5128 XFS_STATS_INC(xs_del_exlist);
5129
5130 if (whichfork == XFS_ATTR_FORK)
5131 state |= BMAP_ATTRFORK;
5132
5133 mp = ip->i_mount;
5134 ifp = XFS_IFORK_PTR(ip, whichfork);
5135 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
5136 (uint)sizeof(xfs_bmbt_rec_t)));
5137 ASSERT(del->br_blockcount > 0);
5138 ep = xfs_iext_get_ext(ifp, *idx);
5139 xfs_bmbt_get_all(ep, &got);
5140 ASSERT(got.br_startoff <= del->br_startoff);
5141 del_endoff = del->br_startoff + del->br_blockcount;
5142 got_endoff = got.br_startoff + got.br_blockcount;
5143 ASSERT(got_endoff >= del_endoff);
5144 delay = isnullstartblock(got.br_startblock);
5145 ASSERT(isnullstartblock(del->br_startblock) == delay);
5146 flags = 0;
5147 qfield = 0;
5148 error = 0;
5149 /*
5150 * If deleting a real allocation, must free up the disk space.
5151 */
5152 if (!delay) {
5153 flags = XFS_ILOG_CORE;
5154 /*
5155 * Realtime allocation. Free it and record di_nblocks update.
5156 */
5157 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5158 xfs_fsblock_t bno;
5159 xfs_filblks_t len;
5160
5161 ASSERT(do_mod(del->br_blockcount,
5162 mp->m_sb.sb_rextsize) == 0);
5163 ASSERT(do_mod(del->br_startblock,
5164 mp->m_sb.sb_rextsize) == 0);
5165 bno = del->br_startblock;
5166 len = del->br_blockcount;
5167 do_div(bno, mp->m_sb.sb_rextsize);
5168 do_div(len, mp->m_sb.sb_rextsize);
5169 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5170 if (error)
5171 goto done;
5172 do_fx = 0;
5173 nblks = len * mp->m_sb.sb_rextsize;
5174 qfield = XFS_TRANS_DQ_RTBCOUNT;
5175 }
5176 /*
5177 * Ordinary allocation.
5178 */
5179 else {
5180 do_fx = 1;
5181 nblks = del->br_blockcount;
5182 qfield = XFS_TRANS_DQ_BCOUNT;
5183 }
5184 /*
5185 * Set up del_endblock and cur for later.
5186 */
5187 del_endblock = del->br_startblock + del->br_blockcount;
5188 if (cur) {
5189 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5190 got.br_startblock, got.br_blockcount,
5191 &i)))
5192 goto done;
5193 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5194 }
5195 da_old = da_new = 0;
5196 } else {
5197 da_old = startblockval(got.br_startblock);
5198 da_new = 0;
5199 nblks = 0;
5200 do_fx = 0;
5201 }
5202 /*
5203 * Set flag value to use in switch statement.
5204 * Left-contig is 2, right-contig is 1.
5205 */
5206 switch (((got.br_startoff == del->br_startoff) << 1) |
5207 (got_endoff == del_endoff)) {
5208 case 3:
5209 /*
5210 * Matches the whole extent. Delete the entry.
5211 */
5212 xfs_iext_remove(ip, *idx, 1,
5213 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5214 --*idx;
5215 if (delay)
5216 break;
5217
5218 XFS_IFORK_NEXT_SET(ip, whichfork,
5219 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5220 flags |= XFS_ILOG_CORE;
5221 if (!cur) {
5222 flags |= xfs_ilog_fext(whichfork);
5223 break;
5224 }
5225 if ((error = xfs_btree_delete(cur, &i)))
5226 goto done;
5227 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5228 break;
5229
5230 case 2:
5231 /*
5232 * Deleting the first part of the extent.
5233 */
5234 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5235 xfs_bmbt_set_startoff(ep, del_endoff);
5236 temp = got.br_blockcount - del->br_blockcount;
5237 xfs_bmbt_set_blockcount(ep, temp);
5238 if (delay) {
5239 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5240 da_old);
5241 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5242 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5243 da_new = temp;
5244 break;
5245 }
5246 xfs_bmbt_set_startblock(ep, del_endblock);
5247 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5248 if (!cur) {
5249 flags |= xfs_ilog_fext(whichfork);
5250 break;
5251 }
5252 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5253 got.br_blockcount - del->br_blockcount,
5254 got.br_state)))
5255 goto done;
5256 break;
5257
5258 case 1:
5259 /*
5260 * Deleting the last part of the extent.
5261 */
5262 temp = got.br_blockcount - del->br_blockcount;
5263 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5264 xfs_bmbt_set_blockcount(ep, temp);
5265 if (delay) {
5266 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5267 da_old);
5268 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5269 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5270 da_new = temp;
5271 break;
5272 }
5273 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5274 if (!cur) {
5275 flags |= xfs_ilog_fext(whichfork);
5276 break;
5277 }
5278 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5279 got.br_startblock,
5280 got.br_blockcount - del->br_blockcount,
5281 got.br_state)))
5282 goto done;
5283 break;
5284
5285 case 0:
5286 /*
5287 * Deleting the middle of the extent.
5288 */
5289 temp = del->br_startoff - got.br_startoff;
5290 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5291 xfs_bmbt_set_blockcount(ep, temp);
5292 new.br_startoff = del_endoff;
5293 temp2 = got_endoff - del_endoff;
5294 new.br_blockcount = temp2;
5295 new.br_state = got.br_state;
5296 if (!delay) {
5297 new.br_startblock = del_endblock;
5298 flags |= XFS_ILOG_CORE;
5299 if (cur) {
5300 if ((error = xfs_bmbt_update(cur,
5301 got.br_startoff,
5302 got.br_startblock, temp,
5303 got.br_state)))
5304 goto done;
5305 if ((error = xfs_btree_increment(cur, 0, &i)))
5306 goto done;
5307 cur->bc_rec.b = new;
5308 error = xfs_btree_insert(cur, &i);
5309 if (error && error != ENOSPC)
5310 goto done;
5311 /*
5312 * If get no-space back from btree insert,
5313 * it tried a split, and we have a zero
5314 * block reservation.
5315 * Fix up our state and return the error.
5316 */
5317 if (error == ENOSPC) {
5318 /*
5319 * Reset the cursor, don't trust
5320 * it after any insert operation.
5321 */
5322 if ((error = xfs_bmbt_lookup_eq(cur,
5323 got.br_startoff,
5324 got.br_startblock,
5325 temp, &i)))
5326 goto done;
5327 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5328 /*
5329 * Update the btree record back
5330 * to the original value.
5331 */
5332 if ((error = xfs_bmbt_update(cur,
5333 got.br_startoff,
5334 got.br_startblock,
5335 got.br_blockcount,
5336 got.br_state)))
5337 goto done;
5338 /*
5339 * Reset the extent record back
5340 * to the original value.
5341 */
5342 xfs_bmbt_set_blockcount(ep,
5343 got.br_blockcount);
5344 flags = 0;
5345 error = XFS_ERROR(ENOSPC);
5346 goto done;
5347 }
5348 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5349 } else
5350 flags |= xfs_ilog_fext(whichfork);
5351 XFS_IFORK_NEXT_SET(ip, whichfork,
5352 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5353 } else {
5354 ASSERT(whichfork == XFS_DATA_FORK);
5355 temp = xfs_bmap_worst_indlen(ip, temp);
5356 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5357 temp2 = xfs_bmap_worst_indlen(ip, temp2);
5358 new.br_startblock = nullstartblock((int)temp2);
5359 da_new = temp + temp2;
5360 while (da_new > da_old) {
5361 if (temp) {
5362 temp--;
5363 da_new--;
5364 xfs_bmbt_set_startblock(ep,
5365 nullstartblock((int)temp));
5366 }
5367 if (da_new == da_old)
5368 break;
5369 if (temp2) {
5370 temp2--;
5371 da_new--;
5372 new.br_startblock =
5373 nullstartblock((int)temp2);
5374 }
5375 }
5376 }
5377 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5378 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5379 ++*idx;
5380 break;
5381 }
5382 /*
5383 * If we need to, add to list of extents to delete.
5384 */
5385 if (do_fx)
5386 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
5387 mp);
5388 /*
5389 * Adjust inode # blocks in the file.
5390 */
5391 if (nblks)
5392 ip->i_d.di_nblocks -= nblks;
5393 /*
5394 * Adjust quota data.
5395 */
5396 if (qfield)
5397 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5398
5399 /*
5400 * Account for change in delayed indirect blocks.
5401 * Nothing to do for disk quota accounting here.
5402 */
5403 ASSERT(da_old >= da_new);
5404 if (da_old > da_new) {
5405 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5406 (int64_t)(da_old - da_new), 0);
5407 }
5408done:
5409 *logflagsp = flags;
5410 return error;
5411}
5412
5413/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 * Unmap (remove) blocks from a file.
5415 * If nexts is nonzero then the number of extents to remove is limited to
5416 * that value. If not all extents in the block range can be removed then
5417 * *done is set.
5418 */
5419int /* error */
5420xfs_bunmapi(
5421 xfs_trans_t *tp, /* transaction pointer */
5422 struct xfs_inode *ip, /* incore inode */
5423 xfs_fileoff_t bno, /* starting offset to unmap */
5424 xfs_filblks_t len, /* length to unmap in file */
5425 int flags, /* misc flags */
5426 xfs_extnum_t nexts, /* number of extents max */
5427 xfs_fsblock_t *firstblock, /* first allocated block
5428 controls a.g. for allocs */
5429 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5430 int *done) /* set if not done yet */
5431{
5432 xfs_btree_cur_t *cur; /* bmap btree cursor */
5433 xfs_bmbt_irec_t del; /* extent being deleted */
5434 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005435 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 int error; /* error return value */
5437 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005438 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005439 xfs_ifork_t *ifp; /* inode fork pointer */
5440 int isrt; /* freeing in rt area */
5441 xfs_extnum_t lastx; /* last extent index used */
5442 int logflags; /* transaction logging flags */
5443 xfs_extlen_t mod; /* rt extent offset */
5444 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005445 xfs_extnum_t nextents; /* number of file extents */
5446 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 xfs_fileoff_t start; /* first file offset deleted */
5448 int tmp_logflags; /* partial logging flags */
5449 int wasdel; /* was a delayed alloc extent */
5450 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 xfs_fsblock_t sum;
5452
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005453 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5454
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5456 XFS_ATTR_FORK : XFS_DATA_FORK;
5457 ifp = XFS_IFORK_PTR(ip, whichfork);
5458 if (unlikely(
5459 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5460 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5461 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5462 ip->i_mount);
5463 return XFS_ERROR(EFSCORRUPTED);
5464 }
5465 mp = ip->i_mount;
5466 if (XFS_FORCED_SHUTDOWN(mp))
5467 return XFS_ERROR(EIO);
Christoph Hellwig54893272011-05-11 15:04:03 +00005468
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 ASSERT(len > 0);
5470 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005471
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5473 (error = xfs_iread_extents(tp, ip, whichfork)))
5474 return error;
5475 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5476 if (nextents == 0) {
5477 *done = 1;
5478 return 0;
5479 }
5480 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005481 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 start = bno;
5483 bno = start + len - 1;
5484 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5485 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005486
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 /*
5488 * Check to see if the given block number is past the end of the
5489 * file, back up to the last block if so...
5490 */
5491 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005492 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005493 xfs_bmbt_get_all(ep, &got);
5494 bno = got.br_startoff + got.br_blockcount - 1;
5495 }
5496 logflags = 0;
5497 if (ifp->if_flags & XFS_IFBROOT) {
5498 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005499 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 cur->bc_private.b.firstblock = *firstblock;
5501 cur->bc_private.b.flist = flist;
5502 cur->bc_private.b.flags = 0;
5503 } else
5504 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005505
5506 if (isrt) {
5507 /*
5508 * Synchronize by locking the bitmap inode.
5509 */
5510 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5511 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5512 }
5513
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 extno = 0;
5515 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5516 (nexts == 0 || extno < nexts)) {
5517 /*
5518 * Is the found extent after a hole in which bno lives?
5519 * Just back up to the previous extent, if so.
5520 */
5521 if (got.br_startoff > bno) {
5522 if (--lastx < 0)
5523 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005524 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 xfs_bmbt_get_all(ep, &got);
5526 }
5527 /*
5528 * Is the last block of this extent before the range
5529 * we're supposed to delete? If so, we're done.
5530 */
5531 bno = XFS_FILEOFF_MIN(bno,
5532 got.br_startoff + got.br_blockcount - 1);
5533 if (bno < start)
5534 break;
5535 /*
5536 * Then deal with the (possibly delayed) allocated space
5537 * we found.
5538 */
5539 ASSERT(ep != NULL);
5540 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005541 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542 if (got.br_startoff < start) {
5543 del.br_startoff = start;
5544 del.br_blockcount -= start - got.br_startoff;
5545 if (!wasdel)
5546 del.br_startblock += start - got.br_startoff;
5547 }
5548 if (del.br_startoff + del.br_blockcount > bno + 1)
5549 del.br_blockcount = bno + 1 - del.br_startoff;
5550 sum = del.br_startblock + del.br_blockcount;
5551 if (isrt &&
5552 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5553 /*
5554 * Realtime extent not lined up at the end.
5555 * The extent could have been split into written
5556 * and unwritten pieces, or we could just be
5557 * unmapping part of it. But we can't really
5558 * get rid of part of a realtime extent.
5559 */
5560 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005561 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 /*
5563 * This piece is unwritten, or we're not
5564 * using unwritten extents. Skip over it.
5565 */
5566 ASSERT(bno >= mod);
5567 bno -= mod > del.br_blockcount ?
5568 del.br_blockcount : mod;
5569 if (bno < got.br_startoff) {
5570 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005571 xfs_bmbt_get_all(xfs_iext_get_ext(
5572 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 }
5574 continue;
5575 }
5576 /*
5577 * It's written, turn it unwritten.
5578 * This is better than zeroing it.
5579 */
5580 ASSERT(del.br_state == XFS_EXT_NORM);
5581 ASSERT(xfs_trans_get_block_res(tp) > 0);
5582 /*
5583 * If this spans a realtime extent boundary,
5584 * chop it back to the start of the one we end at.
5585 */
5586 if (del.br_blockcount > mod) {
5587 del.br_startoff += del.br_blockcount - mod;
5588 del.br_startblock += del.br_blockcount - mod;
5589 del.br_blockcount = mod;
5590 }
5591 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005592 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5593 &lastx, &cur, &del, firstblock, flist,
5594 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 if (error)
5596 goto error0;
5597 goto nodelete;
5598 }
5599 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5600 /*
5601 * Realtime extent is lined up at the end but not
5602 * at the front. We'll get rid of full extents if
5603 * we can.
5604 */
5605 mod = mp->m_sb.sb_rextsize - mod;
5606 if (del.br_blockcount > mod) {
5607 del.br_blockcount -= mod;
5608 del.br_startoff += mod;
5609 del.br_startblock += mod;
5610 } else if ((del.br_startoff == start &&
5611 (del.br_state == XFS_EXT_UNWRITTEN ||
5612 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005613 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 /*
5615 * Can't make it unwritten. There isn't
5616 * a full extent here so just skip it.
5617 */
5618 ASSERT(bno >= del.br_blockcount);
5619 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005620 if (got.br_startoff > bno) {
5621 if (--lastx >= 0) {
5622 ep = xfs_iext_get_ext(ifp,
5623 lastx);
5624 xfs_bmbt_get_all(ep, &got);
5625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 }
5627 continue;
5628 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5629 /*
5630 * This one is already unwritten.
5631 * It must have a written left neighbor.
5632 * Unwrite the killed part of that one and
5633 * try again.
5634 */
5635 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005636 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5637 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005639 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 ASSERT(del.br_startblock ==
5641 prev.br_startblock + prev.br_blockcount);
5642 if (prev.br_startoff < start) {
5643 mod = start - prev.br_startoff;
5644 prev.br_blockcount -= mod;
5645 prev.br_startblock += mod;
5646 prev.br_startoff = start;
5647 }
5648 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005649 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005650 error = xfs_bmap_add_extent_unwritten_real(tp,
5651 ip, &lastx, &cur, &prev,
5652 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 if (error)
5654 goto error0;
5655 goto nodelete;
5656 } else {
5657 ASSERT(del.br_state == XFS_EXT_NORM);
5658 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005659 error = xfs_bmap_add_extent_unwritten_real(tp,
5660 ip, &lastx, &cur, &del,
5661 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662 if (error)
5663 goto error0;
5664 goto nodelete;
5665 }
5666 }
5667 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005668 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005669 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005670 if (isrt) {
5671 xfs_filblks_t rtexts;
5672
5673 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5674 do_div(rtexts, mp->m_sb.sb_rextsize);
5675 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005676 (int64_t)rtexts, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005677 (void)xfs_trans_reserve_quota_nblks(NULL,
5678 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005679 XFS_QMOPT_RES_RTBLKS);
5680 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00005681 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005682 (int64_t)del.br_blockcount, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005683 (void)xfs_trans_reserve_quota_nblks(NULL,
5684 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005687 ip->i_delayed_blks -= del.br_blockcount;
5688 if (cur)
5689 cur->bc_private.b.flags |=
5690 XFS_BTCUR_BPRV_WASDEL;
5691 } else if (cur)
5692 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5693 /*
5694 * If it's the case where the directory code is running
5695 * with no block reservation, and the deleted block is in
5696 * the middle of its extent, and the resulting insert
5697 * of an extent would cause transformation to btree format,
5698 * then reject it. The calling code will then swap
5699 * blocks around instead.
5700 * We have to do this now, rather than waiting for the
5701 * conversion to btree format, since the transaction
5702 * will be dirty.
5703 */
5704 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5705 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005706 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5707 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 del.br_startoff > got.br_startoff &&
5709 del.br_startoff + del.br_blockcount <
5710 got.br_startoff + got.br_blockcount) {
5711 error = XFS_ERROR(ENOSPC);
5712 goto error0;
5713 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00005714 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005715 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 logflags |= tmp_logflags;
5717 if (error)
5718 goto error0;
5719 bno = del.br_startoff - 1;
5720nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 /*
5722 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005725 if (lastx >= 0) {
5726 ep = xfs_iext_get_ext(ifp, lastx);
5727 if (xfs_bmbt_get_startoff(ep) > bno) {
5728 if (--lastx >= 0)
5729 ep = xfs_iext_get_ext(ifp,
5730 lastx);
5731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734 extno++;
5735 }
5736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005738
Linus Torvalds1da177e2005-04-16 15:20:36 -07005739 /*
5740 * Convert to a btree if necessary.
5741 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005742 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 ASSERT(cur == NULL);
5744 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5745 &cur, 0, &tmp_logflags, whichfork);
5746 logflags |= tmp_logflags;
5747 if (error)
5748 goto error0;
5749 }
5750 /*
5751 * transform from btree to extents, give it cur
5752 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005753 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 ASSERT(cur != NULL);
5755 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5756 whichfork);
5757 logflags |= tmp_logflags;
5758 if (error)
5759 goto error0;
5760 }
5761 /*
5762 * transform from extents to local?
5763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 error = 0;
5765error0:
5766 /*
5767 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005768 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005770 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005772 logflags &= ~xfs_ilog_fext(whichfork);
5773 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005775 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 /*
5777 * Log inode even in the error case, if the transaction
5778 * is dirty we'll need to shut down the filesystem.
5779 */
5780 if (logflags)
5781 xfs_trans_log_inode(tp, ip, logflags);
5782 if (cur) {
5783 if (!error) {
5784 *firstblock = cur->bc_private.b.firstblock;
5785 cur->bc_private.b.allocated = 0;
5786 }
5787 xfs_btree_del_cursor(cur,
5788 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5789 }
5790 return error;
5791}
5792
5793/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005794 * returns 1 for success, 0 if we failed to map the extent.
5795 */
5796STATIC int
5797xfs_getbmapx_fix_eof_hole(
5798 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005799 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005800 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005801 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005802 __int64_t end, /* last block requested */
5803 xfs_fsblock_t startblock)
5804{
5805 __int64_t fixlen;
5806 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005807 xfs_ifork_t *ifp; /* inode fork pointer */
5808 xfs_extnum_t lastx; /* last extent pointer */
5809 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005810
5811 if (startblock == HOLESTARTBLOCK) {
5812 mp = ip->i_mount;
5813 out->bmv_block = -1;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005814 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005815 fixlen -= out->bmv_offset;
5816 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5817 /* Came to hole at EOF. Trim it. */
5818 if (fixlen <= 0)
5819 return 0;
5820 out->bmv_length = fixlen;
5821 }
5822 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005823 if (startblock == DELAYSTARTBLOCK)
5824 out->bmv_block = -2;
5825 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005826 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005827 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5828 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5829 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5830 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5831 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005832 }
5833
5834 return 1;
5835}
5836
5837/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005838 * Get inode's extents as described in bmv, and format for output.
5839 * Calls formatter to fill the user's buffer until all extents
5840 * are mapped, until the passed-in bmv->bmv_count slots have
5841 * been filled, or until the formatter short-circuits the loop,
5842 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843 */
5844int /* error code */
5845xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005846 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005847 struct getbmapx *bmv, /* user bmap structure */
5848 xfs_bmap_format_t formatter, /* format to user */
5849 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005850{
5851 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005852 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853 __int64_t fixlen; /* length for -1 case */
5854 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005855 int lock; /* lock state */
5856 xfs_bmbt_irec_t *map; /* buffer for user's data */
5857 xfs_mount_t *mp; /* file system mount point */
5858 int nex; /* # of user extents can do */
5859 int nexleft; /* # of user extents left */
5860 int subnex; /* # of bmapi's can do */
5861 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005862 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005863 int whichfork; /* data or attr fork */
5864 int prealloced; /* this is a file with
5865 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005866 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005867 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005868 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005871 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005872 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005873
Linus Torvalds1da177e2005-04-16 15:20:36 -07005874 if (whichfork == XFS_ATTR_FORK) {
5875 if (XFS_IFORK_Q(ip)) {
5876 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5877 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5878 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5879 return XFS_ERROR(EINVAL);
5880 } else if (unlikely(
5881 ip->i_d.di_aformat != 0 &&
5882 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5883 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5884 ip->i_mount);
5885 return XFS_ERROR(EFSCORRUPTED);
5886 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005887
5888 prealloced = 0;
5889 fixlen = 1LL << 32;
5890 } else {
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005891 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5892 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5893 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5894 return XFS_ERROR(EINVAL);
5895
David Chinner957d0eb2007-06-18 16:50:37 +10005896 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005897 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 prealloced = 1;
Dave Chinner32972382012-06-08 15:44:54 +10005899 fixlen = mp->m_super->s_maxbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 } else {
5901 prealloced = 0;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005902 fixlen = XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005904 }
5905
5906 if (bmv->bmv_length == -1) {
5907 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005908 bmv->bmv_length =
5909 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5910 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911 bmv->bmv_entries = 0;
5912 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005913 } else if (bmv->bmv_length < 0) {
5914 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005916
Linus Torvalds1da177e2005-04-16 15:20:36 -07005917 nex = bmv->bmv_count - 1;
5918 if (nex <= 0)
5919 return XFS_ERROR(EINVAL);
5920 bmvend = bmv->bmv_offset + bmv->bmv_length;
5921
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005922
5923 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5924 return XFS_ERROR(ENOMEM);
5925 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
Dave Chinnerf0742112012-03-07 04:50:22 +00005926 if (!out) {
5927 out = kmem_zalloc_large(bmv->bmv_count *
5928 sizeof(struct getbmapx));
5929 if (!out)
5930 return XFS_ERROR(ENOMEM);
5931 }
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005932
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005934 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005935 if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) {
Dave Chinner4bc1ea62012-11-12 22:53:56 +11005936 error = -filemap_write_and_wait(VFS_I(ip)->i_mapping);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005937 if (error)
5938 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005939 }
Dave Chinner309c8482010-11-30 15:16:02 +11005940 /*
5941 * even after flushing the inode, there can still be delalloc
5942 * blocks on the inode beyond EOF due to speculative
5943 * preallocation. These are not removed until the release
5944 * function is called or the inode is inactivated. Hence we
5945 * cannot assert here that ip->i_delayed_blks == 0.
5946 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948
5949 lock = xfs_ilock_map_shared(ip);
5950
5951 /*
5952 * Don't let nex be bigger than the number of extents
5953 * we can have assuming alternating holes and real extents.
5954 */
5955 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5956 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5957
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005958 bmapi_flags = xfs_bmapi_aflag(whichfork);
5959 if (!(iflags & BMV_IF_PREALLOC))
5960 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005961
5962 /*
5963 * Allocate enough space to handle "subnex" maps at a time.
5964 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005965 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04005967 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005968 if (!map)
5969 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970
5971 bmv->bmv_entries = 0;
5972
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005973 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5974 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5975 error = 0;
5976 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005977 }
5978
5979 nexleft = nex;
5980
5981 do {
5982 nmap = (nexleft > subnex) ? subnex : nexleft;
Dave Chinner5c8ed202011-09-18 20:40:45 +00005983 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5984 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5985 map, &nmap, bmapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005986 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005987 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 ASSERT(nmap <= subnex);
5989
5990 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005991 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005992 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005993 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11005994 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005995 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5996 out[cur_ext].bmv_offset =
5997 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5998 out[cur_ext].bmv_length =
5999 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
6000 out[cur_ext].bmv_unused1 = 0;
6001 out[cur_ext].bmv_unused2 = 0;
Dave Chinner58e20772012-04-29 21:16:17 +10006002
6003 /*
6004 * delayed allocation extents that start beyond EOF can
6005 * occur due to speculative EOF allocation when the
6006 * delalloc extent is larger than the largest freespace
6007 * extent at conversion time. These extents cannot be
6008 * converted by data writeback, so can exist here even
6009 * if we are not supposed to be finding delalloc
6010 * extents.
6011 */
6012 if (map[i].br_startblock == DELAYSTARTBLOCK &&
6013 map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
6014 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
6015
Yingping Lu9af0a702005-11-02 15:09:54 +11006016 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10006017 whichfork == XFS_ATTR_FORK) {
6018 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006019 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006020 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006022
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006023 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
6024 prealloced, bmvend,
6025 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006026 goto out_free_map;
6027
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006028 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006029 out[cur_ext].bmv_offset +
6030 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006031 bmv->bmv_length =
6032 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
Tao Ma9af25462010-08-30 02:44:03 +00006033
6034 /*
6035 * In case we don't want to return the hole,
6036 * don't increase cur_ext so that we can reuse
6037 * it in the next loop.
6038 */
6039 if ((iflags & BMV_IF_NO_HOLES) &&
6040 map[i].br_startblock == HOLESTARTBLOCK) {
6041 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
6042 continue;
6043 }
6044
6045 nexleft--;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006046 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006047 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 }
6049 } while (nmap && nexleft && bmv->bmv_length);
6050
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006051 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10006052 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006053 out_unlock_ilock:
6054 xfs_iunlock_map_shared(ip, lock);
6055 out_unlock_iolock:
6056 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006057
6058 for (i = 0; i < cur_ext; i++) {
6059 int full = 0; /* user array is full */
6060
6061 /* format results & advance arg */
6062 error = formatter(&arg, &out[i], &full);
6063 if (error || full)
6064 break;
6065 }
6066
Dave Chinnerf0742112012-03-07 04:50:22 +00006067 if (is_vmalloc_addr(out))
6068 kmem_free_large(out);
6069 else
6070 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006071 return error;
6072}
6073
Dave Chinnerc726de42010-11-30 15:14:39 +11006074/*
6075 * dead simple method of punching delalyed allocation blocks from a range in
6076 * the inode. Walks a block at a time so will be slow, but is only executed in
6077 * rare error cases so the overhead is not critical. This will alays punch out
6078 * both the start and end blocks, even if the ranges only partially overlap
6079 * them, so it is up to the caller to ensure that partial blocks are not
6080 * passed in.
6081 */
6082int
6083xfs_bmap_punch_delalloc_range(
6084 struct xfs_inode *ip,
6085 xfs_fileoff_t start_fsb,
6086 xfs_fileoff_t length)
6087{
6088 xfs_fileoff_t remaining = length;
6089 int error = 0;
6090
6091 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6092
6093 do {
6094 int done;
6095 xfs_bmbt_irec_t imap;
6096 int nimaps = 1;
6097 xfs_fsblock_t firstblock;
6098 xfs_bmap_free_t flist;
6099
6100 /*
6101 * Map the range first and check that it is a delalloc extent
6102 * before trying to unmap the range. Otherwise we will be
6103 * trying to remove a real extent (which requires a
6104 * transaction) or a hole, which is probably a bad idea...
6105 */
Dave Chinner5c8ed202011-09-18 20:40:45 +00006106 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
6107 XFS_BMAPI_ENTIRE);
Dave Chinnerc726de42010-11-30 15:14:39 +11006108
6109 if (error) {
6110 /* something screwed, just bail */
6111 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner53487782011-03-07 10:05:35 +11006112 xfs_alert(ip->i_mount,
Dave Chinnerc726de42010-11-30 15:14:39 +11006113 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6114 ip->i_ino, start_fsb);
6115 }
6116 break;
6117 }
6118 if (!nimaps) {
6119 /* nothing there */
6120 goto next_block;
6121 }
6122 if (imap.br_startblock != DELAYSTARTBLOCK) {
6123 /* been converted, ignore */
6124 goto next_block;
6125 }
6126 WARN_ON(imap.br_blockcount == 0);
6127
6128 /*
6129 * Note: while we initialise the firstblock/flist pair, they
6130 * should never be used because blocks should never be
6131 * allocated or freed for a delalloc extent and hence we need
6132 * don't cancel or finish them after the xfs_bunmapi() call.
6133 */
6134 xfs_bmap_init(&flist, &firstblock);
6135 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6136 &flist, &done);
6137 if (error)
6138 break;
6139
6140 ASSERT(!flist.xbf_count && !flist.xbf_first);
6141next_block:
6142 start_fsb++;
6143 remaining--;
6144 } while(remaining > 0);
6145
6146 return error;
6147}