blob: 63a5bb9113ee214f22d2fb22b8ee2ab7a0d3f7f2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a9883c2013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100026#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100029#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110032#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_extfree_item.h"
35#include "xfs_alloc.h"
36#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100037#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110038#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_rtalloc.h"
40#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_quota.h"
42#include "xfs_trans_space.h"
43#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000044#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110045#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110046#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110047#include "xfs_filestream.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050kmem_zone_t *xfs_bmap_free_item_zone;
51
52/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110053 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110057 * Compute and fill in the value of the maximum depth of a bmap btree
58 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110060void
61xfs_bmap_compute_maxlevels(
62 xfs_mount_t *mp, /* file system mount structure */
63 int whichfork) /* data or attr fork */
64{
65 int level; /* btree level */
66 uint maxblocks; /* max blocks at this level */
67 uint maxleafents; /* max leaf entries possible */
68 int maxrootrecs; /* max records in root block */
69 int minleafrecs; /* min records in leaf block */
70 int minnoderecs; /* min records in node block */
71 int sz; /* root block size */
72
73 /*
74 * The maximum number of extents in a file, hence the maximum
75 * number of leaf entries, is controlled by the type of di_nextents
76 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
77 * (a signed 16-bit number, xfs_aextnum_t).
78 *
79 * Note that we can no longer assume that if we are in ATTR1 that
80 * the fork offset of all the inodes will be
81 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
82 * with ATTR2 and then mounted back with ATTR1, keeping the
83 * di_forkoff's fixed but probably at various positions. Therefore,
84 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
85 * of a minimum size available.
86 */
87 if (whichfork == XFS_DATA_FORK) {
88 maxleafents = MAXEXTNUM;
89 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
90 } else {
91 maxleafents = MAXAEXTNUM;
92 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
93 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100094 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +110095 minleafrecs = mp->m_bmap_dmnr[0];
96 minnoderecs = mp->m_bmap_dmnr[1];
97 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
98 for (level = 1; maxblocks > 1; level++) {
99 if (maxblocks <= maxrootrecs)
100 maxblocks = 1;
101 else
102 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
103 }
104 mp->m_bm_maxlevels[whichfork] = level;
105}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100107STATIC int /* error */
108xfs_bmbt_lookup_eq(
109 struct xfs_btree_cur *cur,
110 xfs_fileoff_t off,
111 xfs_fsblock_t bno,
112 xfs_filblks_t len,
113 int *stat) /* success/failure */
114{
115 cur->bc_rec.b.br_startoff = off;
116 cur->bc_rec.b.br_startblock = bno;
117 cur->bc_rec.b.br_blockcount = len;
118 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
119}
120
121STATIC int /* error */
122xfs_bmbt_lookup_ge(
123 struct xfs_btree_cur *cur,
124 xfs_fileoff_t off,
125 xfs_fsblock_t bno,
126 xfs_filblks_t len,
127 int *stat) /* success/failure */
128{
129 cur->bc_rec.b.br_startoff = off;
130 cur->bc_rec.b.br_startblock = bno;
131 cur->bc_rec.b.br_blockcount = len;
132 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
133}
134
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100135/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000136 * Check if the inode needs to be converted to btree format.
137 */
138static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
139{
140 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
141 XFS_IFORK_NEXTENTS(ip, whichfork) >
142 XFS_IFORK_MAXEXT(ip, whichfork);
143}
144
145/*
146 * Check if the inode should be converted to extent format.
147 */
148static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
149{
150 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
151 XFS_IFORK_NEXTENTS(ip, whichfork) <=
152 XFS_IFORK_MAXEXT(ip, whichfork);
153}
154
155/*
156 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100157 * by [off, bno, len, state].
158 * This either works (return 0) or gets an EFSCORRUPTED error.
159 */
160STATIC int
161xfs_bmbt_update(
162 struct xfs_btree_cur *cur,
163 xfs_fileoff_t off,
164 xfs_fsblock_t bno,
165 xfs_filblks_t len,
166 xfs_exntst_t state)
167{
168 union xfs_btree_rec rec;
169
170 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
171 return xfs_btree_update(cur, &rec);
172}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100175 * Compute the worst-case number of indirect blocks that will be used
176 * for ip's delayed extent of length "len".
177 */
178STATIC xfs_filblks_t
179xfs_bmap_worst_indlen(
180 xfs_inode_t *ip, /* incore inode pointer */
181 xfs_filblks_t len) /* delayed extent length */
182{
183 int level; /* btree level number */
184 int maxrecs; /* maximum record count at this level */
185 xfs_mount_t *mp; /* mount structure */
186 xfs_filblks_t rval; /* return value */
187
188 mp = ip->i_mount;
189 maxrecs = mp->m_bmap_dmxr[0];
190 for (level = 0, rval = 0;
191 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
192 level++) {
193 len += maxrecs - 1;
194 do_div(len, maxrecs);
195 rval += len;
196 if (len == 1)
197 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
198 level - 1;
199 if (level == 0)
200 maxrecs = mp->m_bmap_dmxr[1];
201 }
202 return rval;
203}
204
205/*
206 * Calculate the default attribute fork offset for newly created inodes.
207 */
208uint
209xfs_default_attroffset(
210 struct xfs_inode *ip)
211{
212 struct xfs_mount *mp = ip->i_mount;
213 uint offset;
214
215 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100216 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100217 XFS_BMDR_SPACE_CALC(MINABTPTRS);
218 } else {
219 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
220 }
221
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100222 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100223 return offset;
224}
225
226/*
227 * Helper routine to reset inode di_forkoff field when switching
228 * attribute fork from local to extent format - we reset it where
229 * possible to make space available for inline data fork extents.
230 */
231STATIC void
232xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100233 xfs_inode_t *ip,
234 int whichfork)
235{
236 if (whichfork == XFS_ATTR_FORK &&
237 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
238 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
239 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
240 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
241
242 if (dfl_forkoff > ip->i_d.di_forkoff)
243 ip->i_d.di_forkoff = dfl_forkoff;
244 }
245}
246
247/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100248 * Debug/sanity checking code
249 */
250
251STATIC int
252xfs_bmap_sanity_check(
253 struct xfs_mount *mp,
254 struct xfs_buf *bp,
255 int level)
256{
257 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
258
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500259 if (block->bb_magic != cpu_to_be32(XFS_BMAP_CRC_MAGIC) &&
260 block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC))
261 return 0;
262
263 if (be16_to_cpu(block->bb_level) != level ||
Dave Chinner9e5987a2013-02-25 12:31:26 +1100264 be16_to_cpu(block->bb_numrecs) == 0 ||
265 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
266 return 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500267
Dave Chinner9e5987a2013-02-25 12:31:26 +1100268 return 1;
269}
270
271#ifdef DEBUG
272STATIC struct xfs_buf *
273xfs_bmap_get_bp(
274 struct xfs_btree_cur *cur,
275 xfs_fsblock_t bno)
276{
277 struct xfs_log_item_desc *lidp;
278 int i;
279
280 if (!cur)
281 return NULL;
282
283 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
284 if (!cur->bc_bufs[i])
285 break;
286 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
287 return cur->bc_bufs[i];
288 }
289
290 /* Chase down all the log items to see if the bp is there */
291 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
292 struct xfs_buf_log_item *bip;
293 bip = (struct xfs_buf_log_item *)lidp->lid_item;
294 if (bip->bli_item.li_type == XFS_LI_BUF &&
295 XFS_BUF_ADDR(bip->bli_buf) == bno)
296 return bip->bli_buf;
297 }
298
299 return NULL;
300}
301
302STATIC void
303xfs_check_block(
304 struct xfs_btree_block *block,
305 xfs_mount_t *mp,
306 int root,
307 short sz)
308{
309 int i, j, dmxr;
310 __be64 *pp, *thispa; /* pointer to block address */
311 xfs_bmbt_key_t *prevp, *keyp;
312
313 ASSERT(be16_to_cpu(block->bb_level) > 0);
314
315 prevp = NULL;
316 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
317 dmxr = mp->m_bmap_dmxr[0];
318 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
319
320 if (prevp) {
321 ASSERT(be64_to_cpu(prevp->br_startoff) <
322 be64_to_cpu(keyp->br_startoff));
323 }
324 prevp = keyp;
325
326 /*
327 * Compare the block numbers to see if there are dups.
328 */
329 if (root)
330 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
331 else
332 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
333
334 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
335 if (root)
336 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
337 else
338 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
339 if (*thispa == *pp) {
340 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
341 __func__, j, i,
342 (unsigned long long)be64_to_cpu(*thispa));
343 panic("%s: ptrs are equal in node\n",
344 __func__);
345 }
346 }
347 }
348}
349
350/*
351 * Check that the extents for the inode ip are in the right order in all
352 * btree leaves.
353 */
354
355STATIC void
356xfs_bmap_check_leaf_extents(
357 xfs_btree_cur_t *cur, /* btree cursor or null */
358 xfs_inode_t *ip, /* incore inode pointer */
359 int whichfork) /* data or attr fork */
360{
361 struct xfs_btree_block *block; /* current btree block */
362 xfs_fsblock_t bno; /* block # of "block" */
363 xfs_buf_t *bp; /* buffer for "block" */
364 int error; /* error return value */
365 xfs_extnum_t i=0, j; /* index into the extents list */
366 xfs_ifork_t *ifp; /* fork structure */
367 int level; /* btree level, for checking */
368 xfs_mount_t *mp; /* file system mount structure */
369 __be64 *pp; /* pointer to block address */
370 xfs_bmbt_rec_t *ep; /* pointer to current extent */
371 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
372 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
373 int bp_release = 0;
374
375 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
376 return;
377 }
378
379 bno = NULLFSBLOCK;
380 mp = ip->i_mount;
381 ifp = XFS_IFORK_PTR(ip, whichfork);
382 block = ifp->if_broot;
383 /*
384 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
385 */
386 level = be16_to_cpu(block->bb_level);
387 ASSERT(level > 0);
388 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
389 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
390 bno = be64_to_cpu(*pp);
391
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000392 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100393 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
394 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
395
396 /*
397 * Go down the tree until leaf level is reached, following the first
398 * pointer (leftmost) at each level.
399 */
400 while (level-- > 0) {
401 /* See if buf is in cur first */
402 bp_release = 0;
403 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
404 if (!bp) {
405 bp_release = 1;
406 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
407 XFS_BMAP_BTREE_REF,
408 &xfs_bmbt_buf_ops);
409 if (error)
410 goto error_norelse;
411 }
412 block = XFS_BUF_TO_BLOCK(bp);
413 XFS_WANT_CORRUPTED_GOTO(
414 xfs_bmap_sanity_check(mp, bp, level),
415 error0);
416 if (level == 0)
417 break;
418
419 /*
420 * Check this block for basic sanity (increasing keys and
421 * no duplicate blocks).
422 */
423
424 xfs_check_block(block, mp, 0, 0);
425 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
426 bno = be64_to_cpu(*pp);
427 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
428 if (bp_release) {
429 bp_release = 0;
430 xfs_trans_brelse(NULL, bp);
431 }
432 }
433
434 /*
435 * Here with bp and block set to the leftmost leaf node in the tree.
436 */
437 i = 0;
438
439 /*
440 * Loop over all leaf nodes checking that all extents are in the right order.
441 */
442 for (;;) {
443 xfs_fsblock_t nextbno;
444 xfs_extnum_t num_recs;
445
446
447 num_recs = xfs_btree_get_numrecs(block);
448
449 /*
450 * Read-ahead the next leaf block, if any.
451 */
452
453 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
454
455 /*
456 * Check all the extents to make sure they are OK.
457 * If we had a previous block, the last entry should
458 * conform with the first entry in this one.
459 */
460
461 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
462 if (i) {
463 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
464 xfs_bmbt_disk_get_blockcount(&last) <=
465 xfs_bmbt_disk_get_startoff(ep));
466 }
467 for (j = 1; j < num_recs; j++) {
468 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
469 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
470 xfs_bmbt_disk_get_blockcount(ep) <=
471 xfs_bmbt_disk_get_startoff(nextp));
472 ep = nextp;
473 }
474
475 last = *ep;
476 i += num_recs;
477 if (bp_release) {
478 bp_release = 0;
479 xfs_trans_brelse(NULL, bp);
480 }
481 bno = nextbno;
482 /*
483 * If we've reached the end, stop.
484 */
485 if (bno == NULLFSBLOCK)
486 break;
487
488 bp_release = 0;
489 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
490 if (!bp) {
491 bp_release = 1;
492 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
493 XFS_BMAP_BTREE_REF,
494 &xfs_bmbt_buf_ops);
495 if (error)
496 goto error_norelse;
497 }
498 block = XFS_BUF_TO_BLOCK(bp);
499 }
500 if (bp_release) {
501 bp_release = 0;
502 xfs_trans_brelse(NULL, bp);
503 }
504 return;
505
506error0:
507 xfs_warn(mp, "%s: at error0", __func__);
508 if (bp_release)
509 xfs_trans_brelse(NULL, bp);
510error_norelse:
511 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
512 __func__, i);
513 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
514 return;
515}
516
517/*
518 * Add bmap trace insert entries for all the contents of the extent records.
519 */
520void
521xfs_bmap_trace_exlist(
522 xfs_inode_t *ip, /* incore inode pointer */
523 xfs_extnum_t cnt, /* count of entries in the list */
524 int whichfork, /* data or attr fork */
525 unsigned long caller_ip)
526{
527 xfs_extnum_t idx; /* extent record index */
528 xfs_ifork_t *ifp; /* inode fork pointer */
529 int state = 0;
530
531 if (whichfork == XFS_ATTR_FORK)
532 state |= BMAP_ATTRFORK;
533
534 ifp = XFS_IFORK_PTR(ip, whichfork);
535 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
536 for (idx = 0; idx < cnt; idx++)
537 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
538}
539
540/*
541 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000542 * given the caller's original parameters. Specifically check the
543 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100544 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
545 */
546STATIC void
547xfs_bmap_validate_ret(
548 xfs_fileoff_t bno,
549 xfs_filblks_t len,
550 int flags,
551 xfs_bmbt_irec_t *mval,
552 int nmap,
553 int ret_nmap)
554{
555 int i; /* index to map values */
556
557 ASSERT(ret_nmap <= nmap);
558
559 for (i = 0; i < ret_nmap; i++) {
560 ASSERT(mval[i].br_blockcount > 0);
561 if (!(flags & XFS_BMAPI_ENTIRE)) {
562 ASSERT(mval[i].br_startoff >= bno);
563 ASSERT(mval[i].br_blockcount <= len);
564 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
565 bno + len);
566 } else {
567 ASSERT(mval[i].br_startoff < bno + len);
568 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
569 bno);
570 }
571 ASSERT(i == 0 ||
572 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
573 mval[i].br_startoff);
574 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
575 mval[i].br_startblock != HOLESTARTBLOCK);
576 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
577 mval[i].br_state == XFS_EXT_UNWRITTEN);
578 }
579}
580
581#else
582#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
583#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
584#endif /* DEBUG */
585
586/*
587 * bmap free list manipulation functions
588 */
589
590/*
591 * Add the extent to the list of extents to be free at transaction end.
592 * The list is maintained sorted (by block number).
593 */
594void
595xfs_bmap_add_free(
596 xfs_fsblock_t bno, /* fs block number of extent */
597 xfs_filblks_t len, /* length of extent */
598 xfs_bmap_free_t *flist, /* list of extents */
599 xfs_mount_t *mp) /* mount point structure */
600{
601 xfs_bmap_free_item_t *cur; /* current (next) element */
602 xfs_bmap_free_item_t *new; /* new element */
603 xfs_bmap_free_item_t *prev; /* previous element */
604#ifdef DEBUG
605 xfs_agnumber_t agno;
606 xfs_agblock_t agbno;
607
608 ASSERT(bno != NULLFSBLOCK);
609 ASSERT(len > 0);
610 ASSERT(len <= MAXEXTLEN);
611 ASSERT(!isnullstartblock(bno));
612 agno = XFS_FSB_TO_AGNO(mp, bno);
613 agbno = XFS_FSB_TO_AGBNO(mp, bno);
614 ASSERT(agno < mp->m_sb.sb_agcount);
615 ASSERT(agbno < mp->m_sb.sb_agblocks);
616 ASSERT(len < mp->m_sb.sb_agblocks);
617 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
618#endif
619 ASSERT(xfs_bmap_free_item_zone != NULL);
620 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
621 new->xbfi_startblock = bno;
622 new->xbfi_blockcount = (xfs_extlen_t)len;
623 for (prev = NULL, cur = flist->xbf_first;
624 cur != NULL;
625 prev = cur, cur = cur->xbfi_next) {
626 if (cur->xbfi_startblock >= bno)
627 break;
628 }
629 if (prev)
630 prev->xbfi_next = new;
631 else
632 flist->xbf_first = new;
633 new->xbfi_next = cur;
634 flist->xbf_count++;
635}
636
637/*
638 * Remove the entry "free" from the free item list. Prev points to the
639 * previous entry, unless "free" is the head of the list.
640 */
Dave Chinner68988112013-08-12 20:49:42 +1000641void
Dave Chinner9e5987a2013-02-25 12:31:26 +1100642xfs_bmap_del_free(
643 xfs_bmap_free_t *flist, /* free item list header */
644 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
645 xfs_bmap_free_item_t *free) /* list item to be freed */
646{
647 if (prev)
648 prev->xbfi_next = free->xbfi_next;
649 else
650 flist->xbf_first = free->xbfi_next;
651 flist->xbf_count--;
652 kmem_zone_free(xfs_bmap_free_item_zone, free);
653}
654
Dave Chinner9e5987a2013-02-25 12:31:26 +1100655/*
656 * Free up any items left in the list.
657 */
658void
659xfs_bmap_cancel(
660 xfs_bmap_free_t *flist) /* list of bmap_free_items */
661{
662 xfs_bmap_free_item_t *free; /* free list item */
663 xfs_bmap_free_item_t *next;
664
665 if (flist->xbf_count == 0)
666 return;
667 ASSERT(flist->xbf_first != NULL);
668 for (free = flist->xbf_first; free; free = next) {
669 next = free->xbfi_next;
670 xfs_bmap_del_free(flist, NULL, free);
671 }
672 ASSERT(flist->xbf_count == 0);
673}
674
675/*
676 * Inode fork format manipulation functions
677 */
678
679/*
680 * Transform a btree format file with only one leaf node, where the
681 * extents list will fit in the inode, into an extents format file.
682 * Since the file extents are already in-core, all we have to do is
683 * give up the space for the btree root and pitch the leaf block.
684 */
685STATIC int /* error */
686xfs_bmap_btree_to_extents(
687 xfs_trans_t *tp, /* transaction pointer */
688 xfs_inode_t *ip, /* incore inode pointer */
689 xfs_btree_cur_t *cur, /* btree cursor */
690 int *logflagsp, /* inode logging flags */
691 int whichfork) /* data or attr fork */
692{
693 /* REFERENCED */
694 struct xfs_btree_block *cblock;/* child btree block */
695 xfs_fsblock_t cbno; /* child block number */
696 xfs_buf_t *cbp; /* child block's buffer */
697 int error; /* error return value */
698 xfs_ifork_t *ifp; /* inode fork data */
699 xfs_mount_t *mp; /* mount point structure */
700 __be64 *pp; /* ptr to block address */
701 struct xfs_btree_block *rblock;/* root btree block */
702
703 mp = ip->i_mount;
704 ifp = XFS_IFORK_PTR(ip, whichfork);
705 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
706 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
707 rblock = ifp->if_broot;
708 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
709 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
710 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
711 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
712 cbno = be64_to_cpu(*pp);
713 *logflagsp = 0;
714#ifdef DEBUG
715 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
716 return error;
717#endif
718 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
719 &xfs_bmbt_buf_ops);
720 if (error)
721 return error;
722 cblock = XFS_BUF_TO_BLOCK(cbp);
723 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
724 return error;
725 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
726 ip->i_d.di_nblocks--;
727 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
728 xfs_trans_binval(tp, cbp);
729 if (cur->bc_bufs[0] == cbp)
730 cur->bc_bufs[0] = NULL;
731 xfs_iroot_realloc(ip, -1, whichfork);
732 ASSERT(ifp->if_broot == NULL);
733 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
734 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
735 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
736 return 0;
737}
738
739/*
740 * Convert an extents-format file into a btree-format file.
741 * The new file will have a root block (in the inode) and a single child block.
742 */
743STATIC int /* error */
744xfs_bmap_extents_to_btree(
745 xfs_trans_t *tp, /* transaction pointer */
746 xfs_inode_t *ip, /* incore inode pointer */
747 xfs_fsblock_t *firstblock, /* first-block-allocated */
748 xfs_bmap_free_t *flist, /* blocks freed in xaction */
749 xfs_btree_cur_t **curp, /* cursor returned to caller */
750 int wasdel, /* converting a delayed alloc */
751 int *logflagsp, /* inode logging flags */
752 int whichfork) /* data or attr fork */
753{
754 struct xfs_btree_block *ablock; /* allocated (child) bt block */
755 xfs_buf_t *abp; /* buffer for ablock */
756 xfs_alloc_arg_t args; /* allocation arguments */
757 xfs_bmbt_rec_t *arp; /* child record pointer */
758 struct xfs_btree_block *block; /* btree root block */
759 xfs_btree_cur_t *cur; /* bmap btree cursor */
760 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
761 int error; /* error return value */
762 xfs_extnum_t i, cnt; /* extent record index */
763 xfs_ifork_t *ifp; /* inode fork pointer */
764 xfs_bmbt_key_t *kp; /* root block key pointer */
765 xfs_mount_t *mp; /* mount structure */
766 xfs_extnum_t nextents; /* number of file extents */
767 xfs_bmbt_ptr_t *pp; /* root block address pointer */
768
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500769 mp = ip->i_mount;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100770 ifp = XFS_IFORK_PTR(ip, whichfork);
771 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
772
773 /*
774 * Make space in the inode incore.
775 */
776 xfs_iroot_realloc(ip, 1, whichfork);
777 ifp->if_flags |= XFS_IFBROOT;
778
779 /*
780 * Fill in the root.
781 */
782 block = ifp->if_broot;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500783 if (xfs_sb_version_hascrc(&mp->m_sb))
784 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
785 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
786 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
787 else
788 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
789 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
790 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100791
792 /*
793 * Need a cursor. Can't allocate until bb_level is filled in.
794 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100795 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
796 cur->bc_private.b.firstblock = *firstblock;
797 cur->bc_private.b.flist = flist;
798 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
799 /*
800 * Convert to a btree with two levels, one record in root.
801 */
802 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
803 memset(&args, 0, sizeof(args));
804 args.tp = tp;
805 args.mp = mp;
806 args.firstblock = *firstblock;
807 if (*firstblock == NULLFSBLOCK) {
808 args.type = XFS_ALLOCTYPE_START_BNO;
809 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
810 } else if (flist->xbf_low) {
811 args.type = XFS_ALLOCTYPE_START_BNO;
812 args.fsbno = *firstblock;
813 } else {
814 args.type = XFS_ALLOCTYPE_NEAR_BNO;
815 args.fsbno = *firstblock;
816 }
817 args.minlen = args.maxlen = args.prod = 1;
818 args.wasdel = wasdel;
819 *logflagsp = 0;
820 if ((error = xfs_alloc_vextent(&args))) {
821 xfs_iroot_realloc(ip, -1, whichfork);
822 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
823 return error;
824 }
825 /*
826 * Allocation can't fail, the space was reserved.
827 */
828 ASSERT(args.fsbno != NULLFSBLOCK);
829 ASSERT(*firstblock == NULLFSBLOCK ||
830 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
831 (flist->xbf_low &&
832 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
833 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
834 cur->bc_private.b.allocated++;
835 ip->i_d.di_nblocks++;
836 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
837 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
838 /*
839 * Fill in the child block.
840 */
841 abp->b_ops = &xfs_bmbt_buf_ops;
842 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500843 if (xfs_sb_version_hascrc(&mp->m_sb))
844 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
845 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
846 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
847 else
848 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
849 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
850 XFS_BTREE_LONG_PTRS);
851
Dave Chinner9e5987a2013-02-25 12:31:26 +1100852 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
853 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
854 for (cnt = i = 0; i < nextents; i++) {
855 ep = xfs_iext_get_ext(ifp, i);
856 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
857 arp->l0 = cpu_to_be64(ep->l0);
858 arp->l1 = cpu_to_be64(ep->l1);
859 arp++; cnt++;
860 }
861 }
862 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
863 xfs_btree_set_numrecs(ablock, cnt);
864
865 /*
866 * Fill in the root key and pointer.
867 */
868 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
869 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
870 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
871 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
872 be16_to_cpu(block->bb_level)));
873 *pp = cpu_to_be64(args.fsbno);
874
875 /*
876 * Do all this logging at the end so that
877 * the root is at the right level.
878 */
879 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
880 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
881 ASSERT(*curp == NULL);
882 *curp = cur;
883 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
884 return 0;
885}
886
887/*
888 * Convert a local file to an extents file.
889 * This code is out of bounds for data forks of regular files,
890 * since the file data needs to get logged so things will stay consistent.
891 * (The bmap-level manipulations are ok, though).
892 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000893void
894xfs_bmap_local_to_extents_empty(
895 struct xfs_inode *ip,
896 int whichfork)
897{
898 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
899
900 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
901 ASSERT(ifp->if_bytes == 0);
902 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
903
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000904 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000905 ifp->if_flags &= ~XFS_IFINLINE;
906 ifp->if_flags |= XFS_IFEXTENTS;
907 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
908}
909
910
Dave Chinner9e5987a2013-02-25 12:31:26 +1100911STATIC int /* error */
912xfs_bmap_local_to_extents(
913 xfs_trans_t *tp, /* transaction pointer */
914 xfs_inode_t *ip, /* incore inode pointer */
915 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
916 xfs_extlen_t total, /* total blocks needed by transaction */
917 int *logflagsp, /* inode logging flags */
918 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500919 void (*init_fn)(struct xfs_trans *tp,
920 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100921 struct xfs_inode *ip,
922 struct xfs_ifork *ifp))
923{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000924 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100925 int flags; /* logging flags returned */
926 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000927 xfs_alloc_arg_t args; /* allocation arguments */
928 xfs_buf_t *bp; /* buffer for extent block */
929 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100930
931 /*
932 * We don't want to deal with the case of keeping inode data inline yet.
933 * So sending the data fork of a regular inode is invalid.
934 */
935 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
936 ifp = XFS_IFORK_PTR(ip, whichfork);
937 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000938
939 if (!ifp->if_bytes) {
940 xfs_bmap_local_to_extents_empty(ip, whichfork);
941 flags = XFS_ILOG_CORE;
942 goto done;
943 }
944
Dave Chinner9e5987a2013-02-25 12:31:26 +1100945 flags = 0;
946 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000947 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
948 XFS_IFINLINE);
949 memset(&args, 0, sizeof(args));
950 args.tp = tp;
951 args.mp = ip->i_mount;
952 args.firstblock = *firstblock;
953 /*
954 * Allocate a block. We know we need only one, since the
955 * file currently fits in an inode.
956 */
957 if (*firstblock == NULLFSBLOCK) {
958 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
959 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100960 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000961 args.fsbno = *firstblock;
962 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100963 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000964 args.total = total;
965 args.minlen = args.maxlen = args.prod = 1;
966 error = xfs_alloc_vextent(&args);
967 if (error)
968 goto done;
969
970 /* Can't fail, the space was reserved. */
971 ASSERT(args.fsbno != NULLFSBLOCK);
972 ASSERT(args.len == 1);
973 *firstblock = args.fsbno;
974 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
975
976 /* initialise the block and copy the data */
977 init_fn(tp, bp, ip, ifp);
978
979 /* account for the change in fork size and log everything */
980 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
981 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
982 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100983 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000984
985 xfs_iext_add(ifp, 0, 1);
986 ep = xfs_iext_get_ext(ifp, 0);
987 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
988 trace_xfs_bmap_post_update(ip, 0,
989 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
990 _THIS_IP_);
991 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
992 ip->i_d.di_nblocks = 1;
993 xfs_trans_mod_dquot_byino(tp, ip,
994 XFS_TRANS_DQ_BCOUNT, 1L);
995 flags |= xfs_ilog_fext(whichfork);
996
Dave Chinner9e5987a2013-02-25 12:31:26 +1100997done:
998 *logflagsp = flags;
999 return error;
1000}
1001
1002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 * Called from xfs_bmap_add_attrfork to handle btree format files.
1004 */
1005STATIC int /* error */
1006xfs_bmap_add_attrfork_btree(
1007 xfs_trans_t *tp, /* transaction pointer */
1008 xfs_inode_t *ip, /* incore inode pointer */
1009 xfs_fsblock_t *firstblock, /* first block allocated */
1010 xfs_bmap_free_t *flist, /* blocks to free at commit */
1011 int *flags) /* inode logging flags */
1012{
1013 xfs_btree_cur_t *cur; /* btree cursor */
1014 int error; /* error return value */
1015 xfs_mount_t *mp; /* file system mount struct */
1016 int stat; /* newroot status */
1017
1018 mp = ip->i_mount;
1019 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1020 *flags |= XFS_ILOG_DBROOT;
1021 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001022 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 cur->bc_private.b.flist = flist;
1024 cur->bc_private.b.firstblock = *firstblock;
1025 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1026 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001027 /* must be at least one entry */
1028 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001029 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 goto error0;
1031 if (stat == 0) {
1032 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +10001033 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035 *firstblock = cur->bc_private.b.firstblock;
1036 cur->bc_private.b.allocated = 0;
1037 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1038 }
1039 return 0;
1040error0:
1041 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1042 return error;
1043}
1044
1045/*
1046 * Called from xfs_bmap_add_attrfork to handle extents format files.
1047 */
1048STATIC int /* error */
1049xfs_bmap_add_attrfork_extents(
1050 xfs_trans_t *tp, /* transaction pointer */
1051 xfs_inode_t *ip, /* incore inode pointer */
1052 xfs_fsblock_t *firstblock, /* first block allocated */
1053 xfs_bmap_free_t *flist, /* blocks to free at commit */
1054 int *flags) /* inode logging flags */
1055{
1056 xfs_btree_cur_t *cur; /* bmap btree cursor */
1057 int error; /* error return value */
1058
1059 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1060 return 0;
1061 cur = NULL;
1062 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
1063 flags, XFS_DATA_FORK);
1064 if (cur) {
1065 cur->bc_private.b.allocated = 0;
1066 xfs_btree_del_cursor(cur,
1067 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1068 }
1069 return error;
1070}
1071
1072/*
Dave Chinner1e823792013-02-11 15:58:13 +11001073 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1074 * different data fork content type needs a different callout to do the
1075 * conversion. Some are basic and only require special block initialisation
1076 * callouts for the data formating, others (directories) are so specialised they
1077 * handle everything themselves.
1078 *
1079 * XXX (dgc): investigate whether directory conversion can use the generic
1080 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001081 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 */
1083STATIC int /* error */
1084xfs_bmap_add_attrfork_local(
1085 xfs_trans_t *tp, /* transaction pointer */
1086 xfs_inode_t *ip, /* incore inode pointer */
1087 xfs_fsblock_t *firstblock, /* first block allocated */
1088 xfs_bmap_free_t *flist, /* blocks to free at commit */
1089 int *flags) /* inode logging flags */
1090{
1091 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1094 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001095
Al Viroabbede12011-07-26 02:31:30 -04001096 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001098 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 dargs.dp = ip;
1100 dargs.firstblock = firstblock;
1101 dargs.flist = flist;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001102 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 dargs.whichfork = XFS_DATA_FORK;
1104 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001105 return xfs_dir2_sf_to_block(&dargs);
1106 }
1107
1108 if (S_ISLNK(ip->i_d.di_mode))
1109 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1110 flags, XFS_DATA_FORK,
1111 xfs_symlink_local_to_remote);
1112
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001113 /* should only be called for types that support local format data */
1114 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001115 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001119 * Convert inode from non-attributed to attributed.
1120 * Must not be in a transaction, ip must not be locked.
1121 */
1122int /* error code */
1123xfs_bmap_add_attrfork(
1124 xfs_inode_t *ip, /* incore inode pointer */
1125 int size, /* space new attribute needs */
1126 int rsvd) /* xact may use reserved blks */
1127{
1128 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1129 xfs_bmap_free_t flist; /* freed extent records */
1130 xfs_mount_t *mp; /* mount structure */
1131 xfs_trans_t *tp; /* transaction pointer */
1132 int blks; /* space reservation */
1133 int version = 1; /* superblock attr version */
1134 int committed; /* xaction was committed */
1135 int logflags; /* logging flags */
1136 int error; /* error return value */
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001137 int cancel_flags = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001138
1139 ASSERT(XFS_IFORK_Q(ip) == 0);
1140
1141 mp = ip->i_mount;
1142 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1143 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
1144 blks = XFS_ADDAFORK_SPACE_RES(mp);
1145 if (rsvd)
1146 tp->t_flags |= XFS_TRANS_RESERVE;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001147 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_addafork, blks, 0);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001148 if (error) {
1149 xfs_trans_cancel(tp, 0);
1150 return error;
1151 }
1152 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001153 xfs_ilock(ip, XFS_ILOCK_EXCL);
1154 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1155 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1156 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001157 if (error)
1158 goto trans_cancel;
1159 cancel_flags |= XFS_TRANS_ABORT;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001160 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001161 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001162 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1163 /*
1164 * For inodes coming from pre-6.2 filesystems.
1165 */
1166 ASSERT(ip->i_d.di_aformat == 0);
1167 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1168 }
1169 ASSERT(ip->i_d.di_anextents == 0);
1170
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001171 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001172 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1173
1174 switch (ip->i_d.di_format) {
1175 case XFS_DINODE_FMT_DEV:
1176 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1177 break;
1178 case XFS_DINODE_FMT_UUID:
1179 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1180 break;
1181 case XFS_DINODE_FMT_LOCAL:
1182 case XFS_DINODE_FMT_EXTENTS:
1183 case XFS_DINODE_FMT_BTREE:
1184 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1185 if (!ip->i_d.di_forkoff)
1186 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1187 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1188 version = 2;
1189 break;
1190 default:
1191 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001192 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001193 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001194 }
1195
1196 ASSERT(ip->i_afp == NULL);
1197 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1198 ip->i_afp->if_flags = XFS_IFEXTENTS;
1199 logflags = 0;
1200 xfs_bmap_init(&flist, &firstblock);
1201 switch (ip->i_d.di_format) {
1202 case XFS_DINODE_FMT_LOCAL:
1203 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
1204 &logflags);
1205 break;
1206 case XFS_DINODE_FMT_EXTENTS:
1207 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1208 &flist, &logflags);
1209 break;
1210 case XFS_DINODE_FMT_BTREE:
1211 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
1212 &logflags);
1213 break;
1214 default:
1215 error = 0;
1216 break;
1217 }
1218 if (logflags)
1219 xfs_trans_log_inode(tp, ip, logflags);
1220 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001221 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001222 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1223 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001224 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001225
1226 spin_lock(&mp->m_sb_lock);
1227 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1228 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001229 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001230 }
1231 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1232 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001233 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001234 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001235 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001236 if (log_sb)
1237 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001238 }
1239
1240 error = xfs_bmap_finish(&tp, &flist, &committed);
1241 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001242 goto bmap_cancel;
1243 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001244 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001245 return error;
1246
1247bmap_cancel:
1248 xfs_bmap_cancel(&flist);
1249trans_cancel:
1250 xfs_trans_cancel(tp, cancel_flags);
1251 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001252 return error;
1253}
1254
1255/*
1256 * Internal and external extent tree search functions.
1257 */
1258
1259/*
1260 * Read in the extents to if_extents.
1261 * All inode fields are set up by caller, we just traverse the btree
1262 * and copy the records in. If the file system cannot contain unwritten
1263 * extents, the records are checked for no "state" flags.
1264 */
1265int /* error */
1266xfs_bmap_read_extents(
1267 xfs_trans_t *tp, /* transaction pointer */
1268 xfs_inode_t *ip, /* incore inode */
1269 int whichfork) /* data or attr fork */
1270{
1271 struct xfs_btree_block *block; /* current btree block */
1272 xfs_fsblock_t bno; /* block # of "block" */
1273 xfs_buf_t *bp; /* buffer for "block" */
1274 int error; /* error return value */
1275 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1276 xfs_extnum_t i, j; /* index into the extents list */
1277 xfs_ifork_t *ifp; /* fork structure */
1278 int level; /* btree level, for checking */
1279 xfs_mount_t *mp; /* file system mount structure */
1280 __be64 *pp; /* pointer to block address */
1281 /* REFERENCED */
1282 xfs_extnum_t room; /* number of entries there's room for */
1283
1284 bno = NULLFSBLOCK;
1285 mp = ip->i_mount;
1286 ifp = XFS_IFORK_PTR(ip, whichfork);
1287 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1288 XFS_EXTFMT_INODE(ip);
1289 block = ifp->if_broot;
1290 /*
1291 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1292 */
1293 level = be16_to_cpu(block->bb_level);
1294 ASSERT(level > 0);
1295 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1296 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001297 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001298 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1299 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1300 /*
1301 * Go down the tree until leaf level is reached, following the first
1302 * pointer (leftmost) at each level.
1303 */
1304 while (level-- > 0) {
1305 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1306 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1307 if (error)
1308 return error;
1309 block = XFS_BUF_TO_BLOCK(bp);
1310 XFS_WANT_CORRUPTED_GOTO(
1311 xfs_bmap_sanity_check(mp, bp, level),
1312 error0);
1313 if (level == 0)
1314 break;
1315 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1316 bno = be64_to_cpu(*pp);
1317 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
1318 xfs_trans_brelse(tp, bp);
1319 }
1320 /*
1321 * Here with bp and block set to the leftmost leaf node in the tree.
1322 */
1323 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1324 i = 0;
1325 /*
1326 * Loop over all leaf nodes. Copy information to the extent records.
1327 */
1328 for (;;) {
1329 xfs_bmbt_rec_t *frp;
1330 xfs_fsblock_t nextbno;
1331 xfs_extnum_t num_recs;
1332 xfs_extnum_t start;
1333
1334 num_recs = xfs_btree_get_numrecs(block);
1335 if (unlikely(i + num_recs > room)) {
1336 ASSERT(i + num_recs <= room);
1337 xfs_warn(ip->i_mount,
1338 "corrupt dinode %Lu, (btree extents).",
1339 (unsigned long long) ip->i_ino);
1340 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1341 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1342 goto error0;
1343 }
1344 XFS_WANT_CORRUPTED_GOTO(
1345 xfs_bmap_sanity_check(mp, bp, 0),
1346 error0);
1347 /*
1348 * Read-ahead the next leaf block, if any.
1349 */
1350 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1351 if (nextbno != NULLFSBLOCK)
1352 xfs_btree_reada_bufl(mp, nextbno, 1,
1353 &xfs_bmbt_buf_ops);
1354 /*
1355 * Copy records into the extent records.
1356 */
1357 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1358 start = i;
1359 for (j = 0; j < num_recs; j++, i++, frp++) {
1360 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1361 trp->l0 = be64_to_cpu(frp->l0);
1362 trp->l1 = be64_to_cpu(frp->l1);
1363 }
1364 if (exntf == XFS_EXTFMT_NOSTATE) {
1365 /*
1366 * Check all attribute bmap btree records and
1367 * any "older" data bmap btree records for a
1368 * set bit in the "extent flag" position.
1369 */
1370 if (unlikely(xfs_check_nostate_extents(ifp,
1371 start, num_recs))) {
1372 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1373 XFS_ERRLEVEL_LOW,
1374 ip->i_mount);
1375 goto error0;
1376 }
1377 }
1378 xfs_trans_brelse(tp, bp);
1379 bno = nextbno;
1380 /*
1381 * If we've reached the end, stop.
1382 */
1383 if (bno == NULLFSBLOCK)
1384 break;
1385 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1386 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1387 if (error)
1388 return error;
1389 block = XFS_BUF_TO_BLOCK(bp);
1390 }
1391 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1392 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1393 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1394 return 0;
1395error0:
1396 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001397 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001398}
1399
1400
1401/*
1402 * Search the extent records for the entry containing block bno.
1403 * If bno lies in a hole, point to the next entry. If bno lies
1404 * past eof, *eofp will be set, and *prevp will contain the last
1405 * entry (null if none). Else, *lastxp will be set to the index
1406 * of the found entry; *gotp will contain the entry.
1407 */
1408STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1409xfs_bmap_search_multi_extents(
1410 xfs_ifork_t *ifp, /* inode fork pointer */
1411 xfs_fileoff_t bno, /* block number searched for */
1412 int *eofp, /* out: end of file found */
1413 xfs_extnum_t *lastxp, /* out: last extent index */
1414 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1415 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1416{
1417 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1418 xfs_extnum_t lastx; /* last extent index */
1419
1420 /*
1421 * Initialize the extent entry structure to catch access to
1422 * uninitialized br_startblock field.
1423 */
1424 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1425 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1426 gotp->br_state = XFS_EXT_INVALID;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001427 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001428 prevp->br_startoff = NULLFILEOFF;
1429
1430 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1431 if (lastx > 0) {
1432 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1433 }
1434 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1435 xfs_bmbt_get_all(ep, gotp);
1436 *eofp = 0;
1437 } else {
1438 if (lastx > 0) {
1439 *gotp = *prevp;
1440 }
1441 *eofp = 1;
1442 ep = NULL;
1443 }
1444 *lastxp = lastx;
1445 return ep;
1446}
1447
1448/*
1449 * Search the extents list for the inode, for the extent containing bno.
1450 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1451 * *eofp will be set, and *prevp will contain the last entry (null if none).
1452 * Else, *lastxp will be set to the index of the found
1453 * entry; *gotp will contain the entry.
1454 */
1455STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1456xfs_bmap_search_extents(
1457 xfs_inode_t *ip, /* incore inode pointer */
1458 xfs_fileoff_t bno, /* block number searched for */
1459 int fork, /* data or attr fork */
1460 int *eofp, /* out: end of file found */
1461 xfs_extnum_t *lastxp, /* out: last extent index */
1462 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1463 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1464{
1465 xfs_ifork_t *ifp; /* inode fork pointer */
1466 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1467
1468 XFS_STATS_INC(xs_look_exlist);
1469 ifp = XFS_IFORK_PTR(ip, fork);
1470
1471 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1472
1473 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1474 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1475 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1476 "Access to block zero in inode %llu "
1477 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -05001478 "blkcnt: %llx extent-state: %x lastx: %x",
Dave Chinner9e5987a2013-02-25 12:31:26 +11001479 (unsigned long long)ip->i_ino,
1480 (unsigned long long)gotp->br_startblock,
1481 (unsigned long long)gotp->br_startoff,
1482 (unsigned long long)gotp->br_blockcount,
1483 gotp->br_state, *lastxp);
1484 *lastxp = NULLEXTNUM;
1485 *eofp = 1;
1486 return NULL;
1487 }
1488 return ep;
1489}
1490
1491/*
1492 * Returns the file-relative block number of the first unused block(s)
1493 * in the file with at least "len" logically contiguous blocks free.
1494 * This is the lowest-address hole if the file has holes, else the first block
1495 * past the end of file.
1496 * Return 0 if the file is currently local (in-inode).
1497 */
1498int /* error */
1499xfs_bmap_first_unused(
1500 xfs_trans_t *tp, /* transaction pointer */
1501 xfs_inode_t *ip, /* incore inode */
1502 xfs_extlen_t len, /* size of hole to find */
1503 xfs_fileoff_t *first_unused, /* unused block */
1504 int whichfork) /* data or attr fork */
1505{
1506 int error; /* error return value */
1507 int idx; /* extent record index */
1508 xfs_ifork_t *ifp; /* inode fork pointer */
1509 xfs_fileoff_t lastaddr; /* last block number seen */
1510 xfs_fileoff_t lowest; /* lowest useful block */
1511 xfs_fileoff_t max; /* starting useful block */
1512 xfs_fileoff_t off; /* offset for this block */
1513 xfs_extnum_t nextents; /* number of extent entries */
1514
1515 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1516 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1517 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1518 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1519 *first_unused = 0;
1520 return 0;
1521 }
1522 ifp = XFS_IFORK_PTR(ip, whichfork);
1523 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1524 (error = xfs_iread_extents(tp, ip, whichfork)))
1525 return error;
1526 lowest = *first_unused;
1527 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1528 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1529 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1530 off = xfs_bmbt_get_startoff(ep);
1531 /*
1532 * See if the hole before this extent will work.
1533 */
1534 if (off >= lowest + len && off - max >= len) {
1535 *first_unused = max;
1536 return 0;
1537 }
1538 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1539 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1540 }
1541 *first_unused = max;
1542 return 0;
1543}
1544
1545/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001546 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001547 * last_block (input value) in the file.
1548 * This is not based on i_size, it is based on the extent records.
1549 * Returns 0 for local files, as they do not have extent records.
1550 */
1551int /* error */
1552xfs_bmap_last_before(
1553 xfs_trans_t *tp, /* transaction pointer */
1554 xfs_inode_t *ip, /* incore inode */
1555 xfs_fileoff_t *last_block, /* last block */
1556 int whichfork) /* data or attr fork */
1557{
1558 xfs_fileoff_t bno; /* input file offset */
1559 int eof; /* hit end of file */
1560 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1561 int error; /* error return value */
1562 xfs_bmbt_irec_t got; /* current extent value */
1563 xfs_ifork_t *ifp; /* inode fork pointer */
1564 xfs_extnum_t lastx; /* last extent used */
1565 xfs_bmbt_irec_t prev; /* previous extent value */
1566
1567 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1568 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1569 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +10001570 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001571 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1572 *last_block = 0;
1573 return 0;
1574 }
1575 ifp = XFS_IFORK_PTR(ip, whichfork);
1576 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1577 (error = xfs_iread_extents(tp, ip, whichfork)))
1578 return error;
1579 bno = *last_block - 1;
1580 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1581 &prev);
1582 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1583 if (prev.br_startoff == NULLFILEOFF)
1584 *last_block = 0;
1585 else
1586 *last_block = prev.br_startoff + prev.br_blockcount;
1587 }
1588 /*
1589 * Otherwise *last_block is already the right answer.
1590 */
1591 return 0;
1592}
1593
Dave Chinner68988112013-08-12 20:49:42 +10001594int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001595xfs_bmap_last_extent(
1596 struct xfs_trans *tp,
1597 struct xfs_inode *ip,
1598 int whichfork,
1599 struct xfs_bmbt_irec *rec,
1600 int *is_empty)
1601{
1602 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1603 int error;
1604 int nextents;
1605
1606 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1607 error = xfs_iread_extents(tp, ip, whichfork);
1608 if (error)
1609 return error;
1610 }
1611
1612 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1613 if (nextents == 0) {
1614 *is_empty = 1;
1615 return 0;
1616 }
1617
1618 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1619 *is_empty = 0;
1620 return 0;
1621}
1622
1623/*
1624 * Check the last inode extent to determine whether this allocation will result
1625 * in blocks being allocated at the end of the file. When we allocate new data
1626 * blocks at the end of the file which do not start at the previous data block,
1627 * we will try to align the new blocks at stripe unit boundaries.
1628 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001629 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001630 * at, or past the EOF.
1631 */
1632STATIC int
1633xfs_bmap_isaeof(
1634 struct xfs_bmalloca *bma,
1635 int whichfork)
1636{
1637 struct xfs_bmbt_irec rec;
1638 int is_empty;
1639 int error;
1640
1641 bma->aeof = 0;
1642 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1643 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001644 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001645 return error;
1646
Dave Chinner6e708bc2013-11-22 10:41:16 +11001647 if (is_empty) {
1648 bma->aeof = 1;
1649 return 0;
1650 }
1651
Dave Chinner9e5987a2013-02-25 12:31:26 +11001652 /*
1653 * Check if we are allocation or past the last extent, or at least into
1654 * the last delayed allocated extent.
1655 */
1656 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1657 (bma->offset >= rec.br_startoff &&
1658 isnullstartblock(rec.br_startblock));
1659 return 0;
1660}
1661
1662/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001663 * Returns the file-relative block number of the first block past eof in
1664 * the file. This is not based on i_size, it is based on the extent records.
1665 * Returns 0 for local files, as they do not have extent records.
1666 */
1667int
1668xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001669 struct xfs_inode *ip,
1670 xfs_fileoff_t *last_block,
1671 int whichfork)
1672{
1673 struct xfs_bmbt_irec rec;
1674 int is_empty;
1675 int error;
1676
1677 *last_block = 0;
1678
1679 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1680 return 0;
1681
1682 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1683 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001684 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001685
1686 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1687 if (error || is_empty)
1688 return error;
1689
1690 *last_block = rec.br_startoff + rec.br_blockcount;
1691 return 0;
1692}
1693
1694/*
1695 * Returns whether the selected fork of the inode has exactly one
1696 * block or not. For the data fork we check this matches di_size,
1697 * implying the file's range is 0..bsize-1.
1698 */
1699int /* 1=>1 block, 0=>otherwise */
1700xfs_bmap_one_block(
1701 xfs_inode_t *ip, /* incore inode */
1702 int whichfork) /* data or attr fork */
1703{
1704 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1705 xfs_ifork_t *ifp; /* inode fork pointer */
1706 int rval; /* return value */
1707 xfs_bmbt_irec_t s; /* internal version of extent */
1708
1709#ifndef DEBUG
1710 if (whichfork == XFS_DATA_FORK)
1711 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1712#endif /* !DEBUG */
1713 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1714 return 0;
1715 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1716 return 0;
1717 ifp = XFS_IFORK_PTR(ip, whichfork);
1718 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1719 ep = xfs_iext_get_ext(ifp, 0);
1720 xfs_bmbt_get_all(ep, &s);
1721 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1722 if (rval && whichfork == XFS_DATA_FORK)
1723 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1724 return rval;
1725}
1726
1727/*
1728 * Extent tree manipulation functions used during allocation.
1729 */
1730
1731/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001732 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
1734STATIC int /* error */
1735xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001736 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001738 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001740 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001743 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 xfs_fileoff_t new_endoff; /* end offset of new entry */
1745 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1746 /* left is 0, right is 1, prev is 2 */
1747 int rval=0; /* return value (logging flags) */
1748 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001749 xfs_filblks_t da_new; /* new count del alloc blocks used */
1750 xfs_filblks_t da_old; /* old count del alloc blocks used */
1751 xfs_filblks_t temp=0; /* value for da_new calculations */
1752 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001755 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001756
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001757 ASSERT(bma->idx >= 0);
1758 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001759 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001760 ASSERT(!bma->cur ||
1761 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001762
1763 XFS_STATS_INC(xs_add_exlist);
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765#define LEFT r[0]
1766#define RIGHT r[1]
1767#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
1769 /*
1770 * Set up a bunch of variables to make the tests simpler.
1771 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001772 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 xfs_bmbt_get_all(ep, &PREV);
1774 new_endoff = new->br_startoff + new->br_blockcount;
1775 ASSERT(PREV.br_startoff <= new->br_startoff);
1776 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001777
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001778 da_old = startblockval(PREV.br_startblock);
1779 da_new = 0;
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 /*
1782 * Set flags determining what part of the previous delayed allocation
1783 * extent is being replaced by a real allocation.
1784 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001785 if (PREV.br_startoff == new->br_startoff)
1786 state |= BMAP_LEFT_FILLING;
1787 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1788 state |= BMAP_RIGHT_FILLING;
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /*
1791 * Check and set flags if this segment has a left neighbor.
1792 * Don't set contiguous if the combined extent would be too large.
1793 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001794 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001795 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001796 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001797
1798 if (isnullstartblock(LEFT.br_startblock))
1799 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001801
1802 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1803 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1804 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1805 LEFT.br_state == new->br_state &&
1806 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1807 state |= BMAP_LEFT_CONTIG;
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 /*
1810 * Check and set flags if this segment has a right neighbor.
1811 * Don't set contiguous if the combined extent would be too large.
1812 * Also check for all-three-contiguous being too large.
1813 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001814 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001815 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001816 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001817
1818 if (isnullstartblock(RIGHT.br_startblock))
1819 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001821
1822 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1823 new_endoff == RIGHT.br_startoff &&
1824 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1825 new->br_state == RIGHT.br_state &&
1826 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1827 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1828 BMAP_RIGHT_FILLING)) !=
1829 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1830 BMAP_RIGHT_FILLING) ||
1831 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1832 <= MAXEXTLEN))
1833 state |= BMAP_RIGHT_CONTIG;
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 error = 0;
1836 /*
1837 * Switch out based on the FILLING and CONTIG state bits.
1838 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001839 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1840 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1841 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1842 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 /*
1844 * Filling in all of a previously delayed allocation extent.
1845 * The left and right neighbors are both contiguous with new.
1846 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 bma->idx--;
1848 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1849 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 LEFT.br_blockcount + PREV.br_blockcount +
1851 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001852 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001853
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001854 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1855 bma->ip->i_d.di_nextents--;
1856 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1858 else {
1859 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001860 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001862 RIGHT.br_blockcount, &i);
1863 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001865 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001866 error = xfs_btree_delete(bma->cur, &i);
1867 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001869 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001870 error = xfs_btree_decrement(bma->cur, 0, &i);
1871 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001873 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001874 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 LEFT.br_startblock,
1876 LEFT.br_blockcount +
1877 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001878 RIGHT.br_blockcount, LEFT.br_state);
1879 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 goto done;
1881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 break;
1883
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001884 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 /*
1886 * Filling in all of a previously delayed allocation extent.
1887 * The left neighbor is contiguous, the right is not.
1888 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001889 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001890
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001891 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1892 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001893 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001894 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001895
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001896 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1897 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 rval = XFS_ILOG_DEXT;
1899 else {
1900 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001901 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001903 &i);
1904 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001906 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001907 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 LEFT.br_startblock,
1909 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001910 PREV.br_blockcount, LEFT.br_state);
1911 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 goto done;
1913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 break;
1915
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001916 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /*
1918 * Filling in all of a previously delayed allocation extent.
1919 * The right neighbor is contiguous, the left is not.
1920 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001921 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 xfs_bmbt_set_startblock(ep, new->br_startblock);
1923 xfs_bmbt_set_blockcount(ep,
1924 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001925 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001926
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001927 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1928 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 rval = XFS_ILOG_DEXT;
1930 else {
1931 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001932 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001934 RIGHT.br_blockcount, &i);
1935 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001937 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001938 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 new->br_startblock,
1940 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001941 RIGHT.br_blockcount, PREV.br_state);
1942 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 goto done;
1944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 break;
1946
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001947 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 /*
1949 * Filling in all of a previously delayed allocation extent.
1950 * Neither the left nor right neighbors are contiguous with
1951 * the new one.
1952 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001953 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001955 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001956
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001957 bma->ip->i_d.di_nextents++;
1958 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1960 else {
1961 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001962 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001964 &i);
1965 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001967 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001968 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1969 error = xfs_btree_insert(bma->cur, &i);
1970 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001972 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 break;
1975
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001976 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 /*
1978 * Filling in the first part of a previous delayed allocation.
1979 * The left neighbor is contiguous.
1980 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001981 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1982 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 LEFT.br_blockcount + new->br_blockcount);
1984 xfs_bmbt_set_startoff(ep,
1985 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001986 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001987
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001989 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001991 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 rval = XFS_ILOG_DEXT;
1993 else {
1994 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001995 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001997 &i);
1998 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002000 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002001 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 LEFT.br_startblock,
2003 LEFT.br_blockcount +
2004 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002005 LEFT.br_state);
2006 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 goto done;
2008 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002009 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002010 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002011 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002012 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002013
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002014 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 break;
2016
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002017 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 /*
2019 * Filling in the first part of a previous delayed allocation.
2020 * The left neighbor is not contiguous.
2021 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002022 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 xfs_bmbt_set_startoff(ep, new_endoff);
2024 temp = PREV.br_blockcount - new->br_blockcount;
2025 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002026 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
2027 bma->ip->i_d.di_nextents++;
2028 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2030 else {
2031 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002032 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002034 &i);
2035 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002037 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002038 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2039 error = xfs_btree_insert(bma->cur, &i);
2040 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002042 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002044
2045 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002046 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2047 bma->firstblock, bma->flist,
2048 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 rval |= tmp_rval;
2050 if (error)
2051 goto done;
2052 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002053 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002054 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002055 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2056 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002057 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002058 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 break;
2060
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002061 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /*
2063 * Filling in the last part of a previous delayed allocation.
2064 * The right neighbor is contiguous with the new allocation.
2065 */
2066 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002067 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002069 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002070 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 new->br_blockcount + RIGHT.br_blockcount,
2072 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002073 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2074 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 rval = XFS_ILOG_DEXT;
2076 else {
2077 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002078 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002080 RIGHT.br_blockcount, &i);
2081 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002083 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002084 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 new->br_startblock,
2086 new->br_blockcount +
2087 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002088 RIGHT.br_state);
2089 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 goto done;
2091 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002092
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002093 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002094 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002095 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002096 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002097 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002098
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002099 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 break;
2101
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002102 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 /*
2104 * Filling in the last part of a previous delayed allocation.
2105 * The right neighbor is not contiguous.
2106 */
2107 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002108 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002110 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2111 bma->ip->i_d.di_nextents++;
2112 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2114 else {
2115 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002116 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002118 &i);
2119 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002121 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002122 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2123 error = xfs_btree_insert(bma->cur, &i);
2124 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002126 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002128
2129 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002130 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2131 bma->firstblock, bma->flist, &bma->cur, 1,
2132 &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 rval |= tmp_rval;
2134 if (error)
2135 goto done;
2136 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002137 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002138 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002139 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2140 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002141 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002142 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002143
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002144 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 break;
2146
2147 case 0:
2148 /*
2149 * Filling in the middle part of a previous delayed allocation.
2150 * Contiguity is impossible here.
2151 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002152 *
2153 * We start with a delayed allocation:
2154 *
2155 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2156 * PREV @ idx
2157 *
2158 * and we are allocating:
2159 * +rrrrrrrrrrrrrrrrr+
2160 * new
2161 *
2162 * and we set it up for insertion as:
2163 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2164 * new
2165 * PREV @ idx LEFT RIGHT
2166 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 */
2168 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002170 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002171 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2172 LEFT = *new;
2173 RIGHT.br_state = PREV.br_state;
2174 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002175 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002176 RIGHT.br_startoff = new_endoff;
2177 RIGHT.br_blockcount = temp2;
2178 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002179 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2180 bma->ip->i_d.di_nextents++;
2181 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2183 else {
2184 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002185 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002187 &i);
2188 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002190 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002191 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2192 error = xfs_btree_insert(bma->cur, &i);
2193 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002195 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002197
2198 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002199 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2200 bma->firstblock, bma->flist, &bma->cur,
2201 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 rval |= tmp_rval;
2203 if (error)
2204 goto done;
2205 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002206 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2207 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002208 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002209 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002210 if (diff > 0) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002211 error = xfs_icsb_modify_counters(bma->ip->i_mount,
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002212 XFS_SBS_FDBLOCKS,
2213 -((int64_t)diff), 0);
2214 ASSERT(!error);
2215 if (error)
2216 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002218
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002219 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002220 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002221 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2222 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2223 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002224 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002225 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002226
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002227 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002228 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 break;
2230
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002231 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2232 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2233 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2234 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2235 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2236 case BMAP_LEFT_CONTIG:
2237 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 /*
2239 * These cases are all impossible.
2240 */
2241 ASSERT(0);
2242 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002243
2244 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002245 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002246 int tmp_logflags; /* partial log flag return val */
2247
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002248 ASSERT(bma->cur == NULL);
2249 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2250 bma->firstblock, bma->flist, &bma->cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002251 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002252 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002253 if (error)
2254 goto done;
2255 }
2256
2257 /* adjust for changes in reserved delayed indirect blocks */
2258 if (da_old || da_new) {
2259 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002260 if (bma->cur)
2261 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002262 ASSERT(temp <= da_old);
2263 if (temp < da_old)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002264 xfs_icsb_modify_counters(bma->ip->i_mount,
2265 XFS_SBS_FDBLOCKS,
2266 (int64_t)(da_old - temp), 0);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002267 }
2268
2269 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002270 if (bma->cur)
2271 bma->cur->bc_private.b.allocated = 0;
2272
2273 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002275 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 return error;
2277#undef LEFT
2278#undef RIGHT
2279#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
2282/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002283 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 */
2285STATIC int /* error */
2286xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002287 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002289 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002291 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002292 xfs_fsblock_t *first, /* pointer to firstblock variable */
2293 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002294 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002297 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002300 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 xfs_fileoff_t new_endoff; /* end offset of new entry */
2302 xfs_exntst_t newext; /* new extent state */
2303 xfs_exntst_t oldext; /* old extent state */
2304 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2305 /* left is 0, right is 1, prev is 2 */
2306 int rval=0; /* return value (logging flags) */
2307 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002309 *logflagsp = 0;
2310
2311 cur = *curp;
2312 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2313
2314 ASSERT(*idx >= 0);
2315 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2316 ASSERT(!isnullstartblock(new->br_startblock));
2317
2318 XFS_STATS_INC(xs_add_exlist);
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320#define LEFT r[0]
2321#define RIGHT r[1]
2322#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002323
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 /*
2325 * Set up a bunch of variables to make the tests simpler.
2326 */
2327 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002328 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 xfs_bmbt_get_all(ep, &PREV);
2330 newext = new->br_state;
2331 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2332 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2333 ASSERT(PREV.br_state == oldext);
2334 new_endoff = new->br_startoff + new->br_blockcount;
2335 ASSERT(PREV.br_startoff <= new->br_startoff);
2336 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /*
2339 * Set flags determining what part of the previous oldext allocation
2340 * extent is being replaced by a newext allocation.
2341 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002342 if (PREV.br_startoff == new->br_startoff)
2343 state |= BMAP_LEFT_FILLING;
2344 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2345 state |= BMAP_RIGHT_FILLING;
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 /*
2348 * Check and set flags if this segment has a left neighbor.
2349 * Don't set contiguous if the combined extent would be too large.
2350 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002351 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002352 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002353 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002354
2355 if (isnullstartblock(LEFT.br_startblock))
2356 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002358
2359 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2360 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2361 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2362 LEFT.br_state == newext &&
2363 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2364 state |= BMAP_LEFT_CONTIG;
2365
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 /*
2367 * Check and set flags if this segment has a right neighbor.
2368 * Don't set contiguous if the combined extent would be too large.
2369 * Also check for all-three-contiguous being too large.
2370 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002371 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002372 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002373 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002374 if (isnullstartblock(RIGHT.br_startblock))
2375 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002377
2378 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2379 new_endoff == RIGHT.br_startoff &&
2380 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2381 newext == RIGHT.br_state &&
2382 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2383 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2384 BMAP_RIGHT_FILLING)) !=
2385 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2386 BMAP_RIGHT_FILLING) ||
2387 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2388 <= MAXEXTLEN))
2389 state |= BMAP_RIGHT_CONTIG;
2390
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 /*
2392 * Switch out based on the FILLING and CONTIG state bits.
2393 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002394 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2395 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2396 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2397 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 /*
2399 * Setting all of a previous oldext extent to newext.
2400 * The left and right neighbors are both contiguous with new.
2401 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002402 --*idx;
2403
2404 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2405 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 LEFT.br_blockcount + PREV.br_blockcount +
2407 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002408 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002409
Christoph Hellwigec90c552011-05-23 08:52:53 +00002410 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 ip->i_d.di_nextents -= 2;
2412 if (cur == NULL)
2413 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2414 else {
2415 rval = XFS_ILOG_CORE;
2416 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2417 RIGHT.br_startblock,
2418 RIGHT.br_blockcount, &i)))
2419 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002420 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002421 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002423 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002424 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002426 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002427 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002429 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002430 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002432 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2434 LEFT.br_startblock,
2435 LEFT.br_blockcount + PREV.br_blockcount +
2436 RIGHT.br_blockcount, LEFT.br_state)))
2437 goto done;
2438 }
2439 break;
2440
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002441 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 /*
2443 * Setting all of a previous oldext extent to newext.
2444 * The left neighbor is contiguous, the right is not.
2445 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002446 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002447
Christoph Hellwigec90c552011-05-23 08:52:53 +00002448 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2449 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2450 LEFT.br_blockcount + PREV.br_blockcount);
2451 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2452
2453 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 ip->i_d.di_nextents--;
2455 if (cur == NULL)
2456 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2457 else {
2458 rval = XFS_ILOG_CORE;
2459 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2460 PREV.br_startblock, PREV.br_blockcount,
2461 &i)))
2462 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002463 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002464 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002466 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002467 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002469 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2471 LEFT.br_startblock,
2472 LEFT.br_blockcount + PREV.br_blockcount,
2473 LEFT.br_state)))
2474 goto done;
2475 }
2476 break;
2477
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002478 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 /*
2480 * Setting all of a previous oldext extent to newext.
2481 * The right neighbor is contiguous, the left is not.
2482 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002483 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 xfs_bmbt_set_blockcount(ep,
2485 PREV.br_blockcount + RIGHT.br_blockcount);
2486 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002487 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2488 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 ip->i_d.di_nextents--;
2490 if (cur == NULL)
2491 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2492 else {
2493 rval = XFS_ILOG_CORE;
2494 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2495 RIGHT.br_startblock,
2496 RIGHT.br_blockcount, &i)))
2497 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002498 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002499 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002501 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002502 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002504 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2506 new->br_startblock,
2507 new->br_blockcount + RIGHT.br_blockcount,
2508 newext)))
2509 goto done;
2510 }
2511 break;
2512
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002513 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 /*
2515 * Setting all of a previous oldext extent to newext.
2516 * Neither the left nor right neighbors are contiguous with
2517 * the new one.
2518 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002519 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002521 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 if (cur == NULL)
2524 rval = XFS_ILOG_DEXT;
2525 else {
2526 rval = 0;
2527 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2528 new->br_startblock, new->br_blockcount,
2529 &i)))
2530 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002531 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2533 new->br_startblock, new->br_blockcount,
2534 newext)))
2535 goto done;
2536 }
2537 break;
2538
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002539 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /*
2541 * Setting the first part of a previous oldext extent to newext.
2542 * The left neighbor is contiguous.
2543 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002544 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2545 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 LEFT.br_blockcount + new->br_blockcount);
2547 xfs_bmbt_set_startoff(ep,
2548 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002549 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002550
Christoph Hellwigec90c552011-05-23 08:52:53 +00002551 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 xfs_bmbt_set_startblock(ep,
2553 new->br_startblock + new->br_blockcount);
2554 xfs_bmbt_set_blockcount(ep,
2555 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002556 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002557
Christoph Hellwigec90c552011-05-23 08:52:53 +00002558 --*idx;
2559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 if (cur == NULL)
2561 rval = XFS_ILOG_DEXT;
2562 else {
2563 rval = 0;
2564 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2565 PREV.br_startblock, PREV.br_blockcount,
2566 &i)))
2567 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002568 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 if ((error = xfs_bmbt_update(cur,
2570 PREV.br_startoff + new->br_blockcount,
2571 PREV.br_startblock + new->br_blockcount,
2572 PREV.br_blockcount - new->br_blockcount,
2573 oldext)))
2574 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002575 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002577 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 LEFT.br_startblock,
2579 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002580 LEFT.br_state);
2581 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 goto done;
2583 }
2584 break;
2585
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002586 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 /*
2588 * Setting the first part of a previous oldext extent to newext.
2589 * The left neighbor is not contiguous.
2590 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002591 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2593 xfs_bmbt_set_startoff(ep, new_endoff);
2594 xfs_bmbt_set_blockcount(ep,
2595 PREV.br_blockcount - new->br_blockcount);
2596 xfs_bmbt_set_startblock(ep,
2597 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002598 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002599
Christoph Hellwigec90c552011-05-23 08:52:53 +00002600 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 ip->i_d.di_nextents++;
2602 if (cur == NULL)
2603 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2604 else {
2605 rval = XFS_ILOG_CORE;
2606 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2607 PREV.br_startblock, PREV.br_blockcount,
2608 &i)))
2609 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002610 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 if ((error = xfs_bmbt_update(cur,
2612 PREV.br_startoff + new->br_blockcount,
2613 PREV.br_startblock + new->br_blockcount,
2614 PREV.br_blockcount - new->br_blockcount,
2615 oldext)))
2616 goto done;
2617 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002618 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002620 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 }
2622 break;
2623
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002624 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 /*
2626 * Setting the last part of a previous oldext extent to newext.
2627 * The right neighbor is contiguous with the new allocation.
2628 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002629 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 xfs_bmbt_set_blockcount(ep,
2631 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002632 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2633
2634 ++*idx;
2635
2636 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2637 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002638 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002640 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 if (cur == NULL)
2643 rval = XFS_ILOG_DEXT;
2644 else {
2645 rval = 0;
2646 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2647 PREV.br_startblock,
2648 PREV.br_blockcount, &i)))
2649 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002650 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2652 PREV.br_startblock,
2653 PREV.br_blockcount - new->br_blockcount,
2654 oldext)))
2655 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002656 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 goto done;
2658 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2659 new->br_startblock,
2660 new->br_blockcount + RIGHT.br_blockcount,
2661 newext)))
2662 goto done;
2663 }
2664 break;
2665
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002666 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 /*
2668 * Setting the last part of a previous oldext extent to newext.
2669 * The right neighbor is not contiguous.
2670 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002671 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 xfs_bmbt_set_blockcount(ep,
2673 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002674 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002675
Christoph Hellwigec90c552011-05-23 08:52:53 +00002676 ++*idx;
2677 xfs_iext_insert(ip, *idx, 1, new, state);
2678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 ip->i_d.di_nextents++;
2680 if (cur == NULL)
2681 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2682 else {
2683 rval = XFS_ILOG_CORE;
2684 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2685 PREV.br_startblock, PREV.br_blockcount,
2686 &i)))
2687 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002688 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2690 PREV.br_startblock,
2691 PREV.br_blockcount - new->br_blockcount,
2692 oldext)))
2693 goto done;
2694 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2695 new->br_startblock, new->br_blockcount,
2696 &i)))
2697 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002698 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002700 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002702 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 }
2704 break;
2705
2706 case 0:
2707 /*
2708 * Setting the middle part of a previous oldext extent to
2709 * newext. Contiguity is impossible here.
2710 * One extent becomes three extents.
2711 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002712 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 xfs_bmbt_set_blockcount(ep,
2714 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002715 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 r[0] = *new;
2718 r[1].br_startoff = new_endoff;
2719 r[1].br_blockcount =
2720 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2721 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2722 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002723
2724 ++*idx;
2725 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2726
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 ip->i_d.di_nextents += 2;
2728 if (cur == NULL)
2729 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2730 else {
2731 rval = XFS_ILOG_CORE;
2732 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2733 PREV.br_startblock, PREV.br_blockcount,
2734 &i)))
2735 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002736 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 /* new right extent - oldext */
2738 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2739 r[1].br_startblock, r[1].br_blockcount,
2740 r[1].br_state)))
2741 goto done;
2742 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002744 cur->bc_rec.b.br_blockcount =
2745 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002746 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002748 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002749 /*
2750 * Reset the cursor to the position of the new extent
2751 * we are about to insert as we can't trust it after
2752 * the previous insert.
2753 */
2754 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2755 new->br_startblock, new->br_blockcount,
2756 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002758 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002760 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002761 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002763 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 }
2765 break;
2766
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002767 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2768 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2769 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2770 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2771 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2772 case BMAP_LEFT_CONTIG:
2773 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 /*
2775 * These cases are all impossible.
2776 */
2777 ASSERT(0);
2778 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002779
2780 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002781 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002782 int tmp_logflags; /* partial log flag return val */
2783
2784 ASSERT(cur == NULL);
2785 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
2786 0, &tmp_logflags, XFS_DATA_FORK);
2787 *logflagsp |= tmp_logflags;
2788 if (error)
2789 goto done;
2790 }
2791
2792 /* clear out the allocated field, done with it now in any case. */
2793 if (cur) {
2794 cur->bc_private.b.allocated = 0;
2795 *curp = cur;
2796 }
2797
2798 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002800 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 return error;
2802#undef LEFT
2803#undef RIGHT
2804#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805}
2806
2807/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002808 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002810STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811xfs_bmap_add_extent_hole_delay(
2812 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002813 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002814 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002816 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2818 xfs_filblks_t newlen=0; /* new indirect size */
2819 xfs_filblks_t oldlen=0; /* old indirect size */
2820 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2821 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002822 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002824 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002826 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 /*
2829 * Check and set flags if this segment has a left neighbor
2830 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002831 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002832 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002833 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002834
2835 if (isnullstartblock(left.br_startblock))
2836 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 /*
2840 * Check and set flags if the current (right) segment exists.
2841 * If it doesn't exist, we're converting the hole at end-of-file.
2842 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002843 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002844 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002845 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002846
2847 if (isnullstartblock(right.br_startblock))
2848 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 /*
2852 * Set contiguity flags on the left and right neighbors.
2853 * Don't let extents get too large, even if the pieces are contiguous.
2854 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002855 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2856 left.br_startoff + left.br_blockcount == new->br_startoff &&
2857 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2858 state |= BMAP_LEFT_CONTIG;
2859
2860 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2861 new->br_startoff + new->br_blockcount == right.br_startoff &&
2862 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2863 (!(state & BMAP_LEFT_CONTIG) ||
2864 (left.br_blockcount + new->br_blockcount +
2865 right.br_blockcount <= MAXEXTLEN)))
2866 state |= BMAP_RIGHT_CONTIG;
2867
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 /*
2869 * Switch out based on the contiguity flags.
2870 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002871 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2872 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 /*
2874 * New allocation is contiguous with delayed allocations
2875 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002876 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002878 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 temp = left.br_blockcount + new->br_blockcount +
2880 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002881
Christoph Hellwigec90c552011-05-23 08:52:53 +00002882 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2883 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002884 oldlen = startblockval(left.br_startblock) +
2885 startblockval(new->br_startblock) +
2886 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002888 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002889 nullstartblock((int)newlen));
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_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 break;
2894
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002895 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 /*
2897 * New allocation is contiguous with a delayed allocation
2898 * on the left.
2899 * Merge the new allocation with the left neighbor.
2900 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002901 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002903
2904 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2905 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002906 oldlen = startblockval(left.br_startblock) +
2907 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002909 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002910 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002911 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 break;
2913
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002914 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 /*
2916 * New allocation is contiguous with a delayed allocation
2917 * on the right.
2918 * Merge the new allocation with the right neighbor.
2919 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002920 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002922 oldlen = startblockval(new->br_startblock) +
2923 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002925 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2926 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002927 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002928 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 break;
2930
2931 case 0:
2932 /*
2933 * New allocation is not contiguous with another
2934 * delayed allocation.
2935 * Insert a new entry.
2936 */
2937 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002938 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 break;
2940 }
2941 if (oldlen != newlen) {
2942 ASSERT(oldlen > newlen);
Christoph Hellwig96540c72010-09-30 02:25:55 +00002943 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00002944 (int64_t)(oldlen - newlen), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 /*
2946 * Nothing to do for disk quota accounting here.
2947 */
2948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
2951/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002952 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 */
2954STATIC int /* error */
2955xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002956 struct xfs_bmalloca *bma,
2957 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002959 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 int i; /* temp state */
2962 xfs_ifork_t *ifp; /* inode fork pointer */
2963 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2964 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002965 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967
Christoph Hellwigc6534242011-09-18 20:41:05 +00002968 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002969
Christoph Hellwigc6534242011-09-18 20:41:05 +00002970 ASSERT(bma->idx >= 0);
2971 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002972 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002973 ASSERT(!bma->cur ||
2974 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002975
2976 XFS_STATS_INC(xs_add_exlist);
2977
2978 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002979 if (whichfork == XFS_ATTR_FORK)
2980 state |= BMAP_ATTRFORK;
2981
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 /*
2983 * Check and set flags if this segment has a left neighbor.
2984 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002985 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002986 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002987 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002988 if (isnullstartblock(left.br_startblock))
2989 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002991
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 /*
2993 * Check and set flags if this segment has a current value.
2994 * Not true if we're inserting into the "hole" at eof.
2995 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002996 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002997 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002998 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002999 if (isnullstartblock(right.br_startblock))
3000 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003002
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 /*
3004 * We're inserting a real allocation between "left" and "right".
3005 * Set the contiguity flags. Don't let extents get too large.
3006 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003007 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
3008 left.br_startoff + left.br_blockcount == new->br_startoff &&
3009 left.br_startblock + left.br_blockcount == new->br_startblock &&
3010 left.br_state == new->br_state &&
3011 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
3012 state |= BMAP_LEFT_CONTIG;
3013
3014 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
3015 new->br_startoff + new->br_blockcount == right.br_startoff &&
3016 new->br_startblock + new->br_blockcount == right.br_startblock &&
3017 new->br_state == right.br_state &&
3018 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3019 (!(state & BMAP_LEFT_CONTIG) ||
3020 left.br_blockcount + new->br_blockcount +
3021 right.br_blockcount <= MAXEXTLEN))
3022 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003024 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 /*
3026 * Select which case we're in here, and implement it.
3027 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003028 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3029 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 /*
3031 * New allocation is contiguous with real allocations on the
3032 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003033 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003035 --bma->idx;
3036 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3037 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 left.br_blockcount + new->br_blockcount +
3039 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003040 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003041
Christoph Hellwigc6534242011-09-18 20:41:05 +00003042 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003043
Christoph Hellwigc6534242011-09-18 20:41:05 +00003044 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3045 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3046 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003047 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003048 } else {
3049 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003050 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3051 right.br_startblock, right.br_blockcount,
3052 &i);
3053 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003054 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003055 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003056 error = xfs_btree_delete(bma->cur, &i);
3057 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003058 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003059 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003060 error = xfs_btree_decrement(bma->cur, 0, &i);
3061 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003062 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003063 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003064 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003065 left.br_startblock,
3066 left.br_blockcount +
3067 new->br_blockcount +
3068 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003069 left.br_state);
3070 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003071 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003073 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003075 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 /*
3077 * New allocation is contiguous with a real allocation
3078 * on the left.
3079 * Merge the new allocation with the left neighbor.
3080 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003081 --bma->idx;
3082 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3083 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003085 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003086
Christoph Hellwigc6534242011-09-18 20:41:05 +00003087 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003088 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003089 } else {
3090 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003091 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3092 left.br_startblock, left.br_blockcount,
3093 &i);
3094 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003095 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003096 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003097 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003098 left.br_startblock,
3099 left.br_blockcount +
3100 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003101 left.br_state);
3102 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003103 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003105 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003107 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 /*
3109 * New allocation is contiguous with a real allocation
3110 * on the right.
3111 * Merge the new allocation with the right neighbor.
3112 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003113 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3114 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003115 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 new->br_blockcount + right.br_blockcount,
3117 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003118 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003119
Christoph Hellwigc6534242011-09-18 20:41:05 +00003120 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003121 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003122 } else {
3123 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003124 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003125 right.br_startoff,
3126 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003127 right.br_blockcount, &i);
3128 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003129 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003130 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003131 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003132 new->br_startblock,
3133 new->br_blockcount +
3134 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003135 right.br_state);
3136 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003137 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003139 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140
3141 case 0:
3142 /*
3143 * New allocation is not contiguous with another
3144 * real allocation.
3145 * Insert a new entry.
3146 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003147 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3148 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3149 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3150 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003151 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003152 } else {
3153 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003154 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003155 new->br_startoff,
3156 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003157 new->br_blockcount, &i);
3158 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003159 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003160 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003161 bma->cur->bc_rec.b.br_state = new->br_state;
3162 error = xfs_btree_insert(bma->cur, &i);
3163 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003164 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003165 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003167 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003169
3170 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003171 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003172 int tmp_logflags; /* partial log flag return val */
3173
Christoph Hellwigc6534242011-09-18 20:41:05 +00003174 ASSERT(bma->cur == NULL);
3175 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3176 bma->firstblock, bma->flist, &bma->cur,
3177 0, &tmp_logflags, whichfork);
3178 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003179 if (error)
3180 goto done;
3181 }
3182
3183 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003184 if (bma->cur)
3185 bma->cur->bc_private.b.allocated = 0;
3186
3187 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003188done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003189 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003190 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191}
3192
Nathan Scottdd9f4382006-01-11 15:28:28 +11003193/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003194 * Functions used in the extent read, allocate and remove paths
3195 */
3196
3197/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003198 * Adjust the size of the new extent based on di_extsize and rt extsize.
3199 */
Dave Chinner68988112013-08-12 20:49:42 +10003200int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003201xfs_bmap_extsize_align(
3202 xfs_mount_t *mp,
3203 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3204 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3205 xfs_extlen_t extsz, /* align to this extent size */
3206 int rt, /* is this a realtime inode? */
3207 int eof, /* is extent at end-of-file? */
3208 int delay, /* creating delalloc extent? */
3209 int convert, /* overwriting unwritten extent? */
3210 xfs_fileoff_t *offp, /* in/out: aligned offset */
3211 xfs_extlen_t *lenp) /* in/out: aligned length */
3212{
3213 xfs_fileoff_t orig_off; /* original offset */
3214 xfs_extlen_t orig_alen; /* original length */
3215 xfs_fileoff_t orig_end; /* original off+len */
3216 xfs_fileoff_t nexto; /* next file offset */
3217 xfs_fileoff_t prevo; /* previous file offset */
3218 xfs_fileoff_t align_off; /* temp for offset */
3219 xfs_extlen_t align_alen; /* temp for length */
3220 xfs_extlen_t temp; /* temp for calculations */
3221
3222 if (convert)
3223 return 0;
3224
3225 orig_off = align_off = *offp;
3226 orig_alen = align_alen = *lenp;
3227 orig_end = orig_off + orig_alen;
3228
3229 /*
3230 * If this request overlaps an existing extent, then don't
3231 * attempt to perform any additional alignment.
3232 */
3233 if (!delay && !eof &&
3234 (orig_off >= gotp->br_startoff) &&
3235 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3236 return 0;
3237 }
3238
3239 /*
3240 * If the file offset is unaligned vs. the extent size
3241 * we need to align it. This will be possible unless
3242 * the file was previously written with a kernel that didn't
3243 * perform this alignment, or if a truncate shot us in the
3244 * foot.
3245 */
3246 temp = do_mod(orig_off, extsz);
3247 if (temp) {
3248 align_alen += temp;
3249 align_off -= temp;
3250 }
3251 /*
3252 * Same adjustment for the end of the requested area.
3253 */
3254 if ((temp = (align_alen % extsz))) {
3255 align_alen += extsz - temp;
3256 }
3257 /*
3258 * If the previous block overlaps with this proposed allocation
3259 * then move the start forward without adjusting the length.
3260 */
3261 if (prevp->br_startoff != NULLFILEOFF) {
3262 if (prevp->br_startblock == HOLESTARTBLOCK)
3263 prevo = prevp->br_startoff;
3264 else
3265 prevo = prevp->br_startoff + prevp->br_blockcount;
3266 } else
3267 prevo = 0;
3268 if (align_off != orig_off && align_off < prevo)
3269 align_off = prevo;
3270 /*
3271 * If the next block overlaps with this proposed allocation
3272 * then move the start back without adjusting the length,
3273 * but not before offset 0.
3274 * This may of course make the start overlap previous block,
3275 * and if we hit the offset 0 limit then the next block
3276 * can still overlap too.
3277 */
3278 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3279 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3280 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3281 nexto = gotp->br_startoff + gotp->br_blockcount;
3282 else
3283 nexto = gotp->br_startoff;
3284 } else
3285 nexto = NULLFILEOFF;
3286 if (!eof &&
3287 align_off + align_alen != orig_end &&
3288 align_off + align_alen > nexto)
3289 align_off = nexto > align_alen ? nexto - align_alen : 0;
3290 /*
3291 * If we're now overlapping the next or previous extent that
3292 * means we can't fit an extsz piece in this hole. Just move
3293 * the start forward to the first valid spot and set
3294 * the length so we hit the end.
3295 */
3296 if (align_off != orig_off && align_off < prevo)
3297 align_off = prevo;
3298 if (align_off + align_alen != orig_end &&
3299 align_off + align_alen > nexto &&
3300 nexto != NULLFILEOFF) {
3301 ASSERT(nexto > prevo);
3302 align_alen = nexto - align_off;
3303 }
3304
3305 /*
3306 * If realtime, and the result isn't a multiple of the realtime
3307 * extent size we need to remove blocks until it is.
3308 */
3309 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3310 /*
3311 * We're not covering the original request, or
3312 * we won't be able to once we fix the length.
3313 */
3314 if (orig_off < align_off ||
3315 orig_end > align_off + align_alen ||
3316 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003317 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003318 /*
3319 * Try to fix it by moving the start up.
3320 */
3321 if (align_off + temp <= orig_off) {
3322 align_alen -= temp;
3323 align_off += temp;
3324 }
3325 /*
3326 * Try to fix it by moving the end in.
3327 */
3328 else if (align_off + align_alen - temp >= orig_end)
3329 align_alen -= temp;
3330 /*
3331 * Set the start to the minimum then trim the length.
3332 */
3333 else {
3334 align_alen -= orig_off - align_off;
3335 align_off = orig_off;
3336 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3337 }
3338 /*
3339 * Result doesn't cover the request, fail it.
3340 */
3341 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003342 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003343 } else {
3344 ASSERT(orig_off >= align_off);
3345 ASSERT(orig_end <= align_off + align_alen);
3346 }
3347
3348#ifdef DEBUG
3349 if (!eof && gotp->br_startoff != NULLFILEOFF)
3350 ASSERT(align_off + align_alen <= gotp->br_startoff);
3351 if (prevp->br_startoff != NULLFILEOFF)
3352 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3353#endif
3354
3355 *lenp = align_alen;
3356 *offp = align_off;
3357 return 0;
3358}
3359
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360#define XFS_ALLOC_GAP_UNITS 4
3361
Dave Chinner68988112013-08-12 20:49:42 +10003362void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003363xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003364 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3368 xfs_mount_t *mp; /* mount point structure */
3369 int nullfb; /* true if ap->firstblock isn't set */
3370 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
3372#define ISVALID(x,y) \
3373 (rt ? \
3374 (x) < mp->m_sb.sb_rblocks : \
3375 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3376 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3377 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3378
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003380 nullfb = *ap->firstblock == NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003382 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 /*
3384 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003385 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003387 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3388 !isnullstartblock(ap->prev.br_startblock) &&
3389 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3390 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003391 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 /*
3393 * Adjust for the gap between prevp and us.
3394 */
Dave Chinner3a756672011-09-18 20:40:58 +00003395 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003396 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003398 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3399 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 }
3401 /*
3402 * If not at eof, then compare the two neighbor blocks.
3403 * Figure out whether either one gives us a good starting point,
3404 * and pick the better one.
3405 */
3406 else if (!ap->eof) {
3407 xfs_fsblock_t gotbno; /* right side block number */
3408 xfs_fsblock_t gotdiff=0; /* right side difference */
3409 xfs_fsblock_t prevbno; /* left side block number */
3410 xfs_fsblock_t prevdiff=0; /* left side difference */
3411
3412 /*
3413 * If there's a previous (left) block, select a requested
3414 * start block based on it.
3415 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003416 if (ap->prev.br_startoff != NULLFILEOFF &&
3417 !isnullstartblock(ap->prev.br_startblock) &&
3418 (prevbno = ap->prev.br_startblock +
3419 ap->prev.br_blockcount) &&
3420 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 /*
3422 * Calculate gap to end of previous block.
3423 */
Dave Chinner3a756672011-09-18 20:40:58 +00003424 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003425 (ap->prev.br_startoff +
3426 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427 /*
3428 * Figure the startblock based on the previous block's
3429 * end and the gap size.
3430 * Heuristic!
3431 * If the gap is large relative to the piece we're
3432 * allocating, or using it gives us an invalid block
3433 * number, then just use the end of the previous block.
3434 */
Dave Chinner3a756672011-09-18 20:40:58 +00003435 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003437 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 prevbno += adjust;
3439 else
3440 prevdiff += adjust;
3441 /*
3442 * If the firstblock forbids it, can't use it,
3443 * must use default.
3444 */
3445 if (!rt && !nullfb &&
3446 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3447 prevbno = NULLFSBLOCK;
3448 }
3449 /*
3450 * No previous block or can't follow it, just default.
3451 */
3452 else
3453 prevbno = NULLFSBLOCK;
3454 /*
3455 * If there's a following (right) block, select a requested
3456 * start block based on it.
3457 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003458 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 /*
3460 * Calculate gap to start of next block.
3461 */
Dave Chinner3a756672011-09-18 20:40:58 +00003462 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 /*
3464 * Figure the startblock based on the next block's
3465 * start and the gap size.
3466 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003467 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 /*
3469 * Heuristic!
3470 * If the gap is large relative to the piece we're
3471 * allocating, or using it gives us an invalid block
3472 * number, then just use the start of the next block
3473 * offset by our length.
3474 */
Dave Chinner3a756672011-09-18 20:40:58 +00003475 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 ISVALID(gotbno - gotdiff, gotbno))
3477 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003478 else if (ISVALID(gotbno - ap->length, gotbno)) {
3479 gotbno -= ap->length;
3480 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 } else
3482 gotdiff += adjust;
3483 /*
3484 * If the firstblock forbids it, can't use it,
3485 * must use default.
3486 */
3487 if (!rt && !nullfb &&
3488 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3489 gotbno = NULLFSBLOCK;
3490 }
3491 /*
3492 * No next block, just default.
3493 */
3494 else
3495 gotbno = NULLFSBLOCK;
3496 /*
3497 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003498 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 */
3500 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003501 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003503 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003505 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003507#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003508}
3509
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003510static int
3511xfs_bmap_longest_free_extent(
3512 struct xfs_trans *tp,
3513 xfs_agnumber_t ag,
3514 xfs_extlen_t *blen,
3515 int *notinit)
3516{
3517 struct xfs_mount *mp = tp->t_mountp;
3518 struct xfs_perag *pag;
3519 xfs_extlen_t longest;
3520 int error = 0;
3521
3522 pag = xfs_perag_get(mp, ag);
3523 if (!pag->pagf_init) {
3524 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3525 if (error)
3526 goto out;
3527
3528 if (!pag->pagf_init) {
3529 *notinit = 1;
3530 goto out;
3531 }
3532 }
3533
3534 longest = xfs_alloc_longest_free_extent(mp, pag);
3535 if (*blen < longest)
3536 *blen = longest;
3537
3538out:
3539 xfs_perag_put(pag);
3540 return error;
3541}
3542
3543static void
3544xfs_bmap_select_minlen(
3545 struct xfs_bmalloca *ap,
3546 struct xfs_alloc_arg *args,
3547 xfs_extlen_t *blen,
3548 int notinit)
3549{
3550 if (notinit || *blen < ap->minlen) {
3551 /*
3552 * Since we did a BUF_TRYLOCK above, it is possible that
3553 * there is space for this request.
3554 */
3555 args->minlen = ap->minlen;
3556 } else if (*blen < args->maxlen) {
3557 /*
3558 * If the best seen length is less than the request length,
3559 * use the best as the minimum.
3560 */
3561 args->minlen = *blen;
3562 } else {
3563 /*
3564 * Otherwise we've seen an extent as big as maxlen, use that
3565 * as the minimum.
3566 */
3567 args->minlen = args->maxlen;
3568 }
3569}
3570
Nathan Scotta365bdd2006-03-14 13:34:16 +11003571STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003572xfs_bmap_btalloc_nullfb(
3573 struct xfs_bmalloca *ap,
3574 struct xfs_alloc_arg *args,
3575 xfs_extlen_t *blen)
3576{
3577 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003578 xfs_agnumber_t ag, startag;
3579 int notinit = 0;
3580 int error;
3581
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003582 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003583 args->total = ap->total;
3584
Christoph Hellwigc467c042010-02-15 23:34:42 +00003585 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3586 if (startag == NULLAGNUMBER)
3587 startag = ag = 0;
3588
Dave Chinner14b064c2011-01-27 12:16:28 +11003589 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003590 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3591 &notinit);
3592 if (error)
3593 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003594
Christoph Hellwigc467c042010-02-15 23:34:42 +00003595 if (++ag == mp->m_sb.sb_agcount)
3596 ag = 0;
3597 if (ag == startag)
3598 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003599 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003600
3601 xfs_bmap_select_minlen(ap, args, blen, notinit);
3602 return 0;
3603}
3604
3605STATIC int
3606xfs_bmap_btalloc_filestreams(
3607 struct xfs_bmalloca *ap,
3608 struct xfs_alloc_arg *args,
3609 xfs_extlen_t *blen)
3610{
3611 struct xfs_mount *mp = ap->ip->i_mount;
3612 xfs_agnumber_t ag;
3613 int notinit = 0;
3614 int error;
3615
3616 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3617 args->total = ap->total;
3618
3619 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3620 if (ag == NULLAGNUMBER)
3621 ag = 0;
3622
3623 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3624 if (error)
3625 return error;
3626
3627 if (*blen < args->maxlen) {
3628 error = xfs_filestream_new_ag(ap, &ag);
3629 if (error)
3630 return error;
3631
3632 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3633 &notinit);
3634 if (error)
3635 return error;
3636
3637 }
3638
3639 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003640
3641 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003642 * Set the failure fallback case to look in the selected AG as stream
3643 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003644 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003645 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003646 return 0;
3647}
3648
3649STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003650xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003651 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003652{
3653 xfs_mount_t *mp; /* mount point structure */
3654 xfs_alloctype_t atype = 0; /* type for allocation routines */
3655 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003656 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003657 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003658 xfs_alloc_arg_t args;
3659 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003660 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003661 int nullfb; /* true if ap->firstblock isn't set */
3662 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003663 int tryagain;
3664 int error;
Dave Chinner33177f02013-12-12 16:34:36 +11003665 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003666
Dave Chinnera99ebf42011-12-01 11:24:20 +00003667 ASSERT(ap->length);
3668
Nathan Scotta365bdd2006-03-14 13:34:16 +11003669 mp = ap->ip->i_mount;
Dave Chinner33177f02013-12-12 16:34:36 +11003670
3671 /* stripe alignment for allocation is determined by mount parameters */
3672 stripe_align = 0;
3673 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3674 stripe_align = mp->m_swidth;
3675 else if (mp->m_dalign)
3676 stripe_align = mp->m_dalign;
3677
David Chinner957d0eb2007-06-18 16:50:37 +10003678 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003679 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003680 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003681 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003682 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003683 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003684 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003685 }
Dave Chinner33177f02013-12-12 16:34:36 +11003686
3687
Dave Chinner0937e0f2011-09-18 20:40:57 +00003688 nullfb = *ap->firstblock == NULLFSBLOCK;
3689 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003690 if (nullfb) {
3691 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
3692 ag = xfs_filestream_lookup_ag(ap->ip);
3693 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003694 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003695 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003696 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003697 }
3698 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003699 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003700
3701 xfs_bmap_adjacent(ap);
3702
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003704 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 * it's in the right allocation group.
3706 */
Dave Chinner3a756672011-09-18 20:40:58 +00003707 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 ;
3709 else
Dave Chinner3a756672011-09-18 20:40:58 +00003710 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 * Normal allocation, done through xfs_alloc_vextent.
3713 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003714 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003715 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003716 args.tp = ap->tp;
3717 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003718 args.fsbno = ap->blkno;
Dave Chinner14b064c2011-01-27 12:16:28 +11003719
3720 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner3a756672011-09-18 20:40:58 +00003721 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003722 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003723 blen = 0;
3724 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003725 /*
3726 * Search for an allocation group with a single extent large
3727 * enough for the request. If one isn't found, then adjust
3728 * the minimum allocation size to the largest space found.
3729 */
3730 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3731 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3732 else
3733 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003734 if (error)
3735 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003736 } else if (ap->flist->xbf_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003737 if (xfs_inode_is_filestream(ap->ip))
3738 args.type = XFS_ALLOCTYPE_FIRST_AG;
3739 else
3740 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003741 args.total = args.minlen = ap->minlen;
3742 } else {
3743 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3744 args.total = ap->total;
3745 args.minlen = ap->minlen;
3746 }
David Chinner957d0eb2007-06-18 16:50:37 +10003747 /* apply extent size hints if obtained earlier */
3748 if (unlikely(align)) {
3749 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003750 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003751 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11003752 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003753 args.prod = 1;
3754 args.mod = 0;
3755 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003756 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003757 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003758 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3759 }
3760 /*
3761 * If we are not low on available data blocks, and the
3762 * underlying logical volume manager is a stripe, and
3763 * the file offset is zero then try to allocate data
3764 * blocks on stripe unit boundary.
3765 * NOTE: ap->aeof is only set if the allocation length
3766 * is >= the stripe unit and the allocation offset is
3767 * at the end of file.
3768 */
Dave Chinner0937e0f2011-09-18 20:40:57 +00003769 if (!ap->flist->xbf_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003770 if (!ap->offset) {
Dave Chinner33177f02013-12-12 16:34:36 +11003771 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003772 atype = args.type;
3773 isaligned = 1;
3774 /*
3775 * Adjust for alignment
3776 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003777 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003778 args.minlen = blen - args.alignment;
3779 args.minalignslop = 0;
3780 } else {
3781 /*
3782 * First try an exact bno allocation.
3783 * If it fails then do a near or start bno
3784 * allocation with alignment turned on.
3785 */
3786 atype = args.type;
3787 tryagain = 1;
3788 args.type = XFS_ALLOCTYPE_THIS_BNO;
3789 args.alignment = 1;
3790 /*
3791 * Compute the minlen+alignment for the
3792 * next case. Set slop so that the value
3793 * of minlen+alignment+slop doesn't go up
3794 * between the calls.
3795 */
Dave Chinner33177f02013-12-12 16:34:36 +11003796 if (blen > stripe_align && blen <= args.maxlen)
3797 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003798 else
3799 nextminlen = args.minlen;
Dave Chinner33177f02013-12-12 16:34:36 +11003800 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003801 args.minalignslop =
Dave Chinner33177f02013-12-12 16:34:36 +11003802 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003803 args.minlen - 1;
3804 else
3805 args.minalignslop = 0;
3806 }
3807 } else {
3808 args.alignment = 1;
3809 args.minalignslop = 0;
3810 }
3811 args.minleft = ap->minleft;
3812 args.wasdel = ap->wasdel;
3813 args.isfl = 0;
3814 args.userdata = ap->userdata;
3815 if ((error = xfs_alloc_vextent(&args)))
3816 return error;
3817 if (tryagain && args.fsbno == NULLFSBLOCK) {
3818 /*
3819 * Exact allocation failed. Now try with alignment
3820 * turned on.
3821 */
3822 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003823 args.fsbno = ap->blkno;
Dave Chinner33177f02013-12-12 16:34:36 +11003824 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003825 args.minlen = nextminlen;
3826 args.minalignslop = 0;
3827 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 if ((error = xfs_alloc_vextent(&args)))
3829 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003830 }
3831 if (isaligned && args.fsbno == NULLFSBLOCK) {
3832 /*
3833 * allocation failed, so turn off alignment and
3834 * try again.
3835 */
3836 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003837 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003838 args.alignment = 0;
3839 if ((error = xfs_alloc_vextent(&args)))
3840 return error;
3841 }
3842 if (args.fsbno == NULLFSBLOCK && nullfb &&
3843 args.minlen > ap->minlen) {
3844 args.minlen = ap->minlen;
3845 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003846 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003847 if ((error = xfs_alloc_vextent(&args)))
3848 return error;
3849 }
3850 if (args.fsbno == NULLFSBLOCK && nullfb) {
3851 args.fsbno = 0;
3852 args.type = XFS_ALLOCTYPE_FIRST_AG;
3853 args.total = ap->minlen;
3854 args.minleft = 0;
3855 if ((error = xfs_alloc_vextent(&args)))
3856 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003857 ap->flist->xbf_low = 1;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003858 }
3859 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003860 /*
3861 * check the allocation happened at the same or higher AG than
3862 * the first block that was allocated.
3863 */
3864 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3865 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3866 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
3867 (ap->flist->xbf_low &&
3868 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3869 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3870
Dave Chinner3a756672011-09-18 20:40:58 +00003871 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003872 if (*ap->firstblock == NULLFSBLOCK)
3873 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003874 ASSERT(nullfb || fb_agno == args.agno ||
Dave Chinner0937e0f2011-09-18 20:40:57 +00003875 (ap->flist->xbf_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003876 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003877 ap->ip->i_d.di_nblocks += args.len;
3878 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3879 if (ap->wasdel)
3880 ap->ip->i_delayed_blks -= args.len;
3881 /*
3882 * Adjust the disk quota also. This was reserved
3883 * earlier.
3884 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003885 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003886 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3887 XFS_TRANS_DQ_BCOUNT,
3888 (long) args.len);
3889 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003890 ap->blkno = NULLFSBLOCK;
3891 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 }
3893 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003894}
3895
3896/*
3897 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3898 * It figures out where to ask the underlying allocator to put the new extent.
3899 */
3900STATIC int
3901xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003902 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003903{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003904 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003905 return xfs_bmap_rtalloc(ap);
3906 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907}
3908
3909/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003910 * Trim the returned map to the required bounds
3911 */
3912STATIC void
3913xfs_bmapi_trim_map(
3914 struct xfs_bmbt_irec *mval,
3915 struct xfs_bmbt_irec *got,
3916 xfs_fileoff_t *bno,
3917 xfs_filblks_t len,
3918 xfs_fileoff_t obno,
3919 xfs_fileoff_t end,
3920 int n,
3921 int flags)
3922{
3923 if ((flags & XFS_BMAPI_ENTIRE) ||
3924 got->br_startoff + got->br_blockcount <= obno) {
3925 *mval = *got;
3926 if (isnullstartblock(got->br_startblock))
3927 mval->br_startblock = DELAYSTARTBLOCK;
3928 return;
3929 }
3930
3931 if (obno > *bno)
3932 *bno = obno;
3933 ASSERT((*bno >= obno) || (n == 0));
3934 ASSERT(*bno < end);
3935 mval->br_startoff = *bno;
3936 if (isnullstartblock(got->br_startblock))
3937 mval->br_startblock = DELAYSTARTBLOCK;
3938 else
3939 mval->br_startblock = got->br_startblock +
3940 (*bno - got->br_startoff);
3941 /*
3942 * Return the minimum of what we got and what we asked for for
3943 * the length. We can use the len variable here because it is
3944 * modified below and we could have been there before coming
3945 * here if the first part of the allocation didn't overlap what
3946 * was asked for.
3947 */
3948 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3949 got->br_blockcount - (*bno - got->br_startoff));
3950 mval->br_state = got->br_state;
3951 ASSERT(mval->br_blockcount <= len);
3952 return;
3953}
3954
3955/*
3956 * Update and validate the extent map to return
3957 */
3958STATIC void
3959xfs_bmapi_update_map(
3960 struct xfs_bmbt_irec **map,
3961 xfs_fileoff_t *bno,
3962 xfs_filblks_t *len,
3963 xfs_fileoff_t obno,
3964 xfs_fileoff_t end,
3965 int *n,
3966 int flags)
3967{
3968 xfs_bmbt_irec_t *mval = *map;
3969
3970 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3971 ((mval->br_startoff + mval->br_blockcount) <= end));
3972 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3973 (mval->br_startoff < obno));
3974
3975 *bno = mval->br_startoff + mval->br_blockcount;
3976 *len = end - *bno;
3977 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3978 /* update previous map with new information */
3979 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3980 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3981 ASSERT(mval->br_state == mval[-1].br_state);
3982 mval[-1].br_blockcount = mval->br_blockcount;
3983 mval[-1].br_state = mval->br_state;
3984 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3985 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3986 mval[-1].br_startblock != HOLESTARTBLOCK &&
3987 mval->br_startblock == mval[-1].br_startblock +
3988 mval[-1].br_blockcount &&
3989 ((flags & XFS_BMAPI_IGSTATE) ||
3990 mval[-1].br_state == mval->br_state)) {
3991 ASSERT(mval->br_startoff ==
3992 mval[-1].br_startoff + mval[-1].br_blockcount);
3993 mval[-1].br_blockcount += mval->br_blockcount;
3994 } else if (*n > 0 &&
3995 mval->br_startblock == DELAYSTARTBLOCK &&
3996 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3997 mval->br_startoff ==
3998 mval[-1].br_startoff + mval[-1].br_blockcount) {
3999 mval[-1].br_blockcount += mval->br_blockcount;
4000 mval[-1].br_state = mval->br_state;
4001 } else if (!((*n == 0) &&
4002 ((mval->br_startoff + mval->br_blockcount) <=
4003 obno))) {
4004 mval++;
4005 (*n)++;
4006 }
4007 *map = mval;
4008}
4009
4010/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004011 * Map file blocks to filesystem blocks without allocation.
4012 */
4013int
4014xfs_bmapi_read(
4015 struct xfs_inode *ip,
4016 xfs_fileoff_t bno,
4017 xfs_filblks_t len,
4018 struct xfs_bmbt_irec *mval,
4019 int *nmap,
4020 int flags)
4021{
4022 struct xfs_mount *mp = ip->i_mount;
4023 struct xfs_ifork *ifp;
4024 struct xfs_bmbt_irec got;
4025 struct xfs_bmbt_irec prev;
4026 xfs_fileoff_t obno;
4027 xfs_fileoff_t end;
4028 xfs_extnum_t lastx;
4029 int error;
4030 int eof;
4031 int n = 0;
4032 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4033 XFS_ATTR_FORK : XFS_DATA_FORK;
4034
4035 ASSERT(*nmap >= 1);
4036 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4037 XFS_BMAPI_IGSTATE)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004038 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004039
4040 if (unlikely(XFS_TEST_ERROR(
4041 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4042 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4043 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4044 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004045 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004046 }
4047
4048 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004049 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004050
4051 XFS_STATS_INC(xs_blk_mapr);
4052
4053 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004054
4055 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4056 error = xfs_iread_extents(NULL, ip, whichfork);
4057 if (error)
4058 return error;
4059 }
4060
4061 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4062 end = bno + len;
4063 obno = bno;
4064
4065 while (bno < end && n < *nmap) {
4066 /* Reading past eof, act as though there's a hole up to end. */
4067 if (eof)
4068 got.br_startoff = end;
4069 if (got.br_startoff > bno) {
4070 /* Reading in a hole. */
4071 mval->br_startoff = bno;
4072 mval->br_startblock = HOLESTARTBLOCK;
4073 mval->br_blockcount =
4074 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4075 mval->br_state = XFS_EXT_NORM;
4076 bno += mval->br_blockcount;
4077 len -= mval->br_blockcount;
4078 mval++;
4079 n++;
4080 continue;
4081 }
4082
4083 /* set up the extent map to return. */
4084 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4085 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4086
4087 /* If we're done, stop now. */
4088 if (bno >= end || n >= *nmap)
4089 break;
4090
4091 /* Else go on to the next record. */
4092 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4093 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4094 else
4095 eof = 1;
4096 }
4097 *nmap = n;
4098 return 0;
4099}
4100
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004101STATIC int
4102xfs_bmapi_reserve_delalloc(
4103 struct xfs_inode *ip,
4104 xfs_fileoff_t aoff,
4105 xfs_filblks_t len,
4106 struct xfs_bmbt_irec *got,
4107 struct xfs_bmbt_irec *prev,
4108 xfs_extnum_t *lastx,
4109 int eof)
4110{
4111 struct xfs_mount *mp = ip->i_mount;
4112 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4113 xfs_extlen_t alen;
4114 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004115 char rt = XFS_IS_REALTIME_INODE(ip);
4116 xfs_extlen_t extsz;
4117 int error;
4118
4119 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4120 if (!eof)
4121 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4122
4123 /* Figure out the extent size, adjust alen */
4124 extsz = xfs_get_extsz_hint(ip);
4125 if (extsz) {
4126 /*
4127 * Make sure we don't exceed a single extent length when we
4128 * align the extent by reducing length we are going to
4129 * allocate by the maximum amount extent size aligment may
4130 * require.
4131 */
4132 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4133 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4134 1, 0, &aoff, &alen);
4135 ASSERT(!error);
4136 }
4137
4138 if (rt)
4139 extsz = alen / mp->m_sb.sb_rextsize;
4140
4141 /*
4142 * Make a transaction-less quota reservation for delayed allocation
4143 * blocks. This number gets adjusted later. We return if we haven't
4144 * allocated blocks already inside this loop.
4145 */
4146 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4147 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4148 if (error)
4149 return error;
4150
4151 /*
4152 * Split changing sb for alen and indlen since they could be coming
4153 * from different places.
4154 */
4155 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4156 ASSERT(indlen > 0);
4157
4158 if (rt) {
4159 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4160 -((int64_t)extsz), 0);
4161 } else {
4162 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4163 -((int64_t)alen), 0);
4164 }
4165
4166 if (error)
4167 goto out_unreserve_quota;
4168
4169 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4170 -((int64_t)indlen), 0);
4171 if (error)
4172 goto out_unreserve_blocks;
4173
4174
4175 ip->i_delayed_blks += alen;
4176
4177 got->br_startoff = aoff;
4178 got->br_startblock = nullstartblock(indlen);
4179 got->br_blockcount = alen;
4180 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004181 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004182
4183 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004184 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4185 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004186 */
4187 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4188
4189 ASSERT(got->br_startoff <= aoff);
4190 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4191 ASSERT(isnullstartblock(got->br_startblock));
4192 ASSERT(got->br_state == XFS_EXT_NORM);
4193 return 0;
4194
4195out_unreserve_blocks:
4196 if (rt)
4197 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4198 else
4199 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4200out_unreserve_quota:
4201 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004202 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004203 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4204 return error;
4205}
4206
Dave Chinner5c8ed202011-09-18 20:40:45 +00004207/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004208 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4209 */
4210int
4211xfs_bmapi_delay(
4212 struct xfs_inode *ip, /* incore inode */
4213 xfs_fileoff_t bno, /* starting file offs. mapped */
4214 xfs_filblks_t len, /* length to map in file */
4215 struct xfs_bmbt_irec *mval, /* output: map values */
4216 int *nmap, /* i/o: mval size/count */
4217 int flags) /* XFS_BMAPI_... */
4218{
4219 struct xfs_mount *mp = ip->i_mount;
4220 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4221 struct xfs_bmbt_irec got; /* current file extent record */
4222 struct xfs_bmbt_irec prev; /* previous file extent record */
4223 xfs_fileoff_t obno; /* old block number (offset) */
4224 xfs_fileoff_t end; /* end of mapped file region */
4225 xfs_extnum_t lastx; /* last useful extent number */
4226 int eof; /* we've hit the end of extents */
4227 int n = 0; /* current extent index */
4228 int error = 0;
4229
4230 ASSERT(*nmap >= 1);
4231 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4232 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004233 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig44032802011-09-18 20:40:48 +00004234
4235 if (unlikely(XFS_TEST_ERROR(
4236 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4237 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4238 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4239 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004240 return -EFSCORRUPTED;
Christoph Hellwig44032802011-09-18 20:40:48 +00004241 }
4242
4243 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004244 return -EIO;
Christoph Hellwig44032802011-09-18 20:40:48 +00004245
4246 XFS_STATS_INC(xs_blk_mapw);
4247
4248 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4249 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4250 if (error)
4251 return error;
4252 }
4253
4254 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4255 end = bno + len;
4256 obno = bno;
4257
4258 while (bno < end && n < *nmap) {
4259 if (eof || got.br_startoff > bno) {
4260 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4261 &prev, &lastx, eof);
4262 if (error) {
4263 if (n == 0) {
4264 *nmap = 0;
4265 return error;
4266 }
4267 break;
4268 }
4269 }
4270
4271 /* set up the extent map to return. */
4272 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4273 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4274
4275 /* If we're done, stop now. */
4276 if (bno >= end || n >= *nmap)
4277 break;
4278
4279 /* Else go on to the next record. */
4280 prev = got;
4281 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4282 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4283 else
4284 eof = 1;
4285 }
4286
4287 *nmap = n;
4288 return 0;
4289}
4290
4291
Dave Chinnercf11da92014-07-15 07:08:24 +10004292static int
4293xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004294 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004295{
4296 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004297 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004298 XFS_ATTR_FORK : XFS_DATA_FORK;
4299 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004300 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004301 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004302
Dave Chinnera99ebf42011-12-01 11:24:20 +00004303 ASSERT(bma->length > 0);
4304
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004305 /*
4306 * For the wasdelay case, we could also just allocate the stuff asked
4307 * for in this bmap call but that wouldn't be as good.
4308 */
4309 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004310 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4311 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004312 if (bma->idx != NULLEXTNUM && bma->idx) {
4313 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004314 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004315 }
4316 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004317 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004318 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004319 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004320 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004321 }
4322
4323 /*
4324 * Indicate if this is the first user data in the file, or just any
4325 * user data.
4326 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004327 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004328 bma->userdata = (bma->offset == 0) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004329 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4330 }
4331
Dave Chinnere04426b2012-10-05 11:06:59 +10004332 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004333
4334 /*
4335 * Only want to do the alignment at the eof if it is userdata and
4336 * allocation length is larger than a stripe unit.
4337 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004338 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004339 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004340 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004341 if (error)
4342 return error;
4343 }
4344
4345 error = xfs_bmap_alloc(bma);
4346 if (error)
4347 return error;
4348
Dave Chinner0937e0f2011-09-18 20:40:57 +00004349 if (bma->flist->xbf_low)
4350 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004351 if (bma->cur)
4352 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004353 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004354 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004355 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4356 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4357 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4358 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004359 }
4360 /*
4361 * Bump the number of extents we've allocated
4362 * in this call.
4363 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004364 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004365
Dave Chinner29c8d172011-09-18 20:41:00 +00004366 if (bma->cur)
4367 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004368 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4369
Dave Chinner963c30c2011-09-18 20:40:59 +00004370 bma->got.br_startoff = bma->offset;
4371 bma->got.br_startblock = bma->blkno;
4372 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004373 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004374
4375 /*
4376 * A wasdelay extent has been initialized, so shouldn't be flagged
4377 * as unwritten.
4378 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004379 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004380 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004381 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004382
Christoph Hellwigc6534242011-09-18 20:41:05 +00004383 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004384 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004385 else
4386 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004387
Christoph Hellwigc315c902011-09-18 20:41:02 +00004388 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004389 if (error)
4390 return error;
4391
4392 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004393 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4394 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4395 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004396 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004397 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004398
Dave Chinner963c30c2011-09-18 20:40:59 +00004399 ASSERT(bma->got.br_startoff <= bma->offset);
4400 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4401 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004402 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4403 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004404 return 0;
4405}
4406
Dave Chinnerb447fe52011-09-18 20:40:51 +00004407STATIC int
4408xfs_bmapi_convert_unwritten(
4409 struct xfs_bmalloca *bma,
4410 struct xfs_bmbt_irec *mval,
4411 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004412 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004413{
4414 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4415 XFS_ATTR_FORK : XFS_DATA_FORK;
4416 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004417 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004418 int error;
4419
Dave Chinnerb447fe52011-09-18 20:40:51 +00004420 /* check if we need to do unwritten->real conversion */
4421 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4422 (flags & XFS_BMAPI_PREALLOC))
4423 return 0;
4424
4425 /* check if we need to do real->unwritten conversion */
4426 if (mval->br_state == XFS_EXT_NORM &&
4427 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4428 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4429 return 0;
4430
4431 /*
4432 * Modify (by adding) the state flag, if writing.
4433 */
4434 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004435 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4436 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004437 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004438 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4439 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004440 }
4441 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4442 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4443
Dave Chinnere0c3da52011-09-18 20:41:01 +00004444 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004445 &bma->cur, mval, bma->firstblock, bma->flist,
4446 &tmp_logflags);
4447 bma->logflags |= tmp_logflags;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004448 if (error)
4449 return error;
4450
4451 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004452 * Update our extent pointer, given that
4453 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4454 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004455 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004456 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004457
4458 /*
4459 * We may have combined previously unwritten space with written space,
4460 * so generate another request.
4461 */
4462 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004463 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004464 return 0;
4465}
4466
Christoph Hellwig44032802011-09-18 20:40:48 +00004467/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004468 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4469 * extent state if necessary. Details behaviour is controlled by the flags
4470 * parameter. Only allocates blocks from a single allocation group, to avoid
4471 * locking problems.
4472 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 * The returned value in "firstblock" from the first call in a transaction
4474 * must be remembered and presented to subsequent calls in "firstblock".
4475 * An upper bound for the number of blocks to be allocated is supplied to
4476 * the first call in "total"; if no allocation group has that many free
4477 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4478 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004479int
4480xfs_bmapi_write(
4481 struct xfs_trans *tp, /* transaction pointer */
4482 struct xfs_inode *ip, /* incore inode */
4483 xfs_fileoff_t bno, /* starting file offs. mapped */
4484 xfs_filblks_t len, /* length to map in file */
4485 int flags, /* XFS_BMAPI_... */
4486 xfs_fsblock_t *firstblock, /* first allocated block
4487 controls a.g. for allocs */
4488 xfs_extlen_t total, /* total blocks needed */
4489 struct xfs_bmbt_irec *mval, /* output: map values */
4490 int *nmap, /* i/o: mval size/count */
4491 struct xfs_bmap_free *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004493 struct xfs_mount *mp = ip->i_mount;
4494 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004495 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004496 xfs_fileoff_t end; /* end of mapped file region */
4497 int eof; /* after the end of extents */
4498 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004499 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004500 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004501 int whichfork; /* data or attr fork */
4502 char inhole; /* current location is hole in file */
4503 char wasdelay; /* old extent was delayed */
4504
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004506 xfs_fileoff_t orig_bno; /* original block number value */
4507 int orig_flags; /* original flags arg value */
4508 xfs_filblks_t orig_len; /* original value of len arg */
4509 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4510 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
4512 orig_bno = bno;
4513 orig_len = len;
4514 orig_flags = flags;
4515 orig_mval = mval;
4516 orig_nmap = *nmap;
4517#endif
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004518 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4519 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004522 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4523 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4524 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004525 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004526 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004527 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004528
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 if (unlikely(XFS_TEST_ERROR(
4530 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004531 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004533 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004534 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004536
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004538 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004539
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004541
4542 XFS_STATS_INC(xs_blk_mapw);
4543
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004544 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004546 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004548 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004549 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004550 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004551 }
4552
4553 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4554 error = xfs_iread_extents(tp, ip, whichfork);
4555 if (error)
4556 goto error0;
4557 }
4558
Dave Chinnere0c3da52011-09-18 20:41:01 +00004559 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004560 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 n = 0;
4562 end = bno + len;
4563 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004564
4565 bma.tp = tp;
4566 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004567 bma.total = total;
4568 bma.userdata = 0;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004569 bma.flist = flist;
4570 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004571
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004573 inhole = eof || bma.got.br_startoff > bno;
4574 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004575
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 /*
4577 * First, deal with the hole before the allocated space
4578 * that we found, if any.
4579 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004580 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004581 bma.eof = eof;
4582 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4583 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004584 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004585 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004586
Dave Chinnera99ebf42011-12-01 11:24:20 +00004587 /*
4588 * There's a 32/64 bit type mismatch between the
4589 * allocation length request (which can be 64 bits in
4590 * length) and the bma length request, which is
4591 * xfs_extlen_t and therefore 32 bits. Hence we have to
4592 * check for 32-bit overflows and handle them here.
4593 */
4594 if (len > (xfs_filblks_t)MAXEXTLEN)
4595 bma.length = MAXEXTLEN;
4596 else
4597 bma.length = len;
4598
4599 ASSERT(len > 0);
4600 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004601 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 if (error)
4603 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004604 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004605 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004607
Dave Chinneraef9a892011-09-18 20:40:44 +00004608 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004609 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4610 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611
Dave Chinnerb447fe52011-09-18 20:40:51 +00004612 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004613 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004614 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004615 continue;
4616 if (error)
4617 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618
Dave Chinneraef9a892011-09-18 20:40:44 +00004619 /* update the extent map to return */
4620 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 /*
4623 * If we're done, stop now. Stop when we've allocated
4624 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4625 * the transaction may get too big.
4626 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004627 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004629
4630 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004631 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004632 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4633 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4634 &bma.got);
4635 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00004636 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004639
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 /*
4641 * Transform from btree to extents, give it cur.
4642 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004643 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004644 int tmp_logflags = 0;
4645
Dave Chinner29c8d172011-09-18 20:41:00 +00004646 ASSERT(bma.cur);
4647 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004649 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 if (error)
4651 goto error0;
4652 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004655 XFS_IFORK_NEXTENTS(ip, whichfork) >
4656 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658error0:
4659 /*
4660 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004661 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004663 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004665 bma.logflags &= ~xfs_ilog_fext(whichfork);
4666 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004668 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 /*
4670 * Log whatever the flags say, even if error. Otherwise we might miss
4671 * detecting a case where the data is changed, there's an error,
4672 * and it's not logged so we don't shutdown when we should.
4673 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004674 if (bma.logflags)
4675 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004676
Dave Chinner29c8d172011-09-18 20:41:00 +00004677 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 if (!error) {
4679 ASSERT(*firstblock == NULLFSBLOCK ||
4680 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4681 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004682 bma.cur->bc_private.b.firstblock) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 (flist->xbf_low &&
4684 XFS_FSB_TO_AGNO(mp, *firstblock) <
4685 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004686 bma.cur->bc_private.b.firstblock)));
4687 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004689 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4691 }
4692 if (!error)
4693 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4694 orig_nmap, *nmap);
4695 return error;
4696}
4697
4698/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004699 * Called by xfs_bmapi to update file extent records and the btree
4700 * after removing space (or undoing a delayed allocation).
4701 */
4702STATIC int /* error */
4703xfs_bmap_del_extent(
4704 xfs_inode_t *ip, /* incore inode pointer */
4705 xfs_trans_t *tp, /* current transaction pointer */
4706 xfs_extnum_t *idx, /* extent number to update/delete */
4707 xfs_bmap_free_t *flist, /* list of extents to be freed */
4708 xfs_btree_cur_t *cur, /* if null, not a btree */
4709 xfs_bmbt_irec_t *del, /* data to remove from extents */
4710 int *logflagsp, /* inode logging flags */
4711 int whichfork) /* data or attr fork */
4712{
4713 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
4714 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
4715 xfs_fsblock_t del_endblock=0; /* first block past del */
4716 xfs_fileoff_t del_endoff; /* first offset past del */
4717 int delay; /* current block is delayed allocated */
4718 int do_fx; /* free extent at end of routine */
4719 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
4720 int error; /* error return value */
4721 int flags; /* inode logging flags */
4722 xfs_bmbt_irec_t got; /* current extent entry */
4723 xfs_fileoff_t got_endoff; /* first offset past got */
4724 int i; /* temp state */
4725 xfs_ifork_t *ifp; /* inode fork pointer */
4726 xfs_mount_t *mp; /* mount structure */
4727 xfs_filblks_t nblks; /* quota/sb block count */
4728 xfs_bmbt_irec_t new; /* new record to be inserted */
4729 /* REFERENCED */
4730 uint qfield; /* quota field to update */
4731 xfs_filblks_t temp; /* for indirect length calculations */
4732 xfs_filblks_t temp2; /* for indirect length calculations */
4733 int state = 0;
4734
4735 XFS_STATS_INC(xs_del_exlist);
4736
4737 if (whichfork == XFS_ATTR_FORK)
4738 state |= BMAP_ATTRFORK;
4739
4740 mp = ip->i_mount;
4741 ifp = XFS_IFORK_PTR(ip, whichfork);
4742 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4743 (uint)sizeof(xfs_bmbt_rec_t)));
4744 ASSERT(del->br_blockcount > 0);
4745 ep = xfs_iext_get_ext(ifp, *idx);
4746 xfs_bmbt_get_all(ep, &got);
4747 ASSERT(got.br_startoff <= del->br_startoff);
4748 del_endoff = del->br_startoff + del->br_blockcount;
4749 got_endoff = got.br_startoff + got.br_blockcount;
4750 ASSERT(got_endoff >= del_endoff);
4751 delay = isnullstartblock(got.br_startblock);
4752 ASSERT(isnullstartblock(del->br_startblock) == delay);
4753 flags = 0;
4754 qfield = 0;
4755 error = 0;
4756 /*
4757 * If deleting a real allocation, must free up the disk space.
4758 */
4759 if (!delay) {
4760 flags = XFS_ILOG_CORE;
4761 /*
4762 * Realtime allocation. Free it and record di_nblocks update.
4763 */
4764 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4765 xfs_fsblock_t bno;
4766 xfs_filblks_t len;
4767
4768 ASSERT(do_mod(del->br_blockcount,
4769 mp->m_sb.sb_rextsize) == 0);
4770 ASSERT(do_mod(del->br_startblock,
4771 mp->m_sb.sb_rextsize) == 0);
4772 bno = del->br_startblock;
4773 len = del->br_blockcount;
4774 do_div(bno, mp->m_sb.sb_rextsize);
4775 do_div(len, mp->m_sb.sb_rextsize);
4776 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4777 if (error)
4778 goto done;
4779 do_fx = 0;
4780 nblks = len * mp->m_sb.sb_rextsize;
4781 qfield = XFS_TRANS_DQ_RTBCOUNT;
4782 }
4783 /*
4784 * Ordinary allocation.
4785 */
4786 else {
4787 do_fx = 1;
4788 nblks = del->br_blockcount;
4789 qfield = XFS_TRANS_DQ_BCOUNT;
4790 }
4791 /*
4792 * Set up del_endblock and cur for later.
4793 */
4794 del_endblock = del->br_startblock + del->br_blockcount;
4795 if (cur) {
4796 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
4797 got.br_startblock, got.br_blockcount,
4798 &i)))
4799 goto done;
4800 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
4801 }
4802 da_old = da_new = 0;
4803 } else {
4804 da_old = startblockval(got.br_startblock);
4805 da_new = 0;
4806 nblks = 0;
4807 do_fx = 0;
4808 }
4809 /*
4810 * Set flag value to use in switch statement.
4811 * Left-contig is 2, right-contig is 1.
4812 */
4813 switch (((got.br_startoff == del->br_startoff) << 1) |
4814 (got_endoff == del_endoff)) {
4815 case 3:
4816 /*
4817 * Matches the whole extent. Delete the entry.
4818 */
4819 xfs_iext_remove(ip, *idx, 1,
4820 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
4821 --*idx;
4822 if (delay)
4823 break;
4824
4825 XFS_IFORK_NEXT_SET(ip, whichfork,
4826 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4827 flags |= XFS_ILOG_CORE;
4828 if (!cur) {
4829 flags |= xfs_ilog_fext(whichfork);
4830 break;
4831 }
4832 if ((error = xfs_btree_delete(cur, &i)))
4833 goto done;
4834 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
4835 break;
4836
4837 case 2:
4838 /*
4839 * Deleting the first part of the extent.
4840 */
4841 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4842 xfs_bmbt_set_startoff(ep, del_endoff);
4843 temp = got.br_blockcount - del->br_blockcount;
4844 xfs_bmbt_set_blockcount(ep, temp);
4845 if (delay) {
4846 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4847 da_old);
4848 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4849 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4850 da_new = temp;
4851 break;
4852 }
4853 xfs_bmbt_set_startblock(ep, del_endblock);
4854 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4855 if (!cur) {
4856 flags |= xfs_ilog_fext(whichfork);
4857 break;
4858 }
4859 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
4860 got.br_blockcount - del->br_blockcount,
4861 got.br_state)))
4862 goto done;
4863 break;
4864
4865 case 1:
4866 /*
4867 * Deleting the last part of the extent.
4868 */
4869 temp = got.br_blockcount - del->br_blockcount;
4870 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4871 xfs_bmbt_set_blockcount(ep, temp);
4872 if (delay) {
4873 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4874 da_old);
4875 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4876 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4877 da_new = temp;
4878 break;
4879 }
4880 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4881 if (!cur) {
4882 flags |= xfs_ilog_fext(whichfork);
4883 break;
4884 }
4885 if ((error = xfs_bmbt_update(cur, got.br_startoff,
4886 got.br_startblock,
4887 got.br_blockcount - del->br_blockcount,
4888 got.br_state)))
4889 goto done;
4890 break;
4891
4892 case 0:
4893 /*
4894 * Deleting the middle of the extent.
4895 */
4896 temp = del->br_startoff - got.br_startoff;
4897 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4898 xfs_bmbt_set_blockcount(ep, temp);
4899 new.br_startoff = del_endoff;
4900 temp2 = got_endoff - del_endoff;
4901 new.br_blockcount = temp2;
4902 new.br_state = got.br_state;
4903 if (!delay) {
4904 new.br_startblock = del_endblock;
4905 flags |= XFS_ILOG_CORE;
4906 if (cur) {
4907 if ((error = xfs_bmbt_update(cur,
4908 got.br_startoff,
4909 got.br_startblock, temp,
4910 got.br_state)))
4911 goto done;
4912 if ((error = xfs_btree_increment(cur, 0, &i)))
4913 goto done;
4914 cur->bc_rec.b = new;
4915 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10004916 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004917 goto done;
4918 /*
4919 * If get no-space back from btree insert,
4920 * it tried a split, and we have a zero
4921 * block reservation.
4922 * Fix up our state and return the error.
4923 */
Dave Chinner24513372014-06-25 14:58:08 +10004924 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11004925 /*
4926 * Reset the cursor, don't trust
4927 * it after any insert operation.
4928 */
4929 if ((error = xfs_bmbt_lookup_eq(cur,
4930 got.br_startoff,
4931 got.br_startblock,
4932 temp, &i)))
4933 goto done;
4934 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
4935 /*
4936 * Update the btree record back
4937 * to the original value.
4938 */
4939 if ((error = xfs_bmbt_update(cur,
4940 got.br_startoff,
4941 got.br_startblock,
4942 got.br_blockcount,
4943 got.br_state)))
4944 goto done;
4945 /*
4946 * Reset the extent record back
4947 * to the original value.
4948 */
4949 xfs_bmbt_set_blockcount(ep,
4950 got.br_blockcount);
4951 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10004952 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004953 goto done;
4954 }
4955 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
4956 } else
4957 flags |= xfs_ilog_fext(whichfork);
4958 XFS_IFORK_NEXT_SET(ip, whichfork,
4959 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
4960 } else {
4961 ASSERT(whichfork == XFS_DATA_FORK);
4962 temp = xfs_bmap_worst_indlen(ip, temp);
4963 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4964 temp2 = xfs_bmap_worst_indlen(ip, temp2);
4965 new.br_startblock = nullstartblock((int)temp2);
4966 da_new = temp + temp2;
4967 while (da_new > da_old) {
4968 if (temp) {
4969 temp--;
4970 da_new--;
4971 xfs_bmbt_set_startblock(ep,
4972 nullstartblock((int)temp));
4973 }
4974 if (da_new == da_old)
4975 break;
4976 if (temp2) {
4977 temp2--;
4978 da_new--;
4979 new.br_startblock =
4980 nullstartblock((int)temp2);
4981 }
4982 }
4983 }
4984 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4985 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
4986 ++*idx;
4987 break;
4988 }
4989 /*
4990 * If we need to, add to list of extents to delete.
4991 */
4992 if (do_fx)
4993 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
4994 mp);
4995 /*
4996 * Adjust inode # blocks in the file.
4997 */
4998 if (nblks)
4999 ip->i_d.di_nblocks -= nblks;
5000 /*
5001 * Adjust quota data.
5002 */
5003 if (qfield)
5004 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5005
5006 /*
5007 * Account for change in delayed indirect blocks.
5008 * Nothing to do for disk quota accounting here.
5009 */
5010 ASSERT(da_old >= da_new);
5011 if (da_old > da_new) {
5012 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5013 (int64_t)(da_old - da_new), 0);
5014 }
5015done:
5016 *logflagsp = flags;
5017 return error;
5018}
5019
5020/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021 * Unmap (remove) blocks from a file.
5022 * If nexts is nonzero then the number of extents to remove is limited to
5023 * that value. If not all extents in the block range can be removed then
5024 * *done is set.
5025 */
5026int /* error */
5027xfs_bunmapi(
5028 xfs_trans_t *tp, /* transaction pointer */
5029 struct xfs_inode *ip, /* incore inode */
5030 xfs_fileoff_t bno, /* starting offset to unmap */
5031 xfs_filblks_t len, /* length to unmap in file */
5032 int flags, /* misc flags */
5033 xfs_extnum_t nexts, /* number of extents max */
5034 xfs_fsblock_t *firstblock, /* first allocated block
5035 controls a.g. for allocs */
5036 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5037 int *done) /* set if not done yet */
5038{
5039 xfs_btree_cur_t *cur; /* bmap btree cursor */
5040 xfs_bmbt_irec_t del; /* extent being deleted */
5041 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005042 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 int error; /* error return value */
5044 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005045 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 xfs_ifork_t *ifp; /* inode fork pointer */
5047 int isrt; /* freeing in rt area */
5048 xfs_extnum_t lastx; /* last extent index used */
5049 int logflags; /* transaction logging flags */
5050 xfs_extlen_t mod; /* rt extent offset */
5051 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005052 xfs_extnum_t nextents; /* number of file extents */
5053 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 xfs_fileoff_t start; /* first file offset deleted */
5055 int tmp_logflags; /* partial logging flags */
5056 int wasdel; /* was a delayed alloc extent */
5057 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 xfs_fsblock_t sum;
5059
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005060 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5061
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5063 XFS_ATTR_FORK : XFS_DATA_FORK;
5064 ifp = XFS_IFORK_PTR(ip, whichfork);
5065 if (unlikely(
5066 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5067 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5068 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5069 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005070 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005071 }
5072 mp = ip->i_mount;
5073 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005074 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005075
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005076 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077 ASSERT(len > 0);
5078 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5081 (error = xfs_iread_extents(tp, ip, whichfork)))
5082 return error;
5083 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5084 if (nextents == 0) {
5085 *done = 1;
5086 return 0;
5087 }
5088 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005089 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 start = bno;
5091 bno = start + len - 1;
5092 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5093 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005094
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 /*
5096 * Check to see if the given block number is past the end of the
5097 * file, back up to the last block if so...
5098 */
5099 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005100 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 xfs_bmbt_get_all(ep, &got);
5102 bno = got.br_startoff + got.br_blockcount - 1;
5103 }
5104 logflags = 0;
5105 if (ifp->if_flags & XFS_IFBROOT) {
5106 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005107 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108 cur->bc_private.b.firstblock = *firstblock;
5109 cur->bc_private.b.flist = flist;
5110 cur->bc_private.b.flags = 0;
5111 } else
5112 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005113
5114 if (isrt) {
5115 /*
5116 * Synchronize by locking the bitmap inode.
5117 */
5118 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5119 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5120 }
5121
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 extno = 0;
5123 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5124 (nexts == 0 || extno < nexts)) {
5125 /*
5126 * Is the found extent after a hole in which bno lives?
5127 * Just back up to the previous extent, if so.
5128 */
5129 if (got.br_startoff > bno) {
5130 if (--lastx < 0)
5131 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005132 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005133 xfs_bmbt_get_all(ep, &got);
5134 }
5135 /*
5136 * Is the last block of this extent before the range
5137 * we're supposed to delete? If so, we're done.
5138 */
5139 bno = XFS_FILEOFF_MIN(bno,
5140 got.br_startoff + got.br_blockcount - 1);
5141 if (bno < start)
5142 break;
5143 /*
5144 * Then deal with the (possibly delayed) allocated space
5145 * we found.
5146 */
5147 ASSERT(ep != NULL);
5148 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005149 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 if (got.br_startoff < start) {
5151 del.br_startoff = start;
5152 del.br_blockcount -= start - got.br_startoff;
5153 if (!wasdel)
5154 del.br_startblock += start - got.br_startoff;
5155 }
5156 if (del.br_startoff + del.br_blockcount > bno + 1)
5157 del.br_blockcount = bno + 1 - del.br_startoff;
5158 sum = del.br_startblock + del.br_blockcount;
5159 if (isrt &&
5160 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5161 /*
5162 * Realtime extent not lined up at the end.
5163 * The extent could have been split into written
5164 * and unwritten pieces, or we could just be
5165 * unmapping part of it. But we can't really
5166 * get rid of part of a realtime extent.
5167 */
5168 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005169 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005170 /*
5171 * This piece is unwritten, or we're not
5172 * using unwritten extents. Skip over it.
5173 */
5174 ASSERT(bno >= mod);
5175 bno -= mod > del.br_blockcount ?
5176 del.br_blockcount : mod;
5177 if (bno < got.br_startoff) {
5178 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005179 xfs_bmbt_get_all(xfs_iext_get_ext(
5180 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 }
5182 continue;
5183 }
5184 /*
5185 * It's written, turn it unwritten.
5186 * This is better than zeroing it.
5187 */
5188 ASSERT(del.br_state == XFS_EXT_NORM);
5189 ASSERT(xfs_trans_get_block_res(tp) > 0);
5190 /*
5191 * If this spans a realtime extent boundary,
5192 * chop it back to the start of the one we end at.
5193 */
5194 if (del.br_blockcount > mod) {
5195 del.br_startoff += del.br_blockcount - mod;
5196 del.br_startblock += del.br_blockcount - mod;
5197 del.br_blockcount = mod;
5198 }
5199 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005200 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5201 &lastx, &cur, &del, firstblock, flist,
5202 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 if (error)
5204 goto error0;
5205 goto nodelete;
5206 }
5207 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5208 /*
5209 * Realtime extent is lined up at the end but not
5210 * at the front. We'll get rid of full extents if
5211 * we can.
5212 */
5213 mod = mp->m_sb.sb_rextsize - mod;
5214 if (del.br_blockcount > mod) {
5215 del.br_blockcount -= mod;
5216 del.br_startoff += mod;
5217 del.br_startblock += mod;
5218 } else if ((del.br_startoff == start &&
5219 (del.br_state == XFS_EXT_UNWRITTEN ||
5220 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005221 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222 /*
5223 * Can't make it unwritten. There isn't
5224 * a full extent here so just skip it.
5225 */
5226 ASSERT(bno >= del.br_blockcount);
5227 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005228 if (got.br_startoff > bno) {
5229 if (--lastx >= 0) {
5230 ep = xfs_iext_get_ext(ifp,
5231 lastx);
5232 xfs_bmbt_get_all(ep, &got);
5233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005234 }
5235 continue;
5236 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5237 /*
5238 * This one is already unwritten.
5239 * It must have a written left neighbor.
5240 * Unwrite the killed part of that one and
5241 * try again.
5242 */
5243 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005244 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5245 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005247 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248 ASSERT(del.br_startblock ==
5249 prev.br_startblock + prev.br_blockcount);
5250 if (prev.br_startoff < start) {
5251 mod = start - prev.br_startoff;
5252 prev.br_blockcount -= mod;
5253 prev.br_startblock += mod;
5254 prev.br_startoff = start;
5255 }
5256 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005257 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005258 error = xfs_bmap_add_extent_unwritten_real(tp,
5259 ip, &lastx, &cur, &prev,
5260 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 if (error)
5262 goto error0;
5263 goto nodelete;
5264 } else {
5265 ASSERT(del.br_state == XFS_EXT_NORM);
5266 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005267 error = xfs_bmap_add_extent_unwritten_real(tp,
5268 ip, &lastx, &cur, &del,
5269 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 if (error)
5271 goto error0;
5272 goto nodelete;
5273 }
5274 }
5275 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005276 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005277 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005278 if (isrt) {
5279 xfs_filblks_t rtexts;
5280
5281 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5282 do_div(rtexts, mp->m_sb.sb_rextsize);
5283 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005284 (int64_t)rtexts, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005285 (void)xfs_trans_reserve_quota_nblks(NULL,
5286 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005287 XFS_QMOPT_RES_RTBLKS);
5288 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00005289 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005290 (int64_t)del.br_blockcount, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005291 (void)xfs_trans_reserve_quota_nblks(NULL,
5292 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005295 ip->i_delayed_blks -= del.br_blockcount;
5296 if (cur)
5297 cur->bc_private.b.flags |=
5298 XFS_BTCUR_BPRV_WASDEL;
5299 } else if (cur)
5300 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5301 /*
5302 * If it's the case where the directory code is running
5303 * with no block reservation, and the deleted block is in
5304 * the middle of its extent, and the resulting insert
5305 * of an extent would cause transformation to btree format,
5306 * then reject it. The calling code will then swap
5307 * blocks around instead.
5308 * We have to do this now, rather than waiting for the
5309 * conversion to btree format, since the transaction
5310 * will be dirty.
5311 */
5312 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5313 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005314 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5315 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 del.br_startoff > got.br_startoff &&
5317 del.br_startoff + del.br_blockcount <
5318 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005319 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 goto error0;
5321 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00005322 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005323 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 logflags |= tmp_logflags;
5325 if (error)
5326 goto error0;
5327 bno = del.br_startoff - 1;
5328nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 /*
5330 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005333 if (lastx >= 0) {
5334 ep = xfs_iext_get_ext(ifp, lastx);
5335 if (xfs_bmbt_get_startoff(ep) > bno) {
5336 if (--lastx >= 0)
5337 ep = xfs_iext_get_ext(ifp,
5338 lastx);
5339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005342 extno++;
5343 }
5344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005346
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 /*
5348 * Convert to a btree if necessary.
5349 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005350 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 ASSERT(cur == NULL);
5352 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5353 &cur, 0, &tmp_logflags, whichfork);
5354 logflags |= tmp_logflags;
5355 if (error)
5356 goto error0;
5357 }
5358 /*
5359 * transform from btree to extents, give it cur
5360 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005361 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 ASSERT(cur != NULL);
5363 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5364 whichfork);
5365 logflags |= tmp_logflags;
5366 if (error)
5367 goto error0;
5368 }
5369 /*
5370 * transform from extents to local?
5371 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 error = 0;
5373error0:
5374 /*
5375 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005376 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005378 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005380 logflags &= ~xfs_ilog_fext(whichfork);
5381 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005383 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005384 /*
5385 * Log inode even in the error case, if the transaction
5386 * is dirty we'll need to shut down the filesystem.
5387 */
5388 if (logflags)
5389 xfs_trans_log_inode(tp, ip, logflags);
5390 if (cur) {
5391 if (!error) {
5392 *firstblock = cur->bc_private.b.firstblock;
5393 cur->bc_private.b.allocated = 0;
5394 }
5395 xfs_btree_del_cursor(cur,
5396 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5397 }
5398 return error;
5399}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005400
5401/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005402 * Determine whether an extent shift can be accomplished by a merge with the
5403 * extent that precedes the target hole of the shift.
5404 */
5405STATIC bool
5406xfs_bmse_can_merge(
5407 struct xfs_bmbt_irec *left, /* preceding extent */
5408 struct xfs_bmbt_irec *got, /* current extent to shift */
5409 xfs_fileoff_t shift) /* shift fsb */
5410{
5411 xfs_fileoff_t startoff;
5412
5413 startoff = got->br_startoff - shift;
5414
5415 /*
5416 * The extent, once shifted, must be adjacent in-file and on-disk with
5417 * the preceding extent.
5418 */
5419 if ((left->br_startoff + left->br_blockcount != startoff) ||
5420 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5421 (left->br_state != got->br_state) ||
5422 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5423 return false;
5424
5425 return true;
5426}
5427
5428/*
5429 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5430 * hole in the file. If an extent shift would result in the extent being fully
5431 * adjacent to the extent that currently precedes the hole, we can merge with
5432 * the preceding extent rather than do the shift.
5433 *
5434 * This function assumes the caller has verified a shift-by-merge is possible
5435 * with the provided extents via xfs_bmse_can_merge().
5436 */
5437STATIC int
5438xfs_bmse_merge(
5439 struct xfs_inode *ip,
5440 int whichfork,
5441 xfs_fileoff_t shift, /* shift fsb */
5442 int current_ext, /* idx of gotp */
5443 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5444 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5445 struct xfs_btree_cur *cur,
5446 int *logflags) /* output */
5447{
Brian Fosterddb19e32014-09-23 15:38:09 +10005448 struct xfs_bmbt_irec got;
5449 struct xfs_bmbt_irec left;
5450 xfs_filblks_t blockcount;
5451 int error, i;
5452
Brian Fosterddb19e32014-09-23 15:38:09 +10005453 xfs_bmbt_get_all(gotp, &got);
5454 xfs_bmbt_get_all(leftp, &left);
5455 blockcount = left.br_blockcount + got.br_blockcount;
5456
5457 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5458 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5459 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5460
5461 /*
5462 * Merge the in-core extents. Note that the host record pointers and
5463 * current_ext index are invalid once the extent has been removed via
5464 * xfs_iext_remove().
5465 */
5466 xfs_bmbt_set_blockcount(leftp, blockcount);
5467 xfs_iext_remove(ip, current_ext, 1, 0);
5468
5469 /*
5470 * Update the on-disk extent count, the btree if necessary and log the
5471 * inode.
5472 */
5473 XFS_IFORK_NEXT_SET(ip, whichfork,
5474 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5475 *logflags |= XFS_ILOG_CORE;
5476 if (!cur) {
5477 *logflags |= XFS_ILOG_DEXT;
5478 return 0;
5479 }
5480
5481 /* lookup and remove the extent to merge */
5482 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5483 got.br_blockcount, &i);
5484 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005485 return error;
5486 XFS_WANT_CORRUPTED_RETURN(i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005487
5488 error = xfs_btree_delete(cur, &i);
5489 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005490 return error;
5491 XFS_WANT_CORRUPTED_RETURN(i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005492
5493 /* lookup and update size of the previous extent */
5494 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5495 left.br_blockcount, &i);
5496 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005497 return error;
5498 XFS_WANT_CORRUPTED_RETURN(i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005499
5500 left.br_blockcount = blockcount;
5501
Dave Chinner4db431f2014-12-04 09:42:40 +11005502 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5503 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005504}
5505
5506/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005507 * Shift a single extent.
5508 */
5509STATIC int
5510xfs_bmse_shift_one(
5511 struct xfs_inode *ip,
5512 int whichfork,
5513 xfs_fileoff_t offset_shift_fsb,
5514 int *current_ext,
5515 struct xfs_bmbt_rec_host *gotp,
5516 struct xfs_btree_cur *cur,
5517 int *logflags)
5518{
5519 struct xfs_ifork *ifp;
5520 xfs_fileoff_t startoff;
5521 struct xfs_bmbt_rec_host *leftp;
5522 struct xfs_bmbt_irec got;
5523 struct xfs_bmbt_irec left;
5524 int error;
5525 int i;
5526
5527 ifp = XFS_IFORK_PTR(ip, whichfork);
5528
5529 xfs_bmbt_get_all(gotp, &got);
5530 startoff = got.br_startoff - offset_shift_fsb;
5531
Brian Fosterf71721d2014-09-23 15:39:05 +10005532 /* delalloc extents should be prevented by caller */
Dave Chinnerb11bd672014-12-04 09:42:24 +11005533 XFS_WANT_CORRUPTED_RETURN(!isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005534
Brian Fostera979bdf2014-09-23 15:39:04 +10005535 /*
Dave Chinnerb11bd672014-12-04 09:42:24 +11005536 * Check for merge if we've got an extent to the left, otherwise make
5537 * sure there's enough room at the start of the file for the shift.
Brian Fostera979bdf2014-09-23 15:39:04 +10005538 */
Dave Chinnerb11bd672014-12-04 09:42:24 +11005539 if (*current_ext) {
5540 /* grab the left extent and check for a large enough hole */
5541 leftp = xfs_iext_get_ext(ifp, *current_ext - 1);
5542 xfs_bmbt_get_all(leftp, &left);
5543
5544 if (startoff < left.br_startoff + left.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005545 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005546
Dave Chinnerb11bd672014-12-04 09:42:24 +11005547 /* check whether to merge the extent or shift it down */
5548 if (xfs_bmse_can_merge(&left, &got, offset_shift_fsb)) {
5549 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5550 *current_ext, gotp, leftp, cur,
5551 logflags);
5552 }
5553 } else if (got.br_startoff < offset_shift_fsb)
Brian Fostera979bdf2014-09-23 15:39:04 +10005554 return -EINVAL;
5555
Brian Fostera979bdf2014-09-23 15:39:04 +10005556 /*
5557 * Increment the extent index for the next iteration, update the start
5558 * offset of the in-core extent and update the btree if applicable.
5559 */
5560 (*current_ext)++;
5561 xfs_bmbt_set_startoff(gotp, startoff);
5562 *logflags |= XFS_ILOG_CORE;
5563 if (!cur) {
5564 *logflags |= XFS_ILOG_DEXT;
5565 return 0;
5566 }
5567
5568 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5569 got.br_blockcount, &i);
5570 if (error)
5571 return error;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005572 XFS_WANT_CORRUPTED_RETURN(i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10005573
5574 got.br_startoff = startoff;
kbuild test robotd254aae2014-12-01 08:42:52 +11005575 return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
Brian Fostera979bdf2014-09-23 15:39:04 +10005576 got.br_blockcount, got.br_state);
Brian Fostera979bdf2014-09-23 15:39:04 +10005577}
5578
5579/*
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005580 * Shift extent records to the left to cover a hole.
5581 *
Brian Foster2c845f52014-09-23 15:37:09 +10005582 * The maximum number of extents to be shifted in a single operation is
5583 * @num_exts. @start_fsb specifies the file offset to start the shift and the
5584 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5585 * is the length by which each extent is shifted. If there is no hole to shift
5586 * the extents into, this will be considered invalid operation and we abort
5587 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005588 */
5589int
5590xfs_bmap_shift_extents(
5591 struct xfs_trans *tp,
5592 struct xfs_inode *ip,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005593 xfs_fileoff_t start_fsb,
5594 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005595 int *done,
5596 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005597 xfs_fsblock_t *firstblock,
5598 struct xfs_bmap_free *flist,
5599 int num_exts)
5600{
Brian Fosterca446d82014-09-02 12:12:53 +10005601 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005602 struct xfs_bmbt_rec_host *gotp;
5603 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005604 struct xfs_mount *mp = ip->i_mount;
5605 struct xfs_ifork *ifp;
5606 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005607 xfs_extnum_t current_ext;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005608 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005609 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10005610 int logflags = 0;
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005611 int total_extents;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005612
5613 if (unlikely(XFS_TEST_ERROR(
5614 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5615 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5616 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5617 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5618 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005619 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005620 }
5621
5622 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005623 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005624
Brian Foster2c845f52014-09-23 15:37:09 +10005625 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5626 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005627
5628 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005629 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5630 /* Read in all the extents */
5631 error = xfs_iread_extents(tp, ip, whichfork);
5632 if (error)
5633 return error;
5634 }
5635
Brian Fosterddb19e32014-09-23 15:38:09 +10005636 if (ifp->if_flags & XFS_IFBROOT) {
5637 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5638 cur->bc_private.b.firstblock = *firstblock;
5639 cur->bc_private.b.flist = flist;
5640 cur->bc_private.b.flags = 0;
5641 }
5642
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005643 /*
Brian Foster2c845f52014-09-23 15:37:09 +10005644 * Look up the extent index for the fsb where we start shifting. We can
5645 * henceforth iterate with current_ext as extent list changes are locked
5646 * out via ilock.
5647 *
5648 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5649 * start_fsb lies in a hole beyond which there are no extents. Either
5650 * way, we are done.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005651 */
Brian Foster2c845f52014-09-23 15:37:09 +10005652 gotp = xfs_iext_bno_to_ext(ifp, start_fsb, &current_ext);
5653 if (!gotp) {
5654 *done = 1;
Brian Fosterddb19e32014-09-23 15:38:09 +10005655 goto del_cursor;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005656 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005657
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005658 /*
5659 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10005660 * are collapsing out, so we cannot use the count of real extents here.
5661 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005662 */
5663 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Brian Foster2c845f52014-09-23 15:37:09 +10005664 while (nexts++ < num_exts && current_ext < total_extents) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005665 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
5666 &current_ext, gotp, cur, &logflags);
5667 if (error)
5668 goto del_cursor;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005669
Brian Fosterddb19e32014-09-23 15:38:09 +10005670 /* update total extent count and grab the next record */
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005671 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Brian Fostera979bdf2014-09-23 15:39:04 +10005672 if (current_ext >= total_extents)
Brian Fosterddb19e32014-09-23 15:38:09 +10005673 break;
5674 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005675 }
5676
5677 /* Check if we are done */
Brian Fostera979bdf2014-09-23 15:39:04 +10005678 if (current_ext == total_extents) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005679 *done = 1;
Brian Fostera979bdf2014-09-23 15:39:04 +10005680 } else if (next_fsb) {
5681 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10005682 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10005683 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005684
5685del_cursor:
5686 if (cur)
5687 xfs_btree_del_cursor(cur,
5688 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5689
Brian Fosterca446d82014-09-02 12:12:53 +10005690 if (logflags)
5691 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10005692
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005693 return error;
5694}