blob: 89042848f9ecfe1726c9322044b21285ab459a30 [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 */
1164STATIC int /* error */
1165xfs_bmap_local_to_extents(
1166 xfs_trans_t *tp, /* transaction pointer */
1167 xfs_inode_t *ip, /* incore inode pointer */
1168 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
1169 xfs_extlen_t total, /* total blocks needed by transaction */
1170 int *logflagsp, /* inode logging flags */
1171 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001172 void (*init_fn)(struct xfs_trans *tp,
1173 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001174 struct xfs_inode *ip,
1175 struct xfs_ifork *ifp))
1176{
1177 int error; /* error return value */
1178 int flags; /* logging flags returned */
1179 xfs_ifork_t *ifp; /* inode fork pointer */
1180
1181 /*
1182 * We don't want to deal with the case of keeping inode data inline yet.
1183 * So sending the data fork of a regular inode is invalid.
1184 */
1185 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
1186 ifp = XFS_IFORK_PTR(ip, whichfork);
1187 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1188 flags = 0;
1189 error = 0;
1190 if (ifp->if_bytes) {
1191 xfs_alloc_arg_t args; /* allocation arguments */
1192 xfs_buf_t *bp; /* buffer for extent block */
1193 xfs_bmbt_rec_host_t *ep;/* extent record pointer */
1194
1195 ASSERT((ifp->if_flags &
1196 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
1197 memset(&args, 0, sizeof(args));
1198 args.tp = tp;
1199 args.mp = ip->i_mount;
1200 args.firstblock = *firstblock;
1201 /*
1202 * Allocate a block. We know we need only one, since the
1203 * file currently fits in an inode.
1204 */
1205 if (*firstblock == NULLFSBLOCK) {
1206 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
1207 args.type = XFS_ALLOCTYPE_START_BNO;
1208 } else {
1209 args.fsbno = *firstblock;
1210 args.type = XFS_ALLOCTYPE_NEAR_BNO;
1211 }
1212 args.total = total;
1213 args.minlen = args.maxlen = args.prod = 1;
1214 error = xfs_alloc_vextent(&args);
1215 if (error)
1216 goto done;
1217
1218 /* Can't fail, the space was reserved. */
1219 ASSERT(args.fsbno != NULLFSBLOCK);
1220 ASSERT(args.len == 1);
1221 *firstblock = args.fsbno;
1222 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
1223
1224 /* initialise the block and copy the data */
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001225 init_fn(tp, bp, ip, ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001226
1227 /* account for the change in fork size and log everything */
1228 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
1229 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
1230 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
1231 xfs_iext_add(ifp, 0, 1);
1232 ep = xfs_iext_get_ext(ifp, 0);
1233 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
1234 trace_xfs_bmap_post_update(ip, 0,
1235 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
1236 _THIS_IP_);
1237 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
1238 ip->i_d.di_nblocks = 1;
1239 xfs_trans_mod_dquot_byino(tp, ip,
1240 XFS_TRANS_DQ_BCOUNT, 1L);
1241 flags |= xfs_ilog_fext(whichfork);
1242 } else {
1243 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
1244 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
1245 }
1246 ifp->if_flags &= ~XFS_IFINLINE;
1247 ifp->if_flags |= XFS_IFEXTENTS;
1248 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
1249 flags |= XFS_ILOG_CORE;
1250done:
1251 *logflagsp = flags;
1252 return error;
1253}
1254
1255/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 * Called from xfs_bmap_add_attrfork to handle btree format files.
1257 */
1258STATIC int /* error */
1259xfs_bmap_add_attrfork_btree(
1260 xfs_trans_t *tp, /* transaction pointer */
1261 xfs_inode_t *ip, /* incore inode pointer */
1262 xfs_fsblock_t *firstblock, /* first block allocated */
1263 xfs_bmap_free_t *flist, /* blocks to free at commit */
1264 int *flags) /* inode logging flags */
1265{
1266 xfs_btree_cur_t *cur; /* btree cursor */
1267 int error; /* error return value */
1268 xfs_mount_t *mp; /* file system mount struct */
1269 int stat; /* newroot status */
1270
1271 mp = ip->i_mount;
1272 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1273 *flags |= XFS_ILOG_DBROOT;
1274 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001275 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 cur->bc_private.b.flist = flist;
1277 cur->bc_private.b.firstblock = *firstblock;
1278 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1279 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001280 /* must be at least one entry */
1281 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001282 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 goto error0;
1284 if (stat == 0) {
1285 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1286 return XFS_ERROR(ENOSPC);
1287 }
1288 *firstblock = cur->bc_private.b.firstblock;
1289 cur->bc_private.b.allocated = 0;
1290 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1291 }
1292 return 0;
1293error0:
1294 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1295 return error;
1296}
1297
1298/*
1299 * Called from xfs_bmap_add_attrfork to handle extents format files.
1300 */
1301STATIC int /* error */
1302xfs_bmap_add_attrfork_extents(
1303 xfs_trans_t *tp, /* transaction pointer */
1304 xfs_inode_t *ip, /* incore inode pointer */
1305 xfs_fsblock_t *firstblock, /* first block allocated */
1306 xfs_bmap_free_t *flist, /* blocks to free at commit */
1307 int *flags) /* inode logging flags */
1308{
1309 xfs_btree_cur_t *cur; /* bmap btree cursor */
1310 int error; /* error return value */
1311
1312 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1313 return 0;
1314 cur = NULL;
1315 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
1316 flags, XFS_DATA_FORK);
1317 if (cur) {
1318 cur->bc_private.b.allocated = 0;
1319 xfs_btree_del_cursor(cur,
1320 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1321 }
1322 return error;
1323}
1324
1325/*
Dave Chinner19de7352013-04-03 16:11:18 +11001326 * Block initialisation function for local to extent format conversion.
1327 *
1328 * This shouldn't actually be called by anyone, so make sure debug kernels cause
1329 * a noticable failure.
Dave Chinner1e823792013-02-11 15:58:13 +11001330 */
1331STATIC void
1332xfs_bmap_local_to_extents_init_fn(
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001333 struct xfs_trans *tp,
Dave Chinner1e823792013-02-11 15:58:13 +11001334 struct xfs_buf *bp,
1335 struct xfs_inode *ip,
1336 struct xfs_ifork *ifp)
1337{
Dave Chinner19de7352013-04-03 16:11:18 +11001338 ASSERT(0);
Dave Chinner1e823792013-02-11 15:58:13 +11001339 bp->b_ops = &xfs_bmbt_buf_ops;
1340 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
Dave Chinner61fe1352013-04-03 16:11:30 +11001341 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_BTREE_BUF);
Dave Chinner1e823792013-02-11 15:58:13 +11001342}
1343
Dave Chinner1e823792013-02-11 15:58:13 +11001344/*
1345 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1346 * different data fork content type needs a different callout to do the
1347 * conversion. Some are basic and only require special block initialisation
1348 * callouts for the data formating, others (directories) are so specialised they
1349 * handle everything themselves.
1350 *
1351 * XXX (dgc): investigate whether directory conversion can use the generic
1352 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001353 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
1355STATIC int /* error */
1356xfs_bmap_add_attrfork_local(
1357 xfs_trans_t *tp, /* transaction pointer */
1358 xfs_inode_t *ip, /* incore inode pointer */
1359 xfs_fsblock_t *firstblock, /* first block allocated */
1360 xfs_bmap_free_t *flist, /* blocks to free at commit */
1361 int *flags) /* inode logging flags */
1362{
1363 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1366 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001367
Al Viroabbede12011-07-26 02:31:30 -04001368 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 memset(&dargs, 0, sizeof(dargs));
1370 dargs.dp = ip;
1371 dargs.firstblock = firstblock;
1372 dargs.flist = flist;
Dave Chinner1e823792013-02-11 15:58:13 +11001373 dargs.total = ip->i_mount->m_dirblkfsbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 dargs.whichfork = XFS_DATA_FORK;
1375 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001376 return xfs_dir2_sf_to_block(&dargs);
1377 }
1378
1379 if (S_ISLNK(ip->i_d.di_mode))
1380 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1381 flags, XFS_DATA_FORK,
1382 xfs_symlink_local_to_remote);
1383
1384 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
1385 XFS_DATA_FORK,
1386 xfs_bmap_local_to_extents_init_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387}
1388
1389/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001390 * Convert inode from non-attributed to attributed.
1391 * Must not be in a transaction, ip must not be locked.
1392 */
1393int /* error code */
1394xfs_bmap_add_attrfork(
1395 xfs_inode_t *ip, /* incore inode pointer */
1396 int size, /* space new attribute needs */
1397 int rsvd) /* xact may use reserved blks */
1398{
1399 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1400 xfs_bmap_free_t flist; /* freed extent records */
1401 xfs_mount_t *mp; /* mount structure */
1402 xfs_trans_t *tp; /* transaction pointer */
1403 int blks; /* space reservation */
1404 int version = 1; /* superblock attr version */
1405 int committed; /* xaction was committed */
1406 int logflags; /* logging flags */
1407 int error; /* error return value */
1408
1409 ASSERT(XFS_IFORK_Q(ip) == 0);
1410
1411 mp = ip->i_mount;
1412 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1413 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
1414 blks = XFS_ADDAFORK_SPACE_RES(mp);
1415 if (rsvd)
1416 tp->t_flags |= XFS_TRANS_RESERVE;
1417 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
1418 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
1419 goto error0;
1420 xfs_ilock(ip, XFS_ILOCK_EXCL);
1421 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1422 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1423 XFS_QMOPT_RES_REGBLKS);
1424 if (error) {
1425 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1426 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
1427 return error;
1428 }
1429 if (XFS_IFORK_Q(ip))
1430 goto error1;
1431 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1432 /*
1433 * For inodes coming from pre-6.2 filesystems.
1434 */
1435 ASSERT(ip->i_d.di_aformat == 0);
1436 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1437 }
1438 ASSERT(ip->i_d.di_anextents == 0);
1439
1440 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1441 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1442
1443 switch (ip->i_d.di_format) {
1444 case XFS_DINODE_FMT_DEV:
1445 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1446 break;
1447 case XFS_DINODE_FMT_UUID:
1448 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1449 break;
1450 case XFS_DINODE_FMT_LOCAL:
1451 case XFS_DINODE_FMT_EXTENTS:
1452 case XFS_DINODE_FMT_BTREE:
1453 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1454 if (!ip->i_d.di_forkoff)
1455 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1456 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1457 version = 2;
1458 break;
1459 default:
1460 ASSERT(0);
1461 error = XFS_ERROR(EINVAL);
1462 goto error1;
1463 }
1464
1465 ASSERT(ip->i_afp == NULL);
1466 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1467 ip->i_afp->if_flags = XFS_IFEXTENTS;
1468 logflags = 0;
1469 xfs_bmap_init(&flist, &firstblock);
1470 switch (ip->i_d.di_format) {
1471 case XFS_DINODE_FMT_LOCAL:
1472 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
1473 &logflags);
1474 break;
1475 case XFS_DINODE_FMT_EXTENTS:
1476 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1477 &flist, &logflags);
1478 break;
1479 case XFS_DINODE_FMT_BTREE:
1480 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
1481 &logflags);
1482 break;
1483 default:
1484 error = 0;
1485 break;
1486 }
1487 if (logflags)
1488 xfs_trans_log_inode(tp, ip, logflags);
1489 if (error)
1490 goto error2;
1491 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1492 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1493 __int64_t sbfields = 0;
1494
1495 spin_lock(&mp->m_sb_lock);
1496 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1497 xfs_sb_version_addattr(&mp->m_sb);
1498 sbfields |= XFS_SB_VERSIONNUM;
1499 }
1500 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1501 xfs_sb_version_addattr2(&mp->m_sb);
1502 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
1503 }
1504 if (sbfields) {
1505 spin_unlock(&mp->m_sb_lock);
1506 xfs_mod_sb(tp, sbfields);
1507 } else
1508 spin_unlock(&mp->m_sb_lock);
1509 }
1510
1511 error = xfs_bmap_finish(&tp, &flist, &committed);
1512 if (error)
1513 goto error2;
1514 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1515error2:
1516 xfs_bmap_cancel(&flist);
1517error1:
1518 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1519error0:
1520 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1521 return error;
1522}
1523
1524/*
1525 * Internal and external extent tree search functions.
1526 */
1527
1528/*
1529 * Read in the extents to if_extents.
1530 * All inode fields are set up by caller, we just traverse the btree
1531 * and copy the records in. If the file system cannot contain unwritten
1532 * extents, the records are checked for no "state" flags.
1533 */
1534int /* error */
1535xfs_bmap_read_extents(
1536 xfs_trans_t *tp, /* transaction pointer */
1537 xfs_inode_t *ip, /* incore inode */
1538 int whichfork) /* data or attr fork */
1539{
1540 struct xfs_btree_block *block; /* current btree block */
1541 xfs_fsblock_t bno; /* block # of "block" */
1542 xfs_buf_t *bp; /* buffer for "block" */
1543 int error; /* error return value */
1544 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1545 xfs_extnum_t i, j; /* index into the extents list */
1546 xfs_ifork_t *ifp; /* fork structure */
1547 int level; /* btree level, for checking */
1548 xfs_mount_t *mp; /* file system mount structure */
1549 __be64 *pp; /* pointer to block address */
1550 /* REFERENCED */
1551 xfs_extnum_t room; /* number of entries there's room for */
1552
1553 bno = NULLFSBLOCK;
1554 mp = ip->i_mount;
1555 ifp = XFS_IFORK_PTR(ip, whichfork);
1556 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1557 XFS_EXTFMT_INODE(ip);
1558 block = ifp->if_broot;
1559 /*
1560 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1561 */
1562 level = be16_to_cpu(block->bb_level);
1563 ASSERT(level > 0);
1564 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1565 bno = be64_to_cpu(*pp);
1566 ASSERT(bno != NULLDFSBNO);
1567 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1568 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1569 /*
1570 * Go down the tree until leaf level is reached, following the first
1571 * pointer (leftmost) at each level.
1572 */
1573 while (level-- > 0) {
1574 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1575 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1576 if (error)
1577 return error;
1578 block = XFS_BUF_TO_BLOCK(bp);
1579 XFS_WANT_CORRUPTED_GOTO(
1580 xfs_bmap_sanity_check(mp, bp, level),
1581 error0);
1582 if (level == 0)
1583 break;
1584 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1585 bno = be64_to_cpu(*pp);
1586 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
1587 xfs_trans_brelse(tp, bp);
1588 }
1589 /*
1590 * Here with bp and block set to the leftmost leaf node in the tree.
1591 */
1592 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1593 i = 0;
1594 /*
1595 * Loop over all leaf nodes. Copy information to the extent records.
1596 */
1597 for (;;) {
1598 xfs_bmbt_rec_t *frp;
1599 xfs_fsblock_t nextbno;
1600 xfs_extnum_t num_recs;
1601 xfs_extnum_t start;
1602
1603 num_recs = xfs_btree_get_numrecs(block);
1604 if (unlikely(i + num_recs > room)) {
1605 ASSERT(i + num_recs <= room);
1606 xfs_warn(ip->i_mount,
1607 "corrupt dinode %Lu, (btree extents).",
1608 (unsigned long long) ip->i_ino);
1609 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1610 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1611 goto error0;
1612 }
1613 XFS_WANT_CORRUPTED_GOTO(
1614 xfs_bmap_sanity_check(mp, bp, 0),
1615 error0);
1616 /*
1617 * Read-ahead the next leaf block, if any.
1618 */
1619 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1620 if (nextbno != NULLFSBLOCK)
1621 xfs_btree_reada_bufl(mp, nextbno, 1,
1622 &xfs_bmbt_buf_ops);
1623 /*
1624 * Copy records into the extent records.
1625 */
1626 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1627 start = i;
1628 for (j = 0; j < num_recs; j++, i++, frp++) {
1629 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1630 trp->l0 = be64_to_cpu(frp->l0);
1631 trp->l1 = be64_to_cpu(frp->l1);
1632 }
1633 if (exntf == XFS_EXTFMT_NOSTATE) {
1634 /*
1635 * Check all attribute bmap btree records and
1636 * any "older" data bmap btree records for a
1637 * set bit in the "extent flag" position.
1638 */
1639 if (unlikely(xfs_check_nostate_extents(ifp,
1640 start, num_recs))) {
1641 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1642 XFS_ERRLEVEL_LOW,
1643 ip->i_mount);
1644 goto error0;
1645 }
1646 }
1647 xfs_trans_brelse(tp, bp);
1648 bno = nextbno;
1649 /*
1650 * If we've reached the end, stop.
1651 */
1652 if (bno == NULLFSBLOCK)
1653 break;
1654 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1655 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1656 if (error)
1657 return error;
1658 block = XFS_BUF_TO_BLOCK(bp);
1659 }
1660 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1661 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1662 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1663 return 0;
1664error0:
1665 xfs_trans_brelse(tp, bp);
1666 return XFS_ERROR(EFSCORRUPTED);
1667}
1668
1669
1670/*
1671 * Search the extent records for the entry containing block bno.
1672 * If bno lies in a hole, point to the next entry. If bno lies
1673 * past eof, *eofp will be set, and *prevp will contain the last
1674 * entry (null if none). Else, *lastxp will be set to the index
1675 * of the found entry; *gotp will contain the entry.
1676 */
1677STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1678xfs_bmap_search_multi_extents(
1679 xfs_ifork_t *ifp, /* inode fork pointer */
1680 xfs_fileoff_t bno, /* block number searched for */
1681 int *eofp, /* out: end of file found */
1682 xfs_extnum_t *lastxp, /* out: last extent index */
1683 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1684 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1685{
1686 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1687 xfs_extnum_t lastx; /* last extent index */
1688
1689 /*
1690 * Initialize the extent entry structure to catch access to
1691 * uninitialized br_startblock field.
1692 */
1693 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1694 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1695 gotp->br_state = XFS_EXT_INVALID;
1696#if XFS_BIG_BLKNOS
1697 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
1698#else
1699 gotp->br_startblock = 0xffffa5a5;
1700#endif
1701 prevp->br_startoff = NULLFILEOFF;
1702
1703 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1704 if (lastx > 0) {
1705 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1706 }
1707 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1708 xfs_bmbt_get_all(ep, gotp);
1709 *eofp = 0;
1710 } else {
1711 if (lastx > 0) {
1712 *gotp = *prevp;
1713 }
1714 *eofp = 1;
1715 ep = NULL;
1716 }
1717 *lastxp = lastx;
1718 return ep;
1719}
1720
1721/*
1722 * Search the extents list for the inode, for the extent containing bno.
1723 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1724 * *eofp will be set, and *prevp will contain the last entry (null if none).
1725 * Else, *lastxp will be set to the index of the found
1726 * entry; *gotp will contain the entry.
1727 */
1728STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1729xfs_bmap_search_extents(
1730 xfs_inode_t *ip, /* incore inode pointer */
1731 xfs_fileoff_t bno, /* block number searched for */
1732 int fork, /* data or attr fork */
1733 int *eofp, /* out: end of file found */
1734 xfs_extnum_t *lastxp, /* out: last extent index */
1735 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1736 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1737{
1738 xfs_ifork_t *ifp; /* inode fork pointer */
1739 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1740
1741 XFS_STATS_INC(xs_look_exlist);
1742 ifp = XFS_IFORK_PTR(ip, fork);
1743
1744 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1745
1746 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1747 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1748 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1749 "Access to block zero in inode %llu "
1750 "start_block: %llx start_off: %llx "
1751 "blkcnt: %llx extent-state: %x lastx: %x\n",
1752 (unsigned long long)ip->i_ino,
1753 (unsigned long long)gotp->br_startblock,
1754 (unsigned long long)gotp->br_startoff,
1755 (unsigned long long)gotp->br_blockcount,
1756 gotp->br_state, *lastxp);
1757 *lastxp = NULLEXTNUM;
1758 *eofp = 1;
1759 return NULL;
1760 }
1761 return ep;
1762}
1763
1764/*
1765 * Returns the file-relative block number of the first unused block(s)
1766 * in the file with at least "len" logically contiguous blocks free.
1767 * This is the lowest-address hole if the file has holes, else the first block
1768 * past the end of file.
1769 * Return 0 if the file is currently local (in-inode).
1770 */
1771int /* error */
1772xfs_bmap_first_unused(
1773 xfs_trans_t *tp, /* transaction pointer */
1774 xfs_inode_t *ip, /* incore inode */
1775 xfs_extlen_t len, /* size of hole to find */
1776 xfs_fileoff_t *first_unused, /* unused block */
1777 int whichfork) /* data or attr fork */
1778{
1779 int error; /* error return value */
1780 int idx; /* extent record index */
1781 xfs_ifork_t *ifp; /* inode fork pointer */
1782 xfs_fileoff_t lastaddr; /* last block number seen */
1783 xfs_fileoff_t lowest; /* lowest useful block */
1784 xfs_fileoff_t max; /* starting useful block */
1785 xfs_fileoff_t off; /* offset for this block */
1786 xfs_extnum_t nextents; /* number of extent entries */
1787
1788 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1789 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1790 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1791 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1792 *first_unused = 0;
1793 return 0;
1794 }
1795 ifp = XFS_IFORK_PTR(ip, whichfork);
1796 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1797 (error = xfs_iread_extents(tp, ip, whichfork)))
1798 return error;
1799 lowest = *first_unused;
1800 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1801 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1802 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1803 off = xfs_bmbt_get_startoff(ep);
1804 /*
1805 * See if the hole before this extent will work.
1806 */
1807 if (off >= lowest + len && off - max >= len) {
1808 *first_unused = max;
1809 return 0;
1810 }
1811 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1812 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1813 }
1814 *first_unused = max;
1815 return 0;
1816}
1817
1818/*
1819 * Returns the file-relative block number of the last block + 1 before
1820 * last_block (input value) in the file.
1821 * This is not based on i_size, it is based on the extent records.
1822 * Returns 0 for local files, as they do not have extent records.
1823 */
1824int /* error */
1825xfs_bmap_last_before(
1826 xfs_trans_t *tp, /* transaction pointer */
1827 xfs_inode_t *ip, /* incore inode */
1828 xfs_fileoff_t *last_block, /* last block */
1829 int whichfork) /* data or attr fork */
1830{
1831 xfs_fileoff_t bno; /* input file offset */
1832 int eof; /* hit end of file */
1833 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1834 int error; /* error return value */
1835 xfs_bmbt_irec_t got; /* current extent value */
1836 xfs_ifork_t *ifp; /* inode fork pointer */
1837 xfs_extnum_t lastx; /* last extent used */
1838 xfs_bmbt_irec_t prev; /* previous extent value */
1839
1840 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1841 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1842 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
1843 return XFS_ERROR(EIO);
1844 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1845 *last_block = 0;
1846 return 0;
1847 }
1848 ifp = XFS_IFORK_PTR(ip, whichfork);
1849 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1850 (error = xfs_iread_extents(tp, ip, whichfork)))
1851 return error;
1852 bno = *last_block - 1;
1853 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1854 &prev);
1855 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1856 if (prev.br_startoff == NULLFILEOFF)
1857 *last_block = 0;
1858 else
1859 *last_block = prev.br_startoff + prev.br_blockcount;
1860 }
1861 /*
1862 * Otherwise *last_block is already the right answer.
1863 */
1864 return 0;
1865}
1866
1867STATIC int
1868xfs_bmap_last_extent(
1869 struct xfs_trans *tp,
1870 struct xfs_inode *ip,
1871 int whichfork,
1872 struct xfs_bmbt_irec *rec,
1873 int *is_empty)
1874{
1875 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1876 int error;
1877 int nextents;
1878
1879 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1880 error = xfs_iread_extents(tp, ip, whichfork);
1881 if (error)
1882 return error;
1883 }
1884
1885 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1886 if (nextents == 0) {
1887 *is_empty = 1;
1888 return 0;
1889 }
1890
1891 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1892 *is_empty = 0;
1893 return 0;
1894}
1895
1896/*
1897 * Check the last inode extent to determine whether this allocation will result
1898 * in blocks being allocated at the end of the file. When we allocate new data
1899 * blocks at the end of the file which do not start at the previous data block,
1900 * we will try to align the new blocks at stripe unit boundaries.
1901 *
1902 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
1903 * at, or past the EOF.
1904 */
1905STATIC int
1906xfs_bmap_isaeof(
1907 struct xfs_bmalloca *bma,
1908 int whichfork)
1909{
1910 struct xfs_bmbt_irec rec;
1911 int is_empty;
1912 int error;
1913
1914 bma->aeof = 0;
1915 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1916 &is_empty);
1917 if (error || is_empty)
1918 return error;
1919
1920 /*
1921 * Check if we are allocation or past the last extent, or at least into
1922 * the last delayed allocated extent.
1923 */
1924 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1925 (bma->offset >= rec.br_startoff &&
1926 isnullstartblock(rec.br_startblock));
1927 return 0;
1928}
1929
1930/*
1931 * Check if the endoff is outside the last extent. If so the caller will grow
1932 * the allocation to a stripe unit boundary. All offsets are considered outside
1933 * the end of file for an empty fork, so 1 is returned in *eof in that case.
1934 */
1935int
1936xfs_bmap_eof(
1937 struct xfs_inode *ip,
1938 xfs_fileoff_t endoff,
1939 int whichfork,
1940 int *eof)
1941{
1942 struct xfs_bmbt_irec rec;
1943 int error;
1944
1945 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
1946 if (error || *eof)
1947 return error;
1948
1949 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
1950 return 0;
1951}
1952
1953/*
1954 * Returns the file-relative block number of the first block past eof in
1955 * the file. This is not based on i_size, it is based on the extent records.
1956 * Returns 0 for local files, as they do not have extent records.
1957 */
1958int
1959xfs_bmap_last_offset(
1960 struct xfs_trans *tp,
1961 struct xfs_inode *ip,
1962 xfs_fileoff_t *last_block,
1963 int whichfork)
1964{
1965 struct xfs_bmbt_irec rec;
1966 int is_empty;
1967 int error;
1968
1969 *last_block = 0;
1970
1971 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1972 return 0;
1973
1974 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1975 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1976 return XFS_ERROR(EIO);
1977
1978 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1979 if (error || is_empty)
1980 return error;
1981
1982 *last_block = rec.br_startoff + rec.br_blockcount;
1983 return 0;
1984}
1985
1986/*
1987 * Returns whether the selected fork of the inode has exactly one
1988 * block or not. For the data fork we check this matches di_size,
1989 * implying the file's range is 0..bsize-1.
1990 */
1991int /* 1=>1 block, 0=>otherwise */
1992xfs_bmap_one_block(
1993 xfs_inode_t *ip, /* incore inode */
1994 int whichfork) /* data or attr fork */
1995{
1996 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1997 xfs_ifork_t *ifp; /* inode fork pointer */
1998 int rval; /* return value */
1999 xfs_bmbt_irec_t s; /* internal version of extent */
2000
2001#ifndef DEBUG
2002 if (whichfork == XFS_DATA_FORK)
2003 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
2004#endif /* !DEBUG */
2005 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
2006 return 0;
2007 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
2008 return 0;
2009 ifp = XFS_IFORK_PTR(ip, whichfork);
2010 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2011 ep = xfs_iext_get_ext(ifp, 0);
2012 xfs_bmbt_get_all(ep, &s);
2013 rval = s.br_startoff == 0 && s.br_blockcount == 1;
2014 if (rval && whichfork == XFS_DATA_FORK)
2015 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
2016 return rval;
2017}
2018
2019/*
2020 * Extent tree manipulation functions used during allocation.
2021 */
2022
2023/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002024 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 */
2026STATIC int /* error */
2027xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002028 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002030 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002032 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002035 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 xfs_fileoff_t new_endoff; /* end offset of new entry */
2037 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2038 /* left is 0, right is 1, prev is 2 */
2039 int rval=0; /* return value (logging flags) */
2040 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002041 xfs_filblks_t da_new; /* new count del alloc blocks used */
2042 xfs_filblks_t da_old; /* old count del alloc blocks used */
2043 xfs_filblks_t temp=0; /* value for da_new calculations */
2044 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002047 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002048
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002049 ASSERT(bma->idx >= 0);
2050 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002051 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002052 ASSERT(!bma->cur ||
2053 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002054
2055 XFS_STATS_INC(xs_add_exlist);
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057#define LEFT r[0]
2058#define RIGHT r[1]
2059#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
2061 /*
2062 * Set up a bunch of variables to make the tests simpler.
2063 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002064 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 xfs_bmbt_get_all(ep, &PREV);
2066 new_endoff = new->br_startoff + new->br_blockcount;
2067 ASSERT(PREV.br_startoff <= new->br_startoff);
2068 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002069
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002070 da_old = startblockval(PREV.br_startblock);
2071 da_new = 0;
2072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 /*
2074 * Set flags determining what part of the previous delayed allocation
2075 * extent is being replaced by a real allocation.
2076 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002077 if (PREV.br_startoff == new->br_startoff)
2078 state |= BMAP_LEFT_FILLING;
2079 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2080 state |= BMAP_RIGHT_FILLING;
2081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 /*
2083 * Check and set flags if this segment has a left neighbor.
2084 * Don't set contiguous if the combined extent would be too large.
2085 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002086 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002087 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002088 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002089
2090 if (isnullstartblock(LEFT.br_startblock))
2091 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002093
2094 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2095 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2096 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2097 LEFT.br_state == new->br_state &&
2098 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2099 state |= BMAP_LEFT_CONTIG;
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 /*
2102 * Check and set flags if this segment has a right neighbor.
2103 * Don't set contiguous if the combined extent would be too large.
2104 * Also check for all-three-contiguous being too large.
2105 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002106 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002107 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002108 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002109
2110 if (isnullstartblock(RIGHT.br_startblock))
2111 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002113
2114 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2115 new_endoff == RIGHT.br_startoff &&
2116 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2117 new->br_state == RIGHT.br_state &&
2118 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2119 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2120 BMAP_RIGHT_FILLING)) !=
2121 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2122 BMAP_RIGHT_FILLING) ||
2123 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2124 <= MAXEXTLEN))
2125 state |= BMAP_RIGHT_CONTIG;
2126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 error = 0;
2128 /*
2129 * Switch out based on the FILLING and CONTIG state bits.
2130 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002131 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2132 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2133 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2134 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 /*
2136 * Filling in all of a previously delayed allocation extent.
2137 * The left and right neighbors are both contiguous with new.
2138 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002139 bma->idx--;
2140 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2141 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 LEFT.br_blockcount + PREV.br_blockcount +
2143 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002144 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002145
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002146 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
2147 bma->ip->i_d.di_nextents--;
2148 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2150 else {
2151 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002152 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002154 RIGHT.br_blockcount, &i);
2155 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002157 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002158 error = xfs_btree_delete(bma->cur, &i);
2159 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002161 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002162 error = xfs_btree_decrement(bma->cur, 0, &i);
2163 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002165 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002166 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 LEFT.br_startblock,
2168 LEFT.br_blockcount +
2169 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002170 RIGHT.br_blockcount, LEFT.br_state);
2171 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 goto done;
2173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 break;
2175
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002176 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 /*
2178 * Filling in all of a previously delayed allocation extent.
2179 * The left neighbor is contiguous, the right is not.
2180 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002181 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002182
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002183 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
2184 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00002185 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002186 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002187
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002188 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
2189 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 rval = XFS_ILOG_DEXT;
2191 else {
2192 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002193 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002195 &i);
2196 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002198 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002199 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 LEFT.br_startblock,
2201 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002202 PREV.br_blockcount, LEFT.br_state);
2203 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 goto done;
2205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 break;
2207
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002208 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /*
2210 * Filling in all of a previously delayed allocation extent.
2211 * The right neighbor is contiguous, the left is not.
2212 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002213 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 xfs_bmbt_set_startblock(ep, new->br_startblock);
2215 xfs_bmbt_set_blockcount(ep,
2216 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002217 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002218
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002219 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
2220 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 rval = XFS_ILOG_DEXT;
2222 else {
2223 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002224 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002226 RIGHT.br_blockcount, &i);
2227 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002229 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002230 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 new->br_startblock,
2232 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002233 RIGHT.br_blockcount, PREV.br_state);
2234 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 goto done;
2236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 break;
2238
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002239 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 /*
2241 * Filling in all of a previously delayed allocation extent.
2242 * Neither the left nor right neighbors are contiguous with
2243 * the new one.
2244 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002245 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002247 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002248
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002249 bma->ip->i_d.di_nextents++;
2250 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2252 else {
2253 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002254 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002256 &i);
2257 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002259 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002260 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2261 error = xfs_btree_insert(bma->cur, &i);
2262 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002264 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 break;
2267
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002268 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 /*
2270 * Filling in the first part of a previous delayed allocation.
2271 * The left neighbor is contiguous.
2272 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002273 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
2274 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 LEFT.br_blockcount + new->br_blockcount);
2276 xfs_bmbt_set_startoff(ep,
2277 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002278 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002281 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002283 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 rval = XFS_ILOG_DEXT;
2285 else {
2286 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002287 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002289 &i);
2290 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002292 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002293 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 LEFT.br_startblock,
2295 LEFT.br_blockcount +
2296 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002297 LEFT.br_state);
2298 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 goto done;
2300 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002301 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002302 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002303 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002304 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002305
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002306 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 break;
2308
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002309 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 /*
2311 * Filling in the first part of a previous delayed allocation.
2312 * The left neighbor is not contiguous.
2313 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002314 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 xfs_bmbt_set_startoff(ep, new_endoff);
2316 temp = PREV.br_blockcount - new->br_blockcount;
2317 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002318 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
2319 bma->ip->i_d.di_nextents++;
2320 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2322 else {
2323 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002324 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002326 &i);
2327 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002329 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002330 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2331 error = xfs_btree_insert(bma->cur, &i);
2332 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002334 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002336
2337 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002338 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2339 bma->firstblock, bma->flist,
2340 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 rval |= tmp_rval;
2342 if (error)
2343 goto done;
2344 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002345 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002346 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002347 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2348 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002349 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002350 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 break;
2352
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002353 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 /*
2355 * Filling in the last part of a previous delayed allocation.
2356 * The right neighbor is contiguous with the new allocation.
2357 */
2358 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002359 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002361 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002362 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 new->br_blockcount + RIGHT.br_blockcount,
2364 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002365 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2366 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 rval = XFS_ILOG_DEXT;
2368 else {
2369 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002370 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002372 RIGHT.br_blockcount, &i);
2373 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002375 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002376 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 new->br_startblock,
2378 new->br_blockcount +
2379 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002380 RIGHT.br_state);
2381 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 goto done;
2383 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002384
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002385 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002386 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002387 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002388 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002389 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002390
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002391 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 break;
2393
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002394 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 /*
2396 * Filling in the last part of a previous delayed allocation.
2397 * The right neighbor is not contiguous.
2398 */
2399 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002400 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002402 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2403 bma->ip->i_d.di_nextents++;
2404 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2406 else {
2407 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002408 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002410 &i);
2411 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002413 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002414 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2415 error = xfs_btree_insert(bma->cur, &i);
2416 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002418 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002420
2421 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002422 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2423 bma->firstblock, bma->flist, &bma->cur, 1,
2424 &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 rval |= tmp_rval;
2426 if (error)
2427 goto done;
2428 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002429 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002430 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002431 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2432 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002433 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002434 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002435
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002436 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 break;
2438
2439 case 0:
2440 /*
2441 * Filling in the middle part of a previous delayed allocation.
2442 * Contiguity is impossible here.
2443 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002444 *
2445 * We start with a delayed allocation:
2446 *
2447 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2448 * PREV @ idx
2449 *
2450 * and we are allocating:
2451 * +rrrrrrrrrrrrrrrrr+
2452 * new
2453 *
2454 * and we set it up for insertion as:
2455 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2456 * new
2457 * PREV @ idx LEFT RIGHT
2458 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 */
2460 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002462 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002463 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2464 LEFT = *new;
2465 RIGHT.br_state = PREV.br_state;
2466 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002467 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002468 RIGHT.br_startoff = new_endoff;
2469 RIGHT.br_blockcount = temp2;
2470 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002471 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2472 bma->ip->i_d.di_nextents++;
2473 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2475 else {
2476 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002477 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002479 &i);
2480 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002482 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002483 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2484 error = xfs_btree_insert(bma->cur, &i);
2485 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002487 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002489
2490 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002491 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2492 bma->firstblock, bma->flist, &bma->cur,
2493 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 rval |= tmp_rval;
2495 if (error)
2496 goto done;
2497 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002498 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2499 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002500 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002501 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002502 if (diff > 0) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002503 error = xfs_icsb_modify_counters(bma->ip->i_mount,
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002504 XFS_SBS_FDBLOCKS,
2505 -((int64_t)diff), 0);
2506 ASSERT(!error);
2507 if (error)
2508 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002510
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002511 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002512 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002513 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2514 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2515 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002516 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002517 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002518
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002519 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002520 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 break;
2522
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002523 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2524 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2525 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2526 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2527 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2528 case BMAP_LEFT_CONTIG:
2529 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 /*
2531 * These cases are all impossible.
2532 */
2533 ASSERT(0);
2534 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002535
2536 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002537 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002538 int tmp_logflags; /* partial log flag return val */
2539
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002540 ASSERT(bma->cur == NULL);
2541 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2542 bma->firstblock, bma->flist, &bma->cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002543 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002544 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002545 if (error)
2546 goto done;
2547 }
2548
2549 /* adjust for changes in reserved delayed indirect blocks */
2550 if (da_old || da_new) {
2551 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002552 if (bma->cur)
2553 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002554 ASSERT(temp <= da_old);
2555 if (temp < da_old)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002556 xfs_icsb_modify_counters(bma->ip->i_mount,
2557 XFS_SBS_FDBLOCKS,
2558 (int64_t)(da_old - temp), 0);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002559 }
2560
2561 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002562 if (bma->cur)
2563 bma->cur->bc_private.b.allocated = 0;
2564
2565 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002567 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return error;
2569#undef LEFT
2570#undef RIGHT
2571#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
2573
2574/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002575 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 */
2577STATIC int /* error */
2578xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002579 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002581 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002583 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002584 xfs_fsblock_t *first, /* pointer to firstblock variable */
2585 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002586 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002589 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002592 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 xfs_fileoff_t new_endoff; /* end offset of new entry */
2594 xfs_exntst_t newext; /* new extent state */
2595 xfs_exntst_t oldext; /* old extent state */
2596 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2597 /* left is 0, right is 1, prev is 2 */
2598 int rval=0; /* return value (logging flags) */
2599 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002601 *logflagsp = 0;
2602
2603 cur = *curp;
2604 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2605
2606 ASSERT(*idx >= 0);
2607 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2608 ASSERT(!isnullstartblock(new->br_startblock));
2609
2610 XFS_STATS_INC(xs_add_exlist);
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612#define LEFT r[0]
2613#define RIGHT r[1]
2614#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 /*
2617 * Set up a bunch of variables to make the tests simpler.
2618 */
2619 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002620 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 xfs_bmbt_get_all(ep, &PREV);
2622 newext = new->br_state;
2623 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2624 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2625 ASSERT(PREV.br_state == oldext);
2626 new_endoff = new->br_startoff + new->br_blockcount;
2627 ASSERT(PREV.br_startoff <= new->br_startoff);
2628 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 /*
2631 * Set flags determining what part of the previous oldext allocation
2632 * extent is being replaced by a newext allocation.
2633 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002634 if (PREV.br_startoff == new->br_startoff)
2635 state |= BMAP_LEFT_FILLING;
2636 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2637 state |= BMAP_RIGHT_FILLING;
2638
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 /*
2640 * Check and set flags if this segment has a left neighbor.
2641 * Don't set contiguous if the combined extent would be too large.
2642 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002643 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002644 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002645 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002646
2647 if (isnullstartblock(LEFT.br_startblock))
2648 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002650
2651 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2652 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2653 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2654 LEFT.br_state == newext &&
2655 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2656 state |= BMAP_LEFT_CONTIG;
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 /*
2659 * Check and set flags if this segment has a right neighbor.
2660 * Don't set contiguous if the combined extent would be too large.
2661 * Also check for all-three-contiguous being too large.
2662 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002663 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002664 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002665 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002666 if (isnullstartblock(RIGHT.br_startblock))
2667 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002669
2670 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2671 new_endoff == RIGHT.br_startoff &&
2672 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2673 newext == RIGHT.br_state &&
2674 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2675 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2676 BMAP_RIGHT_FILLING)) !=
2677 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2678 BMAP_RIGHT_FILLING) ||
2679 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2680 <= MAXEXTLEN))
2681 state |= BMAP_RIGHT_CONTIG;
2682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 /*
2684 * Switch out based on the FILLING and CONTIG state bits.
2685 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002686 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2687 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2688 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2689 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 /*
2691 * Setting all of a previous oldext extent to newext.
2692 * The left and right neighbors are both contiguous with new.
2693 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002694 --*idx;
2695
2696 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2697 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 LEFT.br_blockcount + PREV.br_blockcount +
2699 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002700 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002701
Christoph Hellwigec90c552011-05-23 08:52:53 +00002702 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 ip->i_d.di_nextents -= 2;
2704 if (cur == NULL)
2705 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2706 else {
2707 rval = XFS_ILOG_CORE;
2708 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2709 RIGHT.br_startblock,
2710 RIGHT.br_blockcount, &i)))
2711 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002712 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002713 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002715 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002716 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002718 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002719 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002721 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002722 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002724 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2726 LEFT.br_startblock,
2727 LEFT.br_blockcount + PREV.br_blockcount +
2728 RIGHT.br_blockcount, LEFT.br_state)))
2729 goto done;
2730 }
2731 break;
2732
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002733 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /*
2735 * Setting all of a previous oldext extent to newext.
2736 * The left neighbor is contiguous, the right is not.
2737 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002738 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002739
Christoph Hellwigec90c552011-05-23 08:52:53 +00002740 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2741 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2742 LEFT.br_blockcount + PREV.br_blockcount);
2743 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2744
2745 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 ip->i_d.di_nextents--;
2747 if (cur == NULL)
2748 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2749 else {
2750 rval = XFS_ILOG_CORE;
2751 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2752 PREV.br_startblock, PREV.br_blockcount,
2753 &i)))
2754 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002755 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002756 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002758 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002759 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002761 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2763 LEFT.br_startblock,
2764 LEFT.br_blockcount + PREV.br_blockcount,
2765 LEFT.br_state)))
2766 goto done;
2767 }
2768 break;
2769
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002770 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 /*
2772 * Setting all of a previous oldext extent to newext.
2773 * The right neighbor is contiguous, the left is not.
2774 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002775 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 xfs_bmbt_set_blockcount(ep,
2777 PREV.br_blockcount + RIGHT.br_blockcount);
2778 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002779 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2780 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 ip->i_d.di_nextents--;
2782 if (cur == NULL)
2783 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2784 else {
2785 rval = XFS_ILOG_CORE;
2786 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2787 RIGHT.br_startblock,
2788 RIGHT.br_blockcount, &i)))
2789 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002790 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002791 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002793 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002794 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002796 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2798 new->br_startblock,
2799 new->br_blockcount + RIGHT.br_blockcount,
2800 newext)))
2801 goto done;
2802 }
2803 break;
2804
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002805 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 /*
2807 * Setting all of a previous oldext extent to newext.
2808 * Neither the left nor right neighbors are contiguous with
2809 * the new one.
2810 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002811 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002813 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 if (cur == NULL)
2816 rval = XFS_ILOG_DEXT;
2817 else {
2818 rval = 0;
2819 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2820 new->br_startblock, new->br_blockcount,
2821 &i)))
2822 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002823 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2825 new->br_startblock, new->br_blockcount,
2826 newext)))
2827 goto done;
2828 }
2829 break;
2830
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002831 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 /*
2833 * Setting the first part of a previous oldext extent to newext.
2834 * The left neighbor is contiguous.
2835 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002836 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2837 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 LEFT.br_blockcount + new->br_blockcount);
2839 xfs_bmbt_set_startoff(ep,
2840 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002841 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002842
Christoph Hellwigec90c552011-05-23 08:52:53 +00002843 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 xfs_bmbt_set_startblock(ep,
2845 new->br_startblock + new->br_blockcount);
2846 xfs_bmbt_set_blockcount(ep,
2847 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002848 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002849
Christoph Hellwigec90c552011-05-23 08:52:53 +00002850 --*idx;
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (cur == NULL)
2853 rval = XFS_ILOG_DEXT;
2854 else {
2855 rval = 0;
2856 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2857 PREV.br_startblock, PREV.br_blockcount,
2858 &i)))
2859 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002860 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if ((error = xfs_bmbt_update(cur,
2862 PREV.br_startoff + new->br_blockcount,
2863 PREV.br_startblock + new->br_blockcount,
2864 PREV.br_blockcount - new->br_blockcount,
2865 oldext)))
2866 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002867 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002869 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 LEFT.br_startblock,
2871 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002872 LEFT.br_state);
2873 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 goto done;
2875 }
2876 break;
2877
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002878 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 /*
2880 * Setting the first part of a previous oldext extent to newext.
2881 * The left neighbor is not contiguous.
2882 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002883 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2885 xfs_bmbt_set_startoff(ep, new_endoff);
2886 xfs_bmbt_set_blockcount(ep,
2887 PREV.br_blockcount - new->br_blockcount);
2888 xfs_bmbt_set_startblock(ep,
2889 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002890 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002891
Christoph Hellwigec90c552011-05-23 08:52:53 +00002892 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 ip->i_d.di_nextents++;
2894 if (cur == NULL)
2895 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2896 else {
2897 rval = XFS_ILOG_CORE;
2898 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2899 PREV.br_startblock, PREV.br_blockcount,
2900 &i)))
2901 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002902 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 if ((error = xfs_bmbt_update(cur,
2904 PREV.br_startoff + new->br_blockcount,
2905 PREV.br_startblock + new->br_blockcount,
2906 PREV.br_blockcount - new->br_blockcount,
2907 oldext)))
2908 goto done;
2909 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002910 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002912 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
2914 break;
2915
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002916 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 /*
2918 * Setting the last part of a previous oldext extent to newext.
2919 * The right neighbor is contiguous with the new allocation.
2920 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002921 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 xfs_bmbt_set_blockcount(ep,
2923 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002924 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2925
2926 ++*idx;
2927
2928 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2929 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002930 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002932 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 if (cur == NULL)
2935 rval = XFS_ILOG_DEXT;
2936 else {
2937 rval = 0;
2938 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2939 PREV.br_startblock,
2940 PREV.br_blockcount, &i)))
2941 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002942 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2944 PREV.br_startblock,
2945 PREV.br_blockcount - new->br_blockcount,
2946 oldext)))
2947 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002948 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 goto done;
2950 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2951 new->br_startblock,
2952 new->br_blockcount + RIGHT.br_blockcount,
2953 newext)))
2954 goto done;
2955 }
2956 break;
2957
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002958 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 /*
2960 * Setting the last part of a previous oldext extent to newext.
2961 * The right neighbor is not contiguous.
2962 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002963 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 xfs_bmbt_set_blockcount(ep,
2965 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002966 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002967
Christoph Hellwigec90c552011-05-23 08:52:53 +00002968 ++*idx;
2969 xfs_iext_insert(ip, *idx, 1, new, state);
2970
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 ip->i_d.di_nextents++;
2972 if (cur == NULL)
2973 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2974 else {
2975 rval = XFS_ILOG_CORE;
2976 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2977 PREV.br_startblock, PREV.br_blockcount,
2978 &i)))
2979 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002980 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2982 PREV.br_startblock,
2983 PREV.br_blockcount - new->br_blockcount,
2984 oldext)))
2985 goto done;
2986 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2987 new->br_startblock, new->br_blockcount,
2988 &i)))
2989 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002990 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002992 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002994 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 }
2996 break;
2997
2998 case 0:
2999 /*
3000 * Setting the middle part of a previous oldext extent to
3001 * newext. Contiguity is impossible here.
3002 * One extent becomes three extents.
3003 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003004 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 xfs_bmbt_set_blockcount(ep,
3006 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003007 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 r[0] = *new;
3010 r[1].br_startoff = new_endoff;
3011 r[1].br_blockcount =
3012 PREV.br_startoff + PREV.br_blockcount - new_endoff;
3013 r[1].br_startblock = new->br_startblock + new->br_blockcount;
3014 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003015
3016 ++*idx;
3017 xfs_iext_insert(ip, *idx, 2, &r[0], state);
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 ip->i_d.di_nextents += 2;
3020 if (cur == NULL)
3021 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
3022 else {
3023 rval = XFS_ILOG_CORE;
3024 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
3025 PREV.br_startblock, PREV.br_blockcount,
3026 &i)))
3027 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003028 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 /* new right extent - oldext */
3030 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
3031 r[1].br_startblock, r[1].br_blockcount,
3032 r[1].br_state)))
3033 goto done;
3034 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10003036 cur->bc_rec.b.br_blockcount =
3037 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003038 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003040 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10003041 /*
3042 * Reset the cursor to the position of the new extent
3043 * we are about to insert as we can't trust it after
3044 * the previous insert.
3045 */
3046 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
3047 new->br_startblock, new->br_blockcount,
3048 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10003050 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10003052 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11003053 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003055 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 }
3057 break;
3058
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003059 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3060 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3061 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
3062 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
3063 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
3064 case BMAP_LEFT_CONTIG:
3065 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 /*
3067 * These cases are all impossible.
3068 */
3069 ASSERT(0);
3070 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003071
3072 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003073 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003074 int tmp_logflags; /* partial log flag return val */
3075
3076 ASSERT(cur == NULL);
3077 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
3078 0, &tmp_logflags, XFS_DATA_FORK);
3079 *logflagsp |= tmp_logflags;
3080 if (error)
3081 goto done;
3082 }
3083
3084 /* clear out the allocated field, done with it now in any case. */
3085 if (cur) {
3086 cur->bc_private.b.allocated = 0;
3087 *curp = cur;
3088 }
3089
3090 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003092 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return error;
3094#undef LEFT
3095#undef RIGHT
3096#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
3098
3099/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00003100 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00003102STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103xfs_bmap_add_extent_hole_delay(
3104 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003105 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00003106 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003108 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 xfs_bmbt_irec_t left; /* left neighbor extent entry */
3110 xfs_filblks_t newlen=0; /* new indirect size */
3111 xfs_filblks_t oldlen=0; /* old indirect size */
3112 xfs_bmbt_irec_t right; /* right neighbor extent entry */
3113 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003114 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003116 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003118 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 /*
3121 * Check and set flags if this segment has a left neighbor
3122 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003123 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003124 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003125 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003126
3127 if (isnullstartblock(left.br_startblock))
3128 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003130
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 /*
3132 * Check and set flags if the current (right) segment exists.
3133 * If it doesn't exist, we're converting the hole at end-of-file.
3134 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003135 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003136 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003137 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003138
3139 if (isnullstartblock(right.br_startblock))
3140 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 /*
3144 * Set contiguity flags on the left and right neighbors.
3145 * Don't let extents get too large, even if the pieces are contiguous.
3146 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003147 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
3148 left.br_startoff + left.br_blockcount == new->br_startoff &&
3149 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
3150 state |= BMAP_LEFT_CONTIG;
3151
3152 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
3153 new->br_startoff + new->br_blockcount == right.br_startoff &&
3154 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3155 (!(state & BMAP_LEFT_CONTIG) ||
3156 (left.br_blockcount + new->br_blockcount +
3157 right.br_blockcount <= MAXEXTLEN)))
3158 state |= BMAP_RIGHT_CONTIG;
3159
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 /*
3161 * Switch out based on the contiguity flags.
3162 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003163 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3164 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 /*
3166 * New allocation is contiguous with delayed allocations
3167 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003168 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003170 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 temp = left.br_blockcount + new->br_blockcount +
3172 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003173
Christoph Hellwigec90c552011-05-23 08:52:53 +00003174 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
3175 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003176 oldlen = startblockval(left.br_startblock) +
3177 startblockval(new->br_startblock) +
3178 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003180 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06003181 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00003182 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003183
Christoph Hellwigec90c552011-05-23 08:52:53 +00003184 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 break;
3186
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003187 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 /*
3189 * New allocation is contiguous with a delayed allocation
3190 * on the left.
3191 * Merge the new allocation with the left neighbor.
3192 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003193 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003195
3196 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
3197 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06003198 oldlen = startblockval(left.br_startblock) +
3199 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003201 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06003202 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00003203 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 break;
3205
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003206 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 /*
3208 * New allocation is contiguous with a delayed allocation
3209 * on the right.
3210 * Merge the new allocation with the right neighbor.
3211 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00003212 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06003214 oldlen = startblockval(new->br_startblock) +
3215 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003217 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
3218 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06003219 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003220 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 break;
3222
3223 case 0:
3224 /*
3225 * New allocation is not contiguous with another
3226 * delayed allocation.
3227 * Insert a new entry.
3228 */
3229 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00003230 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 break;
3232 }
3233 if (oldlen != newlen) {
3234 ASSERT(oldlen > newlen);
Christoph Hellwig96540c72010-09-30 02:25:55 +00003235 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00003236 (int64_t)(oldlen - newlen), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 /*
3238 * Nothing to do for disk quota accounting here.
3239 */
3240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
3243/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003244 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 */
3246STATIC int /* error */
3247xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00003248 struct xfs_bmalloca *bma,
3249 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Christoph Hellwigc6534242011-09-18 20:41:05 +00003251 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 int i; /* temp state */
3254 xfs_ifork_t *ifp; /* inode fork pointer */
3255 xfs_bmbt_irec_t left; /* left neighbor extent entry */
3256 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003257 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259
Christoph Hellwigc6534242011-09-18 20:41:05 +00003260 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003261
Christoph Hellwigc6534242011-09-18 20:41:05 +00003262 ASSERT(bma->idx >= 0);
3263 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003264 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00003265 ASSERT(!bma->cur ||
3266 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003267
3268 XFS_STATS_INC(xs_add_exlist);
3269
3270 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00003271 if (whichfork == XFS_ATTR_FORK)
3272 state |= BMAP_ATTRFORK;
3273
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 /*
3275 * Check and set flags if this segment has a left neighbor.
3276 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003277 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003278 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003279 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003280 if (isnullstartblock(left.br_startblock))
3281 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 /*
3285 * Check and set flags if this segment has a current value.
3286 * Not true if we're inserting into the "hole" at eof.
3287 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003288 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003289 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003290 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003291 if (isnullstartblock(right.br_startblock))
3292 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003294
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 /*
3296 * We're inserting a real allocation between "left" and "right".
3297 * Set the contiguity flags. Don't let extents get too large.
3298 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003299 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
3300 left.br_startoff + left.br_blockcount == new->br_startoff &&
3301 left.br_startblock + left.br_blockcount == new->br_startblock &&
3302 left.br_state == new->br_state &&
3303 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
3304 state |= BMAP_LEFT_CONTIG;
3305
3306 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
3307 new->br_startoff + new->br_blockcount == right.br_startoff &&
3308 new->br_startblock + new->br_blockcount == right.br_startblock &&
3309 new->br_state == right.br_state &&
3310 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3311 (!(state & BMAP_LEFT_CONTIG) ||
3312 left.br_blockcount + new->br_blockcount +
3313 right.br_blockcount <= MAXEXTLEN))
3314 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003316 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317 /*
3318 * Select which case we're in here, and implement it.
3319 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003320 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3321 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 /*
3323 * New allocation is contiguous with real allocations on the
3324 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003325 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003327 --bma->idx;
3328 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3329 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 left.br_blockcount + new->br_blockcount +
3331 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003332 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003333
Christoph Hellwigc6534242011-09-18 20:41:05 +00003334 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003335
Christoph Hellwigc6534242011-09-18 20:41:05 +00003336 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3337 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3338 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003339 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003340 } else {
3341 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003342 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3343 right.br_startblock, right.br_blockcount,
3344 &i);
3345 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003346 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003347 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003348 error = xfs_btree_delete(bma->cur, &i);
3349 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003350 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003351 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003352 error = xfs_btree_decrement(bma->cur, 0, &i);
3353 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003354 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003355 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003356 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003357 left.br_startblock,
3358 left.br_blockcount +
3359 new->br_blockcount +
3360 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003361 left.br_state);
3362 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003363 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003365 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003367 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 /*
3369 * New allocation is contiguous with a real allocation
3370 * on the left.
3371 * Merge the new allocation with the left neighbor.
3372 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003373 --bma->idx;
3374 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3375 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003377 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003378
Christoph Hellwigc6534242011-09-18 20:41:05 +00003379 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003380 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003381 } else {
3382 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003383 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3384 left.br_startblock, left.br_blockcount,
3385 &i);
3386 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003387 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003388 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003389 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003390 left.br_startblock,
3391 left.br_blockcount +
3392 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003393 left.br_state);
3394 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003395 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003397 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003399 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 /*
3401 * New allocation is contiguous with a real allocation
3402 * on the right.
3403 * Merge the new allocation with the right neighbor.
3404 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003405 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3406 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003407 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 new->br_blockcount + right.br_blockcount,
3409 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003410 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003411
Christoph Hellwigc6534242011-09-18 20:41:05 +00003412 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003413 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003414 } else {
3415 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003416 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003417 right.br_startoff,
3418 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003419 right.br_blockcount, &i);
3420 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003421 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003422 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003423 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003424 new->br_startblock,
3425 new->br_blockcount +
3426 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003427 right.br_state);
3428 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003429 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
3433 case 0:
3434 /*
3435 * New allocation is not contiguous with another
3436 * real allocation.
3437 * Insert a new entry.
3438 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003439 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3440 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3441 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3442 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003443 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003444 } else {
3445 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003446 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003447 new->br_startoff,
3448 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003449 new->br_blockcount, &i);
3450 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003451 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003452 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003453 bma->cur->bc_rec.b.br_state = new->br_state;
3454 error = xfs_btree_insert(bma->cur, &i);
3455 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003456 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003457 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003461
3462 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003463 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003464 int tmp_logflags; /* partial log flag return val */
3465
Christoph Hellwigc6534242011-09-18 20:41:05 +00003466 ASSERT(bma->cur == NULL);
3467 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3468 bma->firstblock, bma->flist, &bma->cur,
3469 0, &tmp_logflags, whichfork);
3470 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003471 if (error)
3472 goto done;
3473 }
3474
3475 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003476 if (bma->cur)
3477 bma->cur->bc_private.b.allocated = 0;
3478
3479 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003480done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003481 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003482 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484
Nathan Scottdd9f4382006-01-11 15:28:28 +11003485/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003486 * Functions used in the extent read, allocate and remove paths
3487 */
3488
3489/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003490 * Adjust the size of the new extent based on di_extsize and rt extsize.
3491 */
3492STATIC int
3493xfs_bmap_extsize_align(
3494 xfs_mount_t *mp,
3495 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3496 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3497 xfs_extlen_t extsz, /* align to this extent size */
3498 int rt, /* is this a realtime inode? */
3499 int eof, /* is extent at end-of-file? */
3500 int delay, /* creating delalloc extent? */
3501 int convert, /* overwriting unwritten extent? */
3502 xfs_fileoff_t *offp, /* in/out: aligned offset */
3503 xfs_extlen_t *lenp) /* in/out: aligned length */
3504{
3505 xfs_fileoff_t orig_off; /* original offset */
3506 xfs_extlen_t orig_alen; /* original length */
3507 xfs_fileoff_t orig_end; /* original off+len */
3508 xfs_fileoff_t nexto; /* next file offset */
3509 xfs_fileoff_t prevo; /* previous file offset */
3510 xfs_fileoff_t align_off; /* temp for offset */
3511 xfs_extlen_t align_alen; /* temp for length */
3512 xfs_extlen_t temp; /* temp for calculations */
3513
3514 if (convert)
3515 return 0;
3516
3517 orig_off = align_off = *offp;
3518 orig_alen = align_alen = *lenp;
3519 orig_end = orig_off + orig_alen;
3520
3521 /*
3522 * If this request overlaps an existing extent, then don't
3523 * attempt to perform any additional alignment.
3524 */
3525 if (!delay && !eof &&
3526 (orig_off >= gotp->br_startoff) &&
3527 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3528 return 0;
3529 }
3530
3531 /*
3532 * If the file offset is unaligned vs. the extent size
3533 * we need to align it. This will be possible unless
3534 * the file was previously written with a kernel that didn't
3535 * perform this alignment, or if a truncate shot us in the
3536 * foot.
3537 */
3538 temp = do_mod(orig_off, extsz);
3539 if (temp) {
3540 align_alen += temp;
3541 align_off -= temp;
3542 }
3543 /*
3544 * Same adjustment for the end of the requested area.
3545 */
3546 if ((temp = (align_alen % extsz))) {
3547 align_alen += extsz - temp;
3548 }
3549 /*
3550 * If the previous block overlaps with this proposed allocation
3551 * then move the start forward without adjusting the length.
3552 */
3553 if (prevp->br_startoff != NULLFILEOFF) {
3554 if (prevp->br_startblock == HOLESTARTBLOCK)
3555 prevo = prevp->br_startoff;
3556 else
3557 prevo = prevp->br_startoff + prevp->br_blockcount;
3558 } else
3559 prevo = 0;
3560 if (align_off != orig_off && align_off < prevo)
3561 align_off = prevo;
3562 /*
3563 * If the next block overlaps with this proposed allocation
3564 * then move the start back without adjusting the length,
3565 * but not before offset 0.
3566 * This may of course make the start overlap previous block,
3567 * and if we hit the offset 0 limit then the next block
3568 * can still overlap too.
3569 */
3570 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3571 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3572 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3573 nexto = gotp->br_startoff + gotp->br_blockcount;
3574 else
3575 nexto = gotp->br_startoff;
3576 } else
3577 nexto = NULLFILEOFF;
3578 if (!eof &&
3579 align_off + align_alen != orig_end &&
3580 align_off + align_alen > nexto)
3581 align_off = nexto > align_alen ? nexto - align_alen : 0;
3582 /*
3583 * If we're now overlapping the next or previous extent that
3584 * means we can't fit an extsz piece in this hole. Just move
3585 * the start forward to the first valid spot and set
3586 * the length so we hit the end.
3587 */
3588 if (align_off != orig_off && align_off < prevo)
3589 align_off = prevo;
3590 if (align_off + align_alen != orig_end &&
3591 align_off + align_alen > nexto &&
3592 nexto != NULLFILEOFF) {
3593 ASSERT(nexto > prevo);
3594 align_alen = nexto - align_off;
3595 }
3596
3597 /*
3598 * If realtime, and the result isn't a multiple of the realtime
3599 * extent size we need to remove blocks until it is.
3600 */
3601 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3602 /*
3603 * We're not covering the original request, or
3604 * we won't be able to once we fix the length.
3605 */
3606 if (orig_off < align_off ||
3607 orig_end > align_off + align_alen ||
3608 align_alen - temp < orig_alen)
3609 return XFS_ERROR(EINVAL);
3610 /*
3611 * Try to fix it by moving the start up.
3612 */
3613 if (align_off + temp <= orig_off) {
3614 align_alen -= temp;
3615 align_off += temp;
3616 }
3617 /*
3618 * Try to fix it by moving the end in.
3619 */
3620 else if (align_off + align_alen - temp >= orig_end)
3621 align_alen -= temp;
3622 /*
3623 * Set the start to the minimum then trim the length.
3624 */
3625 else {
3626 align_alen -= orig_off - align_off;
3627 align_off = orig_off;
3628 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3629 }
3630 /*
3631 * Result doesn't cover the request, fail it.
3632 */
3633 if (orig_off < align_off || orig_end > align_off + align_alen)
3634 return XFS_ERROR(EINVAL);
3635 } else {
3636 ASSERT(orig_off >= align_off);
3637 ASSERT(orig_end <= align_off + align_alen);
3638 }
3639
3640#ifdef DEBUG
3641 if (!eof && gotp->br_startoff != NULLFILEOFF)
3642 ASSERT(align_off + align_alen <= gotp->br_startoff);
3643 if (prevp->br_startoff != NULLFILEOFF)
3644 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3645#endif
3646
3647 *lenp = align_alen;
3648 *offp = align_off;
3649 return 0;
3650}
3651
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652#define XFS_ALLOC_GAP_UNITS 4
3653
David Chinnerc2b1cba2008-04-10 12:21:40 +10003654STATIC void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003655xfs_bmap_adjacent(
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
3657{
3658 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3660 xfs_mount_t *mp; /* mount point structure */
3661 int nullfb; /* true if ap->firstblock isn't set */
3662 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
3664#define ISVALID(x,y) \
3665 (rt ? \
3666 (x) < mp->m_sb.sb_rblocks : \
3667 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3668 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3669 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003672 nullfb = *ap->firstblock == NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003674 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 /*
3676 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003677 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003679 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3680 !isnullstartblock(ap->prev.br_startblock) &&
3681 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3682 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003683 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 /*
3685 * Adjust for the gap between prevp and us.
3686 */
Dave Chinner3a756672011-09-18 20:40:58 +00003687 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003688 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003690 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3691 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 }
3693 /*
3694 * If not at eof, then compare the two neighbor blocks.
3695 * Figure out whether either one gives us a good starting point,
3696 * and pick the better one.
3697 */
3698 else if (!ap->eof) {
3699 xfs_fsblock_t gotbno; /* right side block number */
3700 xfs_fsblock_t gotdiff=0; /* right side difference */
3701 xfs_fsblock_t prevbno; /* left side block number */
3702 xfs_fsblock_t prevdiff=0; /* left side difference */
3703
3704 /*
3705 * If there's a previous (left) block, select a requested
3706 * start block based on it.
3707 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003708 if (ap->prev.br_startoff != NULLFILEOFF &&
3709 !isnullstartblock(ap->prev.br_startblock) &&
3710 (prevbno = ap->prev.br_startblock +
3711 ap->prev.br_blockcount) &&
3712 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 /*
3714 * Calculate gap to end of previous block.
3715 */
Dave Chinner3a756672011-09-18 20:40:58 +00003716 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003717 (ap->prev.br_startoff +
3718 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 /*
3720 * Figure the startblock based on the previous block's
3721 * end and the gap size.
3722 * Heuristic!
3723 * If the gap is large relative to the piece we're
3724 * allocating, or using it gives us an invalid block
3725 * number, then just use the end of the previous block.
3726 */
Dave Chinner3a756672011-09-18 20:40:58 +00003727 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003729 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 prevbno += adjust;
3731 else
3732 prevdiff += adjust;
3733 /*
3734 * If the firstblock forbids it, can't use it,
3735 * must use default.
3736 */
3737 if (!rt && !nullfb &&
3738 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3739 prevbno = NULLFSBLOCK;
3740 }
3741 /*
3742 * No previous block or can't follow it, just default.
3743 */
3744 else
3745 prevbno = NULLFSBLOCK;
3746 /*
3747 * If there's a following (right) block, select a requested
3748 * start block based on it.
3749 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003750 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 /*
3752 * Calculate gap to start of next block.
3753 */
Dave Chinner3a756672011-09-18 20:40:58 +00003754 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 /*
3756 * Figure the startblock based on the next block's
3757 * start and the gap size.
3758 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003759 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 /*
3761 * Heuristic!
3762 * If the gap is large relative to the piece we're
3763 * allocating, or using it gives us an invalid block
3764 * number, then just use the start of the next block
3765 * offset by our length.
3766 */
Dave Chinner3a756672011-09-18 20:40:58 +00003767 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 ISVALID(gotbno - gotdiff, gotbno))
3769 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003770 else if (ISVALID(gotbno - ap->length, gotbno)) {
3771 gotbno -= ap->length;
3772 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 } else
3774 gotdiff += adjust;
3775 /*
3776 * If the firstblock forbids it, can't use it,
3777 * must use default.
3778 */
3779 if (!rt && !nullfb &&
3780 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3781 gotbno = NULLFSBLOCK;
3782 }
3783 /*
3784 * No next block, just default.
3785 */
3786 else
3787 gotbno = NULLFSBLOCK;
3788 /*
3789 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003790 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 */
3792 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003793 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003795 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003797 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003799#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003800}
3801
3802STATIC int
3803xfs_bmap_rtalloc(
3804 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
3805{
3806 xfs_alloctype_t atype = 0; /* type for allocation routines */
3807 int error; /* error return value */
3808 xfs_mount_t *mp; /* mount point structure */
3809 xfs_extlen_t prod = 0; /* product factor for allocators */
3810 xfs_extlen_t ralen = 0; /* realtime allocation length */
3811 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003812 xfs_rtblock_t rtb;
3813
3814 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10003815 align = xfs_get_extsz_hint(ap->ip);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003816 prod = align / mp->m_sb.sb_rextsize;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003817 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003818 align, 1, ap->eof, 0,
Dave Chinner3a756672011-09-18 20:40:58 +00003819 ap->conv, &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003820 if (error)
3821 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00003822 ASSERT(ap->length);
3823 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003824
3825 /*
3826 * If the offset & length are not perfectly aligned
3827 * then kill prod, it will just get us in trouble.
3828 */
Dave Chinner3a756672011-09-18 20:40:58 +00003829 if (do_mod(ap->offset, align) || ap->length % align)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003830 prod = 1;
3831 /*
3832 * Set ralen to be the actual requested length in rtextents.
3833 */
Dave Chinner3a756672011-09-18 20:40:58 +00003834 ralen = ap->length / mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003835 /*
3836 * If the old value was close enough to MAXEXTLEN that
3837 * we rounded up to it, cut it back so it's valid again.
3838 * Note that if it's a really large request (bigger than
3839 * MAXEXTLEN), we don't hear about that number, and can't
3840 * adjust the starting point to match it.
3841 */
3842 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
3843 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
Christoph Hellwig04e99452011-01-25 09:06:19 +00003844
3845 /*
3846 * Lock out other modifications to the RT bitmap inode.
3847 */
Christoph Hellwig1050c712011-02-13 13:25:31 +00003848 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwigddc34152011-09-19 15:00:54 +00003849 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Christoph Hellwig04e99452011-01-25 09:06:19 +00003850
Nathan Scotta365bdd2006-03-14 13:34:16 +11003851 /*
3852 * If it's an allocation to an empty file at offset 0,
3853 * pick an extent that will space things out in the rt area.
3854 */
Dave Chinner3a756672011-09-18 20:40:58 +00003855 if (ap->eof && ap->offset == 0) {
Andrew Morton0892ccd2007-06-28 16:46:56 +10003856 xfs_rtblock_t uninitialized_var(rtx); /* realtime extent no */
3857
Nathan Scotta365bdd2006-03-14 13:34:16 +11003858 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
3859 if (error)
3860 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00003861 ap->blkno = rtx * mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003862 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003863 ap->blkno = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003864 }
3865
3866 xfs_bmap_adjacent(ap);
3867
3868 /*
3869 * Realtime allocation, done through xfs_rtallocate_extent.
3870 */
Dave Chinner3a756672011-09-18 20:40:58 +00003871 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
3872 do_div(ap->blkno, mp->m_sb.sb_rextsize);
3873 rtb = ap->blkno;
3874 ap->length = ralen;
3875 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003876 &ralen, atype, ap->wasdel, prod, &rtb)))
3877 return error;
3878 if (rtb == NULLFSBLOCK && prod > 1 &&
Dave Chinner3a756672011-09-18 20:40:58 +00003879 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
3880 ap->length, &ralen, atype,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003881 ap->wasdel, 1, &rtb)))
3882 return error;
Dave Chinner3a756672011-09-18 20:40:58 +00003883 ap->blkno = rtb;
3884 if (ap->blkno != NULLFSBLOCK) {
3885 ap->blkno *= mp->m_sb.sb_rextsize;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003886 ralen *= mp->m_sb.sb_rextsize;
Dave Chinner3a756672011-09-18 20:40:58 +00003887 ap->length = ralen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003888 ap->ip->i_d.di_nblocks += ralen;
3889 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3890 if (ap->wasdel)
3891 ap->ip->i_delayed_blks -= ralen;
3892 /*
3893 * Adjust the disk quota also. This was reserved
3894 * earlier.
3895 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003896 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003897 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
3898 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
3899 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003900 ap->length = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003901 }
3902 return 0;
3903}
3904
3905STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003906xfs_bmap_btalloc_nullfb(
3907 struct xfs_bmalloca *ap,
3908 struct xfs_alloc_arg *args,
3909 xfs_extlen_t *blen)
3910{
3911 struct xfs_mount *mp = ap->ip->i_mount;
3912 struct xfs_perag *pag;
3913 xfs_agnumber_t ag, startag;
3914 int notinit = 0;
3915 int error;
3916
3917 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3918 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3919 else
3920 args->type = XFS_ALLOCTYPE_START_BNO;
3921 args->total = ap->total;
3922
3923 /*
3924 * Search for an allocation group with a single extent large enough
3925 * for the request. If one isn't found, then adjust the minimum
3926 * allocation size to the largest space found.
3927 */
3928 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3929 if (startag == NULLAGNUMBER)
3930 startag = ag = 0;
3931
3932 pag = xfs_perag_get(mp, ag);
Dave Chinner14b064c2011-01-27 12:16:28 +11003933 while (*blen < args->maxlen) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00003934 if (!pag->pagf_init) {
3935 error = xfs_alloc_pagf_init(mp, args->tp, ag,
3936 XFS_ALLOC_FLAG_TRYLOCK);
3937 if (error) {
3938 xfs_perag_put(pag);
3939 return error;
3940 }
3941 }
3942
3943 /*
3944 * See xfs_alloc_fix_freelist...
3945 */
3946 if (pag->pagf_init) {
3947 xfs_extlen_t longest;
3948 longest = xfs_alloc_longest_free_extent(mp, pag);
3949 if (*blen < longest)
3950 *blen = longest;
3951 } else
3952 notinit = 1;
3953
3954 if (xfs_inode_is_filestream(ap->ip)) {
Dave Chinner14b064c2011-01-27 12:16:28 +11003955 if (*blen >= args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00003956 break;
3957
3958 if (ap->userdata) {
3959 /*
3960 * If startag is an invalid AG, we've
3961 * come here once before and
3962 * xfs_filestream_new_ag picked the
3963 * best currently available.
3964 *
3965 * Don't continue looping, since we
3966 * could loop forever.
3967 */
3968 if (startag == NULLAGNUMBER)
3969 break;
3970
3971 error = xfs_filestream_new_ag(ap, &ag);
3972 xfs_perag_put(pag);
3973 if (error)
3974 return error;
3975
3976 /* loop again to set 'blen'*/
3977 startag = NULLAGNUMBER;
3978 pag = xfs_perag_get(mp, ag);
3979 continue;
3980 }
3981 }
3982 if (++ag == mp->m_sb.sb_agcount)
3983 ag = 0;
3984 if (ag == startag)
3985 break;
3986 xfs_perag_put(pag);
3987 pag = xfs_perag_get(mp, ag);
3988 }
3989 xfs_perag_put(pag);
3990
3991 /*
3992 * Since the above loop did a BUF_TRYLOCK, it is
3993 * possible that there is space for this request.
3994 */
3995 if (notinit || *blen < ap->minlen)
3996 args->minlen = ap->minlen;
3997 /*
3998 * If the best seen length is less than the request
3999 * length, use the best as the minimum.
4000 */
Dave Chinner14b064c2011-01-27 12:16:28 +11004001 else if (*blen < args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00004002 args->minlen = *blen;
4003 /*
Dave Chinner14b064c2011-01-27 12:16:28 +11004004 * Otherwise we've seen an extent as big as maxlen,
Christoph Hellwigc467c042010-02-15 23:34:42 +00004005 * use that as the minimum.
4006 */
4007 else
Dave Chinner14b064c2011-01-27 12:16:28 +11004008 args->minlen = args->maxlen;
Christoph Hellwigc467c042010-02-15 23:34:42 +00004009
4010 /*
4011 * set the failure fallback case to look in the selected
4012 * AG as the stream may have moved.
4013 */
4014 if (xfs_inode_is_filestream(ap->ip))
Dave Chinner3a756672011-09-18 20:40:58 +00004015 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00004016
4017 return 0;
4018}
4019
4020STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11004021xfs_bmap_btalloc(
4022 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
4023{
4024 xfs_mount_t *mp; /* mount point structure */
4025 xfs_alloctype_t atype = 0; /* type for allocation routines */
4026 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11004027 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00004028 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004029 xfs_alloc_arg_t args;
4030 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004031 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004032 int nullfb; /* true if ap->firstblock isn't set */
4033 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004034 int tryagain;
4035 int error;
4036
Dave Chinnera99ebf42011-12-01 11:24:20 +00004037 ASSERT(ap->length);
4038
Nathan Scotta365bdd2006-03-14 13:34:16 +11004039 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10004040 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004041 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004042 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11004043 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00004044 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11004045 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00004046 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11004047 }
Dave Chinner0937e0f2011-09-18 20:40:57 +00004048 nullfb = *ap->firstblock == NULLFSBLOCK;
4049 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10004050 if (nullfb) {
4051 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
4052 ag = xfs_filestream_lookup_ag(ap->ip);
4053 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00004054 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10004055 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00004056 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10004057 }
4058 } else
Dave Chinner3a756672011-09-18 20:40:58 +00004059 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004060
4061 xfs_bmap_adjacent(ap);
4062
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 /*
Dave Chinner3a756672011-09-18 20:40:58 +00004064 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 * it's in the right allocation group.
4066 */
Dave Chinner3a756672011-09-18 20:40:58 +00004067 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 ;
4069 else
Dave Chinner3a756672011-09-18 20:40:58 +00004070 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 * Normal allocation, done through xfs_alloc_vextent.
4073 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11004074 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05004075 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11004076 args.tp = ap->tp;
4077 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00004078 args.fsbno = ap->blkno;
Dave Chinner14b064c2011-01-27 12:16:28 +11004079
4080 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner3a756672011-09-18 20:40:58 +00004081 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
Dave Chinner0937e0f2011-09-18 20:40:57 +00004082 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004083 blen = 0;
4084 if (nullfb) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00004085 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
4086 if (error)
4087 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004088 } else if (ap->flist->xbf_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10004089 if (xfs_inode_is_filestream(ap->ip))
4090 args.type = XFS_ALLOCTYPE_FIRST_AG;
4091 else
4092 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004093 args.total = args.minlen = ap->minlen;
4094 } else {
4095 args.type = XFS_ALLOCTYPE_NEAR_BNO;
4096 args.total = ap->total;
4097 args.minlen = ap->minlen;
4098 }
David Chinner957d0eb2007-06-18 16:50:37 +10004099 /* apply extent size hints if obtained earlier */
4100 if (unlikely(align)) {
4101 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00004102 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11004103 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11004104 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11004105 args.prod = 1;
4106 args.mod = 0;
4107 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11004108 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00004109 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11004110 args.mod = (xfs_extlen_t)(args.prod - args.mod);
4111 }
4112 /*
4113 * If we are not low on available data blocks, and the
4114 * underlying logical volume manager is a stripe, and
4115 * the file offset is zero then try to allocate data
4116 * blocks on stripe unit boundary.
4117 * NOTE: ap->aeof is only set if the allocation length
4118 * is >= the stripe unit and the allocation offset is
4119 * at the end of file.
4120 */
Dave Chinner0937e0f2011-09-18 20:40:57 +00004121 if (!ap->flist->xbf_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00004122 if (!ap->offset) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11004123 args.alignment = mp->m_dalign;
4124 atype = args.type;
4125 isaligned = 1;
4126 /*
4127 * Adjust for alignment
4128 */
Dave Chinner14b064c2011-01-27 12:16:28 +11004129 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11004130 args.minlen = blen - args.alignment;
4131 args.minalignslop = 0;
4132 } else {
4133 /*
4134 * First try an exact bno allocation.
4135 * If it fails then do a near or start bno
4136 * allocation with alignment turned on.
4137 */
4138 atype = args.type;
4139 tryagain = 1;
4140 args.type = XFS_ALLOCTYPE_THIS_BNO;
4141 args.alignment = 1;
4142 /*
4143 * Compute the minlen+alignment for the
4144 * next case. Set slop so that the value
4145 * of minlen+alignment+slop doesn't go up
4146 * between the calls.
4147 */
Dave Chinner14b064c2011-01-27 12:16:28 +11004148 if (blen > mp->m_dalign && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11004149 nextminlen = blen - mp->m_dalign;
4150 else
4151 nextminlen = args.minlen;
4152 if (nextminlen + mp->m_dalign > args.minlen + 1)
4153 args.minalignslop =
4154 nextminlen + mp->m_dalign -
4155 args.minlen - 1;
4156 else
4157 args.minalignslop = 0;
4158 }
4159 } else {
4160 args.alignment = 1;
4161 args.minalignslop = 0;
4162 }
4163 args.minleft = ap->minleft;
4164 args.wasdel = ap->wasdel;
4165 args.isfl = 0;
4166 args.userdata = ap->userdata;
4167 if ((error = xfs_alloc_vextent(&args)))
4168 return error;
4169 if (tryagain && args.fsbno == NULLFSBLOCK) {
4170 /*
4171 * Exact allocation failed. Now try with alignment
4172 * turned on.
4173 */
4174 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00004175 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004176 args.alignment = mp->m_dalign;
4177 args.minlen = nextminlen;
4178 args.minalignslop = 0;
4179 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 if ((error = xfs_alloc_vextent(&args)))
4181 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004182 }
4183 if (isaligned && args.fsbno == NULLFSBLOCK) {
4184 /*
4185 * allocation failed, so turn off alignment and
4186 * try again.
4187 */
4188 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00004189 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004190 args.alignment = 0;
4191 if ((error = xfs_alloc_vextent(&args)))
4192 return error;
4193 }
4194 if (args.fsbno == NULLFSBLOCK && nullfb &&
4195 args.minlen > ap->minlen) {
4196 args.minlen = ap->minlen;
4197 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00004198 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004199 if ((error = xfs_alloc_vextent(&args)))
4200 return error;
4201 }
4202 if (args.fsbno == NULLFSBLOCK && nullfb) {
4203 args.fsbno = 0;
4204 args.type = XFS_ALLOCTYPE_FIRST_AG;
4205 args.total = ap->minlen;
4206 args.minleft = 0;
4207 if ((error = xfs_alloc_vextent(&args)))
4208 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004209 ap->flist->xbf_low = 1;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004210 }
4211 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004212 /*
4213 * check the allocation happened at the same or higher AG than
4214 * the first block that was allocated.
4215 */
4216 ASSERT(*ap->firstblock == NULLFSBLOCK ||
4217 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
4218 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
4219 (ap->flist->xbf_low &&
4220 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
4221 XFS_FSB_TO_AGNO(mp, args.fsbno)));
4222
Dave Chinner3a756672011-09-18 20:40:58 +00004223 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004224 if (*ap->firstblock == NULLFSBLOCK)
4225 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004226 ASSERT(nullfb || fb_agno == args.agno ||
Dave Chinner0937e0f2011-09-18 20:40:57 +00004227 (ap->flist->xbf_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00004228 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004229 ap->ip->i_d.di_nblocks += args.len;
4230 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
4231 if (ap->wasdel)
4232 ap->ip->i_delayed_blks -= args.len;
4233 /*
4234 * Adjust the disk quota also. This was reserved
4235 * earlier.
4236 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02004237 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11004238 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
4239 XFS_TRANS_DQ_BCOUNT,
4240 (long) args.len);
4241 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00004242 ap->blkno = NULLFSBLOCK;
4243 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 }
4245 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11004246}
4247
4248/*
4249 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
4250 * It figures out where to ask the underlying allocator to put the new extent.
4251 */
4252STATIC int
4253xfs_bmap_alloc(
4254 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
4255{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11004256 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11004257 return xfs_bmap_rtalloc(ap);
4258 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259}
4260
4261/*
Dave Chinneraef9a892011-09-18 20:40:44 +00004262 * Trim the returned map to the required bounds
4263 */
4264STATIC void
4265xfs_bmapi_trim_map(
4266 struct xfs_bmbt_irec *mval,
4267 struct xfs_bmbt_irec *got,
4268 xfs_fileoff_t *bno,
4269 xfs_filblks_t len,
4270 xfs_fileoff_t obno,
4271 xfs_fileoff_t end,
4272 int n,
4273 int flags)
4274{
4275 if ((flags & XFS_BMAPI_ENTIRE) ||
4276 got->br_startoff + got->br_blockcount <= obno) {
4277 *mval = *got;
4278 if (isnullstartblock(got->br_startblock))
4279 mval->br_startblock = DELAYSTARTBLOCK;
4280 return;
4281 }
4282
4283 if (obno > *bno)
4284 *bno = obno;
4285 ASSERT((*bno >= obno) || (n == 0));
4286 ASSERT(*bno < end);
4287 mval->br_startoff = *bno;
4288 if (isnullstartblock(got->br_startblock))
4289 mval->br_startblock = DELAYSTARTBLOCK;
4290 else
4291 mval->br_startblock = got->br_startblock +
4292 (*bno - got->br_startoff);
4293 /*
4294 * Return the minimum of what we got and what we asked for for
4295 * the length. We can use the len variable here because it is
4296 * modified below and we could have been there before coming
4297 * here if the first part of the allocation didn't overlap what
4298 * was asked for.
4299 */
4300 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4301 got->br_blockcount - (*bno - got->br_startoff));
4302 mval->br_state = got->br_state;
4303 ASSERT(mval->br_blockcount <= len);
4304 return;
4305}
4306
4307/*
4308 * Update and validate the extent map to return
4309 */
4310STATIC void
4311xfs_bmapi_update_map(
4312 struct xfs_bmbt_irec **map,
4313 xfs_fileoff_t *bno,
4314 xfs_filblks_t *len,
4315 xfs_fileoff_t obno,
4316 xfs_fileoff_t end,
4317 int *n,
4318 int flags)
4319{
4320 xfs_bmbt_irec_t *mval = *map;
4321
4322 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4323 ((mval->br_startoff + mval->br_blockcount) <= end));
4324 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4325 (mval->br_startoff < obno));
4326
4327 *bno = mval->br_startoff + mval->br_blockcount;
4328 *len = end - *bno;
4329 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4330 /* update previous map with new information */
4331 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4332 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4333 ASSERT(mval->br_state == mval[-1].br_state);
4334 mval[-1].br_blockcount = mval->br_blockcount;
4335 mval[-1].br_state = mval->br_state;
4336 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4337 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4338 mval[-1].br_startblock != HOLESTARTBLOCK &&
4339 mval->br_startblock == mval[-1].br_startblock +
4340 mval[-1].br_blockcount &&
4341 ((flags & XFS_BMAPI_IGSTATE) ||
4342 mval[-1].br_state == mval->br_state)) {
4343 ASSERT(mval->br_startoff ==
4344 mval[-1].br_startoff + mval[-1].br_blockcount);
4345 mval[-1].br_blockcount += mval->br_blockcount;
4346 } else if (*n > 0 &&
4347 mval->br_startblock == DELAYSTARTBLOCK &&
4348 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4349 mval->br_startoff ==
4350 mval[-1].br_startoff + mval[-1].br_blockcount) {
4351 mval[-1].br_blockcount += mval->br_blockcount;
4352 mval[-1].br_state = mval->br_state;
4353 } else if (!((*n == 0) &&
4354 ((mval->br_startoff + mval->br_blockcount) <=
4355 obno))) {
4356 mval++;
4357 (*n)++;
4358 }
4359 *map = mval;
4360}
4361
4362/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004363 * Map file blocks to filesystem blocks without allocation.
4364 */
4365int
4366xfs_bmapi_read(
4367 struct xfs_inode *ip,
4368 xfs_fileoff_t bno,
4369 xfs_filblks_t len,
4370 struct xfs_bmbt_irec *mval,
4371 int *nmap,
4372 int flags)
4373{
4374 struct xfs_mount *mp = ip->i_mount;
4375 struct xfs_ifork *ifp;
4376 struct xfs_bmbt_irec got;
4377 struct xfs_bmbt_irec prev;
4378 xfs_fileoff_t obno;
4379 xfs_fileoff_t end;
4380 xfs_extnum_t lastx;
4381 int error;
4382 int eof;
4383 int n = 0;
4384 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4385 XFS_ATTR_FORK : XFS_DATA_FORK;
4386
4387 ASSERT(*nmap >= 1);
4388 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4389 XFS_BMAPI_IGSTATE)));
4390
4391 if (unlikely(XFS_TEST_ERROR(
4392 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4393 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4394 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4395 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4396 return XFS_ERROR(EFSCORRUPTED);
4397 }
4398
4399 if (XFS_FORCED_SHUTDOWN(mp))
4400 return XFS_ERROR(EIO);
4401
4402 XFS_STATS_INC(xs_blk_mapr);
4403
4404 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004405
4406 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4407 error = xfs_iread_extents(NULL, ip, whichfork);
4408 if (error)
4409 return error;
4410 }
4411
4412 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4413 end = bno + len;
4414 obno = bno;
4415
4416 while (bno < end && n < *nmap) {
4417 /* Reading past eof, act as though there's a hole up to end. */
4418 if (eof)
4419 got.br_startoff = end;
4420 if (got.br_startoff > bno) {
4421 /* Reading in a hole. */
4422 mval->br_startoff = bno;
4423 mval->br_startblock = HOLESTARTBLOCK;
4424 mval->br_blockcount =
4425 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4426 mval->br_state = XFS_EXT_NORM;
4427 bno += mval->br_blockcount;
4428 len -= mval->br_blockcount;
4429 mval++;
4430 n++;
4431 continue;
4432 }
4433
4434 /* set up the extent map to return. */
4435 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4436 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4437
4438 /* If we're done, stop now. */
4439 if (bno >= end || n >= *nmap)
4440 break;
4441
4442 /* Else go on to the next record. */
4443 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4444 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4445 else
4446 eof = 1;
4447 }
4448 *nmap = n;
4449 return 0;
4450}
4451
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004452STATIC int
4453xfs_bmapi_reserve_delalloc(
4454 struct xfs_inode *ip,
4455 xfs_fileoff_t aoff,
4456 xfs_filblks_t len,
4457 struct xfs_bmbt_irec *got,
4458 struct xfs_bmbt_irec *prev,
4459 xfs_extnum_t *lastx,
4460 int eof)
4461{
4462 struct xfs_mount *mp = ip->i_mount;
4463 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4464 xfs_extlen_t alen;
4465 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004466 char rt = XFS_IS_REALTIME_INODE(ip);
4467 xfs_extlen_t extsz;
4468 int error;
4469
4470 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4471 if (!eof)
4472 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4473
4474 /* Figure out the extent size, adjust alen */
4475 extsz = xfs_get_extsz_hint(ip);
4476 if (extsz) {
4477 /*
4478 * Make sure we don't exceed a single extent length when we
4479 * align the extent by reducing length we are going to
4480 * allocate by the maximum amount extent size aligment may
4481 * require.
4482 */
4483 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4484 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4485 1, 0, &aoff, &alen);
4486 ASSERT(!error);
4487 }
4488
4489 if (rt)
4490 extsz = alen / mp->m_sb.sb_rextsize;
4491
4492 /*
4493 * Make a transaction-less quota reservation for delayed allocation
4494 * blocks. This number gets adjusted later. We return if we haven't
4495 * allocated blocks already inside this loop.
4496 */
4497 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4498 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4499 if (error)
4500 return error;
4501
4502 /*
4503 * Split changing sb for alen and indlen since they could be coming
4504 * from different places.
4505 */
4506 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4507 ASSERT(indlen > 0);
4508
4509 if (rt) {
4510 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4511 -((int64_t)extsz), 0);
4512 } else {
4513 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4514 -((int64_t)alen), 0);
4515 }
4516
4517 if (error)
4518 goto out_unreserve_quota;
4519
4520 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4521 -((int64_t)indlen), 0);
4522 if (error)
4523 goto out_unreserve_blocks;
4524
4525
4526 ip->i_delayed_blks += alen;
4527
4528 got->br_startoff = aoff;
4529 got->br_startblock = nullstartblock(indlen);
4530 got->br_blockcount = alen;
4531 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004532 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004533
4534 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004535 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4536 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004537 */
4538 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4539
4540 ASSERT(got->br_startoff <= aoff);
4541 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4542 ASSERT(isnullstartblock(got->br_startblock));
4543 ASSERT(got->br_state == XFS_EXT_NORM);
4544 return 0;
4545
4546out_unreserve_blocks:
4547 if (rt)
4548 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4549 else
4550 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4551out_unreserve_quota:
4552 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004553 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004554 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4555 return error;
4556}
4557
Dave Chinner5c8ed202011-09-18 20:40:45 +00004558/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004559 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4560 */
4561int
4562xfs_bmapi_delay(
4563 struct xfs_inode *ip, /* incore inode */
4564 xfs_fileoff_t bno, /* starting file offs. mapped */
4565 xfs_filblks_t len, /* length to map in file */
4566 struct xfs_bmbt_irec *mval, /* output: map values */
4567 int *nmap, /* i/o: mval size/count */
4568 int flags) /* XFS_BMAPI_... */
4569{
4570 struct xfs_mount *mp = ip->i_mount;
4571 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4572 struct xfs_bmbt_irec got; /* current file extent record */
4573 struct xfs_bmbt_irec prev; /* previous file extent record */
4574 xfs_fileoff_t obno; /* old block number (offset) */
4575 xfs_fileoff_t end; /* end of mapped file region */
4576 xfs_extnum_t lastx; /* last useful extent number */
4577 int eof; /* we've hit the end of extents */
4578 int n = 0; /* current extent index */
4579 int error = 0;
4580
4581 ASSERT(*nmap >= 1);
4582 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4583 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4584
4585 if (unlikely(XFS_TEST_ERROR(
4586 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4587 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4588 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4589 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4590 return XFS_ERROR(EFSCORRUPTED);
4591 }
4592
4593 if (XFS_FORCED_SHUTDOWN(mp))
4594 return XFS_ERROR(EIO);
4595
4596 XFS_STATS_INC(xs_blk_mapw);
4597
4598 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4599 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4600 if (error)
4601 return error;
4602 }
4603
4604 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4605 end = bno + len;
4606 obno = bno;
4607
4608 while (bno < end && n < *nmap) {
4609 if (eof || got.br_startoff > bno) {
4610 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4611 &prev, &lastx, eof);
4612 if (error) {
4613 if (n == 0) {
4614 *nmap = 0;
4615 return error;
4616 }
4617 break;
4618 }
4619 }
4620
4621 /* set up the extent map to return. */
4622 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4623 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4624
4625 /* If we're done, stop now. */
4626 if (bno >= end || n >= *nmap)
4627 break;
4628
4629 /* Else go on to the next record. */
4630 prev = got;
4631 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4632 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4633 else
4634 eof = 1;
4635 }
4636
4637 *nmap = n;
4638 return 0;
4639}
4640
4641
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004642STATIC int
Dave Chinnere04426b2012-10-05 11:06:59 +10004643__xfs_bmapi_allocate(
4644 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004645{
4646 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004647 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004648 XFS_ATTR_FORK : XFS_DATA_FORK;
4649 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004650 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004651 int error;
4652 int rt;
4653
Dave Chinnera99ebf42011-12-01 11:24:20 +00004654 ASSERT(bma->length > 0);
4655
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004656 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip);
4657
4658 /*
4659 * For the wasdelay case, we could also just allocate the stuff asked
4660 * for in this bmap call but that wouldn't be as good.
4661 */
4662 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004663 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4664 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004665 if (bma->idx != NULLEXTNUM && bma->idx) {
4666 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004667 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004668 }
4669 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004670 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004671 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004672 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004673 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004674 }
4675
4676 /*
4677 * Indicate if this is the first user data in the file, or just any
4678 * user data.
4679 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004680 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004681 bma->userdata = (bma->offset == 0) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004682 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4683 }
4684
Dave Chinnere04426b2012-10-05 11:06:59 +10004685 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004686
4687 /*
4688 * Only want to do the alignment at the eof if it is userdata and
4689 * allocation length is larger than a stripe unit.
4690 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004691 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004692 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004693 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004694 if (error)
4695 return error;
4696 }
4697
4698 error = xfs_bmap_alloc(bma);
4699 if (error)
4700 return error;
4701
Dave Chinner0937e0f2011-09-18 20:40:57 +00004702 if (bma->flist->xbf_low)
4703 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004704 if (bma->cur)
4705 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004706 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004707 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004708 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4709 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4710 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4711 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004712 }
4713 /*
4714 * Bump the number of extents we've allocated
4715 * in this call.
4716 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004717 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004718
Dave Chinner29c8d172011-09-18 20:41:00 +00004719 if (bma->cur)
4720 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004721 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4722
Dave Chinner963c30c2011-09-18 20:40:59 +00004723 bma->got.br_startoff = bma->offset;
4724 bma->got.br_startblock = bma->blkno;
4725 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004726 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004727
4728 /*
4729 * A wasdelay extent has been initialized, so shouldn't be flagged
4730 * as unwritten.
4731 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004732 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004733 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004734 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004735
Christoph Hellwigc6534242011-09-18 20:41:05 +00004736 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004737 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004738 else
4739 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004740
Christoph Hellwigc315c902011-09-18 20:41:02 +00004741 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004742 if (error)
4743 return error;
4744
4745 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004746 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4747 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4748 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004749 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004750 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004751
Dave Chinner963c30c2011-09-18 20:40:59 +00004752 ASSERT(bma->got.br_startoff <= bma->offset);
4753 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4754 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004755 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4756 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004757 return 0;
4758}
4759
Dave Chinnere04426b2012-10-05 11:06:59 +10004760static void
4761xfs_bmapi_allocate_worker(
4762 struct work_struct *work)
4763{
4764 struct xfs_bmalloca *args = container_of(work,
4765 struct xfs_bmalloca, work);
4766 unsigned long pflags;
4767
4768 /* we are in a transaction context here */
4769 current_set_flags_nested(&pflags, PF_FSTRANS);
4770
4771 args->result = __xfs_bmapi_allocate(args);
4772 complete(args->done);
4773
4774 current_restore_flags_nested(&pflags, PF_FSTRANS);
4775}
4776
4777/*
4778 * Some allocation requests often come in with little stack to work on. Push
4779 * them off to a worker thread so there is lots of stack to use. Otherwise just
4780 * call directly to avoid the context switch overhead here.
4781 */
4782int
4783xfs_bmapi_allocate(
4784 struct xfs_bmalloca *args)
4785{
4786 DECLARE_COMPLETION_ONSTACK(done);
4787
4788 if (!args->stack_switch)
4789 return __xfs_bmapi_allocate(args);
4790
4791
4792 args->done = &done;
4793 INIT_WORK_ONSTACK(&args->work, xfs_bmapi_allocate_worker);
4794 queue_work(xfs_alloc_wq, &args->work);
4795 wait_for_completion(&done);
4796 return args->result;
4797}
4798
Dave Chinnerb447fe52011-09-18 20:40:51 +00004799STATIC int
4800xfs_bmapi_convert_unwritten(
4801 struct xfs_bmalloca *bma,
4802 struct xfs_bmbt_irec *mval,
4803 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004804 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004805{
4806 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4807 XFS_ATTR_FORK : XFS_DATA_FORK;
4808 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004809 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004810 int error;
4811
Dave Chinnerb447fe52011-09-18 20:40:51 +00004812 /* check if we need to do unwritten->real conversion */
4813 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4814 (flags & XFS_BMAPI_PREALLOC))
4815 return 0;
4816
4817 /* check if we need to do real->unwritten conversion */
4818 if (mval->br_state == XFS_EXT_NORM &&
4819 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4820 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4821 return 0;
4822
4823 /*
4824 * Modify (by adding) the state flag, if writing.
4825 */
4826 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004827 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4828 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004829 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004830 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4831 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004832 }
4833 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4834 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4835
Dave Chinnere0c3da52011-09-18 20:41:01 +00004836 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004837 &bma->cur, mval, bma->firstblock, bma->flist,
4838 &tmp_logflags);
4839 bma->logflags |= tmp_logflags;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004840 if (error)
4841 return error;
4842
4843 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004844 * Update our extent pointer, given that
4845 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4846 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004847 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004848 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004849
4850 /*
4851 * We may have combined previously unwritten space with written space,
4852 * so generate another request.
4853 */
4854 if (mval->br_blockcount < len)
4855 return EAGAIN;
4856 return 0;
4857}
4858
Christoph Hellwig44032802011-09-18 20:40:48 +00004859/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004860 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4861 * extent state if necessary. Details behaviour is controlled by the flags
4862 * parameter. Only allocates blocks from a single allocation group, to avoid
4863 * locking problems.
4864 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 * The returned value in "firstblock" from the first call in a transaction
4866 * must be remembered and presented to subsequent calls in "firstblock".
4867 * An upper bound for the number of blocks to be allocated is supplied to
4868 * the first call in "total"; if no allocation group has that many free
4869 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4870 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004871int
4872xfs_bmapi_write(
4873 struct xfs_trans *tp, /* transaction pointer */
4874 struct xfs_inode *ip, /* incore inode */
4875 xfs_fileoff_t bno, /* starting file offs. mapped */
4876 xfs_filblks_t len, /* length to map in file */
4877 int flags, /* XFS_BMAPI_... */
4878 xfs_fsblock_t *firstblock, /* first allocated block
4879 controls a.g. for allocs */
4880 xfs_extlen_t total, /* total blocks needed */
4881 struct xfs_bmbt_irec *mval, /* output: map values */
4882 int *nmap, /* i/o: mval size/count */
4883 struct xfs_bmap_free *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004885 struct xfs_mount *mp = ip->i_mount;
4886 struct xfs_ifork *ifp;
4887 struct xfs_bmalloca bma = { 0 }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004888 xfs_fileoff_t end; /* end of mapped file region */
4889 int eof; /* after the end of extents */
4890 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004891 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004892 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004893 int whichfork; /* data or attr fork */
4894 char inhole; /* current location is hole in file */
4895 char wasdelay; /* old extent was delayed */
4896
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004898 xfs_fileoff_t orig_bno; /* original block number value */
4899 int orig_flags; /* original flags arg value */
4900 xfs_filblks_t orig_len; /* original value of len arg */
4901 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4902 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903
4904 orig_bno = bno;
4905 orig_len = len;
4906 orig_flags = flags;
4907 orig_mval = mval;
4908 orig_nmap = *nmap;
4909#endif
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004910
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004912 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4913 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4914 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004915 ASSERT(len > 0);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004916
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4918 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004919
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 if (unlikely(XFS_TEST_ERROR(
4921 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4922 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4923 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4924 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004925 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926 return XFS_ERROR(EFSCORRUPTED);
4927 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004928
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 if (XFS_FORCED_SHUTDOWN(mp))
4930 return XFS_ERROR(EIO);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004931
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004933
4934 XFS_STATS_INC(xs_blk_mapw);
4935
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
Dave Chinner1e823792013-02-11 15:58:13 +11004937 /*
4938 * XXX (dgc): This assumes we are only called for inodes that
4939 * contain content neutral data in local format. Anything that
4940 * contains caller-specific data in local format that needs
4941 * transformation to move to a block format needs to do the
4942 * conversion to extent format itself.
4943 *
4944 * Directory data forks and attribute forks handle this
4945 * themselves, but with the addition of metadata verifiers every
4946 * data fork in local format now contains caller specific data
4947 * and as such conversion through this function is likely to be
4948 * broken.
4949 *
4950 * The only likely user of this branch is for remote symlinks,
4951 * but we cannot overwrite the data fork contents of the symlink
4952 * (EEXIST occurs higher up the stack) and so it will never go
4953 * from local format to extent format here. Hence I don't think
4954 * this branch is ever executed intentionally and we should
4955 * consider removing it and asserting that xfs_bmapi_write()
4956 * cannot be called directly on local format forks. i.e. callers
4957 * are completely responsible for local to extent format
4958 * conversion, not xfs_bmapi_write().
4959 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004960 error = xfs_bmap_local_to_extents(tp, ip, firstblock, total,
Dave Chinner1e823792013-02-11 15:58:13 +11004961 &bma.logflags, whichfork,
4962 xfs_bmap_local_to_extents_init_fn);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004963 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 goto error0;
4965 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004966
4967 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004969 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004970 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004971 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004972 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004973 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004974 }
4975
4976 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4977 error = xfs_iread_extents(tp, ip, whichfork);
4978 if (error)
4979 goto error0;
4980 }
4981
Dave Chinnere0c3da52011-09-18 20:41:01 +00004982 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004983 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 n = 0;
4985 end = bno + len;
4986 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004987
4988 bma.tp = tp;
4989 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004990 bma.total = total;
4991 bma.userdata = 0;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004992 bma.flist = flist;
4993 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004994
Brian Foster9e96fe62013-01-17 13:11:29 -05004995 if (flags & XFS_BMAPI_STACK_SWITCH)
4996 bma.stack_switch = 1;
4997
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004999 inhole = eof || bma.got.br_startoff > bno;
5000 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 /*
5003 * First, deal with the hole before the allocated space
5004 * that we found, if any.
5005 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005006 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00005007 bma.eof = eof;
5008 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
5009 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00005010 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10005011 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00005012
Dave Chinnera99ebf42011-12-01 11:24:20 +00005013 /*
5014 * There's a 32/64 bit type mismatch between the
5015 * allocation length request (which can be 64 bits in
5016 * length) and the bma length request, which is
5017 * xfs_extlen_t and therefore 32 bits. Hence we have to
5018 * check for 32-bit overflows and handle them here.
5019 */
5020 if (len > (xfs_filblks_t)MAXEXTLEN)
5021 bma.length = MAXEXTLEN;
5022 else
5023 bma.length = len;
5024
5025 ASSERT(len > 0);
5026 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10005027 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 if (error)
5029 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00005030 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00005031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 }
Christoph Hellwig44032802011-09-18 20:40:48 +00005033
Dave Chinneraef9a892011-09-18 20:40:44 +00005034 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00005035 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
5036 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037
Dave Chinnerb447fe52011-09-18 20:40:51 +00005038 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005039 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005040 if (error == EAGAIN)
5041 continue;
5042 if (error)
5043 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044
Dave Chinneraef9a892011-09-18 20:40:44 +00005045 /* update the extent map to return */
5046 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
5047
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 /*
5049 * If we're done, stop now. Stop when we've allocated
5050 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
5051 * the transaction may get too big.
5052 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00005053 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005055
5056 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00005057 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00005058 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
5059 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
5060 &bma.got);
5061 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00005062 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005065
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 /*
5067 * Transform from btree to extents, give it cur.
5068 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005069 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00005070 int tmp_logflags = 0;
5071
Dave Chinner29c8d172011-09-18 20:41:00 +00005072 ASSERT(bma.cur);
5073 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00005075 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 if (error)
5077 goto error0;
5078 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005081 XFS_IFORK_NEXTENTS(ip, whichfork) >
5082 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084error0:
5085 /*
5086 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005087 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005089 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00005091 bma.logflags &= ~xfs_ilog_fext(whichfork);
5092 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00005094 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 /*
5096 * Log whatever the flags say, even if error. Otherwise we might miss
5097 * detecting a case where the data is changed, there's an error,
5098 * and it's not logged so we don't shutdown when we should.
5099 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00005100 if (bma.logflags)
5101 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00005102
Dave Chinner29c8d172011-09-18 20:41:00 +00005103 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 if (!error) {
5105 ASSERT(*firstblock == NULLFSBLOCK ||
5106 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5107 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00005108 bma.cur->bc_private.b.firstblock) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 (flist->xbf_low &&
5110 XFS_FSB_TO_AGNO(mp, *firstblock) <
5111 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00005112 bma.cur->bc_private.b.firstblock)));
5113 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 }
Dave Chinner29c8d172011-09-18 20:41:00 +00005115 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005116 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5117 }
5118 if (!error)
5119 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5120 orig_nmap, *nmap);
5121 return error;
5122}
5123
5124/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005125 * Called by xfs_bmapi to update file extent records and the btree
5126 * after removing space (or undoing a delayed allocation).
5127 */
5128STATIC int /* error */
5129xfs_bmap_del_extent(
5130 xfs_inode_t *ip, /* incore inode pointer */
5131 xfs_trans_t *tp, /* current transaction pointer */
5132 xfs_extnum_t *idx, /* extent number to update/delete */
5133 xfs_bmap_free_t *flist, /* list of extents to be freed */
5134 xfs_btree_cur_t *cur, /* if null, not a btree */
5135 xfs_bmbt_irec_t *del, /* data to remove from extents */
5136 int *logflagsp, /* inode logging flags */
5137 int whichfork) /* data or attr fork */
5138{
5139 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5140 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5141 xfs_fsblock_t del_endblock=0; /* first block past del */
5142 xfs_fileoff_t del_endoff; /* first offset past del */
5143 int delay; /* current block is delayed allocated */
5144 int do_fx; /* free extent at end of routine */
5145 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5146 int error; /* error return value */
5147 int flags; /* inode logging flags */
5148 xfs_bmbt_irec_t got; /* current extent entry */
5149 xfs_fileoff_t got_endoff; /* first offset past got */
5150 int i; /* temp state */
5151 xfs_ifork_t *ifp; /* inode fork pointer */
5152 xfs_mount_t *mp; /* mount structure */
5153 xfs_filblks_t nblks; /* quota/sb block count */
5154 xfs_bmbt_irec_t new; /* new record to be inserted */
5155 /* REFERENCED */
5156 uint qfield; /* quota field to update */
5157 xfs_filblks_t temp; /* for indirect length calculations */
5158 xfs_filblks_t temp2; /* for indirect length calculations */
5159 int state = 0;
5160
5161 XFS_STATS_INC(xs_del_exlist);
5162
5163 if (whichfork == XFS_ATTR_FORK)
5164 state |= BMAP_ATTRFORK;
5165
5166 mp = ip->i_mount;
5167 ifp = XFS_IFORK_PTR(ip, whichfork);
5168 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
5169 (uint)sizeof(xfs_bmbt_rec_t)));
5170 ASSERT(del->br_blockcount > 0);
5171 ep = xfs_iext_get_ext(ifp, *idx);
5172 xfs_bmbt_get_all(ep, &got);
5173 ASSERT(got.br_startoff <= del->br_startoff);
5174 del_endoff = del->br_startoff + del->br_blockcount;
5175 got_endoff = got.br_startoff + got.br_blockcount;
5176 ASSERT(got_endoff >= del_endoff);
5177 delay = isnullstartblock(got.br_startblock);
5178 ASSERT(isnullstartblock(del->br_startblock) == delay);
5179 flags = 0;
5180 qfield = 0;
5181 error = 0;
5182 /*
5183 * If deleting a real allocation, must free up the disk space.
5184 */
5185 if (!delay) {
5186 flags = XFS_ILOG_CORE;
5187 /*
5188 * Realtime allocation. Free it and record di_nblocks update.
5189 */
5190 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5191 xfs_fsblock_t bno;
5192 xfs_filblks_t len;
5193
5194 ASSERT(do_mod(del->br_blockcount,
5195 mp->m_sb.sb_rextsize) == 0);
5196 ASSERT(do_mod(del->br_startblock,
5197 mp->m_sb.sb_rextsize) == 0);
5198 bno = del->br_startblock;
5199 len = del->br_blockcount;
5200 do_div(bno, mp->m_sb.sb_rextsize);
5201 do_div(len, mp->m_sb.sb_rextsize);
5202 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5203 if (error)
5204 goto done;
5205 do_fx = 0;
5206 nblks = len * mp->m_sb.sb_rextsize;
5207 qfield = XFS_TRANS_DQ_RTBCOUNT;
5208 }
5209 /*
5210 * Ordinary allocation.
5211 */
5212 else {
5213 do_fx = 1;
5214 nblks = del->br_blockcount;
5215 qfield = XFS_TRANS_DQ_BCOUNT;
5216 }
5217 /*
5218 * Set up del_endblock and cur for later.
5219 */
5220 del_endblock = del->br_startblock + del->br_blockcount;
5221 if (cur) {
5222 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5223 got.br_startblock, got.br_blockcount,
5224 &i)))
5225 goto done;
5226 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5227 }
5228 da_old = da_new = 0;
5229 } else {
5230 da_old = startblockval(got.br_startblock);
5231 da_new = 0;
5232 nblks = 0;
5233 do_fx = 0;
5234 }
5235 /*
5236 * Set flag value to use in switch statement.
5237 * Left-contig is 2, right-contig is 1.
5238 */
5239 switch (((got.br_startoff == del->br_startoff) << 1) |
5240 (got_endoff == del_endoff)) {
5241 case 3:
5242 /*
5243 * Matches the whole extent. Delete the entry.
5244 */
5245 xfs_iext_remove(ip, *idx, 1,
5246 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5247 --*idx;
5248 if (delay)
5249 break;
5250
5251 XFS_IFORK_NEXT_SET(ip, whichfork,
5252 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5253 flags |= XFS_ILOG_CORE;
5254 if (!cur) {
5255 flags |= xfs_ilog_fext(whichfork);
5256 break;
5257 }
5258 if ((error = xfs_btree_delete(cur, &i)))
5259 goto done;
5260 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5261 break;
5262
5263 case 2:
5264 /*
5265 * Deleting the first part of the extent.
5266 */
5267 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5268 xfs_bmbt_set_startoff(ep, del_endoff);
5269 temp = got.br_blockcount - del->br_blockcount;
5270 xfs_bmbt_set_blockcount(ep, temp);
5271 if (delay) {
5272 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5273 da_old);
5274 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5275 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5276 da_new = temp;
5277 break;
5278 }
5279 xfs_bmbt_set_startblock(ep, del_endblock);
5280 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5281 if (!cur) {
5282 flags |= xfs_ilog_fext(whichfork);
5283 break;
5284 }
5285 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5286 got.br_blockcount - del->br_blockcount,
5287 got.br_state)))
5288 goto done;
5289 break;
5290
5291 case 1:
5292 /*
5293 * Deleting the last part of the extent.
5294 */
5295 temp = got.br_blockcount - del->br_blockcount;
5296 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5297 xfs_bmbt_set_blockcount(ep, temp);
5298 if (delay) {
5299 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5300 da_old);
5301 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5302 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5303 da_new = temp;
5304 break;
5305 }
5306 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5307 if (!cur) {
5308 flags |= xfs_ilog_fext(whichfork);
5309 break;
5310 }
5311 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5312 got.br_startblock,
5313 got.br_blockcount - del->br_blockcount,
5314 got.br_state)))
5315 goto done;
5316 break;
5317
5318 case 0:
5319 /*
5320 * Deleting the middle of the extent.
5321 */
5322 temp = del->br_startoff - got.br_startoff;
5323 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5324 xfs_bmbt_set_blockcount(ep, temp);
5325 new.br_startoff = del_endoff;
5326 temp2 = got_endoff - del_endoff;
5327 new.br_blockcount = temp2;
5328 new.br_state = got.br_state;
5329 if (!delay) {
5330 new.br_startblock = del_endblock;
5331 flags |= XFS_ILOG_CORE;
5332 if (cur) {
5333 if ((error = xfs_bmbt_update(cur,
5334 got.br_startoff,
5335 got.br_startblock, temp,
5336 got.br_state)))
5337 goto done;
5338 if ((error = xfs_btree_increment(cur, 0, &i)))
5339 goto done;
5340 cur->bc_rec.b = new;
5341 error = xfs_btree_insert(cur, &i);
5342 if (error && error != ENOSPC)
5343 goto done;
5344 /*
5345 * If get no-space back from btree insert,
5346 * it tried a split, and we have a zero
5347 * block reservation.
5348 * Fix up our state and return the error.
5349 */
5350 if (error == ENOSPC) {
5351 /*
5352 * Reset the cursor, don't trust
5353 * it after any insert operation.
5354 */
5355 if ((error = xfs_bmbt_lookup_eq(cur,
5356 got.br_startoff,
5357 got.br_startblock,
5358 temp, &i)))
5359 goto done;
5360 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5361 /*
5362 * Update the btree record back
5363 * to the original value.
5364 */
5365 if ((error = xfs_bmbt_update(cur,
5366 got.br_startoff,
5367 got.br_startblock,
5368 got.br_blockcount,
5369 got.br_state)))
5370 goto done;
5371 /*
5372 * Reset the extent record back
5373 * to the original value.
5374 */
5375 xfs_bmbt_set_blockcount(ep,
5376 got.br_blockcount);
5377 flags = 0;
5378 error = XFS_ERROR(ENOSPC);
5379 goto done;
5380 }
5381 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
5382 } else
5383 flags |= xfs_ilog_fext(whichfork);
5384 XFS_IFORK_NEXT_SET(ip, whichfork,
5385 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5386 } else {
5387 ASSERT(whichfork == XFS_DATA_FORK);
5388 temp = xfs_bmap_worst_indlen(ip, temp);
5389 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5390 temp2 = xfs_bmap_worst_indlen(ip, temp2);
5391 new.br_startblock = nullstartblock((int)temp2);
5392 da_new = temp + temp2;
5393 while (da_new > da_old) {
5394 if (temp) {
5395 temp--;
5396 da_new--;
5397 xfs_bmbt_set_startblock(ep,
5398 nullstartblock((int)temp));
5399 }
5400 if (da_new == da_old)
5401 break;
5402 if (temp2) {
5403 temp2--;
5404 da_new--;
5405 new.br_startblock =
5406 nullstartblock((int)temp2);
5407 }
5408 }
5409 }
5410 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5411 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5412 ++*idx;
5413 break;
5414 }
5415 /*
5416 * If we need to, add to list of extents to delete.
5417 */
5418 if (do_fx)
5419 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
5420 mp);
5421 /*
5422 * Adjust inode # blocks in the file.
5423 */
5424 if (nblks)
5425 ip->i_d.di_nblocks -= nblks;
5426 /*
5427 * Adjust quota data.
5428 */
5429 if (qfield)
5430 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5431
5432 /*
5433 * Account for change in delayed indirect blocks.
5434 * Nothing to do for disk quota accounting here.
5435 */
5436 ASSERT(da_old >= da_new);
5437 if (da_old > da_new) {
5438 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5439 (int64_t)(da_old - da_new), 0);
5440 }
5441done:
5442 *logflagsp = flags;
5443 return error;
5444}
5445
5446/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 * Unmap (remove) blocks from a file.
5448 * If nexts is nonzero then the number of extents to remove is limited to
5449 * that value. If not all extents in the block range can be removed then
5450 * *done is set.
5451 */
5452int /* error */
5453xfs_bunmapi(
5454 xfs_trans_t *tp, /* transaction pointer */
5455 struct xfs_inode *ip, /* incore inode */
5456 xfs_fileoff_t bno, /* starting offset to unmap */
5457 xfs_filblks_t len, /* length to unmap in file */
5458 int flags, /* misc flags */
5459 xfs_extnum_t nexts, /* number of extents max */
5460 xfs_fsblock_t *firstblock, /* first allocated block
5461 controls a.g. for allocs */
5462 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5463 int *done) /* set if not done yet */
5464{
5465 xfs_btree_cur_t *cur; /* bmap btree cursor */
5466 xfs_bmbt_irec_t del; /* extent being deleted */
5467 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005468 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 int error; /* error return value */
5470 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005471 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 xfs_ifork_t *ifp; /* inode fork pointer */
5473 int isrt; /* freeing in rt area */
5474 xfs_extnum_t lastx; /* last extent index used */
5475 int logflags; /* transaction logging flags */
5476 xfs_extlen_t mod; /* rt extent offset */
5477 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005478 xfs_extnum_t nextents; /* number of file extents */
5479 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480 xfs_fileoff_t start; /* first file offset deleted */
5481 int tmp_logflags; /* partial logging flags */
5482 int wasdel; /* was a delayed alloc extent */
5483 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 xfs_fsblock_t sum;
5485
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005486 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5487
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5489 XFS_ATTR_FORK : XFS_DATA_FORK;
5490 ifp = XFS_IFORK_PTR(ip, whichfork);
5491 if (unlikely(
5492 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5493 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5494 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5495 ip->i_mount);
5496 return XFS_ERROR(EFSCORRUPTED);
5497 }
5498 mp = ip->i_mount;
5499 if (XFS_FORCED_SHUTDOWN(mp))
5500 return XFS_ERROR(EIO);
Christoph Hellwig54893272011-05-11 15:04:03 +00005501
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 ASSERT(len > 0);
5503 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005504
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5506 (error = xfs_iread_extents(tp, ip, whichfork)))
5507 return error;
5508 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5509 if (nextents == 0) {
5510 *done = 1;
5511 return 0;
5512 }
5513 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005514 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 start = bno;
5516 bno = start + len - 1;
5517 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5518 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005519
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 /*
5521 * Check to see if the given block number is past the end of the
5522 * file, back up to the last block if so...
5523 */
5524 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005525 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 xfs_bmbt_get_all(ep, &got);
5527 bno = got.br_startoff + got.br_blockcount - 1;
5528 }
5529 logflags = 0;
5530 if (ifp->if_flags & XFS_IFBROOT) {
5531 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005532 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 cur->bc_private.b.firstblock = *firstblock;
5534 cur->bc_private.b.flist = flist;
5535 cur->bc_private.b.flags = 0;
5536 } else
5537 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005538
5539 if (isrt) {
5540 /*
5541 * Synchronize by locking the bitmap inode.
5542 */
5543 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5544 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5545 }
5546
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 extno = 0;
5548 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5549 (nexts == 0 || extno < nexts)) {
5550 /*
5551 * Is the found extent after a hole in which bno lives?
5552 * Just back up to the previous extent, if so.
5553 */
5554 if (got.br_startoff > bno) {
5555 if (--lastx < 0)
5556 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005557 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 xfs_bmbt_get_all(ep, &got);
5559 }
5560 /*
5561 * Is the last block of this extent before the range
5562 * we're supposed to delete? If so, we're done.
5563 */
5564 bno = XFS_FILEOFF_MIN(bno,
5565 got.br_startoff + got.br_blockcount - 1);
5566 if (bno < start)
5567 break;
5568 /*
5569 * Then deal with the (possibly delayed) allocated space
5570 * we found.
5571 */
5572 ASSERT(ep != NULL);
5573 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005574 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005575 if (got.br_startoff < start) {
5576 del.br_startoff = start;
5577 del.br_blockcount -= start - got.br_startoff;
5578 if (!wasdel)
5579 del.br_startblock += start - got.br_startoff;
5580 }
5581 if (del.br_startoff + del.br_blockcount > bno + 1)
5582 del.br_blockcount = bno + 1 - del.br_startoff;
5583 sum = del.br_startblock + del.br_blockcount;
5584 if (isrt &&
5585 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5586 /*
5587 * Realtime extent not lined up at the end.
5588 * The extent could have been split into written
5589 * and unwritten pieces, or we could just be
5590 * unmapping part of it. But we can't really
5591 * get rid of part of a realtime extent.
5592 */
5593 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005594 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 /*
5596 * This piece is unwritten, or we're not
5597 * using unwritten extents. Skip over it.
5598 */
5599 ASSERT(bno >= mod);
5600 bno -= mod > del.br_blockcount ?
5601 del.br_blockcount : mod;
5602 if (bno < got.br_startoff) {
5603 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005604 xfs_bmbt_get_all(xfs_iext_get_ext(
5605 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 }
5607 continue;
5608 }
5609 /*
5610 * It's written, turn it unwritten.
5611 * This is better than zeroing it.
5612 */
5613 ASSERT(del.br_state == XFS_EXT_NORM);
5614 ASSERT(xfs_trans_get_block_res(tp) > 0);
5615 /*
5616 * If this spans a realtime extent boundary,
5617 * chop it back to the start of the one we end at.
5618 */
5619 if (del.br_blockcount > mod) {
5620 del.br_startoff += del.br_blockcount - mod;
5621 del.br_startblock += del.br_blockcount - mod;
5622 del.br_blockcount = mod;
5623 }
5624 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005625 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5626 &lastx, &cur, &del, firstblock, flist,
5627 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 if (error)
5629 goto error0;
5630 goto nodelete;
5631 }
5632 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5633 /*
5634 * Realtime extent is lined up at the end but not
5635 * at the front. We'll get rid of full extents if
5636 * we can.
5637 */
5638 mod = mp->m_sb.sb_rextsize - mod;
5639 if (del.br_blockcount > mod) {
5640 del.br_blockcount -= mod;
5641 del.br_startoff += mod;
5642 del.br_startblock += mod;
5643 } else if ((del.br_startoff == start &&
5644 (del.br_state == XFS_EXT_UNWRITTEN ||
5645 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005646 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647 /*
5648 * Can't make it unwritten. There isn't
5649 * a full extent here so just skip it.
5650 */
5651 ASSERT(bno >= del.br_blockcount);
5652 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005653 if (got.br_startoff > bno) {
5654 if (--lastx >= 0) {
5655 ep = xfs_iext_get_ext(ifp,
5656 lastx);
5657 xfs_bmbt_get_all(ep, &got);
5658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 }
5660 continue;
5661 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5662 /*
5663 * This one is already unwritten.
5664 * It must have a written left neighbor.
5665 * Unwrite the killed part of that one and
5666 * try again.
5667 */
5668 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005669 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5670 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005672 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 ASSERT(del.br_startblock ==
5674 prev.br_startblock + prev.br_blockcount);
5675 if (prev.br_startoff < start) {
5676 mod = start - prev.br_startoff;
5677 prev.br_blockcount -= mod;
5678 prev.br_startblock += mod;
5679 prev.br_startoff = start;
5680 }
5681 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005682 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005683 error = xfs_bmap_add_extent_unwritten_real(tp,
5684 ip, &lastx, &cur, &prev,
5685 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 if (error)
5687 goto error0;
5688 goto nodelete;
5689 } else {
5690 ASSERT(del.br_state == XFS_EXT_NORM);
5691 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005692 error = xfs_bmap_add_extent_unwritten_real(tp,
5693 ip, &lastx, &cur, &del,
5694 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 if (error)
5696 goto error0;
5697 goto nodelete;
5698 }
5699 }
5700 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005701 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005702 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005703 if (isrt) {
5704 xfs_filblks_t rtexts;
5705
5706 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5707 do_div(rtexts, mp->m_sb.sb_rextsize);
5708 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005709 (int64_t)rtexts, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005710 (void)xfs_trans_reserve_quota_nblks(NULL,
5711 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005712 XFS_QMOPT_RES_RTBLKS);
5713 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00005714 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005715 (int64_t)del.br_blockcount, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005716 (void)xfs_trans_reserve_quota_nblks(NULL,
5717 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005720 ip->i_delayed_blks -= del.br_blockcount;
5721 if (cur)
5722 cur->bc_private.b.flags |=
5723 XFS_BTCUR_BPRV_WASDEL;
5724 } else if (cur)
5725 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5726 /*
5727 * If it's the case where the directory code is running
5728 * with no block reservation, and the deleted block is in
5729 * the middle of its extent, and the resulting insert
5730 * of an extent would cause transformation to btree format,
5731 * then reject it. The calling code will then swap
5732 * blocks around instead.
5733 * We have to do this now, rather than waiting for the
5734 * conversion to btree format, since the transaction
5735 * will be dirty.
5736 */
5737 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5738 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005739 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5740 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 del.br_startoff > got.br_startoff &&
5742 del.br_startoff + del.br_blockcount <
5743 got.br_startoff + got.br_blockcount) {
5744 error = XFS_ERROR(ENOSPC);
5745 goto error0;
5746 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00005747 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005748 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 logflags |= tmp_logflags;
5750 if (error)
5751 goto error0;
5752 bno = del.br_startoff - 1;
5753nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005754 /*
5755 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005758 if (lastx >= 0) {
5759 ep = xfs_iext_get_ext(ifp, lastx);
5760 if (xfs_bmbt_get_startoff(ep) > bno) {
5761 if (--lastx >= 0)
5762 ep = xfs_iext_get_ext(ifp,
5763 lastx);
5764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 extno++;
5768 }
5769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005771
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 /*
5773 * Convert to a btree if necessary.
5774 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005775 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 ASSERT(cur == NULL);
5777 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5778 &cur, 0, &tmp_logflags, whichfork);
5779 logflags |= tmp_logflags;
5780 if (error)
5781 goto error0;
5782 }
5783 /*
5784 * transform from btree to extents, give it cur
5785 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005786 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 ASSERT(cur != NULL);
5788 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5789 whichfork);
5790 logflags |= tmp_logflags;
5791 if (error)
5792 goto error0;
5793 }
5794 /*
5795 * transform from extents to local?
5796 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 error = 0;
5798error0:
5799 /*
5800 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005801 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005803 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005805 logflags &= ~xfs_ilog_fext(whichfork);
5806 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005808 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 /*
5810 * Log inode even in the error case, if the transaction
5811 * is dirty we'll need to shut down the filesystem.
5812 */
5813 if (logflags)
5814 xfs_trans_log_inode(tp, ip, logflags);
5815 if (cur) {
5816 if (!error) {
5817 *firstblock = cur->bc_private.b.firstblock;
5818 cur->bc_private.b.allocated = 0;
5819 }
5820 xfs_btree_del_cursor(cur,
5821 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5822 }
5823 return error;
5824}
5825
5826/*
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005827 * returns 1 for success, 0 if we failed to map the extent.
5828 */
5829STATIC int
5830xfs_getbmapx_fix_eof_hole(
5831 xfs_inode_t *ip, /* xfs incore inode pointer */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005832 struct getbmapx *out, /* output structure */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005833 int prealloced, /* this is a file with
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005834 * preallocated data space */
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005835 __int64_t end, /* last block requested */
5836 xfs_fsblock_t startblock)
5837{
5838 __int64_t fixlen;
5839 xfs_mount_t *mp; /* file system mount point */
Eric Sandeen5af317c2008-11-28 14:23:35 +11005840 xfs_ifork_t *ifp; /* inode fork pointer */
5841 xfs_extnum_t lastx; /* last extent pointer */
5842 xfs_fileoff_t fileblock;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005843
5844 if (startblock == HOLESTARTBLOCK) {
5845 mp = ip->i_mount;
5846 out->bmv_block = -1;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005847 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005848 fixlen -= out->bmv_offset;
5849 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5850 /* Came to hole at EOF. Trim it. */
5851 if (fixlen <= 0)
5852 return 0;
5853 out->bmv_length = fixlen;
5854 }
5855 } else {
Eric Sandeen5af317c2008-11-28 14:23:35 +11005856 if (startblock == DELAYSTARTBLOCK)
5857 out->bmv_block = -2;
5858 else
Eric Sandeen9d87c312009-01-14 23:22:07 -06005859 out->bmv_block = xfs_fsb_to_db(ip, startblock);
Eric Sandeen5af317c2008-11-28 14:23:35 +11005860 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5861 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5862 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5863 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5864 out->bmv_oflags |= BMV_OF_LAST;
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10005865 }
5866
5867 return 1;
5868}
5869
5870/*
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005871 * Get inode's extents as described in bmv, and format for output.
5872 * Calls formatter to fill the user's buffer until all extents
5873 * are mapped, until the passed-in bmv->bmv_count slots have
5874 * been filled, or until the formatter short-circuits the loop,
5875 * if it is tracking filled-in extents on its own.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005876 */
5877int /* error code */
5878xfs_getbmap(
Christoph Hellwig993386c2007-08-28 16:12:30 +10005879 xfs_inode_t *ip,
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005880 struct getbmapx *bmv, /* user bmap structure */
5881 xfs_bmap_format_t formatter, /* format to user */
5882 void *arg) /* formatter arg */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005883{
5884 __int64_t bmvend; /* last block requested */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005885 int error = 0; /* return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886 __int64_t fixlen; /* length for -1 case */
5887 int i; /* extent number */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 int lock; /* lock state */
5889 xfs_bmbt_irec_t *map; /* buffer for user's data */
5890 xfs_mount_t *mp; /* file system mount point */
5891 int nex; /* # of user extents can do */
5892 int nexleft; /* # of user extents left */
5893 int subnex; /* # of bmapi's can do */
5894 int nmap; /* number of map entries */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005895 struct getbmapx *out; /* output structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896 int whichfork; /* data or attr fork */
5897 int prealloced; /* this is a file with
5898 * preallocated data space */
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005899 int iflags; /* interface flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900 int bmapi_flags; /* flags for xfs_bmapi */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005901 int cur_ext = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 mp = ip->i_mount;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005904 iflags = bmv->bmv_iflags;
Eric Sandeen8a7141a2008-11-28 14:23:35 +11005905 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005906
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907 if (whichfork == XFS_ATTR_FORK) {
5908 if (XFS_IFORK_Q(ip)) {
5909 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5910 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5911 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5912 return XFS_ERROR(EINVAL);
5913 } else if (unlikely(
5914 ip->i_d.di_aformat != 0 &&
5915 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5916 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5917 ip->i_mount);
5918 return XFS_ERROR(EFSCORRUPTED);
5919 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005920
5921 prealloced = 0;
5922 fixlen = 1LL << 32;
5923 } else {
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005924 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5925 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5926 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5927 return XFS_ERROR(EINVAL);
5928
David Chinner957d0eb2007-06-18 16:50:37 +10005929 if (xfs_get_extsz_hint(ip) ||
Nathan Scottdd9f4382006-01-11 15:28:28 +11005930 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 prealloced = 1;
Dave Chinner32972382012-06-08 15:44:54 +10005932 fixlen = mp->m_super->s_maxbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005933 } else {
5934 prealloced = 0;
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005935 fixlen = XFS_ISIZE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 }
5938
5939 if (bmv->bmv_length == -1) {
5940 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005941 bmv->bmv_length =
5942 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5943 } else if (bmv->bmv_length == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005944 bmv->bmv_entries = 0;
5945 return 0;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005946 } else if (bmv->bmv_length < 0) {
5947 return XFS_ERROR(EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005948 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005949
Linus Torvalds1da177e2005-04-16 15:20:36 -07005950 nex = bmv->bmv_count - 1;
5951 if (nex <= 0)
5952 return XFS_ERROR(EINVAL);
5953 bmvend = bmv->bmv_offset + bmv->bmv_length;
5954
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005955
5956 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5957 return XFS_ERROR(ENOMEM);
5958 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
Dave Chinnerf0742112012-03-07 04:50:22 +00005959 if (!out) {
5960 out = kmem_zalloc_large(bmv->bmv_count *
5961 sizeof(struct getbmapx));
5962 if (!out)
5963 return XFS_ERROR(ENOMEM);
5964 }
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05005965
Linus Torvalds1da177e2005-04-16 15:20:36 -07005966 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005967 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
Christoph Hellwigce7ae1512011-12-18 20:00:11 +00005968 if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) {
Dave Chinner4bc1ea62012-11-12 22:53:56 +11005969 error = -filemap_write_and_wait(VFS_I(ip)->i_mapping);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005970 if (error)
5971 goto out_unlock_iolock;
Niv Sardie12070a2008-03-06 13:43:03 +11005972 }
Dave Chinner309c8482010-11-30 15:16:02 +11005973 /*
5974 * even after flushing the inode, there can still be delalloc
5975 * blocks on the inode beyond EOF due to speculative
5976 * preallocation. These are not removed until the release
5977 * function is called or the inode is inactivated. Hence we
5978 * cannot assert here that ip->i_delayed_blks == 0.
5979 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005981
5982 lock = xfs_ilock_map_shared(ip);
5983
5984 /*
5985 * Don't let nex be bigger than the number of extents
5986 * we can have assuming alternating holes and real extents.
5987 */
5988 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5989 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5990
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005991 bmapi_flags = xfs_bmapi_aflag(whichfork);
5992 if (!(iflags & BMV_IF_PREALLOC))
5993 bmapi_flags |= XFS_BMAPI_IGSTATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994
5995 /*
5996 * Allocate enough space to handle "subnex" maps at a time.
5997 */
Christoph Hellwig4be4a002009-04-29 10:50:48 -04005998 error = ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 subnex = 16;
Christoph Hellwigca35dcd2009-07-18 18:14:54 -04006000 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006001 if (!map)
6002 goto out_unlock_ilock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006003
6004 bmv->bmv_entries = 0;
6005
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006006 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
6007 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
6008 error = 0;
6009 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010 }
6011
6012 nexleft = nex;
6013
6014 do {
6015 nmap = (nexleft > subnex) ? subnex : nexleft;
Dave Chinner5c8ed202011-09-18 20:40:45 +00006016 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
6017 XFS_BB_TO_FSB(mp, bmv->bmv_length),
6018 map, &nmap, bmapi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 if (error)
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006020 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 ASSERT(nmap <= subnex);
6022
6023 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006024 out[cur_ext].bmv_oflags = 0;
Eric Sandeen5af317c2008-11-28 14:23:35 +11006025 if (map[i].br_state == XFS_EXT_UNWRITTEN)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006026 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
Eric Sandeen5af317c2008-11-28 14:23:35 +11006027 else if (map[i].br_startblock == DELAYSTARTBLOCK)
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006028 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
6029 out[cur_ext].bmv_offset =
6030 XFS_FSB_TO_BB(mp, map[i].br_startoff);
6031 out[cur_ext].bmv_length =
6032 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
6033 out[cur_ext].bmv_unused1 = 0;
6034 out[cur_ext].bmv_unused2 = 0;
Dave Chinner58e20772012-04-29 21:16:17 +10006035
6036 /*
6037 * delayed allocation extents that start beyond EOF can
6038 * occur due to speculative EOF allocation when the
6039 * delalloc extent is larger than the largest freespace
6040 * extent at conversion time. These extents cannot be
6041 * converted by data writeback, so can exist here even
6042 * if we are not supposed to be finding delalloc
6043 * extents.
6044 */
6045 if (map[i].br_startblock == DELAYSTARTBLOCK &&
6046 map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
6047 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
6048
Yingping Lu9af0a702005-11-02 15:09:54 +11006049 if (map[i].br_startblock == HOLESTARTBLOCK &&
Vlad Apostolov3bacbcd2007-08-16 15:20:25 +10006050 whichfork == XFS_ATTR_FORK) {
6051 /* came to the end of attribute fork */
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006052 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006053 goto out_free_map;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 }
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006055
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006056 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
6057 prealloced, bmvend,
6058 map[i].br_startblock))
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006059 goto out_free_map;
6060
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006061 bmv->bmv_offset =
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006062 out[cur_ext].bmv_offset +
6063 out[cur_ext].bmv_length;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006064 bmv->bmv_length =
6065 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
Tao Ma9af25462010-08-30 02:44:03 +00006066
6067 /*
6068 * In case we don't want to return the hole,
6069 * don't increase cur_ext so that we can reuse
6070 * it in the next loop.
6071 */
6072 if ((iflags & BMV_IF_NO_HOLES) &&
6073 map[i].br_startblock == HOLESTARTBLOCK) {
6074 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
6075 continue;
6076 }
6077
6078 nexleft--;
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006079 bmv->bmv_entries++;
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006080 cur_ext++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081 }
6082 } while (nmap && nexleft && bmv->bmv_length);
6083
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006084 out_free_map:
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10006085 kmem_free(map);
Christoph Hellwig4be4a002009-04-29 10:50:48 -04006086 out_unlock_ilock:
6087 xfs_iunlock_map_shared(ip, lock);
6088 out_unlock_iolock:
6089 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig6321e3e2009-02-24 08:39:02 -05006090
6091 for (i = 0; i < cur_ext; i++) {
6092 int full = 0; /* user array is full */
6093
6094 /* format results & advance arg */
6095 error = formatter(&arg, &out[i], &full);
6096 if (error || full)
6097 break;
6098 }
6099
Dave Chinnerf0742112012-03-07 04:50:22 +00006100 if (is_vmalloc_addr(out))
6101 kmem_free_large(out);
6102 else
6103 kmem_free(out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006104 return error;
6105}
6106
Dave Chinnerc726de42010-11-30 15:14:39 +11006107/*
6108 * dead simple method of punching delalyed allocation blocks from a range in
6109 * the inode. Walks a block at a time so will be slow, but is only executed in
6110 * rare error cases so the overhead is not critical. This will alays punch out
6111 * both the start and end blocks, even if the ranges only partially overlap
6112 * them, so it is up to the caller to ensure that partial blocks are not
6113 * passed in.
6114 */
6115int
6116xfs_bmap_punch_delalloc_range(
6117 struct xfs_inode *ip,
6118 xfs_fileoff_t start_fsb,
6119 xfs_fileoff_t length)
6120{
6121 xfs_fileoff_t remaining = length;
6122 int error = 0;
6123
6124 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6125
6126 do {
6127 int done;
6128 xfs_bmbt_irec_t imap;
6129 int nimaps = 1;
6130 xfs_fsblock_t firstblock;
6131 xfs_bmap_free_t flist;
6132
6133 /*
6134 * Map the range first and check that it is a delalloc extent
6135 * before trying to unmap the range. Otherwise we will be
6136 * trying to remove a real extent (which requires a
6137 * transaction) or a hole, which is probably a bad idea...
6138 */
Dave Chinner5c8ed202011-09-18 20:40:45 +00006139 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
6140 XFS_BMAPI_ENTIRE);
Dave Chinnerc726de42010-11-30 15:14:39 +11006141
6142 if (error) {
6143 /* something screwed, just bail */
6144 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
Dave Chinner53487782011-03-07 10:05:35 +11006145 xfs_alert(ip->i_mount,
Dave Chinnerc726de42010-11-30 15:14:39 +11006146 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6147 ip->i_ino, start_fsb);
6148 }
6149 break;
6150 }
6151 if (!nimaps) {
6152 /* nothing there */
6153 goto next_block;
6154 }
6155 if (imap.br_startblock != DELAYSTARTBLOCK) {
6156 /* been converted, ignore */
6157 goto next_block;
6158 }
6159 WARN_ON(imap.br_blockcount == 0);
6160
6161 /*
6162 * Note: while we initialise the firstblock/flist pair, they
6163 * should never be used because blocks should never be
6164 * allocated or freed for a delalloc extent and hence we need
6165 * don't cancel or finish them after the xfs_bunmapi() call.
6166 */
6167 xfs_bmap_init(&flist, &firstblock);
6168 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6169 &flist, &done);
6170 if (error)
6171 break;
6172
6173 ASSERT(!flist.xbf_count && !flist.xbf_first);
6174next_block:
6175 start_fsb++;
6176 remaining--;
6177 } while(remaining > 0);
6178
6179 return error;
6180}