blob: a3cc8afed367c7e18d67439e190b35685a79b5f8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100030#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_extfree_item.h"
36#include "xfs_alloc.h"
37#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100038#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110039#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_rtalloc.h"
41#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_quota.h"
43#include "xfs_trans_space.h"
44#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110046#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110047#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110048#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100049#include "xfs_rmap.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100050#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070051#include "xfs_refcount.h"
Brian Foster974ae922016-11-28 14:57:42 +110052#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055kmem_zone_t *xfs_bmap_free_item_zone;
56
57/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110058 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110062 * Compute and fill in the value of the maximum depth of a bmap btree
63 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110065void
66xfs_bmap_compute_maxlevels(
67 xfs_mount_t *mp, /* file system mount structure */
68 int whichfork) /* data or attr fork */
69{
70 int level; /* btree level */
71 uint maxblocks; /* max blocks at this level */
72 uint maxleafents; /* max leaf entries possible */
73 int maxrootrecs; /* max records in root block */
74 int minleafrecs; /* min records in leaf block */
75 int minnoderecs; /* min records in node block */
76 int sz; /* root block size */
77
78 /*
79 * The maximum number of extents in a file, hence the maximum
80 * number of leaf entries, is controlled by the type of di_nextents
81 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
82 * (a signed 16-bit number, xfs_aextnum_t).
83 *
84 * Note that we can no longer assume that if we are in ATTR1 that
85 * the fork offset of all the inodes will be
86 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
87 * with ATTR2 and then mounted back with ATTR1, keeping the
88 * di_forkoff's fixed but probably at various positions. Therefore,
89 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
90 * of a minimum size available.
91 */
92 if (whichfork == XFS_DATA_FORK) {
93 maxleafents = MAXEXTNUM;
94 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
95 } else {
96 maxleafents = MAXAEXTNUM;
97 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
98 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100099 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100100 minleafrecs = mp->m_bmap_dmnr[0];
101 minnoderecs = mp->m_bmap_dmnr[1];
102 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
103 for (level = 1; maxblocks > 1; level++) {
104 if (maxblocks <= maxrootrecs)
105 maxblocks = 1;
106 else
107 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
108 }
109 mp->m_bm_maxlevels[whichfork] = level;
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100112STATIC int /* error */
113xfs_bmbt_lookup_eq(
114 struct xfs_btree_cur *cur,
115 xfs_fileoff_t off,
116 xfs_fsblock_t bno,
117 xfs_filblks_t len,
118 int *stat) /* success/failure */
119{
120 cur->bc_rec.b.br_startoff = off;
121 cur->bc_rec.b.br_startblock = bno;
122 cur->bc_rec.b.br_blockcount = len;
123 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
124}
125
126STATIC int /* error */
127xfs_bmbt_lookup_ge(
128 struct xfs_btree_cur *cur,
129 xfs_fileoff_t off,
130 xfs_fsblock_t bno,
131 xfs_filblks_t len,
132 int *stat) /* success/failure */
133{
134 cur->bc_rec.b.br_startoff = off;
135 cur->bc_rec.b.br_startblock = bno;
136 cur->bc_rec.b.br_blockcount = len;
137 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
138}
139
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100140/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000141 * Check if the inode needs to be converted to btree format.
142 */
143static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
144{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700145 return whichfork != XFS_COW_FORK &&
146 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000147 XFS_IFORK_NEXTENTS(ip, whichfork) >
148 XFS_IFORK_MAXEXT(ip, whichfork);
149}
150
151/*
152 * Check if the inode should be converted to extent format.
153 */
154static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
155{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700156 return whichfork != XFS_COW_FORK &&
157 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000158 XFS_IFORK_NEXTENTS(ip, whichfork) <=
159 XFS_IFORK_MAXEXT(ip, whichfork);
160}
161
162/*
163 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100164 * by [off, bno, len, state].
165 * This either works (return 0) or gets an EFSCORRUPTED error.
166 */
167STATIC int
168xfs_bmbt_update(
169 struct xfs_btree_cur *cur,
170 xfs_fileoff_t off,
171 xfs_fsblock_t bno,
172 xfs_filblks_t len,
173 xfs_exntst_t state)
174{
175 union xfs_btree_rec rec;
176
177 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
178 return xfs_btree_update(cur, &rec);
179}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100182 * Compute the worst-case number of indirect blocks that will be used
183 * for ip's delayed extent of length "len".
184 */
185STATIC xfs_filblks_t
186xfs_bmap_worst_indlen(
187 xfs_inode_t *ip, /* incore inode pointer */
188 xfs_filblks_t len) /* delayed extent length */
189{
190 int level; /* btree level number */
191 int maxrecs; /* maximum record count at this level */
192 xfs_mount_t *mp; /* mount structure */
193 xfs_filblks_t rval; /* return value */
194
195 mp = ip->i_mount;
196 maxrecs = mp->m_bmap_dmxr[0];
197 for (level = 0, rval = 0;
198 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
199 level++) {
200 len += maxrecs - 1;
201 do_div(len, maxrecs);
202 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700203 if (len == 1)
204 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100205 level - 1;
206 if (level == 0)
207 maxrecs = mp->m_bmap_dmxr[1];
208 }
209 return rval;
210}
211
212/*
213 * Calculate the default attribute fork offset for newly created inodes.
214 */
215uint
216xfs_default_attroffset(
217 struct xfs_inode *ip)
218{
219 struct xfs_mount *mp = ip->i_mount;
220 uint offset;
221
222 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100223 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100224 XFS_BMDR_SPACE_CALC(MINABTPTRS);
225 } else {
226 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
227 }
228
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100229 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100230 return offset;
231}
232
233/*
234 * Helper routine to reset inode di_forkoff field when switching
235 * attribute fork from local to extent format - we reset it where
236 * possible to make space available for inline data fork extents.
237 */
238STATIC void
239xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100240 xfs_inode_t *ip,
241 int whichfork)
242{
243 if (whichfork == XFS_ATTR_FORK &&
244 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
245 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
246 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
247 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
248
249 if (dfl_forkoff > ip->i_d.di_forkoff)
250 ip->i_d.di_forkoff = dfl_forkoff;
251 }
252}
253
Dave Chinner9e5987a2013-02-25 12:31:26 +1100254#ifdef DEBUG
255STATIC struct xfs_buf *
256xfs_bmap_get_bp(
257 struct xfs_btree_cur *cur,
258 xfs_fsblock_t bno)
259{
260 struct xfs_log_item_desc *lidp;
261 int i;
262
263 if (!cur)
264 return NULL;
265
266 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
267 if (!cur->bc_bufs[i])
268 break;
269 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
270 return cur->bc_bufs[i];
271 }
272
273 /* Chase down all the log items to see if the bp is there */
274 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
275 struct xfs_buf_log_item *bip;
276 bip = (struct xfs_buf_log_item *)lidp->lid_item;
277 if (bip->bli_item.li_type == XFS_LI_BUF &&
278 XFS_BUF_ADDR(bip->bli_buf) == bno)
279 return bip->bli_buf;
280 }
281
282 return NULL;
283}
284
285STATIC void
286xfs_check_block(
287 struct xfs_btree_block *block,
288 xfs_mount_t *mp,
289 int root,
290 short sz)
291{
292 int i, j, dmxr;
293 __be64 *pp, *thispa; /* pointer to block address */
294 xfs_bmbt_key_t *prevp, *keyp;
295
296 ASSERT(be16_to_cpu(block->bb_level) > 0);
297
298 prevp = NULL;
299 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
300 dmxr = mp->m_bmap_dmxr[0];
301 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
302
303 if (prevp) {
304 ASSERT(be64_to_cpu(prevp->br_startoff) <
305 be64_to_cpu(keyp->br_startoff));
306 }
307 prevp = keyp;
308
309 /*
310 * Compare the block numbers to see if there are dups.
311 */
312 if (root)
313 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
314 else
315 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
316
317 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
318 if (root)
319 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
320 else
321 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
322 if (*thispa == *pp) {
323 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
324 __func__, j, i,
325 (unsigned long long)be64_to_cpu(*thispa));
326 panic("%s: ptrs are equal in node\n",
327 __func__);
328 }
329 }
330 }
331}
332
333/*
334 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100335 * btree leaves. THis becomes prohibitively expensive for large extent count
336 * files, so don't bother with inodes that have more than 10,000 extents in
337 * them. The btree record ordering checks will still be done, so for such large
338 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100339 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100340STATIC void
341xfs_bmap_check_leaf_extents(
342 xfs_btree_cur_t *cur, /* btree cursor or null */
343 xfs_inode_t *ip, /* incore inode pointer */
344 int whichfork) /* data or attr fork */
345{
346 struct xfs_btree_block *block; /* current btree block */
347 xfs_fsblock_t bno; /* block # of "block" */
348 xfs_buf_t *bp; /* buffer for "block" */
349 int error; /* error return value */
350 xfs_extnum_t i=0, j; /* index into the extents list */
351 xfs_ifork_t *ifp; /* fork structure */
352 int level; /* btree level, for checking */
353 xfs_mount_t *mp; /* file system mount structure */
354 __be64 *pp; /* pointer to block address */
355 xfs_bmbt_rec_t *ep; /* pointer to current extent */
356 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
357 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
358 int bp_release = 0;
359
360 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
361 return;
362 }
363
Dave Chinnere3543812016-01-08 11:28:49 +1100364 /* skip large extent count inodes */
365 if (ip->i_d.di_nextents > 10000)
366 return;
367
Dave Chinner9e5987a2013-02-25 12:31:26 +1100368 bno = NULLFSBLOCK;
369 mp = ip->i_mount;
370 ifp = XFS_IFORK_PTR(ip, whichfork);
371 block = ifp->if_broot;
372 /*
373 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
374 */
375 level = be16_to_cpu(block->bb_level);
376 ASSERT(level > 0);
377 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
378 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
379 bno = be64_to_cpu(*pp);
380
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000381 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100382 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
383 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
384
385 /*
386 * Go down the tree until leaf level is reached, following the first
387 * pointer (leftmost) at each level.
388 */
389 while (level-- > 0) {
390 /* See if buf is in cur first */
391 bp_release = 0;
392 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
393 if (!bp) {
394 bp_release = 1;
395 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
396 XFS_BMAP_BTREE_REF,
397 &xfs_bmbt_buf_ops);
398 if (error)
399 goto error_norelse;
400 }
401 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100402 if (level == 0)
403 break;
404
405 /*
406 * Check this block for basic sanity (increasing keys and
407 * no duplicate blocks).
408 */
409
410 xfs_check_block(block, mp, 0, 0);
411 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
412 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100413 XFS_WANT_CORRUPTED_GOTO(mp,
414 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100415 if (bp_release) {
416 bp_release = 0;
417 xfs_trans_brelse(NULL, bp);
418 }
419 }
420
421 /*
422 * Here with bp and block set to the leftmost leaf node in the tree.
423 */
424 i = 0;
425
426 /*
427 * Loop over all leaf nodes checking that all extents are in the right order.
428 */
429 for (;;) {
430 xfs_fsblock_t nextbno;
431 xfs_extnum_t num_recs;
432
433
434 num_recs = xfs_btree_get_numrecs(block);
435
436 /*
437 * Read-ahead the next leaf block, if any.
438 */
439
440 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
441
442 /*
443 * Check all the extents to make sure they are OK.
444 * If we had a previous block, the last entry should
445 * conform with the first entry in this one.
446 */
447
448 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
449 if (i) {
450 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
451 xfs_bmbt_disk_get_blockcount(&last) <=
452 xfs_bmbt_disk_get_startoff(ep));
453 }
454 for (j = 1; j < num_recs; j++) {
455 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
456 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
457 xfs_bmbt_disk_get_blockcount(ep) <=
458 xfs_bmbt_disk_get_startoff(nextp));
459 ep = nextp;
460 }
461
462 last = *ep;
463 i += num_recs;
464 if (bp_release) {
465 bp_release = 0;
466 xfs_trans_brelse(NULL, bp);
467 }
468 bno = nextbno;
469 /*
470 * If we've reached the end, stop.
471 */
472 if (bno == NULLFSBLOCK)
473 break;
474
475 bp_release = 0;
476 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
477 if (!bp) {
478 bp_release = 1;
479 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
480 XFS_BMAP_BTREE_REF,
481 &xfs_bmbt_buf_ops);
482 if (error)
483 goto error_norelse;
484 }
485 block = XFS_BUF_TO_BLOCK(bp);
486 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100487
Dave Chinner9e5987a2013-02-25 12:31:26 +1100488 return;
489
490error0:
491 xfs_warn(mp, "%s: at error0", __func__);
492 if (bp_release)
493 xfs_trans_brelse(NULL, bp);
494error_norelse:
495 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
496 __func__, i);
497 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
498 return;
499}
500
501/*
502 * Add bmap trace insert entries for all the contents of the extent records.
503 */
504void
505xfs_bmap_trace_exlist(
506 xfs_inode_t *ip, /* incore inode pointer */
507 xfs_extnum_t cnt, /* count of entries in the list */
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100508 int whichfork, /* data or attr or cow fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100509 unsigned long caller_ip)
510{
511 xfs_extnum_t idx; /* extent record index */
512 xfs_ifork_t *ifp; /* inode fork pointer */
513 int state = 0;
514
515 if (whichfork == XFS_ATTR_FORK)
516 state |= BMAP_ATTRFORK;
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100517 else if (whichfork == XFS_COW_FORK)
518 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100519
520 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +1100521 ASSERT(cnt == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100522 for (idx = 0; idx < cnt; idx++)
Eric Sandeen77105172016-12-05 12:31:50 +1100523 trace_xfs_extlist(ip, idx, state, caller_ip);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100524}
525
526/*
527 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000528 * given the caller's original parameters. Specifically check the
529 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100530 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
531 */
532STATIC void
533xfs_bmap_validate_ret(
534 xfs_fileoff_t bno,
535 xfs_filblks_t len,
536 int flags,
537 xfs_bmbt_irec_t *mval,
538 int nmap,
539 int ret_nmap)
540{
541 int i; /* index to map values */
542
543 ASSERT(ret_nmap <= nmap);
544
545 for (i = 0; i < ret_nmap; i++) {
546 ASSERT(mval[i].br_blockcount > 0);
547 if (!(flags & XFS_BMAPI_ENTIRE)) {
548 ASSERT(mval[i].br_startoff >= bno);
549 ASSERT(mval[i].br_blockcount <= len);
550 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
551 bno + len);
552 } else {
553 ASSERT(mval[i].br_startoff < bno + len);
554 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
555 bno);
556 }
557 ASSERT(i == 0 ||
558 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
559 mval[i].br_startoff);
560 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
561 mval[i].br_startblock != HOLESTARTBLOCK);
562 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
563 mval[i].br_state == XFS_EXT_UNWRITTEN);
564 }
565}
566
567#else
568#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700569#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100570#endif /* DEBUG */
571
572/*
573 * bmap free list manipulation functions
574 */
575
576/*
577 * Add the extent to the list of extents to be free at transaction end.
578 * The list is maintained sorted (by block number).
579 */
580void
581xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000582 struct xfs_mount *mp,
583 struct xfs_defer_ops *dfops,
584 xfs_fsblock_t bno,
585 xfs_filblks_t len,
586 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100587{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000588 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100589#ifdef DEBUG
590 xfs_agnumber_t agno;
591 xfs_agblock_t agbno;
592
593 ASSERT(bno != NULLFSBLOCK);
594 ASSERT(len > 0);
595 ASSERT(len <= MAXEXTLEN);
596 ASSERT(!isnullstartblock(bno));
597 agno = XFS_FSB_TO_AGNO(mp, bno);
598 agbno = XFS_FSB_TO_AGBNO(mp, bno);
599 ASSERT(agno < mp->m_sb.sb_agcount);
600 ASSERT(agbno < mp->m_sb.sb_agblocks);
601 ASSERT(len < mp->m_sb.sb_agblocks);
602 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
603#endif
604 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000605
Dave Chinner9e5987a2013-02-25 12:31:26 +1100606 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000607 new->xefi_startblock = bno;
608 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000609 if (oinfo)
610 new->xefi_oinfo = *oinfo;
611 else
612 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000613 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
614 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000615 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100616}
617
618/*
619 * Inode fork format manipulation functions
620 */
621
622/*
623 * Transform a btree format file with only one leaf node, where the
624 * extents list will fit in the inode, into an extents format file.
625 * Since the file extents are already in-core, all we have to do is
626 * give up the space for the btree root and pitch the leaf block.
627 */
628STATIC int /* error */
629xfs_bmap_btree_to_extents(
630 xfs_trans_t *tp, /* transaction pointer */
631 xfs_inode_t *ip, /* incore inode pointer */
632 xfs_btree_cur_t *cur, /* btree cursor */
633 int *logflagsp, /* inode logging flags */
634 int whichfork) /* data or attr fork */
635{
636 /* REFERENCED */
637 struct xfs_btree_block *cblock;/* child btree block */
638 xfs_fsblock_t cbno; /* child block number */
639 xfs_buf_t *cbp; /* child block's buffer */
640 int error; /* error return value */
641 xfs_ifork_t *ifp; /* inode fork data */
642 xfs_mount_t *mp; /* mount point structure */
643 __be64 *pp; /* ptr to block address */
644 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000645 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100646
647 mp = ip->i_mount;
648 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700649 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100650 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
651 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
652 rblock = ifp->if_broot;
653 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
654 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
655 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
656 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
657 cbno = be64_to_cpu(*pp);
658 *logflagsp = 0;
659#ifdef DEBUG
660 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
661 return error;
662#endif
663 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
664 &xfs_bmbt_buf_ops);
665 if (error)
666 return error;
667 cblock = XFS_BUF_TO_BLOCK(cbp);
668 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
669 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000670 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
671 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100672 ip->i_d.di_nblocks--;
673 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
674 xfs_trans_binval(tp, cbp);
675 if (cur->bc_bufs[0] == cbp)
676 cur->bc_bufs[0] = NULL;
677 xfs_iroot_realloc(ip, -1, whichfork);
678 ASSERT(ifp->if_broot == NULL);
679 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
680 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
681 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
682 return 0;
683}
684
685/*
686 * Convert an extents-format file into a btree-format file.
687 * The new file will have a root block (in the inode) and a single child block.
688 */
689STATIC int /* error */
690xfs_bmap_extents_to_btree(
691 xfs_trans_t *tp, /* transaction pointer */
692 xfs_inode_t *ip, /* incore inode pointer */
693 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000694 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100695 xfs_btree_cur_t **curp, /* cursor returned to caller */
696 int wasdel, /* converting a delayed alloc */
697 int *logflagsp, /* inode logging flags */
698 int whichfork) /* data or attr fork */
699{
700 struct xfs_btree_block *ablock; /* allocated (child) bt block */
701 xfs_buf_t *abp; /* buffer for ablock */
702 xfs_alloc_arg_t args; /* allocation arguments */
703 xfs_bmbt_rec_t *arp; /* child record pointer */
704 struct xfs_btree_block *block; /* btree root block */
705 xfs_btree_cur_t *cur; /* bmap btree cursor */
706 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
707 int error; /* error return value */
708 xfs_extnum_t i, cnt; /* extent record index */
709 xfs_ifork_t *ifp; /* inode fork pointer */
710 xfs_bmbt_key_t *kp; /* root block key pointer */
711 xfs_mount_t *mp; /* mount structure */
712 xfs_extnum_t nextents; /* number of file extents */
713 xfs_bmbt_ptr_t *pp; /* root block address pointer */
714
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500715 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700716 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100717 ifp = XFS_IFORK_PTR(ip, whichfork);
718 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
719
720 /*
721 * Make space in the inode incore.
722 */
723 xfs_iroot_realloc(ip, 1, whichfork);
724 ifp->if_flags |= XFS_IFBROOT;
725
726 /*
727 * Fill in the root.
728 */
729 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800730 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
731 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800732 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100733 /*
734 * Need a cursor. Can't allocate until bb_level is filled in.
735 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100736 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
737 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000738 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100739 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
740 /*
741 * Convert to a btree with two levels, one record in root.
742 */
743 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
744 memset(&args, 0, sizeof(args));
745 args.tp = tp;
746 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000747 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100748 args.firstblock = *firstblock;
749 if (*firstblock == NULLFSBLOCK) {
750 args.type = XFS_ALLOCTYPE_START_BNO;
751 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000752 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100753 args.type = XFS_ALLOCTYPE_START_BNO;
754 args.fsbno = *firstblock;
755 } else {
756 args.type = XFS_ALLOCTYPE_NEAR_BNO;
757 args.fsbno = *firstblock;
758 }
759 args.minlen = args.maxlen = args.prod = 1;
760 args.wasdel = wasdel;
761 *logflagsp = 0;
762 if ((error = xfs_alloc_vextent(&args))) {
763 xfs_iroot_realloc(ip, -1, whichfork);
764 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
765 return error;
766 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700767
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800768 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
769 xfs_iroot_realloc(ip, -1, whichfork);
770 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
771 return -ENOSPC;
772 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100773 /*
774 * Allocation can't fail, the space was reserved.
775 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100776 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800777 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100778 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
779 cur->bc_private.b.allocated++;
780 ip->i_d.di_nblocks++;
781 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
782 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
783 /*
784 * Fill in the child block.
785 */
786 abp->b_ops = &xfs_bmbt_buf_ops;
787 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800788 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
789 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500790 XFS_BTREE_LONG_PTRS);
791
Dave Chinner9e5987a2013-02-25 12:31:26 +1100792 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100793 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100794 for (cnt = i = 0; i < nextents; i++) {
795 ep = xfs_iext_get_ext(ifp, i);
796 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
797 arp->l0 = cpu_to_be64(ep->l0);
798 arp->l1 = cpu_to_be64(ep->l1);
799 arp++; cnt++;
800 }
801 }
802 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
803 xfs_btree_set_numrecs(ablock, cnt);
804
805 /*
806 * Fill in the root key and pointer.
807 */
808 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
809 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
810 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
811 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
812 be16_to_cpu(block->bb_level)));
813 *pp = cpu_to_be64(args.fsbno);
814
815 /*
816 * Do all this logging at the end so that
817 * the root is at the right level.
818 */
819 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
820 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
821 ASSERT(*curp == NULL);
822 *curp = cur;
823 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
824 return 0;
825}
826
827/*
828 * Convert a local file to an extents file.
829 * This code is out of bounds for data forks of regular files,
830 * since the file data needs to get logged so things will stay consistent.
831 * (The bmap-level manipulations are ok, though).
832 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000833void
834xfs_bmap_local_to_extents_empty(
835 struct xfs_inode *ip,
836 int whichfork)
837{
838 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
839
Darrick J. Wong60b49842016-10-03 09:11:34 -0700840 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000841 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
842 ASSERT(ifp->if_bytes == 0);
843 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
844
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000845 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000846 ifp->if_flags &= ~XFS_IFINLINE;
847 ifp->if_flags |= XFS_IFEXTENTS;
848 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
849}
850
851
Dave Chinner9e5987a2013-02-25 12:31:26 +1100852STATIC int /* error */
853xfs_bmap_local_to_extents(
854 xfs_trans_t *tp, /* transaction pointer */
855 xfs_inode_t *ip, /* incore inode pointer */
856 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
857 xfs_extlen_t total, /* total blocks needed by transaction */
858 int *logflagsp, /* inode logging flags */
859 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500860 void (*init_fn)(struct xfs_trans *tp,
861 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100862 struct xfs_inode *ip,
863 struct xfs_ifork *ifp))
864{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000865 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100866 int flags; /* logging flags returned */
867 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000868 xfs_alloc_arg_t args; /* allocation arguments */
869 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700870 struct xfs_bmbt_irec rec;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100871
872 /*
873 * We don't want to deal with the case of keeping inode data inline yet.
874 * So sending the data fork of a regular inode is invalid.
875 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100876 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100877 ifp = XFS_IFORK_PTR(ip, whichfork);
878 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000879
880 if (!ifp->if_bytes) {
881 xfs_bmap_local_to_extents_empty(ip, whichfork);
882 flags = XFS_ILOG_CORE;
883 goto done;
884 }
885
Dave Chinner9e5987a2013-02-25 12:31:26 +1100886 flags = 0;
887 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000888 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
889 XFS_IFINLINE);
890 memset(&args, 0, sizeof(args));
891 args.tp = tp;
892 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000893 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000894 args.firstblock = *firstblock;
895 /*
896 * Allocate a block. We know we need only one, since the
897 * file currently fits in an inode.
898 */
899 if (*firstblock == NULLFSBLOCK) {
900 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
901 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100902 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000903 args.fsbno = *firstblock;
904 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100905 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000906 args.total = total;
907 args.minlen = args.maxlen = args.prod = 1;
908 error = xfs_alloc_vextent(&args);
909 if (error)
910 goto done;
911
912 /* Can't fail, the space was reserved. */
913 ASSERT(args.fsbno != NULLFSBLOCK);
914 ASSERT(args.len == 1);
915 *firstblock = args.fsbno;
916 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
917
Dave Chinnerfe22d552015-01-22 09:30:06 +1100918 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100919 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100920 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100921 * The callout is responsible for logging because the remote format
922 * might differ from the local format and thus we don't know how much to
923 * log here. Note that init_fn must also set the buffer log item type
924 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100925 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000926 init_fn(tp, bp, ip, ifp);
927
Brian Fosterb7cdc662015-10-12 15:40:24 +1100928 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000929 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
930 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100931 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000932
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700933 rec.br_startoff = 0;
934 rec.br_startblock = args.fsbno;
935 rec.br_blockcount = 1;
936 rec.br_state = XFS_EXT_NORM;
937 xfs_iext_insert(ip, 0, 1, &rec, 0);
938
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000939 trace_xfs_bmap_post_update(ip, 0,
940 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
941 _THIS_IP_);
942 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
943 ip->i_d.di_nblocks = 1;
944 xfs_trans_mod_dquot_byino(tp, ip,
945 XFS_TRANS_DQ_BCOUNT, 1L);
946 flags |= xfs_ilog_fext(whichfork);
947
Dave Chinner9e5987a2013-02-25 12:31:26 +1100948done:
949 *logflagsp = flags;
950 return error;
951}
952
953/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 * Called from xfs_bmap_add_attrfork to handle btree format files.
955 */
956STATIC int /* error */
957xfs_bmap_add_attrfork_btree(
958 xfs_trans_t *tp, /* transaction pointer */
959 xfs_inode_t *ip, /* incore inode pointer */
960 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000961 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 int *flags) /* inode logging flags */
963{
964 xfs_btree_cur_t *cur; /* btree cursor */
965 int error; /* error return value */
966 xfs_mount_t *mp; /* file system mount struct */
967 int stat; /* newroot status */
968
969 mp = ip->i_mount;
970 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
971 *flags |= XFS_ILOG_DBROOT;
972 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100973 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000974 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 cur->bc_private.b.firstblock = *firstblock;
976 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
977 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000978 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100979 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100980 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 goto error0;
982 if (stat == 0) {
983 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000984 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 *firstblock = cur->bc_private.b.firstblock;
987 cur->bc_private.b.allocated = 0;
988 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
989 }
990 return 0;
991error0:
992 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
993 return error;
994}
995
996/*
997 * Called from xfs_bmap_add_attrfork to handle extents format files.
998 */
999STATIC int /* error */
1000xfs_bmap_add_attrfork_extents(
1001 xfs_trans_t *tp, /* transaction pointer */
1002 xfs_inode_t *ip, /* incore inode pointer */
1003 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001004 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int *flags) /* inode logging flags */
1006{
1007 xfs_btree_cur_t *cur; /* bmap btree cursor */
1008 int error; /* error return value */
1009
1010 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1011 return 0;
1012 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001013 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 flags, XFS_DATA_FORK);
1015 if (cur) {
1016 cur->bc_private.b.allocated = 0;
1017 xfs_btree_del_cursor(cur,
1018 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1019 }
1020 return error;
1021}
1022
1023/*
Dave Chinner1e823792013-02-11 15:58:13 +11001024 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1025 * different data fork content type needs a different callout to do the
1026 * conversion. Some are basic and only require special block initialisation
1027 * callouts for the data formating, others (directories) are so specialised they
1028 * handle everything themselves.
1029 *
1030 * XXX (dgc): investigate whether directory conversion can use the generic
1031 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001032 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
1034STATIC int /* error */
1035xfs_bmap_add_attrfork_local(
1036 xfs_trans_t *tp, /* transaction pointer */
1037 xfs_inode_t *ip, /* incore inode pointer */
1038 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001039 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int *flags) /* inode logging flags */
1041{
1042 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1045 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001046
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001047 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001049 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 dargs.dp = ip;
1051 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001052 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001053 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 dargs.whichfork = XFS_DATA_FORK;
1055 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001056 return xfs_dir2_sf_to_block(&dargs);
1057 }
1058
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001059 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001060 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1061 flags, XFS_DATA_FORK,
1062 xfs_symlink_local_to_remote);
1063
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001064 /* should only be called for types that support local format data */
1065 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001066 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001070 * Convert inode from non-attributed to attributed.
1071 * Must not be in a transaction, ip must not be locked.
1072 */
1073int /* error code */
1074xfs_bmap_add_attrfork(
1075 xfs_inode_t *ip, /* incore inode pointer */
1076 int size, /* space new attribute needs */
1077 int rsvd) /* xact may use reserved blks */
1078{
1079 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001080 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001081 xfs_mount_t *mp; /* mount structure */
1082 xfs_trans_t *tp; /* transaction pointer */
1083 int blks; /* space reservation */
1084 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001085 int logflags; /* logging flags */
1086 int error; /* error return value */
1087
1088 ASSERT(XFS_IFORK_Q(ip) == 0);
1089
1090 mp = ip->i_mount;
1091 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001092
Dave Chinner9e5987a2013-02-25 12:31:26 +11001093 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001094
1095 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1096 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1097 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001098 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001099
Dave Chinner9e5987a2013-02-25 12:31:26 +11001100 xfs_ilock(ip, XFS_ILOCK_EXCL);
1101 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1102 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1103 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001104 if (error)
1105 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001106 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001107 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001108 if (ip->i_d.di_anextents != 0) {
1109 error = -EFSCORRUPTED;
1110 goto trans_cancel;
1111 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001112 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1113 /*
1114 * For inodes coming from pre-6.2 filesystems.
1115 */
1116 ASSERT(ip->i_d.di_aformat == 0);
1117 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1118 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001119
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001120 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001121 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1122
1123 switch (ip->i_d.di_format) {
1124 case XFS_DINODE_FMT_DEV:
1125 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1126 break;
1127 case XFS_DINODE_FMT_UUID:
1128 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1129 break;
1130 case XFS_DINODE_FMT_LOCAL:
1131 case XFS_DINODE_FMT_EXTENTS:
1132 case XFS_DINODE_FMT_BTREE:
1133 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1134 if (!ip->i_d.di_forkoff)
1135 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1136 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1137 version = 2;
1138 break;
1139 default:
1140 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001141 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001142 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001143 }
1144
1145 ASSERT(ip->i_afp == NULL);
1146 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1147 ip->i_afp->if_flags = XFS_IFEXTENTS;
1148 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001149 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001150 switch (ip->i_d.di_format) {
1151 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001152 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001153 &logflags);
1154 break;
1155 case XFS_DINODE_FMT_EXTENTS:
1156 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001157 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001158 break;
1159 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001160 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001161 &logflags);
1162 break;
1163 default:
1164 error = 0;
1165 break;
1166 }
1167 if (logflags)
1168 xfs_trans_log_inode(tp, ip, logflags);
1169 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001170 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001171 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1172 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001173 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001174
1175 spin_lock(&mp->m_sb_lock);
1176 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1177 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001178 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001179 }
1180 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1181 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001182 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001183 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001184 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001185 if (log_sb)
1186 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001187 }
1188
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001189 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001190 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001191 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001192 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001193 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001194 return error;
1195
1196bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001197 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001198trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001199 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001200 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001201 return error;
1202}
1203
1204/*
1205 * Internal and external extent tree search functions.
1206 */
1207
1208/*
1209 * Read in the extents to if_extents.
1210 * All inode fields are set up by caller, we just traverse the btree
1211 * and copy the records in. If the file system cannot contain unwritten
1212 * extents, the records are checked for no "state" flags.
1213 */
1214int /* error */
1215xfs_bmap_read_extents(
1216 xfs_trans_t *tp, /* transaction pointer */
1217 xfs_inode_t *ip, /* incore inode */
1218 int whichfork) /* data or attr fork */
1219{
1220 struct xfs_btree_block *block; /* current btree block */
1221 xfs_fsblock_t bno; /* block # of "block" */
1222 xfs_buf_t *bp; /* buffer for "block" */
1223 int error; /* error return value */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001224 xfs_extnum_t i, j; /* index into the extents list */
1225 xfs_ifork_t *ifp; /* fork structure */
1226 int level; /* btree level, for checking */
1227 xfs_mount_t *mp; /* file system mount structure */
1228 __be64 *pp; /* pointer to block address */
1229 /* REFERENCED */
1230 xfs_extnum_t room; /* number of entries there's room for */
1231
Dave Chinner9e5987a2013-02-25 12:31:26 +11001232 mp = ip->i_mount;
1233 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001234 block = ifp->if_broot;
1235 /*
1236 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1237 */
1238 level = be16_to_cpu(block->bb_level);
1239 ASSERT(level > 0);
1240 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1241 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001242
Dave Chinner9e5987a2013-02-25 12:31:26 +11001243 /*
1244 * Go down the tree until leaf level is reached, following the first
1245 * pointer (leftmost) at each level.
1246 */
1247 while (level-- > 0) {
1248 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1249 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1250 if (error)
1251 return error;
1252 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001253 if (level == 0)
1254 break;
1255 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1256 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001257 XFS_WANT_CORRUPTED_GOTO(mp,
1258 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001259 xfs_trans_brelse(tp, bp);
1260 }
1261 /*
1262 * Here with bp and block set to the leftmost leaf node in the tree.
1263 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001264 room = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001265 i = 0;
1266 /*
1267 * Loop over all leaf nodes. Copy information to the extent records.
1268 */
1269 for (;;) {
1270 xfs_bmbt_rec_t *frp;
1271 xfs_fsblock_t nextbno;
1272 xfs_extnum_t num_recs;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001273
1274 num_recs = xfs_btree_get_numrecs(block);
1275 if (unlikely(i + num_recs > room)) {
1276 ASSERT(i + num_recs <= room);
1277 xfs_warn(ip->i_mount,
1278 "corrupt dinode %Lu, (btree extents).",
1279 (unsigned long long) ip->i_ino);
1280 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1281 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1282 goto error0;
1283 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001284 /*
1285 * Read-ahead the next leaf block, if any.
1286 */
1287 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1288 if (nextbno != NULLFSBLOCK)
1289 xfs_btree_reada_bufl(mp, nextbno, 1,
1290 &xfs_bmbt_buf_ops);
1291 /*
1292 * Copy records into the extent records.
1293 */
1294 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001295 for (j = 0; j < num_recs; j++, i++, frp++) {
1296 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1297 trp->l0 = be64_to_cpu(frp->l0);
1298 trp->l1 = be64_to_cpu(frp->l1);
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001299 if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001300 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001301 XFS_ERRLEVEL_LOW, mp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001302 goto error0;
1303 }
1304 }
1305 xfs_trans_brelse(tp, bp);
1306 bno = nextbno;
1307 /*
1308 * If we've reached the end, stop.
1309 */
1310 if (bno == NULLFSBLOCK)
1311 break;
1312 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1313 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1314 if (error)
1315 return error;
1316 block = XFS_BUF_TO_BLOCK(bp);
1317 }
Darrick J. Wong356a3222016-12-05 12:36:56 +11001318 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1319 return -EFSCORRUPTED;
Eric Sandeen5d829302016-11-08 12:59:42 +11001320 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11001321 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1322 return 0;
1323error0:
1324 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001325 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001326}
1327
Dave Chinner9e5987a2013-02-25 12:31:26 +11001328/*
1329 * Returns the file-relative block number of the first unused block(s)
1330 * in the file with at least "len" logically contiguous blocks free.
1331 * This is the lowest-address hole if the file has holes, else the first block
1332 * past the end of file.
1333 * Return 0 if the file is currently local (in-inode).
1334 */
1335int /* error */
1336xfs_bmap_first_unused(
1337 xfs_trans_t *tp, /* transaction pointer */
1338 xfs_inode_t *ip, /* incore inode */
1339 xfs_extlen_t len, /* size of hole to find */
1340 xfs_fileoff_t *first_unused, /* unused block */
1341 int whichfork) /* data or attr fork */
1342{
1343 int error; /* error return value */
1344 int idx; /* extent record index */
1345 xfs_ifork_t *ifp; /* inode fork pointer */
1346 xfs_fileoff_t lastaddr; /* last block number seen */
1347 xfs_fileoff_t lowest; /* lowest useful block */
1348 xfs_fileoff_t max; /* starting useful block */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001349 xfs_extnum_t nextents; /* number of extent entries */
1350
1351 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1352 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1353 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1354 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1355 *first_unused = 0;
1356 return 0;
1357 }
1358 ifp = XFS_IFORK_PTR(ip, whichfork);
1359 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1360 (error = xfs_iread_extents(tp, ip, whichfork)))
1361 return error;
1362 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001363 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001364 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001365 struct xfs_bmbt_irec got;
1366
1367 xfs_iext_get_extent(ifp, idx, &got);
1368
Dave Chinner9e5987a2013-02-25 12:31:26 +11001369 /*
1370 * See if the hole before this extent will work.
1371 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001372 if (got.br_startoff >= lowest + len &&
1373 got.br_startoff - max >= len) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001374 *first_unused = max;
1375 return 0;
1376 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001377 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001378 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1379 }
1380 *first_unused = max;
1381 return 0;
1382}
1383
1384/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001385 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001386 * last_block (input value) in the file.
1387 * This is not based on i_size, it is based on the extent records.
1388 * Returns 0 for local files, as they do not have extent records.
1389 */
1390int /* error */
1391xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001392 struct xfs_trans *tp, /* transaction pointer */
1393 struct xfs_inode *ip, /* incore inode */
1394 xfs_fileoff_t *last_block, /* last block */
1395 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001396{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001397 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1398 struct xfs_bmbt_irec got;
1399 xfs_extnum_t idx;
1400 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001401
Christoph Hellwig86685f72016-11-24 11:39:38 +11001402 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1403 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001404 *last_block = 0;
1405 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001406 case XFS_DINODE_FMT_BTREE:
1407 case XFS_DINODE_FMT_EXTENTS:
1408 break;
1409 default:
1410 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001411 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001412
1413 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1414 error = xfs_iread_extents(tp, ip, whichfork);
1415 if (error)
1416 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001417 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001418
1419 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1420 if (got.br_startoff <= *last_block - 1)
1421 return 0;
1422 }
1423
1424 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1425 *last_block = got.br_startoff + got.br_blockcount;
1426 return 0;
1427 }
1428
1429 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001430 return 0;
1431}
1432
Dave Chinner68988112013-08-12 20:49:42 +10001433int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001434xfs_bmap_last_extent(
1435 struct xfs_trans *tp,
1436 struct xfs_inode *ip,
1437 int whichfork,
1438 struct xfs_bmbt_irec *rec,
1439 int *is_empty)
1440{
1441 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1442 int error;
1443 int nextents;
1444
1445 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1446 error = xfs_iread_extents(tp, ip, whichfork);
1447 if (error)
1448 return error;
1449 }
1450
Eric Sandeen5d829302016-11-08 12:59:42 +11001451 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001452 if (nextents == 0) {
1453 *is_empty = 1;
1454 return 0;
1455 }
1456
1457 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
1458 *is_empty = 0;
1459 return 0;
1460}
1461
1462/*
1463 * Check the last inode extent to determine whether this allocation will result
1464 * in blocks being allocated at the end of the file. When we allocate new data
1465 * blocks at the end of the file which do not start at the previous data block,
1466 * we will try to align the new blocks at stripe unit boundaries.
1467 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001468 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001469 * at, or past the EOF.
1470 */
1471STATIC int
1472xfs_bmap_isaeof(
1473 struct xfs_bmalloca *bma,
1474 int whichfork)
1475{
1476 struct xfs_bmbt_irec rec;
1477 int is_empty;
1478 int error;
1479
Thomas Meyer749f24f2017-10-09 11:38:54 -07001480 bma->aeof = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001481 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1482 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001483 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001484 return error;
1485
Dave Chinner6e708bc2013-11-22 10:41:16 +11001486 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001487 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001488 return 0;
1489 }
1490
Dave Chinner9e5987a2013-02-25 12:31:26 +11001491 /*
1492 * Check if we are allocation or past the last extent, or at least into
1493 * the last delayed allocated extent.
1494 */
1495 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1496 (bma->offset >= rec.br_startoff &&
1497 isnullstartblock(rec.br_startblock));
1498 return 0;
1499}
1500
1501/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001502 * Returns the file-relative block number of the first block past eof in
1503 * the file. This is not based on i_size, it is based on the extent records.
1504 * Returns 0 for local files, as they do not have extent records.
1505 */
1506int
1507xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001508 struct xfs_inode *ip,
1509 xfs_fileoff_t *last_block,
1510 int whichfork)
1511{
1512 struct xfs_bmbt_irec rec;
1513 int is_empty;
1514 int error;
1515
1516 *last_block = 0;
1517
1518 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1519 return 0;
1520
1521 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1522 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001523 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001524
1525 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1526 if (error || is_empty)
1527 return error;
1528
1529 *last_block = rec.br_startoff + rec.br_blockcount;
1530 return 0;
1531}
1532
1533/*
1534 * Returns whether the selected fork of the inode has exactly one
1535 * block or not. For the data fork we check this matches di_size,
1536 * implying the file's range is 0..bsize-1.
1537 */
1538int /* 1=>1 block, 0=>otherwise */
1539xfs_bmap_one_block(
1540 xfs_inode_t *ip, /* incore inode */
1541 int whichfork) /* data or attr fork */
1542{
1543 xfs_bmbt_rec_host_t *ep; /* ptr to fork's extent */
1544 xfs_ifork_t *ifp; /* inode fork pointer */
1545 int rval; /* return value */
1546 xfs_bmbt_irec_t s; /* internal version of extent */
1547
1548#ifndef DEBUG
1549 if (whichfork == XFS_DATA_FORK)
1550 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1551#endif /* !DEBUG */
1552 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1553 return 0;
1554 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1555 return 0;
1556 ifp = XFS_IFORK_PTR(ip, whichfork);
1557 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1558 ep = xfs_iext_get_ext(ifp, 0);
1559 xfs_bmbt_get_all(ep, &s);
1560 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1561 if (rval && whichfork == XFS_DATA_FORK)
1562 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1563 return rval;
1564}
1565
1566/*
1567 * Extent tree manipulation functions used during allocation.
1568 */
1569
1570/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001571 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 */
1573STATIC int /* error */
1574xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001575 struct xfs_bmalloca *bma,
1576 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001578 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001580 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001583 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 xfs_fileoff_t new_endoff; /* end offset of new entry */
1585 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1586 /* left is 0, right is 1, prev is 2 */
1587 int rval=0; /* return value (logging flags) */
1588 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001589 xfs_filblks_t da_new; /* new count del alloc blocks used */
1590 xfs_filblks_t da_old; /* old count del alloc blocks used */
1591 xfs_filblks_t temp=0; /* value for da_new calculations */
1592 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001594 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001595 xfs_extnum_t *nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001597 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001598 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001599 ASSERT(whichfork != XFS_ATTR_FORK);
1600 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1601 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001602
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001603 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001604 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001605 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001606 ASSERT(!bma->cur ||
1607 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001608
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001609 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001610
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611#define LEFT r[0]
1612#define RIGHT r[1]
1613#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Darrick J. Wong60b49842016-10-03 09:11:34 -07001615 if (whichfork == XFS_COW_FORK)
1616 state |= BMAP_COWFORK;
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /*
1619 * Set up a bunch of variables to make the tests simpler.
1620 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001621 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 xfs_bmbt_get_all(ep, &PREV);
1623 new_endoff = new->br_startoff + new->br_blockcount;
1624 ASSERT(PREV.br_startoff <= new->br_startoff);
1625 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001626
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001627 da_old = startblockval(PREV.br_startblock);
1628 da_new = 0;
1629
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 /*
1631 * Set flags determining what part of the previous delayed allocation
1632 * extent is being replaced by a real allocation.
1633 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001634 if (PREV.br_startoff == new->br_startoff)
1635 state |= BMAP_LEFT_FILLING;
1636 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1637 state |= BMAP_RIGHT_FILLING;
1638
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 /*
1640 * Check and set flags if this segment has a left neighbor.
1641 * Don't set contiguous if the combined extent would be too large.
1642 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001643 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001644 state |= BMAP_LEFT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001645 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001646
1647 if (isnullstartblock(LEFT.br_startblock))
1648 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001650
1651 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1652 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1653 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1654 LEFT.br_state == new->br_state &&
1655 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1656 state |= BMAP_LEFT_CONTIG;
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 /*
1659 * Check and set flags if this segment has a right neighbor.
1660 * Don't set contiguous if the combined extent would be too large.
1661 * Also check for all-three-contiguous being too large.
1662 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001663 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001664 state |= BMAP_RIGHT_VALID;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001665 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001666
1667 if (isnullstartblock(RIGHT.br_startblock))
1668 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001670
1671 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1672 new_endoff == RIGHT.br_startoff &&
1673 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1674 new->br_state == RIGHT.br_state &&
1675 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1676 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1677 BMAP_RIGHT_FILLING)) !=
1678 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1679 BMAP_RIGHT_FILLING) ||
1680 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1681 <= MAXEXTLEN))
1682 state |= BMAP_RIGHT_CONTIG;
1683
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 error = 0;
1685 /*
1686 * Switch out based on the FILLING and CONTIG state bits.
1687 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001688 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1689 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1690 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1691 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /*
1693 * Filling in all of a previously delayed allocation extent.
1694 * The left and right neighbors are both contiguous with new.
1695 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001696 bma->idx--;
1697 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1698 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 LEFT.br_blockcount + PREV.br_blockcount +
1700 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001701 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001702
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001703 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001704 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001705 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1707 else {
1708 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001709 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001711 RIGHT.br_blockcount, &i);
1712 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001714 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001715 error = xfs_btree_delete(bma->cur, &i);
1716 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001718 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001719 error = xfs_btree_decrement(bma->cur, 0, &i);
1720 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001722 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001723 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 LEFT.br_startblock,
1725 LEFT.br_blockcount +
1726 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001727 RIGHT.br_blockcount, LEFT.br_state);
1728 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 goto done;
1730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 break;
1732
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001733 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 /*
1735 * Filling in all of a previously delayed allocation extent.
1736 * The left neighbor is contiguous, the right is not.
1737 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001738 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001739
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001740 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1741 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001742 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001743 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001744
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001745 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1746 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 rval = XFS_ILOG_DEXT;
1748 else {
1749 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001750 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001752 &i);
1753 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001755 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001756 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 LEFT.br_startblock,
1758 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001759 PREV.br_blockcount, LEFT.br_state);
1760 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 goto done;
1762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 break;
1764
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001765 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 /*
1767 * Filling in all of a previously delayed allocation extent.
1768 * The right neighbor is contiguous, the left is not.
1769 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001770 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 xfs_bmbt_set_startblock(ep, new->br_startblock);
1772 xfs_bmbt_set_blockcount(ep,
1773 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001774 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001775
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001776 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1777 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 rval = XFS_ILOG_DEXT;
1779 else {
1780 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001781 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001783 RIGHT.br_blockcount, &i);
1784 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001786 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001787 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 new->br_startblock,
1789 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001790 RIGHT.br_blockcount, PREV.br_state);
1791 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 goto done;
1793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 break;
1795
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001796 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 /*
1798 * Filling in all of a previously delayed allocation extent.
1799 * Neither the left nor right neighbors are contiguous with
1800 * the new one.
1801 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001802 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 xfs_bmbt_set_startblock(ep, new->br_startblock);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08001804 xfs_bmbt_set_state(ep, new->br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001805 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001806
Darrick J. Wong60b49842016-10-03 09:11:34 -07001807 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001808 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1810 else {
1811 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001812 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001814 &i);
1815 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001817 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001818 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1819 error = xfs_btree_insert(bma->cur, &i);
1820 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001822 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 break;
1825
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001826 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 /*
1828 * Filling in the first part of a previous delayed allocation.
1829 * The left neighbor is contiguous.
1830 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001831 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1832 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 LEFT.br_blockcount + new->br_blockcount);
1834 xfs_bmbt_set_startoff(ep,
1835 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001836 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001839 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001841 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 rval = XFS_ILOG_DEXT;
1843 else {
1844 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001845 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 &i);
1848 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001850 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001851 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 LEFT.br_startblock,
1853 LEFT.br_blockcount +
1854 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001855 LEFT.br_state);
1856 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 goto done;
1858 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001859 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001860 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001861 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001862 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001863
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001864 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 break;
1866
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001867 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 /*
1869 * Filling in the first part of a previous delayed allocation.
1870 * The left neighbor is not contiguous.
1871 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001872 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 xfs_bmbt_set_startoff(ep, new_endoff);
1874 temp = PREV.br_blockcount - new->br_blockcount;
1875 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001876 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001877 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001878 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1880 else {
1881 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001882 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001884 &i);
1885 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001887 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001888 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1889 error = xfs_btree_insert(bma->cur, &i);
1890 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001892 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001894
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001895 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001896 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001897 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001898 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 rval |= tmp_rval;
1900 if (error)
1901 goto done;
1902 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001903 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001904 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001905 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1906 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001907 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001908 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 break;
1910
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001911 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 /*
1913 * Filling in the last part of a previous delayed allocation.
1914 * The right neighbor is contiguous with the new allocation.
1915 */
1916 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001917 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001919 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001920 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 new->br_blockcount + RIGHT.br_blockcount,
1922 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001923 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1924 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 rval = XFS_ILOG_DEXT;
1926 else {
1927 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001928 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001930 RIGHT.br_blockcount, &i);
1931 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001933 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001934 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 new->br_startblock,
1936 new->br_blockcount +
1937 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001938 RIGHT.br_state);
1939 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 goto done;
1941 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001942
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001943 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001944 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001945 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001946 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001947 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001948
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001949 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 break;
1951
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001952 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 /*
1954 * Filling in the last part of a previous delayed allocation.
1955 * The right neighbor is not contiguous.
1956 */
1957 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001960 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001961 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001962 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1964 else {
1965 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001966 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001968 &i);
1969 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001971 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001972 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1973 error = xfs_btree_insert(bma->cur, &i);
1974 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001976 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001978
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001979 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001980 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001981 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001982 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 rval |= tmp_rval;
1984 if (error)
1985 goto done;
1986 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001987 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001988 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001989 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1990 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001991 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001992 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001993
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001994 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 break;
1996
1997 case 0:
1998 /*
1999 * Filling in the middle part of a previous delayed allocation.
2000 * Contiguity is impossible here.
2001 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002002 *
2003 * We start with a delayed allocation:
2004 *
2005 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2006 * PREV @ idx
2007 *
2008 * and we are allocating:
2009 * +rrrrrrrrrrrrrrrrr+
2010 * new
2011 *
2012 * and we set it up for insertion as:
2013 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2014 * new
2015 * PREV @ idx LEFT RIGHT
2016 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 */
2018 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002020 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002021 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2022 LEFT = *new;
2023 RIGHT.br_state = PREV.br_state;
2024 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002025 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002026 RIGHT.br_startoff = new_endoff;
2027 RIGHT.br_blockcount = temp2;
2028 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002029 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002030 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002031 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2033 else {
2034 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002035 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002037 &i);
2038 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002040 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002041 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2042 error = xfs_btree_insert(bma->cur, &i);
2043 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002045 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002047
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002048 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002049 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002050 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002051 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 rval |= tmp_rval;
2053 if (error)
2054 goto done;
2055 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002056 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2057 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Brian Foster0daaeca2017-05-12 10:44:08 -07002058 diff = (int)(temp + temp2 -
2059 (startblockval(PREV.br_startblock) -
2060 (bma->cur ?
2061 bma->cur->bc_private.b.allocated : 0)));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002062 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002063 error = xfs_mod_fdblocks(bma->ip->i_mount,
2064 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002065 ASSERT(!error);
2066 if (error)
2067 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002069
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002070 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002071 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002072 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2073 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2074 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002075 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002076 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002077
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002078 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002079 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 break;
2081
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002082 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2083 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2084 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2085 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2086 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2087 case BMAP_LEFT_CONTIG:
2088 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 /*
2090 * These cases are all impossible.
2091 */
2092 ASSERT(0);
2093 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002094
Darrick J. Wong9c194642016-08-03 12:16:05 +10002095 /* add reverse mapping */
2096 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2097 if (error)
2098 goto done;
2099
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002100 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002101 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002102 int tmp_logflags; /* partial log flag return val */
2103
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002104 ASSERT(bma->cur == NULL);
2105 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002106 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002107 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002108 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002109 if (error)
2110 goto done;
2111 }
2112
2113 /* adjust for changes in reserved delayed indirect blocks */
2114 if (da_old || da_new) {
2115 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002116 if (bma->cur)
2117 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002118 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002119 xfs_mod_fdblocks(bma->ip->i_mount,
2120 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002121 }
2122
2123 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002124 if (bma->cur)
2125 bma->cur->bc_private.b.allocated = 0;
2126
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002127 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002129 if (whichfork != XFS_COW_FORK)
2130 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 return error;
2132#undef LEFT
2133#undef RIGHT
2134#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135}
2136
2137/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002138 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 */
2140STATIC int /* error */
2141xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002142 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002144 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002145 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002147 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002148 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002149 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002150 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002153 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002156 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 xfs_fileoff_t new_endoff; /* end offset of new entry */
2158 xfs_exntst_t newext; /* new extent state */
2159 xfs_exntst_t oldext; /* old extent state */
2160 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2161 /* left is 0, right is 1, prev is 2 */
2162 int rval=0; /* return value (logging flags) */
2163 int state = 0;/* state bits, accessed thru macros */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002164 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002166 *logflagsp = 0;
2167
2168 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002169 ifp = XFS_IFORK_PTR(ip, whichfork);
2170 if (whichfork == XFS_COW_FORK)
2171 state |= BMAP_COWFORK;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002172
2173 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002174 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002175 ASSERT(!isnullstartblock(new->br_startblock));
2176
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002177 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179#define LEFT r[0]
2180#define RIGHT r[1]
2181#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002182
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 /*
2184 * Set up a bunch of variables to make the tests simpler.
2185 */
2186 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002187 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 xfs_bmbt_get_all(ep, &PREV);
2189 newext = new->br_state;
2190 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2191 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2192 ASSERT(PREV.br_state == oldext);
2193 new_endoff = new->br_startoff + new->br_blockcount;
2194 ASSERT(PREV.br_startoff <= new->br_startoff);
2195 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002196
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 /*
2198 * Set flags determining what part of the previous oldext allocation
2199 * extent is being replaced by a newext allocation.
2200 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002201 if (PREV.br_startoff == new->br_startoff)
2202 state |= BMAP_LEFT_FILLING;
2203 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2204 state |= BMAP_RIGHT_FILLING;
2205
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 /*
2207 * Check and set flags if this segment has a left neighbor.
2208 * Don't set contiguous if the combined extent would be too large.
2209 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002210 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002211 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002212 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002213
2214 if (isnullstartblock(LEFT.br_startblock))
2215 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002217
2218 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2219 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2220 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2221 LEFT.br_state == newext &&
2222 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2223 state |= BMAP_LEFT_CONTIG;
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 /*
2226 * Check and set flags if this segment has a right neighbor.
2227 * Don't set contiguous if the combined extent would be too large.
2228 * Also check for all-three-contiguous being too large.
2229 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002230 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002231 state |= BMAP_RIGHT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002232 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002233 if (isnullstartblock(RIGHT.br_startblock))
2234 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002236
2237 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2238 new_endoff == RIGHT.br_startoff &&
2239 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2240 newext == RIGHT.br_state &&
2241 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2242 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2243 BMAP_RIGHT_FILLING)) !=
2244 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2245 BMAP_RIGHT_FILLING) ||
2246 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2247 <= MAXEXTLEN))
2248 state |= BMAP_RIGHT_CONTIG;
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 /*
2251 * Switch out based on the FILLING and CONTIG state bits.
2252 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002253 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2254 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2255 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2256 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 /*
2258 * Setting all of a previous oldext extent to newext.
2259 * The left and right neighbors are both contiguous with new.
2260 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002261 --*idx;
2262
2263 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2264 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 LEFT.br_blockcount + PREV.br_blockcount +
2266 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002267 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002268
Christoph Hellwigec90c552011-05-23 08:52:53 +00002269 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002270 XFS_IFORK_NEXT_SET(ip, whichfork,
2271 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (cur == NULL)
2273 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2274 else {
2275 rval = XFS_ILOG_CORE;
2276 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2277 RIGHT.br_startblock,
2278 RIGHT.br_blockcount, &i)))
2279 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002280 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002281 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002283 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002284 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002286 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002287 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002289 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002290 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002292 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2294 LEFT.br_startblock,
2295 LEFT.br_blockcount + PREV.br_blockcount +
2296 RIGHT.br_blockcount, LEFT.br_state)))
2297 goto done;
2298 }
2299 break;
2300
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002301 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 /*
2303 * Setting all of a previous oldext extent to newext.
2304 * The left neighbor is contiguous, the right is not.
2305 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002306 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002307
Christoph Hellwigec90c552011-05-23 08:52:53 +00002308 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2309 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2310 LEFT.br_blockcount + PREV.br_blockcount);
2311 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2312
2313 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002314 XFS_IFORK_NEXT_SET(ip, whichfork,
2315 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 if (cur == NULL)
2317 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2318 else {
2319 rval = XFS_ILOG_CORE;
2320 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2321 PREV.br_startblock, PREV.br_blockcount,
2322 &i)))
2323 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002324 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002325 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002327 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002328 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002330 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2332 LEFT.br_startblock,
2333 LEFT.br_blockcount + PREV.br_blockcount,
2334 LEFT.br_state)))
2335 goto done;
2336 }
2337 break;
2338
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002339 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 /*
2341 * Setting all of a previous oldext extent to newext.
2342 * The right neighbor is contiguous, the left is not.
2343 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002344 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 xfs_bmbt_set_blockcount(ep,
2346 PREV.br_blockcount + RIGHT.br_blockcount);
2347 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002348 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2349 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002350 XFS_IFORK_NEXT_SET(ip, whichfork,
2351 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 if (cur == NULL)
2353 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2354 else {
2355 rval = XFS_ILOG_CORE;
2356 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2357 RIGHT.br_startblock,
2358 RIGHT.br_blockcount, &i)))
2359 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002360 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002361 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002363 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002364 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002366 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2368 new->br_startblock,
2369 new->br_blockcount + RIGHT.br_blockcount,
2370 newext)))
2371 goto done;
2372 }
2373 break;
2374
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002375 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 /*
2377 * Setting all of a previous oldext extent to newext.
2378 * Neither the left nor right neighbors are contiguous with
2379 * the new one.
2380 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002381 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002383 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002384
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (cur == NULL)
2386 rval = XFS_ILOG_DEXT;
2387 else {
2388 rval = 0;
2389 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2390 new->br_startblock, new->br_blockcount,
2391 &i)))
2392 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002393 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2395 new->br_startblock, new->br_blockcount,
2396 newext)))
2397 goto done;
2398 }
2399 break;
2400
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002401 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 /*
2403 * Setting the first part of a previous oldext extent to newext.
2404 * The left neighbor is contiguous.
2405 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002406 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2407 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 LEFT.br_blockcount + new->br_blockcount);
2409 xfs_bmbt_set_startoff(ep,
2410 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002411 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002412
Christoph Hellwigec90c552011-05-23 08:52:53 +00002413 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 xfs_bmbt_set_startblock(ep,
2415 new->br_startblock + new->br_blockcount);
2416 xfs_bmbt_set_blockcount(ep,
2417 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002418 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002419
Christoph Hellwigec90c552011-05-23 08:52:53 +00002420 --*idx;
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 if (cur == NULL)
2423 rval = XFS_ILOG_DEXT;
2424 else {
2425 rval = 0;
2426 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2427 PREV.br_startblock, PREV.br_blockcount,
2428 &i)))
2429 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002430 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 if ((error = xfs_bmbt_update(cur,
2432 PREV.br_startoff + new->br_blockcount,
2433 PREV.br_startblock + new->br_blockcount,
2434 PREV.br_blockcount - new->br_blockcount,
2435 oldext)))
2436 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002437 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002439 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 LEFT.br_startblock,
2441 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002442 LEFT.br_state);
2443 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 goto done;
2445 }
2446 break;
2447
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002448 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 /*
2450 * Setting the first part of a previous oldext extent to newext.
2451 * The left neighbor is not contiguous.
2452 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002453 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2455 xfs_bmbt_set_startoff(ep, new_endoff);
2456 xfs_bmbt_set_blockcount(ep,
2457 PREV.br_blockcount - new->br_blockcount);
2458 xfs_bmbt_set_startblock(ep,
2459 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002460 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002461
Christoph Hellwigec90c552011-05-23 08:52:53 +00002462 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002463 XFS_IFORK_NEXT_SET(ip, whichfork,
2464 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 if (cur == NULL)
2466 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2467 else {
2468 rval = XFS_ILOG_CORE;
2469 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2470 PREV.br_startblock, PREV.br_blockcount,
2471 &i)))
2472 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002473 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if ((error = xfs_bmbt_update(cur,
2475 PREV.br_startoff + new->br_blockcount,
2476 PREV.br_startblock + new->br_blockcount,
2477 PREV.br_blockcount - new->br_blockcount,
2478 oldext)))
2479 goto done;
2480 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002481 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002483 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 }
2485 break;
2486
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002487 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 /*
2489 * Setting the last part of a previous oldext extent to newext.
2490 * The right neighbor is contiguous with the new allocation.
2491 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002492 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 xfs_bmbt_set_blockcount(ep,
2494 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002495 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2496
2497 ++*idx;
2498
2499 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2500 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002501 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002503 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 if (cur == NULL)
2506 rval = XFS_ILOG_DEXT;
2507 else {
2508 rval = 0;
2509 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2510 PREV.br_startblock,
2511 PREV.br_blockcount, &i)))
2512 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002513 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2515 PREV.br_startblock,
2516 PREV.br_blockcount - new->br_blockcount,
2517 oldext)))
2518 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002519 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 goto done;
2521 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2522 new->br_startblock,
2523 new->br_blockcount + RIGHT.br_blockcount,
2524 newext)))
2525 goto done;
2526 }
2527 break;
2528
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002529 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 /*
2531 * Setting the last part of a previous oldext extent to newext.
2532 * The right neighbor is not contiguous.
2533 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002534 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 xfs_bmbt_set_blockcount(ep,
2536 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002537 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002538
Christoph Hellwigec90c552011-05-23 08:52:53 +00002539 ++*idx;
2540 xfs_iext_insert(ip, *idx, 1, new, state);
2541
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002542 XFS_IFORK_NEXT_SET(ip, whichfork,
2543 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 if (cur == NULL)
2545 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2546 else {
2547 rval = XFS_ILOG_CORE;
2548 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2549 PREV.br_startblock, PREV.br_blockcount,
2550 &i)))
2551 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002552 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2554 PREV.br_startblock,
2555 PREV.br_blockcount - new->br_blockcount,
2556 oldext)))
2557 goto done;
2558 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2559 new->br_startblock, new->br_blockcount,
2560 &i)))
2561 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002562 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4be2d1a2017-10-17 14:16:19 -07002563 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002564 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002566 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
2568 break;
2569
2570 case 0:
2571 /*
2572 * Setting the middle part of a previous oldext extent to
2573 * newext. Contiguity is impossible here.
2574 * One extent becomes three extents.
2575 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002576 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 xfs_bmbt_set_blockcount(ep,
2578 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002579 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 r[0] = *new;
2582 r[1].br_startoff = new_endoff;
2583 r[1].br_blockcount =
2584 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2585 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2586 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002587
2588 ++*idx;
2589 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2590
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002591 XFS_IFORK_NEXT_SET(ip, whichfork,
2592 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 if (cur == NULL)
2594 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2595 else {
2596 rval = XFS_ILOG_CORE;
2597 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2598 PREV.br_startblock, PREV.br_blockcount,
2599 &i)))
2600 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002601 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /* new right extent - oldext */
2603 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2604 r[1].br_startblock, r[1].br_blockcount,
2605 r[1].br_state)))
2606 goto done;
2607 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002609 cur->bc_rec.b.br_blockcount =
2610 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002611 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002613 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002614 /*
2615 * Reset the cursor to the position of the new extent
2616 * we are about to insert as we can't trust it after
2617 * the previous insert.
2618 */
2619 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2620 new->br_startblock, new->br_blockcount,
2621 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002623 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002625 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002626 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002628 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 }
2630 break;
2631
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002632 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2633 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2634 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2635 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2636 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2637 case BMAP_LEFT_CONTIG:
2638 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 /*
2640 * These cases are all impossible.
2641 */
2642 ASSERT(0);
2643 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002644
Darrick J. Wong9c194642016-08-03 12:16:05 +10002645 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002646 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002647 if (error)
2648 goto done;
2649
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002650 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002651 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002652 int tmp_logflags; /* partial log flag return val */
2653
2654 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002655 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002656 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002657 *logflagsp |= tmp_logflags;
2658 if (error)
2659 goto done;
2660 }
2661
2662 /* clear out the allocated field, done with it now in any case. */
2663 if (cur) {
2664 cur->bc_private.b.allocated = 0;
2665 *curp = cur;
2666 }
2667
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002668 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002670 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 return error;
2672#undef LEFT
2673#undef RIGHT
2674#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
2677/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002678 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002680STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681xfs_bmap_add_extent_hole_delay(
2682 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002683 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002684 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002685 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002687 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2689 xfs_filblks_t newlen=0; /* new indirect size */
2690 xfs_filblks_t oldlen=0; /* old indirect size */
2691 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2692 int state; /* state bits, accessed thru macros */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002693 xfs_filblks_t temp=0; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002695 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002697 if (whichfork == XFS_COW_FORK)
2698 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002699 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 /*
2702 * Check and set flags if this segment has a left neighbor
2703 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002704 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002705 state |= BMAP_LEFT_VALID;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002706 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002707
2708 if (isnullstartblock(left.br_startblock))
2709 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002711
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 /*
2713 * Check and set flags if the current (right) segment exists.
2714 * If it doesn't exist, we're converting the hole at end-of-file.
2715 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002716 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002717 state |= BMAP_RIGHT_VALID;
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002718 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002719
2720 if (isnullstartblock(right.br_startblock))
2721 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 /*
2725 * Set contiguity flags on the left and right neighbors.
2726 * Don't let extents get too large, even if the pieces are contiguous.
2727 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002728 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2729 left.br_startoff + left.br_blockcount == new->br_startoff &&
2730 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2731 state |= BMAP_LEFT_CONTIG;
2732
2733 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2734 new->br_startoff + new->br_blockcount == right.br_startoff &&
2735 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2736 (!(state & BMAP_LEFT_CONTIG) ||
2737 (left.br_blockcount + new->br_blockcount +
2738 right.br_blockcount <= MAXEXTLEN)))
2739 state |= BMAP_RIGHT_CONTIG;
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 /*
2742 * Switch out based on the contiguity flags.
2743 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002744 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2745 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 /*
2747 * New allocation is contiguous with delayed allocations
2748 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002749 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002751 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 temp = left.br_blockcount + new->br_blockcount +
2753 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002754
Christoph Hellwigec90c552011-05-23 08:52:53 +00002755 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2756 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002757 oldlen = startblockval(left.br_startblock) +
2758 startblockval(new->br_startblock) +
2759 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002760 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2761 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002762 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002763 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002764 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002765
Christoph Hellwigec90c552011-05-23 08:52:53 +00002766 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 break;
2768
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002769 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /*
2771 * New allocation is contiguous with a delayed allocation
2772 * on the left.
2773 * Merge the new allocation with the left neighbor.
2774 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002775 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002777
2778 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2779 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002780 oldlen = startblockval(left.br_startblock) +
2781 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002782 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2783 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002784 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002785 nullstartblock((int)newlen));
Christoph Hellwigec90c552011-05-23 08:52:53 +00002786 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 break;
2788
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002789 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 /*
2791 * New allocation is contiguous with a delayed allocation
2792 * on the right.
2793 * Merge the new allocation with the right neighbor.
2794 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002795 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002797 oldlen = startblockval(new->br_startblock) +
2798 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002799 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2800 oldlen);
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002801 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2802 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002803 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002804 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 break;
2806
2807 case 0:
2808 /*
2809 * New allocation is not contiguous with another
2810 * delayed allocation.
2811 * Insert a new entry.
2812 */
2813 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002814 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 break;
2816 }
2817 if (oldlen != newlen) {
2818 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002819 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2820 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 /*
2822 * Nothing to do for disk quota accounting here.
2823 */
2824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825}
2826
2827/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002828 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 */
2830STATIC int /* error */
2831xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002832 struct xfs_trans *tp,
2833 struct xfs_inode *ip,
2834 int whichfork,
2835 xfs_extnum_t *idx,
2836 struct xfs_btree_cur **curp,
2837 struct xfs_bmbt_irec *new,
2838 xfs_fsblock_t *first,
2839 struct xfs_defer_ops *dfops,
2840 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002842 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2843 struct xfs_mount *mp = ip->i_mount;
2844 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2848 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002849 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Christoph Hellwig6d045582017-04-11 16:45:54 -07002852 ASSERT(*idx >= 0);
2853 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002854 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002855 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002856
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002857 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002858
2859 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002860 if (whichfork == XFS_ATTR_FORK)
2861 state |= BMAP_ATTRFORK;
Christoph Hellwiga14234c2017-02-06 10:50:49 -08002862 if (whichfork == XFS_COW_FORK)
2863 state |= BMAP_COWFORK;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002864
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 /*
2866 * Check and set flags if this segment has a left neighbor.
2867 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002868 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002869 state |= BMAP_LEFT_VALID;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002870 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002871 if (isnullstartblock(left.br_startblock))
2872 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002874
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 /*
2876 * Check and set flags if this segment has a current value.
2877 * Not true if we're inserting into the "hole" at eof.
2878 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002879 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002880 state |= BMAP_RIGHT_VALID;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002881 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002882 if (isnullstartblock(right.br_startblock))
2883 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 /*
2887 * We're inserting a real allocation between "left" and "right".
2888 * Set the contiguity flags. Don't let extents get too large.
2889 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002890 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2891 left.br_startoff + left.br_blockcount == new->br_startoff &&
2892 left.br_startblock + left.br_blockcount == new->br_startblock &&
2893 left.br_state == new->br_state &&
2894 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2895 state |= BMAP_LEFT_CONTIG;
2896
2897 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2898 new->br_startoff + new->br_blockcount == right.br_startoff &&
2899 new->br_startblock + new->br_blockcount == right.br_startblock &&
2900 new->br_state == right.br_state &&
2901 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2902 (!(state & BMAP_LEFT_CONTIG) ||
2903 left.br_blockcount + new->br_blockcount +
2904 right.br_blockcount <= MAXEXTLEN))
2905 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002907 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 /*
2909 * Select which case we're in here, and implement it.
2910 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002911 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2912 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 /*
2914 * New allocation is contiguous with real allocations on the
2915 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002916 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002918 --*idx;
2919 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2920 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 left.br_blockcount + new->br_blockcount +
2922 right.br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002923 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002924
Christoph Hellwig6d045582017-04-11 16:45:54 -07002925 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002926
Christoph Hellwig6d045582017-04-11 16:45:54 -07002927 XFS_IFORK_NEXT_SET(ip, whichfork,
2928 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2929 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002930 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002931 } else {
2932 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002933 error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002934 right.br_startblock, right.br_blockcount,
2935 &i);
2936 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002937 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002938 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002939 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002940 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002941 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002942 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002943 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002944 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002945 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002946 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002947 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002948 left.br_startblock,
2949 left.br_blockcount +
2950 new->br_blockcount +
2951 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002952 left.br_state);
2953 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002954 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002956 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002958 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 /*
2960 * New allocation is contiguous with a real allocation
2961 * on the left.
2962 * Merge the new allocation with the left neighbor.
2963 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002964 --*idx;
2965 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2966 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 left.br_blockcount + new->br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002968 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002969
Christoph Hellwig6d045582017-04-11 16:45:54 -07002970 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002971 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002972 } else {
2973 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002974 error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002975 left.br_startblock, left.br_blockcount,
2976 &i);
2977 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002978 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002979 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002980 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002981 left.br_startblock,
2982 left.br_blockcount +
2983 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002984 left.br_state);
2985 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002986 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002988 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002990 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 /*
2992 * New allocation is contiguous with a real allocation
2993 * on the right.
2994 * Merge the new allocation with the right neighbor.
2995 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002996 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2997 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002998 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 new->br_blockcount + right.br_blockcount,
3000 right.br_state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003001 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003002
Christoph Hellwig6d045582017-04-11 16:45:54 -07003003 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003004 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003005 } else {
3006 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003007 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003008 right.br_startoff,
3009 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003010 right.br_blockcount, &i);
3011 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003012 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003013 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003014 error = xfs_bmbt_update(cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003015 new->br_startblock,
3016 new->br_blockcount +
3017 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003018 right.br_state);
3019 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003020 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003022 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
3024 case 0:
3025 /*
3026 * New allocation is not contiguous with another
3027 * real allocation.
3028 * Insert a new entry.
3029 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003030 xfs_iext_insert(ip, *idx, 1, new, state);
3031 XFS_IFORK_NEXT_SET(ip, whichfork,
3032 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3033 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003034 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003035 } else {
3036 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003037 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003038 new->br_startoff,
3039 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003040 new->br_blockcount, &i);
3041 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003042 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003043 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003044 cur->bc_rec.b.br_state = new->br_state;
3045 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003046 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003047 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003048 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003050 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003052
Darrick J. Wong9c194642016-08-03 12:16:05 +10003053 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003054 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10003055 if (error)
3056 goto done;
3057
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003058 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003059 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003060 int tmp_logflags; /* partial log flag return val */
3061
Christoph Hellwig6d045582017-04-11 16:45:54 -07003062 ASSERT(cur == NULL);
3063 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003064 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003065 *logflagsp |= tmp_logflags;
3066 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003067 if (error)
3068 goto done;
3069 }
3070
3071 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003072 if (cur)
3073 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003074
Christoph Hellwig6d045582017-04-11 16:45:54 -07003075 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003076done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07003077 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003078 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079}
3080
Nathan Scottdd9f4382006-01-11 15:28:28 +11003081/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003082 * Functions used in the extent read, allocate and remove paths
3083 */
3084
3085/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003086 * Adjust the size of the new extent based on di_extsize and rt extsize.
3087 */
Dave Chinner68988112013-08-12 20:49:42 +10003088int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003089xfs_bmap_extsize_align(
3090 xfs_mount_t *mp,
3091 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3092 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3093 xfs_extlen_t extsz, /* align to this extent size */
3094 int rt, /* is this a realtime inode? */
3095 int eof, /* is extent at end-of-file? */
3096 int delay, /* creating delalloc extent? */
3097 int convert, /* overwriting unwritten extent? */
3098 xfs_fileoff_t *offp, /* in/out: aligned offset */
3099 xfs_extlen_t *lenp) /* in/out: aligned length */
3100{
3101 xfs_fileoff_t orig_off; /* original offset */
3102 xfs_extlen_t orig_alen; /* original length */
3103 xfs_fileoff_t orig_end; /* original off+len */
3104 xfs_fileoff_t nexto; /* next file offset */
3105 xfs_fileoff_t prevo; /* previous file offset */
3106 xfs_fileoff_t align_off; /* temp for offset */
3107 xfs_extlen_t align_alen; /* temp for length */
3108 xfs_extlen_t temp; /* temp for calculations */
3109
3110 if (convert)
3111 return 0;
3112
3113 orig_off = align_off = *offp;
3114 orig_alen = align_alen = *lenp;
3115 orig_end = orig_off + orig_alen;
3116
3117 /*
3118 * If this request overlaps an existing extent, then don't
3119 * attempt to perform any additional alignment.
3120 */
3121 if (!delay && !eof &&
3122 (orig_off >= gotp->br_startoff) &&
3123 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3124 return 0;
3125 }
3126
3127 /*
3128 * If the file offset is unaligned vs. the extent size
3129 * we need to align it. This will be possible unless
3130 * the file was previously written with a kernel that didn't
3131 * perform this alignment, or if a truncate shot us in the
3132 * foot.
3133 */
3134 temp = do_mod(orig_off, extsz);
3135 if (temp) {
3136 align_alen += temp;
3137 align_off -= temp;
3138 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003139
3140 /* Same adjustment for the end of the requested area. */
3141 temp = (align_alen % extsz);
3142 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003143 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003144
3145 /*
3146 * For large extent hint sizes, the aligned extent might be larger than
3147 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3148 * the length back under MAXEXTLEN. The outer allocation loops handle
3149 * short allocation just fine, so it is safe to do this. We only want to
3150 * do it when we are forced to, though, because it means more allocation
3151 * operations are required.
3152 */
3153 while (align_alen > MAXEXTLEN)
3154 align_alen -= extsz;
3155 ASSERT(align_alen <= MAXEXTLEN);
3156
Nathan Scottdd9f4382006-01-11 15:28:28 +11003157 /*
3158 * If the previous block overlaps with this proposed allocation
3159 * then move the start forward without adjusting the length.
3160 */
3161 if (prevp->br_startoff != NULLFILEOFF) {
3162 if (prevp->br_startblock == HOLESTARTBLOCK)
3163 prevo = prevp->br_startoff;
3164 else
3165 prevo = prevp->br_startoff + prevp->br_blockcount;
3166 } else
3167 prevo = 0;
3168 if (align_off != orig_off && align_off < prevo)
3169 align_off = prevo;
3170 /*
3171 * If the next block overlaps with this proposed allocation
3172 * then move the start back without adjusting the length,
3173 * but not before offset 0.
3174 * This may of course make the start overlap previous block,
3175 * and if we hit the offset 0 limit then the next block
3176 * can still overlap too.
3177 */
3178 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3179 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3180 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3181 nexto = gotp->br_startoff + gotp->br_blockcount;
3182 else
3183 nexto = gotp->br_startoff;
3184 } else
3185 nexto = NULLFILEOFF;
3186 if (!eof &&
3187 align_off + align_alen != orig_end &&
3188 align_off + align_alen > nexto)
3189 align_off = nexto > align_alen ? nexto - align_alen : 0;
3190 /*
3191 * If we're now overlapping the next or previous extent that
3192 * means we can't fit an extsz piece in this hole. Just move
3193 * the start forward to the first valid spot and set
3194 * the length so we hit the end.
3195 */
3196 if (align_off != orig_off && align_off < prevo)
3197 align_off = prevo;
3198 if (align_off + align_alen != orig_end &&
3199 align_off + align_alen > nexto &&
3200 nexto != NULLFILEOFF) {
3201 ASSERT(nexto > prevo);
3202 align_alen = nexto - align_off;
3203 }
3204
3205 /*
3206 * If realtime, and the result isn't a multiple of the realtime
3207 * extent size we need to remove blocks until it is.
3208 */
3209 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3210 /*
3211 * We're not covering the original request, or
3212 * we won't be able to once we fix the length.
3213 */
3214 if (orig_off < align_off ||
3215 orig_end > align_off + align_alen ||
3216 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003217 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003218 /*
3219 * Try to fix it by moving the start up.
3220 */
3221 if (align_off + temp <= orig_off) {
3222 align_alen -= temp;
3223 align_off += temp;
3224 }
3225 /*
3226 * Try to fix it by moving the end in.
3227 */
3228 else if (align_off + align_alen - temp >= orig_end)
3229 align_alen -= temp;
3230 /*
3231 * Set the start to the minimum then trim the length.
3232 */
3233 else {
3234 align_alen -= orig_off - align_off;
3235 align_off = orig_off;
3236 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3237 }
3238 /*
3239 * Result doesn't cover the request, fail it.
3240 */
3241 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003242 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003243 } else {
3244 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003245 /* see MAXEXTLEN handling above */
3246 ASSERT(orig_end <= align_off + align_alen ||
3247 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003248 }
3249
3250#ifdef DEBUG
3251 if (!eof && gotp->br_startoff != NULLFILEOFF)
3252 ASSERT(align_off + align_alen <= gotp->br_startoff);
3253 if (prevp->br_startoff != NULLFILEOFF)
3254 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3255#endif
3256
3257 *lenp = align_alen;
3258 *offp = align_off;
3259 return 0;
3260}
3261
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262#define XFS_ALLOC_GAP_UNITS 4
3263
Dave Chinner68988112013-08-12 20:49:42 +10003264void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003265xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003266 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267{
3268 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3270 xfs_mount_t *mp; /* mount point structure */
3271 int nullfb; /* true if ap->firstblock isn't set */
3272 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
3274#define ISVALID(x,y) \
3275 (rt ? \
3276 (x) < mp->m_sb.sb_rblocks : \
3277 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3278 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3279 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3280
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003282 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003283 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3284 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003285 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 /*
3287 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003288 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003290 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3291 !isnullstartblock(ap->prev.br_startblock) &&
3292 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3293 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003294 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 /*
3296 * Adjust for the gap between prevp and us.
3297 */
Dave Chinner3a756672011-09-18 20:40:58 +00003298 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003299 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003301 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3302 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 }
3304 /*
3305 * If not at eof, then compare the two neighbor blocks.
3306 * Figure out whether either one gives us a good starting point,
3307 * and pick the better one.
3308 */
3309 else if (!ap->eof) {
3310 xfs_fsblock_t gotbno; /* right side block number */
3311 xfs_fsblock_t gotdiff=0; /* right side difference */
3312 xfs_fsblock_t prevbno; /* left side block number */
3313 xfs_fsblock_t prevdiff=0; /* left side difference */
3314
3315 /*
3316 * If there's a previous (left) block, select a requested
3317 * start block based on it.
3318 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003319 if (ap->prev.br_startoff != NULLFILEOFF &&
3320 !isnullstartblock(ap->prev.br_startblock) &&
3321 (prevbno = ap->prev.br_startblock +
3322 ap->prev.br_blockcount) &&
3323 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 /*
3325 * Calculate gap to end of previous block.
3326 */
Dave Chinner3a756672011-09-18 20:40:58 +00003327 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003328 (ap->prev.br_startoff +
3329 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 /*
3331 * Figure the startblock based on the previous block's
3332 * end and the gap size.
3333 * Heuristic!
3334 * If the gap is large relative to the piece we're
3335 * allocating, or using it gives us an invalid block
3336 * number, then just use the end of the previous block.
3337 */
Dave Chinner3a756672011-09-18 20:40:58 +00003338 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003340 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341 prevbno += adjust;
3342 else
3343 prevdiff += adjust;
3344 /*
3345 * If the firstblock forbids it, can't use it,
3346 * must use default.
3347 */
3348 if (!rt && !nullfb &&
3349 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3350 prevbno = NULLFSBLOCK;
3351 }
3352 /*
3353 * No previous block or can't follow it, just default.
3354 */
3355 else
3356 prevbno = NULLFSBLOCK;
3357 /*
3358 * If there's a following (right) block, select a requested
3359 * start block based on it.
3360 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003361 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 /*
3363 * Calculate gap to start of next block.
3364 */
Dave Chinner3a756672011-09-18 20:40:58 +00003365 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 /*
3367 * Figure the startblock based on the next block's
3368 * start and the gap size.
3369 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003370 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 /*
3372 * Heuristic!
3373 * If the gap is large relative to the piece we're
3374 * allocating, or using it gives us an invalid block
3375 * number, then just use the start of the next block
3376 * offset by our length.
3377 */
Dave Chinner3a756672011-09-18 20:40:58 +00003378 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 ISVALID(gotbno - gotdiff, gotbno))
3380 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003381 else if (ISVALID(gotbno - ap->length, gotbno)) {
3382 gotbno -= ap->length;
3383 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 } else
3385 gotdiff += adjust;
3386 /*
3387 * If the firstblock forbids it, can't use it,
3388 * must use default.
3389 */
3390 if (!rt && !nullfb &&
3391 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3392 gotbno = NULLFSBLOCK;
3393 }
3394 /*
3395 * No next block, just default.
3396 */
3397 else
3398 gotbno = NULLFSBLOCK;
3399 /*
3400 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003401 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 */
3403 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003404 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003406 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003408 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003410#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003411}
3412
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003413static int
3414xfs_bmap_longest_free_extent(
3415 struct xfs_trans *tp,
3416 xfs_agnumber_t ag,
3417 xfs_extlen_t *blen,
3418 int *notinit)
3419{
3420 struct xfs_mount *mp = tp->t_mountp;
3421 struct xfs_perag *pag;
3422 xfs_extlen_t longest;
3423 int error = 0;
3424
3425 pag = xfs_perag_get(mp, ag);
3426 if (!pag->pagf_init) {
3427 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3428 if (error)
3429 goto out;
3430
3431 if (!pag->pagf_init) {
3432 *notinit = 1;
3433 goto out;
3434 }
3435 }
3436
Dave Chinner50adbcb2015-06-22 10:04:31 +10003437 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003438 xfs_alloc_min_freelist(mp, pag),
3439 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003440 if (*blen < longest)
3441 *blen = longest;
3442
3443out:
3444 xfs_perag_put(pag);
3445 return error;
3446}
3447
3448static void
3449xfs_bmap_select_minlen(
3450 struct xfs_bmalloca *ap,
3451 struct xfs_alloc_arg *args,
3452 xfs_extlen_t *blen,
3453 int notinit)
3454{
3455 if (notinit || *blen < ap->minlen) {
3456 /*
3457 * Since we did a BUF_TRYLOCK above, it is possible that
3458 * there is space for this request.
3459 */
3460 args->minlen = ap->minlen;
3461 } else if (*blen < args->maxlen) {
3462 /*
3463 * If the best seen length is less than the request length,
3464 * use the best as the minimum.
3465 */
3466 args->minlen = *blen;
3467 } else {
3468 /*
3469 * Otherwise we've seen an extent as big as maxlen, use that
3470 * as the minimum.
3471 */
3472 args->minlen = args->maxlen;
3473 }
3474}
3475
Nathan Scotta365bdd2006-03-14 13:34:16 +11003476STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003477xfs_bmap_btalloc_nullfb(
3478 struct xfs_bmalloca *ap,
3479 struct xfs_alloc_arg *args,
3480 xfs_extlen_t *blen)
3481{
3482 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003483 xfs_agnumber_t ag, startag;
3484 int notinit = 0;
3485 int error;
3486
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003487 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003488 args->total = ap->total;
3489
Christoph Hellwigc467c042010-02-15 23:34:42 +00003490 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3491 if (startag == NULLAGNUMBER)
3492 startag = ag = 0;
3493
Dave Chinner14b064c2011-01-27 12:16:28 +11003494 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003495 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3496 &notinit);
3497 if (error)
3498 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003499
Christoph Hellwigc467c042010-02-15 23:34:42 +00003500 if (++ag == mp->m_sb.sb_agcount)
3501 ag = 0;
3502 if (ag == startag)
3503 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003504 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003505
3506 xfs_bmap_select_minlen(ap, args, blen, notinit);
3507 return 0;
3508}
3509
3510STATIC int
3511xfs_bmap_btalloc_filestreams(
3512 struct xfs_bmalloca *ap,
3513 struct xfs_alloc_arg *args,
3514 xfs_extlen_t *blen)
3515{
3516 struct xfs_mount *mp = ap->ip->i_mount;
3517 xfs_agnumber_t ag;
3518 int notinit = 0;
3519 int error;
3520
3521 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3522 args->total = ap->total;
3523
3524 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3525 if (ag == NULLAGNUMBER)
3526 ag = 0;
3527
3528 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3529 if (error)
3530 return error;
3531
3532 if (*blen < args->maxlen) {
3533 error = xfs_filestream_new_ag(ap, &ag);
3534 if (error)
3535 return error;
3536
3537 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3538 &notinit);
3539 if (error)
3540 return error;
3541
3542 }
3543
3544 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003545
3546 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003547 * Set the failure fallback case to look in the selected AG as stream
3548 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003549 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003550 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003551 return 0;
3552}
3553
3554STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003555xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003556 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003557{
3558 xfs_mount_t *mp; /* mount point structure */
3559 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003560 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003561 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003562 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003563 xfs_alloc_arg_t args;
3564 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003565 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003566 int nullfb; /* true if ap->firstblock isn't set */
3567 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003568 int tryagain;
3569 int error;
Dave Chinner33177f02013-12-12 16:34:36 +11003570 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003571
Dave Chinnera99ebf42011-12-01 11:24:20 +00003572 ASSERT(ap->length);
3573
Nathan Scotta365bdd2006-03-14 13:34:16 +11003574 mp = ap->ip->i_mount;
Dave Chinner33177f02013-12-12 16:34:36 +11003575
3576 /* stripe alignment for allocation is determined by mount parameters */
3577 stripe_align = 0;
3578 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3579 stripe_align = mp->m_swidth;
3580 else if (mp->m_dalign)
3581 stripe_align = mp->m_dalign;
3582
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003583 if (ap->flags & XFS_BMAPI_COWFORK)
3584 align = xfs_get_cowextsz_hint(ap->ip);
3585 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003586 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003587 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003588 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003589 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003590 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003591 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003592 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003593 }
Dave Chinner33177f02013-12-12 16:34:36 +11003594
3595
Dave Chinner0937e0f2011-09-18 20:40:57 +00003596 nullfb = *ap->firstblock == NULLFSBLOCK;
3597 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003598 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003599 if (xfs_alloc_is_userdata(ap->datatype) &&
3600 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003601 ag = xfs_filestream_lookup_ag(ap->ip);
3602 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003603 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003604 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003605 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003606 }
3607 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003608 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003609
3610 xfs_bmap_adjacent(ap);
3611
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003613 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 * it's in the right allocation group.
3615 */
Dave Chinner3a756672011-09-18 20:40:58 +00003616 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 ;
3618 else
Dave Chinner3a756672011-09-18 20:40:58 +00003619 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 * Normal allocation, done through xfs_alloc_vextent.
3622 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003623 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003624 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003625 args.tp = ap->tp;
3626 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003627 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003628 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003629
3630 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003631 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003632 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003633 blen = 0;
3634 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003635 /*
3636 * Search for an allocation group with a single extent large
3637 * enough for the request. If one isn't found, then adjust
3638 * the minimum allocation size to the largest space found.
3639 */
Dave Chinner292378e2016-09-26 08:21:28 +10003640 if (xfs_alloc_is_userdata(ap->datatype) &&
3641 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003642 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3643 else
3644 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003645 if (error)
3646 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003647 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003648 if (xfs_inode_is_filestream(ap->ip))
3649 args.type = XFS_ALLOCTYPE_FIRST_AG;
3650 else
3651 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003652 args.total = args.minlen = ap->minlen;
3653 } else {
3654 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3655 args.total = ap->total;
3656 args.minlen = ap->minlen;
3657 }
David Chinner957d0eb2007-06-18 16:50:37 +10003658 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003659 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003660 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003661 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003662 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003663 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003664 args.prod = 1;
3665 args.mod = 0;
3666 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003667 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003668 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003669 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3670 }
3671 /*
3672 * If we are not low on available data blocks, and the
3673 * underlying logical volume manager is a stripe, and
3674 * the file offset is zero then try to allocate data
3675 * blocks on stripe unit boundary.
3676 * NOTE: ap->aeof is only set if the allocation length
3677 * is >= the stripe unit and the allocation offset is
3678 * at the end of file.
3679 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003680 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003681 if (!ap->offset) {
Dave Chinner33177f02013-12-12 16:34:36 +11003682 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003683 atype = args.type;
3684 isaligned = 1;
3685 /*
3686 * Adjust for alignment
3687 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003688 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003689 args.minlen = blen - args.alignment;
3690 args.minalignslop = 0;
3691 } else {
3692 /*
3693 * First try an exact bno allocation.
3694 * If it fails then do a near or start bno
3695 * allocation with alignment turned on.
3696 */
3697 atype = args.type;
3698 tryagain = 1;
3699 args.type = XFS_ALLOCTYPE_THIS_BNO;
3700 args.alignment = 1;
3701 /*
3702 * Compute the minlen+alignment for the
3703 * next case. Set slop so that the value
3704 * of minlen+alignment+slop doesn't go up
3705 * between the calls.
3706 */
Dave Chinner33177f02013-12-12 16:34:36 +11003707 if (blen > stripe_align && blen <= args.maxlen)
3708 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003709 else
3710 nextminlen = args.minlen;
Dave Chinner33177f02013-12-12 16:34:36 +11003711 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003712 args.minalignslop =
Dave Chinner33177f02013-12-12 16:34:36 +11003713 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003714 args.minlen - 1;
3715 else
3716 args.minalignslop = 0;
3717 }
3718 } else {
3719 args.alignment = 1;
3720 args.minalignslop = 0;
3721 }
3722 args.minleft = ap->minleft;
3723 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003724 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003725 args.datatype = ap->datatype;
3726 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003727 args.ip = ap->ip;
3728
3729 error = xfs_alloc_vextent(&args);
3730 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003731 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003732
Nathan Scotta365bdd2006-03-14 13:34:16 +11003733 if (tryagain && args.fsbno == NULLFSBLOCK) {
3734 /*
3735 * Exact allocation failed. Now try with alignment
3736 * turned on.
3737 */
3738 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003739 args.fsbno = ap->blkno;
Dave Chinner33177f02013-12-12 16:34:36 +11003740 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003741 args.minlen = nextminlen;
3742 args.minalignslop = 0;
3743 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 if ((error = xfs_alloc_vextent(&args)))
3745 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003746 }
3747 if (isaligned && args.fsbno == NULLFSBLOCK) {
3748 /*
3749 * allocation failed, so turn off alignment and
3750 * try again.
3751 */
3752 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003753 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003754 args.alignment = 0;
3755 if ((error = xfs_alloc_vextent(&args)))
3756 return error;
3757 }
3758 if (args.fsbno == NULLFSBLOCK && nullfb &&
3759 args.minlen > ap->minlen) {
3760 args.minlen = ap->minlen;
3761 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003762 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003763 if ((error = xfs_alloc_vextent(&args)))
3764 return error;
3765 }
3766 if (args.fsbno == NULLFSBLOCK && nullfb) {
3767 args.fsbno = 0;
3768 args.type = XFS_ALLOCTYPE_FIRST_AG;
3769 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003770 if ((error = xfs_alloc_vextent(&args)))
3771 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003772 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003773 }
3774 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003775 /*
3776 * check the allocation happened at the same or higher AG than
3777 * the first block that was allocated.
3778 */
3779 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003780 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3781 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003782
Dave Chinner3a756672011-09-18 20:40:58 +00003783 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003784 if (*ap->firstblock == NULLFSBLOCK)
3785 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003786 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003787 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003788 if (!(ap->flags & XFS_BMAPI_COWFORK))
3789 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003790 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3791 if (ap->wasdel)
3792 ap->ip->i_delayed_blks -= args.len;
3793 /*
3794 * Adjust the disk quota also. This was reserved
3795 * earlier.
3796 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003797 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003798 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3799 XFS_TRANS_DQ_BCOUNT,
3800 (long) args.len);
3801 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003802 ap->blkno = NULLFSBLOCK;
3803 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 }
3805 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003806}
3807
3808/*
3809 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3810 * It figures out where to ask the underlying allocator to put the new extent.
3811 */
3812STATIC int
3813xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003814 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003815{
Dave Chinner292378e2016-09-26 08:21:28 +10003816 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3817 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003818 return xfs_bmap_rtalloc(ap);
3819 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003822/* Trim extent to fit a logical block range. */
3823void
3824xfs_trim_extent(
3825 struct xfs_bmbt_irec *irec,
3826 xfs_fileoff_t bno,
3827 xfs_filblks_t len)
3828{
3829 xfs_fileoff_t distance;
3830 xfs_fileoff_t end = bno + len;
3831
3832 if (irec->br_startoff + irec->br_blockcount <= bno ||
3833 irec->br_startoff >= end) {
3834 irec->br_blockcount = 0;
3835 return;
3836 }
3837
3838 if (irec->br_startoff < bno) {
3839 distance = bno - irec->br_startoff;
3840 if (isnullstartblock(irec->br_startblock))
3841 irec->br_startblock = DELAYSTARTBLOCK;
3842 if (irec->br_startblock != DELAYSTARTBLOCK &&
3843 irec->br_startblock != HOLESTARTBLOCK)
3844 irec->br_startblock += distance;
3845 irec->br_startoff += distance;
3846 irec->br_blockcount -= distance;
3847 }
3848
3849 if (end < irec->br_startoff + irec->br_blockcount) {
3850 distance = irec->br_startoff + irec->br_blockcount - end;
3851 irec->br_blockcount -= distance;
3852 }
3853}
3854
Brian Foster40214d12017-10-13 09:47:46 -07003855/* trim extent to within eof */
3856void
3857xfs_trim_extent_eof(
3858 struct xfs_bmbt_irec *irec,
3859 struct xfs_inode *ip)
3860
3861{
3862 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3863 i_size_read(VFS_I(ip))));
3864}
3865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003867 * Trim the returned map to the required bounds
3868 */
3869STATIC void
3870xfs_bmapi_trim_map(
3871 struct xfs_bmbt_irec *mval,
3872 struct xfs_bmbt_irec *got,
3873 xfs_fileoff_t *bno,
3874 xfs_filblks_t len,
3875 xfs_fileoff_t obno,
3876 xfs_fileoff_t end,
3877 int n,
3878 int flags)
3879{
3880 if ((flags & XFS_BMAPI_ENTIRE) ||
3881 got->br_startoff + got->br_blockcount <= obno) {
3882 *mval = *got;
3883 if (isnullstartblock(got->br_startblock))
3884 mval->br_startblock = DELAYSTARTBLOCK;
3885 return;
3886 }
3887
3888 if (obno > *bno)
3889 *bno = obno;
3890 ASSERT((*bno >= obno) || (n == 0));
3891 ASSERT(*bno < end);
3892 mval->br_startoff = *bno;
3893 if (isnullstartblock(got->br_startblock))
3894 mval->br_startblock = DELAYSTARTBLOCK;
3895 else
3896 mval->br_startblock = got->br_startblock +
3897 (*bno - got->br_startoff);
3898 /*
3899 * Return the minimum of what we got and what we asked for for
3900 * the length. We can use the len variable here because it is
3901 * modified below and we could have been there before coming
3902 * here if the first part of the allocation didn't overlap what
3903 * was asked for.
3904 */
3905 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3906 got->br_blockcount - (*bno - got->br_startoff));
3907 mval->br_state = got->br_state;
3908 ASSERT(mval->br_blockcount <= len);
3909 return;
3910}
3911
3912/*
3913 * Update and validate the extent map to return
3914 */
3915STATIC void
3916xfs_bmapi_update_map(
3917 struct xfs_bmbt_irec **map,
3918 xfs_fileoff_t *bno,
3919 xfs_filblks_t *len,
3920 xfs_fileoff_t obno,
3921 xfs_fileoff_t end,
3922 int *n,
3923 int flags)
3924{
3925 xfs_bmbt_irec_t *mval = *map;
3926
3927 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3928 ((mval->br_startoff + mval->br_blockcount) <= end));
3929 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3930 (mval->br_startoff < obno));
3931
3932 *bno = mval->br_startoff + mval->br_blockcount;
3933 *len = end - *bno;
3934 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3935 /* update previous map with new information */
3936 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3937 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3938 ASSERT(mval->br_state == mval[-1].br_state);
3939 mval[-1].br_blockcount = mval->br_blockcount;
3940 mval[-1].br_state = mval->br_state;
3941 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3942 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3943 mval[-1].br_startblock != HOLESTARTBLOCK &&
3944 mval->br_startblock == mval[-1].br_startblock +
3945 mval[-1].br_blockcount &&
3946 ((flags & XFS_BMAPI_IGSTATE) ||
3947 mval[-1].br_state == mval->br_state)) {
3948 ASSERT(mval->br_startoff ==
3949 mval[-1].br_startoff + mval[-1].br_blockcount);
3950 mval[-1].br_blockcount += mval->br_blockcount;
3951 } else if (*n > 0 &&
3952 mval->br_startblock == DELAYSTARTBLOCK &&
3953 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3954 mval->br_startoff ==
3955 mval[-1].br_startoff + mval[-1].br_blockcount) {
3956 mval[-1].br_blockcount += mval->br_blockcount;
3957 mval[-1].br_state = mval->br_state;
3958 } else if (!((*n == 0) &&
3959 ((mval->br_startoff + mval->br_blockcount) <=
3960 obno))) {
3961 mval++;
3962 (*n)++;
3963 }
3964 *map = mval;
3965}
3966
3967/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003968 * Map file blocks to filesystem blocks without allocation.
3969 */
3970int
3971xfs_bmapi_read(
3972 struct xfs_inode *ip,
3973 xfs_fileoff_t bno,
3974 xfs_filblks_t len,
3975 struct xfs_bmbt_irec *mval,
3976 int *nmap,
3977 int flags)
3978{
3979 struct xfs_mount *mp = ip->i_mount;
3980 struct xfs_ifork *ifp;
3981 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003982 xfs_fileoff_t obno;
3983 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003984 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003985 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003986 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003987 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003988 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003989
3990 ASSERT(*nmap >= 1);
3991 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003992 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003993 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003994
3995 if (unlikely(XFS_TEST_ERROR(
3996 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3997 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003998 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003999 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004000 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004001 }
4002
4003 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004004 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004005
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004006 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004007
4008 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004009
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004010 /* No CoW fork? Return a hole. */
4011 if (whichfork == XFS_COW_FORK && !ifp) {
4012 mval->br_startoff = bno;
4013 mval->br_startblock = HOLESTARTBLOCK;
4014 mval->br_blockcount = len;
4015 mval->br_state = XFS_EXT_NORM;
4016 *nmap = 1;
4017 return 0;
4018 }
4019
Dave Chinner5c8ed202011-09-18 20:40:45 +00004020 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4021 error = xfs_iread_extents(NULL, ip, whichfork);
4022 if (error)
4023 return error;
4024 }
4025
Christoph Hellwig334f3422016-11-24 11:39:43 +11004026 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4027 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004028 end = bno + len;
4029 obno = bno;
4030
4031 while (bno < end && n < *nmap) {
4032 /* Reading past eof, act as though there's a hole up to end. */
4033 if (eof)
4034 got.br_startoff = end;
4035 if (got.br_startoff > bno) {
4036 /* Reading in a hole. */
4037 mval->br_startoff = bno;
4038 mval->br_startblock = HOLESTARTBLOCK;
4039 mval->br_blockcount =
4040 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4041 mval->br_state = XFS_EXT_NORM;
4042 bno += mval->br_blockcount;
4043 len -= mval->br_blockcount;
4044 mval++;
4045 n++;
4046 continue;
4047 }
4048
4049 /* set up the extent map to return. */
4050 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4051 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4052
4053 /* If we're done, stop now. */
4054 if (bno >= end || n >= *nmap)
4055 break;
4056
4057 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004058 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4059 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004060 }
4061 *nmap = n;
4062 return 0;
4063}
4064
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004065/*
4066 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4067 * global pool and the extent inserted into the inode in-core extent tree.
4068 *
4069 * On entry, got refers to the first extent beyond the offset of the extent to
4070 * allocate or eof is specified if no such extent exists. On return, got refers
4071 * to the extent record that was inserted to the inode fork.
4072 *
4073 * Note that the allocated extent may have been merged with contiguous extents
4074 * during insertion into the inode fork. Thus, got does not reflect the current
4075 * state of the inode fork on return. If necessary, the caller can use lastx to
4076 * look up the updated record in the inode fork.
4077 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10004078int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004079xfs_bmapi_reserve_delalloc(
4080 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004081 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004082 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004083 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004084 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004085 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004086 xfs_extnum_t *lastx,
4087 int eof)
4088{
4089 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004090 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004091 xfs_extlen_t alen;
4092 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004093 char rt = XFS_IS_REALTIME_INODE(ip);
4094 xfs_extlen_t extsz;
4095 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004096 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004097
Brian Foster974ae922016-11-28 14:57:42 +11004098 /*
4099 * Cap the alloc length. Keep track of prealloc so we know whether to
4100 * tag the inode before we return.
4101 */
4102 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004103 if (!eof)
4104 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004105 if (prealloc && alen >= len)
4106 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004107
4108 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004109 if (whichfork == XFS_COW_FORK)
4110 extsz = xfs_get_cowextsz_hint(ip);
4111 else
4112 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004113 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004114 struct xfs_bmbt_irec prev;
4115
4116 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4117 prev.br_startoff = NULLFILEOFF;
4118
4119 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004120 1, 0, &aoff, &alen);
4121 ASSERT(!error);
4122 }
4123
4124 if (rt)
4125 extsz = alen / mp->m_sb.sb_rextsize;
4126
4127 /*
4128 * Make a transaction-less quota reservation for delayed allocation
4129 * blocks. This number gets adjusted later. We return if we haven't
4130 * allocated blocks already inside this loop.
4131 */
4132 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4133 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4134 if (error)
4135 return error;
4136
4137 /*
4138 * Split changing sb for alen and indlen since they could be coming
4139 * from different places.
4140 */
4141 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4142 ASSERT(indlen > 0);
4143
4144 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004145 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004146 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004147 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004148 }
4149
4150 if (error)
4151 goto out_unreserve_quota;
4152
Dave Chinner0d485ad2015-02-23 21:22:03 +11004153 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004154 if (error)
4155 goto out_unreserve_blocks;
4156
4157
4158 ip->i_delayed_blks += alen;
4159
4160 got->br_startoff = aoff;
4161 got->br_startblock = nullstartblock(indlen);
4162 got->br_blockcount = alen;
4163 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004164
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004165 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004166
Brian Foster974ae922016-11-28 14:57:42 +11004167 /*
4168 * Tag the inode if blocks were preallocated. Note that COW fork
4169 * preallocation can occur at the start or end of the extent, even when
4170 * prealloc == 0, so we must also check the aligned offset and length.
4171 */
4172 if (whichfork == XFS_DATA_FORK && prealloc)
4173 xfs_inode_set_eofblocks_tag(ip);
4174 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4175 xfs_inode_set_cowblocks_tag(ip);
4176
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004177 return 0;
4178
4179out_unreserve_blocks:
4180 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004181 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004182 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004183 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004184out_unreserve_quota:
4185 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004186 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004187 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4188 return error;
4189}
4190
Dave Chinnercf11da92014-07-15 07:08:24 +10004191static int
4192xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004193 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004194{
4195 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004196 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004197 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004198 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004199 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004200
Dave Chinnera99ebf42011-12-01 11:24:20 +00004201 ASSERT(bma->length > 0);
4202
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004203 /*
4204 * For the wasdelay case, we could also just allocate the stuff asked
4205 * for in this bmap call but that wouldn't be as good.
4206 */
4207 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004208 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4209 bma->offset = bma->got.br_startoff;
Christoph Hellwig0e8d6302016-11-24 11:40:32 +11004210 if (bma->idx) {
Dave Chinnere0c3da52011-09-18 20:41:01 +00004211 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004212 &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004213 }
4214 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004215 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004216 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004217 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004218 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004219 }
4220
4221 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004222 * Set the data type being allocated. For the data fork, the first data
4223 * in the file is treated differently to all other allocations. For the
4224 * attribute fork, we only need to ensure the allocated range is not on
4225 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004226 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004227 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004228 bma->datatype = XFS_ALLOC_NOBUSY;
4229 if (whichfork == XFS_DATA_FORK) {
4230 if (bma->offset == 0)
4231 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4232 else
4233 bma->datatype |= XFS_ALLOC_USERDATA;
4234 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004235 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004236 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004237 }
4238
Dave Chinnere04426b2012-10-05 11:06:59 +10004239 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004240
4241 /*
4242 * Only want to do the alignment at the eof if it is userdata and
4243 * allocation length is larger than a stripe unit.
4244 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004245 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004246 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004247 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004248 if (error)
4249 return error;
4250 }
4251
4252 error = xfs_bmap_alloc(bma);
4253 if (error)
4254 return error;
4255
Dave Chinner29c8d172011-09-18 20:41:00 +00004256 if (bma->cur)
4257 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004258 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004259 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004260 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4261 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4262 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004263 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004264 }
4265 /*
4266 * Bump the number of extents we've allocated
4267 * in this call.
4268 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004269 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004270
Dave Chinner29c8d172011-09-18 20:41:00 +00004271 if (bma->cur)
4272 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004273 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4274
Dave Chinner963c30c2011-09-18 20:40:59 +00004275 bma->got.br_startoff = bma->offset;
4276 bma->got.br_startblock = bma->blkno;
4277 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004278 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004279
4280 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004281 * In the data fork, a wasdelay extent has been initialized, so
4282 * shouldn't be flagged as unwritten.
4283 *
4284 * For the cow fork, however, we convert delalloc reservations
4285 * (extents allocated for speculative preallocation) to
4286 * allocated unwritten extents, and only convert the unwritten
4287 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004288 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004289 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4290 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004291 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004292 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004293
Christoph Hellwigc6534242011-09-18 20:41:05 +00004294 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004295 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004296 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004297 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4298 whichfork, &bma->idx, &bma->cur, &bma->got,
4299 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004300
Christoph Hellwigc315c902011-09-18 20:41:02 +00004301 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004302 if (error)
4303 return error;
4304
4305 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004306 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4307 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4308 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004309 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004310 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004311
Dave Chinner963c30c2011-09-18 20:40:59 +00004312 ASSERT(bma->got.br_startoff <= bma->offset);
4313 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4314 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004315 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4316 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004317 return 0;
4318}
4319
Dave Chinnerb447fe52011-09-18 20:40:51 +00004320STATIC int
4321xfs_bmapi_convert_unwritten(
4322 struct xfs_bmalloca *bma,
4323 struct xfs_bmbt_irec *mval,
4324 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004325 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004326{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004327 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004328 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004329 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004330 int error;
4331
Dave Chinnerb447fe52011-09-18 20:40:51 +00004332 /* check if we need to do unwritten->real conversion */
4333 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4334 (flags & XFS_BMAPI_PREALLOC))
4335 return 0;
4336
4337 /* check if we need to do real->unwritten conversion */
4338 if (mval->br_state == XFS_EXT_NORM &&
4339 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4340 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4341 return 0;
4342
4343 /*
4344 * Modify (by adding) the state flag, if writing.
4345 */
4346 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004347 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4348 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004349 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004350 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004351 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004352 }
4353 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4354 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4355
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004356 /*
4357 * Before insertion into the bmbt, zero the range being converted
4358 * if required.
4359 */
4360 if (flags & XFS_BMAPI_ZERO) {
4361 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4362 mval->br_blockcount);
4363 if (error)
4364 return error;
4365 }
4366
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004367 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4368 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004369 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004370 /*
4371 * Log the inode core unconditionally in the unwritten extent conversion
4372 * path because the conversion might not have done so (e.g., if the
4373 * extent count hasn't changed). We need to make sure the inode is dirty
4374 * in the transaction for the sake of fsync(), even if nothing has
4375 * changed, because fsync() will not force the log for this transaction
4376 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004377 *
4378 * Note: If we're only converting cow fork extents, there aren't
4379 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004380 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004381 if (whichfork != XFS_COW_FORK)
4382 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004383 if (error)
4384 return error;
4385
4386 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004387 * Update our extent pointer, given that
4388 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4389 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004390 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004391 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004392
4393 /*
4394 * We may have combined previously unwritten space with written space,
4395 * so generate another request.
4396 */
4397 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004398 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004399 return 0;
4400}
4401
Christoph Hellwig44032802011-09-18 20:40:48 +00004402/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004403 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4404 * extent state if necessary. Details behaviour is controlled by the flags
4405 * parameter. Only allocates blocks from a single allocation group, to avoid
4406 * locking problems.
4407 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 * The returned value in "firstblock" from the first call in a transaction
4409 * must be remembered and presented to subsequent calls in "firstblock".
4410 * An upper bound for the number of blocks to be allocated is supplied to
4411 * the first call in "total"; if no allocation group has that many free
4412 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4413 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004414int
4415xfs_bmapi_write(
4416 struct xfs_trans *tp, /* transaction pointer */
4417 struct xfs_inode *ip, /* incore inode */
4418 xfs_fileoff_t bno, /* starting file offs. mapped */
4419 xfs_filblks_t len, /* length to map in file */
4420 int flags, /* XFS_BMAPI_... */
4421 xfs_fsblock_t *firstblock, /* first allocated block
4422 controls a.g. for allocs */
4423 xfs_extlen_t total, /* total blocks needed */
4424 struct xfs_bmbt_irec *mval, /* output: map values */
4425 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004426 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004428 struct xfs_mount *mp = ip->i_mount;
4429 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004430 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004431 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004432 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004433 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004434 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004435 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004436 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004439 xfs_fileoff_t orig_bno; /* original block number value */
4440 int orig_flags; /* original flags arg value */
4441 xfs_filblks_t orig_len; /* original value of len arg */
4442 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4443 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
4445 orig_bno = bno;
4446 orig_len = len;
4447 orig_flags = flags;
4448 orig_mval = mval;
4449 orig_nmap = *nmap;
4450#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004451 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004454 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4455 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004456 ASSERT(tp != NULL ||
4457 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4458 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004459 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004460 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004461 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004462 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004463
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004464 /* zeroing is for currently only for data extents, not metadata */
4465 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4466 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4467 /*
4468 * we can allocate unwritten extents or pre-zero allocated blocks,
4469 * but it makes no sense to do both at once. This would result in
4470 * zeroing the unwritten extent twice, but it still being an
4471 * unwritten extent....
4472 */
4473 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4474 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4475
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 if (unlikely(XFS_TEST_ERROR(
4477 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004478 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004479 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004480 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004481 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004485 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004486
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004488
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004489 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004490
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004491 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004493 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004495 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004496 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004497 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004498 }
4499
4500 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4501 error = xfs_iread_extents(tp, ip, whichfork);
4502 if (error)
4503 goto error0;
4504 }
4505
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 n = 0;
4507 end = bno + len;
4508 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004509
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004510 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4511 eof = true;
4512 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4513 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004514 bma.tp = tp;
4515 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004516 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004517 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004518 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004519 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004522 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004523
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004524 /* in hole or beyoned EOF? */
4525 if (eof || bma.got.br_startoff > bno) {
4526 if (flags & XFS_BMAPI_DELALLOC) {
4527 /*
4528 * For the COW fork we can reasonably get a
4529 * request for converting an extent that races
4530 * with other threads already having converted
4531 * part of it, as there converting COW to
4532 * regular blocks is not protected using the
4533 * IOLOCK.
4534 */
4535 ASSERT(flags & XFS_BMAPI_COWFORK);
4536 if (!(flags & XFS_BMAPI_COWFORK)) {
4537 error = -EIO;
4538 goto error0;
4539 }
4540
4541 if (eof || bno >= end)
4542 break;
4543 } else {
4544 need_alloc = true;
4545 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004546 } else if (isnullstartblock(bma.got.br_startblock)) {
4547 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004548 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004549
4550 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 * First, deal with the hole before the allocated space
4552 * that we found, if any.
4553 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004554 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004555 bma.eof = eof;
4556 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4557 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004558 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004559 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004560
Dave Chinnera99ebf42011-12-01 11:24:20 +00004561 /*
4562 * There's a 32/64 bit type mismatch between the
4563 * allocation length request (which can be 64 bits in
4564 * length) and the bma length request, which is
4565 * xfs_extlen_t and therefore 32 bits. Hence we have to
4566 * check for 32-bit overflows and handle them here.
4567 */
4568 if (len > (xfs_filblks_t)MAXEXTLEN)
4569 bma.length = MAXEXTLEN;
4570 else
4571 bma.length = len;
4572
4573 ASSERT(len > 0);
4574 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004575 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 if (error)
4577 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004578 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004579 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004580
4581 /*
4582 * If this is a CoW allocation, record the data in
4583 * the refcount btree for orphan recovery.
4584 */
4585 if (whichfork == XFS_COW_FORK) {
4586 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4587 bma.blkno, bma.length);
4588 if (error)
4589 goto error0;
4590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004592
Dave Chinneraef9a892011-09-18 20:40:44 +00004593 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004594 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4595 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596
Dave Chinnerb447fe52011-09-18 20:40:51 +00004597 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004598 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004599 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004600 continue;
4601 if (error)
4602 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Dave Chinneraef9a892011-09-18 20:40:44 +00004604 /* update the extent map to return */
4605 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4606
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 /*
4608 * If we're done, stop now. Stop when we've allocated
4609 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4610 * the transaction may get too big.
4611 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004612 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004614
4615 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004616 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004617 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4618 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 /*
4623 * Transform from btree to extents, give it cur.
4624 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004625 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004626 int tmp_logflags = 0;
4627
Dave Chinner29c8d172011-09-18 20:41:00 +00004628 ASSERT(bma.cur);
4629 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004631 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 if (error)
4633 goto error0;
4634 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004635
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004637 XFS_IFORK_NEXTENTS(ip, whichfork) >
4638 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640error0:
4641 /*
4642 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004643 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004645 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004647 bma.logflags &= ~xfs_ilog_fext(whichfork);
4648 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004650 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 /*
4652 * Log whatever the flags say, even if error. Otherwise we might miss
4653 * detecting a case where the data is changed, there's an error,
4654 * and it's not logged so we don't shutdown when we should.
4655 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004656 if (bma.logflags)
4657 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004658
Dave Chinner29c8d172011-09-18 20:41:00 +00004659 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 if (!error) {
4661 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004662 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004664 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004665 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004667 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4669 }
4670 if (!error)
4671 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4672 orig_nmap, *nmap);
4673 return error;
4674}
4675
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004676static int
4677xfs_bmapi_remap(
4678 struct xfs_trans *tp,
4679 struct xfs_inode *ip,
4680 xfs_fileoff_t bno,
4681 xfs_filblks_t len,
4682 xfs_fsblock_t startblock,
4683 struct xfs_defer_ops *dfops)
4684{
4685 struct xfs_mount *mp = ip->i_mount;
4686 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4687 struct xfs_btree_cur *cur = NULL;
4688 xfs_fsblock_t firstblock = NULLFSBLOCK;
4689 struct xfs_bmbt_irec got;
4690 xfs_extnum_t idx;
4691 int logflags = 0, error;
4692
4693 ASSERT(len > 0);
4694 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4695 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4696
4697 if (unlikely(XFS_TEST_ERROR(
4698 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4699 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004700 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004701 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4702 return -EFSCORRUPTED;
4703 }
4704
4705 if (XFS_FORCED_SHUTDOWN(mp))
4706 return -EIO;
4707
4708 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4709 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4710 if (error)
4711 return error;
4712 }
4713
4714 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4715 /* make sure we only reflink into a hole. */
4716 ASSERT(got.br_startoff > bno);
4717 ASSERT(got.br_startoff - bno >= len);
4718 }
4719
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004720 ip->i_d.di_nblocks += len;
4721 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004722
4723 if (ifp->if_flags & XFS_IFBROOT) {
4724 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4725 cur->bc_private.b.firstblock = firstblock;
4726 cur->bc_private.b.dfops = dfops;
4727 cur->bc_private.b.flags = 0;
4728 }
4729
4730 got.br_startoff = bno;
4731 got.br_startblock = startblock;
4732 got.br_blockcount = len;
4733 got.br_state = XFS_EXT_NORM;
4734
4735 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4736 &got, &firstblock, dfops, &logflags);
4737 if (error)
4738 goto error0;
4739
4740 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4741 int tmp_logflags = 0;
4742
4743 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4744 &tmp_logflags, XFS_DATA_FORK);
4745 logflags |= tmp_logflags;
4746 }
4747
4748error0:
4749 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4750 logflags &= ~XFS_ILOG_DEXT;
4751 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4752 logflags &= ~XFS_ILOG_DBROOT;
4753
4754 if (logflags)
4755 xfs_trans_log_inode(tp, ip, logflags);
4756 if (cur) {
4757 xfs_btree_del_cursor(cur,
4758 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4759 }
4760 return error;
4761}
4762
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004764 * When a delalloc extent is split (e.g., due to a hole punch), the original
4765 * indlen reservation must be shared across the two new extents that are left
4766 * behind.
4767 *
4768 * Given the original reservation and the worst case indlen for the two new
4769 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004770 * reservation fairly across the two new extents. If necessary, steal available
4771 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4772 * ores == 1). The number of stolen blocks is returned. The availability and
4773 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004774 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004775static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004776xfs_bmap_split_indlen(
4777 xfs_filblks_t ores, /* original res. */
4778 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004779 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4780 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004781{
4782 xfs_filblks_t len1 = *indlen1;
4783 xfs_filblks_t len2 = *indlen2;
4784 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004785 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004786 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004787
4788 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004789 * Steal as many blocks as we can to try and satisfy the worst case
4790 * indlen for both new extents.
4791 */
Brian Foster75d65362017-02-13 22:48:30 -08004792 if (ores < nres && avail)
4793 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4794 ores += stolen;
4795
4796 /* nothing else to do if we've satisfied the new reservation */
4797 if (ores >= nres)
4798 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004799
4800 /*
Brian Foster75d65362017-02-13 22:48:30 -08004801 * We can't meet the total required reservation for the two extents.
4802 * Calculate the percent of the overall shortage between both extents
4803 * and apply this percentage to each of the requested indlen values.
4804 * This distributes the shortage fairly and reduces the chances that one
4805 * of the two extents is left with nothing when extents are repeatedly
4806 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004807 */
Brian Foster75d65362017-02-13 22:48:30 -08004808 resfactor = (ores * 100);
4809 do_div(resfactor, nres);
4810 len1 *= resfactor;
4811 do_div(len1, 100);
4812 len2 *= resfactor;
4813 do_div(len2, 100);
4814 ASSERT(len1 + len2 <= ores);
4815 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4816
4817 /*
4818 * Hand out the remainder to each extent. If one of the two reservations
4819 * is zero, we want to make sure that one gets a block first. The loop
4820 * below starts with len1, so hand len2 a block right off the bat if it
4821 * is zero.
4822 */
4823 ores -= (len1 + len2);
4824 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4825 if (ores && !len2 && *indlen2) {
4826 len2++;
4827 ores--;
4828 }
4829 while (ores) {
4830 if (len1 < *indlen1) {
4831 len1++;
4832 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004833 }
Brian Foster75d65362017-02-13 22:48:30 -08004834 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004835 break;
Brian Foster75d65362017-02-13 22:48:30 -08004836 if (len2 < *indlen2) {
4837 len2++;
4838 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004839 }
4840 }
4841
4842 *indlen1 = len1;
4843 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004844
4845 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004846}
4847
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004848int
4849xfs_bmap_del_extent_delay(
4850 struct xfs_inode *ip,
4851 int whichfork,
4852 xfs_extnum_t *idx,
4853 struct xfs_bmbt_irec *got,
4854 struct xfs_bmbt_irec *del)
4855{
4856 struct xfs_mount *mp = ip->i_mount;
4857 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4858 struct xfs_bmbt_irec new;
4859 int64_t da_old, da_new, da_diff = 0;
4860 xfs_fileoff_t del_endoff, got_endoff;
4861 xfs_filblks_t got_indlen, new_indlen, stolen;
4862 int error = 0, state = 0;
4863 bool isrt;
4864
4865 XFS_STATS_INC(mp, xs_del_exlist);
4866
4867 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4868 del_endoff = del->br_startoff + del->br_blockcount;
4869 got_endoff = got->br_startoff + got->br_blockcount;
4870 da_old = startblockval(got->br_startblock);
4871 da_new = 0;
4872
4873 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004874 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004875 ASSERT(del->br_blockcount > 0);
4876 ASSERT(got->br_startoff <= del->br_startoff);
4877 ASSERT(got_endoff >= del_endoff);
4878
4879 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004880 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004881
4882 do_div(rtexts, mp->m_sb.sb_rextsize);
4883 xfs_mod_frextents(mp, rtexts);
4884 }
4885
4886 /*
4887 * Update the inode delalloc counter now and wait to update the
4888 * sb counters as we might have to borrow some blocks for the
4889 * indirect block accounting.
4890 */
Darrick J. Wong4fd29ec2016-11-08 11:59:26 +11004891 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4892 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004893 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec2016-11-08 11:59:26 +11004894 if (error)
4895 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004896 ip->i_delayed_blks -= del->br_blockcount;
4897
4898 if (whichfork == XFS_COW_FORK)
4899 state |= BMAP_COWFORK;
4900
4901 if (got->br_startoff == del->br_startoff)
4902 state |= BMAP_LEFT_CONTIG;
4903 if (got_endoff == del_endoff)
4904 state |= BMAP_RIGHT_CONTIG;
4905
4906 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4907 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4908 /*
4909 * Matches the whole extent. Delete the entry.
4910 */
4911 xfs_iext_remove(ip, *idx, 1, state);
4912 --*idx;
4913 break;
4914 case BMAP_LEFT_CONTIG:
4915 /*
4916 * Deleting the first part of the extent.
4917 */
4918 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4919 got->br_startoff = del_endoff;
4920 got->br_blockcount -= del->br_blockcount;
4921 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4922 got->br_blockcount), da_old);
4923 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004924 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004925 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4926 break;
4927 case BMAP_RIGHT_CONTIG:
4928 /*
4929 * Deleting the last part of the extent.
4930 */
4931 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4932 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4933 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4934 got->br_blockcount), da_old);
4935 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004936 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004937 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4938 break;
4939 case 0:
4940 /*
4941 * Deleting the middle of the extent.
4942 *
4943 * Distribute the original indlen reservation across the two new
4944 * extents. Steal blocks from the deleted extent if necessary.
4945 * Stealing blocks simply fudges the fdblocks accounting below.
4946 * Warn if either of the new indlen reservations is zero as this
4947 * can lead to delalloc problems.
4948 */
4949 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4950
4951 got->br_blockcount = del->br_startoff - got->br_startoff;
4952 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4953
4954 new.br_blockcount = got_endoff - del_endoff;
4955 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4956
4957 WARN_ON_ONCE(!got_indlen || !new_indlen);
4958 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4959 del->br_blockcount);
4960
4961 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004962 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004963 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4964
4965 new.br_startoff = del_endoff;
4966 new.br_state = got->br_state;
4967 new.br_startblock = nullstartblock((int)new_indlen);
4968
4969 ++*idx;
4970 xfs_iext_insert(ip, *idx, 1, &new, state);
4971
4972 da_new = got_indlen + new_indlen - stolen;
4973 del->br_blockcount -= stolen;
4974 break;
4975 }
4976
4977 ASSERT(da_old >= da_new);
4978 da_diff = da_old - da_new;
4979 if (!isrt)
4980 da_diff += del->br_blockcount;
4981 if (da_diff)
4982 xfs_mod_fdblocks(mp, da_diff, false);
4983 return error;
4984}
4985
4986void
4987xfs_bmap_del_extent_cow(
4988 struct xfs_inode *ip,
4989 xfs_extnum_t *idx,
4990 struct xfs_bmbt_irec *got,
4991 struct xfs_bmbt_irec *del)
4992{
4993 struct xfs_mount *mp = ip->i_mount;
4994 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4995 struct xfs_bmbt_irec new;
4996 xfs_fileoff_t del_endoff, got_endoff;
4997 int state = BMAP_COWFORK;
4998
4999 XFS_STATS_INC(mp, xs_del_exlist);
5000
5001 del_endoff = del->br_startoff + del->br_blockcount;
5002 got_endoff = got->br_startoff + got->br_blockcount;
5003
5004 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11005005 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005006 ASSERT(del->br_blockcount > 0);
5007 ASSERT(got->br_startoff <= del->br_startoff);
5008 ASSERT(got_endoff >= del_endoff);
5009 ASSERT(!isnullstartblock(got->br_startblock));
5010
5011 if (got->br_startoff == del->br_startoff)
5012 state |= BMAP_LEFT_CONTIG;
5013 if (got_endoff == del_endoff)
5014 state |= BMAP_RIGHT_CONTIG;
5015
5016 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
5017 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
5018 /*
5019 * Matches the whole extent. Delete the entry.
5020 */
5021 xfs_iext_remove(ip, *idx, 1, state);
5022 --*idx;
5023 break;
5024 case BMAP_LEFT_CONTIG:
5025 /*
5026 * Deleting the first part of the extent.
5027 */
5028 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5029 got->br_startoff = del_endoff;
5030 got->br_blockcount -= del->br_blockcount;
5031 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005032 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005033 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5034 break;
5035 case BMAP_RIGHT_CONTIG:
5036 /*
5037 * Deleting the last part of the extent.
5038 */
5039 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5040 got->br_blockcount -= del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005041 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005042 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5043 break;
5044 case 0:
5045 /*
5046 * Deleting the middle of the extent.
5047 */
5048 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5049 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005050 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005051 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5052
5053 new.br_startoff = del_endoff;
5054 new.br_blockcount = got_endoff - del_endoff;
5055 new.br_state = got->br_state;
5056 new.br_startblock = del->br_startblock + del->br_blockcount;
5057
5058 ++*idx;
5059 xfs_iext_insert(ip, *idx, 1, &new, state);
5060 break;
5061 }
5062}
5063
Brian Fostera9bd24a2016-03-15 11:42:46 +11005064/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005065 * Called by xfs_bmapi to update file extent records and the btree
5066 * after removing space (or undoing a delayed allocation).
5067 */
5068STATIC int /* error */
5069xfs_bmap_del_extent(
5070 xfs_inode_t *ip, /* incore inode pointer */
5071 xfs_trans_t *tp, /* current transaction pointer */
5072 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005073 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005074 xfs_btree_cur_t *cur, /* if null, not a btree */
5075 xfs_bmbt_irec_t *del, /* data to remove from extents */
5076 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005077 int whichfork, /* data or attr fork */
5078 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005079{
5080 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5081 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5082 xfs_fsblock_t del_endblock=0; /* first block past del */
5083 xfs_fileoff_t del_endoff; /* first offset past del */
5084 int delay; /* current block is delayed allocated */
5085 int do_fx; /* free extent at end of routine */
5086 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5087 int error; /* error return value */
5088 int flags; /* inode logging flags */
5089 xfs_bmbt_irec_t got; /* current extent entry */
5090 xfs_fileoff_t got_endoff; /* first offset past got */
5091 int i; /* temp state */
5092 xfs_ifork_t *ifp; /* inode fork pointer */
5093 xfs_mount_t *mp; /* mount structure */
5094 xfs_filblks_t nblks; /* quota/sb block count */
5095 xfs_bmbt_irec_t new; /* new record to be inserted */
5096 /* REFERENCED */
5097 uint qfield; /* quota field to update */
5098 xfs_filblks_t temp; /* for indirect length calculations */
5099 xfs_filblks_t temp2; /* for indirect length calculations */
5100 int state = 0;
5101
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005102 mp = ip->i_mount;
5103 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005104
5105 if (whichfork == XFS_ATTR_FORK)
5106 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005107 else if (whichfork == XFS_COW_FORK)
5108 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005109
Dave Chinner9e5987a2013-02-25 12:31:26 +11005110 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005111 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005112 ASSERT(del->br_blockcount > 0);
5113 ep = xfs_iext_get_ext(ifp, *idx);
5114 xfs_bmbt_get_all(ep, &got);
5115 ASSERT(got.br_startoff <= del->br_startoff);
5116 del_endoff = del->br_startoff + del->br_blockcount;
5117 got_endoff = got.br_startoff + got.br_blockcount;
5118 ASSERT(got_endoff >= del_endoff);
5119 delay = isnullstartblock(got.br_startblock);
5120 ASSERT(isnullstartblock(del->br_startblock) == delay);
5121 flags = 0;
5122 qfield = 0;
5123 error = 0;
5124 /*
5125 * If deleting a real allocation, must free up the disk space.
5126 */
5127 if (!delay) {
5128 flags = XFS_ILOG_CORE;
5129 /*
5130 * Realtime allocation. Free it and record di_nblocks update.
5131 */
5132 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5133 xfs_fsblock_t bno;
5134 xfs_filblks_t len;
5135
5136 ASSERT(do_mod(del->br_blockcount,
5137 mp->m_sb.sb_rextsize) == 0);
5138 ASSERT(do_mod(del->br_startblock,
5139 mp->m_sb.sb_rextsize) == 0);
5140 bno = del->br_startblock;
5141 len = del->br_blockcount;
5142 do_div(bno, mp->m_sb.sb_rextsize);
5143 do_div(len, mp->m_sb.sb_rextsize);
5144 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5145 if (error)
5146 goto done;
5147 do_fx = 0;
5148 nblks = len * mp->m_sb.sb_rextsize;
5149 qfield = XFS_TRANS_DQ_RTBCOUNT;
5150 }
5151 /*
5152 * Ordinary allocation.
5153 */
5154 else {
5155 do_fx = 1;
5156 nblks = del->br_blockcount;
5157 qfield = XFS_TRANS_DQ_BCOUNT;
5158 }
5159 /*
5160 * Set up del_endblock and cur for later.
5161 */
5162 del_endblock = del->br_startblock + del->br_blockcount;
5163 if (cur) {
5164 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5165 got.br_startblock, got.br_blockcount,
5166 &i)))
5167 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005168 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005169 }
5170 da_old = da_new = 0;
5171 } else {
5172 da_old = startblockval(got.br_startblock);
5173 da_new = 0;
5174 nblks = 0;
5175 do_fx = 0;
5176 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005177
Dave Chinner9e5987a2013-02-25 12:31:26 +11005178 /*
5179 * Set flag value to use in switch statement.
5180 * Left-contig is 2, right-contig is 1.
5181 */
5182 switch (((got.br_startoff == del->br_startoff) << 1) |
5183 (got_endoff == del_endoff)) {
5184 case 3:
5185 /*
5186 * Matches the whole extent. Delete the entry.
5187 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005188 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005189 xfs_iext_remove(ip, *idx, 1,
5190 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5191 --*idx;
5192 if (delay)
5193 break;
5194
5195 XFS_IFORK_NEXT_SET(ip, whichfork,
5196 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5197 flags |= XFS_ILOG_CORE;
5198 if (!cur) {
5199 flags |= xfs_ilog_fext(whichfork);
5200 break;
5201 }
5202 if ((error = xfs_btree_delete(cur, &i)))
5203 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005204 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005205 break;
5206
5207 case 2:
5208 /*
5209 * Deleting the first part of the extent.
5210 */
5211 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5212 xfs_bmbt_set_startoff(ep, del_endoff);
5213 temp = got.br_blockcount - del->br_blockcount;
5214 xfs_bmbt_set_blockcount(ep, temp);
5215 if (delay) {
5216 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5217 da_old);
5218 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5219 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5220 da_new = temp;
5221 break;
5222 }
5223 xfs_bmbt_set_startblock(ep, del_endblock);
5224 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5225 if (!cur) {
5226 flags |= xfs_ilog_fext(whichfork);
5227 break;
5228 }
5229 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5230 got.br_blockcount - del->br_blockcount,
5231 got.br_state)))
5232 goto done;
5233 break;
5234
5235 case 1:
5236 /*
5237 * Deleting the last part of the extent.
5238 */
5239 temp = got.br_blockcount - del->br_blockcount;
5240 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5241 xfs_bmbt_set_blockcount(ep, temp);
5242 if (delay) {
5243 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5244 da_old);
5245 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5246 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5247 da_new = temp;
5248 break;
5249 }
5250 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5251 if (!cur) {
5252 flags |= xfs_ilog_fext(whichfork);
5253 break;
5254 }
5255 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5256 got.br_startblock,
5257 got.br_blockcount - del->br_blockcount,
5258 got.br_state)))
5259 goto done;
5260 break;
5261
5262 case 0:
5263 /*
5264 * Deleting the middle of the extent.
5265 */
5266 temp = del->br_startoff - got.br_startoff;
5267 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5268 xfs_bmbt_set_blockcount(ep, temp);
5269 new.br_startoff = del_endoff;
5270 temp2 = got_endoff - del_endoff;
5271 new.br_blockcount = temp2;
5272 new.br_state = got.br_state;
5273 if (!delay) {
5274 new.br_startblock = del_endblock;
5275 flags |= XFS_ILOG_CORE;
5276 if (cur) {
5277 if ((error = xfs_bmbt_update(cur,
5278 got.br_startoff,
5279 got.br_startblock, temp,
5280 got.br_state)))
5281 goto done;
5282 if ((error = xfs_btree_increment(cur, 0, &i)))
5283 goto done;
5284 cur->bc_rec.b = new;
5285 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10005286 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005287 goto done;
5288 /*
5289 * If get no-space back from btree insert,
5290 * it tried a split, and we have a zero
5291 * block reservation.
5292 * Fix up our state and return the error.
5293 */
Dave Chinner24513372014-06-25 14:58:08 +10005294 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11005295 /*
5296 * Reset the cursor, don't trust
5297 * it after any insert operation.
5298 */
5299 if ((error = xfs_bmbt_lookup_eq(cur,
5300 got.br_startoff,
5301 got.br_startblock,
5302 temp, &i)))
5303 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005304 XFS_WANT_CORRUPTED_GOTO(mp,
5305 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005306 /*
5307 * Update the btree record back
5308 * to the original value.
5309 */
5310 if ((error = xfs_bmbt_update(cur,
5311 got.br_startoff,
5312 got.br_startblock,
5313 got.br_blockcount,
5314 got.br_state)))
5315 goto done;
5316 /*
5317 * Reset the extent record back
5318 * to the original value.
5319 */
5320 xfs_bmbt_set_blockcount(ep,
5321 got.br_blockcount);
5322 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005323 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005324 goto done;
5325 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005326 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005327 } else
5328 flags |= xfs_ilog_fext(whichfork);
5329 XFS_IFORK_NEXT_SET(ip, whichfork,
5330 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5331 } else {
Brian Fosterd34999c2016-03-15 11:42:47 +11005332 xfs_filblks_t stolen;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005333 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005334
5335 /*
5336 * Distribute the original indlen reservation across the
Brian Fosterd34999c2016-03-15 11:42:47 +11005337 * two new extents. Steal blocks from the deleted extent
5338 * if necessary. Stealing blocks simply fudges the
5339 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005340 */
5341 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5342 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c2016-03-15 11:42:47 +11005343 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5344 del->br_blockcount);
5345 da_new = temp + temp2 - stolen;
5346 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005347
5348 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11005349 * Set the reservation for each extent. Warn if either
5350 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005351 */
Brian Fosterd34999c2016-03-15 11:42:47 +11005352 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005353 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005354 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005355 }
5356 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5357 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5358 ++*idx;
5359 break;
5360 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005361
5362 /* remove reverse mapping */
5363 if (!delay) {
5364 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5365 if (error)
5366 goto done;
5367 }
5368
Dave Chinner9e5987a2013-02-25 12:31:26 +11005369 /*
5370 * If we need to, add to list of extents to delete.
5371 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005372 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005373 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5374 error = xfs_refcount_decrease_extent(mp, dfops, del);
5375 if (error)
5376 goto done;
5377 } else
5378 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5379 del->br_blockcount, NULL);
5380 }
5381
Dave Chinner9e5987a2013-02-25 12:31:26 +11005382 /*
5383 * Adjust inode # blocks in the file.
5384 */
5385 if (nblks)
5386 ip->i_d.di_nblocks -= nblks;
5387 /*
5388 * Adjust quota data.
5389 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005390 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005391 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5392
5393 /*
5394 * Account for change in delayed indirect blocks.
5395 * Nothing to do for disk quota accounting here.
5396 */
5397 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005398 if (da_old > da_new)
5399 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005400done:
5401 *logflagsp = flags;
5402 return error;
5403}
5404
5405/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 * Unmap (remove) blocks from a file.
5407 * If nexts is nonzero then the number of extents to remove is limited to
5408 * that value. If not all extents in the block range can be removed then
5409 * *done is set.
5410 */
5411int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005412__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 xfs_trans_t *tp, /* transaction pointer */
5414 struct xfs_inode *ip, /* incore inode */
5415 xfs_fileoff_t bno, /* starting offset to unmap */
Darrick J. Wong44535932016-10-03 09:11:29 -07005416 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005417 int flags, /* misc flags */
5418 xfs_extnum_t nexts, /* number of extents max */
5419 xfs_fsblock_t *firstblock, /* first allocated block
5420 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005421 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422{
5423 xfs_btree_cur_t *cur; /* bmap btree cursor */
5424 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 int error; /* error return value */
5426 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005427 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 xfs_ifork_t *ifp; /* inode fork pointer */
5429 int isrt; /* freeing in rt area */
5430 xfs_extnum_t lastx; /* last extent index used */
5431 int logflags; /* transaction logging flags */
5432 xfs_extlen_t mod; /* rt extent offset */
5433 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 xfs_fileoff_t start; /* first file offset deleted */
5435 int tmp_logflags; /* partial logging flags */
5436 int wasdel; /* was a delayed alloc extent */
5437 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005439 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005440 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005441 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005443 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5444
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005445 whichfork = xfs_bmapi_whichfork(flags);
5446 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 ifp = XFS_IFORK_PTR(ip, whichfork);
5448 if (unlikely(
5449 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5450 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5451 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5452 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005453 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 }
5455 mp = ip->i_mount;
5456 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005457 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005458
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005459 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 ASSERT(len > 0);
5461 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005462
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005463 /*
5464 * Guesstimate how many blocks we can unmap without running the risk of
5465 * blowing out the transaction with a mix of EFIs and reflink
5466 * adjustments.
5467 */
5468 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5469 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5470 else
5471 max_len = len;
5472
Linus Torvalds1da177e2005-04-16 15:20:36 -07005473 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5474 (error = xfs_iread_extents(tp, ip, whichfork)))
5475 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005476 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005477 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 return 0;
5479 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005480 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005481 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005482 start = bno;
5483 bno = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005484
Linus Torvalds1da177e2005-04-16 15:20:36 -07005485 /*
5486 * Check to see if the given block number is past the end of the
5487 * file, back up to the last block if so...
5488 */
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005489 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5490 ASSERT(lastx > 0);
5491 xfs_iext_get_extent(ifp, --lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005492 bno = got.br_startoff + got.br_blockcount - 1;
5493 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005494
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 logflags = 0;
5496 if (ifp->if_flags & XFS_IFBROOT) {
5497 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005498 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005500 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 cur->bc_private.b.flags = 0;
5502 } else
5503 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005504
5505 if (isrt) {
5506 /*
5507 * Synchronize by locking the bitmap inode.
5508 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005509 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005510 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005511 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5512 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005513 }
5514
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 extno = 0;
5516 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005517 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 /*
5519 * Is the found extent after a hole in which bno lives?
5520 * Just back up to the previous extent, if so.
5521 */
5522 if (got.br_startoff > bno) {
5523 if (--lastx < 0)
5524 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005525 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005526 }
5527 /*
5528 * Is the last block of this extent before the range
5529 * we're supposed to delete? If so, we're done.
5530 */
5531 bno = XFS_FILEOFF_MIN(bno,
5532 got.br_startoff + got.br_blockcount - 1);
5533 if (bno < start)
5534 break;
5535 /*
5536 * Then deal with the (possibly delayed) allocated space
5537 * we found.
5538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005540 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005541
5542 /*
5543 * Make sure we don't touch multiple AGF headers out of order
5544 * in a single transaction, as that could cause AB-BA deadlocks.
5545 */
5546 if (!wasdel) {
5547 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5548 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5549 break;
5550 prev_agno = agno;
5551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552 if (got.br_startoff < start) {
5553 del.br_startoff = start;
5554 del.br_blockcount -= start - got.br_startoff;
5555 if (!wasdel)
5556 del.br_startblock += start - got.br_startoff;
5557 }
5558 if (del.br_startoff + del.br_blockcount > bno + 1)
5559 del.br_blockcount = bno + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005560
5561 /* How much can we safely unmap? */
5562 if (max_len < del.br_blockcount) {
5563 del.br_startoff += del.br_blockcount - max_len;
5564 if (!wasdel)
5565 del.br_startblock += del.br_blockcount - max_len;
5566 del.br_blockcount = max_len;
5567 }
5568
Linus Torvalds1da177e2005-04-16 15:20:36 -07005569 sum = del.br_startblock + del.br_blockcount;
5570 if (isrt &&
5571 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5572 /*
5573 * Realtime extent not lined up at the end.
5574 * The extent could have been split into written
5575 * and unwritten pieces, or we could just be
5576 * unmapping part of it. But we can't really
5577 * get rid of part of a realtime extent.
5578 */
5579 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005580 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 /*
5582 * This piece is unwritten, or we're not
5583 * using unwritten extents. Skip over it.
5584 */
5585 ASSERT(bno >= mod);
5586 bno -= mod > del.br_blockcount ?
5587 del.br_blockcount : mod;
5588 if (bno < got.br_startoff) {
5589 if (--lastx >= 0)
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005590 xfs_bmbt_get_all(xfs_iext_get_ext(
5591 ifp, lastx), &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592 }
5593 continue;
5594 }
5595 /*
5596 * It's written, turn it unwritten.
5597 * This is better than zeroing it.
5598 */
5599 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005600 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601 /*
5602 * If this spans a realtime extent boundary,
5603 * chop it back to the start of the one we end at.
5604 */
5605 if (del.br_blockcount > mod) {
5606 del.br_startoff += del.br_blockcount - mod;
5607 del.br_startblock += del.br_blockcount - mod;
5608 del.br_blockcount = mod;
5609 }
5610 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005611 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005612 whichfork, &lastx, &cur, &del,
5613 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005614 if (error)
5615 goto error0;
5616 goto nodelete;
5617 }
5618 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5619 /*
5620 * Realtime extent is lined up at the end but not
5621 * at the front. We'll get rid of full extents if
5622 * we can.
5623 */
5624 mod = mp->m_sb.sb_rextsize - mod;
5625 if (del.br_blockcount > mod) {
5626 del.br_blockcount -= mod;
5627 del.br_startoff += mod;
5628 del.br_startblock += mod;
5629 } else if ((del.br_startoff == start &&
5630 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005631 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005632 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 /*
5634 * Can't make it unwritten. There isn't
5635 * a full extent here so just skip it.
5636 */
5637 ASSERT(bno >= del.br_blockcount);
5638 bno -= del.br_blockcount;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005639 if (got.br_startoff > bno && --lastx >= 0)
5640 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 continue;
5642 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005643 struct xfs_bmbt_irec prev;
5644
Linus Torvalds1da177e2005-04-16 15:20:36 -07005645 /*
5646 * This one is already unwritten.
5647 * It must have a written left neighbor.
5648 * Unwrite the killed part of that one and
5649 * try again.
5650 */
5651 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005652 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005654 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005655 ASSERT(del.br_startblock ==
5656 prev.br_startblock + prev.br_blockcount);
5657 if (prev.br_startoff < start) {
5658 mod = start - prev.br_startoff;
5659 prev.br_blockcount -= mod;
5660 prev.br_startblock += mod;
5661 prev.br_startoff = start;
5662 }
5663 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005664 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005665 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005666 ip, whichfork, &lastx, &cur,
5667 &prev, firstblock, dfops,
5668 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 if (error)
5670 goto error0;
5671 goto nodelete;
5672 } else {
5673 ASSERT(del.br_state == XFS_EXT_NORM);
5674 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005675 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005676 ip, whichfork, &lastx, &cur,
5677 &del, firstblock, dfops,
5678 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 if (error)
5680 goto error0;
5681 goto nodelete;
5682 }
5683 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005684
Linus Torvalds1da177e2005-04-16 15:20:36 -07005685 /*
5686 * If it's the case where the directory code is running
5687 * with no block reservation, and the deleted block is in
5688 * the middle of its extent, and the resulting insert
5689 * of an extent would cause transformation to btree format,
5690 * then reject it. The calling code will then swap
5691 * blocks around instead.
5692 * We have to do this now, rather than waiting for the
5693 * conversion to btree format, since the transaction
5694 * will be dirty.
5695 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005696 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005698 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5699 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 del.br_startoff > got.br_startoff &&
5701 del.br_startoff + del.br_blockcount <
5702 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005703 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 goto error0;
5705 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005706
5707 /*
5708 * Unreserve quota and update realtime free space, if
5709 * appropriate. If delayed allocation, update the inode delalloc
5710 * counter now and wait to update the sb counters as
5711 * xfs_bmap_del_extent() might need to borrow some blocks.
5712 */
5713 if (wasdel) {
5714 ASSERT(startblockval(del.br_startblock) > 0);
5715 if (isrt) {
5716 xfs_filblks_t rtexts;
5717
5718 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5719 do_div(rtexts, mp->m_sb.sb_rextsize);
5720 xfs_mod_frextents(mp, (int64_t)rtexts);
5721 (void)xfs_trans_reserve_quota_nblks(NULL,
5722 ip, -((long)del.br_blockcount), 0,
5723 XFS_QMOPT_RES_RTBLKS);
5724 } else {
5725 (void)xfs_trans_reserve_quota_nblks(NULL,
5726 ip, -((long)del.br_blockcount), 0,
5727 XFS_QMOPT_RES_REGBLKS);
5728 }
5729 ip->i_delayed_blks -= del.br_blockcount;
5730 if (cur)
5731 cur->bc_private.b.flags |=
5732 XFS_BTCUR_BPRV_WASDEL;
5733 } else if (cur)
5734 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5735
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005736 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005737 &tmp_logflags, whichfork, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 logflags |= tmp_logflags;
5739 if (error)
5740 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005741
5742 if (!isrt && wasdel)
5743 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5744
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005745 max_len -= del.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746 bno = del.br_startoff - 1;
5747nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 /*
5749 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005752 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005753 xfs_iext_get_extent(ifp, lastx, &got);
5754 if (got.br_startoff > bno && --lastx >= 0)
5755 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 extno++;
5758 }
5759 }
Darrick J. Wong44535932016-10-03 09:11:29 -07005760 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5761 *rlen = 0;
5762 else
5763 *rlen = bno - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005764
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 /*
5766 * Convert to a btree if necessary.
5767 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005768 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005770 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 &cur, 0, &tmp_logflags, whichfork);
5772 logflags |= tmp_logflags;
5773 if (error)
5774 goto error0;
5775 }
5776 /*
5777 * transform from btree to extents, give it cur
5778 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005779 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 ASSERT(cur != NULL);
5781 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5782 whichfork);
5783 logflags |= tmp_logflags;
5784 if (error)
5785 goto error0;
5786 }
5787 /*
5788 * transform from extents to local?
5789 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790 error = 0;
5791error0:
5792 /*
5793 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005794 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005795 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005796 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005798 logflags &= ~xfs_ilog_fext(whichfork);
5799 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005801 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802 /*
5803 * Log inode even in the error case, if the transaction
5804 * is dirty we'll need to shut down the filesystem.
5805 */
5806 if (logflags)
5807 xfs_trans_log_inode(tp, ip, logflags);
5808 if (cur) {
5809 if (!error) {
5810 *firstblock = cur->bc_private.b.firstblock;
5811 cur->bc_private.b.allocated = 0;
5812 }
5813 xfs_btree_del_cursor(cur,
5814 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5815 }
5816 return error;
5817}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005818
Darrick J. Wong44535932016-10-03 09:11:29 -07005819/* Unmap a range of a file. */
5820int
5821xfs_bunmapi(
5822 xfs_trans_t *tp,
5823 struct xfs_inode *ip,
5824 xfs_fileoff_t bno,
5825 xfs_filblks_t len,
5826 int flags,
5827 xfs_extnum_t nexts,
5828 xfs_fsblock_t *firstblock,
5829 struct xfs_defer_ops *dfops,
5830 int *done)
5831{
5832 int error;
5833
5834 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5835 dfops);
5836 *done = (len == 0);
5837 return error;
5838}
5839
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005840/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005841 * Determine whether an extent shift can be accomplished by a merge with the
5842 * extent that precedes the target hole of the shift.
5843 */
5844STATIC bool
5845xfs_bmse_can_merge(
5846 struct xfs_bmbt_irec *left, /* preceding extent */
5847 struct xfs_bmbt_irec *got, /* current extent to shift */
5848 xfs_fileoff_t shift) /* shift fsb */
5849{
5850 xfs_fileoff_t startoff;
5851
5852 startoff = got->br_startoff - shift;
5853
5854 /*
5855 * The extent, once shifted, must be adjacent in-file and on-disk with
5856 * the preceding extent.
5857 */
5858 if ((left->br_startoff + left->br_blockcount != startoff) ||
5859 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5860 (left->br_state != got->br_state) ||
5861 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5862 return false;
5863
5864 return true;
5865}
5866
5867/*
5868 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5869 * hole in the file. If an extent shift would result in the extent being fully
5870 * adjacent to the extent that currently precedes the hole, we can merge with
5871 * the preceding extent rather than do the shift.
5872 *
5873 * This function assumes the caller has verified a shift-by-merge is possible
5874 * with the provided extents via xfs_bmse_can_merge().
5875 */
5876STATIC int
5877xfs_bmse_merge(
5878 struct xfs_inode *ip,
5879 int whichfork,
5880 xfs_fileoff_t shift, /* shift fsb */
5881 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005882 struct xfs_bmbt_irec *got, /* extent to shift */
5883 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005884 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005885 int *logflags, /* output */
5886 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005887{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005888 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5889 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005890 xfs_filblks_t blockcount;
5891 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005892 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005893
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005894 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005895
5896 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5897 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005898 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005899
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005900 new = *left;
5901 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005902
5903 /*
5904 * Update the on-disk extent count, the btree if necessary and log the
5905 * inode.
5906 */
5907 XFS_IFORK_NEXT_SET(ip, whichfork,
5908 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5909 *logflags |= XFS_ILOG_CORE;
5910 if (!cur) {
5911 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005912 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005913 }
5914
5915 /* lookup and remove the extent to merge */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005916 error = xfs_bmbt_lookup_eq(cur, got->br_startoff, got->br_startblock,
5917 got->br_blockcount, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005918 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005919 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005920 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005921
5922 error = xfs_btree_delete(cur, &i);
5923 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005924 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005925 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005926
5927 /* lookup and update size of the previous extent */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005928 error = xfs_bmbt_lookup_eq(cur, left->br_startoff, left->br_startblock,
5929 left->br_blockcount, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005930 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005931 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005932 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005933
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005934 error = xfs_bmbt_update(cur, new.br_startoff, new.br_startblock,
5935 new.br_blockcount, new.br_state);
5936 if (error)
5937 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005938
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005939done:
5940 xfs_iext_update_extent(ifp, current_ext - 1, &new);
5941 xfs_iext_remove(ip, current_ext, 1, 0);
5942
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005943 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005944 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5945 if (error)
5946 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005947 memcpy(&new, got, sizeof(new));
5948 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005949 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005950}
5951
5952/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005953 * Shift a single extent.
5954 */
5955STATIC int
5956xfs_bmse_shift_one(
5957 struct xfs_inode *ip,
5958 int whichfork,
5959 xfs_fileoff_t offset_shift_fsb,
5960 int *current_ext,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005961 struct xfs_bmbt_irec *got,
Brian Fostera979bdf2014-09-23 15:39:04 +10005962 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005963 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005964 enum shift_direction direction,
5965 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005966{
5967 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005968 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005969 xfs_fileoff_t startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005970 struct xfs_bmbt_irec adj_irec, new;
Brian Fostera979bdf2014-09-23 15:39:04 +10005971 int error;
5972 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005973 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005974
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005975 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005976 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005977 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005978
Brian Fosterf71721d2014-09-23 15:39:05 +10005979 /* delalloc extents should be prevented by caller */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005980 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got->br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005981
Namjae Jeona904b1c2015-03-25 15:08:56 +11005982 if (direction == SHIFT_LEFT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005983 startoff = got->br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005984
Namjae Jeona904b1c2015-03-25 15:08:56 +11005985 /*
5986 * Check for merge if we've got an extent to the left,
5987 * otherwise make sure there's enough room at the start
5988 * of the file for the shift.
5989 */
5990 if (!*current_ext) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005991 if (got->br_startoff < offset_shift_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005992 return -EINVAL;
5993 goto update_current_ext;
5994 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005995
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005996 /*
5997 * grab the left extent and check for a large enough hole.
5998 */
5999 xfs_iext_get_extent(ifp, *current_ext - 1, &adj_irec);
6000 if (startoff < adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10006001 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10006002
Dave Chinnerb11bd672014-12-04 09:42:24 +11006003 /* check whether to merge the extent or shift it down */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006004 if (xfs_bmse_can_merge(&adj_irec, got, offset_shift_fsb)) {
6005 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
6006 *current_ext, got, &adj_irec,
6007 cur, logflags, dfops);
Dave Chinnerb11bd672014-12-04 09:42:24 +11006008 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006009 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006010 startoff = got->br_startoff + offset_shift_fsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006011 /* nothing to move if this is the last extent */
6012 if (*current_ext >= (total_extents - 1))
6013 goto update_current_ext;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006014
Namjae Jeona904b1c2015-03-25 15:08:56 +11006015 /*
6016 * If this is not the last extent in the file, make sure there
6017 * is enough room between current extent and next extent for
6018 * accommodating the shift.
6019 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006020 xfs_iext_get_extent(ifp, *current_ext + 1, &adj_irec);
6021 if (startoff + got->br_blockcount > adj_irec.br_startoff)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006022 return -EINVAL;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006023
Namjae Jeona904b1c2015-03-25 15:08:56 +11006024 /*
6025 * Unlike a left shift (which involves a hole punch),
6026 * a right shift does not modify extent neighbors
6027 * in any way. We should never find mergeable extents
6028 * in this scenario. Check anyways and warn if we
6029 * encounter two extents that could be one.
6030 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006031 if (xfs_bmse_can_merge(got, &adj_irec, offset_shift_fsb))
Namjae Jeona904b1c2015-03-25 15:08:56 +11006032 WARN_ON_ONCE(1);
6033 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006034
Brian Fostera979bdf2014-09-23 15:39:04 +10006035 /*
6036 * Increment the extent index for the next iteration, update the start
6037 * offset of the in-core extent and update the btree if applicable.
6038 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11006039update_current_ext:
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006040 *logflags |= XFS_ILOG_CORE;
6041
6042 new = *got;
6043 new.br_startoff = startoff;
6044
6045 if (cur) {
6046 error = xfs_bmbt_lookup_eq(cur, got->br_startoff,
6047 got->br_startblock, got->br_blockcount, &i);
6048 if (error)
6049 return error;
6050 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
6051
6052 error = xfs_bmbt_update(cur, new.br_startoff,
6053 new.br_startblock, new.br_blockcount,
6054 new.br_state);
6055 if (error)
6056 return error;
6057 } else {
6058 *logflags |= XFS_ILOG_DEXT;
6059 }
6060
6061 xfs_iext_update_extent(ifp, *current_ext, &new);
6062
Namjae Jeona904b1c2015-03-25 15:08:56 +11006063 if (direction == SHIFT_LEFT)
6064 (*current_ext)++;
6065 else
6066 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10006067
Darrick J. Wong9c194642016-08-03 12:16:05 +10006068 /* update reverse mapping */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006069 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
Darrick J. Wong9c194642016-08-03 12:16:05 +10006070 if (error)
6071 return error;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006072 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fostera979bdf2014-09-23 15:39:04 +10006073}
6074
6075/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11006076 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006077 *
Brian Foster2c845f52014-09-23 15:37:09 +10006078 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11006079 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10006080 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6081 * is the length by which each extent is shifted. If there is no hole to shift
6082 * the extents into, this will be considered invalid operation and we abort
6083 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006084 */
6085int
6086xfs_bmap_shift_extents(
6087 struct xfs_trans *tp,
6088 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006089 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006090 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10006091 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006092 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006093 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006094 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006095 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006096 int num_exts)
6097{
Brian Fosterca446d82014-09-02 12:12:53 +10006098 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006099 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006100 struct xfs_mount *mp = ip->i_mount;
6101 struct xfs_ifork *ifp;
6102 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10006103 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006104 xfs_extnum_t total_extents;
6105 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006106 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006107 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10006108 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006109
6110 if (unlikely(XFS_TEST_ERROR(
6111 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6112 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006113 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006114 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6115 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10006116 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006117 }
6118
6119 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10006120 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006121
Brian Foster2c845f52014-09-23 15:37:09 +10006122 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6123 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006124 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006125
6126 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006127 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6128 /* Read in all the extents */
6129 error = xfs_iread_extents(tp, ip, whichfork);
6130 if (error)
6131 return error;
6132 }
6133
Brian Fosterddb19e32014-09-23 15:38:09 +10006134 if (ifp->if_flags & XFS_IFBROOT) {
6135 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6136 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006137 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006138 cur->bc_private.b.flags = 0;
6139 }
6140
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006141 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006142 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006143 * are collapsing out, so we cannot use the count of real extents here.
6144 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006145 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006146 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006147 if (total_extents == 0) {
6148 *done = 1;
6149 goto del_cursor;
6150 }
6151
6152 /*
6153 * In case of first right shift, we need to initialize next_fsb
6154 */
6155 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006156 ASSERT(direction == SHIFT_RIGHT);
6157
6158 current_ext = total_extents - 1;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006159 xfs_iext_get_extent(ifp, current_ext, &got);
6160 if (stop_fsb > got.br_startoff) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006161 *done = 1;
6162 goto del_cursor;
6163 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006164 *next_fsb = got.br_startoff;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006165 } else {
6166 /*
6167 * Look up the extent index for the fsb where we start shifting. We can
6168 * henceforth iterate with current_ext as extent list changes are locked
6169 * out via ilock.
6170 *
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006171 * If next_fsb lies in a hole beyond which there are no extents we are
6172 * done.
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006173 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006174 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
6175 &got)) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006176 *done = 1;
6177 goto del_cursor;
6178 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006179 }
6180
6181 /* Lookup the extent index at which we have to stop */
6182 if (direction == SHIFT_RIGHT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006183 struct xfs_bmbt_irec s;
6184
6185 xfs_iext_lookup_extent(ip, ifp, stop_fsb, &stop_extent, &s);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006186 /* Make stop_extent exclusive of shift range */
6187 stop_extent--;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006188 if (current_ext <= stop_extent) {
6189 error = -EIO;
6190 goto del_cursor;
6191 }
6192 } else {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006193 stop_extent = total_extents;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006194 if (current_ext >= stop_extent) {
6195 error = -EIO;
6196 goto del_cursor;
6197 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006198 }
6199
6200 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006201 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006202 &current_ext, &got, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006203 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006204 if (error)
6205 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006206 /*
6207 * If there was an extent merge during the shift, the extent
6208 * count can change. Update the total and grade the next record.
6209 */
6210 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006211 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006212 stop_extent = total_extents;
6213 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006214
Namjae Jeona904b1c2015-03-25 15:08:56 +11006215 if (current_ext == stop_extent) {
6216 *done = 1;
6217 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006218 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006219 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006220 xfs_iext_get_extent(ifp, current_ext, &got);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006221 }
6222
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006223 if (!*done)
Brian Foster2c845f52014-09-23 15:37:09 +10006224 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006225
6226del_cursor:
6227 if (cur)
6228 xfs_btree_del_cursor(cur,
6229 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6230
Brian Fosterca446d82014-09-02 12:12:53 +10006231 if (logflags)
6232 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006233
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006234 return error;
6235}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006236
6237/*
6238 * Splits an extent into two extents at split_fsb block such that it is
6239 * the first block of the current_ext. @current_ext is a target extent
6240 * to be split. @split_fsb is a block where the extents is split.
6241 * If split_fsb lies in a hole or the first block of extents, just return 0.
6242 */
6243STATIC int
6244xfs_bmap_split_extent_at(
6245 struct xfs_trans *tp,
6246 struct xfs_inode *ip,
6247 xfs_fileoff_t split_fsb,
6248 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006249 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006250{
6251 int whichfork = XFS_DATA_FORK;
6252 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006253 struct xfs_bmbt_irec got;
6254 struct xfs_bmbt_irec new; /* split extent */
6255 struct xfs_mount *mp = ip->i_mount;
6256 struct xfs_ifork *ifp;
6257 xfs_fsblock_t gotblkcnt; /* new block count for got */
6258 xfs_extnum_t current_ext;
6259 int error = 0;
6260 int logflags = 0;
6261 int i = 0;
6262
6263 if (unlikely(XFS_TEST_ERROR(
6264 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6265 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006266 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006267 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6268 XFS_ERRLEVEL_LOW, mp);
6269 return -EFSCORRUPTED;
6270 }
6271
6272 if (XFS_FORCED_SHUTDOWN(mp))
6273 return -EIO;
6274
6275 ifp = XFS_IFORK_PTR(ip, whichfork);
6276 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6277 /* Read in all the extents */
6278 error = xfs_iread_extents(tp, ip, whichfork);
6279 if (error)
6280 return error;
6281 }
6282
6283 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006284 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006285 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006286 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
6287 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006288 return 0;
6289
6290 gotblkcnt = split_fsb - got.br_startoff;
6291 new.br_startoff = split_fsb;
6292 new.br_startblock = got.br_startblock + gotblkcnt;
6293 new.br_blockcount = got.br_blockcount - gotblkcnt;
6294 new.br_state = got.br_state;
6295
6296 if (ifp->if_flags & XFS_IFBROOT) {
6297 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6298 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006299 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006300 cur->bc_private.b.flags = 0;
6301 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6302 got.br_startblock,
6303 got.br_blockcount,
6304 &i);
6305 if (error)
6306 goto del_cursor;
6307 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6308 }
6309
Namjae Jeona904b1c2015-03-25 15:08:56 +11006310 got.br_blockcount = gotblkcnt;
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006311 xfs_iext_update_extent(ifp, current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006312
6313 logflags = XFS_ILOG_CORE;
6314 if (cur) {
6315 error = xfs_bmbt_update(cur, got.br_startoff,
6316 got.br_startblock,
6317 got.br_blockcount,
6318 got.br_state);
6319 if (error)
6320 goto del_cursor;
6321 } else
6322 logflags |= XFS_ILOG_DEXT;
6323
6324 /* Add new extent */
6325 current_ext++;
6326 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6327 XFS_IFORK_NEXT_SET(ip, whichfork,
6328 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6329
6330 if (cur) {
6331 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6332 new.br_startblock, new.br_blockcount,
6333 &i);
6334 if (error)
6335 goto del_cursor;
6336 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6337 cur->bc_rec.b.br_state = new.br_state;
6338
6339 error = xfs_btree_insert(cur, &i);
6340 if (error)
6341 goto del_cursor;
6342 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6343 }
6344
6345 /*
6346 * Convert to a btree if necessary.
6347 */
6348 if (xfs_bmap_needs_btree(ip, whichfork)) {
6349 int tmp_logflags; /* partial log flag return val */
6350
6351 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006352 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006353 &cur, 0, &tmp_logflags, whichfork);
6354 logflags |= tmp_logflags;
6355 }
6356
6357del_cursor:
6358 if (cur) {
6359 cur->bc_private.b.allocated = 0;
6360 xfs_btree_del_cursor(cur,
6361 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6362 }
6363
6364 if (logflags)
6365 xfs_trans_log_inode(tp, ip, logflags);
6366 return error;
6367}
6368
6369int
6370xfs_bmap_split_extent(
6371 struct xfs_inode *ip,
6372 xfs_fileoff_t split_fsb)
6373{
6374 struct xfs_mount *mp = ip->i_mount;
6375 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006376 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006377 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006378 int error;
6379
Christoph Hellwig253f4912016-04-06 09:19:55 +10006380 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6381 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6382 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006383 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006384
6385 xfs_ilock(ip, XFS_ILOCK_EXCL);
6386 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6387
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006388 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006389
6390 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006391 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006392 if (error)
6393 goto out;
6394
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006395 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006396 if (error)
6397 goto out;
6398
Christoph Hellwig70393312015-06-04 13:48:08 +10006399 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006400
6401out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006402 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006403 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006404 return error;
6405}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006406
6407/* Deferred mapping is only for real extents in the data fork. */
6408static bool
6409xfs_bmap_is_update_needed(
6410 struct xfs_bmbt_irec *bmap)
6411{
6412 return bmap->br_startblock != HOLESTARTBLOCK &&
6413 bmap->br_startblock != DELAYSTARTBLOCK;
6414}
6415
6416/* Record a bmap intent. */
6417static int
6418__xfs_bmap_add(
6419 struct xfs_mount *mp,
6420 struct xfs_defer_ops *dfops,
6421 enum xfs_bmap_intent_type type,
6422 struct xfs_inode *ip,
6423 int whichfork,
6424 struct xfs_bmbt_irec *bmap)
6425{
6426 int error;
6427 struct xfs_bmap_intent *bi;
6428
6429 trace_xfs_bmap_defer(mp,
6430 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6431 type,
6432 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6433 ip->i_ino, whichfork,
6434 bmap->br_startoff,
6435 bmap->br_blockcount,
6436 bmap->br_state);
6437
6438 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6439 INIT_LIST_HEAD(&bi->bi_list);
6440 bi->bi_type = type;
6441 bi->bi_owner = ip;
6442 bi->bi_whichfork = whichfork;
6443 bi->bi_bmap = *bmap;
6444
Christoph Hellwig882d8782017-08-28 10:21:03 -07006445 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006446 if (error) {
6447 kmem_free(bi);
6448 return error;
6449 }
6450
6451 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6452 return 0;
6453}
6454
6455/* Map an extent into a file. */
6456int
6457xfs_bmap_map_extent(
6458 struct xfs_mount *mp,
6459 struct xfs_defer_ops *dfops,
6460 struct xfs_inode *ip,
6461 struct xfs_bmbt_irec *PREV)
6462{
6463 if (!xfs_bmap_is_update_needed(PREV))
6464 return 0;
6465
6466 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6467 XFS_DATA_FORK, PREV);
6468}
6469
6470/* Unmap an extent out of a file. */
6471int
6472xfs_bmap_unmap_extent(
6473 struct xfs_mount *mp,
6474 struct xfs_defer_ops *dfops,
6475 struct xfs_inode *ip,
6476 struct xfs_bmbt_irec *PREV)
6477{
6478 if (!xfs_bmap_is_update_needed(PREV))
6479 return 0;
6480
6481 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6482 XFS_DATA_FORK, PREV);
6483}
6484
6485/*
6486 * Process one of the deferred bmap operations. We pass back the
6487 * btree cursor to maintain our lock on the bmapbt between calls.
6488 */
6489int
6490xfs_bmap_finish_one(
6491 struct xfs_trans *tp,
6492 struct xfs_defer_ops *dfops,
6493 struct xfs_inode *ip,
6494 enum xfs_bmap_intent_type type,
6495 int whichfork,
6496 xfs_fileoff_t startoff,
6497 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006498 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006499 xfs_exntst_t state)
6500{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006501 xfs_fsblock_t firstfsb;
6502 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006503
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006504 /*
6505 * firstfsb is tied to the transaction lifetime and is used to
6506 * ensure correct AG locking order and schedule work item
6507 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6508 * to only making one bmap call per transaction, so it should
6509 * be safe to have it as a local variable here.
6510 */
6511 firstfsb = NULLFSBLOCK;
6512
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006513 trace_xfs_bmap_deferred(tp->t_mountp,
6514 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6515 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006516 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006517
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006518 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006519 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006520
6521 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006522 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006523 return -EIO;
6524
6525 switch (type) {
6526 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006527 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006528 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006529 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006530 break;
6531 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006532 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6533 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006534 break;
6535 default:
6536 ASSERT(0);
6537 error = -EFSCORRUPTED;
6538 }
6539
6540 return error;
6541}