blob: 041b6948aeccd928f6f88d95ed9985052d915390 [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 Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100026#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100029#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110032#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_extfree_item.h"
35#include "xfs_alloc.h"
36#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100037#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110038#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_rtalloc.h"
40#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_quota.h"
42#include "xfs_trans_space.h"
43#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000044#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110045#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110046#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110047#include "xfs_filestream.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050kmem_zone_t *xfs_bmap_free_item_zone;
51
52/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110053 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110057 * Compute and fill in the value of the maximum depth of a bmap btree
58 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110060void
61xfs_bmap_compute_maxlevels(
62 xfs_mount_t *mp, /* file system mount structure */
63 int whichfork) /* data or attr fork */
64{
65 int level; /* btree level */
66 uint maxblocks; /* max blocks at this level */
67 uint maxleafents; /* max leaf entries possible */
68 int maxrootrecs; /* max records in root block */
69 int minleafrecs; /* min records in leaf block */
70 int minnoderecs; /* min records in node block */
71 int sz; /* root block size */
72
73 /*
74 * The maximum number of extents in a file, hence the maximum
75 * number of leaf entries, is controlled by the type of di_nextents
76 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
77 * (a signed 16-bit number, xfs_aextnum_t).
78 *
79 * Note that we can no longer assume that if we are in ATTR1 that
80 * the fork offset of all the inodes will be
81 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
82 * with ATTR2 and then mounted back with ATTR1, keeping the
83 * di_forkoff's fixed but probably at various positions. Therefore,
84 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
85 * of a minimum size available.
86 */
87 if (whichfork == XFS_DATA_FORK) {
88 maxleafents = MAXEXTNUM;
89 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
90 } else {
91 maxleafents = MAXAEXTNUM;
92 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
93 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100094 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +110095 minleafrecs = mp->m_bmap_dmnr[0];
96 minnoderecs = mp->m_bmap_dmnr[1];
97 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
98 for (level = 1; maxblocks > 1; level++) {
99 if (maxblocks <= maxrootrecs)
100 maxblocks = 1;
101 else
102 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
103 }
104 mp->m_bm_maxlevels[whichfork] = level;
105}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100107STATIC int /* error */
108xfs_bmbt_lookup_eq(
109 struct xfs_btree_cur *cur,
110 xfs_fileoff_t off,
111 xfs_fsblock_t bno,
112 xfs_filblks_t len,
113 int *stat) /* success/failure */
114{
115 cur->bc_rec.b.br_startoff = off;
116 cur->bc_rec.b.br_startblock = bno;
117 cur->bc_rec.b.br_blockcount = len;
118 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
119}
120
121STATIC int /* error */
122xfs_bmbt_lookup_ge(
123 struct xfs_btree_cur *cur,
124 xfs_fileoff_t off,
125 xfs_fsblock_t bno,
126 xfs_filblks_t len,
127 int *stat) /* success/failure */
128{
129 cur->bc_rec.b.br_startoff = off;
130 cur->bc_rec.b.br_startblock = bno;
131 cur->bc_rec.b.br_blockcount = len;
132 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
133}
134
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100135/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000136 * Check if the inode needs to be converted to btree format.
137 */
138static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
139{
140 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
141 XFS_IFORK_NEXTENTS(ip, whichfork) >
142 XFS_IFORK_MAXEXT(ip, whichfork);
143}
144
145/*
146 * Check if the inode should be converted to extent format.
147 */
148static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
149{
150 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
151 XFS_IFORK_NEXTENTS(ip, whichfork) <=
152 XFS_IFORK_MAXEXT(ip, whichfork);
153}
154
155/*
156 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100157 * by [off, bno, len, state].
158 * This either works (return 0) or gets an EFSCORRUPTED error.
159 */
160STATIC int
161xfs_bmbt_update(
162 struct xfs_btree_cur *cur,
163 xfs_fileoff_t off,
164 xfs_fsblock_t bno,
165 xfs_filblks_t len,
166 xfs_exntst_t state)
167{
168 union xfs_btree_rec rec;
169
170 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
171 return xfs_btree_update(cur, &rec);
172}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100175 * Compute the worst-case number of indirect blocks that will be used
176 * for ip's delayed extent of length "len".
177 */
178STATIC xfs_filblks_t
179xfs_bmap_worst_indlen(
180 xfs_inode_t *ip, /* incore inode pointer */
181 xfs_filblks_t len) /* delayed extent length */
182{
183 int level; /* btree level number */
184 int maxrecs; /* maximum record count at this level */
185 xfs_mount_t *mp; /* mount structure */
186 xfs_filblks_t rval; /* return value */
187
188 mp = ip->i_mount;
189 maxrecs = mp->m_bmap_dmxr[0];
190 for (level = 0, rval = 0;
191 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
192 level++) {
193 len += maxrecs - 1;
194 do_div(len, maxrecs);
195 rval += len;
196 if (len == 1)
197 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
198 level - 1;
199 if (level == 0)
200 maxrecs = mp->m_bmap_dmxr[1];
201 }
202 return rval;
203}
204
205/*
206 * Calculate the default attribute fork offset for newly created inodes.
207 */
208uint
209xfs_default_attroffset(
210 struct xfs_inode *ip)
211{
212 struct xfs_mount *mp = ip->i_mount;
213 uint offset;
214
215 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100216 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100217 XFS_BMDR_SPACE_CALC(MINABTPTRS);
218 } else {
219 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
220 }
221
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100222 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100223 return offset;
224}
225
226/*
227 * Helper routine to reset inode di_forkoff field when switching
228 * attribute fork from local to extent format - we reset it where
229 * possible to make space available for inline data fork extents.
230 */
231STATIC void
232xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100233 xfs_inode_t *ip,
234 int whichfork)
235{
236 if (whichfork == XFS_ATTR_FORK &&
237 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
238 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
239 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
240 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
241
242 if (dfl_forkoff > ip->i_d.di_forkoff)
243 ip->i_d.di_forkoff = dfl_forkoff;
244 }
245}
246
Dave Chinner9e5987a2013-02-25 12:31:26 +1100247#ifdef DEBUG
248STATIC struct xfs_buf *
249xfs_bmap_get_bp(
250 struct xfs_btree_cur *cur,
251 xfs_fsblock_t bno)
252{
253 struct xfs_log_item_desc *lidp;
254 int i;
255
256 if (!cur)
257 return NULL;
258
259 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
260 if (!cur->bc_bufs[i])
261 break;
262 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
263 return cur->bc_bufs[i];
264 }
265
266 /* Chase down all the log items to see if the bp is there */
267 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
268 struct xfs_buf_log_item *bip;
269 bip = (struct xfs_buf_log_item *)lidp->lid_item;
270 if (bip->bli_item.li_type == XFS_LI_BUF &&
271 XFS_BUF_ADDR(bip->bli_buf) == bno)
272 return bip->bli_buf;
273 }
274
275 return NULL;
276}
277
278STATIC void
279xfs_check_block(
280 struct xfs_btree_block *block,
281 xfs_mount_t *mp,
282 int root,
283 short sz)
284{
285 int i, j, dmxr;
286 __be64 *pp, *thispa; /* pointer to block address */
287 xfs_bmbt_key_t *prevp, *keyp;
288
289 ASSERT(be16_to_cpu(block->bb_level) > 0);
290
291 prevp = NULL;
292 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
293 dmxr = mp->m_bmap_dmxr[0];
294 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
295
296 if (prevp) {
297 ASSERT(be64_to_cpu(prevp->br_startoff) <
298 be64_to_cpu(keyp->br_startoff));
299 }
300 prevp = keyp;
301
302 /*
303 * Compare the block numbers to see if there are dups.
304 */
305 if (root)
306 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
307 else
308 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
309
310 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
311 if (root)
312 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
313 else
314 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
315 if (*thispa == *pp) {
316 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
317 __func__, j, i,
318 (unsigned long long)be64_to_cpu(*thispa));
319 panic("%s: ptrs are equal in node\n",
320 __func__);
321 }
322 }
323 }
324}
325
326/*
327 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100328 * btree leaves. THis becomes prohibitively expensive for large extent count
329 * files, so don't bother with inodes that have more than 10,000 extents in
330 * them. The btree record ordering checks will still be done, so for such large
331 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100332 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100333STATIC void
334xfs_bmap_check_leaf_extents(
335 xfs_btree_cur_t *cur, /* btree cursor or null */
336 xfs_inode_t *ip, /* incore inode pointer */
337 int whichfork) /* data or attr fork */
338{
339 struct xfs_btree_block *block; /* current btree block */
340 xfs_fsblock_t bno; /* block # of "block" */
341 xfs_buf_t *bp; /* buffer for "block" */
342 int error; /* error return value */
343 xfs_extnum_t i=0, j; /* index into the extents list */
344 xfs_ifork_t *ifp; /* fork structure */
345 int level; /* btree level, for checking */
346 xfs_mount_t *mp; /* file system mount structure */
347 __be64 *pp; /* pointer to block address */
348 xfs_bmbt_rec_t *ep; /* pointer to current extent */
349 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
350 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
351 int bp_release = 0;
352
353 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
354 return;
355 }
356
Dave Chinnere3543812016-01-08 11:28:49 +1100357 /* skip large extent count inodes */
358 if (ip->i_d.di_nextents > 10000)
359 return;
360
Dave Chinner9e5987a2013-02-25 12:31:26 +1100361 bno = NULLFSBLOCK;
362 mp = ip->i_mount;
363 ifp = XFS_IFORK_PTR(ip, whichfork);
364 block = ifp->if_broot;
365 /*
366 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
367 */
368 level = be16_to_cpu(block->bb_level);
369 ASSERT(level > 0);
370 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
371 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
372 bno = be64_to_cpu(*pp);
373
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000374 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100375 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
376 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
377
378 /*
379 * Go down the tree until leaf level is reached, following the first
380 * pointer (leftmost) at each level.
381 */
382 while (level-- > 0) {
383 /* See if buf is in cur first */
384 bp_release = 0;
385 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
386 if (!bp) {
387 bp_release = 1;
388 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
389 XFS_BMAP_BTREE_REF,
390 &xfs_bmbt_buf_ops);
391 if (error)
392 goto error_norelse;
393 }
394 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100395 if (level == 0)
396 break;
397
398 /*
399 * Check this block for basic sanity (increasing keys and
400 * no duplicate blocks).
401 */
402
403 xfs_check_block(block, mp, 0, 0);
404 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
405 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100406 XFS_WANT_CORRUPTED_GOTO(mp,
407 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100408 if (bp_release) {
409 bp_release = 0;
410 xfs_trans_brelse(NULL, bp);
411 }
412 }
413
414 /*
415 * Here with bp and block set to the leftmost leaf node in the tree.
416 */
417 i = 0;
418
419 /*
420 * Loop over all leaf nodes checking that all extents are in the right order.
421 */
422 for (;;) {
423 xfs_fsblock_t nextbno;
424 xfs_extnum_t num_recs;
425
426
427 num_recs = xfs_btree_get_numrecs(block);
428
429 /*
430 * Read-ahead the next leaf block, if any.
431 */
432
433 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
434
435 /*
436 * Check all the extents to make sure they are OK.
437 * If we had a previous block, the last entry should
438 * conform with the first entry in this one.
439 */
440
441 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
442 if (i) {
443 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
444 xfs_bmbt_disk_get_blockcount(&last) <=
445 xfs_bmbt_disk_get_startoff(ep));
446 }
447 for (j = 1; j < num_recs; j++) {
448 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
449 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
450 xfs_bmbt_disk_get_blockcount(ep) <=
451 xfs_bmbt_disk_get_startoff(nextp));
452 ep = nextp;
453 }
454
455 last = *ep;
456 i += num_recs;
457 if (bp_release) {
458 bp_release = 0;
459 xfs_trans_brelse(NULL, bp);
460 }
461 bno = nextbno;
462 /*
463 * If we've reached the end, stop.
464 */
465 if (bno == NULLFSBLOCK)
466 break;
467
468 bp_release = 0;
469 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
470 if (!bp) {
471 bp_release = 1;
472 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
473 XFS_BMAP_BTREE_REF,
474 &xfs_bmbt_buf_ops);
475 if (error)
476 goto error_norelse;
477 }
478 block = XFS_BUF_TO_BLOCK(bp);
479 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100480
Dave Chinner9e5987a2013-02-25 12:31:26 +1100481 return;
482
483error0:
484 xfs_warn(mp, "%s: at error0", __func__);
485 if (bp_release)
486 xfs_trans_brelse(NULL, bp);
487error_norelse:
488 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
489 __func__, i);
490 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
491 return;
492}
493
494/*
495 * Add bmap trace insert entries for all the contents of the extent records.
496 */
497void
498xfs_bmap_trace_exlist(
499 xfs_inode_t *ip, /* incore inode pointer */
500 xfs_extnum_t cnt, /* count of entries in the list */
501 int whichfork, /* data or attr fork */
502 unsigned long caller_ip)
503{
504 xfs_extnum_t idx; /* extent record index */
505 xfs_ifork_t *ifp; /* inode fork pointer */
506 int state = 0;
507
508 if (whichfork == XFS_ATTR_FORK)
509 state |= BMAP_ATTRFORK;
510
511 ifp = XFS_IFORK_PTR(ip, whichfork);
512 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
513 for (idx = 0; idx < cnt; idx++)
514 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
515}
516
517/*
518 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000519 * given the caller's original parameters. Specifically check the
520 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100521 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
522 */
523STATIC void
524xfs_bmap_validate_ret(
525 xfs_fileoff_t bno,
526 xfs_filblks_t len,
527 int flags,
528 xfs_bmbt_irec_t *mval,
529 int nmap,
530 int ret_nmap)
531{
532 int i; /* index to map values */
533
534 ASSERT(ret_nmap <= nmap);
535
536 for (i = 0; i < ret_nmap; i++) {
537 ASSERT(mval[i].br_blockcount > 0);
538 if (!(flags & XFS_BMAPI_ENTIRE)) {
539 ASSERT(mval[i].br_startoff >= bno);
540 ASSERT(mval[i].br_blockcount <= len);
541 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
542 bno + len);
543 } else {
544 ASSERT(mval[i].br_startoff < bno + len);
545 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
546 bno);
547 }
548 ASSERT(i == 0 ||
549 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
550 mval[i].br_startoff);
551 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
552 mval[i].br_startblock != HOLESTARTBLOCK);
553 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
554 mval[i].br_state == XFS_EXT_UNWRITTEN);
555 }
556}
557
558#else
559#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
560#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
561#endif /* DEBUG */
562
563/*
564 * bmap free list manipulation functions
565 */
566
567/*
568 * Add the extent to the list of extents to be free at transaction end.
569 * The list is maintained sorted (by block number).
570 */
571void
572xfs_bmap_add_free(
573 xfs_fsblock_t bno, /* fs block number of extent */
574 xfs_filblks_t len, /* length of extent */
575 xfs_bmap_free_t *flist, /* list of extents */
576 xfs_mount_t *mp) /* mount point structure */
577{
578 xfs_bmap_free_item_t *cur; /* current (next) element */
579 xfs_bmap_free_item_t *new; /* new element */
580 xfs_bmap_free_item_t *prev; /* previous element */
581#ifdef DEBUG
582 xfs_agnumber_t agno;
583 xfs_agblock_t agbno;
584
585 ASSERT(bno != NULLFSBLOCK);
586 ASSERT(len > 0);
587 ASSERT(len <= MAXEXTLEN);
588 ASSERT(!isnullstartblock(bno));
589 agno = XFS_FSB_TO_AGNO(mp, bno);
590 agbno = XFS_FSB_TO_AGBNO(mp, bno);
591 ASSERT(agno < mp->m_sb.sb_agcount);
592 ASSERT(agbno < mp->m_sb.sb_agblocks);
593 ASSERT(len < mp->m_sb.sb_agblocks);
594 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
595#endif
596 ASSERT(xfs_bmap_free_item_zone != NULL);
597 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
598 new->xbfi_startblock = bno;
599 new->xbfi_blockcount = (xfs_extlen_t)len;
600 for (prev = NULL, cur = flist->xbf_first;
601 cur != NULL;
602 prev = cur, cur = cur->xbfi_next) {
603 if (cur->xbfi_startblock >= bno)
604 break;
605 }
606 if (prev)
607 prev->xbfi_next = new;
608 else
609 flist->xbf_first = new;
610 new->xbfi_next = cur;
611 flist->xbf_count++;
612}
613
614/*
615 * Remove the entry "free" from the free item list. Prev points to the
616 * previous entry, unless "free" is the head of the list.
617 */
Dave Chinner68988112013-08-12 20:49:42 +1000618void
Dave Chinner9e5987a2013-02-25 12:31:26 +1100619xfs_bmap_del_free(
620 xfs_bmap_free_t *flist, /* free item list header */
621 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
622 xfs_bmap_free_item_t *free) /* list item to be freed */
623{
624 if (prev)
625 prev->xbfi_next = free->xbfi_next;
626 else
627 flist->xbf_first = free->xbfi_next;
628 flist->xbf_count--;
629 kmem_zone_free(xfs_bmap_free_item_zone, free);
630}
631
Dave Chinner9e5987a2013-02-25 12:31:26 +1100632/*
633 * Free up any items left in the list.
634 */
635void
636xfs_bmap_cancel(
637 xfs_bmap_free_t *flist) /* list of bmap_free_items */
638{
639 xfs_bmap_free_item_t *free; /* free list item */
640 xfs_bmap_free_item_t *next;
641
642 if (flist->xbf_count == 0)
643 return;
644 ASSERT(flist->xbf_first != NULL);
645 for (free = flist->xbf_first; free; free = next) {
646 next = free->xbfi_next;
647 xfs_bmap_del_free(flist, NULL, free);
648 }
649 ASSERT(flist->xbf_count == 0);
650}
651
652/*
653 * Inode fork format manipulation functions
654 */
655
656/*
657 * Transform a btree format file with only one leaf node, where the
658 * extents list will fit in the inode, into an extents format file.
659 * Since the file extents are already in-core, all we have to do is
660 * give up the space for the btree root and pitch the leaf block.
661 */
662STATIC int /* error */
663xfs_bmap_btree_to_extents(
664 xfs_trans_t *tp, /* transaction pointer */
665 xfs_inode_t *ip, /* incore inode pointer */
666 xfs_btree_cur_t *cur, /* btree cursor */
667 int *logflagsp, /* inode logging flags */
668 int whichfork) /* data or attr fork */
669{
670 /* REFERENCED */
671 struct xfs_btree_block *cblock;/* child btree block */
672 xfs_fsblock_t cbno; /* child block number */
673 xfs_buf_t *cbp; /* child block's buffer */
674 int error; /* error return value */
675 xfs_ifork_t *ifp; /* inode fork data */
676 xfs_mount_t *mp; /* mount point structure */
677 __be64 *pp; /* ptr to block address */
678 struct xfs_btree_block *rblock;/* root btree block */
679
680 mp = ip->i_mount;
681 ifp = XFS_IFORK_PTR(ip, whichfork);
682 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
683 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
684 rblock = ifp->if_broot;
685 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
686 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
687 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
688 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
689 cbno = be64_to_cpu(*pp);
690 *logflagsp = 0;
691#ifdef DEBUG
692 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
693 return error;
694#endif
695 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
696 &xfs_bmbt_buf_ops);
697 if (error)
698 return error;
699 cblock = XFS_BUF_TO_BLOCK(cbp);
700 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
701 return error;
702 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
703 ip->i_d.di_nblocks--;
704 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
705 xfs_trans_binval(tp, cbp);
706 if (cur->bc_bufs[0] == cbp)
707 cur->bc_bufs[0] = NULL;
708 xfs_iroot_realloc(ip, -1, whichfork);
709 ASSERT(ifp->if_broot == NULL);
710 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
711 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
712 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
713 return 0;
714}
715
716/*
717 * Convert an extents-format file into a btree-format file.
718 * The new file will have a root block (in the inode) and a single child block.
719 */
720STATIC int /* error */
721xfs_bmap_extents_to_btree(
722 xfs_trans_t *tp, /* transaction pointer */
723 xfs_inode_t *ip, /* incore inode pointer */
724 xfs_fsblock_t *firstblock, /* first-block-allocated */
725 xfs_bmap_free_t *flist, /* blocks freed in xaction */
726 xfs_btree_cur_t **curp, /* cursor returned to caller */
727 int wasdel, /* converting a delayed alloc */
728 int *logflagsp, /* inode logging flags */
729 int whichfork) /* data or attr fork */
730{
731 struct xfs_btree_block *ablock; /* allocated (child) bt block */
732 xfs_buf_t *abp; /* buffer for ablock */
733 xfs_alloc_arg_t args; /* allocation arguments */
734 xfs_bmbt_rec_t *arp; /* child record pointer */
735 struct xfs_btree_block *block; /* btree root block */
736 xfs_btree_cur_t *cur; /* bmap btree cursor */
737 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
738 int error; /* error return value */
739 xfs_extnum_t i, cnt; /* extent record index */
740 xfs_ifork_t *ifp; /* inode fork pointer */
741 xfs_bmbt_key_t *kp; /* root block key pointer */
742 xfs_mount_t *mp; /* mount structure */
743 xfs_extnum_t nextents; /* number of file extents */
744 xfs_bmbt_ptr_t *pp; /* root block address pointer */
745
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500746 mp = ip->i_mount;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100747 ifp = XFS_IFORK_PTR(ip, whichfork);
748 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
749
750 /*
751 * Make space in the inode incore.
752 */
753 xfs_iroot_realloc(ip, 1, whichfork);
754 ifp->if_flags |= XFS_IFBROOT;
755
756 /*
757 * Fill in the root.
758 */
759 block = ifp->if_broot;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500760 if (xfs_sb_version_hascrc(&mp->m_sb))
761 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
762 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
763 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
764 else
765 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
766 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
767 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100768
769 /*
770 * Need a cursor. Can't allocate until bb_level is filled in.
771 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100772 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
773 cur->bc_private.b.firstblock = *firstblock;
774 cur->bc_private.b.flist = flist;
775 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
776 /*
777 * Convert to a btree with two levels, one record in root.
778 */
779 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
780 memset(&args, 0, sizeof(args));
781 args.tp = tp;
782 args.mp = mp;
783 args.firstblock = *firstblock;
784 if (*firstblock == NULLFSBLOCK) {
785 args.type = XFS_ALLOCTYPE_START_BNO;
786 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
787 } else if (flist->xbf_low) {
788 args.type = XFS_ALLOCTYPE_START_BNO;
789 args.fsbno = *firstblock;
790 } else {
791 args.type = XFS_ALLOCTYPE_NEAR_BNO;
792 args.fsbno = *firstblock;
793 }
794 args.minlen = args.maxlen = args.prod = 1;
795 args.wasdel = wasdel;
796 *logflagsp = 0;
797 if ((error = xfs_alloc_vextent(&args))) {
798 xfs_iroot_realloc(ip, -1, whichfork);
799 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
800 return error;
801 }
802 /*
803 * Allocation can't fail, the space was reserved.
804 */
805 ASSERT(args.fsbno != NULLFSBLOCK);
806 ASSERT(*firstblock == NULLFSBLOCK ||
807 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
808 (flist->xbf_low &&
809 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
810 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
811 cur->bc_private.b.allocated++;
812 ip->i_d.di_nblocks++;
813 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
814 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
815 /*
816 * Fill in the child block.
817 */
818 abp->b_ops = &xfs_bmbt_buf_ops;
819 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500820 if (xfs_sb_version_hascrc(&mp->m_sb))
821 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
822 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
823 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
824 else
825 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
826 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
827 XFS_BTREE_LONG_PTRS);
828
Dave Chinner9e5987a2013-02-25 12:31:26 +1100829 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
830 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
831 for (cnt = i = 0; i < nextents; i++) {
832 ep = xfs_iext_get_ext(ifp, i);
833 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
834 arp->l0 = cpu_to_be64(ep->l0);
835 arp->l1 = cpu_to_be64(ep->l1);
836 arp++; cnt++;
837 }
838 }
839 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
840 xfs_btree_set_numrecs(ablock, cnt);
841
842 /*
843 * Fill in the root key and pointer.
844 */
845 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
846 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
847 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
848 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
849 be16_to_cpu(block->bb_level)));
850 *pp = cpu_to_be64(args.fsbno);
851
852 /*
853 * Do all this logging at the end so that
854 * the root is at the right level.
855 */
856 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
857 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
858 ASSERT(*curp == NULL);
859 *curp = cur;
860 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
861 return 0;
862}
863
864/*
865 * Convert a local file to an extents file.
866 * This code is out of bounds for data forks of regular files,
867 * since the file data needs to get logged so things will stay consistent.
868 * (The bmap-level manipulations are ok, though).
869 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000870void
871xfs_bmap_local_to_extents_empty(
872 struct xfs_inode *ip,
873 int whichfork)
874{
875 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
876
877 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
878 ASSERT(ifp->if_bytes == 0);
879 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
880
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000881 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000882 ifp->if_flags &= ~XFS_IFINLINE;
883 ifp->if_flags |= XFS_IFEXTENTS;
884 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
885}
886
887
Dave Chinner9e5987a2013-02-25 12:31:26 +1100888STATIC int /* error */
889xfs_bmap_local_to_extents(
890 xfs_trans_t *tp, /* transaction pointer */
891 xfs_inode_t *ip, /* incore inode pointer */
892 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
893 xfs_extlen_t total, /* total blocks needed by transaction */
894 int *logflagsp, /* inode logging flags */
895 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500896 void (*init_fn)(struct xfs_trans *tp,
897 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100898 struct xfs_inode *ip,
899 struct xfs_ifork *ifp))
900{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000901 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100902 int flags; /* logging flags returned */
903 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000904 xfs_alloc_arg_t args; /* allocation arguments */
905 xfs_buf_t *bp; /* buffer for extent block */
906 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100907
908 /*
909 * We don't want to deal with the case of keeping inode data inline yet.
910 * So sending the data fork of a regular inode is invalid.
911 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100912 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100913 ifp = XFS_IFORK_PTR(ip, whichfork);
914 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000915
916 if (!ifp->if_bytes) {
917 xfs_bmap_local_to_extents_empty(ip, whichfork);
918 flags = XFS_ILOG_CORE;
919 goto done;
920 }
921
Dave Chinner9e5987a2013-02-25 12:31:26 +1100922 flags = 0;
923 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000924 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
925 XFS_IFINLINE);
926 memset(&args, 0, sizeof(args));
927 args.tp = tp;
928 args.mp = ip->i_mount;
929 args.firstblock = *firstblock;
930 /*
931 * Allocate a block. We know we need only one, since the
932 * file currently fits in an inode.
933 */
934 if (*firstblock == NULLFSBLOCK) {
935 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
936 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100937 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000938 args.fsbno = *firstblock;
939 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100940 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000941 args.total = total;
942 args.minlen = args.maxlen = args.prod = 1;
943 error = xfs_alloc_vextent(&args);
944 if (error)
945 goto done;
946
947 /* Can't fail, the space was reserved. */
948 ASSERT(args.fsbno != NULLFSBLOCK);
949 ASSERT(args.len == 1);
950 *firstblock = args.fsbno;
951 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
952
Dave Chinnerfe22d552015-01-22 09:30:06 +1100953 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100954 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100955 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100956 * The callout is responsible for logging because the remote format
957 * might differ from the local format and thus we don't know how much to
958 * log here. Note that init_fn must also set the buffer log item type
959 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100960 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000961 init_fn(tp, bp, ip, ifp);
962
Brian Fosterb7cdc662015-10-12 15:40:24 +1100963 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000964 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
965 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100966 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000967
968 xfs_iext_add(ifp, 0, 1);
969 ep = xfs_iext_get_ext(ifp, 0);
970 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
971 trace_xfs_bmap_post_update(ip, 0,
972 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
973 _THIS_IP_);
974 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
975 ip->i_d.di_nblocks = 1;
976 xfs_trans_mod_dquot_byino(tp, ip,
977 XFS_TRANS_DQ_BCOUNT, 1L);
978 flags |= xfs_ilog_fext(whichfork);
979
Dave Chinner9e5987a2013-02-25 12:31:26 +1100980done:
981 *logflagsp = flags;
982 return error;
983}
984
985/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 * Called from xfs_bmap_add_attrfork to handle btree format files.
987 */
988STATIC int /* error */
989xfs_bmap_add_attrfork_btree(
990 xfs_trans_t *tp, /* transaction pointer */
991 xfs_inode_t *ip, /* incore inode pointer */
992 xfs_fsblock_t *firstblock, /* first block allocated */
993 xfs_bmap_free_t *flist, /* blocks to free at commit */
994 int *flags) /* inode logging flags */
995{
996 xfs_btree_cur_t *cur; /* btree cursor */
997 int error; /* error return value */
998 xfs_mount_t *mp; /* file system mount struct */
999 int stat; /* newroot status */
1000
1001 mp = ip->i_mount;
1002 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1003 *flags |= XFS_ILOG_DBROOT;
1004 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001005 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 cur->bc_private.b.flist = flist;
1007 cur->bc_private.b.firstblock = *firstblock;
1008 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1009 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001010 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001011 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001012 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto error0;
1014 if (stat == 0) {
1015 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +10001016 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018 *firstblock = cur->bc_private.b.firstblock;
1019 cur->bc_private.b.allocated = 0;
1020 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1021 }
1022 return 0;
1023error0:
1024 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1025 return error;
1026}
1027
1028/*
1029 * Called from xfs_bmap_add_attrfork to handle extents format files.
1030 */
1031STATIC int /* error */
1032xfs_bmap_add_attrfork_extents(
1033 xfs_trans_t *tp, /* transaction pointer */
1034 xfs_inode_t *ip, /* incore inode pointer */
1035 xfs_fsblock_t *firstblock, /* first block allocated */
1036 xfs_bmap_free_t *flist, /* blocks to free at commit */
1037 int *flags) /* inode logging flags */
1038{
1039 xfs_btree_cur_t *cur; /* bmap btree cursor */
1040 int error; /* error return value */
1041
1042 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1043 return 0;
1044 cur = NULL;
1045 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
1046 flags, XFS_DATA_FORK);
1047 if (cur) {
1048 cur->bc_private.b.allocated = 0;
1049 xfs_btree_del_cursor(cur,
1050 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1051 }
1052 return error;
1053}
1054
1055/*
Dave Chinner1e823792013-02-11 15:58:13 +11001056 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1057 * different data fork content type needs a different callout to do the
1058 * conversion. Some are basic and only require special block initialisation
1059 * callouts for the data formating, others (directories) are so specialised they
1060 * handle everything themselves.
1061 *
1062 * XXX (dgc): investigate whether directory conversion can use the generic
1063 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001064 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 */
1066STATIC int /* error */
1067xfs_bmap_add_attrfork_local(
1068 xfs_trans_t *tp, /* transaction pointer */
1069 xfs_inode_t *ip, /* incore inode pointer */
1070 xfs_fsblock_t *firstblock, /* first block allocated */
1071 xfs_bmap_free_t *flist, /* blocks to free at commit */
1072 int *flags) /* inode logging flags */
1073{
1074 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1077 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001078
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001079 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001081 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 dargs.dp = ip;
1083 dargs.firstblock = firstblock;
1084 dargs.flist = flist;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001085 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 dargs.whichfork = XFS_DATA_FORK;
1087 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001088 return xfs_dir2_sf_to_block(&dargs);
1089 }
1090
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001091 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001092 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1093 flags, XFS_DATA_FORK,
1094 xfs_symlink_local_to_remote);
1095
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001096 /* should only be called for types that support local format data */
1097 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001098 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
1101/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001102 * Convert inode from non-attributed to attributed.
1103 * Must not be in a transaction, ip must not be locked.
1104 */
1105int /* error code */
1106xfs_bmap_add_attrfork(
1107 xfs_inode_t *ip, /* incore inode pointer */
1108 int size, /* space new attribute needs */
1109 int rsvd) /* xact may use reserved blks */
1110{
1111 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1112 xfs_bmap_free_t flist; /* freed extent records */
1113 xfs_mount_t *mp; /* mount structure */
1114 xfs_trans_t *tp; /* transaction pointer */
1115 int blks; /* space reservation */
1116 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001117 int logflags; /* logging flags */
1118 int error; /* error return value */
1119
1120 ASSERT(XFS_IFORK_Q(ip) == 0);
1121
1122 mp = ip->i_mount;
1123 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1124 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
1125 blks = XFS_ADDAFORK_SPACE_RES(mp);
1126 if (rsvd)
1127 tp->t_flags |= XFS_TRANS_RESERVE;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001128 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_addafork, blks, 0);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001129 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +10001130 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001131 return error;
1132 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001133 xfs_ilock(ip, XFS_ILOCK_EXCL);
1134 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1135 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1136 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001137 if (error)
1138 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001139 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001140 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001141 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1142 /*
1143 * For inodes coming from pre-6.2 filesystems.
1144 */
1145 ASSERT(ip->i_d.di_aformat == 0);
1146 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1147 }
1148 ASSERT(ip->i_d.di_anextents == 0);
1149
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001150 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001151 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1152
1153 switch (ip->i_d.di_format) {
1154 case XFS_DINODE_FMT_DEV:
1155 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1156 break;
1157 case XFS_DINODE_FMT_UUID:
1158 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1159 break;
1160 case XFS_DINODE_FMT_LOCAL:
1161 case XFS_DINODE_FMT_EXTENTS:
1162 case XFS_DINODE_FMT_BTREE:
1163 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1164 if (!ip->i_d.di_forkoff)
1165 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1166 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1167 version = 2;
1168 break;
1169 default:
1170 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001171 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001172 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001173 }
1174
1175 ASSERT(ip->i_afp == NULL);
1176 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1177 ip->i_afp->if_flags = XFS_IFEXTENTS;
1178 logflags = 0;
1179 xfs_bmap_init(&flist, &firstblock);
1180 switch (ip->i_d.di_format) {
1181 case XFS_DINODE_FMT_LOCAL:
1182 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
1183 &logflags);
1184 break;
1185 case XFS_DINODE_FMT_EXTENTS:
1186 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1187 &flist, &logflags);
1188 break;
1189 case XFS_DINODE_FMT_BTREE:
1190 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
1191 &logflags);
1192 break;
1193 default:
1194 error = 0;
1195 break;
1196 }
1197 if (logflags)
1198 xfs_trans_log_inode(tp, ip, logflags);
1199 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001200 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001201 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1202 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001203 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001204
1205 spin_lock(&mp->m_sb_lock);
1206 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1207 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001208 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001209 }
1210 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1211 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001212 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001213 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001214 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001215 if (log_sb)
1216 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001217 }
1218
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001219 error = xfs_bmap_finish(&tp, &flist, NULL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001220 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001221 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001222 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001223 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001224 return error;
1225
1226bmap_cancel:
1227 xfs_bmap_cancel(&flist);
1228trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001229 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001230 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001231 return error;
1232}
1233
1234/*
1235 * Internal and external extent tree search functions.
1236 */
1237
1238/*
1239 * Read in the extents to if_extents.
1240 * All inode fields are set up by caller, we just traverse the btree
1241 * and copy the records in. If the file system cannot contain unwritten
1242 * extents, the records are checked for no "state" flags.
1243 */
1244int /* error */
1245xfs_bmap_read_extents(
1246 xfs_trans_t *tp, /* transaction pointer */
1247 xfs_inode_t *ip, /* incore inode */
1248 int whichfork) /* data or attr fork */
1249{
1250 struct xfs_btree_block *block; /* current btree block */
1251 xfs_fsblock_t bno; /* block # of "block" */
1252 xfs_buf_t *bp; /* buffer for "block" */
1253 int error; /* error return value */
1254 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1255 xfs_extnum_t i, j; /* index into the extents list */
1256 xfs_ifork_t *ifp; /* fork structure */
1257 int level; /* btree level, for checking */
1258 xfs_mount_t *mp; /* file system mount structure */
1259 __be64 *pp; /* pointer to block address */
1260 /* REFERENCED */
1261 xfs_extnum_t room; /* number of entries there's room for */
1262
1263 bno = NULLFSBLOCK;
1264 mp = ip->i_mount;
1265 ifp = XFS_IFORK_PTR(ip, whichfork);
1266 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1267 XFS_EXTFMT_INODE(ip);
1268 block = ifp->if_broot;
1269 /*
1270 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1271 */
1272 level = be16_to_cpu(block->bb_level);
1273 ASSERT(level > 0);
1274 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1275 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001276 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001277 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1278 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1279 /*
1280 * Go down the tree until leaf level is reached, following the first
1281 * pointer (leftmost) at each level.
1282 */
1283 while (level-- > 0) {
1284 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1285 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1286 if (error)
1287 return error;
1288 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001289 if (level == 0)
1290 break;
1291 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1292 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001293 XFS_WANT_CORRUPTED_GOTO(mp,
1294 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001295 xfs_trans_brelse(tp, bp);
1296 }
1297 /*
1298 * Here with bp and block set to the leftmost leaf node in the tree.
1299 */
1300 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1301 i = 0;
1302 /*
1303 * Loop over all leaf nodes. Copy information to the extent records.
1304 */
1305 for (;;) {
1306 xfs_bmbt_rec_t *frp;
1307 xfs_fsblock_t nextbno;
1308 xfs_extnum_t num_recs;
1309 xfs_extnum_t start;
1310
1311 num_recs = xfs_btree_get_numrecs(block);
1312 if (unlikely(i + num_recs > room)) {
1313 ASSERT(i + num_recs <= room);
1314 xfs_warn(ip->i_mount,
1315 "corrupt dinode %Lu, (btree extents).",
1316 (unsigned long long) ip->i_ino);
1317 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1318 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1319 goto error0;
1320 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001321 /*
1322 * Read-ahead the next leaf block, if any.
1323 */
1324 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1325 if (nextbno != NULLFSBLOCK)
1326 xfs_btree_reada_bufl(mp, nextbno, 1,
1327 &xfs_bmbt_buf_ops);
1328 /*
1329 * Copy records into the extent records.
1330 */
1331 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1332 start = i;
1333 for (j = 0; j < num_recs; j++, i++, frp++) {
1334 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1335 trp->l0 = be64_to_cpu(frp->l0);
1336 trp->l1 = be64_to_cpu(frp->l1);
1337 }
1338 if (exntf == XFS_EXTFMT_NOSTATE) {
1339 /*
1340 * Check all attribute bmap btree records and
1341 * any "older" data bmap btree records for a
1342 * set bit in the "extent flag" position.
1343 */
1344 if (unlikely(xfs_check_nostate_extents(ifp,
1345 start, num_recs))) {
1346 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1347 XFS_ERRLEVEL_LOW,
1348 ip->i_mount);
1349 goto error0;
1350 }
1351 }
1352 xfs_trans_brelse(tp, bp);
1353 bno = nextbno;
1354 /*
1355 * If we've reached the end, stop.
1356 */
1357 if (bno == NULLFSBLOCK)
1358 break;
1359 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1360 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1361 if (error)
1362 return error;
1363 block = XFS_BUF_TO_BLOCK(bp);
1364 }
1365 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1366 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1367 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1368 return 0;
1369error0:
1370 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001371 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001372}
1373
1374
1375/*
1376 * Search the extent records for the entry containing block bno.
1377 * If bno lies in a hole, point to the next entry. If bno lies
1378 * past eof, *eofp will be set, and *prevp will contain the last
1379 * entry (null if none). Else, *lastxp will be set to the index
1380 * of the found entry; *gotp will contain the entry.
1381 */
1382STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1383xfs_bmap_search_multi_extents(
1384 xfs_ifork_t *ifp, /* inode fork pointer */
1385 xfs_fileoff_t bno, /* block number searched for */
1386 int *eofp, /* out: end of file found */
1387 xfs_extnum_t *lastxp, /* out: last extent index */
1388 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1389 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1390{
1391 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1392 xfs_extnum_t lastx; /* last extent index */
1393
1394 /*
1395 * Initialize the extent entry structure to catch access to
1396 * uninitialized br_startblock field.
1397 */
1398 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1399 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1400 gotp->br_state = XFS_EXT_INVALID;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001401 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001402 prevp->br_startoff = NULLFILEOFF;
1403
1404 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1405 if (lastx > 0) {
1406 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1407 }
1408 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1409 xfs_bmbt_get_all(ep, gotp);
1410 *eofp = 0;
1411 } else {
1412 if (lastx > 0) {
1413 *gotp = *prevp;
1414 }
1415 *eofp = 1;
1416 ep = NULL;
1417 }
1418 *lastxp = lastx;
1419 return ep;
1420}
1421
1422/*
1423 * Search the extents list for the inode, for the extent containing bno.
1424 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1425 * *eofp will be set, and *prevp will contain the last entry (null if none).
1426 * Else, *lastxp will be set to the index of the found
1427 * entry; *gotp will contain the entry.
1428 */
1429STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1430xfs_bmap_search_extents(
1431 xfs_inode_t *ip, /* incore inode pointer */
1432 xfs_fileoff_t bno, /* block number searched for */
1433 int fork, /* data or attr fork */
1434 int *eofp, /* out: end of file found */
1435 xfs_extnum_t *lastxp, /* out: last extent index */
1436 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1437 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1438{
1439 xfs_ifork_t *ifp; /* inode fork pointer */
1440 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1441
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001442 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001443 ifp = XFS_IFORK_PTR(ip, fork);
1444
1445 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1446
1447 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1448 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1449 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1450 "Access to block zero in inode %llu "
1451 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -05001452 "blkcnt: %llx extent-state: %x lastx: %x",
Dave Chinner9e5987a2013-02-25 12:31:26 +11001453 (unsigned long long)ip->i_ino,
1454 (unsigned long long)gotp->br_startblock,
1455 (unsigned long long)gotp->br_startoff,
1456 (unsigned long long)gotp->br_blockcount,
1457 gotp->br_state, *lastxp);
1458 *lastxp = NULLEXTNUM;
1459 *eofp = 1;
1460 return NULL;
1461 }
1462 return ep;
1463}
1464
1465/*
1466 * Returns the file-relative block number of the first unused block(s)
1467 * in the file with at least "len" logically contiguous blocks free.
1468 * This is the lowest-address hole if the file has holes, else the first block
1469 * past the end of file.
1470 * Return 0 if the file is currently local (in-inode).
1471 */
1472int /* error */
1473xfs_bmap_first_unused(
1474 xfs_trans_t *tp, /* transaction pointer */
1475 xfs_inode_t *ip, /* incore inode */
1476 xfs_extlen_t len, /* size of hole to find */
1477 xfs_fileoff_t *first_unused, /* unused block */
1478 int whichfork) /* data or attr fork */
1479{
1480 int error; /* error return value */
1481 int idx; /* extent record index */
1482 xfs_ifork_t *ifp; /* inode fork pointer */
1483 xfs_fileoff_t lastaddr; /* last block number seen */
1484 xfs_fileoff_t lowest; /* lowest useful block */
1485 xfs_fileoff_t max; /* starting useful block */
1486 xfs_fileoff_t off; /* offset for this block */
1487 xfs_extnum_t nextents; /* number of extent entries */
1488
1489 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1490 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1491 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1492 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1493 *first_unused = 0;
1494 return 0;
1495 }
1496 ifp = XFS_IFORK_PTR(ip, whichfork);
1497 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1498 (error = xfs_iread_extents(tp, ip, whichfork)))
1499 return error;
1500 lowest = *first_unused;
1501 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1502 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1503 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1504 off = xfs_bmbt_get_startoff(ep);
1505 /*
1506 * See if the hole before this extent will work.
1507 */
1508 if (off >= lowest + len && off - max >= len) {
1509 *first_unused = max;
1510 return 0;
1511 }
1512 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1513 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1514 }
1515 *first_unused = max;
1516 return 0;
1517}
1518
1519/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001520 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001521 * last_block (input value) in the file.
1522 * This is not based on i_size, it is based on the extent records.
1523 * Returns 0 for local files, as they do not have extent records.
1524 */
1525int /* error */
1526xfs_bmap_last_before(
1527 xfs_trans_t *tp, /* transaction pointer */
1528 xfs_inode_t *ip, /* incore inode */
1529 xfs_fileoff_t *last_block, /* last block */
1530 int whichfork) /* data or attr fork */
1531{
1532 xfs_fileoff_t bno; /* input file offset */
1533 int eof; /* hit end of file */
1534 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1535 int error; /* error return value */
1536 xfs_bmbt_irec_t got; /* current extent value */
1537 xfs_ifork_t *ifp; /* inode fork pointer */
1538 xfs_extnum_t lastx; /* last extent used */
1539 xfs_bmbt_irec_t prev; /* previous extent value */
1540
1541 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1542 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1543 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +10001544 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001545 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1546 *last_block = 0;
1547 return 0;
1548 }
1549 ifp = XFS_IFORK_PTR(ip, whichfork);
1550 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1551 (error = xfs_iread_extents(tp, ip, whichfork)))
1552 return error;
1553 bno = *last_block - 1;
1554 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1555 &prev);
1556 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1557 if (prev.br_startoff == NULLFILEOFF)
1558 *last_block = 0;
1559 else
1560 *last_block = prev.br_startoff + prev.br_blockcount;
1561 }
1562 /*
1563 * Otherwise *last_block is already the right answer.
1564 */
1565 return 0;
1566}
1567
Dave Chinner68988112013-08-12 20:49:42 +10001568int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001569xfs_bmap_last_extent(
1570 struct xfs_trans *tp,
1571 struct xfs_inode *ip,
1572 int whichfork,
1573 struct xfs_bmbt_irec *rec,
1574 int *is_empty)
1575{
1576 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1577 int error;
1578 int nextents;
1579
1580 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1581 error = xfs_iread_extents(tp, ip, whichfork);
1582 if (error)
1583 return error;
1584 }
1585
1586 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1587 if (nextents == 0) {
1588 *is_empty = 1;
1589 return 0;
1590 }
1591
1592 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1593 *is_empty = 0;
1594 return 0;
1595}
1596
1597/*
1598 * Check the last inode extent to determine whether this allocation will result
1599 * in blocks being allocated at the end of the file. When we allocate new data
1600 * blocks at the end of the file which do not start at the previous data block,
1601 * we will try to align the new blocks at stripe unit boundaries.
1602 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001603 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001604 * at, or past the EOF.
1605 */
1606STATIC int
1607xfs_bmap_isaeof(
1608 struct xfs_bmalloca *bma,
1609 int whichfork)
1610{
1611 struct xfs_bmbt_irec rec;
1612 int is_empty;
1613 int error;
1614
1615 bma->aeof = 0;
1616 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1617 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001618 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001619 return error;
1620
Dave Chinner6e708bc2013-11-22 10:41:16 +11001621 if (is_empty) {
1622 bma->aeof = 1;
1623 return 0;
1624 }
1625
Dave Chinner9e5987a2013-02-25 12:31:26 +11001626 /*
1627 * Check if we are allocation or past the last extent, or at least into
1628 * the last delayed allocated extent.
1629 */
1630 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1631 (bma->offset >= rec.br_startoff &&
1632 isnullstartblock(rec.br_startblock));
1633 return 0;
1634}
1635
1636/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001637 * Returns the file-relative block number of the first block past eof in
1638 * the file. This is not based on i_size, it is based on the extent records.
1639 * Returns 0 for local files, as they do not have extent records.
1640 */
1641int
1642xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001643 struct xfs_inode *ip,
1644 xfs_fileoff_t *last_block,
1645 int whichfork)
1646{
1647 struct xfs_bmbt_irec rec;
1648 int is_empty;
1649 int error;
1650
1651 *last_block = 0;
1652
1653 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1654 return 0;
1655
1656 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1657 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001658 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001659
1660 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1661 if (error || is_empty)
1662 return error;
1663
1664 *last_block = rec.br_startoff + rec.br_blockcount;
1665 return 0;
1666}
1667
1668/*
1669 * Returns whether the selected fork of the inode has exactly one
1670 * block or not. For the data fork we check this matches di_size,
1671 * implying the file's range is 0..bsize-1.
1672 */
1673int /* 1=>1 block, 0=>otherwise */
1674xfs_bmap_one_block(
1675 xfs_inode_t *ip, /* incore inode */
1676 int whichfork) /* data or attr fork */
1677{
1678 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1679 xfs_ifork_t *ifp; /* inode fork pointer */
1680 int rval; /* return value */
1681 xfs_bmbt_irec_t s; /* internal version of extent */
1682
1683#ifndef DEBUG
1684 if (whichfork == XFS_DATA_FORK)
1685 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1686#endif /* !DEBUG */
1687 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1688 return 0;
1689 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1690 return 0;
1691 ifp = XFS_IFORK_PTR(ip, whichfork);
1692 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1693 ep = xfs_iext_get_ext(ifp, 0);
1694 xfs_bmbt_get_all(ep, &s);
1695 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1696 if (rval && whichfork == XFS_DATA_FORK)
1697 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1698 return rval;
1699}
1700
1701/*
1702 * Extent tree manipulation functions used during allocation.
1703 */
1704
1705/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001706 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 */
1708STATIC int /* error */
1709xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001710 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001712 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001714 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001717 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 xfs_fileoff_t new_endoff; /* end offset of new entry */
1719 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1720 /* left is 0, right is 1, prev is 2 */
1721 int rval=0; /* return value (logging flags) */
1722 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001723 xfs_filblks_t da_new; /* new count del alloc blocks used */
1724 xfs_filblks_t da_old; /* old count del alloc blocks used */
1725 xfs_filblks_t temp=0; /* value for da_new calculations */
1726 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 int tmp_rval; /* partial logging flags */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001728 int whichfork = XFS_DATA_FORK;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001729 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001731 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001732 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001733
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001734 ASSERT(bma->idx >= 0);
1735 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001736 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001737 ASSERT(!bma->cur ||
1738 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001739
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001740 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742#define LEFT r[0]
1743#define RIGHT r[1]
1744#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
1746 /*
1747 * Set up a bunch of variables to make the tests simpler.
1748 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001749 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 xfs_bmbt_get_all(ep, &PREV);
1751 new_endoff = new->br_startoff + new->br_blockcount;
1752 ASSERT(PREV.br_startoff <= new->br_startoff);
1753 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001754
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001755 da_old = startblockval(PREV.br_startblock);
1756 da_new = 0;
1757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /*
1759 * Set flags determining what part of the previous delayed allocation
1760 * extent is being replaced by a real allocation.
1761 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001762 if (PREV.br_startoff == new->br_startoff)
1763 state |= BMAP_LEFT_FILLING;
1764 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1765 state |= BMAP_RIGHT_FILLING;
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 /*
1768 * Check and set flags if this segment has a left neighbor.
1769 * Don't set contiguous if the combined extent would be too large.
1770 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001771 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001772 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001773 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001774
1775 if (isnullstartblock(LEFT.br_startblock))
1776 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001778
1779 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1780 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1781 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1782 LEFT.br_state == new->br_state &&
1783 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1784 state |= BMAP_LEFT_CONTIG;
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 /*
1787 * Check and set flags if this segment has a right neighbor.
1788 * Don't set contiguous if the combined extent would be too large.
1789 * Also check for all-three-contiguous being too large.
1790 */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001791 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001792 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001793 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001794
1795 if (isnullstartblock(RIGHT.br_startblock))
1796 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001798
1799 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1800 new_endoff == RIGHT.br_startoff &&
1801 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1802 new->br_state == RIGHT.br_state &&
1803 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1804 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1805 BMAP_RIGHT_FILLING)) !=
1806 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1807 BMAP_RIGHT_FILLING) ||
1808 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1809 <= MAXEXTLEN))
1810 state |= BMAP_RIGHT_CONTIG;
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 error = 0;
1813 /*
1814 * Switch out based on the FILLING and CONTIG state bits.
1815 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001816 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1817 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1818 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1819 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 /*
1821 * Filling in all of a previously delayed allocation extent.
1822 * The left and right neighbors are both contiguous with new.
1823 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001824 bma->idx--;
1825 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1826 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 LEFT.br_blockcount + PREV.br_blockcount +
1828 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001829 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001830
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001831 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1832 bma->ip->i_d.di_nextents--;
1833 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1835 else {
1836 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001837 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001839 RIGHT.br_blockcount, &i);
1840 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001842 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001843 error = xfs_btree_delete(bma->cur, &i);
1844 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001846 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 error = xfs_btree_decrement(bma->cur, 0, &i);
1848 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001850 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001851 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 LEFT.br_startblock,
1853 LEFT.br_blockcount +
1854 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001855 RIGHT.br_blockcount, LEFT.br_state);
1856 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 goto done;
1858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 break;
1860
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001861 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 /*
1863 * Filling in all of a previously delayed allocation extent.
1864 * The left neighbor is contiguous, the right is not.
1865 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001866 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001867
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001868 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1869 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001870 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001871 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001872
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001873 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1874 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 rval = XFS_ILOG_DEXT;
1876 else {
1877 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001878 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001880 &i);
1881 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001883 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001884 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 LEFT.br_startblock,
1886 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001887 PREV.br_blockcount, LEFT.br_state);
1888 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 goto done;
1890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 break;
1892
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001893 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 /*
1895 * Filling in all of a previously delayed allocation extent.
1896 * The right neighbor is contiguous, the left is not.
1897 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001898 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 xfs_bmbt_set_startblock(ep, new->br_startblock);
1900 xfs_bmbt_set_blockcount(ep,
1901 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001902 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001903
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001904 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1905 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 rval = XFS_ILOG_DEXT;
1907 else {
1908 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001909 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001911 RIGHT.br_blockcount, &i);
1912 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001914 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001915 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 new->br_startblock,
1917 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001918 RIGHT.br_blockcount, PREV.br_state);
1919 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 goto done;
1921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 break;
1923
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001924 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 /*
1926 * Filling in all of a previously delayed allocation extent.
1927 * Neither the left nor right neighbors are contiguous with
1928 * the new one.
1929 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001930 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001932 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001933
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001934 bma->ip->i_d.di_nextents++;
1935 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1937 else {
1938 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001939 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001941 &i);
1942 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001944 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001945 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1946 error = xfs_btree_insert(bma->cur, &i);
1947 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001949 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 break;
1952
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001953 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 /*
1955 * Filling in the first part of a previous delayed allocation.
1956 * The left neighbor is contiguous.
1957 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1959 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 LEFT.br_blockcount + new->br_blockcount);
1961 xfs_bmbt_set_startoff(ep,
1962 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001963 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001966 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001968 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 rval = XFS_ILOG_DEXT;
1970 else {
1971 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001972 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001974 &i);
1975 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001977 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001978 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 LEFT.br_startblock,
1980 LEFT.br_blockcount +
1981 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001982 LEFT.br_state);
1983 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 goto done;
1985 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001986 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001987 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001988 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001989 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001990
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001991 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 break;
1993
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001994 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /*
1996 * Filling in the first part of a previous delayed allocation.
1997 * The left neighbor is not contiguous.
1998 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001999 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 xfs_bmbt_set_startoff(ep, new_endoff);
2001 temp = PREV.br_blockcount - new->br_blockcount;
2002 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002003 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
2004 bma->ip->i_d.di_nextents++;
2005 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2007 else {
2008 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002009 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002011 &i);
2012 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002014 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002015 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2016 error = xfs_btree_insert(bma->cur, &i);
2017 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002019 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002021
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002022 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002023 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2024 bma->firstblock, bma->flist,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002025 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 rval |= tmp_rval;
2027 if (error)
2028 goto done;
2029 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002030 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002031 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002032 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2033 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002034 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002035 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 break;
2037
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002038 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 /*
2040 * Filling in the last part of a previous delayed allocation.
2041 * The right neighbor is contiguous with the new allocation.
2042 */
2043 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002044 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002046 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002047 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 new->br_blockcount + RIGHT.br_blockcount,
2049 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002050 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2051 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 rval = XFS_ILOG_DEXT;
2053 else {
2054 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002055 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002057 RIGHT.br_blockcount, &i);
2058 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002060 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002061 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 new->br_startblock,
2063 new->br_blockcount +
2064 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002065 RIGHT.br_state);
2066 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 goto done;
2068 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002069
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002070 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002071 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002072 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002073 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002074 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002075
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002076 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 break;
2078
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002079 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /*
2081 * Filling in the last part of a previous delayed allocation.
2082 * The right neighbor is not contiguous.
2083 */
2084 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002085 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002087 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2088 bma->ip->i_d.di_nextents++;
2089 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2091 else {
2092 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002093 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002095 &i);
2096 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002098 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002099 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2100 error = xfs_btree_insert(bma->cur, &i);
2101 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002103 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002105
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002106 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002107 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2108 bma->firstblock, bma->flist, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002109 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 rval |= tmp_rval;
2111 if (error)
2112 goto done;
2113 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002114 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002115 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002116 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2117 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002118 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002119 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002120
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002121 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 break;
2123
2124 case 0:
2125 /*
2126 * Filling in the middle part of a previous delayed allocation.
2127 * Contiguity is impossible here.
2128 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002129 *
2130 * We start with a delayed allocation:
2131 *
2132 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2133 * PREV @ idx
2134 *
2135 * and we are allocating:
2136 * +rrrrrrrrrrrrrrrrr+
2137 * new
2138 *
2139 * and we set it up for insertion as:
2140 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2141 * new
2142 * PREV @ idx LEFT RIGHT
2143 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 */
2145 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002147 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002148 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2149 LEFT = *new;
2150 RIGHT.br_state = PREV.br_state;
2151 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002152 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002153 RIGHT.br_startoff = new_endoff;
2154 RIGHT.br_blockcount = temp2;
2155 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002156 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2157 bma->ip->i_d.di_nextents++;
2158 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2160 else {
2161 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002162 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002164 &i);
2165 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002167 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002168 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2169 error = xfs_btree_insert(bma->cur, &i);
2170 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002172 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002174
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002175 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002176 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2177 bma->firstblock, bma->flist, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002178 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 rval |= tmp_rval;
2180 if (error)
2181 goto done;
2182 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002183 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2184 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002185 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002186 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002187 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002188 error = xfs_mod_fdblocks(bma->ip->i_mount,
2189 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002190 ASSERT(!error);
2191 if (error)
2192 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002194
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002195 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002196 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002197 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2198 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2199 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002200 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002201 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002202
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002203 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002204 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 break;
2206
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002207 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2208 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2209 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2210 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2211 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2212 case BMAP_LEFT_CONTIG:
2213 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /*
2215 * These cases are all impossible.
2216 */
2217 ASSERT(0);
2218 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002219
2220 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002221 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002222 int tmp_logflags; /* partial log flag return val */
2223
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002224 ASSERT(bma->cur == NULL);
2225 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2226 bma->firstblock, bma->flist, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002227 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002228 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002229 if (error)
2230 goto done;
2231 }
2232
2233 /* adjust for changes in reserved delayed indirect blocks */
2234 if (da_old || da_new) {
2235 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002236 if (bma->cur)
2237 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002238 ASSERT(temp <= da_old);
2239 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002240 xfs_mod_fdblocks(bma->ip->i_mount,
2241 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002242 }
2243
2244 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002245 if (bma->cur)
2246 bma->cur->bc_private.b.allocated = 0;
2247
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002248 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002250 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 return error;
2252#undef LEFT
2253#undef RIGHT
2254#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255}
2256
2257/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002258 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 */
2260STATIC int /* error */
2261xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002262 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002264 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002266 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002267 xfs_fsblock_t *first, /* pointer to firstblock variable */
2268 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002269 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002272 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002275 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 xfs_fileoff_t new_endoff; /* end offset of new entry */
2277 xfs_exntst_t newext; /* new extent state */
2278 xfs_exntst_t oldext; /* old extent state */
2279 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2280 /* left is 0, right is 1, prev is 2 */
2281 int rval=0; /* return value (logging flags) */
2282 int state = 0;/* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002283 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002285 *logflagsp = 0;
2286
2287 cur = *curp;
2288 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2289
2290 ASSERT(*idx >= 0);
2291 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2292 ASSERT(!isnullstartblock(new->br_startblock));
2293
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002294 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002295
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296#define LEFT r[0]
2297#define RIGHT r[1]
2298#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /*
2301 * Set up a bunch of variables to make the tests simpler.
2302 */
2303 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002304 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 xfs_bmbt_get_all(ep, &PREV);
2306 newext = new->br_state;
2307 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2308 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2309 ASSERT(PREV.br_state == oldext);
2310 new_endoff = new->br_startoff + new->br_blockcount;
2311 ASSERT(PREV.br_startoff <= new->br_startoff);
2312 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002313
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 /*
2315 * Set flags determining what part of the previous oldext allocation
2316 * extent is being replaced by a newext allocation.
2317 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002318 if (PREV.br_startoff == new->br_startoff)
2319 state |= BMAP_LEFT_FILLING;
2320 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2321 state |= BMAP_RIGHT_FILLING;
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 /*
2324 * Check and set flags if this segment has a left neighbor.
2325 * Don't set contiguous if the combined extent would be too large.
2326 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002327 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002328 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002329 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002330
2331 if (isnullstartblock(LEFT.br_startblock))
2332 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002334
2335 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2336 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2337 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2338 LEFT.br_state == newext &&
2339 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2340 state |= BMAP_LEFT_CONTIG;
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 /*
2343 * Check and set flags if this segment has a right neighbor.
2344 * Don't set contiguous if the combined extent would be too large.
2345 * Also check for all-three-contiguous being too large.
2346 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002347 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002348 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002349 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002350 if (isnullstartblock(RIGHT.br_startblock))
2351 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002353
2354 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2355 new_endoff == RIGHT.br_startoff &&
2356 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2357 newext == RIGHT.br_state &&
2358 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2359 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2360 BMAP_RIGHT_FILLING)) !=
2361 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2362 BMAP_RIGHT_FILLING) ||
2363 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2364 <= MAXEXTLEN))
2365 state |= BMAP_RIGHT_CONTIG;
2366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 /*
2368 * Switch out based on the FILLING and CONTIG state bits.
2369 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002370 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2371 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2372 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2373 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 /*
2375 * Setting all of a previous oldext extent to newext.
2376 * The left and right neighbors are both contiguous with new.
2377 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002378 --*idx;
2379
2380 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2381 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 LEFT.br_blockcount + PREV.br_blockcount +
2383 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002384 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002385
Christoph Hellwigec90c552011-05-23 08:52:53 +00002386 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 ip->i_d.di_nextents -= 2;
2388 if (cur == NULL)
2389 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2390 else {
2391 rval = XFS_ILOG_CORE;
2392 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2393 RIGHT.br_startblock,
2394 RIGHT.br_blockcount, &i)))
2395 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002396 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002397 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002399 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002400 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002402 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002403 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002405 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002406 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002408 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2410 LEFT.br_startblock,
2411 LEFT.br_blockcount + PREV.br_blockcount +
2412 RIGHT.br_blockcount, LEFT.br_state)))
2413 goto done;
2414 }
2415 break;
2416
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002417 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 /*
2419 * Setting all of a previous oldext extent to newext.
2420 * The left neighbor is contiguous, the right is not.
2421 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002422 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002423
Christoph Hellwigec90c552011-05-23 08:52:53 +00002424 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2425 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2426 LEFT.br_blockcount + PREV.br_blockcount);
2427 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2428
2429 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 ip->i_d.di_nextents--;
2431 if (cur == NULL)
2432 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2433 else {
2434 rval = XFS_ILOG_CORE;
2435 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2436 PREV.br_startblock, PREV.br_blockcount,
2437 &i)))
2438 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002439 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002440 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002442 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002443 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002445 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2447 LEFT.br_startblock,
2448 LEFT.br_blockcount + PREV.br_blockcount,
2449 LEFT.br_state)))
2450 goto done;
2451 }
2452 break;
2453
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002454 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 /*
2456 * Setting all of a previous oldext extent to newext.
2457 * The right neighbor is contiguous, the left is not.
2458 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002459 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 xfs_bmbt_set_blockcount(ep,
2461 PREV.br_blockcount + RIGHT.br_blockcount);
2462 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002463 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2464 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 ip->i_d.di_nextents--;
2466 if (cur == NULL)
2467 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2468 else {
2469 rval = XFS_ILOG_CORE;
2470 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2471 RIGHT.br_startblock,
2472 RIGHT.br_blockcount, &i)))
2473 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002474 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002475 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002477 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002478 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002480 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2482 new->br_startblock,
2483 new->br_blockcount + RIGHT.br_blockcount,
2484 newext)))
2485 goto done;
2486 }
2487 break;
2488
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002489 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 /*
2491 * Setting all of a previous oldext extent to newext.
2492 * Neither the left nor right neighbors are contiguous with
2493 * the new one.
2494 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002495 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002497 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 if (cur == NULL)
2500 rval = XFS_ILOG_DEXT;
2501 else {
2502 rval = 0;
2503 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2504 new->br_startblock, new->br_blockcount,
2505 &i)))
2506 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002507 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2509 new->br_startblock, new->br_blockcount,
2510 newext)))
2511 goto done;
2512 }
2513 break;
2514
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002515 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 /*
2517 * Setting the first part of a previous oldext extent to newext.
2518 * The left neighbor is contiguous.
2519 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002520 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2521 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 LEFT.br_blockcount + new->br_blockcount);
2523 xfs_bmbt_set_startoff(ep,
2524 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002525 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002526
Christoph Hellwigec90c552011-05-23 08:52:53 +00002527 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 xfs_bmbt_set_startblock(ep,
2529 new->br_startblock + new->br_blockcount);
2530 xfs_bmbt_set_blockcount(ep,
2531 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002532 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002533
Christoph Hellwigec90c552011-05-23 08:52:53 +00002534 --*idx;
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 if (cur == NULL)
2537 rval = XFS_ILOG_DEXT;
2538 else {
2539 rval = 0;
2540 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2541 PREV.br_startblock, PREV.br_blockcount,
2542 &i)))
2543 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002544 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 if ((error = xfs_bmbt_update(cur,
2546 PREV.br_startoff + new->br_blockcount,
2547 PREV.br_startblock + new->br_blockcount,
2548 PREV.br_blockcount - new->br_blockcount,
2549 oldext)))
2550 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002551 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002553 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 LEFT.br_startblock,
2555 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002556 LEFT.br_state);
2557 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 goto done;
2559 }
2560 break;
2561
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002562 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 /*
2564 * Setting the first part of a previous oldext extent to newext.
2565 * The left neighbor is not contiguous.
2566 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002567 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2569 xfs_bmbt_set_startoff(ep, new_endoff);
2570 xfs_bmbt_set_blockcount(ep,
2571 PREV.br_blockcount - new->br_blockcount);
2572 xfs_bmbt_set_startblock(ep,
2573 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002574 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002575
Christoph Hellwigec90c552011-05-23 08:52:53 +00002576 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 ip->i_d.di_nextents++;
2578 if (cur == NULL)
2579 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2580 else {
2581 rval = XFS_ILOG_CORE;
2582 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2583 PREV.br_startblock, PREV.br_blockcount,
2584 &i)))
2585 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002586 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 if ((error = xfs_bmbt_update(cur,
2588 PREV.br_startoff + new->br_blockcount,
2589 PREV.br_startblock + new->br_blockcount,
2590 PREV.br_blockcount - new->br_blockcount,
2591 oldext)))
2592 goto done;
2593 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002594 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002596 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 }
2598 break;
2599
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002600 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 /*
2602 * Setting the last part of a previous oldext extent to newext.
2603 * The right neighbor is contiguous with the new allocation.
2604 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002605 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 xfs_bmbt_set_blockcount(ep,
2607 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002608 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2609
2610 ++*idx;
2611
2612 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2613 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002614 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002616 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 if (cur == NULL)
2619 rval = XFS_ILOG_DEXT;
2620 else {
2621 rval = 0;
2622 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2623 PREV.br_startblock,
2624 PREV.br_blockcount, &i)))
2625 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002626 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2628 PREV.br_startblock,
2629 PREV.br_blockcount - new->br_blockcount,
2630 oldext)))
2631 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002632 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 goto done;
2634 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2635 new->br_startblock,
2636 new->br_blockcount + RIGHT.br_blockcount,
2637 newext)))
2638 goto done;
2639 }
2640 break;
2641
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002642 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /*
2644 * Setting the last part of a previous oldext extent to newext.
2645 * The right neighbor is not contiguous.
2646 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002647 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 xfs_bmbt_set_blockcount(ep,
2649 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002650 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002651
Christoph Hellwigec90c552011-05-23 08:52:53 +00002652 ++*idx;
2653 xfs_iext_insert(ip, *idx, 1, new, state);
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 ip->i_d.di_nextents++;
2656 if (cur == NULL)
2657 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2658 else {
2659 rval = XFS_ILOG_CORE;
2660 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2661 PREV.br_startblock, PREV.br_blockcount,
2662 &i)))
2663 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002664 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2666 PREV.br_startblock,
2667 PREV.br_blockcount - new->br_blockcount,
2668 oldext)))
2669 goto done;
2670 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2671 new->br_startblock, new->br_blockcount,
2672 &i)))
2673 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002674 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002676 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002678 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
2680 break;
2681
2682 case 0:
2683 /*
2684 * Setting the middle part of a previous oldext extent to
2685 * newext. Contiguity is impossible here.
2686 * One extent becomes three extents.
2687 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002688 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 xfs_bmbt_set_blockcount(ep,
2690 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002691 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 r[0] = *new;
2694 r[1].br_startoff = new_endoff;
2695 r[1].br_blockcount =
2696 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2697 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2698 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002699
2700 ++*idx;
2701 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 ip->i_d.di_nextents += 2;
2704 if (cur == NULL)
2705 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2706 else {
2707 rval = XFS_ILOG_CORE;
2708 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2709 PREV.br_startblock, PREV.br_blockcount,
2710 &i)))
2711 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002712 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 /* new right extent - oldext */
2714 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2715 r[1].br_startblock, r[1].br_blockcount,
2716 r[1].br_state)))
2717 goto done;
2718 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002720 cur->bc_rec.b.br_blockcount =
2721 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002722 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002724 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002725 /*
2726 * Reset the cursor to the position of the new extent
2727 * we are about to insert as we can't trust it after
2728 * the previous insert.
2729 */
2730 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2731 new->br_startblock, new->br_blockcount,
2732 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002734 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002736 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002737 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002739 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 }
2741 break;
2742
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002743 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2744 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2745 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2746 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2747 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2748 case BMAP_LEFT_CONTIG:
2749 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 /*
2751 * These cases are all impossible.
2752 */
2753 ASSERT(0);
2754 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002755
2756 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002757 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002758 int tmp_logflags; /* partial log flag return val */
2759
2760 ASSERT(cur == NULL);
2761 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
2762 0, &tmp_logflags, XFS_DATA_FORK);
2763 *logflagsp |= tmp_logflags;
2764 if (error)
2765 goto done;
2766 }
2767
2768 /* clear out the allocated field, done with it now in any case. */
2769 if (cur) {
2770 cur->bc_private.b.allocated = 0;
2771 *curp = cur;
2772 }
2773
2774 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002776 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 return error;
2778#undef LEFT
2779#undef RIGHT
2780#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781}
2782
2783/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002784 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002786STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787xfs_bmap_add_extent_hole_delay(
2788 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002789 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002790 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002792 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2794 xfs_filblks_t newlen=0; /* new indirect size */
2795 xfs_filblks_t oldlen=0; /* old indirect size */
2796 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2797 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002798 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002800 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002802 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 /*
2805 * Check and set flags if this segment has a left neighbor
2806 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002807 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002808 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002809 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002810
2811 if (isnullstartblock(left.br_startblock))
2812 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 /*
2816 * Check and set flags if the current (right) segment exists.
2817 * If it doesn't exist, we're converting the hole at end-of-file.
2818 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002819 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002820 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002821 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002822
2823 if (isnullstartblock(right.br_startblock))
2824 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 /*
2828 * Set contiguity flags on the left and right neighbors.
2829 * Don't let extents get too large, even if the pieces are contiguous.
2830 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002831 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2832 left.br_startoff + left.br_blockcount == new->br_startoff &&
2833 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2834 state |= BMAP_LEFT_CONTIG;
2835
2836 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2837 new->br_startoff + new->br_blockcount == right.br_startoff &&
2838 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2839 (!(state & BMAP_LEFT_CONTIG) ||
2840 (left.br_blockcount + new->br_blockcount +
2841 right.br_blockcount <= MAXEXTLEN)))
2842 state |= BMAP_RIGHT_CONTIG;
2843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 /*
2845 * Switch out based on the contiguity flags.
2846 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002847 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2848 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 /*
2850 * New allocation is contiguous with delayed allocations
2851 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002852 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002854 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 temp = left.br_blockcount + new->br_blockcount +
2856 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002857
Christoph Hellwigec90c552011-05-23 08:52:53 +00002858 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2859 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002860 oldlen = startblockval(left.br_startblock) +
2861 startblockval(new->br_startblock) +
2862 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002864 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002865 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002866 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002867
Christoph Hellwigec90c552011-05-23 08:52:53 +00002868 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 break;
2870
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002871 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 /*
2873 * New allocation is contiguous with a delayed allocation
2874 * on the left.
2875 * Merge the new allocation with the left neighbor.
2876 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002877 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002879
2880 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2881 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002882 oldlen = startblockval(left.br_startblock) +
2883 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002885 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002886 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002887 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 break;
2889
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002890 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 /*
2892 * New allocation is contiguous with a delayed allocation
2893 * on the right.
2894 * Merge the new allocation with the right neighbor.
2895 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002896 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002898 oldlen = startblockval(new->br_startblock) +
2899 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002901 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2902 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002903 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002904 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 break;
2906
2907 case 0:
2908 /*
2909 * New allocation is not contiguous with another
2910 * delayed allocation.
2911 * Insert a new entry.
2912 */
2913 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002914 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 break;
2916 }
2917 if (oldlen != newlen) {
2918 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002919 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2920 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 /*
2922 * Nothing to do for disk quota accounting here.
2923 */
2924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925}
2926
2927/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002928 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 */
2930STATIC int /* error */
2931xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002932 struct xfs_bmalloca *bma,
2933 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002935 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 int i; /* temp state */
2938 xfs_ifork_t *ifp; /* inode fork pointer */
2939 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2940 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002941 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002943 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Eric Sandeenf1f96c42016-01-04 16:10:42 +11002945 mp = bma->ip->i_mount;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002946 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002947
Christoph Hellwigc6534242011-09-18 20:41:05 +00002948 ASSERT(bma->idx >= 0);
2949 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002950 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002951 ASSERT(!bma->cur ||
2952 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002953
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002954 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002955
2956 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002957 if (whichfork == XFS_ATTR_FORK)
2958 state |= BMAP_ATTRFORK;
2959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 /*
2961 * Check and set flags if this segment has a left neighbor.
2962 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002963 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002964 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002965 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002966 if (isnullstartblock(left.br_startblock))
2967 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 /*
2971 * Check and set flags if this segment has a current value.
2972 * Not true if we're inserting into the "hole" at eof.
2973 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002974 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002975 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002976 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002977 if (isnullstartblock(right.br_startblock))
2978 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 /*
2982 * We're inserting a real allocation between "left" and "right".
2983 * Set the contiguity flags. Don't let extents get too large.
2984 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002985 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2986 left.br_startoff + left.br_blockcount == new->br_startoff &&
2987 left.br_startblock + left.br_blockcount == new->br_startblock &&
2988 left.br_state == new->br_state &&
2989 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2990 state |= BMAP_LEFT_CONTIG;
2991
2992 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2993 new->br_startoff + new->br_blockcount == right.br_startoff &&
2994 new->br_startblock + new->br_blockcount == right.br_startblock &&
2995 new->br_state == right.br_state &&
2996 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2997 (!(state & BMAP_LEFT_CONTIG) ||
2998 left.br_blockcount + new->br_blockcount +
2999 right.br_blockcount <= MAXEXTLEN))
3000 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003002 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 /*
3004 * Select which case we're in here, and implement it.
3005 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003006 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3007 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 /*
3009 * New allocation is contiguous with real allocations on the
3010 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003011 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003013 --bma->idx;
3014 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3015 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 left.br_blockcount + new->br_blockcount +
3017 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003018 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003019
Christoph Hellwigc6534242011-09-18 20:41:05 +00003020 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003021
Christoph Hellwigc6534242011-09-18 20:41:05 +00003022 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3023 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3024 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003025 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003026 } else {
3027 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003028 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3029 right.br_startblock, right.br_blockcount,
3030 &i);
3031 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003032 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003033 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003034 error = xfs_btree_delete(bma->cur, &i);
3035 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003036 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003037 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003038 error = xfs_btree_decrement(bma->cur, 0, &i);
3039 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003040 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003041 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003042 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003043 left.br_startblock,
3044 left.br_blockcount +
3045 new->br_blockcount +
3046 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003047 left.br_state);
3048 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003049 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003051 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003053 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 /*
3055 * New allocation is contiguous with a real allocation
3056 * on the left.
3057 * Merge the new allocation with the left neighbor.
3058 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003059 --bma->idx;
3060 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3061 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003063 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003064
Christoph Hellwigc6534242011-09-18 20:41:05 +00003065 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003066 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003067 } else {
3068 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003069 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3070 left.br_startblock, left.br_blockcount,
3071 &i);
3072 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003073 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003074 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003075 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003076 left.br_startblock,
3077 left.br_blockcount +
3078 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003079 left.br_state);
3080 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003081 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003083 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003085 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 /*
3087 * New allocation is contiguous with a real allocation
3088 * on the right.
3089 * Merge the new allocation with the right neighbor.
3090 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003091 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3092 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003093 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 new->br_blockcount + right.br_blockcount,
3095 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003096 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003097
Christoph Hellwigc6534242011-09-18 20:41:05 +00003098 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003099 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003100 } else {
3101 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003102 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003103 right.br_startoff,
3104 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003105 right.br_blockcount, &i);
3106 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003107 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003108 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003109 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003110 new->br_startblock,
3111 new->br_blockcount +
3112 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003113 right.br_state);
3114 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003115 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003117 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
3119 case 0:
3120 /*
3121 * New allocation is not contiguous with another
3122 * real allocation.
3123 * Insert a new entry.
3124 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003125 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3126 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3127 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3128 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003129 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003130 } else {
3131 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003132 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003133 new->br_startoff,
3134 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003135 new->br_blockcount, &i);
3136 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003137 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003138 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003139 bma->cur->bc_rec.b.br_state = new->br_state;
3140 error = xfs_btree_insert(bma->cur, &i);
3141 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003142 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003143 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003145 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003147
3148 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003149 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003150 int tmp_logflags; /* partial log flag return val */
3151
Christoph Hellwigc6534242011-09-18 20:41:05 +00003152 ASSERT(bma->cur == NULL);
3153 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3154 bma->firstblock, bma->flist, &bma->cur,
3155 0, &tmp_logflags, whichfork);
3156 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003157 if (error)
3158 goto done;
3159 }
3160
3161 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003162 if (bma->cur)
3163 bma->cur->bc_private.b.allocated = 0;
3164
3165 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003166done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003167 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003168 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
Nathan Scottdd9f4382006-01-11 15:28:28 +11003171/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003172 * Functions used in the extent read, allocate and remove paths
3173 */
3174
3175/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003176 * Adjust the size of the new extent based on di_extsize and rt extsize.
3177 */
Dave Chinner68988112013-08-12 20:49:42 +10003178int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003179xfs_bmap_extsize_align(
3180 xfs_mount_t *mp,
3181 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3182 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3183 xfs_extlen_t extsz, /* align to this extent size */
3184 int rt, /* is this a realtime inode? */
3185 int eof, /* is extent at end-of-file? */
3186 int delay, /* creating delalloc extent? */
3187 int convert, /* overwriting unwritten extent? */
3188 xfs_fileoff_t *offp, /* in/out: aligned offset */
3189 xfs_extlen_t *lenp) /* in/out: aligned length */
3190{
3191 xfs_fileoff_t orig_off; /* original offset */
3192 xfs_extlen_t orig_alen; /* original length */
3193 xfs_fileoff_t orig_end; /* original off+len */
3194 xfs_fileoff_t nexto; /* next file offset */
3195 xfs_fileoff_t prevo; /* previous file offset */
3196 xfs_fileoff_t align_off; /* temp for offset */
3197 xfs_extlen_t align_alen; /* temp for length */
3198 xfs_extlen_t temp; /* temp for calculations */
3199
3200 if (convert)
3201 return 0;
3202
3203 orig_off = align_off = *offp;
3204 orig_alen = align_alen = *lenp;
3205 orig_end = orig_off + orig_alen;
3206
3207 /*
3208 * If this request overlaps an existing extent, then don't
3209 * attempt to perform any additional alignment.
3210 */
3211 if (!delay && !eof &&
3212 (orig_off >= gotp->br_startoff) &&
3213 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3214 return 0;
3215 }
3216
3217 /*
3218 * If the file offset is unaligned vs. the extent size
3219 * we need to align it. This will be possible unless
3220 * the file was previously written with a kernel that didn't
3221 * perform this alignment, or if a truncate shot us in the
3222 * foot.
3223 */
3224 temp = do_mod(orig_off, extsz);
3225 if (temp) {
3226 align_alen += temp;
3227 align_off -= temp;
3228 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003229
3230 /* Same adjustment for the end of the requested area. */
3231 temp = (align_alen % extsz);
3232 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003233 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003234
3235 /*
3236 * For large extent hint sizes, the aligned extent might be larger than
3237 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3238 * the length back under MAXEXTLEN. The outer allocation loops handle
3239 * short allocation just fine, so it is safe to do this. We only want to
3240 * do it when we are forced to, though, because it means more allocation
3241 * operations are required.
3242 */
3243 while (align_alen > MAXEXTLEN)
3244 align_alen -= extsz;
3245 ASSERT(align_alen <= MAXEXTLEN);
3246
Nathan Scottdd9f4382006-01-11 15:28:28 +11003247 /*
3248 * If the previous block overlaps with this proposed allocation
3249 * then move the start forward without adjusting the length.
3250 */
3251 if (prevp->br_startoff != NULLFILEOFF) {
3252 if (prevp->br_startblock == HOLESTARTBLOCK)
3253 prevo = prevp->br_startoff;
3254 else
3255 prevo = prevp->br_startoff + prevp->br_blockcount;
3256 } else
3257 prevo = 0;
3258 if (align_off != orig_off && align_off < prevo)
3259 align_off = prevo;
3260 /*
3261 * If the next block overlaps with this proposed allocation
3262 * then move the start back without adjusting the length,
3263 * but not before offset 0.
3264 * This may of course make the start overlap previous block,
3265 * and if we hit the offset 0 limit then the next block
3266 * can still overlap too.
3267 */
3268 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3269 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3270 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3271 nexto = gotp->br_startoff + gotp->br_blockcount;
3272 else
3273 nexto = gotp->br_startoff;
3274 } else
3275 nexto = NULLFILEOFF;
3276 if (!eof &&
3277 align_off + align_alen != orig_end &&
3278 align_off + align_alen > nexto)
3279 align_off = nexto > align_alen ? nexto - align_alen : 0;
3280 /*
3281 * If we're now overlapping the next or previous extent that
3282 * means we can't fit an extsz piece in this hole. Just move
3283 * the start forward to the first valid spot and set
3284 * the length so we hit the end.
3285 */
3286 if (align_off != orig_off && align_off < prevo)
3287 align_off = prevo;
3288 if (align_off + align_alen != orig_end &&
3289 align_off + align_alen > nexto &&
3290 nexto != NULLFILEOFF) {
3291 ASSERT(nexto > prevo);
3292 align_alen = nexto - align_off;
3293 }
3294
3295 /*
3296 * If realtime, and the result isn't a multiple of the realtime
3297 * extent size we need to remove blocks until it is.
3298 */
3299 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3300 /*
3301 * We're not covering the original request, or
3302 * we won't be able to once we fix the length.
3303 */
3304 if (orig_off < align_off ||
3305 orig_end > align_off + align_alen ||
3306 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003307 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003308 /*
3309 * Try to fix it by moving the start up.
3310 */
3311 if (align_off + temp <= orig_off) {
3312 align_alen -= temp;
3313 align_off += temp;
3314 }
3315 /*
3316 * Try to fix it by moving the end in.
3317 */
3318 else if (align_off + align_alen - temp >= orig_end)
3319 align_alen -= temp;
3320 /*
3321 * Set the start to the minimum then trim the length.
3322 */
3323 else {
3324 align_alen -= orig_off - align_off;
3325 align_off = orig_off;
3326 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3327 }
3328 /*
3329 * Result doesn't cover the request, fail it.
3330 */
3331 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003332 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003333 } else {
3334 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003335 /* see MAXEXTLEN handling above */
3336 ASSERT(orig_end <= align_off + align_alen ||
3337 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003338 }
3339
3340#ifdef DEBUG
3341 if (!eof && gotp->br_startoff != NULLFILEOFF)
3342 ASSERT(align_off + align_alen <= gotp->br_startoff);
3343 if (prevp->br_startoff != NULLFILEOFF)
3344 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3345#endif
3346
3347 *lenp = align_alen;
3348 *offp = align_off;
3349 return 0;
3350}
3351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352#define XFS_ALLOC_GAP_UNITS 4
3353
Dave Chinner68988112013-08-12 20:49:42 +10003354void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003355xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003356 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
3358 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3360 xfs_mount_t *mp; /* mount point structure */
3361 int nullfb; /* true if ap->firstblock isn't set */
3362 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
3364#define ISVALID(x,y) \
3365 (rt ? \
3366 (x) < mp->m_sb.sb_rblocks : \
3367 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3368 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3369 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3370
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003372 nullfb = *ap->firstblock == NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003374 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 /*
3376 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003377 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003379 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3380 !isnullstartblock(ap->prev.br_startblock) &&
3381 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3382 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003383 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 /*
3385 * Adjust for the gap between prevp and us.
3386 */
Dave Chinner3a756672011-09-18 20:40:58 +00003387 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003388 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003390 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3391 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 }
3393 /*
3394 * If not at eof, then compare the two neighbor blocks.
3395 * Figure out whether either one gives us a good starting point,
3396 * and pick the better one.
3397 */
3398 else if (!ap->eof) {
3399 xfs_fsblock_t gotbno; /* right side block number */
3400 xfs_fsblock_t gotdiff=0; /* right side difference */
3401 xfs_fsblock_t prevbno; /* left side block number */
3402 xfs_fsblock_t prevdiff=0; /* left side difference */
3403
3404 /*
3405 * If there's a previous (left) block, select a requested
3406 * start block based on it.
3407 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003408 if (ap->prev.br_startoff != NULLFILEOFF &&
3409 !isnullstartblock(ap->prev.br_startblock) &&
3410 (prevbno = ap->prev.br_startblock +
3411 ap->prev.br_blockcount) &&
3412 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 /*
3414 * Calculate gap to end of previous block.
3415 */
Dave Chinner3a756672011-09-18 20:40:58 +00003416 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003417 (ap->prev.br_startoff +
3418 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 /*
3420 * Figure the startblock based on the previous block's
3421 * end and the gap size.
3422 * Heuristic!
3423 * If the gap is large relative to the piece we're
3424 * allocating, or using it gives us an invalid block
3425 * number, then just use the end of the previous block.
3426 */
Dave Chinner3a756672011-09-18 20:40:58 +00003427 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003429 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 prevbno += adjust;
3431 else
3432 prevdiff += adjust;
3433 /*
3434 * If the firstblock forbids it, can't use it,
3435 * must use default.
3436 */
3437 if (!rt && !nullfb &&
3438 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3439 prevbno = NULLFSBLOCK;
3440 }
3441 /*
3442 * No previous block or can't follow it, just default.
3443 */
3444 else
3445 prevbno = NULLFSBLOCK;
3446 /*
3447 * If there's a following (right) block, select a requested
3448 * start block based on it.
3449 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003450 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 /*
3452 * Calculate gap to start of next block.
3453 */
Dave Chinner3a756672011-09-18 20:40:58 +00003454 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 /*
3456 * Figure the startblock based on the next block's
3457 * start and the gap size.
3458 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003459 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 /*
3461 * Heuristic!
3462 * If the gap is large relative to the piece we're
3463 * allocating, or using it gives us an invalid block
3464 * number, then just use the start of the next block
3465 * offset by our length.
3466 */
Dave Chinner3a756672011-09-18 20:40:58 +00003467 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 ISVALID(gotbno - gotdiff, gotbno))
3469 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003470 else if (ISVALID(gotbno - ap->length, gotbno)) {
3471 gotbno -= ap->length;
3472 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 } else
3474 gotdiff += adjust;
3475 /*
3476 * If the firstblock forbids it, can't use it,
3477 * must use default.
3478 */
3479 if (!rt && !nullfb &&
3480 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3481 gotbno = NULLFSBLOCK;
3482 }
3483 /*
3484 * No next block, just default.
3485 */
3486 else
3487 gotbno = NULLFSBLOCK;
3488 /*
3489 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003490 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 */
3492 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003493 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003495 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003497 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003499#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003500}
3501
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003502static int
3503xfs_bmap_longest_free_extent(
3504 struct xfs_trans *tp,
3505 xfs_agnumber_t ag,
3506 xfs_extlen_t *blen,
3507 int *notinit)
3508{
3509 struct xfs_mount *mp = tp->t_mountp;
3510 struct xfs_perag *pag;
3511 xfs_extlen_t longest;
3512 int error = 0;
3513
3514 pag = xfs_perag_get(mp, ag);
3515 if (!pag->pagf_init) {
3516 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3517 if (error)
3518 goto out;
3519
3520 if (!pag->pagf_init) {
3521 *notinit = 1;
3522 goto out;
3523 }
3524 }
3525
Dave Chinner50adbcb2015-06-22 10:04:31 +10003526 longest = xfs_alloc_longest_free_extent(mp, pag,
Dave Chinner496817b2015-06-22 10:13:30 +10003527 xfs_alloc_min_freelist(mp, pag));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003528 if (*blen < longest)
3529 *blen = longest;
3530
3531out:
3532 xfs_perag_put(pag);
3533 return error;
3534}
3535
3536static void
3537xfs_bmap_select_minlen(
3538 struct xfs_bmalloca *ap,
3539 struct xfs_alloc_arg *args,
3540 xfs_extlen_t *blen,
3541 int notinit)
3542{
3543 if (notinit || *blen < ap->minlen) {
3544 /*
3545 * Since we did a BUF_TRYLOCK above, it is possible that
3546 * there is space for this request.
3547 */
3548 args->minlen = ap->minlen;
3549 } else if (*blen < args->maxlen) {
3550 /*
3551 * If the best seen length is less than the request length,
3552 * use the best as the minimum.
3553 */
3554 args->minlen = *blen;
3555 } else {
3556 /*
3557 * Otherwise we've seen an extent as big as maxlen, use that
3558 * as the minimum.
3559 */
3560 args->minlen = args->maxlen;
3561 }
3562}
3563
Nathan Scotta365bdd2006-03-14 13:34:16 +11003564STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003565xfs_bmap_btalloc_nullfb(
3566 struct xfs_bmalloca *ap,
3567 struct xfs_alloc_arg *args,
3568 xfs_extlen_t *blen)
3569{
3570 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003571 xfs_agnumber_t ag, startag;
3572 int notinit = 0;
3573 int error;
3574
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003575 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003576 args->total = ap->total;
3577
Christoph Hellwigc467c042010-02-15 23:34:42 +00003578 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3579 if (startag == NULLAGNUMBER)
3580 startag = ag = 0;
3581
Dave Chinner14b064c2011-01-27 12:16:28 +11003582 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003583 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3584 &notinit);
3585 if (error)
3586 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003587
Christoph Hellwigc467c042010-02-15 23:34:42 +00003588 if (++ag == mp->m_sb.sb_agcount)
3589 ag = 0;
3590 if (ag == startag)
3591 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003592 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003593
3594 xfs_bmap_select_minlen(ap, args, blen, notinit);
3595 return 0;
3596}
3597
3598STATIC int
3599xfs_bmap_btalloc_filestreams(
3600 struct xfs_bmalloca *ap,
3601 struct xfs_alloc_arg *args,
3602 xfs_extlen_t *blen)
3603{
3604 struct xfs_mount *mp = ap->ip->i_mount;
3605 xfs_agnumber_t ag;
3606 int notinit = 0;
3607 int error;
3608
3609 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3610 args->total = ap->total;
3611
3612 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3613 if (ag == NULLAGNUMBER)
3614 ag = 0;
3615
3616 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3617 if (error)
3618 return error;
3619
3620 if (*blen < args->maxlen) {
3621 error = xfs_filestream_new_ag(ap, &ag);
3622 if (error)
3623 return error;
3624
3625 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3626 &notinit);
3627 if (error)
3628 return error;
3629
3630 }
3631
3632 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003633
3634 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003635 * Set the failure fallback case to look in the selected AG as stream
3636 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003637 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003638 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003639 return 0;
3640}
3641
3642STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003643xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003644 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003645{
3646 xfs_mount_t *mp; /* mount point structure */
3647 xfs_alloctype_t atype = 0; /* type for allocation routines */
3648 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003649 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003650 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003651 xfs_alloc_arg_t args;
3652 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003653 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003654 int nullfb; /* true if ap->firstblock isn't set */
3655 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003656 int tryagain;
3657 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003658 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003659
Dave Chinnera99ebf42011-12-01 11:24:20 +00003660 ASSERT(ap->length);
3661
Nathan Scotta365bdd2006-03-14 13:34:16 +11003662 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003663
3664 /* stripe alignment for allocation is determined by mount parameters */
3665 stripe_align = 0;
3666 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3667 stripe_align = mp->m_swidth;
3668 else if (mp->m_dalign)
3669 stripe_align = mp->m_dalign;
3670
David Chinner957d0eb2007-06-18 16:50:37 +10003671 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003672 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003673 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003674 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003675 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003676 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003677 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003678 }
Dave Chinner33177f052013-12-12 16:34:36 +11003679
3680
Dave Chinner0937e0f2011-09-18 20:40:57 +00003681 nullfb = *ap->firstblock == NULLFSBLOCK;
3682 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003683 if (nullfb) {
3684 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
3685 ag = xfs_filestream_lookup_ag(ap->ip);
3686 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003687 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003688 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003689 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003690 }
3691 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003692 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003693
3694 xfs_bmap_adjacent(ap);
3695
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003697 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 * it's in the right allocation group.
3699 */
Dave Chinner3a756672011-09-18 20:40:58 +00003700 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 ;
3702 else
Dave Chinner3a756672011-09-18 20:40:58 +00003703 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 * Normal allocation, done through xfs_alloc_vextent.
3706 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003707 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003708 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003709 args.tp = ap->tp;
3710 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003711 args.fsbno = ap->blkno;
Dave Chinner14b064c2011-01-27 12:16:28 +11003712
3713 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner3a756672011-09-18 20:40:58 +00003714 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003715 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003716 blen = 0;
3717 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003718 /*
3719 * Search for an allocation group with a single extent large
3720 * enough for the request. If one isn't found, then adjust
3721 * the minimum allocation size to the largest space found.
3722 */
3723 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3724 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3725 else
3726 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003727 if (error)
3728 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003729 } else if (ap->flist->xbf_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003730 if (xfs_inode_is_filestream(ap->ip))
3731 args.type = XFS_ALLOCTYPE_FIRST_AG;
3732 else
3733 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003734 args.total = args.minlen = ap->minlen;
3735 } else {
3736 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3737 args.total = ap->total;
3738 args.minlen = ap->minlen;
3739 }
David Chinner957d0eb2007-06-18 16:50:37 +10003740 /* apply extent size hints if obtained earlier */
3741 if (unlikely(align)) {
3742 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003743 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003744 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11003745 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003746 args.prod = 1;
3747 args.mod = 0;
3748 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003749 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003750 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003751 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3752 }
3753 /*
3754 * If we are not low on available data blocks, and the
3755 * underlying logical volume manager is a stripe, and
3756 * the file offset is zero then try to allocate data
3757 * blocks on stripe unit boundary.
3758 * NOTE: ap->aeof is only set if the allocation length
3759 * is >= the stripe unit and the allocation offset is
3760 * at the end of file.
3761 */
Dave Chinner0937e0f2011-09-18 20:40:57 +00003762 if (!ap->flist->xbf_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003763 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003764 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003765 atype = args.type;
3766 isaligned = 1;
3767 /*
3768 * Adjust for alignment
3769 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003770 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003771 args.minlen = blen - args.alignment;
3772 args.minalignslop = 0;
3773 } else {
3774 /*
3775 * First try an exact bno allocation.
3776 * If it fails then do a near or start bno
3777 * allocation with alignment turned on.
3778 */
3779 atype = args.type;
3780 tryagain = 1;
3781 args.type = XFS_ALLOCTYPE_THIS_BNO;
3782 args.alignment = 1;
3783 /*
3784 * Compute the minlen+alignment for the
3785 * next case. Set slop so that the value
3786 * of minlen+alignment+slop doesn't go up
3787 * between the calls.
3788 */
Dave Chinner33177f052013-12-12 16:34:36 +11003789 if (blen > stripe_align && blen <= args.maxlen)
3790 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003791 else
3792 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003793 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003794 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003795 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003796 args.minlen - 1;
3797 else
3798 args.minalignslop = 0;
3799 }
3800 } else {
3801 args.alignment = 1;
3802 args.minalignslop = 0;
3803 }
3804 args.minleft = ap->minleft;
3805 args.wasdel = ap->wasdel;
3806 args.isfl = 0;
3807 args.userdata = ap->userdata;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003808 if (ap->userdata & XFS_ALLOC_USERDATA_ZERO)
3809 args.ip = ap->ip;
3810
3811 error = xfs_alloc_vextent(&args);
3812 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003813 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003814
Nathan Scotta365bdd2006-03-14 13:34:16 +11003815 if (tryagain && args.fsbno == NULLFSBLOCK) {
3816 /*
3817 * Exact allocation failed. Now try with alignment
3818 * turned on.
3819 */
3820 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003821 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003822 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003823 args.minlen = nextminlen;
3824 args.minalignslop = 0;
3825 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 if ((error = xfs_alloc_vextent(&args)))
3827 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003828 }
3829 if (isaligned && args.fsbno == NULLFSBLOCK) {
3830 /*
3831 * allocation failed, so turn off alignment and
3832 * try again.
3833 */
3834 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003835 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003836 args.alignment = 0;
3837 if ((error = xfs_alloc_vextent(&args)))
3838 return error;
3839 }
3840 if (args.fsbno == NULLFSBLOCK && nullfb &&
3841 args.minlen > ap->minlen) {
3842 args.minlen = ap->minlen;
3843 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003844 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003845 if ((error = xfs_alloc_vextent(&args)))
3846 return error;
3847 }
3848 if (args.fsbno == NULLFSBLOCK && nullfb) {
3849 args.fsbno = 0;
3850 args.type = XFS_ALLOCTYPE_FIRST_AG;
3851 args.total = ap->minlen;
3852 args.minleft = 0;
3853 if ((error = xfs_alloc_vextent(&args)))
3854 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003855 ap->flist->xbf_low = 1;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003856 }
3857 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003858 /*
3859 * check the allocation happened at the same or higher AG than
3860 * the first block that was allocated.
3861 */
3862 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3863 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3864 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
3865 (ap->flist->xbf_low &&
3866 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3867 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3868
Dave Chinner3a756672011-09-18 20:40:58 +00003869 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003870 if (*ap->firstblock == NULLFSBLOCK)
3871 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003872 ASSERT(nullfb || fb_agno == args.agno ||
Dave Chinner0937e0f2011-09-18 20:40:57 +00003873 (ap->flist->xbf_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003874 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003875 ap->ip->i_d.di_nblocks += args.len;
3876 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3877 if (ap->wasdel)
3878 ap->ip->i_delayed_blks -= args.len;
3879 /*
3880 * Adjust the disk quota also. This was reserved
3881 * earlier.
3882 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003883 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003884 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3885 XFS_TRANS_DQ_BCOUNT,
3886 (long) args.len);
3887 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003888 ap->blkno = NULLFSBLOCK;
3889 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 }
3891 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003892}
3893
3894/*
3895 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3896 * It figures out where to ask the underlying allocator to put the new extent.
3897 */
3898STATIC int
3899xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003900 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003901{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003902 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003903 return xfs_bmap_rtalloc(ap);
3904 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905}
3906
3907/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003908 * Trim the returned map to the required bounds
3909 */
3910STATIC void
3911xfs_bmapi_trim_map(
3912 struct xfs_bmbt_irec *mval,
3913 struct xfs_bmbt_irec *got,
3914 xfs_fileoff_t *bno,
3915 xfs_filblks_t len,
3916 xfs_fileoff_t obno,
3917 xfs_fileoff_t end,
3918 int n,
3919 int flags)
3920{
3921 if ((flags & XFS_BMAPI_ENTIRE) ||
3922 got->br_startoff + got->br_blockcount <= obno) {
3923 *mval = *got;
3924 if (isnullstartblock(got->br_startblock))
3925 mval->br_startblock = DELAYSTARTBLOCK;
3926 return;
3927 }
3928
3929 if (obno > *bno)
3930 *bno = obno;
3931 ASSERT((*bno >= obno) || (n == 0));
3932 ASSERT(*bno < end);
3933 mval->br_startoff = *bno;
3934 if (isnullstartblock(got->br_startblock))
3935 mval->br_startblock = DELAYSTARTBLOCK;
3936 else
3937 mval->br_startblock = got->br_startblock +
3938 (*bno - got->br_startoff);
3939 /*
3940 * Return the minimum of what we got and what we asked for for
3941 * the length. We can use the len variable here because it is
3942 * modified below and we could have been there before coming
3943 * here if the first part of the allocation didn't overlap what
3944 * was asked for.
3945 */
3946 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3947 got->br_blockcount - (*bno - got->br_startoff));
3948 mval->br_state = got->br_state;
3949 ASSERT(mval->br_blockcount <= len);
3950 return;
3951}
3952
3953/*
3954 * Update and validate the extent map to return
3955 */
3956STATIC void
3957xfs_bmapi_update_map(
3958 struct xfs_bmbt_irec **map,
3959 xfs_fileoff_t *bno,
3960 xfs_filblks_t *len,
3961 xfs_fileoff_t obno,
3962 xfs_fileoff_t end,
3963 int *n,
3964 int flags)
3965{
3966 xfs_bmbt_irec_t *mval = *map;
3967
3968 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3969 ((mval->br_startoff + mval->br_blockcount) <= end));
3970 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3971 (mval->br_startoff < obno));
3972
3973 *bno = mval->br_startoff + mval->br_blockcount;
3974 *len = end - *bno;
3975 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3976 /* update previous map with new information */
3977 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3978 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3979 ASSERT(mval->br_state == mval[-1].br_state);
3980 mval[-1].br_blockcount = mval->br_blockcount;
3981 mval[-1].br_state = mval->br_state;
3982 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3983 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3984 mval[-1].br_startblock != HOLESTARTBLOCK &&
3985 mval->br_startblock == mval[-1].br_startblock +
3986 mval[-1].br_blockcount &&
3987 ((flags & XFS_BMAPI_IGSTATE) ||
3988 mval[-1].br_state == mval->br_state)) {
3989 ASSERT(mval->br_startoff ==
3990 mval[-1].br_startoff + mval[-1].br_blockcount);
3991 mval[-1].br_blockcount += mval->br_blockcount;
3992 } else if (*n > 0 &&
3993 mval->br_startblock == DELAYSTARTBLOCK &&
3994 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3995 mval->br_startoff ==
3996 mval[-1].br_startoff + mval[-1].br_blockcount) {
3997 mval[-1].br_blockcount += mval->br_blockcount;
3998 mval[-1].br_state = mval->br_state;
3999 } else if (!((*n == 0) &&
4000 ((mval->br_startoff + mval->br_blockcount) <=
4001 obno))) {
4002 mval++;
4003 (*n)++;
4004 }
4005 *map = mval;
4006}
4007
4008/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004009 * Map file blocks to filesystem blocks without allocation.
4010 */
4011int
4012xfs_bmapi_read(
4013 struct xfs_inode *ip,
4014 xfs_fileoff_t bno,
4015 xfs_filblks_t len,
4016 struct xfs_bmbt_irec *mval,
4017 int *nmap,
4018 int flags)
4019{
4020 struct xfs_mount *mp = ip->i_mount;
4021 struct xfs_ifork *ifp;
4022 struct xfs_bmbt_irec got;
4023 struct xfs_bmbt_irec prev;
4024 xfs_fileoff_t obno;
4025 xfs_fileoff_t end;
4026 xfs_extnum_t lastx;
4027 int error;
4028 int eof;
4029 int n = 0;
4030 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4031 XFS_ATTR_FORK : XFS_DATA_FORK;
4032
4033 ASSERT(*nmap >= 1);
4034 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4035 XFS_BMAPI_IGSTATE)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004036 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004037
4038 if (unlikely(XFS_TEST_ERROR(
4039 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4040 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4041 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4042 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004043 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004044 }
4045
4046 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004047 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004048
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004049 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004050
4051 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004052
4053 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4054 error = xfs_iread_extents(NULL, ip, whichfork);
4055 if (error)
4056 return error;
4057 }
4058
4059 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4060 end = bno + len;
4061 obno = bno;
4062
4063 while (bno < end && n < *nmap) {
4064 /* Reading past eof, act as though there's a hole up to end. */
4065 if (eof)
4066 got.br_startoff = end;
4067 if (got.br_startoff > bno) {
4068 /* Reading in a hole. */
4069 mval->br_startoff = bno;
4070 mval->br_startblock = HOLESTARTBLOCK;
4071 mval->br_blockcount =
4072 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4073 mval->br_state = XFS_EXT_NORM;
4074 bno += mval->br_blockcount;
4075 len -= mval->br_blockcount;
4076 mval++;
4077 n++;
4078 continue;
4079 }
4080
4081 /* set up the extent map to return. */
4082 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4083 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4084
4085 /* If we're done, stop now. */
4086 if (bno >= end || n >= *nmap)
4087 break;
4088
4089 /* Else go on to the next record. */
4090 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4091 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4092 else
4093 eof = 1;
4094 }
4095 *nmap = n;
4096 return 0;
4097}
4098
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004099STATIC int
4100xfs_bmapi_reserve_delalloc(
4101 struct xfs_inode *ip,
4102 xfs_fileoff_t aoff,
4103 xfs_filblks_t len,
4104 struct xfs_bmbt_irec *got,
4105 struct xfs_bmbt_irec *prev,
4106 xfs_extnum_t *lastx,
4107 int eof)
4108{
4109 struct xfs_mount *mp = ip->i_mount;
4110 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4111 xfs_extlen_t alen;
4112 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004113 char rt = XFS_IS_REALTIME_INODE(ip);
4114 xfs_extlen_t extsz;
4115 int error;
4116
4117 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4118 if (!eof)
4119 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4120
4121 /* Figure out the extent size, adjust alen */
4122 extsz = xfs_get_extsz_hint(ip);
4123 if (extsz) {
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004124 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4125 1, 0, &aoff, &alen);
4126 ASSERT(!error);
4127 }
4128
4129 if (rt)
4130 extsz = alen / mp->m_sb.sb_rextsize;
4131
4132 /*
4133 * Make a transaction-less quota reservation for delayed allocation
4134 * blocks. This number gets adjusted later. We return if we haven't
4135 * allocated blocks already inside this loop.
4136 */
4137 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4138 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4139 if (error)
4140 return error;
4141
4142 /*
4143 * Split changing sb for alen and indlen since they could be coming
4144 * from different places.
4145 */
4146 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4147 ASSERT(indlen > 0);
4148
4149 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004150 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004151 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004152 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004153 }
4154
4155 if (error)
4156 goto out_unreserve_quota;
4157
Dave Chinner0d485ad2015-02-23 21:22:03 +11004158 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004159 if (error)
4160 goto out_unreserve_blocks;
4161
4162
4163 ip->i_delayed_blks += alen;
4164
4165 got->br_startoff = aoff;
4166 got->br_startblock = nullstartblock(indlen);
4167 got->br_blockcount = alen;
4168 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004169 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004170
4171 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004172 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4173 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004174 */
4175 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4176
4177 ASSERT(got->br_startoff <= aoff);
4178 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4179 ASSERT(isnullstartblock(got->br_startblock));
4180 ASSERT(got->br_state == XFS_EXT_NORM);
4181 return 0;
4182
4183out_unreserve_blocks:
4184 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004185 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004186 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004187 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004188out_unreserve_quota:
4189 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004190 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004191 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4192 return error;
4193}
4194
Dave Chinner5c8ed202011-09-18 20:40:45 +00004195/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004196 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4197 */
4198int
4199xfs_bmapi_delay(
4200 struct xfs_inode *ip, /* incore inode */
4201 xfs_fileoff_t bno, /* starting file offs. mapped */
4202 xfs_filblks_t len, /* length to map in file */
4203 struct xfs_bmbt_irec *mval, /* output: map values */
4204 int *nmap, /* i/o: mval size/count */
4205 int flags) /* XFS_BMAPI_... */
4206{
4207 struct xfs_mount *mp = ip->i_mount;
4208 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4209 struct xfs_bmbt_irec got; /* current file extent record */
4210 struct xfs_bmbt_irec prev; /* previous file extent record */
4211 xfs_fileoff_t obno; /* old block number (offset) */
4212 xfs_fileoff_t end; /* end of mapped file region */
4213 xfs_extnum_t lastx; /* last useful extent number */
4214 int eof; /* we've hit the end of extents */
4215 int n = 0; /* current extent index */
4216 int error = 0;
4217
4218 ASSERT(*nmap >= 1);
4219 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4220 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004221 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig44032802011-09-18 20:40:48 +00004222
4223 if (unlikely(XFS_TEST_ERROR(
4224 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4225 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4226 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4227 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004228 return -EFSCORRUPTED;
Christoph Hellwig44032802011-09-18 20:40:48 +00004229 }
4230
4231 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004232 return -EIO;
Christoph Hellwig44032802011-09-18 20:40:48 +00004233
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004234 XFS_STATS_INC(mp, xs_blk_mapw);
Christoph Hellwig44032802011-09-18 20:40:48 +00004235
4236 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4237 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4238 if (error)
4239 return error;
4240 }
4241
4242 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4243 end = bno + len;
4244 obno = bno;
4245
4246 while (bno < end && n < *nmap) {
4247 if (eof || got.br_startoff > bno) {
4248 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4249 &prev, &lastx, eof);
4250 if (error) {
4251 if (n == 0) {
4252 *nmap = 0;
4253 return error;
4254 }
4255 break;
4256 }
4257 }
4258
4259 /* set up the extent map to return. */
4260 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4261 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4262
4263 /* If we're done, stop now. */
4264 if (bno >= end || n >= *nmap)
4265 break;
4266
4267 /* Else go on to the next record. */
4268 prev = got;
4269 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4270 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4271 else
4272 eof = 1;
4273 }
4274
4275 *nmap = n;
4276 return 0;
4277}
4278
4279
Dave Chinnercf11da92014-07-15 07:08:24 +10004280static int
4281xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004282 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004283{
4284 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004285 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004286 XFS_ATTR_FORK : XFS_DATA_FORK;
4287 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004288 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004289 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004290
Dave Chinnera99ebf42011-12-01 11:24:20 +00004291 ASSERT(bma->length > 0);
4292
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004293 /*
4294 * For the wasdelay case, we could also just allocate the stuff asked
4295 * for in this bmap call but that wouldn't be as good.
4296 */
4297 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004298 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4299 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004300 if (bma->idx != NULLEXTNUM && bma->idx) {
4301 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004302 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004303 }
4304 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004305 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004306 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004307 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004308 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004309 }
4310
4311 /*
4312 * Indicate if this is the first user data in the file, or just any
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004313 * user data. And if it is userdata, indicate whether it needs to
4314 * be initialised to zero during allocation.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004315 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004316 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004317 bma->userdata = (bma->offset == 0) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004318 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004319 if (bma->flags & XFS_BMAPI_ZERO)
4320 bma->userdata |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004321 }
4322
Dave Chinnere04426b2012-10-05 11:06:59 +10004323 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004324
4325 /*
4326 * Only want to do the alignment at the eof if it is userdata and
4327 * allocation length is larger than a stripe unit.
4328 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004329 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004330 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004331 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004332 if (error)
4333 return error;
4334 }
4335
4336 error = xfs_bmap_alloc(bma);
4337 if (error)
4338 return error;
4339
Dave Chinner0937e0f2011-09-18 20:40:57 +00004340 if (bma->flist->xbf_low)
4341 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004342 if (bma->cur)
4343 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004344 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004345 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004346 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4347 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4348 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4349 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004350 }
4351 /*
4352 * Bump the number of extents we've allocated
4353 * in this call.
4354 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004355 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004356
Dave Chinner29c8d172011-09-18 20:41:00 +00004357 if (bma->cur)
4358 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004359 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4360
Dave Chinner963c30c2011-09-18 20:40:59 +00004361 bma->got.br_startoff = bma->offset;
4362 bma->got.br_startblock = bma->blkno;
4363 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004364 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004365
4366 /*
4367 * A wasdelay extent has been initialized, so shouldn't be flagged
4368 * as unwritten.
4369 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004370 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004371 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004372 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004373
Christoph Hellwigc6534242011-09-18 20:41:05 +00004374 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004375 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004376 else
4377 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004378
Christoph Hellwigc315c902011-09-18 20:41:02 +00004379 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004380 if (error)
4381 return error;
4382
4383 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004384 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4385 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4386 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004387 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004388 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004389
Dave Chinner963c30c2011-09-18 20:40:59 +00004390 ASSERT(bma->got.br_startoff <= bma->offset);
4391 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4392 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004393 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4394 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004395 return 0;
4396}
4397
Dave Chinnerb447fe52011-09-18 20:40:51 +00004398STATIC int
4399xfs_bmapi_convert_unwritten(
4400 struct xfs_bmalloca *bma,
4401 struct xfs_bmbt_irec *mval,
4402 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004403 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004404{
4405 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4406 XFS_ATTR_FORK : XFS_DATA_FORK;
4407 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004408 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004409 int error;
4410
Dave Chinnerb447fe52011-09-18 20:40:51 +00004411 /* check if we need to do unwritten->real conversion */
4412 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4413 (flags & XFS_BMAPI_PREALLOC))
4414 return 0;
4415
4416 /* check if we need to do real->unwritten conversion */
4417 if (mval->br_state == XFS_EXT_NORM &&
4418 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4419 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4420 return 0;
4421
4422 /*
4423 * Modify (by adding) the state flag, if writing.
4424 */
4425 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004426 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4427 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004428 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004429 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4430 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004431 }
4432 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4433 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4434
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004435 /*
4436 * Before insertion into the bmbt, zero the range being converted
4437 * if required.
4438 */
4439 if (flags & XFS_BMAPI_ZERO) {
4440 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4441 mval->br_blockcount);
4442 if (error)
4443 return error;
4444 }
4445
Dave Chinnere0c3da52011-09-18 20:41:01 +00004446 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004447 &bma->cur, mval, bma->firstblock, bma->flist,
4448 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004449 /*
4450 * Log the inode core unconditionally in the unwritten extent conversion
4451 * path because the conversion might not have done so (e.g., if the
4452 * extent count hasn't changed). We need to make sure the inode is dirty
4453 * in the transaction for the sake of fsync(), even if nothing has
4454 * changed, because fsync() will not force the log for this transaction
4455 * unless it sees the inode pinned.
4456 */
4457 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004458 if (error)
4459 return error;
4460
4461 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004462 * Update our extent pointer, given that
4463 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4464 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004465 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004466 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004467
4468 /*
4469 * We may have combined previously unwritten space with written space,
4470 * so generate another request.
4471 */
4472 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004473 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004474 return 0;
4475}
4476
Christoph Hellwig44032802011-09-18 20:40:48 +00004477/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004478 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4479 * extent state if necessary. Details behaviour is controlled by the flags
4480 * parameter. Only allocates blocks from a single allocation group, to avoid
4481 * locking problems.
4482 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 * The returned value in "firstblock" from the first call in a transaction
4484 * must be remembered and presented to subsequent calls in "firstblock".
4485 * An upper bound for the number of blocks to be allocated is supplied to
4486 * the first call in "total"; if no allocation group has that many free
4487 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4488 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004489int
4490xfs_bmapi_write(
4491 struct xfs_trans *tp, /* transaction pointer */
4492 struct xfs_inode *ip, /* incore inode */
4493 xfs_fileoff_t bno, /* starting file offs. mapped */
4494 xfs_filblks_t len, /* length to map in file */
4495 int flags, /* XFS_BMAPI_... */
4496 xfs_fsblock_t *firstblock, /* first allocated block
4497 controls a.g. for allocs */
4498 xfs_extlen_t total, /* total blocks needed */
4499 struct xfs_bmbt_irec *mval, /* output: map values */
4500 int *nmap, /* i/o: mval size/count */
4501 struct xfs_bmap_free *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004503 struct xfs_mount *mp = ip->i_mount;
4504 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004505 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004506 xfs_fileoff_t end; /* end of mapped file region */
4507 int eof; /* after the end of extents */
4508 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004509 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004510 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004511 int whichfork; /* data or attr fork */
4512 char inhole; /* current location is hole in file */
4513 char wasdelay; /* old extent was delayed */
4514
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004516 xfs_fileoff_t orig_bno; /* original block number value */
4517 int orig_flags; /* original flags arg value */
4518 xfs_filblks_t orig_len; /* original value of len arg */
4519 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4520 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
4522 orig_bno = bno;
4523 orig_len = len;
4524 orig_flags = flags;
4525 orig_mval = mval;
4526 orig_nmap = *nmap;
4527#endif
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004528 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4529 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004530
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004532 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4533 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4534 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004535 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004536 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004537 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004538
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004539 /* zeroing is for currently only for data extents, not metadata */
4540 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4541 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4542 /*
4543 * we can allocate unwritten extents or pre-zero allocated blocks,
4544 * but it makes no sense to do both at once. This would result in
4545 * zeroing the unwritten extent twice, but it still being an
4546 * unwritten extent....
4547 */
4548 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4549 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4550
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 if (unlikely(XFS_TEST_ERROR(
4552 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004553 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004555 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004556 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004560 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004563
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004564 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004565
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004566 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004568 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004570 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004571 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004572 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004573 }
4574
4575 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4576 error = xfs_iread_extents(tp, ip, whichfork);
4577 if (error)
4578 goto error0;
4579 }
4580
Dave Chinnere0c3da52011-09-18 20:41:01 +00004581 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004582 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 n = 0;
4584 end = bno + len;
4585 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004586
4587 bma.tp = tp;
4588 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004589 bma.total = total;
4590 bma.userdata = 0;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004591 bma.flist = flist;
4592 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004595 inhole = eof || bma.got.br_startoff > bno;
4596 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004597
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 /*
4599 * First, deal with the hole before the allocated space
4600 * that we found, if any.
4601 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004602 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004603 bma.eof = eof;
4604 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4605 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004606 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004607 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004608
Dave Chinnera99ebf42011-12-01 11:24:20 +00004609 /*
4610 * There's a 32/64 bit type mismatch between the
4611 * allocation length request (which can be 64 bits in
4612 * length) and the bma length request, which is
4613 * xfs_extlen_t and therefore 32 bits. Hence we have to
4614 * check for 32-bit overflows and handle them here.
4615 */
4616 if (len > (xfs_filblks_t)MAXEXTLEN)
4617 bma.length = MAXEXTLEN;
4618 else
4619 bma.length = len;
4620
4621 ASSERT(len > 0);
4622 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004623 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 if (error)
4625 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004626 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004627 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004629
Dave Chinneraef9a892011-09-18 20:40:44 +00004630 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004631 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4632 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633
Dave Chinnerb447fe52011-09-18 20:40:51 +00004634 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004635 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004636 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004637 continue;
4638 if (error)
4639 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
Dave Chinneraef9a892011-09-18 20:40:44 +00004641 /* update the extent map to return */
4642 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4643
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 /*
4645 * If we're done, stop now. Stop when we've allocated
4646 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4647 * the transaction may get too big.
4648 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004649 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004651
4652 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004653 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004654 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4655 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4656 &bma.got);
4657 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00004658 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004661
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 /*
4663 * Transform from btree to extents, give it cur.
4664 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004665 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004666 int tmp_logflags = 0;
4667
Dave Chinner29c8d172011-09-18 20:41:00 +00004668 ASSERT(bma.cur);
4669 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004671 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 if (error)
4673 goto error0;
4674 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004675
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004677 XFS_IFORK_NEXTENTS(ip, whichfork) >
4678 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680error0:
4681 /*
4682 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004683 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004685 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004687 bma.logflags &= ~xfs_ilog_fext(whichfork);
4688 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004690 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 /*
4692 * Log whatever the flags say, even if error. Otherwise we might miss
4693 * detecting a case where the data is changed, there's an error,
4694 * and it's not logged so we don't shutdown when we should.
4695 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004696 if (bma.logflags)
4697 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004698
Dave Chinner29c8d172011-09-18 20:41:00 +00004699 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 if (!error) {
4701 ASSERT(*firstblock == NULLFSBLOCK ||
4702 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4703 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004704 bma.cur->bc_private.b.firstblock) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 (flist->xbf_low &&
4706 XFS_FSB_TO_AGNO(mp, *firstblock) <
4707 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004708 bma.cur->bc_private.b.firstblock)));
4709 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004711 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4713 }
4714 if (!error)
4715 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4716 orig_nmap, *nmap);
4717 return error;
4718}
4719
4720/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004721 * When a delalloc extent is split (e.g., due to a hole punch), the original
4722 * indlen reservation must be shared across the two new extents that are left
4723 * behind.
4724 *
4725 * Given the original reservation and the worst case indlen for the two new
4726 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004727 * reservation fairly across the two new extents. If necessary, steal available
4728 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4729 * ores == 1). The number of stolen blocks is returned. The availability and
4730 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004731 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004732static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004733xfs_bmap_split_indlen(
4734 xfs_filblks_t ores, /* original res. */
4735 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004736 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4737 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004738{
4739 xfs_filblks_t len1 = *indlen1;
4740 xfs_filblks_t len2 = *indlen2;
4741 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004742 xfs_filblks_t stolen = 0;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004743
4744 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004745 * Steal as many blocks as we can to try and satisfy the worst case
4746 * indlen for both new extents.
4747 */
4748 while (nres > ores && avail) {
4749 nres--;
4750 avail--;
4751 stolen++;
4752 }
4753
4754 /*
4755 * The only blocks available are those reserved for the original
4756 * extent and what we can steal from the extent being removed.
4757 * If this still isn't enough to satisfy the combined
4758 * requirements for the two new extents, skim blocks off of each
4759 * of the new reservations until they match what is available.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004760 */
4761 while (nres > ores) {
4762 if (len1) {
4763 len1--;
4764 nres--;
4765 }
4766 if (nres == ores)
4767 break;
4768 if (len2) {
4769 len2--;
4770 nres--;
4771 }
4772 }
4773
4774 *indlen1 = len1;
4775 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004776
4777 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004778}
4779
4780/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004781 * Called by xfs_bmapi to update file extent records and the btree
4782 * after removing space (or undoing a delayed allocation).
4783 */
4784STATIC int /* error */
4785xfs_bmap_del_extent(
4786 xfs_inode_t *ip, /* incore inode pointer */
4787 xfs_trans_t *tp, /* current transaction pointer */
4788 xfs_extnum_t *idx, /* extent number to update/delete */
4789 xfs_bmap_free_t *flist, /* list of extents to be freed */
4790 xfs_btree_cur_t *cur, /* if null, not a btree */
4791 xfs_bmbt_irec_t *del, /* data to remove from extents */
4792 int *logflagsp, /* inode logging flags */
4793 int whichfork) /* data or attr fork */
4794{
4795 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
4796 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
4797 xfs_fsblock_t del_endblock=0; /* first block past del */
4798 xfs_fileoff_t del_endoff; /* first offset past del */
4799 int delay; /* current block is delayed allocated */
4800 int do_fx; /* free extent at end of routine */
4801 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
4802 int error; /* error return value */
4803 int flags; /* inode logging flags */
4804 xfs_bmbt_irec_t got; /* current extent entry */
4805 xfs_fileoff_t got_endoff; /* first offset past got */
4806 int i; /* temp state */
4807 xfs_ifork_t *ifp; /* inode fork pointer */
4808 xfs_mount_t *mp; /* mount structure */
4809 xfs_filblks_t nblks; /* quota/sb block count */
4810 xfs_bmbt_irec_t new; /* new record to be inserted */
4811 /* REFERENCED */
4812 uint qfield; /* quota field to update */
4813 xfs_filblks_t temp; /* for indirect length calculations */
4814 xfs_filblks_t temp2; /* for indirect length calculations */
4815 int state = 0;
4816
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004817 mp = ip->i_mount;
4818 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004819
4820 if (whichfork == XFS_ATTR_FORK)
4821 state |= BMAP_ATTRFORK;
4822
Dave Chinner9e5987a2013-02-25 12:31:26 +11004823 ifp = XFS_IFORK_PTR(ip, whichfork);
4824 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4825 (uint)sizeof(xfs_bmbt_rec_t)));
4826 ASSERT(del->br_blockcount > 0);
4827 ep = xfs_iext_get_ext(ifp, *idx);
4828 xfs_bmbt_get_all(ep, &got);
4829 ASSERT(got.br_startoff <= del->br_startoff);
4830 del_endoff = del->br_startoff + del->br_blockcount;
4831 got_endoff = got.br_startoff + got.br_blockcount;
4832 ASSERT(got_endoff >= del_endoff);
4833 delay = isnullstartblock(got.br_startblock);
4834 ASSERT(isnullstartblock(del->br_startblock) == delay);
4835 flags = 0;
4836 qfield = 0;
4837 error = 0;
4838 /*
4839 * If deleting a real allocation, must free up the disk space.
4840 */
4841 if (!delay) {
4842 flags = XFS_ILOG_CORE;
4843 /*
4844 * Realtime allocation. Free it and record di_nblocks update.
4845 */
4846 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4847 xfs_fsblock_t bno;
4848 xfs_filblks_t len;
4849
4850 ASSERT(do_mod(del->br_blockcount,
4851 mp->m_sb.sb_rextsize) == 0);
4852 ASSERT(do_mod(del->br_startblock,
4853 mp->m_sb.sb_rextsize) == 0);
4854 bno = del->br_startblock;
4855 len = del->br_blockcount;
4856 do_div(bno, mp->m_sb.sb_rextsize);
4857 do_div(len, mp->m_sb.sb_rextsize);
4858 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4859 if (error)
4860 goto done;
4861 do_fx = 0;
4862 nblks = len * mp->m_sb.sb_rextsize;
4863 qfield = XFS_TRANS_DQ_RTBCOUNT;
4864 }
4865 /*
4866 * Ordinary allocation.
4867 */
4868 else {
4869 do_fx = 1;
4870 nblks = del->br_blockcount;
4871 qfield = XFS_TRANS_DQ_BCOUNT;
4872 }
4873 /*
4874 * Set up del_endblock and cur for later.
4875 */
4876 del_endblock = del->br_startblock + del->br_blockcount;
4877 if (cur) {
4878 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
4879 got.br_startblock, got.br_blockcount,
4880 &i)))
4881 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004882 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004883 }
4884 da_old = da_new = 0;
4885 } else {
4886 da_old = startblockval(got.br_startblock);
4887 da_new = 0;
4888 nblks = 0;
4889 do_fx = 0;
4890 }
4891 /*
4892 * Set flag value to use in switch statement.
4893 * Left-contig is 2, right-contig is 1.
4894 */
4895 switch (((got.br_startoff == del->br_startoff) << 1) |
4896 (got_endoff == del_endoff)) {
4897 case 3:
4898 /*
4899 * Matches the whole extent. Delete the entry.
4900 */
4901 xfs_iext_remove(ip, *idx, 1,
4902 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
4903 --*idx;
4904 if (delay)
4905 break;
4906
4907 XFS_IFORK_NEXT_SET(ip, whichfork,
4908 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4909 flags |= XFS_ILOG_CORE;
4910 if (!cur) {
4911 flags |= xfs_ilog_fext(whichfork);
4912 break;
4913 }
4914 if ((error = xfs_btree_delete(cur, &i)))
4915 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004916 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004917 break;
4918
4919 case 2:
4920 /*
4921 * Deleting the first part of the extent.
4922 */
4923 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4924 xfs_bmbt_set_startoff(ep, del_endoff);
4925 temp = got.br_blockcount - del->br_blockcount;
4926 xfs_bmbt_set_blockcount(ep, temp);
4927 if (delay) {
4928 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4929 da_old);
4930 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4931 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4932 da_new = temp;
4933 break;
4934 }
4935 xfs_bmbt_set_startblock(ep, del_endblock);
4936 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4937 if (!cur) {
4938 flags |= xfs_ilog_fext(whichfork);
4939 break;
4940 }
4941 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
4942 got.br_blockcount - del->br_blockcount,
4943 got.br_state)))
4944 goto done;
4945 break;
4946
4947 case 1:
4948 /*
4949 * Deleting the last part of the extent.
4950 */
4951 temp = got.br_blockcount - del->br_blockcount;
4952 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4953 xfs_bmbt_set_blockcount(ep, temp);
4954 if (delay) {
4955 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4956 da_old);
4957 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4958 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4959 da_new = temp;
4960 break;
4961 }
4962 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4963 if (!cur) {
4964 flags |= xfs_ilog_fext(whichfork);
4965 break;
4966 }
4967 if ((error = xfs_bmbt_update(cur, got.br_startoff,
4968 got.br_startblock,
4969 got.br_blockcount - del->br_blockcount,
4970 got.br_state)))
4971 goto done;
4972 break;
4973
4974 case 0:
4975 /*
4976 * Deleting the middle of the extent.
4977 */
4978 temp = del->br_startoff - got.br_startoff;
4979 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4980 xfs_bmbt_set_blockcount(ep, temp);
4981 new.br_startoff = del_endoff;
4982 temp2 = got_endoff - del_endoff;
4983 new.br_blockcount = temp2;
4984 new.br_state = got.br_state;
4985 if (!delay) {
4986 new.br_startblock = del_endblock;
4987 flags |= XFS_ILOG_CORE;
4988 if (cur) {
4989 if ((error = xfs_bmbt_update(cur,
4990 got.br_startoff,
4991 got.br_startblock, temp,
4992 got.br_state)))
4993 goto done;
4994 if ((error = xfs_btree_increment(cur, 0, &i)))
4995 goto done;
4996 cur->bc_rec.b = new;
4997 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10004998 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004999 goto done;
5000 /*
5001 * If get no-space back from btree insert,
5002 * it tried a split, and we have a zero
5003 * block reservation.
5004 * Fix up our state and return the error.
5005 */
Dave Chinner24513372014-06-25 14:58:08 +10005006 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11005007 /*
5008 * Reset the cursor, don't trust
5009 * it after any insert operation.
5010 */
5011 if ((error = xfs_bmbt_lookup_eq(cur,
5012 got.br_startoff,
5013 got.br_startblock,
5014 temp, &i)))
5015 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005016 XFS_WANT_CORRUPTED_GOTO(mp,
5017 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005018 /*
5019 * Update the btree record back
5020 * to the original value.
5021 */
5022 if ((error = xfs_bmbt_update(cur,
5023 got.br_startoff,
5024 got.br_startblock,
5025 got.br_blockcount,
5026 got.br_state)))
5027 goto done;
5028 /*
5029 * Reset the extent record back
5030 * to the original value.
5031 */
5032 xfs_bmbt_set_blockcount(ep,
5033 got.br_blockcount);
5034 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005035 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005036 goto done;
5037 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005038 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005039 } else
5040 flags |= xfs_ilog_fext(whichfork);
5041 XFS_IFORK_NEXT_SET(ip, whichfork,
5042 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5043 } else {
Brian Fosterd34999c92016-03-15 11:42:47 +11005044 xfs_filblks_t stolen;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005045 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005046
5047 /*
5048 * Distribute the original indlen reservation across the
Brian Fosterd34999c92016-03-15 11:42:47 +11005049 * two new extents. Steal blocks from the deleted extent
5050 * if necessary. Stealing blocks simply fudges the
5051 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005052 */
5053 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5054 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c92016-03-15 11:42:47 +11005055 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5056 del->br_blockcount);
5057 da_new = temp + temp2 - stolen;
5058 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005059
5060 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11005061 * Set the reservation for each extent. Warn if either
5062 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005063 */
Brian Fosterd34999c92016-03-15 11:42:47 +11005064 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005065 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005066 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005067 }
5068 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5069 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5070 ++*idx;
5071 break;
5072 }
5073 /*
5074 * If we need to, add to list of extents to delete.
5075 */
5076 if (do_fx)
5077 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
5078 mp);
5079 /*
5080 * Adjust inode # blocks in the file.
5081 */
5082 if (nblks)
5083 ip->i_d.di_nblocks -= nblks;
5084 /*
5085 * Adjust quota data.
5086 */
5087 if (qfield)
5088 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5089
5090 /*
5091 * Account for change in delayed indirect blocks.
5092 * Nothing to do for disk quota accounting here.
5093 */
5094 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005095 if (da_old > da_new)
5096 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005097done:
5098 *logflagsp = flags;
5099 return error;
5100}
5101
5102/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 * Unmap (remove) blocks from a file.
5104 * If nexts is nonzero then the number of extents to remove is limited to
5105 * that value. If not all extents in the block range can be removed then
5106 * *done is set.
5107 */
5108int /* error */
5109xfs_bunmapi(
5110 xfs_trans_t *tp, /* transaction pointer */
5111 struct xfs_inode *ip, /* incore inode */
5112 xfs_fileoff_t bno, /* starting offset to unmap */
5113 xfs_filblks_t len, /* length to unmap in file */
5114 int flags, /* misc flags */
5115 xfs_extnum_t nexts, /* number of extents max */
5116 xfs_fsblock_t *firstblock, /* first allocated block
5117 controls a.g. for allocs */
5118 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5119 int *done) /* set if not done yet */
5120{
5121 xfs_btree_cur_t *cur; /* bmap btree cursor */
5122 xfs_bmbt_irec_t del; /* extent being deleted */
5123 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005124 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 int error; /* error return value */
5126 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005127 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 xfs_ifork_t *ifp; /* inode fork pointer */
5129 int isrt; /* freeing in rt area */
5130 xfs_extnum_t lastx; /* last extent index used */
5131 int logflags; /* transaction logging flags */
5132 xfs_extlen_t mod; /* rt extent offset */
5133 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005134 xfs_extnum_t nextents; /* number of file extents */
5135 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005136 xfs_fileoff_t start; /* first file offset deleted */
5137 int tmp_logflags; /* partial logging flags */
5138 int wasdel; /* was a delayed alloc extent */
5139 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140 xfs_fsblock_t sum;
5141
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005142 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5143
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5145 XFS_ATTR_FORK : XFS_DATA_FORK;
5146 ifp = XFS_IFORK_PTR(ip, whichfork);
5147 if (unlikely(
5148 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5149 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5150 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5151 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005152 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 }
5154 mp = ip->i_mount;
5155 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005156 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005157
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005158 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 ASSERT(len > 0);
5160 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005161
Linus Torvalds1da177e2005-04-16 15:20:36 -07005162 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5163 (error = xfs_iread_extents(tp, ip, whichfork)))
5164 return error;
5165 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5166 if (nextents == 0) {
5167 *done = 1;
5168 return 0;
5169 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005170 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005171 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005172 start = bno;
5173 bno = start + len - 1;
5174 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5175 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005176
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 /*
5178 * Check to see if the given block number is past the end of the
5179 * file, back up to the last block if so...
5180 */
5181 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005182 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 xfs_bmbt_get_all(ep, &got);
5184 bno = got.br_startoff + got.br_blockcount - 1;
5185 }
5186 logflags = 0;
5187 if (ifp->if_flags & XFS_IFBROOT) {
5188 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005189 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 cur->bc_private.b.firstblock = *firstblock;
5191 cur->bc_private.b.flist = flist;
5192 cur->bc_private.b.flags = 0;
5193 } else
5194 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005195
5196 if (isrt) {
5197 /*
5198 * Synchronize by locking the bitmap inode.
5199 */
5200 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5201 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5202 }
5203
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204 extno = 0;
5205 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5206 (nexts == 0 || extno < nexts)) {
5207 /*
5208 * Is the found extent after a hole in which bno lives?
5209 * Just back up to the previous extent, if so.
5210 */
5211 if (got.br_startoff > bno) {
5212 if (--lastx < 0)
5213 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005214 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 xfs_bmbt_get_all(ep, &got);
5216 }
5217 /*
5218 * Is the last block of this extent before the range
5219 * we're supposed to delete? If so, we're done.
5220 */
5221 bno = XFS_FILEOFF_MIN(bno,
5222 got.br_startoff + got.br_blockcount - 1);
5223 if (bno < start)
5224 break;
5225 /*
5226 * Then deal with the (possibly delayed) allocated space
5227 * we found.
5228 */
5229 ASSERT(ep != NULL);
5230 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005231 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 if (got.br_startoff < start) {
5233 del.br_startoff = start;
5234 del.br_blockcount -= start - got.br_startoff;
5235 if (!wasdel)
5236 del.br_startblock += start - got.br_startoff;
5237 }
5238 if (del.br_startoff + del.br_blockcount > bno + 1)
5239 del.br_blockcount = bno + 1 - del.br_startoff;
5240 sum = del.br_startblock + del.br_blockcount;
5241 if (isrt &&
5242 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5243 /*
5244 * Realtime extent not lined up at the end.
5245 * The extent could have been split into written
5246 * and unwritten pieces, or we could just be
5247 * unmapping part of it. But we can't really
5248 * get rid of part of a realtime extent.
5249 */
5250 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005251 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252 /*
5253 * This piece is unwritten, or we're not
5254 * using unwritten extents. Skip over it.
5255 */
5256 ASSERT(bno >= mod);
5257 bno -= mod > del.br_blockcount ?
5258 del.br_blockcount : mod;
5259 if (bno < got.br_startoff) {
5260 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005261 xfs_bmbt_get_all(xfs_iext_get_ext(
5262 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 }
5264 continue;
5265 }
5266 /*
5267 * It's written, turn it unwritten.
5268 * This is better than zeroing it.
5269 */
5270 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005271 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 /*
5273 * If this spans a realtime extent boundary,
5274 * chop it back to the start of the one we end at.
5275 */
5276 if (del.br_blockcount > mod) {
5277 del.br_startoff += del.br_blockcount - mod;
5278 del.br_startblock += del.br_blockcount - mod;
5279 del.br_blockcount = mod;
5280 }
5281 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005282 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5283 &lastx, &cur, &del, firstblock, flist,
5284 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 if (error)
5286 goto error0;
5287 goto nodelete;
5288 }
5289 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5290 /*
5291 * Realtime extent is lined up at the end but not
5292 * at the front. We'll get rid of full extents if
5293 * we can.
5294 */
5295 mod = mp->m_sb.sb_rextsize - mod;
5296 if (del.br_blockcount > mod) {
5297 del.br_blockcount -= mod;
5298 del.br_startoff += mod;
5299 del.br_startblock += mod;
5300 } else if ((del.br_startoff == start &&
5301 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005302 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005303 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 /*
5305 * Can't make it unwritten. There isn't
5306 * a full extent here so just skip it.
5307 */
5308 ASSERT(bno >= del.br_blockcount);
5309 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005310 if (got.br_startoff > bno) {
5311 if (--lastx >= 0) {
5312 ep = xfs_iext_get_ext(ifp,
5313 lastx);
5314 xfs_bmbt_get_all(ep, &got);
5315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 }
5317 continue;
5318 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5319 /*
5320 * This one is already unwritten.
5321 * It must have a written left neighbor.
5322 * Unwrite the killed part of that one and
5323 * try again.
5324 */
5325 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005326 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5327 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005329 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005330 ASSERT(del.br_startblock ==
5331 prev.br_startblock + prev.br_blockcount);
5332 if (prev.br_startoff < start) {
5333 mod = start - prev.br_startoff;
5334 prev.br_blockcount -= mod;
5335 prev.br_startblock += mod;
5336 prev.br_startoff = start;
5337 }
5338 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005339 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005340 error = xfs_bmap_add_extent_unwritten_real(tp,
5341 ip, &lastx, &cur, &prev,
5342 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 if (error)
5344 goto error0;
5345 goto nodelete;
5346 } else {
5347 ASSERT(del.br_state == XFS_EXT_NORM);
5348 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005349 error = xfs_bmap_add_extent_unwritten_real(tp,
5350 ip, &lastx, &cur, &del,
5351 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 if (error)
5353 goto error0;
5354 goto nodelete;
5355 }
5356 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005357
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358 /*
5359 * If it's the case where the directory code is running
5360 * with no block reservation, and the deleted block is in
5361 * the middle of its extent, and the resulting insert
5362 * of an extent would cause transformation to btree format,
5363 * then reject it. The calling code will then swap
5364 * blocks around instead.
5365 * We have to do this now, rather than waiting for the
5366 * conversion to btree format, since the transaction
5367 * will be dirty.
5368 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005369 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005371 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5372 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005373 del.br_startoff > got.br_startoff &&
5374 del.br_startoff + del.br_blockcount <
5375 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005376 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 goto error0;
5378 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005379
5380 /*
5381 * Unreserve quota and update realtime free space, if
5382 * appropriate. If delayed allocation, update the inode delalloc
5383 * counter now and wait to update the sb counters as
5384 * xfs_bmap_del_extent() might need to borrow some blocks.
5385 */
5386 if (wasdel) {
5387 ASSERT(startblockval(del.br_startblock) > 0);
5388 if (isrt) {
5389 xfs_filblks_t rtexts;
5390
5391 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5392 do_div(rtexts, mp->m_sb.sb_rextsize);
5393 xfs_mod_frextents(mp, (int64_t)rtexts);
5394 (void)xfs_trans_reserve_quota_nblks(NULL,
5395 ip, -((long)del.br_blockcount), 0,
5396 XFS_QMOPT_RES_RTBLKS);
5397 } else {
5398 (void)xfs_trans_reserve_quota_nblks(NULL,
5399 ip, -((long)del.br_blockcount), 0,
5400 XFS_QMOPT_RES_REGBLKS);
5401 }
5402 ip->i_delayed_blks -= del.br_blockcount;
5403 if (cur)
5404 cur->bc_private.b.flags |=
5405 XFS_BTCUR_BPRV_WASDEL;
5406 } else if (cur)
5407 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5408
Christoph Hellwigec90c552011-05-23 08:52:53 +00005409 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005410 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 logflags |= tmp_logflags;
5412 if (error)
5413 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005414
5415 if (!isrt && wasdel)
5416 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5417
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418 bno = del.br_startoff - 1;
5419nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005420 /*
5421 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005424 if (lastx >= 0) {
5425 ep = xfs_iext_get_ext(ifp, lastx);
5426 if (xfs_bmbt_get_startoff(ep) > bno) {
5427 if (--lastx >= 0)
5428 ep = xfs_iext_get_ext(ifp,
5429 lastx);
5430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 extno++;
5434 }
5435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005437
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 /*
5439 * Convert to a btree if necessary.
5440 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005441 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442 ASSERT(cur == NULL);
5443 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5444 &cur, 0, &tmp_logflags, whichfork);
5445 logflags |= tmp_logflags;
5446 if (error)
5447 goto error0;
5448 }
5449 /*
5450 * transform from btree to extents, give it cur
5451 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005452 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453 ASSERT(cur != NULL);
5454 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5455 whichfork);
5456 logflags |= tmp_logflags;
5457 if (error)
5458 goto error0;
5459 }
5460 /*
5461 * transform from extents to local?
5462 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463 error = 0;
5464error0:
5465 /*
5466 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005467 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005469 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005470 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005471 logflags &= ~xfs_ilog_fext(whichfork);
5472 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005474 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475 /*
5476 * Log inode even in the error case, if the transaction
5477 * is dirty we'll need to shut down the filesystem.
5478 */
5479 if (logflags)
5480 xfs_trans_log_inode(tp, ip, logflags);
5481 if (cur) {
5482 if (!error) {
5483 *firstblock = cur->bc_private.b.firstblock;
5484 cur->bc_private.b.allocated = 0;
5485 }
5486 xfs_btree_del_cursor(cur,
5487 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5488 }
5489 return error;
5490}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005491
5492/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005493 * Determine whether an extent shift can be accomplished by a merge with the
5494 * extent that precedes the target hole of the shift.
5495 */
5496STATIC bool
5497xfs_bmse_can_merge(
5498 struct xfs_bmbt_irec *left, /* preceding extent */
5499 struct xfs_bmbt_irec *got, /* current extent to shift */
5500 xfs_fileoff_t shift) /* shift fsb */
5501{
5502 xfs_fileoff_t startoff;
5503
5504 startoff = got->br_startoff - shift;
5505
5506 /*
5507 * The extent, once shifted, must be adjacent in-file and on-disk with
5508 * the preceding extent.
5509 */
5510 if ((left->br_startoff + left->br_blockcount != startoff) ||
5511 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5512 (left->br_state != got->br_state) ||
5513 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5514 return false;
5515
5516 return true;
5517}
5518
5519/*
5520 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5521 * hole in the file. If an extent shift would result in the extent being fully
5522 * adjacent to the extent that currently precedes the hole, we can merge with
5523 * the preceding extent rather than do the shift.
5524 *
5525 * This function assumes the caller has verified a shift-by-merge is possible
5526 * with the provided extents via xfs_bmse_can_merge().
5527 */
5528STATIC int
5529xfs_bmse_merge(
5530 struct xfs_inode *ip,
5531 int whichfork,
5532 xfs_fileoff_t shift, /* shift fsb */
5533 int current_ext, /* idx of gotp */
5534 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5535 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5536 struct xfs_btree_cur *cur,
5537 int *logflags) /* output */
5538{
Brian Fosterddb19e32014-09-23 15:38:09 +10005539 struct xfs_bmbt_irec got;
5540 struct xfs_bmbt_irec left;
5541 xfs_filblks_t blockcount;
5542 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005543 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005544
Brian Fosterddb19e32014-09-23 15:38:09 +10005545 xfs_bmbt_get_all(gotp, &got);
5546 xfs_bmbt_get_all(leftp, &left);
5547 blockcount = left.br_blockcount + got.br_blockcount;
5548
5549 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5550 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5551 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5552
5553 /*
5554 * Merge the in-core extents. Note that the host record pointers and
5555 * current_ext index are invalid once the extent has been removed via
5556 * xfs_iext_remove().
5557 */
5558 xfs_bmbt_set_blockcount(leftp, blockcount);
5559 xfs_iext_remove(ip, current_ext, 1, 0);
5560
5561 /*
5562 * Update the on-disk extent count, the btree if necessary and log the
5563 * inode.
5564 */
5565 XFS_IFORK_NEXT_SET(ip, whichfork,
5566 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5567 *logflags |= XFS_ILOG_CORE;
5568 if (!cur) {
5569 *logflags |= XFS_ILOG_DEXT;
5570 return 0;
5571 }
5572
5573 /* lookup and remove the extent to merge */
5574 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5575 got.br_blockcount, &i);
5576 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005577 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005578 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005579
5580 error = xfs_btree_delete(cur, &i);
5581 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005582 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005583 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005584
5585 /* lookup and update size of the previous extent */
5586 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5587 left.br_blockcount, &i);
5588 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005589 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005590 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005591
5592 left.br_blockcount = blockcount;
5593
Dave Chinner4db431f2014-12-04 09:42:40 +11005594 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5595 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005596}
5597
5598/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005599 * Shift a single extent.
5600 */
5601STATIC int
5602xfs_bmse_shift_one(
5603 struct xfs_inode *ip,
5604 int whichfork,
5605 xfs_fileoff_t offset_shift_fsb,
5606 int *current_ext,
5607 struct xfs_bmbt_rec_host *gotp,
5608 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005609 int *logflags,
5610 enum shift_direction direction)
Brian Fostera979bdf2014-09-23 15:39:04 +10005611{
5612 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005613 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005614 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005615 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005616 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005617 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005618 int error;
5619 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005620 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005621
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005622 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005623 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005624 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Brian Fostera979bdf2014-09-23 15:39:04 +10005625
5626 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005627
Brian Fosterf71721d2014-09-23 15:39:05 +10005628 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005629 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005630
Namjae Jeona904b1c2015-03-25 15:08:56 +11005631 if (direction == SHIFT_LEFT) {
5632 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005633
Namjae Jeona904b1c2015-03-25 15:08:56 +11005634 /*
5635 * Check for merge if we've got an extent to the left,
5636 * otherwise make sure there's enough room at the start
5637 * of the file for the shift.
5638 */
5639 if (!*current_ext) {
5640 if (got.br_startoff < offset_shift_fsb)
5641 return -EINVAL;
5642 goto update_current_ext;
5643 }
5644 /*
5645 * grab the left extent and check for a large
5646 * enough hole.
5647 */
5648 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5649 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5650
5651 if (startoff <
5652 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005653 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005654
Dave Chinnerb11bd672014-12-04 09:42:24 +11005655 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005656 if (xfs_bmse_can_merge(&adj_irec, &got,
5657 offset_shift_fsb)) {
Dave Chinnerb11bd672014-12-04 09:42:24 +11005658 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005659 *current_ext, gotp, adj_irecp,
5660 cur, logflags);
Dave Chinnerb11bd672014-12-04 09:42:24 +11005661 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005662 } else {
5663 startoff = got.br_startoff + offset_shift_fsb;
5664 /* nothing to move if this is the last extent */
5665 if (*current_ext >= (total_extents - 1))
5666 goto update_current_ext;
5667 /*
5668 * If this is not the last extent in the file, make sure there
5669 * is enough room between current extent and next extent for
5670 * accommodating the shift.
5671 */
5672 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5673 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5674 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5675 return -EINVAL;
5676 /*
5677 * Unlike a left shift (which involves a hole punch),
5678 * a right shift does not modify extent neighbors
5679 * in any way. We should never find mergeable extents
5680 * in this scenario. Check anyways and warn if we
5681 * encounter two extents that could be one.
5682 */
5683 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5684 WARN_ON_ONCE(1);
5685 }
Brian Fostera979bdf2014-09-23 15:39:04 +10005686 /*
5687 * Increment the extent index for the next iteration, update the start
5688 * offset of the in-core extent and update the btree if applicable.
5689 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005690update_current_ext:
5691 if (direction == SHIFT_LEFT)
5692 (*current_ext)++;
5693 else
5694 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005695 xfs_bmbt_set_startoff(gotp, startoff);
5696 *logflags |= XFS_ILOG_CORE;
5697 if (!cur) {
5698 *logflags |= XFS_ILOG_DEXT;
5699 return 0;
5700 }
5701
5702 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5703 got.br_blockcount, &i);
5704 if (error)
5705 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005706 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10005707
5708 got.br_startoff = startoff;
kbuild test robotd254aae2014-12-01 08:42:52 +11005709 return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005710 got.br_blockcount, got.br_state);
Brian Fostera979bdf2014-09-23 15:39:04 +10005711}
5712
5713/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005714 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005715 *
Brian Foster2c845f52014-09-23 15:37:09 +10005716 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005717 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10005718 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5719 * is the length by which each extent is shifted. If there is no hole to shift
5720 * the extents into, this will be considered invalid operation and we abort
5721 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005722 */
5723int
5724xfs_bmap_shift_extents(
5725 struct xfs_trans *tp,
5726 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005727 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005728 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005729 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005730 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005731 xfs_fsblock_t *firstblock,
5732 struct xfs_bmap_free *flist,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005733 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005734 int num_exts)
5735{
Brian Fosterca446d82014-09-02 12:12:53 +10005736 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005737 struct xfs_bmbt_rec_host *gotp;
5738 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005739 struct xfs_mount *mp = ip->i_mount;
5740 struct xfs_ifork *ifp;
5741 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005742 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005743 xfs_extnum_t total_extents;
5744 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005745 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005746 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10005747 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005748
5749 if (unlikely(XFS_TEST_ERROR(
5750 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5751 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5752 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5753 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5754 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005755 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005756 }
5757
5758 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005759 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005760
Brian Foster2c845f52014-09-23 15:37:09 +10005761 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5762 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005763 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
5764 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005765
5766 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005767 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5768 /* Read in all the extents */
5769 error = xfs_iread_extents(tp, ip, whichfork);
5770 if (error)
5771 return error;
5772 }
5773
Brian Fosterddb19e32014-09-23 15:38:09 +10005774 if (ifp->if_flags & XFS_IFBROOT) {
5775 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5776 cur->bc_private.b.firstblock = *firstblock;
5777 cur->bc_private.b.flist = flist;
5778 cur->bc_private.b.flags = 0;
5779 }
5780
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005781 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005782 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10005783 * are collapsing out, so we cannot use the count of real extents here.
5784 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005785 */
5786 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005787 if (total_extents == 0) {
5788 *done = 1;
5789 goto del_cursor;
5790 }
5791
5792 /*
5793 * In case of first right shift, we need to initialize next_fsb
5794 */
5795 if (*next_fsb == NULLFSBLOCK) {
5796 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
5797 xfs_bmbt_get_all(gotp, &got);
5798 *next_fsb = got.br_startoff;
5799 if (stop_fsb > *next_fsb) {
5800 *done = 1;
5801 goto del_cursor;
5802 }
5803 }
5804
5805 /* Lookup the extent index at which we have to stop */
5806 if (direction == SHIFT_RIGHT) {
5807 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
5808 /* Make stop_extent exclusive of shift range */
5809 stop_extent--;
5810 } else
5811 stop_extent = total_extents;
5812
5813 /*
5814 * Look up the extent index for the fsb where we start shifting. We can
5815 * henceforth iterate with current_ext as extent list changes are locked
5816 * out via ilock.
5817 *
5818 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5819 * *next_fsb lies in a hole beyond which there are no extents. Either
5820 * way, we are done.
5821 */
5822 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
5823 if (!gotp) {
5824 *done = 1;
5825 goto del_cursor;
5826 }
5827
5828 /* some sanity checking before we finally start shifting extents */
5829 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
5830 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
5831 error = -EIO;
5832 goto del_cursor;
5833 }
5834
5835 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005836 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005837 &current_ext, gotp, cur, &logflags,
5838 direction);
Brian Fostera979bdf2014-09-23 15:39:04 +10005839 if (error)
5840 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005841 /*
5842 * If there was an extent merge during the shift, the extent
5843 * count can change. Update the total and grade the next record.
5844 */
5845 if (direction == SHIFT_LEFT) {
5846 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5847 stop_extent = total_extents;
5848 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005849
Namjae Jeona904b1c2015-03-25 15:08:56 +11005850 if (current_ext == stop_extent) {
5851 *done = 1;
5852 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10005853 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005854 }
Brian Fosterddb19e32014-09-23 15:38:09 +10005855 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005856 }
5857
Namjae Jeona904b1c2015-03-25 15:08:56 +11005858 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005859 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10005860 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10005861 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005862
5863del_cursor:
5864 if (cur)
5865 xfs_btree_del_cursor(cur,
5866 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5867
Brian Fosterca446d82014-09-02 12:12:53 +10005868 if (logflags)
5869 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10005870
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005871 return error;
5872}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005873
5874/*
5875 * Splits an extent into two extents at split_fsb block such that it is
5876 * the first block of the current_ext. @current_ext is a target extent
5877 * to be split. @split_fsb is a block where the extents is split.
5878 * If split_fsb lies in a hole or the first block of extents, just return 0.
5879 */
5880STATIC int
5881xfs_bmap_split_extent_at(
5882 struct xfs_trans *tp,
5883 struct xfs_inode *ip,
5884 xfs_fileoff_t split_fsb,
5885 xfs_fsblock_t *firstfsb,
5886 struct xfs_bmap_free *free_list)
5887{
5888 int whichfork = XFS_DATA_FORK;
5889 struct xfs_btree_cur *cur = NULL;
5890 struct xfs_bmbt_rec_host *gotp;
5891 struct xfs_bmbt_irec got;
5892 struct xfs_bmbt_irec new; /* split extent */
5893 struct xfs_mount *mp = ip->i_mount;
5894 struct xfs_ifork *ifp;
5895 xfs_fsblock_t gotblkcnt; /* new block count for got */
5896 xfs_extnum_t current_ext;
5897 int error = 0;
5898 int logflags = 0;
5899 int i = 0;
5900
5901 if (unlikely(XFS_TEST_ERROR(
5902 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5903 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5904 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5905 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5906 XFS_ERRLEVEL_LOW, mp);
5907 return -EFSCORRUPTED;
5908 }
5909
5910 if (XFS_FORCED_SHUTDOWN(mp))
5911 return -EIO;
5912
5913 ifp = XFS_IFORK_PTR(ip, whichfork);
5914 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5915 /* Read in all the extents */
5916 error = xfs_iread_extents(tp, ip, whichfork);
5917 if (error)
5918 return error;
5919 }
5920
5921 /*
5922 * gotp can be null in 2 cases: 1) if there are no extents
5923 * or 2) split_fsb lies in a hole beyond which there are
5924 * no extents. Either way, we are done.
5925 */
5926 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
5927 if (!gotp)
5928 return 0;
5929
5930 xfs_bmbt_get_all(gotp, &got);
5931
5932 /*
5933 * Check split_fsb lies in a hole or the start boundary offset
5934 * of the extent.
5935 */
5936 if (got.br_startoff >= split_fsb)
5937 return 0;
5938
5939 gotblkcnt = split_fsb - got.br_startoff;
5940 new.br_startoff = split_fsb;
5941 new.br_startblock = got.br_startblock + gotblkcnt;
5942 new.br_blockcount = got.br_blockcount - gotblkcnt;
5943 new.br_state = got.br_state;
5944
5945 if (ifp->if_flags & XFS_IFBROOT) {
5946 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5947 cur->bc_private.b.firstblock = *firstfsb;
5948 cur->bc_private.b.flist = free_list;
5949 cur->bc_private.b.flags = 0;
5950 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5951 got.br_startblock,
5952 got.br_blockcount,
5953 &i);
5954 if (error)
5955 goto del_cursor;
5956 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5957 }
5958
5959 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
5960 got.br_blockcount = gotblkcnt;
5961
5962 logflags = XFS_ILOG_CORE;
5963 if (cur) {
5964 error = xfs_bmbt_update(cur, got.br_startoff,
5965 got.br_startblock,
5966 got.br_blockcount,
5967 got.br_state);
5968 if (error)
5969 goto del_cursor;
5970 } else
5971 logflags |= XFS_ILOG_DEXT;
5972
5973 /* Add new extent */
5974 current_ext++;
5975 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5976 XFS_IFORK_NEXT_SET(ip, whichfork,
5977 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5978
5979 if (cur) {
5980 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
5981 new.br_startblock, new.br_blockcount,
5982 &i);
5983 if (error)
5984 goto del_cursor;
5985 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
5986 cur->bc_rec.b.br_state = new.br_state;
5987
5988 error = xfs_btree_insert(cur, &i);
5989 if (error)
5990 goto del_cursor;
5991 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5992 }
5993
5994 /*
5995 * Convert to a btree if necessary.
5996 */
5997 if (xfs_bmap_needs_btree(ip, whichfork)) {
5998 int tmp_logflags; /* partial log flag return val */
5999
6000 ASSERT(cur == NULL);
6001 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, free_list,
6002 &cur, 0, &tmp_logflags, whichfork);
6003 logflags |= tmp_logflags;
6004 }
6005
6006del_cursor:
6007 if (cur) {
6008 cur->bc_private.b.allocated = 0;
6009 xfs_btree_del_cursor(cur,
6010 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6011 }
6012
6013 if (logflags)
6014 xfs_trans_log_inode(tp, ip, logflags);
6015 return error;
6016}
6017
6018int
6019xfs_bmap_split_extent(
6020 struct xfs_inode *ip,
6021 xfs_fileoff_t split_fsb)
6022{
6023 struct xfs_mount *mp = ip->i_mount;
6024 struct xfs_trans *tp;
6025 struct xfs_bmap_free free_list;
6026 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006027 int error;
6028
6029 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
6030 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
6031 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0);
6032 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +10006033 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006034 return error;
6035 }
6036
6037 xfs_ilock(ip, XFS_ILOCK_EXCL);
6038 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6039
6040 xfs_bmap_init(&free_list, &firstfsb);
6041
6042 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
6043 &firstfsb, &free_list);
6044 if (error)
6045 goto out;
6046
Eric Sandeenf6106ef2016-01-11 11:34:01 +11006047 error = xfs_bmap_finish(&tp, &free_list, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006048 if (error)
6049 goto out;
6050
Christoph Hellwig70393312015-06-04 13:48:08 +10006051 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006052
6053out:
Brian Fosterd4a97a02015-08-19 10:01:40 +10006054 xfs_bmap_cancel(&free_list);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006055 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006056 return error;
6057}