blob: f47e65c30be6ddde5caa276d3262540d603d07dc [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner6ca1c902013-08-12 20:49:26 +100020#include "xfs_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100027#include "xfs_mount.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100029#include "xfs_dir2_format.h"
30#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_bmap_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_alloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_dinode.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110036#include "xfs_btree.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_mount.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include "xfs_itable.h"
Nathan Scotta844f452005-11-02 14:38:42 +110039#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_extfree_item.h"
41#include "xfs_alloc.h"
42#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100043#include "xfs_bmap_util.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include "xfs_rtalloc.h"
45#include "xfs_error.h"
Nathan Scottd8cc8902005-11-02 10:34:53 +110046#include "xfs_attr_leaf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "xfs_quota.h"
48#include "xfs_trans_space.h"
49#include "xfs_buf_item.h"
David Chinner2a82b8b2007-07-11 11:09:12 +100050#include "xfs_filestream.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000051#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110052#include "xfs_symlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055kmem_zone_t *xfs_bmap_free_item_zone;
56
57/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110058 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110062 * Compute and fill in the value of the maximum depth of a bmap btree
63 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110065void
66xfs_bmap_compute_maxlevels(
67 xfs_mount_t *mp, /* file system mount structure */
68 int whichfork) /* data or attr fork */
69{
70 int level; /* btree level */
71 uint maxblocks; /* max blocks at this level */
72 uint maxleafents; /* max leaf entries possible */
73 int maxrootrecs; /* max records in root block */
74 int minleafrecs; /* min records in leaf block */
75 int minnoderecs; /* min records in node block */
76 int sz; /* root block size */
77
78 /*
79 * The maximum number of extents in a file, hence the maximum
80 * number of leaf entries, is controlled by the type of di_nextents
81 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
82 * (a signed 16-bit number, xfs_aextnum_t).
83 *
84 * Note that we can no longer assume that if we are in ATTR1 that
85 * the fork offset of all the inodes will be
86 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
87 * with ATTR2 and then mounted back with ATTR1, keeping the
88 * di_forkoff's fixed but probably at various positions. Therefore,
89 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
90 * of a minimum size available.
91 */
92 if (whichfork == XFS_DATA_FORK) {
93 maxleafents = MAXEXTNUM;
94 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
95 } else {
96 maxleafents = MAXAEXTNUM;
97 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
98 }
99 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
100 minleafrecs = mp->m_bmap_dmnr[0];
101 minnoderecs = mp->m_bmap_dmnr[1];
102 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
103 for (level = 1; maxblocks > 1; level++) {
104 if (maxblocks <= maxrootrecs)
105 maxblocks = 1;
106 else
107 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
108 }
109 mp->m_bm_maxlevels[whichfork] = level;
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100112STATIC int /* error */
113xfs_bmbt_lookup_eq(
114 struct xfs_btree_cur *cur,
115 xfs_fileoff_t off,
116 xfs_fsblock_t bno,
117 xfs_filblks_t len,
118 int *stat) /* success/failure */
119{
120 cur->bc_rec.b.br_startoff = off;
121 cur->bc_rec.b.br_startblock = bno;
122 cur->bc_rec.b.br_blockcount = len;
123 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
124}
125
126STATIC int /* error */
127xfs_bmbt_lookup_ge(
128 struct xfs_btree_cur *cur,
129 xfs_fileoff_t off,
130 xfs_fsblock_t bno,
131 xfs_filblks_t len,
132 int *stat) /* success/failure */
133{
134 cur->bc_rec.b.br_startoff = off;
135 cur->bc_rec.b.br_startblock = bno;
136 cur->bc_rec.b.br_blockcount = len;
137 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
138}
139
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100140/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000141 * Check if the inode needs to be converted to btree format.
142 */
143static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
144{
145 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
146 XFS_IFORK_NEXTENTS(ip, whichfork) >
147 XFS_IFORK_MAXEXT(ip, whichfork);
148}
149
150/*
151 * Check if the inode should be converted to extent format.
152 */
153static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
154{
155 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
156 XFS_IFORK_NEXTENTS(ip, whichfork) <=
157 XFS_IFORK_MAXEXT(ip, whichfork);
158}
159
160/*
161 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100162 * by [off, bno, len, state].
163 * This either works (return 0) or gets an EFSCORRUPTED error.
164 */
165STATIC int
166xfs_bmbt_update(
167 struct xfs_btree_cur *cur,
168 xfs_fileoff_t off,
169 xfs_fsblock_t bno,
170 xfs_filblks_t len,
171 xfs_exntst_t state)
172{
173 union xfs_btree_rec rec;
174
175 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
176 return xfs_btree_update(cur, &rec);
177}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100180 * Compute the worst-case number of indirect blocks that will be used
181 * for ip's delayed extent of length "len".
182 */
183STATIC xfs_filblks_t
184xfs_bmap_worst_indlen(
185 xfs_inode_t *ip, /* incore inode pointer */
186 xfs_filblks_t len) /* delayed extent length */
187{
188 int level; /* btree level number */
189 int maxrecs; /* maximum record count at this level */
190 xfs_mount_t *mp; /* mount structure */
191 xfs_filblks_t rval; /* return value */
192
193 mp = ip->i_mount;
194 maxrecs = mp->m_bmap_dmxr[0];
195 for (level = 0, rval = 0;
196 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
197 level++) {
198 len += maxrecs - 1;
199 do_div(len, maxrecs);
200 rval += len;
201 if (len == 1)
202 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
203 level - 1;
204 if (level == 0)
205 maxrecs = mp->m_bmap_dmxr[1];
206 }
207 return rval;
208}
209
210/*
211 * Calculate the default attribute fork offset for newly created inodes.
212 */
213uint
214xfs_default_attroffset(
215 struct xfs_inode *ip)
216{
217 struct xfs_mount *mp = ip->i_mount;
218 uint offset;
219
220 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100221 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100222 XFS_BMDR_SPACE_CALC(MINABTPTRS);
223 } else {
224 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
225 }
226
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100227 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100228 return offset;
229}
230
231/*
232 * Helper routine to reset inode di_forkoff field when switching
233 * attribute fork from local to extent format - we reset it where
234 * possible to make space available for inline data fork extents.
235 */
236STATIC void
237xfs_bmap_forkoff_reset(
238 xfs_mount_t *mp,
239 xfs_inode_t *ip,
240 int whichfork)
241{
242 if (whichfork == XFS_ATTR_FORK &&
243 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
244 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
245 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
246 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
247
248 if (dfl_forkoff > ip->i_d.di_forkoff)
249 ip->i_d.di_forkoff = dfl_forkoff;
250 }
251}
252
253/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100254 * Debug/sanity checking code
255 */
256
257STATIC int
258xfs_bmap_sanity_check(
259 struct xfs_mount *mp,
260 struct xfs_buf *bp,
261 int level)
262{
263 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
264
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500265 if (block->bb_magic != cpu_to_be32(XFS_BMAP_CRC_MAGIC) &&
266 block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC))
267 return 0;
268
269 if (be16_to_cpu(block->bb_level) != level ||
Dave Chinner9e5987a2013-02-25 12:31:26 +1100270 be16_to_cpu(block->bb_numrecs) == 0 ||
271 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
272 return 0;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500273
Dave Chinner9e5987a2013-02-25 12:31:26 +1100274 return 1;
275}
276
277#ifdef DEBUG
278STATIC struct xfs_buf *
279xfs_bmap_get_bp(
280 struct xfs_btree_cur *cur,
281 xfs_fsblock_t bno)
282{
283 struct xfs_log_item_desc *lidp;
284 int i;
285
286 if (!cur)
287 return NULL;
288
289 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
290 if (!cur->bc_bufs[i])
291 break;
292 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
293 return cur->bc_bufs[i];
294 }
295
296 /* Chase down all the log items to see if the bp is there */
297 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
298 struct xfs_buf_log_item *bip;
299 bip = (struct xfs_buf_log_item *)lidp->lid_item;
300 if (bip->bli_item.li_type == XFS_LI_BUF &&
301 XFS_BUF_ADDR(bip->bli_buf) == bno)
302 return bip->bli_buf;
303 }
304
305 return NULL;
306}
307
308STATIC void
309xfs_check_block(
310 struct xfs_btree_block *block,
311 xfs_mount_t *mp,
312 int root,
313 short sz)
314{
315 int i, j, dmxr;
316 __be64 *pp, *thispa; /* pointer to block address */
317 xfs_bmbt_key_t *prevp, *keyp;
318
319 ASSERT(be16_to_cpu(block->bb_level) > 0);
320
321 prevp = NULL;
322 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
323 dmxr = mp->m_bmap_dmxr[0];
324 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
325
326 if (prevp) {
327 ASSERT(be64_to_cpu(prevp->br_startoff) <
328 be64_to_cpu(keyp->br_startoff));
329 }
330 prevp = keyp;
331
332 /*
333 * Compare the block numbers to see if there are dups.
334 */
335 if (root)
336 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
337 else
338 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
339
340 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
341 if (root)
342 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
343 else
344 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
345 if (*thispa == *pp) {
346 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
347 __func__, j, i,
348 (unsigned long long)be64_to_cpu(*thispa));
349 panic("%s: ptrs are equal in node\n",
350 __func__);
351 }
352 }
353 }
354}
355
356/*
357 * Check that the extents for the inode ip are in the right order in all
358 * btree leaves.
359 */
360
361STATIC void
362xfs_bmap_check_leaf_extents(
363 xfs_btree_cur_t *cur, /* btree cursor or null */
364 xfs_inode_t *ip, /* incore inode pointer */
365 int whichfork) /* data or attr fork */
366{
367 struct xfs_btree_block *block; /* current btree block */
368 xfs_fsblock_t bno; /* block # of "block" */
369 xfs_buf_t *bp; /* buffer for "block" */
370 int error; /* error return value */
371 xfs_extnum_t i=0, j; /* index into the extents list */
372 xfs_ifork_t *ifp; /* fork structure */
373 int level; /* btree level, for checking */
374 xfs_mount_t *mp; /* file system mount structure */
375 __be64 *pp; /* pointer to block address */
376 xfs_bmbt_rec_t *ep; /* pointer to current extent */
377 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
378 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
379 int bp_release = 0;
380
381 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
382 return;
383 }
384
385 bno = NULLFSBLOCK;
386 mp = ip->i_mount;
387 ifp = XFS_IFORK_PTR(ip, whichfork);
388 block = ifp->if_broot;
389 /*
390 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
391 */
392 level = be16_to_cpu(block->bb_level);
393 ASSERT(level > 0);
394 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
395 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
396 bno = be64_to_cpu(*pp);
397
398 ASSERT(bno != NULLDFSBNO);
399 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
400 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
401
402 /*
403 * Go down the tree until leaf level is reached, following the first
404 * pointer (leftmost) at each level.
405 */
406 while (level-- > 0) {
407 /* See if buf is in cur first */
408 bp_release = 0;
409 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
410 if (!bp) {
411 bp_release = 1;
412 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
413 XFS_BMAP_BTREE_REF,
414 &xfs_bmbt_buf_ops);
415 if (error)
416 goto error_norelse;
417 }
418 block = XFS_BUF_TO_BLOCK(bp);
419 XFS_WANT_CORRUPTED_GOTO(
420 xfs_bmap_sanity_check(mp, bp, level),
421 error0);
422 if (level == 0)
423 break;
424
425 /*
426 * Check this block for basic sanity (increasing keys and
427 * no duplicate blocks).
428 */
429
430 xfs_check_block(block, mp, 0, 0);
431 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
432 bno = be64_to_cpu(*pp);
433 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
434 if (bp_release) {
435 bp_release = 0;
436 xfs_trans_brelse(NULL, bp);
437 }
438 }
439
440 /*
441 * Here with bp and block set to the leftmost leaf node in the tree.
442 */
443 i = 0;
444
445 /*
446 * Loop over all leaf nodes checking that all extents are in the right order.
447 */
448 for (;;) {
449 xfs_fsblock_t nextbno;
450 xfs_extnum_t num_recs;
451
452
453 num_recs = xfs_btree_get_numrecs(block);
454
455 /*
456 * Read-ahead the next leaf block, if any.
457 */
458
459 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
460
461 /*
462 * Check all the extents to make sure they are OK.
463 * If we had a previous block, the last entry should
464 * conform with the first entry in this one.
465 */
466
467 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
468 if (i) {
469 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
470 xfs_bmbt_disk_get_blockcount(&last) <=
471 xfs_bmbt_disk_get_startoff(ep));
472 }
473 for (j = 1; j < num_recs; j++) {
474 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
475 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
476 xfs_bmbt_disk_get_blockcount(ep) <=
477 xfs_bmbt_disk_get_startoff(nextp));
478 ep = nextp;
479 }
480
481 last = *ep;
482 i += num_recs;
483 if (bp_release) {
484 bp_release = 0;
485 xfs_trans_brelse(NULL, bp);
486 }
487 bno = nextbno;
488 /*
489 * If we've reached the end, stop.
490 */
491 if (bno == NULLFSBLOCK)
492 break;
493
494 bp_release = 0;
495 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
496 if (!bp) {
497 bp_release = 1;
498 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
499 XFS_BMAP_BTREE_REF,
500 &xfs_bmbt_buf_ops);
501 if (error)
502 goto error_norelse;
503 }
504 block = XFS_BUF_TO_BLOCK(bp);
505 }
506 if (bp_release) {
507 bp_release = 0;
508 xfs_trans_brelse(NULL, bp);
509 }
510 return;
511
512error0:
513 xfs_warn(mp, "%s: at error0", __func__);
514 if (bp_release)
515 xfs_trans_brelse(NULL, bp);
516error_norelse:
517 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
518 __func__, i);
519 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
520 return;
521}
522
523/*
524 * Add bmap trace insert entries for all the contents of the extent records.
525 */
526void
527xfs_bmap_trace_exlist(
528 xfs_inode_t *ip, /* incore inode pointer */
529 xfs_extnum_t cnt, /* count of entries in the list */
530 int whichfork, /* data or attr fork */
531 unsigned long caller_ip)
532{
533 xfs_extnum_t idx; /* extent record index */
534 xfs_ifork_t *ifp; /* inode fork pointer */
535 int state = 0;
536
537 if (whichfork == XFS_ATTR_FORK)
538 state |= BMAP_ATTRFORK;
539
540 ifp = XFS_IFORK_PTR(ip, whichfork);
541 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
542 for (idx = 0; idx < cnt; idx++)
543 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
544}
545
546/*
547 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000548 * given the caller's original parameters. Specifically check the
549 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100550 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
551 */
552STATIC void
553xfs_bmap_validate_ret(
554 xfs_fileoff_t bno,
555 xfs_filblks_t len,
556 int flags,
557 xfs_bmbt_irec_t *mval,
558 int nmap,
559 int ret_nmap)
560{
561 int i; /* index to map values */
562
563 ASSERT(ret_nmap <= nmap);
564
565 for (i = 0; i < ret_nmap; i++) {
566 ASSERT(mval[i].br_blockcount > 0);
567 if (!(flags & XFS_BMAPI_ENTIRE)) {
568 ASSERT(mval[i].br_startoff >= bno);
569 ASSERT(mval[i].br_blockcount <= len);
570 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
571 bno + len);
572 } else {
573 ASSERT(mval[i].br_startoff < bno + len);
574 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
575 bno);
576 }
577 ASSERT(i == 0 ||
578 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
579 mval[i].br_startoff);
580 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
581 mval[i].br_startblock != HOLESTARTBLOCK);
582 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
583 mval[i].br_state == XFS_EXT_UNWRITTEN);
584 }
585}
586
587#else
588#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
589#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
590#endif /* DEBUG */
591
592/*
593 * bmap free list manipulation functions
594 */
595
596/*
597 * Add the extent to the list of extents to be free at transaction end.
598 * The list is maintained sorted (by block number).
599 */
600void
601xfs_bmap_add_free(
602 xfs_fsblock_t bno, /* fs block number of extent */
603 xfs_filblks_t len, /* length of extent */
604 xfs_bmap_free_t *flist, /* list of extents */
605 xfs_mount_t *mp) /* mount point structure */
606{
607 xfs_bmap_free_item_t *cur; /* current (next) element */
608 xfs_bmap_free_item_t *new; /* new element */
609 xfs_bmap_free_item_t *prev; /* previous element */
610#ifdef DEBUG
611 xfs_agnumber_t agno;
612 xfs_agblock_t agbno;
613
614 ASSERT(bno != NULLFSBLOCK);
615 ASSERT(len > 0);
616 ASSERT(len <= MAXEXTLEN);
617 ASSERT(!isnullstartblock(bno));
618 agno = XFS_FSB_TO_AGNO(mp, bno);
619 agbno = XFS_FSB_TO_AGBNO(mp, bno);
620 ASSERT(agno < mp->m_sb.sb_agcount);
621 ASSERT(agbno < mp->m_sb.sb_agblocks);
622 ASSERT(len < mp->m_sb.sb_agblocks);
623 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
624#endif
625 ASSERT(xfs_bmap_free_item_zone != NULL);
626 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
627 new->xbfi_startblock = bno;
628 new->xbfi_blockcount = (xfs_extlen_t)len;
629 for (prev = NULL, cur = flist->xbf_first;
630 cur != NULL;
631 prev = cur, cur = cur->xbfi_next) {
632 if (cur->xbfi_startblock >= bno)
633 break;
634 }
635 if (prev)
636 prev->xbfi_next = new;
637 else
638 flist->xbf_first = new;
639 new->xbfi_next = cur;
640 flist->xbf_count++;
641}
642
643/*
644 * Remove the entry "free" from the free item list. Prev points to the
645 * previous entry, unless "free" is the head of the list.
646 */
Dave Chinner68988112013-08-12 20:49:42 +1000647void
Dave Chinner9e5987a2013-02-25 12:31:26 +1100648xfs_bmap_del_free(
649 xfs_bmap_free_t *flist, /* free item list header */
650 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
651 xfs_bmap_free_item_t *free) /* list item to be freed */
652{
653 if (prev)
654 prev->xbfi_next = free->xbfi_next;
655 else
656 flist->xbf_first = free->xbfi_next;
657 flist->xbf_count--;
658 kmem_zone_free(xfs_bmap_free_item_zone, free);
659}
660
Dave Chinner9e5987a2013-02-25 12:31:26 +1100661/*
662 * Free up any items left in the list.
663 */
664void
665xfs_bmap_cancel(
666 xfs_bmap_free_t *flist) /* list of bmap_free_items */
667{
668 xfs_bmap_free_item_t *free; /* free list item */
669 xfs_bmap_free_item_t *next;
670
671 if (flist->xbf_count == 0)
672 return;
673 ASSERT(flist->xbf_first != NULL);
674 for (free = flist->xbf_first; free; free = next) {
675 next = free->xbfi_next;
676 xfs_bmap_del_free(flist, NULL, free);
677 }
678 ASSERT(flist->xbf_count == 0);
679}
680
681/*
682 * Inode fork format manipulation functions
683 */
684
685/*
686 * Transform a btree format file with only one leaf node, where the
687 * extents list will fit in the inode, into an extents format file.
688 * Since the file extents are already in-core, all we have to do is
689 * give up the space for the btree root and pitch the leaf block.
690 */
691STATIC int /* error */
692xfs_bmap_btree_to_extents(
693 xfs_trans_t *tp, /* transaction pointer */
694 xfs_inode_t *ip, /* incore inode pointer */
695 xfs_btree_cur_t *cur, /* btree cursor */
696 int *logflagsp, /* inode logging flags */
697 int whichfork) /* data or attr fork */
698{
699 /* REFERENCED */
700 struct xfs_btree_block *cblock;/* child btree block */
701 xfs_fsblock_t cbno; /* child block number */
702 xfs_buf_t *cbp; /* child block's buffer */
703 int error; /* error return value */
704 xfs_ifork_t *ifp; /* inode fork data */
705 xfs_mount_t *mp; /* mount point structure */
706 __be64 *pp; /* ptr to block address */
707 struct xfs_btree_block *rblock;/* root btree block */
708
709 mp = ip->i_mount;
710 ifp = XFS_IFORK_PTR(ip, whichfork);
711 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
712 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
713 rblock = ifp->if_broot;
714 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
715 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
716 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
717 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
718 cbno = be64_to_cpu(*pp);
719 *logflagsp = 0;
720#ifdef DEBUG
721 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
722 return error;
723#endif
724 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
725 &xfs_bmbt_buf_ops);
726 if (error)
727 return error;
728 cblock = XFS_BUF_TO_BLOCK(cbp);
729 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
730 return error;
731 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
732 ip->i_d.di_nblocks--;
733 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
734 xfs_trans_binval(tp, cbp);
735 if (cur->bc_bufs[0] == cbp)
736 cur->bc_bufs[0] = NULL;
737 xfs_iroot_realloc(ip, -1, whichfork);
738 ASSERT(ifp->if_broot == NULL);
739 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
740 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
741 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
742 return 0;
743}
744
745/*
746 * Convert an extents-format file into a btree-format file.
747 * The new file will have a root block (in the inode) and a single child block.
748 */
749STATIC int /* error */
750xfs_bmap_extents_to_btree(
751 xfs_trans_t *tp, /* transaction pointer */
752 xfs_inode_t *ip, /* incore inode pointer */
753 xfs_fsblock_t *firstblock, /* first-block-allocated */
754 xfs_bmap_free_t *flist, /* blocks freed in xaction */
755 xfs_btree_cur_t **curp, /* cursor returned to caller */
756 int wasdel, /* converting a delayed alloc */
757 int *logflagsp, /* inode logging flags */
758 int whichfork) /* data or attr fork */
759{
760 struct xfs_btree_block *ablock; /* allocated (child) bt block */
761 xfs_buf_t *abp; /* buffer for ablock */
762 xfs_alloc_arg_t args; /* allocation arguments */
763 xfs_bmbt_rec_t *arp; /* child record pointer */
764 struct xfs_btree_block *block; /* btree root block */
765 xfs_btree_cur_t *cur; /* bmap btree cursor */
766 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
767 int error; /* error return value */
768 xfs_extnum_t i, cnt; /* extent record index */
769 xfs_ifork_t *ifp; /* inode fork pointer */
770 xfs_bmbt_key_t *kp; /* root block key pointer */
771 xfs_mount_t *mp; /* mount structure */
772 xfs_extnum_t nextents; /* number of file extents */
773 xfs_bmbt_ptr_t *pp; /* root block address pointer */
774
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500775 mp = ip->i_mount;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100776 ifp = XFS_IFORK_PTR(ip, whichfork);
777 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
778
779 /*
780 * Make space in the inode incore.
781 */
782 xfs_iroot_realloc(ip, 1, whichfork);
783 ifp->if_flags |= XFS_IFBROOT;
784
785 /*
786 * Fill in the root.
787 */
788 block = ifp->if_broot;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500789 if (xfs_sb_version_hascrc(&mp->m_sb))
790 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
791 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
792 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
793 else
794 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
795 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
796 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100797
798 /*
799 * Need a cursor. Can't allocate until bb_level is filled in.
800 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100801 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
802 cur->bc_private.b.firstblock = *firstblock;
803 cur->bc_private.b.flist = flist;
804 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
805 /*
806 * Convert to a btree with two levels, one record in root.
807 */
808 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
809 memset(&args, 0, sizeof(args));
810 args.tp = tp;
811 args.mp = mp;
812 args.firstblock = *firstblock;
813 if (*firstblock == NULLFSBLOCK) {
814 args.type = XFS_ALLOCTYPE_START_BNO;
815 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
816 } else if (flist->xbf_low) {
817 args.type = XFS_ALLOCTYPE_START_BNO;
818 args.fsbno = *firstblock;
819 } else {
820 args.type = XFS_ALLOCTYPE_NEAR_BNO;
821 args.fsbno = *firstblock;
822 }
823 args.minlen = args.maxlen = args.prod = 1;
824 args.wasdel = wasdel;
825 *logflagsp = 0;
826 if ((error = xfs_alloc_vextent(&args))) {
827 xfs_iroot_realloc(ip, -1, whichfork);
828 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
829 return error;
830 }
831 /*
832 * Allocation can't fail, the space was reserved.
833 */
834 ASSERT(args.fsbno != NULLFSBLOCK);
835 ASSERT(*firstblock == NULLFSBLOCK ||
836 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
837 (flist->xbf_low &&
838 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
839 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
840 cur->bc_private.b.allocated++;
841 ip->i_d.di_nblocks++;
842 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
843 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
844 /*
845 * Fill in the child block.
846 */
847 abp->b_ops = &xfs_bmbt_buf_ops;
848 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500849 if (xfs_sb_version_hascrc(&mp->m_sb))
850 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
851 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
852 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
853 else
854 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
855 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
856 XFS_BTREE_LONG_PTRS);
857
Dave Chinner9e5987a2013-02-25 12:31:26 +1100858 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
859 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
860 for (cnt = i = 0; i < nextents; i++) {
861 ep = xfs_iext_get_ext(ifp, i);
862 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
863 arp->l0 = cpu_to_be64(ep->l0);
864 arp->l1 = cpu_to_be64(ep->l1);
865 arp++; cnt++;
866 }
867 }
868 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
869 xfs_btree_set_numrecs(ablock, cnt);
870
871 /*
872 * Fill in the root key and pointer.
873 */
874 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
875 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
876 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
877 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
878 be16_to_cpu(block->bb_level)));
879 *pp = cpu_to_be64(args.fsbno);
880
881 /*
882 * Do all this logging at the end so that
883 * the root is at the right level.
884 */
885 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
886 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
887 ASSERT(*curp == NULL);
888 *curp = cur;
889 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
890 return 0;
891}
892
893/*
894 * Convert a local file to an extents file.
895 * This code is out of bounds for data forks of regular files,
896 * since the file data needs to get logged so things will stay consistent.
897 * (The bmap-level manipulations are ok, though).
898 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000899void
900xfs_bmap_local_to_extents_empty(
901 struct xfs_inode *ip,
902 int whichfork)
903{
904 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
905
906 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
907 ASSERT(ifp->if_bytes == 0);
908 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
909
910 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
911 ifp->if_flags &= ~XFS_IFINLINE;
912 ifp->if_flags |= XFS_IFEXTENTS;
913 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
914}
915
916
Dave Chinner9e5987a2013-02-25 12:31:26 +1100917STATIC int /* error */
918xfs_bmap_local_to_extents(
919 xfs_trans_t *tp, /* transaction pointer */
920 xfs_inode_t *ip, /* incore inode pointer */
921 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
922 xfs_extlen_t total, /* total blocks needed by transaction */
923 int *logflagsp, /* inode logging flags */
924 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500925 void (*init_fn)(struct xfs_trans *tp,
926 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100927 struct xfs_inode *ip,
928 struct xfs_ifork *ifp))
929{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000930 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100931 int flags; /* logging flags returned */
932 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000933 xfs_alloc_arg_t args; /* allocation arguments */
934 xfs_buf_t *bp; /* buffer for extent block */
935 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100936
937 /*
938 * We don't want to deal with the case of keeping inode data inline yet.
939 * So sending the data fork of a regular inode is invalid.
940 */
941 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
942 ifp = XFS_IFORK_PTR(ip, whichfork);
943 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000944
945 if (!ifp->if_bytes) {
946 xfs_bmap_local_to_extents_empty(ip, whichfork);
947 flags = XFS_ILOG_CORE;
948 goto done;
949 }
950
Dave Chinner9e5987a2013-02-25 12:31:26 +1100951 flags = 0;
952 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000953 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
954 XFS_IFINLINE);
955 memset(&args, 0, sizeof(args));
956 args.tp = tp;
957 args.mp = ip->i_mount;
958 args.firstblock = *firstblock;
959 /*
960 * Allocate a block. We know we need only one, since the
961 * file currently fits in an inode.
962 */
963 if (*firstblock == NULLFSBLOCK) {
964 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
965 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100966 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000967 args.fsbno = *firstblock;
968 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100969 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000970 args.total = total;
971 args.minlen = args.maxlen = args.prod = 1;
972 error = xfs_alloc_vextent(&args);
973 if (error)
974 goto done;
975
976 /* Can't fail, the space was reserved. */
977 ASSERT(args.fsbno != NULLFSBLOCK);
978 ASSERT(args.len == 1);
979 *firstblock = args.fsbno;
980 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
981
982 /* initialise the block and copy the data */
983 init_fn(tp, bp, ip, ifp);
984
985 /* account for the change in fork size and log everything */
986 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
987 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
988 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100989 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000990
991 xfs_iext_add(ifp, 0, 1);
992 ep = xfs_iext_get_ext(ifp, 0);
993 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
994 trace_xfs_bmap_post_update(ip, 0,
995 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
996 _THIS_IP_);
997 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
998 ip->i_d.di_nblocks = 1;
999 xfs_trans_mod_dquot_byino(tp, ip,
1000 XFS_TRANS_DQ_BCOUNT, 1L);
1001 flags |= xfs_ilog_fext(whichfork);
1002
Dave Chinner9e5987a2013-02-25 12:31:26 +11001003done:
1004 *logflagsp = flags;
1005 return error;
1006}
1007
1008/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 * Called from xfs_bmap_add_attrfork to handle btree format files.
1010 */
1011STATIC int /* error */
1012xfs_bmap_add_attrfork_btree(
1013 xfs_trans_t *tp, /* transaction pointer */
1014 xfs_inode_t *ip, /* incore inode pointer */
1015 xfs_fsblock_t *firstblock, /* first block allocated */
1016 xfs_bmap_free_t *flist, /* blocks to free at commit */
1017 int *flags) /* inode logging flags */
1018{
1019 xfs_btree_cur_t *cur; /* btree cursor */
1020 int error; /* error return value */
1021 xfs_mount_t *mp; /* file system mount struct */
1022 int stat; /* newroot status */
1023
1024 mp = ip->i_mount;
1025 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1026 *flags |= XFS_ILOG_DBROOT;
1027 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001028 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 cur->bc_private.b.flist = flist;
1030 cur->bc_private.b.firstblock = *firstblock;
1031 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1032 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001033 /* must be at least one entry */
1034 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001035 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 goto error0;
1037 if (stat == 0) {
1038 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1039 return XFS_ERROR(ENOSPC);
1040 }
1041 *firstblock = cur->bc_private.b.firstblock;
1042 cur->bc_private.b.allocated = 0;
1043 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1044 }
1045 return 0;
1046error0:
1047 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1048 return error;
1049}
1050
1051/*
1052 * Called from xfs_bmap_add_attrfork to handle extents format files.
1053 */
1054STATIC int /* error */
1055xfs_bmap_add_attrfork_extents(
1056 xfs_trans_t *tp, /* transaction pointer */
1057 xfs_inode_t *ip, /* incore inode pointer */
1058 xfs_fsblock_t *firstblock, /* first block allocated */
1059 xfs_bmap_free_t *flist, /* blocks to free at commit */
1060 int *flags) /* inode logging flags */
1061{
1062 xfs_btree_cur_t *cur; /* bmap btree cursor */
1063 int error; /* error return value */
1064
1065 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1066 return 0;
1067 cur = NULL;
1068 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
1069 flags, XFS_DATA_FORK);
1070 if (cur) {
1071 cur->bc_private.b.allocated = 0;
1072 xfs_btree_del_cursor(cur,
1073 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1074 }
1075 return error;
1076}
1077
1078/*
Dave Chinner1e823792013-02-11 15:58:13 +11001079 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1080 * different data fork content type needs a different callout to do the
1081 * conversion. Some are basic and only require special block initialisation
1082 * callouts for the data formating, others (directories) are so specialised they
1083 * handle everything themselves.
1084 *
1085 * XXX (dgc): investigate whether directory conversion can use the generic
1086 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001087 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 */
1089STATIC int /* error */
1090xfs_bmap_add_attrfork_local(
1091 xfs_trans_t *tp, /* transaction pointer */
1092 xfs_inode_t *ip, /* incore inode pointer */
1093 xfs_fsblock_t *firstblock, /* first block allocated */
1094 xfs_bmap_free_t *flist, /* blocks to free at commit */
1095 int *flags) /* inode logging flags */
1096{
1097 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1100 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001101
Al Viroabbede12011-07-26 02:31:30 -04001102 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 memset(&dargs, 0, sizeof(dargs));
1104 dargs.dp = ip;
1105 dargs.firstblock = firstblock;
1106 dargs.flist = flist;
Dave Chinner1e823792013-02-11 15:58:13 +11001107 dargs.total = ip->i_mount->m_dirblkfsbs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 dargs.whichfork = XFS_DATA_FORK;
1109 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001110 return xfs_dir2_sf_to_block(&dargs);
1111 }
1112
1113 if (S_ISLNK(ip->i_d.di_mode))
1114 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1115 flags, XFS_DATA_FORK,
1116 xfs_symlink_local_to_remote);
1117
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001118 /* should only be called for types that support local format data */
1119 ASSERT(0);
1120 return EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
1123/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001124 * Convert inode from non-attributed to attributed.
1125 * Must not be in a transaction, ip must not be locked.
1126 */
1127int /* error code */
1128xfs_bmap_add_attrfork(
1129 xfs_inode_t *ip, /* incore inode pointer */
1130 int size, /* space new attribute needs */
1131 int rsvd) /* xact may use reserved blks */
1132{
1133 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1134 xfs_bmap_free_t flist; /* freed extent records */
1135 xfs_mount_t *mp; /* mount structure */
1136 xfs_trans_t *tp; /* transaction pointer */
1137 int blks; /* space reservation */
1138 int version = 1; /* superblock attr version */
1139 int committed; /* xaction was committed */
1140 int logflags; /* logging flags */
1141 int error; /* error return value */
1142
1143 ASSERT(XFS_IFORK_Q(ip) == 0);
1144
1145 mp = ip->i_mount;
1146 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1147 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
1148 blks = XFS_ADDAFORK_SPACE_RES(mp);
1149 if (rsvd)
1150 tp->t_flags |= XFS_TRANS_RESERVE;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001151 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_addafork, blks, 0);
1152 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001153 goto error0;
1154 xfs_ilock(ip, XFS_ILOCK_EXCL);
1155 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1156 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1157 XFS_QMOPT_RES_REGBLKS);
1158 if (error) {
1159 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1160 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
1161 return error;
1162 }
1163 if (XFS_IFORK_Q(ip))
1164 goto error1;
1165 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1166 /*
1167 * For inodes coming from pre-6.2 filesystems.
1168 */
1169 ASSERT(ip->i_d.di_aformat == 0);
1170 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1171 }
1172 ASSERT(ip->i_d.di_anextents == 0);
1173
1174 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1175 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1176
1177 switch (ip->i_d.di_format) {
1178 case XFS_DINODE_FMT_DEV:
1179 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1180 break;
1181 case XFS_DINODE_FMT_UUID:
1182 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1183 break;
1184 case XFS_DINODE_FMT_LOCAL:
1185 case XFS_DINODE_FMT_EXTENTS:
1186 case XFS_DINODE_FMT_BTREE:
1187 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1188 if (!ip->i_d.di_forkoff)
1189 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1190 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1191 version = 2;
1192 break;
1193 default:
1194 ASSERT(0);
1195 error = XFS_ERROR(EINVAL);
1196 goto error1;
1197 }
1198
1199 ASSERT(ip->i_afp == NULL);
1200 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1201 ip->i_afp->if_flags = XFS_IFEXTENTS;
1202 logflags = 0;
1203 xfs_bmap_init(&flist, &firstblock);
1204 switch (ip->i_d.di_format) {
1205 case XFS_DINODE_FMT_LOCAL:
1206 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
1207 &logflags);
1208 break;
1209 case XFS_DINODE_FMT_EXTENTS:
1210 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1211 &flist, &logflags);
1212 break;
1213 case XFS_DINODE_FMT_BTREE:
1214 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
1215 &logflags);
1216 break;
1217 default:
1218 error = 0;
1219 break;
1220 }
1221 if (logflags)
1222 xfs_trans_log_inode(tp, ip, logflags);
1223 if (error)
1224 goto error2;
1225 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1226 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
1227 __int64_t sbfields = 0;
1228
1229 spin_lock(&mp->m_sb_lock);
1230 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1231 xfs_sb_version_addattr(&mp->m_sb);
1232 sbfields |= XFS_SB_VERSIONNUM;
1233 }
1234 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1235 xfs_sb_version_addattr2(&mp->m_sb);
1236 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
1237 }
1238 if (sbfields) {
1239 spin_unlock(&mp->m_sb_lock);
1240 xfs_mod_sb(tp, sbfields);
1241 } else
1242 spin_unlock(&mp->m_sb_lock);
1243 }
1244
1245 error = xfs_bmap_finish(&tp, &flist, &committed);
1246 if (error)
1247 goto error2;
1248 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1249error2:
1250 xfs_bmap_cancel(&flist);
1251error1:
1252 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1253error0:
1254 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1255 return error;
1256}
1257
1258/*
1259 * Internal and external extent tree search functions.
1260 */
1261
1262/*
1263 * Read in the extents to if_extents.
1264 * All inode fields are set up by caller, we just traverse the btree
1265 * and copy the records in. If the file system cannot contain unwritten
1266 * extents, the records are checked for no "state" flags.
1267 */
1268int /* error */
1269xfs_bmap_read_extents(
1270 xfs_trans_t *tp, /* transaction pointer */
1271 xfs_inode_t *ip, /* incore inode */
1272 int whichfork) /* data or attr fork */
1273{
1274 struct xfs_btree_block *block; /* current btree block */
1275 xfs_fsblock_t bno; /* block # of "block" */
1276 xfs_buf_t *bp; /* buffer for "block" */
1277 int error; /* error return value */
1278 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1279 xfs_extnum_t i, j; /* index into the extents list */
1280 xfs_ifork_t *ifp; /* fork structure */
1281 int level; /* btree level, for checking */
1282 xfs_mount_t *mp; /* file system mount structure */
1283 __be64 *pp; /* pointer to block address */
1284 /* REFERENCED */
1285 xfs_extnum_t room; /* number of entries there's room for */
1286
1287 bno = NULLFSBLOCK;
1288 mp = ip->i_mount;
1289 ifp = XFS_IFORK_PTR(ip, whichfork);
1290 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1291 XFS_EXTFMT_INODE(ip);
1292 block = ifp->if_broot;
1293 /*
1294 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1295 */
1296 level = be16_to_cpu(block->bb_level);
1297 ASSERT(level > 0);
1298 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1299 bno = be64_to_cpu(*pp);
1300 ASSERT(bno != NULLDFSBNO);
1301 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1302 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1303 /*
1304 * Go down the tree until leaf level is reached, following the first
1305 * pointer (leftmost) at each level.
1306 */
1307 while (level-- > 0) {
1308 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1309 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1310 if (error)
1311 return error;
1312 block = XFS_BUF_TO_BLOCK(bp);
1313 XFS_WANT_CORRUPTED_GOTO(
1314 xfs_bmap_sanity_check(mp, bp, level),
1315 error0);
1316 if (level == 0)
1317 break;
1318 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1319 bno = be64_to_cpu(*pp);
1320 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
1321 xfs_trans_brelse(tp, bp);
1322 }
1323 /*
1324 * Here with bp and block set to the leftmost leaf node in the tree.
1325 */
1326 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1327 i = 0;
1328 /*
1329 * Loop over all leaf nodes. Copy information to the extent records.
1330 */
1331 for (;;) {
1332 xfs_bmbt_rec_t *frp;
1333 xfs_fsblock_t nextbno;
1334 xfs_extnum_t num_recs;
1335 xfs_extnum_t start;
1336
1337 num_recs = xfs_btree_get_numrecs(block);
1338 if (unlikely(i + num_recs > room)) {
1339 ASSERT(i + num_recs <= room);
1340 xfs_warn(ip->i_mount,
1341 "corrupt dinode %Lu, (btree extents).",
1342 (unsigned long long) ip->i_ino);
1343 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1344 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1345 goto error0;
1346 }
1347 XFS_WANT_CORRUPTED_GOTO(
1348 xfs_bmap_sanity_check(mp, bp, 0),
1349 error0);
1350 /*
1351 * Read-ahead the next leaf block, if any.
1352 */
1353 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1354 if (nextbno != NULLFSBLOCK)
1355 xfs_btree_reada_bufl(mp, nextbno, 1,
1356 &xfs_bmbt_buf_ops);
1357 /*
1358 * Copy records into the extent records.
1359 */
1360 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1361 start = i;
1362 for (j = 0; j < num_recs; j++, i++, frp++) {
1363 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1364 trp->l0 = be64_to_cpu(frp->l0);
1365 trp->l1 = be64_to_cpu(frp->l1);
1366 }
1367 if (exntf == XFS_EXTFMT_NOSTATE) {
1368 /*
1369 * Check all attribute bmap btree records and
1370 * any "older" data bmap btree records for a
1371 * set bit in the "extent flag" position.
1372 */
1373 if (unlikely(xfs_check_nostate_extents(ifp,
1374 start, num_recs))) {
1375 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1376 XFS_ERRLEVEL_LOW,
1377 ip->i_mount);
1378 goto error0;
1379 }
1380 }
1381 xfs_trans_brelse(tp, bp);
1382 bno = nextbno;
1383 /*
1384 * If we've reached the end, stop.
1385 */
1386 if (bno == NULLFSBLOCK)
1387 break;
1388 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1389 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1390 if (error)
1391 return error;
1392 block = XFS_BUF_TO_BLOCK(bp);
1393 }
1394 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1395 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1396 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1397 return 0;
1398error0:
1399 xfs_trans_brelse(tp, bp);
1400 return XFS_ERROR(EFSCORRUPTED);
1401}
1402
1403
1404/*
1405 * Search the extent records for the entry containing block bno.
1406 * If bno lies in a hole, point to the next entry. If bno lies
1407 * past eof, *eofp will be set, and *prevp will contain the last
1408 * entry (null if none). Else, *lastxp will be set to the index
1409 * of the found entry; *gotp will contain the entry.
1410 */
1411STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1412xfs_bmap_search_multi_extents(
1413 xfs_ifork_t *ifp, /* inode fork pointer */
1414 xfs_fileoff_t bno, /* block number searched for */
1415 int *eofp, /* out: end of file found */
1416 xfs_extnum_t *lastxp, /* out: last extent index */
1417 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1418 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1419{
1420 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1421 xfs_extnum_t lastx; /* last extent index */
1422
1423 /*
1424 * Initialize the extent entry structure to catch access to
1425 * uninitialized br_startblock field.
1426 */
1427 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1428 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1429 gotp->br_state = XFS_EXT_INVALID;
1430#if XFS_BIG_BLKNOS
1431 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
1432#else
1433 gotp->br_startblock = 0xffffa5a5;
1434#endif
1435 prevp->br_startoff = NULLFILEOFF;
1436
1437 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1438 if (lastx > 0) {
1439 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1440 }
1441 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1442 xfs_bmbt_get_all(ep, gotp);
1443 *eofp = 0;
1444 } else {
1445 if (lastx > 0) {
1446 *gotp = *prevp;
1447 }
1448 *eofp = 1;
1449 ep = NULL;
1450 }
1451 *lastxp = lastx;
1452 return ep;
1453}
1454
1455/*
1456 * Search the extents list for the inode, for the extent containing bno.
1457 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1458 * *eofp will be set, and *prevp will contain the last entry (null if none).
1459 * Else, *lastxp will be set to the index of the found
1460 * entry; *gotp will contain the entry.
1461 */
1462STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1463xfs_bmap_search_extents(
1464 xfs_inode_t *ip, /* incore inode pointer */
1465 xfs_fileoff_t bno, /* block number searched for */
1466 int fork, /* data or attr fork */
1467 int *eofp, /* out: end of file found */
1468 xfs_extnum_t *lastxp, /* out: last extent index */
1469 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1470 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1471{
1472 xfs_ifork_t *ifp; /* inode fork pointer */
1473 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1474
1475 XFS_STATS_INC(xs_look_exlist);
1476 ifp = XFS_IFORK_PTR(ip, fork);
1477
1478 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1479
1480 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1481 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1482 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1483 "Access to block zero in inode %llu "
1484 "start_block: %llx start_off: %llx "
1485 "blkcnt: %llx extent-state: %x lastx: %x\n",
1486 (unsigned long long)ip->i_ino,
1487 (unsigned long long)gotp->br_startblock,
1488 (unsigned long long)gotp->br_startoff,
1489 (unsigned long long)gotp->br_blockcount,
1490 gotp->br_state, *lastxp);
1491 *lastxp = NULLEXTNUM;
1492 *eofp = 1;
1493 return NULL;
1494 }
1495 return ep;
1496}
1497
1498/*
1499 * Returns the file-relative block number of the first unused block(s)
1500 * in the file with at least "len" logically contiguous blocks free.
1501 * This is the lowest-address hole if the file has holes, else the first block
1502 * past the end of file.
1503 * Return 0 if the file is currently local (in-inode).
1504 */
1505int /* error */
1506xfs_bmap_first_unused(
1507 xfs_trans_t *tp, /* transaction pointer */
1508 xfs_inode_t *ip, /* incore inode */
1509 xfs_extlen_t len, /* size of hole to find */
1510 xfs_fileoff_t *first_unused, /* unused block */
1511 int whichfork) /* data or attr fork */
1512{
1513 int error; /* error return value */
1514 int idx; /* extent record index */
1515 xfs_ifork_t *ifp; /* inode fork pointer */
1516 xfs_fileoff_t lastaddr; /* last block number seen */
1517 xfs_fileoff_t lowest; /* lowest useful block */
1518 xfs_fileoff_t max; /* starting useful block */
1519 xfs_fileoff_t off; /* offset for this block */
1520 xfs_extnum_t nextents; /* number of extent entries */
1521
1522 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1523 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1524 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1525 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1526 *first_unused = 0;
1527 return 0;
1528 }
1529 ifp = XFS_IFORK_PTR(ip, whichfork);
1530 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1531 (error = xfs_iread_extents(tp, ip, whichfork)))
1532 return error;
1533 lowest = *first_unused;
1534 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1535 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1536 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1537 off = xfs_bmbt_get_startoff(ep);
1538 /*
1539 * See if the hole before this extent will work.
1540 */
1541 if (off >= lowest + len && off - max >= len) {
1542 *first_unused = max;
1543 return 0;
1544 }
1545 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1546 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1547 }
1548 *first_unused = max;
1549 return 0;
1550}
1551
1552/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001553 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001554 * last_block (input value) in the file.
1555 * This is not based on i_size, it is based on the extent records.
1556 * Returns 0 for local files, as they do not have extent records.
1557 */
1558int /* error */
1559xfs_bmap_last_before(
1560 xfs_trans_t *tp, /* transaction pointer */
1561 xfs_inode_t *ip, /* incore inode */
1562 xfs_fileoff_t *last_block, /* last block */
1563 int whichfork) /* data or attr fork */
1564{
1565 xfs_fileoff_t bno; /* input file offset */
1566 int eof; /* hit end of file */
1567 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1568 int error; /* error return value */
1569 xfs_bmbt_irec_t got; /* current extent value */
1570 xfs_ifork_t *ifp; /* inode fork pointer */
1571 xfs_extnum_t lastx; /* last extent used */
1572 xfs_bmbt_irec_t prev; /* previous extent value */
1573
1574 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1575 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1576 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
1577 return XFS_ERROR(EIO);
1578 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1579 *last_block = 0;
1580 return 0;
1581 }
1582 ifp = XFS_IFORK_PTR(ip, whichfork);
1583 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1584 (error = xfs_iread_extents(tp, ip, whichfork)))
1585 return error;
1586 bno = *last_block - 1;
1587 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1588 &prev);
1589 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1590 if (prev.br_startoff == NULLFILEOFF)
1591 *last_block = 0;
1592 else
1593 *last_block = prev.br_startoff + prev.br_blockcount;
1594 }
1595 /*
1596 * Otherwise *last_block is already the right answer.
1597 */
1598 return 0;
1599}
1600
Dave Chinner68988112013-08-12 20:49:42 +10001601int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001602xfs_bmap_last_extent(
1603 struct xfs_trans *tp,
1604 struct xfs_inode *ip,
1605 int whichfork,
1606 struct xfs_bmbt_irec *rec,
1607 int *is_empty)
1608{
1609 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1610 int error;
1611 int nextents;
1612
1613 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1614 error = xfs_iread_extents(tp, ip, whichfork);
1615 if (error)
1616 return error;
1617 }
1618
1619 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1620 if (nextents == 0) {
1621 *is_empty = 1;
1622 return 0;
1623 }
1624
1625 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1626 *is_empty = 0;
1627 return 0;
1628}
1629
1630/*
1631 * Check the last inode extent to determine whether this allocation will result
1632 * in blocks being allocated at the end of the file. When we allocate new data
1633 * blocks at the end of the file which do not start at the previous data block,
1634 * we will try to align the new blocks at stripe unit boundaries.
1635 *
1636 * Returns 0 in bma->aeof if the file (fork) is empty as any new write will be
1637 * at, or past the EOF.
1638 */
1639STATIC int
1640xfs_bmap_isaeof(
1641 struct xfs_bmalloca *bma,
1642 int whichfork)
1643{
1644 struct xfs_bmbt_irec rec;
1645 int is_empty;
1646 int error;
1647
1648 bma->aeof = 0;
1649 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1650 &is_empty);
1651 if (error || is_empty)
1652 return error;
1653
1654 /*
1655 * Check if we are allocation or past the last extent, or at least into
1656 * the last delayed allocated extent.
1657 */
1658 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1659 (bma->offset >= rec.br_startoff &&
1660 isnullstartblock(rec.br_startblock));
1661 return 0;
1662}
1663
1664/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001665 * Returns the file-relative block number of the first block past eof in
1666 * the file. This is not based on i_size, it is based on the extent records.
1667 * Returns 0 for local files, as they do not have extent records.
1668 */
1669int
1670xfs_bmap_last_offset(
1671 struct xfs_trans *tp,
1672 struct xfs_inode *ip,
1673 xfs_fileoff_t *last_block,
1674 int whichfork)
1675{
1676 struct xfs_bmbt_irec rec;
1677 int is_empty;
1678 int error;
1679
1680 *last_block = 0;
1681
1682 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1683 return 0;
1684
1685 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1686 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1687 return XFS_ERROR(EIO);
1688
1689 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1690 if (error || is_empty)
1691 return error;
1692
1693 *last_block = rec.br_startoff + rec.br_blockcount;
1694 return 0;
1695}
1696
1697/*
1698 * Returns whether the selected fork of the inode has exactly one
1699 * block or not. For the data fork we check this matches di_size,
1700 * implying the file's range is 0..bsize-1.
1701 */
1702int /* 1=>1 block, 0=>otherwise */
1703xfs_bmap_one_block(
1704 xfs_inode_t *ip, /* incore inode */
1705 int whichfork) /* data or attr fork */
1706{
1707 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1708 xfs_ifork_t *ifp; /* inode fork pointer */
1709 int rval; /* return value */
1710 xfs_bmbt_irec_t s; /* internal version of extent */
1711
1712#ifndef DEBUG
1713 if (whichfork == XFS_DATA_FORK)
1714 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1715#endif /* !DEBUG */
1716 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1717 return 0;
1718 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1719 return 0;
1720 ifp = XFS_IFORK_PTR(ip, whichfork);
1721 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1722 ep = xfs_iext_get_ext(ifp, 0);
1723 xfs_bmbt_get_all(ep, &s);
1724 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1725 if (rval && whichfork == XFS_DATA_FORK)
1726 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1727 return rval;
1728}
1729
1730/*
1731 * Extent tree manipulation functions used during allocation.
1732 */
1733
1734/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001735 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
1737STATIC int /* error */
1738xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001739 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001741 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001743 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001746 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 xfs_fileoff_t new_endoff; /* end offset of new entry */
1748 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1749 /* left is 0, right is 1, prev is 2 */
1750 int rval=0; /* return value (logging flags) */
1751 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001752 xfs_filblks_t da_new; /* new count del alloc blocks used */
1753 xfs_filblks_t da_old; /* old count del alloc blocks used */
1754 xfs_filblks_t temp=0; /* value for da_new calculations */
1755 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 int tmp_rval; /* partial logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001758 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001759
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001760 ASSERT(bma->idx >= 0);
1761 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001762 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001763 ASSERT(!bma->cur ||
1764 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001765
1766 XFS_STATS_INC(xs_add_exlist);
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768#define LEFT r[0]
1769#define RIGHT r[1]
1770#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 /*
1773 * Set up a bunch of variables to make the tests simpler.
1774 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001775 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 xfs_bmbt_get_all(ep, &PREV);
1777 new_endoff = new->br_startoff + new->br_blockcount;
1778 ASSERT(PREV.br_startoff <= new->br_startoff);
1779 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001780
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001781 da_old = startblockval(PREV.br_startblock);
1782 da_new = 0;
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 /*
1785 * Set flags determining what part of the previous delayed allocation
1786 * extent is being replaced by a real allocation.
1787 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001788 if (PREV.br_startoff == new->br_startoff)
1789 state |= BMAP_LEFT_FILLING;
1790 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1791 state |= BMAP_RIGHT_FILLING;
1792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 /*
1794 * Check and set flags if this segment has a left neighbor.
1795 * Don't set contiguous if the combined extent would be too large.
1796 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001797 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001798 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001799 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001800
1801 if (isnullstartblock(LEFT.br_startblock))
1802 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001804
1805 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1806 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1807 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1808 LEFT.br_state == new->br_state &&
1809 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1810 state |= BMAP_LEFT_CONTIG;
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 /*
1813 * Check and set flags if this segment has a right neighbor.
1814 * Don't set contiguous if the combined extent would be too large.
1815 * Also check for all-three-contiguous being too large.
1816 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001817 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001818 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001820
1821 if (isnullstartblock(RIGHT.br_startblock))
1822 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001824
1825 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1826 new_endoff == RIGHT.br_startoff &&
1827 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1828 new->br_state == RIGHT.br_state &&
1829 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1830 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1831 BMAP_RIGHT_FILLING)) !=
1832 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1833 BMAP_RIGHT_FILLING) ||
1834 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1835 <= MAXEXTLEN))
1836 state |= BMAP_RIGHT_CONTIG;
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 error = 0;
1839 /*
1840 * Switch out based on the FILLING and CONTIG state bits.
1841 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001842 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1843 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1844 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1845 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 /*
1847 * Filling in all of a previously delayed allocation extent.
1848 * The left and right neighbors are both contiguous with new.
1849 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001850 bma->idx--;
1851 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1852 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 LEFT.br_blockcount + PREV.br_blockcount +
1854 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001855 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001856
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001857 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1858 bma->ip->i_d.di_nextents--;
1859 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1861 else {
1862 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001863 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001865 RIGHT.br_blockcount, &i);
1866 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001868 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001869 error = xfs_btree_delete(bma->cur, &i);
1870 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001872 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001873 error = xfs_btree_decrement(bma->cur, 0, &i);
1874 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001876 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001877 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 LEFT.br_startblock,
1879 LEFT.br_blockcount +
1880 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001881 RIGHT.br_blockcount, LEFT.br_state);
1882 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 goto done;
1884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 break;
1886
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001887 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /*
1889 * Filling in all of a previously delayed allocation extent.
1890 * The left neighbor is contiguous, the right is not.
1891 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001892 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001893
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001894 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1895 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001896 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001897 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001898
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001899 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1900 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 rval = XFS_ILOG_DEXT;
1902 else {
1903 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001904 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001906 &i);
1907 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001909 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001910 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 LEFT.br_startblock,
1912 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001913 PREV.br_blockcount, LEFT.br_state);
1914 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 goto done;
1916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 break;
1918
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001919 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 /*
1921 * Filling in all of a previously delayed allocation extent.
1922 * The right neighbor is contiguous, the left is not.
1923 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001924 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 xfs_bmbt_set_startblock(ep, new->br_startblock);
1926 xfs_bmbt_set_blockcount(ep,
1927 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001928 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001929
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001930 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1931 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 rval = XFS_ILOG_DEXT;
1933 else {
1934 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001935 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001937 RIGHT.br_blockcount, &i);
1938 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001940 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001941 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 new->br_startblock,
1943 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001944 RIGHT.br_blockcount, PREV.br_state);
1945 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 goto done;
1947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 break;
1949
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001950 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /*
1952 * Filling in all of a previously delayed allocation extent.
1953 * Neither the left nor right neighbors are contiguous with
1954 * the new one.
1955 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001956 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001959
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001960 bma->ip->i_d.di_nextents++;
1961 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1963 else {
1964 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001965 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001967 &i);
1968 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001970 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001971 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1972 error = xfs_btree_insert(bma->cur, &i);
1973 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001975 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 break;
1978
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001979 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 /*
1981 * Filling in the first part of a previous delayed allocation.
1982 * The left neighbor is contiguous.
1983 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001984 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1985 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 LEFT.br_blockcount + new->br_blockcount);
1987 xfs_bmbt_set_startoff(ep,
1988 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001989 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001990
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001992 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001994 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 rval = XFS_ILOG_DEXT;
1996 else {
1997 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001998 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002000 &i);
2001 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002003 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002004 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 LEFT.br_startblock,
2006 LEFT.br_blockcount +
2007 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002008 LEFT.br_state);
2009 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 goto done;
2011 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002012 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002013 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002014 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002015 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002016
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002017 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 break;
2019
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002020 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 /*
2022 * Filling in the first part of a previous delayed allocation.
2023 * The left neighbor is not contiguous.
2024 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002025 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 xfs_bmbt_set_startoff(ep, new_endoff);
2027 temp = PREV.br_blockcount - new->br_blockcount;
2028 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002029 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
2030 bma->ip->i_d.di_nextents++;
2031 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2033 else {
2034 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002035 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002037 &i);
2038 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002040 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002041 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2042 error = xfs_btree_insert(bma->cur, &i);
2043 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002045 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002047
2048 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002049 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2050 bma->firstblock, bma->flist,
2051 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 rval |= tmp_rval;
2053 if (error)
2054 goto done;
2055 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002056 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002057 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002058 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2059 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002060 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002061 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 break;
2063
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002064 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 /*
2066 * Filling in the last part of a previous delayed allocation.
2067 * The right neighbor is contiguous with the new allocation.
2068 */
2069 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002070 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002072 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002073 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 new->br_blockcount + RIGHT.br_blockcount,
2075 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002076 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2077 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 rval = XFS_ILOG_DEXT;
2079 else {
2080 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002081 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002083 RIGHT.br_blockcount, &i);
2084 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002086 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002087 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 new->br_startblock,
2089 new->br_blockcount +
2090 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002091 RIGHT.br_state);
2092 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 goto done;
2094 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002095
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002096 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002097 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002098 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002099 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002100 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002101
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002102 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 break;
2104
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002105 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /*
2107 * Filling in the last part of a previous delayed allocation.
2108 * The right neighbor is not contiguous.
2109 */
2110 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002111 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002113 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2114 bma->ip->i_d.di_nextents++;
2115 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2117 else {
2118 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002119 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002121 &i);
2122 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002124 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002125 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2126 error = xfs_btree_insert(bma->cur, &i);
2127 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002129 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002131
2132 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002133 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2134 bma->firstblock, bma->flist, &bma->cur, 1,
2135 &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 rval |= tmp_rval;
2137 if (error)
2138 goto done;
2139 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002140 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002141 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002142 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2143 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002144 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002145 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002146
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002147 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 break;
2149
2150 case 0:
2151 /*
2152 * Filling in the middle part of a previous delayed allocation.
2153 * Contiguity is impossible here.
2154 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002155 *
2156 * We start with a delayed allocation:
2157 *
2158 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2159 * PREV @ idx
2160 *
2161 * and we are allocating:
2162 * +rrrrrrrrrrrrrrrrr+
2163 * new
2164 *
2165 * and we set it up for insertion as:
2166 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2167 * new
2168 * PREV @ idx LEFT RIGHT
2169 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 */
2171 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002173 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002174 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2175 LEFT = *new;
2176 RIGHT.br_state = PREV.br_state;
2177 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002178 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002179 RIGHT.br_startoff = new_endoff;
2180 RIGHT.br_blockcount = temp2;
2181 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002182 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2183 bma->ip->i_d.di_nextents++;
2184 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2186 else {
2187 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002188 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002190 &i);
2191 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002193 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002194 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2195 error = xfs_btree_insert(bma->cur, &i);
2196 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002198 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002200
2201 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002202 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2203 bma->firstblock, bma->flist, &bma->cur,
2204 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 rval |= tmp_rval;
2206 if (error)
2207 goto done;
2208 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002209 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2210 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002211 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002212 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002213 if (diff > 0) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002214 error = xfs_icsb_modify_counters(bma->ip->i_mount,
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002215 XFS_SBS_FDBLOCKS,
2216 -((int64_t)diff), 0);
2217 ASSERT(!error);
2218 if (error)
2219 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002221
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002222 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002223 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002224 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2225 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2226 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002227 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002228 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002229
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002230 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002231 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 break;
2233
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002234 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2235 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2236 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2237 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2238 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2239 case BMAP_LEFT_CONTIG:
2240 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 /*
2242 * These cases are all impossible.
2243 */
2244 ASSERT(0);
2245 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002246
2247 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002248 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002249 int tmp_logflags; /* partial log flag return val */
2250
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002251 ASSERT(bma->cur == NULL);
2252 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2253 bma->firstblock, bma->flist, &bma->cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002254 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002255 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002256 if (error)
2257 goto done;
2258 }
2259
2260 /* adjust for changes in reserved delayed indirect blocks */
2261 if (da_old || da_new) {
2262 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002263 if (bma->cur)
2264 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002265 ASSERT(temp <= da_old);
2266 if (temp < da_old)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002267 xfs_icsb_modify_counters(bma->ip->i_mount,
2268 XFS_SBS_FDBLOCKS,
2269 (int64_t)(da_old - temp), 0);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002270 }
2271
2272 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002273 if (bma->cur)
2274 bma->cur->bc_private.b.allocated = 0;
2275
2276 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002278 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 return error;
2280#undef LEFT
2281#undef RIGHT
2282#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283}
2284
2285/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002286 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 */
2288STATIC int /* error */
2289xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002290 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002292 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002294 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002295 xfs_fsblock_t *first, /* pointer to firstblock variable */
2296 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002297 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002300 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002303 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 xfs_fileoff_t new_endoff; /* end offset of new entry */
2305 xfs_exntst_t newext; /* new extent state */
2306 xfs_exntst_t oldext; /* old extent state */
2307 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2308 /* left is 0, right is 1, prev is 2 */
2309 int rval=0; /* return value (logging flags) */
2310 int state = 0;/* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002312 *logflagsp = 0;
2313
2314 cur = *curp;
2315 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2316
2317 ASSERT(*idx >= 0);
2318 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2319 ASSERT(!isnullstartblock(new->br_startblock));
2320
2321 XFS_STATS_INC(xs_add_exlist);
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323#define LEFT r[0]
2324#define RIGHT r[1]
2325#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 /*
2328 * Set up a bunch of variables to make the tests simpler.
2329 */
2330 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002331 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 xfs_bmbt_get_all(ep, &PREV);
2333 newext = new->br_state;
2334 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2335 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2336 ASSERT(PREV.br_state == oldext);
2337 new_endoff = new->br_startoff + new->br_blockcount;
2338 ASSERT(PREV.br_startoff <= new->br_startoff);
2339 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002340
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 /*
2342 * Set flags determining what part of the previous oldext allocation
2343 * extent is being replaced by a newext allocation.
2344 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002345 if (PREV.br_startoff == new->br_startoff)
2346 state |= BMAP_LEFT_FILLING;
2347 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2348 state |= BMAP_RIGHT_FILLING;
2349
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 /*
2351 * Check and set flags if this segment has a left neighbor.
2352 * Don't set contiguous if the combined extent would be too large.
2353 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002354 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002355 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002356 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002357
2358 if (isnullstartblock(LEFT.br_startblock))
2359 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002361
2362 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2363 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2364 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2365 LEFT.br_state == newext &&
2366 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2367 state |= BMAP_LEFT_CONTIG;
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 /*
2370 * Check and set flags if this segment has a right neighbor.
2371 * Don't set contiguous if the combined extent would be too large.
2372 * Also check for all-three-contiguous being too large.
2373 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002374 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002375 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002376 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002377 if (isnullstartblock(RIGHT.br_startblock))
2378 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002380
2381 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2382 new_endoff == RIGHT.br_startoff &&
2383 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2384 newext == RIGHT.br_state &&
2385 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2386 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2387 BMAP_RIGHT_FILLING)) !=
2388 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2389 BMAP_RIGHT_FILLING) ||
2390 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2391 <= MAXEXTLEN))
2392 state |= BMAP_RIGHT_CONTIG;
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 /*
2395 * Switch out based on the FILLING and CONTIG state bits.
2396 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002397 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2398 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2399 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2400 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 /*
2402 * Setting all of a previous oldext extent to newext.
2403 * The left and right neighbors are both contiguous with new.
2404 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002405 --*idx;
2406
2407 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2408 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 LEFT.br_blockcount + PREV.br_blockcount +
2410 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002411 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002412
Christoph Hellwigec90c552011-05-23 08:52:53 +00002413 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 ip->i_d.di_nextents -= 2;
2415 if (cur == NULL)
2416 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2417 else {
2418 rval = XFS_ILOG_CORE;
2419 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2420 RIGHT.br_startblock,
2421 RIGHT.br_blockcount, &i)))
2422 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002423 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002424 if ((error = xfs_btree_delete(cur, &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 Hellwig8df4da42008-10-30 16:55:58 +11002427 if ((error = xfs_btree_decrement(cur, 0, &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 Hellwig91cca5df2008-10-30 16:58:01 +11002430 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002433 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002435 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2437 LEFT.br_startblock,
2438 LEFT.br_blockcount + PREV.br_blockcount +
2439 RIGHT.br_blockcount, LEFT.br_state)))
2440 goto done;
2441 }
2442 break;
2443
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002444 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 /*
2446 * Setting all of a previous oldext extent to newext.
2447 * The left neighbor is contiguous, the right is not.
2448 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002449 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002450
Christoph Hellwigec90c552011-05-23 08:52:53 +00002451 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2452 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2453 LEFT.br_blockcount + PREV.br_blockcount);
2454 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2455
2456 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 ip->i_d.di_nextents--;
2458 if (cur == NULL)
2459 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2460 else {
2461 rval = XFS_ILOG_CORE;
2462 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2463 PREV.br_startblock, PREV.br_blockcount,
2464 &i)))
2465 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002466 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002467 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002470 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002472 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2474 LEFT.br_startblock,
2475 LEFT.br_blockcount + PREV.br_blockcount,
2476 LEFT.br_state)))
2477 goto done;
2478 }
2479 break;
2480
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002481 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 /*
2483 * Setting all of a previous oldext extent to newext.
2484 * The right neighbor is contiguous, the left is not.
2485 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002486 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 xfs_bmbt_set_blockcount(ep,
2488 PREV.br_blockcount + RIGHT.br_blockcount);
2489 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002490 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2491 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 ip->i_d.di_nextents--;
2493 if (cur == NULL)
2494 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2495 else {
2496 rval = XFS_ILOG_CORE;
2497 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2498 RIGHT.br_startblock,
2499 RIGHT.br_blockcount, &i)))
2500 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002501 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002502 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002505 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002507 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2509 new->br_startblock,
2510 new->br_blockcount + RIGHT.br_blockcount,
2511 newext)))
2512 goto done;
2513 }
2514 break;
2515
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002516 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 /*
2518 * Setting all of a previous oldext extent to newext.
2519 * Neither the left nor right neighbors are contiguous with
2520 * the new one.
2521 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002522 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002524 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 if (cur == NULL)
2527 rval = XFS_ILOG_DEXT;
2528 else {
2529 rval = 0;
2530 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2531 new->br_startblock, new->br_blockcount,
2532 &i)))
2533 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002534 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2536 new->br_startblock, new->br_blockcount,
2537 newext)))
2538 goto done;
2539 }
2540 break;
2541
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002542 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 /*
2544 * Setting the first part of a previous oldext extent to newext.
2545 * The left neighbor is contiguous.
2546 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002547 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2548 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 LEFT.br_blockcount + new->br_blockcount);
2550 xfs_bmbt_set_startoff(ep,
2551 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002552 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002553
Christoph Hellwigec90c552011-05-23 08:52:53 +00002554 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 xfs_bmbt_set_startblock(ep,
2556 new->br_startblock + new->br_blockcount);
2557 xfs_bmbt_set_blockcount(ep,
2558 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002559 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002560
Christoph Hellwigec90c552011-05-23 08:52:53 +00002561 --*idx;
2562
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (cur == NULL)
2564 rval = XFS_ILOG_DEXT;
2565 else {
2566 rval = 0;
2567 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2568 PREV.br_startblock, PREV.br_blockcount,
2569 &i)))
2570 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002571 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 if ((error = xfs_bmbt_update(cur,
2573 PREV.br_startoff + new->br_blockcount,
2574 PREV.br_startblock + new->br_blockcount,
2575 PREV.br_blockcount - new->br_blockcount,
2576 oldext)))
2577 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002578 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002580 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 LEFT.br_startblock,
2582 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002583 LEFT.br_state);
2584 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 goto done;
2586 }
2587 break;
2588
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002589 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 /*
2591 * Setting the first part of a previous oldext extent to newext.
2592 * The left neighbor is not contiguous.
2593 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002594 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2596 xfs_bmbt_set_startoff(ep, new_endoff);
2597 xfs_bmbt_set_blockcount(ep,
2598 PREV.br_blockcount - new->br_blockcount);
2599 xfs_bmbt_set_startblock(ep,
2600 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002601 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002602
Christoph Hellwigec90c552011-05-23 08:52:53 +00002603 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 ip->i_d.di_nextents++;
2605 if (cur == NULL)
2606 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2607 else {
2608 rval = XFS_ILOG_CORE;
2609 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2610 PREV.br_startblock, PREV.br_blockcount,
2611 &i)))
2612 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002613 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 if ((error = xfs_bmbt_update(cur,
2615 PREV.br_startoff + new->br_blockcount,
2616 PREV.br_startblock + new->br_blockcount,
2617 PREV.br_blockcount - new->br_blockcount,
2618 oldext)))
2619 goto done;
2620 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002621 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002623 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 }
2625 break;
2626
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002627 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 /*
2629 * Setting the last part of a previous oldext extent to newext.
2630 * The right neighbor is contiguous with the new allocation.
2631 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002632 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 xfs_bmbt_set_blockcount(ep,
2634 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002635 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2636
2637 ++*idx;
2638
2639 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2640 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002641 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002643 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 if (cur == NULL)
2646 rval = XFS_ILOG_DEXT;
2647 else {
2648 rval = 0;
2649 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2650 PREV.br_startblock,
2651 PREV.br_blockcount, &i)))
2652 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002653 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2655 PREV.br_startblock,
2656 PREV.br_blockcount - new->br_blockcount,
2657 oldext)))
2658 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002659 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 goto done;
2661 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2662 new->br_startblock,
2663 new->br_blockcount + RIGHT.br_blockcount,
2664 newext)))
2665 goto done;
2666 }
2667 break;
2668
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002669 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 /*
2671 * Setting the last part of a previous oldext extent to newext.
2672 * The right neighbor is not contiguous.
2673 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002674 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 xfs_bmbt_set_blockcount(ep,
2676 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002677 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002678
Christoph Hellwigec90c552011-05-23 08:52:53 +00002679 ++*idx;
2680 xfs_iext_insert(ip, *idx, 1, new, state);
2681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 ip->i_d.di_nextents++;
2683 if (cur == NULL)
2684 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2685 else {
2686 rval = XFS_ILOG_CORE;
2687 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2688 PREV.br_startblock, PREV.br_blockcount,
2689 &i)))
2690 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002691 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2693 PREV.br_startblock,
2694 PREV.br_blockcount - new->br_blockcount,
2695 oldext)))
2696 goto done;
2697 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2698 new->br_startblock, new->br_blockcount,
2699 &i)))
2700 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002701 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002703 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002705 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 }
2707 break;
2708
2709 case 0:
2710 /*
2711 * Setting the middle part of a previous oldext extent to
2712 * newext. Contiguity is impossible here.
2713 * One extent becomes three extents.
2714 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002715 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 xfs_bmbt_set_blockcount(ep,
2717 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002718 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002719
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 r[0] = *new;
2721 r[1].br_startoff = new_endoff;
2722 r[1].br_blockcount =
2723 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2724 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2725 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002726
2727 ++*idx;
2728 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 ip->i_d.di_nextents += 2;
2731 if (cur == NULL)
2732 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2733 else {
2734 rval = XFS_ILOG_CORE;
2735 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2736 PREV.br_startblock, PREV.br_blockcount,
2737 &i)))
2738 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002739 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 /* new right extent - oldext */
2741 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2742 r[1].br_startblock, r[1].br_blockcount,
2743 r[1].br_state)))
2744 goto done;
2745 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002747 cur->bc_rec.b.br_blockcount =
2748 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002749 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002751 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002752 /*
2753 * Reset the cursor to the position of the new extent
2754 * we are about to insert as we can't trust it after
2755 * the previous insert.
2756 */
2757 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2758 new->br_startblock, new->br_blockcount,
2759 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 goto done;
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002761 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002763 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002764 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10002766 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
2768 break;
2769
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002770 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2771 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2772 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2773 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2774 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2775 case BMAP_LEFT_CONTIG:
2776 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 /*
2778 * These cases are all impossible.
2779 */
2780 ASSERT(0);
2781 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002782
2783 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002784 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002785 int tmp_logflags; /* partial log flag return val */
2786
2787 ASSERT(cur == NULL);
2788 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
2789 0, &tmp_logflags, XFS_DATA_FORK);
2790 *logflagsp |= tmp_logflags;
2791 if (error)
2792 goto done;
2793 }
2794
2795 /* clear out the allocated field, done with it now in any case. */
2796 if (cur) {
2797 cur->bc_private.b.allocated = 0;
2798 *curp = cur;
2799 }
2800
2801 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002803 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return error;
2805#undef LEFT
2806#undef RIGHT
2807#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808}
2809
2810/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002811 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002813STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814xfs_bmap_add_extent_hole_delay(
2815 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002816 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002817 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002819 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2821 xfs_filblks_t newlen=0; /* new indirect size */
2822 xfs_filblks_t oldlen=0; /* old indirect size */
2823 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2824 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002825 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002827 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002829 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 /*
2832 * Check and set flags if this segment has a left neighbor
2833 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002834 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002835 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002836 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002837
2838 if (isnullstartblock(left.br_startblock))
2839 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 /*
2843 * Check and set flags if the current (right) segment exists.
2844 * If it doesn't exist, we're converting the hole at end-of-file.
2845 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002846 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002847 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002848 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002849
2850 if (isnullstartblock(right.br_startblock))
2851 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 /*
2855 * Set contiguity flags on the left and right neighbors.
2856 * Don't let extents get too large, even if the pieces are contiguous.
2857 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002858 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2859 left.br_startoff + left.br_blockcount == new->br_startoff &&
2860 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2861 state |= BMAP_LEFT_CONTIG;
2862
2863 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2864 new->br_startoff + new->br_blockcount == right.br_startoff &&
2865 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2866 (!(state & BMAP_LEFT_CONTIG) ||
2867 (left.br_blockcount + new->br_blockcount +
2868 right.br_blockcount <= MAXEXTLEN)))
2869 state |= BMAP_RIGHT_CONTIG;
2870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 /*
2872 * Switch out based on the contiguity flags.
2873 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002874 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2875 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 /*
2877 * New allocation is contiguous with delayed allocations
2878 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002879 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002881 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 temp = left.br_blockcount + new->br_blockcount +
2883 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002884
Christoph Hellwigec90c552011-05-23 08:52:53 +00002885 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2886 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002887 oldlen = startblockval(left.br_startblock) +
2888 startblockval(new->br_startblock) +
2889 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002891 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002892 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002893 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002894
Christoph Hellwigec90c552011-05-23 08:52:53 +00002895 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 break;
2897
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002898 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 /*
2900 * New allocation is contiguous with a delayed allocation
2901 * on the left.
2902 * Merge the new allocation with the left neighbor.
2903 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002904 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002906
2907 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2908 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002909 oldlen = startblockval(left.br_startblock) +
2910 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002912 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002913 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002914 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 break;
2916
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002917 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 /*
2919 * New allocation is contiguous with a delayed allocation
2920 * on the right.
2921 * Merge the new allocation with the right neighbor.
2922 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002923 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002925 oldlen = startblockval(new->br_startblock) +
2926 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002928 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2929 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002930 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002931 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 break;
2933
2934 case 0:
2935 /*
2936 * New allocation is not contiguous with another
2937 * delayed allocation.
2938 * Insert a new entry.
2939 */
2940 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002941 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 break;
2943 }
2944 if (oldlen != newlen) {
2945 ASSERT(oldlen > newlen);
Christoph Hellwig96540c72010-09-30 02:25:55 +00002946 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00002947 (int64_t)(oldlen - newlen), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 /*
2949 * Nothing to do for disk quota accounting here.
2950 */
2951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952}
2953
2954/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002955 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 */
2957STATIC int /* error */
2958xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002959 struct xfs_bmalloca *bma,
2960 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002962 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 int i; /* temp state */
2965 xfs_ifork_t *ifp; /* inode fork pointer */
2966 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2967 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002968 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
Christoph Hellwigc6534242011-09-18 20:41:05 +00002971 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002972
Christoph Hellwigc6534242011-09-18 20:41:05 +00002973 ASSERT(bma->idx >= 0);
2974 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002975 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002976 ASSERT(!bma->cur ||
2977 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002978
2979 XFS_STATS_INC(xs_add_exlist);
2980
2981 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002982 if (whichfork == XFS_ATTR_FORK)
2983 state |= BMAP_ATTRFORK;
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 /*
2986 * Check and set flags if this segment has a left neighbor.
2987 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002988 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002989 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002990 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002991 if (isnullstartblock(left.br_startblock))
2992 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002994
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 /*
2996 * Check and set flags if this segment has a current value.
2997 * Not true if we're inserting into the "hole" at eof.
2998 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002999 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003000 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003001 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003002 if (isnullstartblock(right.br_startblock))
3003 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 /*
3007 * We're inserting a real allocation between "left" and "right".
3008 * Set the contiguity flags. Don't let extents get too large.
3009 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003010 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
3011 left.br_startoff + left.br_blockcount == new->br_startoff &&
3012 left.br_startblock + left.br_blockcount == new->br_startblock &&
3013 left.br_state == new->br_state &&
3014 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
3015 state |= BMAP_LEFT_CONTIG;
3016
3017 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
3018 new->br_startoff + new->br_blockcount == right.br_startoff &&
3019 new->br_startblock + new->br_blockcount == right.br_startblock &&
3020 new->br_state == right.br_state &&
3021 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3022 (!(state & BMAP_LEFT_CONTIG) ||
3023 left.br_blockcount + new->br_blockcount +
3024 right.br_blockcount <= MAXEXTLEN))
3025 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003027 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 /*
3029 * Select which case we're in here, and implement it.
3030 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003031 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3032 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 /*
3034 * New allocation is contiguous with real allocations on the
3035 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003036 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003038 --bma->idx;
3039 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3040 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 left.br_blockcount + new->br_blockcount +
3042 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003043 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003044
Christoph Hellwigc6534242011-09-18 20:41:05 +00003045 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003046
Christoph Hellwigc6534242011-09-18 20:41:05 +00003047 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3048 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3049 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003050 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003051 } else {
3052 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003053 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3054 right.br_startblock, right.br_blockcount,
3055 &i);
3056 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003057 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003058 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003059 error = xfs_btree_delete(bma->cur, &i);
3060 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003061 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003062 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003063 error = xfs_btree_decrement(bma->cur, 0, &i);
3064 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003065 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003066 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003067 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003068 left.br_startblock,
3069 left.br_blockcount +
3070 new->br_blockcount +
3071 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003072 left.br_state);
3073 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003074 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003076 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003078 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 /*
3080 * New allocation is contiguous with a real allocation
3081 * on the left.
3082 * Merge the new allocation with the left neighbor.
3083 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003084 --bma->idx;
3085 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3086 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003088 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003089
Christoph Hellwigc6534242011-09-18 20:41:05 +00003090 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003091 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003092 } else {
3093 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003094 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3095 left.br_startblock, left.br_blockcount,
3096 &i);
3097 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003098 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003099 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003100 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003101 left.br_startblock,
3102 left.br_blockcount +
3103 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003104 left.br_state);
3105 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003106 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003108 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003110 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 /*
3112 * New allocation is contiguous with a real allocation
3113 * on the right.
3114 * Merge the new allocation with the right neighbor.
3115 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003116 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3117 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003118 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 new->br_blockcount + right.br_blockcount,
3120 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003121 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003122
Christoph Hellwigc6534242011-09-18 20:41:05 +00003123 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003124 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003125 } else {
3126 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003127 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003128 right.br_startoff,
3129 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003130 right.br_blockcount, &i);
3131 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003132 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003133 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003134 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003135 new->br_startblock,
3136 new->br_blockcount +
3137 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003138 right.br_state);
3139 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003140 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003142 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143
3144 case 0:
3145 /*
3146 * New allocation is not contiguous with another
3147 * real allocation.
3148 * Insert a new entry.
3149 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003150 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3151 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3152 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3153 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003154 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003155 } else {
3156 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003157 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003158 new->br_startoff,
3159 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003160 new->br_blockcount, &i);
3161 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003162 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003163 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003164 bma->cur->bc_rec.b.br_state = new->br_state;
3165 error = xfs_btree_insert(bma->cur, &i);
3166 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003167 goto done;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10003168 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003170 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003172
3173 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003174 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003175 int tmp_logflags; /* partial log flag return val */
3176
Christoph Hellwigc6534242011-09-18 20:41:05 +00003177 ASSERT(bma->cur == NULL);
3178 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3179 bma->firstblock, bma->flist, &bma->cur,
3180 0, &tmp_logflags, whichfork);
3181 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003182 if (error)
3183 goto done;
3184 }
3185
3186 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003187 if (bma->cur)
3188 bma->cur->bc_private.b.allocated = 0;
3189
3190 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003191done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003192 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003193 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194}
3195
Nathan Scottdd9f4382006-01-11 15:28:28 +11003196/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003197 * Functions used in the extent read, allocate and remove paths
3198 */
3199
3200/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003201 * Adjust the size of the new extent based on di_extsize and rt extsize.
3202 */
Dave Chinner68988112013-08-12 20:49:42 +10003203int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003204xfs_bmap_extsize_align(
3205 xfs_mount_t *mp,
3206 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3207 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3208 xfs_extlen_t extsz, /* align to this extent size */
3209 int rt, /* is this a realtime inode? */
3210 int eof, /* is extent at end-of-file? */
3211 int delay, /* creating delalloc extent? */
3212 int convert, /* overwriting unwritten extent? */
3213 xfs_fileoff_t *offp, /* in/out: aligned offset */
3214 xfs_extlen_t *lenp) /* in/out: aligned length */
3215{
3216 xfs_fileoff_t orig_off; /* original offset */
3217 xfs_extlen_t orig_alen; /* original length */
3218 xfs_fileoff_t orig_end; /* original off+len */
3219 xfs_fileoff_t nexto; /* next file offset */
3220 xfs_fileoff_t prevo; /* previous file offset */
3221 xfs_fileoff_t align_off; /* temp for offset */
3222 xfs_extlen_t align_alen; /* temp for length */
3223 xfs_extlen_t temp; /* temp for calculations */
3224
3225 if (convert)
3226 return 0;
3227
3228 orig_off = align_off = *offp;
3229 orig_alen = align_alen = *lenp;
3230 orig_end = orig_off + orig_alen;
3231
3232 /*
3233 * If this request overlaps an existing extent, then don't
3234 * attempt to perform any additional alignment.
3235 */
3236 if (!delay && !eof &&
3237 (orig_off >= gotp->br_startoff) &&
3238 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3239 return 0;
3240 }
3241
3242 /*
3243 * If the file offset is unaligned vs. the extent size
3244 * we need to align it. This will be possible unless
3245 * the file was previously written with a kernel that didn't
3246 * perform this alignment, or if a truncate shot us in the
3247 * foot.
3248 */
3249 temp = do_mod(orig_off, extsz);
3250 if (temp) {
3251 align_alen += temp;
3252 align_off -= temp;
3253 }
3254 /*
3255 * Same adjustment for the end of the requested area.
3256 */
3257 if ((temp = (align_alen % extsz))) {
3258 align_alen += extsz - temp;
3259 }
3260 /*
3261 * If the previous block overlaps with this proposed allocation
3262 * then move the start forward without adjusting the length.
3263 */
3264 if (prevp->br_startoff != NULLFILEOFF) {
3265 if (prevp->br_startblock == HOLESTARTBLOCK)
3266 prevo = prevp->br_startoff;
3267 else
3268 prevo = prevp->br_startoff + prevp->br_blockcount;
3269 } else
3270 prevo = 0;
3271 if (align_off != orig_off && align_off < prevo)
3272 align_off = prevo;
3273 /*
3274 * If the next block overlaps with this proposed allocation
3275 * then move the start back without adjusting the length,
3276 * but not before offset 0.
3277 * This may of course make the start overlap previous block,
3278 * and if we hit the offset 0 limit then the next block
3279 * can still overlap too.
3280 */
3281 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3282 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3283 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3284 nexto = gotp->br_startoff + gotp->br_blockcount;
3285 else
3286 nexto = gotp->br_startoff;
3287 } else
3288 nexto = NULLFILEOFF;
3289 if (!eof &&
3290 align_off + align_alen != orig_end &&
3291 align_off + align_alen > nexto)
3292 align_off = nexto > align_alen ? nexto - align_alen : 0;
3293 /*
3294 * If we're now overlapping the next or previous extent that
3295 * means we can't fit an extsz piece in this hole. Just move
3296 * the start forward to the first valid spot and set
3297 * the length so we hit the end.
3298 */
3299 if (align_off != orig_off && align_off < prevo)
3300 align_off = prevo;
3301 if (align_off + align_alen != orig_end &&
3302 align_off + align_alen > nexto &&
3303 nexto != NULLFILEOFF) {
3304 ASSERT(nexto > prevo);
3305 align_alen = nexto - align_off;
3306 }
3307
3308 /*
3309 * If realtime, and the result isn't a multiple of the realtime
3310 * extent size we need to remove blocks until it is.
3311 */
3312 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3313 /*
3314 * We're not covering the original request, or
3315 * we won't be able to once we fix the length.
3316 */
3317 if (orig_off < align_off ||
3318 orig_end > align_off + align_alen ||
3319 align_alen - temp < orig_alen)
3320 return XFS_ERROR(EINVAL);
3321 /*
3322 * Try to fix it by moving the start up.
3323 */
3324 if (align_off + temp <= orig_off) {
3325 align_alen -= temp;
3326 align_off += temp;
3327 }
3328 /*
3329 * Try to fix it by moving the end in.
3330 */
3331 else if (align_off + align_alen - temp >= orig_end)
3332 align_alen -= temp;
3333 /*
3334 * Set the start to the minimum then trim the length.
3335 */
3336 else {
3337 align_alen -= orig_off - align_off;
3338 align_off = orig_off;
3339 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3340 }
3341 /*
3342 * Result doesn't cover the request, fail it.
3343 */
3344 if (orig_off < align_off || orig_end > align_off + align_alen)
3345 return XFS_ERROR(EINVAL);
3346 } else {
3347 ASSERT(orig_off >= align_off);
3348 ASSERT(orig_end <= align_off + align_alen);
3349 }
3350
3351#ifdef DEBUG
3352 if (!eof && gotp->br_startoff != NULLFILEOFF)
3353 ASSERT(align_off + align_alen <= gotp->br_startoff);
3354 if (prevp->br_startoff != NULLFILEOFF)
3355 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3356#endif
3357
3358 *lenp = align_alen;
3359 *offp = align_off;
3360 return 0;
3361}
3362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363#define XFS_ALLOC_GAP_UNITS 4
3364
Dave Chinner68988112013-08-12 20:49:42 +10003365void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003366xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003367 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368{
3369 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3371 xfs_mount_t *mp; /* mount point structure */
3372 int nullfb; /* true if ap->firstblock isn't set */
3373 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
3375#define ISVALID(x,y) \
3376 (rt ? \
3377 (x) < mp->m_sb.sb_rblocks : \
3378 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3379 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3380 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3381
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003383 nullfb = *ap->firstblock == NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003385 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386 /*
3387 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003388 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003390 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3391 !isnullstartblock(ap->prev.br_startblock) &&
3392 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3393 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003394 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 /*
3396 * Adjust for the gap between prevp and us.
3397 */
Dave Chinner3a756672011-09-18 20:40:58 +00003398 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003399 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003401 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3402 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 }
3404 /*
3405 * If not at eof, then compare the two neighbor blocks.
3406 * Figure out whether either one gives us a good starting point,
3407 * and pick the better one.
3408 */
3409 else if (!ap->eof) {
3410 xfs_fsblock_t gotbno; /* right side block number */
3411 xfs_fsblock_t gotdiff=0; /* right side difference */
3412 xfs_fsblock_t prevbno; /* left side block number */
3413 xfs_fsblock_t prevdiff=0; /* left side difference */
3414
3415 /*
3416 * If there's a previous (left) block, select a requested
3417 * start block based on it.
3418 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003419 if (ap->prev.br_startoff != NULLFILEOFF &&
3420 !isnullstartblock(ap->prev.br_startblock) &&
3421 (prevbno = ap->prev.br_startblock +
3422 ap->prev.br_blockcount) &&
3423 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 /*
3425 * Calculate gap to end of previous block.
3426 */
Dave Chinner3a756672011-09-18 20:40:58 +00003427 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003428 (ap->prev.br_startoff +
3429 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 /*
3431 * Figure the startblock based on the previous block's
3432 * end and the gap size.
3433 * Heuristic!
3434 * If the gap is large relative to the piece we're
3435 * allocating, or using it gives us an invalid block
3436 * number, then just use the end of the previous block.
3437 */
Dave Chinner3a756672011-09-18 20:40:58 +00003438 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003440 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 prevbno += adjust;
3442 else
3443 prevdiff += adjust;
3444 /*
3445 * If the firstblock forbids it, can't use it,
3446 * must use default.
3447 */
3448 if (!rt && !nullfb &&
3449 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3450 prevbno = NULLFSBLOCK;
3451 }
3452 /*
3453 * No previous block or can't follow it, just default.
3454 */
3455 else
3456 prevbno = NULLFSBLOCK;
3457 /*
3458 * If there's a following (right) block, select a requested
3459 * start block based on it.
3460 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003461 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 /*
3463 * Calculate gap to start of next block.
3464 */
Dave Chinner3a756672011-09-18 20:40:58 +00003465 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 /*
3467 * Figure the startblock based on the next block's
3468 * start and the gap size.
3469 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003470 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 /*
3472 * Heuristic!
3473 * If the gap is large relative to the piece we're
3474 * allocating, or using it gives us an invalid block
3475 * number, then just use the start of the next block
3476 * offset by our length.
3477 */
Dave Chinner3a756672011-09-18 20:40:58 +00003478 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479 ISVALID(gotbno - gotdiff, gotbno))
3480 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003481 else if (ISVALID(gotbno - ap->length, gotbno)) {
3482 gotbno -= ap->length;
3483 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 } else
3485 gotdiff += adjust;
3486 /*
3487 * If the firstblock forbids it, can't use it,
3488 * must use default.
3489 */
3490 if (!rt && !nullfb &&
3491 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3492 gotbno = NULLFSBLOCK;
3493 }
3494 /*
3495 * No next block, just default.
3496 */
3497 else
3498 gotbno = NULLFSBLOCK;
3499 /*
3500 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003501 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 */
3503 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003504 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003506 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003508 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003510#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003511}
3512
3513STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003514xfs_bmap_btalloc_nullfb(
3515 struct xfs_bmalloca *ap,
3516 struct xfs_alloc_arg *args,
3517 xfs_extlen_t *blen)
3518{
3519 struct xfs_mount *mp = ap->ip->i_mount;
3520 struct xfs_perag *pag;
3521 xfs_agnumber_t ag, startag;
3522 int notinit = 0;
3523 int error;
3524
3525 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3526 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3527 else
3528 args->type = XFS_ALLOCTYPE_START_BNO;
3529 args->total = ap->total;
3530
3531 /*
3532 * Search for an allocation group with a single extent large enough
3533 * for the request. If one isn't found, then adjust the minimum
3534 * allocation size to the largest space found.
3535 */
3536 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3537 if (startag == NULLAGNUMBER)
3538 startag = ag = 0;
3539
3540 pag = xfs_perag_get(mp, ag);
Dave Chinner14b064c2011-01-27 12:16:28 +11003541 while (*blen < args->maxlen) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00003542 if (!pag->pagf_init) {
3543 error = xfs_alloc_pagf_init(mp, args->tp, ag,
3544 XFS_ALLOC_FLAG_TRYLOCK);
3545 if (error) {
3546 xfs_perag_put(pag);
3547 return error;
3548 }
3549 }
3550
3551 /*
3552 * See xfs_alloc_fix_freelist...
3553 */
3554 if (pag->pagf_init) {
3555 xfs_extlen_t longest;
3556 longest = xfs_alloc_longest_free_extent(mp, pag);
3557 if (*blen < longest)
3558 *blen = longest;
3559 } else
3560 notinit = 1;
3561
3562 if (xfs_inode_is_filestream(ap->ip)) {
Dave Chinner14b064c2011-01-27 12:16:28 +11003563 if (*blen >= args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00003564 break;
3565
3566 if (ap->userdata) {
3567 /*
3568 * If startag is an invalid AG, we've
3569 * come here once before and
3570 * xfs_filestream_new_ag picked the
3571 * best currently available.
3572 *
3573 * Don't continue looping, since we
3574 * could loop forever.
3575 */
3576 if (startag == NULLAGNUMBER)
3577 break;
3578
3579 error = xfs_filestream_new_ag(ap, &ag);
3580 xfs_perag_put(pag);
3581 if (error)
3582 return error;
3583
3584 /* loop again to set 'blen'*/
3585 startag = NULLAGNUMBER;
3586 pag = xfs_perag_get(mp, ag);
3587 continue;
3588 }
3589 }
3590 if (++ag == mp->m_sb.sb_agcount)
3591 ag = 0;
3592 if (ag == startag)
3593 break;
3594 xfs_perag_put(pag);
3595 pag = xfs_perag_get(mp, ag);
3596 }
3597 xfs_perag_put(pag);
3598
3599 /*
3600 * Since the above loop did a BUF_TRYLOCK, it is
3601 * possible that there is space for this request.
3602 */
3603 if (notinit || *blen < ap->minlen)
3604 args->minlen = ap->minlen;
3605 /*
3606 * If the best seen length is less than the request
3607 * length, use the best as the minimum.
3608 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003609 else if (*blen < args->maxlen)
Christoph Hellwigc467c042010-02-15 23:34:42 +00003610 args->minlen = *blen;
3611 /*
Dave Chinner14b064c2011-01-27 12:16:28 +11003612 * Otherwise we've seen an extent as big as maxlen,
Christoph Hellwigc467c042010-02-15 23:34:42 +00003613 * use that as the minimum.
3614 */
3615 else
Dave Chinner14b064c2011-01-27 12:16:28 +11003616 args->minlen = args->maxlen;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003617
3618 /*
3619 * set the failure fallback case to look in the selected
3620 * AG as the stream may have moved.
3621 */
3622 if (xfs_inode_is_filestream(ap->ip))
Dave Chinner3a756672011-09-18 20:40:58 +00003623 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003624
3625 return 0;
3626}
3627
3628STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003629xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003630 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003631{
3632 xfs_mount_t *mp; /* mount point structure */
3633 xfs_alloctype_t atype = 0; /* type for allocation routines */
3634 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003635 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003636 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003637 xfs_alloc_arg_t args;
3638 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003639 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003640 int nullfb; /* true if ap->firstblock isn't set */
3641 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003642 int tryagain;
3643 int error;
3644
Dave Chinnera99ebf42011-12-01 11:24:20 +00003645 ASSERT(ap->length);
3646
Nathan Scotta365bdd2006-03-14 13:34:16 +11003647 mp = ap->ip->i_mount;
David Chinner957d0eb2007-06-18 16:50:37 +10003648 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003649 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003650 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003651 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003652 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003653 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003654 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003655 }
Dave Chinner0937e0f2011-09-18 20:40:57 +00003656 nullfb = *ap->firstblock == NULLFSBLOCK;
3657 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003658 if (nullfb) {
3659 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
3660 ag = xfs_filestream_lookup_ag(ap->ip);
3661 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003662 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003663 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003664 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003665 }
3666 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003667 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003668
3669 xfs_bmap_adjacent(ap);
3670
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003672 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 * it's in the right allocation group.
3674 */
Dave Chinner3a756672011-09-18 20:40:58 +00003675 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 ;
3677 else
Dave Chinner3a756672011-09-18 20:40:58 +00003678 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 * Normal allocation, done through xfs_alloc_vextent.
3681 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003682 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003683 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003684 args.tp = ap->tp;
3685 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003686 args.fsbno = ap->blkno;
Dave Chinner14b064c2011-01-27 12:16:28 +11003687
3688 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner3a756672011-09-18 20:40:58 +00003689 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003690 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003691 blen = 0;
3692 if (nullfb) {
Christoph Hellwigc467c042010-02-15 23:34:42 +00003693 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
3694 if (error)
3695 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003696 } else if (ap->flist->xbf_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003697 if (xfs_inode_is_filestream(ap->ip))
3698 args.type = XFS_ALLOCTYPE_FIRST_AG;
3699 else
3700 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003701 args.total = args.minlen = ap->minlen;
3702 } else {
3703 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3704 args.total = ap->total;
3705 args.minlen = ap->minlen;
3706 }
David Chinner957d0eb2007-06-18 16:50:37 +10003707 /* apply extent size hints if obtained earlier */
3708 if (unlikely(align)) {
3709 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003710 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003711 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11003712 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003713 args.prod = 1;
3714 args.mod = 0;
3715 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003716 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003717 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003718 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3719 }
3720 /*
3721 * If we are not low on available data blocks, and the
3722 * underlying logical volume manager is a stripe, and
3723 * the file offset is zero then try to allocate data
3724 * blocks on stripe unit boundary.
3725 * NOTE: ap->aeof is only set if the allocation length
3726 * is >= the stripe unit and the allocation offset is
3727 * at the end of file.
3728 */
Dave Chinner0937e0f2011-09-18 20:40:57 +00003729 if (!ap->flist->xbf_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003730 if (!ap->offset) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003731 args.alignment = mp->m_dalign;
3732 atype = args.type;
3733 isaligned = 1;
3734 /*
3735 * Adjust for alignment
3736 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003737 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003738 args.minlen = blen - args.alignment;
3739 args.minalignslop = 0;
3740 } else {
3741 /*
3742 * First try an exact bno allocation.
3743 * If it fails then do a near or start bno
3744 * allocation with alignment turned on.
3745 */
3746 atype = args.type;
3747 tryagain = 1;
3748 args.type = XFS_ALLOCTYPE_THIS_BNO;
3749 args.alignment = 1;
3750 /*
3751 * Compute the minlen+alignment for the
3752 * next case. Set slop so that the value
3753 * of minlen+alignment+slop doesn't go up
3754 * between the calls.
3755 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003756 if (blen > mp->m_dalign && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003757 nextminlen = blen - mp->m_dalign;
3758 else
3759 nextminlen = args.minlen;
3760 if (nextminlen + mp->m_dalign > args.minlen + 1)
3761 args.minalignslop =
3762 nextminlen + mp->m_dalign -
3763 args.minlen - 1;
3764 else
3765 args.minalignslop = 0;
3766 }
3767 } else {
3768 args.alignment = 1;
3769 args.minalignslop = 0;
3770 }
3771 args.minleft = ap->minleft;
3772 args.wasdel = ap->wasdel;
3773 args.isfl = 0;
3774 args.userdata = ap->userdata;
3775 if ((error = xfs_alloc_vextent(&args)))
3776 return error;
3777 if (tryagain && args.fsbno == NULLFSBLOCK) {
3778 /*
3779 * Exact allocation failed. Now try with alignment
3780 * turned on.
3781 */
3782 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003783 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003784 args.alignment = mp->m_dalign;
3785 args.minlen = nextminlen;
3786 args.minalignslop = 0;
3787 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 if ((error = xfs_alloc_vextent(&args)))
3789 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003790 }
3791 if (isaligned && args.fsbno == NULLFSBLOCK) {
3792 /*
3793 * allocation failed, so turn off alignment and
3794 * try again.
3795 */
3796 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003797 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003798 args.alignment = 0;
3799 if ((error = xfs_alloc_vextent(&args)))
3800 return error;
3801 }
3802 if (args.fsbno == NULLFSBLOCK && nullfb &&
3803 args.minlen > ap->minlen) {
3804 args.minlen = ap->minlen;
3805 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003806 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003807 if ((error = xfs_alloc_vextent(&args)))
3808 return error;
3809 }
3810 if (args.fsbno == NULLFSBLOCK && nullfb) {
3811 args.fsbno = 0;
3812 args.type = XFS_ALLOCTYPE_FIRST_AG;
3813 args.total = ap->minlen;
3814 args.minleft = 0;
3815 if ((error = xfs_alloc_vextent(&args)))
3816 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003817 ap->flist->xbf_low = 1;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003818 }
3819 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003820 /*
3821 * check the allocation happened at the same or higher AG than
3822 * the first block that was allocated.
3823 */
3824 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3825 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3826 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
3827 (ap->flist->xbf_low &&
3828 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3829 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3830
Dave Chinner3a756672011-09-18 20:40:58 +00003831 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003832 if (*ap->firstblock == NULLFSBLOCK)
3833 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003834 ASSERT(nullfb || fb_agno == args.agno ||
Dave Chinner0937e0f2011-09-18 20:40:57 +00003835 (ap->flist->xbf_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003836 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003837 ap->ip->i_d.di_nblocks += args.len;
3838 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3839 if (ap->wasdel)
3840 ap->ip->i_delayed_blks -= args.len;
3841 /*
3842 * Adjust the disk quota also. This was reserved
3843 * earlier.
3844 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003845 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003846 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3847 XFS_TRANS_DQ_BCOUNT,
3848 (long) args.len);
3849 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003850 ap->blkno = NULLFSBLOCK;
3851 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
3853 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003854}
3855
3856/*
3857 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3858 * It figures out where to ask the underlying allocator to put the new extent.
3859 */
3860STATIC int
3861xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003862 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003863{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003864 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003865 return xfs_bmap_rtalloc(ap);
3866 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867}
3868
3869/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003870 * Trim the returned map to the required bounds
3871 */
3872STATIC void
3873xfs_bmapi_trim_map(
3874 struct xfs_bmbt_irec *mval,
3875 struct xfs_bmbt_irec *got,
3876 xfs_fileoff_t *bno,
3877 xfs_filblks_t len,
3878 xfs_fileoff_t obno,
3879 xfs_fileoff_t end,
3880 int n,
3881 int flags)
3882{
3883 if ((flags & XFS_BMAPI_ENTIRE) ||
3884 got->br_startoff + got->br_blockcount <= obno) {
3885 *mval = *got;
3886 if (isnullstartblock(got->br_startblock))
3887 mval->br_startblock = DELAYSTARTBLOCK;
3888 return;
3889 }
3890
3891 if (obno > *bno)
3892 *bno = obno;
3893 ASSERT((*bno >= obno) || (n == 0));
3894 ASSERT(*bno < end);
3895 mval->br_startoff = *bno;
3896 if (isnullstartblock(got->br_startblock))
3897 mval->br_startblock = DELAYSTARTBLOCK;
3898 else
3899 mval->br_startblock = got->br_startblock +
3900 (*bno - got->br_startoff);
3901 /*
3902 * Return the minimum of what we got and what we asked for for
3903 * the length. We can use the len variable here because it is
3904 * modified below and we could have been there before coming
3905 * here if the first part of the allocation didn't overlap what
3906 * was asked for.
3907 */
3908 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3909 got->br_blockcount - (*bno - got->br_startoff));
3910 mval->br_state = got->br_state;
3911 ASSERT(mval->br_blockcount <= len);
3912 return;
3913}
3914
3915/*
3916 * Update and validate the extent map to return
3917 */
3918STATIC void
3919xfs_bmapi_update_map(
3920 struct xfs_bmbt_irec **map,
3921 xfs_fileoff_t *bno,
3922 xfs_filblks_t *len,
3923 xfs_fileoff_t obno,
3924 xfs_fileoff_t end,
3925 int *n,
3926 int flags)
3927{
3928 xfs_bmbt_irec_t *mval = *map;
3929
3930 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3931 ((mval->br_startoff + mval->br_blockcount) <= end));
3932 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3933 (mval->br_startoff < obno));
3934
3935 *bno = mval->br_startoff + mval->br_blockcount;
3936 *len = end - *bno;
3937 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3938 /* update previous map with new information */
3939 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3940 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3941 ASSERT(mval->br_state == mval[-1].br_state);
3942 mval[-1].br_blockcount = mval->br_blockcount;
3943 mval[-1].br_state = mval->br_state;
3944 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3945 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3946 mval[-1].br_startblock != HOLESTARTBLOCK &&
3947 mval->br_startblock == mval[-1].br_startblock +
3948 mval[-1].br_blockcount &&
3949 ((flags & XFS_BMAPI_IGSTATE) ||
3950 mval[-1].br_state == mval->br_state)) {
3951 ASSERT(mval->br_startoff ==
3952 mval[-1].br_startoff + mval[-1].br_blockcount);
3953 mval[-1].br_blockcount += mval->br_blockcount;
3954 } else if (*n > 0 &&
3955 mval->br_startblock == DELAYSTARTBLOCK &&
3956 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3957 mval->br_startoff ==
3958 mval[-1].br_startoff + mval[-1].br_blockcount) {
3959 mval[-1].br_blockcount += mval->br_blockcount;
3960 mval[-1].br_state = mval->br_state;
3961 } else if (!((*n == 0) &&
3962 ((mval->br_startoff + mval->br_blockcount) <=
3963 obno))) {
3964 mval++;
3965 (*n)++;
3966 }
3967 *map = mval;
3968}
3969
3970/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003971 * Map file blocks to filesystem blocks without allocation.
3972 */
3973int
3974xfs_bmapi_read(
3975 struct xfs_inode *ip,
3976 xfs_fileoff_t bno,
3977 xfs_filblks_t len,
3978 struct xfs_bmbt_irec *mval,
3979 int *nmap,
3980 int flags)
3981{
3982 struct xfs_mount *mp = ip->i_mount;
3983 struct xfs_ifork *ifp;
3984 struct xfs_bmbt_irec got;
3985 struct xfs_bmbt_irec prev;
3986 xfs_fileoff_t obno;
3987 xfs_fileoff_t end;
3988 xfs_extnum_t lastx;
3989 int error;
3990 int eof;
3991 int n = 0;
3992 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
3993 XFS_ATTR_FORK : XFS_DATA_FORK;
3994
3995 ASSERT(*nmap >= 1);
3996 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
3997 XFS_BMAPI_IGSTATE)));
3998
3999 if (unlikely(XFS_TEST_ERROR(
4000 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4001 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4002 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4003 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4004 return XFS_ERROR(EFSCORRUPTED);
4005 }
4006
4007 if (XFS_FORCED_SHUTDOWN(mp))
4008 return XFS_ERROR(EIO);
4009
4010 XFS_STATS_INC(xs_blk_mapr);
4011
4012 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004013
4014 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4015 error = xfs_iread_extents(NULL, ip, whichfork);
4016 if (error)
4017 return error;
4018 }
4019
4020 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4021 end = bno + len;
4022 obno = bno;
4023
4024 while (bno < end && n < *nmap) {
4025 /* Reading past eof, act as though there's a hole up to end. */
4026 if (eof)
4027 got.br_startoff = end;
4028 if (got.br_startoff > bno) {
4029 /* Reading in a hole. */
4030 mval->br_startoff = bno;
4031 mval->br_startblock = HOLESTARTBLOCK;
4032 mval->br_blockcount =
4033 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4034 mval->br_state = XFS_EXT_NORM;
4035 bno += mval->br_blockcount;
4036 len -= mval->br_blockcount;
4037 mval++;
4038 n++;
4039 continue;
4040 }
4041
4042 /* set up the extent map to return. */
4043 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4044 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4045
4046 /* If we're done, stop now. */
4047 if (bno >= end || n >= *nmap)
4048 break;
4049
4050 /* Else go on to the next record. */
4051 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4052 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4053 else
4054 eof = 1;
4055 }
4056 *nmap = n;
4057 return 0;
4058}
4059
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004060STATIC int
4061xfs_bmapi_reserve_delalloc(
4062 struct xfs_inode *ip,
4063 xfs_fileoff_t aoff,
4064 xfs_filblks_t len,
4065 struct xfs_bmbt_irec *got,
4066 struct xfs_bmbt_irec *prev,
4067 xfs_extnum_t *lastx,
4068 int eof)
4069{
4070 struct xfs_mount *mp = ip->i_mount;
4071 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4072 xfs_extlen_t alen;
4073 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004074 char rt = XFS_IS_REALTIME_INODE(ip);
4075 xfs_extlen_t extsz;
4076 int error;
4077
4078 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4079 if (!eof)
4080 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4081
4082 /* Figure out the extent size, adjust alen */
4083 extsz = xfs_get_extsz_hint(ip);
4084 if (extsz) {
4085 /*
4086 * Make sure we don't exceed a single extent length when we
4087 * align the extent by reducing length we are going to
4088 * allocate by the maximum amount extent size aligment may
4089 * require.
4090 */
4091 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4092 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4093 1, 0, &aoff, &alen);
4094 ASSERT(!error);
4095 }
4096
4097 if (rt)
4098 extsz = alen / mp->m_sb.sb_rextsize;
4099
4100 /*
4101 * Make a transaction-less quota reservation for delayed allocation
4102 * blocks. This number gets adjusted later. We return if we haven't
4103 * allocated blocks already inside this loop.
4104 */
4105 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4106 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4107 if (error)
4108 return error;
4109
4110 /*
4111 * Split changing sb for alen and indlen since they could be coming
4112 * from different places.
4113 */
4114 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4115 ASSERT(indlen > 0);
4116
4117 if (rt) {
4118 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4119 -((int64_t)extsz), 0);
4120 } else {
4121 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4122 -((int64_t)alen), 0);
4123 }
4124
4125 if (error)
4126 goto out_unreserve_quota;
4127
4128 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4129 -((int64_t)indlen), 0);
4130 if (error)
4131 goto out_unreserve_blocks;
4132
4133
4134 ip->i_delayed_blks += alen;
4135
4136 got->br_startoff = aoff;
4137 got->br_startblock = nullstartblock(indlen);
4138 got->br_blockcount = alen;
4139 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004140 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004141
4142 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004143 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4144 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004145 */
4146 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4147
4148 ASSERT(got->br_startoff <= aoff);
4149 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4150 ASSERT(isnullstartblock(got->br_startblock));
4151 ASSERT(got->br_state == XFS_EXT_NORM);
4152 return 0;
4153
4154out_unreserve_blocks:
4155 if (rt)
4156 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4157 else
4158 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4159out_unreserve_quota:
4160 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004161 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004162 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4163 return error;
4164}
4165
Dave Chinner5c8ed202011-09-18 20:40:45 +00004166/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004167 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4168 */
4169int
4170xfs_bmapi_delay(
4171 struct xfs_inode *ip, /* incore inode */
4172 xfs_fileoff_t bno, /* starting file offs. mapped */
4173 xfs_filblks_t len, /* length to map in file */
4174 struct xfs_bmbt_irec *mval, /* output: map values */
4175 int *nmap, /* i/o: mval size/count */
4176 int flags) /* XFS_BMAPI_... */
4177{
4178 struct xfs_mount *mp = ip->i_mount;
4179 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4180 struct xfs_bmbt_irec got; /* current file extent record */
4181 struct xfs_bmbt_irec prev; /* previous file extent record */
4182 xfs_fileoff_t obno; /* old block number (offset) */
4183 xfs_fileoff_t end; /* end of mapped file region */
4184 xfs_extnum_t lastx; /* last useful extent number */
4185 int eof; /* we've hit the end of extents */
4186 int n = 0; /* current extent index */
4187 int error = 0;
4188
4189 ASSERT(*nmap >= 1);
4190 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4191 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4192
4193 if (unlikely(XFS_TEST_ERROR(
4194 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4195 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4196 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4197 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4198 return XFS_ERROR(EFSCORRUPTED);
4199 }
4200
4201 if (XFS_FORCED_SHUTDOWN(mp))
4202 return XFS_ERROR(EIO);
4203
4204 XFS_STATS_INC(xs_blk_mapw);
4205
4206 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4207 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4208 if (error)
4209 return error;
4210 }
4211
4212 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4213 end = bno + len;
4214 obno = bno;
4215
4216 while (bno < end && n < *nmap) {
4217 if (eof || got.br_startoff > bno) {
4218 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4219 &prev, &lastx, eof);
4220 if (error) {
4221 if (n == 0) {
4222 *nmap = 0;
4223 return error;
4224 }
4225 break;
4226 }
4227 }
4228
4229 /* set up the extent map to return. */
4230 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4231 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4232
4233 /* If we're done, stop now. */
4234 if (bno >= end || n >= *nmap)
4235 break;
4236
4237 /* Else go on to the next record. */
4238 prev = got;
4239 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4240 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4241 else
4242 eof = 1;
4243 }
4244
4245 *nmap = n;
4246 return 0;
4247}
4248
4249
Dave Chinner68988112013-08-12 20:49:42 +10004250int
Dave Chinnere04426b2012-10-05 11:06:59 +10004251__xfs_bmapi_allocate(
4252 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004253{
4254 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004255 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004256 XFS_ATTR_FORK : XFS_DATA_FORK;
4257 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004258 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004259 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004260
Dave Chinnera99ebf42011-12-01 11:24:20 +00004261 ASSERT(bma->length > 0);
4262
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004263 /*
4264 * For the wasdelay case, we could also just allocate the stuff asked
4265 * for in this bmap call but that wouldn't be as good.
4266 */
4267 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004268 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4269 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004270 if (bma->idx != NULLEXTNUM && bma->idx) {
4271 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004272 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004273 }
4274 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004275 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004276 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004277 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004278 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004279 }
4280
4281 /*
4282 * Indicate if this is the first user data in the file, or just any
4283 * user data.
4284 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004285 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004286 bma->userdata = (bma->offset == 0) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004287 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4288 }
4289
Dave Chinnere04426b2012-10-05 11:06:59 +10004290 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004291
4292 /*
4293 * Only want to do the alignment at the eof if it is userdata and
4294 * allocation length is larger than a stripe unit.
4295 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004296 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004297 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004298 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004299 if (error)
4300 return error;
4301 }
4302
4303 error = xfs_bmap_alloc(bma);
4304 if (error)
4305 return error;
4306
Dave Chinner0937e0f2011-09-18 20:40:57 +00004307 if (bma->flist->xbf_low)
4308 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004309 if (bma->cur)
4310 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004311 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004312 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004313 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4314 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4315 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4316 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004317 }
4318 /*
4319 * Bump the number of extents we've allocated
4320 * in this call.
4321 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004322 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004323
Dave Chinner29c8d172011-09-18 20:41:00 +00004324 if (bma->cur)
4325 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004326 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4327
Dave Chinner963c30c2011-09-18 20:40:59 +00004328 bma->got.br_startoff = bma->offset;
4329 bma->got.br_startblock = bma->blkno;
4330 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004331 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004332
4333 /*
4334 * A wasdelay extent has been initialized, so shouldn't be flagged
4335 * as unwritten.
4336 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004337 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004338 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004339 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004340
Christoph Hellwigc6534242011-09-18 20:41:05 +00004341 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004342 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004343 else
4344 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004345
Christoph Hellwigc315c902011-09-18 20:41:02 +00004346 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004347 if (error)
4348 return error;
4349
4350 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004351 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4352 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4353 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004354 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004355 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004356
Dave Chinner963c30c2011-09-18 20:40:59 +00004357 ASSERT(bma->got.br_startoff <= bma->offset);
4358 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4359 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004360 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4361 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004362 return 0;
4363}
4364
Dave Chinnerb447fe52011-09-18 20:40:51 +00004365STATIC int
4366xfs_bmapi_convert_unwritten(
4367 struct xfs_bmalloca *bma,
4368 struct xfs_bmbt_irec *mval,
4369 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004370 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004371{
4372 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4373 XFS_ATTR_FORK : XFS_DATA_FORK;
4374 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004375 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004376 int error;
4377
Dave Chinnerb447fe52011-09-18 20:40:51 +00004378 /* check if we need to do unwritten->real conversion */
4379 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4380 (flags & XFS_BMAPI_PREALLOC))
4381 return 0;
4382
4383 /* check if we need to do real->unwritten conversion */
4384 if (mval->br_state == XFS_EXT_NORM &&
4385 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4386 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4387 return 0;
4388
4389 /*
4390 * Modify (by adding) the state flag, if writing.
4391 */
4392 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004393 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4394 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004395 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004396 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4397 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004398 }
4399 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4400 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4401
Dave Chinnere0c3da52011-09-18 20:41:01 +00004402 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004403 &bma->cur, mval, bma->firstblock, bma->flist,
4404 &tmp_logflags);
4405 bma->logflags |= tmp_logflags;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004406 if (error)
4407 return error;
4408
4409 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004410 * Update our extent pointer, given that
4411 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4412 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004413 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004414 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004415
4416 /*
4417 * We may have combined previously unwritten space with written space,
4418 * so generate another request.
4419 */
4420 if (mval->br_blockcount < len)
4421 return EAGAIN;
4422 return 0;
4423}
4424
Christoph Hellwig44032802011-09-18 20:40:48 +00004425/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004426 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4427 * extent state if necessary. Details behaviour is controlled by the flags
4428 * parameter. Only allocates blocks from a single allocation group, to avoid
4429 * locking problems.
4430 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 * The returned value in "firstblock" from the first call in a transaction
4432 * must be remembered and presented to subsequent calls in "firstblock".
4433 * An upper bound for the number of blocks to be allocated is supplied to
4434 * the first call in "total"; if no allocation group has that many free
4435 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4436 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004437int
4438xfs_bmapi_write(
4439 struct xfs_trans *tp, /* transaction pointer */
4440 struct xfs_inode *ip, /* incore inode */
4441 xfs_fileoff_t bno, /* starting file offs. mapped */
4442 xfs_filblks_t len, /* length to map in file */
4443 int flags, /* XFS_BMAPI_... */
4444 xfs_fsblock_t *firstblock, /* first allocated block
4445 controls a.g. for allocs */
4446 xfs_extlen_t total, /* total blocks needed */
4447 struct xfs_bmbt_irec *mval, /* output: map values */
4448 int *nmap, /* i/o: mval size/count */
4449 struct xfs_bmap_free *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004451 struct xfs_mount *mp = ip->i_mount;
4452 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004453 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004454 xfs_fileoff_t end; /* end of mapped file region */
4455 int eof; /* after the end of extents */
4456 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004457 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004458 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004459 int whichfork; /* data or attr fork */
4460 char inhole; /* current location is hole in file */
4461 char wasdelay; /* old extent was delayed */
4462
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004464 xfs_fileoff_t orig_bno; /* original block number value */
4465 int orig_flags; /* original flags arg value */
4466 xfs_filblks_t orig_len; /* original value of len arg */
4467 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4468 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469
4470 orig_bno = bno;
4471 orig_len = len;
4472 orig_flags = flags;
4473 orig_mval = mval;
4474 orig_nmap = *nmap;
4475#endif
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004476 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4477 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004478
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004480 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4481 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4482 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004483 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004484 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004485
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 if (unlikely(XFS_TEST_ERROR(
4487 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004488 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004490 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 return XFS_ERROR(EFSCORRUPTED);
4492 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004493
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 if (XFS_FORCED_SHUTDOWN(mp))
4495 return XFS_ERROR(EIO);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004496
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004498
4499 XFS_STATS_INC(xs_blk_mapw);
4500
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004501 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004503 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004505 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004506 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004507 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004508 }
4509
4510 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4511 error = xfs_iread_extents(tp, ip, whichfork);
4512 if (error)
4513 goto error0;
4514 }
4515
Dave Chinnere0c3da52011-09-18 20:41:01 +00004516 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004517 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 n = 0;
4519 end = bno + len;
4520 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004521
4522 bma.tp = tp;
4523 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004524 bma.total = total;
4525 bma.userdata = 0;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004526 bma.flist = flist;
4527 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004528
Brian Foster9e96fe62013-01-17 13:11:29 -05004529 if (flags & XFS_BMAPI_STACK_SWITCH)
4530 bma.stack_switch = 1;
4531
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004533 inhole = eof || bma.got.br_startoff > bno;
4534 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 /*
4537 * First, deal with the hole before the allocated space
4538 * that we found, if any.
4539 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004540 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004541 bma.eof = eof;
4542 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4543 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004544 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004545 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004546
Dave Chinnera99ebf42011-12-01 11:24:20 +00004547 /*
4548 * There's a 32/64 bit type mismatch between the
4549 * allocation length request (which can be 64 bits in
4550 * length) and the bma length request, which is
4551 * xfs_extlen_t and therefore 32 bits. Hence we have to
4552 * check for 32-bit overflows and handle them here.
4553 */
4554 if (len > (xfs_filblks_t)MAXEXTLEN)
4555 bma.length = MAXEXTLEN;
4556 else
4557 bma.length = len;
4558
4559 ASSERT(len > 0);
4560 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004561 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 if (error)
4563 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004564 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004565 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004567
Dave Chinneraef9a892011-09-18 20:40:44 +00004568 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004569 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4570 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571
Dave Chinnerb447fe52011-09-18 20:40:51 +00004572 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004573 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004574 if (error == EAGAIN)
4575 continue;
4576 if (error)
4577 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578
Dave Chinneraef9a892011-09-18 20:40:44 +00004579 /* update the extent map to return */
4580 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4581
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 /*
4583 * If we're done, stop now. Stop when we've allocated
4584 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4585 * the transaction may get too big.
4586 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004587 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004589
4590 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004591 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004592 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4593 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4594 &bma.got);
4595 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00004596 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 /*
4601 * Transform from btree to extents, give it cur.
4602 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004603 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004604 int tmp_logflags = 0;
4605
Dave Chinner29c8d172011-09-18 20:41:00 +00004606 ASSERT(bma.cur);
4607 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004609 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 if (error)
4611 goto error0;
4612 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004613
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004615 XFS_IFORK_NEXTENTS(ip, whichfork) >
4616 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618error0:
4619 /*
4620 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004621 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004623 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004625 bma.logflags &= ~xfs_ilog_fext(whichfork);
4626 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004628 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 /*
4630 * Log whatever the flags say, even if error. Otherwise we might miss
4631 * detecting a case where the data is changed, there's an error,
4632 * and it's not logged so we don't shutdown when we should.
4633 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004634 if (bma.logflags)
4635 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004636
Dave Chinner29c8d172011-09-18 20:41:00 +00004637 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 if (!error) {
4639 ASSERT(*firstblock == NULLFSBLOCK ||
4640 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4641 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004642 bma.cur->bc_private.b.firstblock) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 (flist->xbf_low &&
4644 XFS_FSB_TO_AGNO(mp, *firstblock) <
4645 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004646 bma.cur->bc_private.b.firstblock)));
4647 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004649 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4651 }
4652 if (!error)
4653 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4654 orig_nmap, *nmap);
4655 return error;
4656}
4657
4658/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004659 * Called by xfs_bmapi to update file extent records and the btree
4660 * after removing space (or undoing a delayed allocation).
4661 */
4662STATIC int /* error */
4663xfs_bmap_del_extent(
4664 xfs_inode_t *ip, /* incore inode pointer */
4665 xfs_trans_t *tp, /* current transaction pointer */
4666 xfs_extnum_t *idx, /* extent number to update/delete */
4667 xfs_bmap_free_t *flist, /* list of extents to be freed */
4668 xfs_btree_cur_t *cur, /* if null, not a btree */
4669 xfs_bmbt_irec_t *del, /* data to remove from extents */
4670 int *logflagsp, /* inode logging flags */
4671 int whichfork) /* data or attr fork */
4672{
4673 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
4674 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
4675 xfs_fsblock_t del_endblock=0; /* first block past del */
4676 xfs_fileoff_t del_endoff; /* first offset past del */
4677 int delay; /* current block is delayed allocated */
4678 int do_fx; /* free extent at end of routine */
4679 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
4680 int error; /* error return value */
4681 int flags; /* inode logging flags */
4682 xfs_bmbt_irec_t got; /* current extent entry */
4683 xfs_fileoff_t got_endoff; /* first offset past got */
4684 int i; /* temp state */
4685 xfs_ifork_t *ifp; /* inode fork pointer */
4686 xfs_mount_t *mp; /* mount structure */
4687 xfs_filblks_t nblks; /* quota/sb block count */
4688 xfs_bmbt_irec_t new; /* new record to be inserted */
4689 /* REFERENCED */
4690 uint qfield; /* quota field to update */
4691 xfs_filblks_t temp; /* for indirect length calculations */
4692 xfs_filblks_t temp2; /* for indirect length calculations */
4693 int state = 0;
4694
4695 XFS_STATS_INC(xs_del_exlist);
4696
4697 if (whichfork == XFS_ATTR_FORK)
4698 state |= BMAP_ATTRFORK;
4699
4700 mp = ip->i_mount;
4701 ifp = XFS_IFORK_PTR(ip, whichfork);
4702 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4703 (uint)sizeof(xfs_bmbt_rec_t)));
4704 ASSERT(del->br_blockcount > 0);
4705 ep = xfs_iext_get_ext(ifp, *idx);
4706 xfs_bmbt_get_all(ep, &got);
4707 ASSERT(got.br_startoff <= del->br_startoff);
4708 del_endoff = del->br_startoff + del->br_blockcount;
4709 got_endoff = got.br_startoff + got.br_blockcount;
4710 ASSERT(got_endoff >= del_endoff);
4711 delay = isnullstartblock(got.br_startblock);
4712 ASSERT(isnullstartblock(del->br_startblock) == delay);
4713 flags = 0;
4714 qfield = 0;
4715 error = 0;
4716 /*
4717 * If deleting a real allocation, must free up the disk space.
4718 */
4719 if (!delay) {
4720 flags = XFS_ILOG_CORE;
4721 /*
4722 * Realtime allocation. Free it and record di_nblocks update.
4723 */
4724 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4725 xfs_fsblock_t bno;
4726 xfs_filblks_t len;
4727
4728 ASSERT(do_mod(del->br_blockcount,
4729 mp->m_sb.sb_rextsize) == 0);
4730 ASSERT(do_mod(del->br_startblock,
4731 mp->m_sb.sb_rextsize) == 0);
4732 bno = del->br_startblock;
4733 len = del->br_blockcount;
4734 do_div(bno, mp->m_sb.sb_rextsize);
4735 do_div(len, mp->m_sb.sb_rextsize);
4736 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4737 if (error)
4738 goto done;
4739 do_fx = 0;
4740 nblks = len * mp->m_sb.sb_rextsize;
4741 qfield = XFS_TRANS_DQ_RTBCOUNT;
4742 }
4743 /*
4744 * Ordinary allocation.
4745 */
4746 else {
4747 do_fx = 1;
4748 nblks = del->br_blockcount;
4749 qfield = XFS_TRANS_DQ_BCOUNT;
4750 }
4751 /*
4752 * Set up del_endblock and cur for later.
4753 */
4754 del_endblock = del->br_startblock + del->br_blockcount;
4755 if (cur) {
4756 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
4757 got.br_startblock, got.br_blockcount,
4758 &i)))
4759 goto done;
4760 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
4761 }
4762 da_old = da_new = 0;
4763 } else {
4764 da_old = startblockval(got.br_startblock);
4765 da_new = 0;
4766 nblks = 0;
4767 do_fx = 0;
4768 }
4769 /*
4770 * Set flag value to use in switch statement.
4771 * Left-contig is 2, right-contig is 1.
4772 */
4773 switch (((got.br_startoff == del->br_startoff) << 1) |
4774 (got_endoff == del_endoff)) {
4775 case 3:
4776 /*
4777 * Matches the whole extent. Delete the entry.
4778 */
4779 xfs_iext_remove(ip, *idx, 1,
4780 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
4781 --*idx;
4782 if (delay)
4783 break;
4784
4785 XFS_IFORK_NEXT_SET(ip, whichfork,
4786 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4787 flags |= XFS_ILOG_CORE;
4788 if (!cur) {
4789 flags |= xfs_ilog_fext(whichfork);
4790 break;
4791 }
4792 if ((error = xfs_btree_delete(cur, &i)))
4793 goto done;
4794 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
4795 break;
4796
4797 case 2:
4798 /*
4799 * Deleting the first part of the extent.
4800 */
4801 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4802 xfs_bmbt_set_startoff(ep, del_endoff);
4803 temp = got.br_blockcount - del->br_blockcount;
4804 xfs_bmbt_set_blockcount(ep, temp);
4805 if (delay) {
4806 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4807 da_old);
4808 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4809 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4810 da_new = temp;
4811 break;
4812 }
4813 xfs_bmbt_set_startblock(ep, del_endblock);
4814 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4815 if (!cur) {
4816 flags |= xfs_ilog_fext(whichfork);
4817 break;
4818 }
4819 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
4820 got.br_blockcount - del->br_blockcount,
4821 got.br_state)))
4822 goto done;
4823 break;
4824
4825 case 1:
4826 /*
4827 * Deleting the last part of the extent.
4828 */
4829 temp = got.br_blockcount - del->br_blockcount;
4830 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4831 xfs_bmbt_set_blockcount(ep, temp);
4832 if (delay) {
4833 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4834 da_old);
4835 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4836 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4837 da_new = temp;
4838 break;
4839 }
4840 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4841 if (!cur) {
4842 flags |= xfs_ilog_fext(whichfork);
4843 break;
4844 }
4845 if ((error = xfs_bmbt_update(cur, got.br_startoff,
4846 got.br_startblock,
4847 got.br_blockcount - del->br_blockcount,
4848 got.br_state)))
4849 goto done;
4850 break;
4851
4852 case 0:
4853 /*
4854 * Deleting the middle of the extent.
4855 */
4856 temp = del->br_startoff - got.br_startoff;
4857 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4858 xfs_bmbt_set_blockcount(ep, temp);
4859 new.br_startoff = del_endoff;
4860 temp2 = got_endoff - del_endoff;
4861 new.br_blockcount = temp2;
4862 new.br_state = got.br_state;
4863 if (!delay) {
4864 new.br_startblock = del_endblock;
4865 flags |= XFS_ILOG_CORE;
4866 if (cur) {
4867 if ((error = xfs_bmbt_update(cur,
4868 got.br_startoff,
4869 got.br_startblock, temp,
4870 got.br_state)))
4871 goto done;
4872 if ((error = xfs_btree_increment(cur, 0, &i)))
4873 goto done;
4874 cur->bc_rec.b = new;
4875 error = xfs_btree_insert(cur, &i);
4876 if (error && error != ENOSPC)
4877 goto done;
4878 /*
4879 * If get no-space back from btree insert,
4880 * it tried a split, and we have a zero
4881 * block reservation.
4882 * Fix up our state and return the error.
4883 */
4884 if (error == ENOSPC) {
4885 /*
4886 * Reset the cursor, don't trust
4887 * it after any insert operation.
4888 */
4889 if ((error = xfs_bmbt_lookup_eq(cur,
4890 got.br_startoff,
4891 got.br_startblock,
4892 temp, &i)))
4893 goto done;
4894 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
4895 /*
4896 * Update the btree record back
4897 * to the original value.
4898 */
4899 if ((error = xfs_bmbt_update(cur,
4900 got.br_startoff,
4901 got.br_startblock,
4902 got.br_blockcount,
4903 got.br_state)))
4904 goto done;
4905 /*
4906 * Reset the extent record back
4907 * to the original value.
4908 */
4909 xfs_bmbt_set_blockcount(ep,
4910 got.br_blockcount);
4911 flags = 0;
4912 error = XFS_ERROR(ENOSPC);
4913 goto done;
4914 }
4915 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
4916 } else
4917 flags |= xfs_ilog_fext(whichfork);
4918 XFS_IFORK_NEXT_SET(ip, whichfork,
4919 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
4920 } else {
4921 ASSERT(whichfork == XFS_DATA_FORK);
4922 temp = xfs_bmap_worst_indlen(ip, temp);
4923 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4924 temp2 = xfs_bmap_worst_indlen(ip, temp2);
4925 new.br_startblock = nullstartblock((int)temp2);
4926 da_new = temp + temp2;
4927 while (da_new > da_old) {
4928 if (temp) {
4929 temp--;
4930 da_new--;
4931 xfs_bmbt_set_startblock(ep,
4932 nullstartblock((int)temp));
4933 }
4934 if (da_new == da_old)
4935 break;
4936 if (temp2) {
4937 temp2--;
4938 da_new--;
4939 new.br_startblock =
4940 nullstartblock((int)temp2);
4941 }
4942 }
4943 }
4944 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4945 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
4946 ++*idx;
4947 break;
4948 }
4949 /*
4950 * If we need to, add to list of extents to delete.
4951 */
4952 if (do_fx)
4953 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
4954 mp);
4955 /*
4956 * Adjust inode # blocks in the file.
4957 */
4958 if (nblks)
4959 ip->i_d.di_nblocks -= nblks;
4960 /*
4961 * Adjust quota data.
4962 */
4963 if (qfield)
4964 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
4965
4966 /*
4967 * Account for change in delayed indirect blocks.
4968 * Nothing to do for disk quota accounting here.
4969 */
4970 ASSERT(da_old >= da_new);
4971 if (da_old > da_new) {
4972 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4973 (int64_t)(da_old - da_new), 0);
4974 }
4975done:
4976 *logflagsp = flags;
4977 return error;
4978}
4979
4980/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 * Unmap (remove) blocks from a file.
4982 * If nexts is nonzero then the number of extents to remove is limited to
4983 * that value. If not all extents in the block range can be removed then
4984 * *done is set.
4985 */
4986int /* error */
4987xfs_bunmapi(
4988 xfs_trans_t *tp, /* transaction pointer */
4989 struct xfs_inode *ip, /* incore inode */
4990 xfs_fileoff_t bno, /* starting offset to unmap */
4991 xfs_filblks_t len, /* length to unmap in file */
4992 int flags, /* misc flags */
4993 xfs_extnum_t nexts, /* number of extents max */
4994 xfs_fsblock_t *firstblock, /* first allocated block
4995 controls a.g. for allocs */
4996 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4997 int *done) /* set if not done yet */
4998{
4999 xfs_btree_cur_t *cur; /* bmap btree cursor */
5000 xfs_bmbt_irec_t del; /* extent being deleted */
5001 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005002 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 int error; /* error return value */
5004 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005005 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 xfs_ifork_t *ifp; /* inode fork pointer */
5007 int isrt; /* freeing in rt area */
5008 xfs_extnum_t lastx; /* last extent index used */
5009 int logflags; /* transaction logging flags */
5010 xfs_extlen_t mod; /* rt extent offset */
5011 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005012 xfs_extnum_t nextents; /* number of file extents */
5013 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 xfs_fileoff_t start; /* first file offset deleted */
5015 int tmp_logflags; /* partial logging flags */
5016 int wasdel; /* was a delayed alloc extent */
5017 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 xfs_fsblock_t sum;
5019
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005020 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5021
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5023 XFS_ATTR_FORK : XFS_DATA_FORK;
5024 ifp = XFS_IFORK_PTR(ip, whichfork);
5025 if (unlikely(
5026 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5027 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5028 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5029 ip->i_mount);
5030 return XFS_ERROR(EFSCORRUPTED);
5031 }
5032 mp = ip->i_mount;
5033 if (XFS_FORCED_SHUTDOWN(mp))
5034 return XFS_ERROR(EIO);
Christoph Hellwig54893272011-05-11 15:04:03 +00005035
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036 ASSERT(len > 0);
5037 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005038
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5040 (error = xfs_iread_extents(tp, ip, whichfork)))
5041 return error;
5042 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5043 if (nextents == 0) {
5044 *done = 1;
5045 return 0;
5046 }
5047 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005048 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 start = bno;
5050 bno = start + len - 1;
5051 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5052 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005053
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054 /*
5055 * Check to see if the given block number is past the end of the
5056 * file, back up to the last block if so...
5057 */
5058 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005059 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060 xfs_bmbt_get_all(ep, &got);
5061 bno = got.br_startoff + got.br_blockcount - 1;
5062 }
5063 logflags = 0;
5064 if (ifp->if_flags & XFS_IFBROOT) {
5065 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005066 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 cur->bc_private.b.firstblock = *firstblock;
5068 cur->bc_private.b.flist = flist;
5069 cur->bc_private.b.flags = 0;
5070 } else
5071 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005072
5073 if (isrt) {
5074 /*
5075 * Synchronize by locking the bitmap inode.
5076 */
5077 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5078 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5079 }
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 extno = 0;
5082 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5083 (nexts == 0 || extno < nexts)) {
5084 /*
5085 * Is the found extent after a hole in which bno lives?
5086 * Just back up to the previous extent, if so.
5087 */
5088 if (got.br_startoff > bno) {
5089 if (--lastx < 0)
5090 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005091 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 xfs_bmbt_get_all(ep, &got);
5093 }
5094 /*
5095 * Is the last block of this extent before the range
5096 * we're supposed to delete? If so, we're done.
5097 */
5098 bno = XFS_FILEOFF_MIN(bno,
5099 got.br_startoff + got.br_blockcount - 1);
5100 if (bno < start)
5101 break;
5102 /*
5103 * Then deal with the (possibly delayed) allocated space
5104 * we found.
5105 */
5106 ASSERT(ep != NULL);
5107 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005108 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 if (got.br_startoff < start) {
5110 del.br_startoff = start;
5111 del.br_blockcount -= start - got.br_startoff;
5112 if (!wasdel)
5113 del.br_startblock += start - got.br_startoff;
5114 }
5115 if (del.br_startoff + del.br_blockcount > bno + 1)
5116 del.br_blockcount = bno + 1 - del.br_startoff;
5117 sum = del.br_startblock + del.br_blockcount;
5118 if (isrt &&
5119 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5120 /*
5121 * Realtime extent not lined up at the end.
5122 * The extent could have been split into written
5123 * and unwritten pieces, or we could just be
5124 * unmapping part of it. But we can't really
5125 * get rid of part of a realtime extent.
5126 */
5127 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005128 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 /*
5130 * This piece is unwritten, or we're not
5131 * using unwritten extents. Skip over it.
5132 */
5133 ASSERT(bno >= mod);
5134 bno -= mod > del.br_blockcount ?
5135 del.br_blockcount : mod;
5136 if (bno < got.br_startoff) {
5137 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005138 xfs_bmbt_get_all(xfs_iext_get_ext(
5139 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 }
5141 continue;
5142 }
5143 /*
5144 * It's written, turn it unwritten.
5145 * This is better than zeroing it.
5146 */
5147 ASSERT(del.br_state == XFS_EXT_NORM);
5148 ASSERT(xfs_trans_get_block_res(tp) > 0);
5149 /*
5150 * If this spans a realtime extent boundary,
5151 * chop it back to the start of the one we end at.
5152 */
5153 if (del.br_blockcount > mod) {
5154 del.br_startoff += del.br_blockcount - mod;
5155 del.br_startblock += del.br_blockcount - mod;
5156 del.br_blockcount = mod;
5157 }
5158 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005159 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5160 &lastx, &cur, &del, firstblock, flist,
5161 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 if (error)
5163 goto error0;
5164 goto nodelete;
5165 }
5166 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5167 /*
5168 * Realtime extent is lined up at the end but not
5169 * at the front. We'll get rid of full extents if
5170 * we can.
5171 */
5172 mod = mp->m_sb.sb_rextsize - mod;
5173 if (del.br_blockcount > mod) {
5174 del.br_blockcount -= mod;
5175 del.br_startoff += mod;
5176 del.br_startblock += mod;
5177 } else if ((del.br_startoff == start &&
5178 (del.br_state == XFS_EXT_UNWRITTEN ||
5179 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005180 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 /*
5182 * Can't make it unwritten. There isn't
5183 * a full extent here so just skip it.
5184 */
5185 ASSERT(bno >= del.br_blockcount);
5186 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005187 if (got.br_startoff > bno) {
5188 if (--lastx >= 0) {
5189 ep = xfs_iext_get_ext(ifp,
5190 lastx);
5191 xfs_bmbt_get_all(ep, &got);
5192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005193 }
5194 continue;
5195 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5196 /*
5197 * This one is already unwritten.
5198 * It must have a written left neighbor.
5199 * Unwrite the killed part of that one and
5200 * try again.
5201 */
5202 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005203 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5204 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005206 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 ASSERT(del.br_startblock ==
5208 prev.br_startblock + prev.br_blockcount);
5209 if (prev.br_startoff < start) {
5210 mod = start - prev.br_startoff;
5211 prev.br_blockcount -= mod;
5212 prev.br_startblock += mod;
5213 prev.br_startoff = start;
5214 }
5215 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005216 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005217 error = xfs_bmap_add_extent_unwritten_real(tp,
5218 ip, &lastx, &cur, &prev,
5219 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005220 if (error)
5221 goto error0;
5222 goto nodelete;
5223 } else {
5224 ASSERT(del.br_state == XFS_EXT_NORM);
5225 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005226 error = xfs_bmap_add_extent_unwritten_real(tp,
5227 ip, &lastx, &cur, &del,
5228 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005229 if (error)
5230 goto error0;
5231 goto nodelete;
5232 }
5233 }
5234 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005235 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005236 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005237 if (isrt) {
5238 xfs_filblks_t rtexts;
5239
5240 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5241 do_div(rtexts, mp->m_sb.sb_rextsize);
5242 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005243 (int64_t)rtexts, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005244 (void)xfs_trans_reserve_quota_nblks(NULL,
5245 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005246 XFS_QMOPT_RES_RTBLKS);
5247 } else {
Christoph Hellwig96540c72010-09-30 02:25:55 +00005248 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
Christoph Hellwig54893272011-05-11 15:04:03 +00005249 (int64_t)del.br_blockcount, 0);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005250 (void)xfs_trans_reserve_quota_nblks(NULL,
5251 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 ip->i_delayed_blks -= del.br_blockcount;
5255 if (cur)
5256 cur->bc_private.b.flags |=
5257 XFS_BTCUR_BPRV_WASDEL;
5258 } else if (cur)
5259 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5260 /*
5261 * If it's the case where the directory code is running
5262 * with no block reservation, and the deleted block is in
5263 * the middle of its extent, and the resulting insert
5264 * of an extent would cause transformation to btree format,
5265 * then reject it. The calling code will then swap
5266 * blocks around instead.
5267 * We have to do this now, rather than waiting for the
5268 * conversion to btree format, since the transaction
5269 * will be dirty.
5270 */
5271 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5272 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005273 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5274 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005275 del.br_startoff > got.br_startoff &&
5276 del.br_startoff + del.br_blockcount <
5277 got.br_startoff + got.br_blockcount) {
5278 error = XFS_ERROR(ENOSPC);
5279 goto error0;
5280 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00005281 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005282 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283 logflags |= tmp_logflags;
5284 if (error)
5285 goto error0;
5286 bno = del.br_startoff - 1;
5287nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005288 /*
5289 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005291 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005292 if (lastx >= 0) {
5293 ep = xfs_iext_get_ext(ifp, lastx);
5294 if (xfs_bmbt_get_startoff(ep) > bno) {
5295 if (--lastx >= 0)
5296 ep = xfs_iext_get_ext(ifp,
5297 lastx);
5298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 extno++;
5302 }
5303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005305
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 /*
5307 * Convert to a btree if necessary.
5308 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005309 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 ASSERT(cur == NULL);
5311 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5312 &cur, 0, &tmp_logflags, whichfork);
5313 logflags |= tmp_logflags;
5314 if (error)
5315 goto error0;
5316 }
5317 /*
5318 * transform from btree to extents, give it cur
5319 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005320 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 ASSERT(cur != NULL);
5322 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5323 whichfork);
5324 logflags |= tmp_logflags;
5325 if (error)
5326 goto error0;
5327 }
5328 /*
5329 * transform from extents to local?
5330 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 error = 0;
5332error0:
5333 /*
5334 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005335 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005336 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005337 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005339 logflags &= ~xfs_ilog_fext(whichfork);
5340 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005342 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 /*
5344 * Log inode even in the error case, if the transaction
5345 * is dirty we'll need to shut down the filesystem.
5346 */
5347 if (logflags)
5348 xfs_trans_log_inode(tp, ip, logflags);
5349 if (cur) {
5350 if (!error) {
5351 *firstblock = cur->bc_private.b.firstblock;
5352 cur->bc_private.b.allocated = 0;
5353 }
5354 xfs_btree_del_cursor(cur,
5355 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5356 }
5357 return error;
5358}