blob: f1026e86dabc9a00ead716785a3acb5c19ee8e10 [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 */
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001115 int cancel_flags = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001116
1117 ASSERT(XFS_IFORK_Q(ip) == 0);
1118
1119 mp = ip->i_mount;
1120 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
1121 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
1122 blks = XFS_ADDAFORK_SPACE_RES(mp);
1123 if (rsvd)
1124 tp->t_flags |= XFS_TRANS_RESERVE;
Jie Liu3d3c8b52013-08-12 20:49:59 +10001125 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_addafork, blks, 0);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001126 if (error) {
1127 xfs_trans_cancel(tp, 0);
1128 return error;
1129 }
1130 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001131 xfs_ilock(ip, XFS_ILOCK_EXCL);
1132 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1133 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1134 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001135 if (error)
1136 goto trans_cancel;
1137 cancel_flags |= XFS_TRANS_ABORT;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001138 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001139 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001140 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1141 /*
1142 * For inodes coming from pre-6.2 filesystems.
1143 */
1144 ASSERT(ip->i_d.di_aformat == 0);
1145 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1146 }
1147 ASSERT(ip->i_d.di_anextents == 0);
1148
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001149 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001150 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1151
1152 switch (ip->i_d.di_format) {
1153 case XFS_DINODE_FMT_DEV:
1154 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1155 break;
1156 case XFS_DINODE_FMT_UUID:
1157 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1158 break;
1159 case XFS_DINODE_FMT_LOCAL:
1160 case XFS_DINODE_FMT_EXTENTS:
1161 case XFS_DINODE_FMT_BTREE:
1162 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1163 if (!ip->i_d.di_forkoff)
1164 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1165 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1166 version = 2;
1167 break;
1168 default:
1169 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001170 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001171 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001172 }
1173
1174 ASSERT(ip->i_afp == NULL);
1175 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1176 ip->i_afp->if_flags = XFS_IFEXTENTS;
1177 logflags = 0;
1178 xfs_bmap_init(&flist, &firstblock);
1179 switch (ip->i_d.di_format) {
1180 case XFS_DINODE_FMT_LOCAL:
1181 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
1182 &logflags);
1183 break;
1184 case XFS_DINODE_FMT_EXTENTS:
1185 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
1186 &flist, &logflags);
1187 break;
1188 case XFS_DINODE_FMT_BTREE:
1189 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
1190 &logflags);
1191 break;
1192 default:
1193 error = 0;
1194 break;
1195 }
1196 if (logflags)
1197 xfs_trans_log_inode(tp, ip, logflags);
1198 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001199 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001200 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1201 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001202 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001203
1204 spin_lock(&mp->m_sb_lock);
1205 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1206 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001207 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001208 }
1209 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1210 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001211 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001212 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001213 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001214 if (log_sb)
1215 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001216 }
1217
1218 error = xfs_bmap_finish(&tp, &flist, &committed);
1219 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001220 goto bmap_cancel;
1221 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001222 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001223 return error;
1224
1225bmap_cancel:
1226 xfs_bmap_cancel(&flist);
1227trans_cancel:
1228 xfs_trans_cancel(tp, cancel_flags);
1229 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001230 return error;
1231}
1232
1233/*
1234 * Internal and external extent tree search functions.
1235 */
1236
1237/*
1238 * Read in the extents to if_extents.
1239 * All inode fields are set up by caller, we just traverse the btree
1240 * and copy the records in. If the file system cannot contain unwritten
1241 * extents, the records are checked for no "state" flags.
1242 */
1243int /* error */
1244xfs_bmap_read_extents(
1245 xfs_trans_t *tp, /* transaction pointer */
1246 xfs_inode_t *ip, /* incore inode */
1247 int whichfork) /* data or attr fork */
1248{
1249 struct xfs_btree_block *block; /* current btree block */
1250 xfs_fsblock_t bno; /* block # of "block" */
1251 xfs_buf_t *bp; /* buffer for "block" */
1252 int error; /* error return value */
1253 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
1254 xfs_extnum_t i, j; /* index into the extents list */
1255 xfs_ifork_t *ifp; /* fork structure */
1256 int level; /* btree level, for checking */
1257 xfs_mount_t *mp; /* file system mount structure */
1258 __be64 *pp; /* pointer to block address */
1259 /* REFERENCED */
1260 xfs_extnum_t room; /* number of entries there's room for */
1261
1262 bno = NULLFSBLOCK;
1263 mp = ip->i_mount;
1264 ifp = XFS_IFORK_PTR(ip, whichfork);
1265 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
1266 XFS_EXTFMT_INODE(ip);
1267 block = ifp->if_broot;
1268 /*
1269 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1270 */
1271 level = be16_to_cpu(block->bb_level);
1272 ASSERT(level > 0);
1273 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1274 bno = be64_to_cpu(*pp);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +10001275 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001276 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
1277 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
1278 /*
1279 * Go down the tree until leaf level is reached, following the first
1280 * pointer (leftmost) at each level.
1281 */
1282 while (level-- > 0) {
1283 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1284 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1285 if (error)
1286 return error;
1287 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001288 if (level == 0)
1289 break;
1290 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1291 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001292 XFS_WANT_CORRUPTED_GOTO(mp,
1293 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001294 xfs_trans_brelse(tp, bp);
1295 }
1296 /*
1297 * Here with bp and block set to the leftmost leaf node in the tree.
1298 */
1299 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1300 i = 0;
1301 /*
1302 * Loop over all leaf nodes. Copy information to the extent records.
1303 */
1304 for (;;) {
1305 xfs_bmbt_rec_t *frp;
1306 xfs_fsblock_t nextbno;
1307 xfs_extnum_t num_recs;
1308 xfs_extnum_t start;
1309
1310 num_recs = xfs_btree_get_numrecs(block);
1311 if (unlikely(i + num_recs > room)) {
1312 ASSERT(i + num_recs <= room);
1313 xfs_warn(ip->i_mount,
1314 "corrupt dinode %Lu, (btree extents).",
1315 (unsigned long long) ip->i_ino);
1316 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1317 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1318 goto error0;
1319 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001320 /*
1321 * Read-ahead the next leaf block, if any.
1322 */
1323 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1324 if (nextbno != NULLFSBLOCK)
1325 xfs_btree_reada_bufl(mp, nextbno, 1,
1326 &xfs_bmbt_buf_ops);
1327 /*
1328 * Copy records into the extent records.
1329 */
1330 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
1331 start = i;
1332 for (j = 0; j < num_recs; j++, i++, frp++) {
1333 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1334 trp->l0 = be64_to_cpu(frp->l0);
1335 trp->l1 = be64_to_cpu(frp->l1);
1336 }
1337 if (exntf == XFS_EXTFMT_NOSTATE) {
1338 /*
1339 * Check all attribute bmap btree records and
1340 * any "older" data bmap btree records for a
1341 * set bit in the "extent flag" position.
1342 */
1343 if (unlikely(xfs_check_nostate_extents(ifp,
1344 start, num_recs))) {
1345 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
1346 XFS_ERRLEVEL_LOW,
1347 ip->i_mount);
1348 goto error0;
1349 }
1350 }
1351 xfs_trans_brelse(tp, bp);
1352 bno = nextbno;
1353 /*
1354 * If we've reached the end, stop.
1355 */
1356 if (bno == NULLFSBLOCK)
1357 break;
1358 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1359 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1360 if (error)
1361 return error;
1362 block = XFS_BUF_TO_BLOCK(bp);
1363 }
1364 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
1365 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
1366 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1367 return 0;
1368error0:
1369 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001370 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001371}
1372
1373
1374/*
1375 * Search the extent records for the entry containing block bno.
1376 * If bno lies in a hole, point to the next entry. If bno lies
1377 * past eof, *eofp will be set, and *prevp will contain the last
1378 * entry (null if none). Else, *lastxp will be set to the index
1379 * of the found entry; *gotp will contain the entry.
1380 */
1381STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1382xfs_bmap_search_multi_extents(
1383 xfs_ifork_t *ifp, /* inode fork pointer */
1384 xfs_fileoff_t bno, /* block number searched for */
1385 int *eofp, /* out: end of file found */
1386 xfs_extnum_t *lastxp, /* out: last extent index */
1387 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1388 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1389{
1390 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1391 xfs_extnum_t lastx; /* last extent index */
1392
1393 /*
1394 * Initialize the extent entry structure to catch access to
1395 * uninitialized br_startblock field.
1396 */
1397 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
1398 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
1399 gotp->br_state = XFS_EXT_INVALID;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001400 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001401 prevp->br_startoff = NULLFILEOFF;
1402
1403 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
1404 if (lastx > 0) {
1405 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
1406 }
1407 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1408 xfs_bmbt_get_all(ep, gotp);
1409 *eofp = 0;
1410 } else {
1411 if (lastx > 0) {
1412 *gotp = *prevp;
1413 }
1414 *eofp = 1;
1415 ep = NULL;
1416 }
1417 *lastxp = lastx;
1418 return ep;
1419}
1420
1421/*
1422 * Search the extents list for the inode, for the extent containing bno.
1423 * If bno lies in a hole, point to the next entry. If bno lies past eof,
1424 * *eofp will be set, and *prevp will contain the last entry (null if none).
1425 * Else, *lastxp will be set to the index of the found
1426 * entry; *gotp will contain the entry.
1427 */
1428STATIC xfs_bmbt_rec_host_t * /* pointer to found extent entry */
1429xfs_bmap_search_extents(
1430 xfs_inode_t *ip, /* incore inode pointer */
1431 xfs_fileoff_t bno, /* block number searched for */
1432 int fork, /* data or attr fork */
1433 int *eofp, /* out: end of file found */
1434 xfs_extnum_t *lastxp, /* out: last extent index */
1435 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
1436 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
1437{
1438 xfs_ifork_t *ifp; /* inode fork pointer */
1439 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1440
1441 XFS_STATS_INC(xs_look_exlist);
1442 ifp = XFS_IFORK_PTR(ip, fork);
1443
1444 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
1445
1446 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
1447 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
1448 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
1449 "Access to block zero in inode %llu "
1450 "start_block: %llx start_off: %llx "
Eric Sandeen08e96e12013-10-11 20:59:05 -05001451 "blkcnt: %llx extent-state: %x lastx: %x",
Dave Chinner9e5987a2013-02-25 12:31:26 +11001452 (unsigned long long)ip->i_ino,
1453 (unsigned long long)gotp->br_startblock,
1454 (unsigned long long)gotp->br_startoff,
1455 (unsigned long long)gotp->br_blockcount,
1456 gotp->br_state, *lastxp);
1457 *lastxp = NULLEXTNUM;
1458 *eofp = 1;
1459 return NULL;
1460 }
1461 return ep;
1462}
1463
1464/*
1465 * Returns the file-relative block number of the first unused block(s)
1466 * in the file with at least "len" logically contiguous blocks free.
1467 * This is the lowest-address hole if the file has holes, else the first block
1468 * past the end of file.
1469 * Return 0 if the file is currently local (in-inode).
1470 */
1471int /* error */
1472xfs_bmap_first_unused(
1473 xfs_trans_t *tp, /* transaction pointer */
1474 xfs_inode_t *ip, /* incore inode */
1475 xfs_extlen_t len, /* size of hole to find */
1476 xfs_fileoff_t *first_unused, /* unused block */
1477 int whichfork) /* data or attr fork */
1478{
1479 int error; /* error return value */
1480 int idx; /* extent record index */
1481 xfs_ifork_t *ifp; /* inode fork pointer */
1482 xfs_fileoff_t lastaddr; /* last block number seen */
1483 xfs_fileoff_t lowest; /* lowest useful block */
1484 xfs_fileoff_t max; /* starting useful block */
1485 xfs_fileoff_t off; /* offset for this block */
1486 xfs_extnum_t nextents; /* number of extent entries */
1487
1488 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1489 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1490 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1491 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1492 *first_unused = 0;
1493 return 0;
1494 }
1495 ifp = XFS_IFORK_PTR(ip, whichfork);
1496 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1497 (error = xfs_iread_extents(tp, ip, whichfork)))
1498 return error;
1499 lowest = *first_unused;
1500 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1501 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
1502 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
1503 off = xfs_bmbt_get_startoff(ep);
1504 /*
1505 * See if the hole before this extent will work.
1506 */
1507 if (off >= lowest + len && off - max >= len) {
1508 *first_unused = max;
1509 return 0;
1510 }
1511 lastaddr = off + xfs_bmbt_get_blockcount(ep);
1512 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1513 }
1514 *first_unused = max;
1515 return 0;
1516}
1517
1518/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001519 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001520 * last_block (input value) in the file.
1521 * This is not based on i_size, it is based on the extent records.
1522 * Returns 0 for local files, as they do not have extent records.
1523 */
1524int /* error */
1525xfs_bmap_last_before(
1526 xfs_trans_t *tp, /* transaction pointer */
1527 xfs_inode_t *ip, /* incore inode */
1528 xfs_fileoff_t *last_block, /* last block */
1529 int whichfork) /* data or attr fork */
1530{
1531 xfs_fileoff_t bno; /* input file offset */
1532 int eof; /* hit end of file */
1533 xfs_bmbt_rec_host_t *ep; /* pointer to last extent */
1534 int error; /* error return value */
1535 xfs_bmbt_irec_t got; /* current extent value */
1536 xfs_ifork_t *ifp; /* inode fork pointer */
1537 xfs_extnum_t lastx; /* last extent used */
1538 xfs_bmbt_irec_t prev; /* previous extent value */
1539
1540 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1541 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
1542 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
Dave Chinner24513372014-06-25 14:58:08 +10001543 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001544 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1545 *last_block = 0;
1546 return 0;
1547 }
1548 ifp = XFS_IFORK_PTR(ip, whichfork);
1549 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1550 (error = xfs_iread_extents(tp, ip, whichfork)))
1551 return error;
1552 bno = *last_block - 1;
1553 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
1554 &prev);
1555 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
1556 if (prev.br_startoff == NULLFILEOFF)
1557 *last_block = 0;
1558 else
1559 *last_block = prev.br_startoff + prev.br_blockcount;
1560 }
1561 /*
1562 * Otherwise *last_block is already the right answer.
1563 */
1564 return 0;
1565}
1566
Dave Chinner68988112013-08-12 20:49:42 +10001567int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001568xfs_bmap_last_extent(
1569 struct xfs_trans *tp,
1570 struct xfs_inode *ip,
1571 int whichfork,
1572 struct xfs_bmbt_irec *rec,
1573 int *is_empty)
1574{
1575 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1576 int error;
1577 int nextents;
1578
1579 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1580 error = xfs_iread_extents(tp, ip, whichfork);
1581 if (error)
1582 return error;
1583 }
1584
1585 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
1586 if (nextents == 0) {
1587 *is_empty = 1;
1588 return 0;
1589 }
1590
1591 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1592 *is_empty = 0;
1593 return 0;
1594}
1595
1596/*
1597 * Check the last inode extent to determine whether this allocation will result
1598 * in blocks being allocated at the end of the file. When we allocate new data
1599 * blocks at the end of the file which do not start at the previous data block,
1600 * we will try to align the new blocks at stripe unit boundaries.
1601 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001602 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001603 * at, or past the EOF.
1604 */
1605STATIC int
1606xfs_bmap_isaeof(
1607 struct xfs_bmalloca *bma,
1608 int whichfork)
1609{
1610 struct xfs_bmbt_irec rec;
1611 int is_empty;
1612 int error;
1613
1614 bma->aeof = 0;
1615 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1616 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001617 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001618 return error;
1619
Dave Chinner6e708bc2013-11-22 10:41:16 +11001620 if (is_empty) {
1621 bma->aeof = 1;
1622 return 0;
1623 }
1624
Dave Chinner9e5987a2013-02-25 12:31:26 +11001625 /*
1626 * Check if we are allocation or past the last extent, or at least into
1627 * the last delayed allocated extent.
1628 */
1629 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1630 (bma->offset >= rec.br_startoff &&
1631 isnullstartblock(rec.br_startblock));
1632 return 0;
1633}
1634
1635/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001636 * Returns the file-relative block number of the first block past eof in
1637 * the file. This is not based on i_size, it is based on the extent records.
1638 * Returns 0 for local files, as they do not have extent records.
1639 */
1640int
1641xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001642 struct xfs_inode *ip,
1643 xfs_fileoff_t *last_block,
1644 int whichfork)
1645{
1646 struct xfs_bmbt_irec rec;
1647 int is_empty;
1648 int error;
1649
1650 *last_block = 0;
1651
1652 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1653 return 0;
1654
1655 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1656 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001657 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001658
1659 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1660 if (error || is_empty)
1661 return error;
1662
1663 *last_block = rec.br_startoff + rec.br_blockcount;
1664 return 0;
1665}
1666
1667/*
1668 * Returns whether the selected fork of the inode has exactly one
1669 * block or not. For the data fork we check this matches di_size,
1670 * implying the file's range is 0..bsize-1.
1671 */
1672int /* 1=>1 block, 0=>otherwise */
1673xfs_bmap_one_block(
1674 xfs_inode_t *ip, /* incore inode */
1675 int whichfork) /* data or attr fork */
1676{
1677 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1678 xfs_ifork_t *ifp; /* inode fork pointer */
1679 int rval; /* return value */
1680 xfs_bmbt_irec_t s; /* internal version of extent */
1681
1682#ifndef DEBUG
1683 if (whichfork == XFS_DATA_FORK)
1684 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1685#endif /* !DEBUG */
1686 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1687 return 0;
1688 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1689 return 0;
1690 ifp = XFS_IFORK_PTR(ip, whichfork);
1691 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1692 ep = xfs_iext_get_ext(ifp, 0);
1693 xfs_bmbt_get_all(ep, &s);
1694 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1695 if (rval && whichfork == XFS_DATA_FORK)
1696 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1697 return rval;
1698}
1699
1700/*
1701 * Extent tree manipulation functions used during allocation.
1702 */
1703
1704/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001705 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 */
1707STATIC int /* error */
1708xfs_bmap_add_extent_delay_real(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001709 struct xfs_bmalloca *bma)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001711 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001713 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001716 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 xfs_fileoff_t new_endoff; /* end offset of new entry */
1718 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1719 /* left is 0, right is 1, prev is 2 */
1720 int rval=0; /* return value (logging flags) */
1721 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001722 xfs_filblks_t da_new; /* new count del alloc blocks used */
1723 xfs_filblks_t da_old; /* old count del alloc blocks used */
1724 xfs_filblks_t temp=0; /* value for da_new calculations */
1725 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001727 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Eric Sandeenc29aad42015-02-23 22:39:08 +11001729 mp = bma->tp ? bma->tp->t_mountp : NULL;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001730 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001731
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001732 ASSERT(bma->idx >= 0);
1733 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001734 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001735 ASSERT(!bma->cur ||
1736 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001737
1738 XFS_STATS_INC(xs_add_exlist);
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740#define LEFT r[0]
1741#define RIGHT r[1]
1742#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 /*
1745 * Set up a bunch of variables to make the tests simpler.
1746 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001747 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 xfs_bmbt_get_all(ep, &PREV);
1749 new_endoff = new->br_startoff + new->br_blockcount;
1750 ASSERT(PREV.br_startoff <= new->br_startoff);
1751 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001752
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001753 da_old = startblockval(PREV.br_startblock);
1754 da_new = 0;
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /*
1757 * Set flags determining what part of the previous delayed allocation
1758 * extent is being replaced by a real allocation.
1759 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001760 if (PREV.br_startoff == new->br_startoff)
1761 state |= BMAP_LEFT_FILLING;
1762 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1763 state |= BMAP_RIGHT_FILLING;
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 /*
1766 * Check and set flags if this segment has a left neighbor.
1767 * Don't set contiguous if the combined extent would be too large.
1768 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001769 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001770 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001771 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001772
1773 if (isnullstartblock(LEFT.br_startblock))
1774 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001776
1777 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1778 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1779 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1780 LEFT.br_state == new->br_state &&
1781 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1782 state |= BMAP_LEFT_CONTIG;
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 /*
1785 * Check and set flags if this segment has a right neighbor.
1786 * Don't set contiguous if the combined extent would be too large.
1787 * Also check for all-three-contiguous being too large.
1788 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001789 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001790 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001791 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001792
1793 if (isnullstartblock(RIGHT.br_startblock))
1794 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001796
1797 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1798 new_endoff == RIGHT.br_startoff &&
1799 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1800 new->br_state == RIGHT.br_state &&
1801 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1802 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1803 BMAP_RIGHT_FILLING)) !=
1804 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1805 BMAP_RIGHT_FILLING) ||
1806 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1807 <= MAXEXTLEN))
1808 state |= BMAP_RIGHT_CONTIG;
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 error = 0;
1811 /*
1812 * Switch out based on the FILLING and CONTIG state bits.
1813 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001814 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1815 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1816 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1817 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 /*
1819 * Filling in all of a previously delayed allocation extent.
1820 * The left and right neighbors are both contiguous with new.
1821 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001822 bma->idx--;
1823 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1824 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 LEFT.br_blockcount + PREV.br_blockcount +
1826 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001827 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001828
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001829 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
1830 bma->ip->i_d.di_nextents--;
1831 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1833 else {
1834 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001835 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001837 RIGHT.br_blockcount, &i);
1838 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001840 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001841 error = xfs_btree_delete(bma->cur, &i);
1842 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001844 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001845 error = xfs_btree_decrement(bma->cur, 0, &i);
1846 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001848 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001849 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 LEFT.br_startblock,
1851 LEFT.br_blockcount +
1852 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001853 RIGHT.br_blockcount, LEFT.br_state);
1854 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 goto done;
1856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 break;
1858
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001859 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /*
1861 * Filling in all of a previously delayed allocation extent.
1862 * The left neighbor is contiguous, the right is not.
1863 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001864 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001865
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001866 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1867 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001868 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001869 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001870
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001871 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1872 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 rval = XFS_ILOG_DEXT;
1874 else {
1875 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001876 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001878 &i);
1879 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001881 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001882 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 LEFT.br_startblock,
1884 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001885 PREV.br_blockcount, LEFT.br_state);
1886 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 goto done;
1888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 break;
1890
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001891 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /*
1893 * Filling in all of a previously delayed allocation extent.
1894 * The right neighbor is contiguous, the left is not.
1895 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001896 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 xfs_bmbt_set_startblock(ep, new->br_startblock);
1898 xfs_bmbt_set_blockcount(ep,
1899 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001900 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001901
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001902 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1903 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 rval = XFS_ILOG_DEXT;
1905 else {
1906 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001907 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001909 RIGHT.br_blockcount, &i);
1910 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001912 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001913 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 new->br_startblock,
1915 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001916 RIGHT.br_blockcount, PREV.br_state);
1917 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 goto done;
1919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 break;
1921
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001922 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 /*
1924 * Filling in all of a previously delayed allocation extent.
1925 * Neither the left nor right neighbors are contiguous with
1926 * the new one.
1927 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001928 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 xfs_bmbt_set_startblock(ep, new->br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001930 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001931
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001932 bma->ip->i_d.di_nextents++;
1933 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1935 else {
1936 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001937 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001939 &i);
1940 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001942 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001943 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1944 error = xfs_btree_insert(bma->cur, &i);
1945 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001947 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 break;
1950
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001951 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 /*
1953 * Filling in the first part of a previous delayed allocation.
1954 * The left neighbor is contiguous.
1955 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001956 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1957 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 LEFT.br_blockcount + new->br_blockcount);
1959 xfs_bmbt_set_startoff(ep,
1960 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001961 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001964 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001966 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 rval = XFS_ILOG_DEXT;
1968 else {
1969 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001970 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001972 &i);
1973 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001975 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001976 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 LEFT.br_startblock,
1978 LEFT.br_blockcount +
1979 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001980 LEFT.br_state);
1981 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 goto done;
1983 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001984 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001985 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001986 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001987 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001988
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001989 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 break;
1991
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001992 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 /*
1994 * Filling in the first part of a previous delayed allocation.
1995 * The left neighbor is not contiguous.
1996 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001997 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 xfs_bmbt_set_startoff(ep, new_endoff);
1999 temp = PREV.br_blockcount - new->br_blockcount;
2000 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002001 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
2002 bma->ip->i_d.di_nextents++;
2003 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2005 else {
2006 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002007 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002009 &i);
2010 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002012 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002013 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2014 error = xfs_btree_insert(bma->cur, &i);
2015 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002017 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002019
2020 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002021 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2022 bma->firstblock, bma->flist,
2023 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 rval |= tmp_rval;
2025 if (error)
2026 goto done;
2027 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002028 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002029 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002030 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2031 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002032 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002033 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 break;
2035
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002036 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 /*
2038 * Filling in the last part of a previous delayed allocation.
2039 * The right neighbor is contiguous with the new allocation.
2040 */
2041 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002042 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002044 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002045 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 new->br_blockcount + RIGHT.br_blockcount,
2047 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002048 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
2049 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 rval = XFS_ILOG_DEXT;
2051 else {
2052 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002053 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002055 RIGHT.br_blockcount, &i);
2056 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002058 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002059 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 new->br_startblock,
2061 new->br_blockcount +
2062 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002063 RIGHT.br_state);
2064 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 goto done;
2066 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00002067
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002068 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002069 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002070 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002071 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002072 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002073
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002074 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 break;
2076
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002077 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 /*
2079 * Filling in the last part of a previous delayed allocation.
2080 * The right neighbor is not contiguous.
2081 */
2082 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002083 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002085 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
2086 bma->ip->i_d.di_nextents++;
2087 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2089 else {
2090 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002091 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002093 &i);
2094 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002096 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002097 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2098 error = xfs_btree_insert(bma->cur, &i);
2099 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002101 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002103
2104 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002105 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2106 bma->firstblock, bma->flist, &bma->cur, 1,
2107 &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 rval |= tmp_rval;
2109 if (error)
2110 goto done;
2111 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002112 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002113 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002114 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
2115 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002116 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002117 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002118
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002119 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 break;
2121
2122 case 0:
2123 /*
2124 * Filling in the middle part of a previous delayed allocation.
2125 * Contiguity is impossible here.
2126 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002127 *
2128 * We start with a delayed allocation:
2129 *
2130 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2131 * PREV @ idx
2132 *
2133 * and we are allocating:
2134 * +rrrrrrrrrrrrrrrrr+
2135 * new
2136 *
2137 * and we set it up for insertion as:
2138 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2139 * new
2140 * PREV @ idx LEFT RIGHT
2141 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 */
2143 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002145 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002146 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2147 LEFT = *new;
2148 RIGHT.br_state = PREV.br_state;
2149 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002150 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002151 RIGHT.br_startoff = new_endoff;
2152 RIGHT.br_blockcount = temp2;
2153 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002154 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
2155 bma->ip->i_d.di_nextents++;
2156 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2158 else {
2159 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002160 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002162 &i);
2163 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002165 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002166 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2167 error = xfs_btree_insert(bma->cur, &i);
2168 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002170 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002172
2173 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002174 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2175 bma->firstblock, bma->flist, &bma->cur,
2176 1, &tmp_rval, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 rval |= tmp_rval;
2178 if (error)
2179 goto done;
2180 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002181 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2182 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002183 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002184 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002185 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002186 error = xfs_mod_fdblocks(bma->ip->i_mount,
2187 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002188 ASSERT(!error);
2189 if (error)
2190 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002192
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002193 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002194 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002195 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2196 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2197 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002198 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002199 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002200
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002201 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002202 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 break;
2204
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002205 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2206 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2207 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2208 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2209 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2210 case BMAP_LEFT_CONTIG:
2211 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 /*
2213 * These cases are all impossible.
2214 */
2215 ASSERT(0);
2216 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002217
2218 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002219 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002220 int tmp_logflags; /* partial log flag return val */
2221
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002222 ASSERT(bma->cur == NULL);
2223 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
2224 bma->firstblock, bma->flist, &bma->cur,
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002225 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002226 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002227 if (error)
2228 goto done;
2229 }
2230
2231 /* adjust for changes in reserved delayed indirect blocks */
2232 if (da_old || da_new) {
2233 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002234 if (bma->cur)
2235 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002236 ASSERT(temp <= da_old);
2237 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002238 xfs_mod_fdblocks(bma->ip->i_mount,
2239 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002240 }
2241
2242 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002243 if (bma->cur)
2244 bma->cur->bc_private.b.allocated = 0;
2245
2246 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247done:
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002248 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 return error;
2250#undef LEFT
2251#undef RIGHT
2252#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
2254
2255/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002256 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 */
2258STATIC int /* error */
2259xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002260 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002262 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002264 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002265 xfs_fsblock_t *first, /* pointer to firstblock variable */
2266 xfs_bmap_free_t *flist, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002267 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002270 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002273 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 xfs_fileoff_t new_endoff; /* end offset of new entry */
2275 xfs_exntst_t newext; /* new extent state */
2276 xfs_exntst_t oldext; /* old extent state */
2277 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2278 /* left is 0, right is 1, prev is 2 */
2279 int rval=0; /* return value (logging flags) */
2280 int state = 0;/* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002281 struct xfs_mount *mp = tp->t_mountp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002283 *logflagsp = 0;
2284
2285 cur = *curp;
2286 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
2287
2288 ASSERT(*idx >= 0);
2289 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
2290 ASSERT(!isnullstartblock(new->br_startblock));
2291
2292 XFS_STATS_INC(xs_add_exlist);
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294#define LEFT r[0]
2295#define RIGHT r[1]
2296#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002297
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /*
2299 * Set up a bunch of variables to make the tests simpler.
2300 */
2301 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002302 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 xfs_bmbt_get_all(ep, &PREV);
2304 newext = new->br_state;
2305 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2306 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2307 ASSERT(PREV.br_state == oldext);
2308 new_endoff = new->br_startoff + new->br_blockcount;
2309 ASSERT(PREV.br_startoff <= new->br_startoff);
2310 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 /*
2313 * Set flags determining what part of the previous oldext allocation
2314 * extent is being replaced by a newext allocation.
2315 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002316 if (PREV.br_startoff == new->br_startoff)
2317 state |= BMAP_LEFT_FILLING;
2318 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2319 state |= BMAP_RIGHT_FILLING;
2320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 /*
2322 * Check and set flags if this segment has a left neighbor.
2323 * Don't set contiguous if the combined extent would be too large.
2324 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002325 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002326 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002327 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002328
2329 if (isnullstartblock(LEFT.br_startblock))
2330 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002332
2333 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2334 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2335 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2336 LEFT.br_state == newext &&
2337 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2338 state |= BMAP_LEFT_CONTIG;
2339
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 /*
2341 * Check and set flags if this segment has a right neighbor.
2342 * Don't set contiguous if the combined extent would be too large.
2343 * Also check for all-three-contiguous being too large.
2344 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002345 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002346 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002347 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002348 if (isnullstartblock(RIGHT.br_startblock))
2349 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002351
2352 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2353 new_endoff == RIGHT.br_startoff &&
2354 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2355 newext == RIGHT.br_state &&
2356 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2357 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2358 BMAP_RIGHT_FILLING)) !=
2359 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2360 BMAP_RIGHT_FILLING) ||
2361 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2362 <= MAXEXTLEN))
2363 state |= BMAP_RIGHT_CONTIG;
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 /*
2366 * Switch out based on the FILLING and CONTIG state bits.
2367 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002368 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2369 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2370 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2371 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 /*
2373 * Setting all of a previous oldext extent to newext.
2374 * The left and right neighbors are both contiguous with new.
2375 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002376 --*idx;
2377
2378 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2379 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 LEFT.br_blockcount + PREV.br_blockcount +
2381 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002382 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002383
Christoph Hellwigec90c552011-05-23 08:52:53 +00002384 xfs_iext_remove(ip, *idx + 1, 2, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 ip->i_d.di_nextents -= 2;
2386 if (cur == NULL)
2387 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2388 else {
2389 rval = XFS_ILOG_CORE;
2390 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2391 RIGHT.br_startblock,
2392 RIGHT.br_blockcount, &i)))
2393 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002394 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002395 if ((error = xfs_btree_delete(cur, &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 Hellwig8df4da42008-10-30 16:55:58 +11002398 if ((error = xfs_btree_decrement(cur, 0, &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 Hellwig91cca5df2008-10-30 16:58:01 +11002401 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002404 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002406 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2408 LEFT.br_startblock,
2409 LEFT.br_blockcount + PREV.br_blockcount +
2410 RIGHT.br_blockcount, LEFT.br_state)))
2411 goto done;
2412 }
2413 break;
2414
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002415 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 /*
2417 * Setting all of a previous oldext extent to newext.
2418 * The left neighbor is contiguous, the right is not.
2419 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002420 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002421
Christoph Hellwigec90c552011-05-23 08:52:53 +00002422 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2423 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2424 LEFT.br_blockcount + PREV.br_blockcount);
2425 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2426
2427 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 ip->i_d.di_nextents--;
2429 if (cur == NULL)
2430 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2431 else {
2432 rval = XFS_ILOG_CORE;
2433 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2434 PREV.br_startblock, PREV.br_blockcount,
2435 &i)))
2436 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002437 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002438 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002441 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002443 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2445 LEFT.br_startblock,
2446 LEFT.br_blockcount + PREV.br_blockcount,
2447 LEFT.br_state)))
2448 goto done;
2449 }
2450 break;
2451
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002452 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 /*
2454 * Setting all of a previous oldext extent to newext.
2455 * The right neighbor is contiguous, the left is not.
2456 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002457 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 xfs_bmbt_set_blockcount(ep,
2459 PREV.br_blockcount + RIGHT.br_blockcount);
2460 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002461 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2462 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 ip->i_d.di_nextents--;
2464 if (cur == NULL)
2465 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2466 else {
2467 rval = XFS_ILOG_CORE;
2468 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2469 RIGHT.br_startblock,
2470 RIGHT.br_blockcount, &i)))
2471 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002472 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002473 if ((error = xfs_btree_delete(cur, &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);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002476 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002478 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2480 new->br_startblock,
2481 new->br_blockcount + RIGHT.br_blockcount,
2482 newext)))
2483 goto done;
2484 }
2485 break;
2486
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002487 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 /*
2489 * Setting all of a previous oldext extent to newext.
2490 * Neither the left nor right neighbors are contiguous with
2491 * the new one.
2492 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002493 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002495 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 if (cur == NULL)
2498 rval = XFS_ILOG_DEXT;
2499 else {
2500 rval = 0;
2501 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2502 new->br_startblock, new->br_blockcount,
2503 &i)))
2504 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002505 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2507 new->br_startblock, new->br_blockcount,
2508 newext)))
2509 goto done;
2510 }
2511 break;
2512
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002513 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 /*
2515 * Setting the first part of a previous oldext extent to newext.
2516 * The left neighbor is contiguous.
2517 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002518 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2519 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 LEFT.br_blockcount + new->br_blockcount);
2521 xfs_bmbt_set_startoff(ep,
2522 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002523 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002524
Christoph Hellwigec90c552011-05-23 08:52:53 +00002525 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 xfs_bmbt_set_startblock(ep,
2527 new->br_startblock + new->br_blockcount);
2528 xfs_bmbt_set_blockcount(ep,
2529 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002530 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002531
Christoph Hellwigec90c552011-05-23 08:52:53 +00002532 --*idx;
2533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 if (cur == NULL)
2535 rval = XFS_ILOG_DEXT;
2536 else {
2537 rval = 0;
2538 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2539 PREV.br_startblock, PREV.br_blockcount,
2540 &i)))
2541 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002542 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 if ((error = xfs_bmbt_update(cur,
2544 PREV.br_startoff + new->br_blockcount,
2545 PREV.br_startblock + new->br_blockcount,
2546 PREV.br_blockcount - new->br_blockcount,
2547 oldext)))
2548 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002549 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002551 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 LEFT.br_startblock,
2553 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002554 LEFT.br_state);
2555 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 goto done;
2557 }
2558 break;
2559
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002560 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 /*
2562 * Setting the first part of a previous oldext extent to newext.
2563 * The left neighbor is not contiguous.
2564 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002565 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2567 xfs_bmbt_set_startoff(ep, new_endoff);
2568 xfs_bmbt_set_blockcount(ep,
2569 PREV.br_blockcount - new->br_blockcount);
2570 xfs_bmbt_set_startblock(ep,
2571 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002572 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002573
Christoph Hellwigec90c552011-05-23 08:52:53 +00002574 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 ip->i_d.di_nextents++;
2576 if (cur == NULL)
2577 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2578 else {
2579 rval = XFS_ILOG_CORE;
2580 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2581 PREV.br_startblock, PREV.br_blockcount,
2582 &i)))
2583 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002584 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if ((error = xfs_bmbt_update(cur,
2586 PREV.br_startoff + new->br_blockcount,
2587 PREV.br_startblock + new->br_blockcount,
2588 PREV.br_blockcount - new->br_blockcount,
2589 oldext)))
2590 goto done;
2591 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002592 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002594 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 }
2596 break;
2597
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002598 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 /*
2600 * Setting the last part of a previous oldext extent to newext.
2601 * The right neighbor is contiguous with the new allocation.
2602 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002603 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 xfs_bmbt_set_blockcount(ep,
2605 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002606 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2607
2608 ++*idx;
2609
2610 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2611 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002612 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002614 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002615
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 if (cur == NULL)
2617 rval = XFS_ILOG_DEXT;
2618 else {
2619 rval = 0;
2620 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2621 PREV.br_startblock,
2622 PREV.br_blockcount, &i)))
2623 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002624 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2626 PREV.br_startblock,
2627 PREV.br_blockcount - new->br_blockcount,
2628 oldext)))
2629 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002630 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 goto done;
2632 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2633 new->br_startblock,
2634 new->br_blockcount + RIGHT.br_blockcount,
2635 newext)))
2636 goto done;
2637 }
2638 break;
2639
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002640 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 /*
2642 * Setting the last part of a previous oldext extent to newext.
2643 * The right neighbor is not contiguous.
2644 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002645 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 xfs_bmbt_set_blockcount(ep,
2647 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002648 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002649
Christoph Hellwigec90c552011-05-23 08:52:53 +00002650 ++*idx;
2651 xfs_iext_insert(ip, *idx, 1, new, state);
2652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 ip->i_d.di_nextents++;
2654 if (cur == NULL)
2655 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2656 else {
2657 rval = XFS_ILOG_CORE;
2658 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2659 PREV.br_startblock, PREV.br_blockcount,
2660 &i)))
2661 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002662 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2664 PREV.br_startblock,
2665 PREV.br_blockcount - new->br_blockcount,
2666 oldext)))
2667 goto done;
2668 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2669 new->br_startblock, new->br_blockcount,
2670 &i)))
2671 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002672 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 cur->bc_rec.b.br_state = XFS_EXT_NORM;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002674 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002676 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 }
2678 break;
2679
2680 case 0:
2681 /*
2682 * Setting the middle part of a previous oldext extent to
2683 * newext. Contiguity is impossible here.
2684 * One extent becomes three extents.
2685 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002686 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 xfs_bmbt_set_blockcount(ep,
2688 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002689 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 r[0] = *new;
2692 r[1].br_startoff = new_endoff;
2693 r[1].br_blockcount =
2694 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2695 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2696 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002697
2698 ++*idx;
2699 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 ip->i_d.di_nextents += 2;
2702 if (cur == NULL)
2703 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2704 else {
2705 rval = XFS_ILOG_CORE;
2706 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2707 PREV.br_startblock, PREV.br_blockcount,
2708 &i)))
2709 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002710 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 /* new right extent - oldext */
2712 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2713 r[1].br_startblock, r[1].br_blockcount,
2714 r[1].br_state)))
2715 goto done;
2716 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002718 cur->bc_rec.b.br_blockcount =
2719 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002720 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002722 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002723 /*
2724 * Reset the cursor to the position of the new extent
2725 * we are about to insert as we can't trust it after
2726 * the previous insert.
2727 */
2728 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2729 new->br_startblock, new->br_blockcount,
2730 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002732 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002734 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002735 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002737 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 }
2739 break;
2740
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002741 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2742 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2743 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2744 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2745 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2746 case BMAP_LEFT_CONTIG:
2747 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 /*
2749 * These cases are all impossible.
2750 */
2751 ASSERT(0);
2752 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002753
2754 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002755 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002756 int tmp_logflags; /* partial log flag return val */
2757
2758 ASSERT(cur == NULL);
2759 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
2760 0, &tmp_logflags, XFS_DATA_FORK);
2761 *logflagsp |= tmp_logflags;
2762 if (error)
2763 goto done;
2764 }
2765
2766 /* clear out the allocated field, done with it now in any case. */
2767 if (cur) {
2768 cur->bc_private.b.allocated = 0;
2769 *curp = cur;
2770 }
2771
2772 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002774 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 return error;
2776#undef LEFT
2777#undef RIGHT
2778#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779}
2780
2781/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002782 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002784STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785xfs_bmap_add_extent_hole_delay(
2786 xfs_inode_t *ip, /* incore inode pointer */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002787 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002788 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002790 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2792 xfs_filblks_t newlen=0; /* new indirect size */
2793 xfs_filblks_t oldlen=0; /* old indirect size */
2794 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2795 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002796 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002798 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 state = 0;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002800 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 /*
2803 * Check and set flags if this segment has a left neighbor
2804 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002805 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002806 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002807 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002808
2809 if (isnullstartblock(left.br_startblock))
2810 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002812
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 /*
2814 * Check and set flags if the current (right) segment exists.
2815 * If it doesn't exist, we're converting the hole at end-of-file.
2816 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002817 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002818 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002819 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002820
2821 if (isnullstartblock(right.br_startblock))
2822 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 /*
2826 * Set contiguity flags on the left and right neighbors.
2827 * Don't let extents get too large, even if the pieces are contiguous.
2828 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002829 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2830 left.br_startoff + left.br_blockcount == new->br_startoff &&
2831 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2832 state |= BMAP_LEFT_CONTIG;
2833
2834 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2835 new->br_startoff + new->br_blockcount == right.br_startoff &&
2836 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2837 (!(state & BMAP_LEFT_CONTIG) ||
2838 (left.br_blockcount + new->br_blockcount +
2839 right.br_blockcount <= MAXEXTLEN)))
2840 state |= BMAP_RIGHT_CONTIG;
2841
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 /*
2843 * Switch out based on the contiguity flags.
2844 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002845 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2846 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 /*
2848 * New allocation is contiguous with delayed allocations
2849 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002850 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002852 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 temp = left.br_blockcount + new->br_blockcount +
2854 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002855
Christoph Hellwigec90c552011-05-23 08:52:53 +00002856 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2857 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002858 oldlen = startblockval(left.br_startblock) +
2859 startblockval(new->br_startblock) +
2860 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002862 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002863 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002864 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002865
Christoph Hellwigec90c552011-05-23 08:52:53 +00002866 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 break;
2868
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002869 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 /*
2871 * New allocation is contiguous with a delayed allocation
2872 * on the left.
2873 * Merge the new allocation with the left neighbor.
2874 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002875 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002877
2878 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2879 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002880 oldlen = startblockval(left.br_startblock) +
2881 startblockval(new->br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002883 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002884 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002885 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 break;
2887
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002888 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 /*
2890 * New allocation is contiguous with a delayed allocation
2891 * on the right.
2892 * Merge the new allocation with the right neighbor.
2893 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002894 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002896 oldlen = startblockval(new->br_startblock) +
2897 startblockval(right.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 newlen = xfs_bmap_worst_indlen(ip, temp);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002899 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2900 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002901 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002902 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 break;
2904
2905 case 0:
2906 /*
2907 * New allocation is not contiguous with another
2908 * delayed allocation.
2909 * Insert a new entry.
2910 */
2911 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002912 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 break;
2914 }
2915 if (oldlen != newlen) {
2916 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002917 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2918 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 /*
2920 * Nothing to do for disk quota accounting here.
2921 */
2922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923}
2924
2925/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002926 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 */
2928STATIC int /* error */
2929xfs_bmap_add_extent_hole_real(
Christoph Hellwigc6534242011-09-18 20:41:05 +00002930 struct xfs_bmalloca *bma,
2931 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Christoph Hellwigc6534242011-09-18 20:41:05 +00002933 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 int i; /* temp state */
2936 xfs_ifork_t *ifp; /* inode fork pointer */
2937 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2938 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002939 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 int state; /* state bits, accessed thru macros */
Eric Sandeenc29aad42015-02-23 22:39:08 +11002941 struct xfs_mount *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Eric Sandeenc29aad42015-02-23 22:39:08 +11002943 mp = bma->tp ? bma->tp->t_mountp : NULL;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002944 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002945
Christoph Hellwigc6534242011-09-18 20:41:05 +00002946 ASSERT(bma->idx >= 0);
2947 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002948 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwigc6534242011-09-18 20:41:05 +00002949 ASSERT(!bma->cur ||
2950 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002951
2952 XFS_STATS_INC(xs_add_exlist);
2953
2954 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002955 if (whichfork == XFS_ATTR_FORK)
2956 state |= BMAP_ATTRFORK;
2957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 /*
2959 * Check and set flags if this segment has a left neighbor.
2960 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002961 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002962 state |= BMAP_LEFT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002963 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002964 if (isnullstartblock(left.br_startblock))
2965 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 /*
2969 * Check and set flags if this segment has a current value.
2970 * Not true if we're inserting into the "hole" at eof.
2971 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00002972 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002973 state |= BMAP_RIGHT_VALID;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002974 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002975 if (isnullstartblock(right.br_startblock))
2976 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 /*
2980 * We're inserting a real allocation between "left" and "right".
2981 * Set the contiguity flags. Don't let extents get too large.
2982 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002983 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2984 left.br_startoff + left.br_blockcount == new->br_startoff &&
2985 left.br_startblock + left.br_blockcount == new->br_startblock &&
2986 left.br_state == new->br_state &&
2987 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2988 state |= BMAP_LEFT_CONTIG;
2989
2990 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2991 new->br_startoff + new->br_blockcount == right.br_startoff &&
2992 new->br_startblock + new->br_blockcount == right.br_startblock &&
2993 new->br_state == right.br_state &&
2994 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2995 (!(state & BMAP_LEFT_CONTIG) ||
2996 left.br_blockcount + new->br_blockcount +
2997 right.br_blockcount <= MAXEXTLEN))
2998 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003000 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 /*
3002 * Select which case we're in here, and implement it.
3003 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003004 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
3005 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 /*
3007 * New allocation is contiguous with real allocations on the
3008 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11003009 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003011 --bma->idx;
3012 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3013 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 left.br_blockcount + new->br_blockcount +
3015 right.br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003016 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003017
Christoph Hellwigc6534242011-09-18 20:41:05 +00003018 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00003019
Christoph Hellwigc6534242011-09-18 20:41:05 +00003020 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3021 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
3022 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003023 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003024 } else {
3025 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003026 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
3027 right.br_startblock, right.br_blockcount,
3028 &i);
3029 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003030 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003031 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003032 error = xfs_btree_delete(bma->cur, &i);
3033 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003034 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003035 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003036 error = xfs_btree_decrement(bma->cur, 0, &i);
3037 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003038 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003039 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003040 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003041 left.br_startblock,
3042 left.br_blockcount +
3043 new->br_blockcount +
3044 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003045 left.br_state);
3046 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003047 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003049 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003051 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 /*
3053 * New allocation is contiguous with a real allocation
3054 * on the left.
3055 * Merge the new allocation with the left neighbor.
3056 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003057 --bma->idx;
3058 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3059 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 left.br_blockcount + new->br_blockcount);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003061 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003062
Christoph Hellwigc6534242011-09-18 20:41:05 +00003063 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003064 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003065 } else {
3066 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003067 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
3068 left.br_startblock, left.br_blockcount,
3069 &i);
3070 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003071 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003072 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003073 error = xfs_bmbt_update(bma->cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003074 left.br_startblock,
3075 left.br_blockcount +
3076 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003077 left.br_state);
3078 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003079 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003081 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
Christoph Hellwig7574aa92009-11-25 00:00:19 +00003083 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 /*
3085 * New allocation is contiguous with a real allocation
3086 * on the right.
3087 * Merge the new allocation with the right neighbor.
3088 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003089 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
3090 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00003091 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 new->br_blockcount + right.br_blockcount,
3093 right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003094 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003095
Christoph Hellwigc6534242011-09-18 20:41:05 +00003096 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003097 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003098 } else {
3099 rval = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003100 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003101 right.br_startoff,
3102 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003103 right.br_blockcount, &i);
3104 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003105 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003106 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003107 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003108 new->br_startblock,
3109 new->br_blockcount +
3110 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003111 right.br_state);
3112 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003113 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003115 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 case 0:
3118 /*
3119 * New allocation is not contiguous with another
3120 * real allocation.
3121 * Insert a new entry.
3122 */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003123 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
3124 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
3125 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
3126 if (bma->cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003127 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003128 } else {
3129 rval = XFS_ILOG_CORE;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003130 error = xfs_bmbt_lookup_eq(bma->cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003131 new->br_startoff,
3132 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003133 new->br_blockcount, &i);
3134 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003135 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003136 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003137 bma->cur->bc_rec.b.br_state = new->br_state;
3138 error = xfs_btree_insert(bma->cur, &i);
3139 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003140 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003141 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003143 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003145
3146 /* convert to a btree if necessary */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00003147 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003148 int tmp_logflags; /* partial log flag return val */
3149
Christoph Hellwigc6534242011-09-18 20:41:05 +00003150 ASSERT(bma->cur == NULL);
3151 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
3152 bma->firstblock, bma->flist, &bma->cur,
3153 0, &tmp_logflags, whichfork);
3154 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003155 if (error)
3156 goto done;
3157 }
3158
3159 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwigc6534242011-09-18 20:41:05 +00003160 if (bma->cur)
3161 bma->cur->bc_private.b.allocated = 0;
3162
3163 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003164done:
Christoph Hellwigc6534242011-09-18 20:41:05 +00003165 bma->logflags |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003166 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167}
3168
Nathan Scottdd9f4382006-01-11 15:28:28 +11003169/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003170 * Functions used in the extent read, allocate and remove paths
3171 */
3172
3173/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003174 * Adjust the size of the new extent based on di_extsize and rt extsize.
3175 */
Dave Chinner68988112013-08-12 20:49:42 +10003176int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003177xfs_bmap_extsize_align(
3178 xfs_mount_t *mp,
3179 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3180 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3181 xfs_extlen_t extsz, /* align to this extent size */
3182 int rt, /* is this a realtime inode? */
3183 int eof, /* is extent at end-of-file? */
3184 int delay, /* creating delalloc extent? */
3185 int convert, /* overwriting unwritten extent? */
3186 xfs_fileoff_t *offp, /* in/out: aligned offset */
3187 xfs_extlen_t *lenp) /* in/out: aligned length */
3188{
3189 xfs_fileoff_t orig_off; /* original offset */
3190 xfs_extlen_t orig_alen; /* original length */
3191 xfs_fileoff_t orig_end; /* original off+len */
3192 xfs_fileoff_t nexto; /* next file offset */
3193 xfs_fileoff_t prevo; /* previous file offset */
3194 xfs_fileoff_t align_off; /* temp for offset */
3195 xfs_extlen_t align_alen; /* temp for length */
3196 xfs_extlen_t temp; /* temp for calculations */
3197
3198 if (convert)
3199 return 0;
3200
3201 orig_off = align_off = *offp;
3202 orig_alen = align_alen = *lenp;
3203 orig_end = orig_off + orig_alen;
3204
3205 /*
3206 * If this request overlaps an existing extent, then don't
3207 * attempt to perform any additional alignment.
3208 */
3209 if (!delay && !eof &&
3210 (orig_off >= gotp->br_startoff) &&
3211 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3212 return 0;
3213 }
3214
3215 /*
3216 * If the file offset is unaligned vs. the extent size
3217 * we need to align it. This will be possible unless
3218 * the file was previously written with a kernel that didn't
3219 * perform this alignment, or if a truncate shot us in the
3220 * foot.
3221 */
3222 temp = do_mod(orig_off, extsz);
3223 if (temp) {
3224 align_alen += temp;
3225 align_off -= temp;
3226 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003227
3228 /* Same adjustment for the end of the requested area. */
3229 temp = (align_alen % extsz);
3230 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003231 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003232
3233 /*
3234 * For large extent hint sizes, the aligned extent might be larger than
3235 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3236 * the length back under MAXEXTLEN. The outer allocation loops handle
3237 * short allocation just fine, so it is safe to do this. We only want to
3238 * do it when we are forced to, though, because it means more allocation
3239 * operations are required.
3240 */
3241 while (align_alen > MAXEXTLEN)
3242 align_alen -= extsz;
3243 ASSERT(align_alen <= MAXEXTLEN);
3244
Nathan Scottdd9f4382006-01-11 15:28:28 +11003245 /*
3246 * If the previous block overlaps with this proposed allocation
3247 * then move the start forward without adjusting the length.
3248 */
3249 if (prevp->br_startoff != NULLFILEOFF) {
3250 if (prevp->br_startblock == HOLESTARTBLOCK)
3251 prevo = prevp->br_startoff;
3252 else
3253 prevo = prevp->br_startoff + prevp->br_blockcount;
3254 } else
3255 prevo = 0;
3256 if (align_off != orig_off && align_off < prevo)
3257 align_off = prevo;
3258 /*
3259 * If the next block overlaps with this proposed allocation
3260 * then move the start back without adjusting the length,
3261 * but not before offset 0.
3262 * This may of course make the start overlap previous block,
3263 * and if we hit the offset 0 limit then the next block
3264 * can still overlap too.
3265 */
3266 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3267 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3268 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3269 nexto = gotp->br_startoff + gotp->br_blockcount;
3270 else
3271 nexto = gotp->br_startoff;
3272 } else
3273 nexto = NULLFILEOFF;
3274 if (!eof &&
3275 align_off + align_alen != orig_end &&
3276 align_off + align_alen > nexto)
3277 align_off = nexto > align_alen ? nexto - align_alen : 0;
3278 /*
3279 * If we're now overlapping the next or previous extent that
3280 * means we can't fit an extsz piece in this hole. Just move
3281 * the start forward to the first valid spot and set
3282 * the length so we hit the end.
3283 */
3284 if (align_off != orig_off && align_off < prevo)
3285 align_off = prevo;
3286 if (align_off + align_alen != orig_end &&
3287 align_off + align_alen > nexto &&
3288 nexto != NULLFILEOFF) {
3289 ASSERT(nexto > prevo);
3290 align_alen = nexto - align_off;
3291 }
3292
3293 /*
3294 * If realtime, and the result isn't a multiple of the realtime
3295 * extent size we need to remove blocks until it is.
3296 */
3297 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3298 /*
3299 * We're not covering the original request, or
3300 * we won't be able to once we fix the length.
3301 */
3302 if (orig_off < align_off ||
3303 orig_end > align_off + align_alen ||
3304 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003305 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003306 /*
3307 * Try to fix it by moving the start up.
3308 */
3309 if (align_off + temp <= orig_off) {
3310 align_alen -= temp;
3311 align_off += temp;
3312 }
3313 /*
3314 * Try to fix it by moving the end in.
3315 */
3316 else if (align_off + align_alen - temp >= orig_end)
3317 align_alen -= temp;
3318 /*
3319 * Set the start to the minimum then trim the length.
3320 */
3321 else {
3322 align_alen -= orig_off - align_off;
3323 align_off = orig_off;
3324 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3325 }
3326 /*
3327 * Result doesn't cover the request, fail it.
3328 */
3329 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003330 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003331 } else {
3332 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003333 /* see MAXEXTLEN handling above */
3334 ASSERT(orig_end <= align_off + align_alen ||
3335 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003336 }
3337
3338#ifdef DEBUG
3339 if (!eof && gotp->br_startoff != NULLFILEOFF)
3340 ASSERT(align_off + align_alen <= gotp->br_startoff);
3341 if (prevp->br_startoff != NULLFILEOFF)
3342 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3343#endif
3344
3345 *lenp = align_alen;
3346 *offp = align_off;
3347 return 0;
3348}
3349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350#define XFS_ALLOC_GAP_UNITS 4
3351
Dave Chinner68988112013-08-12 20:49:42 +10003352void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003353xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003354 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355{
3356 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3358 xfs_mount_t *mp; /* mount point structure */
3359 int nullfb; /* true if ap->firstblock isn't set */
3360 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361
3362#define ISVALID(x,y) \
3363 (rt ? \
3364 (x) < mp->m_sb.sb_rblocks : \
3365 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3366 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3367 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3368
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003370 nullfb = *ap->firstblock == NULLFSBLOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003372 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 /*
3374 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003375 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003377 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3378 !isnullstartblock(ap->prev.br_startblock) &&
3379 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3380 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003381 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 /*
3383 * Adjust for the gap between prevp and us.
3384 */
Dave Chinner3a756672011-09-18 20:40:58 +00003385 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003386 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003388 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3389 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 }
3391 /*
3392 * If not at eof, then compare the two neighbor blocks.
3393 * Figure out whether either one gives us a good starting point,
3394 * and pick the better one.
3395 */
3396 else if (!ap->eof) {
3397 xfs_fsblock_t gotbno; /* right side block number */
3398 xfs_fsblock_t gotdiff=0; /* right side difference */
3399 xfs_fsblock_t prevbno; /* left side block number */
3400 xfs_fsblock_t prevdiff=0; /* left side difference */
3401
3402 /*
3403 * If there's a previous (left) block, select a requested
3404 * start block based on it.
3405 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003406 if (ap->prev.br_startoff != NULLFILEOFF &&
3407 !isnullstartblock(ap->prev.br_startblock) &&
3408 (prevbno = ap->prev.br_startblock +
3409 ap->prev.br_blockcount) &&
3410 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 /*
3412 * Calculate gap to end of previous block.
3413 */
Dave Chinner3a756672011-09-18 20:40:58 +00003414 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003415 (ap->prev.br_startoff +
3416 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 /*
3418 * Figure the startblock based on the previous block's
3419 * end and the gap size.
3420 * Heuristic!
3421 * If the gap is large relative to the piece we're
3422 * allocating, or using it gives us an invalid block
3423 * number, then just use the end of the previous block.
3424 */
Dave Chinner3a756672011-09-18 20:40:58 +00003425 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003427 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 prevbno += adjust;
3429 else
3430 prevdiff += adjust;
3431 /*
3432 * If the firstblock forbids it, can't use it,
3433 * must use default.
3434 */
3435 if (!rt && !nullfb &&
3436 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3437 prevbno = NULLFSBLOCK;
3438 }
3439 /*
3440 * No previous block or can't follow it, just default.
3441 */
3442 else
3443 prevbno = NULLFSBLOCK;
3444 /*
3445 * If there's a following (right) block, select a requested
3446 * start block based on it.
3447 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003448 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 /*
3450 * Calculate gap to start of next block.
3451 */
Dave Chinner3a756672011-09-18 20:40:58 +00003452 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 /*
3454 * Figure the startblock based on the next block's
3455 * start and the gap size.
3456 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003457 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 /*
3459 * Heuristic!
3460 * If the gap is large relative to the piece we're
3461 * allocating, or using it gives us an invalid block
3462 * number, then just use the start of the next block
3463 * offset by our length.
3464 */
Dave Chinner3a756672011-09-18 20:40:58 +00003465 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 ISVALID(gotbno - gotdiff, gotbno))
3467 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003468 else if (ISVALID(gotbno - ap->length, gotbno)) {
3469 gotbno -= ap->length;
3470 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471 } else
3472 gotdiff += adjust;
3473 /*
3474 * If the firstblock forbids it, can't use it,
3475 * must use default.
3476 */
3477 if (!rt && !nullfb &&
3478 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3479 gotbno = NULLFSBLOCK;
3480 }
3481 /*
3482 * No next block, just default.
3483 */
3484 else
3485 gotbno = NULLFSBLOCK;
3486 /*
3487 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003488 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 */
3490 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003491 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003493 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003495 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003497#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003498}
3499
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003500static int
3501xfs_bmap_longest_free_extent(
3502 struct xfs_trans *tp,
3503 xfs_agnumber_t ag,
3504 xfs_extlen_t *blen,
3505 int *notinit)
3506{
3507 struct xfs_mount *mp = tp->t_mountp;
3508 struct xfs_perag *pag;
3509 xfs_extlen_t longest;
3510 int error = 0;
3511
3512 pag = xfs_perag_get(mp, ag);
3513 if (!pag->pagf_init) {
3514 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3515 if (error)
3516 goto out;
3517
3518 if (!pag->pagf_init) {
3519 *notinit = 1;
3520 goto out;
3521 }
3522 }
3523
3524 longest = xfs_alloc_longest_free_extent(mp, pag);
3525 if (*blen < longest)
3526 *blen = longest;
3527
3528out:
3529 xfs_perag_put(pag);
3530 return error;
3531}
3532
3533static void
3534xfs_bmap_select_minlen(
3535 struct xfs_bmalloca *ap,
3536 struct xfs_alloc_arg *args,
3537 xfs_extlen_t *blen,
3538 int notinit)
3539{
3540 if (notinit || *blen < ap->minlen) {
3541 /*
3542 * Since we did a BUF_TRYLOCK above, it is possible that
3543 * there is space for this request.
3544 */
3545 args->minlen = ap->minlen;
3546 } else if (*blen < args->maxlen) {
3547 /*
3548 * If the best seen length is less than the request length,
3549 * use the best as the minimum.
3550 */
3551 args->minlen = *blen;
3552 } else {
3553 /*
3554 * Otherwise we've seen an extent as big as maxlen, use that
3555 * as the minimum.
3556 */
3557 args->minlen = args->maxlen;
3558 }
3559}
3560
Nathan Scotta365bdd2006-03-14 13:34:16 +11003561STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003562xfs_bmap_btalloc_nullfb(
3563 struct xfs_bmalloca *ap,
3564 struct xfs_alloc_arg *args,
3565 xfs_extlen_t *blen)
3566{
3567 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003568 xfs_agnumber_t ag, startag;
3569 int notinit = 0;
3570 int error;
3571
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003572 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003573 args->total = ap->total;
3574
Christoph Hellwigc467c042010-02-15 23:34:42 +00003575 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3576 if (startag == NULLAGNUMBER)
3577 startag = ag = 0;
3578
Dave Chinner14b064c2011-01-27 12:16:28 +11003579 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003580 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3581 &notinit);
3582 if (error)
3583 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003584
Christoph Hellwigc467c042010-02-15 23:34:42 +00003585 if (++ag == mp->m_sb.sb_agcount)
3586 ag = 0;
3587 if (ag == startag)
3588 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003589 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003590
3591 xfs_bmap_select_minlen(ap, args, blen, notinit);
3592 return 0;
3593}
3594
3595STATIC int
3596xfs_bmap_btalloc_filestreams(
3597 struct xfs_bmalloca *ap,
3598 struct xfs_alloc_arg *args,
3599 xfs_extlen_t *blen)
3600{
3601 struct xfs_mount *mp = ap->ip->i_mount;
3602 xfs_agnumber_t ag;
3603 int notinit = 0;
3604 int error;
3605
3606 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3607 args->total = ap->total;
3608
3609 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3610 if (ag == NULLAGNUMBER)
3611 ag = 0;
3612
3613 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3614 if (error)
3615 return error;
3616
3617 if (*blen < args->maxlen) {
3618 error = xfs_filestream_new_ag(ap, &ag);
3619 if (error)
3620 return error;
3621
3622 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3623 &notinit);
3624 if (error)
3625 return error;
3626
3627 }
3628
3629 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003630
3631 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003632 * Set the failure fallback case to look in the selected AG as stream
3633 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003634 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003635 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003636 return 0;
3637}
3638
3639STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003640xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003641 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003642{
3643 xfs_mount_t *mp; /* mount point structure */
3644 xfs_alloctype_t atype = 0; /* type for allocation routines */
3645 xfs_extlen_t align; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003646 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003647 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003648 xfs_alloc_arg_t args;
3649 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003650 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003651 int nullfb; /* true if ap->firstblock isn't set */
3652 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003653 int tryagain;
3654 int error;
Dave Chinner33177f02013-12-12 16:34:36 +11003655 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003656
Dave Chinnera99ebf42011-12-01 11:24:20 +00003657 ASSERT(ap->length);
3658
Nathan Scotta365bdd2006-03-14 13:34:16 +11003659 mp = ap->ip->i_mount;
Dave Chinner33177f02013-12-12 16:34:36 +11003660
3661 /* stripe alignment for allocation is determined by mount parameters */
3662 stripe_align = 0;
3663 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3664 stripe_align = mp->m_swidth;
3665 else if (mp->m_dalign)
3666 stripe_align = mp->m_dalign;
3667
David Chinner957d0eb2007-06-18 16:50:37 +10003668 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003669 if (unlikely(align)) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003670 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003671 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003672 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003673 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003674 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003675 }
Dave Chinner33177f02013-12-12 16:34:36 +11003676
3677
Dave Chinner0937e0f2011-09-18 20:40:57 +00003678 nullfb = *ap->firstblock == NULLFSBLOCK;
3679 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003680 if (nullfb) {
3681 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
3682 ag = xfs_filestream_lookup_ag(ap->ip);
3683 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003684 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003685 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003686 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003687 }
3688 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003689 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003690
3691 xfs_bmap_adjacent(ap);
3692
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003694 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 * it's in the right allocation group.
3696 */
Dave Chinner3a756672011-09-18 20:40:58 +00003697 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 ;
3699 else
Dave Chinner3a756672011-09-18 20:40:58 +00003700 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 * Normal allocation, done through xfs_alloc_vextent.
3703 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003704 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003705 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003706 args.tp = ap->tp;
3707 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003708 args.fsbno = ap->blkno;
Dave Chinner14b064c2011-01-27 12:16:28 +11003709
3710 /* Trim the allocation back to the maximum an AG can fit. */
Dave Chinner3a756672011-09-18 20:40:58 +00003711 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003712 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003713 blen = 0;
3714 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003715 /*
3716 * Search for an allocation group with a single extent large
3717 * enough for the request. If one isn't found, then adjust
3718 * the minimum allocation size to the largest space found.
3719 */
3720 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
3721 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3722 else
3723 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003724 if (error)
3725 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003726 } else if (ap->flist->xbf_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003727 if (xfs_inode_is_filestream(ap->ip))
3728 args.type = XFS_ALLOCTYPE_FIRST_AG;
3729 else
3730 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003731 args.total = args.minlen = ap->minlen;
3732 } else {
3733 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3734 args.total = ap->total;
3735 args.minlen = ap->minlen;
3736 }
David Chinner957d0eb2007-06-18 16:50:37 +10003737 /* apply extent size hints if obtained earlier */
3738 if (unlikely(align)) {
3739 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003740 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003741 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Tim Shimmine6a4b372007-11-23 16:30:42 +11003742 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003743 args.prod = 1;
3744 args.mod = 0;
3745 } else {
Tim Shimmine6a4b372007-11-23 16:30:42 +11003746 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003747 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003748 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3749 }
3750 /*
3751 * If we are not low on available data blocks, and the
3752 * underlying logical volume manager is a stripe, and
3753 * the file offset is zero then try to allocate data
3754 * blocks on stripe unit boundary.
3755 * NOTE: ap->aeof is only set if the allocation length
3756 * is >= the stripe unit and the allocation offset is
3757 * at the end of file.
3758 */
Dave Chinner0937e0f2011-09-18 20:40:57 +00003759 if (!ap->flist->xbf_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003760 if (!ap->offset) {
Dave Chinner33177f02013-12-12 16:34:36 +11003761 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003762 atype = args.type;
3763 isaligned = 1;
3764 /*
3765 * Adjust for alignment
3766 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003767 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003768 args.minlen = blen - args.alignment;
3769 args.minalignslop = 0;
3770 } else {
3771 /*
3772 * First try an exact bno allocation.
3773 * If it fails then do a near or start bno
3774 * allocation with alignment turned on.
3775 */
3776 atype = args.type;
3777 tryagain = 1;
3778 args.type = XFS_ALLOCTYPE_THIS_BNO;
3779 args.alignment = 1;
3780 /*
3781 * Compute the minlen+alignment for the
3782 * next case. Set slop so that the value
3783 * of minlen+alignment+slop doesn't go up
3784 * between the calls.
3785 */
Dave Chinner33177f02013-12-12 16:34:36 +11003786 if (blen > stripe_align && blen <= args.maxlen)
3787 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003788 else
3789 nextminlen = args.minlen;
Dave Chinner33177f02013-12-12 16:34:36 +11003790 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003791 args.minalignslop =
Dave Chinner33177f02013-12-12 16:34:36 +11003792 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003793 args.minlen - 1;
3794 else
3795 args.minalignslop = 0;
3796 }
3797 } else {
3798 args.alignment = 1;
3799 args.minalignslop = 0;
3800 }
3801 args.minleft = ap->minleft;
3802 args.wasdel = ap->wasdel;
3803 args.isfl = 0;
3804 args.userdata = ap->userdata;
3805 if ((error = xfs_alloc_vextent(&args)))
3806 return error;
3807 if (tryagain && args.fsbno == NULLFSBLOCK) {
3808 /*
3809 * Exact allocation failed. Now try with alignment
3810 * turned on.
3811 */
3812 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003813 args.fsbno = ap->blkno;
Dave Chinner33177f02013-12-12 16:34:36 +11003814 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003815 args.minlen = nextminlen;
3816 args.minalignslop = 0;
3817 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 if ((error = xfs_alloc_vextent(&args)))
3819 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003820 }
3821 if (isaligned && args.fsbno == NULLFSBLOCK) {
3822 /*
3823 * allocation failed, so turn off alignment and
3824 * try again.
3825 */
3826 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003827 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003828 args.alignment = 0;
3829 if ((error = xfs_alloc_vextent(&args)))
3830 return error;
3831 }
3832 if (args.fsbno == NULLFSBLOCK && nullfb &&
3833 args.minlen > ap->minlen) {
3834 args.minlen = ap->minlen;
3835 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003836 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003837 if ((error = xfs_alloc_vextent(&args)))
3838 return error;
3839 }
3840 if (args.fsbno == NULLFSBLOCK && nullfb) {
3841 args.fsbno = 0;
3842 args.type = XFS_ALLOCTYPE_FIRST_AG;
3843 args.total = ap->minlen;
3844 args.minleft = 0;
3845 if ((error = xfs_alloc_vextent(&args)))
3846 return error;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003847 ap->flist->xbf_low = 1;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003848 }
3849 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003850 /*
3851 * check the allocation happened at the same or higher AG than
3852 * the first block that was allocated.
3853 */
3854 ASSERT(*ap->firstblock == NULLFSBLOCK ||
3855 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
3856 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
3857 (ap->flist->xbf_low &&
3858 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
3859 XFS_FSB_TO_AGNO(mp, args.fsbno)));
3860
Dave Chinner3a756672011-09-18 20:40:58 +00003861 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003862 if (*ap->firstblock == NULLFSBLOCK)
3863 *ap->firstblock = args.fsbno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003864 ASSERT(nullfb || fb_agno == args.agno ||
Dave Chinner0937e0f2011-09-18 20:40:57 +00003865 (ap->flist->xbf_low && fb_agno < args.agno));
Dave Chinner3a756672011-09-18 20:40:58 +00003866 ap->length = args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003867 ap->ip->i_d.di_nblocks += args.len;
3868 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3869 if (ap->wasdel)
3870 ap->ip->i_delayed_blks -= args.len;
3871 /*
3872 * Adjust the disk quota also. This was reserved
3873 * earlier.
3874 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003875 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003876 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3877 XFS_TRANS_DQ_BCOUNT,
3878 (long) args.len);
3879 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003880 ap->blkno = NULLFSBLOCK;
3881 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 }
3883 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003884}
3885
3886/*
3887 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3888 * It figures out where to ask the underlying allocator to put the new extent.
3889 */
3890STATIC int
3891xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003892 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003893{
Eric Sandeen71ddabb2007-11-23 16:29:42 +11003894 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003895 return xfs_bmap_rtalloc(ap);
3896 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897}
3898
3899/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003900 * Trim the returned map to the required bounds
3901 */
3902STATIC void
3903xfs_bmapi_trim_map(
3904 struct xfs_bmbt_irec *mval,
3905 struct xfs_bmbt_irec *got,
3906 xfs_fileoff_t *bno,
3907 xfs_filblks_t len,
3908 xfs_fileoff_t obno,
3909 xfs_fileoff_t end,
3910 int n,
3911 int flags)
3912{
3913 if ((flags & XFS_BMAPI_ENTIRE) ||
3914 got->br_startoff + got->br_blockcount <= obno) {
3915 *mval = *got;
3916 if (isnullstartblock(got->br_startblock))
3917 mval->br_startblock = DELAYSTARTBLOCK;
3918 return;
3919 }
3920
3921 if (obno > *bno)
3922 *bno = obno;
3923 ASSERT((*bno >= obno) || (n == 0));
3924 ASSERT(*bno < end);
3925 mval->br_startoff = *bno;
3926 if (isnullstartblock(got->br_startblock))
3927 mval->br_startblock = DELAYSTARTBLOCK;
3928 else
3929 mval->br_startblock = got->br_startblock +
3930 (*bno - got->br_startoff);
3931 /*
3932 * Return the minimum of what we got and what we asked for for
3933 * the length. We can use the len variable here because it is
3934 * modified below and we could have been there before coming
3935 * here if the first part of the allocation didn't overlap what
3936 * was asked for.
3937 */
3938 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3939 got->br_blockcount - (*bno - got->br_startoff));
3940 mval->br_state = got->br_state;
3941 ASSERT(mval->br_blockcount <= len);
3942 return;
3943}
3944
3945/*
3946 * Update and validate the extent map to return
3947 */
3948STATIC void
3949xfs_bmapi_update_map(
3950 struct xfs_bmbt_irec **map,
3951 xfs_fileoff_t *bno,
3952 xfs_filblks_t *len,
3953 xfs_fileoff_t obno,
3954 xfs_fileoff_t end,
3955 int *n,
3956 int flags)
3957{
3958 xfs_bmbt_irec_t *mval = *map;
3959
3960 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3961 ((mval->br_startoff + mval->br_blockcount) <= end));
3962 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3963 (mval->br_startoff < obno));
3964
3965 *bno = mval->br_startoff + mval->br_blockcount;
3966 *len = end - *bno;
3967 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3968 /* update previous map with new information */
3969 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3970 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3971 ASSERT(mval->br_state == mval[-1].br_state);
3972 mval[-1].br_blockcount = mval->br_blockcount;
3973 mval[-1].br_state = mval->br_state;
3974 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3975 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3976 mval[-1].br_startblock != HOLESTARTBLOCK &&
3977 mval->br_startblock == mval[-1].br_startblock +
3978 mval[-1].br_blockcount &&
3979 ((flags & XFS_BMAPI_IGSTATE) ||
3980 mval[-1].br_state == mval->br_state)) {
3981 ASSERT(mval->br_startoff ==
3982 mval[-1].br_startoff + mval[-1].br_blockcount);
3983 mval[-1].br_blockcount += mval->br_blockcount;
3984 } else if (*n > 0 &&
3985 mval->br_startblock == DELAYSTARTBLOCK &&
3986 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3987 mval->br_startoff ==
3988 mval[-1].br_startoff + mval[-1].br_blockcount) {
3989 mval[-1].br_blockcount += mval->br_blockcount;
3990 mval[-1].br_state = mval->br_state;
3991 } else if (!((*n == 0) &&
3992 ((mval->br_startoff + mval->br_blockcount) <=
3993 obno))) {
3994 mval++;
3995 (*n)++;
3996 }
3997 *map = mval;
3998}
3999
4000/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00004001 * Map file blocks to filesystem blocks without allocation.
4002 */
4003int
4004xfs_bmapi_read(
4005 struct xfs_inode *ip,
4006 xfs_fileoff_t bno,
4007 xfs_filblks_t len,
4008 struct xfs_bmbt_irec *mval,
4009 int *nmap,
4010 int flags)
4011{
4012 struct xfs_mount *mp = ip->i_mount;
4013 struct xfs_ifork *ifp;
4014 struct xfs_bmbt_irec got;
4015 struct xfs_bmbt_irec prev;
4016 xfs_fileoff_t obno;
4017 xfs_fileoff_t end;
4018 xfs_extnum_t lastx;
4019 int error;
4020 int eof;
4021 int n = 0;
4022 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4023 XFS_ATTR_FORK : XFS_DATA_FORK;
4024
4025 ASSERT(*nmap >= 1);
4026 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4027 XFS_BMAPI_IGSTATE)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004028 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00004029
4030 if (unlikely(XFS_TEST_ERROR(
4031 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4032 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4033 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4034 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004035 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004036 }
4037
4038 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004039 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004040
4041 XFS_STATS_INC(xs_blk_mapr);
4042
4043 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004044
4045 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4046 error = xfs_iread_extents(NULL, ip, whichfork);
4047 if (error)
4048 return error;
4049 }
4050
4051 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4052 end = bno + len;
4053 obno = bno;
4054
4055 while (bno < end && n < *nmap) {
4056 /* Reading past eof, act as though there's a hole up to end. */
4057 if (eof)
4058 got.br_startoff = end;
4059 if (got.br_startoff > bno) {
4060 /* Reading in a hole. */
4061 mval->br_startoff = bno;
4062 mval->br_startblock = HOLESTARTBLOCK;
4063 mval->br_blockcount =
4064 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4065 mval->br_state = XFS_EXT_NORM;
4066 bno += mval->br_blockcount;
4067 len -= mval->br_blockcount;
4068 mval++;
4069 n++;
4070 continue;
4071 }
4072
4073 /* set up the extent map to return. */
4074 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4075 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4076
4077 /* If we're done, stop now. */
4078 if (bno >= end || n >= *nmap)
4079 break;
4080
4081 /* Else go on to the next record. */
4082 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4083 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4084 else
4085 eof = 1;
4086 }
4087 *nmap = n;
4088 return 0;
4089}
4090
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004091STATIC int
4092xfs_bmapi_reserve_delalloc(
4093 struct xfs_inode *ip,
4094 xfs_fileoff_t aoff,
4095 xfs_filblks_t len,
4096 struct xfs_bmbt_irec *got,
4097 struct xfs_bmbt_irec *prev,
4098 xfs_extnum_t *lastx,
4099 int eof)
4100{
4101 struct xfs_mount *mp = ip->i_mount;
4102 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4103 xfs_extlen_t alen;
4104 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004105 char rt = XFS_IS_REALTIME_INODE(ip);
4106 xfs_extlen_t extsz;
4107 int error;
4108
4109 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4110 if (!eof)
4111 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4112
4113 /* Figure out the extent size, adjust alen */
4114 extsz = xfs_get_extsz_hint(ip);
4115 if (extsz) {
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004116 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4117 1, 0, &aoff, &alen);
4118 ASSERT(!error);
4119 }
4120
4121 if (rt)
4122 extsz = alen / mp->m_sb.sb_rextsize;
4123
4124 /*
4125 * Make a transaction-less quota reservation for delayed allocation
4126 * blocks. This number gets adjusted later. We return if we haven't
4127 * allocated blocks already inside this loop.
4128 */
4129 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4130 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4131 if (error)
4132 return error;
4133
4134 /*
4135 * Split changing sb for alen and indlen since they could be coming
4136 * from different places.
4137 */
4138 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4139 ASSERT(indlen > 0);
4140
4141 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004142 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004143 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004144 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004145 }
4146
4147 if (error)
4148 goto out_unreserve_quota;
4149
Dave Chinner0d485ad2015-02-23 21:22:03 +11004150 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004151 if (error)
4152 goto out_unreserve_blocks;
4153
4154
4155 ip->i_delayed_blks += alen;
4156
4157 got->br_startoff = aoff;
4158 got->br_startblock = nullstartblock(indlen);
4159 got->br_blockcount = alen;
4160 got->br_state = XFS_EXT_NORM;
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004161 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004162
4163 /*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00004164 * Update our extent pointer, given that xfs_bmap_add_extent_hole_delay
4165 * might have merged it into one of the neighbouring ones.
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004166 */
4167 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4168
4169 ASSERT(got->br_startoff <= aoff);
4170 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4171 ASSERT(isnullstartblock(got->br_startblock));
4172 ASSERT(got->br_state == XFS_EXT_NORM);
4173 return 0;
4174
4175out_unreserve_blocks:
4176 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004177 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004178 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004179 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004180out_unreserve_quota:
4181 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004182 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004183 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4184 return error;
4185}
4186
Dave Chinner5c8ed202011-09-18 20:40:45 +00004187/*
Christoph Hellwig44032802011-09-18 20:40:48 +00004188 * Map file blocks to filesystem blocks, adding delayed allocations as needed.
4189 */
4190int
4191xfs_bmapi_delay(
4192 struct xfs_inode *ip, /* incore inode */
4193 xfs_fileoff_t bno, /* starting file offs. mapped */
4194 xfs_filblks_t len, /* length to map in file */
4195 struct xfs_bmbt_irec *mval, /* output: map values */
4196 int *nmap, /* i/o: mval size/count */
4197 int flags) /* XFS_BMAPI_... */
4198{
4199 struct xfs_mount *mp = ip->i_mount;
4200 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4201 struct xfs_bmbt_irec got; /* current file extent record */
4202 struct xfs_bmbt_irec prev; /* previous file extent record */
4203 xfs_fileoff_t obno; /* old block number (offset) */
4204 xfs_fileoff_t end; /* end of mapped file region */
4205 xfs_extnum_t lastx; /* last useful extent number */
4206 int eof; /* we've hit the end of extents */
4207 int n = 0; /* current extent index */
4208 int error = 0;
4209
4210 ASSERT(*nmap >= 1);
4211 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4212 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004213 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig44032802011-09-18 20:40:48 +00004214
4215 if (unlikely(XFS_TEST_ERROR(
4216 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4217 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4218 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4219 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004220 return -EFSCORRUPTED;
Christoph Hellwig44032802011-09-18 20:40:48 +00004221 }
4222
4223 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004224 return -EIO;
Christoph Hellwig44032802011-09-18 20:40:48 +00004225
4226 XFS_STATS_INC(xs_blk_mapw);
4227
4228 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4229 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4230 if (error)
4231 return error;
4232 }
4233
4234 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4235 end = bno + len;
4236 obno = bno;
4237
4238 while (bno < end && n < *nmap) {
4239 if (eof || got.br_startoff > bno) {
4240 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4241 &prev, &lastx, eof);
4242 if (error) {
4243 if (n == 0) {
4244 *nmap = 0;
4245 return error;
4246 }
4247 break;
4248 }
4249 }
4250
4251 /* set up the extent map to return. */
4252 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4253 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4254
4255 /* If we're done, stop now. */
4256 if (bno >= end || n >= *nmap)
4257 break;
4258
4259 /* Else go on to the next record. */
4260 prev = got;
4261 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4262 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4263 else
4264 eof = 1;
4265 }
4266
4267 *nmap = n;
4268 return 0;
4269}
4270
4271
Dave Chinnercf11da92014-07-15 07:08:24 +10004272static int
4273xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004274 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004275{
4276 struct xfs_mount *mp = bma->ip->i_mount;
Dave Chinnere04426b2012-10-05 11:06:59 +10004277 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004278 XFS_ATTR_FORK : XFS_DATA_FORK;
4279 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004280 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004281 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004282
Dave Chinnera99ebf42011-12-01 11:24:20 +00004283 ASSERT(bma->length > 0);
4284
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004285 /*
4286 * For the wasdelay case, we could also just allocate the stuff asked
4287 * for in this bmap call but that wouldn't be as good.
4288 */
4289 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004290 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4291 bma->offset = bma->got.br_startoff;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004292 if (bma->idx != NULLEXTNUM && bma->idx) {
4293 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004294 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004295 }
4296 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004297 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004298 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004299 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004300 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004301 }
4302
4303 /*
4304 * Indicate if this is the first user data in the file, or just any
4305 * user data.
4306 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004307 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004308 bma->userdata = (bma->offset == 0) ?
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004309 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4310 }
4311
Dave Chinnere04426b2012-10-05 11:06:59 +10004312 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004313
4314 /*
4315 * Only want to do the alignment at the eof if it is userdata and
4316 * allocation length is larger than a stripe unit.
4317 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004318 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004319 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004320 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004321 if (error)
4322 return error;
4323 }
4324
4325 error = xfs_bmap_alloc(bma);
4326 if (error)
4327 return error;
4328
Dave Chinner0937e0f2011-09-18 20:40:57 +00004329 if (bma->flist->xbf_low)
4330 bma->minleft = 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004331 if (bma->cur)
4332 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004333 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004334 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004335 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4336 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4337 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4338 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004339 }
4340 /*
4341 * Bump the number of extents we've allocated
4342 * in this call.
4343 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004344 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004345
Dave Chinner29c8d172011-09-18 20:41:00 +00004346 if (bma->cur)
4347 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004348 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4349
Dave Chinner963c30c2011-09-18 20:40:59 +00004350 bma->got.br_startoff = bma->offset;
4351 bma->got.br_startblock = bma->blkno;
4352 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004353 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004354
4355 /*
4356 * A wasdelay extent has been initialized, so shouldn't be flagged
4357 * as unwritten.
4358 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004359 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004360 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004361 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004362
Christoph Hellwigc6534242011-09-18 20:41:05 +00004363 if (bma->wasdel)
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00004364 error = xfs_bmap_add_extent_delay_real(bma);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004365 else
4366 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004367
Christoph Hellwigc315c902011-09-18 20:41:02 +00004368 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004369 if (error)
4370 return error;
4371
4372 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004373 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4374 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4375 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004376 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004377 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004378
Dave Chinner963c30c2011-09-18 20:40:59 +00004379 ASSERT(bma->got.br_startoff <= bma->offset);
4380 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4381 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004382 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4383 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004384 return 0;
4385}
4386
Dave Chinnerb447fe52011-09-18 20:40:51 +00004387STATIC int
4388xfs_bmapi_convert_unwritten(
4389 struct xfs_bmalloca *bma,
4390 struct xfs_bmbt_irec *mval,
4391 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004392 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004393{
4394 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4395 XFS_ATTR_FORK : XFS_DATA_FORK;
4396 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004397 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004398 int error;
4399
Dave Chinnerb447fe52011-09-18 20:40:51 +00004400 /* check if we need to do unwritten->real conversion */
4401 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4402 (flags & XFS_BMAPI_PREALLOC))
4403 return 0;
4404
4405 /* check if we need to do real->unwritten conversion */
4406 if (mval->br_state == XFS_EXT_NORM &&
4407 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4408 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4409 return 0;
4410
4411 /*
4412 * Modify (by adding) the state flag, if writing.
4413 */
4414 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004415 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4416 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004417 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004418 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4419 bma->cur->bc_private.b.flist = bma->flist;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004420 }
4421 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4422 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4423
Dave Chinnere0c3da52011-09-18 20:41:01 +00004424 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004425 &bma->cur, mval, bma->firstblock, bma->flist,
4426 &tmp_logflags);
4427 bma->logflags |= tmp_logflags;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004428 if (error)
4429 return error;
4430
4431 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004432 * Update our extent pointer, given that
4433 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4434 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004435 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004436 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004437
4438 /*
4439 * We may have combined previously unwritten space with written space,
4440 * so generate another request.
4441 */
4442 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004443 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004444 return 0;
4445}
4446
Christoph Hellwig44032802011-09-18 20:40:48 +00004447/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004448 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4449 * extent state if necessary. Details behaviour is controlled by the flags
4450 * parameter. Only allocates blocks from a single allocation group, to avoid
4451 * locking problems.
4452 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 * The returned value in "firstblock" from the first call in a transaction
4454 * must be remembered and presented to subsequent calls in "firstblock".
4455 * An upper bound for the number of blocks to be allocated is supplied to
4456 * the first call in "total"; if no allocation group has that many free
4457 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4458 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004459int
4460xfs_bmapi_write(
4461 struct xfs_trans *tp, /* transaction pointer */
4462 struct xfs_inode *ip, /* incore inode */
4463 xfs_fileoff_t bno, /* starting file offs. mapped */
4464 xfs_filblks_t len, /* length to map in file */
4465 int flags, /* XFS_BMAPI_... */
4466 xfs_fsblock_t *firstblock, /* first allocated block
4467 controls a.g. for allocs */
4468 xfs_extlen_t total, /* total blocks needed */
4469 struct xfs_bmbt_irec *mval, /* output: map values */
4470 int *nmap, /* i/o: mval size/count */
4471 struct xfs_bmap_free *flist) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004473 struct xfs_mount *mp = ip->i_mount;
4474 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004475 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004476 xfs_fileoff_t end; /* end of mapped file region */
4477 int eof; /* after the end of extents */
4478 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004479 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004480 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004481 int whichfork; /* data or attr fork */
4482 char inhole; /* current location is hole in file */
4483 char wasdelay; /* old extent was delayed */
4484
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004486 xfs_fileoff_t orig_bno; /* original block number value */
4487 int orig_flags; /* original flags arg value */
4488 xfs_filblks_t orig_len; /* original value of len arg */
4489 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4490 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
4492 orig_bno = bno;
4493 orig_len = len;
4494 orig_flags = flags;
4495 orig_mval = mval;
4496 orig_nmap = *nmap;
4497#endif
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004498 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4499 XFS_ATTR_FORK : XFS_DATA_FORK;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004500
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004502 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4503 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4504 ASSERT(tp != NULL);
Dave Chinnera99ebf42011-12-01 11:24:20 +00004505 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004506 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004507 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004508
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509 if (unlikely(XFS_TEST_ERROR(
4510 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004511 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004513 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004514 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004516
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004518 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004519
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004521
4522 XFS_STATS_INC(xs_blk_mapw);
4523
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004524 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004526 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004528 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004529 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004530 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004531 }
4532
4533 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4534 error = xfs_iread_extents(tp, ip, whichfork);
4535 if (error)
4536 goto error0;
4537 }
4538
Dave Chinnere0c3da52011-09-18 20:41:01 +00004539 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004540 &bma.prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 n = 0;
4542 end = bno + len;
4543 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004544
4545 bma.tp = tp;
4546 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004547 bma.total = total;
4548 bma.userdata = 0;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004549 bma.flist = flist;
4550 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 while (bno < end && n < *nmap) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004553 inhole = eof || bma.got.br_startoff > bno;
4554 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004555
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 /*
4557 * First, deal with the hole before the allocated space
4558 * that we found, if any.
4559 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004560 if (inhole || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004561 bma.eof = eof;
4562 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4563 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004564 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004565 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004566
Dave Chinnera99ebf42011-12-01 11:24:20 +00004567 /*
4568 * There's a 32/64 bit type mismatch between the
4569 * allocation length request (which can be 64 bits in
4570 * length) and the bma length request, which is
4571 * xfs_extlen_t and therefore 32 bits. Hence we have to
4572 * check for 32-bit overflows and handle them here.
4573 */
4574 if (len > (xfs_filblks_t)MAXEXTLEN)
4575 bma.length = MAXEXTLEN;
4576 else
4577 bma.length = len;
4578
4579 ASSERT(len > 0);
4580 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004581 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 if (error)
4583 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004584 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004585 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004587
Dave Chinneraef9a892011-09-18 20:40:44 +00004588 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004589 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4590 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Dave Chinnerb447fe52011-09-18 20:40:51 +00004592 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004593 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004594 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004595 continue;
4596 if (error)
4597 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Dave Chinneraef9a892011-09-18 20:40:44 +00004599 /* update the extent map to return */
4600 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4601
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 /*
4603 * If we're done, stop now. Stop when we've allocated
4604 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4605 * the transaction may get too big.
4606 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004607 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004609
4610 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004611 bma.prev = bma.got;
Dave Chinnere0c3da52011-09-18 20:41:01 +00004612 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
4613 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
4614 &bma.got);
4615 } else
Christoph Hellwig5690f922011-05-11 15:04:07 +00004616 eof = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 /*
4621 * Transform from btree to extents, give it cur.
4622 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004623 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004624 int tmp_logflags = 0;
4625
Dave Chinner29c8d172011-09-18 20:41:00 +00004626 ASSERT(bma.cur);
4627 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004629 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 if (error)
4631 goto error0;
4632 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004633
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004635 XFS_IFORK_NEXTENTS(ip, whichfork) >
4636 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638error0:
4639 /*
4640 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004641 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004643 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004645 bma.logflags &= ~xfs_ilog_fext(whichfork);
4646 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004648 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 /*
4650 * Log whatever the flags say, even if error. Otherwise we might miss
4651 * detecting a case where the data is changed, there's an error,
4652 * and it's not logged so we don't shutdown when we should.
4653 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004654 if (bma.logflags)
4655 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004656
Dave Chinner29c8d172011-09-18 20:41:00 +00004657 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 if (!error) {
4659 ASSERT(*firstblock == NULLFSBLOCK ||
4660 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4661 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004662 bma.cur->bc_private.b.firstblock) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 (flist->xbf_low &&
4664 XFS_FSB_TO_AGNO(mp, *firstblock) <
4665 XFS_FSB_TO_AGNO(mp,
Dave Chinner29c8d172011-09-18 20:41:00 +00004666 bma.cur->bc_private.b.firstblock)));
4667 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004669 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4671 }
4672 if (!error)
4673 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4674 orig_nmap, *nmap);
4675 return error;
4676}
4677
4678/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004679 * Called by xfs_bmapi to update file extent records and the btree
4680 * after removing space (or undoing a delayed allocation).
4681 */
4682STATIC int /* error */
4683xfs_bmap_del_extent(
4684 xfs_inode_t *ip, /* incore inode pointer */
4685 xfs_trans_t *tp, /* current transaction pointer */
4686 xfs_extnum_t *idx, /* extent number to update/delete */
4687 xfs_bmap_free_t *flist, /* list of extents to be freed */
4688 xfs_btree_cur_t *cur, /* if null, not a btree */
4689 xfs_bmbt_irec_t *del, /* data to remove from extents */
4690 int *logflagsp, /* inode logging flags */
4691 int whichfork) /* data or attr fork */
4692{
4693 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
4694 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
4695 xfs_fsblock_t del_endblock=0; /* first block past del */
4696 xfs_fileoff_t del_endoff; /* first offset past del */
4697 int delay; /* current block is delayed allocated */
4698 int do_fx; /* free extent at end of routine */
4699 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
4700 int error; /* error return value */
4701 int flags; /* inode logging flags */
4702 xfs_bmbt_irec_t got; /* current extent entry */
4703 xfs_fileoff_t got_endoff; /* first offset past got */
4704 int i; /* temp state */
4705 xfs_ifork_t *ifp; /* inode fork pointer */
4706 xfs_mount_t *mp; /* mount structure */
4707 xfs_filblks_t nblks; /* quota/sb block count */
4708 xfs_bmbt_irec_t new; /* new record to be inserted */
4709 /* REFERENCED */
4710 uint qfield; /* quota field to update */
4711 xfs_filblks_t temp; /* for indirect length calculations */
4712 xfs_filblks_t temp2; /* for indirect length calculations */
4713 int state = 0;
4714
4715 XFS_STATS_INC(xs_del_exlist);
4716
4717 if (whichfork == XFS_ATTR_FORK)
4718 state |= BMAP_ATTRFORK;
4719
4720 mp = ip->i_mount;
4721 ifp = XFS_IFORK_PTR(ip, whichfork);
4722 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
4723 (uint)sizeof(xfs_bmbt_rec_t)));
4724 ASSERT(del->br_blockcount > 0);
4725 ep = xfs_iext_get_ext(ifp, *idx);
4726 xfs_bmbt_get_all(ep, &got);
4727 ASSERT(got.br_startoff <= del->br_startoff);
4728 del_endoff = del->br_startoff + del->br_blockcount;
4729 got_endoff = got.br_startoff + got.br_blockcount;
4730 ASSERT(got_endoff >= del_endoff);
4731 delay = isnullstartblock(got.br_startblock);
4732 ASSERT(isnullstartblock(del->br_startblock) == delay);
4733 flags = 0;
4734 qfield = 0;
4735 error = 0;
4736 /*
4737 * If deleting a real allocation, must free up the disk space.
4738 */
4739 if (!delay) {
4740 flags = XFS_ILOG_CORE;
4741 /*
4742 * Realtime allocation. Free it and record di_nblocks update.
4743 */
4744 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4745 xfs_fsblock_t bno;
4746 xfs_filblks_t len;
4747
4748 ASSERT(do_mod(del->br_blockcount,
4749 mp->m_sb.sb_rextsize) == 0);
4750 ASSERT(do_mod(del->br_startblock,
4751 mp->m_sb.sb_rextsize) == 0);
4752 bno = del->br_startblock;
4753 len = del->br_blockcount;
4754 do_div(bno, mp->m_sb.sb_rextsize);
4755 do_div(len, mp->m_sb.sb_rextsize);
4756 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4757 if (error)
4758 goto done;
4759 do_fx = 0;
4760 nblks = len * mp->m_sb.sb_rextsize;
4761 qfield = XFS_TRANS_DQ_RTBCOUNT;
4762 }
4763 /*
4764 * Ordinary allocation.
4765 */
4766 else {
4767 do_fx = 1;
4768 nblks = del->br_blockcount;
4769 qfield = XFS_TRANS_DQ_BCOUNT;
4770 }
4771 /*
4772 * Set up del_endblock and cur for later.
4773 */
4774 del_endblock = del->br_startblock + del->br_blockcount;
4775 if (cur) {
4776 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
4777 got.br_startblock, got.br_blockcount,
4778 &i)))
4779 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004780 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004781 }
4782 da_old = da_new = 0;
4783 } else {
4784 da_old = startblockval(got.br_startblock);
4785 da_new = 0;
4786 nblks = 0;
4787 do_fx = 0;
4788 }
4789 /*
4790 * Set flag value to use in switch statement.
4791 * Left-contig is 2, right-contig is 1.
4792 */
4793 switch (((got.br_startoff == del->br_startoff) << 1) |
4794 (got_endoff == del_endoff)) {
4795 case 3:
4796 /*
4797 * Matches the whole extent. Delete the entry.
4798 */
4799 xfs_iext_remove(ip, *idx, 1,
4800 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
4801 --*idx;
4802 if (delay)
4803 break;
4804
4805 XFS_IFORK_NEXT_SET(ip, whichfork,
4806 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4807 flags |= XFS_ILOG_CORE;
4808 if (!cur) {
4809 flags |= xfs_ilog_fext(whichfork);
4810 break;
4811 }
4812 if ((error = xfs_btree_delete(cur, &i)))
4813 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004814 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004815 break;
4816
4817 case 2:
4818 /*
4819 * Deleting the first part of the extent.
4820 */
4821 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4822 xfs_bmbt_set_startoff(ep, del_endoff);
4823 temp = got.br_blockcount - del->br_blockcount;
4824 xfs_bmbt_set_blockcount(ep, temp);
4825 if (delay) {
4826 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4827 da_old);
4828 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4829 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4830 da_new = temp;
4831 break;
4832 }
4833 xfs_bmbt_set_startblock(ep, del_endblock);
4834 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4835 if (!cur) {
4836 flags |= xfs_ilog_fext(whichfork);
4837 break;
4838 }
4839 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
4840 got.br_blockcount - del->br_blockcount,
4841 got.br_state)))
4842 goto done;
4843 break;
4844
4845 case 1:
4846 /*
4847 * Deleting the last part of the extent.
4848 */
4849 temp = got.br_blockcount - del->br_blockcount;
4850 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4851 xfs_bmbt_set_blockcount(ep, temp);
4852 if (delay) {
4853 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
4854 da_old);
4855 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4856 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4857 da_new = temp;
4858 break;
4859 }
4860 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4861 if (!cur) {
4862 flags |= xfs_ilog_fext(whichfork);
4863 break;
4864 }
4865 if ((error = xfs_bmbt_update(cur, got.br_startoff,
4866 got.br_startblock,
4867 got.br_blockcount - del->br_blockcount,
4868 got.br_state)))
4869 goto done;
4870 break;
4871
4872 case 0:
4873 /*
4874 * Deleting the middle of the extent.
4875 */
4876 temp = del->br_startoff - got.br_startoff;
4877 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4878 xfs_bmbt_set_blockcount(ep, temp);
4879 new.br_startoff = del_endoff;
4880 temp2 = got_endoff - del_endoff;
4881 new.br_blockcount = temp2;
4882 new.br_state = got.br_state;
4883 if (!delay) {
4884 new.br_startblock = del_endblock;
4885 flags |= XFS_ILOG_CORE;
4886 if (cur) {
4887 if ((error = xfs_bmbt_update(cur,
4888 got.br_startoff,
4889 got.br_startblock, temp,
4890 got.br_state)))
4891 goto done;
4892 if ((error = xfs_btree_increment(cur, 0, &i)))
4893 goto done;
4894 cur->bc_rec.b = new;
4895 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10004896 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004897 goto done;
4898 /*
4899 * If get no-space back from btree insert,
4900 * it tried a split, and we have a zero
4901 * block reservation.
4902 * Fix up our state and return the error.
4903 */
Dave Chinner24513372014-06-25 14:58:08 +10004904 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11004905 /*
4906 * Reset the cursor, don't trust
4907 * it after any insert operation.
4908 */
4909 if ((error = xfs_bmbt_lookup_eq(cur,
4910 got.br_startoff,
4911 got.br_startblock,
4912 temp, &i)))
4913 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004914 XFS_WANT_CORRUPTED_GOTO(mp,
4915 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004916 /*
4917 * Update the btree record back
4918 * to the original value.
4919 */
4920 if ((error = xfs_bmbt_update(cur,
4921 got.br_startoff,
4922 got.br_startblock,
4923 got.br_blockcount,
4924 got.br_state)))
4925 goto done;
4926 /*
4927 * Reset the extent record back
4928 * to the original value.
4929 */
4930 xfs_bmbt_set_blockcount(ep,
4931 got.br_blockcount);
4932 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10004933 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004934 goto done;
4935 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11004936 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004937 } else
4938 flags |= xfs_ilog_fext(whichfork);
4939 XFS_IFORK_NEXT_SET(ip, whichfork,
4940 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
4941 } else {
4942 ASSERT(whichfork == XFS_DATA_FORK);
4943 temp = xfs_bmap_worst_indlen(ip, temp);
4944 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
4945 temp2 = xfs_bmap_worst_indlen(ip, temp2);
4946 new.br_startblock = nullstartblock((int)temp2);
4947 da_new = temp + temp2;
4948 while (da_new > da_old) {
4949 if (temp) {
4950 temp--;
4951 da_new--;
4952 xfs_bmbt_set_startblock(ep,
4953 nullstartblock((int)temp));
4954 }
4955 if (da_new == da_old)
4956 break;
4957 if (temp2) {
4958 temp2--;
4959 da_new--;
4960 new.br_startblock =
4961 nullstartblock((int)temp2);
4962 }
4963 }
4964 }
4965 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4966 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
4967 ++*idx;
4968 break;
4969 }
4970 /*
4971 * If we need to, add to list of extents to delete.
4972 */
4973 if (do_fx)
4974 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
4975 mp);
4976 /*
4977 * Adjust inode # blocks in the file.
4978 */
4979 if (nblks)
4980 ip->i_d.di_nblocks -= nblks;
4981 /*
4982 * Adjust quota data.
4983 */
4984 if (qfield)
4985 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
4986
4987 /*
4988 * Account for change in delayed indirect blocks.
4989 * Nothing to do for disk quota accounting here.
4990 */
4991 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11004992 if (da_old > da_new)
4993 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004994done:
4995 *logflagsp = flags;
4996 return error;
4997}
4998
4999/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000 * Unmap (remove) blocks from a file.
5001 * If nexts is nonzero then the number of extents to remove is limited to
5002 * that value. If not all extents in the block range can be removed then
5003 * *done is set.
5004 */
5005int /* error */
5006xfs_bunmapi(
5007 xfs_trans_t *tp, /* transaction pointer */
5008 struct xfs_inode *ip, /* incore inode */
5009 xfs_fileoff_t bno, /* starting offset to unmap */
5010 xfs_filblks_t len, /* length to unmap in file */
5011 int flags, /* misc flags */
5012 xfs_extnum_t nexts, /* number of extents max */
5013 xfs_fsblock_t *firstblock, /* first allocated block
5014 controls a.g. for allocs */
5015 xfs_bmap_free_t *flist, /* i/o: list extents to free */
5016 int *done) /* set if not done yet */
5017{
5018 xfs_btree_cur_t *cur; /* bmap btree cursor */
5019 xfs_bmbt_irec_t del; /* extent being deleted */
5020 int eof; /* is deleting at eof */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10005021 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022 int error; /* error return value */
5023 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005024 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 xfs_ifork_t *ifp; /* inode fork pointer */
5026 int isrt; /* freeing in rt area */
5027 xfs_extnum_t lastx; /* last extent index used */
5028 int logflags; /* transaction logging flags */
5029 xfs_extlen_t mod; /* rt extent offset */
5030 xfs_mount_t *mp; /* mount structure */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005031 xfs_extnum_t nextents; /* number of file extents */
5032 xfs_bmbt_irec_t prev; /* previous extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 xfs_fileoff_t start; /* first file offset deleted */
5034 int tmp_logflags; /* partial logging flags */
5035 int wasdel; /* was a delayed alloc extent */
5036 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 xfs_fsblock_t sum;
5038
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005039 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5040
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5042 XFS_ATTR_FORK : XFS_DATA_FORK;
5043 ifp = XFS_IFORK_PTR(ip, whichfork);
5044 if (unlikely(
5045 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5046 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5047 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5048 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005049 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 }
5051 mp = ip->i_mount;
5052 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005053 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005054
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005055 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056 ASSERT(len > 0);
5057 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005058
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5060 (error = xfs_iread_extents(tp, ip, whichfork)))
5061 return error;
5062 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5063 if (nextents == 0) {
5064 *done = 1;
5065 return 0;
5066 }
5067 XFS_STATS_INC(xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005068 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 start = bno;
5070 bno = start + len - 1;
5071 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5072 &prev);
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005073
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074 /*
5075 * Check to see if the given block number is past the end of the
5076 * file, back up to the last block if so...
5077 */
5078 if (eof) {
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005079 ep = xfs_iext_get_ext(ifp, --lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080 xfs_bmbt_get_all(ep, &got);
5081 bno = got.br_startoff + got.br_blockcount - 1;
5082 }
5083 logflags = 0;
5084 if (ifp->if_flags & XFS_IFBROOT) {
5085 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005086 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 cur->bc_private.b.firstblock = *firstblock;
5088 cur->bc_private.b.flist = flist;
5089 cur->bc_private.b.flags = 0;
5090 } else
5091 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005092
5093 if (isrt) {
5094 /*
5095 * Synchronize by locking the bitmap inode.
5096 */
5097 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5098 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5099 }
5100
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101 extno = 0;
5102 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5103 (nexts == 0 || extno < nexts)) {
5104 /*
5105 * Is the found extent after a hole in which bno lives?
5106 * Just back up to the previous extent, if so.
5107 */
5108 if (got.br_startoff > bno) {
5109 if (--lastx < 0)
5110 break;
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005111 ep = xfs_iext_get_ext(ifp, lastx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112 xfs_bmbt_get_all(ep, &got);
5113 }
5114 /*
5115 * Is the last block of this extent before the range
5116 * we're supposed to delete? If so, we're done.
5117 */
5118 bno = XFS_FILEOFF_MIN(bno,
5119 got.br_startoff + got.br_blockcount - 1);
5120 if (bno < start)
5121 break;
5122 /*
5123 * Then deal with the (possibly delayed) allocated space
5124 * we found.
5125 */
5126 ASSERT(ep != NULL);
5127 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005128 wasdel = isnullstartblock(del.br_startblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005129 if (got.br_startoff < start) {
5130 del.br_startoff = start;
5131 del.br_blockcount -= start - got.br_startoff;
5132 if (!wasdel)
5133 del.br_startblock += start - got.br_startoff;
5134 }
5135 if (del.br_startoff + del.br_blockcount > bno + 1)
5136 del.br_blockcount = bno + 1 - del.br_startoff;
5137 sum = del.br_startblock + del.br_blockcount;
5138 if (isrt &&
5139 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5140 /*
5141 * Realtime extent not lined up at the end.
5142 * The extent could have been split into written
5143 * and unwritten pieces, or we could just be
5144 * unmapping part of it. But we can't really
5145 * get rid of part of a realtime extent.
5146 */
5147 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005148 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 /*
5150 * This piece is unwritten, or we're not
5151 * using unwritten extents. Skip over it.
5152 */
5153 ASSERT(bno >= mod);
5154 bno -= mod > del.br_blockcount ?
5155 del.br_blockcount : mod;
5156 if (bno < got.br_startoff) {
5157 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005158 xfs_bmbt_get_all(xfs_iext_get_ext(
5159 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160 }
5161 continue;
5162 }
5163 /*
5164 * It's written, turn it unwritten.
5165 * This is better than zeroing it.
5166 */
5167 ASSERT(del.br_state == XFS_EXT_NORM);
5168 ASSERT(xfs_trans_get_block_res(tp) > 0);
5169 /*
5170 * If this spans a realtime extent boundary,
5171 * chop it back to the start of the one we end at.
5172 */
5173 if (del.br_blockcount > mod) {
5174 del.br_startoff += del.br_blockcount - mod;
5175 del.br_startblock += del.br_blockcount - mod;
5176 del.br_blockcount = mod;
5177 }
5178 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005179 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5180 &lastx, &cur, &del, firstblock, flist,
5181 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005182 if (error)
5183 goto error0;
5184 goto nodelete;
5185 }
5186 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5187 /*
5188 * Realtime extent is lined up at the end but not
5189 * at the front. We'll get rid of full extents if
5190 * we can.
5191 */
5192 mod = mp->m_sb.sb_rextsize - mod;
5193 if (del.br_blockcount > mod) {
5194 del.br_blockcount -= mod;
5195 del.br_startoff += mod;
5196 del.br_startblock += mod;
5197 } else if ((del.br_startoff == start &&
5198 (del.br_state == XFS_EXT_UNWRITTEN ||
5199 xfs_trans_get_block_res(tp) == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005200 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201 /*
5202 * Can't make it unwritten. There isn't
5203 * a full extent here so just skip it.
5204 */
5205 ASSERT(bno >= del.br_blockcount);
5206 bno -= del.br_blockcount;
Christoph Hellwigf1c63b72011-05-11 15:04:09 +00005207 if (got.br_startoff > bno) {
5208 if (--lastx >= 0) {
5209 ep = xfs_iext_get_ext(ifp,
5210 lastx);
5211 xfs_bmbt_get_all(ep, &got);
5212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213 }
5214 continue;
5215 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5216 /*
5217 * This one is already unwritten.
5218 * It must have a written left neighbor.
5219 * Unwrite the killed part of that one and
5220 * try again.
5221 */
5222 ASSERT(lastx > 0);
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005223 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5224 lastx - 1), &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005226 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005227 ASSERT(del.br_startblock ==
5228 prev.br_startblock + prev.br_blockcount);
5229 if (prev.br_startoff < start) {
5230 mod = start - prev.br_startoff;
5231 prev.br_blockcount -= mod;
5232 prev.br_startblock += mod;
5233 prev.br_startoff = start;
5234 }
5235 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005236 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005237 error = xfs_bmap_add_extent_unwritten_real(tp,
5238 ip, &lastx, &cur, &prev,
5239 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240 if (error)
5241 goto error0;
5242 goto nodelete;
5243 } else {
5244 ASSERT(del.br_state == XFS_EXT_NORM);
5245 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005246 error = xfs_bmap_add_extent_unwritten_real(tp,
5247 ip, &lastx, &cur, &del,
5248 firstblock, flist, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 if (error)
5250 goto error0;
5251 goto nodelete;
5252 }
5253 }
5254 if (wasdel) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06005255 ASSERT(startblockval(del.br_startblock) > 0);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005256 /* Update realtime/data freespace, unreserve quota */
Nathan Scott06d10dd2005-06-21 15:48:47 +10005257 if (isrt) {
5258 xfs_filblks_t rtexts;
5259
5260 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5261 do_div(rtexts, mp->m_sb.sb_rextsize);
Dave Chinnerbab98bb2015-02-23 21:22:54 +11005262 xfs_mod_frextents(mp, (int64_t)rtexts);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005263 (void)xfs_trans_reserve_quota_nblks(NULL,
5264 ip, -((long)del.br_blockcount), 0,
Nathan Scott06d10dd2005-06-21 15:48:47 +10005265 XFS_QMOPT_RES_RTBLKS);
5266 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11005267 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount,
5268 false);
Christoph Hellwig7d095252009-06-08 15:33:32 +02005269 (void)xfs_trans_reserve_quota_nblks(NULL,
5270 ip, -((long)del.br_blockcount), 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005271 XFS_QMOPT_RES_REGBLKS);
Nathan Scott06d10dd2005-06-21 15:48:47 +10005272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273 ip->i_delayed_blks -= del.br_blockcount;
5274 if (cur)
5275 cur->bc_private.b.flags |=
5276 XFS_BTCUR_BPRV_WASDEL;
5277 } else if (cur)
5278 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5279 /*
5280 * If it's the case where the directory code is running
5281 * with no block reservation, and the deleted block is in
5282 * the middle of its extent, and the resulting insert
5283 * of an extent would cause transformation to btree format,
5284 * then reject it. The calling code will then swap
5285 * blocks around instead.
5286 * We have to do this now, rather than waiting for the
5287 * conversion to btree format, since the transaction
5288 * will be dirty.
5289 */
5290 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5291 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005292 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5293 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 del.br_startoff > got.br_startoff &&
5295 del.br_startoff + del.br_blockcount <
5296 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005297 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298 goto error0;
5299 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00005300 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
Christoph Hellwig54893272011-05-11 15:04:03 +00005301 &tmp_logflags, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 logflags |= tmp_logflags;
5303 if (error)
5304 goto error0;
5305 bno = del.br_startoff - 1;
5306nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307 /*
5308 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005311 if (lastx >= 0) {
5312 ep = xfs_iext_get_ext(ifp, lastx);
5313 if (xfs_bmbt_get_startoff(ep) > bno) {
5314 if (--lastx >= 0)
5315 ep = xfs_iext_get_ext(ifp,
5316 lastx);
5317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 xfs_bmbt_get_all(ep, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 extno++;
5321 }
5322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005324
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 /*
5326 * Convert to a btree if necessary.
5327 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005328 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 ASSERT(cur == NULL);
5330 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5331 &cur, 0, &tmp_logflags, whichfork);
5332 logflags |= tmp_logflags;
5333 if (error)
5334 goto error0;
5335 }
5336 /*
5337 * transform from btree to extents, give it cur
5338 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005339 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 ASSERT(cur != NULL);
5341 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5342 whichfork);
5343 logflags |= tmp_logflags;
5344 if (error)
5345 goto error0;
5346 }
5347 /*
5348 * transform from extents to local?
5349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 error = 0;
5351error0:
5352 /*
5353 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005354 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005356 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005357 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005358 logflags &= ~xfs_ilog_fext(whichfork);
5359 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005361 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 /*
5363 * Log inode even in the error case, if the transaction
5364 * is dirty we'll need to shut down the filesystem.
5365 */
5366 if (logflags)
5367 xfs_trans_log_inode(tp, ip, logflags);
5368 if (cur) {
5369 if (!error) {
5370 *firstblock = cur->bc_private.b.firstblock;
5371 cur->bc_private.b.allocated = 0;
5372 }
5373 xfs_btree_del_cursor(cur,
5374 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5375 }
5376 return error;
5377}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005378
5379/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005380 * Determine whether an extent shift can be accomplished by a merge with the
5381 * extent that precedes the target hole of the shift.
5382 */
5383STATIC bool
5384xfs_bmse_can_merge(
5385 struct xfs_bmbt_irec *left, /* preceding extent */
5386 struct xfs_bmbt_irec *got, /* current extent to shift */
5387 xfs_fileoff_t shift) /* shift fsb */
5388{
5389 xfs_fileoff_t startoff;
5390
5391 startoff = got->br_startoff - shift;
5392
5393 /*
5394 * The extent, once shifted, must be adjacent in-file and on-disk with
5395 * the preceding extent.
5396 */
5397 if ((left->br_startoff + left->br_blockcount != startoff) ||
5398 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5399 (left->br_state != got->br_state) ||
5400 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5401 return false;
5402
5403 return true;
5404}
5405
5406/*
5407 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5408 * hole in the file. If an extent shift would result in the extent being fully
5409 * adjacent to the extent that currently precedes the hole, we can merge with
5410 * the preceding extent rather than do the shift.
5411 *
5412 * This function assumes the caller has verified a shift-by-merge is possible
5413 * with the provided extents via xfs_bmse_can_merge().
5414 */
5415STATIC int
5416xfs_bmse_merge(
5417 struct xfs_inode *ip,
5418 int whichfork,
5419 xfs_fileoff_t shift, /* shift fsb */
5420 int current_ext, /* idx of gotp */
5421 struct xfs_bmbt_rec_host *gotp, /* extent to shift */
5422 struct xfs_bmbt_rec_host *leftp, /* preceding extent */
5423 struct xfs_btree_cur *cur,
5424 int *logflags) /* output */
5425{
Brian Fosterddb19e32014-09-23 15:38:09 +10005426 struct xfs_bmbt_irec got;
5427 struct xfs_bmbt_irec left;
5428 xfs_filblks_t blockcount;
5429 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005430 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005431
Brian Fosterddb19e32014-09-23 15:38:09 +10005432 xfs_bmbt_get_all(gotp, &got);
5433 xfs_bmbt_get_all(leftp, &left);
5434 blockcount = left.br_blockcount + got.br_blockcount;
5435
5436 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5437 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
5438 ASSERT(xfs_bmse_can_merge(&left, &got, shift));
5439
5440 /*
5441 * Merge the in-core extents. Note that the host record pointers and
5442 * current_ext index are invalid once the extent has been removed via
5443 * xfs_iext_remove().
5444 */
5445 xfs_bmbt_set_blockcount(leftp, blockcount);
5446 xfs_iext_remove(ip, current_ext, 1, 0);
5447
5448 /*
5449 * Update the on-disk extent count, the btree if necessary and log the
5450 * inode.
5451 */
5452 XFS_IFORK_NEXT_SET(ip, whichfork,
5453 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5454 *logflags |= XFS_ILOG_CORE;
5455 if (!cur) {
5456 *logflags |= XFS_ILOG_DEXT;
5457 return 0;
5458 }
5459
5460 /* lookup and remove the extent to merge */
5461 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5462 got.br_blockcount, &i);
5463 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005464 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005465 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005466
5467 error = xfs_btree_delete(cur, &i);
5468 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005469 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005470 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005471
5472 /* lookup and update size of the previous extent */
5473 error = xfs_bmbt_lookup_eq(cur, left.br_startoff, left.br_startblock,
5474 left.br_blockcount, &i);
5475 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005476 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005477 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005478
5479 left.br_blockcount = blockcount;
5480
Dave Chinner4db431f2014-12-04 09:42:40 +11005481 return xfs_bmbt_update(cur, left.br_startoff, left.br_startblock,
5482 left.br_blockcount, left.br_state);
Brian Fosterddb19e32014-09-23 15:38:09 +10005483}
5484
5485/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005486 * Shift a single extent.
5487 */
5488STATIC int
5489xfs_bmse_shift_one(
5490 struct xfs_inode *ip,
5491 int whichfork,
5492 xfs_fileoff_t offset_shift_fsb,
5493 int *current_ext,
5494 struct xfs_bmbt_rec_host *gotp,
5495 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005496 int *logflags,
5497 enum shift_direction direction)
Brian Fostera979bdf2014-09-23 15:39:04 +10005498{
5499 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005500 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005501 xfs_fileoff_t startoff;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005502 struct xfs_bmbt_rec_host *adj_irecp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005503 struct xfs_bmbt_irec got;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005504 struct xfs_bmbt_irec adj_irec;
Brian Fostera979bdf2014-09-23 15:39:04 +10005505 int error;
5506 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005507 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005508
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005509 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005510 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005511 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Brian Fostera979bdf2014-09-23 15:39:04 +10005512
5513 xfs_bmbt_get_all(gotp, &got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005514
Brian Fosterf71721d2014-09-23 15:39:05 +10005515 /* delalloc extents should be prevented by caller */
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005516 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005517
Namjae Jeona904b1c2015-03-25 15:08:56 +11005518 if (direction == SHIFT_LEFT) {
5519 startoff = got.br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005520
Namjae Jeona904b1c2015-03-25 15:08:56 +11005521 /*
5522 * Check for merge if we've got an extent to the left,
5523 * otherwise make sure there's enough room at the start
5524 * of the file for the shift.
5525 */
5526 if (!*current_ext) {
5527 if (got.br_startoff < offset_shift_fsb)
5528 return -EINVAL;
5529 goto update_current_ext;
5530 }
5531 /*
5532 * grab the left extent and check for a large
5533 * enough hole.
5534 */
5535 adj_irecp = xfs_iext_get_ext(ifp, *current_ext - 1);
5536 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5537
5538 if (startoff <
5539 adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005540 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005541
Dave Chinnerb11bd672014-12-04 09:42:24 +11005542 /* check whether to merge the extent or shift it down */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005543 if (xfs_bmse_can_merge(&adj_irec, &got,
5544 offset_shift_fsb)) {
Dave Chinnerb11bd672014-12-04 09:42:24 +11005545 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005546 *current_ext, gotp, adj_irecp,
5547 cur, logflags);
Dave Chinnerb11bd672014-12-04 09:42:24 +11005548 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005549 } else {
5550 startoff = got.br_startoff + offset_shift_fsb;
5551 /* nothing to move if this is the last extent */
5552 if (*current_ext >= (total_extents - 1))
5553 goto update_current_ext;
5554 /*
5555 * If this is not the last extent in the file, make sure there
5556 * is enough room between current extent and next extent for
5557 * accommodating the shift.
5558 */
5559 adj_irecp = xfs_iext_get_ext(ifp, *current_ext + 1);
5560 xfs_bmbt_get_all(adj_irecp, &adj_irec);
5561 if (startoff + got.br_blockcount > adj_irec.br_startoff)
5562 return -EINVAL;
5563 /*
5564 * Unlike a left shift (which involves a hole punch),
5565 * a right shift does not modify extent neighbors
5566 * in any way. We should never find mergeable extents
5567 * in this scenario. Check anyways and warn if we
5568 * encounter two extents that could be one.
5569 */
5570 if (xfs_bmse_can_merge(&got, &adj_irec, offset_shift_fsb))
5571 WARN_ON_ONCE(1);
5572 }
Brian Fostera979bdf2014-09-23 15:39:04 +10005573 /*
5574 * Increment the extent index for the next iteration, update the start
5575 * offset of the in-core extent and update the btree if applicable.
5576 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005577update_current_ext:
5578 if (direction == SHIFT_LEFT)
5579 (*current_ext)++;
5580 else
5581 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005582 xfs_bmbt_set_startoff(gotp, startoff);
5583 *logflags |= XFS_ILOG_CORE;
5584 if (!cur) {
5585 *logflags |= XFS_ILOG_DEXT;
5586 return 0;
5587 }
5588
5589 error = xfs_bmbt_lookup_eq(cur, got.br_startoff, got.br_startblock,
5590 got.br_blockcount, &i);
5591 if (error)
5592 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005593 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fostera979bdf2014-09-23 15:39:04 +10005594
5595 got.br_startoff = startoff;
kbuild test robotd254aae2014-12-01 08:42:52 +11005596 return xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005597 got.br_blockcount, got.br_state);
Brian Fostera979bdf2014-09-23 15:39:04 +10005598}
5599
5600/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005601 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005602 *
Brian Foster2c845f52014-09-23 15:37:09 +10005603 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005604 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10005605 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5606 * is the length by which each extent is shifted. If there is no hole to shift
5607 * the extents into, this will be considered invalid operation and we abort
5608 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005609 */
5610int
5611xfs_bmap_shift_extents(
5612 struct xfs_trans *tp,
5613 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005614 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005615 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005616 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005617 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005618 xfs_fsblock_t *firstblock,
5619 struct xfs_bmap_free *flist,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005620 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005621 int num_exts)
5622{
Brian Fosterca446d82014-09-02 12:12:53 +10005623 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005624 struct xfs_bmbt_rec_host *gotp;
5625 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005626 struct xfs_mount *mp = ip->i_mount;
5627 struct xfs_ifork *ifp;
5628 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005629 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005630 xfs_extnum_t total_extents;
5631 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005632 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005633 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10005634 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005635
5636 if (unlikely(XFS_TEST_ERROR(
5637 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5638 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5639 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5640 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5641 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005642 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005643 }
5644
5645 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005646 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005647
Brian Foster2c845f52014-09-23 15:37:09 +10005648 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5649 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005650 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
5651 ASSERT(*next_fsb != NULLFSBLOCK || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005652
5653 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005654 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5655 /* Read in all the extents */
5656 error = xfs_iread_extents(tp, ip, whichfork);
5657 if (error)
5658 return error;
5659 }
5660
Brian Fosterddb19e32014-09-23 15:38:09 +10005661 if (ifp->if_flags & XFS_IFBROOT) {
5662 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5663 cur->bc_private.b.firstblock = *firstblock;
5664 cur->bc_private.b.flist = flist;
5665 cur->bc_private.b.flags = 0;
5666 }
5667
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005668 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005669 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10005670 * are collapsing out, so we cannot use the count of real extents here.
5671 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005672 */
5673 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005674 if (total_extents == 0) {
5675 *done = 1;
5676 goto del_cursor;
5677 }
5678
5679 /*
5680 * In case of first right shift, we need to initialize next_fsb
5681 */
5682 if (*next_fsb == NULLFSBLOCK) {
5683 gotp = xfs_iext_get_ext(ifp, total_extents - 1);
5684 xfs_bmbt_get_all(gotp, &got);
5685 *next_fsb = got.br_startoff;
5686 if (stop_fsb > *next_fsb) {
5687 *done = 1;
5688 goto del_cursor;
5689 }
5690 }
5691
5692 /* Lookup the extent index at which we have to stop */
5693 if (direction == SHIFT_RIGHT) {
5694 gotp = xfs_iext_bno_to_ext(ifp, stop_fsb, &stop_extent);
5695 /* Make stop_extent exclusive of shift range */
5696 stop_extent--;
5697 } else
5698 stop_extent = total_extents;
5699
5700 /*
5701 * Look up the extent index for the fsb where we start shifting. We can
5702 * henceforth iterate with current_ext as extent list changes are locked
5703 * out via ilock.
5704 *
5705 * gotp can be null in 2 cases: 1) if there are no extents or 2)
5706 * *next_fsb lies in a hole beyond which there are no extents. Either
5707 * way, we are done.
5708 */
5709 gotp = xfs_iext_bno_to_ext(ifp, *next_fsb, &current_ext);
5710 if (!gotp) {
5711 *done = 1;
5712 goto del_cursor;
5713 }
5714
5715 /* some sanity checking before we finally start shifting extents */
5716 if ((direction == SHIFT_LEFT && current_ext >= stop_extent) ||
5717 (direction == SHIFT_RIGHT && current_ext <= stop_extent)) {
5718 error = -EIO;
5719 goto del_cursor;
5720 }
5721
5722 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005723 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005724 &current_ext, gotp, cur, &logflags,
5725 direction);
Brian Fostera979bdf2014-09-23 15:39:04 +10005726 if (error)
5727 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005728 /*
5729 * If there was an extent merge during the shift, the extent
5730 * count can change. Update the total and grade the next record.
5731 */
5732 if (direction == SHIFT_LEFT) {
5733 total_extents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
5734 stop_extent = total_extents;
5735 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005736
Namjae Jeona904b1c2015-03-25 15:08:56 +11005737 if (current_ext == stop_extent) {
5738 *done = 1;
5739 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10005740 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005741 }
Brian Fosterddb19e32014-09-23 15:38:09 +10005742 gotp = xfs_iext_get_ext(ifp, current_ext);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005743 }
5744
Namjae Jeona904b1c2015-03-25 15:08:56 +11005745 if (!*done) {
Brian Fostera979bdf2014-09-23 15:39:04 +10005746 xfs_bmbt_get_all(gotp, &got);
Brian Foster2c845f52014-09-23 15:37:09 +10005747 *next_fsb = got.br_startoff;
Brian Fostera979bdf2014-09-23 15:39:04 +10005748 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005749
5750del_cursor:
5751 if (cur)
5752 xfs_btree_del_cursor(cur,
5753 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5754
Brian Fosterca446d82014-09-02 12:12:53 +10005755 if (logflags)
5756 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10005757
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005758 return error;
5759}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005760
5761/*
5762 * Splits an extent into two extents at split_fsb block such that it is
5763 * the first block of the current_ext. @current_ext is a target extent
5764 * to be split. @split_fsb is a block where the extents is split.
5765 * If split_fsb lies in a hole or the first block of extents, just return 0.
5766 */
5767STATIC int
5768xfs_bmap_split_extent_at(
5769 struct xfs_trans *tp,
5770 struct xfs_inode *ip,
5771 xfs_fileoff_t split_fsb,
5772 xfs_fsblock_t *firstfsb,
5773 struct xfs_bmap_free *free_list)
5774{
5775 int whichfork = XFS_DATA_FORK;
5776 struct xfs_btree_cur *cur = NULL;
5777 struct xfs_bmbt_rec_host *gotp;
5778 struct xfs_bmbt_irec got;
5779 struct xfs_bmbt_irec new; /* split extent */
5780 struct xfs_mount *mp = ip->i_mount;
5781 struct xfs_ifork *ifp;
5782 xfs_fsblock_t gotblkcnt; /* new block count for got */
5783 xfs_extnum_t current_ext;
5784 int error = 0;
5785 int logflags = 0;
5786 int i = 0;
5787
5788 if (unlikely(XFS_TEST_ERROR(
5789 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5790 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5791 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
5792 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5793 XFS_ERRLEVEL_LOW, mp);
5794 return -EFSCORRUPTED;
5795 }
5796
5797 if (XFS_FORCED_SHUTDOWN(mp))
5798 return -EIO;
5799
5800 ifp = XFS_IFORK_PTR(ip, whichfork);
5801 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5802 /* Read in all the extents */
5803 error = xfs_iread_extents(tp, ip, whichfork);
5804 if (error)
5805 return error;
5806 }
5807
5808 /*
5809 * gotp can be null in 2 cases: 1) if there are no extents
5810 * or 2) split_fsb lies in a hole beyond which there are
5811 * no extents. Either way, we are done.
5812 */
5813 gotp = xfs_iext_bno_to_ext(ifp, split_fsb, &current_ext);
5814 if (!gotp)
5815 return 0;
5816
5817 xfs_bmbt_get_all(gotp, &got);
5818
5819 /*
5820 * Check split_fsb lies in a hole or the start boundary offset
5821 * of the extent.
5822 */
5823 if (got.br_startoff >= split_fsb)
5824 return 0;
5825
5826 gotblkcnt = split_fsb - got.br_startoff;
5827 new.br_startoff = split_fsb;
5828 new.br_startblock = got.br_startblock + gotblkcnt;
5829 new.br_blockcount = got.br_blockcount - gotblkcnt;
5830 new.br_state = got.br_state;
5831
5832 if (ifp->if_flags & XFS_IFBROOT) {
5833 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5834 cur->bc_private.b.firstblock = *firstfsb;
5835 cur->bc_private.b.flist = free_list;
5836 cur->bc_private.b.flags = 0;
5837 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5838 got.br_startblock,
5839 got.br_blockcount,
5840 &i);
5841 if (error)
5842 goto del_cursor;
5843 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5844 }
5845
5846 xfs_bmbt_set_blockcount(gotp, gotblkcnt);
5847 got.br_blockcount = gotblkcnt;
5848
5849 logflags = XFS_ILOG_CORE;
5850 if (cur) {
5851 error = xfs_bmbt_update(cur, got.br_startoff,
5852 got.br_startblock,
5853 got.br_blockcount,
5854 got.br_state);
5855 if (error)
5856 goto del_cursor;
5857 } else
5858 logflags |= XFS_ILOG_DEXT;
5859
5860 /* Add new extent */
5861 current_ext++;
5862 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5863 XFS_IFORK_NEXT_SET(ip, whichfork,
5864 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5865
5866 if (cur) {
5867 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
5868 new.br_startblock, new.br_blockcount,
5869 &i);
5870 if (error)
5871 goto del_cursor;
5872 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
5873 cur->bc_rec.b.br_state = new.br_state;
5874
5875 error = xfs_btree_insert(cur, &i);
5876 if (error)
5877 goto del_cursor;
5878 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5879 }
5880
5881 /*
5882 * Convert to a btree if necessary.
5883 */
5884 if (xfs_bmap_needs_btree(ip, whichfork)) {
5885 int tmp_logflags; /* partial log flag return val */
5886
5887 ASSERT(cur == NULL);
5888 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, free_list,
5889 &cur, 0, &tmp_logflags, whichfork);
5890 logflags |= tmp_logflags;
5891 }
5892
5893del_cursor:
5894 if (cur) {
5895 cur->bc_private.b.allocated = 0;
5896 xfs_btree_del_cursor(cur,
5897 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5898 }
5899
5900 if (logflags)
5901 xfs_trans_log_inode(tp, ip, logflags);
5902 return error;
5903}
5904
5905int
5906xfs_bmap_split_extent(
5907 struct xfs_inode *ip,
5908 xfs_fileoff_t split_fsb)
5909{
5910 struct xfs_mount *mp = ip->i_mount;
5911 struct xfs_trans *tp;
5912 struct xfs_bmap_free free_list;
5913 xfs_fsblock_t firstfsb;
5914 int committed;
5915 int error;
5916
5917 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
5918 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
5919 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0);
5920 if (error) {
5921 xfs_trans_cancel(tp, 0);
5922 return error;
5923 }
5924
5925 xfs_ilock(ip, XFS_ILOCK_EXCL);
5926 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
5927
5928 xfs_bmap_init(&free_list, &firstfsb);
5929
5930 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
5931 &firstfsb, &free_list);
5932 if (error)
5933 goto out;
5934
5935 error = xfs_bmap_finish(&tp, &free_list, &committed);
5936 if (error)
5937 goto out;
5938
5939 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
5940
5941
5942out:
5943 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
5944 return error;
5945}