blob: 9db566b7e45e547d1bd11a3e6e5087fdc419fab9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_sb.h"
Dave Chinnerf5ea1102013-04-24 18:58:02 +100026#include "xfs_mount.h"
Darrick J. Wong3ab78df2016-08-03 11:15:38 +100027#include "xfs_defer.h"
Dave Chinner57062782013-10-15 09:17:51 +110028#include "xfs_da_format.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100030#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_btree.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_trans.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_extfree_item.h"
36#include "xfs_alloc.h"
37#include "xfs_bmap.h"
Dave Chinner68988112013-08-12 20:49:42 +100038#include "xfs_bmap_util.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110039#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include "xfs_rtalloc.h"
41#include "xfs_error.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include "xfs_quota.h"
43#include "xfs_trans_space.h"
44#include "xfs_buf_item.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000045#include "xfs_trace.h"
Dave Chinner19de7352013-04-03 16:11:18 +110046#include "xfs_symlink.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110047#include "xfs_attr_leaf.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110048#include "xfs_filestream.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100049#include "xfs_rmap.h"
Darrick J. Wong3fd129b2016-09-19 10:30:52 +100050#include "xfs_ag_resv.h"
Darrick J. Wong62aab202016-10-03 09:11:23 -070051#include "xfs_refcount.h"
Brian Foster974ae922016-11-28 14:57:42 +110052#include "xfs_icache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055kmem_zone_t *xfs_bmap_free_item_zone;
56
57/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110058 * Miscellaneous helper functions
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 */
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
Dave Chinner9e5987a2013-02-25 12:31:26 +110062 * Compute and fill in the value of the maximum depth of a bmap btree
63 * in this filesystem. Done once, during mount.
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
Dave Chinner9e5987a2013-02-25 12:31:26 +110065void
66xfs_bmap_compute_maxlevels(
67 xfs_mount_t *mp, /* file system mount structure */
68 int whichfork) /* data or attr fork */
69{
70 int level; /* btree level */
71 uint maxblocks; /* max blocks at this level */
72 uint maxleafents; /* max leaf entries possible */
73 int maxrootrecs; /* max records in root block */
74 int minleafrecs; /* min records in leaf block */
75 int minnoderecs; /* min records in node block */
76 int sz; /* root block size */
77
78 /*
79 * The maximum number of extents in a file, hence the maximum
80 * number of leaf entries, is controlled by the type of di_nextents
81 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
82 * (a signed 16-bit number, xfs_aextnum_t).
83 *
84 * Note that we can no longer assume that if we are in ATTR1 that
85 * the fork offset of all the inodes will be
86 * (xfs_default_attroffset(ip) >> 3) because we could have mounted
87 * with ATTR2 and then mounted back with ATTR1, keeping the
88 * di_forkoff's fixed but probably at various positions. Therefore,
89 * for both ATTR1 and ATTR2 we have to assume the worst case scenario
90 * of a minimum size available.
91 */
92 if (whichfork == XFS_DATA_FORK) {
93 maxleafents = MAXEXTNUM;
94 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
95 } else {
96 maxleafents = MAXAEXTNUM;
97 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
98 }
Eric Sandeen152d93b2014-04-14 18:58:51 +100099 maxrootrecs = xfs_bmdr_maxrecs(sz, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100100 minleafrecs = mp->m_bmap_dmnr[0];
101 minnoderecs = mp->m_bmap_dmnr[1];
102 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
103 for (level = 1; maxblocks > 1; level++) {
104 if (maxblocks <= maxrootrecs)
105 maxblocks = 1;
106 else
107 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
108 }
109 mp->m_bm_maxlevels[whichfork] = level;
110}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100112STATIC int /* error */
113xfs_bmbt_lookup_eq(
114 struct xfs_btree_cur *cur,
115 xfs_fileoff_t off,
116 xfs_fsblock_t bno,
117 xfs_filblks_t len,
118 int *stat) /* success/failure */
119{
120 cur->bc_rec.b.br_startoff = off;
121 cur->bc_rec.b.br_startblock = bno;
122 cur->bc_rec.b.br_blockcount = len;
123 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
124}
125
126STATIC int /* error */
127xfs_bmbt_lookup_ge(
128 struct xfs_btree_cur *cur,
129 xfs_fileoff_t off,
130 xfs_fsblock_t bno,
131 xfs_filblks_t len,
132 int *stat) /* success/failure */
133{
134 cur->bc_rec.b.br_startoff = off;
135 cur->bc_rec.b.br_startblock = bno;
136 cur->bc_rec.b.br_blockcount = len;
137 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
138}
139
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100140/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000141 * Check if the inode needs to be converted to btree format.
142 */
143static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
144{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700145 return whichfork != XFS_COW_FORK &&
146 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000147 XFS_IFORK_NEXTENTS(ip, whichfork) >
148 XFS_IFORK_MAXEXT(ip, whichfork);
149}
150
151/*
152 * Check if the inode should be converted to extent format.
153 */
154static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
155{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700156 return whichfork != XFS_COW_FORK &&
157 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000158 XFS_IFORK_NEXTENTS(ip, whichfork) <=
159 XFS_IFORK_MAXEXT(ip, whichfork);
160}
161
162/*
163 * Update the record referred to by cur to the value given
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100164 * by [off, bno, len, state].
165 * This either works (return 0) or gets an EFSCORRUPTED error.
166 */
167STATIC int
168xfs_bmbt_update(
169 struct xfs_btree_cur *cur,
170 xfs_fileoff_t off,
171 xfs_fsblock_t bno,
172 xfs_filblks_t len,
173 xfs_exntst_t state)
174{
175 union xfs_btree_rec rec;
176
177 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
178 return xfs_btree_update(cur, &rec);
179}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100182 * Compute the worst-case number of indirect blocks that will be used
183 * for ip's delayed extent of length "len".
184 */
185STATIC xfs_filblks_t
186xfs_bmap_worst_indlen(
187 xfs_inode_t *ip, /* incore inode pointer */
188 xfs_filblks_t len) /* delayed extent length */
189{
190 int level; /* btree level number */
191 int maxrecs; /* maximum record count at this level */
192 xfs_mount_t *mp; /* mount structure */
193 xfs_filblks_t rval; /* return value */
194
195 mp = ip->i_mount;
196 maxrecs = mp->m_bmap_dmxr[0];
197 for (level = 0, rval = 0;
198 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
199 level++) {
200 len += maxrecs - 1;
201 do_div(len, maxrecs);
202 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700203 if (len == 1)
204 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100205 level - 1;
206 if (level == 0)
207 maxrecs = mp->m_bmap_dmxr[1];
208 }
209 return rval;
210}
211
212/*
213 * Calculate the default attribute fork offset for newly created inodes.
214 */
215uint
216xfs_default_attroffset(
217 struct xfs_inode *ip)
218{
219 struct xfs_mount *mp = ip->i_mount;
220 uint offset;
221
222 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100223 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100224 XFS_BMDR_SPACE_CALC(MINABTPTRS);
225 } else {
226 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
227 }
228
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100229 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100230 return offset;
231}
232
233/*
234 * Helper routine to reset inode di_forkoff field when switching
235 * attribute fork from local to extent format - we reset it where
236 * possible to make space available for inline data fork extents.
237 */
238STATIC void
239xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100240 xfs_inode_t *ip,
241 int whichfork)
242{
243 if (whichfork == XFS_ATTR_FORK &&
244 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
245 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
246 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
247 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
248
249 if (dfl_forkoff > ip->i_d.di_forkoff)
250 ip->i_d.di_forkoff = dfl_forkoff;
251 }
252}
253
Dave Chinner9e5987a2013-02-25 12:31:26 +1100254#ifdef DEBUG
255STATIC struct xfs_buf *
256xfs_bmap_get_bp(
257 struct xfs_btree_cur *cur,
258 xfs_fsblock_t bno)
259{
260 struct xfs_log_item_desc *lidp;
261 int i;
262
263 if (!cur)
264 return NULL;
265
266 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
267 if (!cur->bc_bufs[i])
268 break;
269 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
270 return cur->bc_bufs[i];
271 }
272
273 /* Chase down all the log items to see if the bp is there */
274 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
275 struct xfs_buf_log_item *bip;
276 bip = (struct xfs_buf_log_item *)lidp->lid_item;
277 if (bip->bli_item.li_type == XFS_LI_BUF &&
278 XFS_BUF_ADDR(bip->bli_buf) == bno)
279 return bip->bli_buf;
280 }
281
282 return NULL;
283}
284
285STATIC void
286xfs_check_block(
287 struct xfs_btree_block *block,
288 xfs_mount_t *mp,
289 int root,
290 short sz)
291{
292 int i, j, dmxr;
293 __be64 *pp, *thispa; /* pointer to block address */
294 xfs_bmbt_key_t *prevp, *keyp;
295
296 ASSERT(be16_to_cpu(block->bb_level) > 0);
297
298 prevp = NULL;
299 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
300 dmxr = mp->m_bmap_dmxr[0];
301 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
302
303 if (prevp) {
304 ASSERT(be64_to_cpu(prevp->br_startoff) <
305 be64_to_cpu(keyp->br_startoff));
306 }
307 prevp = keyp;
308
309 /*
310 * Compare the block numbers to see if there are dups.
311 */
312 if (root)
313 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
314 else
315 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
316
317 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
318 if (root)
319 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
320 else
321 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
322 if (*thispa == *pp) {
323 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
324 __func__, j, i,
325 (unsigned long long)be64_to_cpu(*thispa));
326 panic("%s: ptrs are equal in node\n",
327 __func__);
328 }
329 }
330 }
331}
332
333/*
334 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100335 * btree leaves. THis becomes prohibitively expensive for large extent count
336 * files, so don't bother with inodes that have more than 10,000 extents in
337 * them. The btree record ordering checks will still be done, so for such large
338 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100339 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100340STATIC void
341xfs_bmap_check_leaf_extents(
342 xfs_btree_cur_t *cur, /* btree cursor or null */
343 xfs_inode_t *ip, /* incore inode pointer */
344 int whichfork) /* data or attr fork */
345{
346 struct xfs_btree_block *block; /* current btree block */
347 xfs_fsblock_t bno; /* block # of "block" */
348 xfs_buf_t *bp; /* buffer for "block" */
349 int error; /* error return value */
350 xfs_extnum_t i=0, j; /* index into the extents list */
351 xfs_ifork_t *ifp; /* fork structure */
352 int level; /* btree level, for checking */
353 xfs_mount_t *mp; /* file system mount structure */
354 __be64 *pp; /* pointer to block address */
355 xfs_bmbt_rec_t *ep; /* pointer to current extent */
356 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
357 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
358 int bp_release = 0;
359
360 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
361 return;
362 }
363
Dave Chinnere3543812016-01-08 11:28:49 +1100364 /* skip large extent count inodes */
365 if (ip->i_d.di_nextents > 10000)
366 return;
367
Dave Chinner9e5987a2013-02-25 12:31:26 +1100368 bno = NULLFSBLOCK;
369 mp = ip->i_mount;
370 ifp = XFS_IFORK_PTR(ip, whichfork);
371 block = ifp->if_broot;
372 /*
373 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
374 */
375 level = be16_to_cpu(block->bb_level);
376 ASSERT(level > 0);
377 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
378 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
379 bno = be64_to_cpu(*pp);
380
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000381 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100382 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
383 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
384
385 /*
386 * Go down the tree until leaf level is reached, following the first
387 * pointer (leftmost) at each level.
388 */
389 while (level-- > 0) {
390 /* See if buf is in cur first */
391 bp_release = 0;
392 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
393 if (!bp) {
394 bp_release = 1;
395 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
396 XFS_BMAP_BTREE_REF,
397 &xfs_bmbt_buf_ops);
398 if (error)
399 goto error_norelse;
400 }
401 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100402 if (level == 0)
403 break;
404
405 /*
406 * Check this block for basic sanity (increasing keys and
407 * no duplicate blocks).
408 */
409
410 xfs_check_block(block, mp, 0, 0);
411 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
412 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100413 XFS_WANT_CORRUPTED_GOTO(mp,
414 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100415 if (bp_release) {
416 bp_release = 0;
417 xfs_trans_brelse(NULL, bp);
418 }
419 }
420
421 /*
422 * Here with bp and block set to the leftmost leaf node in the tree.
423 */
424 i = 0;
425
426 /*
427 * Loop over all leaf nodes checking that all extents are in the right order.
428 */
429 for (;;) {
430 xfs_fsblock_t nextbno;
431 xfs_extnum_t num_recs;
432
433
434 num_recs = xfs_btree_get_numrecs(block);
435
436 /*
437 * Read-ahead the next leaf block, if any.
438 */
439
440 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
441
442 /*
443 * Check all the extents to make sure they are OK.
444 * If we had a previous block, the last entry should
445 * conform with the first entry in this one.
446 */
447
448 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
449 if (i) {
450 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
451 xfs_bmbt_disk_get_blockcount(&last) <=
452 xfs_bmbt_disk_get_startoff(ep));
453 }
454 for (j = 1; j < num_recs; j++) {
455 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
456 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
457 xfs_bmbt_disk_get_blockcount(ep) <=
458 xfs_bmbt_disk_get_startoff(nextp));
459 ep = nextp;
460 }
461
462 last = *ep;
463 i += num_recs;
464 if (bp_release) {
465 bp_release = 0;
466 xfs_trans_brelse(NULL, bp);
467 }
468 bno = nextbno;
469 /*
470 * If we've reached the end, stop.
471 */
472 if (bno == NULLFSBLOCK)
473 break;
474
475 bp_release = 0;
476 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
477 if (!bp) {
478 bp_release = 1;
479 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
480 XFS_BMAP_BTREE_REF,
481 &xfs_bmbt_buf_ops);
482 if (error)
483 goto error_norelse;
484 }
485 block = XFS_BUF_TO_BLOCK(bp);
486 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100487
Dave Chinner9e5987a2013-02-25 12:31:26 +1100488 return;
489
490error0:
491 xfs_warn(mp, "%s: at error0", __func__);
492 if (bp_release)
493 xfs_trans_brelse(NULL, bp);
494error_norelse:
495 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
496 __func__, i);
497 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
498 return;
499}
500
501/*
502 * Add bmap trace insert entries for all the contents of the extent records.
503 */
504void
505xfs_bmap_trace_exlist(
506 xfs_inode_t *ip, /* incore inode pointer */
507 xfs_extnum_t cnt, /* count of entries in the list */
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100508 int whichfork, /* data or attr or cow fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100509 unsigned long caller_ip)
510{
511 xfs_extnum_t idx; /* extent record index */
512 xfs_ifork_t *ifp; /* inode fork pointer */
513 int state = 0;
514
515 if (whichfork == XFS_ATTR_FORK)
516 state |= BMAP_ATTRFORK;
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100517 else if (whichfork == XFS_COW_FORK)
518 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100519
520 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +1100521 ASSERT(cnt == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100522 for (idx = 0; idx < cnt; idx++)
Eric Sandeen77105172016-12-05 12:31:50 +1100523 trace_xfs_extlist(ip, idx, state, caller_ip);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100524}
525
526/*
527 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000528 * given the caller's original parameters. Specifically check the
529 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100530 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
531 */
532STATIC void
533xfs_bmap_validate_ret(
534 xfs_fileoff_t bno,
535 xfs_filblks_t len,
536 int flags,
537 xfs_bmbt_irec_t *mval,
538 int nmap,
539 int ret_nmap)
540{
541 int i; /* index to map values */
542
543 ASSERT(ret_nmap <= nmap);
544
545 for (i = 0; i < ret_nmap; i++) {
546 ASSERT(mval[i].br_blockcount > 0);
547 if (!(flags & XFS_BMAPI_ENTIRE)) {
548 ASSERT(mval[i].br_startoff >= bno);
549 ASSERT(mval[i].br_blockcount <= len);
550 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
551 bno + len);
552 } else {
553 ASSERT(mval[i].br_startoff < bno + len);
554 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
555 bno);
556 }
557 ASSERT(i == 0 ||
558 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
559 mval[i].br_startoff);
560 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
561 mval[i].br_startblock != HOLESTARTBLOCK);
562 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
563 mval[i].br_state == XFS_EXT_UNWRITTEN);
564 }
565}
566
567#else
568#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700569#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100570#endif /* DEBUG */
571
572/*
573 * bmap free list manipulation functions
574 */
575
576/*
577 * Add the extent to the list of extents to be free at transaction end.
578 * The list is maintained sorted (by block number).
579 */
580void
581xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000582 struct xfs_mount *mp,
583 struct xfs_defer_ops *dfops,
584 xfs_fsblock_t bno,
585 xfs_filblks_t len,
586 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100587{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000588 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100589#ifdef DEBUG
590 xfs_agnumber_t agno;
591 xfs_agblock_t agbno;
592
593 ASSERT(bno != NULLFSBLOCK);
594 ASSERT(len > 0);
595 ASSERT(len <= MAXEXTLEN);
596 ASSERT(!isnullstartblock(bno));
597 agno = XFS_FSB_TO_AGNO(mp, bno);
598 agbno = XFS_FSB_TO_AGBNO(mp, bno);
599 ASSERT(agno < mp->m_sb.sb_agcount);
600 ASSERT(agbno < mp->m_sb.sb_agblocks);
601 ASSERT(len < mp->m_sb.sb_agblocks);
602 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
603#endif
604 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000605
Dave Chinner9e5987a2013-02-25 12:31:26 +1100606 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000607 new->xefi_startblock = bno;
608 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000609 if (oinfo)
610 new->xefi_oinfo = *oinfo;
611 else
612 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000613 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
614 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000615 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100616}
617
618/*
619 * Inode fork format manipulation functions
620 */
621
622/*
623 * Transform a btree format file with only one leaf node, where the
624 * extents list will fit in the inode, into an extents format file.
625 * Since the file extents are already in-core, all we have to do is
626 * give up the space for the btree root and pitch the leaf block.
627 */
628STATIC int /* error */
629xfs_bmap_btree_to_extents(
630 xfs_trans_t *tp, /* transaction pointer */
631 xfs_inode_t *ip, /* incore inode pointer */
632 xfs_btree_cur_t *cur, /* btree cursor */
633 int *logflagsp, /* inode logging flags */
634 int whichfork) /* data or attr fork */
635{
636 /* REFERENCED */
637 struct xfs_btree_block *cblock;/* child btree block */
638 xfs_fsblock_t cbno; /* child block number */
639 xfs_buf_t *cbp; /* child block's buffer */
640 int error; /* error return value */
641 xfs_ifork_t *ifp; /* inode fork data */
642 xfs_mount_t *mp; /* mount point structure */
643 __be64 *pp; /* ptr to block address */
644 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000645 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100646
647 mp = ip->i_mount;
648 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700649 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100650 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
651 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
652 rblock = ifp->if_broot;
653 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
654 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
655 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
656 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
657 cbno = be64_to_cpu(*pp);
658 *logflagsp = 0;
659#ifdef DEBUG
660 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
661 return error;
662#endif
663 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
664 &xfs_bmbt_buf_ops);
665 if (error)
666 return error;
667 cblock = XFS_BUF_TO_BLOCK(cbp);
668 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
669 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000670 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
671 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100672 ip->i_d.di_nblocks--;
673 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
674 xfs_trans_binval(tp, cbp);
675 if (cur->bc_bufs[0] == cbp)
676 cur->bc_bufs[0] = NULL;
677 xfs_iroot_realloc(ip, -1, whichfork);
678 ASSERT(ifp->if_broot == NULL);
679 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
680 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
681 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
682 return 0;
683}
684
685/*
686 * Convert an extents-format file into a btree-format file.
687 * The new file will have a root block (in the inode) and a single child block.
688 */
689STATIC int /* error */
690xfs_bmap_extents_to_btree(
691 xfs_trans_t *tp, /* transaction pointer */
692 xfs_inode_t *ip, /* incore inode pointer */
693 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000694 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100695 xfs_btree_cur_t **curp, /* cursor returned to caller */
696 int wasdel, /* converting a delayed alloc */
697 int *logflagsp, /* inode logging flags */
698 int whichfork) /* data or attr fork */
699{
700 struct xfs_btree_block *ablock; /* allocated (child) bt block */
701 xfs_buf_t *abp; /* buffer for ablock */
702 xfs_alloc_arg_t args; /* allocation arguments */
703 xfs_bmbt_rec_t *arp; /* child record pointer */
704 struct xfs_btree_block *block; /* btree root block */
705 xfs_btree_cur_t *cur; /* bmap btree cursor */
706 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
707 int error; /* error return value */
708 xfs_extnum_t i, cnt; /* extent record index */
709 xfs_ifork_t *ifp; /* inode fork pointer */
710 xfs_bmbt_key_t *kp; /* root block key pointer */
711 xfs_mount_t *mp; /* mount structure */
712 xfs_extnum_t nextents; /* number of file extents */
713 xfs_bmbt_ptr_t *pp; /* root block address pointer */
714
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500715 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700716 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100717 ifp = XFS_IFORK_PTR(ip, whichfork);
718 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
719
720 /*
721 * Make space in the inode incore.
722 */
723 xfs_iroot_realloc(ip, 1, whichfork);
724 ifp->if_flags |= XFS_IFBROOT;
725
726 /*
727 * Fill in the root.
728 */
729 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800730 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
731 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800732 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100733 /*
734 * Need a cursor. Can't allocate until bb_level is filled in.
735 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100736 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
737 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000738 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100739 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
740 /*
741 * Convert to a btree with two levels, one record in root.
742 */
743 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
744 memset(&args, 0, sizeof(args));
745 args.tp = tp;
746 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000747 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100748 args.firstblock = *firstblock;
749 if (*firstblock == NULLFSBLOCK) {
750 args.type = XFS_ALLOCTYPE_START_BNO;
751 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000752 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100753 args.type = XFS_ALLOCTYPE_START_BNO;
754 args.fsbno = *firstblock;
755 } else {
756 args.type = XFS_ALLOCTYPE_NEAR_BNO;
757 args.fsbno = *firstblock;
758 }
759 args.minlen = args.maxlen = args.prod = 1;
760 args.wasdel = wasdel;
761 *logflagsp = 0;
762 if ((error = xfs_alloc_vextent(&args))) {
763 xfs_iroot_realloc(ip, -1, whichfork);
764 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
765 return error;
766 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700767
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800768 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
769 xfs_iroot_realloc(ip, -1, whichfork);
770 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
771 return -ENOSPC;
772 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100773 /*
774 * Allocation can't fail, the space was reserved.
775 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100776 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800777 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100778 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
779 cur->bc_private.b.allocated++;
780 ip->i_d.di_nblocks++;
781 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
782 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
783 /*
784 * Fill in the child block.
785 */
786 abp->b_ops = &xfs_bmbt_buf_ops;
787 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800788 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
789 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500790 XFS_BTREE_LONG_PTRS);
791
Dave Chinner9e5987a2013-02-25 12:31:26 +1100792 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100793 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100794 for (cnt = i = 0; i < nextents; i++) {
795 ep = xfs_iext_get_ext(ifp, i);
796 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
797 arp->l0 = cpu_to_be64(ep->l0);
798 arp->l1 = cpu_to_be64(ep->l1);
799 arp++; cnt++;
800 }
801 }
802 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
803 xfs_btree_set_numrecs(ablock, cnt);
804
805 /*
806 * Fill in the root key and pointer.
807 */
808 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
809 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
810 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
811 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
812 be16_to_cpu(block->bb_level)));
813 *pp = cpu_to_be64(args.fsbno);
814
815 /*
816 * Do all this logging at the end so that
817 * the root is at the right level.
818 */
819 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
820 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
821 ASSERT(*curp == NULL);
822 *curp = cur;
823 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
824 return 0;
825}
826
827/*
828 * Convert a local file to an extents file.
829 * This code is out of bounds for data forks of regular files,
830 * since the file data needs to get logged so things will stay consistent.
831 * (The bmap-level manipulations are ok, though).
832 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000833void
834xfs_bmap_local_to_extents_empty(
835 struct xfs_inode *ip,
836 int whichfork)
837{
838 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
839
Darrick J. Wong60b49842016-10-03 09:11:34 -0700840 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000841 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
842 ASSERT(ifp->if_bytes == 0);
843 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
844
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000845 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000846 ifp->if_flags &= ~XFS_IFINLINE;
847 ifp->if_flags |= XFS_IFEXTENTS;
848 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
849}
850
851
Dave Chinner9e5987a2013-02-25 12:31:26 +1100852STATIC int /* error */
853xfs_bmap_local_to_extents(
854 xfs_trans_t *tp, /* transaction pointer */
855 xfs_inode_t *ip, /* incore inode pointer */
856 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
857 xfs_extlen_t total, /* total blocks needed by transaction */
858 int *logflagsp, /* inode logging flags */
859 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500860 void (*init_fn)(struct xfs_trans *tp,
861 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100862 struct xfs_inode *ip,
863 struct xfs_ifork *ifp))
864{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000865 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100866 int flags; /* logging flags returned */
867 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000868 xfs_alloc_arg_t args; /* allocation arguments */
869 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700870 struct xfs_bmbt_irec rec;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100871
872 /*
873 * We don't want to deal with the case of keeping inode data inline yet.
874 * So sending the data fork of a regular inode is invalid.
875 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100876 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100877 ifp = XFS_IFORK_PTR(ip, whichfork);
878 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000879
880 if (!ifp->if_bytes) {
881 xfs_bmap_local_to_extents_empty(ip, whichfork);
882 flags = XFS_ILOG_CORE;
883 goto done;
884 }
885
Dave Chinner9e5987a2013-02-25 12:31:26 +1100886 flags = 0;
887 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000888 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
889 XFS_IFINLINE);
890 memset(&args, 0, sizeof(args));
891 args.tp = tp;
892 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000893 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000894 args.firstblock = *firstblock;
895 /*
896 * Allocate a block. We know we need only one, since the
897 * file currently fits in an inode.
898 */
899 if (*firstblock == NULLFSBLOCK) {
900 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
901 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100902 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000903 args.fsbno = *firstblock;
904 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100905 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000906 args.total = total;
907 args.minlen = args.maxlen = args.prod = 1;
908 error = xfs_alloc_vextent(&args);
909 if (error)
910 goto done;
911
912 /* Can't fail, the space was reserved. */
913 ASSERT(args.fsbno != NULLFSBLOCK);
914 ASSERT(args.len == 1);
915 *firstblock = args.fsbno;
916 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
917
Dave Chinnerfe22d552015-01-22 09:30:06 +1100918 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100919 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100920 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100921 * The callout is responsible for logging because the remote format
922 * might differ from the local format and thus we don't know how much to
923 * log here. Note that init_fn must also set the buffer log item type
924 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100925 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000926 init_fn(tp, bp, ip, ifp);
927
Brian Fosterb7cdc662015-10-12 15:40:24 +1100928 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000929 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
930 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100931 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000932
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700933 rec.br_startoff = 0;
934 rec.br_startblock = args.fsbno;
935 rec.br_blockcount = 1;
936 rec.br_state = XFS_EXT_NORM;
937 xfs_iext_insert(ip, 0, 1, &rec, 0);
938
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000939 trace_xfs_bmap_post_update(ip, 0,
940 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
941 _THIS_IP_);
942 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
943 ip->i_d.di_nblocks = 1;
944 xfs_trans_mod_dquot_byino(tp, ip,
945 XFS_TRANS_DQ_BCOUNT, 1L);
946 flags |= xfs_ilog_fext(whichfork);
947
Dave Chinner9e5987a2013-02-25 12:31:26 +1100948done:
949 *logflagsp = flags;
950 return error;
951}
952
953/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 * Called from xfs_bmap_add_attrfork to handle btree format files.
955 */
956STATIC int /* error */
957xfs_bmap_add_attrfork_btree(
958 xfs_trans_t *tp, /* transaction pointer */
959 xfs_inode_t *ip, /* incore inode pointer */
960 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000961 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 int *flags) /* inode logging flags */
963{
964 xfs_btree_cur_t *cur; /* btree cursor */
965 int error; /* error return value */
966 xfs_mount_t *mp; /* file system mount struct */
967 int stat; /* newroot status */
968
969 mp = ip->i_mount;
970 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
971 *flags |= XFS_ILOG_DBROOT;
972 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100973 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000974 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 cur->bc_private.b.firstblock = *firstblock;
976 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
977 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000978 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100979 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100980 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 goto error0;
982 if (stat == 0) {
983 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000984 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 *firstblock = cur->bc_private.b.firstblock;
987 cur->bc_private.b.allocated = 0;
988 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
989 }
990 return 0;
991error0:
992 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
993 return error;
994}
995
996/*
997 * Called from xfs_bmap_add_attrfork to handle extents format files.
998 */
999STATIC int /* error */
1000xfs_bmap_add_attrfork_extents(
1001 xfs_trans_t *tp, /* transaction pointer */
1002 xfs_inode_t *ip, /* incore inode pointer */
1003 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001004 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 int *flags) /* inode logging flags */
1006{
1007 xfs_btree_cur_t *cur; /* bmap btree cursor */
1008 int error; /* error return value */
1009
1010 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1011 return 0;
1012 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001013 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 flags, XFS_DATA_FORK);
1015 if (cur) {
1016 cur->bc_private.b.allocated = 0;
1017 xfs_btree_del_cursor(cur,
1018 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1019 }
1020 return error;
1021}
1022
1023/*
Dave Chinner1e823792013-02-11 15:58:13 +11001024 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1025 * different data fork content type needs a different callout to do the
1026 * conversion. Some are basic and only require special block initialisation
1027 * callouts for the data formating, others (directories) are so specialised they
1028 * handle everything themselves.
1029 *
1030 * XXX (dgc): investigate whether directory conversion can use the generic
1031 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001032 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 */
1034STATIC int /* error */
1035xfs_bmap_add_attrfork_local(
1036 xfs_trans_t *tp, /* transaction pointer */
1037 xfs_inode_t *ip, /* incore inode pointer */
1038 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001039 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 int *flags) /* inode logging flags */
1041{
1042 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
1044 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1045 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001046
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001047 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001049 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 dargs.dp = ip;
1051 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001052 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001053 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 dargs.whichfork = XFS_DATA_FORK;
1055 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001056 return xfs_dir2_sf_to_block(&dargs);
1057 }
1058
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001059 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001060 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1061 flags, XFS_DATA_FORK,
1062 xfs_symlink_local_to_remote);
1063
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001064 /* should only be called for types that support local format data */
1065 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001066 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067}
1068
1069/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001070 * Convert inode from non-attributed to attributed.
1071 * Must not be in a transaction, ip must not be locked.
1072 */
1073int /* error code */
1074xfs_bmap_add_attrfork(
1075 xfs_inode_t *ip, /* incore inode pointer */
1076 int size, /* space new attribute needs */
1077 int rsvd) /* xact may use reserved blks */
1078{
1079 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001080 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001081 xfs_mount_t *mp; /* mount structure */
1082 xfs_trans_t *tp; /* transaction pointer */
1083 int blks; /* space reservation */
1084 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001085 int logflags; /* logging flags */
1086 int error; /* error return value */
1087
1088 ASSERT(XFS_IFORK_Q(ip) == 0);
1089
1090 mp = ip->i_mount;
1091 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001092
Dave Chinner9e5987a2013-02-25 12:31:26 +11001093 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001094
1095 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1096 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1097 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001098 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001099
Dave Chinner9e5987a2013-02-25 12:31:26 +11001100 xfs_ilock(ip, XFS_ILOCK_EXCL);
1101 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1102 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1103 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001104 if (error)
1105 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001106 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001107 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001108 if (ip->i_d.di_anextents != 0) {
1109 error = -EFSCORRUPTED;
1110 goto trans_cancel;
1111 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001112 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1113 /*
1114 * For inodes coming from pre-6.2 filesystems.
1115 */
1116 ASSERT(ip->i_d.di_aformat == 0);
1117 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1118 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001119
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001120 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001121 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1122
1123 switch (ip->i_d.di_format) {
1124 case XFS_DINODE_FMT_DEV:
1125 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1126 break;
1127 case XFS_DINODE_FMT_UUID:
1128 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1129 break;
1130 case XFS_DINODE_FMT_LOCAL:
1131 case XFS_DINODE_FMT_EXTENTS:
1132 case XFS_DINODE_FMT_BTREE:
1133 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1134 if (!ip->i_d.di_forkoff)
1135 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1136 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1137 version = 2;
1138 break;
1139 default:
1140 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001141 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001142 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001143 }
1144
1145 ASSERT(ip->i_afp == NULL);
1146 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1147 ip->i_afp->if_flags = XFS_IFEXTENTS;
1148 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001149 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001150 switch (ip->i_d.di_format) {
1151 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001152 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001153 &logflags);
1154 break;
1155 case XFS_DINODE_FMT_EXTENTS:
1156 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001157 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001158 break;
1159 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001160 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001161 &logflags);
1162 break;
1163 default:
1164 error = 0;
1165 break;
1166 }
1167 if (logflags)
1168 xfs_trans_log_inode(tp, ip, logflags);
1169 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001170 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001171 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1172 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001173 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001174
1175 spin_lock(&mp->m_sb_lock);
1176 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1177 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001178 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001179 }
1180 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1181 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001182 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001183 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001184 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001185 if (log_sb)
1186 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001187 }
1188
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001189 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001190 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001191 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001192 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001193 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001194 return error;
1195
1196bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001197 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001198trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001199 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001200 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001201 return error;
1202}
1203
1204/*
1205 * Internal and external extent tree search functions.
1206 */
1207
1208/*
1209 * Read in the extents to if_extents.
1210 * All inode fields are set up by caller, we just traverse the btree
1211 * and copy the records in. If the file system cannot contain unwritten
1212 * extents, the records are checked for no "state" flags.
1213 */
1214int /* error */
1215xfs_bmap_read_extents(
1216 xfs_trans_t *tp, /* transaction pointer */
1217 xfs_inode_t *ip, /* incore inode */
1218 int whichfork) /* data or attr fork */
1219{
1220 struct xfs_btree_block *block; /* current btree block */
1221 xfs_fsblock_t bno; /* block # of "block" */
1222 xfs_buf_t *bp; /* buffer for "block" */
1223 int error; /* error return value */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001224 xfs_extnum_t i, j; /* index into the extents list */
1225 xfs_ifork_t *ifp; /* fork structure */
1226 int level; /* btree level, for checking */
1227 xfs_mount_t *mp; /* file system mount structure */
1228 __be64 *pp; /* pointer to block address */
1229 /* REFERENCED */
1230 xfs_extnum_t room; /* number of entries there's room for */
1231
Dave Chinner9e5987a2013-02-25 12:31:26 +11001232 mp = ip->i_mount;
1233 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001234 block = ifp->if_broot;
1235 /*
1236 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1237 */
1238 level = be16_to_cpu(block->bb_level);
1239 ASSERT(level > 0);
1240 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1241 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001242
Dave Chinner9e5987a2013-02-25 12:31:26 +11001243 /*
1244 * Go down the tree until leaf level is reached, following the first
1245 * pointer (leftmost) at each level.
1246 */
1247 while (level-- > 0) {
1248 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1249 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1250 if (error)
1251 return error;
1252 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001253 if (level == 0)
1254 break;
1255 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1256 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001257 XFS_WANT_CORRUPTED_GOTO(mp,
1258 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001259 xfs_trans_brelse(tp, bp);
1260 }
1261 /*
1262 * Here with bp and block set to the leftmost leaf node in the tree.
1263 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001264 room = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001265 i = 0;
1266 /*
1267 * Loop over all leaf nodes. Copy information to the extent records.
1268 */
1269 for (;;) {
1270 xfs_bmbt_rec_t *frp;
1271 xfs_fsblock_t nextbno;
1272 xfs_extnum_t num_recs;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001273
1274 num_recs = xfs_btree_get_numrecs(block);
1275 if (unlikely(i + num_recs > room)) {
1276 ASSERT(i + num_recs <= room);
1277 xfs_warn(ip->i_mount,
1278 "corrupt dinode %Lu, (btree extents).",
1279 (unsigned long long) ip->i_ino);
1280 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1281 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1282 goto error0;
1283 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001284 /*
1285 * Read-ahead the next leaf block, if any.
1286 */
1287 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1288 if (nextbno != NULLFSBLOCK)
1289 xfs_btree_reada_bufl(mp, nextbno, 1,
1290 &xfs_bmbt_buf_ops);
1291 /*
1292 * Copy records into the extent records.
1293 */
1294 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001295 for (j = 0; j < num_recs; j++, i++, frp++) {
1296 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1297 trp->l0 = be64_to_cpu(frp->l0);
1298 trp->l1 = be64_to_cpu(frp->l1);
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001299 if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001300 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001301 XFS_ERRLEVEL_LOW, mp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001302 goto error0;
1303 }
1304 }
1305 xfs_trans_brelse(tp, bp);
1306 bno = nextbno;
1307 /*
1308 * If we've reached the end, stop.
1309 */
1310 if (bno == NULLFSBLOCK)
1311 break;
1312 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1313 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1314 if (error)
1315 return error;
1316 block = XFS_BUF_TO_BLOCK(bp);
1317 }
Darrick J. Wong356a3222016-12-05 12:36:56 +11001318 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1319 return -EFSCORRUPTED;
Eric Sandeen5d829302016-11-08 12:59:42 +11001320 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11001321 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1322 return 0;
1323error0:
1324 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001325 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001326}
1327
Dave Chinner9e5987a2013-02-25 12:31:26 +11001328/*
1329 * Returns the file-relative block number of the first unused block(s)
1330 * in the file with at least "len" logically contiguous blocks free.
1331 * This is the lowest-address hole if the file has holes, else the first block
1332 * past the end of file.
1333 * Return 0 if the file is currently local (in-inode).
1334 */
1335int /* error */
1336xfs_bmap_first_unused(
1337 xfs_trans_t *tp, /* transaction pointer */
1338 xfs_inode_t *ip, /* incore inode */
1339 xfs_extlen_t len, /* size of hole to find */
1340 xfs_fileoff_t *first_unused, /* unused block */
1341 int whichfork) /* data or attr fork */
1342{
1343 int error; /* error return value */
1344 int idx; /* extent record index */
1345 xfs_ifork_t *ifp; /* inode fork pointer */
1346 xfs_fileoff_t lastaddr; /* last block number seen */
1347 xfs_fileoff_t lowest; /* lowest useful block */
1348 xfs_fileoff_t max; /* starting useful block */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001349 xfs_extnum_t nextents; /* number of extent entries */
1350
1351 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1352 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1353 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1354 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1355 *first_unused = 0;
1356 return 0;
1357 }
1358 ifp = XFS_IFORK_PTR(ip, whichfork);
1359 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1360 (error = xfs_iread_extents(tp, ip, whichfork)))
1361 return error;
1362 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001363 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001364 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001365 struct xfs_bmbt_irec got;
1366
1367 xfs_iext_get_extent(ifp, idx, &got);
1368
Dave Chinner9e5987a2013-02-25 12:31:26 +11001369 /*
1370 * See if the hole before this extent will work.
1371 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001372 if (got.br_startoff >= lowest + len &&
1373 got.br_startoff - max >= len) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001374 *first_unused = max;
1375 return 0;
1376 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001377 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001378 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1379 }
1380 *first_unused = max;
1381 return 0;
1382}
1383
1384/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001385 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001386 * last_block (input value) in the file.
1387 * This is not based on i_size, it is based on the extent records.
1388 * Returns 0 for local files, as they do not have extent records.
1389 */
1390int /* error */
1391xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001392 struct xfs_trans *tp, /* transaction pointer */
1393 struct xfs_inode *ip, /* incore inode */
1394 xfs_fileoff_t *last_block, /* last block */
1395 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001396{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001397 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1398 struct xfs_bmbt_irec got;
1399 xfs_extnum_t idx;
1400 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001401
Christoph Hellwig86685f72016-11-24 11:39:38 +11001402 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1403 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001404 *last_block = 0;
1405 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001406 case XFS_DINODE_FMT_BTREE:
1407 case XFS_DINODE_FMT_EXTENTS:
1408 break;
1409 default:
1410 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001411 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001412
1413 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1414 error = xfs_iread_extents(tp, ip, whichfork);
1415 if (error)
1416 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001417 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001418
1419 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1420 if (got.br_startoff <= *last_block - 1)
1421 return 0;
1422 }
1423
1424 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1425 *last_block = got.br_startoff + got.br_blockcount;
1426 return 0;
1427 }
1428
1429 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001430 return 0;
1431}
1432
Dave Chinner68988112013-08-12 20:49:42 +10001433int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001434xfs_bmap_last_extent(
1435 struct xfs_trans *tp,
1436 struct xfs_inode *ip,
1437 int whichfork,
1438 struct xfs_bmbt_irec *rec,
1439 int *is_empty)
1440{
1441 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1442 int error;
1443 int nextents;
1444
1445 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1446 error = xfs_iread_extents(tp, ip, whichfork);
1447 if (error)
1448 return error;
1449 }
1450
Eric Sandeen5d829302016-11-08 12:59:42 +11001451 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001452 if (nextents == 0) {
1453 *is_empty = 1;
1454 return 0;
1455 }
1456
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001457 xfs_iext_get_extent(ifp, nextents - 1, rec);
Dave Chinner9e5987a2013-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 Chinner9e5987a2013-02-25 12:31:26 +11001469 * at, or past the EOF.
1470 */
1471STATIC int
1472xfs_bmap_isaeof(
1473 struct xfs_bmalloca *bma,
1474 int whichfork)
1475{
1476 struct xfs_bmbt_irec rec;
1477 int is_empty;
1478 int error;
1479
Thomas Meyer749f24f2017-10-09 11:38:54 -07001480 bma->aeof = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001481 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1482 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001483 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001484 return error;
1485
Dave Chinner6e708bc2013-11-22 10:41:16 +11001486 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001487 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001488 return 0;
1489 }
1490
Dave Chinner9e5987a2013-02-25 12:31:26 +11001491 /*
1492 * Check if we are allocation or past the last extent, or at least into
1493 * the last delayed allocated extent.
1494 */
1495 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1496 (bma->offset >= rec.br_startoff &&
1497 isnullstartblock(rec.br_startblock));
1498 return 0;
1499}
1500
1501/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001502 * Returns the file-relative block number of the first block past eof in
1503 * the file. This is not based on i_size, it is based on the extent records.
1504 * Returns 0 for local files, as they do not have extent records.
1505 */
1506int
1507xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001508 struct xfs_inode *ip,
1509 xfs_fileoff_t *last_block,
1510 int whichfork)
1511{
1512 struct xfs_bmbt_irec rec;
1513 int is_empty;
1514 int error;
1515
1516 *last_block = 0;
1517
1518 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1519 return 0;
1520
1521 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1522 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001523 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001524
1525 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1526 if (error || is_empty)
1527 return error;
1528
1529 *last_block = rec.br_startoff + rec.br_blockcount;
1530 return 0;
1531}
1532
1533/*
1534 * Returns whether the selected fork of the inode has exactly one
1535 * block or not. For the data fork we check this matches di_size,
1536 * implying the file's range is 0..bsize-1.
1537 */
1538int /* 1=>1 block, 0=>otherwise */
1539xfs_bmap_one_block(
1540 xfs_inode_t *ip, /* incore inode */
1541 int whichfork) /* data or attr fork */
1542{
Dave Chinner9e5987a2013-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 Chinner9e5987a2013-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 */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002691 xfs_filblks_t temp=0; /* 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_);
2754 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002755 oldlen = startblockval(left.br_startblock) +
2756 startblockval(new->br_startblock) +
2757 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002758 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2759 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002760 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002761 nullstartblock((int)newlen));
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_);
2777 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002778 oldlen = startblockval(left.br_startblock) +
2779 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002780 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2781 oldlen);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002782 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
Eric Sandeen9d87c312009-01-14 23:22:07 -06002783 nullstartblock((int)newlen));
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 Hellwig2f2b3222011-05-11 15:04:06 +00002799 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
2800 new->br_startoff,
Eric Sandeen9d87c312009-01-14 23:22:07 -06002801 nullstartblock((int)newlen), temp, right.br_state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002802 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 break;
2804
2805 case 0:
2806 /*
2807 * New allocation is not contiguous with another
2808 * delayed allocation.
2809 * Insert a new entry.
2810 */
2811 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002812 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 break;
2814 }
2815 if (oldlen != newlen) {
2816 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002817 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2818 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 /*
2820 * Nothing to do for disk quota accounting here.
2821 */
2822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823}
2824
2825/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002826 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 */
2828STATIC int /* error */
2829xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002830 struct xfs_trans *tp,
2831 struct xfs_inode *ip,
2832 int whichfork,
2833 xfs_extnum_t *idx,
2834 struct xfs_btree_cur **curp,
2835 struct xfs_bmbt_irec *new,
2836 xfs_fsblock_t *first,
2837 struct xfs_defer_ops *dfops,
2838 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002840 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2841 struct xfs_mount *mp = ip->i_mount;
2842 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2846 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002847 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 int state; /* state bits, accessed thru macros */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
Christoph Hellwig6d045582017-04-11 16:45:54 -07002850 ASSERT(*idx >= 0);
2851 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002852 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002853 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002854
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002855 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002856
2857 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002858 if (whichfork == XFS_ATTR_FORK)
2859 state |= BMAP_ATTRFORK;
Christoph Hellwiga14234c2017-02-06 10:50:49 -08002860 if (whichfork == XFS_COW_FORK)
2861 state |= BMAP_COWFORK;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 /*
2864 * Check and set flags if this segment has a left neighbor.
2865 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002866 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002867 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002868 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002869 if (isnullstartblock(left.br_startblock))
2870 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 /*
2874 * Check and set flags if this segment has a current value.
2875 * Not true if we're inserting into the "hole" at eof.
2876 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002877 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002878 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002879 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002880 if (isnullstartblock(right.br_startblock))
2881 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 /*
2885 * We're inserting a real allocation between "left" and "right".
2886 * Set the contiguity flags. Don't let extents get too large.
2887 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002888 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2889 left.br_startoff + left.br_blockcount == new->br_startoff &&
2890 left.br_startblock + left.br_blockcount == new->br_startblock &&
2891 left.br_state == new->br_state &&
2892 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2893 state |= BMAP_LEFT_CONTIG;
2894
2895 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2896 new->br_startoff + new->br_blockcount == right.br_startoff &&
2897 new->br_startblock + new->br_blockcount == right.br_startblock &&
2898 new->br_state == right.br_state &&
2899 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2900 (!(state & BMAP_LEFT_CONTIG) ||
2901 left.br_blockcount + new->br_blockcount +
2902 right.br_blockcount <= MAXEXTLEN))
2903 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002905 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 /*
2907 * Select which case we're in here, and implement it.
2908 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002909 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2910 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 /*
2912 * New allocation is contiguous with real allocations on the
2913 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002914 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002916 --*idx;
2917 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2918 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 left.br_blockcount + new->br_blockcount +
2920 right.br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002921 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002922
Christoph Hellwig6d045582017-04-11 16:45:54 -07002923 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002924
Christoph Hellwig6d045582017-04-11 16:45:54 -07002925 XFS_IFORK_NEXT_SET(ip, whichfork,
2926 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2927 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002928 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002929 } else {
2930 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002931 error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002932 right.br_startblock, right.br_blockcount,
2933 &i);
2934 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002935 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002936 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002937 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002938 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002939 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002940 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002941 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002942 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002943 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002944 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002945 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002946 left.br_startblock,
2947 left.br_blockcount +
2948 new->br_blockcount +
2949 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002950 left.br_state);
2951 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002952 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002954 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002956 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 /*
2958 * New allocation is contiguous with a real allocation
2959 * on the left.
2960 * Merge the new allocation with the left neighbor.
2961 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002962 --*idx;
2963 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2964 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 left.br_blockcount + new->br_blockcount);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002966 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002967
Christoph Hellwig6d045582017-04-11 16:45:54 -07002968 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002969 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002970 } else {
2971 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002972 error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002973 left.br_startblock, left.br_blockcount,
2974 &i);
2975 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002976 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002977 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002978 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002979 left.br_startblock,
2980 left.br_blockcount +
2981 new->br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002982 left.br_state);
2983 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002984 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002986 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002988 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 /*
2990 * New allocation is contiguous with a real allocation
2991 * on the right.
2992 * Merge the new allocation with the right neighbor.
2993 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002994 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2995 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Christoph Hellwig2f2b3222011-05-11 15:04:06 +00002996 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 new->br_blockcount + right.br_blockcount,
2998 right.br_state);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002999 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00003000
Christoph Hellwig6d045582017-04-11 16:45:54 -07003001 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003002 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003003 } else {
3004 rval = 0;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003005 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003006 right.br_startoff,
3007 right.br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003008 right.br_blockcount, &i);
3009 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003010 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003011 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003012 error = xfs_bmbt_update(cur, new->br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003013 new->br_startblock,
3014 new->br_blockcount +
3015 right.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003016 right.br_state);
3017 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003018 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003020 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
3022 case 0:
3023 /*
3024 * New allocation is not contiguous with another
3025 * real allocation.
3026 * Insert a new entry.
3027 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003028 xfs_iext_insert(ip, *idx, 1, new, state);
3029 XFS_IFORK_NEXT_SET(ip, whichfork,
3030 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3031 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003032 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003033 } else {
3034 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003035 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003036 new->br_startoff,
3037 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003038 new->br_blockcount, &i);
3039 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003040 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003041 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003042 cur->bc_rec.b.br_state = new->br_state;
3043 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003044 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003045 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003046 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003048 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003050
Darrick J. Wong9c194642016-08-03 12:16:05 +10003051 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003052 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10003053 if (error)
3054 goto done;
3055
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003056 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003057 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003058 int tmp_logflags; /* partial log flag return val */
3059
Christoph Hellwig6d045582017-04-11 16:45:54 -07003060 ASSERT(cur == NULL);
3061 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003062 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003063 *logflagsp |= tmp_logflags;
3064 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003065 if (error)
3066 goto done;
3067 }
3068
3069 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003070 if (cur)
3071 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003072
Christoph Hellwig6d045582017-04-11 16:45:54 -07003073 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003074done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07003075 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003076 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
Nathan Scottdd9f4382006-01-11 15:28:28 +11003079/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003080 * Functions used in the extent read, allocate and remove paths
3081 */
3082
3083/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003084 * Adjust the size of the new extent based on di_extsize and rt extsize.
3085 */
Dave Chinner68988112013-08-12 20:49:42 +10003086int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003087xfs_bmap_extsize_align(
3088 xfs_mount_t *mp,
3089 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3090 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3091 xfs_extlen_t extsz, /* align to this extent size */
3092 int rt, /* is this a realtime inode? */
3093 int eof, /* is extent at end-of-file? */
3094 int delay, /* creating delalloc extent? */
3095 int convert, /* overwriting unwritten extent? */
3096 xfs_fileoff_t *offp, /* in/out: aligned offset */
3097 xfs_extlen_t *lenp) /* in/out: aligned length */
3098{
3099 xfs_fileoff_t orig_off; /* original offset */
3100 xfs_extlen_t orig_alen; /* original length */
3101 xfs_fileoff_t orig_end; /* original off+len */
3102 xfs_fileoff_t nexto; /* next file offset */
3103 xfs_fileoff_t prevo; /* previous file offset */
3104 xfs_fileoff_t align_off; /* temp for offset */
3105 xfs_extlen_t align_alen; /* temp for length */
3106 xfs_extlen_t temp; /* temp for calculations */
3107
3108 if (convert)
3109 return 0;
3110
3111 orig_off = align_off = *offp;
3112 orig_alen = align_alen = *lenp;
3113 orig_end = orig_off + orig_alen;
3114
3115 /*
3116 * If this request overlaps an existing extent, then don't
3117 * attempt to perform any additional alignment.
3118 */
3119 if (!delay && !eof &&
3120 (orig_off >= gotp->br_startoff) &&
3121 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3122 return 0;
3123 }
3124
3125 /*
3126 * If the file offset is unaligned vs. the extent size
3127 * we need to align it. This will be possible unless
3128 * the file was previously written with a kernel that didn't
3129 * perform this alignment, or if a truncate shot us in the
3130 * foot.
3131 */
3132 temp = do_mod(orig_off, extsz);
3133 if (temp) {
3134 align_alen += temp;
3135 align_off -= temp;
3136 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003137
3138 /* Same adjustment for the end of the requested area. */
3139 temp = (align_alen % extsz);
3140 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003141 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003142
3143 /*
3144 * For large extent hint sizes, the aligned extent might be larger than
3145 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3146 * the length back under MAXEXTLEN. The outer allocation loops handle
3147 * short allocation just fine, so it is safe to do this. We only want to
3148 * do it when we are forced to, though, because it means more allocation
3149 * operations are required.
3150 */
3151 while (align_alen > MAXEXTLEN)
3152 align_alen -= extsz;
3153 ASSERT(align_alen <= MAXEXTLEN);
3154
Nathan Scottdd9f4382006-01-11 15:28:28 +11003155 /*
3156 * If the previous block overlaps with this proposed allocation
3157 * then move the start forward without adjusting the length.
3158 */
3159 if (prevp->br_startoff != NULLFILEOFF) {
3160 if (prevp->br_startblock == HOLESTARTBLOCK)
3161 prevo = prevp->br_startoff;
3162 else
3163 prevo = prevp->br_startoff + prevp->br_blockcount;
3164 } else
3165 prevo = 0;
3166 if (align_off != orig_off && align_off < prevo)
3167 align_off = prevo;
3168 /*
3169 * If the next block overlaps with this proposed allocation
3170 * then move the start back without adjusting the length,
3171 * but not before offset 0.
3172 * This may of course make the start overlap previous block,
3173 * and if we hit the offset 0 limit then the next block
3174 * can still overlap too.
3175 */
3176 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3177 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3178 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3179 nexto = gotp->br_startoff + gotp->br_blockcount;
3180 else
3181 nexto = gotp->br_startoff;
3182 } else
3183 nexto = NULLFILEOFF;
3184 if (!eof &&
3185 align_off + align_alen != orig_end &&
3186 align_off + align_alen > nexto)
3187 align_off = nexto > align_alen ? nexto - align_alen : 0;
3188 /*
3189 * If we're now overlapping the next or previous extent that
3190 * means we can't fit an extsz piece in this hole. Just move
3191 * the start forward to the first valid spot and set
3192 * the length so we hit the end.
3193 */
3194 if (align_off != orig_off && align_off < prevo)
3195 align_off = prevo;
3196 if (align_off + align_alen != orig_end &&
3197 align_off + align_alen > nexto &&
3198 nexto != NULLFILEOFF) {
3199 ASSERT(nexto > prevo);
3200 align_alen = nexto - align_off;
3201 }
3202
3203 /*
3204 * If realtime, and the result isn't a multiple of the realtime
3205 * extent size we need to remove blocks until it is.
3206 */
3207 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3208 /*
3209 * We're not covering the original request, or
3210 * we won't be able to once we fix the length.
3211 */
3212 if (orig_off < align_off ||
3213 orig_end > align_off + align_alen ||
3214 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003215 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003216 /*
3217 * Try to fix it by moving the start up.
3218 */
3219 if (align_off + temp <= orig_off) {
3220 align_alen -= temp;
3221 align_off += temp;
3222 }
3223 /*
3224 * Try to fix it by moving the end in.
3225 */
3226 else if (align_off + align_alen - temp >= orig_end)
3227 align_alen -= temp;
3228 /*
3229 * Set the start to the minimum then trim the length.
3230 */
3231 else {
3232 align_alen -= orig_off - align_off;
3233 align_off = orig_off;
3234 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3235 }
3236 /*
3237 * Result doesn't cover the request, fail it.
3238 */
3239 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003240 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003241 } else {
3242 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003243 /* see MAXEXTLEN handling above */
3244 ASSERT(orig_end <= align_off + align_alen ||
3245 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003246 }
3247
3248#ifdef DEBUG
3249 if (!eof && gotp->br_startoff != NULLFILEOFF)
3250 ASSERT(align_off + align_alen <= gotp->br_startoff);
3251 if (prevp->br_startoff != NULLFILEOFF)
3252 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3253#endif
3254
3255 *lenp = align_alen;
3256 *offp = align_off;
3257 return 0;
3258}
3259
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260#define XFS_ALLOC_GAP_UNITS 4
3261
Dave Chinner68988112013-08-12 20:49:42 +10003262void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003263xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003264 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265{
3266 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3268 xfs_mount_t *mp; /* mount point structure */
3269 int nullfb; /* true if ap->firstblock isn't set */
3270 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
3272#define ISVALID(x,y) \
3273 (rt ? \
3274 (x) < mp->m_sb.sb_rblocks : \
3275 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3276 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3277 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003280 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003281 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3282 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003283 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 /*
3285 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003286 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003288 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3289 !isnullstartblock(ap->prev.br_startblock) &&
3290 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3291 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003292 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 /*
3294 * Adjust for the gap between prevp and us.
3295 */
Dave Chinner3a756672011-09-18 20:40:58 +00003296 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003297 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003299 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3300 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003301 }
3302 /*
3303 * If not at eof, then compare the two neighbor blocks.
3304 * Figure out whether either one gives us a good starting point,
3305 * and pick the better one.
3306 */
3307 else if (!ap->eof) {
3308 xfs_fsblock_t gotbno; /* right side block number */
3309 xfs_fsblock_t gotdiff=0; /* right side difference */
3310 xfs_fsblock_t prevbno; /* left side block number */
3311 xfs_fsblock_t prevdiff=0; /* left side difference */
3312
3313 /*
3314 * If there's a previous (left) block, select a requested
3315 * start block based on it.
3316 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003317 if (ap->prev.br_startoff != NULLFILEOFF &&
3318 !isnullstartblock(ap->prev.br_startblock) &&
3319 (prevbno = ap->prev.br_startblock +
3320 ap->prev.br_blockcount) &&
3321 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 /*
3323 * Calculate gap to end of previous block.
3324 */
Dave Chinner3a756672011-09-18 20:40:58 +00003325 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003326 (ap->prev.br_startoff +
3327 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328 /*
3329 * Figure the startblock based on the previous block's
3330 * end and the gap size.
3331 * Heuristic!
3332 * If the gap is large relative to the piece we're
3333 * allocating, or using it gives us an invalid block
3334 * number, then just use the end of the previous block.
3335 */
Dave Chinner3a756672011-09-18 20:40:58 +00003336 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003338 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 prevbno += adjust;
3340 else
3341 prevdiff += adjust;
3342 /*
3343 * If the firstblock forbids it, can't use it,
3344 * must use default.
3345 */
3346 if (!rt && !nullfb &&
3347 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3348 prevbno = NULLFSBLOCK;
3349 }
3350 /*
3351 * No previous block or can't follow it, just default.
3352 */
3353 else
3354 prevbno = NULLFSBLOCK;
3355 /*
3356 * If there's a following (right) block, select a requested
3357 * start block based on it.
3358 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003359 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 /*
3361 * Calculate gap to start of next block.
3362 */
Dave Chinner3a756672011-09-18 20:40:58 +00003363 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 /*
3365 * Figure the startblock based on the next block's
3366 * start and the gap size.
3367 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003368 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 /*
3370 * Heuristic!
3371 * If the gap is large relative to the piece we're
3372 * allocating, or using it gives us an invalid block
3373 * number, then just use the start of the next block
3374 * offset by our length.
3375 */
Dave Chinner3a756672011-09-18 20:40:58 +00003376 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 ISVALID(gotbno - gotdiff, gotbno))
3378 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003379 else if (ISVALID(gotbno - ap->length, gotbno)) {
3380 gotbno -= ap->length;
3381 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 } else
3383 gotdiff += adjust;
3384 /*
3385 * If the firstblock forbids it, can't use it,
3386 * must use default.
3387 */
3388 if (!rt && !nullfb &&
3389 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3390 gotbno = NULLFSBLOCK;
3391 }
3392 /*
3393 * No next block, just default.
3394 */
3395 else
3396 gotbno = NULLFSBLOCK;
3397 /*
3398 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003399 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 */
3401 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003402 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003404 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003406 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003408#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003409}
3410
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003411static int
3412xfs_bmap_longest_free_extent(
3413 struct xfs_trans *tp,
3414 xfs_agnumber_t ag,
3415 xfs_extlen_t *blen,
3416 int *notinit)
3417{
3418 struct xfs_mount *mp = tp->t_mountp;
3419 struct xfs_perag *pag;
3420 xfs_extlen_t longest;
3421 int error = 0;
3422
3423 pag = xfs_perag_get(mp, ag);
3424 if (!pag->pagf_init) {
3425 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3426 if (error)
3427 goto out;
3428
3429 if (!pag->pagf_init) {
3430 *notinit = 1;
3431 goto out;
3432 }
3433 }
3434
Dave Chinner50adbcb2015-06-22 10:04:31 +10003435 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003436 xfs_alloc_min_freelist(mp, pag),
3437 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003438 if (*blen < longest)
3439 *blen = longest;
3440
3441out:
3442 xfs_perag_put(pag);
3443 return error;
3444}
3445
3446static void
3447xfs_bmap_select_minlen(
3448 struct xfs_bmalloca *ap,
3449 struct xfs_alloc_arg *args,
3450 xfs_extlen_t *blen,
3451 int notinit)
3452{
3453 if (notinit || *blen < ap->minlen) {
3454 /*
3455 * Since we did a BUF_TRYLOCK above, it is possible that
3456 * there is space for this request.
3457 */
3458 args->minlen = ap->minlen;
3459 } else if (*blen < args->maxlen) {
3460 /*
3461 * If the best seen length is less than the request length,
3462 * use the best as the minimum.
3463 */
3464 args->minlen = *blen;
3465 } else {
3466 /*
3467 * Otherwise we've seen an extent as big as maxlen, use that
3468 * as the minimum.
3469 */
3470 args->minlen = args->maxlen;
3471 }
3472}
3473
Nathan Scotta365bdd2006-03-14 13:34:16 +11003474STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003475xfs_bmap_btalloc_nullfb(
3476 struct xfs_bmalloca *ap,
3477 struct xfs_alloc_arg *args,
3478 xfs_extlen_t *blen)
3479{
3480 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003481 xfs_agnumber_t ag, startag;
3482 int notinit = 0;
3483 int error;
3484
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003485 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003486 args->total = ap->total;
3487
Christoph Hellwigc467c042010-02-15 23:34:42 +00003488 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3489 if (startag == NULLAGNUMBER)
3490 startag = ag = 0;
3491
Dave Chinner14b064c2011-01-27 12:16:28 +11003492 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003493 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3494 &notinit);
3495 if (error)
3496 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003497
Christoph Hellwigc467c042010-02-15 23:34:42 +00003498 if (++ag == mp->m_sb.sb_agcount)
3499 ag = 0;
3500 if (ag == startag)
3501 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003502 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003503
3504 xfs_bmap_select_minlen(ap, args, blen, notinit);
3505 return 0;
3506}
3507
3508STATIC int
3509xfs_bmap_btalloc_filestreams(
3510 struct xfs_bmalloca *ap,
3511 struct xfs_alloc_arg *args,
3512 xfs_extlen_t *blen)
3513{
3514 struct xfs_mount *mp = ap->ip->i_mount;
3515 xfs_agnumber_t ag;
3516 int notinit = 0;
3517 int error;
3518
3519 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3520 args->total = ap->total;
3521
3522 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3523 if (ag == NULLAGNUMBER)
3524 ag = 0;
3525
3526 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3527 if (error)
3528 return error;
3529
3530 if (*blen < args->maxlen) {
3531 error = xfs_filestream_new_ag(ap, &ag);
3532 if (error)
3533 return error;
3534
3535 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3536 &notinit);
3537 if (error)
3538 return error;
3539
3540 }
3541
3542 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003543
3544 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003545 * Set the failure fallback case to look in the selected AG as stream
3546 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003547 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003548 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003549 return 0;
3550}
3551
3552STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003553xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003554 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003555{
3556 xfs_mount_t *mp; /* mount point structure */
3557 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003558 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003559 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003560 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003561 xfs_alloc_arg_t args;
3562 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003563 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003564 int nullfb; /* true if ap->firstblock isn't set */
3565 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003566 int tryagain;
3567 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003568 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003569
Dave Chinnera99ebf42011-12-01 11:24:20 +00003570 ASSERT(ap->length);
3571
Nathan Scotta365bdd2006-03-14 13:34:16 +11003572 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003573
3574 /* stripe alignment for allocation is determined by mount parameters */
3575 stripe_align = 0;
3576 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3577 stripe_align = mp->m_swidth;
3578 else if (mp->m_dalign)
3579 stripe_align = mp->m_dalign;
3580
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003581 if (ap->flags & XFS_BMAPI_COWFORK)
3582 align = xfs_get_cowextsz_hint(ap->ip);
3583 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003584 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003585 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003586 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003587 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003588 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003589 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003590 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003591 }
Dave Chinner33177f052013-12-12 16:34:36 +11003592
3593
Dave Chinner0937e0f2011-09-18 20:40:57 +00003594 nullfb = *ap->firstblock == NULLFSBLOCK;
3595 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003596 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003597 if (xfs_alloc_is_userdata(ap->datatype) &&
3598 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003599 ag = xfs_filestream_lookup_ag(ap->ip);
3600 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003601 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003602 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003603 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003604 }
3605 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003606 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003607
3608 xfs_bmap_adjacent(ap);
3609
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003611 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 * it's in the right allocation group.
3613 */
Dave Chinner3a756672011-09-18 20:40:58 +00003614 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 ;
3616 else
Dave Chinner3a756672011-09-18 20:40:58 +00003617 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 * Normal allocation, done through xfs_alloc_vextent.
3620 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003621 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003622 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003623 args.tp = ap->tp;
3624 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003625 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003626 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003627
3628 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003629 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003630 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003631 blen = 0;
3632 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003633 /*
3634 * Search for an allocation group with a single extent large
3635 * enough for the request. If one isn't found, then adjust
3636 * the minimum allocation size to the largest space found.
3637 */
Dave Chinner292378e2016-09-26 08:21:28 +10003638 if (xfs_alloc_is_userdata(ap->datatype) &&
3639 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003640 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3641 else
3642 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003643 if (error)
3644 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003645 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003646 if (xfs_inode_is_filestream(ap->ip))
3647 args.type = XFS_ALLOCTYPE_FIRST_AG;
3648 else
3649 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003650 args.total = args.minlen = ap->minlen;
3651 } else {
3652 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3653 args.total = ap->total;
3654 args.minlen = ap->minlen;
3655 }
David Chinner957d0eb2007-06-18 16:50:37 +10003656 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003657 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003658 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003659 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003660 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003661 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003662 args.prod = 1;
3663 args.mod = 0;
3664 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003665 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003666 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003667 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3668 }
3669 /*
3670 * If we are not low on available data blocks, and the
3671 * underlying logical volume manager is a stripe, and
3672 * the file offset is zero then try to allocate data
3673 * blocks on stripe unit boundary.
3674 * NOTE: ap->aeof is only set if the allocation length
3675 * is >= the stripe unit and the allocation offset is
3676 * at the end of file.
3677 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003678 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003679 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003680 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003681 atype = args.type;
3682 isaligned = 1;
3683 /*
3684 * Adjust for alignment
3685 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003686 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003687 args.minlen = blen - args.alignment;
3688 args.minalignslop = 0;
3689 } else {
3690 /*
3691 * First try an exact bno allocation.
3692 * If it fails then do a near or start bno
3693 * allocation with alignment turned on.
3694 */
3695 atype = args.type;
3696 tryagain = 1;
3697 args.type = XFS_ALLOCTYPE_THIS_BNO;
3698 args.alignment = 1;
3699 /*
3700 * Compute the minlen+alignment for the
3701 * next case. Set slop so that the value
3702 * of minlen+alignment+slop doesn't go up
3703 * between the calls.
3704 */
Dave Chinner33177f052013-12-12 16:34:36 +11003705 if (blen > stripe_align && blen <= args.maxlen)
3706 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003707 else
3708 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003709 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003710 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003711 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003712 args.minlen - 1;
3713 else
3714 args.minalignslop = 0;
3715 }
3716 } else {
3717 args.alignment = 1;
3718 args.minalignslop = 0;
3719 }
3720 args.minleft = ap->minleft;
3721 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003722 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003723 args.datatype = ap->datatype;
3724 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003725 args.ip = ap->ip;
3726
3727 error = xfs_alloc_vextent(&args);
3728 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003729 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003730
Nathan Scotta365bdd2006-03-14 13:34:16 +11003731 if (tryagain && args.fsbno == NULLFSBLOCK) {
3732 /*
3733 * Exact allocation failed. Now try with alignment
3734 * turned on.
3735 */
3736 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003737 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003738 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003739 args.minlen = nextminlen;
3740 args.minalignslop = 0;
3741 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 if ((error = xfs_alloc_vextent(&args)))
3743 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003744 }
3745 if (isaligned && args.fsbno == NULLFSBLOCK) {
3746 /*
3747 * allocation failed, so turn off alignment and
3748 * try again.
3749 */
3750 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003751 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003752 args.alignment = 0;
3753 if ((error = xfs_alloc_vextent(&args)))
3754 return error;
3755 }
3756 if (args.fsbno == NULLFSBLOCK && nullfb &&
3757 args.minlen > ap->minlen) {
3758 args.minlen = ap->minlen;
3759 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003760 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003761 if ((error = xfs_alloc_vextent(&args)))
3762 return error;
3763 }
3764 if (args.fsbno == NULLFSBLOCK && nullfb) {
3765 args.fsbno = 0;
3766 args.type = XFS_ALLOCTYPE_FIRST_AG;
3767 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003768 if ((error = xfs_alloc_vextent(&args)))
3769 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003770 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003771 }
3772 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003773 /*
3774 * check the allocation happened at the same or higher AG than
3775 * the first block that was allocated.
3776 */
3777 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003778 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3779 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003780
Dave Chinner3a756672011-09-18 20:40:58 +00003781 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003782 if (*ap->firstblock == NULLFSBLOCK)
3783 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003784 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003785 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003786 if (!(ap->flags & XFS_BMAPI_COWFORK))
3787 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003788 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3789 if (ap->wasdel)
3790 ap->ip->i_delayed_blks -= args.len;
3791 /*
3792 * Adjust the disk quota also. This was reserved
3793 * earlier.
3794 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003795 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003796 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3797 XFS_TRANS_DQ_BCOUNT,
3798 (long) args.len);
3799 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003800 ap->blkno = NULLFSBLOCK;
3801 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
3803 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003804}
3805
3806/*
3807 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3808 * It figures out where to ask the underlying allocator to put the new extent.
3809 */
3810STATIC int
3811xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003812 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003813{
Dave Chinner292378e2016-09-26 08:21:28 +10003814 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3815 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003816 return xfs_bmap_rtalloc(ap);
3817 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818}
3819
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003820/* Trim extent to fit a logical block range. */
3821void
3822xfs_trim_extent(
3823 struct xfs_bmbt_irec *irec,
3824 xfs_fileoff_t bno,
3825 xfs_filblks_t len)
3826{
3827 xfs_fileoff_t distance;
3828 xfs_fileoff_t end = bno + len;
3829
3830 if (irec->br_startoff + irec->br_blockcount <= bno ||
3831 irec->br_startoff >= end) {
3832 irec->br_blockcount = 0;
3833 return;
3834 }
3835
3836 if (irec->br_startoff < bno) {
3837 distance = bno - irec->br_startoff;
3838 if (isnullstartblock(irec->br_startblock))
3839 irec->br_startblock = DELAYSTARTBLOCK;
3840 if (irec->br_startblock != DELAYSTARTBLOCK &&
3841 irec->br_startblock != HOLESTARTBLOCK)
3842 irec->br_startblock += distance;
3843 irec->br_startoff += distance;
3844 irec->br_blockcount -= distance;
3845 }
3846
3847 if (end < irec->br_startoff + irec->br_blockcount) {
3848 distance = irec->br_startoff + irec->br_blockcount - end;
3849 irec->br_blockcount -= distance;
3850 }
3851}
3852
Brian Foster40214d12017-10-13 09:47:46 -07003853/* trim extent to within eof */
3854void
3855xfs_trim_extent_eof(
3856 struct xfs_bmbt_irec *irec,
3857 struct xfs_inode *ip)
3858
3859{
3860 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3861 i_size_read(VFS_I(ip))));
3862}
3863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003865 * Trim the returned map to the required bounds
3866 */
3867STATIC void
3868xfs_bmapi_trim_map(
3869 struct xfs_bmbt_irec *mval,
3870 struct xfs_bmbt_irec *got,
3871 xfs_fileoff_t *bno,
3872 xfs_filblks_t len,
3873 xfs_fileoff_t obno,
3874 xfs_fileoff_t end,
3875 int n,
3876 int flags)
3877{
3878 if ((flags & XFS_BMAPI_ENTIRE) ||
3879 got->br_startoff + got->br_blockcount <= obno) {
3880 *mval = *got;
3881 if (isnullstartblock(got->br_startblock))
3882 mval->br_startblock = DELAYSTARTBLOCK;
3883 return;
3884 }
3885
3886 if (obno > *bno)
3887 *bno = obno;
3888 ASSERT((*bno >= obno) || (n == 0));
3889 ASSERT(*bno < end);
3890 mval->br_startoff = *bno;
3891 if (isnullstartblock(got->br_startblock))
3892 mval->br_startblock = DELAYSTARTBLOCK;
3893 else
3894 mval->br_startblock = got->br_startblock +
3895 (*bno - got->br_startoff);
3896 /*
3897 * Return the minimum of what we got and what we asked for for
3898 * the length. We can use the len variable here because it is
3899 * modified below and we could have been there before coming
3900 * here if the first part of the allocation didn't overlap what
3901 * was asked for.
3902 */
3903 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3904 got->br_blockcount - (*bno - got->br_startoff));
3905 mval->br_state = got->br_state;
3906 ASSERT(mval->br_blockcount <= len);
3907 return;
3908}
3909
3910/*
3911 * Update and validate the extent map to return
3912 */
3913STATIC void
3914xfs_bmapi_update_map(
3915 struct xfs_bmbt_irec **map,
3916 xfs_fileoff_t *bno,
3917 xfs_filblks_t *len,
3918 xfs_fileoff_t obno,
3919 xfs_fileoff_t end,
3920 int *n,
3921 int flags)
3922{
3923 xfs_bmbt_irec_t *mval = *map;
3924
3925 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3926 ((mval->br_startoff + mval->br_blockcount) <= end));
3927 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3928 (mval->br_startoff < obno));
3929
3930 *bno = mval->br_startoff + mval->br_blockcount;
3931 *len = end - *bno;
3932 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3933 /* update previous map with new information */
3934 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3935 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3936 ASSERT(mval->br_state == mval[-1].br_state);
3937 mval[-1].br_blockcount = mval->br_blockcount;
3938 mval[-1].br_state = mval->br_state;
3939 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3940 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3941 mval[-1].br_startblock != HOLESTARTBLOCK &&
3942 mval->br_startblock == mval[-1].br_startblock +
3943 mval[-1].br_blockcount &&
3944 ((flags & XFS_BMAPI_IGSTATE) ||
3945 mval[-1].br_state == mval->br_state)) {
3946 ASSERT(mval->br_startoff ==
3947 mval[-1].br_startoff + mval[-1].br_blockcount);
3948 mval[-1].br_blockcount += mval->br_blockcount;
3949 } else if (*n > 0 &&
3950 mval->br_startblock == DELAYSTARTBLOCK &&
3951 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3952 mval->br_startoff ==
3953 mval[-1].br_startoff + mval[-1].br_blockcount) {
3954 mval[-1].br_blockcount += mval->br_blockcount;
3955 mval[-1].br_state = mval->br_state;
3956 } else if (!((*n == 0) &&
3957 ((mval->br_startoff + mval->br_blockcount) <=
3958 obno))) {
3959 mval++;
3960 (*n)++;
3961 }
3962 *map = mval;
3963}
3964
3965/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003966 * Map file blocks to filesystem blocks without allocation.
3967 */
3968int
3969xfs_bmapi_read(
3970 struct xfs_inode *ip,
3971 xfs_fileoff_t bno,
3972 xfs_filblks_t len,
3973 struct xfs_bmbt_irec *mval,
3974 int *nmap,
3975 int flags)
3976{
3977 struct xfs_mount *mp = ip->i_mount;
3978 struct xfs_ifork *ifp;
3979 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003980 xfs_fileoff_t obno;
3981 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003982 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003983 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003984 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003985 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003986 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003987
3988 ASSERT(*nmap >= 1);
3989 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003990 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003991 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003992
3993 if (unlikely(XFS_TEST_ERROR(
3994 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3995 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003996 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003997 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003998 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003999 }
4000
4001 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004002 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004003
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004004 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004005
4006 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00004007
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004008 /* No CoW fork? Return a hole. */
4009 if (whichfork == XFS_COW_FORK && !ifp) {
4010 mval->br_startoff = bno;
4011 mval->br_startblock = HOLESTARTBLOCK;
4012 mval->br_blockcount = len;
4013 mval->br_state = XFS_EXT_NORM;
4014 *nmap = 1;
4015 return 0;
4016 }
4017
Dave Chinner5c8ed202011-09-18 20:40:45 +00004018 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4019 error = xfs_iread_extents(NULL, ip, whichfork);
4020 if (error)
4021 return error;
4022 }
4023
Christoph Hellwig334f3422016-11-24 11:39:43 +11004024 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4025 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004026 end = bno + len;
4027 obno = bno;
4028
4029 while (bno < end && n < *nmap) {
4030 /* Reading past eof, act as though there's a hole up to end. */
4031 if (eof)
4032 got.br_startoff = end;
4033 if (got.br_startoff > bno) {
4034 /* Reading in a hole. */
4035 mval->br_startoff = bno;
4036 mval->br_startblock = HOLESTARTBLOCK;
4037 mval->br_blockcount =
4038 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4039 mval->br_state = XFS_EXT_NORM;
4040 bno += mval->br_blockcount;
4041 len -= mval->br_blockcount;
4042 mval++;
4043 n++;
4044 continue;
4045 }
4046
4047 /* set up the extent map to return. */
4048 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4049 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4050
4051 /* If we're done, stop now. */
4052 if (bno >= end || n >= *nmap)
4053 break;
4054
4055 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004056 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4057 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004058 }
4059 *nmap = n;
4060 return 0;
4061}
4062
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004063/*
4064 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4065 * global pool and the extent inserted into the inode in-core extent tree.
4066 *
4067 * On entry, got refers to the first extent beyond the offset of the extent to
4068 * allocate or eof is specified if no such extent exists. On return, got refers
4069 * to the extent record that was inserted to the inode fork.
4070 *
4071 * Note that the allocated extent may have been merged with contiguous extents
4072 * during insertion into the inode fork. Thus, got does not reflect the current
4073 * state of the inode fork on return. If necessary, the caller can use lastx to
4074 * look up the updated record in the inode fork.
4075 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10004076int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004077xfs_bmapi_reserve_delalloc(
4078 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004079 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004080 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004081 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004082 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004083 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004084 xfs_extnum_t *lastx,
4085 int eof)
4086{
4087 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004088 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004089 xfs_extlen_t alen;
4090 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004091 char rt = XFS_IS_REALTIME_INODE(ip);
4092 xfs_extlen_t extsz;
4093 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004094 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004095
Brian Foster974ae922016-11-28 14:57:42 +11004096 /*
4097 * Cap the alloc length. Keep track of prealloc so we know whether to
4098 * tag the inode before we return.
4099 */
4100 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004101 if (!eof)
4102 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004103 if (prealloc && alen >= len)
4104 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004105
4106 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004107 if (whichfork == XFS_COW_FORK)
4108 extsz = xfs_get_cowextsz_hint(ip);
4109 else
4110 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004111 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004112 struct xfs_bmbt_irec prev;
4113
4114 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4115 prev.br_startoff = NULLFILEOFF;
4116
4117 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004118 1, 0, &aoff, &alen);
4119 ASSERT(!error);
4120 }
4121
4122 if (rt)
4123 extsz = alen / mp->m_sb.sb_rextsize;
4124
4125 /*
4126 * Make a transaction-less quota reservation for delayed allocation
4127 * blocks. This number gets adjusted later. We return if we haven't
4128 * allocated blocks already inside this loop.
4129 */
4130 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4131 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4132 if (error)
4133 return error;
4134
4135 /*
4136 * Split changing sb for alen and indlen since they could be coming
4137 * from different places.
4138 */
4139 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4140 ASSERT(indlen > 0);
4141
4142 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004143 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004144 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004145 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004146 }
4147
4148 if (error)
4149 goto out_unreserve_quota;
4150
Dave Chinner0d485ad2015-02-23 21:22:03 +11004151 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004152 if (error)
4153 goto out_unreserve_blocks;
4154
4155
4156 ip->i_delayed_blks += alen;
4157
4158 got->br_startoff = aoff;
4159 got->br_startblock = nullstartblock(indlen);
4160 got->br_blockcount = alen;
4161 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004162
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004163 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004164
Brian Foster974ae922016-11-28 14:57:42 +11004165 /*
4166 * Tag the inode if blocks were preallocated. Note that COW fork
4167 * preallocation can occur at the start or end of the extent, even when
4168 * prealloc == 0, so we must also check the aligned offset and length.
4169 */
4170 if (whichfork == XFS_DATA_FORK && prealloc)
4171 xfs_inode_set_eofblocks_tag(ip);
4172 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4173 xfs_inode_set_cowblocks_tag(ip);
4174
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004175 return 0;
4176
4177out_unreserve_blocks:
4178 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004179 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004180 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004181 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004182out_unreserve_quota:
4183 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004184 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004185 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4186 return error;
4187}
4188
Dave Chinnercf11da92014-07-15 07:08:24 +10004189static int
4190xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004191 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004192{
4193 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004194 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004195 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004196 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004197 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004198
Dave Chinnera99ebf42011-12-01 11:24:20 +00004199 ASSERT(bma->length > 0);
4200
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004201 /*
4202 * For the wasdelay case, we could also just allocate the stuff asked
4203 * for in this bmap call but that wouldn't be as good.
4204 */
4205 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004206 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4207 bma->offset = bma->got.br_startoff;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004208 if (bma->idx)
4209 xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004210 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004211 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004212 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004213 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004214 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004215 }
4216
4217 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004218 * Set the data type being allocated. For the data fork, the first data
4219 * in the file is treated differently to all other allocations. For the
4220 * attribute fork, we only need to ensure the allocated range is not on
4221 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004222 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004223 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004224 bma->datatype = XFS_ALLOC_NOBUSY;
4225 if (whichfork == XFS_DATA_FORK) {
4226 if (bma->offset == 0)
4227 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4228 else
4229 bma->datatype |= XFS_ALLOC_USERDATA;
4230 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004231 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004232 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004233 }
4234
Dave Chinnere04426b2012-10-05 11:06:59 +10004235 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004236
4237 /*
4238 * Only want to do the alignment at the eof if it is userdata and
4239 * allocation length is larger than a stripe unit.
4240 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004241 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004242 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004243 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004244 if (error)
4245 return error;
4246 }
4247
4248 error = xfs_bmap_alloc(bma);
4249 if (error)
4250 return error;
4251
Dave Chinner29c8d172011-09-18 20:41:00 +00004252 if (bma->cur)
4253 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004254 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004255 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004256 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4257 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4258 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004259 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004260 }
4261 /*
4262 * Bump the number of extents we've allocated
4263 * in this call.
4264 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004265 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004266
Dave Chinner29c8d172011-09-18 20:41:00 +00004267 if (bma->cur)
4268 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004269 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4270
Dave Chinner963c30c2011-09-18 20:40:59 +00004271 bma->got.br_startoff = bma->offset;
4272 bma->got.br_startblock = bma->blkno;
4273 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004274 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004275
4276 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004277 * In the data fork, a wasdelay extent has been initialized, so
4278 * shouldn't be flagged as unwritten.
4279 *
4280 * For the cow fork, however, we convert delalloc reservations
4281 * (extents allocated for speculative preallocation) to
4282 * allocated unwritten extents, and only convert the unwritten
4283 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004284 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004285 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4286 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004287 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004288 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004289
Christoph Hellwigc6534242011-09-18 20:41:05 +00004290 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004291 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004292 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004293 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4294 whichfork, &bma->idx, &bma->cur, &bma->got,
4295 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004296
Christoph Hellwigc315c902011-09-18 20:41:02 +00004297 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004298 if (error)
4299 return error;
4300
4301 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004302 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4303 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4304 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004305 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004306 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004307
Dave Chinner963c30c2011-09-18 20:40:59 +00004308 ASSERT(bma->got.br_startoff <= bma->offset);
4309 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4310 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004311 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4312 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004313 return 0;
4314}
4315
Dave Chinnerb447fe52011-09-18 20:40:51 +00004316STATIC int
4317xfs_bmapi_convert_unwritten(
4318 struct xfs_bmalloca *bma,
4319 struct xfs_bmbt_irec *mval,
4320 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004321 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004322{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004323 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004324 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004325 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004326 int error;
4327
Dave Chinnerb447fe52011-09-18 20:40:51 +00004328 /* check if we need to do unwritten->real conversion */
4329 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4330 (flags & XFS_BMAPI_PREALLOC))
4331 return 0;
4332
4333 /* check if we need to do real->unwritten conversion */
4334 if (mval->br_state == XFS_EXT_NORM &&
4335 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4336 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4337 return 0;
4338
4339 /*
4340 * Modify (by adding) the state flag, if writing.
4341 */
4342 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004343 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4344 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004345 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004346 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004347 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004348 }
4349 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4350 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4351
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004352 /*
4353 * Before insertion into the bmbt, zero the range being converted
4354 * if required.
4355 */
4356 if (flags & XFS_BMAPI_ZERO) {
4357 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4358 mval->br_blockcount);
4359 if (error)
4360 return error;
4361 }
4362
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004363 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4364 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004365 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004366 /*
4367 * Log the inode core unconditionally in the unwritten extent conversion
4368 * path because the conversion might not have done so (e.g., if the
4369 * extent count hasn't changed). We need to make sure the inode is dirty
4370 * in the transaction for the sake of fsync(), even if nothing has
4371 * changed, because fsync() will not force the log for this transaction
4372 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004373 *
4374 * Note: If we're only converting cow fork extents, there aren't
4375 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004376 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004377 if (whichfork != XFS_COW_FORK)
4378 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004379 if (error)
4380 return error;
4381
4382 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004383 * Update our extent pointer, given that
4384 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4385 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004386 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004387 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004388
4389 /*
4390 * We may have combined previously unwritten space with written space,
4391 * so generate another request.
4392 */
4393 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004394 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004395 return 0;
4396}
4397
Christoph Hellwig44032802011-09-18 20:40:48 +00004398/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004399 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4400 * extent state if necessary. Details behaviour is controlled by the flags
4401 * parameter. Only allocates blocks from a single allocation group, to avoid
4402 * locking problems.
4403 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 * The returned value in "firstblock" from the first call in a transaction
4405 * must be remembered and presented to subsequent calls in "firstblock".
4406 * An upper bound for the number of blocks to be allocated is supplied to
4407 * the first call in "total"; if no allocation group has that many free
4408 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4409 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004410int
4411xfs_bmapi_write(
4412 struct xfs_trans *tp, /* transaction pointer */
4413 struct xfs_inode *ip, /* incore inode */
4414 xfs_fileoff_t bno, /* starting file offs. mapped */
4415 xfs_filblks_t len, /* length to map in file */
4416 int flags, /* XFS_BMAPI_... */
4417 xfs_fsblock_t *firstblock, /* first allocated block
4418 controls a.g. for allocs */
4419 xfs_extlen_t total, /* total blocks needed */
4420 struct xfs_bmbt_irec *mval, /* output: map values */
4421 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004422 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004424 struct xfs_mount *mp = ip->i_mount;
4425 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004426 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004427 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004428 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004429 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004430 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004431 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004432 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004435 xfs_fileoff_t orig_bno; /* original block number value */
4436 int orig_flags; /* original flags arg value */
4437 xfs_filblks_t orig_len; /* original value of len arg */
4438 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4439 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440
4441 orig_bno = bno;
4442 orig_len = len;
4443 orig_flags = flags;
4444 orig_mval = mval;
4445 orig_nmap = *nmap;
4446#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004447 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004448
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004450 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4451 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004452 ASSERT(tp != NULL ||
4453 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4454 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004455 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004456 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004457 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004458 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004459
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004460 /* zeroing is for currently only for data extents, not metadata */
4461 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4462 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4463 /*
4464 * we can allocate unwritten extents or pre-zero allocated blocks,
4465 * but it makes no sense to do both at once. This would result in
4466 * zeroing the unwritten extent twice, but it still being an
4467 * unwritten extent....
4468 */
4469 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4470 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 if (unlikely(XFS_TEST_ERROR(
4473 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004474 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004475 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004476 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004477 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004479
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004481 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004482
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004484
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004485 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004486
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004487 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004489 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004491 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004492 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004493 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004494 }
4495
4496 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4497 error = xfs_iread_extents(tp, ip, whichfork);
4498 if (error)
4499 goto error0;
4500 }
4501
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 n = 0;
4503 end = bno + len;
4504 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004505
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004506 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4507 eof = true;
4508 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4509 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004510 bma.tp = tp;
4511 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004512 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004513 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004514 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004515 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004516
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004518 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004519
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004520 /* in hole or beyoned EOF? */
4521 if (eof || bma.got.br_startoff > bno) {
4522 if (flags & XFS_BMAPI_DELALLOC) {
4523 /*
4524 * For the COW fork we can reasonably get a
4525 * request for converting an extent that races
4526 * with other threads already having converted
4527 * part of it, as there converting COW to
4528 * regular blocks is not protected using the
4529 * IOLOCK.
4530 */
4531 ASSERT(flags & XFS_BMAPI_COWFORK);
4532 if (!(flags & XFS_BMAPI_COWFORK)) {
4533 error = -EIO;
4534 goto error0;
4535 }
4536
4537 if (eof || bno >= end)
4538 break;
4539 } else {
4540 need_alloc = true;
4541 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004542 } else if (isnullstartblock(bma.got.br_startblock)) {
4543 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004544 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004545
4546 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 * First, deal with the hole before the allocated space
4548 * that we found, if any.
4549 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004550 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004551 bma.eof = eof;
4552 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4553 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004554 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004555 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004556
Dave Chinnera99ebf42011-12-01 11:24:20 +00004557 /*
4558 * There's a 32/64 bit type mismatch between the
4559 * allocation length request (which can be 64 bits in
4560 * length) and the bma length request, which is
4561 * xfs_extlen_t and therefore 32 bits. Hence we have to
4562 * check for 32-bit overflows and handle them here.
4563 */
4564 if (len > (xfs_filblks_t)MAXEXTLEN)
4565 bma.length = MAXEXTLEN;
4566 else
4567 bma.length = len;
4568
4569 ASSERT(len > 0);
4570 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004571 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 if (error)
4573 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004574 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004575 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004576
4577 /*
4578 * If this is a CoW allocation, record the data in
4579 * the refcount btree for orphan recovery.
4580 */
4581 if (whichfork == XFS_COW_FORK) {
4582 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4583 bma.blkno, bma.length);
4584 if (error)
4585 goto error0;
4586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004588
Dave Chinneraef9a892011-09-18 20:40:44 +00004589 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004590 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4591 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
Dave Chinnerb447fe52011-09-18 20:40:51 +00004593 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004594 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004595 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004596 continue;
4597 if (error)
4598 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
Dave Chinneraef9a892011-09-18 20:40:44 +00004600 /* update the extent map to return */
4601 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 /*
4604 * If we're done, stop now. Stop when we've allocated
4605 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4606 * the transaction may get too big.
4607 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004608 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004610
4611 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004612 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004613 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4614 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004617
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 /*
4619 * Transform from btree to extents, give it cur.
4620 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004621 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004622 int tmp_logflags = 0;
4623
Dave Chinner29c8d172011-09-18 20:41:00 +00004624 ASSERT(bma.cur);
4625 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004627 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 if (error)
4629 goto error0;
4630 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004631
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004633 XFS_IFORK_NEXTENTS(ip, whichfork) >
4634 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636error0:
4637 /*
4638 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004639 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004641 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004643 bma.logflags &= ~xfs_ilog_fext(whichfork);
4644 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004646 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 /*
4648 * Log whatever the flags say, even if error. Otherwise we might miss
4649 * detecting a case where the data is changed, there's an error,
4650 * and it's not logged so we don't shutdown when we should.
4651 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004652 if (bma.logflags)
4653 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004654
Dave Chinner29c8d172011-09-18 20:41:00 +00004655 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 if (!error) {
4657 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004658 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004660 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004661 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004663 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4665 }
4666 if (!error)
4667 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4668 orig_nmap, *nmap);
4669 return error;
4670}
4671
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004672static int
4673xfs_bmapi_remap(
4674 struct xfs_trans *tp,
4675 struct xfs_inode *ip,
4676 xfs_fileoff_t bno,
4677 xfs_filblks_t len,
4678 xfs_fsblock_t startblock,
4679 struct xfs_defer_ops *dfops)
4680{
4681 struct xfs_mount *mp = ip->i_mount;
4682 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4683 struct xfs_btree_cur *cur = NULL;
4684 xfs_fsblock_t firstblock = NULLFSBLOCK;
4685 struct xfs_bmbt_irec got;
4686 xfs_extnum_t idx;
4687 int logflags = 0, error;
4688
4689 ASSERT(len > 0);
4690 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4691 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4692
4693 if (unlikely(XFS_TEST_ERROR(
4694 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4695 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004696 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004697 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4698 return -EFSCORRUPTED;
4699 }
4700
4701 if (XFS_FORCED_SHUTDOWN(mp))
4702 return -EIO;
4703
4704 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4705 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4706 if (error)
4707 return error;
4708 }
4709
4710 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4711 /* make sure we only reflink into a hole. */
4712 ASSERT(got.br_startoff > bno);
4713 ASSERT(got.br_startoff - bno >= len);
4714 }
4715
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004716 ip->i_d.di_nblocks += len;
4717 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004718
4719 if (ifp->if_flags & XFS_IFBROOT) {
4720 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4721 cur->bc_private.b.firstblock = firstblock;
4722 cur->bc_private.b.dfops = dfops;
4723 cur->bc_private.b.flags = 0;
4724 }
4725
4726 got.br_startoff = bno;
4727 got.br_startblock = startblock;
4728 got.br_blockcount = len;
4729 got.br_state = XFS_EXT_NORM;
4730
4731 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4732 &got, &firstblock, dfops, &logflags);
4733 if (error)
4734 goto error0;
4735
4736 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4737 int tmp_logflags = 0;
4738
4739 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4740 &tmp_logflags, XFS_DATA_FORK);
4741 logflags |= tmp_logflags;
4742 }
4743
4744error0:
4745 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4746 logflags &= ~XFS_ILOG_DEXT;
4747 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4748 logflags &= ~XFS_ILOG_DBROOT;
4749
4750 if (logflags)
4751 xfs_trans_log_inode(tp, ip, logflags);
4752 if (cur) {
4753 xfs_btree_del_cursor(cur,
4754 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4755 }
4756 return error;
4757}
4758
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004760 * When a delalloc extent is split (e.g., due to a hole punch), the original
4761 * indlen reservation must be shared across the two new extents that are left
4762 * behind.
4763 *
4764 * Given the original reservation and the worst case indlen for the two new
4765 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004766 * reservation fairly across the two new extents. If necessary, steal available
4767 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4768 * ores == 1). The number of stolen blocks is returned. The availability and
4769 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004770 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004771static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004772xfs_bmap_split_indlen(
4773 xfs_filblks_t ores, /* original res. */
4774 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004775 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4776 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004777{
4778 xfs_filblks_t len1 = *indlen1;
4779 xfs_filblks_t len2 = *indlen2;
4780 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004781 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004782 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004783
4784 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004785 * Steal as many blocks as we can to try and satisfy the worst case
4786 * indlen for both new extents.
4787 */
Brian Foster75d65362017-02-13 22:48:30 -08004788 if (ores < nres && avail)
4789 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4790 ores += stolen;
4791
4792 /* nothing else to do if we've satisfied the new reservation */
4793 if (ores >= nres)
4794 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004795
4796 /*
Brian Foster75d65362017-02-13 22:48:30 -08004797 * We can't meet the total required reservation for the two extents.
4798 * Calculate the percent of the overall shortage between both extents
4799 * and apply this percentage to each of the requested indlen values.
4800 * This distributes the shortage fairly and reduces the chances that one
4801 * of the two extents is left with nothing when extents are repeatedly
4802 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004803 */
Brian Foster75d65362017-02-13 22:48:30 -08004804 resfactor = (ores * 100);
4805 do_div(resfactor, nres);
4806 len1 *= resfactor;
4807 do_div(len1, 100);
4808 len2 *= resfactor;
4809 do_div(len2, 100);
4810 ASSERT(len1 + len2 <= ores);
4811 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4812
4813 /*
4814 * Hand out the remainder to each extent. If one of the two reservations
4815 * is zero, we want to make sure that one gets a block first. The loop
4816 * below starts with len1, so hand len2 a block right off the bat if it
4817 * is zero.
4818 */
4819 ores -= (len1 + len2);
4820 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4821 if (ores && !len2 && *indlen2) {
4822 len2++;
4823 ores--;
4824 }
4825 while (ores) {
4826 if (len1 < *indlen1) {
4827 len1++;
4828 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004829 }
Brian Foster75d65362017-02-13 22:48:30 -08004830 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004831 break;
Brian Foster75d65362017-02-13 22:48:30 -08004832 if (len2 < *indlen2) {
4833 len2++;
4834 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004835 }
4836 }
4837
4838 *indlen1 = len1;
4839 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004840
4841 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004842}
4843
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004844int
4845xfs_bmap_del_extent_delay(
4846 struct xfs_inode *ip,
4847 int whichfork,
4848 xfs_extnum_t *idx,
4849 struct xfs_bmbt_irec *got,
4850 struct xfs_bmbt_irec *del)
4851{
4852 struct xfs_mount *mp = ip->i_mount;
4853 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4854 struct xfs_bmbt_irec new;
4855 int64_t da_old, da_new, da_diff = 0;
4856 xfs_fileoff_t del_endoff, got_endoff;
4857 xfs_filblks_t got_indlen, new_indlen, stolen;
4858 int error = 0, state = 0;
4859 bool isrt;
4860
4861 XFS_STATS_INC(mp, xs_del_exlist);
4862
4863 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4864 del_endoff = del->br_startoff + del->br_blockcount;
4865 got_endoff = got->br_startoff + got->br_blockcount;
4866 da_old = startblockval(got->br_startblock);
4867 da_new = 0;
4868
4869 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004870 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004871 ASSERT(del->br_blockcount > 0);
4872 ASSERT(got->br_startoff <= del->br_startoff);
4873 ASSERT(got_endoff >= del_endoff);
4874
4875 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004876 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004877
4878 do_div(rtexts, mp->m_sb.sb_rextsize);
4879 xfs_mod_frextents(mp, rtexts);
4880 }
4881
4882 /*
4883 * Update the inode delalloc counter now and wait to update the
4884 * sb counters as we might have to borrow some blocks for the
4885 * indirect block accounting.
4886 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004887 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4888 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004889 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004890 if (error)
4891 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004892 ip->i_delayed_blks -= del->br_blockcount;
4893
4894 if (whichfork == XFS_COW_FORK)
4895 state |= BMAP_COWFORK;
4896
4897 if (got->br_startoff == del->br_startoff)
4898 state |= BMAP_LEFT_CONTIG;
4899 if (got_endoff == del_endoff)
4900 state |= BMAP_RIGHT_CONTIG;
4901
4902 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
4903 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
4904 /*
4905 * Matches the whole extent. Delete the entry.
4906 */
4907 xfs_iext_remove(ip, *idx, 1, state);
4908 --*idx;
4909 break;
4910 case BMAP_LEFT_CONTIG:
4911 /*
4912 * Deleting the first part of the extent.
4913 */
4914 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4915 got->br_startoff = del_endoff;
4916 got->br_blockcount -= del->br_blockcount;
4917 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4918 got->br_blockcount), da_old);
4919 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004920 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004921 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4922 break;
4923 case BMAP_RIGHT_CONTIG:
4924 /*
4925 * Deleting the last part of the extent.
4926 */
4927 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4928 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4929 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4930 got->br_blockcount), da_old);
4931 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004932 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004933 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4934 break;
4935 case 0:
4936 /*
4937 * Deleting the middle of the extent.
4938 *
4939 * Distribute the original indlen reservation across the two new
4940 * extents. Steal blocks from the deleted extent if necessary.
4941 * Stealing blocks simply fudges the fdblocks accounting below.
4942 * Warn if either of the new indlen reservations is zero as this
4943 * can lead to delalloc problems.
4944 */
4945 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4946
4947 got->br_blockcount = del->br_startoff - got->br_startoff;
4948 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4949
4950 new.br_blockcount = got_endoff - del_endoff;
4951 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4952
4953 WARN_ON_ONCE(!got_indlen || !new_indlen);
4954 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4955 del->br_blockcount);
4956
4957 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004958 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004959 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4960
4961 new.br_startoff = del_endoff;
4962 new.br_state = got->br_state;
4963 new.br_startblock = nullstartblock((int)new_indlen);
4964
4965 ++*idx;
4966 xfs_iext_insert(ip, *idx, 1, &new, state);
4967
4968 da_new = got_indlen + new_indlen - stolen;
4969 del->br_blockcount -= stolen;
4970 break;
4971 }
4972
4973 ASSERT(da_old >= da_new);
4974 da_diff = da_old - da_new;
4975 if (!isrt)
4976 da_diff += del->br_blockcount;
4977 if (da_diff)
4978 xfs_mod_fdblocks(mp, da_diff, false);
4979 return error;
4980}
4981
4982void
4983xfs_bmap_del_extent_cow(
4984 struct xfs_inode *ip,
4985 xfs_extnum_t *idx,
4986 struct xfs_bmbt_irec *got,
4987 struct xfs_bmbt_irec *del)
4988{
4989 struct xfs_mount *mp = ip->i_mount;
4990 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4991 struct xfs_bmbt_irec new;
4992 xfs_fileoff_t del_endoff, got_endoff;
4993 int state = BMAP_COWFORK;
4994
4995 XFS_STATS_INC(mp, xs_del_exlist);
4996
4997 del_endoff = del->br_startoff + del->br_blockcount;
4998 got_endoff = got->br_startoff + got->br_blockcount;
4999
5000 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11005001 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005002 ASSERT(del->br_blockcount > 0);
5003 ASSERT(got->br_startoff <= del->br_startoff);
5004 ASSERT(got_endoff >= del_endoff);
5005 ASSERT(!isnullstartblock(got->br_startblock));
5006
5007 if (got->br_startoff == del->br_startoff)
5008 state |= BMAP_LEFT_CONTIG;
5009 if (got_endoff == del_endoff)
5010 state |= BMAP_RIGHT_CONTIG;
5011
5012 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
5013 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
5014 /*
5015 * Matches the whole extent. Delete the entry.
5016 */
5017 xfs_iext_remove(ip, *idx, 1, state);
5018 --*idx;
5019 break;
5020 case BMAP_LEFT_CONTIG:
5021 /*
5022 * Deleting the first part of the extent.
5023 */
5024 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5025 got->br_startoff = del_endoff;
5026 got->br_blockcount -= del->br_blockcount;
5027 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005028 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005029 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5030 break;
5031 case BMAP_RIGHT_CONTIG:
5032 /*
5033 * Deleting the last part of the extent.
5034 */
5035 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5036 got->br_blockcount -= del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005037 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005038 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5039 break;
5040 case 0:
5041 /*
5042 * Deleting the middle of the extent.
5043 */
5044 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5045 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005046 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005047 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5048
5049 new.br_startoff = del_endoff;
5050 new.br_blockcount = got_endoff - del_endoff;
5051 new.br_state = got->br_state;
5052 new.br_startblock = del->br_startblock + del->br_blockcount;
5053
5054 ++*idx;
5055 xfs_iext_insert(ip, *idx, 1, &new, state);
5056 break;
5057 }
5058}
5059
Brian Fostera9bd24a2016-03-15 11:42:46 +11005060/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005061 * Called by xfs_bmapi to update file extent records and the btree
5062 * after removing space (or undoing a delayed allocation).
5063 */
5064STATIC int /* error */
5065xfs_bmap_del_extent(
5066 xfs_inode_t *ip, /* incore inode pointer */
5067 xfs_trans_t *tp, /* current transaction pointer */
5068 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005069 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005070 xfs_btree_cur_t *cur, /* if null, not a btree */
5071 xfs_bmbt_irec_t *del, /* data to remove from extents */
5072 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005073 int whichfork, /* data or attr fork */
5074 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005075{
5076 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
5077 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
5078 xfs_fsblock_t del_endblock=0; /* first block past del */
5079 xfs_fileoff_t del_endoff; /* first offset past del */
5080 int delay; /* current block is delayed allocated */
5081 int do_fx; /* free extent at end of routine */
5082 xfs_bmbt_rec_host_t *ep; /* current extent entry pointer */
5083 int error; /* error return value */
5084 int flags; /* inode logging flags */
5085 xfs_bmbt_irec_t got; /* current extent entry */
5086 xfs_fileoff_t got_endoff; /* first offset past got */
5087 int i; /* temp state */
5088 xfs_ifork_t *ifp; /* inode fork pointer */
5089 xfs_mount_t *mp; /* mount structure */
5090 xfs_filblks_t nblks; /* quota/sb block count */
5091 xfs_bmbt_irec_t new; /* new record to be inserted */
5092 /* REFERENCED */
5093 uint qfield; /* quota field to update */
5094 xfs_filblks_t temp; /* for indirect length calculations */
5095 xfs_filblks_t temp2; /* for indirect length calculations */
5096 int state = 0;
5097
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005098 mp = ip->i_mount;
5099 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005100
5101 if (whichfork == XFS_ATTR_FORK)
5102 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005103 else if (whichfork == XFS_COW_FORK)
5104 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005105
Dave Chinner9e5987a2013-02-25 12:31:26 +11005106 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005107 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005108 ASSERT(del->br_blockcount > 0);
5109 ep = xfs_iext_get_ext(ifp, *idx);
5110 xfs_bmbt_get_all(ep, &got);
5111 ASSERT(got.br_startoff <= del->br_startoff);
5112 del_endoff = del->br_startoff + del->br_blockcount;
5113 got_endoff = got.br_startoff + got.br_blockcount;
5114 ASSERT(got_endoff >= del_endoff);
5115 delay = isnullstartblock(got.br_startblock);
5116 ASSERT(isnullstartblock(del->br_startblock) == delay);
5117 flags = 0;
5118 qfield = 0;
5119 error = 0;
5120 /*
5121 * If deleting a real allocation, must free up the disk space.
5122 */
5123 if (!delay) {
5124 flags = XFS_ILOG_CORE;
5125 /*
5126 * Realtime allocation. Free it and record di_nblocks update.
5127 */
5128 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5129 xfs_fsblock_t bno;
5130 xfs_filblks_t len;
5131
5132 ASSERT(do_mod(del->br_blockcount,
5133 mp->m_sb.sb_rextsize) == 0);
5134 ASSERT(do_mod(del->br_startblock,
5135 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;
5143 do_fx = 0;
5144 nblks = len * mp->m_sb.sb_rextsize;
5145 qfield = XFS_TRANS_DQ_RTBCOUNT;
5146 }
5147 /*
5148 * Ordinary allocation.
5149 */
5150 else {
5151 do_fx = 1;
5152 nblks = del->br_blockcount;
5153 qfield = XFS_TRANS_DQ_BCOUNT;
5154 }
5155 /*
5156 * Set up del_endblock and cur for later.
5157 */
5158 del_endblock = del->br_startblock + del->br_blockcount;
5159 if (cur) {
5160 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5161 got.br_startblock, got.br_blockcount,
5162 &i)))
5163 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005164 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005165 }
5166 da_old = da_new = 0;
5167 } else {
5168 da_old = startblockval(got.br_startblock);
5169 da_new = 0;
5170 nblks = 0;
5171 do_fx = 0;
5172 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005173
Dave Chinner9e5987a2013-02-25 12:31:26 +11005174 /*
5175 * Set flag value to use in switch statement.
5176 * Left-contig is 2, right-contig is 1.
5177 */
5178 switch (((got.br_startoff == del->br_startoff) << 1) |
5179 (got_endoff == del_endoff)) {
5180 case 3:
5181 /*
5182 * Matches the whole extent. Delete the entry.
5183 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005184 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005185 xfs_iext_remove(ip, *idx, 1,
5186 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5187 --*idx;
5188 if (delay)
5189 break;
5190
5191 XFS_IFORK_NEXT_SET(ip, whichfork,
5192 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5193 flags |= XFS_ILOG_CORE;
5194 if (!cur) {
5195 flags |= xfs_ilog_fext(whichfork);
5196 break;
5197 }
5198 if ((error = xfs_btree_delete(cur, &i)))
5199 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005200 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005201 break;
5202
5203 case 2:
5204 /*
5205 * Deleting the first part of the extent.
5206 */
5207 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5208 xfs_bmbt_set_startoff(ep, del_endoff);
5209 temp = got.br_blockcount - del->br_blockcount;
5210 xfs_bmbt_set_blockcount(ep, temp);
5211 if (delay) {
5212 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5213 da_old);
5214 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5215 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5216 da_new = temp;
5217 break;
5218 }
5219 xfs_bmbt_set_startblock(ep, del_endblock);
5220 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5221 if (!cur) {
5222 flags |= xfs_ilog_fext(whichfork);
5223 break;
5224 }
5225 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
5226 got.br_blockcount - del->br_blockcount,
5227 got.br_state)))
5228 goto done;
5229 break;
5230
5231 case 1:
5232 /*
5233 * Deleting the last part of the extent.
5234 */
5235 temp = got.br_blockcount - del->br_blockcount;
5236 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5237 xfs_bmbt_set_blockcount(ep, temp);
5238 if (delay) {
5239 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
5240 da_old);
5241 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
5242 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5243 da_new = temp;
5244 break;
5245 }
5246 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5247 if (!cur) {
5248 flags |= xfs_ilog_fext(whichfork);
5249 break;
5250 }
5251 if ((error = xfs_bmbt_update(cur, got.br_startoff,
5252 got.br_startblock,
5253 got.br_blockcount - del->br_blockcount,
5254 got.br_state)))
5255 goto done;
5256 break;
5257
5258 case 0:
5259 /*
5260 * Deleting the middle of the extent.
5261 */
5262 temp = del->br_startoff - got.br_startoff;
5263 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5264 xfs_bmbt_set_blockcount(ep, temp);
5265 new.br_startoff = del_endoff;
5266 temp2 = got_endoff - del_endoff;
5267 new.br_blockcount = temp2;
5268 new.br_state = got.br_state;
5269 if (!delay) {
5270 new.br_startblock = del_endblock;
5271 flags |= XFS_ILOG_CORE;
5272 if (cur) {
5273 if ((error = xfs_bmbt_update(cur,
5274 got.br_startoff,
5275 got.br_startblock, temp,
5276 got.br_state)))
5277 goto done;
5278 if ((error = xfs_btree_increment(cur, 0, &i)))
5279 goto done;
5280 cur->bc_rec.b = new;
5281 error = xfs_btree_insert(cur, &i);
Dave Chinner24513372014-06-25 14:58:08 +10005282 if (error && error != -ENOSPC)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005283 goto done;
5284 /*
5285 * If get no-space back from btree insert,
5286 * it tried a split, and we have a zero
5287 * block reservation.
5288 * Fix up our state and return the error.
5289 */
Dave Chinner24513372014-06-25 14:58:08 +10005290 if (error == -ENOSPC) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11005291 /*
5292 * Reset the cursor, don't trust
5293 * it after any insert operation.
5294 */
5295 if ((error = xfs_bmbt_lookup_eq(cur,
5296 got.br_startoff,
5297 got.br_startblock,
5298 temp, &i)))
5299 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005300 XFS_WANT_CORRUPTED_GOTO(mp,
5301 i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005302 /*
5303 * Update the btree record back
5304 * to the original value.
5305 */
5306 if ((error = xfs_bmbt_update(cur,
5307 got.br_startoff,
5308 got.br_startblock,
5309 got.br_blockcount,
5310 got.br_state)))
5311 goto done;
5312 /*
5313 * Reset the extent record back
5314 * to the original value.
5315 */
5316 xfs_bmbt_set_blockcount(ep,
5317 got.br_blockcount);
5318 flags = 0;
Dave Chinner24513372014-06-25 14:58:08 +10005319 error = -ENOSPC;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005320 goto done;
5321 }
Eric Sandeenc29aad42015-02-23 22:39:08 +11005322 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005323 } else
5324 flags |= xfs_ilog_fext(whichfork);
5325 XFS_IFORK_NEXT_SET(ip, whichfork,
5326 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5327 } else {
Brian Fosterd34999c92016-03-15 11:42:47 +11005328 xfs_filblks_t stolen;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005329 ASSERT(whichfork == XFS_DATA_FORK);
Brian Fostera9bd24a2016-03-15 11:42:46 +11005330
5331 /*
5332 * Distribute the original indlen reservation across the
Brian Fosterd34999c92016-03-15 11:42:47 +11005333 * two new extents. Steal blocks from the deleted extent
5334 * if necessary. Stealing blocks simply fudges the
5335 * fdblocks accounting in xfs_bunmapi().
Brian Fostera9bd24a2016-03-15 11:42:46 +11005336 */
5337 temp = xfs_bmap_worst_indlen(ip, got.br_blockcount);
5338 temp2 = xfs_bmap_worst_indlen(ip, new.br_blockcount);
Brian Fosterd34999c92016-03-15 11:42:47 +11005339 stolen = xfs_bmap_split_indlen(da_old, &temp, &temp2,
5340 del->br_blockcount);
5341 da_new = temp + temp2 - stolen;
5342 del->br_blockcount -= stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11005343
5344 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11005345 * Set the reservation for each extent. Warn if either
5346 * is zero as this can lead to delalloc problems.
Brian Fostera9bd24a2016-03-15 11:42:46 +11005347 */
Brian Fosterd34999c92016-03-15 11:42:47 +11005348 WARN_ON_ONCE(!temp || !temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005349 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005350 new.br_startblock = nullstartblock((int)temp2);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005351 }
5352 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5353 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5354 ++*idx;
5355 break;
5356 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005357
5358 /* remove reverse mapping */
5359 if (!delay) {
5360 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5361 if (error)
5362 goto done;
5363 }
5364
Dave Chinner9e5987a2013-02-25 12:31:26 +11005365 /*
5366 * If we need to, add to list of extents to delete.
5367 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005368 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005369 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5370 error = xfs_refcount_decrease_extent(mp, dfops, del);
5371 if (error)
5372 goto done;
5373 } else
5374 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5375 del->br_blockcount, NULL);
5376 }
5377
Dave Chinner9e5987a2013-02-25 12:31:26 +11005378 /*
5379 * Adjust inode # blocks in the file.
5380 */
5381 if (nblks)
5382 ip->i_d.di_nblocks -= nblks;
5383 /*
5384 * Adjust quota data.
5385 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005386 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005387 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5388
5389 /*
5390 * Account for change in delayed indirect blocks.
5391 * Nothing to do for disk quota accounting here.
5392 */
5393 ASSERT(da_old >= da_new);
Dave Chinner0d485ad2015-02-23 21:22:03 +11005394 if (da_old > da_new)
5395 xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new), false);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005396done:
5397 *logflagsp = flags;
5398 return error;
5399}
5400
5401/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 * Unmap (remove) blocks from a file.
5403 * If nexts is nonzero then the number of extents to remove is limited to
5404 * that value. If not all extents in the block range can be removed then
5405 * *done is set.
5406 */
5407int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005408__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 xfs_trans_t *tp, /* transaction pointer */
5410 struct xfs_inode *ip, /* incore inode */
5411 xfs_fileoff_t bno, /* starting offset to unmap */
Darrick J. Wong44535932016-10-03 09:11:29 -07005412 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 int flags, /* misc flags */
5414 xfs_extnum_t nexts, /* number of extents max */
5415 xfs_fsblock_t *firstblock, /* first allocated block
5416 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005417 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005418{
5419 xfs_btree_cur_t *cur; /* bmap btree cursor */
5420 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005421 int error; /* error return value */
5422 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005423 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 xfs_ifork_t *ifp; /* inode fork pointer */
5425 int isrt; /* freeing in rt area */
5426 xfs_extnum_t lastx; /* last extent index used */
5427 int logflags; /* transaction logging flags */
5428 xfs_extlen_t mod; /* rt extent offset */
5429 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 xfs_fileoff_t start; /* first file offset deleted */
5431 int tmp_logflags; /* partial logging flags */
5432 int wasdel; /* was a delayed alloc extent */
5433 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005435 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005436 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005437 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005439 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5440
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005441 whichfork = xfs_bmapi_whichfork(flags);
5442 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 ifp = XFS_IFORK_PTR(ip, whichfork);
5444 if (unlikely(
5445 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5446 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5447 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5448 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005449 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005450 }
5451 mp = ip->i_mount;
5452 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005453 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005454
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005455 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 ASSERT(len > 0);
5457 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005458
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005459 /*
5460 * Guesstimate how many blocks we can unmap without running the risk of
5461 * blowing out the transaction with a mix of EFIs and reflink
5462 * adjustments.
5463 */
5464 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5465 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5466 else
5467 max_len = len;
5468
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5470 (error = xfs_iread_extents(tp, ip, whichfork)))
5471 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005472 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005473 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005474 return 0;
5475 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005476 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005477 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 start = bno;
5479 bno = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005480
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 /*
5482 * Check to see if the given block number is past the end of the
5483 * file, back up to the last block if so...
5484 */
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005485 if (!xfs_iext_lookup_extent(ip, ifp, bno, &lastx, &got)) {
5486 ASSERT(lastx > 0);
5487 xfs_iext_get_extent(ifp, --lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005488 bno = got.br_startoff + got.br_blockcount - 1;
5489 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005490
Linus Torvalds1da177e2005-04-16 15:20:36 -07005491 logflags = 0;
5492 if (ifp->if_flags & XFS_IFBROOT) {
5493 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005494 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005495 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005496 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005497 cur->bc_private.b.flags = 0;
5498 } else
5499 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005500
5501 if (isrt) {
5502 /*
5503 * Synchronize by locking the bitmap inode.
5504 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005505 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005506 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005507 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5508 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005509 }
5510
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 extno = 0;
5512 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005513 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005514 /*
5515 * Is the found extent after a hole in which bno lives?
5516 * Just back up to the previous extent, if so.
5517 */
5518 if (got.br_startoff > bno) {
5519 if (--lastx < 0)
5520 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005521 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 }
5523 /*
5524 * Is the last block of this extent before the range
5525 * we're supposed to delete? If so, we're done.
5526 */
5527 bno = XFS_FILEOFF_MIN(bno,
5528 got.br_startoff + got.br_blockcount - 1);
5529 if (bno < start)
5530 break;
5531 /*
5532 * Then deal with the (possibly delayed) allocated space
5533 * we found.
5534 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005535 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005536 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005537
5538 /*
5539 * Make sure we don't touch multiple AGF headers out of order
5540 * in a single transaction, as that could cause AB-BA deadlocks.
5541 */
5542 if (!wasdel) {
5543 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5544 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5545 break;
5546 prev_agno = agno;
5547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 if (got.br_startoff < start) {
5549 del.br_startoff = start;
5550 del.br_blockcount -= start - got.br_startoff;
5551 if (!wasdel)
5552 del.br_startblock += start - got.br_startoff;
5553 }
5554 if (del.br_startoff + del.br_blockcount > bno + 1)
5555 del.br_blockcount = bno + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005556
5557 /* How much can we safely unmap? */
5558 if (max_len < del.br_blockcount) {
5559 del.br_startoff += del.br_blockcount - max_len;
5560 if (!wasdel)
5561 del.br_startblock += del.br_blockcount - max_len;
5562 del.br_blockcount = max_len;
5563 }
5564
Linus Torvalds1da177e2005-04-16 15:20:36 -07005565 sum = del.br_startblock + del.br_blockcount;
5566 if (isrt &&
5567 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5568 /*
5569 * Realtime extent not lined up at the end.
5570 * The extent could have been split into written
5571 * and unwritten pieces, or we could just be
5572 * unmapping part of it. But we can't really
5573 * get rid of part of a realtime extent.
5574 */
5575 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005576 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 /*
5578 * This piece is unwritten, or we're not
5579 * using unwritten extents. Skip over it.
5580 */
5581 ASSERT(bno >= mod);
5582 bno -= mod > del.br_blockcount ?
5583 del.br_blockcount : mod;
5584 if (bno < got.br_startoff) {
5585 if (--lastx >= 0)
Christoph Hellwige3f0f752017-10-17 14:16:20 -07005586 xfs_iext_get_extent(ifp, lastx,
5587 &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 }
5589 continue;
5590 }
5591 /*
5592 * It's written, turn it unwritten.
5593 * This is better than zeroing it.
5594 */
5595 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005596 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597 /*
5598 * If this spans a realtime extent boundary,
5599 * chop it back to the start of the one we end at.
5600 */
5601 if (del.br_blockcount > mod) {
5602 del.br_startoff += del.br_blockcount - mod;
5603 del.br_startblock += del.br_blockcount - mod;
5604 del.br_blockcount = mod;
5605 }
5606 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005607 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005608 whichfork, &lastx, &cur, &del,
5609 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 if (error)
5611 goto error0;
5612 goto nodelete;
5613 }
5614 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5615 /*
5616 * Realtime extent is lined up at the end but not
5617 * at the front. We'll get rid of full extents if
5618 * we can.
5619 */
5620 mod = mp->m_sb.sb_rextsize - mod;
5621 if (del.br_blockcount > mod) {
5622 del.br_blockcount -= mod;
5623 del.br_startoff += mod;
5624 del.br_startblock += mod;
5625 } else if ((del.br_startoff == start &&
5626 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005627 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005628 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005629 /*
5630 * Can't make it unwritten. There isn't
5631 * a full extent here so just skip it.
5632 */
5633 ASSERT(bno >= del.br_blockcount);
5634 bno -= del.br_blockcount;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005635 if (got.br_startoff > bno && --lastx >= 0)
5636 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 continue;
5638 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005639 struct xfs_bmbt_irec prev;
5640
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 /*
5642 * This one is already unwritten.
5643 * It must have a written left neighbor.
5644 * Unwrite the killed part of that one and
5645 * try again.
5646 */
5647 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005648 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005650 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005651 ASSERT(del.br_startblock ==
5652 prev.br_startblock + prev.br_blockcount);
5653 if (prev.br_startoff < start) {
5654 mod = start - prev.br_startoff;
5655 prev.br_blockcount -= mod;
5656 prev.br_startblock += mod;
5657 prev.br_startoff = start;
5658 }
5659 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005660 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005661 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005662 ip, whichfork, &lastx, &cur,
5663 &prev, firstblock, dfops,
5664 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 if (error)
5666 goto error0;
5667 goto nodelete;
5668 } else {
5669 ASSERT(del.br_state == XFS_EXT_NORM);
5670 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005671 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005672 ip, whichfork, &lastx, &cur,
5673 &del, firstblock, dfops,
5674 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 if (error)
5676 goto error0;
5677 goto nodelete;
5678 }
5679 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005680
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 /*
5682 * If it's the case where the directory code is running
5683 * with no block reservation, and the deleted block is in
5684 * the middle of its extent, and the resulting insert
5685 * of an extent would cause transformation to btree format,
5686 * then reject it. The calling code will then swap
5687 * blocks around instead.
5688 * We have to do this now, rather than waiting for the
5689 * conversion to btree format, since the transaction
5690 * will be dirty.
5691 */
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005692 if (!wasdel && tp->t_blk_res == 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005694 XFS_IFORK_NEXTENTS(ip, whichfork) >= /* Note the >= */
5695 XFS_IFORK_MAXEXT(ip, whichfork) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 del.br_startoff > got.br_startoff &&
5697 del.br_startoff + del.br_blockcount <
5698 got.br_startoff + got.br_blockcount) {
Dave Chinner24513372014-06-25 14:58:08 +10005699 error = -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005700 goto error0;
5701 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005702
5703 /*
5704 * Unreserve quota and update realtime free space, if
5705 * appropriate. If delayed allocation, update the inode delalloc
5706 * counter now and wait to update the sb counters as
5707 * xfs_bmap_del_extent() might need to borrow some blocks.
5708 */
5709 if (wasdel) {
5710 ASSERT(startblockval(del.br_startblock) > 0);
5711 if (isrt) {
5712 xfs_filblks_t rtexts;
5713
5714 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5715 do_div(rtexts, mp->m_sb.sb_rextsize);
5716 xfs_mod_frextents(mp, (int64_t)rtexts);
5717 (void)xfs_trans_reserve_quota_nblks(NULL,
5718 ip, -((long)del.br_blockcount), 0,
5719 XFS_QMOPT_RES_RTBLKS);
5720 } else {
5721 (void)xfs_trans_reserve_quota_nblks(NULL,
5722 ip, -((long)del.br_blockcount), 0,
5723 XFS_QMOPT_RES_REGBLKS);
5724 }
5725 ip->i_delayed_blks -= del.br_blockcount;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005726 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005727
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005728 error = xfs_bmap_del_extent(ip, tp, &lastx, dfops, cur, &del,
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005729 &tmp_logflags, whichfork, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 logflags |= tmp_logflags;
5731 if (error)
5732 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005733
5734 if (!isrt && wasdel)
5735 xfs_mod_fdblocks(mp, (int64_t)del.br_blockcount, false);
5736
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005737 max_len -= del.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 bno = del.br_startoff - 1;
5739nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005740 /*
5741 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005744 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005745 xfs_iext_get_extent(ifp, lastx, &got);
5746 if (got.br_startoff > bno && --lastx >= 0)
5747 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005749 extno++;
5750 }
5751 }
Darrick J. Wong44535932016-10-03 09:11:29 -07005752 if (bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0)
5753 *rlen = 0;
5754 else
5755 *rlen = bno - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005756
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 /*
5758 * Convert to a btree if necessary.
5759 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005760 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005762 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 &cur, 0, &tmp_logflags, whichfork);
5764 logflags |= tmp_logflags;
5765 if (error)
5766 goto error0;
5767 }
5768 /*
5769 * transform from btree to extents, give it cur
5770 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005771 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 ASSERT(cur != NULL);
5773 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5774 whichfork);
5775 logflags |= tmp_logflags;
5776 if (error)
5777 goto error0;
5778 }
5779 /*
5780 * transform from extents to local?
5781 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 error = 0;
5783error0:
5784 /*
5785 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005786 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005788 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005789 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005790 logflags &= ~xfs_ilog_fext(whichfork);
5791 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005793 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 /*
5795 * Log inode even in the error case, if the transaction
5796 * is dirty we'll need to shut down the filesystem.
5797 */
5798 if (logflags)
5799 xfs_trans_log_inode(tp, ip, logflags);
5800 if (cur) {
5801 if (!error) {
5802 *firstblock = cur->bc_private.b.firstblock;
5803 cur->bc_private.b.allocated = 0;
5804 }
5805 xfs_btree_del_cursor(cur,
5806 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5807 }
5808 return error;
5809}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005810
Darrick J. Wong44535932016-10-03 09:11:29 -07005811/* Unmap a range of a file. */
5812int
5813xfs_bunmapi(
5814 xfs_trans_t *tp,
5815 struct xfs_inode *ip,
5816 xfs_fileoff_t bno,
5817 xfs_filblks_t len,
5818 int flags,
5819 xfs_extnum_t nexts,
5820 xfs_fsblock_t *firstblock,
5821 struct xfs_defer_ops *dfops,
5822 int *done)
5823{
5824 int error;
5825
5826 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5827 dfops);
5828 *done = (len == 0);
5829 return error;
5830}
5831
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005832/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005833 * Determine whether an extent shift can be accomplished by a merge with the
5834 * extent that precedes the target hole of the shift.
5835 */
5836STATIC bool
5837xfs_bmse_can_merge(
5838 struct xfs_bmbt_irec *left, /* preceding extent */
5839 struct xfs_bmbt_irec *got, /* current extent to shift */
5840 xfs_fileoff_t shift) /* shift fsb */
5841{
5842 xfs_fileoff_t startoff;
5843
5844 startoff = got->br_startoff - shift;
5845
5846 /*
5847 * The extent, once shifted, must be adjacent in-file and on-disk with
5848 * the preceding extent.
5849 */
5850 if ((left->br_startoff + left->br_blockcount != startoff) ||
5851 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5852 (left->br_state != got->br_state) ||
5853 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5854 return false;
5855
5856 return true;
5857}
5858
5859/*
5860 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5861 * hole in the file. If an extent shift would result in the extent being fully
5862 * adjacent to the extent that currently precedes the hole, we can merge with
5863 * the preceding extent rather than do the shift.
5864 *
5865 * This function assumes the caller has verified a shift-by-merge is possible
5866 * with the provided extents via xfs_bmse_can_merge().
5867 */
5868STATIC int
5869xfs_bmse_merge(
5870 struct xfs_inode *ip,
5871 int whichfork,
5872 xfs_fileoff_t shift, /* shift fsb */
5873 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005874 struct xfs_bmbt_irec *got, /* extent to shift */
5875 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005876 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005877 int *logflags, /* output */
5878 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005879{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005880 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5881 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005882 xfs_filblks_t blockcount;
5883 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005884 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005885
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005886 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005887
5888 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5889 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005890 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005891
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005892 new = *left;
5893 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005894
5895 /*
5896 * Update the on-disk extent count, the btree if necessary and log the
5897 * inode.
5898 */
5899 XFS_IFORK_NEXT_SET(ip, whichfork,
5900 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5901 *logflags |= XFS_ILOG_CORE;
5902 if (!cur) {
5903 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005904 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005905 }
5906
5907 /* lookup and remove the extent to merge */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005908 error = xfs_bmbt_lookup_eq(cur, got->br_startoff, got->br_startblock,
5909 got->br_blockcount, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005910 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005911 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005912 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005913
5914 error = xfs_btree_delete(cur, &i);
5915 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005916 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005917 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005918
5919 /* lookup and update size of the previous extent */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005920 error = xfs_bmbt_lookup_eq(cur, left->br_startoff, left->br_startblock,
5921 left->br_blockcount, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005922 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005923 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005924 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005925
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005926 error = xfs_bmbt_update(cur, new.br_startoff, new.br_startblock,
5927 new.br_blockcount, new.br_state);
5928 if (error)
5929 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005930
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005931done:
5932 xfs_iext_update_extent(ifp, current_ext - 1, &new);
5933 xfs_iext_remove(ip, current_ext, 1, 0);
5934
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005935 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005936 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5937 if (error)
5938 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005939 memcpy(&new, got, sizeof(new));
5940 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005941 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005942}
5943
5944/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005945 * Shift a single extent.
5946 */
5947STATIC int
5948xfs_bmse_shift_one(
5949 struct xfs_inode *ip,
5950 int whichfork,
5951 xfs_fileoff_t offset_shift_fsb,
5952 int *current_ext,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005953 struct xfs_bmbt_irec *got,
Brian Fostera979bdf2014-09-23 15:39:04 +10005954 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005955 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005956 enum shift_direction direction,
5957 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005958{
5959 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005960 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005961 xfs_fileoff_t startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005962 struct xfs_bmbt_irec adj_irec, new;
Brian Fostera979bdf2014-09-23 15:39:04 +10005963 int error;
5964 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005965 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005966
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005967 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005968 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005969 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005970
Brian Fosterf71721d2014-09-23 15:39:05 +10005971 /* delalloc extents should be prevented by caller */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005972 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got->br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005973
Namjae Jeona904b1c2015-03-25 15:08:56 +11005974 if (direction == SHIFT_LEFT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005975 startoff = got->br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005976
Namjae Jeona904b1c2015-03-25 15:08:56 +11005977 /*
5978 * Check for merge if we've got an extent to the left,
5979 * otherwise make sure there's enough room at the start
5980 * of the file for the shift.
5981 */
5982 if (!*current_ext) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005983 if (got->br_startoff < offset_shift_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005984 return -EINVAL;
5985 goto update_current_ext;
5986 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005987
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005988 /*
5989 * grab the left extent and check for a large enough hole.
5990 */
5991 xfs_iext_get_extent(ifp, *current_ext - 1, &adj_irec);
5992 if (startoff < adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005993 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005994
Dave Chinnerb11bd672014-12-04 09:42:24 +11005995 /* check whether to merge the extent or shift it down */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005996 if (xfs_bmse_can_merge(&adj_irec, got, offset_shift_fsb)) {
5997 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5998 *current_ext, got, &adj_irec,
5999 cur, logflags, dfops);
Dave Chinnerb11bd672014-12-04 09:42:24 +11006000 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006001 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006002 startoff = got->br_startoff + offset_shift_fsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006003 /* nothing to move if this is the last extent */
6004 if (*current_ext >= (total_extents - 1))
6005 goto update_current_ext;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006006
Namjae Jeona904b1c2015-03-25 15:08:56 +11006007 /*
6008 * If this is not the last extent in the file, make sure there
6009 * is enough room between current extent and next extent for
6010 * accommodating the shift.
6011 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006012 xfs_iext_get_extent(ifp, *current_ext + 1, &adj_irec);
6013 if (startoff + got->br_blockcount > adj_irec.br_startoff)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006014 return -EINVAL;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006015
Namjae Jeona904b1c2015-03-25 15:08:56 +11006016 /*
6017 * Unlike a left shift (which involves a hole punch),
6018 * a right shift does not modify extent neighbors
6019 * in any way. We should never find mergeable extents
6020 * in this scenario. Check anyways and warn if we
6021 * encounter two extents that could be one.
6022 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006023 if (xfs_bmse_can_merge(got, &adj_irec, offset_shift_fsb))
Namjae Jeona904b1c2015-03-25 15:08:56 +11006024 WARN_ON_ONCE(1);
6025 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006026
Brian Fostera979bdf2014-09-23 15:39:04 +10006027 /*
6028 * Increment the extent index for the next iteration, update the start
6029 * offset of the in-core extent and update the btree if applicable.
6030 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11006031update_current_ext:
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006032 *logflags |= XFS_ILOG_CORE;
6033
6034 new = *got;
6035 new.br_startoff = startoff;
6036
6037 if (cur) {
6038 error = xfs_bmbt_lookup_eq(cur, got->br_startoff,
6039 got->br_startblock, got->br_blockcount, &i);
6040 if (error)
6041 return error;
6042 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
6043
6044 error = xfs_bmbt_update(cur, new.br_startoff,
6045 new.br_startblock, new.br_blockcount,
6046 new.br_state);
6047 if (error)
6048 return error;
6049 } else {
6050 *logflags |= XFS_ILOG_DEXT;
6051 }
6052
6053 xfs_iext_update_extent(ifp, *current_ext, &new);
6054
Namjae Jeona904b1c2015-03-25 15:08:56 +11006055 if (direction == SHIFT_LEFT)
6056 (*current_ext)++;
6057 else
6058 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10006059
Darrick J. Wong9c194642016-08-03 12:16:05 +10006060 /* update reverse mapping */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006061 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
Darrick J. Wong9c194642016-08-03 12:16:05 +10006062 if (error)
6063 return error;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006064 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fostera979bdf2014-09-23 15:39:04 +10006065}
6066
6067/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11006068 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006069 *
Brian Foster2c845f52014-09-23 15:37:09 +10006070 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11006071 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10006072 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
6073 * is the length by which each extent is shifted. If there is no hole to shift
6074 * the extents into, this will be considered invalid operation and we abort
6075 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006076 */
6077int
6078xfs_bmap_shift_extents(
6079 struct xfs_trans *tp,
6080 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006081 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006082 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10006083 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006084 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006085 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006086 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006087 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006088 int num_exts)
6089{
Brian Fosterca446d82014-09-02 12:12:53 +10006090 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006091 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006092 struct xfs_mount *mp = ip->i_mount;
6093 struct xfs_ifork *ifp;
6094 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10006095 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006096 xfs_extnum_t total_extents;
6097 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006098 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006099 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10006100 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006101
6102 if (unlikely(XFS_TEST_ERROR(
6103 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6104 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006105 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006106 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
6107 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10006108 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006109 }
6110
6111 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10006112 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006113
Brian Foster2c845f52014-09-23 15:37:09 +10006114 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
6115 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11006116 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006117
6118 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006119 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6120 /* Read in all the extents */
6121 error = xfs_iread_extents(tp, ip, whichfork);
6122 if (error)
6123 return error;
6124 }
6125
Brian Fosterddb19e32014-09-23 15:38:09 +10006126 if (ifp->if_flags & XFS_IFBROOT) {
6127 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6128 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006129 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006130 cur->bc_private.b.flags = 0;
6131 }
6132
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006133 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006134 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006135 * are collapsing out, so we cannot use the count of real extents here.
6136 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006137 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006138 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006139 if (total_extents == 0) {
6140 *done = 1;
6141 goto del_cursor;
6142 }
6143
6144 /*
6145 * In case of first right shift, we need to initialize next_fsb
6146 */
6147 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006148 ASSERT(direction == SHIFT_RIGHT);
6149
6150 current_ext = total_extents - 1;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006151 xfs_iext_get_extent(ifp, current_ext, &got);
6152 if (stop_fsb > got.br_startoff) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006153 *done = 1;
6154 goto del_cursor;
6155 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006156 *next_fsb = got.br_startoff;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006157 } else {
6158 /*
6159 * Look up the extent index for the fsb where we start shifting. We can
6160 * henceforth iterate with current_ext as extent list changes are locked
6161 * out via ilock.
6162 *
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006163 * If next_fsb lies in a hole beyond which there are no extents we are
6164 * done.
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006165 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006166 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
6167 &got)) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006168 *done = 1;
6169 goto del_cursor;
6170 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006171 }
6172
6173 /* Lookup the extent index at which we have to stop */
6174 if (direction == SHIFT_RIGHT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006175 struct xfs_bmbt_irec s;
6176
6177 xfs_iext_lookup_extent(ip, ifp, stop_fsb, &stop_extent, &s);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006178 /* Make stop_extent exclusive of shift range */
6179 stop_extent--;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006180 if (current_ext <= stop_extent) {
6181 error = -EIO;
6182 goto del_cursor;
6183 }
6184 } else {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006185 stop_extent = total_extents;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006186 if (current_ext >= stop_extent) {
6187 error = -EIO;
6188 goto del_cursor;
6189 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006190 }
6191
6192 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006193 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006194 &current_ext, &got, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006195 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006196 if (error)
6197 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006198 /*
6199 * If there was an extent merge during the shift, the extent
6200 * count can change. Update the total and grade the next record.
6201 */
6202 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006203 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006204 stop_extent = total_extents;
6205 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006206
Namjae Jeona904b1c2015-03-25 15:08:56 +11006207 if (current_ext == stop_extent) {
6208 *done = 1;
6209 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006210 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006211 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006212 xfs_iext_get_extent(ifp, current_ext, &got);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006213 }
6214
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006215 if (!*done)
Brian Foster2c845f52014-09-23 15:37:09 +10006216 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006217
6218del_cursor:
6219 if (cur)
6220 xfs_btree_del_cursor(cur,
6221 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6222
Brian Fosterca446d82014-09-02 12:12:53 +10006223 if (logflags)
6224 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006225
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006226 return error;
6227}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006228
6229/*
6230 * Splits an extent into two extents at split_fsb block such that it is
6231 * the first block of the current_ext. @current_ext is a target extent
6232 * to be split. @split_fsb is a block where the extents is split.
6233 * If split_fsb lies in a hole or the first block of extents, just return 0.
6234 */
6235STATIC int
6236xfs_bmap_split_extent_at(
6237 struct xfs_trans *tp,
6238 struct xfs_inode *ip,
6239 xfs_fileoff_t split_fsb,
6240 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006241 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006242{
6243 int whichfork = XFS_DATA_FORK;
6244 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006245 struct xfs_bmbt_irec got;
6246 struct xfs_bmbt_irec new; /* split extent */
6247 struct xfs_mount *mp = ip->i_mount;
6248 struct xfs_ifork *ifp;
6249 xfs_fsblock_t gotblkcnt; /* new block count for got */
6250 xfs_extnum_t current_ext;
6251 int error = 0;
6252 int logflags = 0;
6253 int i = 0;
6254
6255 if (unlikely(XFS_TEST_ERROR(
6256 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6257 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006258 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006259 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6260 XFS_ERRLEVEL_LOW, mp);
6261 return -EFSCORRUPTED;
6262 }
6263
6264 if (XFS_FORCED_SHUTDOWN(mp))
6265 return -EIO;
6266
6267 ifp = XFS_IFORK_PTR(ip, whichfork);
6268 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6269 /* Read in all the extents */
6270 error = xfs_iread_extents(tp, ip, whichfork);
6271 if (error)
6272 return error;
6273 }
6274
6275 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006276 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006277 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006278 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
6279 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006280 return 0;
6281
6282 gotblkcnt = split_fsb - got.br_startoff;
6283 new.br_startoff = split_fsb;
6284 new.br_startblock = got.br_startblock + gotblkcnt;
6285 new.br_blockcount = got.br_blockcount - gotblkcnt;
6286 new.br_state = got.br_state;
6287
6288 if (ifp->if_flags & XFS_IFBROOT) {
6289 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6290 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006291 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006292 cur->bc_private.b.flags = 0;
6293 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6294 got.br_startblock,
6295 got.br_blockcount,
6296 &i);
6297 if (error)
6298 goto del_cursor;
6299 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6300 }
6301
Namjae Jeona904b1c2015-03-25 15:08:56 +11006302 got.br_blockcount = gotblkcnt;
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006303 xfs_iext_update_extent(ifp, current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006304
6305 logflags = XFS_ILOG_CORE;
6306 if (cur) {
6307 error = xfs_bmbt_update(cur, got.br_startoff,
6308 got.br_startblock,
6309 got.br_blockcount,
6310 got.br_state);
6311 if (error)
6312 goto del_cursor;
6313 } else
6314 logflags |= XFS_ILOG_DEXT;
6315
6316 /* Add new extent */
6317 current_ext++;
6318 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6319 XFS_IFORK_NEXT_SET(ip, whichfork,
6320 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6321
6322 if (cur) {
6323 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6324 new.br_startblock, new.br_blockcount,
6325 &i);
6326 if (error)
6327 goto del_cursor;
6328 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6329 cur->bc_rec.b.br_state = new.br_state;
6330
6331 error = xfs_btree_insert(cur, &i);
6332 if (error)
6333 goto del_cursor;
6334 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6335 }
6336
6337 /*
6338 * Convert to a btree if necessary.
6339 */
6340 if (xfs_bmap_needs_btree(ip, whichfork)) {
6341 int tmp_logflags; /* partial log flag return val */
6342
6343 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006344 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006345 &cur, 0, &tmp_logflags, whichfork);
6346 logflags |= tmp_logflags;
6347 }
6348
6349del_cursor:
6350 if (cur) {
6351 cur->bc_private.b.allocated = 0;
6352 xfs_btree_del_cursor(cur,
6353 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6354 }
6355
6356 if (logflags)
6357 xfs_trans_log_inode(tp, ip, logflags);
6358 return error;
6359}
6360
6361int
6362xfs_bmap_split_extent(
6363 struct xfs_inode *ip,
6364 xfs_fileoff_t split_fsb)
6365{
6366 struct xfs_mount *mp = ip->i_mount;
6367 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006368 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006369 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006370 int error;
6371
Christoph Hellwig253f4912016-04-06 09:19:55 +10006372 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6373 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6374 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006375 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006376
6377 xfs_ilock(ip, XFS_ILOCK_EXCL);
6378 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6379
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006380 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006381
6382 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006383 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006384 if (error)
6385 goto out;
6386
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006387 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006388 if (error)
6389 goto out;
6390
Christoph Hellwig70393312015-06-04 13:48:08 +10006391 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006392
6393out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006394 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006395 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006396 return error;
6397}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006398
6399/* Deferred mapping is only for real extents in the data fork. */
6400static bool
6401xfs_bmap_is_update_needed(
6402 struct xfs_bmbt_irec *bmap)
6403{
6404 return bmap->br_startblock != HOLESTARTBLOCK &&
6405 bmap->br_startblock != DELAYSTARTBLOCK;
6406}
6407
6408/* Record a bmap intent. */
6409static int
6410__xfs_bmap_add(
6411 struct xfs_mount *mp,
6412 struct xfs_defer_ops *dfops,
6413 enum xfs_bmap_intent_type type,
6414 struct xfs_inode *ip,
6415 int whichfork,
6416 struct xfs_bmbt_irec *bmap)
6417{
6418 int error;
6419 struct xfs_bmap_intent *bi;
6420
6421 trace_xfs_bmap_defer(mp,
6422 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6423 type,
6424 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6425 ip->i_ino, whichfork,
6426 bmap->br_startoff,
6427 bmap->br_blockcount,
6428 bmap->br_state);
6429
6430 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6431 INIT_LIST_HEAD(&bi->bi_list);
6432 bi->bi_type = type;
6433 bi->bi_owner = ip;
6434 bi->bi_whichfork = whichfork;
6435 bi->bi_bmap = *bmap;
6436
Christoph Hellwig882d8782017-08-28 10:21:03 -07006437 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006438 if (error) {
6439 kmem_free(bi);
6440 return error;
6441 }
6442
6443 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6444 return 0;
6445}
6446
6447/* Map an extent into a file. */
6448int
6449xfs_bmap_map_extent(
6450 struct xfs_mount *mp,
6451 struct xfs_defer_ops *dfops,
6452 struct xfs_inode *ip,
6453 struct xfs_bmbt_irec *PREV)
6454{
6455 if (!xfs_bmap_is_update_needed(PREV))
6456 return 0;
6457
6458 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6459 XFS_DATA_FORK, PREV);
6460}
6461
6462/* Unmap an extent out of a file. */
6463int
6464xfs_bmap_unmap_extent(
6465 struct xfs_mount *mp,
6466 struct xfs_defer_ops *dfops,
6467 struct xfs_inode *ip,
6468 struct xfs_bmbt_irec *PREV)
6469{
6470 if (!xfs_bmap_is_update_needed(PREV))
6471 return 0;
6472
6473 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6474 XFS_DATA_FORK, PREV);
6475}
6476
6477/*
6478 * Process one of the deferred bmap operations. We pass back the
6479 * btree cursor to maintain our lock on the bmapbt between calls.
6480 */
6481int
6482xfs_bmap_finish_one(
6483 struct xfs_trans *tp,
6484 struct xfs_defer_ops *dfops,
6485 struct xfs_inode *ip,
6486 enum xfs_bmap_intent_type type,
6487 int whichfork,
6488 xfs_fileoff_t startoff,
6489 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006490 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006491 xfs_exntst_t state)
6492{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006493 xfs_fsblock_t firstfsb;
6494 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006495
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006496 /*
6497 * firstfsb is tied to the transaction lifetime and is used to
6498 * ensure correct AG locking order and schedule work item
6499 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6500 * to only making one bmap call per transaction, so it should
6501 * be safe to have it as a local variable here.
6502 */
6503 firstfsb = NULLFSBLOCK;
6504
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006505 trace_xfs_bmap_deferred(tp->t_mountp,
6506 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6507 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006508 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006509
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006510 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006511 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006512
6513 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006514 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006515 return -EIO;
6516
6517 switch (type) {
6518 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006519 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006520 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006521 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006522 break;
6523 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006524 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6525 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006526 break;
6527 default:
6528 ASSERT(0);
6529 error = -EFSCORRUPTED;
6530 }
6531
6532 return error;
6533}