blob: 8a6b6cceceeff3f28b7765a07c1731f98af4510d [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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100339 */
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100587{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000588 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100733 /*
734 * Need a cursor. Can't allocate until bb_level is filled in.
735 */
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +1100773 /*
774 * Allocation can't fail, the space was reserved.
775 */
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +11001119
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001120 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +11001187 }
1188
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001189 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +11001232 mp = ip->i_mount;
1233 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +11001326}
1327
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +11001430 return 0;
1431}
1432
Dave Chinner68988112013-08-12 20:49:42 +10001433int
Dave Chinner9e5987a72013-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 Chinner9e5987a72013-02-25 12:31:26 +11001452 if (nextents == 0) {
1453 *is_empty = 1;
1454 return 0;
1455 }
1456
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001457 xfs_iext_get_extent(ifp, nextents - 1, rec);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001458 *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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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 Chinner9e5987a72013-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{
Dave Chinner9e5987a72013-02-25 12:31:26 +11001543 xfs_ifork_t *ifp; /* inode fork pointer */
1544 int rval; /* return value */
1545 xfs_bmbt_irec_t s; /* internal version of extent */
1546
1547#ifndef DEBUG
1548 if (whichfork == XFS_DATA_FORK)
1549 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1550#endif /* !DEBUG */
1551 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1552 return 0;
1553 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1554 return 0;
1555 ifp = XFS_IFORK_PTR(ip, whichfork);
1556 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001557 xfs_iext_get_extent(ifp, 0, &s);
Dave Chinner9e5987a72013-02-25 12:31:26 +11001558 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1559 if (rval && whichfork == XFS_DATA_FORK)
1560 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1561 return rval;
1562}
1563
1564/*
1565 * Extent tree manipulation functions used during allocation.
1566 */
1567
1568/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001569 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 */
1571STATIC int /* error */
1572xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001573 struct xfs_bmalloca *bma,
1574 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001576 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 int diff; /* temp value */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10001578 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001581 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 xfs_fileoff_t new_endoff; /* end offset of new entry */
1583 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1584 /* left is 0, right is 1, prev is 2 */
1585 int rval=0; /* return value (logging flags) */
1586 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001587 xfs_filblks_t da_new; /* new count del alloc blocks used */
1588 xfs_filblks_t da_old; /* old count del alloc blocks used */
1589 xfs_filblks_t temp=0; /* value for da_new calculations */
1590 xfs_filblks_t temp2=0;/* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001592 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001593 xfs_extnum_t *nextents;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001595 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001596 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001597 ASSERT(whichfork != XFS_ATTR_FORK);
1598 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1599 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001600
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001601 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001602 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001603 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001604 ASSERT(!bma->cur ||
1605 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001606
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001607 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609#define LEFT r[0]
1610#define RIGHT r[1]
1611#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
Darrick J. Wong60b49842016-10-03 09:11:34 -07001613 if (whichfork == XFS_COW_FORK)
1614 state |= BMAP_COWFORK;
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /*
1617 * Set up a bunch of variables to make the tests simpler.
1618 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001619 ep = xfs_iext_get_ext(ifp, bma->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 xfs_bmbt_get_all(ep, &PREV);
1621 new_endoff = new->br_startoff + new->br_blockcount;
1622 ASSERT(PREV.br_startoff <= new->br_startoff);
1623 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001624
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001625 da_old = startblockval(PREV.br_startblock);
1626 da_new = 0;
1627
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 /*
1629 * Set flags determining what part of the previous delayed allocation
1630 * extent is being replaced by a real allocation.
1631 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001632 if (PREV.br_startoff == new->br_startoff)
1633 state |= BMAP_LEFT_FILLING;
1634 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1635 state |= BMAP_RIGHT_FILLING;
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 /*
1638 * Check and set flags if this segment has a left neighbor.
1639 * Don't set contiguous if the combined extent would be too large.
1640 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001641 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001642 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001643 xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001644
1645 if (isnullstartblock(LEFT.br_startblock))
1646 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001648
1649 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1650 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1651 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1652 LEFT.br_state == new->br_state &&
1653 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1654 state |= BMAP_LEFT_CONTIG;
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /*
1657 * Check and set flags if this segment has a right neighbor.
1658 * Don't set contiguous if the combined extent would be too large.
1659 * Also check for all-three-contiguous being too large.
1660 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001661 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001662 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001663 xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001664
1665 if (isnullstartblock(RIGHT.br_startblock))
1666 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001668
1669 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1670 new_endoff == RIGHT.br_startoff &&
1671 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1672 new->br_state == RIGHT.br_state &&
1673 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1674 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1675 BMAP_RIGHT_FILLING)) !=
1676 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1677 BMAP_RIGHT_FILLING) ||
1678 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1679 <= MAXEXTLEN))
1680 state |= BMAP_RIGHT_CONTIG;
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 error = 0;
1683 /*
1684 * Switch out based on the FILLING and CONTIG state bits.
1685 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001686 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1687 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1688 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1689 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 /*
1691 * Filling in all of a previously delayed allocation extent.
1692 * The left and right neighbors are both contiguous with new.
1693 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001694 bma->idx--;
1695 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1696 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 LEFT.br_blockcount + PREV.br_blockcount +
1698 RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001699 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001700
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001701 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001702 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001703 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1705 else {
1706 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001707 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001709 RIGHT.br_blockcount, &i);
1710 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001712 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001713 error = xfs_btree_delete(bma->cur, &i);
1714 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001716 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001717 error = xfs_btree_decrement(bma->cur, 0, &i);
1718 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001720 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001721 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 LEFT.br_startblock,
1723 LEFT.br_blockcount +
1724 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001725 RIGHT.br_blockcount, LEFT.br_state);
1726 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 goto done;
1728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 break;
1730
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001731 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 /*
1733 * Filling in all of a previously delayed allocation extent.
1734 * The left neighbor is contiguous, the right is not.
1735 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001736 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001737
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001738 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1739 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
Christoph Hellwigec90c552011-05-23 08:52:53 +00001740 LEFT.br_blockcount + PREV.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001741 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001742
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001743 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1744 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 rval = XFS_ILOG_DEXT;
1746 else {
1747 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001748 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001750 &i);
1751 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001753 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001754 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 LEFT.br_startblock,
1756 LEFT.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001757 PREV.br_blockcount, LEFT.br_state);
1758 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 goto done;
1760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 break;
1762
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001763 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 /*
1765 * Filling in all of a previously delayed allocation extent.
1766 * The right neighbor is contiguous, the left is not.
1767 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001768 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 xfs_bmbt_set_startblock(ep, new->br_startblock);
1770 xfs_bmbt_set_blockcount(ep,
1771 PREV.br_blockcount + RIGHT.br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001772 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001773
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001774 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1775 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 rval = XFS_ILOG_DEXT;
1777 else {
1778 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001779 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001781 RIGHT.br_blockcount, &i);
1782 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001784 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001785 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 new->br_startblock,
1787 PREV.br_blockcount +
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001788 RIGHT.br_blockcount, PREV.br_state);
1789 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 goto done;
1791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 break;
1793
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001794 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /*
1796 * Filling in all of a previously delayed allocation extent.
1797 * Neither the left nor right neighbors are contiguous with
1798 * the new one.
1799 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001800 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 xfs_bmbt_set_startblock(ep, new->br_startblock);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08001802 xfs_bmbt_set_state(ep, new->br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001803 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001804
Darrick J. Wong60b49842016-10-03 09:11:34 -07001805 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001806 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1808 else {
1809 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001810 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001812 &i);
1813 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001815 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001816 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1817 error = xfs_btree_insert(bma->cur, &i);
1818 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001820 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 break;
1823
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001824 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 /*
1826 * Filling in the first part of a previous delayed allocation.
1827 * The left neighbor is contiguous.
1828 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001829 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
1830 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 LEFT.br_blockcount + new->br_blockcount);
1832 xfs_bmbt_set_startoff(ep,
1833 PREV.br_startoff + new->br_blockcount);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001834 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001837 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001839 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 rval = XFS_ILOG_DEXT;
1841 else {
1842 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001843 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 LEFT.br_startblock, LEFT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001845 &i);
1846 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001848 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001849 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 LEFT.br_startblock,
1851 LEFT.br_blockcount +
1852 new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001853 LEFT.br_state);
1854 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 goto done;
1856 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001857 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001858 startblockval(PREV.br_startblock));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001859 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001860 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001861
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001862 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 break;
1864
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001865 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 /*
1867 * Filling in the first part of a previous delayed allocation.
1868 * The left neighbor is not contiguous.
1869 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001870 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 xfs_bmbt_set_startoff(ep, new_endoff);
1872 temp = PREV.br_blockcount - new->br_blockcount;
1873 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001874 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001875 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001876 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1878 else {
1879 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001880 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001882 &i);
1883 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001885 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001886 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1887 error = xfs_btree_insert(bma->cur, &i);
1888 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001890 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001892
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001893 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001894 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001895 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001896 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 rval |= tmp_rval;
1898 if (error)
1899 goto done;
1900 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001901 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001902 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001903 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1904 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001905 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001906 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 break;
1908
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001909 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /*
1911 * Filling in the last part of a previous delayed allocation.
1912 * The right neighbor is contiguous with the new allocation.
1913 */
1914 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001915 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001917 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001918 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 new->br_blockcount + RIGHT.br_blockcount,
1920 RIGHT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001921 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1922 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 rval = XFS_ILOG_DEXT;
1924 else {
1925 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001926 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001928 RIGHT.br_blockcount, &i);
1929 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001931 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001932 error = xfs_bmbt_update(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 new->br_startblock,
1934 new->br_blockcount +
1935 RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001936 RIGHT.br_state);
1937 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 goto done;
1939 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001940
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001941 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001942 startblockval(PREV.br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001943 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001944 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001945 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001946
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001947 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 break;
1949
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001950 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /*
1952 * Filling in the last part of a previous delayed allocation.
1953 * The right neighbor is not contiguous.
1954 */
1955 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001956 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 xfs_bmbt_set_blockcount(ep, temp);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001959 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001960 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1962 else {
1963 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001964 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001966 &i);
1967 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001969 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001970 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1971 error = xfs_btree_insert(bma->cur, &i);
1972 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001974 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001976
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001977 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001978 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001979 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001980 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 rval |= tmp_rval;
1982 if (error)
1983 goto done;
1984 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001985 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001986 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001987 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
1988 ep = xfs_iext_get_ext(ifp, bma->idx);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001989 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001990 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001991
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001992 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 break;
1994
1995 case 0:
1996 /*
1997 * Filling in the middle part of a previous delayed allocation.
1998 * Contiguity is impossible here.
1999 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002000 *
2001 * We start with a delayed allocation:
2002 *
2003 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2004 * PREV @ idx
2005 *
2006 * and we are allocating:
2007 * +rrrrrrrrrrrrrrrrr+
2008 * new
2009 *
2010 * and we set it up for insertion as:
2011 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2012 * new
2013 * PREV @ idx LEFT RIGHT
2014 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 */
2016 temp = new->br_startoff - PREV.br_startoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002018 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002019 xfs_bmbt_set_blockcount(ep, temp); /* truncate PREV */
2020 LEFT = *new;
2021 RIGHT.br_state = PREV.br_state;
2022 RIGHT.br_startblock = nullstartblock(
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002023 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002024 RIGHT.br_startoff = new_endoff;
2025 RIGHT.br_blockcount = temp2;
2026 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002027 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002028 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002029 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2031 else {
2032 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002033 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002035 &i);
2036 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002038 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002039 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2040 error = xfs_btree_insert(bma->cur, &i);
2041 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002043 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002045
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002046 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002047 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002048 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002049 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 rval |= tmp_rval;
2051 if (error)
2052 goto done;
2053 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002054 temp = xfs_bmap_worst_indlen(bma->ip, temp);
2055 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
Brian Foster0daaeca2017-05-12 10:44:08 -07002056 diff = (int)(temp + temp2 -
2057 (startblockval(PREV.br_startblock) -
2058 (bma->cur ?
2059 bma->cur->bc_private.b.allocated : 0)));
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002060 if (diff > 0) {
Dave Chinner0d485ad2015-02-23 21:22:03 +11002061 error = xfs_mod_fdblocks(bma->ip->i_mount,
2062 -((int64_t)diff), false);
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002063 ASSERT(!error);
2064 if (error)
2065 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
Christoph Hellwigb9b984d2011-09-18 20:40:42 +00002067
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002068 ep = xfs_iext_get_ext(ifp, bma->idx);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002069 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002070 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2071 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
2072 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002073 nullstartblock((int)temp2));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002074 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002075
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002076 bma->idx++;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002077 da_new = temp + temp2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 break;
2079
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002080 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2081 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2082 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2083 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2084 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2085 case BMAP_LEFT_CONTIG:
2086 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 /*
2088 * These cases are all impossible.
2089 */
2090 ASSERT(0);
2091 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002092
Darrick J. Wong9c194642016-08-03 12:16:05 +10002093 /* add reverse mapping */
2094 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2095 if (error)
2096 goto done;
2097
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002098 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002099 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002100 int tmp_logflags; /* partial log flag return val */
2101
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002102 ASSERT(bma->cur == NULL);
2103 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002104 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002105 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002106 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002107 if (error)
2108 goto done;
2109 }
2110
2111 /* adjust for changes in reserved delayed indirect blocks */
2112 if (da_old || da_new) {
2113 temp = da_new;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002114 if (bma->cur)
2115 temp += bma->cur->bc_private.b.allocated;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002116 if (temp < da_old)
Dave Chinner0d485ad2015-02-23 21:22:03 +11002117 xfs_mod_fdblocks(bma->ip->i_mount,
2118 (int64_t)(da_old - temp), false);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002119 }
2120
2121 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002122 if (bma->cur)
2123 bma->cur->bc_private.b.allocated = 0;
2124
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002125 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002127 if (whichfork != XFS_COW_FORK)
2128 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 return error;
2130#undef LEFT
2131#undef RIGHT
2132#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
2135/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002136 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 */
2138STATIC int /* error */
2139xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002140 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002142 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002143 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002145 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002146 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002147 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002148 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002151 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002154 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 xfs_fileoff_t new_endoff; /* end offset of new entry */
2156 xfs_exntst_t newext; /* new extent state */
2157 xfs_exntst_t oldext; /* old extent state */
2158 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2159 /* left is 0, right is 1, prev is 2 */
2160 int rval=0; /* return value (logging flags) */
2161 int state = 0;/* state bits, accessed thru macros */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002162 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002164 *logflagsp = 0;
2165
2166 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002167 ifp = XFS_IFORK_PTR(ip, whichfork);
2168 if (whichfork == XFS_COW_FORK)
2169 state |= BMAP_COWFORK;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002170
2171 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002172 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002173 ASSERT(!isnullstartblock(new->br_startblock));
2174
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002175 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177#define LEFT r[0]
2178#define RIGHT r[1]
2179#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002180
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 /*
2182 * Set up a bunch of variables to make the tests simpler.
2183 */
2184 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002185 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 xfs_bmbt_get_all(ep, &PREV);
2187 newext = new->br_state;
2188 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2189 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2190 ASSERT(PREV.br_state == oldext);
2191 new_endoff = new->br_startoff + new->br_blockcount;
2192 ASSERT(PREV.br_startoff <= new->br_startoff);
2193 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /*
2196 * Set flags determining what part of the previous oldext allocation
2197 * extent is being replaced by a newext allocation.
2198 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002199 if (PREV.br_startoff == new->br_startoff)
2200 state |= BMAP_LEFT_FILLING;
2201 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2202 state |= BMAP_RIGHT_FILLING;
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /*
2205 * Check and set flags if this segment has a left neighbor.
2206 * Don't set contiguous if the combined extent would be too large.
2207 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002208 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002209 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002210 xfs_iext_get_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002211
2212 if (isnullstartblock(LEFT.br_startblock))
2213 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002215
2216 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2217 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2218 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2219 LEFT.br_state == newext &&
2220 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2221 state |= BMAP_LEFT_CONTIG;
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /*
2224 * Check and set flags if this segment has a right neighbor.
2225 * Don't set contiguous if the combined extent would be too large.
2226 * Also check for all-three-contiguous being too large.
2227 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002228 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002229 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002230 xfs_iext_get_extent(ifp, *idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002231 if (isnullstartblock(RIGHT.br_startblock))
2232 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002234
2235 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2236 new_endoff == RIGHT.br_startoff &&
2237 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2238 newext == RIGHT.br_state &&
2239 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2240 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2241 BMAP_RIGHT_FILLING)) !=
2242 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2243 BMAP_RIGHT_FILLING) ||
2244 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2245 <= MAXEXTLEN))
2246 state |= BMAP_RIGHT_CONTIG;
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 /*
2249 * Switch out based on the FILLING and CONTIG state bits.
2250 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002251 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2252 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2253 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2254 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 /*
2256 * Setting all of a previous oldext extent to newext.
2257 * The left and right neighbors are both contiguous with new.
2258 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002259 --*idx;
2260
2261 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2262 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 LEFT.br_blockcount + PREV.br_blockcount +
2264 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002265 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002266
Christoph Hellwigec90c552011-05-23 08:52:53 +00002267 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002268 XFS_IFORK_NEXT_SET(ip, whichfork,
2269 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 if (cur == NULL)
2271 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2272 else {
2273 rval = XFS_ILOG_CORE;
2274 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2275 RIGHT.br_startblock,
2276 RIGHT.br_blockcount, &i)))
2277 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002278 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002279 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002281 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002282 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002284 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002285 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002287 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002288 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002290 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2292 LEFT.br_startblock,
2293 LEFT.br_blockcount + PREV.br_blockcount +
2294 RIGHT.br_blockcount, LEFT.br_state)))
2295 goto done;
2296 }
2297 break;
2298
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002299 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 /*
2301 * Setting all of a previous oldext extent to newext.
2302 * The left neighbor is contiguous, the right is not.
2303 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002304 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002305
Christoph Hellwigec90c552011-05-23 08:52:53 +00002306 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2307 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2308 LEFT.br_blockcount + PREV.br_blockcount);
2309 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2310
2311 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002312 XFS_IFORK_NEXT_SET(ip, whichfork,
2313 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 if (cur == NULL)
2315 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2316 else {
2317 rval = XFS_ILOG_CORE;
2318 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2319 PREV.br_startblock, PREV.br_blockcount,
2320 &i)))
2321 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002322 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002323 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002325 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002326 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002328 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2330 LEFT.br_startblock,
2331 LEFT.br_blockcount + PREV.br_blockcount,
2332 LEFT.br_state)))
2333 goto done;
2334 }
2335 break;
2336
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002337 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /*
2339 * Setting all of a previous oldext extent to newext.
2340 * The right neighbor is contiguous, the left is not.
2341 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002342 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 xfs_bmbt_set_blockcount(ep,
2344 PREV.br_blockcount + RIGHT.br_blockcount);
2345 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002346 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2347 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002348 XFS_IFORK_NEXT_SET(ip, whichfork,
2349 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if (cur == NULL)
2351 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2352 else {
2353 rval = XFS_ILOG_CORE;
2354 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2355 RIGHT.br_startblock,
2356 RIGHT.br_blockcount, &i)))
2357 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002358 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002359 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002361 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002362 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002364 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2366 new->br_startblock,
2367 new->br_blockcount + RIGHT.br_blockcount,
2368 newext)))
2369 goto done;
2370 }
2371 break;
2372
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002373 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 /*
2375 * Setting all of a previous oldext extent to newext.
2376 * Neither the left nor right neighbors are contiguous with
2377 * the new one.
2378 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002379 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002381 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 if (cur == NULL)
2384 rval = XFS_ILOG_DEXT;
2385 else {
2386 rval = 0;
2387 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2388 new->br_startblock, new->br_blockcount,
2389 &i)))
2390 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002391 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2393 new->br_startblock, new->br_blockcount,
2394 newext)))
2395 goto done;
2396 }
2397 break;
2398
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002399 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 /*
2401 * Setting the first part of a previous oldext extent to newext.
2402 * The left neighbor is contiguous.
2403 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002404 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2405 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 LEFT.br_blockcount + new->br_blockcount);
2407 xfs_bmbt_set_startoff(ep,
2408 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002409 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002410
Christoph Hellwigec90c552011-05-23 08:52:53 +00002411 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 xfs_bmbt_set_startblock(ep,
2413 new->br_startblock + new->br_blockcount);
2414 xfs_bmbt_set_blockcount(ep,
2415 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002416 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002417
Christoph Hellwigec90c552011-05-23 08:52:53 +00002418 --*idx;
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 if (cur == NULL)
2421 rval = XFS_ILOG_DEXT;
2422 else {
2423 rval = 0;
2424 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2425 PREV.br_startblock, PREV.br_blockcount,
2426 &i)))
2427 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002428 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 if ((error = xfs_bmbt_update(cur,
2430 PREV.br_startoff + new->br_blockcount,
2431 PREV.br_startblock + new->br_blockcount,
2432 PREV.br_blockcount - new->br_blockcount,
2433 oldext)))
2434 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002435 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002437 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 LEFT.br_startblock,
2439 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002440 LEFT.br_state);
2441 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 goto done;
2443 }
2444 break;
2445
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002446 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 /*
2448 * Setting the first part of a previous oldext extent to newext.
2449 * The left neighbor is not contiguous.
2450 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002451 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2453 xfs_bmbt_set_startoff(ep, new_endoff);
2454 xfs_bmbt_set_blockcount(ep,
2455 PREV.br_blockcount - new->br_blockcount);
2456 xfs_bmbt_set_startblock(ep,
2457 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002458 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002459
Christoph Hellwigec90c552011-05-23 08:52:53 +00002460 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002461 XFS_IFORK_NEXT_SET(ip, whichfork,
2462 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 if (cur == NULL)
2464 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2465 else {
2466 rval = XFS_ILOG_CORE;
2467 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2468 PREV.br_startblock, PREV.br_blockcount,
2469 &i)))
2470 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002471 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 if ((error = xfs_bmbt_update(cur,
2473 PREV.br_startoff + new->br_blockcount,
2474 PREV.br_startblock + new->br_blockcount,
2475 PREV.br_blockcount - new->br_blockcount,
2476 oldext)))
2477 goto done;
2478 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002479 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002481 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
2483 break;
2484
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002485 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 /*
2487 * Setting the last part of a previous oldext extent to newext.
2488 * The right neighbor is contiguous with the new allocation.
2489 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002490 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 xfs_bmbt_set_blockcount(ep,
2492 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002493 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2494
2495 ++*idx;
2496
2497 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2498 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002499 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002501 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (cur == NULL)
2504 rval = XFS_ILOG_DEXT;
2505 else {
2506 rval = 0;
2507 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2508 PREV.br_startblock,
2509 PREV.br_blockcount, &i)))
2510 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002511 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2513 PREV.br_startblock,
2514 PREV.br_blockcount - new->br_blockcount,
2515 oldext)))
2516 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002517 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 goto done;
2519 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2520 new->br_startblock,
2521 new->br_blockcount + RIGHT.br_blockcount,
2522 newext)))
2523 goto done;
2524 }
2525 break;
2526
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002527 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 /*
2529 * Setting the last part of a previous oldext extent to newext.
2530 * The right neighbor is not contiguous.
2531 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002532 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 xfs_bmbt_set_blockcount(ep,
2534 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002535 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002536
Christoph Hellwigec90c552011-05-23 08:52:53 +00002537 ++*idx;
2538 xfs_iext_insert(ip, *idx, 1, new, state);
2539
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002540 XFS_IFORK_NEXT_SET(ip, whichfork,
2541 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 if (cur == NULL)
2543 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2544 else {
2545 rval = XFS_ILOG_CORE;
2546 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2547 PREV.br_startblock, PREV.br_blockcount,
2548 &i)))
2549 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002550 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2552 PREV.br_startblock,
2553 PREV.br_blockcount - new->br_blockcount,
2554 oldext)))
2555 goto done;
2556 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2557 new->br_startblock, new->br_blockcount,
2558 &i)))
2559 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002560 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig5e422f52017-10-17 14:16:19 -07002561 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002562 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002564 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
2566 break;
2567
2568 case 0:
2569 /*
2570 * Setting the middle part of a previous oldext extent to
2571 * newext. Contiguity is impossible here.
2572 * One extent becomes three extents.
2573 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002574 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 xfs_bmbt_set_blockcount(ep,
2576 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002577 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 r[0] = *new;
2580 r[1].br_startoff = new_endoff;
2581 r[1].br_blockcount =
2582 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2583 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2584 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002585
2586 ++*idx;
2587 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2588
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002589 XFS_IFORK_NEXT_SET(ip, whichfork,
2590 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 if (cur == NULL)
2592 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2593 else {
2594 rval = XFS_ILOG_CORE;
2595 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2596 PREV.br_startblock, PREV.br_blockcount,
2597 &i)))
2598 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002599 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 /* new right extent - oldext */
2601 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2602 r[1].br_startblock, r[1].br_blockcount,
2603 r[1].br_state)))
2604 goto done;
2605 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002607 cur->bc_rec.b.br_blockcount =
2608 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002609 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002611 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002612 /*
2613 * Reset the cursor to the position of the new extent
2614 * we are about to insert as we can't trust it after
2615 * the previous insert.
2616 */
2617 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2618 new->br_startblock, new->br_blockcount,
2619 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002621 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002623 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002624 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002626 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
2628 break;
2629
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002630 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2631 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2632 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2633 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2634 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2635 case BMAP_LEFT_CONTIG:
2636 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 /*
2638 * These cases are all impossible.
2639 */
2640 ASSERT(0);
2641 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002642
Darrick J. Wong9c194642016-08-03 12:16:05 +10002643 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002644 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002645 if (error)
2646 goto done;
2647
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002648 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002649 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002650 int tmp_logflags; /* partial log flag return val */
2651
2652 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002653 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002654 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002655 *logflagsp |= tmp_logflags;
2656 if (error)
2657 goto done;
2658 }
2659
2660 /* clear out the allocated field, done with it now in any case. */
2661 if (cur) {
2662 cur->bc_private.b.allocated = 0;
2663 *curp = cur;
2664 }
2665
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002666 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002668 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 return error;
2670#undef LEFT
2671#undef RIGHT
2672#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673}
2674
2675/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002676 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002678STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679xfs_bmap_add_extent_hole_delay(
2680 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002681 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002682 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002683 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002685 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2687 xfs_filblks_t newlen=0; /* new indirect size */
2688 xfs_filblks_t oldlen=0; /* old indirect size */
2689 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2690 int state; /* state bits, accessed thru macros */
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002691 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002693 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002695 if (whichfork == XFS_COW_FORK)
2696 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002697 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 /*
2700 * Check and set flags if this segment has a left neighbor
2701 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002702 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002703 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002704 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002705
2706 if (isnullstartblock(left.br_startblock))
2707 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 /*
2711 * Check and set flags if the current (right) segment exists.
2712 * If it doesn't exist, we're converting the hole at end-of-file.
2713 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002714 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002715 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002716 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002717
2718 if (isnullstartblock(right.br_startblock))
2719 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002721
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 /*
2723 * Set contiguity flags on the left and right neighbors.
2724 * Don't let extents get too large, even if the pieces are contiguous.
2725 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002726 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2727 left.br_startoff + left.br_blockcount == new->br_startoff &&
2728 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2729 state |= BMAP_LEFT_CONTIG;
2730
2731 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2732 new->br_startoff + new->br_blockcount == right.br_startoff &&
2733 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2734 (!(state & BMAP_LEFT_CONTIG) ||
2735 (left.br_blockcount + new->br_blockcount +
2736 right.br_blockcount <= MAXEXTLEN)))
2737 state |= BMAP_RIGHT_CONTIG;
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 /*
2740 * Switch out based on the contiguity flags.
2741 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002742 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2743 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 /*
2745 * New allocation is contiguous with delayed allocations
2746 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002747 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002749 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 temp = left.br_blockcount + new->br_blockcount +
2751 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002752
Christoph Hellwigec90c552011-05-23 08:52:53 +00002753 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002754 oldlen = startblockval(left.br_startblock) +
2755 startblockval(new->br_startblock) +
2756 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002757 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2758 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002759 left.br_startblock = nullstartblock(newlen);
2760 left.br_blockcount = temp;
2761 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002762 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002763
Christoph Hellwigec90c552011-05-23 08:52:53 +00002764 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 break;
2766
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002767 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 /*
2769 * New allocation is contiguous with a delayed allocation
2770 * on the left.
2771 * Merge the new allocation with the left neighbor.
2772 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002773 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002775
2776 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002777 oldlen = startblockval(left.br_startblock) +
2778 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002779 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2780 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002781 left.br_blockcount = temp;
2782 left.br_startblock = nullstartblock(newlen);
2783 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002784 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 break;
2786
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002787 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 /*
2789 * New allocation is contiguous with a delayed allocation
2790 * on the right.
2791 * Merge the new allocation with the right neighbor.
2792 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002793 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002795 oldlen = startblockval(new->br_startblock) +
2796 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002797 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2798 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002799 right.br_startoff = new->br_startoff;
2800 right.br_startblock = nullstartblock(newlen);
2801 right.br_blockcount = temp;
2802 xfs_iext_update_extent(ifp, *idx, &right);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002803 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 break;
2805
2806 case 0:
2807 /*
2808 * New allocation is not contiguous with another
2809 * delayed allocation.
2810 * Insert a new entry.
2811 */
2812 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002813 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 break;
2815 }
2816 if (oldlen != newlen) {
2817 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002818 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2819 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 /*
2821 * Nothing to do for disk quota accounting here.
2822 */
2823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824}
2825
2826/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002827 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 */
2829STATIC int /* error */
2830xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002831 struct xfs_trans *tp,
2832 struct xfs_inode *ip,
2833 int whichfork,
2834 xfs_extnum_t *idx,
2835 struct xfs_btree_cur **curp,
2836 struct xfs_bmbt_irec *new,
2837 xfs_fsblock_t *first,
2838 struct xfs_defer_ops *dfops,
2839 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002841 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2842 struct xfs_mount *mp = ip->i_mount;
2843 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2847 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002848 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Christoph Hellwig6d045582017-04-11 16:45:54 -07002851 ASSERT(*idx >= 0);
2852 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002853 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002854 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002855
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002856 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002857
2858 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002859 if (whichfork == XFS_ATTR_FORK)
2860 state |= BMAP_ATTRFORK;
Christoph Hellwiga14234c2017-02-06 10:50:49 -08002861 if (whichfork == XFS_COW_FORK)
2862 state |= BMAP_COWFORK;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 /*
2865 * Check and set flags if this segment has a left neighbor.
2866 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002867 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002868 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002869 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002870 if (isnullstartblock(left.br_startblock))
2871 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 /*
2875 * Check and set flags if this segment has a current value.
2876 * Not true if we're inserting into the "hole" at eof.
2877 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002878 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002879 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002880 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002881 if (isnullstartblock(right.br_startblock))
2882 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 /*
2886 * We're inserting a real allocation between "left" and "right".
2887 * Set the contiguity flags. Don't let extents get too large.
2888 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002889 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2890 left.br_startoff + left.br_blockcount == new->br_startoff &&
2891 left.br_startblock + left.br_blockcount == new->br_startblock &&
2892 left.br_state == new->br_state &&
2893 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2894 state |= BMAP_LEFT_CONTIG;
2895
2896 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2897 new->br_startoff + new->br_blockcount == right.br_startoff &&
2898 new->br_startblock + new->br_blockcount == right.br_startblock &&
2899 new->br_state == right.br_state &&
2900 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2901 (!(state & BMAP_LEFT_CONTIG) ||
2902 left.br_blockcount + new->br_blockcount +
2903 right.br_blockcount <= MAXEXTLEN))
2904 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002906 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 /*
2908 * Select which case we're in here, and implement it.
2909 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002910 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2911 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 /*
2913 * New allocation is contiguous with real allocations on the
2914 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002915 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002917 --*idx;
2918 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2919 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 left.br_blockcount + new->br_blockcount +
2921 right.br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002922 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002923
Christoph Hellwig6d045582017-04-11 16:45:54 -07002924 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002925
Christoph Hellwig6d045582017-04-11 16:45:54 -07002926 XFS_IFORK_NEXT_SET(ip, whichfork,
2927 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2928 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002929 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002930 } else {
2931 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002932 error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002933 right.br_startblock, right.br_blockcount,
2934 &i);
2935 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002936 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002937 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002938 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002939 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002940 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002941 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002942 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002943 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002944 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002945 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002946 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002947 left.br_startblock,
2948 left.br_blockcount +
2949 new->br_blockcount +
2950 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002951 left.br_state);
2952 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002953 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002955 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002957 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 /*
2959 * New allocation is contiguous with a real allocation
2960 * on the left.
2961 * Merge the new allocation with the left neighbor.
2962 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002963 --*idx;
2964 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2965 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 left.br_blockcount + new->br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002967 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002968
Christoph Hellwig6d045582017-04-11 16:45:54 -07002969 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002970 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002971 } else {
2972 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002973 error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002974 left.br_startblock, left.br_blockcount,
2975 &i);
2976 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002977 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002978 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002979 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002980 left.br_startblock,
2981 left.br_blockcount +
2982 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002983 left.br_state);
2984 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002985 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002987 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002989 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 /*
2991 * New allocation is contiguous with a real allocation
2992 * on the right.
2993 * Merge the new allocation with the right neighbor.
2994 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002995 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2996 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002997 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 new->br_blockcount + right.br_blockcount,
2999 right.br_state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003000 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003001
Christoph Hellwig6d045582017-04-11 16:45:54 -07003002 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003003 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003004 } else {
3005 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003006 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003007 right.br_startoff,
3008 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003009 right.br_blockcount, &i);
3010 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003011 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003012 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003013 error = xfs_bmbt_update(cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003014 new->br_startblock,
3015 new->br_blockcount +
3016 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003017 right.br_state);
3018 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003019 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003021 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 case 0:
3024 /*
3025 * New allocation is not contiguous with another
3026 * real allocation.
3027 * Insert a new entry.
3028 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003029 xfs_iext_insert(ip, *idx, 1, new, state);
3030 XFS_IFORK_NEXT_SET(ip, whichfork,
3031 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3032 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003033 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003034 } else {
3035 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003036 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003037 new->br_startoff,
3038 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003039 new->br_blockcount, &i);
3040 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003041 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003042 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003043 cur->bc_rec.b.br_state = new->br_state;
3044 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003045 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003046 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003047 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003049 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003051
Darrick J. Wong9c194642016-08-03 12:16:05 +10003052 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003053 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10003054 if (error)
3055 goto done;
3056
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003057 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003058 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003059 int tmp_logflags; /* partial log flag return val */
3060
Christoph Hellwig6d045582017-04-11 16:45:54 -07003061 ASSERT(cur == NULL);
3062 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003063 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003064 *logflagsp |= tmp_logflags;
3065 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003066 if (error)
3067 goto done;
3068 }
3069
3070 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003071 if (cur)
3072 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003073
Christoph Hellwig6d045582017-04-11 16:45:54 -07003074 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003075done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07003076 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003077 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
3079
Nathan Scottdd9f4382006-01-11 15:28:28 +11003080/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11003081 * Functions used in the extent read, allocate and remove paths
3082 */
3083
3084/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003085 * Adjust the size of the new extent based on di_extsize and rt extsize.
3086 */
Dave Chinner68988112013-08-12 20:49:42 +10003087int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003088xfs_bmap_extsize_align(
3089 xfs_mount_t *mp,
3090 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3091 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3092 xfs_extlen_t extsz, /* align to this extent size */
3093 int rt, /* is this a realtime inode? */
3094 int eof, /* is extent at end-of-file? */
3095 int delay, /* creating delalloc extent? */
3096 int convert, /* overwriting unwritten extent? */
3097 xfs_fileoff_t *offp, /* in/out: aligned offset */
3098 xfs_extlen_t *lenp) /* in/out: aligned length */
3099{
3100 xfs_fileoff_t orig_off; /* original offset */
3101 xfs_extlen_t orig_alen; /* original length */
3102 xfs_fileoff_t orig_end; /* original off+len */
3103 xfs_fileoff_t nexto; /* next file offset */
3104 xfs_fileoff_t prevo; /* previous file offset */
3105 xfs_fileoff_t align_off; /* temp for offset */
3106 xfs_extlen_t align_alen; /* temp for length */
3107 xfs_extlen_t temp; /* temp for calculations */
3108
3109 if (convert)
3110 return 0;
3111
3112 orig_off = align_off = *offp;
3113 orig_alen = align_alen = *lenp;
3114 orig_end = orig_off + orig_alen;
3115
3116 /*
3117 * If this request overlaps an existing extent, then don't
3118 * attempt to perform any additional alignment.
3119 */
3120 if (!delay && !eof &&
3121 (orig_off >= gotp->br_startoff) &&
3122 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3123 return 0;
3124 }
3125
3126 /*
3127 * If the file offset is unaligned vs. the extent size
3128 * we need to align it. This will be possible unless
3129 * the file was previously written with a kernel that didn't
3130 * perform this alignment, or if a truncate shot us in the
3131 * foot.
3132 */
3133 temp = do_mod(orig_off, extsz);
3134 if (temp) {
3135 align_alen += temp;
3136 align_off -= temp;
3137 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003138
3139 /* Same adjustment for the end of the requested area. */
3140 temp = (align_alen % extsz);
3141 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003142 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003143
3144 /*
3145 * For large extent hint sizes, the aligned extent might be larger than
3146 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3147 * the length back under MAXEXTLEN. The outer allocation loops handle
3148 * short allocation just fine, so it is safe to do this. We only want to
3149 * do it when we are forced to, though, because it means more allocation
3150 * operations are required.
3151 */
3152 while (align_alen > MAXEXTLEN)
3153 align_alen -= extsz;
3154 ASSERT(align_alen <= MAXEXTLEN);
3155
Nathan Scottdd9f4382006-01-11 15:28:28 +11003156 /*
3157 * If the previous block overlaps with this proposed allocation
3158 * then move the start forward without adjusting the length.
3159 */
3160 if (prevp->br_startoff != NULLFILEOFF) {
3161 if (prevp->br_startblock == HOLESTARTBLOCK)
3162 prevo = prevp->br_startoff;
3163 else
3164 prevo = prevp->br_startoff + prevp->br_blockcount;
3165 } else
3166 prevo = 0;
3167 if (align_off != orig_off && align_off < prevo)
3168 align_off = prevo;
3169 /*
3170 * If the next block overlaps with this proposed allocation
3171 * then move the start back without adjusting the length,
3172 * but not before offset 0.
3173 * This may of course make the start overlap previous block,
3174 * and if we hit the offset 0 limit then the next block
3175 * can still overlap too.
3176 */
3177 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3178 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3179 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3180 nexto = gotp->br_startoff + gotp->br_blockcount;
3181 else
3182 nexto = gotp->br_startoff;
3183 } else
3184 nexto = NULLFILEOFF;
3185 if (!eof &&
3186 align_off + align_alen != orig_end &&
3187 align_off + align_alen > nexto)
3188 align_off = nexto > align_alen ? nexto - align_alen : 0;
3189 /*
3190 * If we're now overlapping the next or previous extent that
3191 * means we can't fit an extsz piece in this hole. Just move
3192 * the start forward to the first valid spot and set
3193 * the length so we hit the end.
3194 */
3195 if (align_off != orig_off && align_off < prevo)
3196 align_off = prevo;
3197 if (align_off + align_alen != orig_end &&
3198 align_off + align_alen > nexto &&
3199 nexto != NULLFILEOFF) {
3200 ASSERT(nexto > prevo);
3201 align_alen = nexto - align_off;
3202 }
3203
3204 /*
3205 * If realtime, and the result isn't a multiple of the realtime
3206 * extent size we need to remove blocks until it is.
3207 */
3208 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3209 /*
3210 * We're not covering the original request, or
3211 * we won't be able to once we fix the length.
3212 */
3213 if (orig_off < align_off ||
3214 orig_end > align_off + align_alen ||
3215 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003216 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003217 /*
3218 * Try to fix it by moving the start up.
3219 */
3220 if (align_off + temp <= orig_off) {
3221 align_alen -= temp;
3222 align_off += temp;
3223 }
3224 /*
3225 * Try to fix it by moving the end in.
3226 */
3227 else if (align_off + align_alen - temp >= orig_end)
3228 align_alen -= temp;
3229 /*
3230 * Set the start to the minimum then trim the length.
3231 */
3232 else {
3233 align_alen -= orig_off - align_off;
3234 align_off = orig_off;
3235 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3236 }
3237 /*
3238 * Result doesn't cover the request, fail it.
3239 */
3240 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003241 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003242 } else {
3243 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003244 /* see MAXEXTLEN handling above */
3245 ASSERT(orig_end <= align_off + align_alen ||
3246 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003247 }
3248
3249#ifdef DEBUG
3250 if (!eof && gotp->br_startoff != NULLFILEOFF)
3251 ASSERT(align_off + align_alen <= gotp->br_startoff);
3252 if (prevp->br_startoff != NULLFILEOFF)
3253 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3254#endif
3255
3256 *lenp = align_alen;
3257 *offp = align_off;
3258 return 0;
3259}
3260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261#define XFS_ALLOC_GAP_UNITS 4
3262
Dave Chinner68988112013-08-12 20:49:42 +10003263void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003264xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003265 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
3267 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3269 xfs_mount_t *mp; /* mount point structure */
3270 int nullfb; /* true if ap->firstblock isn't set */
3271 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
3273#define ISVALID(x,y) \
3274 (rt ? \
3275 (x) < mp->m_sb.sb_rblocks : \
3276 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3277 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3278 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003281 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003282 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3283 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003284 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285 /*
3286 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003287 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003289 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3290 !isnullstartblock(ap->prev.br_startblock) &&
3291 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3292 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003293 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 /*
3295 * Adjust for the gap between prevp and us.
3296 */
Dave Chinner3a756672011-09-18 20:40:58 +00003297 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003298 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003300 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3301 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 }
3303 /*
3304 * If not at eof, then compare the two neighbor blocks.
3305 * Figure out whether either one gives us a good starting point,
3306 * and pick the better one.
3307 */
3308 else if (!ap->eof) {
3309 xfs_fsblock_t gotbno; /* right side block number */
3310 xfs_fsblock_t gotdiff=0; /* right side difference */
3311 xfs_fsblock_t prevbno; /* left side block number */
3312 xfs_fsblock_t prevdiff=0; /* left side difference */
3313
3314 /*
3315 * If there's a previous (left) block, select a requested
3316 * start block based on it.
3317 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003318 if (ap->prev.br_startoff != NULLFILEOFF &&
3319 !isnullstartblock(ap->prev.br_startblock) &&
3320 (prevbno = ap->prev.br_startblock +
3321 ap->prev.br_blockcount) &&
3322 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 /*
3324 * Calculate gap to end of previous block.
3325 */
Dave Chinner3a756672011-09-18 20:40:58 +00003326 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003327 (ap->prev.br_startoff +
3328 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 /*
3330 * Figure the startblock based on the previous block's
3331 * end and the gap size.
3332 * Heuristic!
3333 * If the gap is large relative to the piece we're
3334 * allocating, or using it gives us an invalid block
3335 * number, then just use the end of the previous block.
3336 */
Dave Chinner3a756672011-09-18 20:40:58 +00003337 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003339 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 prevbno += adjust;
3341 else
3342 prevdiff += adjust;
3343 /*
3344 * If the firstblock forbids it, can't use it,
3345 * must use default.
3346 */
3347 if (!rt && !nullfb &&
3348 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3349 prevbno = NULLFSBLOCK;
3350 }
3351 /*
3352 * No previous block or can't follow it, just default.
3353 */
3354 else
3355 prevbno = NULLFSBLOCK;
3356 /*
3357 * If there's a following (right) block, select a requested
3358 * start block based on it.
3359 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003360 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 /*
3362 * Calculate gap to start of next block.
3363 */
Dave Chinner3a756672011-09-18 20:40:58 +00003364 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 /*
3366 * Figure the startblock based on the next block's
3367 * start and the gap size.
3368 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003369 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 /*
3371 * Heuristic!
3372 * If the gap is large relative to the piece we're
3373 * allocating, or using it gives us an invalid block
3374 * number, then just use the start of the next block
3375 * offset by our length.
3376 */
Dave Chinner3a756672011-09-18 20:40:58 +00003377 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 ISVALID(gotbno - gotdiff, gotbno))
3379 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003380 else if (ISVALID(gotbno - ap->length, gotbno)) {
3381 gotbno -= ap->length;
3382 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 } else
3384 gotdiff += adjust;
3385 /*
3386 * If the firstblock forbids it, can't use it,
3387 * must use default.
3388 */
3389 if (!rt && !nullfb &&
3390 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3391 gotbno = NULLFSBLOCK;
3392 }
3393 /*
3394 * No next block, just default.
3395 */
3396 else
3397 gotbno = NULLFSBLOCK;
3398 /*
3399 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003400 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 */
3402 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003403 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003405 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003407 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003409#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003410}
3411
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003412static int
3413xfs_bmap_longest_free_extent(
3414 struct xfs_trans *tp,
3415 xfs_agnumber_t ag,
3416 xfs_extlen_t *blen,
3417 int *notinit)
3418{
3419 struct xfs_mount *mp = tp->t_mountp;
3420 struct xfs_perag *pag;
3421 xfs_extlen_t longest;
3422 int error = 0;
3423
3424 pag = xfs_perag_get(mp, ag);
3425 if (!pag->pagf_init) {
3426 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3427 if (error)
3428 goto out;
3429
3430 if (!pag->pagf_init) {
3431 *notinit = 1;
3432 goto out;
3433 }
3434 }
3435
Dave Chinner50adbcb2015-06-22 10:04:31 +10003436 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003437 xfs_alloc_min_freelist(mp, pag),
3438 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003439 if (*blen < longest)
3440 *blen = longest;
3441
3442out:
3443 xfs_perag_put(pag);
3444 return error;
3445}
3446
3447static void
3448xfs_bmap_select_minlen(
3449 struct xfs_bmalloca *ap,
3450 struct xfs_alloc_arg *args,
3451 xfs_extlen_t *blen,
3452 int notinit)
3453{
3454 if (notinit || *blen < ap->minlen) {
3455 /*
3456 * Since we did a BUF_TRYLOCK above, it is possible that
3457 * there is space for this request.
3458 */
3459 args->minlen = ap->minlen;
3460 } else if (*blen < args->maxlen) {
3461 /*
3462 * If the best seen length is less than the request length,
3463 * use the best as the minimum.
3464 */
3465 args->minlen = *blen;
3466 } else {
3467 /*
3468 * Otherwise we've seen an extent as big as maxlen, use that
3469 * as the minimum.
3470 */
3471 args->minlen = args->maxlen;
3472 }
3473}
3474
Nathan Scotta365bdd2006-03-14 13:34:16 +11003475STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003476xfs_bmap_btalloc_nullfb(
3477 struct xfs_bmalloca *ap,
3478 struct xfs_alloc_arg *args,
3479 xfs_extlen_t *blen)
3480{
3481 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003482 xfs_agnumber_t ag, startag;
3483 int notinit = 0;
3484 int error;
3485
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003486 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003487 args->total = ap->total;
3488
Christoph Hellwigc467c042010-02-15 23:34:42 +00003489 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3490 if (startag == NULLAGNUMBER)
3491 startag = ag = 0;
3492
Dave Chinner14b064c2011-01-27 12:16:28 +11003493 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003494 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3495 &notinit);
3496 if (error)
3497 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003498
Christoph Hellwigc467c042010-02-15 23:34:42 +00003499 if (++ag == mp->m_sb.sb_agcount)
3500 ag = 0;
3501 if (ag == startag)
3502 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003503 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003504
3505 xfs_bmap_select_minlen(ap, args, blen, notinit);
3506 return 0;
3507}
3508
3509STATIC int
3510xfs_bmap_btalloc_filestreams(
3511 struct xfs_bmalloca *ap,
3512 struct xfs_alloc_arg *args,
3513 xfs_extlen_t *blen)
3514{
3515 struct xfs_mount *mp = ap->ip->i_mount;
3516 xfs_agnumber_t ag;
3517 int notinit = 0;
3518 int error;
3519
3520 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3521 args->total = ap->total;
3522
3523 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3524 if (ag == NULLAGNUMBER)
3525 ag = 0;
3526
3527 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3528 if (error)
3529 return error;
3530
3531 if (*blen < args->maxlen) {
3532 error = xfs_filestream_new_ag(ap, &ag);
3533 if (error)
3534 return error;
3535
3536 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3537 &notinit);
3538 if (error)
3539 return error;
3540
3541 }
3542
3543 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003544
3545 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003546 * Set the failure fallback case to look in the selected AG as stream
3547 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003548 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003549 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003550 return 0;
3551}
3552
3553STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003554xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003555 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003556{
3557 xfs_mount_t *mp; /* mount point structure */
3558 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003559 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003560 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003561 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003562 xfs_alloc_arg_t args;
3563 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003564 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003565 int nullfb; /* true if ap->firstblock isn't set */
3566 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003567 int tryagain;
3568 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003569 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003570
Dave Chinnera99ebf42011-12-01 11:24:20 +00003571 ASSERT(ap->length);
3572
Nathan Scotta365bdd2006-03-14 13:34:16 +11003573 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003574
3575 /* stripe alignment for allocation is determined by mount parameters */
3576 stripe_align = 0;
3577 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3578 stripe_align = mp->m_swidth;
3579 else if (mp->m_dalign)
3580 stripe_align = mp->m_dalign;
3581
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003582 if (ap->flags & XFS_BMAPI_COWFORK)
3583 align = xfs_get_cowextsz_hint(ap->ip);
3584 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003585 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003586 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003587 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003588 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003589 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003590 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003591 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003592 }
Dave Chinner33177f052013-12-12 16:34:36 +11003593
3594
Dave Chinner0937e0f2011-09-18 20:40:57 +00003595 nullfb = *ap->firstblock == NULLFSBLOCK;
3596 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003597 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003598 if (xfs_alloc_is_userdata(ap->datatype) &&
3599 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003600 ag = xfs_filestream_lookup_ag(ap->ip);
3601 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003602 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003603 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003604 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003605 }
3606 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003607 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003608
3609 xfs_bmap_adjacent(ap);
3610
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003612 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 * it's in the right allocation group.
3614 */
Dave Chinner3a756672011-09-18 20:40:58 +00003615 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 ;
3617 else
Dave Chinner3a756672011-09-18 20:40:58 +00003618 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 * Normal allocation, done through xfs_alloc_vextent.
3621 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003622 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003623 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003624 args.tp = ap->tp;
3625 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003626 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003627 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003628
3629 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003630 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003631 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003632 blen = 0;
3633 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003634 /*
3635 * Search for an allocation group with a single extent large
3636 * enough for the request. If one isn't found, then adjust
3637 * the minimum allocation size to the largest space found.
3638 */
Dave Chinner292378e2016-09-26 08:21:28 +10003639 if (xfs_alloc_is_userdata(ap->datatype) &&
3640 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003641 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3642 else
3643 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003644 if (error)
3645 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003646 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003647 if (xfs_inode_is_filestream(ap->ip))
3648 args.type = XFS_ALLOCTYPE_FIRST_AG;
3649 else
3650 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003651 args.total = args.minlen = ap->minlen;
3652 } else {
3653 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3654 args.total = ap->total;
3655 args.minlen = ap->minlen;
3656 }
David Chinner957d0eb2007-06-18 16:50:37 +10003657 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003658 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003659 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003660 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003661 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003662 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003663 args.prod = 1;
3664 args.mod = 0;
3665 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003666 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003667 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003668 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3669 }
3670 /*
3671 * If we are not low on available data blocks, and the
3672 * underlying logical volume manager is a stripe, and
3673 * the file offset is zero then try to allocate data
3674 * blocks on stripe unit boundary.
3675 * NOTE: ap->aeof is only set if the allocation length
3676 * is >= the stripe unit and the allocation offset is
3677 * at the end of file.
3678 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003679 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003680 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003681 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003682 atype = args.type;
3683 isaligned = 1;
3684 /*
3685 * Adjust for alignment
3686 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003687 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003688 args.minlen = blen - args.alignment;
3689 args.minalignslop = 0;
3690 } else {
3691 /*
3692 * First try an exact bno allocation.
3693 * If it fails then do a near or start bno
3694 * allocation with alignment turned on.
3695 */
3696 atype = args.type;
3697 tryagain = 1;
3698 args.type = XFS_ALLOCTYPE_THIS_BNO;
3699 args.alignment = 1;
3700 /*
3701 * Compute the minlen+alignment for the
3702 * next case. Set slop so that the value
3703 * of minlen+alignment+slop doesn't go up
3704 * between the calls.
3705 */
Dave Chinner33177f052013-12-12 16:34:36 +11003706 if (blen > stripe_align && blen <= args.maxlen)
3707 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003708 else
3709 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003710 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003711 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003712 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003713 args.minlen - 1;
3714 else
3715 args.minalignslop = 0;
3716 }
3717 } else {
3718 args.alignment = 1;
3719 args.minalignslop = 0;
3720 }
3721 args.minleft = ap->minleft;
3722 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003723 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003724 args.datatype = ap->datatype;
3725 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003726 args.ip = ap->ip;
3727
3728 error = xfs_alloc_vextent(&args);
3729 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003730 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003731
Nathan Scotta365bdd2006-03-14 13:34:16 +11003732 if (tryagain && args.fsbno == NULLFSBLOCK) {
3733 /*
3734 * Exact allocation failed. Now try with alignment
3735 * turned on.
3736 */
3737 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003738 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003739 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003740 args.minlen = nextminlen;
3741 args.minalignslop = 0;
3742 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 if ((error = xfs_alloc_vextent(&args)))
3744 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003745 }
3746 if (isaligned && args.fsbno == NULLFSBLOCK) {
3747 /*
3748 * allocation failed, so turn off alignment and
3749 * try again.
3750 */
3751 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003752 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003753 args.alignment = 0;
3754 if ((error = xfs_alloc_vextent(&args)))
3755 return error;
3756 }
3757 if (args.fsbno == NULLFSBLOCK && nullfb &&
3758 args.minlen > ap->minlen) {
3759 args.minlen = ap->minlen;
3760 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003761 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003762 if ((error = xfs_alloc_vextent(&args)))
3763 return error;
3764 }
3765 if (args.fsbno == NULLFSBLOCK && nullfb) {
3766 args.fsbno = 0;
3767 args.type = XFS_ALLOCTYPE_FIRST_AG;
3768 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003769 if ((error = xfs_alloc_vextent(&args)))
3770 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003771 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003772 }
3773 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003774 /*
3775 * check the allocation happened at the same or higher AG than
3776 * the first block that was allocated.
3777 */
3778 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003779 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3780 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003781
Dave Chinner3a756672011-09-18 20:40:58 +00003782 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003783 if (*ap->firstblock == NULLFSBLOCK)
3784 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003785 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003786 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003787 if (!(ap->flags & XFS_BMAPI_COWFORK))
3788 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003789 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3790 if (ap->wasdel)
3791 ap->ip->i_delayed_blks -= args.len;
3792 /*
3793 * Adjust the disk quota also. This was reserved
3794 * earlier.
3795 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003796 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003797 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3798 XFS_TRANS_DQ_BCOUNT,
3799 (long) args.len);
3800 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003801 ap->blkno = NULLFSBLOCK;
3802 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 }
3804 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003805}
3806
3807/*
3808 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3809 * It figures out where to ask the underlying allocator to put the new extent.
3810 */
3811STATIC int
3812xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003813 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003814{
Dave Chinner292378e2016-09-26 08:21:28 +10003815 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3816 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003817 return xfs_bmap_rtalloc(ap);
3818 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819}
3820
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003821/* Trim extent to fit a logical block range. */
3822void
3823xfs_trim_extent(
3824 struct xfs_bmbt_irec *irec,
3825 xfs_fileoff_t bno,
3826 xfs_filblks_t len)
3827{
3828 xfs_fileoff_t distance;
3829 xfs_fileoff_t end = bno + len;
3830
3831 if (irec->br_startoff + irec->br_blockcount <= bno ||
3832 irec->br_startoff >= end) {
3833 irec->br_blockcount = 0;
3834 return;
3835 }
3836
3837 if (irec->br_startoff < bno) {
3838 distance = bno - irec->br_startoff;
3839 if (isnullstartblock(irec->br_startblock))
3840 irec->br_startblock = DELAYSTARTBLOCK;
3841 if (irec->br_startblock != DELAYSTARTBLOCK &&
3842 irec->br_startblock != HOLESTARTBLOCK)
3843 irec->br_startblock += distance;
3844 irec->br_startoff += distance;
3845 irec->br_blockcount -= distance;
3846 }
3847
3848 if (end < irec->br_startoff + irec->br_blockcount) {
3849 distance = irec->br_startoff + irec->br_blockcount - end;
3850 irec->br_blockcount -= distance;
3851 }
3852}
3853
Brian Foster40214d12017-10-13 09:47:46 -07003854/* trim extent to within eof */
3855void
3856xfs_trim_extent_eof(
3857 struct xfs_bmbt_irec *irec,
3858 struct xfs_inode *ip)
3859
3860{
3861 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3862 i_size_read(VFS_I(ip))));
3863}
3864
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003866 * Trim the returned map to the required bounds
3867 */
3868STATIC void
3869xfs_bmapi_trim_map(
3870 struct xfs_bmbt_irec *mval,
3871 struct xfs_bmbt_irec *got,
3872 xfs_fileoff_t *bno,
3873 xfs_filblks_t len,
3874 xfs_fileoff_t obno,
3875 xfs_fileoff_t end,
3876 int n,
3877 int flags)
3878{
3879 if ((flags & XFS_BMAPI_ENTIRE) ||
3880 got->br_startoff + got->br_blockcount <= obno) {
3881 *mval = *got;
3882 if (isnullstartblock(got->br_startblock))
3883 mval->br_startblock = DELAYSTARTBLOCK;
3884 return;
3885 }
3886
3887 if (obno > *bno)
3888 *bno = obno;
3889 ASSERT((*bno >= obno) || (n == 0));
3890 ASSERT(*bno < end);
3891 mval->br_startoff = *bno;
3892 if (isnullstartblock(got->br_startblock))
3893 mval->br_startblock = DELAYSTARTBLOCK;
3894 else
3895 mval->br_startblock = got->br_startblock +
3896 (*bno - got->br_startoff);
3897 /*
3898 * Return the minimum of what we got and what we asked for for
3899 * the length. We can use the len variable here because it is
3900 * modified below and we could have been there before coming
3901 * here if the first part of the allocation didn't overlap what
3902 * was asked for.
3903 */
3904 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3905 got->br_blockcount - (*bno - got->br_startoff));
3906 mval->br_state = got->br_state;
3907 ASSERT(mval->br_blockcount <= len);
3908 return;
3909}
3910
3911/*
3912 * Update and validate the extent map to return
3913 */
3914STATIC void
3915xfs_bmapi_update_map(
3916 struct xfs_bmbt_irec **map,
3917 xfs_fileoff_t *bno,
3918 xfs_filblks_t *len,
3919 xfs_fileoff_t obno,
3920 xfs_fileoff_t end,
3921 int *n,
3922 int flags)
3923{
3924 xfs_bmbt_irec_t *mval = *map;
3925
3926 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3927 ((mval->br_startoff + mval->br_blockcount) <= end));
3928 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3929 (mval->br_startoff < obno));
3930
3931 *bno = mval->br_startoff + mval->br_blockcount;
3932 *len = end - *bno;
3933 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3934 /* update previous map with new information */
3935 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3936 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3937 ASSERT(mval->br_state == mval[-1].br_state);
3938 mval[-1].br_blockcount = mval->br_blockcount;
3939 mval[-1].br_state = mval->br_state;
3940 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3941 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3942 mval[-1].br_startblock != HOLESTARTBLOCK &&
3943 mval->br_startblock == mval[-1].br_startblock +
3944 mval[-1].br_blockcount &&
3945 ((flags & XFS_BMAPI_IGSTATE) ||
3946 mval[-1].br_state == mval->br_state)) {
3947 ASSERT(mval->br_startoff ==
3948 mval[-1].br_startoff + mval[-1].br_blockcount);
3949 mval[-1].br_blockcount += mval->br_blockcount;
3950 } else if (*n > 0 &&
3951 mval->br_startblock == DELAYSTARTBLOCK &&
3952 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3953 mval->br_startoff ==
3954 mval[-1].br_startoff + mval[-1].br_blockcount) {
3955 mval[-1].br_blockcount += mval->br_blockcount;
3956 mval[-1].br_state = mval->br_state;
3957 } else if (!((*n == 0) &&
3958 ((mval->br_startoff + mval->br_blockcount) <=
3959 obno))) {
3960 mval++;
3961 (*n)++;
3962 }
3963 *map = mval;
3964}
3965
3966/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003967 * Map file blocks to filesystem blocks without allocation.
3968 */
3969int
3970xfs_bmapi_read(
3971 struct xfs_inode *ip,
3972 xfs_fileoff_t bno,
3973 xfs_filblks_t len,
3974 struct xfs_bmbt_irec *mval,
3975 int *nmap,
3976 int flags)
3977{
3978 struct xfs_mount *mp = ip->i_mount;
3979 struct xfs_ifork *ifp;
3980 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003981 xfs_fileoff_t obno;
3982 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003983 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003984 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003985 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003986 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003987 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003988
3989 ASSERT(*nmap >= 1);
3990 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003991 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003992 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003993
3994 if (unlikely(XFS_TEST_ERROR(
3995 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3996 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003997 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003998 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003999 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004000 }
4001
4002 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004003 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004004
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004005 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004006
4007 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004008
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004009 /* No CoW fork? Return a hole. */
4010 if (whichfork == XFS_COW_FORK && !ifp) {
4011 mval->br_startoff = bno;
4012 mval->br_startblock = HOLESTARTBLOCK;
4013 mval->br_blockcount = len;
4014 mval->br_state = XFS_EXT_NORM;
4015 *nmap = 1;
4016 return 0;
4017 }
4018
Dave Chinner5c8ed202011-09-18 20:40:45 +00004019 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4020 error = xfs_iread_extents(NULL, ip, whichfork);
4021 if (error)
4022 return error;
4023 }
4024
Christoph Hellwig334f3422016-11-24 11:39:43 +11004025 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4026 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004027 end = bno + len;
4028 obno = bno;
4029
4030 while (bno < end && n < *nmap) {
4031 /* Reading past eof, act as though there's a hole up to end. */
4032 if (eof)
4033 got.br_startoff = end;
4034 if (got.br_startoff > bno) {
4035 /* Reading in a hole. */
4036 mval->br_startoff = bno;
4037 mval->br_startblock = HOLESTARTBLOCK;
4038 mval->br_blockcount =
4039 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4040 mval->br_state = XFS_EXT_NORM;
4041 bno += mval->br_blockcount;
4042 len -= mval->br_blockcount;
4043 mval++;
4044 n++;
4045 continue;
4046 }
4047
4048 /* set up the extent map to return. */
4049 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4050 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4051
4052 /* If we're done, stop now. */
4053 if (bno >= end || n >= *nmap)
4054 break;
4055
4056 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004057 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4058 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004059 }
4060 *nmap = n;
4061 return 0;
4062}
4063
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004064/*
4065 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4066 * global pool and the extent inserted into the inode in-core extent tree.
4067 *
4068 * On entry, got refers to the first extent beyond the offset of the extent to
4069 * allocate or eof is specified if no such extent exists. On return, got refers
4070 * to the extent record that was inserted to the inode fork.
4071 *
4072 * Note that the allocated extent may have been merged with contiguous extents
4073 * during insertion into the inode fork. Thus, got does not reflect the current
4074 * state of the inode fork on return. If necessary, the caller can use lastx to
4075 * look up the updated record in the inode fork.
4076 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10004077int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004078xfs_bmapi_reserve_delalloc(
4079 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004080 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004081 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004082 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004083 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004084 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004085 xfs_extnum_t *lastx,
4086 int eof)
4087{
4088 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004089 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004090 xfs_extlen_t alen;
4091 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004092 char rt = XFS_IS_REALTIME_INODE(ip);
4093 xfs_extlen_t extsz;
4094 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004095 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004096
Brian Foster974ae922016-11-28 14:57:42 +11004097 /*
4098 * Cap the alloc length. Keep track of prealloc so we know whether to
4099 * tag the inode before we return.
4100 */
4101 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004102 if (!eof)
4103 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004104 if (prealloc && alen >= len)
4105 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004106
4107 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004108 if (whichfork == XFS_COW_FORK)
4109 extsz = xfs_get_cowextsz_hint(ip);
4110 else
4111 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004112 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004113 struct xfs_bmbt_irec prev;
4114
4115 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4116 prev.br_startoff = NULLFILEOFF;
4117
4118 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004119 1, 0, &aoff, &alen);
4120 ASSERT(!error);
4121 }
4122
4123 if (rt)
4124 extsz = alen / mp->m_sb.sb_rextsize;
4125
4126 /*
4127 * Make a transaction-less quota reservation for delayed allocation
4128 * blocks. This number gets adjusted later. We return if we haven't
4129 * allocated blocks already inside this loop.
4130 */
4131 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4132 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4133 if (error)
4134 return error;
4135
4136 /*
4137 * Split changing sb for alen and indlen since they could be coming
4138 * from different places.
4139 */
4140 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4141 ASSERT(indlen > 0);
4142
4143 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004144 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004145 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004146 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004147 }
4148
4149 if (error)
4150 goto out_unreserve_quota;
4151
Dave Chinner0d485ad2015-02-23 21:22:03 +11004152 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004153 if (error)
4154 goto out_unreserve_blocks;
4155
4156
4157 ip->i_delayed_blks += alen;
4158
4159 got->br_startoff = aoff;
4160 got->br_startblock = nullstartblock(indlen);
4161 got->br_blockcount = alen;
4162 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004163
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004164 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004165
Brian Foster974ae922016-11-28 14:57:42 +11004166 /*
4167 * Tag the inode if blocks were preallocated. Note that COW fork
4168 * preallocation can occur at the start or end of the extent, even when
4169 * prealloc == 0, so we must also check the aligned offset and length.
4170 */
4171 if (whichfork == XFS_DATA_FORK && prealloc)
4172 xfs_inode_set_eofblocks_tag(ip);
4173 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4174 xfs_inode_set_cowblocks_tag(ip);
4175
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004176 return 0;
4177
4178out_unreserve_blocks:
4179 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004180 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004181 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004182 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004183out_unreserve_quota:
4184 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004185 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004186 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4187 return error;
4188}
4189
Dave Chinnercf11da92014-07-15 07:08:24 +10004190static int
4191xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004192 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004193{
4194 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004195 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004196 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004197 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004198 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004199
Dave Chinnera99ebf42011-12-01 11:24:20 +00004200 ASSERT(bma->length > 0);
4201
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004202 /*
4203 * For the wasdelay case, we could also just allocate the stuff asked
4204 * for in this bmap call but that wouldn't be as good.
4205 */
4206 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004207 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4208 bma->offset = bma->got.br_startoff;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004209 if (bma->idx)
4210 xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004211 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004212 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004213 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004214 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004215 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004216 }
4217
4218 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004219 * Set the data type being allocated. For the data fork, the first data
4220 * in the file is treated differently to all other allocations. For the
4221 * attribute fork, we only need to ensure the allocated range is not on
4222 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004223 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004224 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004225 bma->datatype = XFS_ALLOC_NOBUSY;
4226 if (whichfork == XFS_DATA_FORK) {
4227 if (bma->offset == 0)
4228 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4229 else
4230 bma->datatype |= XFS_ALLOC_USERDATA;
4231 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004232 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004233 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004234 }
4235
Dave Chinnere04426b2012-10-05 11:06:59 +10004236 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004237
4238 /*
4239 * Only want to do the alignment at the eof if it is userdata and
4240 * allocation length is larger than a stripe unit.
4241 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004242 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004243 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004244 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004245 if (error)
4246 return error;
4247 }
4248
4249 error = xfs_bmap_alloc(bma);
4250 if (error)
4251 return error;
4252
Dave Chinner29c8d172011-09-18 20:41:00 +00004253 if (bma->cur)
4254 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004255 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004256 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004257 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4258 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4259 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004260 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004261 }
4262 /*
4263 * Bump the number of extents we've allocated
4264 * in this call.
4265 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004266 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004267
Dave Chinner29c8d172011-09-18 20:41:00 +00004268 if (bma->cur)
4269 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004270 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4271
Dave Chinner963c30c2011-09-18 20:40:59 +00004272 bma->got.br_startoff = bma->offset;
4273 bma->got.br_startblock = bma->blkno;
4274 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004275 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004276
4277 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004278 * In the data fork, a wasdelay extent has been initialized, so
4279 * shouldn't be flagged as unwritten.
4280 *
4281 * For the cow fork, however, we convert delalloc reservations
4282 * (extents allocated for speculative preallocation) to
4283 * allocated unwritten extents, and only convert the unwritten
4284 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004285 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004286 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4287 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004288 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004289 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004290
Christoph Hellwigc6534242011-09-18 20:41:05 +00004291 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004292 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004293 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004294 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4295 whichfork, &bma->idx, &bma->cur, &bma->got,
4296 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004297
Christoph Hellwigc315c902011-09-18 20:41:02 +00004298 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004299 if (error)
4300 return error;
4301
4302 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004303 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4304 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4305 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004306 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004307 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004308
Dave Chinner963c30c2011-09-18 20:40:59 +00004309 ASSERT(bma->got.br_startoff <= bma->offset);
4310 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4311 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004312 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4313 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004314 return 0;
4315}
4316
Dave Chinnerb447fe52011-09-18 20:40:51 +00004317STATIC int
4318xfs_bmapi_convert_unwritten(
4319 struct xfs_bmalloca *bma,
4320 struct xfs_bmbt_irec *mval,
4321 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004322 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004323{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004324 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004325 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004326 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004327 int error;
4328
Dave Chinnerb447fe52011-09-18 20:40:51 +00004329 /* check if we need to do unwritten->real conversion */
4330 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4331 (flags & XFS_BMAPI_PREALLOC))
4332 return 0;
4333
4334 /* check if we need to do real->unwritten conversion */
4335 if (mval->br_state == XFS_EXT_NORM &&
4336 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4337 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4338 return 0;
4339
4340 /*
4341 * Modify (by adding) the state flag, if writing.
4342 */
4343 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004344 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4345 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004346 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004347 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004348 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004349 }
4350 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4351 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4352
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004353 /*
4354 * Before insertion into the bmbt, zero the range being converted
4355 * if required.
4356 */
4357 if (flags & XFS_BMAPI_ZERO) {
4358 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4359 mval->br_blockcount);
4360 if (error)
4361 return error;
4362 }
4363
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004364 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4365 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004366 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004367 /*
4368 * Log the inode core unconditionally in the unwritten extent conversion
4369 * path because the conversion might not have done so (e.g., if the
4370 * extent count hasn't changed). We need to make sure the inode is dirty
4371 * in the transaction for the sake of fsync(), even if nothing has
4372 * changed, because fsync() will not force the log for this transaction
4373 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004374 *
4375 * Note: If we're only converting cow fork extents, there aren't
4376 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004377 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004378 if (whichfork != XFS_COW_FORK)
4379 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004380 if (error)
4381 return error;
4382
4383 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004384 * Update our extent pointer, given that
4385 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4386 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004387 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004388 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004389
4390 /*
4391 * We may have combined previously unwritten space with written space,
4392 * so generate another request.
4393 */
4394 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004395 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004396 return 0;
4397}
4398
Christoph Hellwig44032802011-09-18 20:40:48 +00004399/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004400 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4401 * extent state if necessary. Details behaviour is controlled by the flags
4402 * parameter. Only allocates blocks from a single allocation group, to avoid
4403 * locking problems.
4404 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 * The returned value in "firstblock" from the first call in a transaction
4406 * must be remembered and presented to subsequent calls in "firstblock".
4407 * An upper bound for the number of blocks to be allocated is supplied to
4408 * the first call in "total"; if no allocation group has that many free
4409 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4410 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004411int
4412xfs_bmapi_write(
4413 struct xfs_trans *tp, /* transaction pointer */
4414 struct xfs_inode *ip, /* incore inode */
4415 xfs_fileoff_t bno, /* starting file offs. mapped */
4416 xfs_filblks_t len, /* length to map in file */
4417 int flags, /* XFS_BMAPI_... */
4418 xfs_fsblock_t *firstblock, /* first allocated block
4419 controls a.g. for allocs */
4420 xfs_extlen_t total, /* total blocks needed */
4421 struct xfs_bmbt_irec *mval, /* output: map values */
4422 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004423 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004425 struct xfs_mount *mp = ip->i_mount;
4426 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004427 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004428 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004429 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004430 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004431 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004432 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004433 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004436 xfs_fileoff_t orig_bno; /* original block number value */
4437 int orig_flags; /* original flags arg value */
4438 xfs_filblks_t orig_len; /* original value of len arg */
4439 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4440 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441
4442 orig_bno = bno;
4443 orig_len = len;
4444 orig_flags = flags;
4445 orig_mval = mval;
4446 orig_nmap = *nmap;
4447#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004448 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004449
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004451 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4452 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004453 ASSERT(tp != NULL ||
4454 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4455 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004456 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004457 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004458 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004459 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004460
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004461 /* zeroing is for currently only for data extents, not metadata */
4462 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4463 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4464 /*
4465 * we can allocate unwritten extents or pre-zero allocated blocks,
4466 * but it makes no sense to do both at once. This would result in
4467 * zeroing the unwritten extent twice, but it still being an
4468 * unwritten extent....
4469 */
4470 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4471 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4472
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 if (unlikely(XFS_TEST_ERROR(
4474 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004475 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004476 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004477 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004478 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004480
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004482 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004485
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004486 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004487
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004488 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004490 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004492 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004493 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004494 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004495 }
4496
4497 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4498 error = xfs_iread_extents(tp, ip, whichfork);
4499 if (error)
4500 goto error0;
4501 }
4502
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 n = 0;
4504 end = bno + len;
4505 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004506
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004507 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4508 eof = true;
4509 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4510 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004511 bma.tp = tp;
4512 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004513 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004514 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004515 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004516 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004517
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004519 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004520
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004521 /* in hole or beyoned EOF? */
4522 if (eof || bma.got.br_startoff > bno) {
4523 if (flags & XFS_BMAPI_DELALLOC) {
4524 /*
4525 * For the COW fork we can reasonably get a
4526 * request for converting an extent that races
4527 * with other threads already having converted
4528 * part of it, as there converting COW to
4529 * regular blocks is not protected using the
4530 * IOLOCK.
4531 */
4532 ASSERT(flags & XFS_BMAPI_COWFORK);
4533 if (!(flags & XFS_BMAPI_COWFORK)) {
4534 error = -EIO;
4535 goto error0;
4536 }
4537
4538 if (eof || bno >= end)
4539 break;
4540 } else {
4541 need_alloc = true;
4542 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004543 } else if (isnullstartblock(bma.got.br_startblock)) {
4544 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004545 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004546
4547 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 * First, deal with the hole before the allocated space
4549 * that we found, if any.
4550 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004551 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004552 bma.eof = eof;
4553 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4554 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004555 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004556 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004557
Dave Chinnera99ebf42011-12-01 11:24:20 +00004558 /*
4559 * There's a 32/64 bit type mismatch between the
4560 * allocation length request (which can be 64 bits in
4561 * length) and the bma length request, which is
4562 * xfs_extlen_t and therefore 32 bits. Hence we have to
4563 * check for 32-bit overflows and handle them here.
4564 */
4565 if (len > (xfs_filblks_t)MAXEXTLEN)
4566 bma.length = MAXEXTLEN;
4567 else
4568 bma.length = len;
4569
4570 ASSERT(len > 0);
4571 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004572 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573 if (error)
4574 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004575 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004576 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004577
4578 /*
4579 * If this is a CoW allocation, record the data in
4580 * the refcount btree for orphan recovery.
4581 */
4582 if (whichfork == XFS_COW_FORK) {
4583 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4584 bma.blkno, bma.length);
4585 if (error)
4586 goto error0;
4587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004589
Dave Chinneraef9a892011-09-18 20:40:44 +00004590 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004591 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4592 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593
Dave Chinnerb447fe52011-09-18 20:40:51 +00004594 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004595 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004596 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004597 continue;
4598 if (error)
4599 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600
Dave Chinneraef9a892011-09-18 20:40:44 +00004601 /* update the extent map to return */
4602 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 /*
4605 * If we're done, stop now. Stop when we've allocated
4606 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4607 * the transaction may get too big.
4608 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004609 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004611
4612 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004613 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004614 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4615 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004618
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 /*
4620 * Transform from btree to extents, give it cur.
4621 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004622 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004623 int tmp_logflags = 0;
4624
Dave Chinner29c8d172011-09-18 20:41:00 +00004625 ASSERT(bma.cur);
4626 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004628 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 if (error)
4630 goto error0;
4631 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004632
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004634 XFS_IFORK_NEXTENTS(ip, whichfork) >
4635 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637error0:
4638 /*
4639 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004640 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004642 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004644 bma.logflags &= ~xfs_ilog_fext(whichfork);
4645 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004647 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 /*
4649 * Log whatever the flags say, even if error. Otherwise we might miss
4650 * detecting a case where the data is changed, there's an error,
4651 * and it's not logged so we don't shutdown when we should.
4652 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004653 if (bma.logflags)
4654 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004655
Dave Chinner29c8d172011-09-18 20:41:00 +00004656 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 if (!error) {
4658 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004659 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004661 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004662 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004664 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4666 }
4667 if (!error)
4668 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4669 orig_nmap, *nmap);
4670 return error;
4671}
4672
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004673static int
4674xfs_bmapi_remap(
4675 struct xfs_trans *tp,
4676 struct xfs_inode *ip,
4677 xfs_fileoff_t bno,
4678 xfs_filblks_t len,
4679 xfs_fsblock_t startblock,
4680 struct xfs_defer_ops *dfops)
4681{
4682 struct xfs_mount *mp = ip->i_mount;
4683 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4684 struct xfs_btree_cur *cur = NULL;
4685 xfs_fsblock_t firstblock = NULLFSBLOCK;
4686 struct xfs_bmbt_irec got;
4687 xfs_extnum_t idx;
4688 int logflags = 0, error;
4689
4690 ASSERT(len > 0);
4691 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4692 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4693
4694 if (unlikely(XFS_TEST_ERROR(
4695 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4696 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004697 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004698 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4699 return -EFSCORRUPTED;
4700 }
4701
4702 if (XFS_FORCED_SHUTDOWN(mp))
4703 return -EIO;
4704
4705 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4706 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4707 if (error)
4708 return error;
4709 }
4710
4711 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4712 /* make sure we only reflink into a hole. */
4713 ASSERT(got.br_startoff > bno);
4714 ASSERT(got.br_startoff - bno >= len);
4715 }
4716
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004717 ip->i_d.di_nblocks += len;
4718 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004719
4720 if (ifp->if_flags & XFS_IFBROOT) {
4721 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4722 cur->bc_private.b.firstblock = firstblock;
4723 cur->bc_private.b.dfops = dfops;
4724 cur->bc_private.b.flags = 0;
4725 }
4726
4727 got.br_startoff = bno;
4728 got.br_startblock = startblock;
4729 got.br_blockcount = len;
4730 got.br_state = XFS_EXT_NORM;
4731
4732 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4733 &got, &firstblock, dfops, &logflags);
4734 if (error)
4735 goto error0;
4736
4737 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4738 int tmp_logflags = 0;
4739
4740 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4741 &tmp_logflags, XFS_DATA_FORK);
4742 logflags |= tmp_logflags;
4743 }
4744
4745error0:
4746 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4747 logflags &= ~XFS_ILOG_DEXT;
4748 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4749 logflags &= ~XFS_ILOG_DBROOT;
4750
4751 if (logflags)
4752 xfs_trans_log_inode(tp, ip, logflags);
4753 if (cur) {
4754 xfs_btree_del_cursor(cur,
4755 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4756 }
4757 return error;
4758}
4759
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004761 * When a delalloc extent is split (e.g., due to a hole punch), the original
4762 * indlen reservation must be shared across the two new extents that are left
4763 * behind.
4764 *
4765 * Given the original reservation and the worst case indlen for the two new
4766 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c2016-03-15 11:42:47 +11004767 * reservation fairly across the two new extents. If necessary, steal available
4768 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4769 * ores == 1). The number of stolen blocks is returned. The availability and
4770 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004771 */
Brian Fosterd34999c2016-03-15 11:42:47 +11004772static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004773xfs_bmap_split_indlen(
4774 xfs_filblks_t ores, /* original res. */
4775 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c2016-03-15 11:42:47 +11004776 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4777 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004778{
4779 xfs_filblks_t len1 = *indlen1;
4780 xfs_filblks_t len2 = *indlen2;
4781 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c2016-03-15 11:42:47 +11004782 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004783 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004784
4785 /*
Brian Fosterd34999c2016-03-15 11:42:47 +11004786 * Steal as many blocks as we can to try and satisfy the worst case
4787 * indlen for both new extents.
4788 */
Brian Foster75d65362017-02-13 22:48:30 -08004789 if (ores < nres && avail)
4790 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4791 ores += stolen;
4792
4793 /* nothing else to do if we've satisfied the new reservation */
4794 if (ores >= nres)
4795 return stolen;
Brian Fosterd34999c2016-03-15 11:42:47 +11004796
4797 /*
Brian Foster75d65362017-02-13 22:48:30 -08004798 * We can't meet the total required reservation for the two extents.
4799 * Calculate the percent of the overall shortage between both extents
4800 * and apply this percentage to each of the requested indlen values.
4801 * This distributes the shortage fairly and reduces the chances that one
4802 * of the two extents is left with nothing when extents are repeatedly
4803 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004804 */
Brian Foster75d65362017-02-13 22:48:30 -08004805 resfactor = (ores * 100);
4806 do_div(resfactor, nres);
4807 len1 *= resfactor;
4808 do_div(len1, 100);
4809 len2 *= resfactor;
4810 do_div(len2, 100);
4811 ASSERT(len1 + len2 <= ores);
4812 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4813
4814 /*
4815 * Hand out the remainder to each extent. If one of the two reservations
4816 * is zero, we want to make sure that one gets a block first. The loop
4817 * below starts with len1, so hand len2 a block right off the bat if it
4818 * is zero.
4819 */
4820 ores -= (len1 + len2);
4821 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4822 if (ores && !len2 && *indlen2) {
4823 len2++;
4824 ores--;
4825 }
4826 while (ores) {
4827 if (len1 < *indlen1) {
4828 len1++;
4829 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004830 }
Brian Foster75d65362017-02-13 22:48:30 -08004831 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004832 break;
Brian Foster75d65362017-02-13 22:48:30 -08004833 if (len2 < *indlen2) {
4834 len2++;
4835 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004836 }
4837 }
4838
4839 *indlen1 = len1;
4840 *indlen2 = len2;
Brian Fosterd34999c2016-03-15 11:42:47 +11004841
4842 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004843}
4844
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004845int
4846xfs_bmap_del_extent_delay(
4847 struct xfs_inode *ip,
4848 int whichfork,
4849 xfs_extnum_t *idx,
4850 struct xfs_bmbt_irec *got,
4851 struct xfs_bmbt_irec *del)
4852{
4853 struct xfs_mount *mp = ip->i_mount;
4854 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4855 struct xfs_bmbt_irec new;
4856 int64_t da_old, da_new, da_diff = 0;
4857 xfs_fileoff_t del_endoff, got_endoff;
4858 xfs_filblks_t got_indlen, new_indlen, stolen;
4859 int error = 0, state = 0;
4860 bool isrt;
4861
4862 XFS_STATS_INC(mp, xs_del_exlist);
4863
4864 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4865 del_endoff = del->br_startoff + del->br_blockcount;
4866 got_endoff = got->br_startoff + got->br_blockcount;
4867 da_old = startblockval(got->br_startblock);
4868 da_new = 0;
4869
4870 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004871 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004872 ASSERT(del->br_blockcount > 0);
4873 ASSERT(got->br_startoff <= del->br_startoff);
4874 ASSERT(got_endoff >= del_endoff);
4875
4876 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004877 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004878
4879 do_div(rtexts, mp->m_sb.sb_rextsize);
4880 xfs_mod_frextents(mp, rtexts);
4881 }
4882
4883 /*
4884 * Update the inode delalloc counter now and wait to update the
4885 * sb counters as we might have to borrow some blocks for the
4886 * indirect block accounting.
4887 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004888 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4889 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004890 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004891 if (error)
4892 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004893 ip->i_delayed_blks -= del->br_blockcount;
4894
4895 if (whichfork == XFS_COW_FORK)
4896 state |= BMAP_COWFORK;
4897
4898 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004899 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004900 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004901 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004902
Christoph Hellwig0173c682017-10-17 14:16:22 -07004903 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4904 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004905 /*
4906 * Matches the whole extent. Delete the entry.
4907 */
4908 xfs_iext_remove(ip, *idx, 1, state);
4909 --*idx;
4910 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004911 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004912 /*
4913 * Deleting the first part of the extent.
4914 */
4915 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4916 got->br_startoff = del_endoff;
4917 got->br_blockcount -= del->br_blockcount;
4918 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4919 got->br_blockcount), da_old);
4920 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004921 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004922 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4923 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004924 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004925 /*
4926 * Deleting the last part of the extent.
4927 */
4928 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4929 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4930 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4931 got->br_blockcount), da_old);
4932 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004933 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004934 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4935 break;
4936 case 0:
4937 /*
4938 * Deleting the middle of the extent.
4939 *
4940 * Distribute the original indlen reservation across the two new
4941 * extents. Steal blocks from the deleted extent if necessary.
4942 * Stealing blocks simply fudges the fdblocks accounting below.
4943 * Warn if either of the new indlen reservations is zero as this
4944 * can lead to delalloc problems.
4945 */
4946 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4947
4948 got->br_blockcount = del->br_startoff - got->br_startoff;
4949 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4950
4951 new.br_blockcount = got_endoff - del_endoff;
4952 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4953
4954 WARN_ON_ONCE(!got_indlen || !new_indlen);
4955 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4956 del->br_blockcount);
4957
4958 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004959 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004960 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4961
4962 new.br_startoff = del_endoff;
4963 new.br_state = got->br_state;
4964 new.br_startblock = nullstartblock((int)new_indlen);
4965
4966 ++*idx;
4967 xfs_iext_insert(ip, *idx, 1, &new, state);
4968
4969 da_new = got_indlen + new_indlen - stolen;
4970 del->br_blockcount -= stolen;
4971 break;
4972 }
4973
4974 ASSERT(da_old >= da_new);
4975 da_diff = da_old - da_new;
4976 if (!isrt)
4977 da_diff += del->br_blockcount;
4978 if (da_diff)
4979 xfs_mod_fdblocks(mp, da_diff, false);
4980 return error;
4981}
4982
4983void
4984xfs_bmap_del_extent_cow(
4985 struct xfs_inode *ip,
4986 xfs_extnum_t *idx,
4987 struct xfs_bmbt_irec *got,
4988 struct xfs_bmbt_irec *del)
4989{
4990 struct xfs_mount *mp = ip->i_mount;
4991 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4992 struct xfs_bmbt_irec new;
4993 xfs_fileoff_t del_endoff, got_endoff;
4994 int state = BMAP_COWFORK;
4995
4996 XFS_STATS_INC(mp, xs_del_exlist);
4997
4998 del_endoff = del->br_startoff + del->br_blockcount;
4999 got_endoff = got->br_startoff + got->br_blockcount;
5000
5001 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11005002 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005003 ASSERT(del->br_blockcount > 0);
5004 ASSERT(got->br_startoff <= del->br_startoff);
5005 ASSERT(got_endoff >= del_endoff);
5006 ASSERT(!isnullstartblock(got->br_startblock));
5007
5008 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07005009 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005010 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07005011 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005012
Christoph Hellwig0173c682017-10-17 14:16:22 -07005013 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5014 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005015 /*
5016 * Matches the whole extent. Delete the entry.
5017 */
5018 xfs_iext_remove(ip, *idx, 1, state);
5019 --*idx;
5020 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07005021 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005022 /*
5023 * Deleting the first part of the extent.
5024 */
5025 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5026 got->br_startoff = del_endoff;
5027 got->br_blockcount -= del->br_blockcount;
5028 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005029 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005030 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5031 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07005032 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005033 /*
5034 * Deleting the last part of the extent.
5035 */
5036 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5037 got->br_blockcount -= del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005038 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005039 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5040 break;
5041 case 0:
5042 /*
5043 * Deleting the middle of the extent.
5044 */
5045 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5046 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005047 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005048 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5049
5050 new.br_startoff = del_endoff;
5051 new.br_blockcount = got_endoff - del_endoff;
5052 new.br_state = got->br_state;
5053 new.br_startblock = del->br_startblock + del->br_blockcount;
5054
5055 ++*idx;
5056 xfs_iext_insert(ip, *idx, 1, &new, state);
5057 break;
5058 }
5059}
5060
Brian Fostera9bd24a2016-03-15 11:42:46 +11005061/*
Dave Chinner9e5987a72013-02-25 12:31:26 +11005062 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07005063 * after removing space.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005064 */
5065STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005066xfs_bmap_del_extent_real(
Dave Chinner9e5987a72013-02-25 12:31:26 +11005067 xfs_inode_t *ip, /* incore inode pointer */
5068 xfs_trans_t *tp, /* current transaction pointer */
5069 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005070 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005071 xfs_btree_cur_t *cur, /* if null, not a btree */
5072 xfs_bmbt_irec_t *del, /* data to remove from extents */
5073 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005074 int whichfork, /* data or attr fork */
5075 int bflags) /* bmapi flags */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005076{
Dave Chinner9e5987a72013-02-25 12:31:26 +11005077 xfs_fsblock_t del_endblock=0; /* first block past del */
5078 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005079 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005080 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005081 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005082 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005083 xfs_fileoff_t got_endoff; /* first offset past got */
5084 int i; /* temp state */
5085 xfs_ifork_t *ifp; /* inode fork pointer */
5086 xfs_mount_t *mp; /* mount structure */
5087 xfs_filblks_t nblks; /* quota/sb block count */
5088 xfs_bmbt_irec_t new; /* new record to be inserted */
5089 /* REFERENCED */
5090 uint qfield; /* quota field to update */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005091 int state = 0;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005092 struct xfs_bmbt_irec old;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005093
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005094 mp = ip->i_mount;
5095 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005096
5097 if (whichfork == XFS_ATTR_FORK)
5098 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005099 else if (whichfork == XFS_COW_FORK)
5100 state |= BMAP_COWFORK;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005101
Dave Chinner9e5987a72013-02-25 12:31:26 +11005102 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005103 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005104 ASSERT(del->br_blockcount > 0);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005105 xfs_iext_get_extent(ifp, *idx, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005106 ASSERT(got.br_startoff <= del->br_startoff);
5107 del_endoff = del->br_startoff + del->br_blockcount;
5108 got_endoff = got.br_startoff + got.br_blockcount;
5109 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005110 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a72013-02-25 12:31:26 +11005111 qfield = 0;
5112 error = 0;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005113
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005114 /*
5115 * If it's the case where the directory code is running with no block
5116 * reservation, and the deleted block is in the middle of its extent,
5117 * and the resulting insert of an extent would cause transformation to
5118 * btree format, then reject it. The calling code will then swap blocks
5119 * around instead. We have to do this now, rather than waiting for the
5120 * conversion to btree format, since the transaction will be dirty then.
5121 */
5122 if (tp->t_blk_res == 0 &&
5123 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5124 XFS_IFORK_NEXTENTS(ip, whichfork) >=
5125 XFS_IFORK_MAXEXT(ip, whichfork) &&
5126 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5127 return -ENOSPC;
5128
5129 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005130 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5131 xfs_fsblock_t bno;
5132 xfs_filblks_t len;
5133
5134 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
5135 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
5136 bno = del->br_startblock;
5137 len = del->br_blockcount;
5138 do_div(bno, mp->m_sb.sb_rextsize);
5139 do_div(len, mp->m_sb.sb_rextsize);
5140 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5141 if (error)
5142 goto done;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005143 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005144 nblks = len * mp->m_sb.sb_rextsize;
5145 qfield = XFS_TRANS_DQ_RTBCOUNT;
5146 } else {
5147 do_fx = 1;
5148 nblks = del->br_blockcount;
5149 qfield = XFS_TRANS_DQ_BCOUNT;
5150 }
5151
5152 del_endblock = del->br_startblock + del->br_blockcount;
5153 if (cur) {
5154 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5155 got.br_startblock, got.br_blockcount, &i);
5156 if (error)
5157 goto done;
5158 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005159 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005160
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005161 if (got.br_startoff == del->br_startoff)
5162 state |= BMAP_LEFT_FILLING;
5163 if (got_endoff == del_endoff)
5164 state |= BMAP_RIGHT_FILLING;
5165
5166 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5167 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005168 /*
5169 * Matches the whole extent. Delete the entry.
5170 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005171 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005172 xfs_iext_remove(ip, *idx, 1,
5173 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5174 --*idx;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005175
5176 XFS_IFORK_NEXT_SET(ip, whichfork,
5177 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5178 flags |= XFS_ILOG_CORE;
5179 if (!cur) {
5180 flags |= xfs_ilog_fext(whichfork);
5181 break;
5182 }
5183 if ((error = xfs_btree_delete(cur, &i)))
5184 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005185 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005186 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005187 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005188 /*
5189 * Deleting the first part of the extent.
5190 */
5191 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005192 got.br_startoff = del_endoff;
5193 got.br_startblock = del_endblock;
5194 got.br_blockcount -= del->br_blockcount;
5195 xfs_iext_update_extent(ifp, *idx, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005196 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5197 if (!cur) {
5198 flags |= xfs_ilog_fext(whichfork);
5199 break;
5200 }
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005201 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5202 got.br_blockcount, got.br_state);
5203 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005204 goto done;
5205 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005206 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a72013-02-25 12:31:26 +11005207 /*
5208 * Deleting the last part of the extent.
5209 */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005210 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005211 got.br_blockcount -= del->br_blockcount;
5212 xfs_iext_update_extent(ifp, *idx, &got);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005213 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5214 if (!cur) {
5215 flags |= xfs_ilog_fext(whichfork);
5216 break;
5217 }
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005218 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5219 got.br_blockcount, got.br_state);
5220 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005221 goto done;
5222 break;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005223 case 0:
5224 /*
5225 * Deleting the middle of the extent.
5226 */
Dave Chinner9e5987a72013-02-25 12:31:26 +11005227 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005228
5229 old = got;
5230 got.br_blockcount = del->br_startoff - got.br_startoff;
5231 xfs_iext_update_extent(ifp, *idx, &got);
5232
Dave Chinner9e5987a72013-02-25 12:31:26 +11005233 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005234 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a72013-02-25 12:31:26 +11005235 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005236 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005237
Christoph Hellwige1d75532017-10-17 14:16:21 -07005238 flags |= XFS_ILOG_CORE;
5239 if (cur) {
5240 error = xfs_bmbt_update(cur, got.br_startoff,
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005241 got.br_startblock, got.br_blockcount,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005242 got.br_state);
5243 if (error)
5244 goto done;
5245 error = xfs_btree_increment(cur, 0, &i);
5246 if (error)
5247 goto done;
5248 cur->bc_rec.b = new;
5249 error = xfs_btree_insert(cur, &i);
5250 if (error && error != -ENOSPC)
5251 goto done;
5252 /*
5253 * If get no-space back from btree insert, it tried a
5254 * split, and we have a zero block reservation. Fix up
5255 * our state and return the error.
5256 */
5257 if (error == -ENOSPC) {
5258 /*
5259 * Reset the cursor, don't trust it after any
5260 * insert operation.
5261 */
5262 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005263 got.br_startblock,
5264 got.br_blockcount, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005265 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005266 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005267 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5268 /*
5269 * Update the btree record back
5270 * to the original value.
5271 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005272 error = xfs_bmbt_update(cur, old.br_startoff,
5273 old.br_startblock,
5274 old.br_blockcount,
5275 old.br_state);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005276 if (error)
Dave Chinner9e5987a72013-02-25 12:31:26 +11005277 goto done;
5278 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005279 * Reset the extent record back
5280 * to the original value.
Dave Chinner9e5987a72013-02-25 12:31:26 +11005281 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005282 xfs_iext_update_extent(ifp, *idx, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005283 flags = 0;
5284 error = -ENOSPC;
5285 goto done;
5286 }
5287 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5288 } else
5289 flags |= xfs_ilog_fext(whichfork);
5290 XFS_IFORK_NEXT_SET(ip, whichfork,
5291 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Dave Chinner9e5987a72013-02-25 12:31:26 +11005292 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5293 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5294 ++*idx;
5295 break;
5296 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005297
5298 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005299 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5300 if (error)
5301 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005302
Dave Chinner9e5987a72013-02-25 12:31:26 +11005303 /*
5304 * If we need to, add to list of extents to delete.
5305 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005306 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005307 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5308 error = xfs_refcount_decrease_extent(mp, dfops, del);
5309 if (error)
5310 goto done;
5311 } else
5312 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5313 del->br_blockcount, NULL);
5314 }
5315
Dave Chinner9e5987a72013-02-25 12:31:26 +11005316 /*
5317 * Adjust inode # blocks in the file.
5318 */
5319 if (nblks)
5320 ip->i_d.di_nblocks -= nblks;
5321 /*
5322 * Adjust quota data.
5323 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005324 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a72013-02-25 12:31:26 +11005325 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5326
Dave Chinner9e5987a72013-02-25 12:31:26 +11005327done:
5328 *logflagsp = flags;
5329 return error;
5330}
5331
5332/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 * Unmap (remove) blocks from a file.
5334 * If nexts is nonzero then the number of extents to remove is limited to
5335 * that value. If not all extents in the block range can be removed then
5336 * *done is set.
5337 */
5338int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005339__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 xfs_trans_t *tp, /* transaction pointer */
5341 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005342 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005343 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 int flags, /* misc flags */
5345 xfs_extnum_t nexts, /* number of extents max */
5346 xfs_fsblock_t *firstblock, /* first allocated block
5347 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005348 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349{
5350 xfs_btree_cur_t *cur; /* bmap btree cursor */
5351 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 int error; /* error return value */
5353 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005354 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 xfs_ifork_t *ifp; /* inode fork pointer */
5356 int isrt; /* freeing in rt area */
5357 xfs_extnum_t lastx; /* last extent index used */
5358 int logflags; /* transaction logging flags */
5359 xfs_extlen_t mod; /* rt extent offset */
5360 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 int tmp_logflags; /* partial logging flags */
5362 int wasdel; /* was a delayed alloc extent */
5363 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005365 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005366 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005367 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005368 xfs_fileoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005370 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005371
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005372 whichfork = xfs_bmapi_whichfork(flags);
5373 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 ifp = XFS_IFORK_PTR(ip, whichfork);
5375 if (unlikely(
5376 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5377 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5378 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5379 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005380 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 }
5382 mp = ip->i_mount;
5383 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005384 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005385
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005386 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 ASSERT(len > 0);
5388 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005389
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005390 /*
5391 * Guesstimate how many blocks we can unmap without running the risk of
5392 * blowing out the transaction with a mix of EFIs and reflink
5393 * adjustments.
5394 */
5395 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5396 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5397 else
5398 max_len = len;
5399
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5401 (error = xfs_iread_extents(tp, ip, whichfork)))
5402 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005403 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005404 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 return 0;
5406 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005407 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005408 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005409 end = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005410
Linus Torvalds1da177e2005-04-16 15:20:36 -07005411 /*
5412 * Check to see if the given block number is past the end of the
5413 * file, back up to the last block if so...
5414 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005415 if (!xfs_iext_lookup_extent(ip, ifp, end, &lastx, &got)) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005416 ASSERT(lastx > 0);
5417 xfs_iext_get_extent(ifp, --lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005418 end = got.br_startoff + got.br_blockcount - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005420
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 logflags = 0;
5422 if (ifp->if_flags & XFS_IFBROOT) {
5423 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005424 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005426 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 cur->bc_private.b.flags = 0;
5428 } else
5429 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005430
5431 if (isrt) {
5432 /*
5433 * Synchronize by locking the bitmap inode.
5434 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005435 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005436 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005437 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5438 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005439 }
5440
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 extno = 0;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005442 while (end != (xfs_fileoff_t)-1 && end >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005443 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005444 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005445 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005446 * Just back up to the previous extent, if so.
5447 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005448 if (got.br_startoff > end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 if (--lastx < 0)
5450 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005451 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 }
5453 /*
5454 * Is the last block of this extent before the range
5455 * we're supposed to delete? If so, we're done.
5456 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005457 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005459 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005460 break;
5461 /*
5462 * Then deal with the (possibly delayed) allocated space
5463 * we found.
5464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005465 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005466 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005467
5468 /*
5469 * Make sure we don't touch multiple AGF headers out of order
5470 * in a single transaction, as that could cause AB-BA deadlocks.
5471 */
5472 if (!wasdel) {
5473 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5474 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5475 break;
5476 prev_agno = agno;
5477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 if (got.br_startoff < start) {
5479 del.br_startoff = start;
5480 del.br_blockcount -= start - got.br_startoff;
5481 if (!wasdel)
5482 del.br_startblock += start - got.br_startoff;
5483 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005484 if (del.br_startoff + del.br_blockcount > end + 1)
5485 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005486
5487 /* How much can we safely unmap? */
5488 if (max_len < del.br_blockcount) {
5489 del.br_startoff += del.br_blockcount - max_len;
5490 if (!wasdel)
5491 del.br_startblock += del.br_blockcount - max_len;
5492 del.br_blockcount = max_len;
5493 }
5494
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 sum = del.br_startblock + del.br_blockcount;
5496 if (isrt &&
5497 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5498 /*
5499 * Realtime extent not lined up at the end.
5500 * The extent could have been split into written
5501 * and unwritten pieces, or we could just be
5502 * unmapping part of it. But we can't really
5503 * get rid of part of a realtime extent.
5504 */
5505 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005506 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 /*
5508 * This piece is unwritten, or we're not
5509 * using unwritten extents. Skip over it.
5510 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005511 ASSERT(end >= mod);
5512 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005513 del.br_blockcount : mod;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005514 if (end < got.br_startoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 if (--lastx >= 0)
Christoph Hellwige3f0f752017-10-17 14:16:20 -07005516 xfs_iext_get_extent(ifp, lastx,
5517 &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005518 }
5519 continue;
5520 }
5521 /*
5522 * It's written, turn it unwritten.
5523 * This is better than zeroing it.
5524 */
5525 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005526 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 /*
5528 * If this spans a realtime extent boundary,
5529 * chop it back to the start of the one we end at.
5530 */
5531 if (del.br_blockcount > mod) {
5532 del.br_startoff += del.br_blockcount - mod;
5533 del.br_startblock += del.br_blockcount - mod;
5534 del.br_blockcount = mod;
5535 }
5536 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005537 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005538 whichfork, &lastx, &cur, &del,
5539 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 if (error)
5541 goto error0;
5542 goto nodelete;
5543 }
5544 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5545 /*
5546 * Realtime extent is lined up at the end but not
5547 * at the front. We'll get rid of full extents if
5548 * we can.
5549 */
5550 mod = mp->m_sb.sb_rextsize - mod;
5551 if (del.br_blockcount > mod) {
5552 del.br_blockcount -= mod;
5553 del.br_startoff += mod;
5554 del.br_startblock += mod;
5555 } else if ((del.br_startoff == start &&
5556 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005557 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005558 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 /*
5560 * Can't make it unwritten. There isn't
5561 * a full extent here so just skip it.
5562 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005563 ASSERT(end >= del.br_blockcount);
5564 end -= del.br_blockcount;
5565 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005566 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 continue;
5568 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005569 struct xfs_bmbt_irec prev;
5570
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 /*
5572 * This one is already unwritten.
5573 * It must have a written left neighbor.
5574 * Unwrite the killed part of that one and
5575 * try again.
5576 */
5577 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005578 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005580 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581 ASSERT(del.br_startblock ==
5582 prev.br_startblock + prev.br_blockcount);
5583 if (prev.br_startoff < start) {
5584 mod = start - prev.br_startoff;
5585 prev.br_blockcount -= mod;
5586 prev.br_startblock += mod;
5587 prev.br_startoff = start;
5588 }
5589 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005590 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005591 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005592 ip, whichfork, &lastx, &cur,
5593 &prev, firstblock, dfops,
5594 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005595 if (error)
5596 goto error0;
5597 goto nodelete;
5598 } else {
5599 ASSERT(del.br_state == XFS_EXT_NORM);
5600 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005601 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005602 ip, whichfork, &lastx, &cur,
5603 &del, firstblock, dfops,
5604 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005605 if (error)
5606 goto error0;
5607 goto nodelete;
5608 }
5609 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005610
Brian Fosterb2706a02016-03-15 11:42:46 +11005611 if (wasdel) {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005612 error = xfs_bmap_del_extent_delay(ip, whichfork, &lastx,
5613 &got, &del);
5614 } else {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005615 error = xfs_bmap_del_extent_real(ip, tp, &lastx, dfops,
5616 cur, &del, &tmp_logflags, whichfork,
5617 flags);
5618 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005619 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005620
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621 if (error)
5622 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005623
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005624 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005625 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005627 /*
5628 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005630 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005631 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005632 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005633 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005634 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005636 extno++;
5637 }
5638 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005639 if (end == (xfs_fileoff_t)-1 || end < start || lastx < 0)
Darrick J. Wong44535932016-10-03 09:11:29 -07005640 *rlen = 0;
5641 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005642 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005643
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 /*
5645 * Convert to a btree if necessary.
5646 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005647 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005649 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 &cur, 0, &tmp_logflags, whichfork);
5651 logflags |= tmp_logflags;
5652 if (error)
5653 goto error0;
5654 }
5655 /*
5656 * transform from btree to extents, give it cur
5657 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005658 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 ASSERT(cur != NULL);
5660 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5661 whichfork);
5662 logflags |= tmp_logflags;
5663 if (error)
5664 goto error0;
5665 }
5666 /*
5667 * transform from extents to local?
5668 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669 error = 0;
5670error0:
5671 /*
5672 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005673 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005675 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005676 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005677 logflags &= ~xfs_ilog_fext(whichfork);
5678 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005679 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005680 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 /*
5682 * Log inode even in the error case, if the transaction
5683 * is dirty we'll need to shut down the filesystem.
5684 */
5685 if (logflags)
5686 xfs_trans_log_inode(tp, ip, logflags);
5687 if (cur) {
5688 if (!error) {
5689 *firstblock = cur->bc_private.b.firstblock;
5690 cur->bc_private.b.allocated = 0;
5691 }
5692 xfs_btree_del_cursor(cur,
5693 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5694 }
5695 return error;
5696}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005697
Darrick J. Wong44535932016-10-03 09:11:29 -07005698/* Unmap a range of a file. */
5699int
5700xfs_bunmapi(
5701 xfs_trans_t *tp,
5702 struct xfs_inode *ip,
5703 xfs_fileoff_t bno,
5704 xfs_filblks_t len,
5705 int flags,
5706 xfs_extnum_t nexts,
5707 xfs_fsblock_t *firstblock,
5708 struct xfs_defer_ops *dfops,
5709 int *done)
5710{
5711 int error;
5712
5713 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5714 dfops);
5715 *done = (len == 0);
5716 return error;
5717}
5718
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005719/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005720 * Determine whether an extent shift can be accomplished by a merge with the
5721 * extent that precedes the target hole of the shift.
5722 */
5723STATIC bool
5724xfs_bmse_can_merge(
5725 struct xfs_bmbt_irec *left, /* preceding extent */
5726 struct xfs_bmbt_irec *got, /* current extent to shift */
5727 xfs_fileoff_t shift) /* shift fsb */
5728{
5729 xfs_fileoff_t startoff;
5730
5731 startoff = got->br_startoff - shift;
5732
5733 /*
5734 * The extent, once shifted, must be adjacent in-file and on-disk with
5735 * the preceding extent.
5736 */
5737 if ((left->br_startoff + left->br_blockcount != startoff) ||
5738 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5739 (left->br_state != got->br_state) ||
5740 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5741 return false;
5742
5743 return true;
5744}
5745
5746/*
5747 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5748 * hole in the file. If an extent shift would result in the extent being fully
5749 * adjacent to the extent that currently precedes the hole, we can merge with
5750 * the preceding extent rather than do the shift.
5751 *
5752 * This function assumes the caller has verified a shift-by-merge is possible
5753 * with the provided extents via xfs_bmse_can_merge().
5754 */
5755STATIC int
5756xfs_bmse_merge(
5757 struct xfs_inode *ip,
5758 int whichfork,
5759 xfs_fileoff_t shift, /* shift fsb */
5760 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005761 struct xfs_bmbt_irec *got, /* extent to shift */
5762 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005763 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005764 int *logflags, /* output */
5765 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005766{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005767 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5768 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005769 xfs_filblks_t blockcount;
5770 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005771 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005772
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005773 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005774
5775 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5776 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005777 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005778
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005779 new = *left;
5780 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005781
5782 /*
5783 * Update the on-disk extent count, the btree if necessary and log the
5784 * inode.
5785 */
5786 XFS_IFORK_NEXT_SET(ip, whichfork,
5787 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5788 *logflags |= XFS_ILOG_CORE;
5789 if (!cur) {
5790 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005791 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005792 }
5793
5794 /* lookup and remove the extent to merge */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005795 error = xfs_bmbt_lookup_eq(cur, got->br_startoff, got->br_startblock,
5796 got->br_blockcount, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005797 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005798 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005799 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005800
5801 error = xfs_btree_delete(cur, &i);
5802 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005803 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005804 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005805
5806 /* lookup and update size of the previous extent */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005807 error = xfs_bmbt_lookup_eq(cur, left->br_startoff, left->br_startblock,
5808 left->br_blockcount, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005809 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005810 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005811 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005812
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005813 error = xfs_bmbt_update(cur, new.br_startoff, new.br_startblock,
5814 new.br_blockcount, new.br_state);
5815 if (error)
5816 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005817
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005818done:
5819 xfs_iext_update_extent(ifp, current_ext - 1, &new);
5820 xfs_iext_remove(ip, current_ext, 1, 0);
5821
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005822 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005823 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5824 if (error)
5825 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005826 memcpy(&new, got, sizeof(new));
5827 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005828 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005829}
5830
5831/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005832 * Shift a single extent.
5833 */
5834STATIC int
5835xfs_bmse_shift_one(
5836 struct xfs_inode *ip,
5837 int whichfork,
5838 xfs_fileoff_t offset_shift_fsb,
5839 int *current_ext,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005840 struct xfs_bmbt_irec *got,
Brian Fostera979bdf2014-09-23 15:39:04 +10005841 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005842 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005843 enum shift_direction direction,
5844 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005845{
5846 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005847 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005848 xfs_fileoff_t startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005849 struct xfs_bmbt_irec adj_irec, new;
Brian Fostera979bdf2014-09-23 15:39:04 +10005850 int error;
5851 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005852 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005853
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005854 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005855 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005856 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005857
Brian Fosterf71721d2014-09-23 15:39:05 +10005858 /* delalloc extents should be prevented by caller */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005859 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got->br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005860
Namjae Jeona904b1c2015-03-25 15:08:56 +11005861 if (direction == SHIFT_LEFT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005862 startoff = got->br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005863
Namjae Jeona904b1c2015-03-25 15:08:56 +11005864 /*
5865 * Check for merge if we've got an extent to the left,
5866 * otherwise make sure there's enough room at the start
5867 * of the file for the shift.
5868 */
5869 if (!*current_ext) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005870 if (got->br_startoff < offset_shift_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005871 return -EINVAL;
5872 goto update_current_ext;
5873 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005874
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005875 /*
5876 * grab the left extent and check for a large enough hole.
5877 */
5878 xfs_iext_get_extent(ifp, *current_ext - 1, &adj_irec);
5879 if (startoff < adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005880 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005881
Dave Chinnerb11bd672014-12-04 09:42:24 +11005882 /* check whether to merge the extent or shift it down */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005883 if (xfs_bmse_can_merge(&adj_irec, got, offset_shift_fsb)) {
5884 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5885 *current_ext, got, &adj_irec,
5886 cur, logflags, dfops);
Dave Chinnerb11bd672014-12-04 09:42:24 +11005887 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005888 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005889 startoff = got->br_startoff + offset_shift_fsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005890 /* nothing to move if this is the last extent */
5891 if (*current_ext >= (total_extents - 1))
5892 goto update_current_ext;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005893
Namjae Jeona904b1c2015-03-25 15:08:56 +11005894 /*
5895 * If this is not the last extent in the file, make sure there
5896 * is enough room between current extent and next extent for
5897 * accommodating the shift.
5898 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005899 xfs_iext_get_extent(ifp, *current_ext + 1, &adj_irec);
5900 if (startoff + got->br_blockcount > adj_irec.br_startoff)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005901 return -EINVAL;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005902
Namjae Jeona904b1c2015-03-25 15:08:56 +11005903 /*
5904 * Unlike a left shift (which involves a hole punch),
5905 * a right shift does not modify extent neighbors
5906 * in any way. We should never find mergeable extents
5907 * in this scenario. Check anyways and warn if we
5908 * encounter two extents that could be one.
5909 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005910 if (xfs_bmse_can_merge(got, &adj_irec, offset_shift_fsb))
Namjae Jeona904b1c2015-03-25 15:08:56 +11005911 WARN_ON_ONCE(1);
5912 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005913
Brian Fostera979bdf2014-09-23 15:39:04 +10005914 /*
5915 * Increment the extent index for the next iteration, update the start
5916 * offset of the in-core extent and update the btree if applicable.
5917 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005918update_current_ext:
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005919 *logflags |= XFS_ILOG_CORE;
5920
5921 new = *got;
5922 new.br_startoff = startoff;
5923
5924 if (cur) {
5925 error = xfs_bmbt_lookup_eq(cur, got->br_startoff,
5926 got->br_startblock, got->br_blockcount, &i);
5927 if (error)
5928 return error;
5929 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5930
5931 error = xfs_bmbt_update(cur, new.br_startoff,
5932 new.br_startblock, new.br_blockcount,
5933 new.br_state);
5934 if (error)
5935 return error;
5936 } else {
5937 *logflags |= XFS_ILOG_DEXT;
5938 }
5939
5940 xfs_iext_update_extent(ifp, *current_ext, &new);
5941
Namjae Jeona904b1c2015-03-25 15:08:56 +11005942 if (direction == SHIFT_LEFT)
5943 (*current_ext)++;
5944 else
5945 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005946
Darrick J. Wong9c194642016-08-03 12:16:05 +10005947 /* update reverse mapping */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005948 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005949 if (error)
5950 return error;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005951 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fostera979bdf2014-09-23 15:39:04 +10005952}
5953
5954/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005955 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005956 *
Brian Foster2c845f52014-09-23 15:37:09 +10005957 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005958 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10005959 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5960 * is the length by which each extent is shifted. If there is no hole to shift
5961 * the extents into, this will be considered invalid operation and we abort
5962 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005963 */
5964int
5965xfs_bmap_shift_extents(
5966 struct xfs_trans *tp,
5967 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005968 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005969 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005970 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005971 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005972 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005973 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005974 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005975 int num_exts)
5976{
Brian Fosterca446d82014-09-02 12:12:53 +10005977 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005978 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005979 struct xfs_mount *mp = ip->i_mount;
5980 struct xfs_ifork *ifp;
5981 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005982 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005983 xfs_extnum_t total_extents;
5984 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005985 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005986 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10005987 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005988
5989 if (unlikely(XFS_TEST_ERROR(
5990 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5991 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005992 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005993 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5994 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005995 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005996 }
5997
5998 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005999 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006000
Brian Foster2c845f52014-09-23 15:37:09 +10006001 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6002 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006003 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006004
6005 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006006 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6007 /* Read in all the extents */
6008 error = xfs_iread_extents(tp, ip, whichfork);
6009 if (error)
6010 return error;
6011 }
6012
Brian Fosterddb19e32014-09-23 15:38:09 +10006013 if (ifp->if_flags & XFS_IFBROOT) {
6014 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6015 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006016 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006017 cur->bc_private.b.flags = 0;
6018 }
6019
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006020 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006021 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006022 * are collapsing out, so we cannot use the count of real extents here.
6023 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006024 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006025 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006026 if (total_extents == 0) {
6027 *done = 1;
6028 goto del_cursor;
6029 }
6030
6031 /*
6032 * In case of first right shift, we need to initialize next_fsb
6033 */
6034 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006035 ASSERT(direction == SHIFT_RIGHT);
6036
6037 current_ext = total_extents - 1;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006038 xfs_iext_get_extent(ifp, current_ext, &got);
6039 if (stop_fsb > got.br_startoff) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006040 *done = 1;
6041 goto del_cursor;
6042 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006043 *next_fsb = got.br_startoff;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006044 } else {
6045 /*
6046 * Look up the extent index for the fsb where we start shifting. We can
6047 * henceforth iterate with current_ext as extent list changes are locked
6048 * out via ilock.
6049 *
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006050 * If next_fsb lies in a hole beyond which there are no extents we are
6051 * done.
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006052 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006053 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
6054 &got)) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006055 *done = 1;
6056 goto del_cursor;
6057 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006058 }
6059
6060 /* Lookup the extent index at which we have to stop */
6061 if (direction == SHIFT_RIGHT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006062 struct xfs_bmbt_irec s;
6063
6064 xfs_iext_lookup_extent(ip, ifp, stop_fsb, &stop_extent, &s);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006065 /* Make stop_extent exclusive of shift range */
6066 stop_extent--;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006067 if (current_ext <= stop_extent) {
6068 error = -EIO;
6069 goto del_cursor;
6070 }
6071 } else {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006072 stop_extent = total_extents;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006073 if (current_ext >= stop_extent) {
6074 error = -EIO;
6075 goto del_cursor;
6076 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006077 }
6078
6079 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006080 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006081 &current_ext, &got, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006082 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006083 if (error)
6084 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006085 /*
6086 * If there was an extent merge during the shift, the extent
6087 * count can change. Update the total and grade the next record.
6088 */
6089 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006090 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006091 stop_extent = total_extents;
6092 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006093
Namjae Jeona904b1c2015-03-25 15:08:56 +11006094 if (current_ext == stop_extent) {
6095 *done = 1;
6096 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006097 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006098 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006099 xfs_iext_get_extent(ifp, current_ext, &got);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006100 }
6101
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006102 if (!*done)
Brian Foster2c845f52014-09-23 15:37:09 +10006103 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006104
6105del_cursor:
6106 if (cur)
6107 xfs_btree_del_cursor(cur,
6108 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6109
Brian Fosterca446d82014-09-02 12:12:53 +10006110 if (logflags)
6111 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006112
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006113 return error;
6114}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006115
6116/*
6117 * Splits an extent into two extents at split_fsb block such that it is
6118 * the first block of the current_ext. @current_ext is a target extent
6119 * to be split. @split_fsb is a block where the extents is split.
6120 * If split_fsb lies in a hole or the first block of extents, just return 0.
6121 */
6122STATIC int
6123xfs_bmap_split_extent_at(
6124 struct xfs_trans *tp,
6125 struct xfs_inode *ip,
6126 xfs_fileoff_t split_fsb,
6127 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006128 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006129{
6130 int whichfork = XFS_DATA_FORK;
6131 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006132 struct xfs_bmbt_irec got;
6133 struct xfs_bmbt_irec new; /* split extent */
6134 struct xfs_mount *mp = ip->i_mount;
6135 struct xfs_ifork *ifp;
6136 xfs_fsblock_t gotblkcnt; /* new block count for got */
6137 xfs_extnum_t current_ext;
6138 int error = 0;
6139 int logflags = 0;
6140 int i = 0;
6141
6142 if (unlikely(XFS_TEST_ERROR(
6143 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6144 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006145 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006146 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6147 XFS_ERRLEVEL_LOW, mp);
6148 return -EFSCORRUPTED;
6149 }
6150
6151 if (XFS_FORCED_SHUTDOWN(mp))
6152 return -EIO;
6153
6154 ifp = XFS_IFORK_PTR(ip, whichfork);
6155 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6156 /* Read in all the extents */
6157 error = xfs_iread_extents(tp, ip, whichfork);
6158 if (error)
6159 return error;
6160 }
6161
6162 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006163 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006164 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006165 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
6166 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006167 return 0;
6168
6169 gotblkcnt = split_fsb - got.br_startoff;
6170 new.br_startoff = split_fsb;
6171 new.br_startblock = got.br_startblock + gotblkcnt;
6172 new.br_blockcount = got.br_blockcount - gotblkcnt;
6173 new.br_state = got.br_state;
6174
6175 if (ifp->if_flags & XFS_IFBROOT) {
6176 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6177 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006178 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006179 cur->bc_private.b.flags = 0;
6180 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6181 got.br_startblock,
6182 got.br_blockcount,
6183 &i);
6184 if (error)
6185 goto del_cursor;
6186 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6187 }
6188
Namjae Jeona904b1c2015-03-25 15:08:56 +11006189 got.br_blockcount = gotblkcnt;
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006190 xfs_iext_update_extent(ifp, current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006191
6192 logflags = XFS_ILOG_CORE;
6193 if (cur) {
6194 error = xfs_bmbt_update(cur, got.br_startoff,
6195 got.br_startblock,
6196 got.br_blockcount,
6197 got.br_state);
6198 if (error)
6199 goto del_cursor;
6200 } else
6201 logflags |= XFS_ILOG_DEXT;
6202
6203 /* Add new extent */
6204 current_ext++;
6205 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6206 XFS_IFORK_NEXT_SET(ip, whichfork,
6207 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6208
6209 if (cur) {
6210 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6211 new.br_startblock, new.br_blockcount,
6212 &i);
6213 if (error)
6214 goto del_cursor;
6215 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6216 cur->bc_rec.b.br_state = new.br_state;
6217
6218 error = xfs_btree_insert(cur, &i);
6219 if (error)
6220 goto del_cursor;
6221 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6222 }
6223
6224 /*
6225 * Convert to a btree if necessary.
6226 */
6227 if (xfs_bmap_needs_btree(ip, whichfork)) {
6228 int tmp_logflags; /* partial log flag return val */
6229
6230 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006231 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006232 &cur, 0, &tmp_logflags, whichfork);
6233 logflags |= tmp_logflags;
6234 }
6235
6236del_cursor:
6237 if (cur) {
6238 cur->bc_private.b.allocated = 0;
6239 xfs_btree_del_cursor(cur,
6240 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6241 }
6242
6243 if (logflags)
6244 xfs_trans_log_inode(tp, ip, logflags);
6245 return error;
6246}
6247
6248int
6249xfs_bmap_split_extent(
6250 struct xfs_inode *ip,
6251 xfs_fileoff_t split_fsb)
6252{
6253 struct xfs_mount *mp = ip->i_mount;
6254 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006255 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006256 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006257 int error;
6258
Christoph Hellwig253f4912016-04-06 09:19:55 +10006259 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6260 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6261 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006262 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006263
6264 xfs_ilock(ip, XFS_ILOCK_EXCL);
6265 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6266
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006267 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006268
6269 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006270 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006271 if (error)
6272 goto out;
6273
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006274 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006275 if (error)
6276 goto out;
6277
Christoph Hellwig70393312015-06-04 13:48:08 +10006278 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006279
6280out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006281 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006282 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006283 return error;
6284}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006285
6286/* Deferred mapping is only for real extents in the data fork. */
6287static bool
6288xfs_bmap_is_update_needed(
6289 struct xfs_bmbt_irec *bmap)
6290{
6291 return bmap->br_startblock != HOLESTARTBLOCK &&
6292 bmap->br_startblock != DELAYSTARTBLOCK;
6293}
6294
6295/* Record a bmap intent. */
6296static int
6297__xfs_bmap_add(
6298 struct xfs_mount *mp,
6299 struct xfs_defer_ops *dfops,
6300 enum xfs_bmap_intent_type type,
6301 struct xfs_inode *ip,
6302 int whichfork,
6303 struct xfs_bmbt_irec *bmap)
6304{
6305 int error;
6306 struct xfs_bmap_intent *bi;
6307
6308 trace_xfs_bmap_defer(mp,
6309 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6310 type,
6311 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6312 ip->i_ino, whichfork,
6313 bmap->br_startoff,
6314 bmap->br_blockcount,
6315 bmap->br_state);
6316
6317 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6318 INIT_LIST_HEAD(&bi->bi_list);
6319 bi->bi_type = type;
6320 bi->bi_owner = ip;
6321 bi->bi_whichfork = whichfork;
6322 bi->bi_bmap = *bmap;
6323
Christoph Hellwig882d8782017-08-28 10:21:03 -07006324 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006325 if (error) {
6326 kmem_free(bi);
6327 return error;
6328 }
6329
6330 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6331 return 0;
6332}
6333
6334/* Map an extent into a file. */
6335int
6336xfs_bmap_map_extent(
6337 struct xfs_mount *mp,
6338 struct xfs_defer_ops *dfops,
6339 struct xfs_inode *ip,
6340 struct xfs_bmbt_irec *PREV)
6341{
6342 if (!xfs_bmap_is_update_needed(PREV))
6343 return 0;
6344
6345 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6346 XFS_DATA_FORK, PREV);
6347}
6348
6349/* Unmap an extent out of a file. */
6350int
6351xfs_bmap_unmap_extent(
6352 struct xfs_mount *mp,
6353 struct xfs_defer_ops *dfops,
6354 struct xfs_inode *ip,
6355 struct xfs_bmbt_irec *PREV)
6356{
6357 if (!xfs_bmap_is_update_needed(PREV))
6358 return 0;
6359
6360 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6361 XFS_DATA_FORK, PREV);
6362}
6363
6364/*
6365 * Process one of the deferred bmap operations. We pass back the
6366 * btree cursor to maintain our lock on the bmapbt between calls.
6367 */
6368int
6369xfs_bmap_finish_one(
6370 struct xfs_trans *tp,
6371 struct xfs_defer_ops *dfops,
6372 struct xfs_inode *ip,
6373 enum xfs_bmap_intent_type type,
6374 int whichfork,
6375 xfs_fileoff_t startoff,
6376 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006377 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006378 xfs_exntst_t state)
6379{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006380 xfs_fsblock_t firstfsb;
6381 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006382
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006383 /*
6384 * firstfsb is tied to the transaction lifetime and is used to
6385 * ensure correct AG locking order and schedule work item
6386 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6387 * to only making one bmap call per transaction, so it should
6388 * be safe to have it as a local variable here.
6389 */
6390 firstfsb = NULLFSBLOCK;
6391
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006392 trace_xfs_bmap_deferred(tp->t_mountp,
6393 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6394 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006395 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006396
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006397 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006398 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006399
6400 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006401 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006402 return -EIO;
6403
6404 switch (type) {
6405 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006406 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006407 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006408 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006409 break;
6410 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006411 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6412 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006413 break;
6414 default:
6415 ASSERT(0);
6416 error = -EFSCORRUPTED;
6417 }
6418
6419 return error;
6420}