blob: 6ca185dbd764589d17e4a4ccecab750fd9408069 [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 error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001579 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 xfs_fileoff_t new_endoff; /* end offset of new entry */
1581 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1582 /* left is 0, right is 1, prev is 2 */
1583 int rval=0; /* return value (logging flags) */
1584 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001585 xfs_filblks_t da_new; /* new count del alloc blocks used */
1586 xfs_filblks_t da_old; /* old count del alloc blocks used */
1587 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001589 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001590 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001591 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001593 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001594 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001595 ASSERT(whichfork != XFS_ATTR_FORK);
1596 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1597 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001598
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001599 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001600 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001601 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001602 ASSERT(!bma->cur ||
1603 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001604
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001605 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607#define LEFT r[0]
1608#define RIGHT r[1]
1609#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Darrick J. Wong60b49842016-10-03 09:11:34 -07001611 if (whichfork == XFS_COW_FORK)
1612 state |= BMAP_COWFORK;
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 /*
1615 * Set up a bunch of variables to make the tests simpler.
1616 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001617 xfs_iext_get_extent(ifp, bma->idx, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001619 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 ASSERT(PREV.br_startoff <= new->br_startoff);
1621 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001622
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001623 da_old = startblockval(PREV.br_startblock);
1624 da_new = 0;
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 /*
1627 * Set flags determining what part of the previous delayed allocation
1628 * extent is being replaced by a real allocation.
1629 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001630 if (PREV.br_startoff == new->br_startoff)
1631 state |= BMAP_LEFT_FILLING;
1632 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1633 state |= BMAP_RIGHT_FILLING;
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /*
1636 * Check and set flags if this segment has a left neighbor.
1637 * Don't set contiguous if the combined extent would be too large.
1638 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001639 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001640 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001641 xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001642
1643 if (isnullstartblock(LEFT.br_startblock))
1644 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001646
1647 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1648 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1649 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1650 LEFT.br_state == new->br_state &&
1651 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1652 state |= BMAP_LEFT_CONTIG;
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 /*
1655 * Check and set flags if this segment has a right neighbor.
1656 * Don't set contiguous if the combined extent would be too large.
1657 * Also check for all-three-contiguous being too large.
1658 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001659 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001660 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001661 xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001662
1663 if (isnullstartblock(RIGHT.br_startblock))
1664 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001666
1667 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1668 new_endoff == RIGHT.br_startoff &&
1669 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1670 new->br_state == RIGHT.br_state &&
1671 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1672 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1673 BMAP_RIGHT_FILLING)) !=
1674 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1675 BMAP_RIGHT_FILLING) ||
1676 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1677 <= MAXEXTLEN))
1678 state |= BMAP_RIGHT_CONTIG;
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 error = 0;
1681 /*
1682 * Switch out based on the FILLING and CONTIG state bits.
1683 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001684 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1685 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1686 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1687 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 /*
1689 * Filling in all of a previously delayed allocation extent.
1690 * The left and right neighbors are both contiguous with new.
1691 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001692 bma->idx--;
1693 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001694 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1695 xfs_iext_update_extent(ifp, bma->idx, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001696 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001697
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001698 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001699 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001700 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1702 else {
1703 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001704 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001706 RIGHT.br_blockcount, &i);
1707 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001709 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001710 error = xfs_btree_delete(bma->cur, &i);
1711 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001713 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001714 error = xfs_btree_decrement(bma->cur, 0, &i);
1715 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001717 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001718 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 LEFT.br_startblock,
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001720 LEFT.br_blockcount, LEFT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001721 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 goto done;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 break;
1725
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001726 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /*
1728 * Filling in all of a previously delayed allocation extent.
1729 * The left neighbor is contiguous, the right is not.
1730 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001731 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001732
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001733 old = LEFT;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001734 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001735 LEFT.br_blockcount += PREV.br_blockcount;
1736 xfs_iext_update_extent(ifp, bma->idx, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001737 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001738
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001739 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1740 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 rval = XFS_ILOG_DEXT;
1742 else {
1743 rval = 0;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001744 error = xfs_bmbt_lookup_eq(bma->cur, old.br_startoff,
1745 old.br_startblock, old.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001746 &i);
1747 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001749 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001750 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 LEFT.br_startblock,
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001752 LEFT.br_blockcount, LEFT.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001753 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 goto done;
1755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 break;
1757
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001758 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 /*
1760 * Filling in all of a previously delayed allocation extent.
1761 * The right neighbor is contiguous, the left is not.
1762 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001763 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001764 PREV.br_startblock = new->br_startblock;
1765 PREV.br_blockcount += RIGHT.br_blockcount;
1766 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001767 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001768
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001769 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1770 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 rval = XFS_ILOG_DEXT;
1772 else {
1773 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001774 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001776 RIGHT.br_blockcount, &i);
1777 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001779 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001780 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001781 PREV.br_startblock,
1782 PREV.br_blockcount, PREV.br_state);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001783 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 goto done;
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 break;
1787
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001788 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 /*
1790 * Filling in all of a previously delayed allocation extent.
1791 * Neither the left nor right neighbors are contiguous with
1792 * the new one.
1793 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001794 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001795 PREV.br_startblock = new->br_startblock;
1796 PREV.br_state = new->br_state;
1797 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001798 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001799
Darrick J. Wong60b49842016-10-03 09:11:34 -07001800 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001801 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1803 else {
1804 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001805 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001807 &i);
1808 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001810 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001811 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1812 error = xfs_btree_insert(bma->cur, &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 == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 break;
1818
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001819 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 /*
1821 * Filling in the first part of a previous delayed allocation.
1822 * The left neighbor is contiguous.
1823 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001824 old = LEFT;
1825 temp = PREV.br_blockcount - new->br_blockcount;
1826 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1827 startblockval(PREV.br_startblock));
1828
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001829 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001830 LEFT.br_blockcount += new->br_blockcount;
1831 xfs_iext_update_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001832 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001833
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001834 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001835 PREV.br_blockcount = temp = PREV.br_blockcount - new->br_blockcount;
1836 PREV.br_startoff += new->br_blockcount;
1837 PREV.br_startblock = nullstartblock(da_new);
1838 xfs_iext_update_extent(ifp, bma->idx, &PREV);
1839 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1840
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001841 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 rval = XFS_ILOG_DEXT;
1843 else {
1844 rval = 0;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001845 error = xfs_bmbt_lookup_eq(bma->cur, old.br_startoff,
1846 old.br_startblock, old.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 &i);
1848 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001850 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001851 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001852 LEFT.br_startblock, LEFT.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 Hellwigec90c552011-05-23 08:52:53 +00001857
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001858 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 break;
1860
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001861 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 /*
1863 * Filling in the first part of a previous delayed allocation.
1864 * The left neighbor is not contiguous.
1865 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001866 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001867 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001868 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1870 else {
1871 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001872 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001874 &i);
1875 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001877 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001878 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1879 error = xfs_btree_insert(bma->cur, &i);
1880 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001882 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001884
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001885 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001886 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001887 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001888 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 rval |= tmp_rval;
1890 if (error)
1891 goto done;
1892 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001893
1894 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001895 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001896 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001897 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001898
1899 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1900 PREV.br_startoff = new_endoff;
1901 PREV.br_blockcount = temp;
1902 PREV.br_startblock = nullstartblock(da_new);
1903 xfs_iext_update_extent(ifp, bma->idx + 1, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001904 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 break;
1907
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001908 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 /*
1910 * Filling in the last part of a previous delayed allocation.
1911 * The right neighbor is contiguous with the new allocation.
1912 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001913 old = RIGHT;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001914 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001915 RIGHT.br_startoff = new->br_startoff;
1916 RIGHT.br_startblock = new->br_startblock;
1917 RIGHT.br_blockcount += new->br_blockcount;
1918 xfs_iext_update_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001919 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001920
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001921 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 rval = XFS_ILOG_DEXT;
1923 else {
1924 rval = 0;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001925 error = xfs_bmbt_lookup_eq(bma->cur, old.br_startoff,
1926 old.br_startblock,
1927 old.br_blockcount, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001928 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001930 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001931 error = xfs_bmbt_update(bma->cur, RIGHT.br_startoff,
1932 RIGHT.br_startblock, RIGHT.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001933 RIGHT.br_state);
1934 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 goto done;
1936 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001937
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001938 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001939 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001940 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001941
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001942 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001943 PREV.br_blockcount = temp;
1944 PREV.br_startblock = nullstartblock(da_new);
1945 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001946 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001947
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001948 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 break;
1950
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001951 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 /*
1953 * Filling in the last part of a previous delayed allocation.
1954 * The right neighbor is not contiguous.
1955 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001956 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001957 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1960 else {
1961 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001962 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001964 &i);
1965 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001967 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001968 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1969 error = xfs_btree_insert(bma->cur, &i);
1970 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001972 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001974
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001975 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001976 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001977 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001978 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 rval |= tmp_rval;
1980 if (error)
1981 goto done;
1982 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001983
1984 temp = PREV.br_blockcount - new->br_blockcount;
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));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001988
1989 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1990 PREV.br_startblock = nullstartblock(da_new);
1991 PREV.br_blockcount = temp;
1992 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001993 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001994
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001995 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 break;
1997
1998 case 0:
1999 /*
2000 * Filling in the middle part of a previous delayed allocation.
2001 * Contiguity is impossible here.
2002 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002003 *
2004 * We start with a delayed allocation:
2005 *
2006 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
2007 * PREV @ idx
2008 *
2009 * and we are allocating:
2010 * +rrrrrrrrrrrrrrrrr+
2011 * new
2012 *
2013 * and we set it up for insertion as:
2014 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2015 * new
2016 * PREV @ idx LEFT RIGHT
2017 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07002019 old = PREV;
2020
2021 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002022 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07002023
2024 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002025 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002026 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07002027 RIGHT.br_blockcount =
2028 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2029 RIGHT.br_startblock =
2030 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
2031 RIGHT.br_blockcount));
2032
2033 /* truncate PREV */
2034 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
2035 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2036 PREV.br_startblock =
2037 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
2038 PREV.br_blockcount));
2039 xfs_iext_update_extent(ifp, bma->idx, &PREV);
2040 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2041
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002042 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002043 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002044 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07002045
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002046 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2048 else {
2049 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002050 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002052 &i);
2053 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002055 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002056 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2057 error = xfs_btree_insert(bma->cur, &i);
2058 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002060 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002062
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002063 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002064 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002065 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002066 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 rval |= tmp_rval;
2068 if (error)
2069 goto done;
2070 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07002071
2072 da_new = startblockval(PREV.br_startblock) +
2073 startblockval(RIGHT.br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002074 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 break;
2076
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002077 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2078 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2079 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2080 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2081 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2082 case BMAP_LEFT_CONTIG:
2083 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 /*
2085 * These cases are all impossible.
2086 */
2087 ASSERT(0);
2088 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002089
Darrick J. Wong9c194642016-08-03 12:16:05 +10002090 /* add reverse mapping */
2091 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2092 if (error)
2093 goto done;
2094
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002095 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002096 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002097 int tmp_logflags; /* partial log flag return val */
2098
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002099 ASSERT(bma->cur == NULL);
2100 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002101 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002102 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002103 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002104 if (error)
2105 goto done;
2106 }
2107
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002108 if (bma->cur) {
2109 da_new += bma->cur->bc_private.b.allocated;
2110 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002111 }
2112
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002113 /* adjust for changes in reserved delayed indirect blocks */
2114 if (da_new != da_old) {
2115 ASSERT(state == 0 || da_new < da_old);
2116 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2117 false);
2118 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002119
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002120 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002122 if (whichfork != XFS_COW_FORK)
2123 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 return error;
2125#undef LEFT
2126#undef RIGHT
2127#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128}
2129
2130/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002131 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 */
2133STATIC int /* error */
2134xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002135 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002137 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002138 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002140 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002141 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002142 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002143 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 xfs_btree_cur_t *cur; /* btree cursor */
Christoph Hellwiga6f64d42007-08-16 16:23:40 +10002146 xfs_bmbt_rec_host_t *ep; /* extent entry for idx */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002149 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 xfs_fileoff_t new_endoff; /* end offset of new entry */
2151 xfs_exntst_t newext; /* new extent state */
2152 xfs_exntst_t oldext; /* old extent state */
2153 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2154 /* left is 0, right is 1, prev is 2 */
2155 int rval=0; /* return value (logging flags) */
2156 int state = 0;/* state bits, accessed thru macros */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002157 struct xfs_mount *mp = ip->i_mount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002159 *logflagsp = 0;
2160
2161 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002162 ifp = XFS_IFORK_PTR(ip, whichfork);
2163 if (whichfork == XFS_COW_FORK)
2164 state |= BMAP_COWFORK;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002165
2166 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002167 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002168 ASSERT(!isnullstartblock(new->br_startblock));
2169
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002170 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172#define LEFT r[0]
2173#define RIGHT r[1]
2174#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 /*
2177 * Set up a bunch of variables to make the tests simpler.
2178 */
2179 error = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002180 ep = xfs_iext_get_ext(ifp, *idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 xfs_bmbt_get_all(ep, &PREV);
2182 newext = new->br_state;
2183 oldext = (newext == XFS_EXT_UNWRITTEN) ?
2184 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
2185 ASSERT(PREV.br_state == oldext);
2186 new_endoff = new->br_startoff + new->br_blockcount;
2187 ASSERT(PREV.br_startoff <= new->br_startoff);
2188 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 /*
2191 * Set flags determining what part of the previous oldext allocation
2192 * extent is being replaced by a newext allocation.
2193 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002194 if (PREV.br_startoff == new->br_startoff)
2195 state |= BMAP_LEFT_FILLING;
2196 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2197 state |= BMAP_RIGHT_FILLING;
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 /*
2200 * Check and set flags if this segment has a left neighbor.
2201 * Don't set contiguous if the combined extent would be too large.
2202 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002203 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002204 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002205 xfs_iext_get_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002206
2207 if (isnullstartblock(LEFT.br_startblock))
2208 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002210
2211 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2212 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2213 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
2214 LEFT.br_state == newext &&
2215 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2216 state |= BMAP_LEFT_CONTIG;
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /*
2219 * Check and set flags if this segment has a right neighbor.
2220 * Don't set contiguous if the combined extent would be too large.
2221 * Also check for all-three-contiguous being too large.
2222 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002223 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002224 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002225 xfs_iext_get_extent(ifp, *idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002226 if (isnullstartblock(RIGHT.br_startblock))
2227 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002229
2230 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2231 new_endoff == RIGHT.br_startoff &&
2232 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
2233 newext == RIGHT.br_state &&
2234 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2235 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2236 BMAP_RIGHT_FILLING)) !=
2237 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2238 BMAP_RIGHT_FILLING) ||
2239 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2240 <= MAXEXTLEN))
2241 state |= BMAP_RIGHT_CONTIG;
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 /*
2244 * Switch out based on the FILLING and CONTIG state bits.
2245 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002246 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2247 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2248 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2249 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 /*
2251 * Setting all of a previous oldext extent to newext.
2252 * The left and right neighbors are both contiguous with new.
2253 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002254 --*idx;
2255
2256 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2257 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 LEFT.br_blockcount + PREV.br_blockcount +
2259 RIGHT.br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002260 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002261
Christoph Hellwigec90c552011-05-23 08:52:53 +00002262 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002263 XFS_IFORK_NEXT_SET(ip, whichfork,
2264 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (cur == NULL)
2266 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2267 else {
2268 rval = XFS_ILOG_CORE;
2269 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2270 RIGHT.br_startblock,
2271 RIGHT.br_blockcount, &i)))
2272 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002273 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002274 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002276 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002277 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002279 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002280 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002282 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002283 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002285 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2287 LEFT.br_startblock,
2288 LEFT.br_blockcount + PREV.br_blockcount +
2289 RIGHT.br_blockcount, LEFT.br_state)))
2290 goto done;
2291 }
2292 break;
2293
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002294 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 /*
2296 * Setting all of a previous oldext extent to newext.
2297 * The left neighbor is contiguous, the right is not.
2298 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002299 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002300
Christoph Hellwigec90c552011-05-23 08:52:53 +00002301 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2302 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
2303 LEFT.br_blockcount + PREV.br_blockcount);
2304 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2305
2306 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002307 XFS_IFORK_NEXT_SET(ip, whichfork,
2308 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (cur == NULL)
2310 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2311 else {
2312 rval = XFS_ILOG_CORE;
2313 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2314 PREV.br_startblock, PREV.br_blockcount,
2315 &i)))
2316 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002317 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002318 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002320 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002321 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002323 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2325 LEFT.br_startblock,
2326 LEFT.br_blockcount + PREV.br_blockcount,
2327 LEFT.br_state)))
2328 goto done;
2329 }
2330 break;
2331
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002332 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 /*
2334 * Setting all of a previous oldext extent to newext.
2335 * The right neighbor is contiguous, the left is not.
2336 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002337 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 xfs_bmbt_set_blockcount(ep,
2339 PREV.br_blockcount + RIGHT.br_blockcount);
2340 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002341 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2342 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002343 XFS_IFORK_NEXT_SET(ip, whichfork,
2344 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (cur == NULL)
2346 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2347 else {
2348 rval = XFS_ILOG_CORE;
2349 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2350 RIGHT.br_startblock,
2351 RIGHT.br_blockcount, &i)))
2352 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002353 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002354 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002356 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002357 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002359 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2361 new->br_startblock,
2362 new->br_blockcount + RIGHT.br_blockcount,
2363 newext)))
2364 goto done;
2365 }
2366 break;
2367
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002368 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 /*
2370 * Setting all of a previous oldext extent to newext.
2371 * Neither the left nor right neighbors are contiguous with
2372 * the new one.
2373 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002374 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 xfs_bmbt_set_state(ep, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002376 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (cur == NULL)
2379 rval = XFS_ILOG_DEXT;
2380 else {
2381 rval = 0;
2382 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2383 new->br_startblock, new->br_blockcount,
2384 &i)))
2385 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002386 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2388 new->br_startblock, new->br_blockcount,
2389 newext)))
2390 goto done;
2391 }
2392 break;
2393
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002394 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 /*
2396 * Setting the first part of a previous oldext extent to newext.
2397 * The left neighbor is contiguous.
2398 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002399 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
2400 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 LEFT.br_blockcount + new->br_blockcount);
2402 xfs_bmbt_set_startoff(ep,
2403 PREV.br_startoff + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002404 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002405
Christoph Hellwigec90c552011-05-23 08:52:53 +00002406 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 xfs_bmbt_set_startblock(ep,
2408 new->br_startblock + new->br_blockcount);
2409 xfs_bmbt_set_blockcount(ep,
2410 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002411 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002412
Christoph Hellwigec90c552011-05-23 08:52:53 +00002413 --*idx;
2414
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 if (cur == NULL)
2416 rval = XFS_ILOG_DEXT;
2417 else {
2418 rval = 0;
2419 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2420 PREV.br_startblock, PREV.br_blockcount,
2421 &i)))
2422 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002423 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 if ((error = xfs_bmbt_update(cur,
2425 PREV.br_startoff + new->br_blockcount,
2426 PREV.br_startblock + new->br_blockcount,
2427 PREV.br_blockcount - new->br_blockcount,
2428 oldext)))
2429 goto done;
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002430 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 goto done;
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002432 error = xfs_bmbt_update(cur, LEFT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 LEFT.br_startblock,
2434 LEFT.br_blockcount + new->br_blockcount,
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002435 LEFT.br_state);
2436 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 goto done;
2438 }
2439 break;
2440
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002441 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 /*
2443 * Setting the first part of a previous oldext extent to newext.
2444 * The left neighbor is not contiguous.
2445 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002446 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
2448 xfs_bmbt_set_startoff(ep, new_endoff);
2449 xfs_bmbt_set_blockcount(ep,
2450 PREV.br_blockcount - new->br_blockcount);
2451 xfs_bmbt_set_startblock(ep,
2452 new->br_startblock + new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002453 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002454
Christoph Hellwigec90c552011-05-23 08:52:53 +00002455 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002456 XFS_IFORK_NEXT_SET(ip, whichfork,
2457 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 if (cur == NULL)
2459 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2460 else {
2461 rval = XFS_ILOG_CORE;
2462 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2463 PREV.br_startblock, PREV.br_blockcount,
2464 &i)))
2465 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002466 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if ((error = xfs_bmbt_update(cur,
2468 PREV.br_startoff + new->br_blockcount,
2469 PREV.br_startblock + new->br_blockcount,
2470 PREV.br_blockcount - new->br_blockcount,
2471 oldext)))
2472 goto done;
2473 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002474 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002476 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
2478 break;
2479
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002480 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 /*
2482 * Setting the last part of a previous oldext extent to newext.
2483 * The right neighbor is contiguous with the new allocation.
2484 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002485 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 xfs_bmbt_set_blockcount(ep,
2487 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002488 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2489
2490 ++*idx;
2491
2492 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2493 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002494 new->br_startoff, new->br_startblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 new->br_blockcount + RIGHT.br_blockcount, newext);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002496 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002497
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 if (cur == NULL)
2499 rval = XFS_ILOG_DEXT;
2500 else {
2501 rval = 0;
2502 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2503 PREV.br_startblock,
2504 PREV.br_blockcount, &i)))
2505 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002506 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2508 PREV.br_startblock,
2509 PREV.br_blockcount - new->br_blockcount,
2510 oldext)))
2511 goto done;
Christoph Hellwig637aa502008-10-30 16:55:45 +11002512 if ((error = xfs_btree_increment(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 goto done;
2514 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2515 new->br_startblock,
2516 new->br_blockcount + RIGHT.br_blockcount,
2517 newext)))
2518 goto done;
2519 }
2520 break;
2521
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002522 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 /*
2524 * Setting the last part of a previous oldext extent to newext.
2525 * The right neighbor is not contiguous.
2526 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002527 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 xfs_bmbt_set_blockcount(ep,
2529 PREV.br_blockcount - new->br_blockcount);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002530 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002531
Christoph Hellwigec90c552011-05-23 08:52:53 +00002532 ++*idx;
2533 xfs_iext_insert(ip, *idx, 1, new, state);
2534
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002535 XFS_IFORK_NEXT_SET(ip, whichfork,
2536 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 if (cur == NULL)
2538 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2539 else {
2540 rval = XFS_ILOG_CORE;
2541 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2542 PREV.br_startblock, PREV.br_blockcount,
2543 &i)))
2544 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002545 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2547 PREV.br_startblock,
2548 PREV.br_blockcount - new->br_blockcount,
2549 oldext)))
2550 goto done;
2551 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2552 new->br_startblock, new->br_blockcount,
2553 &i)))
2554 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002555 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig5e422f52017-10-17 14:16:19 -07002556 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002557 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002559 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
2561 break;
2562
2563 case 0:
2564 /*
2565 * Setting the middle part of a previous oldext extent to
2566 * newext. Contiguity is impossible here.
2567 * One extent becomes three extents.
2568 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002569 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 xfs_bmbt_set_blockcount(ep,
2571 new->br_startoff - PREV.br_startoff);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002572 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 r[0] = *new;
2575 r[1].br_startoff = new_endoff;
2576 r[1].br_blockcount =
2577 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2578 r[1].br_startblock = new->br_startblock + new->br_blockcount;
2579 r[1].br_state = oldext;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002580
2581 ++*idx;
2582 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2583
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002584 XFS_IFORK_NEXT_SET(ip, whichfork,
2585 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 if (cur == NULL)
2587 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2588 else {
2589 rval = XFS_ILOG_CORE;
2590 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2591 PREV.br_startblock, PREV.br_blockcount,
2592 &i)))
2593 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002594 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 /* new right extent - oldext */
2596 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2597 r[1].br_startblock, r[1].br_blockcount,
2598 r[1].br_state)))
2599 goto done;
2600 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 cur->bc_rec.b = PREV;
Tim Shimmin6a617dd2008-07-18 17:13:04 +10002602 cur->bc_rec.b.br_blockcount =
2603 new->br_startoff - PREV.br_startoff;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002604 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002606 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002607 /*
2608 * Reset the cursor to the position of the new extent
2609 * we are about to insert as we can't trust it after
2610 * the previous insert.
2611 */
2612 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2613 new->br_startblock, new->br_blockcount,
2614 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002616 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002618 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002619 if ((error = xfs_btree_insert(cur, &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 == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 }
2623 break;
2624
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002625 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2626 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2627 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2628 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2629 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2630 case BMAP_LEFT_CONTIG:
2631 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 /*
2633 * These cases are all impossible.
2634 */
2635 ASSERT(0);
2636 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002637
Darrick J. Wong9c194642016-08-03 12:16:05 +10002638 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002639 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002640 if (error)
2641 goto done;
2642
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002643 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002644 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002645 int tmp_logflags; /* partial log flag return val */
2646
2647 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002648 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002649 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002650 *logflagsp |= tmp_logflags;
2651 if (error)
2652 goto done;
2653 }
2654
2655 /* clear out the allocated field, done with it now in any case. */
2656 if (cur) {
2657 cur->bc_private.b.allocated = 0;
2658 *curp = cur;
2659 }
2660
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002661 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002663 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 return error;
2665#undef LEFT
2666#undef RIGHT
2667#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668}
2669
2670/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002671 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002673STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674xfs_bmap_add_extent_hole_delay(
2675 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002676 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002677 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002678 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002680 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2682 xfs_filblks_t newlen=0; /* new indirect size */
2683 xfs_filblks_t oldlen=0; /* old indirect size */
2684 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2685 int state; /* state bits, accessed thru macros */
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002686 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002688 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002690 if (whichfork == XFS_COW_FORK)
2691 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002692 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 /*
2695 * Check and set flags if this segment has a left neighbor
2696 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002697 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002698 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002699 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002700
2701 if (isnullstartblock(left.br_startblock))
2702 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002704
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 /*
2706 * Check and set flags if the current (right) segment exists.
2707 * If it doesn't exist, we're converting the hole at end-of-file.
2708 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002709 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002710 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002711 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002712
2713 if (isnullstartblock(right.br_startblock))
2714 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 /*
2718 * Set contiguity flags on the left and right neighbors.
2719 * Don't let extents get too large, even if the pieces are contiguous.
2720 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002721 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2722 left.br_startoff + left.br_blockcount == new->br_startoff &&
2723 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2724 state |= BMAP_LEFT_CONTIG;
2725
2726 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2727 new->br_startoff + new->br_blockcount == right.br_startoff &&
2728 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2729 (!(state & BMAP_LEFT_CONTIG) ||
2730 (left.br_blockcount + new->br_blockcount +
2731 right.br_blockcount <= MAXEXTLEN)))
2732 state |= BMAP_RIGHT_CONTIG;
2733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 /*
2735 * Switch out based on the contiguity flags.
2736 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002737 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2738 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 /*
2740 * New allocation is contiguous with delayed allocations
2741 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002742 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002744 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 temp = left.br_blockcount + new->br_blockcount +
2746 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002747
Christoph Hellwigec90c552011-05-23 08:52:53 +00002748 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002749 oldlen = startblockval(left.br_startblock) +
2750 startblockval(new->br_startblock) +
2751 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002752 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2753 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002754 left.br_startblock = nullstartblock(newlen);
2755 left.br_blockcount = temp;
2756 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002757 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002758
Christoph Hellwigec90c552011-05-23 08:52:53 +00002759 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 break;
2761
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002762 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 /*
2764 * New allocation is contiguous with a delayed allocation
2765 * on the left.
2766 * Merge the new allocation with the left neighbor.
2767 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002768 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002770
2771 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002772 oldlen = startblockval(left.br_startblock) +
2773 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002774 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2775 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002776 left.br_blockcount = temp;
2777 left.br_startblock = nullstartblock(newlen);
2778 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002779 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 break;
2781
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002782 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 /*
2784 * New allocation is contiguous with a delayed allocation
2785 * on the right.
2786 * Merge the new allocation with the right neighbor.
2787 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002788 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002790 oldlen = startblockval(new->br_startblock) +
2791 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002792 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2793 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002794 right.br_startoff = new->br_startoff;
2795 right.br_startblock = nullstartblock(newlen);
2796 right.br_blockcount = temp;
2797 xfs_iext_update_extent(ifp, *idx, &right);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002798 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 break;
2800
2801 case 0:
2802 /*
2803 * New allocation is not contiguous with another
2804 * delayed allocation.
2805 * Insert a new entry.
2806 */
2807 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002808 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 break;
2810 }
2811 if (oldlen != newlen) {
2812 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002813 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2814 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 /*
2816 * Nothing to do for disk quota accounting here.
2817 */
2818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
2820
2821/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002822 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 */
2824STATIC int /* error */
2825xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002826 struct xfs_trans *tp,
2827 struct xfs_inode *ip,
2828 int whichfork,
2829 xfs_extnum_t *idx,
2830 struct xfs_btree_cur **curp,
2831 struct xfs_bmbt_irec *new,
2832 xfs_fsblock_t *first,
2833 struct xfs_defer_ops *dfops,
2834 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002836 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2837 struct xfs_mount *mp = ip->i_mount;
2838 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2842 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002843 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 int state; /* state bits, accessed thru macros */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002845 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Christoph Hellwig6d045582017-04-11 16:45:54 -07002847 ASSERT(*idx >= 0);
2848 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002849 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002850 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002851
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002852 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002853
2854 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002855 if (whichfork == XFS_ATTR_FORK)
2856 state |= BMAP_ATTRFORK;
Christoph Hellwiga14234c2017-02-06 10:50:49 -08002857 if (whichfork == XFS_COW_FORK)
2858 state |= BMAP_COWFORK;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860 /*
2861 * Check and set flags if this segment has a left neighbor.
2862 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002863 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002864 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002865 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002866 if (isnullstartblock(left.br_startblock))
2867 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 /*
2871 * Check and set flags if this segment has a current value.
2872 * Not true if we're inserting into the "hole" at eof.
2873 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002874 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002875 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002876 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002877 if (isnullstartblock(right.br_startblock))
2878 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002880
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 /*
2882 * We're inserting a real allocation between "left" and "right".
2883 * Set the contiguity flags. Don't let extents get too large.
2884 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002885 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2886 left.br_startoff + left.br_blockcount == new->br_startoff &&
2887 left.br_startblock + left.br_blockcount == new->br_startblock &&
2888 left.br_state == new->br_state &&
2889 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2890 state |= BMAP_LEFT_CONTIG;
2891
2892 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2893 new->br_startoff + new->br_blockcount == right.br_startoff &&
2894 new->br_startblock + new->br_blockcount == right.br_startblock &&
2895 new->br_state == right.br_state &&
2896 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2897 (!(state & BMAP_LEFT_CONTIG) ||
2898 left.br_blockcount + new->br_blockcount +
2899 right.br_blockcount <= MAXEXTLEN))
2900 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002902 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 /*
2904 * Select which case we're in here, and implement it.
2905 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002906 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2907 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 /*
2909 * New allocation is contiguous with real allocations on the
2910 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002911 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002913 --*idx;
2914 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002915 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2916 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002917 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002918
Christoph Hellwig6d045582017-04-11 16:45:54 -07002919 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002920
Christoph Hellwig6d045582017-04-11 16:45:54 -07002921 XFS_IFORK_NEXT_SET(ip, whichfork,
2922 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2923 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002924 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002925 } else {
2926 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002927 error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002928 right.br_startblock, right.br_blockcount,
2929 &i);
2930 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002931 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002932 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002933 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002934 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_decrement(cur, 0, &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_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002942 left.br_startblock,
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002943 left.br_blockcount, left.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002944 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002945 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002947 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002949 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 /*
2951 * New allocation is contiguous with a real allocation
2952 * on the left.
2953 * Merge the new allocation with the left neighbor.
2954 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002955 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002956 old = left;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002957 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002958 left.br_blockcount += new->br_blockcount;
2959 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002960 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002961
Christoph Hellwig6d045582017-04-11 16:45:54 -07002962 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002963 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002964 } else {
2965 rval = 0;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002966 error = xfs_bmbt_lookup_eq(cur, old.br_startoff,
2967 old.br_startblock, old.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002968 &i);
2969 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002970 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002971 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002972 error = xfs_bmbt_update(cur, left.br_startoff,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002973 left.br_startblock,
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002974 left.br_blockcount, left.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002975 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002976 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002978 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002980 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 /*
2982 * New allocation is contiguous with a real allocation
2983 * on the right.
2984 * Merge the new allocation with the right neighbor.
2985 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002986 old = right;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002987 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002988 right.br_startoff = new->br_startoff;
2989 right.br_startblock = new->br_startblock;
2990 right.br_blockcount += new->br_blockcount;
2991 xfs_iext_update_extent(ifp, *idx, &right);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002992 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002993
Christoph Hellwig6d045582017-04-11 16:45:54 -07002994 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002995 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002996 } else {
2997 rval = 0;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002998 error = xfs_bmbt_lookup_eq(cur, old.br_startoff,
2999 old.br_startblock, old.br_blockcount,
3000 &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003001 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003002 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003003 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07003004 error = xfs_bmbt_update(cur, right.br_startoff,
3005 right.br_startblock,
3006 right.br_blockcount, right.br_state);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003007 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003008 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003010 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
3012 case 0:
3013 /*
3014 * New allocation is not contiguous with another
3015 * real allocation.
3016 * Insert a new entry.
3017 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003018 xfs_iext_insert(ip, *idx, 1, new, state);
3019 XFS_IFORK_NEXT_SET(ip, whichfork,
3020 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
3021 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06003022 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003023 } else {
3024 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07003025 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003026 new->br_startoff,
3027 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003028 new->br_blockcount, &i);
3029 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003030 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003031 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003032 cur->bc_rec.b.br_state = new->br_state;
3033 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00003034 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003035 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11003036 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003038 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003040
Darrick J. Wong9c194642016-08-03 12:16:05 +10003041 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003042 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10003043 if (error)
3044 goto done;
3045
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003046 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003047 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003048 int tmp_logflags; /* partial log flag return val */
3049
Christoph Hellwig6d045582017-04-11 16:45:54 -07003050 ASSERT(cur == NULL);
3051 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003052 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003053 *logflagsp |= tmp_logflags;
3054 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003055 if (error)
3056 goto done;
3057 }
3058
3059 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003060 if (cur)
3061 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003062
Christoph Hellwig6d045582017-04-11 16:45:54 -07003063 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003064done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07003065 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003066 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067}
3068
Nathan Scottdd9f4382006-01-11 15:28:28 +11003069/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003070 * Functions used in the extent read, allocate and remove paths
3071 */
3072
3073/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003074 * Adjust the size of the new extent based on di_extsize and rt extsize.
3075 */
Dave Chinner68988112013-08-12 20:49:42 +10003076int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003077xfs_bmap_extsize_align(
3078 xfs_mount_t *mp,
3079 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3080 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3081 xfs_extlen_t extsz, /* align to this extent size */
3082 int rt, /* is this a realtime inode? */
3083 int eof, /* is extent at end-of-file? */
3084 int delay, /* creating delalloc extent? */
3085 int convert, /* overwriting unwritten extent? */
3086 xfs_fileoff_t *offp, /* in/out: aligned offset */
3087 xfs_extlen_t *lenp) /* in/out: aligned length */
3088{
3089 xfs_fileoff_t orig_off; /* original offset */
3090 xfs_extlen_t orig_alen; /* original length */
3091 xfs_fileoff_t orig_end; /* original off+len */
3092 xfs_fileoff_t nexto; /* next file offset */
3093 xfs_fileoff_t prevo; /* previous file offset */
3094 xfs_fileoff_t align_off; /* temp for offset */
3095 xfs_extlen_t align_alen; /* temp for length */
3096 xfs_extlen_t temp; /* temp for calculations */
3097
3098 if (convert)
3099 return 0;
3100
3101 orig_off = align_off = *offp;
3102 orig_alen = align_alen = *lenp;
3103 orig_end = orig_off + orig_alen;
3104
3105 /*
3106 * If this request overlaps an existing extent, then don't
3107 * attempt to perform any additional alignment.
3108 */
3109 if (!delay && !eof &&
3110 (orig_off >= gotp->br_startoff) &&
3111 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3112 return 0;
3113 }
3114
3115 /*
3116 * If the file offset is unaligned vs. the extent size
3117 * we need to align it. This will be possible unless
3118 * the file was previously written with a kernel that didn't
3119 * perform this alignment, or if a truncate shot us in the
3120 * foot.
3121 */
3122 temp = do_mod(orig_off, extsz);
3123 if (temp) {
3124 align_alen += temp;
3125 align_off -= temp;
3126 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003127
3128 /* Same adjustment for the end of the requested area. */
3129 temp = (align_alen % extsz);
3130 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003131 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003132
3133 /*
3134 * For large extent hint sizes, the aligned extent might be larger than
3135 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3136 * the length back under MAXEXTLEN. The outer allocation loops handle
3137 * short allocation just fine, so it is safe to do this. We only want to
3138 * do it when we are forced to, though, because it means more allocation
3139 * operations are required.
3140 */
3141 while (align_alen > MAXEXTLEN)
3142 align_alen -= extsz;
3143 ASSERT(align_alen <= MAXEXTLEN);
3144
Nathan Scottdd9f4382006-01-11 15:28:28 +11003145 /*
3146 * If the previous block overlaps with this proposed allocation
3147 * then move the start forward without adjusting the length.
3148 */
3149 if (prevp->br_startoff != NULLFILEOFF) {
3150 if (prevp->br_startblock == HOLESTARTBLOCK)
3151 prevo = prevp->br_startoff;
3152 else
3153 prevo = prevp->br_startoff + prevp->br_blockcount;
3154 } else
3155 prevo = 0;
3156 if (align_off != orig_off && align_off < prevo)
3157 align_off = prevo;
3158 /*
3159 * If the next block overlaps with this proposed allocation
3160 * then move the start back without adjusting the length,
3161 * but not before offset 0.
3162 * This may of course make the start overlap previous block,
3163 * and if we hit the offset 0 limit then the next block
3164 * can still overlap too.
3165 */
3166 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3167 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3168 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3169 nexto = gotp->br_startoff + gotp->br_blockcount;
3170 else
3171 nexto = gotp->br_startoff;
3172 } else
3173 nexto = NULLFILEOFF;
3174 if (!eof &&
3175 align_off + align_alen != orig_end &&
3176 align_off + align_alen > nexto)
3177 align_off = nexto > align_alen ? nexto - align_alen : 0;
3178 /*
3179 * If we're now overlapping the next or previous extent that
3180 * means we can't fit an extsz piece in this hole. Just move
3181 * the start forward to the first valid spot and set
3182 * the length so we hit the end.
3183 */
3184 if (align_off != orig_off && align_off < prevo)
3185 align_off = prevo;
3186 if (align_off + align_alen != orig_end &&
3187 align_off + align_alen > nexto &&
3188 nexto != NULLFILEOFF) {
3189 ASSERT(nexto > prevo);
3190 align_alen = nexto - align_off;
3191 }
3192
3193 /*
3194 * If realtime, and the result isn't a multiple of the realtime
3195 * extent size we need to remove blocks until it is.
3196 */
3197 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3198 /*
3199 * We're not covering the original request, or
3200 * we won't be able to once we fix the length.
3201 */
3202 if (orig_off < align_off ||
3203 orig_end > align_off + align_alen ||
3204 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003205 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003206 /*
3207 * Try to fix it by moving the start up.
3208 */
3209 if (align_off + temp <= orig_off) {
3210 align_alen -= temp;
3211 align_off += temp;
3212 }
3213 /*
3214 * Try to fix it by moving the end in.
3215 */
3216 else if (align_off + align_alen - temp >= orig_end)
3217 align_alen -= temp;
3218 /*
3219 * Set the start to the minimum then trim the length.
3220 */
3221 else {
3222 align_alen -= orig_off - align_off;
3223 align_off = orig_off;
3224 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3225 }
3226 /*
3227 * Result doesn't cover the request, fail it.
3228 */
3229 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003230 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003231 } else {
3232 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003233 /* see MAXEXTLEN handling above */
3234 ASSERT(orig_end <= align_off + align_alen ||
3235 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003236 }
3237
3238#ifdef DEBUG
3239 if (!eof && gotp->br_startoff != NULLFILEOFF)
3240 ASSERT(align_off + align_alen <= gotp->br_startoff);
3241 if (prevp->br_startoff != NULLFILEOFF)
3242 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3243#endif
3244
3245 *lenp = align_alen;
3246 *offp = align_off;
3247 return 0;
3248}
3249
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250#define XFS_ALLOC_GAP_UNITS 4
3251
Dave Chinner68988112013-08-12 20:49:42 +10003252void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003253xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003254 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255{
3256 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3258 xfs_mount_t *mp; /* mount point structure */
3259 int nullfb; /* true if ap->firstblock isn't set */
3260 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
3262#define ISVALID(x,y) \
3263 (rt ? \
3264 (x) < mp->m_sb.sb_rblocks : \
3265 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3266 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3267 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3268
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003270 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003271 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3272 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003273 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 /*
3275 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003276 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003278 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3279 !isnullstartblock(ap->prev.br_startblock) &&
3280 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3281 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003282 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 /*
3284 * Adjust for the gap between prevp and us.
3285 */
Dave Chinner3a756672011-09-18 20:40:58 +00003286 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003287 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003289 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3290 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 }
3292 /*
3293 * If not at eof, then compare the two neighbor blocks.
3294 * Figure out whether either one gives us a good starting point,
3295 * and pick the better one.
3296 */
3297 else if (!ap->eof) {
3298 xfs_fsblock_t gotbno; /* right side block number */
3299 xfs_fsblock_t gotdiff=0; /* right side difference */
3300 xfs_fsblock_t prevbno; /* left side block number */
3301 xfs_fsblock_t prevdiff=0; /* left side difference */
3302
3303 /*
3304 * If there's a previous (left) block, select a requested
3305 * start block based on it.
3306 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003307 if (ap->prev.br_startoff != NULLFILEOFF &&
3308 !isnullstartblock(ap->prev.br_startblock) &&
3309 (prevbno = ap->prev.br_startblock +
3310 ap->prev.br_blockcount) &&
3311 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 /*
3313 * Calculate gap to end of previous block.
3314 */
Dave Chinner3a756672011-09-18 20:40:58 +00003315 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003316 (ap->prev.br_startoff +
3317 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 /*
3319 * Figure the startblock based on the previous block's
3320 * end and the gap size.
3321 * Heuristic!
3322 * If the gap is large relative to the piece we're
3323 * allocating, or using it gives us an invalid block
3324 * number, then just use the end of the previous block.
3325 */
Dave Chinner3a756672011-09-18 20:40:58 +00003326 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003328 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329 prevbno += adjust;
3330 else
3331 prevdiff += adjust;
3332 /*
3333 * If the firstblock forbids it, can't use it,
3334 * must use default.
3335 */
3336 if (!rt && !nullfb &&
3337 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3338 prevbno = NULLFSBLOCK;
3339 }
3340 /*
3341 * No previous block or can't follow it, just default.
3342 */
3343 else
3344 prevbno = NULLFSBLOCK;
3345 /*
3346 * If there's a following (right) block, select a requested
3347 * start block based on it.
3348 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003349 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 /*
3351 * Calculate gap to start of next block.
3352 */
Dave Chinner3a756672011-09-18 20:40:58 +00003353 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 /*
3355 * Figure the startblock based on the next block's
3356 * start and the gap size.
3357 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003358 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 /*
3360 * Heuristic!
3361 * If the gap is large relative to the piece we're
3362 * allocating, or using it gives us an invalid block
3363 * number, then just use the start of the next block
3364 * offset by our length.
3365 */
Dave Chinner3a756672011-09-18 20:40:58 +00003366 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 ISVALID(gotbno - gotdiff, gotbno))
3368 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003369 else if (ISVALID(gotbno - ap->length, gotbno)) {
3370 gotbno -= ap->length;
3371 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 } else
3373 gotdiff += adjust;
3374 /*
3375 * If the firstblock forbids it, can't use it,
3376 * must use default.
3377 */
3378 if (!rt && !nullfb &&
3379 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3380 gotbno = NULLFSBLOCK;
3381 }
3382 /*
3383 * No next block, just default.
3384 */
3385 else
3386 gotbno = NULLFSBLOCK;
3387 /*
3388 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003389 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 */
3391 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003392 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003394 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003396 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003398#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003399}
3400
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003401static int
3402xfs_bmap_longest_free_extent(
3403 struct xfs_trans *tp,
3404 xfs_agnumber_t ag,
3405 xfs_extlen_t *blen,
3406 int *notinit)
3407{
3408 struct xfs_mount *mp = tp->t_mountp;
3409 struct xfs_perag *pag;
3410 xfs_extlen_t longest;
3411 int error = 0;
3412
3413 pag = xfs_perag_get(mp, ag);
3414 if (!pag->pagf_init) {
3415 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3416 if (error)
3417 goto out;
3418
3419 if (!pag->pagf_init) {
3420 *notinit = 1;
3421 goto out;
3422 }
3423 }
3424
Dave Chinner50adbcb2015-06-22 10:04:31 +10003425 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003426 xfs_alloc_min_freelist(mp, pag),
3427 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003428 if (*blen < longest)
3429 *blen = longest;
3430
3431out:
3432 xfs_perag_put(pag);
3433 return error;
3434}
3435
3436static void
3437xfs_bmap_select_minlen(
3438 struct xfs_bmalloca *ap,
3439 struct xfs_alloc_arg *args,
3440 xfs_extlen_t *blen,
3441 int notinit)
3442{
3443 if (notinit || *blen < ap->minlen) {
3444 /*
3445 * Since we did a BUF_TRYLOCK above, it is possible that
3446 * there is space for this request.
3447 */
3448 args->minlen = ap->minlen;
3449 } else if (*blen < args->maxlen) {
3450 /*
3451 * If the best seen length is less than the request length,
3452 * use the best as the minimum.
3453 */
3454 args->minlen = *blen;
3455 } else {
3456 /*
3457 * Otherwise we've seen an extent as big as maxlen, use that
3458 * as the minimum.
3459 */
3460 args->minlen = args->maxlen;
3461 }
3462}
3463
Nathan Scotta365bdd2006-03-14 13:34:16 +11003464STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003465xfs_bmap_btalloc_nullfb(
3466 struct xfs_bmalloca *ap,
3467 struct xfs_alloc_arg *args,
3468 xfs_extlen_t *blen)
3469{
3470 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003471 xfs_agnumber_t ag, startag;
3472 int notinit = 0;
3473 int error;
3474
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003475 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003476 args->total = ap->total;
3477
Christoph Hellwigc467c042010-02-15 23:34:42 +00003478 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3479 if (startag == NULLAGNUMBER)
3480 startag = ag = 0;
3481
Dave Chinner14b064c2011-01-27 12:16:28 +11003482 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003483 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3484 &notinit);
3485 if (error)
3486 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003487
Christoph Hellwigc467c042010-02-15 23:34:42 +00003488 if (++ag == mp->m_sb.sb_agcount)
3489 ag = 0;
3490 if (ag == startag)
3491 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003492 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003493
3494 xfs_bmap_select_minlen(ap, args, blen, notinit);
3495 return 0;
3496}
3497
3498STATIC int
3499xfs_bmap_btalloc_filestreams(
3500 struct xfs_bmalloca *ap,
3501 struct xfs_alloc_arg *args,
3502 xfs_extlen_t *blen)
3503{
3504 struct xfs_mount *mp = ap->ip->i_mount;
3505 xfs_agnumber_t ag;
3506 int notinit = 0;
3507 int error;
3508
3509 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3510 args->total = ap->total;
3511
3512 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3513 if (ag == NULLAGNUMBER)
3514 ag = 0;
3515
3516 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3517 if (error)
3518 return error;
3519
3520 if (*blen < args->maxlen) {
3521 error = xfs_filestream_new_ag(ap, &ag);
3522 if (error)
3523 return error;
3524
3525 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3526 &notinit);
3527 if (error)
3528 return error;
3529
3530 }
3531
3532 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003533
3534 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003535 * Set the failure fallback case to look in the selected AG as stream
3536 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003537 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003538 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003539 return 0;
3540}
3541
3542STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003543xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003544 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003545{
3546 xfs_mount_t *mp; /* mount point structure */
3547 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003548 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003549 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003550 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003551 xfs_alloc_arg_t args;
3552 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003553 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003554 int nullfb; /* true if ap->firstblock isn't set */
3555 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003556 int tryagain;
3557 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003558 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003559
Dave Chinnera99ebf42011-12-01 11:24:20 +00003560 ASSERT(ap->length);
3561
Nathan Scotta365bdd2006-03-14 13:34:16 +11003562 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003563
3564 /* stripe alignment for allocation is determined by mount parameters */
3565 stripe_align = 0;
3566 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3567 stripe_align = mp->m_swidth;
3568 else if (mp->m_dalign)
3569 stripe_align = mp->m_dalign;
3570
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003571 if (ap->flags & XFS_BMAPI_COWFORK)
3572 align = xfs_get_cowextsz_hint(ap->ip);
3573 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003574 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003575 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003576 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003577 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003578 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003579 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003580 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003581 }
Dave Chinner33177f052013-12-12 16:34:36 +11003582
3583
Dave Chinner0937e0f2011-09-18 20:40:57 +00003584 nullfb = *ap->firstblock == NULLFSBLOCK;
3585 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003586 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003587 if (xfs_alloc_is_userdata(ap->datatype) &&
3588 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003589 ag = xfs_filestream_lookup_ag(ap->ip);
3590 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003591 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003592 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003593 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003594 }
3595 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003596 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003597
3598 xfs_bmap_adjacent(ap);
3599
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003601 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 * it's in the right allocation group.
3603 */
Dave Chinner3a756672011-09-18 20:40:58 +00003604 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 ;
3606 else
Dave Chinner3a756672011-09-18 20:40:58 +00003607 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 * Normal allocation, done through xfs_alloc_vextent.
3610 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003611 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003612 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003613 args.tp = ap->tp;
3614 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003615 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003616 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003617
3618 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003619 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003620 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003621 blen = 0;
3622 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003623 /*
3624 * Search for an allocation group with a single extent large
3625 * enough for the request. If one isn't found, then adjust
3626 * the minimum allocation size to the largest space found.
3627 */
Dave Chinner292378e2016-09-26 08:21:28 +10003628 if (xfs_alloc_is_userdata(ap->datatype) &&
3629 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003630 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3631 else
3632 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003633 if (error)
3634 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003635 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003636 if (xfs_inode_is_filestream(ap->ip))
3637 args.type = XFS_ALLOCTYPE_FIRST_AG;
3638 else
3639 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003640 args.total = args.minlen = ap->minlen;
3641 } else {
3642 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3643 args.total = ap->total;
3644 args.minlen = ap->minlen;
3645 }
David Chinner957d0eb2007-06-18 16:50:37 +10003646 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003647 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003648 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003649 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003650 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003651 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003652 args.prod = 1;
3653 args.mod = 0;
3654 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003655 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003656 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003657 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3658 }
3659 /*
3660 * If we are not low on available data blocks, and the
3661 * underlying logical volume manager is a stripe, and
3662 * the file offset is zero then try to allocate data
3663 * blocks on stripe unit boundary.
3664 * NOTE: ap->aeof is only set if the allocation length
3665 * is >= the stripe unit and the allocation offset is
3666 * at the end of file.
3667 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003668 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003669 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003670 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003671 atype = args.type;
3672 isaligned = 1;
3673 /*
3674 * Adjust for alignment
3675 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003676 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003677 args.minlen = blen - args.alignment;
3678 args.minalignslop = 0;
3679 } else {
3680 /*
3681 * First try an exact bno allocation.
3682 * If it fails then do a near or start bno
3683 * allocation with alignment turned on.
3684 */
3685 atype = args.type;
3686 tryagain = 1;
3687 args.type = XFS_ALLOCTYPE_THIS_BNO;
3688 args.alignment = 1;
3689 /*
3690 * Compute the minlen+alignment for the
3691 * next case. Set slop so that the value
3692 * of minlen+alignment+slop doesn't go up
3693 * between the calls.
3694 */
Dave Chinner33177f052013-12-12 16:34:36 +11003695 if (blen > stripe_align && blen <= args.maxlen)
3696 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003697 else
3698 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003699 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003700 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003701 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003702 args.minlen - 1;
3703 else
3704 args.minalignslop = 0;
3705 }
3706 } else {
3707 args.alignment = 1;
3708 args.minalignslop = 0;
3709 }
3710 args.minleft = ap->minleft;
3711 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003712 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003713 args.datatype = ap->datatype;
3714 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003715 args.ip = ap->ip;
3716
3717 error = xfs_alloc_vextent(&args);
3718 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003719 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003720
Nathan Scotta365bdd2006-03-14 13:34:16 +11003721 if (tryagain && args.fsbno == NULLFSBLOCK) {
3722 /*
3723 * Exact allocation failed. Now try with alignment
3724 * turned on.
3725 */
3726 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003727 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003728 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003729 args.minlen = nextminlen;
3730 args.minalignslop = 0;
3731 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 if ((error = xfs_alloc_vextent(&args)))
3733 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003734 }
3735 if (isaligned && args.fsbno == NULLFSBLOCK) {
3736 /*
3737 * allocation failed, so turn off alignment and
3738 * try again.
3739 */
3740 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003741 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003742 args.alignment = 0;
3743 if ((error = xfs_alloc_vextent(&args)))
3744 return error;
3745 }
3746 if (args.fsbno == NULLFSBLOCK && nullfb &&
3747 args.minlen > ap->minlen) {
3748 args.minlen = ap->minlen;
3749 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003750 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003751 if ((error = xfs_alloc_vextent(&args)))
3752 return error;
3753 }
3754 if (args.fsbno == NULLFSBLOCK && nullfb) {
3755 args.fsbno = 0;
3756 args.type = XFS_ALLOCTYPE_FIRST_AG;
3757 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003758 if ((error = xfs_alloc_vextent(&args)))
3759 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003760 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003761 }
3762 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003763 /*
3764 * check the allocation happened at the same or higher AG than
3765 * the first block that was allocated.
3766 */
3767 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003768 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3769 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003770
Dave Chinner3a756672011-09-18 20:40:58 +00003771 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003772 if (*ap->firstblock == NULLFSBLOCK)
3773 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003774 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003775 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003776 if (!(ap->flags & XFS_BMAPI_COWFORK))
3777 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003778 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3779 if (ap->wasdel)
3780 ap->ip->i_delayed_blks -= args.len;
3781 /*
3782 * Adjust the disk quota also. This was reserved
3783 * earlier.
3784 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003785 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003786 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3787 XFS_TRANS_DQ_BCOUNT,
3788 (long) args.len);
3789 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003790 ap->blkno = NULLFSBLOCK;
3791 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 }
3793 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003794}
3795
3796/*
3797 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3798 * It figures out where to ask the underlying allocator to put the new extent.
3799 */
3800STATIC int
3801xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003802 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003803{
Dave Chinner292378e2016-09-26 08:21:28 +10003804 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3805 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003806 return xfs_bmap_rtalloc(ap);
3807 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808}
3809
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003810/* Trim extent to fit a logical block range. */
3811void
3812xfs_trim_extent(
3813 struct xfs_bmbt_irec *irec,
3814 xfs_fileoff_t bno,
3815 xfs_filblks_t len)
3816{
3817 xfs_fileoff_t distance;
3818 xfs_fileoff_t end = bno + len;
3819
3820 if (irec->br_startoff + irec->br_blockcount <= bno ||
3821 irec->br_startoff >= end) {
3822 irec->br_blockcount = 0;
3823 return;
3824 }
3825
3826 if (irec->br_startoff < bno) {
3827 distance = bno - irec->br_startoff;
3828 if (isnullstartblock(irec->br_startblock))
3829 irec->br_startblock = DELAYSTARTBLOCK;
3830 if (irec->br_startblock != DELAYSTARTBLOCK &&
3831 irec->br_startblock != HOLESTARTBLOCK)
3832 irec->br_startblock += distance;
3833 irec->br_startoff += distance;
3834 irec->br_blockcount -= distance;
3835 }
3836
3837 if (end < irec->br_startoff + irec->br_blockcount) {
3838 distance = irec->br_startoff + irec->br_blockcount - end;
3839 irec->br_blockcount -= distance;
3840 }
3841}
3842
Brian Foster40214d12017-10-13 09:47:46 -07003843/* trim extent to within eof */
3844void
3845xfs_trim_extent_eof(
3846 struct xfs_bmbt_irec *irec,
3847 struct xfs_inode *ip)
3848
3849{
3850 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3851 i_size_read(VFS_I(ip))));
3852}
3853
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003855 * Trim the returned map to the required bounds
3856 */
3857STATIC void
3858xfs_bmapi_trim_map(
3859 struct xfs_bmbt_irec *mval,
3860 struct xfs_bmbt_irec *got,
3861 xfs_fileoff_t *bno,
3862 xfs_filblks_t len,
3863 xfs_fileoff_t obno,
3864 xfs_fileoff_t end,
3865 int n,
3866 int flags)
3867{
3868 if ((flags & XFS_BMAPI_ENTIRE) ||
3869 got->br_startoff + got->br_blockcount <= obno) {
3870 *mval = *got;
3871 if (isnullstartblock(got->br_startblock))
3872 mval->br_startblock = DELAYSTARTBLOCK;
3873 return;
3874 }
3875
3876 if (obno > *bno)
3877 *bno = obno;
3878 ASSERT((*bno >= obno) || (n == 0));
3879 ASSERT(*bno < end);
3880 mval->br_startoff = *bno;
3881 if (isnullstartblock(got->br_startblock))
3882 mval->br_startblock = DELAYSTARTBLOCK;
3883 else
3884 mval->br_startblock = got->br_startblock +
3885 (*bno - got->br_startoff);
3886 /*
3887 * Return the minimum of what we got and what we asked for for
3888 * the length. We can use the len variable here because it is
3889 * modified below and we could have been there before coming
3890 * here if the first part of the allocation didn't overlap what
3891 * was asked for.
3892 */
3893 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3894 got->br_blockcount - (*bno - got->br_startoff));
3895 mval->br_state = got->br_state;
3896 ASSERT(mval->br_blockcount <= len);
3897 return;
3898}
3899
3900/*
3901 * Update and validate the extent map to return
3902 */
3903STATIC void
3904xfs_bmapi_update_map(
3905 struct xfs_bmbt_irec **map,
3906 xfs_fileoff_t *bno,
3907 xfs_filblks_t *len,
3908 xfs_fileoff_t obno,
3909 xfs_fileoff_t end,
3910 int *n,
3911 int flags)
3912{
3913 xfs_bmbt_irec_t *mval = *map;
3914
3915 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3916 ((mval->br_startoff + mval->br_blockcount) <= end));
3917 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3918 (mval->br_startoff < obno));
3919
3920 *bno = mval->br_startoff + mval->br_blockcount;
3921 *len = end - *bno;
3922 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3923 /* update previous map with new information */
3924 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3925 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3926 ASSERT(mval->br_state == mval[-1].br_state);
3927 mval[-1].br_blockcount = mval->br_blockcount;
3928 mval[-1].br_state = mval->br_state;
3929 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3930 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3931 mval[-1].br_startblock != HOLESTARTBLOCK &&
3932 mval->br_startblock == mval[-1].br_startblock +
3933 mval[-1].br_blockcount &&
3934 ((flags & XFS_BMAPI_IGSTATE) ||
3935 mval[-1].br_state == mval->br_state)) {
3936 ASSERT(mval->br_startoff ==
3937 mval[-1].br_startoff + mval[-1].br_blockcount);
3938 mval[-1].br_blockcount += mval->br_blockcount;
3939 } else if (*n > 0 &&
3940 mval->br_startblock == DELAYSTARTBLOCK &&
3941 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3942 mval->br_startoff ==
3943 mval[-1].br_startoff + mval[-1].br_blockcount) {
3944 mval[-1].br_blockcount += mval->br_blockcount;
3945 mval[-1].br_state = mval->br_state;
3946 } else if (!((*n == 0) &&
3947 ((mval->br_startoff + mval->br_blockcount) <=
3948 obno))) {
3949 mval++;
3950 (*n)++;
3951 }
3952 *map = mval;
3953}
3954
3955/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003956 * Map file blocks to filesystem blocks without allocation.
3957 */
3958int
3959xfs_bmapi_read(
3960 struct xfs_inode *ip,
3961 xfs_fileoff_t bno,
3962 xfs_filblks_t len,
3963 struct xfs_bmbt_irec *mval,
3964 int *nmap,
3965 int flags)
3966{
3967 struct xfs_mount *mp = ip->i_mount;
3968 struct xfs_ifork *ifp;
3969 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003970 xfs_fileoff_t obno;
3971 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003972 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003973 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003974 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003975 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003976 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003977
3978 ASSERT(*nmap >= 1);
3979 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003980 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003981 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003982
3983 if (unlikely(XFS_TEST_ERROR(
3984 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3985 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003986 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003987 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003988 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003989 }
3990
3991 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003992 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003993
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003994 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003995
3996 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003997
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003998 /* No CoW fork? Return a hole. */
3999 if (whichfork == XFS_COW_FORK && !ifp) {
4000 mval->br_startoff = bno;
4001 mval->br_startblock = HOLESTARTBLOCK;
4002 mval->br_blockcount = len;
4003 mval->br_state = XFS_EXT_NORM;
4004 *nmap = 1;
4005 return 0;
4006 }
4007
Dave Chinner5c8ed202011-09-18 20:40:45 +00004008 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4009 error = xfs_iread_extents(NULL, ip, whichfork);
4010 if (error)
4011 return error;
4012 }
4013
Christoph Hellwig334f3422016-11-24 11:39:43 +11004014 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
4015 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004016 end = bno + len;
4017 obno = bno;
4018
4019 while (bno < end && n < *nmap) {
4020 /* Reading past eof, act as though there's a hole up to end. */
4021 if (eof)
4022 got.br_startoff = end;
4023 if (got.br_startoff > bno) {
4024 /* Reading in a hole. */
4025 mval->br_startoff = bno;
4026 mval->br_startblock = HOLESTARTBLOCK;
4027 mval->br_blockcount =
4028 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4029 mval->br_state = XFS_EXT_NORM;
4030 bno += mval->br_blockcount;
4031 len -= mval->br_blockcount;
4032 mval++;
4033 n++;
4034 continue;
4035 }
4036
4037 /* set up the extent map to return. */
4038 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4039 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4040
4041 /* If we're done, stop now. */
4042 if (bno >= end || n >= *nmap)
4043 break;
4044
4045 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004046 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4047 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004048 }
4049 *nmap = n;
4050 return 0;
4051}
4052
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004053/*
4054 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4055 * global pool and the extent inserted into the inode in-core extent tree.
4056 *
4057 * On entry, got refers to the first extent beyond the offset of the extent to
4058 * allocate or eof is specified if no such extent exists. On return, got refers
4059 * to the extent record that was inserted to the inode fork.
4060 *
4061 * Note that the allocated extent may have been merged with contiguous extents
4062 * during insertion into the inode fork. Thus, got does not reflect the current
4063 * state of the inode fork on return. If necessary, the caller can use lastx to
4064 * look up the updated record in the inode fork.
4065 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10004066int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004067xfs_bmapi_reserve_delalloc(
4068 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004069 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004070 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004071 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004072 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004073 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004074 xfs_extnum_t *lastx,
4075 int eof)
4076{
4077 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004078 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004079 xfs_extlen_t alen;
4080 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004081 char rt = XFS_IS_REALTIME_INODE(ip);
4082 xfs_extlen_t extsz;
4083 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004084 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004085
Brian Foster974ae922016-11-28 14:57:42 +11004086 /*
4087 * Cap the alloc length. Keep track of prealloc so we know whether to
4088 * tag the inode before we return.
4089 */
4090 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004091 if (!eof)
4092 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004093 if (prealloc && alen >= len)
4094 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004095
4096 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004097 if (whichfork == XFS_COW_FORK)
4098 extsz = xfs_get_cowextsz_hint(ip);
4099 else
4100 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004101 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004102 struct xfs_bmbt_irec prev;
4103
4104 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4105 prev.br_startoff = NULLFILEOFF;
4106
4107 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004108 1, 0, &aoff, &alen);
4109 ASSERT(!error);
4110 }
4111
4112 if (rt)
4113 extsz = alen / mp->m_sb.sb_rextsize;
4114
4115 /*
4116 * Make a transaction-less quota reservation for delayed allocation
4117 * blocks. This number gets adjusted later. We return if we haven't
4118 * allocated blocks already inside this loop.
4119 */
4120 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4121 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4122 if (error)
4123 return error;
4124
4125 /*
4126 * Split changing sb for alen and indlen since they could be coming
4127 * from different places.
4128 */
4129 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4130 ASSERT(indlen > 0);
4131
4132 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004133 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004134 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004135 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004136 }
4137
4138 if (error)
4139 goto out_unreserve_quota;
4140
Dave Chinner0d485ad2015-02-23 21:22:03 +11004141 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004142 if (error)
4143 goto out_unreserve_blocks;
4144
4145
4146 ip->i_delayed_blks += alen;
4147
4148 got->br_startoff = aoff;
4149 got->br_startblock = nullstartblock(indlen);
4150 got->br_blockcount = alen;
4151 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004152
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004153 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004154
Brian Foster974ae922016-11-28 14:57:42 +11004155 /*
4156 * Tag the inode if blocks were preallocated. Note that COW fork
4157 * preallocation can occur at the start or end of the extent, even when
4158 * prealloc == 0, so we must also check the aligned offset and length.
4159 */
4160 if (whichfork == XFS_DATA_FORK && prealloc)
4161 xfs_inode_set_eofblocks_tag(ip);
4162 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4163 xfs_inode_set_cowblocks_tag(ip);
4164
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004165 return 0;
4166
4167out_unreserve_blocks:
4168 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004169 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004170 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004171 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004172out_unreserve_quota:
4173 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004174 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004175 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4176 return error;
4177}
4178
Dave Chinnercf11da92014-07-15 07:08:24 +10004179static int
4180xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004181 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004182{
4183 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004184 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004185 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004186 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004187 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004188
Dave Chinnera99ebf42011-12-01 11:24:20 +00004189 ASSERT(bma->length > 0);
4190
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004191 /*
4192 * For the wasdelay case, we could also just allocate the stuff asked
4193 * for in this bmap call but that wouldn't be as good.
4194 */
4195 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004196 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4197 bma->offset = bma->got.br_startoff;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004198 if (bma->idx)
4199 xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004200 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004201 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004202 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004203 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004204 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004205 }
4206
4207 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004208 * Set the data type being allocated. For the data fork, the first data
4209 * in the file is treated differently to all other allocations. For the
4210 * attribute fork, we only need to ensure the allocated range is not on
4211 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004212 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004213 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004214 bma->datatype = XFS_ALLOC_NOBUSY;
4215 if (whichfork == XFS_DATA_FORK) {
4216 if (bma->offset == 0)
4217 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4218 else
4219 bma->datatype |= XFS_ALLOC_USERDATA;
4220 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004221 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004222 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004223 }
4224
Dave Chinnere04426b2012-10-05 11:06:59 +10004225 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004226
4227 /*
4228 * Only want to do the alignment at the eof if it is userdata and
4229 * allocation length is larger than a stripe unit.
4230 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004231 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004232 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004233 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004234 if (error)
4235 return error;
4236 }
4237
4238 error = xfs_bmap_alloc(bma);
4239 if (error)
4240 return error;
4241
Dave Chinner29c8d172011-09-18 20:41:00 +00004242 if (bma->cur)
4243 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004244 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004245 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004246 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4247 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4248 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004249 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004250 }
4251 /*
4252 * Bump the number of extents we've allocated
4253 * in this call.
4254 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004255 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004256
Dave Chinner29c8d172011-09-18 20:41:00 +00004257 if (bma->cur)
4258 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004259 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4260
Dave Chinner963c30c2011-09-18 20:40:59 +00004261 bma->got.br_startoff = bma->offset;
4262 bma->got.br_startblock = bma->blkno;
4263 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004264 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004265
4266 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004267 * In the data fork, a wasdelay extent has been initialized, so
4268 * shouldn't be flagged as unwritten.
4269 *
4270 * For the cow fork, however, we convert delalloc reservations
4271 * (extents allocated for speculative preallocation) to
4272 * allocated unwritten extents, and only convert the unwritten
4273 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004274 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004275 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4276 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004277 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004278 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004279
Christoph Hellwigc6534242011-09-18 20:41:05 +00004280 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004281 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004282 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004283 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4284 whichfork, &bma->idx, &bma->cur, &bma->got,
4285 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004286
Christoph Hellwigc315c902011-09-18 20:41:02 +00004287 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004288 if (error)
4289 return error;
4290
4291 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004292 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4293 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4294 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004295 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004296 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004297
Dave Chinner963c30c2011-09-18 20:40:59 +00004298 ASSERT(bma->got.br_startoff <= bma->offset);
4299 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4300 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004301 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4302 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004303 return 0;
4304}
4305
Dave Chinnerb447fe52011-09-18 20:40:51 +00004306STATIC int
4307xfs_bmapi_convert_unwritten(
4308 struct xfs_bmalloca *bma,
4309 struct xfs_bmbt_irec *mval,
4310 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004311 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004312{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004313 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004314 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004315 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004316 int error;
4317
Dave Chinnerb447fe52011-09-18 20:40:51 +00004318 /* check if we need to do unwritten->real conversion */
4319 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4320 (flags & XFS_BMAPI_PREALLOC))
4321 return 0;
4322
4323 /* check if we need to do real->unwritten conversion */
4324 if (mval->br_state == XFS_EXT_NORM &&
4325 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4326 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4327 return 0;
4328
4329 /*
4330 * Modify (by adding) the state flag, if writing.
4331 */
4332 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004333 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4334 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004335 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004336 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004337 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004338 }
4339 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4340 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4341
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004342 /*
4343 * Before insertion into the bmbt, zero the range being converted
4344 * if required.
4345 */
4346 if (flags & XFS_BMAPI_ZERO) {
4347 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4348 mval->br_blockcount);
4349 if (error)
4350 return error;
4351 }
4352
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004353 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4354 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004355 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004356 /*
4357 * Log the inode core unconditionally in the unwritten extent conversion
4358 * path because the conversion might not have done so (e.g., if the
4359 * extent count hasn't changed). We need to make sure the inode is dirty
4360 * in the transaction for the sake of fsync(), even if nothing has
4361 * changed, because fsync() will not force the log for this transaction
4362 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004363 *
4364 * Note: If we're only converting cow fork extents, there aren't
4365 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004366 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004367 if (whichfork != XFS_COW_FORK)
4368 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004369 if (error)
4370 return error;
4371
4372 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004373 * Update our extent pointer, given that
4374 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4375 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004376 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004377 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004378
4379 /*
4380 * We may have combined previously unwritten space with written space,
4381 * so generate another request.
4382 */
4383 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004384 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004385 return 0;
4386}
4387
Christoph Hellwig44032802011-09-18 20:40:48 +00004388/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004389 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4390 * extent state if necessary. Details behaviour is controlled by the flags
4391 * parameter. Only allocates blocks from a single allocation group, to avoid
4392 * locking problems.
4393 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 * The returned value in "firstblock" from the first call in a transaction
4395 * must be remembered and presented to subsequent calls in "firstblock".
4396 * An upper bound for the number of blocks to be allocated is supplied to
4397 * the first call in "total"; if no allocation group has that many free
4398 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4399 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004400int
4401xfs_bmapi_write(
4402 struct xfs_trans *tp, /* transaction pointer */
4403 struct xfs_inode *ip, /* incore inode */
4404 xfs_fileoff_t bno, /* starting file offs. mapped */
4405 xfs_filblks_t len, /* length to map in file */
4406 int flags, /* XFS_BMAPI_... */
4407 xfs_fsblock_t *firstblock, /* first allocated block
4408 controls a.g. for allocs */
4409 xfs_extlen_t total, /* total blocks needed */
4410 struct xfs_bmbt_irec *mval, /* output: map values */
4411 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004412 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004414 struct xfs_mount *mp = ip->i_mount;
4415 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004416 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004417 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004418 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004419 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004420 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004421 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004422 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004423
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004425 xfs_fileoff_t orig_bno; /* original block number value */
4426 int orig_flags; /* original flags arg value */
4427 xfs_filblks_t orig_len; /* original value of len arg */
4428 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4429 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
4431 orig_bno = bno;
4432 orig_len = len;
4433 orig_flags = flags;
4434 orig_mval = mval;
4435 orig_nmap = *nmap;
4436#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004437 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004438
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004440 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4441 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004442 ASSERT(tp != NULL ||
4443 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4444 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004445 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004446 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004447 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004448 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004449
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004450 /* zeroing is for currently only for data extents, not metadata */
4451 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4452 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4453 /*
4454 * we can allocate unwritten extents or pre-zero allocated blocks,
4455 * but it makes no sense to do both at once. This would result in
4456 * zeroing the unwritten extent twice, but it still being an
4457 * unwritten extent....
4458 */
4459 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4460 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 if (unlikely(XFS_TEST_ERROR(
4463 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004464 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004465 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004466 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004467 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004469
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004471 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004472
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004474
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004475 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004476
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004477 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004479 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004481 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004482 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004483 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004484 }
4485
4486 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4487 error = xfs_iread_extents(tp, ip, whichfork);
4488 if (error)
4489 goto error0;
4490 }
4491
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 n = 0;
4493 end = bno + len;
4494 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004495
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004496 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4497 eof = true;
4498 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4499 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004500 bma.tp = tp;
4501 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004502 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004503 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004504 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004505 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004506
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004508 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004509
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004510 /* in hole or beyoned EOF? */
4511 if (eof || bma.got.br_startoff > bno) {
4512 if (flags & XFS_BMAPI_DELALLOC) {
4513 /*
4514 * For the COW fork we can reasonably get a
4515 * request for converting an extent that races
4516 * with other threads already having converted
4517 * part of it, as there converting COW to
4518 * regular blocks is not protected using the
4519 * IOLOCK.
4520 */
4521 ASSERT(flags & XFS_BMAPI_COWFORK);
4522 if (!(flags & XFS_BMAPI_COWFORK)) {
4523 error = -EIO;
4524 goto error0;
4525 }
4526
4527 if (eof || bno >= end)
4528 break;
4529 } else {
4530 need_alloc = true;
4531 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004532 } else if (isnullstartblock(bma.got.br_startblock)) {
4533 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004534 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004535
4536 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 * First, deal with the hole before the allocated space
4538 * that we found, if any.
4539 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004540 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004541 bma.eof = eof;
4542 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4543 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004544 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004545 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004546
Dave Chinnera99ebf42011-12-01 11:24:20 +00004547 /*
4548 * There's a 32/64 bit type mismatch between the
4549 * allocation length request (which can be 64 bits in
4550 * length) and the bma length request, which is
4551 * xfs_extlen_t and therefore 32 bits. Hence we have to
4552 * check for 32-bit overflows and handle them here.
4553 */
4554 if (len > (xfs_filblks_t)MAXEXTLEN)
4555 bma.length = MAXEXTLEN;
4556 else
4557 bma.length = len;
4558
4559 ASSERT(len > 0);
4560 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004561 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 if (error)
4563 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004564 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004565 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004566
4567 /*
4568 * If this is a CoW allocation, record the data in
4569 * the refcount btree for orphan recovery.
4570 */
4571 if (whichfork == XFS_COW_FORK) {
4572 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4573 bma.blkno, bma.length);
4574 if (error)
4575 goto error0;
4576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004578
Dave Chinneraef9a892011-09-18 20:40:44 +00004579 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004580 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4581 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
Dave Chinnerb447fe52011-09-18 20:40:51 +00004583 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004584 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004585 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004586 continue;
4587 if (error)
4588 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
Dave Chinneraef9a892011-09-18 20:40:44 +00004590 /* update the extent map to return */
4591 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 /*
4594 * If we're done, stop now. Stop when we've allocated
4595 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4596 * the transaction may get too big.
4597 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004598 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004600
4601 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004602 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004603 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4604 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 /*
4609 * Transform from btree to extents, give it cur.
4610 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004611 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004612 int tmp_logflags = 0;
4613
Dave Chinner29c8d172011-09-18 20:41:00 +00004614 ASSERT(bma.cur);
4615 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004617 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 if (error)
4619 goto error0;
4620 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004623 XFS_IFORK_NEXTENTS(ip, whichfork) >
4624 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626error0:
4627 /*
4628 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004629 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004631 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004633 bma.logflags &= ~xfs_ilog_fext(whichfork);
4634 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004636 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 /*
4638 * Log whatever the flags say, even if error. Otherwise we might miss
4639 * detecting a case where the data is changed, there's an error,
4640 * and it's not logged so we don't shutdown when we should.
4641 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004642 if (bma.logflags)
4643 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004644
Dave Chinner29c8d172011-09-18 20:41:00 +00004645 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 if (!error) {
4647 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004648 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004650 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004651 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004653 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4655 }
4656 if (!error)
4657 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4658 orig_nmap, *nmap);
4659 return error;
4660}
4661
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004662static int
4663xfs_bmapi_remap(
4664 struct xfs_trans *tp,
4665 struct xfs_inode *ip,
4666 xfs_fileoff_t bno,
4667 xfs_filblks_t len,
4668 xfs_fsblock_t startblock,
4669 struct xfs_defer_ops *dfops)
4670{
4671 struct xfs_mount *mp = ip->i_mount;
4672 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4673 struct xfs_btree_cur *cur = NULL;
4674 xfs_fsblock_t firstblock = NULLFSBLOCK;
4675 struct xfs_bmbt_irec got;
4676 xfs_extnum_t idx;
4677 int logflags = 0, error;
4678
4679 ASSERT(len > 0);
4680 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4681 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4682
4683 if (unlikely(XFS_TEST_ERROR(
4684 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4685 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004686 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004687 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4688 return -EFSCORRUPTED;
4689 }
4690
4691 if (XFS_FORCED_SHUTDOWN(mp))
4692 return -EIO;
4693
4694 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4695 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4696 if (error)
4697 return error;
4698 }
4699
4700 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4701 /* make sure we only reflink into a hole. */
4702 ASSERT(got.br_startoff > bno);
4703 ASSERT(got.br_startoff - bno >= len);
4704 }
4705
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004706 ip->i_d.di_nblocks += len;
4707 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004708
4709 if (ifp->if_flags & XFS_IFBROOT) {
4710 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4711 cur->bc_private.b.firstblock = firstblock;
4712 cur->bc_private.b.dfops = dfops;
4713 cur->bc_private.b.flags = 0;
4714 }
4715
4716 got.br_startoff = bno;
4717 got.br_startblock = startblock;
4718 got.br_blockcount = len;
4719 got.br_state = XFS_EXT_NORM;
4720
4721 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4722 &got, &firstblock, dfops, &logflags);
4723 if (error)
4724 goto error0;
4725
4726 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4727 int tmp_logflags = 0;
4728
4729 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4730 &tmp_logflags, XFS_DATA_FORK);
4731 logflags |= tmp_logflags;
4732 }
4733
4734error0:
4735 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4736 logflags &= ~XFS_ILOG_DEXT;
4737 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4738 logflags &= ~XFS_ILOG_DBROOT;
4739
4740 if (logflags)
4741 xfs_trans_log_inode(tp, ip, logflags);
4742 if (cur) {
4743 xfs_btree_del_cursor(cur,
4744 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4745 }
4746 return error;
4747}
4748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004750 * When a delalloc extent is split (e.g., due to a hole punch), the original
4751 * indlen reservation must be shared across the two new extents that are left
4752 * behind.
4753 *
4754 * Given the original reservation and the worst case indlen for the two new
4755 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004756 * reservation fairly across the two new extents. If necessary, steal available
4757 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4758 * ores == 1). The number of stolen blocks is returned. The availability and
4759 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004760 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004761static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004762xfs_bmap_split_indlen(
4763 xfs_filblks_t ores, /* original res. */
4764 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004765 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4766 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004767{
4768 xfs_filblks_t len1 = *indlen1;
4769 xfs_filblks_t len2 = *indlen2;
4770 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004771 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004772 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004773
4774 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004775 * Steal as many blocks as we can to try and satisfy the worst case
4776 * indlen for both new extents.
4777 */
Brian Foster75d65362017-02-13 22:48:30 -08004778 if (ores < nres && avail)
4779 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4780 ores += stolen;
4781
4782 /* nothing else to do if we've satisfied the new reservation */
4783 if (ores >= nres)
4784 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004785
4786 /*
Brian Foster75d65362017-02-13 22:48:30 -08004787 * We can't meet the total required reservation for the two extents.
4788 * Calculate the percent of the overall shortage between both extents
4789 * and apply this percentage to each of the requested indlen values.
4790 * This distributes the shortage fairly and reduces the chances that one
4791 * of the two extents is left with nothing when extents are repeatedly
4792 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004793 */
Brian Foster75d65362017-02-13 22:48:30 -08004794 resfactor = (ores * 100);
4795 do_div(resfactor, nres);
4796 len1 *= resfactor;
4797 do_div(len1, 100);
4798 len2 *= resfactor;
4799 do_div(len2, 100);
4800 ASSERT(len1 + len2 <= ores);
4801 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4802
4803 /*
4804 * Hand out the remainder to each extent. If one of the two reservations
4805 * is zero, we want to make sure that one gets a block first. The loop
4806 * below starts with len1, so hand len2 a block right off the bat if it
4807 * is zero.
4808 */
4809 ores -= (len1 + len2);
4810 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4811 if (ores && !len2 && *indlen2) {
4812 len2++;
4813 ores--;
4814 }
4815 while (ores) {
4816 if (len1 < *indlen1) {
4817 len1++;
4818 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004819 }
Brian Foster75d65362017-02-13 22:48:30 -08004820 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004821 break;
Brian Foster75d65362017-02-13 22:48:30 -08004822 if (len2 < *indlen2) {
4823 len2++;
4824 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004825 }
4826 }
4827
4828 *indlen1 = len1;
4829 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004830
4831 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004832}
4833
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004834int
4835xfs_bmap_del_extent_delay(
4836 struct xfs_inode *ip,
4837 int whichfork,
4838 xfs_extnum_t *idx,
4839 struct xfs_bmbt_irec *got,
4840 struct xfs_bmbt_irec *del)
4841{
4842 struct xfs_mount *mp = ip->i_mount;
4843 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4844 struct xfs_bmbt_irec new;
4845 int64_t da_old, da_new, da_diff = 0;
4846 xfs_fileoff_t del_endoff, got_endoff;
4847 xfs_filblks_t got_indlen, new_indlen, stolen;
4848 int error = 0, state = 0;
4849 bool isrt;
4850
4851 XFS_STATS_INC(mp, xs_del_exlist);
4852
4853 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4854 del_endoff = del->br_startoff + del->br_blockcount;
4855 got_endoff = got->br_startoff + got->br_blockcount;
4856 da_old = startblockval(got->br_startblock);
4857 da_new = 0;
4858
4859 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004860 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004861 ASSERT(del->br_blockcount > 0);
4862 ASSERT(got->br_startoff <= del->br_startoff);
4863 ASSERT(got_endoff >= del_endoff);
4864
4865 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004866 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004867
4868 do_div(rtexts, mp->m_sb.sb_rextsize);
4869 xfs_mod_frextents(mp, rtexts);
4870 }
4871
4872 /*
4873 * Update the inode delalloc counter now and wait to update the
4874 * sb counters as we might have to borrow some blocks for the
4875 * indirect block accounting.
4876 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004877 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4878 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004879 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004880 if (error)
4881 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004882 ip->i_delayed_blks -= del->br_blockcount;
4883
4884 if (whichfork == XFS_COW_FORK)
4885 state |= BMAP_COWFORK;
4886
4887 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004888 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004889 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004890 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004891
Christoph Hellwig0173c682017-10-17 14:16:22 -07004892 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4893 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004894 /*
4895 * Matches the whole extent. Delete the entry.
4896 */
4897 xfs_iext_remove(ip, *idx, 1, state);
4898 --*idx;
4899 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004900 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004901 /*
4902 * Deleting the first part of the extent.
4903 */
4904 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4905 got->br_startoff = del_endoff;
4906 got->br_blockcount -= del->br_blockcount;
4907 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4908 got->br_blockcount), da_old);
4909 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004910 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004911 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4912 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004913 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004914 /*
4915 * Deleting the last part of the extent.
4916 */
4917 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4918 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4919 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4920 got->br_blockcount), da_old);
4921 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004922 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004923 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4924 break;
4925 case 0:
4926 /*
4927 * Deleting the middle of the extent.
4928 *
4929 * Distribute the original indlen reservation across the two new
4930 * extents. Steal blocks from the deleted extent if necessary.
4931 * Stealing blocks simply fudges the fdblocks accounting below.
4932 * Warn if either of the new indlen reservations is zero as this
4933 * can lead to delalloc problems.
4934 */
4935 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4936
4937 got->br_blockcount = del->br_startoff - got->br_startoff;
4938 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4939
4940 new.br_blockcount = got_endoff - del_endoff;
4941 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4942
4943 WARN_ON_ONCE(!got_indlen || !new_indlen);
4944 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4945 del->br_blockcount);
4946
4947 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004948 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004949 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4950
4951 new.br_startoff = del_endoff;
4952 new.br_state = got->br_state;
4953 new.br_startblock = nullstartblock((int)new_indlen);
4954
4955 ++*idx;
4956 xfs_iext_insert(ip, *idx, 1, &new, state);
4957
4958 da_new = got_indlen + new_indlen - stolen;
4959 del->br_blockcount -= stolen;
4960 break;
4961 }
4962
4963 ASSERT(da_old >= da_new);
4964 da_diff = da_old - da_new;
4965 if (!isrt)
4966 da_diff += del->br_blockcount;
4967 if (da_diff)
4968 xfs_mod_fdblocks(mp, da_diff, false);
4969 return error;
4970}
4971
4972void
4973xfs_bmap_del_extent_cow(
4974 struct xfs_inode *ip,
4975 xfs_extnum_t *idx,
4976 struct xfs_bmbt_irec *got,
4977 struct xfs_bmbt_irec *del)
4978{
4979 struct xfs_mount *mp = ip->i_mount;
4980 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4981 struct xfs_bmbt_irec new;
4982 xfs_fileoff_t del_endoff, got_endoff;
4983 int state = BMAP_COWFORK;
4984
4985 XFS_STATS_INC(mp, xs_del_exlist);
4986
4987 del_endoff = del->br_startoff + del->br_blockcount;
4988 got_endoff = got->br_startoff + got->br_blockcount;
4989
4990 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004991 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004992 ASSERT(del->br_blockcount > 0);
4993 ASSERT(got->br_startoff <= del->br_startoff);
4994 ASSERT(got_endoff >= del_endoff);
4995 ASSERT(!isnullstartblock(got->br_startblock));
4996
4997 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004998 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004999 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07005000 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005001
Christoph Hellwig0173c682017-10-17 14:16:22 -07005002 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5003 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005004 /*
5005 * Matches the whole extent. Delete the entry.
5006 */
5007 xfs_iext_remove(ip, *idx, 1, state);
5008 --*idx;
5009 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07005010 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005011 /*
5012 * Deleting the first part of the extent.
5013 */
5014 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5015 got->br_startoff = del_endoff;
5016 got->br_blockcount -= del->br_blockcount;
5017 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005018 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005019 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5020 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07005021 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005022 /*
5023 * Deleting the last part of the extent.
5024 */
5025 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5026 got->br_blockcount -= del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005027 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005028 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5029 break;
5030 case 0:
5031 /*
5032 * Deleting the middle of the extent.
5033 */
5034 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
5035 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07005036 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11005037 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5038
5039 new.br_startoff = del_endoff;
5040 new.br_blockcount = got_endoff - del_endoff;
5041 new.br_state = got->br_state;
5042 new.br_startblock = del->br_startblock + del->br_blockcount;
5043
5044 ++*idx;
5045 xfs_iext_insert(ip, *idx, 1, &new, state);
5046 break;
5047 }
5048}
5049
Brian Fostera9bd24a2016-03-15 11:42:46 +11005050/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005051 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07005052 * after removing space.
Dave Chinner9e5987a2013-02-25 12:31:26 +11005053 */
5054STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005055xfs_bmap_del_extent_real(
Dave Chinner9e5987a2013-02-25 12:31:26 +11005056 xfs_inode_t *ip, /* incore inode pointer */
5057 xfs_trans_t *tp, /* current transaction pointer */
5058 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005059 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005060 xfs_btree_cur_t *cur, /* if null, not a btree */
5061 xfs_bmbt_irec_t *del, /* data to remove from extents */
5062 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005063 int whichfork, /* data or attr fork */
5064 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005065{
Dave Chinner9e5987a2013-02-25 12:31:26 +11005066 xfs_fsblock_t del_endblock=0; /* first block past del */
5067 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005068 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005069 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005070 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005071 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005072 xfs_fileoff_t got_endoff; /* first offset past got */
5073 int i; /* temp state */
5074 xfs_ifork_t *ifp; /* inode fork pointer */
5075 xfs_mount_t *mp; /* mount structure */
5076 xfs_filblks_t nblks; /* quota/sb block count */
5077 xfs_bmbt_irec_t new; /* new record to be inserted */
5078 /* REFERENCED */
5079 uint qfield; /* quota field to update */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005080 int state = 0;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005081 struct xfs_bmbt_irec old;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005082
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005083 mp = ip->i_mount;
5084 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005085
5086 if (whichfork == XFS_ATTR_FORK)
5087 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005088 else if (whichfork == XFS_COW_FORK)
5089 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005090
Dave Chinner9e5987a2013-02-25 12:31:26 +11005091 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005092 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005093 ASSERT(del->br_blockcount > 0);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005094 xfs_iext_get_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005095 ASSERT(got.br_startoff <= del->br_startoff);
5096 del_endoff = del->br_startoff + del->br_blockcount;
5097 got_endoff = got.br_startoff + got.br_blockcount;
5098 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005099 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005100 qfield = 0;
5101 error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005102
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005103 /*
5104 * If it's the case where the directory code is running with no block
5105 * reservation, and the deleted block is in the middle of its extent,
5106 * and the resulting insert of an extent would cause transformation to
5107 * btree format, then reject it. The calling code will then swap blocks
5108 * around instead. We have to do this now, rather than waiting for the
5109 * conversion to btree format, since the transaction will be dirty then.
5110 */
5111 if (tp->t_blk_res == 0 &&
5112 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5113 XFS_IFORK_NEXTENTS(ip, whichfork) >=
5114 XFS_IFORK_MAXEXT(ip, whichfork) &&
5115 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5116 return -ENOSPC;
5117
5118 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005119 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5120 xfs_fsblock_t bno;
5121 xfs_filblks_t len;
5122
5123 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
5124 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
5125 bno = del->br_startblock;
5126 len = del->br_blockcount;
5127 do_div(bno, mp->m_sb.sb_rextsize);
5128 do_div(len, mp->m_sb.sb_rextsize);
5129 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5130 if (error)
5131 goto done;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005132 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005133 nblks = len * mp->m_sb.sb_rextsize;
5134 qfield = XFS_TRANS_DQ_RTBCOUNT;
5135 } else {
5136 do_fx = 1;
5137 nblks = del->br_blockcount;
5138 qfield = XFS_TRANS_DQ_BCOUNT;
5139 }
5140
5141 del_endblock = del->br_startblock + del->br_blockcount;
5142 if (cur) {
5143 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5144 got.br_startblock, got.br_blockcount, &i);
5145 if (error)
5146 goto done;
5147 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005148 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005149
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005150 if (got.br_startoff == del->br_startoff)
5151 state |= BMAP_LEFT_FILLING;
5152 if (got_endoff == del_endoff)
5153 state |= BMAP_RIGHT_FILLING;
5154
5155 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5156 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11005157 /*
5158 * Matches the whole extent. Delete the entry.
5159 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005160 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005161 xfs_iext_remove(ip, *idx, 1,
5162 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5163 --*idx;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005164
5165 XFS_IFORK_NEXT_SET(ip, whichfork,
5166 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5167 flags |= XFS_ILOG_CORE;
5168 if (!cur) {
5169 flags |= xfs_ilog_fext(whichfork);
5170 break;
5171 }
5172 if ((error = xfs_btree_delete(cur, &i)))
5173 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005174 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005175 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005176 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11005177 /*
5178 * Deleting the first part of the extent.
5179 */
5180 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005181 got.br_startoff = del_endoff;
5182 got.br_startblock = del_endblock;
5183 got.br_blockcount -= del->br_blockcount;
5184 xfs_iext_update_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005185 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5186 if (!cur) {
5187 flags |= xfs_ilog_fext(whichfork);
5188 break;
5189 }
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005190 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5191 got.br_blockcount, got.br_state);
5192 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005193 goto done;
5194 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005195 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11005196 /*
5197 * Deleting the last part of the extent.
5198 */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005199 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005200 got.br_blockcount -= del->br_blockcount;
5201 xfs_iext_update_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005202 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5203 if (!cur) {
5204 flags |= xfs_ilog_fext(whichfork);
5205 break;
5206 }
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005207 error = xfs_bmbt_update(cur, got.br_startoff, got.br_startblock,
5208 got.br_blockcount, got.br_state);
5209 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005210 goto done;
5211 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005212 case 0:
5213 /*
5214 * Deleting the middle of the extent.
5215 */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005216 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005217
5218 old = got;
5219 got.br_blockcount = del->br_startoff - got.br_startoff;
5220 xfs_iext_update_extent(ifp, *idx, &got);
5221
Dave Chinner9e5987a2013-02-25 12:31:26 +11005222 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005223 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005224 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005225 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005226
Christoph Hellwige1d75532017-10-17 14:16:21 -07005227 flags |= XFS_ILOG_CORE;
5228 if (cur) {
5229 error = xfs_bmbt_update(cur, got.br_startoff,
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005230 got.br_startblock, got.br_blockcount,
Christoph Hellwige1d75532017-10-17 14:16:21 -07005231 got.br_state);
5232 if (error)
5233 goto done;
5234 error = xfs_btree_increment(cur, 0, &i);
5235 if (error)
5236 goto done;
5237 cur->bc_rec.b = new;
5238 error = xfs_btree_insert(cur, &i);
5239 if (error && error != -ENOSPC)
5240 goto done;
5241 /*
5242 * If get no-space back from btree insert, it tried a
5243 * split, and we have a zero block reservation. Fix up
5244 * our state and return the error.
5245 */
5246 if (error == -ENOSPC) {
5247 /*
5248 * Reset the cursor, don't trust it after any
5249 * insert operation.
5250 */
5251 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005252 got.br_startblock,
5253 got.br_blockcount, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005254 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005255 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005256 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5257 /*
5258 * Update the btree record back
5259 * to the original value.
5260 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005261 error = xfs_bmbt_update(cur, old.br_startoff,
5262 old.br_startblock,
5263 old.br_blockcount,
5264 old.br_state);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005265 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005266 goto done;
5267 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005268 * Reset the extent record back
5269 * to the original value.
Dave Chinner9e5987a2013-02-25 12:31:26 +11005270 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005271 xfs_iext_update_extent(ifp, *idx, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005272 flags = 0;
5273 error = -ENOSPC;
5274 goto done;
5275 }
5276 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5277 } else
5278 flags |= xfs_ilog_fext(whichfork);
5279 XFS_IFORK_NEXT_SET(ip, whichfork,
5280 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005281 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5282 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5283 ++*idx;
5284 break;
5285 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005286
5287 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005288 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5289 if (error)
5290 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005291
Dave Chinner9e5987a2013-02-25 12:31:26 +11005292 /*
5293 * If we need to, add to list of extents to delete.
5294 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005295 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005296 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5297 error = xfs_refcount_decrease_extent(mp, dfops, del);
5298 if (error)
5299 goto done;
5300 } else
5301 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5302 del->br_blockcount, NULL);
5303 }
5304
Dave Chinner9e5987a2013-02-25 12:31:26 +11005305 /*
5306 * Adjust inode # blocks in the file.
5307 */
5308 if (nblks)
5309 ip->i_d.di_nblocks -= nblks;
5310 /*
5311 * Adjust quota data.
5312 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005313 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005314 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5315
Dave Chinner9e5987a2013-02-25 12:31:26 +11005316done:
5317 *logflagsp = flags;
5318 return error;
5319}
5320
5321/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 * Unmap (remove) blocks from a file.
5323 * If nexts is nonzero then the number of extents to remove is limited to
5324 * that value. If not all extents in the block range can be removed then
5325 * *done is set.
5326 */
5327int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005328__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 xfs_trans_t *tp, /* transaction pointer */
5330 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005331 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005332 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 int flags, /* misc flags */
5334 xfs_extnum_t nexts, /* number of extents max */
5335 xfs_fsblock_t *firstblock, /* first allocated block
5336 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005337 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338{
5339 xfs_btree_cur_t *cur; /* bmap btree cursor */
5340 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 int error; /* error return value */
5342 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005343 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 xfs_ifork_t *ifp; /* inode fork pointer */
5345 int isrt; /* freeing in rt area */
5346 xfs_extnum_t lastx; /* last extent index used */
5347 int logflags; /* transaction logging flags */
5348 xfs_extlen_t mod; /* rt extent offset */
5349 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005350 int tmp_logflags; /* partial logging flags */
5351 int wasdel; /* was a delayed alloc extent */
5352 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005353 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005354 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005355 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005356 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005357 xfs_fileoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005359 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005360
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005361 whichfork = xfs_bmapi_whichfork(flags);
5362 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 ifp = XFS_IFORK_PTR(ip, whichfork);
5364 if (unlikely(
5365 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5366 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5367 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5368 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005369 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 }
5371 mp = ip->i_mount;
5372 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005373 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005374
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005375 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 ASSERT(len > 0);
5377 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005378
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005379 /*
5380 * Guesstimate how many blocks we can unmap without running the risk of
5381 * blowing out the transaction with a mix of EFIs and reflink
5382 * adjustments.
5383 */
5384 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5385 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5386 else
5387 max_len = len;
5388
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5390 (error = xfs_iread_extents(tp, ip, whichfork)))
5391 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005392 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005393 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 return 0;
5395 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005396 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005397 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005398 end = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005399
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 /*
5401 * Check to see if the given block number is past the end of the
5402 * file, back up to the last block if so...
5403 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005404 if (!xfs_iext_lookup_extent(ip, ifp, end, &lastx, &got)) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005405 ASSERT(lastx > 0);
5406 xfs_iext_get_extent(ifp, --lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005407 end = got.br_startoff + got.br_blockcount - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005409
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 logflags = 0;
5411 if (ifp->if_flags & XFS_IFBROOT) {
5412 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005413 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005415 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416 cur->bc_private.b.flags = 0;
5417 } else
5418 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005419
5420 if (isrt) {
5421 /*
5422 * Synchronize by locking the bitmap inode.
5423 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005424 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005425 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005426 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5427 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005428 }
5429
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 extno = 0;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005431 while (end != (xfs_fileoff_t)-1 && end >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005432 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005434 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 * Just back up to the previous extent, if so.
5436 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005437 if (got.br_startoff > end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005438 if (--lastx < 0)
5439 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005440 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005441 }
5442 /*
5443 * Is the last block of this extent before the range
5444 * we're supposed to delete? If so, we're done.
5445 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005446 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005448 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 break;
5450 /*
5451 * Then deal with the (possibly delayed) allocated space
5452 * we found.
5453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005455 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005456
5457 /*
5458 * Make sure we don't touch multiple AGF headers out of order
5459 * in a single transaction, as that could cause AB-BA deadlocks.
5460 */
5461 if (!wasdel) {
5462 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5463 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5464 break;
5465 prev_agno = agno;
5466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005467 if (got.br_startoff < start) {
5468 del.br_startoff = start;
5469 del.br_blockcount -= start - got.br_startoff;
5470 if (!wasdel)
5471 del.br_startblock += start - got.br_startoff;
5472 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005473 if (del.br_startoff + del.br_blockcount > end + 1)
5474 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005475
5476 /* How much can we safely unmap? */
5477 if (max_len < del.br_blockcount) {
5478 del.br_startoff += del.br_blockcount - max_len;
5479 if (!wasdel)
5480 del.br_startblock += del.br_blockcount - max_len;
5481 del.br_blockcount = max_len;
5482 }
5483
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 sum = del.br_startblock + del.br_blockcount;
5485 if (isrt &&
5486 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5487 /*
5488 * Realtime extent not lined up at the end.
5489 * The extent could have been split into written
5490 * and unwritten pieces, or we could just be
5491 * unmapping part of it. But we can't really
5492 * get rid of part of a realtime extent.
5493 */
5494 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005495 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005496 /*
5497 * This piece is unwritten, or we're not
5498 * using unwritten extents. Skip over it.
5499 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005500 ASSERT(end >= mod);
5501 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502 del.br_blockcount : mod;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005503 if (end < got.br_startoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 if (--lastx >= 0)
Christoph Hellwige3f0f752017-10-17 14:16:20 -07005505 xfs_iext_get_extent(ifp, lastx,
5506 &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005507 }
5508 continue;
5509 }
5510 /*
5511 * It's written, turn it unwritten.
5512 * This is better than zeroing it.
5513 */
5514 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005515 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 /*
5517 * If this spans a realtime extent boundary,
5518 * chop it back to the start of the one we end at.
5519 */
5520 if (del.br_blockcount > mod) {
5521 del.br_startoff += del.br_blockcount - mod;
5522 del.br_startblock += del.br_blockcount - mod;
5523 del.br_blockcount = mod;
5524 }
5525 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005526 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005527 whichfork, &lastx, &cur, &del,
5528 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005529 if (error)
5530 goto error0;
5531 goto nodelete;
5532 }
5533 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5534 /*
5535 * Realtime extent is lined up at the end but not
5536 * at the front. We'll get rid of full extents if
5537 * we can.
5538 */
5539 mod = mp->m_sb.sb_rextsize - mod;
5540 if (del.br_blockcount > mod) {
5541 del.br_blockcount -= mod;
5542 del.br_startoff += mod;
5543 del.br_startblock += mod;
5544 } else if ((del.br_startoff == start &&
5545 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005546 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005547 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005548 /*
5549 * Can't make it unwritten. There isn't
5550 * a full extent here so just skip it.
5551 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005552 ASSERT(end >= del.br_blockcount);
5553 end -= del.br_blockcount;
5554 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005555 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 continue;
5557 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005558 struct xfs_bmbt_irec prev;
5559
Linus Torvalds1da177e2005-04-16 15:20:36 -07005560 /*
5561 * This one is already unwritten.
5562 * It must have a written left neighbor.
5563 * Unwrite the killed part of that one and
5564 * try again.
5565 */
5566 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005567 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005569 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 ASSERT(del.br_startblock ==
5571 prev.br_startblock + prev.br_blockcount);
5572 if (prev.br_startoff < start) {
5573 mod = start - prev.br_startoff;
5574 prev.br_blockcount -= mod;
5575 prev.br_startblock += mod;
5576 prev.br_startoff = start;
5577 }
5578 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005579 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005580 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005581 ip, whichfork, &lastx, &cur,
5582 &prev, firstblock, dfops,
5583 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 if (error)
5585 goto error0;
5586 goto nodelete;
5587 } else {
5588 ASSERT(del.br_state == XFS_EXT_NORM);
5589 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005590 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005591 ip, whichfork, &lastx, &cur,
5592 &del, firstblock, dfops,
5593 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 if (error)
5595 goto error0;
5596 goto nodelete;
5597 }
5598 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005599
Brian Fosterb2706a02016-03-15 11:42:46 +11005600 if (wasdel) {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005601 error = xfs_bmap_del_extent_delay(ip, whichfork, &lastx,
5602 &got, &del);
5603 } else {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005604 error = xfs_bmap_del_extent_real(ip, tp, &lastx, dfops,
5605 cur, &del, &tmp_logflags, whichfork,
5606 flags);
5607 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005608 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005609
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 if (error)
5611 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005612
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005613 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005614 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005616 /*
5617 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005619 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005620 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005621 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005622 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005623 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 extno++;
5626 }
5627 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005628 if (end == (xfs_fileoff_t)-1 || end < start || lastx < 0)
Darrick J. Wong44535932016-10-03 09:11:29 -07005629 *rlen = 0;
5630 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005631 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005632
Linus Torvalds1da177e2005-04-16 15:20:36 -07005633 /*
5634 * Convert to a btree if necessary.
5635 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005636 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005638 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 &cur, 0, &tmp_logflags, whichfork);
5640 logflags |= tmp_logflags;
5641 if (error)
5642 goto error0;
5643 }
5644 /*
5645 * transform from btree to extents, give it cur
5646 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005647 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 ASSERT(cur != NULL);
5649 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5650 whichfork);
5651 logflags |= tmp_logflags;
5652 if (error)
5653 goto error0;
5654 }
5655 /*
5656 * transform from extents to local?
5657 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 error = 0;
5659error0:
5660 /*
5661 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005662 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005664 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005665 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005666 logflags &= ~xfs_ilog_fext(whichfork);
5667 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005669 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 /*
5671 * Log inode even in the error case, if the transaction
5672 * is dirty we'll need to shut down the filesystem.
5673 */
5674 if (logflags)
5675 xfs_trans_log_inode(tp, ip, logflags);
5676 if (cur) {
5677 if (!error) {
5678 *firstblock = cur->bc_private.b.firstblock;
5679 cur->bc_private.b.allocated = 0;
5680 }
5681 xfs_btree_del_cursor(cur,
5682 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5683 }
5684 return error;
5685}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005686
Darrick J. Wong44535932016-10-03 09:11:29 -07005687/* Unmap a range of a file. */
5688int
5689xfs_bunmapi(
5690 xfs_trans_t *tp,
5691 struct xfs_inode *ip,
5692 xfs_fileoff_t bno,
5693 xfs_filblks_t len,
5694 int flags,
5695 xfs_extnum_t nexts,
5696 xfs_fsblock_t *firstblock,
5697 struct xfs_defer_ops *dfops,
5698 int *done)
5699{
5700 int error;
5701
5702 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5703 dfops);
5704 *done = (len == 0);
5705 return error;
5706}
5707
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005708/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005709 * Determine whether an extent shift can be accomplished by a merge with the
5710 * extent that precedes the target hole of the shift.
5711 */
5712STATIC bool
5713xfs_bmse_can_merge(
5714 struct xfs_bmbt_irec *left, /* preceding extent */
5715 struct xfs_bmbt_irec *got, /* current extent to shift */
5716 xfs_fileoff_t shift) /* shift fsb */
5717{
5718 xfs_fileoff_t startoff;
5719
5720 startoff = got->br_startoff - shift;
5721
5722 /*
5723 * The extent, once shifted, must be adjacent in-file and on-disk with
5724 * the preceding extent.
5725 */
5726 if ((left->br_startoff + left->br_blockcount != startoff) ||
5727 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5728 (left->br_state != got->br_state) ||
5729 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5730 return false;
5731
5732 return true;
5733}
5734
5735/*
5736 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5737 * hole in the file. If an extent shift would result in the extent being fully
5738 * adjacent to the extent that currently precedes the hole, we can merge with
5739 * the preceding extent rather than do the shift.
5740 *
5741 * This function assumes the caller has verified a shift-by-merge is possible
5742 * with the provided extents via xfs_bmse_can_merge().
5743 */
5744STATIC int
5745xfs_bmse_merge(
5746 struct xfs_inode *ip,
5747 int whichfork,
5748 xfs_fileoff_t shift, /* shift fsb */
5749 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005750 struct xfs_bmbt_irec *got, /* extent to shift */
5751 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005752 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005753 int *logflags, /* output */
5754 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005755{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005756 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5757 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005758 xfs_filblks_t blockcount;
5759 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005760 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005761
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005762 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005763
5764 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5765 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005766 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005767
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005768 new = *left;
5769 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005770
5771 /*
5772 * Update the on-disk extent count, the btree if necessary and log the
5773 * inode.
5774 */
5775 XFS_IFORK_NEXT_SET(ip, whichfork,
5776 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5777 *logflags |= XFS_ILOG_CORE;
5778 if (!cur) {
5779 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005780 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005781 }
5782
5783 /* lookup and remove the extent to merge */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005784 error = xfs_bmbt_lookup_eq(cur, got->br_startoff, got->br_startblock,
5785 got->br_blockcount, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005786 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005787 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005788 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005789
5790 error = xfs_btree_delete(cur, &i);
5791 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005792 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005793 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005794
5795 /* lookup and update size of the previous extent */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005796 error = xfs_bmbt_lookup_eq(cur, left->br_startoff, left->br_startblock,
5797 left->br_blockcount, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005798 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005799 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005800 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005801
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005802 error = xfs_bmbt_update(cur, new.br_startoff, new.br_startblock,
5803 new.br_blockcount, new.br_state);
5804 if (error)
5805 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005806
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005807done:
5808 xfs_iext_update_extent(ifp, current_ext - 1, &new);
5809 xfs_iext_remove(ip, current_ext, 1, 0);
5810
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005811 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005812 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5813 if (error)
5814 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005815 memcpy(&new, got, sizeof(new));
5816 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005817 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005818}
5819
5820/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005821 * Shift a single extent.
5822 */
5823STATIC int
5824xfs_bmse_shift_one(
5825 struct xfs_inode *ip,
5826 int whichfork,
5827 xfs_fileoff_t offset_shift_fsb,
5828 int *current_ext,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005829 struct xfs_bmbt_irec *got,
Brian Fostera979bdf2014-09-23 15:39:04 +10005830 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005831 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005832 enum shift_direction direction,
5833 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005834{
5835 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005836 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005837 xfs_fileoff_t startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005838 struct xfs_bmbt_irec adj_irec, new;
Brian Fostera979bdf2014-09-23 15:39:04 +10005839 int error;
5840 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005841 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005842
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005843 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005844 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005845 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005846
Brian Fosterf71721d2014-09-23 15:39:05 +10005847 /* delalloc extents should be prevented by caller */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005848 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got->br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005849
Namjae Jeona904b1c2015-03-25 15:08:56 +11005850 if (direction == SHIFT_LEFT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005851 startoff = got->br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005852
Namjae Jeona904b1c2015-03-25 15:08:56 +11005853 /*
5854 * Check for merge if we've got an extent to the left,
5855 * otherwise make sure there's enough room at the start
5856 * of the file for the shift.
5857 */
5858 if (!*current_ext) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005859 if (got->br_startoff < offset_shift_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005860 return -EINVAL;
5861 goto update_current_ext;
5862 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005863
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005864 /*
5865 * grab the left extent and check for a large enough hole.
5866 */
5867 xfs_iext_get_extent(ifp, *current_ext - 1, &adj_irec);
5868 if (startoff < adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005869 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005870
Dave Chinnerb11bd672014-12-04 09:42:24 +11005871 /* check whether to merge the extent or shift it down */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005872 if (xfs_bmse_can_merge(&adj_irec, got, offset_shift_fsb)) {
5873 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5874 *current_ext, got, &adj_irec,
5875 cur, logflags, dfops);
Dave Chinnerb11bd672014-12-04 09:42:24 +11005876 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005877 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005878 startoff = got->br_startoff + offset_shift_fsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005879 /* nothing to move if this is the last extent */
5880 if (*current_ext >= (total_extents - 1))
5881 goto update_current_ext;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005882
Namjae Jeona904b1c2015-03-25 15:08:56 +11005883 /*
5884 * If this is not the last extent in the file, make sure there
5885 * is enough room between current extent and next extent for
5886 * accommodating the shift.
5887 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005888 xfs_iext_get_extent(ifp, *current_ext + 1, &adj_irec);
5889 if (startoff + got->br_blockcount > adj_irec.br_startoff)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005890 return -EINVAL;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005891
Namjae Jeona904b1c2015-03-25 15:08:56 +11005892 /*
5893 * Unlike a left shift (which involves a hole punch),
5894 * a right shift does not modify extent neighbors
5895 * in any way. We should never find mergeable extents
5896 * in this scenario. Check anyways and warn if we
5897 * encounter two extents that could be one.
5898 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005899 if (xfs_bmse_can_merge(got, &adj_irec, offset_shift_fsb))
Namjae Jeona904b1c2015-03-25 15:08:56 +11005900 WARN_ON_ONCE(1);
5901 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005902
Brian Fostera979bdf2014-09-23 15:39:04 +10005903 /*
5904 * Increment the extent index for the next iteration, update the start
5905 * offset of the in-core extent and update the btree if applicable.
5906 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005907update_current_ext:
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005908 *logflags |= XFS_ILOG_CORE;
5909
5910 new = *got;
5911 new.br_startoff = startoff;
5912
5913 if (cur) {
5914 error = xfs_bmbt_lookup_eq(cur, got->br_startoff,
5915 got->br_startblock, got->br_blockcount, &i);
5916 if (error)
5917 return error;
5918 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5919
5920 error = xfs_bmbt_update(cur, new.br_startoff,
5921 new.br_startblock, new.br_blockcount,
5922 new.br_state);
5923 if (error)
5924 return error;
5925 } else {
5926 *logflags |= XFS_ILOG_DEXT;
5927 }
5928
5929 xfs_iext_update_extent(ifp, *current_ext, &new);
5930
Namjae Jeona904b1c2015-03-25 15:08:56 +11005931 if (direction == SHIFT_LEFT)
5932 (*current_ext)++;
5933 else
5934 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005935
Darrick J. Wong9c194642016-08-03 12:16:05 +10005936 /* update reverse mapping */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005937 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005938 if (error)
5939 return error;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005940 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fostera979bdf2014-09-23 15:39:04 +10005941}
5942
5943/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005944 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005945 *
Brian Foster2c845f52014-09-23 15:37:09 +10005946 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005947 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10005948 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5949 * is the length by which each extent is shifted. If there is no hole to shift
5950 * the extents into, this will be considered invalid operation and we abort
5951 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005952 */
5953int
5954xfs_bmap_shift_extents(
5955 struct xfs_trans *tp,
5956 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005957 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005958 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005959 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005960 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005961 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005962 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005963 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005964 int num_exts)
5965{
Brian Fosterca446d82014-09-02 12:12:53 +10005966 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005967 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005968 struct xfs_mount *mp = ip->i_mount;
5969 struct xfs_ifork *ifp;
5970 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005971 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005972 xfs_extnum_t total_extents;
5973 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005974 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005975 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10005976 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005977
5978 if (unlikely(XFS_TEST_ERROR(
5979 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5980 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005981 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005982 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5983 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005984 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005985 }
5986
5987 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005988 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005989
Brian Foster2c845f52014-09-23 15:37:09 +10005990 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5991 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005992 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005993
5994 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005995 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5996 /* Read in all the extents */
5997 error = xfs_iread_extents(tp, ip, whichfork);
5998 if (error)
5999 return error;
6000 }
6001
Brian Fosterddb19e32014-09-23 15:38:09 +10006002 if (ifp->if_flags & XFS_IFBROOT) {
6003 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6004 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006005 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10006006 cur->bc_private.b.flags = 0;
6007 }
6008
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006009 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006010 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10006011 * are collapsing out, so we cannot use the count of real extents here.
6012 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10006013 */
Eric Sandeen5d829302016-11-08 12:59:42 +11006014 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006015 if (total_extents == 0) {
6016 *done = 1;
6017 goto del_cursor;
6018 }
6019
6020 /*
6021 * In case of first right shift, we need to initialize next_fsb
6022 */
6023 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006024 ASSERT(direction == SHIFT_RIGHT);
6025
6026 current_ext = total_extents - 1;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006027 xfs_iext_get_extent(ifp, current_ext, &got);
6028 if (stop_fsb > got.br_startoff) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006029 *done = 1;
6030 goto del_cursor;
6031 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006032 *next_fsb = got.br_startoff;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006033 } else {
6034 /*
6035 * Look up the extent index for the fsb where we start shifting. We can
6036 * henceforth iterate with current_ext as extent list changes are locked
6037 * out via ilock.
6038 *
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006039 * If next_fsb lies in a hole beyond which there are no extents we are
6040 * done.
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006041 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006042 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
6043 &got)) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006044 *done = 1;
6045 goto del_cursor;
6046 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006047 }
6048
6049 /* Lookup the extent index at which we have to stop */
6050 if (direction == SHIFT_RIGHT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006051 struct xfs_bmbt_irec s;
6052
6053 xfs_iext_lookup_extent(ip, ifp, stop_fsb, &stop_extent, &s);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006054 /* Make stop_extent exclusive of shift range */
6055 stop_extent--;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006056 if (current_ext <= stop_extent) {
6057 error = -EIO;
6058 goto del_cursor;
6059 }
6060 } else {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006061 stop_extent = total_extents;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006062 if (current_ext >= stop_extent) {
6063 error = -EIO;
6064 goto del_cursor;
6065 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006066 }
6067
6068 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006069 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006070 &current_ext, &got, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006071 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006072 if (error)
6073 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006074 /*
6075 * If there was an extent merge during the shift, the extent
6076 * count can change. Update the total and grade the next record.
6077 */
6078 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006079 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006080 stop_extent = total_extents;
6081 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006082
Namjae Jeona904b1c2015-03-25 15:08:56 +11006083 if (current_ext == stop_extent) {
6084 *done = 1;
6085 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006086 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006087 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006088 xfs_iext_get_extent(ifp, current_ext, &got);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006089 }
6090
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006091 if (!*done)
Brian Foster2c845f52014-09-23 15:37:09 +10006092 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006093
6094del_cursor:
6095 if (cur)
6096 xfs_btree_del_cursor(cur,
6097 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6098
Brian Fosterca446d82014-09-02 12:12:53 +10006099 if (logflags)
6100 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006101
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006102 return error;
6103}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006104
6105/*
6106 * Splits an extent into two extents at split_fsb block such that it is
6107 * the first block of the current_ext. @current_ext is a target extent
6108 * to be split. @split_fsb is a block where the extents is split.
6109 * If split_fsb lies in a hole or the first block of extents, just return 0.
6110 */
6111STATIC int
6112xfs_bmap_split_extent_at(
6113 struct xfs_trans *tp,
6114 struct xfs_inode *ip,
6115 xfs_fileoff_t split_fsb,
6116 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006117 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006118{
6119 int whichfork = XFS_DATA_FORK;
6120 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006121 struct xfs_bmbt_irec got;
6122 struct xfs_bmbt_irec new; /* split extent */
6123 struct xfs_mount *mp = ip->i_mount;
6124 struct xfs_ifork *ifp;
6125 xfs_fsblock_t gotblkcnt; /* new block count for got */
6126 xfs_extnum_t current_ext;
6127 int error = 0;
6128 int logflags = 0;
6129 int i = 0;
6130
6131 if (unlikely(XFS_TEST_ERROR(
6132 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6133 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006134 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006135 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6136 XFS_ERRLEVEL_LOW, mp);
6137 return -EFSCORRUPTED;
6138 }
6139
6140 if (XFS_FORCED_SHUTDOWN(mp))
6141 return -EIO;
6142
6143 ifp = XFS_IFORK_PTR(ip, whichfork);
6144 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6145 /* Read in all the extents */
6146 error = xfs_iread_extents(tp, ip, whichfork);
6147 if (error)
6148 return error;
6149 }
6150
6151 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006152 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006153 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006154 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
6155 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006156 return 0;
6157
6158 gotblkcnt = split_fsb - got.br_startoff;
6159 new.br_startoff = split_fsb;
6160 new.br_startblock = got.br_startblock + gotblkcnt;
6161 new.br_blockcount = got.br_blockcount - gotblkcnt;
6162 new.br_state = got.br_state;
6163
6164 if (ifp->if_flags & XFS_IFBROOT) {
6165 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6166 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006167 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006168 cur->bc_private.b.flags = 0;
6169 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6170 got.br_startblock,
6171 got.br_blockcount,
6172 &i);
6173 if (error)
6174 goto del_cursor;
6175 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6176 }
6177
Namjae Jeona904b1c2015-03-25 15:08:56 +11006178 got.br_blockcount = gotblkcnt;
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006179 xfs_iext_update_extent(ifp, current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006180
6181 logflags = XFS_ILOG_CORE;
6182 if (cur) {
6183 error = xfs_bmbt_update(cur, got.br_startoff,
6184 got.br_startblock,
6185 got.br_blockcount,
6186 got.br_state);
6187 if (error)
6188 goto del_cursor;
6189 } else
6190 logflags |= XFS_ILOG_DEXT;
6191
6192 /* Add new extent */
6193 current_ext++;
6194 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6195 XFS_IFORK_NEXT_SET(ip, whichfork,
6196 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6197
6198 if (cur) {
6199 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6200 new.br_startblock, new.br_blockcount,
6201 &i);
6202 if (error)
6203 goto del_cursor;
6204 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6205 cur->bc_rec.b.br_state = new.br_state;
6206
6207 error = xfs_btree_insert(cur, &i);
6208 if (error)
6209 goto del_cursor;
6210 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6211 }
6212
6213 /*
6214 * Convert to a btree if necessary.
6215 */
6216 if (xfs_bmap_needs_btree(ip, whichfork)) {
6217 int tmp_logflags; /* partial log flag return val */
6218
6219 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006220 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006221 &cur, 0, &tmp_logflags, whichfork);
6222 logflags |= tmp_logflags;
6223 }
6224
6225del_cursor:
6226 if (cur) {
6227 cur->bc_private.b.allocated = 0;
6228 xfs_btree_del_cursor(cur,
6229 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6230 }
6231
6232 if (logflags)
6233 xfs_trans_log_inode(tp, ip, logflags);
6234 return error;
6235}
6236
6237int
6238xfs_bmap_split_extent(
6239 struct xfs_inode *ip,
6240 xfs_fileoff_t split_fsb)
6241{
6242 struct xfs_mount *mp = ip->i_mount;
6243 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006244 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006245 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006246 int error;
6247
Christoph Hellwig253f4912016-04-06 09:19:55 +10006248 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6249 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6250 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006251 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006252
6253 xfs_ilock(ip, XFS_ILOCK_EXCL);
6254 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6255
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006256 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006257
6258 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006259 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006260 if (error)
6261 goto out;
6262
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006263 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006264 if (error)
6265 goto out;
6266
Christoph Hellwig70393312015-06-04 13:48:08 +10006267 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006268
6269out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006270 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006271 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006272 return error;
6273}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006274
6275/* Deferred mapping is only for real extents in the data fork. */
6276static bool
6277xfs_bmap_is_update_needed(
6278 struct xfs_bmbt_irec *bmap)
6279{
6280 return bmap->br_startblock != HOLESTARTBLOCK &&
6281 bmap->br_startblock != DELAYSTARTBLOCK;
6282}
6283
6284/* Record a bmap intent. */
6285static int
6286__xfs_bmap_add(
6287 struct xfs_mount *mp,
6288 struct xfs_defer_ops *dfops,
6289 enum xfs_bmap_intent_type type,
6290 struct xfs_inode *ip,
6291 int whichfork,
6292 struct xfs_bmbt_irec *bmap)
6293{
6294 int error;
6295 struct xfs_bmap_intent *bi;
6296
6297 trace_xfs_bmap_defer(mp,
6298 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6299 type,
6300 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6301 ip->i_ino, whichfork,
6302 bmap->br_startoff,
6303 bmap->br_blockcount,
6304 bmap->br_state);
6305
6306 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6307 INIT_LIST_HEAD(&bi->bi_list);
6308 bi->bi_type = type;
6309 bi->bi_owner = ip;
6310 bi->bi_whichfork = whichfork;
6311 bi->bi_bmap = *bmap;
6312
Christoph Hellwig882d8782017-08-28 10:21:03 -07006313 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006314 if (error) {
6315 kmem_free(bi);
6316 return error;
6317 }
6318
6319 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6320 return 0;
6321}
6322
6323/* Map an extent into a file. */
6324int
6325xfs_bmap_map_extent(
6326 struct xfs_mount *mp,
6327 struct xfs_defer_ops *dfops,
6328 struct xfs_inode *ip,
6329 struct xfs_bmbt_irec *PREV)
6330{
6331 if (!xfs_bmap_is_update_needed(PREV))
6332 return 0;
6333
6334 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6335 XFS_DATA_FORK, PREV);
6336}
6337
6338/* Unmap an extent out of a file. */
6339int
6340xfs_bmap_unmap_extent(
6341 struct xfs_mount *mp,
6342 struct xfs_defer_ops *dfops,
6343 struct xfs_inode *ip,
6344 struct xfs_bmbt_irec *PREV)
6345{
6346 if (!xfs_bmap_is_update_needed(PREV))
6347 return 0;
6348
6349 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6350 XFS_DATA_FORK, PREV);
6351}
6352
6353/*
6354 * Process one of the deferred bmap operations. We pass back the
6355 * btree cursor to maintain our lock on the bmapbt between calls.
6356 */
6357int
6358xfs_bmap_finish_one(
6359 struct xfs_trans *tp,
6360 struct xfs_defer_ops *dfops,
6361 struct xfs_inode *ip,
6362 enum xfs_bmap_intent_type type,
6363 int whichfork,
6364 xfs_fileoff_t startoff,
6365 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006366 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006367 xfs_exntst_t state)
6368{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006369 xfs_fsblock_t firstfsb;
6370 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006371
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006372 /*
6373 * firstfsb is tied to the transaction lifetime and is used to
6374 * ensure correct AG locking order and schedule work item
6375 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6376 * to only making one bmap call per transaction, so it should
6377 * be safe to have it as a local variable here.
6378 */
6379 firstfsb = NULLFSBLOCK;
6380
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006381 trace_xfs_bmap_deferred(tp->t_mountp,
6382 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6383 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006384 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006385
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006386 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006387 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006388
6389 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006390 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006391 return -EIO;
6392
6393 switch (type) {
6394 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006395 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006396 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006397 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006398 break;
6399 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006400 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6401 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006402 break;
6403 default:
6404 ASSERT(0);
6405 error = -EFSCORRUPTED;
6406 }
6407
6408 return error;
6409}