blob: 8e2010d53b079556de461d6a1be1f9cbbc7e2924 [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
328 * btree leaves.
329 */
330
331STATIC void
332xfs_bmap_check_leaf_extents(
333 xfs_btree_cur_t *cur, /* btree cursor or null */
334 xfs_inode_t *ip, /* incore inode pointer */
335 int whichfork) /* data or attr fork */
336{
337 struct xfs_btree_block *block; /* current btree block */
338 xfs_fsblock_t bno; /* block # of "block" */
339 xfs_buf_t *bp; /* buffer for "block" */
340 int error; /* error return value */
341 xfs_extnum_t i=0, j; /* index into the extents list */
342 xfs_ifork_t *ifp; /* fork structure */
343 int level; /* btree level, for checking */
344 xfs_mount_t *mp; /* file system mount structure */
345 __be64 *pp; /* pointer to block address */
346 xfs_bmbt_rec_t *ep; /* pointer to current extent */
347 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
348 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
349 int bp_release = 0;
350
351 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
352 return;
353 }
354
355 bno = NULLFSBLOCK;
356 mp = ip->i_mount;
357 ifp = XFS_IFORK_PTR(ip, whichfork);
358 block = ifp->if_broot;
359 /*
360 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
361 */
362 level = be16_to_cpu(block->bb_level);
363 ASSERT(level > 0);
364 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
365 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
366 bno = be64_to_cpu(*pp);
367
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000368 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100369 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
370 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
371
372 /*
373 * Go down the tree until leaf level is reached, following the first
374 * pointer (leftmost) at each level.
375 */
376 while (level-- > 0) {
377 /* See if buf is in cur first */
378 bp_release = 0;
379 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
380 if (!bp) {
381 bp_release = 1;
382 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
383 XFS_BMAP_BTREE_REF,
384 &xfs_bmbt_buf_ops);
385 if (error)
386 goto error_norelse;
387 }
388 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100389 if (level == 0)
390 break;
391
392 /*
393 * Check this block for basic sanity (increasing keys and
394 * no duplicate blocks).
395 */
396
397 xfs_check_block(block, mp, 0, 0);
398 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
399 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100400 XFS_WANT_CORRUPTED_GOTO(mp,
401 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100402 if (bp_release) {
403 bp_release = 0;
404 xfs_trans_brelse(NULL, bp);
405 }
406 }
407
408 /*
409 * Here with bp and block set to the leftmost leaf node in the tree.
410 */
411 i = 0;
412
413 /*
414 * Loop over all leaf nodes checking that all extents are in the right order.
415 */
416 for (;;) {
417 xfs_fsblock_t nextbno;
418 xfs_extnum_t num_recs;
419
420
421 num_recs = xfs_btree_get_numrecs(block);
422
423 /*
424 * Read-ahead the next leaf block, if any.
425 */
426
427 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
428
429 /*
430 * Check all the extents to make sure they are OK.
431 * If we had a previous block, the last entry should
432 * conform with the first entry in this one.
433 */
434
435 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
436 if (i) {
437 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
438 xfs_bmbt_disk_get_blockcount(&last) <=
439 xfs_bmbt_disk_get_startoff(ep));
440 }
441 for (j = 1; j < num_recs; j++) {
442 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
443 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
444 xfs_bmbt_disk_get_blockcount(ep) <=
445 xfs_bmbt_disk_get_startoff(nextp));
446 ep = nextp;
447 }
448
449 last = *ep;
450 i += num_recs;
451 if (bp_release) {
452 bp_release = 0;
453 xfs_trans_brelse(NULL, bp);
454 }
455 bno = nextbno;
456 /*
457 * If we've reached the end, stop.
458 */
459 if (bno == NULLFSBLOCK)
460 break;
461
462 bp_release = 0;
463 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
464 if (!bp) {
465 bp_release = 1;
466 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
467 XFS_BMAP_BTREE_REF,
468 &xfs_bmbt_buf_ops);
469 if (error)
470 goto error_norelse;
471 }
472 block = XFS_BUF_TO_BLOCK(bp);
473 }
474 if (bp_release) {
475 bp_release = 0;
476 xfs_trans_brelse(NULL, bp);
477 }
478 return;
479
480error0:
481 xfs_warn(mp, "%s: at error0", __func__);
482 if (bp_release)
483 xfs_trans_brelse(NULL, bp);
484error_norelse:
485 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
486 __func__, i);
487 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
488 return;
489}
490
491/*
492 * Add bmap trace insert entries for all the contents of the extent records.
493 */
494void
495xfs_bmap_trace_exlist(
496 xfs_inode_t *ip, /* incore inode pointer */
497 xfs_extnum_t cnt, /* count of entries in the list */
498 int whichfork, /* data or attr fork */
499 unsigned long caller_ip)
500{
501 xfs_extnum_t idx; /* extent record index */
502 xfs_ifork_t *ifp; /* inode fork pointer */
503 int state = 0;
504
505 if (whichfork == XFS_ATTR_FORK)
506 state |= BMAP_ATTRFORK;
507
508 ifp = XFS_IFORK_PTR(ip, whichfork);
509 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
510 for (idx = 0; idx < cnt; idx++)
511 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
512}
513
514/*
515 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000516 * given the caller's original parameters. Specifically check the
517 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100518 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
519 */
520STATIC void
521xfs_bmap_validate_ret(
522 xfs_fileoff_t bno,
523 xfs_filblks_t len,
524 int flags,
525 xfs_bmbt_irec_t *mval,
526 int nmap,
527 int ret_nmap)
528{
529 int i; /* index to map values */
530
531 ASSERT(ret_nmap <= nmap);
532
533 for (i = 0; i < ret_nmap; i++) {
534 ASSERT(mval[i].br_blockcount > 0);
535 if (!(flags & XFS_BMAPI_ENTIRE)) {
536 ASSERT(mval[i].br_startoff >= bno);
537 ASSERT(mval[i].br_blockcount <= len);
538 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
539 bno + len);
540 } else {
541 ASSERT(mval[i].br_startoff < bno + len);
542 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
543 bno);
544 }
545 ASSERT(i == 0 ||
546 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
547 mval[i].br_startoff);
548 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
549 mval[i].br_startblock != HOLESTARTBLOCK);
550 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
551 mval[i].br_state == XFS_EXT_UNWRITTEN);
552 }
553}
554
555#else
556#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
557#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
558#endif /* DEBUG */
559
560/*
561 * bmap free list manipulation functions
562 */
563
564/*
565 * Add the extent to the list of extents to be free at transaction end.
566 * The list is maintained sorted (by block number).
567 */
568void
569xfs_bmap_add_free(
570 xfs_fsblock_t bno, /* fs block number of extent */
571 xfs_filblks_t len, /* length of extent */
572 xfs_bmap_free_t *flist, /* list of extents */
573 xfs_mount_t *mp) /* mount point structure */
574{
575 xfs_bmap_free_item_t *cur; /* current (next) element */
576 xfs_bmap_free_item_t *new; /* new element */
577 xfs_bmap_free_item_t *prev; /* previous element */
578#ifdef DEBUG
579 xfs_agnumber_t agno;
580 xfs_agblock_t agbno;
581
582 ASSERT(bno != NULLFSBLOCK);
583 ASSERT(len > 0);
584 ASSERT(len <= MAXEXTLEN);
585 ASSERT(!isnullstartblock(bno));
586 agno = XFS_FSB_TO_AGNO(mp, bno);
587 agbno = XFS_FSB_TO_AGBNO(mp, bno);
588 ASSERT(agno < mp->m_sb.sb_agcount);
589 ASSERT(agbno < mp->m_sb.sb_agblocks);
590 ASSERT(len < mp->m_sb.sb_agblocks);
591 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
592#endif
593 ASSERT(xfs_bmap_free_item_zone != NULL);
594 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
595 new->xbfi_startblock = bno;
596 new->xbfi_blockcount = (xfs_extlen_t)len;
597 for (prev = NULL, cur = flist->xbf_first;
598 cur != NULL;
599 prev = cur, cur = cur->xbfi_next) {
600 if (cur->xbfi_startblock >= bno)
601 break;
602 }
603 if (prev)
604 prev->xbfi_next = new;
605 else
606 flist->xbf_first = new;
607 new->xbfi_next = cur;
608 flist->xbf_count++;
609}
610
611/*
612 * Remove the entry "free" from the free item list. Prev points to the
613 * previous entry, unless "free" is the head of the list.
614 */
Dave Chinner68988112013-08-12 20:49:42 +1000615void
Dave Chinner9e5987a2013-02-25 12:31:26 +1100616xfs_bmap_del_free(
617 xfs_bmap_free_t *flist, /* free item list header */
618 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
619 xfs_bmap_free_item_t *free) /* list item to be freed */
620{
621 if (prev)
622 prev->xbfi_next = free->xbfi_next;
623 else
624 flist->xbf_first = free->xbfi_next;
625 flist->xbf_count--;
626 kmem_zone_free(xfs_bmap_free_item_zone, free);
627}
628
Dave Chinner9e5987a2013-02-25 12:31:26 +1100629/*
630 * Free up any items left in the list.
631 */
632void
633xfs_bmap_cancel(
634 xfs_bmap_free_t *flist) /* list of bmap_free_items */
635{
636 xfs_bmap_free_item_t *free; /* free list item */
637 xfs_bmap_free_item_t *next;
638
639 if (flist->xbf_count == 0)
640 return;
641 ASSERT(flist->xbf_first != NULL);
642 for (free = flist->xbf_first; free; free = next) {
643 next = free->xbfi_next;
644 xfs_bmap_del_free(flist, NULL, free);
645 }
646 ASSERT(flist->xbf_count == 0);
647}
648
649/*
650 * Inode fork format manipulation functions
651 */
652
653/*
654 * Transform a btree format file with only one leaf node, where the
655 * extents list will fit in the inode, into an extents format file.
656 * Since the file extents are already in-core, all we have to do is
657 * give up the space for the btree root and pitch the leaf block.
658 */
659STATIC int /* error */
660xfs_bmap_btree_to_extents(
661 xfs_trans_t *tp, /* transaction pointer */
662 xfs_inode_t *ip, /* incore inode pointer */
663 xfs_btree_cur_t *cur, /* btree cursor */
664 int *logflagsp, /* inode logging flags */
665 int whichfork) /* data or attr fork */
666{
667 /* REFERENCED */
668 struct xfs_btree_block *cblock;/* child btree block */
669 xfs_fsblock_t cbno; /* child block number */
670 xfs_buf_t *cbp; /* child block's buffer */
671 int error; /* error return value */
672 xfs_ifork_t *ifp; /* inode fork data */
673 xfs_mount_t *mp; /* mount point structure */
674 __be64 *pp; /* ptr to block address */
675 struct xfs_btree_block *rblock;/* root btree block */
676
677 mp = ip->i_mount;
678 ifp = XFS_IFORK_PTR(ip, whichfork);
679 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
680 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
681 rblock = ifp->if_broot;
682 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
683 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
684 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
685 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
686 cbno = be64_to_cpu(*pp);
687 *logflagsp = 0;
688#ifdef DEBUG
689 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
690 return error;
691#endif
692 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
693 &xfs_bmbt_buf_ops);
694 if (error)
695 return error;
696 cblock = XFS_BUF_TO_BLOCK(cbp);
697 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
698 return error;
699 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
700 ip->i_d.di_nblocks--;
701 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
702 xfs_trans_binval(tp, cbp);
703 if (cur->bc_bufs[0] == cbp)
704 cur->bc_bufs[0] = NULL;
705 xfs_iroot_realloc(ip, -1, whichfork);
706 ASSERT(ifp->if_broot == NULL);
707 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
708 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
709 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
710 return 0;
711}
712
713/*
714 * Convert an extents-format file into a btree-format file.
715 * The new file will have a root block (in the inode) and a single child block.
716 */
717STATIC int /* error */
718xfs_bmap_extents_to_btree(
719 xfs_trans_t *tp, /* transaction pointer */
720 xfs_inode_t *ip, /* incore inode pointer */
721 xfs_fsblock_t *firstblock, /* first-block-allocated */
722 xfs_bmap_free_t *flist, /* blocks freed in xaction */
723 xfs_btree_cur_t **curp, /* cursor returned to caller */
724 int wasdel, /* converting a delayed alloc */
725 int *logflagsp, /* inode logging flags */
726 int whichfork) /* data or attr fork */
727{
728 struct xfs_btree_block *ablock; /* allocated (child) bt block */
729 xfs_buf_t *abp; /* buffer for ablock */
730 xfs_alloc_arg_t args; /* allocation arguments */
731 xfs_bmbt_rec_t *arp; /* child record pointer */
732 struct xfs_btree_block *block; /* btree root block */
733 xfs_btree_cur_t *cur; /* bmap btree cursor */
734 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
735 int error; /* error return value */
736 xfs_extnum_t i, cnt; /* extent record index */
737 xfs_ifork_t *ifp; /* inode fork pointer */
738 xfs_bmbt_key_t *kp; /* root block key pointer */
739 xfs_mount_t *mp; /* mount structure */
740 xfs_extnum_t nextents; /* number of file extents */
741 xfs_bmbt_ptr_t *pp; /* root block address pointer */
742
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500743 mp = ip->i_mount;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100744 ifp = XFS_IFORK_PTR(ip, whichfork);
745 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
746
747 /*
748 * Make space in the inode incore.
749 */
750 xfs_iroot_realloc(ip, 1, whichfork);
751 ifp->if_flags |= XFS_IFBROOT;
752
753 /*
754 * Fill in the root.
755 */
756 block = ifp->if_broot;
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500757 if (xfs_sb_version_hascrc(&mp->m_sb))
758 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
759 XFS_BMAP_CRC_MAGIC, 1, 1, ip->i_ino,
760 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
761 else
762 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
763 XFS_BMAP_MAGIC, 1, 1, ip->i_ino,
764 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100765
766 /*
767 * Need a cursor. Can't allocate until bb_level is filled in.
768 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100769 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
770 cur->bc_private.b.firstblock = *firstblock;
771 cur->bc_private.b.flist = flist;
772 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
773 /*
774 * Convert to a btree with two levels, one record in root.
775 */
776 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
777 memset(&args, 0, sizeof(args));
778 args.tp = tp;
779 args.mp = mp;
780 args.firstblock = *firstblock;
781 if (*firstblock == NULLFSBLOCK) {
782 args.type = XFS_ALLOCTYPE_START_BNO;
783 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
784 } else if (flist->xbf_low) {
785 args.type = XFS_ALLOCTYPE_START_BNO;
786 args.fsbno = *firstblock;
787 } else {
788 args.type = XFS_ALLOCTYPE_NEAR_BNO;
789 args.fsbno = *firstblock;
790 }
791 args.minlen = args.maxlen = args.prod = 1;
792 args.wasdel = wasdel;
793 *logflagsp = 0;
794 if ((error = xfs_alloc_vextent(&args))) {
795 xfs_iroot_realloc(ip, -1, whichfork);
796 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
797 return error;
798 }
799 /*
800 * Allocation can't fail, the space was reserved.
801 */
802 ASSERT(args.fsbno != NULLFSBLOCK);
803 ASSERT(*firstblock == NULLFSBLOCK ||
804 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
805 (flist->xbf_low &&
806 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
807 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
808 cur->bc_private.b.allocated++;
809 ip->i_d.di_nblocks++;
810 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
811 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
812 /*
813 * Fill in the child block.
814 */
815 abp->b_ops = &xfs_bmbt_buf_ops;
816 ablock = XFS_BUF_TO_BLOCK(abp);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500817 if (xfs_sb_version_hascrc(&mp->m_sb))
818 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
819 XFS_BMAP_CRC_MAGIC, 0, 0, ip->i_ino,
820 XFS_BTREE_LONG_PTRS | XFS_BTREE_CRC_BLOCKS);
821 else
822 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
823 XFS_BMAP_MAGIC, 0, 0, ip->i_ino,
824 XFS_BTREE_LONG_PTRS);
825
Dave Chinner9e5987a2013-02-25 12:31:26 +1100826 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
827 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
828 for (cnt = i = 0; i < nextents; i++) {
829 ep = xfs_iext_get_ext(ifp, i);
830 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
831 arp->l0 = cpu_to_be64(ep->l0);
832 arp->l1 = cpu_to_be64(ep->l1);
833 arp++; cnt++;
834 }
835 }
836 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
837 xfs_btree_set_numrecs(ablock, cnt);
838
839 /*
840 * Fill in the root key and pointer.
841 */
842 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
843 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
844 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
845 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
846 be16_to_cpu(block->bb_level)));
847 *pp = cpu_to_be64(args.fsbno);
848
849 /*
850 * Do all this logging at the end so that
851 * the root is at the right level.
852 */
853 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
854 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
855 ASSERT(*curp == NULL);
856 *curp = cur;
857 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
858 return 0;
859}
860
861/*
862 * Convert a local file to an extents file.
863 * This code is out of bounds for data forks of regular files,
864 * since the file data needs to get logged so things will stay consistent.
865 * (The bmap-level manipulations are ok, though).
866 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000867void
868xfs_bmap_local_to_extents_empty(
869 struct xfs_inode *ip,
870 int whichfork)
871{
872 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
873
874 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
875 ASSERT(ifp->if_bytes == 0);
876 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
877
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000878 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000879 ifp->if_flags &= ~XFS_IFINLINE;
880 ifp->if_flags |= XFS_IFEXTENTS;
881 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
882}
883
884
Dave Chinner9e5987a2013-02-25 12:31:26 +1100885STATIC int /* error */
886xfs_bmap_local_to_extents(
887 xfs_trans_t *tp, /* transaction pointer */
888 xfs_inode_t *ip, /* incore inode pointer */
889 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
890 xfs_extlen_t total, /* total blocks needed by transaction */
891 int *logflagsp, /* inode logging flags */
892 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500893 void (*init_fn)(struct xfs_trans *tp,
894 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100895 struct xfs_inode *ip,
896 struct xfs_ifork *ifp))
897{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000898 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100899 int flags; /* logging flags returned */
900 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000901 xfs_alloc_arg_t args; /* allocation arguments */
902 xfs_buf_t *bp; /* buffer for extent block */
903 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100904
905 /*
906 * We don't want to deal with the case of keeping inode data inline yet.
907 * So sending the data fork of a regular inode is invalid.
908 */
909 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
910 ifp = XFS_IFORK_PTR(ip, whichfork);
911 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000912
913 if (!ifp->if_bytes) {
914 xfs_bmap_local_to_extents_empty(ip, whichfork);
915 flags = XFS_ILOG_CORE;
916 goto done;
917 }
918
Dave Chinner9e5987a2013-02-25 12:31:26 +1100919 flags = 0;
920 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000921 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
922 XFS_IFINLINE);
923 memset(&args, 0, sizeof(args));
924 args.tp = tp;
925 args.mp = ip->i_mount;
926 args.firstblock = *firstblock;
927 /*
928 * Allocate a block. We know we need only one, since the
929 * file currently fits in an inode.
930 */
931 if (*firstblock == NULLFSBLOCK) {
932 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
933 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100934 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000935 args.fsbno = *firstblock;
936 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100937 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000938 args.total = total;
939 args.minlen = args.maxlen = args.prod = 1;
940 error = xfs_alloc_vextent(&args);
941 if (error)
942 goto done;
943
944 /* Can't fail, the space was reserved. */
945 ASSERT(args.fsbno != NULLFSBLOCK);
946 ASSERT(args.len == 1);
947 *firstblock = args.fsbno;
948 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
949
Dave Chinnerfe22d552015-01-22 09:30:06 +1100950 /*
951 * Initialise the block and copy the data
952 *
953 * Note: init_fn must set the buffer log item type correctly!
954 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000955 init_fn(tp, bp, ip, ifp);
956
957 /* account for the change in fork size and log everything */
958 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
959 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
960 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100961 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000962
963 xfs_iext_add(ifp, 0, 1);
964 ep = xfs_iext_get_ext(ifp, 0);
965 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
966 trace_xfs_bmap_post_update(ip, 0,
967 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
968 _THIS_IP_);
969 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
970 ip->i_d.di_nblocks = 1;
971 xfs_trans_mod_dquot_byino(tp, ip,
972 XFS_TRANS_DQ_BCOUNT, 1L);
973 flags |= xfs_ilog_fext(whichfork);
974
Dave Chinner9e5987a2013-02-25 12:31:26 +1100975done:
976 *logflagsp = flags;
977 return error;
978}
979
980/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 * Called from xfs_bmap_add_attrfork to handle btree format files.
982 */
983STATIC int /* error */
984xfs_bmap_add_attrfork_btree(
985 xfs_trans_t *tp, /* transaction pointer */
986 xfs_inode_t *ip, /* incore inode pointer */
987 xfs_fsblock_t *firstblock, /* first block allocated */
988 xfs_bmap_free_t *flist, /* blocks to free at commit */
989 int *flags) /* inode logging flags */
990{
991 xfs_btree_cur_t *cur; /* btree cursor */
992 int error; /* error return value */
993 xfs_mount_t *mp; /* file system mount struct */
994 int stat; /* newroot status */
995
996 mp = ip->i_mount;
997 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
998 *flags |= XFS_ILOG_DBROOT;
999 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +11001000 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 cur->bc_private.b.flist = flist;
1002 cur->bc_private.b.firstblock = *firstblock;
1003 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
1004 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +10001005 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001006 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +11001007 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto error0;
1009 if (stat == 0) {
1010 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +10001011 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013 *firstblock = cur->bc_private.b.firstblock;
1014 cur->bc_private.b.allocated = 0;
1015 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
1016 }
1017 return 0;
1018error0:
1019 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
1020 return error;
1021}
1022
1023/*
1024 * Called from xfs_bmap_add_attrfork to handle extents format files.
1025 */
1026STATIC int /* error */
1027xfs_bmap_add_attrfork_extents(
1028 xfs_trans_t *tp, /* transaction pointer */
1029 xfs_inode_t *ip, /* incore inode pointer */
1030 xfs_fsblock_t *firstblock, /* first block allocated */
1031 xfs_bmap_free_t *flist, /* blocks to free at commit */
1032 int *flags) /* inode logging flags */
1033{
1034 xfs_btree_cur_t *cur; /* bmap btree cursor */
1035 int error; /* error return value */
1036
1037 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1038 return 0;
1039 cur = NULL;
1040 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
1041 flags, XFS_DATA_FORK);
1042 if (cur) {
1043 cur->bc_private.b.allocated = 0;
1044 xfs_btree_del_cursor(cur,
1045 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1046 }
1047 return error;
1048}
1049
1050/*
Dave Chinner1e823792013-02-11 15:58:13 +11001051 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1052 * different data fork content type needs a different callout to do the
1053 * conversion. Some are basic and only require special block initialisation
1054 * callouts for the data formating, others (directories) are so specialised they
1055 * handle everything themselves.
1056 *
1057 * XXX (dgc): investigate whether directory conversion can use the generic
1058 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001059 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 */
1061STATIC int /* error */
1062xfs_bmap_add_attrfork_local(
1063 xfs_trans_t *tp, /* transaction pointer */
1064 xfs_inode_t *ip, /* incore inode pointer */
1065 xfs_fsblock_t *firstblock, /* first block allocated */
1066 xfs_bmap_free_t *flist, /* blocks to free at commit */
1067 int *flags) /* inode logging flags */
1068{
1069 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1072 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001073
Al Viroabbede12011-07-26 02:31:30 -04001074 if (S_ISDIR(ip->i_d.di_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001076 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 dargs.dp = ip;
1078 dargs.firstblock = firstblock;
1079 dargs.flist = flist;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001080 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 dargs.whichfork = XFS_DATA_FORK;
1082 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001083 return xfs_dir2_sf_to_block(&dargs);
1084 }
1085
1086 if (S_ISLNK(ip->i_d.di_mode))
1087 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1088 flags, XFS_DATA_FORK,
1089 xfs_symlink_local_to_remote);
1090
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001091 /* should only be called for types that support local format data */
1092 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001093 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
1096/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001097 * Convert inode from non-attributed to attributed.
1098 * Must not be in a transaction, ip must not be locked.
1099 */
1100int /* error code */
1101xfs_bmap_add_attrfork(
1102 xfs_inode_t *ip, /* incore inode pointer */
1103 int size, /* space new attribute needs */
1104 int rsvd) /* xact may use reserved blks */
1105{
1106 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
1107 xfs_bmap_free_t flist; /* freed extent records */
1108 xfs_mount_t *mp; /* mount structure */
1109 xfs_trans_t *tp; /* transaction pointer */
1110 int blks; /* space reservation */
1111 int version = 1; /* superblock attr version */
1112 int committed; /* xaction was committed */
1113 int logflags; /* logging flags */
1114 int error; /* error return value */
1115
1116 ASSERT(XFS_IFORK_Q(ip) == 0);
1117
1118 mp = ip->i_mount;
1119 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1120 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
1121 blks = XFS_ADDAFORK_SPACE_RES(mp);
1122 if (rsvd)
1123 tp->t_flags |= XFS_TRANS_RESERVE;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001124 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_addafork, blks, 0);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001125 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +10001126 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001127 return error;
1128 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001129 xfs_ilock(ip, XFS_ILOCK_EXCL);
1130 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1131 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1132 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001133 if (error)
1134 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001135 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001136 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001137 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1138 /*
1139 * For inodes coming from pre-6.2 filesystems.
1140 */
1141 ASSERT(ip->i_d.di_aformat == 0);
1142 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1143 }
1144 ASSERT(ip->i_d.di_anextents == 0);
1145
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001146 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001147 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1148
1149 switch (ip->i_d.di_format) {
1150 case XFS_DINODE_FMT_DEV:
1151 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1152 break;
1153 case XFS_DINODE_FMT_UUID:
1154 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1155 break;
1156 case XFS_DINODE_FMT_LOCAL:
1157 case XFS_DINODE_FMT_EXTENTS:
1158 case XFS_DINODE_FMT_BTREE:
1159 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1160 if (!ip->i_d.di_forkoff)
1161 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1162 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1163 version = 2;
1164 break;
1165 default:
1166 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001167 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001168 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001169 }
1170
1171 ASSERT(ip->i_afp == NULL);
1172 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1173 ip->i_afp->if_flags = XFS_IFEXTENTS;
1174 logflags = 0;
1175 xfs_bmap_init(&flist, &firstblock);
1176 switch (ip->i_d.di_format) {
1177 case XFS_DINODE_FMT_LOCAL:
1178 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
1179 &logflags);
1180 break;
1181 case XFS_DINODE_FMT_EXTENTS:
1182 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1183 &flist, &logflags);
1184 break;
1185 case XFS_DINODE_FMT_BTREE:
1186 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
1187 &logflags);
1188 break;
1189 default:
1190 error = 0;
1191 break;
1192 }
1193 if (logflags)
1194 xfs_trans_log_inode(tp, ip, logflags);
1195 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001196 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001197 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1198 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001199 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001200
1201 spin_lock(&mp->m_sb_lock);
1202 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1203 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001204 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001205 }
1206 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1207 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001208 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001209 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001210 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001211 if (log_sb)
1212 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001213 }
1214
1215 error = xfs_bmap_finish(&tp, &flist, &committed);
1216 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001217 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001218 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001219 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001220 return error;
1221
1222bmap_cancel:
1223 xfs_bmap_cancel(&flist);
1224trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001225 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001226 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001227 return error;
1228}
1229
1230/*
1231 * Internal and external extent tree search functions.
1232 */
1233
1234/*
1235 * Read in the extents to if_extents.
1236 * All inode fields are set up by caller, we just traverse the btree
1237 * and copy the records in. If the file system cannot contain unwritten
1238 * extents, the records are checked for no "state" flags.
1239 */
1240int /* error */
1241xfs_bmap_read_extents(
1242 xfs_trans_t *tp, /* transaction pointer */
1243 xfs_inode_t *ip, /* incore inode */
1244 int whichfork) /* data or attr fork */
1245{
1246 struct xfs_btree_block *block; /* current btree block */
1247 xfs_fsblock_t bno; /* block # of "block" */
1248 xfs_buf_t *bp; /* buffer for "block" */
1249 int error; /* error return value */
1250 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1251 xfs_extnum_t i, j; /* index into the extents list */
1252 xfs_ifork_t *ifp; /* fork structure */
1253 int level; /* btree level, for checking */
1254 xfs_mount_t *mp; /* file system mount structure */
1255 __be64 *pp; /* pointer to block address */
1256 /* REFERENCED */
1257 xfs_extnum_t room; /* number of entries there's room for */
1258
1259 bno = NULLFSBLOCK;
1260 mp = ip->i_mount;
1261 ifp = XFS_IFORK_PTR(ip, whichfork);
1262 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1263 XFS_EXTFMT_INODE(ip);
1264 block = ifp->if_broot;
1265 /*
1266 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1267 */
1268 level = be16_to_cpu(block->bb_level);
1269 ASSERT(level > 0);
1270 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1271 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001272 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001273 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1274 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1275 /*
1276 * Go down the tree until leaf level is reached, following the first
1277 * pointer (leftmost) at each level.
1278 */
1279 while (level-- > 0) {
1280 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1281 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1282 if (error)
1283 return error;
1284 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001285 if (level == 0)
1286 break;
1287 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1288 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001289 XFS_WANT_CORRUPTED_GOTO(mp,
1290 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001291 xfs_trans_brelse(tp, bp);
1292 }
1293 /*
1294 * Here with bp and block set to the leftmost leaf node in the tree.
1295 */
1296 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1297 i = 0;
1298 /*
1299 * Loop over all leaf nodes. Copy information to the extent records.
1300 */
1301 for (;;) {
1302 xfs_bmbt_rec_t *frp;
1303 xfs_fsblock_t nextbno;
1304 xfs_extnum_t num_recs;
1305 xfs_extnum_t start;
1306
1307 num_recs = xfs_btree_get_numrecs(block);
1308 if (unlikely(i + num_recs > room)) {
1309 ASSERT(i + num_recs <= room);
1310 xfs_warn(ip->i_mount,
1311 "corrupt dinode %Lu, (btree extents).",
1312 (unsigned long long) ip->i_ino);
1313 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1314 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1315 goto error0;
1316 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001317 /*
1318 * Read-ahead the next leaf block, if any.
1319 */
1320 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1321 if (nextbno != NULLFSBLOCK)
1322 xfs_btree_reada_bufl(mp, nextbno, 1,
1323 &xfs_bmbt_buf_ops);
1324 /*
1325 * Copy records into the extent records.
1326 */
1327 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1328 start = i;
1329 for (j = 0; j < num_recs; j++, i++, frp++) {
1330 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1331 trp->l0 = be64_to_cpu(frp->l0);
1332 trp->l1 = be64_to_cpu(frp->l1);
1333 }
1334 if (exntf == XFS_EXTFMT_NOSTATE) {
1335 /*
1336 * Check all attribute bmap btree records and
1337 * any "older" data bmap btree records for a
1338 * set bit in the "extent flag" position.
1339 */
1340 if (unlikely(xfs_check_nostate_extents(ifp,
1341 start, num_recs))) {
1342 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1343 XFS_ERRLEVEL_LOW,
1344 ip->i_mount);
1345 goto error0;
1346 }
1347 }
1348 xfs_trans_brelse(tp, bp);
1349 bno = nextbno;
1350 /*
1351 * If we've reached the end, stop.
1352 */
1353 if (bno == NULLFSBLOCK)
1354 break;
1355 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1356 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1357 if (error)
1358 return error;
1359 block = XFS_BUF_TO_BLOCK(bp);
1360 }
1361 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1362 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1363 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1364 return 0;
1365error0:
1366 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001367 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001368}
1369
1370
1371/*
1372 * Search the extent records for the entry containing block bno.
1373 * If bno lies in a hole, point to the next entry. If bno lies
1374 * past eof, *eofp will be set, and *prevp will contain the last
1375 * entry (null if none). Else, *lastxp will be set to the index
1376 * of the found entry; *gotp will contain the entry.
1377 */
1378STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1379xfs_bmap_search_multi_extents(
1380 xfs_ifork_t *ifp, /* inode fork pointer */
1381 xfs_fileoff_t bno, /* block number searched for */
1382 int *eofp, /* out: end of file found */
1383 xfs_extnum_t *lastxp, /* out: last extent index */
1384 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1385 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1386{
1387 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1388 xfs_extnum_t lastx; /* last extent index */
1389
1390 /*
1391 * Initialize the extent entry structure to catch access to
1392 * uninitialized br_startblock field.
1393 */
1394 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1395 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1396 gotp->br_state = XFS_EXT_INVALID;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001397 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001398 prevp->br_startoff = NULLFILEOFF;
1399
1400 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1401 if (lastx > 0) {
1402 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1403 }
1404 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1405 xfs_bmbt_get_all(ep, gotp);
1406 *eofp = 0;
1407 } else {
1408 if (lastx > 0) {
1409 *gotp = *prevp;
1410 }
1411 *eofp = 1;
1412 ep = NULL;
1413 }
1414 *lastxp = lastx;
1415 return ep;
1416}
1417
1418/*
1419 * Search the extents list for the inode, for the extent containing bno.
1420 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1421 * *eofp will be set, and *prevp will contain the last entry (null if none).
1422 * Else, *lastxp will be set to the index of the found
1423 * entry; *gotp will contain the entry.
1424 */
1425STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1426xfs_bmap_search_extents(
1427 xfs_inode_t *ip, /* incore inode pointer */
1428 xfs_fileoff_t bno, /* block number searched for */
1429 int fork, /* data or attr fork */
1430 int *eofp, /* out: end of file found */
1431 xfs_extnum_t *lastxp, /* out: last extent index */
1432 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1433 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1434{
1435 xfs_ifork_t *ifp; /* inode fork pointer */
1436 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1437
1438 XFS_STATS_INC(xs_look_exlist);
1439 ifp = XFS_IFORK_PTR(ip, fork);
1440
1441 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1442
1443 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1444 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1445 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1446 "Access to block zero in inode %llu "
1447 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -05001448 "blkcnt: %llx extent-state: %x lastx: %x",
Dave Chinner9e5987a2013-02-25 12:31:26 +11001449 (unsigned long long)ip->i_ino,
1450 (unsigned long long)gotp->br_startblock,
1451 (unsigned long long)gotp->br_startoff,
1452 (unsigned long long)gotp->br_blockcount,
1453 gotp->br_state, *lastxp);
1454 *lastxp = NULLEXTNUM;
1455 *eofp = 1;
1456 return NULL;
1457 }
1458 return ep;
1459}
1460
1461/*
1462 * Returns the file-relative block number of the first unused block(s)
1463 * in the file with at least "len" logically contiguous blocks free.
1464 * This is the lowest-address hole if the file has holes, else the first block
1465 * past the end of file.
1466 * Return 0 if the file is currently local (in-inode).
1467 */
1468int /* error */
1469xfs_bmap_first_unused(
1470 xfs_trans_t *tp, /* transaction pointer */
1471 xfs_inode_t *ip, /* incore inode */
1472 xfs_extlen_t len, /* size of hole to find */
1473 xfs_fileoff_t *first_unused, /* unused block */
1474 int whichfork) /* data or attr fork */
1475{
1476 int error; /* error return value */
1477 int idx; /* extent record index */
1478 xfs_ifork_t *ifp; /* inode fork pointer */
1479 xfs_fileoff_t lastaddr; /* last block number seen */
1480 xfs_fileoff_t lowest; /* lowest useful block */
1481 xfs_fileoff_t max; /* starting useful block */
1482 xfs_fileoff_t off; /* offset for this block */
1483 xfs_extnum_t nextents; /* number of extent entries */
1484
1485 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1486 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1487 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1488 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1489 *first_unused = 0;
1490 return 0;
1491 }
1492 ifp = XFS_IFORK_PTR(ip, whichfork);
1493 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1494 (error = xfs_iread_extents(tp, ip, whichfork)))
1495 return error;
1496 lowest = *first_unused;
1497 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1498 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1499 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1500 off = xfs_bmbt_get_startoff(ep);
1501 /*
1502 * See if the hole before this extent will work.
1503 */
1504 if (off >= lowest + len && off - max >= len) {
1505 *first_unused = max;
1506 return 0;
1507 }
1508 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1509 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1510 }
1511 *first_unused = max;
1512 return 0;
1513}
1514
1515/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001516 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001517 * last_block (input value) in the file.
1518 * This is not based on i_size, it is based on the extent records.
1519 * Returns 0 for local files, as they do not have extent records.
1520 */
1521int /* error */
1522xfs_bmap_last_before(
1523 xfs_trans_t *tp, /* transaction pointer */
1524 xfs_inode_t *ip, /* incore inode */
1525 xfs_fileoff_t *last_block, /* last block */
1526 int whichfork) /* data or attr fork */
1527{
1528 xfs_fileoff_t bno; /* input file offset */
1529 int eof; /* hit end of file */
1530 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1531 int error; /* error return value */
1532 xfs_bmbt_irec_t got; /* current extent value */
1533 xfs_ifork_t *ifp; /* inode fork pointer */
1534 xfs_extnum_t lastx; /* last extent used */
1535 xfs_bmbt_irec_t prev; /* previous extent value */
1536
1537 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1538 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1539 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +10001540 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001541 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1542 *last_block = 0;
1543 return 0;
1544 }
1545 ifp = XFS_IFORK_PTR(ip, whichfork);
1546 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1547 (error = xfs_iread_extents(tp, ip, whichfork)))
1548 return error;
1549 bno = *last_block - 1;
1550 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1551 &prev);
1552 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1553 if (prev.br_startoff == NULLFILEOFF)
1554 *last_block = 0;
1555 else
1556 *last_block = prev.br_startoff + prev.br_blockcount;
1557 }
1558 /*
1559 * Otherwise *last_block is already the right answer.
1560 */
1561 return 0;
1562}
1563
Dave Chinner68988112013-08-12 20:49:42 +10001564int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001565xfs_bmap_last_extent(
1566 struct xfs_trans *tp,
1567 struct xfs_inode *ip,
1568 int whichfork,
1569 struct xfs_bmbt_irec *rec,
1570 int *is_empty)
1571{
1572 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1573 int error;
1574 int nextents;
1575
1576 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1577 error = xfs_iread_extents(tp, ip, whichfork);
1578 if (error)
1579 return error;
1580 }
1581
1582 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1583 if (nextents == 0) {
1584 *is_empty = 1;
1585 return 0;
1586 }
1587
1588 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1589 *is_empty = 0;
1590 return 0;
1591}
1592
1593/*
1594 * Check the last inode extent to determine whether this allocation will result
1595 * in blocks being allocated at the end of the file. When we allocate new data
1596 * blocks at the end of the file which do not start at the previous data block,
1597 * we will try to align the new blocks at stripe unit boundaries.
1598 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001599 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001600 * at, or past the EOF.
1601 */
1602STATIC int
1603xfs_bmap_isaeof(
1604 struct xfs_bmalloca *bma,
1605 int whichfork)
1606{
1607 struct xfs_bmbt_irec rec;
1608 int is_empty;
1609 int error;
1610
1611 bma->aeof = 0;
1612 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1613 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001614 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001615 return error;
1616
Dave Chinner6e708bc2013-11-22 10:41:16 +11001617 if (is_empty) {
1618 bma->aeof = 1;
1619 return 0;
1620 }
1621
Dave Chinner9e5987a2013-02-25 12:31:26 +11001622 /*
1623 * Check if we are allocation or past the last extent, or at least into
1624 * the last delayed allocated extent.
1625 */
1626 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1627 (bma->offset >= rec.br_startoff &&
1628 isnullstartblock(rec.br_startblock));
1629 return 0;
1630}
1631
1632/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001633 * Returns the file-relative block number of the first block past eof in
1634 * the file. This is not based on i_size, it is based on the extent records.
1635 * Returns 0 for local files, as they do not have extent records.
1636 */
1637int
1638xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001639 struct xfs_inode *ip,
1640 xfs_fileoff_t *last_block,
1641 int whichfork)
1642{
1643 struct xfs_bmbt_irec rec;
1644 int is_empty;
1645 int error;
1646
1647 *last_block = 0;
1648
1649 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1650 return 0;
1651
1652 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1653 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001654 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001655
1656 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1657 if (error || is_empty)
1658 return error;
1659
1660 *last_block = rec.br_startoff + rec.br_blockcount;
1661 return 0;
1662}
1663
1664/*
1665 * Returns whether the selected fork of the inode has exactly one
1666 * block or not. For the data fork we check this matches di_size,
1667 * implying the file's range is 0..bsize-1.
1668 */
1669int /* 1=>1 block, 0=>otherwise */
1670xfs_bmap_one_block(
1671 xfs_inode_t *ip, /* incore inode */
1672 int whichfork) /* data or attr fork */
1673{
1674 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1675 xfs_ifork_t *ifp; /* inode fork pointer */
1676 int rval; /* return value */
1677 xfs_bmbt_irec_t s; /* internal version of extent */
1678
1679#ifndef DEBUG
1680 if (whichfork == XFS_DATA_FORK)
1681 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1682#endif /* !DEBUG */
1683 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1684 return 0;
1685 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1686 return 0;
1687 ifp = XFS_IFORK_PTR(ip, whichfork);
1688 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1689 ep = xfs_iext_get_ext(ifp, 0);
1690 xfs_bmbt_get_all(ep, &s);
1691 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1692 if (rval && whichfork == XFS_DATA_FORK)
1693 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1694 return rval;
1695}
1696
1697/*
1698 * Extent tree manipulation functions used during allocation.
1699 */
1700
1701/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001702 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 */
1704STATIC int /* error */
1705xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001706 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001708 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001710 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001713 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 xfs_fileoff_t new_endoff; /* end offset of new entry */
1715 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1716 /* left is 0, right is 1, prev is 2 */
1717 int rval=0; /* return value (logging flags) */
1718 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001719 xfs_filblks_t da_new; /* new count del alloc blocks used */
1720 xfs_filblks_t da_old; /* old count del alloc blocks used */
1721 xfs_filblks_t temp=0; /* value for da_new calculations */
1722 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001724 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Eric Sandeenc29aad42015-02-23 22:39:08 +11001726 mp = bma->tp ? bma->tp->t_mountp : NULL;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001727 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001728
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001729 ASSERT(bma->idx >= 0);
1730 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001731 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001732 ASSERT(!bma->cur ||
1733 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001734
1735 XFS_STATS_INC(xs_add_exlist);
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737#define LEFT r[0]
1738#define RIGHT r[1]
1739#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
1741 /*
1742 * Set up a bunch of variables to make the tests simpler.
1743 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001744 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 xfs_bmbt_get_all(ep, &PREV);
1746 new_endoff = new->br_startoff + new->br_blockcount;
1747 ASSERT(PREV.br_startoff <= new->br_startoff);
1748 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001749
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001750 da_old = startblockval(PREV.br_startblock);
1751 da_new = 0;
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 /*
1754 * Set flags determining what part of the previous delayed allocation
1755 * extent is being replaced by a real allocation.
1756 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001757 if (PREV.br_startoff == new->br_startoff)
1758 state |= BMAP_LEFT_FILLING;
1759 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1760 state |= BMAP_RIGHT_FILLING;
1761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 /*
1763 * Check and set flags if this segment has a left neighbor.
1764 * Don't set contiguous if the combined extent would be too large.
1765 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001766 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001767 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001768 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001769
1770 if (isnullstartblock(LEFT.br_startblock))
1771 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001773
1774 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1775 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1776 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1777 LEFT.br_state == new->br_state &&
1778 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1779 state |= BMAP_LEFT_CONTIG;
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 /*
1782 * Check and set flags if this segment has a right neighbor.
1783 * Don't set contiguous if the combined extent would be too large.
1784 * Also check for all-three-contiguous being too large.
1785 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001786 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001787 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001788 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001789
1790 if (isnullstartblock(RIGHT.br_startblock))
1791 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001793
1794 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1795 new_endoff == RIGHT.br_startoff &&
1796 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1797 new->br_state == RIGHT.br_state &&
1798 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1799 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1800 BMAP_RIGHT_FILLING)) !=
1801 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1802 BMAP_RIGHT_FILLING) ||
1803 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1804 <= MAXEXTLEN))
1805 state |= BMAP_RIGHT_CONTIG;
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 error = 0;
1808 /*
1809 * Switch out based on the FILLING and CONTIG state bits.
1810 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001811 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1812 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1813 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1814 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 /*
1816 * Filling in all of a previously delayed allocation extent.
1817 * The left and right neighbors are both contiguous with new.
1818 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 bma->idx--;
1820 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1821 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 LEFT.br_blockcount + PREV.br_blockcount +
1823 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001824 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001825
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001826 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1827 bma->ip->i_d.di_nextents--;
1828 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1830 else {
1831 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001832 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001834 RIGHT.br_blockcount, &i);
1835 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001837 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001838 error = xfs_btree_delete(bma->cur, &i);
1839 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001841 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001842 error = xfs_btree_decrement(bma->cur, 0, &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_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 LEFT.br_startblock,
1848 LEFT.br_blockcount +
1849 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001850 RIGHT.br_blockcount, LEFT.br_state);
1851 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto done;
1853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 break;
1855
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001856 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /*
1858 * Filling in all of a previously delayed allocation extent.
1859 * The left neighbor is contiguous, the right is not.
1860 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001861 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001862
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001863 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1864 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001865 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001866 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001867
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001868 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1869 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 rval = XFS_ILOG_DEXT;
1871 else {
1872 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001873 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001875 &i);
1876 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001878 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001879 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 LEFT.br_startblock,
1881 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001882 PREV.br_blockcount, LEFT.br_state);
1883 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 goto done;
1885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 break;
1887
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001888 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 /*
1890 * Filling in all of a previously delayed allocation extent.
1891 * The right neighbor is contiguous, the left is not.
1892 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001893 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 xfs_bmbt_set_startblock(ep, new->br_startblock);
1895 xfs_bmbt_set_blockcount(ep,
1896 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001897 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001898
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001899 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1900 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 rval = XFS_ILOG_DEXT;
1902 else {
1903 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001904 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001906 RIGHT.br_blockcount, &i);
1907 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001909 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001910 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 new->br_startblock,
1912 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001913 RIGHT.br_blockcount, PREV.br_state);
1914 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 goto done;
1916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 break;
1918
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001919 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 /*
1921 * Filling in all of a previously delayed allocation extent.
1922 * Neither the left nor right neighbors are contiguous with
1923 * the new one.
1924 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001925 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001927 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001928
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001929 bma->ip->i_d.di_nextents++;
1930 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1932 else {
1933 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001934 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001936 &i);
1937 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001939 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001940 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1941 error = xfs_btree_insert(bma->cur, &i);
1942 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001944 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 break;
1947
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001948 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 /*
1950 * Filling in the first part of a previous delayed allocation.
1951 * The left neighbor is contiguous.
1952 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001953 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1954 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 LEFT.br_blockcount + new->br_blockcount);
1956 xfs_bmbt_set_startoff(ep,
1957 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001961 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001963 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 rval = XFS_ILOG_DEXT;
1965 else {
1966 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001967 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001969 &i);
1970 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001972 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001973 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 LEFT.br_startblock,
1975 LEFT.br_blockcount +
1976 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001977 LEFT.br_state);
1978 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 goto done;
1980 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001981 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001982 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001983 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001984 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001985
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001986 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 break;
1988
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001989 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 /*
1991 * Filling in the first part of a previous delayed allocation.
1992 * The left neighbor is not contiguous.
1993 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001994 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 xfs_bmbt_set_startoff(ep, new_endoff);
1996 temp = PREV.br_blockcount - new->br_blockcount;
1997 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001998 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
1999 bma->ip->i_d.di_nextents++;
2000 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2002 else {
2003 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002004 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002006 &i);
2007 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002009 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002010 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2011 error = xfs_btree_insert(bma->cur, &i);
2012 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002014 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002016
2017 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002018 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2019 bma->firstblock, bma->flist,
2020 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 rval |= tmp_rval;
2022 if (error)
2023 goto done;
2024 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002025 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002026 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002027 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2028 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002029 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002030 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 break;
2032
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002033 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 /*
2035 * Filling in the last part of a previous delayed allocation.
2036 * The right neighbor is contiguous with the new allocation.
2037 */
2038 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002039 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002041 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002042 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 new->br_blockcount + RIGHT.br_blockcount,
2044 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002045 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2046 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 rval = XFS_ILOG_DEXT;
2048 else {
2049 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002050 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002052 RIGHT.br_blockcount, &i);
2053 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002055 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002056 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 new->br_startblock,
2058 new->br_blockcount +
2059 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002060 RIGHT.br_state);
2061 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 goto done;
2063 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002064
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002065 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002066 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002067 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002068 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002069 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002070
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002071 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 break;
2073
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002074 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 /*
2076 * Filling in the last part of a previous delayed allocation.
2077 * The right neighbor is not contiguous.
2078 */
2079 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002080 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002082 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2083 bma->ip->i_d.di_nextents++;
2084 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2086 else {
2087 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002088 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002090 &i);
2091 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002093 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002094 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2095 error = xfs_btree_insert(bma->cur, &i);
2096 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002098 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002100
2101 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002102 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2103 bma->firstblock, bma->flist, &bma->cur, 1,
2104 &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 rval |= tmp_rval;
2106 if (error)
2107 goto done;
2108 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002109 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002110 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002111 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2112 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002113 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002114 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002115
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002116 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 break;
2118
2119 case 0:
2120 /*
2121 * Filling in the middle part of a previous delayed allocation.
2122 * Contiguity is impossible here.
2123 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002124 *
2125 * We start with a delayed allocation:
2126 *
2127 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2128 * PREV @ idx
2129 *
2130 * and we are allocating:
2131 * +rrrrrrrrrrrrrrrrr+
2132 * new
2133 *
2134 * and we set it up for insertion as:
2135 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2136 * new
2137 * PREV @ idx LEFT RIGHT
2138 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 */
2140 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002142 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002143 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2144 LEFT = *new;
2145 RIGHT.br_state = PREV.br_state;
2146 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002147 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002148 RIGHT.br_startoff = new_endoff;
2149 RIGHT.br_blockcount = temp2;
2150 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002151 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2152 bma->ip->i_d.di_nextents++;
2153 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2155 else {
2156 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002157 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002159 &i);
2160 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002162 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002163 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2164 error = xfs_btree_insert(bma->cur, &i);
2165 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002167 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002169
2170 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002171 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2172 bma->firstblock, bma->flist, &bma->cur,
2173 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 rval |= tmp_rval;
2175 if (error)
2176 goto done;
2177 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002178 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2179 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002180 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002181 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002182 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002183 error = xfs_mod_fdblocks(bma->ip->i_mount,
2184 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002185 ASSERT(!error);
2186 if (error)
2187 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002189
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002190 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002191 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002192 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2193 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2194 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002195 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002196 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002197
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002198 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002199 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 break;
2201
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002202 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2203 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2204 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2205 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2206 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2207 case BMAP_LEFT_CONTIG:
2208 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 /*
2210 * These cases are all impossible.
2211 */
2212 ASSERT(0);
2213 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002214
2215 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002216 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002217 int tmp_logflags; /* partial log flag return val */
2218
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002219 ASSERT(bma->cur == NULL);
2220 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2221 bma->firstblock, bma->flist, &bma->cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002222 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002223 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002224 if (error)
2225 goto done;
2226 }
2227
2228 /* adjust for changes in reserved delayed indirect blocks */
2229 if (da_old || da_new) {
2230 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002231 if (bma->cur)
2232 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002233 ASSERT(temp <= da_old);
2234 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002235 xfs_mod_fdblocks(bma->ip->i_mount,
2236 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002237 }
2238
2239 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002240 if (bma->cur)
2241 bma->cur->bc_private.b.allocated = 0;
2242
2243 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002245 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return error;
2247#undef LEFT
2248#undef RIGHT
2249#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250}
2251
2252/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002253 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 */
2255STATIC int /* error */
2256xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002257 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002259 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002261 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002262 xfs_fsblock_t *first, /* pointer to firstblock variable */
2263 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002264 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002267 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002270 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 xfs_fileoff_t new_endoff; /* end offset of new entry */
2272 xfs_exntst_t newext; /* new extent state */
2273 xfs_exntst_t oldext; /* old extent state */
2274 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2275 /* left is 0, right is 1, prev is 2 */
2276 int rval=0; /* return value (logging flags) */
2277 int state = 0;/* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002278 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002280 *logflagsp = 0;
2281
2282 cur = *curp;
2283 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2284
2285 ASSERT(*idx >= 0);
2286 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2287 ASSERT(!isnullstartblock(new->br_startblock));
2288
2289 XFS_STATS_INC(xs_add_exlist);
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291#define LEFT r[0]
2292#define RIGHT r[1]
2293#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002294
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /*
2296 * Set up a bunch of variables to make the tests simpler.
2297 */
2298 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002299 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 xfs_bmbt_get_all(ep, &PREV);
2301 newext = new->br_state;
2302 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2303 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2304 ASSERT(PREV.br_state == oldext);
2305 new_endoff = new->br_startoff + new->br_blockcount;
2306 ASSERT(PREV.br_startoff <= new->br_startoff);
2307 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002308
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 /*
2310 * Set flags determining what part of the previous oldext allocation
2311 * extent is being replaced by a newext allocation.
2312 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002313 if (PREV.br_startoff == new->br_startoff)
2314 state |= BMAP_LEFT_FILLING;
2315 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2316 state |= BMAP_RIGHT_FILLING;
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 /*
2319 * Check and set flags if this segment has a left neighbor.
2320 * Don't set contiguous if the combined extent would be too large.
2321 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002322 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002323 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002324 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002325
2326 if (isnullstartblock(LEFT.br_startblock))
2327 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002329
2330 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2331 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2332 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2333 LEFT.br_state == newext &&
2334 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2335 state |= BMAP_LEFT_CONTIG;
2336
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 /*
2338 * Check and set flags if this segment has a right neighbor.
2339 * Don't set contiguous if the combined extent would be too large.
2340 * Also check for all-three-contiguous being too large.
2341 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002342 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002343 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002344 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002345 if (isnullstartblock(RIGHT.br_startblock))
2346 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002348
2349 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2350 new_endoff == RIGHT.br_startoff &&
2351 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2352 newext == RIGHT.br_state &&
2353 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2354 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2355 BMAP_RIGHT_FILLING)) !=
2356 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2357 BMAP_RIGHT_FILLING) ||
2358 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2359 <= MAXEXTLEN))
2360 state |= BMAP_RIGHT_CONTIG;
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 /*
2363 * Switch out based on the FILLING and CONTIG state bits.
2364 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002365 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2366 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2367 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2368 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 /*
2370 * Setting all of a previous oldext extent to newext.
2371 * The left and right neighbors are both contiguous with new.
2372 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002373 --*idx;
2374
2375 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2376 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 LEFT.br_blockcount + PREV.br_blockcount +
2378 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002379 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002380
Christoph Hellwigec90c552011-05-23 08:52:53 +00002381 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 ip->i_d.di_nextents -= 2;
2383 if (cur == NULL)
2384 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2385 else {
2386 rval = XFS_ILOG_CORE;
2387 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2388 RIGHT.br_startblock,
2389 RIGHT.br_blockcount, &i)))
2390 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002391 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002392 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002394 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002395 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002397 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002398 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002400 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002401 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002403 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2405 LEFT.br_startblock,
2406 LEFT.br_blockcount + PREV.br_blockcount +
2407 RIGHT.br_blockcount, LEFT.br_state)))
2408 goto done;
2409 }
2410 break;
2411
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002412 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 /*
2414 * Setting all of a previous oldext extent to newext.
2415 * The left neighbor is contiguous, the right is not.
2416 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002417 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002418
Christoph Hellwigec90c552011-05-23 08:52:53 +00002419 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2420 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2421 LEFT.br_blockcount + PREV.br_blockcount);
2422 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2423
2424 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 ip->i_d.di_nextents--;
2426 if (cur == NULL)
2427 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2428 else {
2429 rval = XFS_ILOG_CORE;
2430 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2431 PREV.br_startblock, PREV.br_blockcount,
2432 &i)))
2433 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002434 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002435 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002437 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002438 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002440 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2442 LEFT.br_startblock,
2443 LEFT.br_blockcount + PREV.br_blockcount,
2444 LEFT.br_state)))
2445 goto done;
2446 }
2447 break;
2448
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002449 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 /*
2451 * Setting all of a previous oldext extent to newext.
2452 * The right neighbor is contiguous, the left is not.
2453 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002454 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 xfs_bmbt_set_blockcount(ep,
2456 PREV.br_blockcount + RIGHT.br_blockcount);
2457 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002458 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2459 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 ip->i_d.di_nextents--;
2461 if (cur == NULL)
2462 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2463 else {
2464 rval = XFS_ILOG_CORE;
2465 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2466 RIGHT.br_startblock,
2467 RIGHT.br_blockcount, &i)))
2468 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002469 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002470 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002472 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002473 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002475 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2477 new->br_startblock,
2478 new->br_blockcount + RIGHT.br_blockcount,
2479 newext)))
2480 goto done;
2481 }
2482 break;
2483
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002484 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 /*
2486 * Setting all of a previous oldext extent to newext.
2487 * Neither the left nor right neighbors are contiguous with
2488 * the new one.
2489 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002490 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002492 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002493
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 if (cur == NULL)
2495 rval = XFS_ILOG_DEXT;
2496 else {
2497 rval = 0;
2498 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2499 new->br_startblock, new->br_blockcount,
2500 &i)))
2501 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002502 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2504 new->br_startblock, new->br_blockcount,
2505 newext)))
2506 goto done;
2507 }
2508 break;
2509
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002510 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 /*
2512 * Setting the first part of a previous oldext extent to newext.
2513 * The left neighbor is contiguous.
2514 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002515 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2516 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 LEFT.br_blockcount + new->br_blockcount);
2518 xfs_bmbt_set_startoff(ep,
2519 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002520 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002521
Christoph Hellwigec90c552011-05-23 08:52:53 +00002522 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 xfs_bmbt_set_startblock(ep,
2524 new->br_startblock + new->br_blockcount);
2525 xfs_bmbt_set_blockcount(ep,
2526 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002527 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002528
Christoph Hellwigec90c552011-05-23 08:52:53 +00002529 --*idx;
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (cur == NULL)
2532 rval = XFS_ILOG_DEXT;
2533 else {
2534 rval = 0;
2535 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2536 PREV.br_startblock, PREV.br_blockcount,
2537 &i)))
2538 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002539 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 if ((error = xfs_bmbt_update(cur,
2541 PREV.br_startoff + new->br_blockcount,
2542 PREV.br_startblock + new->br_blockcount,
2543 PREV.br_blockcount - new->br_blockcount,
2544 oldext)))
2545 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002546 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002548 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 LEFT.br_startblock,
2550 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002551 LEFT.br_state);
2552 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 goto done;
2554 }
2555 break;
2556
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002557 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 /*
2559 * Setting the first part of a previous oldext extent to newext.
2560 * The left neighbor is not contiguous.
2561 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002562 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2564 xfs_bmbt_set_startoff(ep, new_endoff);
2565 xfs_bmbt_set_blockcount(ep,
2566 PREV.br_blockcount - new->br_blockcount);
2567 xfs_bmbt_set_startblock(ep,
2568 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002569 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002570
Christoph Hellwigec90c552011-05-23 08:52:53 +00002571 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 ip->i_d.di_nextents++;
2573 if (cur == NULL)
2574 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2575 else {
2576 rval = XFS_ILOG_CORE;
2577 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2578 PREV.br_startblock, PREV.br_blockcount,
2579 &i)))
2580 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002581 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if ((error = xfs_bmbt_update(cur,
2583 PREV.br_startoff + new->br_blockcount,
2584 PREV.br_startblock + new->br_blockcount,
2585 PREV.br_blockcount - new->br_blockcount,
2586 oldext)))
2587 goto done;
2588 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002589 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002591 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 }
2593 break;
2594
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002595 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 /*
2597 * Setting the last part of a previous oldext extent to newext.
2598 * The right neighbor is contiguous with the new allocation.
2599 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002600 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 xfs_bmbt_set_blockcount(ep,
2602 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002603 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2604
2605 ++*idx;
2606
2607 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2608 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002609 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002611 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (cur == NULL)
2614 rval = XFS_ILOG_DEXT;
2615 else {
2616 rval = 0;
2617 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2618 PREV.br_startblock,
2619 PREV.br_blockcount, &i)))
2620 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002621 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2623 PREV.br_startblock,
2624 PREV.br_blockcount - new->br_blockcount,
2625 oldext)))
2626 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002627 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 goto done;
2629 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2630 new->br_startblock,
2631 new->br_blockcount + RIGHT.br_blockcount,
2632 newext)))
2633 goto done;
2634 }
2635 break;
2636
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002637 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 /*
2639 * Setting the last part of a previous oldext extent to newext.
2640 * The right neighbor is not contiguous.
2641 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002642 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 xfs_bmbt_set_blockcount(ep,
2644 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002645 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002646
Christoph Hellwigec90c552011-05-23 08:52:53 +00002647 ++*idx;
2648 xfs_iext_insert(ip, *idx, 1, new, state);
2649
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 ip->i_d.di_nextents++;
2651 if (cur == NULL)
2652 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2653 else {
2654 rval = XFS_ILOG_CORE;
2655 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2656 PREV.br_startblock, PREV.br_blockcount,
2657 &i)))
2658 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002659 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2661 PREV.br_startblock,
2662 PREV.br_blockcount - new->br_blockcount,
2663 oldext)))
2664 goto done;
2665 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2666 new->br_startblock, new->br_blockcount,
2667 &i)))
2668 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002669 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002671 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002673 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
2675 break;
2676
2677 case 0:
2678 /*
2679 * Setting the middle part of a previous oldext extent to
2680 * newext. Contiguity is impossible here.
2681 * One extent becomes three extents.
2682 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002683 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 xfs_bmbt_set_blockcount(ep,
2685 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002686 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 r[0] = *new;
2689 r[1].br_startoff = new_endoff;
2690 r[1].br_blockcount =
2691 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2692 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2693 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002694
2695 ++*idx;
2696 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 ip->i_d.di_nextents += 2;
2699 if (cur == NULL)
2700 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2701 else {
2702 rval = XFS_ILOG_CORE;
2703 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2704 PREV.br_startblock, PREV.br_blockcount,
2705 &i)))
2706 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002707 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 /* new right extent - oldext */
2709 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2710 r[1].br_startblock, r[1].br_blockcount,
2711 r[1].br_state)))
2712 goto done;
2713 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002715 cur->bc_rec.b.br_blockcount =
2716 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002717 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002719 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002720 /*
2721 * Reset the cursor to the position of the new extent
2722 * we are about to insert as we can't trust it after
2723 * the previous insert.
2724 */
2725 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2726 new->br_startblock, new->br_blockcount,
2727 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002729 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002731 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002732 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002734 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 }
2736 break;
2737
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002738 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2739 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2740 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2741 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2742 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2743 case BMAP_LEFT_CONTIG:
2744 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 /*
2746 * These cases are all impossible.
2747 */
2748 ASSERT(0);
2749 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002750
2751 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002752 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002753 int tmp_logflags; /* partial log flag return val */
2754
2755 ASSERT(cur == NULL);
2756 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
2757 0, &tmp_logflags, XFS_DATA_FORK);
2758 *logflagsp |= tmp_logflags;
2759 if (error)
2760 goto done;
2761 }
2762
2763 /* clear out the allocated field, done with it now in any case. */
2764 if (cur) {
2765 cur->bc_private.b.allocated = 0;
2766 *curp = cur;
2767 }
2768
2769 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002771 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return error;
2773#undef LEFT
2774#undef RIGHT
2775#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
2777
2778/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002779 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002781STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782xfs_bmap_add_extent_hole_delay(
2783 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002784 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002785 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002787 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2789 xfs_filblks_t newlen=0; /* new indirect size */
2790 xfs_filblks_t oldlen=0; /* old indirect size */
2791 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2792 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002793 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002795 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002797 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 /*
2800 * Check and set flags if this segment has a left neighbor
2801 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002802 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002803 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002804 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002805
2806 if (isnullstartblock(left.br_startblock))
2807 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 /*
2811 * Check and set flags if the current (right) segment exists.
2812 * If it doesn't exist, we're converting the hole at end-of-file.
2813 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002814 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002815 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002816 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002817
2818 if (isnullstartblock(right.br_startblock))
2819 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002821
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 /*
2823 * Set contiguity flags on the left and right neighbors.
2824 * Don't let extents get too large, even if the pieces are contiguous.
2825 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002826 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2827 left.br_startoff + left.br_blockcount == new->br_startoff &&
2828 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2829 state |= BMAP_LEFT_CONTIG;
2830
2831 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2832 new->br_startoff + new->br_blockcount == right.br_startoff &&
2833 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2834 (!(state & BMAP_LEFT_CONTIG) ||
2835 (left.br_blockcount + new->br_blockcount +
2836 right.br_blockcount <= MAXEXTLEN)))
2837 state |= BMAP_RIGHT_CONTIG;
2838
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 /*
2840 * Switch out based on the contiguity flags.
2841 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002842 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2843 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 /*
2845 * New allocation is contiguous with delayed allocations
2846 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002847 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002849 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 temp = left.br_blockcount + new->br_blockcount +
2851 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002852
Christoph Hellwigec90c552011-05-23 08:52:53 +00002853 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2854 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002855 oldlen = startblockval(left.br_startblock) +
2856 startblockval(new->br_startblock) +
2857 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002859 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002860 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002861 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002862
Christoph Hellwigec90c552011-05-23 08:52:53 +00002863 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 break;
2865
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002866 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 /*
2868 * New allocation is contiguous with a delayed allocation
2869 * on the left.
2870 * Merge the new allocation with the left neighbor.
2871 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002872 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002874
2875 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2876 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002877 oldlen = startblockval(left.br_startblock) +
2878 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002880 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002881 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002882 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 break;
2884
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002885 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 /*
2887 * New allocation is contiguous with a delayed allocation
2888 * on the right.
2889 * Merge the new allocation with the right neighbor.
2890 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002891 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002893 oldlen = startblockval(new->br_startblock) +
2894 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002896 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2897 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002898 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002899 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 break;
2901
2902 case 0:
2903 /*
2904 * New allocation is not contiguous with another
2905 * delayed allocation.
2906 * Insert a new entry.
2907 */
2908 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002909 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 break;
2911 }
2912 if (oldlen != newlen) {
2913 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002914 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2915 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 /*
2917 * Nothing to do for disk quota accounting here.
2918 */
2919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
2922/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002923 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 */
2925STATIC int /* error */
2926xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002927 struct xfs_bmalloca *bma,
2928 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002930 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 int i; /* temp state */
2933 xfs_ifork_t *ifp; /* inode fork pointer */
2934 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2935 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002936 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002938 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Eric Sandeenc29aad42015-02-23 22:39:08 +11002940 mp = bma->tp ? bma->tp->t_mountp : NULL;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002941 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002942
Christoph Hellwigc6534242011-09-18 20:41:05 +00002943 ASSERT(bma->idx >= 0);
2944 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002945 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002946 ASSERT(!bma->cur ||
2947 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002948
2949 XFS_STATS_INC(xs_add_exlist);
2950
2951 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002952 if (whichfork == XFS_ATTR_FORK)
2953 state |= BMAP_ATTRFORK;
2954
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 /*
2956 * Check and set flags if this segment has a left neighbor.
2957 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002958 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002959 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002960 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002961 if (isnullstartblock(left.br_startblock))
2962 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 /*
2966 * Check and set flags if this segment has a current value.
2967 * Not true if we're inserting into the "hole" at eof.
2968 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002969 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002970 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002971 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002972 if (isnullstartblock(right.br_startblock))
2973 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 /*
2977 * We're inserting a real allocation between "left" and "right".
2978 * Set the contiguity flags. Don't let extents get too large.
2979 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002980 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2981 left.br_startoff + left.br_blockcount == new->br_startoff &&
2982 left.br_startblock + left.br_blockcount == new->br_startblock &&
2983 left.br_state == new->br_state &&
2984 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2985 state |= BMAP_LEFT_CONTIG;
2986
2987 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2988 new->br_startoff + new->br_blockcount == right.br_startoff &&
2989 new->br_startblock + new->br_blockcount == right.br_startblock &&
2990 new->br_state == right.br_state &&
2991 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2992 (!(state & BMAP_LEFT_CONTIG) ||
2993 left.br_blockcount + new->br_blockcount +
2994 right.br_blockcount <= MAXEXTLEN))
2995 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002997 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 /*
2999 * Select which case we're in here, and implement it.
3000 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003001 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3002 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 /*
3004 * New allocation is contiguous with real allocations on the
3005 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003006 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003008 --bma->idx;
3009 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3010 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 left.br_blockcount + new->br_blockcount +
3012 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003013 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003014
Christoph Hellwigc6534242011-09-18 20:41:05 +00003015 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003016
Christoph Hellwigc6534242011-09-18 20:41:05 +00003017 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3018 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3019 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003020 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003021 } else {
3022 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003023 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3024 right.br_startblock, right.br_blockcount,
3025 &i);
3026 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003027 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003028 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003029 error = xfs_btree_delete(bma->cur, &i);
3030 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003031 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003032 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003033 error = xfs_btree_decrement(bma->cur, 0, &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_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003038 left.br_startblock,
3039 left.br_blockcount +
3040 new->br_blockcount +
3041 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003042 left.br_state);
3043 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003044 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003046 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003048 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 /*
3050 * New allocation is contiguous with a real allocation
3051 * on the left.
3052 * Merge the new allocation with the left neighbor.
3053 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003054 --bma->idx;
3055 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3056 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003058 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003059
Christoph Hellwigc6534242011-09-18 20:41:05 +00003060 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003061 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003062 } else {
3063 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003064 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3065 left.br_startblock, left.br_blockcount,
3066 &i);
3067 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003068 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003069 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003070 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003071 left.br_startblock,
3072 left.br_blockcount +
3073 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003074 left.br_state);
3075 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003076 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003078 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003080 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 /*
3082 * New allocation is contiguous with a real allocation
3083 * on the right.
3084 * Merge the new allocation with the right neighbor.
3085 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003086 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3087 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003088 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 new->br_blockcount + right.br_blockcount,
3090 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003091 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003092
Christoph Hellwigc6534242011-09-18 20:41:05 +00003093 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003094 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003095 } else {
3096 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003097 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003098 right.br_startoff,
3099 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003100 right.br_blockcount, &i);
3101 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003102 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003103 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003104 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003105 new->br_startblock,
3106 new->br_blockcount +
3107 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003108 right.br_state);
3109 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003110 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003112 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113
3114 case 0:
3115 /*
3116 * New allocation is not contiguous with another
3117 * real allocation.
3118 * Insert a new entry.
3119 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003120 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3121 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3122 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3123 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003124 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003125 } else {
3126 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003127 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003128 new->br_startoff,
3129 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003130 new->br_blockcount, &i);
3131 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003132 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003133 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003134 bma->cur->bc_rec.b.br_state = new->br_state;
3135 error = xfs_btree_insert(bma->cur, &i);
3136 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003137 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003138 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003140 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003142
3143 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003144 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003145 int tmp_logflags; /* partial log flag return val */
3146
Christoph Hellwigc6534242011-09-18 20:41:05 +00003147 ASSERT(bma->cur == NULL);
3148 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3149 bma->firstblock, bma->flist, &bma->cur,
3150 0, &tmp_logflags, whichfork);
3151 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003152 if (error)
3153 goto done;
3154 }
3155
3156 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003157 if (bma->cur)
3158 bma->cur->bc_private.b.allocated = 0;
3159
3160 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003161done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003162 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003163 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
Nathan Scottdd9f4382006-01-11 15:28:28 +11003166/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003167 * Functions used in the extent read, allocate and remove paths
3168 */
3169
3170/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003171 * Adjust the size of the new extent based on di_extsize and rt extsize.
3172 */
Dave Chinner68988112013-08-12 20:49:42 +10003173int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003174xfs_bmap_extsize_align(
3175 xfs_mount_t *mp,
3176 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3177 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3178 xfs_extlen_t extsz, /* align to this extent size */
3179 int rt, /* is this a realtime inode? */
3180 int eof, /* is extent at end-of-file? */
3181 int delay, /* creating delalloc extent? */
3182 int convert, /* overwriting unwritten extent? */
3183 xfs_fileoff_t *offp, /* in/out: aligned offset */
3184 xfs_extlen_t *lenp) /* in/out: aligned length */
3185{
3186 xfs_fileoff_t orig_off; /* original offset */
3187 xfs_extlen_t orig_alen; /* original length */
3188 xfs_fileoff_t orig_end; /* original off+len */
3189 xfs_fileoff_t nexto; /* next file offset */
3190 xfs_fileoff_t prevo; /* previous file offset */
3191 xfs_fileoff_t align_off; /* temp for offset */
3192 xfs_extlen_t align_alen; /* temp for length */
3193 xfs_extlen_t temp; /* temp for calculations */
3194
3195 if (convert)
3196 return 0;
3197
3198 orig_off = align_off = *offp;
3199 orig_alen = align_alen = *lenp;
3200 orig_end = orig_off + orig_alen;
3201
3202 /*
3203 * If this request overlaps an existing extent, then don't
3204 * attempt to perform any additional alignment.
3205 */
3206 if (!delay && !eof &&
3207 (orig_off >= gotp->br_startoff) &&
3208 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3209 return 0;
3210 }
3211
3212 /*
3213 * If the file offset is unaligned vs. the extent size
3214 * we need to align it. This will be possible unless
3215 * the file was previously written with a kernel that didn't
3216 * perform this alignment, or if a truncate shot us in the
3217 * foot.
3218 */
3219 temp = do_mod(orig_off, extsz);
3220 if (temp) {
3221 align_alen += temp;
3222 align_off -= temp;
3223 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003224
3225 /* Same adjustment for the end of the requested area. */
3226 temp = (align_alen % extsz);
3227 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003228 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003229
3230 /*
3231 * For large extent hint sizes, the aligned extent might be larger than
3232 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3233 * the length back under MAXEXTLEN. The outer allocation loops handle
3234 * short allocation just fine, so it is safe to do this. We only want to
3235 * do it when we are forced to, though, because it means more allocation
3236 * operations are required.
3237 */
3238 while (align_alen > MAXEXTLEN)
3239 align_alen -= extsz;
3240 ASSERT(align_alen <= MAXEXTLEN);
3241
Nathan Scottdd9f4382006-01-11 15:28:28 +11003242 /*
3243 * If the previous block overlaps with this proposed allocation
3244 * then move the start forward without adjusting the length.
3245 */
3246 if (prevp->br_startoff != NULLFILEOFF) {
3247 if (prevp->br_startblock == HOLESTARTBLOCK)
3248 prevo = prevp->br_startoff;
3249 else
3250 prevo = prevp->br_startoff + prevp->br_blockcount;
3251 } else
3252 prevo = 0;
3253 if (align_off != orig_off && align_off < prevo)
3254 align_off = prevo;
3255 /*
3256 * If the next block overlaps with this proposed allocation
3257 * then move the start back without adjusting the length,
3258 * but not before offset 0.
3259 * This may of course make the start overlap previous block,
3260 * and if we hit the offset 0 limit then the next block
3261 * can still overlap too.
3262 */
3263 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3264 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3265 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3266 nexto = gotp->br_startoff + gotp->br_blockcount;
3267 else
3268 nexto = gotp->br_startoff;
3269 } else
3270 nexto = NULLFILEOFF;
3271 if (!eof &&
3272 align_off + align_alen != orig_end &&
3273 align_off + align_alen > nexto)
3274 align_off = nexto > align_alen ? nexto - align_alen : 0;
3275 /*
3276 * If we're now overlapping the next or previous extent that
3277 * means we can't fit an extsz piece in this hole. Just move
3278 * the start forward to the first valid spot and set
3279 * the length so we hit the end.
3280 */
3281 if (align_off != orig_off && align_off < prevo)
3282 align_off = prevo;
3283 if (align_off + align_alen != orig_end &&
3284 align_off + align_alen > nexto &&
3285 nexto != NULLFILEOFF) {
3286 ASSERT(nexto > prevo);
3287 align_alen = nexto - align_off;
3288 }
3289
3290 /*
3291 * If realtime, and the result isn't a multiple of the realtime
3292 * extent size we need to remove blocks until it is.
3293 */
3294 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3295 /*
3296 * We're not covering the original request, or
3297 * we won't be able to once we fix the length.
3298 */
3299 if (orig_off < align_off ||
3300 orig_end > align_off + align_alen ||
3301 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003302 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003303 /*
3304 * Try to fix it by moving the start up.
3305 */
3306 if (align_off + temp <= orig_off) {
3307 align_alen -= temp;
3308 align_off += temp;
3309 }
3310 /*
3311 * Try to fix it by moving the end in.
3312 */
3313 else if (align_off + align_alen - temp >= orig_end)
3314 align_alen -= temp;
3315 /*
3316 * Set the start to the minimum then trim the length.
3317 */
3318 else {
3319 align_alen -= orig_off - align_off;
3320 align_off = orig_off;
3321 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3322 }
3323 /*
3324 * Result doesn't cover the request, fail it.
3325 */
3326 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003327 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003328 } else {
3329 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003330 /* see MAXEXTLEN handling above */
3331 ASSERT(orig_end <= align_off + align_alen ||
3332 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003333 }
3334
3335#ifdef DEBUG
3336 if (!eof && gotp->br_startoff != NULLFILEOFF)
3337 ASSERT(align_off + align_alen <= gotp->br_startoff);
3338 if (prevp->br_startoff != NULLFILEOFF)
3339 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3340#endif
3341
3342 *lenp = align_alen;
3343 *offp = align_off;
3344 return 0;
3345}
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347#define XFS_ALLOC_GAP_UNITS 4
3348
Dave Chinner68988112013-08-12 20:49:42 +10003349void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003350xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003351 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352{
3353 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3355 xfs_mount_t *mp; /* mount point structure */
3356 int nullfb; /* true if ap->firstblock isn't set */
3357 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358
3359#define ISVALID(x,y) \
3360 (rt ? \
3361 (x) < mp->m_sb.sb_rblocks : \
3362 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3363 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3364 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3365
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003367 nullfb = *ap->firstblock == NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003369 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 /*
3371 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003372 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003374 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3375 !isnullstartblock(ap->prev.br_startblock) &&
3376 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3377 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003378 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 /*
3380 * Adjust for the gap between prevp and us.
3381 */
Dave Chinner3a756672011-09-18 20:40:58 +00003382 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003383 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003385 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3386 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 }
3388 /*
3389 * If not at eof, then compare the two neighbor blocks.
3390 * Figure out whether either one gives us a good starting point,
3391 * and pick the better one.
3392 */
3393 else if (!ap->eof) {
3394 xfs_fsblock_t gotbno; /* right side block number */
3395 xfs_fsblock_t gotdiff=0; /* right side difference */
3396 xfs_fsblock_t prevbno; /* left side block number */
3397 xfs_fsblock_t prevdiff=0; /* left side difference */
3398
3399 /*
3400 * If there's a previous (left) block, select a requested
3401 * start block based on it.
3402 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003403 if (ap->prev.br_startoff != NULLFILEOFF &&
3404 !isnullstartblock(ap->prev.br_startblock) &&
3405 (prevbno = ap->prev.br_startblock +
3406 ap->prev.br_blockcount) &&
3407 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 /*
3409 * Calculate gap to end of previous block.
3410 */
Dave Chinner3a756672011-09-18 20:40:58 +00003411 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003412 (ap->prev.br_startoff +
3413 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 /*
3415 * Figure the startblock based on the previous block's
3416 * end and the gap size.
3417 * Heuristic!
3418 * If the gap is large relative to the piece we're
3419 * allocating, or using it gives us an invalid block
3420 * number, then just use the end of the previous block.
3421 */
Dave Chinner3a756672011-09-18 20:40:58 +00003422 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003424 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 prevbno += adjust;
3426 else
3427 prevdiff += adjust;
3428 /*
3429 * If the firstblock forbids it, can't use it,
3430 * must use default.
3431 */
3432 if (!rt && !nullfb &&
3433 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3434 prevbno = NULLFSBLOCK;
3435 }
3436 /*
3437 * No previous block or can't follow it, just default.
3438 */
3439 else
3440 prevbno = NULLFSBLOCK;
3441 /*
3442 * If there's a following (right) block, select a requested
3443 * start block based on it.
3444 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003445 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 /*
3447 * Calculate gap to start of next block.
3448 */
Dave Chinner3a756672011-09-18 20:40:58 +00003449 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 /*
3451 * Figure the startblock based on the next block's
3452 * start and the gap size.
3453 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003454 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 /*
3456 * Heuristic!
3457 * If the gap is large relative to the piece we're
3458 * allocating, or using it gives us an invalid block
3459 * number, then just use the start of the next block
3460 * offset by our length.
3461 */
Dave Chinner3a756672011-09-18 20:40:58 +00003462 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 ISVALID(gotbno - gotdiff, gotbno))
3464 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003465 else if (ISVALID(gotbno - ap->length, gotbno)) {
3466 gotbno -= ap->length;
3467 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 } else
3469 gotdiff += adjust;
3470 /*
3471 * If the firstblock forbids it, can't use it,
3472 * must use default.
3473 */
3474 if (!rt && !nullfb &&
3475 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3476 gotbno = NULLFSBLOCK;
3477 }
3478 /*
3479 * No next block, just default.
3480 */
3481 else
3482 gotbno = NULLFSBLOCK;
3483 /*
3484 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003485 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486 */
3487 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003488 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003490 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003492 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003494#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003495}
3496
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003497static int
3498xfs_bmap_longest_free_extent(
3499 struct xfs_trans *tp,
3500 xfs_agnumber_t ag,
3501 xfs_extlen_t *blen,
3502 int *notinit)
3503{
3504 struct xfs_mount *mp = tp->t_mountp;
3505 struct xfs_perag *pag;
3506 xfs_extlen_t longest;
3507 int error = 0;
3508
3509 pag = xfs_perag_get(mp, ag);
3510 if (!pag->pagf_init) {
3511 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3512 if (error)
3513 goto out;
3514
3515 if (!pag->pagf_init) {
3516 *notinit = 1;
3517 goto out;
3518 }
3519 }
3520
Dave Chinner50adbcb2015-06-22 10:04:31 +10003521 longest = xfs_alloc_longest_free_extent(mp, pag,
Dave Chinner496817b2015-06-22 10:13:30 +10003522 xfs_alloc_min_freelist(mp, pag));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003523 if (*blen < longest)
3524 *blen = longest;
3525
3526out:
3527 xfs_perag_put(pag);
3528 return error;
3529}
3530
3531static void
3532xfs_bmap_select_minlen(
3533 struct xfs_bmalloca *ap,
3534 struct xfs_alloc_arg *args,
3535 xfs_extlen_t *blen,
3536 int notinit)
3537{
3538 if (notinit || *blen < ap->minlen) {
3539 /*
3540 * Since we did a BUF_TRYLOCK above, it is possible that
3541 * there is space for this request.
3542 */
3543 args->minlen = ap->minlen;
3544 } else if (*blen < args->maxlen) {
3545 /*
3546 * If the best seen length is less than the request length,
3547 * use the best as the minimum.
3548 */
3549 args->minlen = *blen;
3550 } else {
3551 /*
3552 * Otherwise we've seen an extent as big as maxlen, use that
3553 * as the minimum.
3554 */
3555 args->minlen = args->maxlen;
3556 }
3557}
3558
Nathan Scotta365bdd2006-03-14 13:34:16 +11003559STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003560xfs_bmap_btalloc_nullfb(
3561 struct xfs_bmalloca *ap,
3562 struct xfs_alloc_arg *args,
3563 xfs_extlen_t *blen)
3564{
3565 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003566 xfs_agnumber_t ag, startag;
3567 int notinit = 0;
3568 int error;
3569
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003570 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003571 args->total = ap->total;
3572
Christoph Hellwigc467c042010-02-15 23:34:42 +00003573 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3574 if (startag == NULLAGNUMBER)
3575 startag = ag = 0;
3576
Dave Chinner14b064c2011-01-27 12:16:28 +11003577 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003578 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3579 &notinit);
3580 if (error)
3581 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003582
Christoph Hellwigc467c042010-02-15 23:34:42 +00003583 if (++ag == mp->m_sb.sb_agcount)
3584 ag = 0;
3585 if (ag == startag)
3586 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003587 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003588
3589 xfs_bmap_select_minlen(ap, args, blen, notinit);
3590 return 0;
3591}
3592
3593STATIC int
3594xfs_bmap_btalloc_filestreams(
3595 struct xfs_bmalloca *ap,
3596 struct xfs_alloc_arg *args,
3597 xfs_extlen_t *blen)
3598{
3599 struct xfs_mount *mp = ap->ip->i_mount;
3600 xfs_agnumber_t ag;
3601 int notinit = 0;
3602 int error;
3603
3604 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3605 args->total = ap->total;
3606
3607 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3608 if (ag == NULLAGNUMBER)
3609 ag = 0;
3610
3611 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3612 if (error)
3613 return error;
3614
3615 if (*blen < args->maxlen) {
3616 error = xfs_filestream_new_ag(ap, &ag);
3617 if (error)
3618 return error;
3619
3620 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3621 &notinit);
3622 if (error)
3623 return error;
3624
3625 }
3626
3627 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003628
3629 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003630 * Set the failure fallback case to look in the selected AG as stream
3631 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003632 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003633 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003634 return 0;
3635}
3636
3637STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003638xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003639 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003640{
3641 xfs_mount_t *mp; /* mount point structure */
3642 xfs_alloctype_t atype = 0; /* type for allocation routines */
3643 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003644 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003645 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003646 xfs_alloc_arg_t args;
3647 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003648 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003649 int nullfb; /* true if ap->firstblock isn't set */
3650 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003651 int tryagain;
3652 int error;
Dave Chinner33177f02013-12-12 16:34:36 +11003653 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003654
Dave Chinnera99ebf42011-12-01 11:24:20 +00003655 ASSERT(ap->length);
3656
Nathan Scotta365bdd2006-03-14 13:34:16 +11003657 mp = ap->ip->i_mount;
Dave Chinner33177f02013-12-12 16:34:36 +11003658
3659 /* stripe alignment for allocation is determined by mount parameters */
3660 stripe_align = 0;
3661 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3662 stripe_align = mp->m_swidth;
3663 else if (mp->m_dalign)
3664 stripe_align = mp->m_dalign;
3665
David Chinner957d0eb2007-06-18 16:50:37 +10003666 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003667 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003668 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003669 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003670 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003671 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003672 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003673 }
Dave Chinner33177f02013-12-12 16:34:36 +11003674
3675
Dave Chinner0937e0f2011-09-18 20:40:57 +00003676 nullfb = *ap->firstblock == NULLFSBLOCK;
3677 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003678 if (nullfb) {
3679 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
3680 ag = xfs_filestream_lookup_ag(ap->ip);
3681 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003682 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003683 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003684 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003685 }
3686 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003687 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003688
3689 xfs_bmap_adjacent(ap);
3690
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003692 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 * it's in the right allocation group.
3694 */
Dave Chinner3a756672011-09-18 20:40:58 +00003695 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696 ;
3697 else
Dave Chinner3a756672011-09-18 20:40:58 +00003698 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 * Normal allocation, done through xfs_alloc_vextent.
3701 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003702 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003703 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003704 args.tp = ap->tp;
3705 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003706 args.fsbno = ap->blkno;
Dave Chinner14b064c2011-01-27 12:16:28 +11003707
3708 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner3a756672011-09-18 20:40:58 +00003709 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003710 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003711 blen = 0;
3712 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003713 /*
3714 * Search for an allocation group with a single extent large
3715 * enough for the request. If one isn't found, then adjust
3716 * the minimum allocation size to the largest space found.
3717 */
3718 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3719 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3720 else
3721 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003722 if (error)
3723 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003724 } else if (ap->flist->xbf_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003725 if (xfs_inode_is_filestream(ap->ip))
3726 args.type = XFS_ALLOCTYPE_FIRST_AG;
3727 else
3728 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003729 args.total = args.minlen = ap->minlen;
3730 } else {
3731 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3732 args.total = ap->total;
3733 args.minlen = ap->minlen;
3734 }
David Chinner957d0eb2007-06-18 16:50:37 +10003735 /* apply extent size hints if obtained earlier */
3736 if (unlikely(align)) {
3737 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003738 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003739 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11003740 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003741 args.prod = 1;
3742 args.mod = 0;
3743 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003744 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003745 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003746 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3747 }
3748 /*
3749 * If we are not low on available data blocks, and the
3750 * underlying logical volume manager is a stripe, and
3751 * the file offset is zero then try to allocate data
3752 * blocks on stripe unit boundary.
3753 * NOTE: ap->aeof is only set if the allocation length
3754 * is >= the stripe unit and the allocation offset is
3755 * at the end of file.
3756 */
Dave Chinner0937e0f2011-09-18 20:40:57 +00003757 if (!ap->flist->xbf_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003758 if (!ap->offset) {
Dave Chinner33177f02013-12-12 16:34:36 +11003759 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003760 atype = args.type;
3761 isaligned = 1;
3762 /*
3763 * Adjust for alignment
3764 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003765 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003766 args.minlen = blen - args.alignment;
3767 args.minalignslop = 0;
3768 } else {
3769 /*
3770 * First try an exact bno allocation.
3771 * If it fails then do a near or start bno
3772 * allocation with alignment turned on.
3773 */
3774 atype = args.type;
3775 tryagain = 1;
3776 args.type = XFS_ALLOCTYPE_THIS_BNO;
3777 args.alignment = 1;
3778 /*
3779 * Compute the minlen+alignment for the
3780 * next case. Set slop so that the value
3781 * of minlen+alignment+slop doesn't go up
3782 * between the calls.
3783 */
Dave Chinner33177f02013-12-12 16:34:36 +11003784 if (blen > stripe_align && blen <= args.maxlen)
3785 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003786 else
3787 nextminlen = args.minlen;
Dave Chinner33177f02013-12-12 16:34:36 +11003788 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003789 args.minalignslop =
Dave Chinner33177f02013-12-12 16:34:36 +11003790 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003791 args.minlen - 1;
3792 else
3793 args.minalignslop = 0;
3794 }
3795 } else {
3796 args.alignment = 1;
3797 args.minalignslop = 0;
3798 }
3799 args.minleft = ap->minleft;
3800 args.wasdel = ap->wasdel;
3801 args.isfl = 0;
3802 args.userdata = ap->userdata;
3803 if ((error = xfs_alloc_vextent(&args)))
3804 return error;
3805 if (tryagain && args.fsbno == NULLFSBLOCK) {
3806 /*
3807 * Exact allocation failed. Now try with alignment
3808 * turned on.
3809 */
3810 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003811 args.fsbno = ap->blkno;
Dave Chinner33177f02013-12-12 16:34:36 +11003812 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003813 args.minlen = nextminlen;
3814 args.minalignslop = 0;
3815 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 if ((error = xfs_alloc_vextent(&args)))
3817 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003818 }
3819 if (isaligned && args.fsbno == NULLFSBLOCK) {
3820 /*
3821 * allocation failed, so turn off alignment and
3822 * try again.
3823 */
3824 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003825 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003826 args.alignment = 0;
3827 if ((error = xfs_alloc_vextent(&args)))
3828 return error;
3829 }
3830 if (args.fsbno == NULLFSBLOCK && nullfb &&
3831 args.minlen > ap->minlen) {
3832 args.minlen = ap->minlen;
3833 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003834 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003835 if ((error = xfs_alloc_vextent(&args)))
3836 return error;
3837 }
3838 if (args.fsbno == NULLFSBLOCK && nullfb) {
3839 args.fsbno = 0;
3840 args.type = XFS_ALLOCTYPE_FIRST_AG;
3841 args.total = ap->minlen;
3842 args.minleft = 0;
3843 if ((error = xfs_alloc_vextent(&args)))
3844 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003845 ap->flist->xbf_low = 1;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003846 }
3847 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003848 /*
3849 * check the allocation happened at the same or higher AG than
3850 * the first block that was allocated.
3851 */
3852 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3853 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3854 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
3855 (ap->flist->xbf_low &&
3856 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3857 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3858
Dave Chinner3a756672011-09-18 20:40:58 +00003859 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003860 if (*ap->firstblock == NULLFSBLOCK)
3861 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003862 ASSERT(nullfb || fb_agno == args.agno ||
Dave Chinner0937e0f2011-09-18 20:40:57 +00003863 (ap->flist->xbf_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003864 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003865 ap->ip->i_d.di_nblocks += args.len;
3866 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3867 if (ap->wasdel)
3868 ap->ip->i_delayed_blks -= args.len;
3869 /*
3870 * Adjust the disk quota also. This was reserved
3871 * earlier.
3872 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003873 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003874 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3875 XFS_TRANS_DQ_BCOUNT,
3876 (long) args.len);
3877 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003878 ap->blkno = NULLFSBLOCK;
3879 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 }
3881 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003882}
3883
3884/*
3885 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3886 * It figures out where to ask the underlying allocator to put the new extent.
3887 */
3888STATIC int
3889xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003890 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003891{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003892 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003893 return xfs_bmap_rtalloc(ap);
3894 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895}
3896
3897/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003898 * Trim the returned map to the required bounds
3899 */
3900STATIC void
3901xfs_bmapi_trim_map(
3902 struct xfs_bmbt_irec *mval,
3903 struct xfs_bmbt_irec *got,
3904 xfs_fileoff_t *bno,
3905 xfs_filblks_t len,
3906 xfs_fileoff_t obno,
3907 xfs_fileoff_t end,
3908 int n,
3909 int flags)
3910{
3911 if ((flags & XFS_BMAPI_ENTIRE) ||
3912 got->br_startoff + got->br_blockcount <= obno) {
3913 *mval = *got;
3914 if (isnullstartblock(got->br_startblock))
3915 mval->br_startblock = DELAYSTARTBLOCK;
3916 return;
3917 }
3918
3919 if (obno > *bno)
3920 *bno = obno;
3921 ASSERT((*bno >= obno) || (n == 0));
3922 ASSERT(*bno < end);
3923 mval->br_startoff = *bno;
3924 if (isnullstartblock(got->br_startblock))
3925 mval->br_startblock = DELAYSTARTBLOCK;
3926 else
3927 mval->br_startblock = got->br_startblock +
3928 (*bno - got->br_startoff);
3929 /*
3930 * Return the minimum of what we got and what we asked for for
3931 * the length. We can use the len variable here because it is
3932 * modified below and we could have been there before coming
3933 * here if the first part of the allocation didn't overlap what
3934 * was asked for.
3935 */
3936 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3937 got->br_blockcount - (*bno - got->br_startoff));
3938 mval->br_state = got->br_state;
3939 ASSERT(mval->br_blockcount <= len);
3940 return;
3941}
3942
3943/*
3944 * Update and validate the extent map to return
3945 */
3946STATIC void
3947xfs_bmapi_update_map(
3948 struct xfs_bmbt_irec **map,
3949 xfs_fileoff_t *bno,
3950 xfs_filblks_t *len,
3951 xfs_fileoff_t obno,
3952 xfs_fileoff_t end,
3953 int *n,
3954 int flags)
3955{
3956 xfs_bmbt_irec_t *mval = *map;
3957
3958 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3959 ((mval->br_startoff + mval->br_blockcount) <= end));
3960 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3961 (mval->br_startoff < obno));
3962
3963 *bno = mval->br_startoff + mval->br_blockcount;
3964 *len = end - *bno;
3965 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3966 /* update previous map with new information */
3967 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3968 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3969 ASSERT(mval->br_state == mval[-1].br_state);
3970 mval[-1].br_blockcount = mval->br_blockcount;
3971 mval[-1].br_state = mval->br_state;
3972 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3973 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3974 mval[-1].br_startblock != HOLESTARTBLOCK &&
3975 mval->br_startblock == mval[-1].br_startblock +
3976 mval[-1].br_blockcount &&
3977 ((flags & XFS_BMAPI_IGSTATE) ||
3978 mval[-1].br_state == mval->br_state)) {
3979 ASSERT(mval->br_startoff ==
3980 mval[-1].br_startoff + mval[-1].br_blockcount);
3981 mval[-1].br_blockcount += mval->br_blockcount;
3982 } else if (*n > 0 &&
3983 mval->br_startblock == DELAYSTARTBLOCK &&
3984 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3985 mval->br_startoff ==
3986 mval[-1].br_startoff + mval[-1].br_blockcount) {
3987 mval[-1].br_blockcount += mval->br_blockcount;
3988 mval[-1].br_state = mval->br_state;
3989 } else if (!((*n == 0) &&
3990 ((mval->br_startoff + mval->br_blockcount) <=
3991 obno))) {
3992 mval++;
3993 (*n)++;
3994 }
3995 *map = mval;
3996}
3997
3998/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003999 * Map file blocks to filesystem blocks without allocation.
4000 */
4001int
4002xfs_bmapi_read(
4003 struct xfs_inode *ip,
4004 xfs_fileoff_t bno,
4005 xfs_filblks_t len,
4006 struct xfs_bmbt_irec *mval,
4007 int *nmap,
4008 int flags)
4009{
4010 struct xfs_mount *mp = ip->i_mount;
4011 struct xfs_ifork *ifp;
4012 struct xfs_bmbt_irec got;
4013 struct xfs_bmbt_irec prev;
4014 xfs_fileoff_t obno;
4015 xfs_fileoff_t end;
4016 xfs_extnum_t lastx;
4017 int error;
4018 int eof;
4019 int n = 0;
4020 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4021 XFS_ATTR_FORK : XFS_DATA_FORK;
4022
4023 ASSERT(*nmap >= 1);
4024 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4025 XFS_BMAPI_IGSTATE)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004026 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004027
4028 if (unlikely(XFS_TEST_ERROR(
4029 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4030 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4031 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4032 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004033 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004034 }
4035
4036 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004037 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004038
4039 XFS_STATS_INC(xs_blk_mapr);
4040
4041 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004042
4043 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4044 error = xfs_iread_extents(NULL, ip, whichfork);
4045 if (error)
4046 return error;
4047 }
4048
4049 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4050 end = bno + len;
4051 obno = bno;
4052
4053 while (bno < end && n < *nmap) {
4054 /* Reading past eof, act as though there's a hole up to end. */
4055 if (eof)
4056 got.br_startoff = end;
4057 if (got.br_startoff > bno) {
4058 /* Reading in a hole. */
4059 mval->br_startoff = bno;
4060 mval->br_startblock = HOLESTARTBLOCK;
4061 mval->br_blockcount =
4062 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4063 mval->br_state = XFS_EXT_NORM;
4064 bno += mval->br_blockcount;
4065 len -= mval->br_blockcount;
4066 mval++;
4067 n++;
4068 continue;
4069 }
4070
4071 /* set up the extent map to return. */
4072 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4073 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4074
4075 /* If we're done, stop now. */
4076 if (bno >= end || n >= *nmap)
4077 break;
4078
4079 /* Else go on to the next record. */
4080 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4081 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4082 else
4083 eof = 1;
4084 }
4085 *nmap = n;
4086 return 0;
4087}
4088
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004089STATIC int
4090xfs_bmapi_reserve_delalloc(
4091 struct xfs_inode *ip,
4092 xfs_fileoff_t aoff,
4093 xfs_filblks_t len,
4094 struct xfs_bmbt_irec *got,
4095 struct xfs_bmbt_irec *prev,
4096 xfs_extnum_t *lastx,
4097 int eof)
4098{
4099 struct xfs_mount *mp = ip->i_mount;
4100 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4101 xfs_extlen_t alen;
4102 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004103 char rt = XFS_IS_REALTIME_INODE(ip);
4104 xfs_extlen_t extsz;
4105 int error;
4106
4107 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4108 if (!eof)
4109 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4110
4111 /* Figure out the extent size, adjust alen */
4112 extsz = xfs_get_extsz_hint(ip);
4113 if (extsz) {
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004114 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4115 1, 0, &aoff, &alen);
4116 ASSERT(!error);
4117 }
4118
4119 if (rt)
4120 extsz = alen / mp->m_sb.sb_rextsize;
4121
4122 /*
4123 * Make a transaction-less quota reservation for delayed allocation
4124 * blocks. This number gets adjusted later. We return if we haven't
4125 * allocated blocks already inside this loop.
4126 */
4127 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4128 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4129 if (error)
4130 return error;
4131
4132 /*
4133 * Split changing sb for alen and indlen since they could be coming
4134 * from different places.
4135 */
4136 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4137 ASSERT(indlen > 0);
4138
4139 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004140 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004141 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004142 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004143 }
4144
4145 if (error)
4146 goto out_unreserve_quota;
4147
Dave Chinner0d485ad2015-02-23 21:22:03 +11004148 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004149 if (error)
4150 goto out_unreserve_blocks;
4151
4152
4153 ip->i_delayed_blks += alen;
4154
4155 got->br_startoff = aoff;
4156 got->br_startblock = nullstartblock(indlen);
4157 got->br_blockcount = alen;
4158 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004159 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004160
4161 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004162 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4163 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004164 */
4165 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4166
4167 ASSERT(got->br_startoff <= aoff);
4168 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4169 ASSERT(isnullstartblock(got->br_startblock));
4170 ASSERT(got->br_state == XFS_EXT_NORM);
4171 return 0;
4172
4173out_unreserve_blocks:
4174 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004175 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004176 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004177 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004178out_unreserve_quota:
4179 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004180 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004181 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4182 return error;
4183}
4184
Dave Chinner5c8ed202011-09-18 20:40:45 +00004185/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004186 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4187 */
4188int
4189xfs_bmapi_delay(
4190 struct xfs_inode *ip, /* incore inode */
4191 xfs_fileoff_t bno, /* starting file offs. mapped */
4192 xfs_filblks_t len, /* length to map in file */
4193 struct xfs_bmbt_irec *mval, /* output: map values */
4194 int *nmap, /* i/o: mval size/count */
4195 int flags) /* XFS_BMAPI_... */
4196{
4197 struct xfs_mount *mp = ip->i_mount;
4198 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4199 struct xfs_bmbt_irec got; /* current file extent record */
4200 struct xfs_bmbt_irec prev; /* previous file extent record */
4201 xfs_fileoff_t obno; /* old block number (offset) */
4202 xfs_fileoff_t end; /* end of mapped file region */
4203 xfs_extnum_t lastx; /* last useful extent number */
4204 int eof; /* we've hit the end of extents */
4205 int n = 0; /* current extent index */
4206 int error = 0;
4207
4208 ASSERT(*nmap >= 1);
4209 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4210 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004211 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig44032802011-09-18 20:40:48 +00004212
4213 if (unlikely(XFS_TEST_ERROR(
4214 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4215 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4216 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4217 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004218 return -EFSCORRUPTED;
Christoph Hellwig44032802011-09-18 20:40:48 +00004219 }
4220
4221 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004222 return -EIO;
Christoph Hellwig44032802011-09-18 20:40:48 +00004223
4224 XFS_STATS_INC(xs_blk_mapw);
4225
4226 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4227 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4228 if (error)
4229 return error;
4230 }
4231
4232 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4233 end = bno + len;
4234 obno = bno;
4235
4236 while (bno < end && n < *nmap) {
4237 if (eof || got.br_startoff > bno) {
4238 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4239 &prev, &lastx, eof);
4240 if (error) {
4241 if (n == 0) {
4242 *nmap = 0;
4243 return error;
4244 }
4245 break;
4246 }
4247 }
4248
4249 /* set up the extent map to return. */
4250 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4251 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4252
4253 /* If we're done, stop now. */
4254 if (bno >= end || n >= *nmap)
4255 break;
4256
4257 /* Else go on to the next record. */
4258 prev = got;
4259 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4260 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4261 else
4262 eof = 1;
4263 }
4264
4265 *nmap = n;
4266 return 0;
4267}
4268
4269
Dave Chinnercf11da92014-07-15 07:08:24 +10004270static int
4271xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004272 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004273{
4274 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004275 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004276 XFS_ATTR_FORK : XFS_DATA_FORK;
4277 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004278 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004279 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004280
Dave Chinnera99ebf42011-12-01 11:24:20 +00004281 ASSERT(bma->length > 0);
4282
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004283 /*
4284 * For the wasdelay case, we could also just allocate the stuff asked
4285 * for in this bmap call but that wouldn't be as good.
4286 */
4287 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004288 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4289 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004290 if (bma->idx != NULLEXTNUM && bma->idx) {
4291 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004292 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004293 }
4294 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004295 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004296 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004297 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004298 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004299 }
4300
4301 /*
4302 * Indicate if this is the first user data in the file, or just any
4303 * user data.
4304 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004305 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004306 bma->userdata = (bma->offset == 0) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004307 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4308 }
4309
Dave Chinnere04426b2012-10-05 11:06:59 +10004310 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004311
4312 /*
4313 * Only want to do the alignment at the eof if it is userdata and
4314 * allocation length is larger than a stripe unit.
4315 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004316 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004317 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004318 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004319 if (error)
4320 return error;
4321 }
4322
4323 error = xfs_bmap_alloc(bma);
4324 if (error)
4325 return error;
4326
Dave Chinner0937e0f2011-09-18 20:40:57 +00004327 if (bma->flist->xbf_low)
4328 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004329 if (bma->cur)
4330 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004331 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004332 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004333 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4334 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4335 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4336 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004337 }
4338 /*
4339 * Bump the number of extents we've allocated
4340 * in this call.
4341 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004342 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004343
Dave Chinner29c8d172011-09-18 20:41:00 +00004344 if (bma->cur)
4345 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004346 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4347
Dave Chinner963c30c2011-09-18 20:40:59 +00004348 bma->got.br_startoff = bma->offset;
4349 bma->got.br_startblock = bma->blkno;
4350 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004351 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004352
4353 /*
4354 * A wasdelay extent has been initialized, so shouldn't be flagged
4355 * as unwritten.
4356 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004357 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004358 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004359 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004360
Christoph Hellwigc6534242011-09-18 20:41:05 +00004361 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004362 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004363 else
4364 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004365
Christoph Hellwigc315c902011-09-18 20:41:02 +00004366 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004367 if (error)
4368 return error;
4369
4370 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004371 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4372 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4373 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004374 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004375 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004376
Dave Chinner963c30c2011-09-18 20:40:59 +00004377 ASSERT(bma->got.br_startoff <= bma->offset);
4378 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4379 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004380 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4381 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004382 return 0;
4383}
4384
Dave Chinnerb447fe52011-09-18 20:40:51 +00004385STATIC int
4386xfs_bmapi_convert_unwritten(
4387 struct xfs_bmalloca *bma,
4388 struct xfs_bmbt_irec *mval,
4389 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004390 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004391{
4392 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4393 XFS_ATTR_FORK : XFS_DATA_FORK;
4394 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004395 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004396 int error;
4397
Dave Chinnerb447fe52011-09-18 20:40:51 +00004398 /* check if we need to do unwritten->real conversion */
4399 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4400 (flags & XFS_BMAPI_PREALLOC))
4401 return 0;
4402
4403 /* check if we need to do real->unwritten conversion */
4404 if (mval->br_state == XFS_EXT_NORM &&
4405 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4406 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4407 return 0;
4408
4409 /*
4410 * Modify (by adding) the state flag, if writing.
4411 */
4412 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004413 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4414 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004415 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004416 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4417 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004418 }
4419 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4420 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4421
Dave Chinnere0c3da52011-09-18 20:41:01 +00004422 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004423 &bma->cur, mval, bma->firstblock, bma->flist,
4424 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004425 /*
4426 * Log the inode core unconditionally in the unwritten extent conversion
4427 * path because the conversion might not have done so (e.g., if the
4428 * extent count hasn't changed). We need to make sure the inode is dirty
4429 * in the transaction for the sake of fsync(), even if nothing has
4430 * changed, because fsync() will not force the log for this transaction
4431 * unless it sees the inode pinned.
4432 */
4433 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004434 if (error)
4435 return error;
4436
4437 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004438 * Update our extent pointer, given that
4439 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4440 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004441 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004442 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004443
4444 /*
4445 * We may have combined previously unwritten space with written space,
4446 * so generate another request.
4447 */
4448 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004449 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004450 return 0;
4451}
4452
Christoph Hellwig44032802011-09-18 20:40:48 +00004453/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004454 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4455 * extent state if necessary. Details behaviour is controlled by the flags
4456 * parameter. Only allocates blocks from a single allocation group, to avoid
4457 * locking problems.
4458 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 * The returned value in "firstblock" from the first call in a transaction
4460 * must be remembered and presented to subsequent calls in "firstblock".
4461 * An upper bound for the number of blocks to be allocated is supplied to
4462 * the first call in "total"; if no allocation group has that many free
4463 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4464 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004465int
4466xfs_bmapi_write(
4467 struct xfs_trans *tp, /* transaction pointer */
4468 struct xfs_inode *ip, /* incore inode */
4469 xfs_fileoff_t bno, /* starting file offs. mapped */
4470 xfs_filblks_t len, /* length to map in file */
4471 int flags, /* XFS_BMAPI_... */
4472 xfs_fsblock_t *firstblock, /* first allocated block
4473 controls a.g. for allocs */
4474 xfs_extlen_t total, /* total blocks needed */
4475 struct xfs_bmbt_irec *mval, /* output: map values */
4476 int *nmap, /* i/o: mval size/count */
4477 struct xfs_bmap_free *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004479 struct xfs_mount *mp = ip->i_mount;
4480 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004481 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004482 xfs_fileoff_t end; /* end of mapped file region */
4483 int eof; /* after the end of extents */
4484 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004485 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004486 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004487 int whichfork; /* data or attr fork */
4488 char inhole; /* current location is hole in file */
4489 char wasdelay; /* old extent was delayed */
4490
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004492 xfs_fileoff_t orig_bno; /* original block number value */
4493 int orig_flags; /* original flags arg value */
4494 xfs_filblks_t orig_len; /* original value of len arg */
4495 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4496 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
4498 orig_bno = bno;
4499 orig_len = len;
4500 orig_flags = flags;
4501 orig_mval = mval;
4502 orig_nmap = *nmap;
4503#endif
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004504 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4505 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004506
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004508 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4509 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4510 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004511 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004512 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004513 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004514
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 if (unlikely(XFS_TEST_ERROR(
4516 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004517 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004519 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004520 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004522
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004524 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004525
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004527
4528 XFS_STATS_INC(xs_blk_mapw);
4529
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004530 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004532 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004534 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004535 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004536 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004537 }
4538
4539 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4540 error = xfs_iread_extents(tp, ip, whichfork);
4541 if (error)
4542 goto error0;
4543 }
4544
Dave Chinnere0c3da52011-09-18 20:41:01 +00004545 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004546 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 n = 0;
4548 end = bno + len;
4549 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004550
4551 bma.tp = tp;
4552 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004553 bma.total = total;
4554 bma.userdata = 0;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004555 bma.flist = flist;
4556 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004557
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004559 inhole = eof || bma.got.br_startoff > bno;
4560 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 /*
4563 * First, deal with the hole before the allocated space
4564 * that we found, if any.
4565 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004566 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004567 bma.eof = eof;
4568 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4569 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004570 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004571 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004572
Dave Chinnera99ebf42011-12-01 11:24:20 +00004573 /*
4574 * There's a 32/64 bit type mismatch between the
4575 * allocation length request (which can be 64 bits in
4576 * length) and the bma length request, which is
4577 * xfs_extlen_t and therefore 32 bits. Hence we have to
4578 * check for 32-bit overflows and handle them here.
4579 */
4580 if (len > (xfs_filblks_t)MAXEXTLEN)
4581 bma.length = MAXEXTLEN;
4582 else
4583 bma.length = len;
4584
4585 ASSERT(len > 0);
4586 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004587 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 if (error)
4589 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004590 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004591 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004593
Dave Chinneraef9a892011-09-18 20:40:44 +00004594 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004595 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4596 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
Dave Chinnerb447fe52011-09-18 20:40:51 +00004598 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004599 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004600 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004601 continue;
4602 if (error)
4603 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Dave Chinneraef9a892011-09-18 20:40:44 +00004605 /* update the extent map to return */
4606 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 /*
4609 * If we're done, stop now. Stop when we've allocated
4610 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4611 * the transaction may get too big.
4612 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004613 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004615
4616 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004617 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004618 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4619 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4620 &bma.got);
4621 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00004622 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004625
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 /*
4627 * Transform from btree to extents, give it cur.
4628 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004629 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004630 int tmp_logflags = 0;
4631
Dave Chinner29c8d172011-09-18 20:41:00 +00004632 ASSERT(bma.cur);
4633 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004635 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 if (error)
4637 goto error0;
4638 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004639
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004641 XFS_IFORK_NEXTENTS(ip, whichfork) >
4642 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644error0:
4645 /*
4646 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004647 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004649 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004651 bma.logflags &= ~xfs_ilog_fext(whichfork);
4652 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004654 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 /*
4656 * Log whatever the flags say, even if error. Otherwise we might miss
4657 * detecting a case where the data is changed, there's an error,
4658 * and it's not logged so we don't shutdown when we should.
4659 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004660 if (bma.logflags)
4661 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004662
Dave Chinner29c8d172011-09-18 20:41:00 +00004663 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 if (!error) {
4665 ASSERT(*firstblock == NULLFSBLOCK ||
4666 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4667 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004668 bma.cur->bc_private.b.firstblock) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 (flist->xbf_low &&
4670 XFS_FSB_TO_AGNO(mp, *firstblock) <
4671 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004672 bma.cur->bc_private.b.firstblock)));
4673 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004675 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4677 }
4678 if (!error)
4679 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4680 orig_nmap, *nmap);
4681 return error;
4682}
4683
4684/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004685 * Called by xfs_bmapi to update file extent records and the btree
4686 * after removing space (or undoing a delayed allocation).
4687 */
4688STATIC int /* error */
4689xfs_bmap_del_extent(
4690 xfs_inode_t *ip, /* incore inode pointer */
4691 xfs_trans_t *tp, /* current transaction pointer */
4692 xfs_extnum_t *idx, /* extent number to update/delete */
4693 xfs_bmap_free_t *flist, /* list of extents to be freed */
4694 xfs_btree_cur_t *cur, /* if null, not a btree */
4695 xfs_bmbt_irec_t *del, /* data to remove from extents */
4696 int *logflagsp, /* inode logging flags */
4697 int whichfork) /* data or attr fork */
4698{
4699 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
4700 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
4701 xfs_fsblock_t del_endblock=0; /* first block past del */
4702 xfs_fileoff_t del_endoff; /* first offset past del */
4703 int delay; /* current block is delayed allocated */
4704 int do_fx; /* free extent at end of routine */
4705 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
4706 int error; /* error return value */
4707 int flags; /* inode logging flags */
4708 xfs_bmbt_irec_t got; /* current extent entry */
4709 xfs_fileoff_t got_endoff; /* first offset past got */
4710 int i; /* temp state */
4711 xfs_ifork_t *ifp; /* inode fork pointer */
4712 xfs_mount_t *mp; /* mount structure */
4713 xfs_filblks_t nblks; /* quota/sb block count */
4714 xfs_bmbt_irec_t new; /* new record to be inserted */
4715 /* REFERENCED */
4716 uint qfield; /* quota field to update */
4717 xfs_filblks_t temp; /* for indirect length calculations */
4718 xfs_filblks_t temp2; /* for indirect length calculations */
4719 int state = 0;
4720
4721 XFS_STATS_INC(xs_del_exlist);
4722
4723 if (whichfork == XFS_ATTR_FORK)
4724 state |= BMAP_ATTRFORK;
4725
4726 mp = ip->i_mount;
4727 ifp = XFS_IFORK_PTR(ip, whichfork);
4728 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4729 (uint)sizeof(xfs_bmbt_rec_t)));
4730 ASSERT(del->br_blockcount > 0);
4731 ep = xfs_iext_get_ext(ifp, *idx);
4732 xfs_bmbt_get_all(ep, &got);
4733 ASSERT(got.br_startoff <= del->br_startoff);
4734 del_endoff = del->br_startoff + del->br_blockcount;
4735 got_endoff = got.br_startoff + got.br_blockcount;
4736 ASSERT(got_endoff >= del_endoff);
4737 delay = isnullstartblock(got.br_startblock);
4738 ASSERT(isnullstartblock(del->br_startblock) == delay);
4739 flags = 0;
4740 qfield = 0;
4741 error = 0;
4742 /*
4743 * If deleting a real allocation, must free up the disk space.
4744 */
4745 if (!delay) {
4746 flags = XFS_ILOG_CORE;
4747 /*
4748 * Realtime allocation. Free it and record di_nblocks update.
4749 */
4750 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4751 xfs_fsblock_t bno;
4752 xfs_filblks_t len;
4753
4754 ASSERT(do_mod(del->br_blockcount,
4755 mp->m_sb.sb_rextsize) == 0);
4756 ASSERT(do_mod(del->br_startblock,
4757 mp->m_sb.sb_rextsize) == 0);
4758 bno = del->br_startblock;
4759 len = del->br_blockcount;
4760 do_div(bno, mp->m_sb.sb_rextsize);
4761 do_div(len, mp->m_sb.sb_rextsize);
4762 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4763 if (error)
4764 goto done;
4765 do_fx = 0;
4766 nblks = len * mp->m_sb.sb_rextsize;
4767 qfield = XFS_TRANS_DQ_RTBCOUNT;
4768 }
4769 /*
4770 * Ordinary allocation.
4771 */
4772 else {
4773 do_fx = 1;
4774 nblks = del->br_blockcount;
4775 qfield = XFS_TRANS_DQ_BCOUNT;
4776 }
4777 /*
4778 * Set up del_endblock and cur for later.
4779 */
4780 del_endblock = del->br_startblock + del->br_blockcount;
4781 if (cur) {
4782 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
4783 got.br_startblock, got.br_blockcount,
4784 &i)))
4785 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004786 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004787 }
4788 da_old = da_new = 0;
4789 } else {
4790 da_old = startblockval(got.br_startblock);
4791 da_new = 0;
4792 nblks = 0;
4793 do_fx = 0;
4794 }
4795 /*
4796 * Set flag value to use in switch statement.
4797 * Left-contig is 2, right-contig is 1.
4798 */
4799 switch (((got.br_startoff == del->br_startoff) << 1) |
4800 (got_endoff == del_endoff)) {
4801 case 3:
4802 /*
4803 * Matches the whole extent. Delete the entry.
4804 */
4805 xfs_iext_remove(ip, *idx, 1,
4806 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
4807 --*idx;
4808 if (delay)
4809 break;
4810
4811 XFS_IFORK_NEXT_SET(ip, whichfork,
4812 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4813 flags |= XFS_ILOG_CORE;
4814 if (!cur) {
4815 flags |= xfs_ilog_fext(whichfork);
4816 break;
4817 }
4818 if ((error = xfs_btree_delete(cur, &i)))
4819 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004820 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004821 break;
4822
4823 case 2:
4824 /*
4825 * Deleting the first part of the extent.
4826 */
4827 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4828 xfs_bmbt_set_startoff(ep, del_endoff);
4829 temp = got.br_blockcount - del->br_blockcount;
4830 xfs_bmbt_set_blockcount(ep, temp);
4831 if (delay) {
4832 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4833 da_old);
4834 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4835 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4836 da_new = temp;
4837 break;
4838 }
4839 xfs_bmbt_set_startblock(ep, del_endblock);
4840 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4841 if (!cur) {
4842 flags |= xfs_ilog_fext(whichfork);
4843 break;
4844 }
4845 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
4846 got.br_blockcount - del->br_blockcount,
4847 got.br_state)))
4848 goto done;
4849 break;
4850
4851 case 1:
4852 /*
4853 * Deleting the last part of the extent.
4854 */
4855 temp = got.br_blockcount - del->br_blockcount;
4856 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4857 xfs_bmbt_set_blockcount(ep, temp);
4858 if (delay) {
4859 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4860 da_old);
4861 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4862 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4863 da_new = temp;
4864 break;
4865 }
4866 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4867 if (!cur) {
4868 flags |= xfs_ilog_fext(whichfork);
4869 break;
4870 }
4871 if ((error = xfs_bmbt_update(cur, got.br_startoff,
4872 got.br_startblock,
4873 got.br_blockcount - del->br_blockcount,
4874 got.br_state)))
4875 goto done;
4876 break;
4877
4878 case 0:
4879 /*
4880 * Deleting the middle of the extent.
4881 */
4882 temp = del->br_startoff - got.br_startoff;
4883 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4884 xfs_bmbt_set_blockcount(ep, temp);
4885 new.br_startoff = del_endoff;
4886 temp2 = got_endoff - del_endoff;
4887 new.br_blockcount = temp2;
4888 new.br_state = got.br_state;
4889 if (!delay) {
4890 new.br_startblock = del_endblock;
4891 flags |= XFS_ILOG_CORE;
4892 if (cur) {
4893 if ((error = xfs_bmbt_update(cur,
4894 got.br_startoff,
4895 got.br_startblock, temp,
4896 got.br_state)))
4897 goto done;
4898 if ((error = xfs_btree_increment(cur, 0, &i)))
4899 goto done;
4900 cur->bc_rec.b = new;
4901 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10004902 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004903 goto done;
4904 /*
4905 * If get no-space back from btree insert,
4906 * it tried a split, and we have a zero
4907 * block reservation.
4908 * Fix up our state and return the error.
4909 */
Dave Chinner24513372014-06-25 14:58:08 +10004910 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11004911 /*
4912 * Reset the cursor, don't trust
4913 * it after any insert operation.
4914 */
4915 if ((error = xfs_bmbt_lookup_eq(cur,
4916 got.br_startoff,
4917 got.br_startblock,
4918 temp, &i)))
4919 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004920 XFS_WANT_CORRUPTED_GOTO(mp,
4921 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004922 /*
4923 * Update the btree record back
4924 * to the original value.
4925 */
4926 if ((error = xfs_bmbt_update(cur,
4927 got.br_startoff,
4928 got.br_startblock,
4929 got.br_blockcount,
4930 got.br_state)))
4931 goto done;
4932 /*
4933 * Reset the extent record back
4934 * to the original value.
4935 */
4936 xfs_bmbt_set_blockcount(ep,
4937 got.br_blockcount);
4938 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10004939 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004940 goto done;
4941 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11004942 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004943 } else
4944 flags |= xfs_ilog_fext(whichfork);
4945 XFS_IFORK_NEXT_SET(ip, whichfork,
4946 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
4947 } else {
4948 ASSERT(whichfork == XFS_DATA_FORK);
4949 temp = xfs_bmap_worst_indlen(ip, temp);
4950 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4951 temp2 = xfs_bmap_worst_indlen(ip, temp2);
4952 new.br_startblock = nullstartblock((int)temp2);
4953 da_new = temp + temp2;
4954 while (da_new > da_old) {
4955 if (temp) {
4956 temp--;
4957 da_new--;
4958 xfs_bmbt_set_startblock(ep,
4959 nullstartblock((int)temp));
4960 }
4961 if (da_new == da_old)
4962 break;
4963 if (temp2) {
4964 temp2--;
4965 da_new--;
4966 new.br_startblock =
4967 nullstartblock((int)temp2);
4968 }
4969 }
4970 }
4971 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4972 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
4973 ++*idx;
4974 break;
4975 }
4976 /*
4977 * If we need to, add to list of extents to delete.
4978 */
4979 if (do_fx)
4980 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
4981 mp);
4982 /*
4983 * Adjust inode # blocks in the file.
4984 */
4985 if (nblks)
4986 ip->i_d.di_nblocks -= nblks;
4987 /*
4988 * Adjust quota data.
4989 */
4990 if (qfield)
4991 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
4992
4993 /*
4994 * Account for change in delayed indirect blocks.
4995 * Nothing to do for disk quota accounting here.
4996 */
4997 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11004998 if (da_old > da_new)
4999 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005000done:
5001 *logflagsp = flags;
5002 return error;
5003}
5004
5005/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 * Unmap (remove) blocks from a file.
5007 * If nexts is nonzero then the number of extents to remove is limited to
5008 * that value. If not all extents in the block range can be removed then
5009 * *done is set.
5010 */
5011int /* error */
5012xfs_bunmapi(
5013 xfs_trans_t *tp, /* transaction pointer */
5014 struct xfs_inode *ip, /* incore inode */
5015 xfs_fileoff_t bno, /* starting offset to unmap */
5016 xfs_filblks_t len, /* length to unmap in file */
5017 int flags, /* misc flags */
5018 xfs_extnum_t nexts, /* number of extents max */
5019 xfs_fsblock_t *firstblock, /* first allocated block
5020 controls a.g. for allocs */
5021 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5022 int *done) /* set if not done yet */
5023{
5024 xfs_btree_cur_t *cur; /* bmap btree cursor */
5025 xfs_bmbt_irec_t del; /* extent being deleted */
5026 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005027 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028 int error; /* error return value */
5029 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005030 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 xfs_ifork_t *ifp; /* inode fork pointer */
5032 int isrt; /* freeing in rt area */
5033 xfs_extnum_t lastx; /* last extent index used */
5034 int logflags; /* transaction logging flags */
5035 xfs_extlen_t mod; /* rt extent offset */
5036 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005037 xfs_extnum_t nextents; /* number of file extents */
5038 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039 xfs_fileoff_t start; /* first file offset deleted */
5040 int tmp_logflags; /* partial logging flags */
5041 int wasdel; /* was a delayed alloc extent */
5042 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 xfs_fsblock_t sum;
5044
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005045 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5046
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5048 XFS_ATTR_FORK : XFS_DATA_FORK;
5049 ifp = XFS_IFORK_PTR(ip, whichfork);
5050 if (unlikely(
5051 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5052 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5053 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5054 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005055 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 }
5057 mp = ip->i_mount;
5058 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005059 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005060
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005061 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 ASSERT(len > 0);
5063 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005064
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5066 (error = xfs_iread_extents(tp, ip, whichfork)))
5067 return error;
5068 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5069 if (nextents == 0) {
5070 *done = 1;
5071 return 0;
5072 }
5073 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005074 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075 start = bno;
5076 bno = start + len - 1;
5077 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5078 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005079
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 /*
5081 * Check to see if the given block number is past the end of the
5082 * file, back up to the last block if so...
5083 */
5084 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005085 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086 xfs_bmbt_get_all(ep, &got);
5087 bno = got.br_startoff + got.br_blockcount - 1;
5088 }
5089 logflags = 0;
5090 if (ifp->if_flags & XFS_IFBROOT) {
5091 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005092 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 cur->bc_private.b.firstblock = *firstblock;
5094 cur->bc_private.b.flist = flist;
5095 cur->bc_private.b.flags = 0;
5096 } else
5097 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005098
5099 if (isrt) {
5100 /*
5101 * Synchronize by locking the bitmap inode.
5102 */
5103 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5104 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5105 }
5106
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 extno = 0;
5108 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5109 (nexts == 0 || extno < nexts)) {
5110 /*
5111 * Is the found extent after a hole in which bno lives?
5112 * Just back up to the previous extent, if so.
5113 */
5114 if (got.br_startoff > bno) {
5115 if (--lastx < 0)
5116 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005117 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 xfs_bmbt_get_all(ep, &got);
5119 }
5120 /*
5121 * Is the last block of this extent before the range
5122 * we're supposed to delete? If so, we're done.
5123 */
5124 bno = XFS_FILEOFF_MIN(bno,
5125 got.br_startoff + got.br_blockcount - 1);
5126 if (bno < start)
5127 break;
5128 /*
5129 * Then deal with the (possibly delayed) allocated space
5130 * we found.
5131 */
5132 ASSERT(ep != NULL);
5133 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005134 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 if (got.br_startoff < start) {
5136 del.br_startoff = start;
5137 del.br_blockcount -= start - got.br_startoff;
5138 if (!wasdel)
5139 del.br_startblock += start - got.br_startoff;
5140 }
5141 if (del.br_startoff + del.br_blockcount > bno + 1)
5142 del.br_blockcount = bno + 1 - del.br_startoff;
5143 sum = del.br_startblock + del.br_blockcount;
5144 if (isrt &&
5145 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5146 /*
5147 * Realtime extent not lined up at the end.
5148 * The extent could have been split into written
5149 * and unwritten pieces, or we could just be
5150 * unmapping part of it. But we can't really
5151 * get rid of part of a realtime extent.
5152 */
5153 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005154 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 /*
5156 * This piece is unwritten, or we're not
5157 * using unwritten extents. Skip over it.
5158 */
5159 ASSERT(bno >= mod);
5160 bno -= mod > del.br_blockcount ?
5161 del.br_blockcount : mod;
5162 if (bno < got.br_startoff) {
5163 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005164 xfs_bmbt_get_all(xfs_iext_get_ext(
5165 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005166 }
5167 continue;
5168 }
5169 /*
5170 * It's written, turn it unwritten.
5171 * This is better than zeroing it.
5172 */
5173 ASSERT(del.br_state == XFS_EXT_NORM);
5174 ASSERT(xfs_trans_get_block_res(tp) > 0);
5175 /*
5176 * If this spans a realtime extent boundary,
5177 * chop it back to the start of the one we end at.
5178 */
5179 if (del.br_blockcount > mod) {
5180 del.br_startoff += del.br_blockcount - mod;
5181 del.br_startblock += del.br_blockcount - mod;
5182 del.br_blockcount = mod;
5183 }
5184 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005185 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5186 &lastx, &cur, &del, firstblock, flist,
5187 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188 if (error)
5189 goto error0;
5190 goto nodelete;
5191 }
5192 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5193 /*
5194 * Realtime extent is lined up at the end but not
5195 * at the front. We'll get rid of full extents if
5196 * we can.
5197 */
5198 mod = mp->m_sb.sb_rextsize - mod;
5199 if (del.br_blockcount > mod) {
5200 del.br_blockcount -= mod;
5201 del.br_startoff += mod;
5202 del.br_startblock += mod;
5203 } else if ((del.br_startoff == start &&
5204 (del.br_state == XFS_EXT_UNWRITTEN ||
5205 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005206 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005207 /*
5208 * Can't make it unwritten. There isn't
5209 * a full extent here so just skip it.
5210 */
5211 ASSERT(bno >= del.br_blockcount);
5212 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005213 if (got.br_startoff > bno) {
5214 if (--lastx >= 0) {
5215 ep = xfs_iext_get_ext(ifp,
5216 lastx);
5217 xfs_bmbt_get_all(ep, &got);
5218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 }
5220 continue;
5221 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5222 /*
5223 * This one is already unwritten.
5224 * It must have a written left neighbor.
5225 * Unwrite the killed part of that one and
5226 * try again.
5227 */
5228 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005229 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5230 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005231 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005232 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005233 ASSERT(del.br_startblock ==
5234 prev.br_startblock + prev.br_blockcount);
5235 if (prev.br_startoff < start) {
5236 mod = start - prev.br_startoff;
5237 prev.br_blockcount -= mod;
5238 prev.br_startblock += mod;
5239 prev.br_startoff = start;
5240 }
5241 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005242 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005243 error = xfs_bmap_add_extent_unwritten_real(tp,
5244 ip, &lastx, &cur, &prev,
5245 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246 if (error)
5247 goto error0;
5248 goto nodelete;
5249 } else {
5250 ASSERT(del.br_state == XFS_EXT_NORM);
5251 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005252 error = xfs_bmap_add_extent_unwritten_real(tp,
5253 ip, &lastx, &cur, &del,
5254 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005255 if (error)
5256 goto error0;
5257 goto nodelete;
5258 }
5259 }
5260 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005261 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005262 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005263 if (isrt) {
5264 xfs_filblks_t rtexts;
5265
5266 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5267 do_div(rtexts, mp->m_sb.sb_rextsize);
Dave Chinnerbab98bb2015-02-23 21:22:54 +11005268 xfs_mod_frextents(mp, (int64_t)rtexts);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005269 (void)xfs_trans_reserve_quota_nblks(NULL,
5270 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005271 XFS_QMOPT_RES_RTBLKS);
5272 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11005273 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount,
5274 false);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005275 (void)xfs_trans_reserve_quota_nblks(NULL,
5276 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279 ip->i_delayed_blks -= del.br_blockcount;
5280 if (cur)
5281 cur->bc_private.b.flags |=
5282 XFS_BTCUR_BPRV_WASDEL;
5283 } else if (cur)
5284 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5285 /*
5286 * If it's the case where the directory code is running
5287 * with no block reservation, and the deleted block is in
5288 * the middle of its extent, and the resulting insert
5289 * of an extent would cause transformation to btree format,
5290 * then reject it. The calling code will then swap
5291 * blocks around instead.
5292 * We have to do this now, rather than waiting for the
5293 * conversion to btree format, since the transaction
5294 * will be dirty.
5295 */
5296 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5297 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005298 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5299 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300 del.br_startoff > got.br_startoff &&
5301 del.br_startoff + del.br_blockcount <
5302 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005303 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005304 goto error0;
5305 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00005306 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005307 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005308 logflags |= tmp_logflags;
5309 if (error)
5310 goto error0;
5311 bno = del.br_startoff - 1;
5312nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 /*
5314 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005317 if (lastx >= 0) {
5318 ep = xfs_iext_get_ext(ifp, lastx);
5319 if (xfs_bmbt_get_startoff(ep) > bno) {
5320 if (--lastx >= 0)
5321 ep = xfs_iext_get_ext(ifp,
5322 lastx);
5323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 extno++;
5327 }
5328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005330
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 /*
5332 * Convert to a btree if necessary.
5333 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005334 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 ASSERT(cur == NULL);
5336 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5337 &cur, 0, &tmp_logflags, whichfork);
5338 logflags |= tmp_logflags;
5339 if (error)
5340 goto error0;
5341 }
5342 /*
5343 * transform from btree to extents, give it cur
5344 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005345 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 ASSERT(cur != NULL);
5347 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5348 whichfork);
5349 logflags |= tmp_logflags;
5350 if (error)
5351 goto error0;
5352 }
5353 /*
5354 * transform from extents to local?
5355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 error = 0;
5357error0:
5358 /*
5359 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005360 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005362 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005364 logflags &= ~xfs_ilog_fext(whichfork);
5365 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005367 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 /*
5369 * Log inode even in the error case, if the transaction
5370 * is dirty we'll need to shut down the filesystem.
5371 */
5372 if (logflags)
5373 xfs_trans_log_inode(tp, ip, logflags);
5374 if (cur) {
5375 if (!error) {
5376 *firstblock = cur->bc_private.b.firstblock;
5377 cur->bc_private.b.allocated = 0;
5378 }
5379 xfs_btree_del_cursor(cur,
5380 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5381 }
5382 return error;
5383}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005384
5385/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005386 * Determine whether an extent shift can be accomplished by a merge with the
5387 * extent that precedes the target hole of the shift.
5388 */
5389STATIC bool
5390xfs_bmse_can_merge(
5391 struct xfs_bmbt_irec *left, /* preceding extent */
5392 struct xfs_bmbt_irec *got, /* current extent to shift */
5393 xfs_fileoff_t shift) /* shift fsb */
5394{
5395 xfs_fileoff_t startoff;
5396
5397 startoff = got->br_startoff - shift;
5398
5399 /*
5400 * The extent, once shifted, must be adjacent in-file and on-disk with
5401 * the preceding extent.
5402 */
5403 if ((left->br_startoff + left->br_blockcount != startoff) ||
5404 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5405 (left->br_state != got->br_state) ||
5406 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5407 return false;
5408
5409 return true;
5410}
5411
5412/*
5413 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5414 * hole in the file. If an extent shift would result in the extent being fully
5415 * adjacent to the extent that currently precedes the hole, we can merge with
5416 * the preceding extent rather than do the shift.
5417 *
5418 * This function assumes the caller has verified a shift-by-merge is possible
5419 * with the provided extents via xfs_bmse_can_merge().
5420 */
5421STATIC int
5422xfs_bmse_merge(
5423 struct xfs_inode *ip,
5424 int whichfork,
5425 xfs_fileoff_t shift, /* shift fsb */
5426 int current_ext, /* idx of gotp */
5427 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5428 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5429 struct xfs_btree_cur *cur,
5430 int *logflags) /* output */
5431{
Brian Fosterddb19e32014-09-23 15:38:09 +10005432 struct xfs_bmbt_irec got;
5433 struct xfs_bmbt_irec left;
5434 xfs_filblks_t blockcount;
5435 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005436 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005437
Brian Fosterddb19e32014-09-23 15:38:09 +10005438 xfs_bmbt_get_all(gotp, &got);
5439 xfs_bmbt_get_all(leftp, &left);
5440 blockcount = left.br_blockcount + got.br_blockcount;
5441
5442 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5443 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5444 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5445
5446 /*
5447 * Merge the in-core extents. Note that the host record pointers and
5448 * current_ext index are invalid once the extent has been removed via
5449 * xfs_iext_remove().
5450 */
5451 xfs_bmbt_set_blockcount(leftp, blockcount);
5452 xfs_iext_remove(ip, current_ext, 1, 0);
5453
5454 /*
5455 * Update the on-disk extent count, the btree if necessary and log the
5456 * inode.
5457 */
5458 XFS_IFORK_NEXT_SET(ip, whichfork,
5459 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5460 *logflags |= XFS_ILOG_CORE;
5461 if (!cur) {
5462 *logflags |= XFS_ILOG_DEXT;
5463 return 0;
5464 }
5465
5466 /* lookup and remove the extent to merge */
5467 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5468 got.br_blockcount, &i);
5469 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005470 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005471 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005472
5473 error = xfs_btree_delete(cur, &i);
5474 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005475 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005476 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005477
5478 /* lookup and update size of the previous extent */
5479 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5480 left.br_blockcount, &i);
5481 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005482 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005483 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005484
5485 left.br_blockcount = blockcount;
5486
Dave Chinner4db431f2014-12-04 09:42:40 +11005487 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5488 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005489}
5490
5491/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005492 * Shift a single extent.
5493 */
5494STATIC int
5495xfs_bmse_shift_one(
5496 struct xfs_inode *ip,
5497 int whichfork,
5498 xfs_fileoff_t offset_shift_fsb,
5499 int *current_ext,
5500 struct xfs_bmbt_rec_host *gotp,
5501 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005502 int *logflags,
5503 enum shift_direction direction)
Brian Fostera979bdf2014-09-23 15:39:04 +10005504{
5505 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005506 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005507 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005508 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005509 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005510 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005511 int error;
5512 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005513 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005514
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005515 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005516 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005517 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Brian Fostera979bdf2014-09-23 15:39:04 +10005518
5519 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005520
Brian Fosterf71721d2014-09-23 15:39:05 +10005521 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005522 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005523
Namjae Jeona904b1c2015-03-25 15:08:56 +11005524 if (direction == SHIFT_LEFT) {
5525 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005526
Namjae Jeona904b1c2015-03-25 15:08:56 +11005527 /*
5528 * Check for merge if we've got an extent to the left,
5529 * otherwise make sure there's enough room at the start
5530 * of the file for the shift.
5531 */
5532 if (!*current_ext) {
5533 if (got.br_startoff < offset_shift_fsb)
5534 return -EINVAL;
5535 goto update_current_ext;
5536 }
5537 /*
5538 * grab the left extent and check for a large
5539 * enough hole.
5540 */
5541 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5542 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5543
5544 if (startoff <
5545 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005546 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005547
Dave Chinnerb11bd672014-12-04 09:42:24 +11005548 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005549 if (xfs_bmse_can_merge(&adj_irec, &got,
5550 offset_shift_fsb)) {
Dave Chinnerb11bd672014-12-04 09:42:24 +11005551 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005552 *current_ext, gotp, adj_irecp,
5553 cur, logflags);
Dave Chinnerb11bd672014-12-04 09:42:24 +11005554 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005555 } else {
5556 startoff = got.br_startoff + offset_shift_fsb;
5557 /* nothing to move if this is the last extent */
5558 if (*current_ext >= (total_extents - 1))
5559 goto update_current_ext;
5560 /*
5561 * If this is not the last extent in the file, make sure there
5562 * is enough room between current extent and next extent for
5563 * accommodating the shift.
5564 */
5565 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5566 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5567 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5568 return -EINVAL;
5569 /*
5570 * Unlike a left shift (which involves a hole punch),
5571 * a right shift does not modify extent neighbors
5572 * in any way. We should never find mergeable extents
5573 * in this scenario. Check anyways and warn if we
5574 * encounter two extents that could be one.
5575 */
5576 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5577 WARN_ON_ONCE(1);
5578 }
Brian Fostera979bdf2014-09-23 15:39:04 +10005579 /*
5580 * Increment the extent index for the next iteration, update the start
5581 * offset of the in-core extent and update the btree if applicable.
5582 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005583update_current_ext:
5584 if (direction == SHIFT_LEFT)
5585 (*current_ext)++;
5586 else
5587 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005588 xfs_bmbt_set_startoff(gotp, startoff);
5589 *logflags |= XFS_ILOG_CORE;
5590 if (!cur) {
5591 *logflags |= XFS_ILOG_DEXT;
5592 return 0;
5593 }
5594
5595 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5596 got.br_blockcount, &i);
5597 if (error)
5598 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005599 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10005600
5601 got.br_startoff = startoff;
kbuild test robotd254aae2014-12-01 08:42:52 +11005602 return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005603 got.br_blockcount, got.br_state);
Brian Fostera979bdf2014-09-23 15:39:04 +10005604}
5605
5606/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005607 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005608 *
Brian Foster2c845f52014-09-23 15:37:09 +10005609 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005610 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10005611 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5612 * is the length by which each extent is shifted. If there is no hole to shift
5613 * the extents into, this will be considered invalid operation and we abort
5614 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005615 */
5616int
5617xfs_bmap_shift_extents(
5618 struct xfs_trans *tp,
5619 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005620 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005621 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005622 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005623 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005624 xfs_fsblock_t *firstblock,
5625 struct xfs_bmap_free *flist,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005626 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005627 int num_exts)
5628{
Brian Fosterca446d82014-09-02 12:12:53 +10005629 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005630 struct xfs_bmbt_rec_host *gotp;
5631 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005632 struct xfs_mount *mp = ip->i_mount;
5633 struct xfs_ifork *ifp;
5634 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005635 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005636 xfs_extnum_t total_extents;
5637 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005638 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005639 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10005640 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005641
5642 if (unlikely(XFS_TEST_ERROR(
5643 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5644 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5645 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5646 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5647 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005648 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005649 }
5650
5651 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005652 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005653
Brian Foster2c845f52014-09-23 15:37:09 +10005654 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5655 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005656 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
5657 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005658
5659 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005660 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5661 /* Read in all the extents */
5662 error = xfs_iread_extents(tp, ip, whichfork);
5663 if (error)
5664 return error;
5665 }
5666
Brian Fosterddb19e32014-09-23 15:38:09 +10005667 if (ifp->if_flags & XFS_IFBROOT) {
5668 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5669 cur->bc_private.b.firstblock = *firstblock;
5670 cur->bc_private.b.flist = flist;
5671 cur->bc_private.b.flags = 0;
5672 }
5673
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005674 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005675 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10005676 * are collapsing out, so we cannot use the count of real extents here.
5677 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005678 */
5679 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005680 if (total_extents == 0) {
5681 *done = 1;
5682 goto del_cursor;
5683 }
5684
5685 /*
5686 * In case of first right shift, we need to initialize next_fsb
5687 */
5688 if (*next_fsb == NULLFSBLOCK) {
5689 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
5690 xfs_bmbt_get_all(gotp, &got);
5691 *next_fsb = got.br_startoff;
5692 if (stop_fsb > *next_fsb) {
5693 *done = 1;
5694 goto del_cursor;
5695 }
5696 }
5697
5698 /* Lookup the extent index at which we have to stop */
5699 if (direction == SHIFT_RIGHT) {
5700 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
5701 /* Make stop_extent exclusive of shift range */
5702 stop_extent--;
5703 } else
5704 stop_extent = total_extents;
5705
5706 /*
5707 * Look up the extent index for the fsb where we start shifting. We can
5708 * henceforth iterate with current_ext as extent list changes are locked
5709 * out via ilock.
5710 *
5711 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5712 * *next_fsb lies in a hole beyond which there are no extents. Either
5713 * way, we are done.
5714 */
5715 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
5716 if (!gotp) {
5717 *done = 1;
5718 goto del_cursor;
5719 }
5720
5721 /* some sanity checking before we finally start shifting extents */
5722 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
5723 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
5724 error = -EIO;
5725 goto del_cursor;
5726 }
5727
5728 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005729 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005730 &current_ext, gotp, cur, &logflags,
5731 direction);
Brian Fostera979bdf2014-09-23 15:39:04 +10005732 if (error)
5733 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005734 /*
5735 * If there was an extent merge during the shift, the extent
5736 * count can change. Update the total and grade the next record.
5737 */
5738 if (direction == SHIFT_LEFT) {
5739 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5740 stop_extent = total_extents;
5741 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005742
Namjae Jeona904b1c2015-03-25 15:08:56 +11005743 if (current_ext == stop_extent) {
5744 *done = 1;
5745 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10005746 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005747 }
Brian Fosterddb19e32014-09-23 15:38:09 +10005748 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005749 }
5750
Namjae Jeona904b1c2015-03-25 15:08:56 +11005751 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005752 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10005753 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10005754 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005755
5756del_cursor:
5757 if (cur)
5758 xfs_btree_del_cursor(cur,
5759 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5760
Brian Fosterca446d82014-09-02 12:12:53 +10005761 if (logflags)
5762 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10005763
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005764 return error;
5765}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005766
5767/*
5768 * Splits an extent into two extents at split_fsb block such that it is
5769 * the first block of the current_ext. @current_ext is a target extent
5770 * to be split. @split_fsb is a block where the extents is split.
5771 * If split_fsb lies in a hole or the first block of extents, just return 0.
5772 */
5773STATIC int
5774xfs_bmap_split_extent_at(
5775 struct xfs_trans *tp,
5776 struct xfs_inode *ip,
5777 xfs_fileoff_t split_fsb,
5778 xfs_fsblock_t *firstfsb,
5779 struct xfs_bmap_free *free_list)
5780{
5781 int whichfork = XFS_DATA_FORK;
5782 struct xfs_btree_cur *cur = NULL;
5783 struct xfs_bmbt_rec_host *gotp;
5784 struct xfs_bmbt_irec got;
5785 struct xfs_bmbt_irec new; /* split extent */
5786 struct xfs_mount *mp = ip->i_mount;
5787 struct xfs_ifork *ifp;
5788 xfs_fsblock_t gotblkcnt; /* new block count for got */
5789 xfs_extnum_t current_ext;
5790 int error = 0;
5791 int logflags = 0;
5792 int i = 0;
5793
5794 if (unlikely(XFS_TEST_ERROR(
5795 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5796 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5797 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5798 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5799 XFS_ERRLEVEL_LOW, mp);
5800 return -EFSCORRUPTED;
5801 }
5802
5803 if (XFS_FORCED_SHUTDOWN(mp))
5804 return -EIO;
5805
5806 ifp = XFS_IFORK_PTR(ip, whichfork);
5807 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5808 /* Read in all the extents */
5809 error = xfs_iread_extents(tp, ip, whichfork);
5810 if (error)
5811 return error;
5812 }
5813
5814 /*
5815 * gotp can be null in 2 cases: 1) if there are no extents
5816 * or 2) split_fsb lies in a hole beyond which there are
5817 * no extents. Either way, we are done.
5818 */
5819 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
5820 if (!gotp)
5821 return 0;
5822
5823 xfs_bmbt_get_all(gotp, &got);
5824
5825 /*
5826 * Check split_fsb lies in a hole or the start boundary offset
5827 * of the extent.
5828 */
5829 if (got.br_startoff >= split_fsb)
5830 return 0;
5831
5832 gotblkcnt = split_fsb - got.br_startoff;
5833 new.br_startoff = split_fsb;
5834 new.br_startblock = got.br_startblock + gotblkcnt;
5835 new.br_blockcount = got.br_blockcount - gotblkcnt;
5836 new.br_state = got.br_state;
5837
5838 if (ifp->if_flags & XFS_IFBROOT) {
5839 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5840 cur->bc_private.b.firstblock = *firstfsb;
5841 cur->bc_private.b.flist = free_list;
5842 cur->bc_private.b.flags = 0;
5843 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5844 got.br_startblock,
5845 got.br_blockcount,
5846 &i);
5847 if (error)
5848 goto del_cursor;
5849 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5850 }
5851
5852 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
5853 got.br_blockcount = gotblkcnt;
5854
5855 logflags = XFS_ILOG_CORE;
5856 if (cur) {
5857 error = xfs_bmbt_update(cur, got.br_startoff,
5858 got.br_startblock,
5859 got.br_blockcount,
5860 got.br_state);
5861 if (error)
5862 goto del_cursor;
5863 } else
5864 logflags |= XFS_ILOG_DEXT;
5865
5866 /* Add new extent */
5867 current_ext++;
5868 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5869 XFS_IFORK_NEXT_SET(ip, whichfork,
5870 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5871
5872 if (cur) {
5873 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
5874 new.br_startblock, new.br_blockcount,
5875 &i);
5876 if (error)
5877 goto del_cursor;
5878 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
5879 cur->bc_rec.b.br_state = new.br_state;
5880
5881 error = xfs_btree_insert(cur, &i);
5882 if (error)
5883 goto del_cursor;
5884 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5885 }
5886
5887 /*
5888 * Convert to a btree if necessary.
5889 */
5890 if (xfs_bmap_needs_btree(ip, whichfork)) {
5891 int tmp_logflags; /* partial log flag return val */
5892
5893 ASSERT(cur == NULL);
5894 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, free_list,
5895 &cur, 0, &tmp_logflags, whichfork);
5896 logflags |= tmp_logflags;
5897 }
5898
5899del_cursor:
5900 if (cur) {
5901 cur->bc_private.b.allocated = 0;
5902 xfs_btree_del_cursor(cur,
5903 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5904 }
5905
5906 if (logflags)
5907 xfs_trans_log_inode(tp, ip, logflags);
5908 return error;
5909}
5910
5911int
5912xfs_bmap_split_extent(
5913 struct xfs_inode *ip,
5914 xfs_fileoff_t split_fsb)
5915{
5916 struct xfs_mount *mp = ip->i_mount;
5917 struct xfs_trans *tp;
5918 struct xfs_bmap_free free_list;
5919 xfs_fsblock_t firstfsb;
5920 int committed;
5921 int error;
5922
5923 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
5924 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
5925 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0);
5926 if (error) {
Christoph Hellwig4906e212015-06-04 13:47:56 +10005927 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005928 return error;
5929 }
5930
5931 xfs_ilock(ip, XFS_ILOCK_EXCL);
5932 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
5933
5934 xfs_bmap_init(&free_list, &firstfsb);
5935
5936 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
5937 &firstfsb, &free_list);
5938 if (error)
5939 goto out;
5940
5941 error = xfs_bmap_finish(&tp, &free_list, &committed);
5942 if (error)
5943 goto out;
5944
Christoph Hellwig70393312015-06-04 13:48:08 +10005945 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005946
5947out:
Brian Fosterd4a97a02015-08-19 10:01:40 +10005948 xfs_bmap_cancel(&free_list);
Christoph Hellwig4906e212015-06-04 13:47:56 +10005949 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005950 return error;
5951}