blob: 6a051662d8f9b2d0dfb184689ddc9130cd9bec74 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a9883c2013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100026#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110027#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110028#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100029#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110031#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110032#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110033#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_extfree_item.h"
35#include "xfs_alloc.h"
36#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100037#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110038#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_rtalloc.h"
40#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_quota.h"
42#include "xfs_trans_space.h"
43#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000044#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110045#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110046#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110047#include "xfs_filestream.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050kmem_zone_t *xfs_bmap_free_item_zone;
51
52/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110053 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110057 * Compute and fill in the value of the maximum depth of a bmap btree
58 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110060void
61xfs_bmap_compute_maxlevels(
62 xfs_mount_t *mp, /* file system mount structure */
63 int whichfork) /* data or attr fork */
64{
65 int level; /* btree level */
66 uint maxblocks; /* max blocks at this level */
67 uint maxleafents; /* max leaf entries possible */
68 int maxrootrecs; /* max records in root block */
69 int minleafrecs; /* min records in leaf block */
70 int minnoderecs; /* min records in node block */
71 int sz; /* root block size */
72
73 /*
74 * The maximum number of extents in a file, hence the maximum
75 * number of leaf entries, is controlled by the type of di_nextents
76 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
77 * (a signed 16-bit number, xfs_aextnum_t).
78 *
79 * Note that we can no longer assume that if we are in ATTR1 that
80 * the fork offset of all the inodes will be
81 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
82 * with ATTR2 and then mounted back with ATTR1, keeping the
83 * di_forkoff's fixed but probably at various positions. Therefore,
84 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
85 * of a minimum size available.
86 */
87 if (whichfork == XFS_DATA_FORK) {
88 maxleafents = MAXEXTNUM;
89 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
90 } else {
91 maxleafents = MAXAEXTNUM;
92 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
93 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100094 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +110095 minleafrecs = mp->m_bmap_dmnr[0];
96 minnoderecs = mp->m_bmap_dmnr[1];
97 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
98 for (level = 1; maxblocks > 1; level++) {
99 if (maxblocks <= maxrootrecs)
100 maxblocks = 1;
101 else
102 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
103 }
104 mp->m_bm_maxlevels[whichfork] = level;
105}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100107STATIC int /* error */
108xfs_bmbt_lookup_eq(
109 struct xfs_btree_cur *cur,
110 xfs_fileoff_t off,
111 xfs_fsblock_t bno,
112 xfs_filblks_t len,
113 int *stat) /* success/failure */
114{
115 cur->bc_rec.b.br_startoff = off;
116 cur->bc_rec.b.br_startblock = bno;
117 cur->bc_rec.b.br_blockcount = len;
118 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
119}
120
121STATIC int /* error */
122xfs_bmbt_lookup_ge(
123 struct xfs_btree_cur *cur,
124 xfs_fileoff_t off,
125 xfs_fsblock_t bno,
126 xfs_filblks_t len,
127 int *stat) /* success/failure */
128{
129 cur->bc_rec.b.br_startoff = off;
130 cur->bc_rec.b.br_startblock = bno;
131 cur->bc_rec.b.br_blockcount = len;
132 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
133}
134
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100135/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000136 * Check if the inode needs to be converted to btree format.
137 */
138static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
139{
140 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
141 XFS_IFORK_NEXTENTS(ip, whichfork) >
142 XFS_IFORK_MAXEXT(ip, whichfork);
143}
144
145/*
146 * Check if the inode should be converted to extent format.
147 */
148static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
149{
150 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
151 XFS_IFORK_NEXTENTS(ip, whichfork) <=
152 XFS_IFORK_MAXEXT(ip, whichfork);
153}
154
155/*
156 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100157 * by [off, bno, len, state].
158 * This either works (return 0) or gets an EFSCORRUPTED error.
159 */
160STATIC int
161xfs_bmbt_update(
162 struct xfs_btree_cur *cur,
163 xfs_fileoff_t off,
164 xfs_fsblock_t bno,
165 xfs_filblks_t len,
166 xfs_exntst_t state)
167{
168 union xfs_btree_rec rec;
169
170 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
171 return xfs_btree_update(cur, &rec);
172}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100175 * Compute the worst-case number of indirect blocks that will be used
176 * for ip's delayed extent of length "len".
177 */
178STATIC xfs_filblks_t
179xfs_bmap_worst_indlen(
180 xfs_inode_t *ip, /* incore inode pointer */
181 xfs_filblks_t len) /* delayed extent length */
182{
183 int level; /* btree level number */
184 int maxrecs; /* maximum record count at this level */
185 xfs_mount_t *mp; /* mount structure */
186 xfs_filblks_t rval; /* return value */
187
188 mp = ip->i_mount;
189 maxrecs = mp->m_bmap_dmxr[0];
190 for (level = 0, rval = 0;
191 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
192 level++) {
193 len += maxrecs - 1;
194 do_div(len, maxrecs);
195 rval += len;
196 if (len == 1)
197 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
198 level - 1;
199 if (level == 0)
200 maxrecs = mp->m_bmap_dmxr[1];
201 }
202 return rval;
203}
204
205/*
206 * Calculate the default attribute fork offset for newly created inodes.
207 */
208uint
209xfs_default_attroffset(
210 struct xfs_inode *ip)
211{
212 struct xfs_mount *mp = ip->i_mount;
213 uint offset;
214
215 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100216 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100217 XFS_BMDR_SPACE_CALC(MINABTPTRS);
218 } else {
219 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
220 }
221
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100222 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100223 return offset;
224}
225
226/*
227 * Helper routine to reset inode di_forkoff field when switching
228 * attribute fork from local to extent format - we reset it where
229 * possible to make space available for inline data fork extents.
230 */
231STATIC void
232xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100233 xfs_inode_t *ip,
234 int whichfork)
235{
236 if (whichfork == XFS_ATTR_FORK &&
237 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
238 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
239 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
240 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
241
242 if (dfl_forkoff > ip->i_d.di_forkoff)
243 ip->i_d.di_forkoff = dfl_forkoff;
244 }
245}
246
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 }
480 if (bp_release) {
481 bp_release = 0;
482 xfs_trans_brelse(NULL, bp);
483 }
484 return;
485
486error0:
487 xfs_warn(mp, "%s: at error0", __func__);
488 if (bp_release)
489 xfs_trans_brelse(NULL, bp);
490error_norelse:
491 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
492 __func__, i);
493 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
494 return;
495}
496
497/*
498 * Add bmap trace insert entries for all the contents of the extent records.
499 */
500void
501xfs_bmap_trace_exlist(
502 xfs_inode_t *ip, /* incore inode pointer */
503 xfs_extnum_t cnt, /* count of entries in the list */
504 int whichfork, /* data or attr fork */
505 unsigned long caller_ip)
506{
507 xfs_extnum_t idx; /* extent record index */
508 xfs_ifork_t *ifp; /* inode fork pointer */
509 int state = 0;
510
511 if (whichfork == XFS_ATTR_FORK)
512 state |= BMAP_ATTRFORK;
513
514 ifp = XFS_IFORK_PTR(ip, whichfork);
515 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
516 for (idx = 0; idx < cnt; idx++)
517 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
518}
519
520/*
521 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000522 * given the caller's original parameters. Specifically check the
523 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100524 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
525 */
526STATIC void
527xfs_bmap_validate_ret(
528 xfs_fileoff_t bno,
529 xfs_filblks_t len,
530 int flags,
531 xfs_bmbt_irec_t *mval,
532 int nmap,
533 int ret_nmap)
534{
535 int i; /* index to map values */
536
537 ASSERT(ret_nmap <= nmap);
538
539 for (i = 0; i < ret_nmap; i++) {
540 ASSERT(mval[i].br_blockcount > 0);
541 if (!(flags & XFS_BMAPI_ENTIRE)) {
542 ASSERT(mval[i].br_startoff >= bno);
543 ASSERT(mval[i].br_blockcount <= len);
544 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
545 bno + len);
546 } else {
547 ASSERT(mval[i].br_startoff < bno + len);
548 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
549 bno);
550 }
551 ASSERT(i == 0 ||
552 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
553 mval[i].br_startoff);
554 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
555 mval[i].br_startblock != HOLESTARTBLOCK);
556 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
557 mval[i].br_state == XFS_EXT_UNWRITTEN);
558 }
559}
560
561#else
562#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
563#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
564#endif /* DEBUG */
565
566/*
567 * bmap free list manipulation functions
568 */
569
570/*
571 * Add the extent to the list of extents to be free at transaction end.
572 * The list is maintained sorted (by block number).
573 */
574void
575xfs_bmap_add_free(
576 xfs_fsblock_t bno, /* fs block number of extent */
577 xfs_filblks_t len, /* length of extent */
578 xfs_bmap_free_t *flist, /* list of extents */
579 xfs_mount_t *mp) /* mount point structure */
580{
581 xfs_bmap_free_item_t *cur; /* current (next) element */
582 xfs_bmap_free_item_t *new; /* new element */
583 xfs_bmap_free_item_t *prev; /* previous element */
584#ifdef DEBUG
585 xfs_agnumber_t agno;
586 xfs_agblock_t agbno;
587
588 ASSERT(bno != NULLFSBLOCK);
589 ASSERT(len > 0);
590 ASSERT(len <= MAXEXTLEN);
591 ASSERT(!isnullstartblock(bno));
592 agno = XFS_FSB_TO_AGNO(mp, bno);
593 agbno = XFS_FSB_TO_AGBNO(mp, bno);
594 ASSERT(agno < mp->m_sb.sb_agcount);
595 ASSERT(agbno < mp->m_sb.sb_agblocks);
596 ASSERT(len < mp->m_sb.sb_agblocks);
597 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
598#endif
599 ASSERT(xfs_bmap_free_item_zone != NULL);
600 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
601 new->xbfi_startblock = bno;
602 new->xbfi_blockcount = (xfs_extlen_t)len;
603 for (prev = NULL, cur = flist->xbf_first;
604 cur != NULL;
605 prev = cur, cur = cur->xbfi_next) {
606 if (cur->xbfi_startblock >= bno)
607 break;
608 }
609 if (prev)
610 prev->xbfi_next = new;
611 else
612 flist->xbf_first = new;
613 new->xbfi_next = cur;
614 flist->xbf_count++;
615}
616
617/*
618 * Remove the entry "free" from the free item list. Prev points to the
619 * previous entry, unless "free" is the head of the list.
620 */
Dave Chinner68988112013-08-12 20:49:42 +1000621void
Dave Chinner9e5987a2013-02-25 12:31:26 +1100622xfs_bmap_del_free(
623 xfs_bmap_free_t *flist, /* free item list header */
624 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
625 xfs_bmap_free_item_t *free) /* list item to be freed */
626{
627 if (prev)
628 prev->xbfi_next = free->xbfi_next;
629 else
630 flist->xbf_first = free->xbfi_next;
631 flist->xbf_count--;
632 kmem_zone_free(xfs_bmap_free_item_zone, free);
633}
634
Dave Chinner9e5987a2013-02-25 12:31:26 +1100635/*
636 * Free up any items left in the list.
637 */
638void
639xfs_bmap_cancel(
640 xfs_bmap_free_t *flist) /* list of bmap_free_items */
641{
642 xfs_bmap_free_item_t *free; /* free list item */
643 xfs_bmap_free_item_t *next;
644
645 if (flist->xbf_count == 0)
646 return;
647 ASSERT(flist->xbf_first != NULL);
648 for (free = flist->xbf_first; free; free = next) {
649 next = free->xbfi_next;
650 xfs_bmap_del_free(flist, NULL, free);
651 }
652 ASSERT(flist->xbf_count == 0);
653}
654
655/*
656 * Inode fork format manipulation functions
657 */
658
659/*
660 * Transform a btree format file with only one leaf node, where the
661 * extents list will fit in the inode, into an extents format file.
662 * Since the file extents are already in-core, all we have to do is
663 * give up the space for the btree root and pitch the leaf block.
664 */
665STATIC int /* error */
666xfs_bmap_btree_to_extents(
667 xfs_trans_t *tp, /* transaction pointer */
668 xfs_inode_t *ip, /* incore inode pointer */
669 xfs_btree_cur_t *cur, /* btree cursor */
670 int *logflagsp, /* inode logging flags */
671 int whichfork) /* data or attr fork */
672{
673 /* REFERENCED */
674 struct xfs_btree_block *cblock;/* child btree block */
675 xfs_fsblock_t cbno; /* child block number */
676 xfs_buf_t *cbp; /* child block's buffer */
677 int error; /* error return value */
678 xfs_ifork_t *ifp; /* inode fork data */
679 xfs_mount_t *mp; /* mount point structure */
680 __be64 *pp; /* ptr to block address */
681 struct xfs_btree_block *rblock;/* root btree block */
682
683 mp = ip->i_mount;
684 ifp = XFS_IFORK_PTR(ip, whichfork);
685 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
686 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
687 rblock = ifp->if_broot;
688 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
689 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
690 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
691 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
692 cbno = be64_to_cpu(*pp);
693 *logflagsp = 0;
694#ifdef DEBUG
695 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
696 return error;
697#endif
698 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
699 &xfs_bmbt_buf_ops);
700 if (error)
701 return error;
702 cblock = XFS_BUF_TO_BLOCK(cbp);
703 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
704 return error;
705 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
706 ip->i_d.di_nblocks--;
707 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
708 xfs_trans_binval(tp, cbp);
709 if (cur->bc_bufs[0] == cbp)
710 cur->bc_bufs[0] = NULL;
711 xfs_iroot_realloc(ip, -1, whichfork);
712 ASSERT(ifp->if_broot == NULL);
713 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
714 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
715 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
716 return 0;
717}
718
719/*
720 * Convert an extents-format file into a btree-format file.
721 * The new file will have a root block (in the inode) and a single child block.
722 */
723STATIC int /* error */
724xfs_bmap_extents_to_btree(
725 xfs_trans_t *tp, /* transaction pointer */
726 xfs_inode_t *ip, /* incore inode pointer */
727 xfs_fsblock_t *firstblock, /* first-block-allocated */
728 xfs_bmap_free_t *flist, /* blocks freed in xaction */
729 xfs_btree_cur_t **curp, /* cursor returned to caller */
730 int wasdel, /* converting a delayed alloc */
731 int *logflagsp, /* inode logging flags */
732 int whichfork) /* data or attr fork */
733{
734 struct xfs_btree_block *ablock; /* allocated (child) bt block */
735 xfs_buf_t *abp; /* buffer for ablock */
736 xfs_alloc_arg_t args; /* allocation arguments */
737 xfs_bmbt_rec_t *arp; /* child record pointer */
738 struct xfs_btree_block *block; /* btree root block */
739 xfs_btree_cur_t *cur; /* bmap btree cursor */
740 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
741 int error; /* error return value */
742 xfs_extnum_t i, cnt; /* extent record index */
743 xfs_ifork_t *ifp; /* inode fork pointer */
744 xfs_bmbt_key_t *kp; /* root block key pointer */
745 xfs_mount_t *mp; /* mount structure */
746 xfs_extnum_t nextents; /* number of file extents */
747 xfs_bmbt_ptr_t *pp; /* root block address pointer */
748
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500749 mp = ip->i_mount;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100750 ifp = XFS_IFORK_PTR(ip, whichfork);
751 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
752
753 /*
754 * Make space in the inode incore.
755 */
756 xfs_iroot_realloc(ip, 1, whichfork);
757 ifp->if_flags |= XFS_IFBROOT;
758
759 /*
760 * Fill in the root.
761 */
762 block = ifp->if_broot;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500763 if (xfs_sb_version_hascrc(&mp->m_sb))
764 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
765 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
766 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
767 else
768 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
769 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
770 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100771
772 /*
773 * Need a cursor. Can't allocate until bb_level is filled in.
774 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100775 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
776 cur->bc_private.b.firstblock = *firstblock;
777 cur->bc_private.b.flist = flist;
778 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
779 /*
780 * Convert to a btree with two levels, one record in root.
781 */
782 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
783 memset(&args, 0, sizeof(args));
784 args.tp = tp;
785 args.mp = mp;
786 args.firstblock = *firstblock;
787 if (*firstblock == NULLFSBLOCK) {
788 args.type = XFS_ALLOCTYPE_START_BNO;
789 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
790 } else if (flist->xbf_low) {
791 args.type = XFS_ALLOCTYPE_START_BNO;
792 args.fsbno = *firstblock;
793 } else {
794 args.type = XFS_ALLOCTYPE_NEAR_BNO;
795 args.fsbno = *firstblock;
796 }
797 args.minlen = args.maxlen = args.prod = 1;
798 args.wasdel = wasdel;
799 *logflagsp = 0;
800 if ((error = xfs_alloc_vextent(&args))) {
801 xfs_iroot_realloc(ip, -1, whichfork);
802 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
803 return error;
804 }
805 /*
806 * Allocation can't fail, the space was reserved.
807 */
808 ASSERT(args.fsbno != NULLFSBLOCK);
809 ASSERT(*firstblock == NULLFSBLOCK ||
810 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
811 (flist->xbf_low &&
812 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
813 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
814 cur->bc_private.b.allocated++;
815 ip->i_d.di_nblocks++;
816 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
817 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
818 /*
819 * Fill in the child block.
820 */
821 abp->b_ops = &xfs_bmbt_buf_ops;
822 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500823 if (xfs_sb_version_hascrc(&mp->m_sb))
824 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
825 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
826 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
827 else
828 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
829 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
830 XFS_BTREE_LONG_PTRS);
831
Dave Chinner9e5987a2013-02-25 12:31:26 +1100832 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
833 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
834 for (cnt = i = 0; i < nextents; i++) {
835 ep = xfs_iext_get_ext(ifp, i);
836 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
837 arp->l0 = cpu_to_be64(ep->l0);
838 arp->l1 = cpu_to_be64(ep->l1);
839 arp++; cnt++;
840 }
841 }
842 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
843 xfs_btree_set_numrecs(ablock, cnt);
844
845 /*
846 * Fill in the root key and pointer.
847 */
848 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
849 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
850 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
851 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
852 be16_to_cpu(block->bb_level)));
853 *pp = cpu_to_be64(args.fsbno);
854
855 /*
856 * Do all this logging at the end so that
857 * the root is at the right level.
858 */
859 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
860 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
861 ASSERT(*curp == NULL);
862 *curp = cur;
863 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
864 return 0;
865}
866
867/*
868 * Convert a local file to an extents file.
869 * This code is out of bounds for data forks of regular files,
870 * since the file data needs to get logged so things will stay consistent.
871 * (The bmap-level manipulations are ok, though).
872 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000873void
874xfs_bmap_local_to_extents_empty(
875 struct xfs_inode *ip,
876 int whichfork)
877{
878 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
879
880 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
881 ASSERT(ifp->if_bytes == 0);
882 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
883
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000884 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000885 ifp->if_flags &= ~XFS_IFINLINE;
886 ifp->if_flags |= XFS_IFEXTENTS;
887 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
888}
889
890
Dave Chinner9e5987a2013-02-25 12:31:26 +1100891STATIC int /* error */
892xfs_bmap_local_to_extents(
893 xfs_trans_t *tp, /* transaction pointer */
894 xfs_inode_t *ip, /* incore inode pointer */
895 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
896 xfs_extlen_t total, /* total blocks needed by transaction */
897 int *logflagsp, /* inode logging flags */
898 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500899 void (*init_fn)(struct xfs_trans *tp,
900 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100901 struct xfs_inode *ip,
902 struct xfs_ifork *ifp))
903{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000904 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100905 int flags; /* logging flags returned */
906 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000907 xfs_alloc_arg_t args; /* allocation arguments */
908 xfs_buf_t *bp; /* buffer for extent block */
909 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100910
911 /*
912 * We don't want to deal with the case of keeping inode data inline yet.
913 * So sending the data fork of a regular inode is invalid.
914 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100915 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100916 ifp = XFS_IFORK_PTR(ip, whichfork);
917 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000918
919 if (!ifp->if_bytes) {
920 xfs_bmap_local_to_extents_empty(ip, whichfork);
921 flags = XFS_ILOG_CORE;
922 goto done;
923 }
924
Dave Chinner9e5987a2013-02-25 12:31:26 +1100925 flags = 0;
926 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000927 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
928 XFS_IFINLINE);
929 memset(&args, 0, sizeof(args));
930 args.tp = tp;
931 args.mp = ip->i_mount;
932 args.firstblock = *firstblock;
933 /*
934 * Allocate a block. We know we need only one, since the
935 * file currently fits in an inode.
936 */
937 if (*firstblock == NULLFSBLOCK) {
938 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
939 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100940 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000941 args.fsbno = *firstblock;
942 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100943 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000944 args.total = total;
945 args.minlen = args.maxlen = args.prod = 1;
946 error = xfs_alloc_vextent(&args);
947 if (error)
948 goto done;
949
950 /* Can't fail, the space was reserved. */
951 ASSERT(args.fsbno != NULLFSBLOCK);
952 ASSERT(args.len == 1);
953 *firstblock = args.fsbno;
954 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
955
Dave Chinnerfe22d552015-01-22 09:30:06 +1100956 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100957 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100958 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100959 * The callout is responsible for logging because the remote format
960 * might differ from the local format and thus we don't know how much to
961 * log here. Note that init_fn must also set the buffer log item type
962 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100963 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000964 init_fn(tp, bp, ip, ifp);
965
Brian Fosterb7cdc662015-10-12 15:40:24 +1100966 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000967 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
968 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100969 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000970
971 xfs_iext_add(ifp, 0, 1);
972 ep = xfs_iext_get_ext(ifp, 0);
973 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
974 trace_xfs_bmap_post_update(ip, 0,
975 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
976 _THIS_IP_);
977 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
978 ip->i_d.di_nblocks = 1;
979 xfs_trans_mod_dquot_byino(tp, ip,
980 XFS_TRANS_DQ_BCOUNT, 1L);
981 flags |= xfs_ilog_fext(whichfork);
982
Dave Chinner9e5987a2013-02-25 12:31:26 +1100983done:
984 *logflagsp = flags;
985 return error;
986}
987
988/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 * Called from xfs_bmap_add_attrfork to handle btree format files.
990 */
991STATIC int /* error */
992xfs_bmap_add_attrfork_btree(
993 xfs_trans_t *tp, /* transaction pointer */
994 xfs_inode_t *ip, /* incore inode pointer */
995 xfs_fsblock_t *firstblock, /* first block allocated */
996 xfs_bmap_free_t *flist, /* blocks to free at commit */
997 int *flags) /* inode logging flags */
998{
999 xfs_btree_cur_t *cur; /* btree cursor */
1000 int error; /* error return value */
1001 xfs_mount_t *mp; /* file system mount struct */
1002 int stat; /* newroot status */
1003
1004 mp = ip->i_mount;
1005 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
1006 *flags |= XFS_ILOG_DBROOT;
1007 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001008 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 cur->bc_private.b.flist = flist;
1010 cur->bc_private.b.firstblock = *firstblock;
1011 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1012 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001013 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001014 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001015 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 goto error0;
1017 if (stat == 0) {
1018 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +10001019 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
1021 *firstblock = cur->bc_private.b.firstblock;
1022 cur->bc_private.b.allocated = 0;
1023 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1024 }
1025 return 0;
1026error0:
1027 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1028 return error;
1029}
1030
1031/*
1032 * Called from xfs_bmap_add_attrfork to handle extents format files.
1033 */
1034STATIC int /* error */
1035xfs_bmap_add_attrfork_extents(
1036 xfs_trans_t *tp, /* transaction pointer */
1037 xfs_inode_t *ip, /* incore inode pointer */
1038 xfs_fsblock_t *firstblock, /* first block allocated */
1039 xfs_bmap_free_t *flist, /* blocks to free at commit */
1040 int *flags) /* inode logging flags */
1041{
1042 xfs_btree_cur_t *cur; /* bmap btree cursor */
1043 int error; /* error return value */
1044
1045 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1046 return 0;
1047 cur = NULL;
1048 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
1049 flags, XFS_DATA_FORK);
1050 if (cur) {
1051 cur->bc_private.b.allocated = 0;
1052 xfs_btree_del_cursor(cur,
1053 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1054 }
1055 return error;
1056}
1057
1058/*
Dave Chinner1e823792013-02-11 15:58:13 +11001059 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1060 * different data fork content type needs a different callout to do the
1061 * conversion. Some are basic and only require special block initialisation
1062 * callouts for the data formating, others (directories) are so specialised they
1063 * handle everything themselves.
1064 *
1065 * XXX (dgc): investigate whether directory conversion can use the generic
1066 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001067 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 */
1069STATIC int /* error */
1070xfs_bmap_add_attrfork_local(
1071 xfs_trans_t *tp, /* transaction pointer */
1072 xfs_inode_t *ip, /* incore inode pointer */
1073 xfs_fsblock_t *firstblock, /* first block allocated */
1074 xfs_bmap_free_t *flist, /* blocks to free at commit */
1075 int *flags) /* inode logging flags */
1076{
1077 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1080 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001081
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001082 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001084 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 dargs.dp = ip;
1086 dargs.firstblock = firstblock;
1087 dargs.flist = flist;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001088 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 dargs.whichfork = XFS_DATA_FORK;
1090 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001091 return xfs_dir2_sf_to_block(&dargs);
1092 }
1093
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001094 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001095 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1096 flags, XFS_DATA_FORK,
1097 xfs_symlink_local_to_remote);
1098
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001099 /* should only be called for types that support local format data */
1100 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001101 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
1104/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001105 * Convert inode from non-attributed to attributed.
1106 * Must not be in a transaction, ip must not be locked.
1107 */
1108int /* error code */
1109xfs_bmap_add_attrfork(
1110 xfs_inode_t *ip, /* incore inode pointer */
1111 int size, /* space new attribute needs */
1112 int rsvd) /* xact may use reserved blks */
1113{
1114 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1115 xfs_bmap_free_t flist; /* freed extent records */
1116 xfs_mount_t *mp; /* mount structure */
1117 xfs_trans_t *tp; /* transaction pointer */
1118 int blks; /* space reservation */
1119 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001120 int logflags; /* logging flags */
1121 int error; /* error return value */
1122
1123 ASSERT(XFS_IFORK_Q(ip) == 0);
1124
1125 mp = ip->i_mount;
1126 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1127 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
1128 blks = XFS_ADDAFORK_SPACE_RES(mp);
1129 if (rsvd)
1130 tp->t_flags |= XFS_TRANS_RESERVE;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001131 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_addafork, blks, 0);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001132 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +10001133 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001134 return error;
1135 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001136 xfs_ilock(ip, XFS_ILOCK_EXCL);
1137 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1138 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1139 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001140 if (error)
1141 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001142 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001143 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001144 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1145 /*
1146 * For inodes coming from pre-6.2 filesystems.
1147 */
1148 ASSERT(ip->i_d.di_aformat == 0);
1149 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1150 }
1151 ASSERT(ip->i_d.di_anextents == 0);
1152
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001153 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001154 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1155
1156 switch (ip->i_d.di_format) {
1157 case XFS_DINODE_FMT_DEV:
1158 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1159 break;
1160 case XFS_DINODE_FMT_UUID:
1161 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1162 break;
1163 case XFS_DINODE_FMT_LOCAL:
1164 case XFS_DINODE_FMT_EXTENTS:
1165 case XFS_DINODE_FMT_BTREE:
1166 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1167 if (!ip->i_d.di_forkoff)
1168 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1169 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1170 version = 2;
1171 break;
1172 default:
1173 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001174 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001175 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001176 }
1177
1178 ASSERT(ip->i_afp == NULL);
1179 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1180 ip->i_afp->if_flags = XFS_IFEXTENTS;
1181 logflags = 0;
1182 xfs_bmap_init(&flist, &firstblock);
1183 switch (ip->i_d.di_format) {
1184 case XFS_DINODE_FMT_LOCAL:
1185 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
1186 &logflags);
1187 break;
1188 case XFS_DINODE_FMT_EXTENTS:
1189 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1190 &flist, &logflags);
1191 break;
1192 case XFS_DINODE_FMT_BTREE:
1193 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
1194 &logflags);
1195 break;
1196 default:
1197 error = 0;
1198 break;
1199 }
1200 if (logflags)
1201 xfs_trans_log_inode(tp, ip, logflags);
1202 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001203 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001204 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1205 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001206 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001207
1208 spin_lock(&mp->m_sb_lock);
1209 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1210 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001211 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001212 }
1213 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1214 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001215 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001216 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001217 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001218 if (log_sb)
1219 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001220 }
1221
Eric Sandeenf6106ef2016-01-11 11:34:01 +11001222 error = xfs_bmap_finish(&tp, &flist, NULL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001223 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001224 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001225 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001226 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001227 return error;
1228
1229bmap_cancel:
1230 xfs_bmap_cancel(&flist);
1231trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001232 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001233 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001234 return error;
1235}
1236
1237/*
1238 * Internal and external extent tree search functions.
1239 */
1240
1241/*
1242 * Read in the extents to if_extents.
1243 * All inode fields are set up by caller, we just traverse the btree
1244 * and copy the records in. If the file system cannot contain unwritten
1245 * extents, the records are checked for no "state" flags.
1246 */
1247int /* error */
1248xfs_bmap_read_extents(
1249 xfs_trans_t *tp, /* transaction pointer */
1250 xfs_inode_t *ip, /* incore inode */
1251 int whichfork) /* data or attr fork */
1252{
1253 struct xfs_btree_block *block; /* current btree block */
1254 xfs_fsblock_t bno; /* block # of "block" */
1255 xfs_buf_t *bp; /* buffer for "block" */
1256 int error; /* error return value */
1257 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1258 xfs_extnum_t i, j; /* index into the extents list */
1259 xfs_ifork_t *ifp; /* fork structure */
1260 int level; /* btree level, for checking */
1261 xfs_mount_t *mp; /* file system mount structure */
1262 __be64 *pp; /* pointer to block address */
1263 /* REFERENCED */
1264 xfs_extnum_t room; /* number of entries there's room for */
1265
1266 bno = NULLFSBLOCK;
1267 mp = ip->i_mount;
1268 ifp = XFS_IFORK_PTR(ip, whichfork);
1269 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1270 XFS_EXTFMT_INODE(ip);
1271 block = ifp->if_broot;
1272 /*
1273 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1274 */
1275 level = be16_to_cpu(block->bb_level);
1276 ASSERT(level > 0);
1277 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1278 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001279 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001280 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1281 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1282 /*
1283 * Go down the tree until leaf level is reached, following the first
1284 * pointer (leftmost) at each level.
1285 */
1286 while (level-- > 0) {
1287 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1288 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1289 if (error)
1290 return error;
1291 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001292 if (level == 0)
1293 break;
1294 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1295 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001296 XFS_WANT_CORRUPTED_GOTO(mp,
1297 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001298 xfs_trans_brelse(tp, bp);
1299 }
1300 /*
1301 * Here with bp and block set to the leftmost leaf node in the tree.
1302 */
1303 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1304 i = 0;
1305 /*
1306 * Loop over all leaf nodes. Copy information to the extent records.
1307 */
1308 for (;;) {
1309 xfs_bmbt_rec_t *frp;
1310 xfs_fsblock_t nextbno;
1311 xfs_extnum_t num_recs;
1312 xfs_extnum_t start;
1313
1314 num_recs = xfs_btree_get_numrecs(block);
1315 if (unlikely(i + num_recs > room)) {
1316 ASSERT(i + num_recs <= room);
1317 xfs_warn(ip->i_mount,
1318 "corrupt dinode %Lu, (btree extents).",
1319 (unsigned long long) ip->i_ino);
1320 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1321 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1322 goto error0;
1323 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001324 /*
1325 * Read-ahead the next leaf block, if any.
1326 */
1327 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1328 if (nextbno != NULLFSBLOCK)
1329 xfs_btree_reada_bufl(mp, nextbno, 1,
1330 &xfs_bmbt_buf_ops);
1331 /*
1332 * Copy records into the extent records.
1333 */
1334 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1335 start = i;
1336 for (j = 0; j < num_recs; j++, i++, frp++) {
1337 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1338 trp->l0 = be64_to_cpu(frp->l0);
1339 trp->l1 = be64_to_cpu(frp->l1);
1340 }
1341 if (exntf == XFS_EXTFMT_NOSTATE) {
1342 /*
1343 * Check all attribute bmap btree records and
1344 * any "older" data bmap btree records for a
1345 * set bit in the "extent flag" position.
1346 */
1347 if (unlikely(xfs_check_nostate_extents(ifp,
1348 start, num_recs))) {
1349 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1350 XFS_ERRLEVEL_LOW,
1351 ip->i_mount);
1352 goto error0;
1353 }
1354 }
1355 xfs_trans_brelse(tp, bp);
1356 bno = nextbno;
1357 /*
1358 * If we've reached the end, stop.
1359 */
1360 if (bno == NULLFSBLOCK)
1361 break;
1362 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1363 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1364 if (error)
1365 return error;
1366 block = XFS_BUF_TO_BLOCK(bp);
1367 }
1368 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1369 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1370 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1371 return 0;
1372error0:
1373 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001374 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001375}
1376
1377
1378/*
1379 * Search the extent records for the entry containing block bno.
1380 * If bno lies in a hole, point to the next entry. If bno lies
1381 * past eof, *eofp will be set, and *prevp will contain the last
1382 * entry (null if none). Else, *lastxp will be set to the index
1383 * of the found entry; *gotp will contain the entry.
1384 */
1385STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1386xfs_bmap_search_multi_extents(
1387 xfs_ifork_t *ifp, /* inode fork pointer */
1388 xfs_fileoff_t bno, /* block number searched for */
1389 int *eofp, /* out: end of file found */
1390 xfs_extnum_t *lastxp, /* out: last extent index */
1391 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1392 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1393{
1394 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1395 xfs_extnum_t lastx; /* last extent index */
1396
1397 /*
1398 * Initialize the extent entry structure to catch access to
1399 * uninitialized br_startblock field.
1400 */
1401 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1402 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1403 gotp->br_state = XFS_EXT_INVALID;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001404 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001405 prevp->br_startoff = NULLFILEOFF;
1406
1407 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1408 if (lastx > 0) {
1409 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1410 }
1411 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1412 xfs_bmbt_get_all(ep, gotp);
1413 *eofp = 0;
1414 } else {
1415 if (lastx > 0) {
1416 *gotp = *prevp;
1417 }
1418 *eofp = 1;
1419 ep = NULL;
1420 }
1421 *lastxp = lastx;
1422 return ep;
1423}
1424
1425/*
1426 * Search the extents list for the inode, for the extent containing bno.
1427 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1428 * *eofp will be set, and *prevp will contain the last entry (null if none).
1429 * Else, *lastxp will be set to the index of the found
1430 * entry; *gotp will contain the entry.
1431 */
1432STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1433xfs_bmap_search_extents(
1434 xfs_inode_t *ip, /* incore inode pointer */
1435 xfs_fileoff_t bno, /* block number searched for */
1436 int fork, /* data or attr fork */
1437 int *eofp, /* out: end of file found */
1438 xfs_extnum_t *lastxp, /* out: last extent index */
1439 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1440 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1441{
1442 xfs_ifork_t *ifp; /* inode fork pointer */
1443 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1444
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001445 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001446 ifp = XFS_IFORK_PTR(ip, fork);
1447
1448 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1449
1450 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1451 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1452 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1453 "Access to block zero in inode %llu "
1454 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -05001455 "blkcnt: %llx extent-state: %x lastx: %x",
Dave Chinner9e5987a2013-02-25 12:31:26 +11001456 (unsigned long long)ip->i_ino,
1457 (unsigned long long)gotp->br_startblock,
1458 (unsigned long long)gotp->br_startoff,
1459 (unsigned long long)gotp->br_blockcount,
1460 gotp->br_state, *lastxp);
1461 *lastxp = NULLEXTNUM;
1462 *eofp = 1;
1463 return NULL;
1464 }
1465 return ep;
1466}
1467
1468/*
1469 * Returns the file-relative block number of the first unused block(s)
1470 * in the file with at least "len" logically contiguous blocks free.
1471 * This is the lowest-address hole if the file has holes, else the first block
1472 * past the end of file.
1473 * Return 0 if the file is currently local (in-inode).
1474 */
1475int /* error */
1476xfs_bmap_first_unused(
1477 xfs_trans_t *tp, /* transaction pointer */
1478 xfs_inode_t *ip, /* incore inode */
1479 xfs_extlen_t len, /* size of hole to find */
1480 xfs_fileoff_t *first_unused, /* unused block */
1481 int whichfork) /* data or attr fork */
1482{
1483 int error; /* error return value */
1484 int idx; /* extent record index */
1485 xfs_ifork_t *ifp; /* inode fork pointer */
1486 xfs_fileoff_t lastaddr; /* last block number seen */
1487 xfs_fileoff_t lowest; /* lowest useful block */
1488 xfs_fileoff_t max; /* starting useful block */
1489 xfs_fileoff_t off; /* offset for this block */
1490 xfs_extnum_t nextents; /* number of extent entries */
1491
1492 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1493 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1494 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1495 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1496 *first_unused = 0;
1497 return 0;
1498 }
1499 ifp = XFS_IFORK_PTR(ip, whichfork);
1500 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1501 (error = xfs_iread_extents(tp, ip, whichfork)))
1502 return error;
1503 lowest = *first_unused;
1504 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1505 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1506 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1507 off = xfs_bmbt_get_startoff(ep);
1508 /*
1509 * See if the hole before this extent will work.
1510 */
1511 if (off >= lowest + len && off - max >= len) {
1512 *first_unused = max;
1513 return 0;
1514 }
1515 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1516 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1517 }
1518 *first_unused = max;
1519 return 0;
1520}
1521
1522/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001523 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001524 * last_block (input value) in the file.
1525 * This is not based on i_size, it is based on the extent records.
1526 * Returns 0 for local files, as they do not have extent records.
1527 */
1528int /* error */
1529xfs_bmap_last_before(
1530 xfs_trans_t *tp, /* transaction pointer */
1531 xfs_inode_t *ip, /* incore inode */
1532 xfs_fileoff_t *last_block, /* last block */
1533 int whichfork) /* data or attr fork */
1534{
1535 xfs_fileoff_t bno; /* input file offset */
1536 int eof; /* hit end of file */
1537 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1538 int error; /* error return value */
1539 xfs_bmbt_irec_t got; /* current extent value */
1540 xfs_ifork_t *ifp; /* inode fork pointer */
1541 xfs_extnum_t lastx; /* last extent used */
1542 xfs_bmbt_irec_t prev; /* previous extent value */
1543
1544 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1545 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1546 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +10001547 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001548 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1549 *last_block = 0;
1550 return 0;
1551 }
1552 ifp = XFS_IFORK_PTR(ip, whichfork);
1553 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1554 (error = xfs_iread_extents(tp, ip, whichfork)))
1555 return error;
1556 bno = *last_block - 1;
1557 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1558 &prev);
1559 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1560 if (prev.br_startoff == NULLFILEOFF)
1561 *last_block = 0;
1562 else
1563 *last_block = prev.br_startoff + prev.br_blockcount;
1564 }
1565 /*
1566 * Otherwise *last_block is already the right answer.
1567 */
1568 return 0;
1569}
1570
Dave Chinner68988112013-08-12 20:49:42 +10001571int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001572xfs_bmap_last_extent(
1573 struct xfs_trans *tp,
1574 struct xfs_inode *ip,
1575 int whichfork,
1576 struct xfs_bmbt_irec *rec,
1577 int *is_empty)
1578{
1579 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1580 int error;
1581 int nextents;
1582
1583 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1584 error = xfs_iread_extents(tp, ip, whichfork);
1585 if (error)
1586 return error;
1587 }
1588
1589 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1590 if (nextents == 0) {
1591 *is_empty = 1;
1592 return 0;
1593 }
1594
1595 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1596 *is_empty = 0;
1597 return 0;
1598}
1599
1600/*
1601 * Check the last inode extent to determine whether this allocation will result
1602 * in blocks being allocated at the end of the file. When we allocate new data
1603 * blocks at the end of the file which do not start at the previous data block,
1604 * we will try to align the new blocks at stripe unit boundaries.
1605 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001606 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001607 * at, or past the EOF.
1608 */
1609STATIC int
1610xfs_bmap_isaeof(
1611 struct xfs_bmalloca *bma,
1612 int whichfork)
1613{
1614 struct xfs_bmbt_irec rec;
1615 int is_empty;
1616 int error;
1617
1618 bma->aeof = 0;
1619 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1620 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001621 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001622 return error;
1623
Dave Chinner6e708bc2013-11-22 10:41:16 +11001624 if (is_empty) {
1625 bma->aeof = 1;
1626 return 0;
1627 }
1628
Dave Chinner9e5987a2013-02-25 12:31:26 +11001629 /*
1630 * Check if we are allocation or past the last extent, or at least into
1631 * the last delayed allocated extent.
1632 */
1633 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1634 (bma->offset >= rec.br_startoff &&
1635 isnullstartblock(rec.br_startblock));
1636 return 0;
1637}
1638
1639/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001640 * Returns the file-relative block number of the first block past eof in
1641 * the file. This is not based on i_size, it is based on the extent records.
1642 * Returns 0 for local files, as they do not have extent records.
1643 */
1644int
1645xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001646 struct xfs_inode *ip,
1647 xfs_fileoff_t *last_block,
1648 int whichfork)
1649{
1650 struct xfs_bmbt_irec rec;
1651 int is_empty;
1652 int error;
1653
1654 *last_block = 0;
1655
1656 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1657 return 0;
1658
1659 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1660 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001661 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001662
1663 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1664 if (error || is_empty)
1665 return error;
1666
1667 *last_block = rec.br_startoff + rec.br_blockcount;
1668 return 0;
1669}
1670
1671/*
1672 * Returns whether the selected fork of the inode has exactly one
1673 * block or not. For the data fork we check this matches di_size,
1674 * implying the file's range is 0..bsize-1.
1675 */
1676int /* 1=>1 block, 0=>otherwise */
1677xfs_bmap_one_block(
1678 xfs_inode_t *ip, /* incore inode */
1679 int whichfork) /* data or attr fork */
1680{
1681 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1682 xfs_ifork_t *ifp; /* inode fork pointer */
1683 int rval; /* return value */
1684 xfs_bmbt_irec_t s; /* internal version of extent */
1685
1686#ifndef DEBUG
1687 if (whichfork == XFS_DATA_FORK)
1688 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1689#endif /* !DEBUG */
1690 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1691 return 0;
1692 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1693 return 0;
1694 ifp = XFS_IFORK_PTR(ip, whichfork);
1695 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1696 ep = xfs_iext_get_ext(ifp, 0);
1697 xfs_bmbt_get_all(ep, &s);
1698 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1699 if (rval && whichfork == XFS_DATA_FORK)
1700 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1701 return rval;
1702}
1703
1704/*
1705 * Extent tree manipulation functions used during allocation.
1706 */
1707
1708/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001709 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 */
1711STATIC int /* error */
1712xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001713 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001715 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001717 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001720 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 xfs_fileoff_t new_endoff; /* end offset of new entry */
1722 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1723 /* left is 0, right is 1, prev is 2 */
1724 int rval=0; /* return value (logging flags) */
1725 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001726 xfs_filblks_t da_new; /* new count del alloc blocks used */
1727 xfs_filblks_t da_old; /* old count del alloc blocks used */
1728 xfs_filblks_t temp=0; /* value for da_new calculations */
1729 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 int tmp_rval; /* partial logging flags */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001731 int whichfork = XFS_DATA_FORK;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001732 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001734 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001735 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001736
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001737 ASSERT(bma->idx >= 0);
1738 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001739 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001740 ASSERT(!bma->cur ||
1741 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001742
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001743 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745#define LEFT r[0]
1746#define RIGHT r[1]
1747#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 /*
1750 * Set up a bunch of variables to make the tests simpler.
1751 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001752 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 xfs_bmbt_get_all(ep, &PREV);
1754 new_endoff = new->br_startoff + new->br_blockcount;
1755 ASSERT(PREV.br_startoff <= new->br_startoff);
1756 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001757
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001758 da_old = startblockval(PREV.br_startblock);
1759 da_new = 0;
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 /*
1762 * Set flags determining what part of the previous delayed allocation
1763 * extent is being replaced by a real allocation.
1764 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001765 if (PREV.br_startoff == new->br_startoff)
1766 state |= BMAP_LEFT_FILLING;
1767 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1768 state |= BMAP_RIGHT_FILLING;
1769
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 /*
1771 * Check and set flags if this segment has a left neighbor.
1772 * Don't set contiguous if the combined extent would be too large.
1773 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001774 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001775 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001776 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001777
1778 if (isnullstartblock(LEFT.br_startblock))
1779 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001781
1782 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1783 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1784 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1785 LEFT.br_state == new->br_state &&
1786 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1787 state |= BMAP_LEFT_CONTIG;
1788
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 /*
1790 * Check and set flags if this segment has a right neighbor.
1791 * Don't set contiguous if the combined extent would be too large.
1792 * Also check for all-three-contiguous being too large.
1793 */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001794 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001795 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001796 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001797
1798 if (isnullstartblock(RIGHT.br_startblock))
1799 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001801
1802 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1803 new_endoff == RIGHT.br_startoff &&
1804 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1805 new->br_state == RIGHT.br_state &&
1806 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1807 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1808 BMAP_RIGHT_FILLING)) !=
1809 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1810 BMAP_RIGHT_FILLING) ||
1811 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1812 <= MAXEXTLEN))
1813 state |= BMAP_RIGHT_CONTIG;
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 error = 0;
1816 /*
1817 * Switch out based on the FILLING and CONTIG state bits.
1818 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001819 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1820 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1821 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1822 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 /*
1824 * Filling in all of a previously delayed allocation extent.
1825 * The left and right neighbors are both contiguous with new.
1826 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001827 bma->idx--;
1828 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1829 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 LEFT.br_blockcount + PREV.br_blockcount +
1831 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001832 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001833
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001834 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1835 bma->ip->i_d.di_nextents--;
1836 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1838 else {
1839 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001840 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001842 RIGHT.br_blockcount, &i);
1843 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001845 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001846 error = xfs_btree_delete(bma->cur, &i);
1847 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001849 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001850 error = xfs_btree_decrement(bma->cur, 0, &i);
1851 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001853 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001854 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 LEFT.br_startblock,
1856 LEFT.br_blockcount +
1857 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001858 RIGHT.br_blockcount, LEFT.br_state);
1859 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 goto done;
1861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 break;
1863
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001864 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 /*
1866 * Filling in all of a previously delayed allocation extent.
1867 * The left neighbor is contiguous, the right is not.
1868 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001869 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001870
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001871 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1872 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001873 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001874 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001875
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001876 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1877 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 rval = XFS_ILOG_DEXT;
1879 else {
1880 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001881 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001883 &i);
1884 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001886 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001887 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 LEFT.br_startblock,
1889 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001890 PREV.br_blockcount, LEFT.br_state);
1891 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 goto done;
1893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 break;
1895
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001896 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
1898 * Filling in all of a previously delayed allocation extent.
1899 * The right neighbor is contiguous, the left is not.
1900 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001901 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 xfs_bmbt_set_startblock(ep, new->br_startblock);
1903 xfs_bmbt_set_blockcount(ep,
1904 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001905 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001906
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001907 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1908 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 rval = XFS_ILOG_DEXT;
1910 else {
1911 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001912 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001914 RIGHT.br_blockcount, &i);
1915 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001917 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001918 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 new->br_startblock,
1920 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001921 RIGHT.br_blockcount, PREV.br_state);
1922 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 goto done;
1924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 break;
1926
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001927 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 /*
1929 * Filling in all of a previously delayed allocation extent.
1930 * Neither the left nor right neighbors are contiguous with
1931 * the new one.
1932 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001933 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001935 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001936
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001937 bma->ip->i_d.di_nextents++;
1938 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1940 else {
1941 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001942 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001944 &i);
1945 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001947 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001948 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1949 error = xfs_btree_insert(bma->cur, &i);
1950 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001952 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 break;
1955
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001956 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 /*
1958 * Filling in the first part of a previous delayed allocation.
1959 * The left neighbor is contiguous.
1960 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001961 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1962 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 LEFT.br_blockcount + new->br_blockcount);
1964 xfs_bmbt_set_startoff(ep,
1965 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001966 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001969 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001971 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 rval = XFS_ILOG_DEXT;
1973 else {
1974 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001975 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001977 &i);
1978 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001980 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001981 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 LEFT.br_startblock,
1983 LEFT.br_blockcount +
1984 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001985 LEFT.br_state);
1986 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 goto done;
1988 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001989 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001990 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001991 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001992 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001993
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001994 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 break;
1996
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001997 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 /*
1999 * Filling in the first part of a previous delayed allocation.
2000 * The left neighbor is not contiguous.
2001 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002002 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 xfs_bmbt_set_startoff(ep, new_endoff);
2004 temp = PREV.br_blockcount - new->br_blockcount;
2005 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002006 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
2007 bma->ip->i_d.di_nextents++;
2008 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2010 else {
2011 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002012 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002014 &i);
2015 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002017 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002018 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2019 error = xfs_btree_insert(bma->cur, &i);
2020 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002022 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002024
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002025 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002026 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2027 bma->firstblock, bma->flist,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002028 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 rval |= tmp_rval;
2030 if (error)
2031 goto done;
2032 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002033 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002034 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002035 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2036 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002037 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002038 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 break;
2040
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002041 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 /*
2043 * Filling in the last part of a previous delayed allocation.
2044 * The right neighbor is contiguous with the new allocation.
2045 */
2046 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002047 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002049 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002050 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 new->br_blockcount + RIGHT.br_blockcount,
2052 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002053 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2054 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 rval = XFS_ILOG_DEXT;
2056 else {
2057 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002058 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002060 RIGHT.br_blockcount, &i);
2061 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002063 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002064 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 new->br_startblock,
2066 new->br_blockcount +
2067 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002068 RIGHT.br_state);
2069 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 goto done;
2071 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002072
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002073 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002074 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002075 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002076 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002077 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002078
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002079 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 break;
2081
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002082 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /*
2084 * Filling in the last part of a previous delayed allocation.
2085 * The right neighbor is not contiguous.
2086 */
2087 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002088 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002090 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2091 bma->ip->i_d.di_nextents++;
2092 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2094 else {
2095 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002096 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002098 &i);
2099 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002101 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002102 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2103 error = xfs_btree_insert(bma->cur, &i);
2104 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002106 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002108
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002109 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002110 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2111 bma->firstblock, bma->flist, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002112 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 rval |= tmp_rval;
2114 if (error)
2115 goto done;
2116 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002117 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002118 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002119 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2120 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002121 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002122 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002123
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002124 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 break;
2126
2127 case 0:
2128 /*
2129 * Filling in the middle part of a previous delayed allocation.
2130 * Contiguity is impossible here.
2131 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002132 *
2133 * We start with a delayed allocation:
2134 *
2135 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2136 * PREV @ idx
2137 *
2138 * and we are allocating:
2139 * +rrrrrrrrrrrrrrrrr+
2140 * new
2141 *
2142 * and we set it up for insertion as:
2143 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2144 * new
2145 * PREV @ idx LEFT RIGHT
2146 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 */
2148 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002150 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002151 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2152 LEFT = *new;
2153 RIGHT.br_state = PREV.br_state;
2154 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002155 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002156 RIGHT.br_startoff = new_endoff;
2157 RIGHT.br_blockcount = temp2;
2158 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002159 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2160 bma->ip->i_d.di_nextents++;
2161 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2163 else {
2164 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002165 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002167 &i);
2168 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002170 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002171 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2172 error = xfs_btree_insert(bma->cur, &i);
2173 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002175 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002177
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002178 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002179 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2180 bma->firstblock, bma->flist, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002181 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 rval |= tmp_rval;
2183 if (error)
2184 goto done;
2185 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002186 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2187 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002188 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002189 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002190 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002191 error = xfs_mod_fdblocks(bma->ip->i_mount,
2192 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002193 ASSERT(!error);
2194 if (error)
2195 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002197
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002198 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002199 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002200 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2201 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2202 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002203 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002204 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002205
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002206 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002207 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 break;
2209
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002210 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2211 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2212 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2213 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2214 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2215 case BMAP_LEFT_CONTIG:
2216 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 /*
2218 * These cases are all impossible.
2219 */
2220 ASSERT(0);
2221 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002222
2223 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002224 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002225 int tmp_logflags; /* partial log flag return val */
2226
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002227 ASSERT(bma->cur == NULL);
2228 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2229 bma->firstblock, bma->flist, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002230 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002231 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002232 if (error)
2233 goto done;
2234 }
2235
2236 /* adjust for changes in reserved delayed indirect blocks */
2237 if (da_old || da_new) {
2238 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002239 if (bma->cur)
2240 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002241 ASSERT(temp <= da_old);
2242 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002243 xfs_mod_fdblocks(bma->ip->i_mount,
2244 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002245 }
2246
2247 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002248 if (bma->cur)
2249 bma->cur->bc_private.b.allocated = 0;
2250
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002251 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002253 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 return error;
2255#undef LEFT
2256#undef RIGHT
2257#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258}
2259
2260/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002261 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 */
2263STATIC int /* error */
2264xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002265 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002267 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002269 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002270 xfs_fsblock_t *first, /* pointer to firstblock variable */
2271 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002272 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002275 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002278 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 xfs_fileoff_t new_endoff; /* end offset of new entry */
2280 xfs_exntst_t newext; /* new extent state */
2281 xfs_exntst_t oldext; /* old extent state */
2282 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2283 /* left is 0, right is 1, prev is 2 */
2284 int rval=0; /* return value (logging flags) */
2285 int state = 0;/* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002286 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002288 *logflagsp = 0;
2289
2290 cur = *curp;
2291 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2292
2293 ASSERT(*idx >= 0);
2294 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2295 ASSERT(!isnullstartblock(new->br_startblock));
2296
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002297 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299#define LEFT r[0]
2300#define RIGHT r[1]
2301#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 /*
2304 * Set up a bunch of variables to make the tests simpler.
2305 */
2306 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002307 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 xfs_bmbt_get_all(ep, &PREV);
2309 newext = new->br_state;
2310 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2311 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2312 ASSERT(PREV.br_state == oldext);
2313 new_endoff = new->br_startoff + new->br_blockcount;
2314 ASSERT(PREV.br_startoff <= new->br_startoff);
2315 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 /*
2318 * Set flags determining what part of the previous oldext allocation
2319 * extent is being replaced by a newext allocation.
2320 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002321 if (PREV.br_startoff == new->br_startoff)
2322 state |= BMAP_LEFT_FILLING;
2323 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2324 state |= BMAP_RIGHT_FILLING;
2325
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 /*
2327 * Check and set flags if this segment has a left neighbor.
2328 * Don't set contiguous if the combined extent would be too large.
2329 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002330 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002331 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002332 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002333
2334 if (isnullstartblock(LEFT.br_startblock))
2335 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002337
2338 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2339 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2340 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2341 LEFT.br_state == newext &&
2342 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2343 state |= BMAP_LEFT_CONTIG;
2344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 /*
2346 * Check and set flags if this segment has a right neighbor.
2347 * Don't set contiguous if the combined extent would be too large.
2348 * Also check for all-three-contiguous being too large.
2349 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002350 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002351 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002352 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002353 if (isnullstartblock(RIGHT.br_startblock))
2354 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002356
2357 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2358 new_endoff == RIGHT.br_startoff &&
2359 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2360 newext == RIGHT.br_state &&
2361 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2362 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2363 BMAP_RIGHT_FILLING)) !=
2364 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2365 BMAP_RIGHT_FILLING) ||
2366 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2367 <= MAXEXTLEN))
2368 state |= BMAP_RIGHT_CONTIG;
2369
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 /*
2371 * Switch out based on the FILLING and CONTIG state bits.
2372 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002373 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2374 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2375 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2376 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 /*
2378 * Setting all of a previous oldext extent to newext.
2379 * The left and right neighbors are both contiguous with new.
2380 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002381 --*idx;
2382
2383 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2384 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 LEFT.br_blockcount + PREV.br_blockcount +
2386 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002387 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002388
Christoph Hellwigec90c552011-05-23 08:52:53 +00002389 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 ip->i_d.di_nextents -= 2;
2391 if (cur == NULL)
2392 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2393 else {
2394 rval = XFS_ILOG_CORE;
2395 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2396 RIGHT.br_startblock,
2397 RIGHT.br_blockcount, &i)))
2398 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002399 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002400 if ((error = xfs_btree_delete(cur, &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 Hellwig8df4da42008-10-30 16:55:58 +11002403 if ((error = xfs_btree_decrement(cur, 0, &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 Hellwig91cca5df2008-10-30 16:58:01 +11002406 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002409 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002411 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2413 LEFT.br_startblock,
2414 LEFT.br_blockcount + PREV.br_blockcount +
2415 RIGHT.br_blockcount, LEFT.br_state)))
2416 goto done;
2417 }
2418 break;
2419
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002420 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 /*
2422 * Setting all of a previous oldext extent to newext.
2423 * The left neighbor is contiguous, the right is not.
2424 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002425 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002426
Christoph Hellwigec90c552011-05-23 08:52:53 +00002427 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2428 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2429 LEFT.br_blockcount + PREV.br_blockcount);
2430 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2431
2432 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 ip->i_d.di_nextents--;
2434 if (cur == NULL)
2435 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2436 else {
2437 rval = XFS_ILOG_CORE;
2438 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2439 PREV.br_startblock, PREV.br_blockcount,
2440 &i)))
2441 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002442 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002443 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002446 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002448 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2450 LEFT.br_startblock,
2451 LEFT.br_blockcount + PREV.br_blockcount,
2452 LEFT.br_state)))
2453 goto done;
2454 }
2455 break;
2456
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002457 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 /*
2459 * Setting all of a previous oldext extent to newext.
2460 * The right neighbor is contiguous, the left is not.
2461 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002462 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 xfs_bmbt_set_blockcount(ep,
2464 PREV.br_blockcount + RIGHT.br_blockcount);
2465 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002466 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2467 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 ip->i_d.di_nextents--;
2469 if (cur == NULL)
2470 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2471 else {
2472 rval = XFS_ILOG_CORE;
2473 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2474 RIGHT.br_startblock,
2475 RIGHT.br_blockcount, &i)))
2476 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002477 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002478 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002481 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002483 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2485 new->br_startblock,
2486 new->br_blockcount + RIGHT.br_blockcount,
2487 newext)))
2488 goto done;
2489 }
2490 break;
2491
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002492 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 /*
2494 * Setting all of a previous oldext extent to newext.
2495 * Neither the left nor right neighbors are contiguous with
2496 * the new one.
2497 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002498 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002500 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 if (cur == NULL)
2503 rval = XFS_ILOG_DEXT;
2504 else {
2505 rval = 0;
2506 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2507 new->br_startblock, new->br_blockcount,
2508 &i)))
2509 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002510 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2512 new->br_startblock, new->br_blockcount,
2513 newext)))
2514 goto done;
2515 }
2516 break;
2517
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002518 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 /*
2520 * Setting the first part of a previous oldext extent to newext.
2521 * The left neighbor is contiguous.
2522 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002523 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2524 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 LEFT.br_blockcount + new->br_blockcount);
2526 xfs_bmbt_set_startoff(ep,
2527 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002528 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002529
Christoph Hellwigec90c552011-05-23 08:52:53 +00002530 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 xfs_bmbt_set_startblock(ep,
2532 new->br_startblock + new->br_blockcount);
2533 xfs_bmbt_set_blockcount(ep,
2534 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002535 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002536
Christoph Hellwigec90c552011-05-23 08:52:53 +00002537 --*idx;
2538
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 if (cur == NULL)
2540 rval = XFS_ILOG_DEXT;
2541 else {
2542 rval = 0;
2543 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2544 PREV.br_startblock, PREV.br_blockcount,
2545 &i)))
2546 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002547 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 if ((error = xfs_bmbt_update(cur,
2549 PREV.br_startoff + new->br_blockcount,
2550 PREV.br_startblock + new->br_blockcount,
2551 PREV.br_blockcount - new->br_blockcount,
2552 oldext)))
2553 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002554 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002556 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 LEFT.br_startblock,
2558 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002559 LEFT.br_state);
2560 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 goto done;
2562 }
2563 break;
2564
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002565 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 /*
2567 * Setting the first part of a previous oldext extent to newext.
2568 * The left neighbor is not contiguous.
2569 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002570 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2572 xfs_bmbt_set_startoff(ep, new_endoff);
2573 xfs_bmbt_set_blockcount(ep,
2574 PREV.br_blockcount - new->br_blockcount);
2575 xfs_bmbt_set_startblock(ep,
2576 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002577 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002578
Christoph Hellwigec90c552011-05-23 08:52:53 +00002579 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 ip->i_d.di_nextents++;
2581 if (cur == NULL)
2582 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2583 else {
2584 rval = XFS_ILOG_CORE;
2585 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2586 PREV.br_startblock, PREV.br_blockcount,
2587 &i)))
2588 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002589 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if ((error = xfs_bmbt_update(cur,
2591 PREV.br_startoff + new->br_blockcount,
2592 PREV.br_startblock + new->br_blockcount,
2593 PREV.br_blockcount - new->br_blockcount,
2594 oldext)))
2595 goto done;
2596 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002597 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002599 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 }
2601 break;
2602
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002603 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 /*
2605 * Setting the last part of a previous oldext extent to newext.
2606 * The right neighbor is contiguous with the new allocation.
2607 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002608 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 xfs_bmbt_set_blockcount(ep,
2610 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002611 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2612
2613 ++*idx;
2614
2615 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2616 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002617 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002619 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (cur == NULL)
2622 rval = XFS_ILOG_DEXT;
2623 else {
2624 rval = 0;
2625 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2626 PREV.br_startblock,
2627 PREV.br_blockcount, &i)))
2628 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002629 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2631 PREV.br_startblock,
2632 PREV.br_blockcount - new->br_blockcount,
2633 oldext)))
2634 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002635 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 goto done;
2637 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2638 new->br_startblock,
2639 new->br_blockcount + RIGHT.br_blockcount,
2640 newext)))
2641 goto done;
2642 }
2643 break;
2644
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002645 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 /*
2647 * Setting the last part of a previous oldext extent to newext.
2648 * The right neighbor is not contiguous.
2649 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002650 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 xfs_bmbt_set_blockcount(ep,
2652 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002653 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002654
Christoph Hellwigec90c552011-05-23 08:52:53 +00002655 ++*idx;
2656 xfs_iext_insert(ip, *idx, 1, new, state);
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 ip->i_d.di_nextents++;
2659 if (cur == NULL)
2660 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2661 else {
2662 rval = XFS_ILOG_CORE;
2663 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2664 PREV.br_startblock, PREV.br_blockcount,
2665 &i)))
2666 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002667 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2669 PREV.br_startblock,
2670 PREV.br_blockcount - new->br_blockcount,
2671 oldext)))
2672 goto done;
2673 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2674 new->br_startblock, new->br_blockcount,
2675 &i)))
2676 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002677 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002679 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002681 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
2683 break;
2684
2685 case 0:
2686 /*
2687 * Setting the middle part of a previous oldext extent to
2688 * newext. Contiguity is impossible here.
2689 * One extent becomes three extents.
2690 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002691 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 xfs_bmbt_set_blockcount(ep,
2693 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002694 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002695
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 r[0] = *new;
2697 r[1].br_startoff = new_endoff;
2698 r[1].br_blockcount =
2699 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2700 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2701 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002702
2703 ++*idx;
2704 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 ip->i_d.di_nextents += 2;
2707 if (cur == NULL)
2708 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2709 else {
2710 rval = XFS_ILOG_CORE;
2711 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2712 PREV.br_startblock, PREV.br_blockcount,
2713 &i)))
2714 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002715 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 /* new right extent - oldext */
2717 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2718 r[1].br_startblock, r[1].br_blockcount,
2719 r[1].br_state)))
2720 goto done;
2721 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002723 cur->bc_rec.b.br_blockcount =
2724 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002725 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002727 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002728 /*
2729 * Reset the cursor to the position of the new extent
2730 * we are about to insert as we can't trust it after
2731 * the previous insert.
2732 */
2733 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2734 new->br_startblock, new->br_blockcount,
2735 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002737 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002739 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002740 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002742 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 }
2744 break;
2745
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002746 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2747 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2748 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2749 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2750 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2751 case BMAP_LEFT_CONTIG:
2752 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 /*
2754 * These cases are all impossible.
2755 */
2756 ASSERT(0);
2757 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002758
2759 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002760 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002761 int tmp_logflags; /* partial log flag return val */
2762
2763 ASSERT(cur == NULL);
2764 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
2765 0, &tmp_logflags, XFS_DATA_FORK);
2766 *logflagsp |= tmp_logflags;
2767 if (error)
2768 goto done;
2769 }
2770
2771 /* clear out the allocated field, done with it now in any case. */
2772 if (cur) {
2773 cur->bc_private.b.allocated = 0;
2774 *curp = cur;
2775 }
2776
2777 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002779 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return error;
2781#undef LEFT
2782#undef RIGHT
2783#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
2786/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002787 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002789STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790xfs_bmap_add_extent_hole_delay(
2791 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002792 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002793 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002795 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2797 xfs_filblks_t newlen=0; /* new indirect size */
2798 xfs_filblks_t oldlen=0; /* old indirect size */
2799 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2800 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002801 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002803 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002805 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002806
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 /*
2808 * Check and set flags if this segment has a left neighbor
2809 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002810 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002811 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002812 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002813
2814 if (isnullstartblock(left.br_startblock))
2815 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 /*
2819 * Check and set flags if the current (right) segment exists.
2820 * If it doesn't exist, we're converting the hole at end-of-file.
2821 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002822 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002823 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002824 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002825
2826 if (isnullstartblock(right.br_startblock))
2827 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002829
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 /*
2831 * Set contiguity flags on the left and right neighbors.
2832 * Don't let extents get too large, even if the pieces are contiguous.
2833 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002834 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2835 left.br_startoff + left.br_blockcount == new->br_startoff &&
2836 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2837 state |= BMAP_LEFT_CONTIG;
2838
2839 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2840 new->br_startoff + new->br_blockcount == right.br_startoff &&
2841 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2842 (!(state & BMAP_LEFT_CONTIG) ||
2843 (left.br_blockcount + new->br_blockcount +
2844 right.br_blockcount <= MAXEXTLEN)))
2845 state |= BMAP_RIGHT_CONTIG;
2846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 /*
2848 * Switch out based on the contiguity flags.
2849 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002850 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2851 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 /*
2853 * New allocation is contiguous with delayed allocations
2854 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002855 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002857 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 temp = left.br_blockcount + new->br_blockcount +
2859 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002860
Christoph Hellwigec90c552011-05-23 08:52:53 +00002861 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2862 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002863 oldlen = startblockval(left.br_startblock) +
2864 startblockval(new->br_startblock) +
2865 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002867 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002868 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002869 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002870
Christoph Hellwigec90c552011-05-23 08:52:53 +00002871 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 break;
2873
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002874 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 /*
2876 * New allocation is contiguous with a delayed allocation
2877 * on the left.
2878 * Merge the new allocation with the left neighbor.
2879 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002880 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002882
2883 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2884 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002885 oldlen = startblockval(left.br_startblock) +
2886 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002888 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002889 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002890 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 break;
2892
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002893 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 /*
2895 * New allocation is contiguous with a delayed allocation
2896 * on the right.
2897 * Merge the new allocation with the right neighbor.
2898 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002899 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002901 oldlen = startblockval(new->br_startblock) +
2902 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002904 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2905 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002906 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002907 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 break;
2909
2910 case 0:
2911 /*
2912 * New allocation is not contiguous with another
2913 * delayed allocation.
2914 * Insert a new entry.
2915 */
2916 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002917 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 break;
2919 }
2920 if (oldlen != newlen) {
2921 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002922 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2923 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 /*
2925 * Nothing to do for disk quota accounting here.
2926 */
2927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
2930/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002931 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 */
2933STATIC int /* error */
2934xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002935 struct xfs_bmalloca *bma,
2936 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002938 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 int i; /* temp state */
2941 xfs_ifork_t *ifp; /* inode fork pointer */
2942 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2943 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002944 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002946 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Eric Sandeenf1f96c42016-01-04 16:10:42 +11002948 mp = bma->ip->i_mount;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002949 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002950
Christoph Hellwigc6534242011-09-18 20:41:05 +00002951 ASSERT(bma->idx >= 0);
2952 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002953 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002954 ASSERT(!bma->cur ||
2955 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002956
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002957 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002958
2959 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002960 if (whichfork == XFS_ATTR_FORK)
2961 state |= BMAP_ATTRFORK;
2962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 /*
2964 * Check and set flags if this segment has a left neighbor.
2965 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002966 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002967 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002968 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002969 if (isnullstartblock(left.br_startblock))
2970 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 /*
2974 * Check and set flags if this segment has a current value.
2975 * Not true if we're inserting into the "hole" at eof.
2976 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002977 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002978 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002979 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002980 if (isnullstartblock(right.br_startblock))
2981 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 /*
2985 * We're inserting a real allocation between "left" and "right".
2986 * Set the contiguity flags. Don't let extents get too large.
2987 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002988 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2989 left.br_startoff + left.br_blockcount == new->br_startoff &&
2990 left.br_startblock + left.br_blockcount == new->br_startblock &&
2991 left.br_state == new->br_state &&
2992 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2993 state |= BMAP_LEFT_CONTIG;
2994
2995 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2996 new->br_startoff + new->br_blockcount == right.br_startoff &&
2997 new->br_startblock + new->br_blockcount == right.br_startblock &&
2998 new->br_state == right.br_state &&
2999 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
3000 (!(state & BMAP_LEFT_CONTIG) ||
3001 left.br_blockcount + new->br_blockcount +
3002 right.br_blockcount <= MAXEXTLEN))
3003 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003005 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 /*
3007 * Select which case we're in here, and implement it.
3008 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003009 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3010 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 /*
3012 * New allocation is contiguous with real allocations on the
3013 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003014 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003016 --bma->idx;
3017 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3018 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 left.br_blockcount + new->br_blockcount +
3020 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003021 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003022
Christoph Hellwigc6534242011-09-18 20:41:05 +00003023 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003024
Christoph Hellwigc6534242011-09-18 20:41:05 +00003025 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3026 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3027 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003028 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003029 } else {
3030 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003031 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3032 right.br_startblock, right.br_blockcount,
3033 &i);
3034 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003035 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003036 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003037 error = xfs_btree_delete(bma->cur, &i);
3038 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003039 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003040 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003041 error = xfs_btree_decrement(bma->cur, 0, &i);
3042 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003043 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003044 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003045 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003046 left.br_startblock,
3047 left.br_blockcount +
3048 new->br_blockcount +
3049 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003050 left.br_state);
3051 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003052 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003054 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003056 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 /*
3058 * New allocation is contiguous with a real allocation
3059 * on the left.
3060 * Merge the new allocation with the left neighbor.
3061 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003062 --bma->idx;
3063 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3064 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003066 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003067
Christoph Hellwigc6534242011-09-18 20:41:05 +00003068 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003069 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003070 } else {
3071 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003072 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3073 left.br_startblock, left.br_blockcount,
3074 &i);
3075 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003076 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003077 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003078 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003079 left.br_startblock,
3080 left.br_blockcount +
3081 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003082 left.br_state);
3083 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003084 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003086 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003088 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 /*
3090 * New allocation is contiguous with a real allocation
3091 * on the right.
3092 * Merge the new allocation with the right neighbor.
3093 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003094 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3095 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003096 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 new->br_blockcount + right.br_blockcount,
3098 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003099 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003100
Christoph Hellwigc6534242011-09-18 20:41:05 +00003101 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003102 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003103 } else {
3104 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003105 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003106 right.br_startoff,
3107 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003108 right.br_blockcount, &i);
3109 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003110 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003111 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003112 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003113 new->br_startblock,
3114 new->br_blockcount +
3115 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003116 right.br_state);
3117 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003118 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003120 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121
3122 case 0:
3123 /*
3124 * New allocation is not contiguous with another
3125 * real allocation.
3126 * Insert a new entry.
3127 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003128 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3129 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3130 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3131 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003132 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003133 } else {
3134 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003135 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003136 new->br_startoff,
3137 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003138 new->br_blockcount, &i);
3139 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003140 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003141 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003142 bma->cur->bc_rec.b.br_state = new->br_state;
3143 error = xfs_btree_insert(bma->cur, &i);
3144 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003145 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003146 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003148 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003150
3151 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003152 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003153 int tmp_logflags; /* partial log flag return val */
3154
Christoph Hellwigc6534242011-09-18 20:41:05 +00003155 ASSERT(bma->cur == NULL);
3156 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3157 bma->firstblock, bma->flist, &bma->cur,
3158 0, &tmp_logflags, whichfork);
3159 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003160 if (error)
3161 goto done;
3162 }
3163
3164 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003165 if (bma->cur)
3166 bma->cur->bc_private.b.allocated = 0;
3167
3168 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003169done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003170 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003171 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172}
3173
Nathan Scottdd9f4382006-01-11 15:28:28 +11003174/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003175 * Functions used in the extent read, allocate and remove paths
3176 */
3177
3178/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003179 * Adjust the size of the new extent based on di_extsize and rt extsize.
3180 */
Dave Chinner68988112013-08-12 20:49:42 +10003181int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003182xfs_bmap_extsize_align(
3183 xfs_mount_t *mp,
3184 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3185 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3186 xfs_extlen_t extsz, /* align to this extent size */
3187 int rt, /* is this a realtime inode? */
3188 int eof, /* is extent at end-of-file? */
3189 int delay, /* creating delalloc extent? */
3190 int convert, /* overwriting unwritten extent? */
3191 xfs_fileoff_t *offp, /* in/out: aligned offset */
3192 xfs_extlen_t *lenp) /* in/out: aligned length */
3193{
3194 xfs_fileoff_t orig_off; /* original offset */
3195 xfs_extlen_t orig_alen; /* original length */
3196 xfs_fileoff_t orig_end; /* original off+len */
3197 xfs_fileoff_t nexto; /* next file offset */
3198 xfs_fileoff_t prevo; /* previous file offset */
3199 xfs_fileoff_t align_off; /* temp for offset */
3200 xfs_extlen_t align_alen; /* temp for length */
3201 xfs_extlen_t temp; /* temp for calculations */
3202
3203 if (convert)
3204 return 0;
3205
3206 orig_off = align_off = *offp;
3207 orig_alen = align_alen = *lenp;
3208 orig_end = orig_off + orig_alen;
3209
3210 /*
3211 * If this request overlaps an existing extent, then don't
3212 * attempt to perform any additional alignment.
3213 */
3214 if (!delay && !eof &&
3215 (orig_off >= gotp->br_startoff) &&
3216 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3217 return 0;
3218 }
3219
3220 /*
3221 * If the file offset is unaligned vs. the extent size
3222 * we need to align it. This will be possible unless
3223 * the file was previously written with a kernel that didn't
3224 * perform this alignment, or if a truncate shot us in the
3225 * foot.
3226 */
3227 temp = do_mod(orig_off, extsz);
3228 if (temp) {
3229 align_alen += temp;
3230 align_off -= temp;
3231 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003232
3233 /* Same adjustment for the end of the requested area. */
3234 temp = (align_alen % extsz);
3235 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003236 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003237
3238 /*
3239 * For large extent hint sizes, the aligned extent might be larger than
3240 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3241 * the length back under MAXEXTLEN. The outer allocation loops handle
3242 * short allocation just fine, so it is safe to do this. We only want to
3243 * do it when we are forced to, though, because it means more allocation
3244 * operations are required.
3245 */
3246 while (align_alen > MAXEXTLEN)
3247 align_alen -= extsz;
3248 ASSERT(align_alen <= MAXEXTLEN);
3249
Nathan Scottdd9f4382006-01-11 15:28:28 +11003250 /*
3251 * If the previous block overlaps with this proposed allocation
3252 * then move the start forward without adjusting the length.
3253 */
3254 if (prevp->br_startoff != NULLFILEOFF) {
3255 if (prevp->br_startblock == HOLESTARTBLOCK)
3256 prevo = prevp->br_startoff;
3257 else
3258 prevo = prevp->br_startoff + prevp->br_blockcount;
3259 } else
3260 prevo = 0;
3261 if (align_off != orig_off && align_off < prevo)
3262 align_off = prevo;
3263 /*
3264 * If the next block overlaps with this proposed allocation
3265 * then move the start back without adjusting the length,
3266 * but not before offset 0.
3267 * This may of course make the start overlap previous block,
3268 * and if we hit the offset 0 limit then the next block
3269 * can still overlap too.
3270 */
3271 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3272 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3273 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3274 nexto = gotp->br_startoff + gotp->br_blockcount;
3275 else
3276 nexto = gotp->br_startoff;
3277 } else
3278 nexto = NULLFILEOFF;
3279 if (!eof &&
3280 align_off + align_alen != orig_end &&
3281 align_off + align_alen > nexto)
3282 align_off = nexto > align_alen ? nexto - align_alen : 0;
3283 /*
3284 * If we're now overlapping the next or previous extent that
3285 * means we can't fit an extsz piece in this hole. Just move
3286 * the start forward to the first valid spot and set
3287 * the length so we hit the end.
3288 */
3289 if (align_off != orig_off && align_off < prevo)
3290 align_off = prevo;
3291 if (align_off + align_alen != orig_end &&
3292 align_off + align_alen > nexto &&
3293 nexto != NULLFILEOFF) {
3294 ASSERT(nexto > prevo);
3295 align_alen = nexto - align_off;
3296 }
3297
3298 /*
3299 * If realtime, and the result isn't a multiple of the realtime
3300 * extent size we need to remove blocks until it is.
3301 */
3302 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3303 /*
3304 * We're not covering the original request, or
3305 * we won't be able to once we fix the length.
3306 */
3307 if (orig_off < align_off ||
3308 orig_end > align_off + align_alen ||
3309 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003310 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003311 /*
3312 * Try to fix it by moving the start up.
3313 */
3314 if (align_off + temp <= orig_off) {
3315 align_alen -= temp;
3316 align_off += temp;
3317 }
3318 /*
3319 * Try to fix it by moving the end in.
3320 */
3321 else if (align_off + align_alen - temp >= orig_end)
3322 align_alen -= temp;
3323 /*
3324 * Set the start to the minimum then trim the length.
3325 */
3326 else {
3327 align_alen -= orig_off - align_off;
3328 align_off = orig_off;
3329 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3330 }
3331 /*
3332 * Result doesn't cover the request, fail it.
3333 */
3334 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003335 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003336 } else {
3337 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003338 /* see MAXEXTLEN handling above */
3339 ASSERT(orig_end <= align_off + align_alen ||
3340 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003341 }
3342
3343#ifdef DEBUG
3344 if (!eof && gotp->br_startoff != NULLFILEOFF)
3345 ASSERT(align_off + align_alen <= gotp->br_startoff);
3346 if (prevp->br_startoff != NULLFILEOFF)
3347 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3348#endif
3349
3350 *lenp = align_alen;
3351 *offp = align_off;
3352 return 0;
3353}
3354
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355#define XFS_ALLOC_GAP_UNITS 4
3356
Dave Chinner68988112013-08-12 20:49:42 +10003357void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003358xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003359 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360{
3361 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3363 xfs_mount_t *mp; /* mount point structure */
3364 int nullfb; /* true if ap->firstblock isn't set */
3365 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
3367#define ISVALID(x,y) \
3368 (rt ? \
3369 (x) < mp->m_sb.sb_rblocks : \
3370 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3371 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3372 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3373
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003375 nullfb = *ap->firstblock == NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003377 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 /*
3379 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003380 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003382 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3383 !isnullstartblock(ap->prev.br_startblock) &&
3384 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3385 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003386 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 /*
3388 * Adjust for the gap between prevp and us.
3389 */
Dave Chinner3a756672011-09-18 20:40:58 +00003390 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003391 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003393 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3394 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
3396 /*
3397 * If not at eof, then compare the two neighbor blocks.
3398 * Figure out whether either one gives us a good starting point,
3399 * and pick the better one.
3400 */
3401 else if (!ap->eof) {
3402 xfs_fsblock_t gotbno; /* right side block number */
3403 xfs_fsblock_t gotdiff=0; /* right side difference */
3404 xfs_fsblock_t prevbno; /* left side block number */
3405 xfs_fsblock_t prevdiff=0; /* left side difference */
3406
3407 /*
3408 * If there's a previous (left) block, select a requested
3409 * start block based on it.
3410 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003411 if (ap->prev.br_startoff != NULLFILEOFF &&
3412 !isnullstartblock(ap->prev.br_startblock) &&
3413 (prevbno = ap->prev.br_startblock +
3414 ap->prev.br_blockcount) &&
3415 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 /*
3417 * Calculate gap to end of previous block.
3418 */
Dave Chinner3a756672011-09-18 20:40:58 +00003419 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003420 (ap->prev.br_startoff +
3421 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 /*
3423 * Figure the startblock based on the previous block's
3424 * end and the gap size.
3425 * Heuristic!
3426 * If the gap is large relative to the piece we're
3427 * allocating, or using it gives us an invalid block
3428 * number, then just use the end of the previous block.
3429 */
Dave Chinner3a756672011-09-18 20:40:58 +00003430 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003432 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 prevbno += adjust;
3434 else
3435 prevdiff += adjust;
3436 /*
3437 * If the firstblock forbids it, can't use it,
3438 * must use default.
3439 */
3440 if (!rt && !nullfb &&
3441 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3442 prevbno = NULLFSBLOCK;
3443 }
3444 /*
3445 * No previous block or can't follow it, just default.
3446 */
3447 else
3448 prevbno = NULLFSBLOCK;
3449 /*
3450 * If there's a following (right) block, select a requested
3451 * start block based on it.
3452 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003453 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 /*
3455 * Calculate gap to start of next block.
3456 */
Dave Chinner3a756672011-09-18 20:40:58 +00003457 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 /*
3459 * Figure the startblock based on the next block's
3460 * start and the gap size.
3461 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003462 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 /*
3464 * Heuristic!
3465 * If the gap is large relative to the piece we're
3466 * allocating, or using it gives us an invalid block
3467 * number, then just use the start of the next block
3468 * offset by our length.
3469 */
Dave Chinner3a756672011-09-18 20:40:58 +00003470 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 ISVALID(gotbno - gotdiff, gotbno))
3472 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003473 else if (ISVALID(gotbno - ap->length, gotbno)) {
3474 gotbno -= ap->length;
3475 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 } else
3477 gotdiff += adjust;
3478 /*
3479 * If the firstblock forbids it, can't use it,
3480 * must use default.
3481 */
3482 if (!rt && !nullfb &&
3483 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3484 gotbno = NULLFSBLOCK;
3485 }
3486 /*
3487 * No next block, just default.
3488 */
3489 else
3490 gotbno = NULLFSBLOCK;
3491 /*
3492 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003493 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 */
3495 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003496 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003498 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003500 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003502#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003503}
3504
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003505static int
3506xfs_bmap_longest_free_extent(
3507 struct xfs_trans *tp,
3508 xfs_agnumber_t ag,
3509 xfs_extlen_t *blen,
3510 int *notinit)
3511{
3512 struct xfs_mount *mp = tp->t_mountp;
3513 struct xfs_perag *pag;
3514 xfs_extlen_t longest;
3515 int error = 0;
3516
3517 pag = xfs_perag_get(mp, ag);
3518 if (!pag->pagf_init) {
3519 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3520 if (error)
3521 goto out;
3522
3523 if (!pag->pagf_init) {
3524 *notinit = 1;
3525 goto out;
3526 }
3527 }
3528
Dave Chinner50adbcb2015-06-22 10:04:31 +10003529 longest = xfs_alloc_longest_free_extent(mp, pag,
Dave Chinner496817b2015-06-22 10:13:30 +10003530 xfs_alloc_min_freelist(mp, pag));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003531 if (*blen < longest)
3532 *blen = longest;
3533
3534out:
3535 xfs_perag_put(pag);
3536 return error;
3537}
3538
3539static void
3540xfs_bmap_select_minlen(
3541 struct xfs_bmalloca *ap,
3542 struct xfs_alloc_arg *args,
3543 xfs_extlen_t *blen,
3544 int notinit)
3545{
3546 if (notinit || *blen < ap->minlen) {
3547 /*
3548 * Since we did a BUF_TRYLOCK above, it is possible that
3549 * there is space for this request.
3550 */
3551 args->minlen = ap->minlen;
3552 } else if (*blen < args->maxlen) {
3553 /*
3554 * If the best seen length is less than the request length,
3555 * use the best as the minimum.
3556 */
3557 args->minlen = *blen;
3558 } else {
3559 /*
3560 * Otherwise we've seen an extent as big as maxlen, use that
3561 * as the minimum.
3562 */
3563 args->minlen = args->maxlen;
3564 }
3565}
3566
Nathan Scotta365bdd2006-03-14 13:34:16 +11003567STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003568xfs_bmap_btalloc_nullfb(
3569 struct xfs_bmalloca *ap,
3570 struct xfs_alloc_arg *args,
3571 xfs_extlen_t *blen)
3572{
3573 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003574 xfs_agnumber_t ag, startag;
3575 int notinit = 0;
3576 int error;
3577
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003578 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003579 args->total = ap->total;
3580
Christoph Hellwigc467c042010-02-15 23:34:42 +00003581 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3582 if (startag == NULLAGNUMBER)
3583 startag = ag = 0;
3584
Dave Chinner14b064c2011-01-27 12:16:28 +11003585 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003586 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3587 &notinit);
3588 if (error)
3589 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003590
Christoph Hellwigc467c042010-02-15 23:34:42 +00003591 if (++ag == mp->m_sb.sb_agcount)
3592 ag = 0;
3593 if (ag == startag)
3594 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003595 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003596
3597 xfs_bmap_select_minlen(ap, args, blen, notinit);
3598 return 0;
3599}
3600
3601STATIC int
3602xfs_bmap_btalloc_filestreams(
3603 struct xfs_bmalloca *ap,
3604 struct xfs_alloc_arg *args,
3605 xfs_extlen_t *blen)
3606{
3607 struct xfs_mount *mp = ap->ip->i_mount;
3608 xfs_agnumber_t ag;
3609 int notinit = 0;
3610 int error;
3611
3612 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3613 args->total = ap->total;
3614
3615 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3616 if (ag == NULLAGNUMBER)
3617 ag = 0;
3618
3619 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3620 if (error)
3621 return error;
3622
3623 if (*blen < args->maxlen) {
3624 error = xfs_filestream_new_ag(ap, &ag);
3625 if (error)
3626 return error;
3627
3628 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3629 &notinit);
3630 if (error)
3631 return error;
3632
3633 }
3634
3635 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003636
3637 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003638 * Set the failure fallback case to look in the selected AG as stream
3639 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003640 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003641 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003642 return 0;
3643}
3644
3645STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003646xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003647 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003648{
3649 xfs_mount_t *mp; /* mount point structure */
3650 xfs_alloctype_t atype = 0; /* type for allocation routines */
3651 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003652 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003653 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003654 xfs_alloc_arg_t args;
3655 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003656 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003657 int nullfb; /* true if ap->firstblock isn't set */
3658 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003659 int tryagain;
3660 int error;
Dave Chinner33177f02013-12-12 16:34:36 +11003661 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003662
Dave Chinnera99ebf42011-12-01 11:24:20 +00003663 ASSERT(ap->length);
3664
Nathan Scotta365bdd2006-03-14 13:34:16 +11003665 mp = ap->ip->i_mount;
Dave Chinner33177f02013-12-12 16:34:36 +11003666
3667 /* stripe alignment for allocation is determined by mount parameters */
3668 stripe_align = 0;
3669 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3670 stripe_align = mp->m_swidth;
3671 else if (mp->m_dalign)
3672 stripe_align = mp->m_dalign;
3673
David Chinner957d0eb2007-06-18 16:50:37 +10003674 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003675 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003676 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003677 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003678 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003679 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003680 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003681 }
Dave Chinner33177f02013-12-12 16:34:36 +11003682
3683
Dave Chinner0937e0f2011-09-18 20:40:57 +00003684 nullfb = *ap->firstblock == NULLFSBLOCK;
3685 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003686 if (nullfb) {
3687 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
3688 ag = xfs_filestream_lookup_ag(ap->ip);
3689 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003690 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003691 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003692 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003693 }
3694 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003695 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003696
3697 xfs_bmap_adjacent(ap);
3698
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003700 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 * it's in the right allocation group.
3702 */
Dave Chinner3a756672011-09-18 20:40:58 +00003703 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 ;
3705 else
Dave Chinner3a756672011-09-18 20:40:58 +00003706 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 * Normal allocation, done through xfs_alloc_vextent.
3709 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003710 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003711 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003712 args.tp = ap->tp;
3713 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003714 args.fsbno = ap->blkno;
Dave Chinner14b064c2011-01-27 12:16:28 +11003715
3716 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner3a756672011-09-18 20:40:58 +00003717 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003718 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003719 blen = 0;
3720 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003721 /*
3722 * Search for an allocation group with a single extent large
3723 * enough for the request. If one isn't found, then adjust
3724 * the minimum allocation size to the largest space found.
3725 */
3726 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3727 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3728 else
3729 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003730 if (error)
3731 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003732 } else if (ap->flist->xbf_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003733 if (xfs_inode_is_filestream(ap->ip))
3734 args.type = XFS_ALLOCTYPE_FIRST_AG;
3735 else
3736 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003737 args.total = args.minlen = ap->minlen;
3738 } else {
3739 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3740 args.total = ap->total;
3741 args.minlen = ap->minlen;
3742 }
David Chinner957d0eb2007-06-18 16:50:37 +10003743 /* apply extent size hints if obtained earlier */
3744 if (unlikely(align)) {
3745 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003746 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003747 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11003748 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003749 args.prod = 1;
3750 args.mod = 0;
3751 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003752 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003753 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003754 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3755 }
3756 /*
3757 * If we are not low on available data blocks, and the
3758 * underlying logical volume manager is a stripe, and
3759 * the file offset is zero then try to allocate data
3760 * blocks on stripe unit boundary.
3761 * NOTE: ap->aeof is only set if the allocation length
3762 * is >= the stripe unit and the allocation offset is
3763 * at the end of file.
3764 */
Dave Chinner0937e0f2011-09-18 20:40:57 +00003765 if (!ap->flist->xbf_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003766 if (!ap->offset) {
Dave Chinner33177f02013-12-12 16:34:36 +11003767 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003768 atype = args.type;
3769 isaligned = 1;
3770 /*
3771 * Adjust for alignment
3772 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003773 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003774 args.minlen = blen - args.alignment;
3775 args.minalignslop = 0;
3776 } else {
3777 /*
3778 * First try an exact bno allocation.
3779 * If it fails then do a near or start bno
3780 * allocation with alignment turned on.
3781 */
3782 atype = args.type;
3783 tryagain = 1;
3784 args.type = XFS_ALLOCTYPE_THIS_BNO;
3785 args.alignment = 1;
3786 /*
3787 * Compute the minlen+alignment for the
3788 * next case. Set slop so that the value
3789 * of minlen+alignment+slop doesn't go up
3790 * between the calls.
3791 */
Dave Chinner33177f02013-12-12 16:34:36 +11003792 if (blen > stripe_align && blen <= args.maxlen)
3793 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003794 else
3795 nextminlen = args.minlen;
Dave Chinner33177f02013-12-12 16:34:36 +11003796 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003797 args.minalignslop =
Dave Chinner33177f02013-12-12 16:34:36 +11003798 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003799 args.minlen - 1;
3800 else
3801 args.minalignslop = 0;
3802 }
3803 } else {
3804 args.alignment = 1;
3805 args.minalignslop = 0;
3806 }
3807 args.minleft = ap->minleft;
3808 args.wasdel = ap->wasdel;
3809 args.isfl = 0;
3810 args.userdata = ap->userdata;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003811 if (ap->userdata & XFS_ALLOC_USERDATA_ZERO)
3812 args.ip = ap->ip;
3813
3814 error = xfs_alloc_vextent(&args);
3815 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003816 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003817
Nathan Scotta365bdd2006-03-14 13:34:16 +11003818 if (tryagain && args.fsbno == NULLFSBLOCK) {
3819 /*
3820 * Exact allocation failed. Now try with alignment
3821 * turned on.
3822 */
3823 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003824 args.fsbno = ap->blkno;
Dave Chinner33177f02013-12-12 16:34:36 +11003825 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003826 args.minlen = nextminlen;
3827 args.minalignslop = 0;
3828 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 if ((error = xfs_alloc_vextent(&args)))
3830 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003831 }
3832 if (isaligned && args.fsbno == NULLFSBLOCK) {
3833 /*
3834 * allocation failed, so turn off alignment and
3835 * try again.
3836 */
3837 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003838 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003839 args.alignment = 0;
3840 if ((error = xfs_alloc_vextent(&args)))
3841 return error;
3842 }
3843 if (args.fsbno == NULLFSBLOCK && nullfb &&
3844 args.minlen > ap->minlen) {
3845 args.minlen = ap->minlen;
3846 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003847 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003848 if ((error = xfs_alloc_vextent(&args)))
3849 return error;
3850 }
3851 if (args.fsbno == NULLFSBLOCK && nullfb) {
3852 args.fsbno = 0;
3853 args.type = XFS_ALLOCTYPE_FIRST_AG;
3854 args.total = ap->minlen;
3855 args.minleft = 0;
3856 if ((error = xfs_alloc_vextent(&args)))
3857 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003858 ap->flist->xbf_low = 1;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003859 }
3860 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003861 /*
3862 * check the allocation happened at the same or higher AG than
3863 * the first block that was allocated.
3864 */
3865 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3866 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3867 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
3868 (ap->flist->xbf_low &&
3869 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3870 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3871
Dave Chinner3a756672011-09-18 20:40:58 +00003872 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003873 if (*ap->firstblock == NULLFSBLOCK)
3874 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003875 ASSERT(nullfb || fb_agno == args.agno ||
Dave Chinner0937e0f2011-09-18 20:40:57 +00003876 (ap->flist->xbf_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003877 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003878 ap->ip->i_d.di_nblocks += args.len;
3879 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3880 if (ap->wasdel)
3881 ap->ip->i_delayed_blks -= args.len;
3882 /*
3883 * Adjust the disk quota also. This was reserved
3884 * earlier.
3885 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003886 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003887 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3888 XFS_TRANS_DQ_BCOUNT,
3889 (long) args.len);
3890 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003891 ap->blkno = NULLFSBLOCK;
3892 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 }
3894 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003895}
3896
3897/*
3898 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3899 * It figures out where to ask the underlying allocator to put the new extent.
3900 */
3901STATIC int
3902xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003903 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003904{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003905 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003906 return xfs_bmap_rtalloc(ap);
3907 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908}
3909
3910/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003911 * Trim the returned map to the required bounds
3912 */
3913STATIC void
3914xfs_bmapi_trim_map(
3915 struct xfs_bmbt_irec *mval,
3916 struct xfs_bmbt_irec *got,
3917 xfs_fileoff_t *bno,
3918 xfs_filblks_t len,
3919 xfs_fileoff_t obno,
3920 xfs_fileoff_t end,
3921 int n,
3922 int flags)
3923{
3924 if ((flags & XFS_BMAPI_ENTIRE) ||
3925 got->br_startoff + got->br_blockcount <= obno) {
3926 *mval = *got;
3927 if (isnullstartblock(got->br_startblock))
3928 mval->br_startblock = DELAYSTARTBLOCK;
3929 return;
3930 }
3931
3932 if (obno > *bno)
3933 *bno = obno;
3934 ASSERT((*bno >= obno) || (n == 0));
3935 ASSERT(*bno < end);
3936 mval->br_startoff = *bno;
3937 if (isnullstartblock(got->br_startblock))
3938 mval->br_startblock = DELAYSTARTBLOCK;
3939 else
3940 mval->br_startblock = got->br_startblock +
3941 (*bno - got->br_startoff);
3942 /*
3943 * Return the minimum of what we got and what we asked for for
3944 * the length. We can use the len variable here because it is
3945 * modified below and we could have been there before coming
3946 * here if the first part of the allocation didn't overlap what
3947 * was asked for.
3948 */
3949 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3950 got->br_blockcount - (*bno - got->br_startoff));
3951 mval->br_state = got->br_state;
3952 ASSERT(mval->br_blockcount <= len);
3953 return;
3954}
3955
3956/*
3957 * Update and validate the extent map to return
3958 */
3959STATIC void
3960xfs_bmapi_update_map(
3961 struct xfs_bmbt_irec **map,
3962 xfs_fileoff_t *bno,
3963 xfs_filblks_t *len,
3964 xfs_fileoff_t obno,
3965 xfs_fileoff_t end,
3966 int *n,
3967 int flags)
3968{
3969 xfs_bmbt_irec_t *mval = *map;
3970
3971 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3972 ((mval->br_startoff + mval->br_blockcount) <= end));
3973 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3974 (mval->br_startoff < obno));
3975
3976 *bno = mval->br_startoff + mval->br_blockcount;
3977 *len = end - *bno;
3978 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3979 /* update previous map with new information */
3980 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3981 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3982 ASSERT(mval->br_state == mval[-1].br_state);
3983 mval[-1].br_blockcount = mval->br_blockcount;
3984 mval[-1].br_state = mval->br_state;
3985 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3986 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3987 mval[-1].br_startblock != HOLESTARTBLOCK &&
3988 mval->br_startblock == mval[-1].br_startblock +
3989 mval[-1].br_blockcount &&
3990 ((flags & XFS_BMAPI_IGSTATE) ||
3991 mval[-1].br_state == mval->br_state)) {
3992 ASSERT(mval->br_startoff ==
3993 mval[-1].br_startoff + mval[-1].br_blockcount);
3994 mval[-1].br_blockcount += mval->br_blockcount;
3995 } else if (*n > 0 &&
3996 mval->br_startblock == DELAYSTARTBLOCK &&
3997 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3998 mval->br_startoff ==
3999 mval[-1].br_startoff + mval[-1].br_blockcount) {
4000 mval[-1].br_blockcount += mval->br_blockcount;
4001 mval[-1].br_state = mval->br_state;
4002 } else if (!((*n == 0) &&
4003 ((mval->br_startoff + mval->br_blockcount) <=
4004 obno))) {
4005 mval++;
4006 (*n)++;
4007 }
4008 *map = mval;
4009}
4010
4011/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004012 * Map file blocks to filesystem blocks without allocation.
4013 */
4014int
4015xfs_bmapi_read(
4016 struct xfs_inode *ip,
4017 xfs_fileoff_t bno,
4018 xfs_filblks_t len,
4019 struct xfs_bmbt_irec *mval,
4020 int *nmap,
4021 int flags)
4022{
4023 struct xfs_mount *mp = ip->i_mount;
4024 struct xfs_ifork *ifp;
4025 struct xfs_bmbt_irec got;
4026 struct xfs_bmbt_irec prev;
4027 xfs_fileoff_t obno;
4028 xfs_fileoff_t end;
4029 xfs_extnum_t lastx;
4030 int error;
4031 int eof;
4032 int n = 0;
4033 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4034 XFS_ATTR_FORK : XFS_DATA_FORK;
4035
4036 ASSERT(*nmap >= 1);
4037 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4038 XFS_BMAPI_IGSTATE)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004039 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004040
4041 if (unlikely(XFS_TEST_ERROR(
4042 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4043 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4044 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4045 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004046 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004047 }
4048
4049 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004050 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004051
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004052 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004053
4054 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004055
4056 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4057 error = xfs_iread_extents(NULL, ip, whichfork);
4058 if (error)
4059 return error;
4060 }
4061
4062 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4063 end = bno + len;
4064 obno = bno;
4065
4066 while (bno < end && n < *nmap) {
4067 /* Reading past eof, act as though there's a hole up to end. */
4068 if (eof)
4069 got.br_startoff = end;
4070 if (got.br_startoff > bno) {
4071 /* Reading in a hole. */
4072 mval->br_startoff = bno;
4073 mval->br_startblock = HOLESTARTBLOCK;
4074 mval->br_blockcount =
4075 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4076 mval->br_state = XFS_EXT_NORM;
4077 bno += mval->br_blockcount;
4078 len -= mval->br_blockcount;
4079 mval++;
4080 n++;
4081 continue;
4082 }
4083
4084 /* set up the extent map to return. */
4085 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4086 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4087
4088 /* If we're done, stop now. */
4089 if (bno >= end || n >= *nmap)
4090 break;
4091
4092 /* Else go on to the next record. */
4093 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4094 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4095 else
4096 eof = 1;
4097 }
4098 *nmap = n;
4099 return 0;
4100}
4101
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004102STATIC int
4103xfs_bmapi_reserve_delalloc(
4104 struct xfs_inode *ip,
4105 xfs_fileoff_t aoff,
4106 xfs_filblks_t len,
4107 struct xfs_bmbt_irec *got,
4108 struct xfs_bmbt_irec *prev,
4109 xfs_extnum_t *lastx,
4110 int eof)
4111{
4112 struct xfs_mount *mp = ip->i_mount;
4113 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4114 xfs_extlen_t alen;
4115 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004116 char rt = XFS_IS_REALTIME_INODE(ip);
4117 xfs_extlen_t extsz;
4118 int error;
4119
4120 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4121 if (!eof)
4122 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4123
4124 /* Figure out the extent size, adjust alen */
4125 extsz = xfs_get_extsz_hint(ip);
4126 if (extsz) {
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004127 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4128 1, 0, &aoff, &alen);
4129 ASSERT(!error);
4130 }
4131
4132 if (rt)
4133 extsz = alen / mp->m_sb.sb_rextsize;
4134
4135 /*
4136 * Make a transaction-less quota reservation for delayed allocation
4137 * blocks. This number gets adjusted later. We return if we haven't
4138 * allocated blocks already inside this loop.
4139 */
4140 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4141 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4142 if (error)
4143 return error;
4144
4145 /*
4146 * Split changing sb for alen and indlen since they could be coming
4147 * from different places.
4148 */
4149 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4150 ASSERT(indlen > 0);
4151
4152 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004153 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004154 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004155 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004156 }
4157
4158 if (error)
4159 goto out_unreserve_quota;
4160
Dave Chinner0d485ad2015-02-23 21:22:03 +11004161 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004162 if (error)
4163 goto out_unreserve_blocks;
4164
4165
4166 ip->i_delayed_blks += alen;
4167
4168 got->br_startoff = aoff;
4169 got->br_startblock = nullstartblock(indlen);
4170 got->br_blockcount = alen;
4171 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004172 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004173
4174 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004175 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4176 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004177 */
4178 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4179
4180 ASSERT(got->br_startoff <= aoff);
4181 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4182 ASSERT(isnullstartblock(got->br_startblock));
4183 ASSERT(got->br_state == XFS_EXT_NORM);
4184 return 0;
4185
4186out_unreserve_blocks:
4187 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004188 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004189 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004190 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004191out_unreserve_quota:
4192 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004193 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004194 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4195 return error;
4196}
4197
Dave Chinner5c8ed202011-09-18 20:40:45 +00004198/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004199 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4200 */
4201int
4202xfs_bmapi_delay(
4203 struct xfs_inode *ip, /* incore inode */
4204 xfs_fileoff_t bno, /* starting file offs. mapped */
4205 xfs_filblks_t len, /* length to map in file */
4206 struct xfs_bmbt_irec *mval, /* output: map values */
4207 int *nmap, /* i/o: mval size/count */
4208 int flags) /* XFS_BMAPI_... */
4209{
4210 struct xfs_mount *mp = ip->i_mount;
4211 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4212 struct xfs_bmbt_irec got; /* current file extent record */
4213 struct xfs_bmbt_irec prev; /* previous file extent record */
4214 xfs_fileoff_t obno; /* old block number (offset) */
4215 xfs_fileoff_t end; /* end of mapped file region */
4216 xfs_extnum_t lastx; /* last useful extent number */
4217 int eof; /* we've hit the end of extents */
4218 int n = 0; /* current extent index */
4219 int error = 0;
4220
4221 ASSERT(*nmap >= 1);
4222 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4223 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004224 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig44032802011-09-18 20:40:48 +00004225
4226 if (unlikely(XFS_TEST_ERROR(
4227 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4228 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4229 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4230 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004231 return -EFSCORRUPTED;
Christoph Hellwig44032802011-09-18 20:40:48 +00004232 }
4233
4234 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004235 return -EIO;
Christoph Hellwig44032802011-09-18 20:40:48 +00004236
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004237 XFS_STATS_INC(mp, xs_blk_mapw);
Christoph Hellwig44032802011-09-18 20:40:48 +00004238
4239 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4240 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4241 if (error)
4242 return error;
4243 }
4244
4245 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4246 end = bno + len;
4247 obno = bno;
4248
4249 while (bno < end && n < *nmap) {
4250 if (eof || got.br_startoff > bno) {
4251 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4252 &prev, &lastx, eof);
4253 if (error) {
4254 if (n == 0) {
4255 *nmap = 0;
4256 return error;
4257 }
4258 break;
4259 }
4260 }
4261
4262 /* set up the extent map to return. */
4263 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4264 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4265
4266 /* If we're done, stop now. */
4267 if (bno >= end || n >= *nmap)
4268 break;
4269
4270 /* Else go on to the next record. */
4271 prev = got;
4272 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4273 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4274 else
4275 eof = 1;
4276 }
4277
4278 *nmap = n;
4279 return 0;
4280}
4281
4282
Dave Chinnercf11da92014-07-15 07:08:24 +10004283static int
4284xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004285 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004286{
4287 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004288 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004289 XFS_ATTR_FORK : XFS_DATA_FORK;
4290 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004291 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004292 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004293
Dave Chinnera99ebf42011-12-01 11:24:20 +00004294 ASSERT(bma->length > 0);
4295
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004296 /*
4297 * For the wasdelay case, we could also just allocate the stuff asked
4298 * for in this bmap call but that wouldn't be as good.
4299 */
4300 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004301 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4302 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004303 if (bma->idx != NULLEXTNUM && bma->idx) {
4304 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004305 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004306 }
4307 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004308 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004309 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004310 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004311 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004312 }
4313
4314 /*
4315 * Indicate if this is the first user data in the file, or just any
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004316 * user data. And if it is userdata, indicate whether it needs to
4317 * be initialised to zero during allocation.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004318 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004319 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004320 bma->userdata = (bma->offset == 0) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004321 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004322 if (bma->flags & XFS_BMAPI_ZERO)
4323 bma->userdata |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004324 }
4325
Dave Chinnere04426b2012-10-05 11:06:59 +10004326 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004327
4328 /*
4329 * Only want to do the alignment at the eof if it is userdata and
4330 * allocation length is larger than a stripe unit.
4331 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004332 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004333 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004334 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004335 if (error)
4336 return error;
4337 }
4338
4339 error = xfs_bmap_alloc(bma);
4340 if (error)
4341 return error;
4342
Dave Chinner0937e0f2011-09-18 20:40:57 +00004343 if (bma->flist->xbf_low)
4344 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004345 if (bma->cur)
4346 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004347 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004348 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004349 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4350 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4351 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4352 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004353 }
4354 /*
4355 * Bump the number of extents we've allocated
4356 * in this call.
4357 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004358 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004359
Dave Chinner29c8d172011-09-18 20:41:00 +00004360 if (bma->cur)
4361 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004362 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4363
Dave Chinner963c30c2011-09-18 20:40:59 +00004364 bma->got.br_startoff = bma->offset;
4365 bma->got.br_startblock = bma->blkno;
4366 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004367 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004368
4369 /*
4370 * A wasdelay extent has been initialized, so shouldn't be flagged
4371 * as unwritten.
4372 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004373 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004374 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004375 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004376
Christoph Hellwigc6534242011-09-18 20:41:05 +00004377 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004378 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004379 else
4380 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004381
Christoph Hellwigc315c902011-09-18 20:41:02 +00004382 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004383 if (error)
4384 return error;
4385
4386 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004387 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4388 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4389 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004390 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004391 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004392
Dave Chinner963c30c2011-09-18 20:40:59 +00004393 ASSERT(bma->got.br_startoff <= bma->offset);
4394 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4395 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004396 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4397 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004398 return 0;
4399}
4400
Dave Chinnerb447fe52011-09-18 20:40:51 +00004401STATIC int
4402xfs_bmapi_convert_unwritten(
4403 struct xfs_bmalloca *bma,
4404 struct xfs_bmbt_irec *mval,
4405 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004406 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004407{
4408 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4409 XFS_ATTR_FORK : XFS_DATA_FORK;
4410 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004411 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004412 int error;
4413
Dave Chinnerb447fe52011-09-18 20:40:51 +00004414 /* check if we need to do unwritten->real conversion */
4415 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4416 (flags & XFS_BMAPI_PREALLOC))
4417 return 0;
4418
4419 /* check if we need to do real->unwritten conversion */
4420 if (mval->br_state == XFS_EXT_NORM &&
4421 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4422 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4423 return 0;
4424
4425 /*
4426 * Modify (by adding) the state flag, if writing.
4427 */
4428 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004429 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4430 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004431 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004432 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4433 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004434 }
4435 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4436 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4437
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004438 /*
4439 * Before insertion into the bmbt, zero the range being converted
4440 * if required.
4441 */
4442 if (flags & XFS_BMAPI_ZERO) {
4443 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4444 mval->br_blockcount);
4445 if (error)
4446 return error;
4447 }
4448
Dave Chinnere0c3da52011-09-18 20:41:01 +00004449 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004450 &bma->cur, mval, bma->firstblock, bma->flist,
4451 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004452 /*
4453 * Log the inode core unconditionally in the unwritten extent conversion
4454 * path because the conversion might not have done so (e.g., if the
4455 * extent count hasn't changed). We need to make sure the inode is dirty
4456 * in the transaction for the sake of fsync(), even if nothing has
4457 * changed, because fsync() will not force the log for this transaction
4458 * unless it sees the inode pinned.
4459 */
4460 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004461 if (error)
4462 return error;
4463
4464 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004465 * Update our extent pointer, given that
4466 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4467 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004468 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004469 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004470
4471 /*
4472 * We may have combined previously unwritten space with written space,
4473 * so generate another request.
4474 */
4475 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004476 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004477 return 0;
4478}
4479
Christoph Hellwig44032802011-09-18 20:40:48 +00004480/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004481 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4482 * extent state if necessary. Details behaviour is controlled by the flags
4483 * parameter. Only allocates blocks from a single allocation group, to avoid
4484 * locking problems.
4485 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 * The returned value in "firstblock" from the first call in a transaction
4487 * must be remembered and presented to subsequent calls in "firstblock".
4488 * An upper bound for the number of blocks to be allocated is supplied to
4489 * the first call in "total"; if no allocation group has that many free
4490 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4491 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004492int
4493xfs_bmapi_write(
4494 struct xfs_trans *tp, /* transaction pointer */
4495 struct xfs_inode *ip, /* incore inode */
4496 xfs_fileoff_t bno, /* starting file offs. mapped */
4497 xfs_filblks_t len, /* length to map in file */
4498 int flags, /* XFS_BMAPI_... */
4499 xfs_fsblock_t *firstblock, /* first allocated block
4500 controls a.g. for allocs */
4501 xfs_extlen_t total, /* total blocks needed */
4502 struct xfs_bmbt_irec *mval, /* output: map values */
4503 int *nmap, /* i/o: mval size/count */
4504 struct xfs_bmap_free *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004506 struct xfs_mount *mp = ip->i_mount;
4507 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004508 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004509 xfs_fileoff_t end; /* end of mapped file region */
4510 int eof; /* after the end of extents */
4511 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004512 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004513 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004514 int whichfork; /* data or attr fork */
4515 char inhole; /* current location is hole in file */
4516 char wasdelay; /* old extent was delayed */
4517
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004519 xfs_fileoff_t orig_bno; /* original block number value */
4520 int orig_flags; /* original flags arg value */
4521 xfs_filblks_t orig_len; /* original value of len arg */
4522 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4523 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
4525 orig_bno = bno;
4526 orig_len = len;
4527 orig_flags = flags;
4528 orig_mval = mval;
4529 orig_nmap = *nmap;
4530#endif
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004531 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4532 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004533
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004535 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4536 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4537 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004538 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004539 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004540 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004541
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004542 /* zeroing is for currently only for data extents, not metadata */
4543 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4544 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4545 /*
4546 * we can allocate unwritten extents or pre-zero allocated blocks,
4547 * but it makes no sense to do both at once. This would result in
4548 * zeroing the unwritten extent twice, but it still being an
4549 * unwritten extent....
4550 */
4551 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4552 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4553
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 if (unlikely(XFS_TEST_ERROR(
4555 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004556 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004558 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004559 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004563 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004564
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004566
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004567 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004568
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004569 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004571 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004573 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004574 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004575 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004576 }
4577
4578 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4579 error = xfs_iread_extents(tp, ip, whichfork);
4580 if (error)
4581 goto error0;
4582 }
4583
Dave Chinnere0c3da52011-09-18 20:41:01 +00004584 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004585 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 n = 0;
4587 end = bno + len;
4588 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004589
4590 bma.tp = tp;
4591 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004592 bma.total = total;
4593 bma.userdata = 0;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004594 bma.flist = flist;
4595 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004596
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004598 inhole = eof || bma.got.br_startoff > bno;
4599 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 /*
4602 * First, deal with the hole before the allocated space
4603 * that we found, if any.
4604 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004605 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004606 bma.eof = eof;
4607 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4608 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004609 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004610 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004611
Dave Chinnera99ebf42011-12-01 11:24:20 +00004612 /*
4613 * There's a 32/64 bit type mismatch between the
4614 * allocation length request (which can be 64 bits in
4615 * length) and the bma length request, which is
4616 * xfs_extlen_t and therefore 32 bits. Hence we have to
4617 * check for 32-bit overflows and handle them here.
4618 */
4619 if (len > (xfs_filblks_t)MAXEXTLEN)
4620 bma.length = MAXEXTLEN;
4621 else
4622 bma.length = len;
4623
4624 ASSERT(len > 0);
4625 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004626 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 if (error)
4628 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004629 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004630 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004632
Dave Chinneraef9a892011-09-18 20:40:44 +00004633 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004634 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4635 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
Dave Chinnerb447fe52011-09-18 20:40:51 +00004637 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004638 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004639 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004640 continue;
4641 if (error)
4642 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643
Dave Chinneraef9a892011-09-18 20:40:44 +00004644 /* update the extent map to return */
4645 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4646
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 /*
4648 * If we're done, stop now. Stop when we've allocated
4649 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4650 * the transaction may get too big.
4651 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004652 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004654
4655 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004656 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004657 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4658 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4659 &bma.got);
4660 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00004661 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004664
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 /*
4666 * Transform from btree to extents, give it cur.
4667 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004668 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004669 int tmp_logflags = 0;
4670
Dave Chinner29c8d172011-09-18 20:41:00 +00004671 ASSERT(bma.cur);
4672 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004674 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675 if (error)
4676 goto error0;
4677 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004678
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004680 XFS_IFORK_NEXTENTS(ip, whichfork) >
4681 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683error0:
4684 /*
4685 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004686 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004688 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004690 bma.logflags &= ~xfs_ilog_fext(whichfork);
4691 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004693 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 /*
4695 * Log whatever the flags say, even if error. Otherwise we might miss
4696 * detecting a case where the data is changed, there's an error,
4697 * and it's not logged so we don't shutdown when we should.
4698 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004699 if (bma.logflags)
4700 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004701
Dave Chinner29c8d172011-09-18 20:41:00 +00004702 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 if (!error) {
4704 ASSERT(*firstblock == NULLFSBLOCK ||
4705 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4706 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004707 bma.cur->bc_private.b.firstblock) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 (flist->xbf_low &&
4709 XFS_FSB_TO_AGNO(mp, *firstblock) <
4710 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004711 bma.cur->bc_private.b.firstblock)));
4712 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004714 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4716 }
4717 if (!error)
4718 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4719 orig_nmap, *nmap);
4720 return error;
4721}
4722
4723/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004724 * Called by xfs_bmapi to update file extent records and the btree
4725 * after removing space (or undoing a delayed allocation).
4726 */
4727STATIC int /* error */
4728xfs_bmap_del_extent(
4729 xfs_inode_t *ip, /* incore inode pointer */
4730 xfs_trans_t *tp, /* current transaction pointer */
4731 xfs_extnum_t *idx, /* extent number to update/delete */
4732 xfs_bmap_free_t *flist, /* list of extents to be freed */
4733 xfs_btree_cur_t *cur, /* if null, not a btree */
4734 xfs_bmbt_irec_t *del, /* data to remove from extents */
4735 int *logflagsp, /* inode logging flags */
4736 int whichfork) /* data or attr fork */
4737{
4738 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
4739 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
4740 xfs_fsblock_t del_endblock=0; /* first block past del */
4741 xfs_fileoff_t del_endoff; /* first offset past del */
4742 int delay; /* current block is delayed allocated */
4743 int do_fx; /* free extent at end of routine */
4744 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
4745 int error; /* error return value */
4746 int flags; /* inode logging flags */
4747 xfs_bmbt_irec_t got; /* current extent entry */
4748 xfs_fileoff_t got_endoff; /* first offset past got */
4749 int i; /* temp state */
4750 xfs_ifork_t *ifp; /* inode fork pointer */
4751 xfs_mount_t *mp; /* mount structure */
4752 xfs_filblks_t nblks; /* quota/sb block count */
4753 xfs_bmbt_irec_t new; /* new record to be inserted */
4754 /* REFERENCED */
4755 uint qfield; /* quota field to update */
4756 xfs_filblks_t temp; /* for indirect length calculations */
4757 xfs_filblks_t temp2; /* for indirect length calculations */
4758 int state = 0;
4759
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004760 mp = ip->i_mount;
4761 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004762
4763 if (whichfork == XFS_ATTR_FORK)
4764 state |= BMAP_ATTRFORK;
4765
Dave Chinner9e5987a2013-02-25 12:31:26 +11004766 ifp = XFS_IFORK_PTR(ip, whichfork);
4767 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4768 (uint)sizeof(xfs_bmbt_rec_t)));
4769 ASSERT(del->br_blockcount > 0);
4770 ep = xfs_iext_get_ext(ifp, *idx);
4771 xfs_bmbt_get_all(ep, &got);
4772 ASSERT(got.br_startoff <= del->br_startoff);
4773 del_endoff = del->br_startoff + del->br_blockcount;
4774 got_endoff = got.br_startoff + got.br_blockcount;
4775 ASSERT(got_endoff >= del_endoff);
4776 delay = isnullstartblock(got.br_startblock);
4777 ASSERT(isnullstartblock(del->br_startblock) == delay);
4778 flags = 0;
4779 qfield = 0;
4780 error = 0;
4781 /*
4782 * If deleting a real allocation, must free up the disk space.
4783 */
4784 if (!delay) {
4785 flags = XFS_ILOG_CORE;
4786 /*
4787 * Realtime allocation. Free it and record di_nblocks update.
4788 */
4789 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4790 xfs_fsblock_t bno;
4791 xfs_filblks_t len;
4792
4793 ASSERT(do_mod(del->br_blockcount,
4794 mp->m_sb.sb_rextsize) == 0);
4795 ASSERT(do_mod(del->br_startblock,
4796 mp->m_sb.sb_rextsize) == 0);
4797 bno = del->br_startblock;
4798 len = del->br_blockcount;
4799 do_div(bno, mp->m_sb.sb_rextsize);
4800 do_div(len, mp->m_sb.sb_rextsize);
4801 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4802 if (error)
4803 goto done;
4804 do_fx = 0;
4805 nblks = len * mp->m_sb.sb_rextsize;
4806 qfield = XFS_TRANS_DQ_RTBCOUNT;
4807 }
4808 /*
4809 * Ordinary allocation.
4810 */
4811 else {
4812 do_fx = 1;
4813 nblks = del->br_blockcount;
4814 qfield = XFS_TRANS_DQ_BCOUNT;
4815 }
4816 /*
4817 * Set up del_endblock and cur for later.
4818 */
4819 del_endblock = del->br_startblock + del->br_blockcount;
4820 if (cur) {
4821 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
4822 got.br_startblock, got.br_blockcount,
4823 &i)))
4824 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004825 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004826 }
4827 da_old = da_new = 0;
4828 } else {
4829 da_old = startblockval(got.br_startblock);
4830 da_new = 0;
4831 nblks = 0;
4832 do_fx = 0;
4833 }
4834 /*
4835 * Set flag value to use in switch statement.
4836 * Left-contig is 2, right-contig is 1.
4837 */
4838 switch (((got.br_startoff == del->br_startoff) << 1) |
4839 (got_endoff == del_endoff)) {
4840 case 3:
4841 /*
4842 * Matches the whole extent. Delete the entry.
4843 */
4844 xfs_iext_remove(ip, *idx, 1,
4845 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
4846 --*idx;
4847 if (delay)
4848 break;
4849
4850 XFS_IFORK_NEXT_SET(ip, whichfork,
4851 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4852 flags |= XFS_ILOG_CORE;
4853 if (!cur) {
4854 flags |= xfs_ilog_fext(whichfork);
4855 break;
4856 }
4857 if ((error = xfs_btree_delete(cur, &i)))
4858 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004859 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004860 break;
4861
4862 case 2:
4863 /*
4864 * Deleting the first part of the extent.
4865 */
4866 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4867 xfs_bmbt_set_startoff(ep, del_endoff);
4868 temp = got.br_blockcount - del->br_blockcount;
4869 xfs_bmbt_set_blockcount(ep, temp);
4870 if (delay) {
4871 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4872 da_old);
4873 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4874 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4875 da_new = temp;
4876 break;
4877 }
4878 xfs_bmbt_set_startblock(ep, del_endblock);
4879 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4880 if (!cur) {
4881 flags |= xfs_ilog_fext(whichfork);
4882 break;
4883 }
4884 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
4885 got.br_blockcount - del->br_blockcount,
4886 got.br_state)))
4887 goto done;
4888 break;
4889
4890 case 1:
4891 /*
4892 * Deleting the last part of the extent.
4893 */
4894 temp = got.br_blockcount - del->br_blockcount;
4895 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4896 xfs_bmbt_set_blockcount(ep, temp);
4897 if (delay) {
4898 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4899 da_old);
4900 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4901 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4902 da_new = temp;
4903 break;
4904 }
4905 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4906 if (!cur) {
4907 flags |= xfs_ilog_fext(whichfork);
4908 break;
4909 }
4910 if ((error = xfs_bmbt_update(cur, got.br_startoff,
4911 got.br_startblock,
4912 got.br_blockcount - del->br_blockcount,
4913 got.br_state)))
4914 goto done;
4915 break;
4916
4917 case 0:
4918 /*
4919 * Deleting the middle of the extent.
4920 */
4921 temp = del->br_startoff - got.br_startoff;
4922 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4923 xfs_bmbt_set_blockcount(ep, temp);
4924 new.br_startoff = del_endoff;
4925 temp2 = got_endoff - del_endoff;
4926 new.br_blockcount = temp2;
4927 new.br_state = got.br_state;
4928 if (!delay) {
4929 new.br_startblock = del_endblock;
4930 flags |= XFS_ILOG_CORE;
4931 if (cur) {
4932 if ((error = xfs_bmbt_update(cur,
4933 got.br_startoff,
4934 got.br_startblock, temp,
4935 got.br_state)))
4936 goto done;
4937 if ((error = xfs_btree_increment(cur, 0, &i)))
4938 goto done;
4939 cur->bc_rec.b = new;
4940 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10004941 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004942 goto done;
4943 /*
4944 * If get no-space back from btree insert,
4945 * it tried a split, and we have a zero
4946 * block reservation.
4947 * Fix up our state and return the error.
4948 */
Dave Chinner24513372014-06-25 14:58:08 +10004949 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11004950 /*
4951 * Reset the cursor, don't trust
4952 * it after any insert operation.
4953 */
4954 if ((error = xfs_bmbt_lookup_eq(cur,
4955 got.br_startoff,
4956 got.br_startblock,
4957 temp, &i)))
4958 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004959 XFS_WANT_CORRUPTED_GOTO(mp,
4960 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004961 /*
4962 * Update the btree record back
4963 * to the original value.
4964 */
4965 if ((error = xfs_bmbt_update(cur,
4966 got.br_startoff,
4967 got.br_startblock,
4968 got.br_blockcount,
4969 got.br_state)))
4970 goto done;
4971 /*
4972 * Reset the extent record back
4973 * to the original value.
4974 */
4975 xfs_bmbt_set_blockcount(ep,
4976 got.br_blockcount);
4977 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10004978 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004979 goto done;
4980 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11004981 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004982 } else
4983 flags |= xfs_ilog_fext(whichfork);
4984 XFS_IFORK_NEXT_SET(ip, whichfork,
4985 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
4986 } else {
4987 ASSERT(whichfork == XFS_DATA_FORK);
4988 temp = xfs_bmap_worst_indlen(ip, temp);
4989 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4990 temp2 = xfs_bmap_worst_indlen(ip, temp2);
4991 new.br_startblock = nullstartblock((int)temp2);
4992 da_new = temp + temp2;
4993 while (da_new > da_old) {
4994 if (temp) {
4995 temp--;
4996 da_new--;
4997 xfs_bmbt_set_startblock(ep,
4998 nullstartblock((int)temp));
4999 }
5000 if (da_new == da_old)
5001 break;
5002 if (temp2) {
5003 temp2--;
5004 da_new--;
5005 new.br_startblock =
5006 nullstartblock((int)temp2);
5007 }
5008 }
5009 }
5010 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5011 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5012 ++*idx;
5013 break;
5014 }
5015 /*
5016 * If we need to, add to list of extents to delete.
5017 */
5018 if (do_fx)
5019 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
5020 mp);
5021 /*
5022 * Adjust inode # blocks in the file.
5023 */
5024 if (nblks)
5025 ip->i_d.di_nblocks -= nblks;
5026 /*
5027 * Adjust quota data.
5028 */
5029 if (qfield)
5030 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5031
5032 /*
5033 * Account for change in delayed indirect blocks.
5034 * Nothing to do for disk quota accounting here.
5035 */
5036 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005037 if (da_old > da_new)
5038 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005039done:
5040 *logflagsp = flags;
5041 return error;
5042}
5043
5044/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 * Unmap (remove) blocks from a file.
5046 * If nexts is nonzero then the number of extents to remove is limited to
5047 * that value. If not all extents in the block range can be removed then
5048 * *done is set.
5049 */
5050int /* error */
5051xfs_bunmapi(
5052 xfs_trans_t *tp, /* transaction pointer */
5053 struct xfs_inode *ip, /* incore inode */
5054 xfs_fileoff_t bno, /* starting offset to unmap */
5055 xfs_filblks_t len, /* length to unmap in file */
5056 int flags, /* misc flags */
5057 xfs_extnum_t nexts, /* number of extents max */
5058 xfs_fsblock_t *firstblock, /* first allocated block
5059 controls a.g. for allocs */
5060 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5061 int *done) /* set if not done yet */
5062{
5063 xfs_btree_cur_t *cur; /* bmap btree cursor */
5064 xfs_bmbt_irec_t del; /* extent being deleted */
5065 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005066 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 int error; /* error return value */
5068 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005069 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070 xfs_ifork_t *ifp; /* inode fork pointer */
5071 int isrt; /* freeing in rt area */
5072 xfs_extnum_t lastx; /* last extent index used */
5073 int logflags; /* transaction logging flags */
5074 xfs_extlen_t mod; /* rt extent offset */
5075 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005076 xfs_extnum_t nextents; /* number of file extents */
5077 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 xfs_fileoff_t start; /* first file offset deleted */
5079 int tmp_logflags; /* partial logging flags */
5080 int wasdel; /* was a delayed alloc extent */
5081 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 xfs_fsblock_t sum;
5083
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005084 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5085
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5087 XFS_ATTR_FORK : XFS_DATA_FORK;
5088 ifp = XFS_IFORK_PTR(ip, whichfork);
5089 if (unlikely(
5090 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5091 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5092 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5093 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005094 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 }
5096 mp = ip->i_mount;
5097 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005098 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005099
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005100 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 ASSERT(len > 0);
5102 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005103
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5105 (error = xfs_iread_extents(tp, ip, whichfork)))
5106 return error;
5107 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5108 if (nextents == 0) {
5109 *done = 1;
5110 return 0;
5111 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005112 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005113 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 start = bno;
5115 bno = start + len - 1;
5116 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5117 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005118
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119 /*
5120 * Check to see if the given block number is past the end of the
5121 * file, back up to the last block if so...
5122 */
5123 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005124 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 xfs_bmbt_get_all(ep, &got);
5126 bno = got.br_startoff + got.br_blockcount - 1;
5127 }
5128 logflags = 0;
5129 if (ifp->if_flags & XFS_IFBROOT) {
5130 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005131 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 cur->bc_private.b.firstblock = *firstblock;
5133 cur->bc_private.b.flist = flist;
5134 cur->bc_private.b.flags = 0;
5135 } else
5136 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005137
5138 if (isrt) {
5139 /*
5140 * Synchronize by locking the bitmap inode.
5141 */
5142 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5143 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5144 }
5145
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 extno = 0;
5147 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5148 (nexts == 0 || extno < nexts)) {
5149 /*
5150 * Is the found extent after a hole in which bno lives?
5151 * Just back up to the previous extent, if so.
5152 */
5153 if (got.br_startoff > bno) {
5154 if (--lastx < 0)
5155 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005156 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157 xfs_bmbt_get_all(ep, &got);
5158 }
5159 /*
5160 * Is the last block of this extent before the range
5161 * we're supposed to delete? If so, we're done.
5162 */
5163 bno = XFS_FILEOFF_MIN(bno,
5164 got.br_startoff + got.br_blockcount - 1);
5165 if (bno < start)
5166 break;
5167 /*
5168 * Then deal with the (possibly delayed) allocated space
5169 * we found.
5170 */
5171 ASSERT(ep != NULL);
5172 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005173 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005174 if (got.br_startoff < start) {
5175 del.br_startoff = start;
5176 del.br_blockcount -= start - got.br_startoff;
5177 if (!wasdel)
5178 del.br_startblock += start - got.br_startoff;
5179 }
5180 if (del.br_startoff + del.br_blockcount > bno + 1)
5181 del.br_blockcount = bno + 1 - del.br_startoff;
5182 sum = del.br_startblock + del.br_blockcount;
5183 if (isrt &&
5184 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5185 /*
5186 * Realtime extent not lined up at the end.
5187 * The extent could have been split into written
5188 * and unwritten pieces, or we could just be
5189 * unmapping part of it. But we can't really
5190 * get rid of part of a realtime extent.
5191 */
5192 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005193 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 /*
5195 * This piece is unwritten, or we're not
5196 * using unwritten extents. Skip over it.
5197 */
5198 ASSERT(bno >= mod);
5199 bno -= mod > del.br_blockcount ?
5200 del.br_blockcount : mod;
5201 if (bno < got.br_startoff) {
5202 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005203 xfs_bmbt_get_all(xfs_iext_get_ext(
5204 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 }
5206 continue;
5207 }
5208 /*
5209 * It's written, turn it unwritten.
5210 * This is better than zeroing it.
5211 */
5212 ASSERT(del.br_state == XFS_EXT_NORM);
5213 ASSERT(xfs_trans_get_block_res(tp) > 0);
5214 /*
5215 * If this spans a realtime extent boundary,
5216 * chop it back to the start of the one we end at.
5217 */
5218 if (del.br_blockcount > mod) {
5219 del.br_startoff += del.br_blockcount - mod;
5220 del.br_startblock += del.br_blockcount - mod;
5221 del.br_blockcount = mod;
5222 }
5223 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005224 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5225 &lastx, &cur, &del, firstblock, flist,
5226 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 if (error)
5228 goto error0;
5229 goto nodelete;
5230 }
5231 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5232 /*
5233 * Realtime extent is lined up at the end but not
5234 * at the front. We'll get rid of full extents if
5235 * we can.
5236 */
5237 mod = mp->m_sb.sb_rextsize - mod;
5238 if (del.br_blockcount > mod) {
5239 del.br_blockcount -= mod;
5240 del.br_startoff += mod;
5241 del.br_startblock += mod;
5242 } else if ((del.br_startoff == start &&
5243 (del.br_state == XFS_EXT_UNWRITTEN ||
5244 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005245 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 /*
5247 * Can't make it unwritten. There isn't
5248 * a full extent here so just skip it.
5249 */
5250 ASSERT(bno >= del.br_blockcount);
5251 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005252 if (got.br_startoff > bno) {
5253 if (--lastx >= 0) {
5254 ep = xfs_iext_get_ext(ifp,
5255 lastx);
5256 xfs_bmbt_get_all(ep, &got);
5257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 }
5259 continue;
5260 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5261 /*
5262 * This one is already unwritten.
5263 * It must have a written left neighbor.
5264 * Unwrite the killed part of that one and
5265 * try again.
5266 */
5267 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005268 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5269 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005270 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005271 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 ASSERT(del.br_startblock ==
5273 prev.br_startblock + prev.br_blockcount);
5274 if (prev.br_startoff < start) {
5275 mod = start - prev.br_startoff;
5276 prev.br_blockcount -= mod;
5277 prev.br_startblock += mod;
5278 prev.br_startoff = start;
5279 }
5280 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005281 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005282 error = xfs_bmap_add_extent_unwritten_real(tp,
5283 ip, &lastx, &cur, &prev,
5284 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 if (error)
5286 goto error0;
5287 goto nodelete;
5288 } else {
5289 ASSERT(del.br_state == XFS_EXT_NORM);
5290 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005291 error = xfs_bmap_add_extent_unwritten_real(tp,
5292 ip, &lastx, &cur, &del,
5293 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 if (error)
5295 goto error0;
5296 goto nodelete;
5297 }
5298 }
5299 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005300 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005301 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005302 if (isrt) {
5303 xfs_filblks_t rtexts;
5304
5305 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5306 do_div(rtexts, mp->m_sb.sb_rextsize);
Dave Chinnerbab98bb2015-02-23 21:22:54 +11005307 xfs_mod_frextents(mp, (int64_t)rtexts);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005308 (void)xfs_trans_reserve_quota_nblks(NULL,
5309 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005310 XFS_QMOPT_RES_RTBLKS);
5311 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11005312 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount,
5313 false);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005314 (void)xfs_trans_reserve_quota_nblks(NULL,
5315 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 ip->i_delayed_blks -= del.br_blockcount;
5319 if (cur)
5320 cur->bc_private.b.flags |=
5321 XFS_BTCUR_BPRV_WASDEL;
5322 } else if (cur)
5323 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5324 /*
5325 * If it's the case where the directory code is running
5326 * with no block reservation, and the deleted block is in
5327 * the middle of its extent, and the resulting insert
5328 * of an extent would cause transformation to btree format,
5329 * then reject it. The calling code will then swap
5330 * blocks around instead.
5331 * We have to do this now, rather than waiting for the
5332 * conversion to btree format, since the transaction
5333 * will be dirty.
5334 */
5335 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5336 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005337 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5338 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 del.br_startoff > got.br_startoff &&
5340 del.br_startoff + del.br_blockcount <
5341 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005342 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343 goto error0;
5344 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00005345 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005346 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005347 logflags |= tmp_logflags;
5348 if (error)
5349 goto error0;
5350 bno = del.br_startoff - 1;
5351nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 /*
5353 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005354 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005356 if (lastx >= 0) {
5357 ep = xfs_iext_get_ext(ifp, lastx);
5358 if (xfs_bmbt_get_startoff(ep) > bno) {
5359 if (--lastx >= 0)
5360 ep = xfs_iext_get_ext(ifp,
5361 lastx);
5362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005365 extno++;
5366 }
5367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005369
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 /*
5371 * Convert to a btree if necessary.
5372 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005373 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 ASSERT(cur == NULL);
5375 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5376 &cur, 0, &tmp_logflags, whichfork);
5377 logflags |= tmp_logflags;
5378 if (error)
5379 goto error0;
5380 }
5381 /*
5382 * transform from btree to extents, give it cur
5383 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005384 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 ASSERT(cur != NULL);
5386 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5387 whichfork);
5388 logflags |= tmp_logflags;
5389 if (error)
5390 goto error0;
5391 }
5392 /*
5393 * transform from extents to local?
5394 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395 error = 0;
5396error0:
5397 /*
5398 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005399 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005401 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005403 logflags &= ~xfs_ilog_fext(whichfork);
5404 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005406 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 /*
5408 * Log inode even in the error case, if the transaction
5409 * is dirty we'll need to shut down the filesystem.
5410 */
5411 if (logflags)
5412 xfs_trans_log_inode(tp, ip, logflags);
5413 if (cur) {
5414 if (!error) {
5415 *firstblock = cur->bc_private.b.firstblock;
5416 cur->bc_private.b.allocated = 0;
5417 }
5418 xfs_btree_del_cursor(cur,
5419 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5420 }
5421 return error;
5422}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005423
5424/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005425 * Determine whether an extent shift can be accomplished by a merge with the
5426 * extent that precedes the target hole of the shift.
5427 */
5428STATIC bool
5429xfs_bmse_can_merge(
5430 struct xfs_bmbt_irec *left, /* preceding extent */
5431 struct xfs_bmbt_irec *got, /* current extent to shift */
5432 xfs_fileoff_t shift) /* shift fsb */
5433{
5434 xfs_fileoff_t startoff;
5435
5436 startoff = got->br_startoff - shift;
5437
5438 /*
5439 * The extent, once shifted, must be adjacent in-file and on-disk with
5440 * the preceding extent.
5441 */
5442 if ((left->br_startoff + left->br_blockcount != startoff) ||
5443 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5444 (left->br_state != got->br_state) ||
5445 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5446 return false;
5447
5448 return true;
5449}
5450
5451/*
5452 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5453 * hole in the file. If an extent shift would result in the extent being fully
5454 * adjacent to the extent that currently precedes the hole, we can merge with
5455 * the preceding extent rather than do the shift.
5456 *
5457 * This function assumes the caller has verified a shift-by-merge is possible
5458 * with the provided extents via xfs_bmse_can_merge().
5459 */
5460STATIC int
5461xfs_bmse_merge(
5462 struct xfs_inode *ip,
5463 int whichfork,
5464 xfs_fileoff_t shift, /* shift fsb */
5465 int current_ext, /* idx of gotp */
5466 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5467 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5468 struct xfs_btree_cur *cur,
5469 int *logflags) /* output */
5470{
Brian Fosterddb19e32014-09-23 15:38:09 +10005471 struct xfs_bmbt_irec got;
5472 struct xfs_bmbt_irec left;
5473 xfs_filblks_t blockcount;
5474 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005475 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005476
Brian Fosterddb19e32014-09-23 15:38:09 +10005477 xfs_bmbt_get_all(gotp, &got);
5478 xfs_bmbt_get_all(leftp, &left);
5479 blockcount = left.br_blockcount + got.br_blockcount;
5480
5481 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5482 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5483 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5484
5485 /*
5486 * Merge the in-core extents. Note that the host record pointers and
5487 * current_ext index are invalid once the extent has been removed via
5488 * xfs_iext_remove().
5489 */
5490 xfs_bmbt_set_blockcount(leftp, blockcount);
5491 xfs_iext_remove(ip, current_ext, 1, 0);
5492
5493 /*
5494 * Update the on-disk extent count, the btree if necessary and log the
5495 * inode.
5496 */
5497 XFS_IFORK_NEXT_SET(ip, whichfork,
5498 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5499 *logflags |= XFS_ILOG_CORE;
5500 if (!cur) {
5501 *logflags |= XFS_ILOG_DEXT;
5502 return 0;
5503 }
5504
5505 /* lookup and remove the extent to merge */
5506 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5507 got.br_blockcount, &i);
5508 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005509 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005510 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005511
5512 error = xfs_btree_delete(cur, &i);
5513 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005514 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005515 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005516
5517 /* lookup and update size of the previous extent */
5518 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5519 left.br_blockcount, &i);
5520 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005521 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005522 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005523
5524 left.br_blockcount = blockcount;
5525
Dave Chinner4db431f2014-12-04 09:42:40 +11005526 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5527 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005528}
5529
5530/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005531 * Shift a single extent.
5532 */
5533STATIC int
5534xfs_bmse_shift_one(
5535 struct xfs_inode *ip,
5536 int whichfork,
5537 xfs_fileoff_t offset_shift_fsb,
5538 int *current_ext,
5539 struct xfs_bmbt_rec_host *gotp,
5540 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005541 int *logflags,
5542 enum shift_direction direction)
Brian Fostera979bdf2014-09-23 15:39:04 +10005543{
5544 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005545 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005546 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005547 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005548 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005549 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005550 int error;
5551 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005552 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005553
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005554 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005555 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005556 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Brian Fostera979bdf2014-09-23 15:39:04 +10005557
5558 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005559
Brian Fosterf71721d2014-09-23 15:39:05 +10005560 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005561 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005562
Namjae Jeona904b1c2015-03-25 15:08:56 +11005563 if (direction == SHIFT_LEFT) {
5564 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005565
Namjae Jeona904b1c2015-03-25 15:08:56 +11005566 /*
5567 * Check for merge if we've got an extent to the left,
5568 * otherwise make sure there's enough room at the start
5569 * of the file for the shift.
5570 */
5571 if (!*current_ext) {
5572 if (got.br_startoff < offset_shift_fsb)
5573 return -EINVAL;
5574 goto update_current_ext;
5575 }
5576 /*
5577 * grab the left extent and check for a large
5578 * enough hole.
5579 */
5580 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5581 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5582
5583 if (startoff <
5584 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005585 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005586
Dave Chinnerb11bd672014-12-04 09:42:24 +11005587 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005588 if (xfs_bmse_can_merge(&adj_irec, &got,
5589 offset_shift_fsb)) {
Dave Chinnerb11bd672014-12-04 09:42:24 +11005590 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005591 *current_ext, gotp, adj_irecp,
5592 cur, logflags);
Dave Chinnerb11bd672014-12-04 09:42:24 +11005593 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005594 } else {
5595 startoff = got.br_startoff + offset_shift_fsb;
5596 /* nothing to move if this is the last extent */
5597 if (*current_ext >= (total_extents - 1))
5598 goto update_current_ext;
5599 /*
5600 * If this is not the last extent in the file, make sure there
5601 * is enough room between current extent and next extent for
5602 * accommodating the shift.
5603 */
5604 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5605 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5606 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5607 return -EINVAL;
5608 /*
5609 * Unlike a left shift (which involves a hole punch),
5610 * a right shift does not modify extent neighbors
5611 * in any way. We should never find mergeable extents
5612 * in this scenario. Check anyways and warn if we
5613 * encounter two extents that could be one.
5614 */
5615 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5616 WARN_ON_ONCE(1);
5617 }
Brian Fostera979bdf2014-09-23 15:39:04 +10005618 /*
5619 * Increment the extent index for the next iteration, update the start
5620 * offset of the in-core extent and update the btree if applicable.
5621 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005622update_current_ext:
5623 if (direction == SHIFT_LEFT)
5624 (*current_ext)++;
5625 else
5626 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005627 xfs_bmbt_set_startoff(gotp, startoff);
5628 *logflags |= XFS_ILOG_CORE;
5629 if (!cur) {
5630 *logflags |= XFS_ILOG_DEXT;
5631 return 0;
5632 }
5633
5634 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5635 got.br_blockcount, &i);
5636 if (error)
5637 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005638 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10005639
5640 got.br_startoff = startoff;
kbuild test robotd254aae2014-12-01 08:42:52 +11005641 return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005642 got.br_blockcount, got.br_state);
Brian Fostera979bdf2014-09-23 15:39:04 +10005643}
5644
5645/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005646 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005647 *
Brian Foster2c845f52014-09-23 15:37:09 +10005648 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005649 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10005650 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5651 * is the length by which each extent is shifted. If there is no hole to shift
5652 * the extents into, this will be considered invalid operation and we abort
5653 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005654 */
5655int
5656xfs_bmap_shift_extents(
5657 struct xfs_trans *tp,
5658 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005659 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005660 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005661 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005662 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005663 xfs_fsblock_t *firstblock,
5664 struct xfs_bmap_free *flist,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005665 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005666 int num_exts)
5667{
Brian Fosterca446d82014-09-02 12:12:53 +10005668 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005669 struct xfs_bmbt_rec_host *gotp;
5670 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005671 struct xfs_mount *mp = ip->i_mount;
5672 struct xfs_ifork *ifp;
5673 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005674 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005675 xfs_extnum_t total_extents;
5676 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005677 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005678 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10005679 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005680
5681 if (unlikely(XFS_TEST_ERROR(
5682 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5683 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5684 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5685 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5686 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005687 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005688 }
5689
5690 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005691 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005692
Brian Foster2c845f52014-09-23 15:37:09 +10005693 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5694 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005695 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
5696 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005697
5698 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005699 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5700 /* Read in all the extents */
5701 error = xfs_iread_extents(tp, ip, whichfork);
5702 if (error)
5703 return error;
5704 }
5705
Brian Fosterddb19e32014-09-23 15:38:09 +10005706 if (ifp->if_flags & XFS_IFBROOT) {
5707 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5708 cur->bc_private.b.firstblock = *firstblock;
5709 cur->bc_private.b.flist = flist;
5710 cur->bc_private.b.flags = 0;
5711 }
5712
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005713 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005714 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10005715 * are collapsing out, so we cannot use the count of real extents here.
5716 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005717 */
5718 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005719 if (total_extents == 0) {
5720 *done = 1;
5721 goto del_cursor;
5722 }
5723
5724 /*
5725 * In case of first right shift, we need to initialize next_fsb
5726 */
5727 if (*next_fsb == NULLFSBLOCK) {
5728 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
5729 xfs_bmbt_get_all(gotp, &got);
5730 *next_fsb = got.br_startoff;
5731 if (stop_fsb > *next_fsb) {
5732 *done = 1;
5733 goto del_cursor;
5734 }
5735 }
5736
5737 /* Lookup the extent index at which we have to stop */
5738 if (direction == SHIFT_RIGHT) {
5739 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
5740 /* Make stop_extent exclusive of shift range */
5741 stop_extent--;
5742 } else
5743 stop_extent = total_extents;
5744
5745 /*
5746 * Look up the extent index for the fsb where we start shifting. We can
5747 * henceforth iterate with current_ext as extent list changes are locked
5748 * out via ilock.
5749 *
5750 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5751 * *next_fsb lies in a hole beyond which there are no extents. Either
5752 * way, we are done.
5753 */
5754 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
5755 if (!gotp) {
5756 *done = 1;
5757 goto del_cursor;
5758 }
5759
5760 /* some sanity checking before we finally start shifting extents */
5761 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
5762 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
5763 error = -EIO;
5764 goto del_cursor;
5765 }
5766
5767 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005768 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005769 &current_ext, gotp, cur, &logflags,
5770 direction);
Brian Fostera979bdf2014-09-23 15:39:04 +10005771 if (error)
5772 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005773 /*
5774 * If there was an extent merge during the shift, the extent
5775 * count can change. Update the total and grade the next record.
5776 */
5777 if (direction == SHIFT_LEFT) {
5778 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5779 stop_extent = total_extents;
5780 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005781
Namjae Jeona904b1c2015-03-25 15:08:56 +11005782 if (current_ext == stop_extent) {
5783 *done = 1;
5784 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10005785 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005786 }
Brian Fosterddb19e32014-09-23 15:38:09 +10005787 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005788 }
5789
Namjae Jeona904b1c2015-03-25 15:08:56 +11005790 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005791 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10005792 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10005793 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005794
5795del_cursor:
5796 if (cur)
5797 xfs_btree_del_cursor(cur,
5798 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5799
Brian Fosterca446d82014-09-02 12:12:53 +10005800 if (logflags)
5801 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10005802
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005803 return error;
5804}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005805
5806/*
5807 * Splits an extent into two extents at split_fsb block such that it is
5808 * the first block of the current_ext. @current_ext is a target extent
5809 * to be split. @split_fsb is a block where the extents is split.
5810 * If split_fsb lies in a hole or the first block of extents, just return 0.
5811 */
5812STATIC int
5813xfs_bmap_split_extent_at(
5814 struct xfs_trans *tp,
5815 struct xfs_inode *ip,
5816 xfs_fileoff_t split_fsb,
5817 xfs_fsblock_t *firstfsb,
5818 struct xfs_bmap_free *free_list)
5819{
5820 int whichfork = XFS_DATA_FORK;
5821 struct xfs_btree_cur *cur = NULL;
5822 struct xfs_bmbt_rec_host *gotp;
5823 struct xfs_bmbt_irec got;
5824 struct xfs_bmbt_irec new; /* split extent */
5825 struct xfs_mount *mp = ip->i_mount;
5826 struct xfs_ifork *ifp;
5827 xfs_fsblock_t gotblkcnt; /* new block count for got */
5828 xfs_extnum_t current_ext;
5829 int error = 0;
5830 int logflags = 0;
5831 int i = 0;
5832
5833 if (unlikely(XFS_TEST_ERROR(
5834 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5835 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5836 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5837 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5838 XFS_ERRLEVEL_LOW, mp);
5839 return -EFSCORRUPTED;
5840 }
5841
5842 if (XFS_FORCED_SHUTDOWN(mp))
5843 return -EIO;
5844
5845 ifp = XFS_IFORK_PTR(ip, whichfork);
5846 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5847 /* Read in all the extents */
5848 error = xfs_iread_extents(tp, ip, whichfork);
5849 if (error)
5850 return error;
5851 }
5852
5853 /*
5854 * gotp can be null in 2 cases: 1) if there are no extents
5855 * or 2) split_fsb lies in a hole beyond which there are
5856 * no extents. Either way, we are done.
5857 */
5858 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
5859 if (!gotp)
5860 return 0;
5861
5862 xfs_bmbt_get_all(gotp, &got);
5863
5864 /*
5865 * Check split_fsb lies in a hole or the start boundary offset
5866 * of the extent.
5867 */
5868 if (got.br_startoff >= split_fsb)
5869 return 0;
5870
5871 gotblkcnt = split_fsb - got.br_startoff;
5872 new.br_startoff = split_fsb;
5873 new.br_startblock = got.br_startblock + gotblkcnt;
5874 new.br_blockcount = got.br_blockcount - gotblkcnt;
5875 new.br_state = got.br_state;
5876
5877 if (ifp->if_flags & XFS_IFBROOT) {
5878 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5879 cur->bc_private.b.firstblock = *firstfsb;
5880 cur->bc_private.b.flist = free_list;
5881 cur->bc_private.b.flags = 0;
5882 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5883 got.br_startblock,
5884 got.br_blockcount,
5885 &i);
5886 if (error)
5887 goto del_cursor;
5888 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5889 }
5890
5891 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
5892 got.br_blockcount = gotblkcnt;
5893
5894 logflags = XFS_ILOG_CORE;
5895 if (cur) {
5896 error = xfs_bmbt_update(cur, got.br_startoff,
5897 got.br_startblock,
5898 got.br_blockcount,
5899 got.br_state);
5900 if (error)
5901 goto del_cursor;
5902 } else
5903 logflags |= XFS_ILOG_DEXT;
5904
5905 /* Add new extent */
5906 current_ext++;
5907 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5908 XFS_IFORK_NEXT_SET(ip, whichfork,
5909 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5910
5911 if (cur) {
5912 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
5913 new.br_startblock, new.br_blockcount,
5914 &i);
5915 if (error)
5916 goto del_cursor;
5917 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
5918 cur->bc_rec.b.br_state = new.br_state;
5919
5920 error = xfs_btree_insert(cur, &i);
5921 if (error)
5922 goto del_cursor;
5923 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5924 }
5925
5926 /*
5927 * Convert to a btree if necessary.
5928 */
5929 if (xfs_bmap_needs_btree(ip, whichfork)) {
5930 int tmp_logflags; /* partial log flag return val */
5931
5932 ASSERT(cur == NULL);
5933 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, free_list,
5934 &cur, 0, &tmp_logflags, whichfork);
5935 logflags |= tmp_logflags;
5936 }
5937
5938del_cursor:
5939 if (cur) {
5940 cur->bc_private.b.allocated = 0;
5941 xfs_btree_del_cursor(cur,
5942 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5943 }
5944
5945 if (logflags)
5946 xfs_trans_log_inode(tp, ip, logflags);
5947 return error;
5948}
5949
5950int
5951xfs_bmap_split_extent(
5952 struct xfs_inode *ip,
5953 xfs_fileoff_t split_fsb)
5954{
5955 struct xfs_mount *mp = ip->i_mount;
5956 struct xfs_trans *tp;
5957 struct xfs_bmap_free free_list;
5958 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005959 int error;
5960
5961 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
5962 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
5963 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0);
5964 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +10005965 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005966 return error;
5967 }
5968
5969 xfs_ilock(ip, XFS_ILOCK_EXCL);
5970 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
5971
5972 xfs_bmap_init(&free_list, &firstfsb);
5973
5974 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
5975 &firstfsb, &free_list);
5976 if (error)
5977 goto out;
5978
Eric Sandeenf6106ef2016-01-11 11:34:01 +11005979 error = xfs_bmap_finish(&tp, &free_list, NULL);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005980 if (error)
5981 goto out;
5982
Christoph Hellwig70393312015-06-04 13:48:08 +10005983 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005984
5985out:
Brian Fosterd4a97a02015-08-19 10:01:40 +10005986 xfs_bmap_cancel(&free_list);
Christoph Hellwig4906e212015-06-04 13:47:56 +10005987 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005988 return error;
5989}