blob: a7bd6ed0185a21c8d1a26de7a726748fd644fdb0 [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/*
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700163 * Update the record referred to by cur to the value given by irec
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100164 * This either works (return 0) or gets an EFSCORRUPTED error.
165 */
166STATIC int
167xfs_bmbt_update(
168 struct xfs_btree_cur *cur,
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700169 struct xfs_bmbt_irec *irec)
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100170{
171 union xfs_btree_rec rec;
172
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700173 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100174 return xfs_btree_update(cur, &rec);
175}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100178 * Compute the worst-case number of indirect blocks that will be used
179 * for ip's delayed extent of length "len".
180 */
181STATIC xfs_filblks_t
182xfs_bmap_worst_indlen(
183 xfs_inode_t *ip, /* incore inode pointer */
184 xfs_filblks_t len) /* delayed extent length */
185{
186 int level; /* btree level number */
187 int maxrecs; /* maximum record count at this level */
188 xfs_mount_t *mp; /* mount structure */
189 xfs_filblks_t rval; /* return value */
190
191 mp = ip->i_mount;
192 maxrecs = mp->m_bmap_dmxr[0];
193 for (level = 0, rval = 0;
194 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
195 level++) {
196 len += maxrecs - 1;
197 do_div(len, maxrecs);
198 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700199 if (len == 1)
200 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100201 level - 1;
202 if (level == 0)
203 maxrecs = mp->m_bmap_dmxr[1];
204 }
205 return rval;
206}
207
208/*
209 * Calculate the default attribute fork offset for newly created inodes.
210 */
211uint
212xfs_default_attroffset(
213 struct xfs_inode *ip)
214{
215 struct xfs_mount *mp = ip->i_mount;
216 uint offset;
217
218 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100219 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100220 XFS_BMDR_SPACE_CALC(MINABTPTRS);
221 } else {
222 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
223 }
224
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100225 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100226 return offset;
227}
228
229/*
230 * Helper routine to reset inode di_forkoff field when switching
231 * attribute fork from local to extent format - we reset it where
232 * possible to make space available for inline data fork extents.
233 */
234STATIC void
235xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100236 xfs_inode_t *ip,
237 int whichfork)
238{
239 if (whichfork == XFS_ATTR_FORK &&
240 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
241 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
242 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
243 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
244
245 if (dfl_forkoff > ip->i_d.di_forkoff)
246 ip->i_d.di_forkoff = dfl_forkoff;
247 }
248}
249
Dave Chinner9e5987a2013-02-25 12:31:26 +1100250#ifdef DEBUG
251STATIC struct xfs_buf *
252xfs_bmap_get_bp(
253 struct xfs_btree_cur *cur,
254 xfs_fsblock_t bno)
255{
256 struct xfs_log_item_desc *lidp;
257 int i;
258
259 if (!cur)
260 return NULL;
261
262 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
263 if (!cur->bc_bufs[i])
264 break;
265 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
266 return cur->bc_bufs[i];
267 }
268
269 /* Chase down all the log items to see if the bp is there */
270 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
271 struct xfs_buf_log_item *bip;
272 bip = (struct xfs_buf_log_item *)lidp->lid_item;
273 if (bip->bli_item.li_type == XFS_LI_BUF &&
274 XFS_BUF_ADDR(bip->bli_buf) == bno)
275 return bip->bli_buf;
276 }
277
278 return NULL;
279}
280
281STATIC void
282xfs_check_block(
283 struct xfs_btree_block *block,
284 xfs_mount_t *mp,
285 int root,
286 short sz)
287{
288 int i, j, dmxr;
289 __be64 *pp, *thispa; /* pointer to block address */
290 xfs_bmbt_key_t *prevp, *keyp;
291
292 ASSERT(be16_to_cpu(block->bb_level) > 0);
293
294 prevp = NULL;
295 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
296 dmxr = mp->m_bmap_dmxr[0];
297 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
298
299 if (prevp) {
300 ASSERT(be64_to_cpu(prevp->br_startoff) <
301 be64_to_cpu(keyp->br_startoff));
302 }
303 prevp = keyp;
304
305 /*
306 * Compare the block numbers to see if there are dups.
307 */
308 if (root)
309 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
310 else
311 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
312
313 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
314 if (root)
315 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
316 else
317 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
318 if (*thispa == *pp) {
319 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
320 __func__, j, i,
321 (unsigned long long)be64_to_cpu(*thispa));
322 panic("%s: ptrs are equal in node\n",
323 __func__);
324 }
325 }
326 }
327}
328
329/*
330 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100331 * btree leaves. THis becomes prohibitively expensive for large extent count
332 * files, so don't bother with inodes that have more than 10,000 extents in
333 * them. The btree record ordering checks will still be done, so for such large
334 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100335 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100336STATIC void
337xfs_bmap_check_leaf_extents(
338 xfs_btree_cur_t *cur, /* btree cursor or null */
339 xfs_inode_t *ip, /* incore inode pointer */
340 int whichfork) /* data or attr fork */
341{
342 struct xfs_btree_block *block; /* current btree block */
343 xfs_fsblock_t bno; /* block # of "block" */
344 xfs_buf_t *bp; /* buffer for "block" */
345 int error; /* error return value */
346 xfs_extnum_t i=0, j; /* index into the extents list */
347 xfs_ifork_t *ifp; /* fork structure */
348 int level; /* btree level, for checking */
349 xfs_mount_t *mp; /* file system mount structure */
350 __be64 *pp; /* pointer to block address */
351 xfs_bmbt_rec_t *ep; /* pointer to current extent */
352 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
353 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
354 int bp_release = 0;
355
356 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
357 return;
358 }
359
Dave Chinnere3543812016-01-08 11:28:49 +1100360 /* skip large extent count inodes */
361 if (ip->i_d.di_nextents > 10000)
362 return;
363
Dave Chinner9e5987a2013-02-25 12:31:26 +1100364 bno = NULLFSBLOCK;
365 mp = ip->i_mount;
366 ifp = XFS_IFORK_PTR(ip, whichfork);
367 block = ifp->if_broot;
368 /*
369 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
370 */
371 level = be16_to_cpu(block->bb_level);
372 ASSERT(level > 0);
373 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
374 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
375 bno = be64_to_cpu(*pp);
376
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000377 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100378 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
379 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
380
381 /*
382 * Go down the tree until leaf level is reached, following the first
383 * pointer (leftmost) at each level.
384 */
385 while (level-- > 0) {
386 /* See if buf is in cur first */
387 bp_release = 0;
388 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
389 if (!bp) {
390 bp_release = 1;
391 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
392 XFS_BMAP_BTREE_REF,
393 &xfs_bmbt_buf_ops);
394 if (error)
395 goto error_norelse;
396 }
397 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100398 if (level == 0)
399 break;
400
401 /*
402 * Check this block for basic sanity (increasing keys and
403 * no duplicate blocks).
404 */
405
406 xfs_check_block(block, mp, 0, 0);
407 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
408 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100409 XFS_WANT_CORRUPTED_GOTO(mp,
410 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100411 if (bp_release) {
412 bp_release = 0;
413 xfs_trans_brelse(NULL, bp);
414 }
415 }
416
417 /*
418 * Here with bp and block set to the leftmost leaf node in the tree.
419 */
420 i = 0;
421
422 /*
423 * Loop over all leaf nodes checking that all extents are in the right order.
424 */
425 for (;;) {
426 xfs_fsblock_t nextbno;
427 xfs_extnum_t num_recs;
428
429
430 num_recs = xfs_btree_get_numrecs(block);
431
432 /*
433 * Read-ahead the next leaf block, if any.
434 */
435
436 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
437
438 /*
439 * Check all the extents to make sure they are OK.
440 * If we had a previous block, the last entry should
441 * conform with the first entry in this one.
442 */
443
444 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
445 if (i) {
446 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
447 xfs_bmbt_disk_get_blockcount(&last) <=
448 xfs_bmbt_disk_get_startoff(ep));
449 }
450 for (j = 1; j < num_recs; j++) {
451 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
452 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
453 xfs_bmbt_disk_get_blockcount(ep) <=
454 xfs_bmbt_disk_get_startoff(nextp));
455 ep = nextp;
456 }
457
458 last = *ep;
459 i += num_recs;
460 if (bp_release) {
461 bp_release = 0;
462 xfs_trans_brelse(NULL, bp);
463 }
464 bno = nextbno;
465 /*
466 * If we've reached the end, stop.
467 */
468 if (bno == NULLFSBLOCK)
469 break;
470
471 bp_release = 0;
472 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
473 if (!bp) {
474 bp_release = 1;
475 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
476 XFS_BMAP_BTREE_REF,
477 &xfs_bmbt_buf_ops);
478 if (error)
479 goto error_norelse;
480 }
481 block = XFS_BUF_TO_BLOCK(bp);
482 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100483
Dave Chinner9e5987a2013-02-25 12:31:26 +1100484 return;
485
486error0:
487 xfs_warn(mp, "%s: at error0", __func__);
488 if (bp_release)
489 xfs_trans_brelse(NULL, bp);
490error_norelse:
491 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
492 __func__, i);
493 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
494 return;
495}
496
497/*
498 * Add bmap trace insert entries for all the contents of the extent records.
499 */
500void
501xfs_bmap_trace_exlist(
502 xfs_inode_t *ip, /* incore inode pointer */
503 xfs_extnum_t cnt, /* count of entries in the list */
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100504 int whichfork, /* data or attr or cow fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100505 unsigned long caller_ip)
506{
507 xfs_extnum_t idx; /* extent record index */
508 xfs_ifork_t *ifp; /* inode fork pointer */
509 int state = 0;
510
511 if (whichfork == XFS_ATTR_FORK)
512 state |= BMAP_ATTRFORK;
Eric Sandeenc44a1f22016-12-05 12:32:00 +1100513 else if (whichfork == XFS_COW_FORK)
514 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100515
516 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +1100517 ASSERT(cnt == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100518 for (idx = 0; idx < cnt; idx++)
Eric Sandeen77105172016-12-05 12:31:50 +1100519 trace_xfs_extlist(ip, idx, state, caller_ip);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100520}
521
522/*
523 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000524 * given the caller's original parameters. Specifically check the
525 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100526 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
527 */
528STATIC void
529xfs_bmap_validate_ret(
530 xfs_fileoff_t bno,
531 xfs_filblks_t len,
532 int flags,
533 xfs_bmbt_irec_t *mval,
534 int nmap,
535 int ret_nmap)
536{
537 int i; /* index to map values */
538
539 ASSERT(ret_nmap <= nmap);
540
541 for (i = 0; i < ret_nmap; i++) {
542 ASSERT(mval[i].br_blockcount > 0);
543 if (!(flags & XFS_BMAPI_ENTIRE)) {
544 ASSERT(mval[i].br_startoff >= bno);
545 ASSERT(mval[i].br_blockcount <= len);
546 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
547 bno + len);
548 } else {
549 ASSERT(mval[i].br_startoff < bno + len);
550 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
551 bno);
552 }
553 ASSERT(i == 0 ||
554 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
555 mval[i].br_startoff);
556 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
557 mval[i].br_startblock != HOLESTARTBLOCK);
558 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
559 mval[i].br_state == XFS_EXT_UNWRITTEN);
560 }
561}
562
563#else
564#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700565#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100566#endif /* DEBUG */
567
568/*
569 * bmap free list manipulation functions
570 */
571
572/*
573 * Add the extent to the list of extents to be free at transaction end.
574 * The list is maintained sorted (by block number).
575 */
576void
577xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000578 struct xfs_mount *mp,
579 struct xfs_defer_ops *dfops,
580 xfs_fsblock_t bno,
581 xfs_filblks_t len,
582 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100583{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000584 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100585#ifdef DEBUG
586 xfs_agnumber_t agno;
587 xfs_agblock_t agbno;
588
589 ASSERT(bno != NULLFSBLOCK);
590 ASSERT(len > 0);
591 ASSERT(len <= MAXEXTLEN);
592 ASSERT(!isnullstartblock(bno));
593 agno = XFS_FSB_TO_AGNO(mp, bno);
594 agbno = XFS_FSB_TO_AGBNO(mp, bno);
595 ASSERT(agno < mp->m_sb.sb_agcount);
596 ASSERT(agbno < mp->m_sb.sb_agblocks);
597 ASSERT(len < mp->m_sb.sb_agblocks);
598 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
599#endif
600 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000601
Dave Chinner9e5987a2013-02-25 12:31:26 +1100602 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000603 new->xefi_startblock = bno;
604 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000605 if (oinfo)
606 new->xefi_oinfo = *oinfo;
607 else
608 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000609 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
610 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000611 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100612}
613
614/*
615 * Inode fork format manipulation functions
616 */
617
618/*
619 * Transform a btree format file with only one leaf node, where the
620 * extents list will fit in the inode, into an extents format file.
621 * Since the file extents are already in-core, all we have to do is
622 * give up the space for the btree root and pitch the leaf block.
623 */
624STATIC int /* error */
625xfs_bmap_btree_to_extents(
626 xfs_trans_t *tp, /* transaction pointer */
627 xfs_inode_t *ip, /* incore inode pointer */
628 xfs_btree_cur_t *cur, /* btree cursor */
629 int *logflagsp, /* inode logging flags */
630 int whichfork) /* data or attr fork */
631{
632 /* REFERENCED */
633 struct xfs_btree_block *cblock;/* child btree block */
634 xfs_fsblock_t cbno; /* child block number */
635 xfs_buf_t *cbp; /* child block's buffer */
636 int error; /* error return value */
637 xfs_ifork_t *ifp; /* inode fork data */
638 xfs_mount_t *mp; /* mount point structure */
639 __be64 *pp; /* ptr to block address */
640 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000641 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100642
643 mp = ip->i_mount;
644 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700645 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100646 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
647 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
648 rblock = ifp->if_broot;
649 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
650 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
651 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
652 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
653 cbno = be64_to_cpu(*pp);
654 *logflagsp = 0;
655#ifdef DEBUG
656 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
657 return error;
658#endif
659 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
660 &xfs_bmbt_buf_ops);
661 if (error)
662 return error;
663 cblock = XFS_BUF_TO_BLOCK(cbp);
664 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
665 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000666 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
667 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100668 ip->i_d.di_nblocks--;
669 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
670 xfs_trans_binval(tp, cbp);
671 if (cur->bc_bufs[0] == cbp)
672 cur->bc_bufs[0] = NULL;
673 xfs_iroot_realloc(ip, -1, whichfork);
674 ASSERT(ifp->if_broot == NULL);
675 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
676 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
677 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
678 return 0;
679}
680
681/*
682 * Convert an extents-format file into a btree-format file.
683 * The new file will have a root block (in the inode) and a single child block.
684 */
685STATIC int /* error */
686xfs_bmap_extents_to_btree(
687 xfs_trans_t *tp, /* transaction pointer */
688 xfs_inode_t *ip, /* incore inode pointer */
689 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000690 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100691 xfs_btree_cur_t **curp, /* cursor returned to caller */
692 int wasdel, /* converting a delayed alloc */
693 int *logflagsp, /* inode logging flags */
694 int whichfork) /* data or attr fork */
695{
696 struct xfs_btree_block *ablock; /* allocated (child) bt block */
697 xfs_buf_t *abp; /* buffer for ablock */
698 xfs_alloc_arg_t args; /* allocation arguments */
699 xfs_bmbt_rec_t *arp; /* child record pointer */
700 struct xfs_btree_block *block; /* btree root block */
701 xfs_btree_cur_t *cur; /* bmap btree cursor */
702 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
703 int error; /* error return value */
704 xfs_extnum_t i, cnt; /* extent record index */
705 xfs_ifork_t *ifp; /* inode fork pointer */
706 xfs_bmbt_key_t *kp; /* root block key pointer */
707 xfs_mount_t *mp; /* mount structure */
708 xfs_extnum_t nextents; /* number of file extents */
709 xfs_bmbt_ptr_t *pp; /* root block address pointer */
710
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500711 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700712 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100713 ifp = XFS_IFORK_PTR(ip, whichfork);
714 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
715
716 /*
717 * Make space in the inode incore.
718 */
719 xfs_iroot_realloc(ip, 1, whichfork);
720 ifp->if_flags |= XFS_IFBROOT;
721
722 /*
723 * Fill in the root.
724 */
725 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800726 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
727 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800728 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100729 /*
730 * Need a cursor. Can't allocate until bb_level is filled in.
731 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100732 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
733 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000734 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100735 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
736 /*
737 * Convert to a btree with two levels, one record in root.
738 */
739 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
740 memset(&args, 0, sizeof(args));
741 args.tp = tp;
742 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000743 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100744 args.firstblock = *firstblock;
745 if (*firstblock == NULLFSBLOCK) {
746 args.type = XFS_ALLOCTYPE_START_BNO;
747 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000748 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100749 args.type = XFS_ALLOCTYPE_START_BNO;
750 args.fsbno = *firstblock;
751 } else {
752 args.type = XFS_ALLOCTYPE_NEAR_BNO;
753 args.fsbno = *firstblock;
754 }
755 args.minlen = args.maxlen = args.prod = 1;
756 args.wasdel = wasdel;
757 *logflagsp = 0;
758 if ((error = xfs_alloc_vextent(&args))) {
759 xfs_iroot_realloc(ip, -1, whichfork);
760 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
761 return error;
762 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700763
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800764 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
765 xfs_iroot_realloc(ip, -1, whichfork);
766 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
767 return -ENOSPC;
768 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100769 /*
770 * Allocation can't fail, the space was reserved.
771 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100772 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800773 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100774 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
775 cur->bc_private.b.allocated++;
776 ip->i_d.di_nblocks++;
777 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
778 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
779 /*
780 * Fill in the child block.
781 */
782 abp->b_ops = &xfs_bmbt_buf_ops;
783 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800784 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
785 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500786 XFS_BTREE_LONG_PTRS);
787
Dave Chinner9e5987a2013-02-25 12:31:26 +1100788 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100789 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100790 for (cnt = i = 0; i < nextents; i++) {
791 ep = xfs_iext_get_ext(ifp, i);
792 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
793 arp->l0 = cpu_to_be64(ep->l0);
794 arp->l1 = cpu_to_be64(ep->l1);
795 arp++; cnt++;
796 }
797 }
798 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
799 xfs_btree_set_numrecs(ablock, cnt);
800
801 /*
802 * Fill in the root key and pointer.
803 */
804 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
805 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
806 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
807 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
808 be16_to_cpu(block->bb_level)));
809 *pp = cpu_to_be64(args.fsbno);
810
811 /*
812 * Do all this logging at the end so that
813 * the root is at the right level.
814 */
815 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
816 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
817 ASSERT(*curp == NULL);
818 *curp = cur;
819 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
820 return 0;
821}
822
823/*
824 * Convert a local file to an extents file.
825 * This code is out of bounds for data forks of regular files,
826 * since the file data needs to get logged so things will stay consistent.
827 * (The bmap-level manipulations are ok, though).
828 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000829void
830xfs_bmap_local_to_extents_empty(
831 struct xfs_inode *ip,
832 int whichfork)
833{
834 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
835
Darrick J. Wong60b49842016-10-03 09:11:34 -0700836 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000837 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
838 ASSERT(ifp->if_bytes == 0);
839 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
840
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000841 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000842 ifp->if_flags &= ~XFS_IFINLINE;
843 ifp->if_flags |= XFS_IFEXTENTS;
844 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
845}
846
847
Dave Chinner9e5987a2013-02-25 12:31:26 +1100848STATIC int /* error */
849xfs_bmap_local_to_extents(
850 xfs_trans_t *tp, /* transaction pointer */
851 xfs_inode_t *ip, /* incore inode pointer */
852 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
853 xfs_extlen_t total, /* total blocks needed by transaction */
854 int *logflagsp, /* inode logging flags */
855 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500856 void (*init_fn)(struct xfs_trans *tp,
857 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100858 struct xfs_inode *ip,
859 struct xfs_ifork *ifp))
860{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000861 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100862 int flags; /* logging flags returned */
863 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000864 xfs_alloc_arg_t args; /* allocation arguments */
865 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700866 struct xfs_bmbt_irec rec;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100867
868 /*
869 * We don't want to deal with the case of keeping inode data inline yet.
870 * So sending the data fork of a regular inode is invalid.
871 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100872 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100873 ifp = XFS_IFORK_PTR(ip, whichfork);
874 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000875
876 if (!ifp->if_bytes) {
877 xfs_bmap_local_to_extents_empty(ip, whichfork);
878 flags = XFS_ILOG_CORE;
879 goto done;
880 }
881
Dave Chinner9e5987a2013-02-25 12:31:26 +1100882 flags = 0;
883 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000884 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
885 XFS_IFINLINE);
886 memset(&args, 0, sizeof(args));
887 args.tp = tp;
888 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000889 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000890 args.firstblock = *firstblock;
891 /*
892 * Allocate a block. We know we need only one, since the
893 * file currently fits in an inode.
894 */
895 if (*firstblock == NULLFSBLOCK) {
896 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
897 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100898 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000899 args.fsbno = *firstblock;
900 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100901 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000902 args.total = total;
903 args.minlen = args.maxlen = args.prod = 1;
904 error = xfs_alloc_vextent(&args);
905 if (error)
906 goto done;
907
908 /* Can't fail, the space was reserved. */
909 ASSERT(args.fsbno != NULLFSBLOCK);
910 ASSERT(args.len == 1);
911 *firstblock = args.fsbno;
912 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
913
Dave Chinnerfe22d552015-01-22 09:30:06 +1100914 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100915 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100916 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100917 * The callout is responsible for logging because the remote format
918 * might differ from the local format and thus we don't know how much to
919 * log here. Note that init_fn must also set the buffer log item type
920 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100921 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000922 init_fn(tp, bp, ip, ifp);
923
Brian Fosterb7cdc662015-10-12 15:40:24 +1100924 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000925 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
926 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100927 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000928
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700929 rec.br_startoff = 0;
930 rec.br_startblock = args.fsbno;
931 rec.br_blockcount = 1;
932 rec.br_state = XFS_EXT_NORM;
933 xfs_iext_insert(ip, 0, 1, &rec, 0);
934
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000935 trace_xfs_bmap_post_update(ip, 0,
936 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
937 _THIS_IP_);
938 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
939 ip->i_d.di_nblocks = 1;
940 xfs_trans_mod_dquot_byino(tp, ip,
941 XFS_TRANS_DQ_BCOUNT, 1L);
942 flags |= xfs_ilog_fext(whichfork);
943
Dave Chinner9e5987a2013-02-25 12:31:26 +1100944done:
945 *logflagsp = flags;
946 return error;
947}
948
949/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 * Called from xfs_bmap_add_attrfork to handle btree format files.
951 */
952STATIC int /* error */
953xfs_bmap_add_attrfork_btree(
954 xfs_trans_t *tp, /* transaction pointer */
955 xfs_inode_t *ip, /* incore inode pointer */
956 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000957 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 int *flags) /* inode logging flags */
959{
960 xfs_btree_cur_t *cur; /* btree cursor */
961 int error; /* error return value */
962 xfs_mount_t *mp; /* file system mount struct */
963 int stat; /* newroot status */
964
965 mp = ip->i_mount;
966 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
967 *flags |= XFS_ILOG_DBROOT;
968 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100969 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000970 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 cur->bc_private.b.firstblock = *firstblock;
972 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
973 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000974 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100975 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100976 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 goto error0;
978 if (stat == 0) {
979 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000980 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982 *firstblock = cur->bc_private.b.firstblock;
983 cur->bc_private.b.allocated = 0;
984 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
985 }
986 return 0;
987error0:
988 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
989 return error;
990}
991
992/*
993 * Called from xfs_bmap_add_attrfork to handle extents format files.
994 */
995STATIC int /* error */
996xfs_bmap_add_attrfork_extents(
997 xfs_trans_t *tp, /* transaction pointer */
998 xfs_inode_t *ip, /* incore inode pointer */
999 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001000 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 int *flags) /* inode logging flags */
1002{
1003 xfs_btree_cur_t *cur; /* bmap btree cursor */
1004 int error; /* error return value */
1005
1006 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
1007 return 0;
1008 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001009 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 flags, XFS_DATA_FORK);
1011 if (cur) {
1012 cur->bc_private.b.allocated = 0;
1013 xfs_btree_del_cursor(cur,
1014 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
1015 }
1016 return error;
1017}
1018
1019/*
Dave Chinner1e823792013-02-11 15:58:13 +11001020 * Called from xfs_bmap_add_attrfork to handle local format files. Each
1021 * different data fork content type needs a different callout to do the
1022 * conversion. Some are basic and only require special block initialisation
1023 * callouts for the data formating, others (directories) are so specialised they
1024 * handle everything themselves.
1025 *
1026 * XXX (dgc): investigate whether directory conversion can use the generic
1027 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05001028 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 */
1030STATIC int /* error */
1031xfs_bmap_add_attrfork_local(
1032 xfs_trans_t *tp, /* transaction pointer */
1033 xfs_inode_t *ip, /* incore inode pointer */
1034 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001035 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 int *flags) /* inode logging flags */
1037{
1038 xfs_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1041 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001042
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001043 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001045 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 dargs.dp = ip;
1047 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001048 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001049 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 dargs.whichfork = XFS_DATA_FORK;
1051 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001052 return xfs_dir2_sf_to_block(&dargs);
1053 }
1054
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001055 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001056 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1057 flags, XFS_DATA_FORK,
1058 xfs_symlink_local_to_remote);
1059
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001060 /* should only be called for types that support local format data */
1061 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001062 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
1065/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001066 * Convert inode from non-attributed to attributed.
1067 * Must not be in a transaction, ip must not be locked.
1068 */
1069int /* error code */
1070xfs_bmap_add_attrfork(
1071 xfs_inode_t *ip, /* incore inode pointer */
1072 int size, /* space new attribute needs */
1073 int rsvd) /* xact may use reserved blks */
1074{
1075 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001076 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001077 xfs_mount_t *mp; /* mount structure */
1078 xfs_trans_t *tp; /* transaction pointer */
1079 int blks; /* space reservation */
1080 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001081 int logflags; /* logging flags */
1082 int error; /* error return value */
1083
1084 ASSERT(XFS_IFORK_Q(ip) == 0);
1085
1086 mp = ip->i_mount;
1087 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001088
Dave Chinner9e5987a2013-02-25 12:31:26 +11001089 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001090
1091 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1092 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1093 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001094 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001095
Dave Chinner9e5987a2013-02-25 12:31:26 +11001096 xfs_ilock(ip, XFS_ILOCK_EXCL);
1097 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1098 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1099 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001100 if (error)
1101 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001102 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001103 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001104 if (ip->i_d.di_anextents != 0) {
1105 error = -EFSCORRUPTED;
1106 goto trans_cancel;
1107 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001108 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1109 /*
1110 * For inodes coming from pre-6.2 filesystems.
1111 */
1112 ASSERT(ip->i_d.di_aformat == 0);
1113 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1114 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001115
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001116 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001117 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1118
1119 switch (ip->i_d.di_format) {
1120 case XFS_DINODE_FMT_DEV:
1121 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1122 break;
1123 case XFS_DINODE_FMT_UUID:
1124 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
1125 break;
1126 case XFS_DINODE_FMT_LOCAL:
1127 case XFS_DINODE_FMT_EXTENTS:
1128 case XFS_DINODE_FMT_BTREE:
1129 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1130 if (!ip->i_d.di_forkoff)
1131 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1132 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1133 version = 2;
1134 break;
1135 default:
1136 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001137 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001138 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001139 }
1140
1141 ASSERT(ip->i_afp == NULL);
1142 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1143 ip->i_afp->if_flags = XFS_IFEXTENTS;
1144 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001145 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001146 switch (ip->i_d.di_format) {
1147 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001148 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001149 &logflags);
1150 break;
1151 case XFS_DINODE_FMT_EXTENTS:
1152 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001153 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001154 break;
1155 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001156 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001157 &logflags);
1158 break;
1159 default:
1160 error = 0;
1161 break;
1162 }
1163 if (logflags)
1164 xfs_trans_log_inode(tp, ip, logflags);
1165 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001166 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001167 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1168 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001169 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001170
1171 spin_lock(&mp->m_sb_lock);
1172 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1173 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001174 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001175 }
1176 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1177 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001178 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001179 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001180 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001181 if (log_sb)
1182 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001183 }
1184
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001185 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001186 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001187 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001188 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001189 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001190 return error;
1191
1192bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001193 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001194trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001195 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001196 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001197 return error;
1198}
1199
1200/*
1201 * Internal and external extent tree search functions.
1202 */
1203
1204/*
1205 * Read in the extents to if_extents.
1206 * All inode fields are set up by caller, we just traverse the btree
1207 * and copy the records in. If the file system cannot contain unwritten
1208 * extents, the records are checked for no "state" flags.
1209 */
1210int /* error */
1211xfs_bmap_read_extents(
1212 xfs_trans_t *tp, /* transaction pointer */
1213 xfs_inode_t *ip, /* incore inode */
1214 int whichfork) /* data or attr fork */
1215{
1216 struct xfs_btree_block *block; /* current btree block */
1217 xfs_fsblock_t bno; /* block # of "block" */
1218 xfs_buf_t *bp; /* buffer for "block" */
1219 int error; /* error return value */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001220 xfs_extnum_t i, j; /* index into the extents list */
1221 xfs_ifork_t *ifp; /* fork structure */
1222 int level; /* btree level, for checking */
1223 xfs_mount_t *mp; /* file system mount structure */
1224 __be64 *pp; /* pointer to block address */
1225 /* REFERENCED */
1226 xfs_extnum_t room; /* number of entries there's room for */
1227
Dave Chinner9e5987a2013-02-25 12:31:26 +11001228 mp = ip->i_mount;
1229 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001230 block = ifp->if_broot;
1231 /*
1232 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1233 */
1234 level = be16_to_cpu(block->bb_level);
1235 ASSERT(level > 0);
1236 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1237 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001238
Dave Chinner9e5987a2013-02-25 12:31:26 +11001239 /*
1240 * Go down the tree until leaf level is reached, following the first
1241 * pointer (leftmost) at each level.
1242 */
1243 while (level-- > 0) {
1244 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1245 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1246 if (error)
1247 return error;
1248 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001249 if (level == 0)
1250 break;
1251 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1252 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001253 XFS_WANT_CORRUPTED_GOTO(mp,
1254 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001255 xfs_trans_brelse(tp, bp);
1256 }
1257 /*
1258 * Here with bp and block set to the leftmost leaf node in the tree.
1259 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001260 room = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001261 i = 0;
1262 /*
1263 * Loop over all leaf nodes. Copy information to the extent records.
1264 */
1265 for (;;) {
1266 xfs_bmbt_rec_t *frp;
1267 xfs_fsblock_t nextbno;
1268 xfs_extnum_t num_recs;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001269
1270 num_recs = xfs_btree_get_numrecs(block);
1271 if (unlikely(i + num_recs > room)) {
1272 ASSERT(i + num_recs <= room);
1273 xfs_warn(ip->i_mount,
1274 "corrupt dinode %Lu, (btree extents).",
1275 (unsigned long long) ip->i_ino);
1276 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1277 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1278 goto error0;
1279 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001280 /*
1281 * Read-ahead the next leaf block, if any.
1282 */
1283 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1284 if (nextbno != NULLFSBLOCK)
1285 xfs_btree_reada_bufl(mp, nextbno, 1,
1286 &xfs_bmbt_buf_ops);
1287 /*
1288 * Copy records into the extent records.
1289 */
1290 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001291 for (j = 0; j < num_recs; j++, i++, frp++) {
1292 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1293 trp->l0 = be64_to_cpu(frp->l0);
1294 trp->l1 = be64_to_cpu(frp->l1);
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001295 if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001296 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001297 XFS_ERRLEVEL_LOW, mp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001298 goto error0;
1299 }
1300 }
1301 xfs_trans_brelse(tp, bp);
1302 bno = nextbno;
1303 /*
1304 * If we've reached the end, stop.
1305 */
1306 if (bno == NULLFSBLOCK)
1307 break;
1308 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1309 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1310 if (error)
1311 return error;
1312 block = XFS_BUF_TO_BLOCK(bp);
1313 }
Darrick J. Wong356a3222016-12-05 12:36:56 +11001314 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1315 return -EFSCORRUPTED;
Eric Sandeen5d829302016-11-08 12:59:42 +11001316 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11001317 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
1318 return 0;
1319error0:
1320 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001321 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001322}
1323
Dave Chinner9e5987a2013-02-25 12:31:26 +11001324/*
1325 * Returns the file-relative block number of the first unused block(s)
1326 * in the file with at least "len" logically contiguous blocks free.
1327 * This is the lowest-address hole if the file has holes, else the first block
1328 * past the end of file.
1329 * Return 0 if the file is currently local (in-inode).
1330 */
1331int /* error */
1332xfs_bmap_first_unused(
1333 xfs_trans_t *tp, /* transaction pointer */
1334 xfs_inode_t *ip, /* incore inode */
1335 xfs_extlen_t len, /* size of hole to find */
1336 xfs_fileoff_t *first_unused, /* unused block */
1337 int whichfork) /* data or attr fork */
1338{
1339 int error; /* error return value */
1340 int idx; /* extent record index */
1341 xfs_ifork_t *ifp; /* inode fork pointer */
1342 xfs_fileoff_t lastaddr; /* last block number seen */
1343 xfs_fileoff_t lowest; /* lowest useful block */
1344 xfs_fileoff_t max; /* starting useful block */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001345 xfs_extnum_t nextents; /* number of extent entries */
1346
1347 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1348 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1349 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1350 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1351 *first_unused = 0;
1352 return 0;
1353 }
1354 ifp = XFS_IFORK_PTR(ip, whichfork);
1355 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1356 (error = xfs_iread_extents(tp, ip, whichfork)))
1357 return error;
1358 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001359 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001360 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001361 struct xfs_bmbt_irec got;
1362
1363 xfs_iext_get_extent(ifp, idx, &got);
1364
Dave Chinner9e5987a2013-02-25 12:31:26 +11001365 /*
1366 * See if the hole before this extent will work.
1367 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001368 if (got.br_startoff >= lowest + len &&
1369 got.br_startoff - max >= len) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001370 *first_unused = max;
1371 return 0;
1372 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001373 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001374 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1375 }
1376 *first_unused = max;
1377 return 0;
1378}
1379
1380/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001381 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001382 * last_block (input value) in the file.
1383 * This is not based on i_size, it is based on the extent records.
1384 * Returns 0 for local files, as they do not have extent records.
1385 */
1386int /* error */
1387xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001388 struct xfs_trans *tp, /* transaction pointer */
1389 struct xfs_inode *ip, /* incore inode */
1390 xfs_fileoff_t *last_block, /* last block */
1391 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001392{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001393 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1394 struct xfs_bmbt_irec got;
1395 xfs_extnum_t idx;
1396 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001397
Christoph Hellwig86685f72016-11-24 11:39:38 +11001398 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1399 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001400 *last_block = 0;
1401 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001402 case XFS_DINODE_FMT_BTREE:
1403 case XFS_DINODE_FMT_EXTENTS:
1404 break;
1405 default:
1406 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001407 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001408
1409 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1410 error = xfs_iread_extents(tp, ip, whichfork);
1411 if (error)
1412 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001413 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001414
1415 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1416 if (got.br_startoff <= *last_block - 1)
1417 return 0;
1418 }
1419
1420 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1421 *last_block = got.br_startoff + got.br_blockcount;
1422 return 0;
1423 }
1424
1425 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001426 return 0;
1427}
1428
Dave Chinner68988112013-08-12 20:49:42 +10001429int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001430xfs_bmap_last_extent(
1431 struct xfs_trans *tp,
1432 struct xfs_inode *ip,
1433 int whichfork,
1434 struct xfs_bmbt_irec *rec,
1435 int *is_empty)
1436{
1437 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1438 int error;
1439 int nextents;
1440
1441 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1442 error = xfs_iread_extents(tp, ip, whichfork);
1443 if (error)
1444 return error;
1445 }
1446
Eric Sandeen5d829302016-11-08 12:59:42 +11001447 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001448 if (nextents == 0) {
1449 *is_empty = 1;
1450 return 0;
1451 }
1452
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001453 xfs_iext_get_extent(ifp, nextents - 1, rec);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001454 *is_empty = 0;
1455 return 0;
1456}
1457
1458/*
1459 * Check the last inode extent to determine whether this allocation will result
1460 * in blocks being allocated at the end of the file. When we allocate new data
1461 * blocks at the end of the file which do not start at the previous data block,
1462 * we will try to align the new blocks at stripe unit boundaries.
1463 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001464 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001465 * at, or past the EOF.
1466 */
1467STATIC int
1468xfs_bmap_isaeof(
1469 struct xfs_bmalloca *bma,
1470 int whichfork)
1471{
1472 struct xfs_bmbt_irec rec;
1473 int is_empty;
1474 int error;
1475
Thomas Meyer749f24f2017-10-09 11:38:54 -07001476 bma->aeof = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001477 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1478 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001479 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001480 return error;
1481
Dave Chinner6e708bc2013-11-22 10:41:16 +11001482 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001483 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001484 return 0;
1485 }
1486
Dave Chinner9e5987a2013-02-25 12:31:26 +11001487 /*
1488 * Check if we are allocation or past the last extent, or at least into
1489 * the last delayed allocated extent.
1490 */
1491 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1492 (bma->offset >= rec.br_startoff &&
1493 isnullstartblock(rec.br_startblock));
1494 return 0;
1495}
1496
1497/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001498 * Returns the file-relative block number of the first block past eof in
1499 * the file. This is not based on i_size, it is based on the extent records.
1500 * Returns 0 for local files, as they do not have extent records.
1501 */
1502int
1503xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001504 struct xfs_inode *ip,
1505 xfs_fileoff_t *last_block,
1506 int whichfork)
1507{
1508 struct xfs_bmbt_irec rec;
1509 int is_empty;
1510 int error;
1511
1512 *last_block = 0;
1513
1514 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1515 return 0;
1516
1517 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1518 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001519 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001520
1521 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1522 if (error || is_empty)
1523 return error;
1524
1525 *last_block = rec.br_startoff + rec.br_blockcount;
1526 return 0;
1527}
1528
1529/*
1530 * Returns whether the selected fork of the inode has exactly one
1531 * block or not. For the data fork we check this matches di_size,
1532 * implying the file's range is 0..bsize-1.
1533 */
1534int /* 1=>1 block, 0=>otherwise */
1535xfs_bmap_one_block(
1536 xfs_inode_t *ip, /* incore inode */
1537 int whichfork) /* data or attr fork */
1538{
Dave Chinner9e5987a2013-02-25 12:31:26 +11001539 xfs_ifork_t *ifp; /* inode fork pointer */
1540 int rval; /* return value */
1541 xfs_bmbt_irec_t s; /* internal version of extent */
1542
1543#ifndef DEBUG
1544 if (whichfork == XFS_DATA_FORK)
1545 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1546#endif /* !DEBUG */
1547 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1548 return 0;
1549 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1550 return 0;
1551 ifp = XFS_IFORK_PTR(ip, whichfork);
1552 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001553 xfs_iext_get_extent(ifp, 0, &s);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001554 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1555 if (rval && whichfork == XFS_DATA_FORK)
1556 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1557 return rval;
1558}
1559
1560/*
1561 * Extent tree manipulation functions used during allocation.
1562 */
1563
1564/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001565 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 */
1567STATIC int /* error */
1568xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001569 struct xfs_bmalloca *bma,
1570 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001572 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001575 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 xfs_fileoff_t new_endoff; /* end offset of new entry */
1577 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1578 /* left is 0, right is 1, prev is 2 */
1579 int rval=0; /* return value (logging flags) */
1580 int state = 0;/* state bits, accessed thru macros */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001581 xfs_filblks_t da_new; /* new count del alloc blocks used */
1582 xfs_filblks_t da_old; /* old count del alloc blocks used */
1583 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001585 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001586 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001587 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001589 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001590 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001591 ASSERT(whichfork != XFS_ATTR_FORK);
1592 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1593 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001594
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001595 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001596 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001597 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001598 ASSERT(!bma->cur ||
1599 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001600
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001601 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001602
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603#define LEFT r[0]
1604#define RIGHT r[1]
1605#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Darrick J. Wong60b49842016-10-03 09:11:34 -07001607 if (whichfork == XFS_COW_FORK)
1608 state |= BMAP_COWFORK;
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /*
1611 * Set up a bunch of variables to make the tests simpler.
1612 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001613 xfs_iext_get_extent(ifp, bma->idx, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001615 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 ASSERT(PREV.br_startoff <= new->br_startoff);
1617 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001618
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001619 da_old = startblockval(PREV.br_startblock);
1620 da_new = 0;
1621
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 /*
1623 * Set flags determining what part of the previous delayed allocation
1624 * extent is being replaced by a real allocation.
1625 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001626 if (PREV.br_startoff == new->br_startoff)
1627 state |= BMAP_LEFT_FILLING;
1628 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1629 state |= BMAP_RIGHT_FILLING;
1630
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /*
1632 * Check and set flags if this segment has a left neighbor.
1633 * Don't set contiguous if the combined extent would be too large.
1634 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001635 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001636 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001637 xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001638
1639 if (isnullstartblock(LEFT.br_startblock))
1640 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001642
1643 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1644 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1645 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1646 LEFT.br_state == new->br_state &&
1647 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1648 state |= BMAP_LEFT_CONTIG;
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 /*
1651 * Check and set flags if this segment has a right neighbor.
1652 * Don't set contiguous if the combined extent would be too large.
1653 * Also check for all-three-contiguous being too large.
1654 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001655 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001656 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001657 xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001658
1659 if (isnullstartblock(RIGHT.br_startblock))
1660 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001662
1663 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1664 new_endoff == RIGHT.br_startoff &&
1665 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1666 new->br_state == RIGHT.br_state &&
1667 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1668 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1669 BMAP_RIGHT_FILLING)) !=
1670 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1671 BMAP_RIGHT_FILLING) ||
1672 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1673 <= MAXEXTLEN))
1674 state |= BMAP_RIGHT_CONTIG;
1675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 error = 0;
1677 /*
1678 * Switch out based on the FILLING and CONTIG state bits.
1679 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001680 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1681 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1682 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1683 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 /*
1685 * Filling in all of a previously delayed allocation extent.
1686 * The left and right neighbors are both contiguous with new.
1687 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001688 bma->idx--;
1689 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001690 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
1691 xfs_iext_update_extent(ifp, bma->idx, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001692 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001693
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001694 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001695 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001696 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1698 else {
1699 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001700 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001702 RIGHT.br_blockcount, &i);
1703 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001705 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001706 error = xfs_btree_delete(bma->cur, &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_decrement(bma->cur, 0, &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 Hellwiga67d00a2017-10-17 14:16:26 -07001714 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001715 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 goto done;
1717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 break;
1719
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001720 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /*
1722 * Filling in all of a previously delayed allocation extent.
1723 * The left neighbor is contiguous, the right is not.
1724 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001725 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001726
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001727 old = LEFT;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001728 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001729 LEFT.br_blockcount += PREV.br_blockcount;
1730 xfs_iext_update_extent(ifp, bma->idx, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001731 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001732
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001733 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1734 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 rval = XFS_ILOG_DEXT;
1736 else {
1737 rval = 0;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001738 error = xfs_bmbt_lookup_eq(bma->cur, old.br_startoff,
1739 old.br_startblock, old.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001740 &i);
1741 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001743 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001744 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001745 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 goto done;
1747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 break;
1749
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001750 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 /*
1752 * Filling in all of a previously delayed allocation extent.
1753 * The right neighbor is contiguous, the left is not.
1754 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001755 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001756 PREV.br_startblock = new->br_startblock;
1757 PREV.br_blockcount += RIGHT.br_blockcount;
1758 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001759 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001760
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001761 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1762 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 rval = XFS_ILOG_DEXT;
1764 else {
1765 rval = 0;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001766 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 RIGHT.br_startblock,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001768 RIGHT.br_blockcount, &i);
1769 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001771 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001772 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001773 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 goto done;
1775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 break;
1777
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001778 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 /*
1780 * Filling in all of a previously delayed allocation extent.
1781 * Neither the left nor right neighbors are contiguous with
1782 * the new one.
1783 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001784 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001785 PREV.br_startblock = new->br_startblock;
1786 PREV.br_state = new->br_state;
1787 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001788 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001789
Darrick J. Wong60b49842016-10-03 09:11:34 -07001790 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001791 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1793 else {
1794 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001795 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001797 &i);
1798 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001800 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001801 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1802 error = xfs_btree_insert(bma->cur, &i);
1803 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001805 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 break;
1808
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001809 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 /*
1811 * Filling in the first part of a previous delayed allocation.
1812 * The left neighbor is contiguous.
1813 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001814 old = LEFT;
1815 temp = PREV.br_blockcount - new->br_blockcount;
1816 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1817 startblockval(PREV.br_startblock));
1818
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001820 LEFT.br_blockcount += new->br_blockcount;
1821 xfs_iext_update_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001822 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001823
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001824 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001825 PREV.br_blockcount = temp = PREV.br_blockcount - new->br_blockcount;
1826 PREV.br_startoff += new->br_blockcount;
1827 PREV.br_startblock = nullstartblock(da_new);
1828 xfs_iext_update_extent(ifp, bma->idx, &PREV);
1829 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1830
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001831 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 rval = XFS_ILOG_DEXT;
1833 else {
1834 rval = 0;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001835 error = xfs_bmbt_lookup_eq(bma->cur, old.br_startoff,
1836 old.br_startblock, old.br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001837 &i);
1838 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001840 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001841 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001842 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 goto done;
1844 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001845
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001846 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 break;
1848
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001849 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 /*
1851 * Filling in the first part of a previous delayed allocation.
1852 * The left neighbor is not contiguous.
1853 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001854 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001855 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001856 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1858 else {
1859 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001860 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001862 &i);
1863 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001865 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001866 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1867 error = xfs_btree_insert(bma->cur, &i);
1868 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001870 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001872
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001873 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001874 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001875 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001876 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 rval |= tmp_rval;
1878 if (error)
1879 goto done;
1880 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001881
1882 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001883 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001884 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001885 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001886
1887 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
1888 PREV.br_startoff = new_endoff;
1889 PREV.br_blockcount = temp;
1890 PREV.br_startblock = nullstartblock(da_new);
1891 xfs_iext_update_extent(ifp, bma->idx + 1, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001892 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 break;
1895
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001896 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /*
1898 * Filling in the last part of a previous delayed allocation.
1899 * The right neighbor is contiguous with the new allocation.
1900 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001901 old = RIGHT;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001902 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001903 RIGHT.br_startoff = new->br_startoff;
1904 RIGHT.br_startblock = new->br_startblock;
1905 RIGHT.br_blockcount += new->br_blockcount;
1906 xfs_iext_update_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001907 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001908
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001909 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 rval = XFS_ILOG_DEXT;
1911 else {
1912 rval = 0;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001913 error = xfs_bmbt_lookup_eq(bma->cur, old.br_startoff,
1914 old.br_startblock,
1915 old.br_blockcount, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001916 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001918 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001919 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001920 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 goto done;
1922 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001923
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001924 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001925 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001926 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001927
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001928 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001929 PREV.br_blockcount = temp;
1930 PREV.br_startblock = nullstartblock(da_new);
1931 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001932 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001933
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001934 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 break;
1936
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001937 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 /*
1939 * Filling in the last part of a previous delayed allocation.
1940 * The right neighbor is not contiguous.
1941 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001942 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001943 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001944 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1946 else {
1947 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001948 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001950 &i);
1951 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001953 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001954 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
1955 error = xfs_btree_insert(bma->cur, &i);
1956 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001958 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001960
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001961 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001962 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001963 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001964 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 rval |= tmp_rval;
1966 if (error)
1967 goto done;
1968 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001969
1970 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001971 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001972 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001973 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001974
1975 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1976 PREV.br_startblock = nullstartblock(da_new);
1977 PREV.br_blockcount = temp;
1978 xfs_iext_update_extent(ifp, bma->idx, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001979 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001980
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001981 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 break;
1983
1984 case 0:
1985 /*
1986 * Filling in the middle part of a previous delayed allocation.
1987 * Contiguity is impossible here.
1988 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001989 *
1990 * We start with a delayed allocation:
1991 *
1992 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1993 * PREV @ idx
1994 *
1995 * and we are allocating:
1996 * +rrrrrrrrrrrrrrrrr+
1997 * new
1998 *
1999 * and we set it up for insertion as:
2000 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
2001 * new
2002 * PREV @ idx LEFT RIGHT
2003 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07002005 old = PREV;
2006
2007 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002008 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07002009
2010 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002011 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002012 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07002013 RIGHT.br_blockcount =
2014 PREV.br_startoff + PREV.br_blockcount - new_endoff;
2015 RIGHT.br_startblock =
2016 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
2017 RIGHT.br_blockcount));
2018
2019 /* truncate PREV */
2020 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
2021 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2022 PREV.br_startblock =
2023 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
2024 PREV.br_blockcount));
2025 xfs_iext_update_extent(ifp, bma->idx, &PREV);
2026 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
2027
bpm@sgi.com24446fc2011-01-19 17:41:58 +00002028 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002029 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07002030 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07002031
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002032 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2034 else {
2035 rval = XFS_ILOG_CORE;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002036 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 new->br_startblock, new->br_blockcount,
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002038 &i);
2039 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002041 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002042 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
2043 error = xfs_btree_insert(bma->cur, &i);
2044 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002046 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00002048
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002049 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002050 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002051 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002052 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 rval |= tmp_rval;
2054 if (error)
2055 goto done;
2056 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07002057
2058 da_new = startblockval(PREV.br_startblock) +
2059 startblockval(RIGHT.br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002060 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 break;
2062
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002063 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2064 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2065 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2066 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2067 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2068 case BMAP_LEFT_CONTIG:
2069 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 /*
2071 * These cases are all impossible.
2072 */
2073 ASSERT(0);
2074 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002075
Darrick J. Wong9c194642016-08-03 12:16:05 +10002076 /* add reverse mapping */
2077 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
2078 if (error)
2079 goto done;
2080
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002081 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002082 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002083 int tmp_logflags; /* partial log flag return val */
2084
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002085 ASSERT(bma->cur == NULL);
2086 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002087 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002088 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002089 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002090 if (error)
2091 goto done;
2092 }
2093
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002094 if (bma->cur) {
2095 da_new += bma->cur->bc_private.b.allocated;
2096 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002097 }
2098
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002099 /* adjust for changes in reserved delayed indirect blocks */
2100 if (da_new != da_old) {
2101 ASSERT(state == 0 || da_new < da_old);
2102 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2103 false);
2104 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002105
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002106 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002108 if (whichfork != XFS_COW_FORK)
2109 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return error;
2111#undef LEFT
2112#undef RIGHT
2113#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
2116/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002117 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 */
2119STATIC int /* error */
2120xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002121 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002123 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002124 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002126 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002127 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002128 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002129 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002134 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2137 /* left is 0, right is 1, prev is 2 */
2138 int rval=0; /* return value (logging flags) */
2139 int state = 0;/* state bits, accessed thru macros */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002140 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002141 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002143 *logflagsp = 0;
2144
2145 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002146 ifp = XFS_IFORK_PTR(ip, whichfork);
2147 if (whichfork == XFS_COW_FORK)
2148 state |= BMAP_COWFORK;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002149
2150 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002151 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002152 ASSERT(!isnullstartblock(new->br_startblock));
2153
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002154 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156#define LEFT r[0]
2157#define RIGHT r[1]
2158#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /*
2161 * Set up a bunch of variables to make the tests simpler.
2162 */
2163 error = 0;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002164 xfs_iext_get_extent(ifp, *idx, &PREV);
2165 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 new_endoff = new->br_startoff + new->br_blockcount;
2167 ASSERT(PREV.br_startoff <= new->br_startoff);
2168 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /*
2171 * Set flags determining what part of the previous oldext allocation
2172 * extent is being replaced by a newext allocation.
2173 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002174 if (PREV.br_startoff == new->br_startoff)
2175 state |= BMAP_LEFT_FILLING;
2176 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2177 state |= BMAP_RIGHT_FILLING;
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /*
2180 * Check and set flags if this segment has a left neighbor.
2181 * Don't set contiguous if the combined extent would be too large.
2182 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002183 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002184 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002185 xfs_iext_get_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002186
2187 if (isnullstartblock(LEFT.br_startblock))
2188 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002190
2191 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2192 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2193 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002194 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002195 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2196 state |= BMAP_LEFT_CONTIG;
2197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 /*
2199 * Check and set flags if this segment has a right neighbor.
2200 * Don't set contiguous if the combined extent would be too large.
2201 * Also check for all-three-contiguous being too large.
2202 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002203 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002204 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002205 xfs_iext_get_extent(ifp, *idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002206 if (isnullstartblock(RIGHT.br_startblock))
2207 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002209
2210 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2211 new_endoff == RIGHT.br_startoff &&
2212 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002213 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002214 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2215 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2216 BMAP_RIGHT_FILLING)) !=
2217 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2218 BMAP_RIGHT_FILLING) ||
2219 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2220 <= MAXEXTLEN))
2221 state |= BMAP_RIGHT_CONTIG;
2222
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 /*
2224 * Switch out based on the FILLING and CONTIG state bits.
2225 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002226 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2227 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2228 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2229 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 /*
2231 * Setting all of a previous oldext extent to newext.
2232 * The left and right neighbors are both contiguous with new.
2233 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002234 --*idx;
2235
2236 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002237 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
2238 xfs_iext_update_extent(ifp, *idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002239 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002240
Christoph Hellwigec90c552011-05-23 08:52:53 +00002241 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002242 XFS_IFORK_NEXT_SET(ip, whichfork,
2243 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 if (cur == NULL)
2245 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2246 else {
2247 rval = XFS_ILOG_CORE;
2248 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2249 RIGHT.br_startblock,
2250 RIGHT.br_blockcount, &i)))
2251 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002252 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002253 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002255 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002256 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002258 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002259 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002261 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002262 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002264 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002265 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002266 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 goto done;
2268 }
2269 break;
2270
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002271 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 /*
2273 * Setting all of a previous oldext extent to newext.
2274 * The left neighbor is contiguous, the right is not.
2275 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002276 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002277
Christoph Hellwigec90c552011-05-23 08:52:53 +00002278 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002279 LEFT.br_blockcount += PREV.br_blockcount;
2280 xfs_iext_update_extent(ifp, *idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002281 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2282
2283 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002284 XFS_IFORK_NEXT_SET(ip, whichfork,
2285 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 if (cur == NULL)
2287 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2288 else {
2289 rval = XFS_ILOG_CORE;
2290 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2291 PREV.br_startblock, PREV.br_blockcount,
2292 &i)))
2293 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002294 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002295 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002297 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002298 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002300 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002301 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002302 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 goto done;
2304 }
2305 break;
2306
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002307 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 /*
2309 * Setting all of a previous oldext extent to newext.
2310 * The right neighbor is contiguous, the left is not.
2311 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002312 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002313 PREV.br_blockcount += RIGHT.br_blockcount;
2314 PREV.br_state = new->br_state;
2315 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002316 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002317
Christoph Hellwigec90c552011-05-23 08:52:53 +00002318 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002319 XFS_IFORK_NEXT_SET(ip, whichfork,
2320 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (cur == NULL)
2322 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2323 else {
2324 rval = XFS_ILOG_CORE;
2325 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2326 RIGHT.br_startblock,
2327 RIGHT.br_blockcount, &i)))
2328 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002329 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002330 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002332 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002333 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002335 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002336 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002337 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 goto done;
2339 }
2340 break;
2341
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002342 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 /*
2344 * Setting all of a previous oldext extent to newext.
2345 * Neither the left nor right neighbors are contiguous with
2346 * the new one.
2347 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002348 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002349 PREV.br_state = new->br_state;
2350 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002351 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002352
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 if (cur == NULL)
2354 rval = XFS_ILOG_DEXT;
2355 else {
2356 rval = 0;
2357 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2358 new->br_startblock, new->br_blockcount,
2359 &i)))
2360 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002361 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002362 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002363 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 goto done;
2365 }
2366 break;
2367
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002368 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 /*
2370 * Setting the first part of a previous oldext extent to newext.
2371 * The left neighbor is contiguous.
2372 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002373 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002374 LEFT.br_blockcount += new->br_blockcount;
2375 xfs_iext_update_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002376 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002377
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002378 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002379 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002380 PREV.br_startoff += new->br_blockcount;
2381 PREV.br_startblock += new->br_blockcount;
2382 PREV.br_blockcount -= new->br_blockcount;
2383 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002384 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002385
Christoph Hellwigec90c552011-05-23 08:52:53 +00002386 --*idx;
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (cur == NULL)
2389 rval = XFS_ILOG_DEXT;
2390 else {
2391 rval = 0;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002392 error = xfs_bmbt_lookup_eq(cur, old.br_startoff,
2393 old.br_startblock, old.br_blockcount,
2394 &i);
2395 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002397 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002398 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002399 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002401 error = xfs_btree_decrement(cur, 0, &i);
2402 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002404 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002405 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 goto done;
2407 }
2408 break;
2409
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002410 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 /*
2412 * Setting the first part of a previous oldext extent to newext.
2413 * The left neighbor is not contiguous.
2414 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002415 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002416 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002417 PREV.br_startoff += new->br_blockcount;
2418 PREV.br_startblock += new->br_blockcount;
2419 PREV.br_blockcount -= new->br_blockcount;
2420 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002421 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002422
Christoph Hellwigec90c552011-05-23 08:52:53 +00002423 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002424 XFS_IFORK_NEXT_SET(ip, whichfork,
2425 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (cur == NULL)
2427 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2428 else {
2429 rval = XFS_ILOG_CORE;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002430 error = xfs_bmbt_lookup_eq(cur, old.br_startoff,
2431 old.br_startblock, old.br_blockcount,
2432 &i);
2433 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002435 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002436 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002437 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 goto done;
2439 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002440 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002442 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444 break;
2445
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002446 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 /*
2448 * Setting the last part of a previous oldext extent to newext.
2449 * The right neighbor is contiguous with the new allocation.
2450 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002451 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002452 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002453 PREV.br_blockcount -= new->br_blockcount;
2454 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002455 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2456
2457 ++*idx;
2458
2459 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002460 RIGHT.br_startoff = new->br_startoff;
2461 RIGHT.br_startblock = new->br_startblock;
2462 RIGHT.br_blockcount += new->br_blockcount;
2463 xfs_iext_update_extent(ifp, *idx, &RIGHT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002464 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 if (cur == NULL)
2467 rval = XFS_ILOG_DEXT;
2468 else {
2469 rval = 0;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002470 error = xfs_bmbt_lookup_eq(cur, old.br_startoff,
2471 old.br_startblock, old.br_blockcount,
2472 &i);
2473 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002475 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002476 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002477 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002479 error = xfs_btree_increment(cur, 0, &i);
2480 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002482 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002483 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 goto done;
2485 }
2486 break;
2487
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002488 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 /*
2490 * Setting the last part of a previous oldext extent to newext.
2491 * The right neighbor is not contiguous.
2492 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002493 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002494 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002495 PREV.br_blockcount -= new->br_blockcount;
2496 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002497 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002498
Christoph Hellwigec90c552011-05-23 08:52:53 +00002499 ++*idx;
2500 xfs_iext_insert(ip, *idx, 1, new, state);
2501
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002502 XFS_IFORK_NEXT_SET(ip, whichfork,
2503 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 if (cur == NULL)
2505 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2506 else {
2507 rval = XFS_ILOG_CORE;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002508 error = xfs_bmbt_lookup_eq(cur, old.br_startoff,
2509 old.br_startblock, old.br_blockcount,
2510 &i);
2511 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002513 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002514 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002515 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 goto done;
2517 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2518 new->br_startblock, new->br_blockcount,
2519 &i)))
2520 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002521 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig5e422f52017-10-17 14:16:19 -07002522 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002523 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002525 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 }
2527 break;
2528
2529 case 0:
2530 /*
2531 * Setting the middle part of a previous oldext extent to
2532 * newext. Contiguity is impossible here.
2533 * One extent becomes three extents.
2534 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002535 old = PREV;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002536 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002537 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
2538 xfs_iext_update_extent(ifp, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002539 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002540
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 r[0] = *new;
2542 r[1].br_startoff = new_endoff;
2543 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002544 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002546 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002547
2548 ++*idx;
2549 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2550
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002551 XFS_IFORK_NEXT_SET(ip, whichfork,
2552 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 if (cur == NULL)
2554 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2555 else {
2556 rval = XFS_ILOG_CORE;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002557 error = xfs_bmbt_lookup_eq(cur, old.br_startoff,
2558 old.br_startblock, old.br_blockcount,
2559 &i);
2560 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002562 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002564 error = xfs_bmbt_update(cur, &r[1]);
2565 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 goto done;
2567 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002569 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002571 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002572 /*
2573 * Reset the cursor to the position of the new extent
2574 * we are about to insert as we can't trust it after
2575 * the previous insert.
2576 */
2577 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2578 new->br_startblock, new->br_blockcount,
2579 &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002581 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 /* new middle extent - newext */
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002583 cur->bc_rec.b.br_state = new->br_state;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002584 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002586 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 }
2588 break;
2589
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002590 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2591 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2592 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2593 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2594 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2595 case BMAP_LEFT_CONTIG:
2596 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 /*
2598 * These cases are all impossible.
2599 */
2600 ASSERT(0);
2601 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002602
Darrick J. Wong9c194642016-08-03 12:16:05 +10002603 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002604 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002605 if (error)
2606 goto done;
2607
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002608 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002609 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002610 int tmp_logflags; /* partial log flag return val */
2611
2612 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002613 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002614 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002615 *logflagsp |= tmp_logflags;
2616 if (error)
2617 goto done;
2618 }
2619
2620 /* clear out the allocated field, done with it now in any case. */
2621 if (cur) {
2622 cur->bc_private.b.allocated = 0;
2623 *curp = cur;
2624 }
2625
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002626 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002628 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 return error;
2630#undef LEFT
2631#undef RIGHT
2632#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633}
2634
2635/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002636 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002638STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639xfs_bmap_add_extent_hole_delay(
2640 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002641 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002642 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002643 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002645 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2647 xfs_filblks_t newlen=0; /* new indirect size */
2648 xfs_filblks_t oldlen=0; /* old indirect size */
2649 xfs_bmbt_irec_t right; /* right neighbor extent entry */
2650 int state; /* state bits, accessed thru macros */
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002651 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002653 ifp = XFS_IFORK_PTR(ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 state = 0;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002655 if (whichfork == XFS_COW_FORK)
2656 state |= BMAP_COWFORK;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002657 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002658
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 /*
2660 * Check and set flags if this segment has a left neighbor
2661 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002662 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002663 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002664 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002665
2666 if (isnullstartblock(left.br_startblock))
2667 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002669
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 /*
2671 * Check and set flags if the current (right) segment exists.
2672 * If it doesn't exist, we're converting the hole at end-of-file.
2673 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002674 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002675 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002676 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002677
2678 if (isnullstartblock(right.br_startblock))
2679 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 /*
2683 * Set contiguity flags on the left and right neighbors.
2684 * Don't let extents get too large, even if the pieces are contiguous.
2685 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002686 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2687 left.br_startoff + left.br_blockcount == new->br_startoff &&
2688 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2689 state |= BMAP_LEFT_CONTIG;
2690
2691 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2692 new->br_startoff + new->br_blockcount == right.br_startoff &&
2693 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2694 (!(state & BMAP_LEFT_CONTIG) ||
2695 (left.br_blockcount + new->br_blockcount +
2696 right.br_blockcount <= MAXEXTLEN)))
2697 state |= BMAP_RIGHT_CONTIG;
2698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 /*
2700 * Switch out based on the contiguity flags.
2701 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002702 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2703 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /*
2705 * New allocation is contiguous with delayed allocations
2706 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002707 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002709 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 temp = left.br_blockcount + new->br_blockcount +
2711 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002712
Christoph Hellwigec90c552011-05-23 08:52:53 +00002713 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002714 oldlen = startblockval(left.br_startblock) +
2715 startblockval(new->br_startblock) +
2716 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002717 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2718 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002719 left.br_startblock = nullstartblock(newlen);
2720 left.br_blockcount = temp;
2721 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002722 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002723
Christoph Hellwigec90c552011-05-23 08:52:53 +00002724 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 break;
2726
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002727 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 /*
2729 * New allocation is contiguous with a delayed allocation
2730 * on the left.
2731 * Merge the new allocation with the left neighbor.
2732 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002733 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002735
2736 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002737 oldlen = startblockval(left.br_startblock) +
2738 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002739 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2740 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002741 left.br_blockcount = temp;
2742 left.br_startblock = nullstartblock(newlen);
2743 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002744 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 break;
2746
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002747 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 /*
2749 * New allocation is contiguous with a delayed allocation
2750 * on the right.
2751 * Merge the new allocation with the right neighbor.
2752 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002753 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002755 oldlen = startblockval(new->br_startblock) +
2756 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002757 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2758 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002759 right.br_startoff = new->br_startoff;
2760 right.br_startblock = nullstartblock(newlen);
2761 right.br_blockcount = temp;
2762 xfs_iext_update_extent(ifp, *idx, &right);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002763 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 break;
2765
2766 case 0:
2767 /*
2768 * New allocation is not contiguous with another
2769 * delayed allocation.
2770 * Insert a new entry.
2771 */
2772 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002773 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 break;
2775 }
2776 if (oldlen != newlen) {
2777 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002778 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2779 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 /*
2781 * Nothing to do for disk quota accounting here.
2782 */
2783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784}
2785
2786/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002787 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 */
2789STATIC int /* error */
2790xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002791 struct xfs_trans *tp,
2792 struct xfs_inode *ip,
2793 int whichfork,
2794 xfs_extnum_t *idx,
2795 struct xfs_btree_cur **curp,
2796 struct xfs_bmbt_irec *new,
2797 xfs_fsblock_t *first,
2798 struct xfs_defer_ops *dfops,
2799 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002801 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2802 struct xfs_mount *mp = ip->i_mount;
2803 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2807 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002808 int rval=0; /* return value (logging flags) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 int state; /* state bits, accessed thru macros */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002810 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Christoph Hellwig6d045582017-04-11 16:45:54 -07002812 ASSERT(*idx >= 0);
2813 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002814 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002815 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002816
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002817 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002818
2819 state = 0;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002820 if (whichfork == XFS_ATTR_FORK)
2821 state |= BMAP_ATTRFORK;
Christoph Hellwiga14234c2017-02-06 10:50:49 -08002822 if (whichfork == XFS_COW_FORK)
2823 state |= BMAP_COWFORK;
Christoph Hellwig6ef35542009-11-25 00:00:21 +00002824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 /*
2826 * Check and set flags if this segment has a left neighbor.
2827 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002828 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002829 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002830 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002831 if (isnullstartblock(left.br_startblock))
2832 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 /*
2836 * Check and set flags if this segment has a current value.
2837 * Not true if we're inserting into the "hole" at eof.
2838 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002839 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002840 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002841 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002842 if (isnullstartblock(right.br_startblock))
2843 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002845
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 /*
2847 * We're inserting a real allocation between "left" and "right".
2848 * Set the contiguity flags. Don't let extents get too large.
2849 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002850 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2851 left.br_startoff + left.br_blockcount == new->br_startoff &&
2852 left.br_startblock + left.br_blockcount == new->br_startblock &&
2853 left.br_state == new->br_state &&
2854 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2855 state |= BMAP_LEFT_CONTIG;
2856
2857 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2858 new->br_startoff + new->br_blockcount == right.br_startoff &&
2859 new->br_startblock + new->br_blockcount == right.br_startblock &&
2860 new->br_state == right.br_state &&
2861 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2862 (!(state & BMAP_LEFT_CONTIG) ||
2863 left.br_blockcount + new->br_blockcount +
2864 right.br_blockcount <= MAXEXTLEN))
2865 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002867 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 /*
2869 * Select which case we're in here, and implement it.
2870 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002871 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2872 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 /*
2874 * New allocation is contiguous with real allocations on the
2875 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002876 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002878 --*idx;
2879 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002880 left.br_blockcount += new->br_blockcount + right.br_blockcount;
2881 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002882 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002883
Christoph Hellwig6d045582017-04-11 16:45:54 -07002884 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002885
Christoph Hellwig6d045582017-04-11 16:45:54 -07002886 XFS_IFORK_NEXT_SET(ip, whichfork,
2887 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2888 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002889 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002890 } else {
2891 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002892 error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002893 right.br_startblock, right.br_blockcount,
2894 &i);
2895 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002896 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002897 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002898 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002899 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002900 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002901 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002902 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002903 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002904 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002905 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002906 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002907 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002908 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002910 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002912 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 /*
2914 * New allocation is contiguous with a real allocation
2915 * on the left.
2916 * Merge the new allocation with the left neighbor.
2917 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002918 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002919 old = left;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002920 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002921 left.br_blockcount += new->br_blockcount;
2922 xfs_iext_update_extent(ifp, *idx, &left);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002923 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002924
Christoph Hellwig6d045582017-04-11 16:45:54 -07002925 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002926 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002927 } else {
2928 rval = 0;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002929 error = xfs_bmbt_lookup_eq(cur, old.br_startoff,
2930 old.br_startblock, old.br_blockcount,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002931 &i);
2932 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002933 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002934 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002935 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002936 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002937 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002939 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002941 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 /*
2943 * New allocation is contiguous with a real allocation
2944 * on the right.
2945 * Merge the new allocation with the right neighbor.
2946 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002947 old = right;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002948 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002949 right.br_startoff = new->br_startoff;
2950 right.br_startblock = new->br_startblock;
2951 right.br_blockcount += new->br_blockcount;
2952 xfs_iext_update_extent(ifp, *idx, &right);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002953 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002954
Christoph Hellwig6d045582017-04-11 16:45:54 -07002955 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002956 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002957 } else {
2958 rval = 0;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002959 error = xfs_bmbt_lookup_eq(cur, old.br_startoff,
2960 old.br_startblock, old.br_blockcount,
2961 &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002962 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002963 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002964 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002965 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002966 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002967 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002969 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970
2971 case 0:
2972 /*
2973 * New allocation is not contiguous with another
2974 * real allocation.
2975 * Insert a new entry.
2976 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002977 xfs_iext_insert(ip, *idx, 1, new, state);
2978 XFS_IFORK_NEXT_SET(ip, whichfork,
2979 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2980 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002981 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002982 } else {
2983 rval = XFS_ILOG_CORE;
Christoph Hellwig6d045582017-04-11 16:45:54 -07002984 error = xfs_bmbt_lookup_eq(cur,
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002985 new->br_startoff,
2986 new->br_startblock,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002987 new->br_blockcount, &i);
2988 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002989 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002990 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002991 cur->bc_rec.b.br_state = new->br_state;
2992 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002993 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002994 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002995 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002997 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002999
Darrick J. Wong9c194642016-08-03 12:16:05 +10003000 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003001 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10003002 if (error)
3003 goto done;
3004
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003005 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003006 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003007 int tmp_logflags; /* partial log flag return val */
3008
Christoph Hellwig6d045582017-04-11 16:45:54 -07003009 ASSERT(cur == NULL);
3010 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00003011 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07003012 *logflagsp |= tmp_logflags;
3013 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00003014 if (error)
3015 goto done;
3016 }
3017
3018 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07003019 if (cur)
3020 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00003021
Christoph Hellwig6d045582017-04-11 16:45:54 -07003022 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003023done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07003024 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10003025 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
Nathan Scottdd9f4382006-01-11 15:28:28 +11003028/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11003029 * Functions used in the extent read, allocate and remove paths
3030 */
3031
3032/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11003033 * Adjust the size of the new extent based on di_extsize and rt extsize.
3034 */
Dave Chinner68988112013-08-12 20:49:42 +10003035int
Nathan Scottdd9f4382006-01-11 15:28:28 +11003036xfs_bmap_extsize_align(
3037 xfs_mount_t *mp,
3038 xfs_bmbt_irec_t *gotp, /* next extent pointer */
3039 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
3040 xfs_extlen_t extsz, /* align to this extent size */
3041 int rt, /* is this a realtime inode? */
3042 int eof, /* is extent at end-of-file? */
3043 int delay, /* creating delalloc extent? */
3044 int convert, /* overwriting unwritten extent? */
3045 xfs_fileoff_t *offp, /* in/out: aligned offset */
3046 xfs_extlen_t *lenp) /* in/out: aligned length */
3047{
3048 xfs_fileoff_t orig_off; /* original offset */
3049 xfs_extlen_t orig_alen; /* original length */
3050 xfs_fileoff_t orig_end; /* original off+len */
3051 xfs_fileoff_t nexto; /* next file offset */
3052 xfs_fileoff_t prevo; /* previous file offset */
3053 xfs_fileoff_t align_off; /* temp for offset */
3054 xfs_extlen_t align_alen; /* temp for length */
3055 xfs_extlen_t temp; /* temp for calculations */
3056
3057 if (convert)
3058 return 0;
3059
3060 orig_off = align_off = *offp;
3061 orig_alen = align_alen = *lenp;
3062 orig_end = orig_off + orig_alen;
3063
3064 /*
3065 * If this request overlaps an existing extent, then don't
3066 * attempt to perform any additional alignment.
3067 */
3068 if (!delay && !eof &&
3069 (orig_off >= gotp->br_startoff) &&
3070 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
3071 return 0;
3072 }
3073
3074 /*
3075 * If the file offset is unaligned vs. the extent size
3076 * we need to align it. This will be possible unless
3077 * the file was previously written with a kernel that didn't
3078 * perform this alignment, or if a truncate shot us in the
3079 * foot.
3080 */
3081 temp = do_mod(orig_off, extsz);
3082 if (temp) {
3083 align_alen += temp;
3084 align_off -= temp;
3085 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10003086
3087 /* Same adjustment for the end of the requested area. */
3088 temp = (align_alen % extsz);
3089 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11003090 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10003091
3092 /*
3093 * For large extent hint sizes, the aligned extent might be larger than
3094 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
3095 * the length back under MAXEXTLEN. The outer allocation loops handle
3096 * short allocation just fine, so it is safe to do this. We only want to
3097 * do it when we are forced to, though, because it means more allocation
3098 * operations are required.
3099 */
3100 while (align_alen > MAXEXTLEN)
3101 align_alen -= extsz;
3102 ASSERT(align_alen <= MAXEXTLEN);
3103
Nathan Scottdd9f4382006-01-11 15:28:28 +11003104 /*
3105 * If the previous block overlaps with this proposed allocation
3106 * then move the start forward without adjusting the length.
3107 */
3108 if (prevp->br_startoff != NULLFILEOFF) {
3109 if (prevp->br_startblock == HOLESTARTBLOCK)
3110 prevo = prevp->br_startoff;
3111 else
3112 prevo = prevp->br_startoff + prevp->br_blockcount;
3113 } else
3114 prevo = 0;
3115 if (align_off != orig_off && align_off < prevo)
3116 align_off = prevo;
3117 /*
3118 * If the next block overlaps with this proposed allocation
3119 * then move the start back without adjusting the length,
3120 * but not before offset 0.
3121 * This may of course make the start overlap previous block,
3122 * and if we hit the offset 0 limit then the next block
3123 * can still overlap too.
3124 */
3125 if (!eof && gotp->br_startoff != NULLFILEOFF) {
3126 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
3127 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
3128 nexto = gotp->br_startoff + gotp->br_blockcount;
3129 else
3130 nexto = gotp->br_startoff;
3131 } else
3132 nexto = NULLFILEOFF;
3133 if (!eof &&
3134 align_off + align_alen != orig_end &&
3135 align_off + align_alen > nexto)
3136 align_off = nexto > align_alen ? nexto - align_alen : 0;
3137 /*
3138 * If we're now overlapping the next or previous extent that
3139 * means we can't fit an extsz piece in this hole. Just move
3140 * the start forward to the first valid spot and set
3141 * the length so we hit the end.
3142 */
3143 if (align_off != orig_off && align_off < prevo)
3144 align_off = prevo;
3145 if (align_off + align_alen != orig_end &&
3146 align_off + align_alen > nexto &&
3147 nexto != NULLFILEOFF) {
3148 ASSERT(nexto > prevo);
3149 align_alen = nexto - align_off;
3150 }
3151
3152 /*
3153 * If realtime, and the result isn't a multiple of the realtime
3154 * extent size we need to remove blocks until it is.
3155 */
3156 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3157 /*
3158 * We're not covering the original request, or
3159 * we won't be able to once we fix the length.
3160 */
3161 if (orig_off < align_off ||
3162 orig_end > align_off + align_alen ||
3163 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003164 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003165 /*
3166 * Try to fix it by moving the start up.
3167 */
3168 if (align_off + temp <= orig_off) {
3169 align_alen -= temp;
3170 align_off += temp;
3171 }
3172 /*
3173 * Try to fix it by moving the end in.
3174 */
3175 else if (align_off + align_alen - temp >= orig_end)
3176 align_alen -= temp;
3177 /*
3178 * Set the start to the minimum then trim the length.
3179 */
3180 else {
3181 align_alen -= orig_off - align_off;
3182 align_off = orig_off;
3183 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3184 }
3185 /*
3186 * Result doesn't cover the request, fail it.
3187 */
3188 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003189 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003190 } else {
3191 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003192 /* see MAXEXTLEN handling above */
3193 ASSERT(orig_end <= align_off + align_alen ||
3194 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003195 }
3196
3197#ifdef DEBUG
3198 if (!eof && gotp->br_startoff != NULLFILEOFF)
3199 ASSERT(align_off + align_alen <= gotp->br_startoff);
3200 if (prevp->br_startoff != NULLFILEOFF)
3201 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3202#endif
3203
3204 *lenp = align_alen;
3205 *offp = align_off;
3206 return 0;
3207}
3208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209#define XFS_ALLOC_GAP_UNITS 4
3210
Dave Chinner68988112013-08-12 20:49:42 +10003211void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003212xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003213 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
3215 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3217 xfs_mount_t *mp; /* mount point structure */
3218 int nullfb; /* true if ap->firstblock isn't set */
3219 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220
3221#define ISVALID(x,y) \
3222 (rt ? \
3223 (x) < mp->m_sb.sb_rblocks : \
3224 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3225 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3226 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3227
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003229 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003230 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3231 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003232 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 /*
3234 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003235 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003237 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3238 !isnullstartblock(ap->prev.br_startblock) &&
3239 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3240 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003241 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 /*
3243 * Adjust for the gap between prevp and us.
3244 */
Dave Chinner3a756672011-09-18 20:40:58 +00003245 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003246 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003248 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3249 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 }
3251 /*
3252 * If not at eof, then compare the two neighbor blocks.
3253 * Figure out whether either one gives us a good starting point,
3254 * and pick the better one.
3255 */
3256 else if (!ap->eof) {
3257 xfs_fsblock_t gotbno; /* right side block number */
3258 xfs_fsblock_t gotdiff=0; /* right side difference */
3259 xfs_fsblock_t prevbno; /* left side block number */
3260 xfs_fsblock_t prevdiff=0; /* left side difference */
3261
3262 /*
3263 * If there's a previous (left) block, select a requested
3264 * start block based on it.
3265 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003266 if (ap->prev.br_startoff != NULLFILEOFF &&
3267 !isnullstartblock(ap->prev.br_startblock) &&
3268 (prevbno = ap->prev.br_startblock +
3269 ap->prev.br_blockcount) &&
3270 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 /*
3272 * Calculate gap to end of previous block.
3273 */
Dave Chinner3a756672011-09-18 20:40:58 +00003274 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003275 (ap->prev.br_startoff +
3276 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 /*
3278 * Figure the startblock based on the previous block's
3279 * end and the gap size.
3280 * Heuristic!
3281 * If the gap is large relative to the piece we're
3282 * allocating, or using it gives us an invalid block
3283 * number, then just use the end of the previous block.
3284 */
Dave Chinner3a756672011-09-18 20:40:58 +00003285 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003287 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 prevbno += adjust;
3289 else
3290 prevdiff += adjust;
3291 /*
3292 * If the firstblock forbids it, can't use it,
3293 * must use default.
3294 */
3295 if (!rt && !nullfb &&
3296 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3297 prevbno = NULLFSBLOCK;
3298 }
3299 /*
3300 * No previous block or can't follow it, just default.
3301 */
3302 else
3303 prevbno = NULLFSBLOCK;
3304 /*
3305 * If there's a following (right) block, select a requested
3306 * start block based on it.
3307 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003308 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 /*
3310 * Calculate gap to start of next block.
3311 */
Dave Chinner3a756672011-09-18 20:40:58 +00003312 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 /*
3314 * Figure the startblock based on the next block's
3315 * start and the gap size.
3316 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003317 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 /*
3319 * Heuristic!
3320 * If the gap is large relative to the piece we're
3321 * allocating, or using it gives us an invalid block
3322 * number, then just use the start of the next block
3323 * offset by our length.
3324 */
Dave Chinner3a756672011-09-18 20:40:58 +00003325 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326 ISVALID(gotbno - gotdiff, gotbno))
3327 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003328 else if (ISVALID(gotbno - ap->length, gotbno)) {
3329 gotbno -= ap->length;
3330 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 } else
3332 gotdiff += adjust;
3333 /*
3334 * If the firstblock forbids it, can't use it,
3335 * must use default.
3336 */
3337 if (!rt && !nullfb &&
3338 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3339 gotbno = NULLFSBLOCK;
3340 }
3341 /*
3342 * No next block, just default.
3343 */
3344 else
3345 gotbno = NULLFSBLOCK;
3346 /*
3347 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003348 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 */
3350 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003351 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003353 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003355 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003357#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003358}
3359
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003360static int
3361xfs_bmap_longest_free_extent(
3362 struct xfs_trans *tp,
3363 xfs_agnumber_t ag,
3364 xfs_extlen_t *blen,
3365 int *notinit)
3366{
3367 struct xfs_mount *mp = tp->t_mountp;
3368 struct xfs_perag *pag;
3369 xfs_extlen_t longest;
3370 int error = 0;
3371
3372 pag = xfs_perag_get(mp, ag);
3373 if (!pag->pagf_init) {
3374 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3375 if (error)
3376 goto out;
3377
3378 if (!pag->pagf_init) {
3379 *notinit = 1;
3380 goto out;
3381 }
3382 }
3383
Dave Chinner50adbcb2015-06-22 10:04:31 +10003384 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003385 xfs_alloc_min_freelist(mp, pag),
3386 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003387 if (*blen < longest)
3388 *blen = longest;
3389
3390out:
3391 xfs_perag_put(pag);
3392 return error;
3393}
3394
3395static void
3396xfs_bmap_select_minlen(
3397 struct xfs_bmalloca *ap,
3398 struct xfs_alloc_arg *args,
3399 xfs_extlen_t *blen,
3400 int notinit)
3401{
3402 if (notinit || *blen < ap->minlen) {
3403 /*
3404 * Since we did a BUF_TRYLOCK above, it is possible that
3405 * there is space for this request.
3406 */
3407 args->minlen = ap->minlen;
3408 } else if (*blen < args->maxlen) {
3409 /*
3410 * If the best seen length is less than the request length,
3411 * use the best as the minimum.
3412 */
3413 args->minlen = *blen;
3414 } else {
3415 /*
3416 * Otherwise we've seen an extent as big as maxlen, use that
3417 * as the minimum.
3418 */
3419 args->minlen = args->maxlen;
3420 }
3421}
3422
Nathan Scotta365bdd2006-03-14 13:34:16 +11003423STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003424xfs_bmap_btalloc_nullfb(
3425 struct xfs_bmalloca *ap,
3426 struct xfs_alloc_arg *args,
3427 xfs_extlen_t *blen)
3428{
3429 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003430 xfs_agnumber_t ag, startag;
3431 int notinit = 0;
3432 int error;
3433
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003434 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003435 args->total = ap->total;
3436
Christoph Hellwigc467c042010-02-15 23:34:42 +00003437 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3438 if (startag == NULLAGNUMBER)
3439 startag = ag = 0;
3440
Dave Chinner14b064c2011-01-27 12:16:28 +11003441 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003442 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3443 &notinit);
3444 if (error)
3445 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003446
Christoph Hellwigc467c042010-02-15 23:34:42 +00003447 if (++ag == mp->m_sb.sb_agcount)
3448 ag = 0;
3449 if (ag == startag)
3450 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003451 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003452
3453 xfs_bmap_select_minlen(ap, args, blen, notinit);
3454 return 0;
3455}
3456
3457STATIC int
3458xfs_bmap_btalloc_filestreams(
3459 struct xfs_bmalloca *ap,
3460 struct xfs_alloc_arg *args,
3461 xfs_extlen_t *blen)
3462{
3463 struct xfs_mount *mp = ap->ip->i_mount;
3464 xfs_agnumber_t ag;
3465 int notinit = 0;
3466 int error;
3467
3468 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3469 args->total = ap->total;
3470
3471 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3472 if (ag == NULLAGNUMBER)
3473 ag = 0;
3474
3475 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3476 if (error)
3477 return error;
3478
3479 if (*blen < args->maxlen) {
3480 error = xfs_filestream_new_ag(ap, &ag);
3481 if (error)
3482 return error;
3483
3484 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3485 &notinit);
3486 if (error)
3487 return error;
3488
3489 }
3490
3491 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003492
3493 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003494 * Set the failure fallback case to look in the selected AG as stream
3495 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003496 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003497 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003498 return 0;
3499}
3500
3501STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003502xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003503 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003504{
3505 xfs_mount_t *mp; /* mount point structure */
3506 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003507 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003508 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003509 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003510 xfs_alloc_arg_t args;
3511 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003512 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003513 int nullfb; /* true if ap->firstblock isn't set */
3514 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003515 int tryagain;
3516 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003517 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003518
Dave Chinnera99ebf42011-12-01 11:24:20 +00003519 ASSERT(ap->length);
3520
Nathan Scotta365bdd2006-03-14 13:34:16 +11003521 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003522
3523 /* stripe alignment for allocation is determined by mount parameters */
3524 stripe_align = 0;
3525 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3526 stripe_align = mp->m_swidth;
3527 else if (mp->m_dalign)
3528 stripe_align = mp->m_dalign;
3529
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003530 if (ap->flags & XFS_BMAPI_COWFORK)
3531 align = xfs_get_cowextsz_hint(ap->ip);
3532 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003533 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003534 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003535 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003536 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003537 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003538 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003539 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003540 }
Dave Chinner33177f052013-12-12 16:34:36 +11003541
3542
Dave Chinner0937e0f2011-09-18 20:40:57 +00003543 nullfb = *ap->firstblock == NULLFSBLOCK;
3544 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003545 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003546 if (xfs_alloc_is_userdata(ap->datatype) &&
3547 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003548 ag = xfs_filestream_lookup_ag(ap->ip);
3549 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003550 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003551 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003552 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003553 }
3554 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003555 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003556
3557 xfs_bmap_adjacent(ap);
3558
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003560 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 * it's in the right allocation group.
3562 */
Dave Chinner3a756672011-09-18 20:40:58 +00003563 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 ;
3565 else
Dave Chinner3a756672011-09-18 20:40:58 +00003566 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568 * Normal allocation, done through xfs_alloc_vextent.
3569 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003570 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003571 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003572 args.tp = ap->tp;
3573 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003574 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003575 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003576
3577 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003578 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003579 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003580 blen = 0;
3581 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003582 /*
3583 * Search for an allocation group with a single extent large
3584 * enough for the request. If one isn't found, then adjust
3585 * the minimum allocation size to the largest space found.
3586 */
Dave Chinner292378e2016-09-26 08:21:28 +10003587 if (xfs_alloc_is_userdata(ap->datatype) &&
3588 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003589 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3590 else
3591 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003592 if (error)
3593 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003594 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003595 if (xfs_inode_is_filestream(ap->ip))
3596 args.type = XFS_ALLOCTYPE_FIRST_AG;
3597 else
3598 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003599 args.total = args.minlen = ap->minlen;
3600 } else {
3601 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3602 args.total = ap->total;
3603 args.minlen = ap->minlen;
3604 }
David Chinner957d0eb2007-06-18 16:50:37 +10003605 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003606 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003607 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003608 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003609 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003610 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003611 args.prod = 1;
3612 args.mod = 0;
3613 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003614 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003615 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003616 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3617 }
3618 /*
3619 * If we are not low on available data blocks, and the
3620 * underlying logical volume manager is a stripe, and
3621 * the file offset is zero then try to allocate data
3622 * blocks on stripe unit boundary.
3623 * NOTE: ap->aeof is only set if the allocation length
3624 * is >= the stripe unit and the allocation offset is
3625 * at the end of file.
3626 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003627 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003628 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003629 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003630 atype = args.type;
3631 isaligned = 1;
3632 /*
3633 * Adjust for alignment
3634 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003635 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003636 args.minlen = blen - args.alignment;
3637 args.minalignslop = 0;
3638 } else {
3639 /*
3640 * First try an exact bno allocation.
3641 * If it fails then do a near or start bno
3642 * allocation with alignment turned on.
3643 */
3644 atype = args.type;
3645 tryagain = 1;
3646 args.type = XFS_ALLOCTYPE_THIS_BNO;
3647 args.alignment = 1;
3648 /*
3649 * Compute the minlen+alignment for the
3650 * next case. Set slop so that the value
3651 * of minlen+alignment+slop doesn't go up
3652 * between the calls.
3653 */
Dave Chinner33177f052013-12-12 16:34:36 +11003654 if (blen > stripe_align && blen <= args.maxlen)
3655 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003656 else
3657 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003658 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003659 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003660 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003661 args.minlen - 1;
3662 else
3663 args.minalignslop = 0;
3664 }
3665 } else {
3666 args.alignment = 1;
3667 args.minalignslop = 0;
3668 }
3669 args.minleft = ap->minleft;
3670 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003671 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003672 args.datatype = ap->datatype;
3673 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003674 args.ip = ap->ip;
3675
3676 error = xfs_alloc_vextent(&args);
3677 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003678 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003679
Nathan Scotta365bdd2006-03-14 13:34:16 +11003680 if (tryagain && args.fsbno == NULLFSBLOCK) {
3681 /*
3682 * Exact allocation failed. Now try with alignment
3683 * turned on.
3684 */
3685 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003686 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003687 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003688 args.minlen = nextminlen;
3689 args.minalignslop = 0;
3690 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 if ((error = xfs_alloc_vextent(&args)))
3692 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003693 }
3694 if (isaligned && args.fsbno == NULLFSBLOCK) {
3695 /*
3696 * allocation failed, so turn off alignment and
3697 * try again.
3698 */
3699 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003700 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003701 args.alignment = 0;
3702 if ((error = xfs_alloc_vextent(&args)))
3703 return error;
3704 }
3705 if (args.fsbno == NULLFSBLOCK && nullfb &&
3706 args.minlen > ap->minlen) {
3707 args.minlen = ap->minlen;
3708 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003709 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003710 if ((error = xfs_alloc_vextent(&args)))
3711 return error;
3712 }
3713 if (args.fsbno == NULLFSBLOCK && nullfb) {
3714 args.fsbno = 0;
3715 args.type = XFS_ALLOCTYPE_FIRST_AG;
3716 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003717 if ((error = xfs_alloc_vextent(&args)))
3718 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003719 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003720 }
3721 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003722 /*
3723 * check the allocation happened at the same or higher AG than
3724 * the first block that was allocated.
3725 */
3726 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003727 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3728 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003729
Dave Chinner3a756672011-09-18 20:40:58 +00003730 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003731 if (*ap->firstblock == NULLFSBLOCK)
3732 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003733 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003734 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003735 if (!(ap->flags & XFS_BMAPI_COWFORK))
3736 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003737 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3738 if (ap->wasdel)
3739 ap->ip->i_delayed_blks -= args.len;
3740 /*
3741 * Adjust the disk quota also. This was reserved
3742 * earlier.
3743 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003744 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003745 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3746 XFS_TRANS_DQ_BCOUNT,
3747 (long) args.len);
3748 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003749 ap->blkno = NULLFSBLOCK;
3750 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 }
3752 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003753}
3754
3755/*
3756 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3757 * It figures out where to ask the underlying allocator to put the new extent.
3758 */
3759STATIC int
3760xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003761 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003762{
Dave Chinner292378e2016-09-26 08:21:28 +10003763 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3764 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003765 return xfs_bmap_rtalloc(ap);
3766 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767}
3768
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003769/* Trim extent to fit a logical block range. */
3770void
3771xfs_trim_extent(
3772 struct xfs_bmbt_irec *irec,
3773 xfs_fileoff_t bno,
3774 xfs_filblks_t len)
3775{
3776 xfs_fileoff_t distance;
3777 xfs_fileoff_t end = bno + len;
3778
3779 if (irec->br_startoff + irec->br_blockcount <= bno ||
3780 irec->br_startoff >= end) {
3781 irec->br_blockcount = 0;
3782 return;
3783 }
3784
3785 if (irec->br_startoff < bno) {
3786 distance = bno - irec->br_startoff;
3787 if (isnullstartblock(irec->br_startblock))
3788 irec->br_startblock = DELAYSTARTBLOCK;
3789 if (irec->br_startblock != DELAYSTARTBLOCK &&
3790 irec->br_startblock != HOLESTARTBLOCK)
3791 irec->br_startblock += distance;
3792 irec->br_startoff += distance;
3793 irec->br_blockcount -= distance;
3794 }
3795
3796 if (end < irec->br_startoff + irec->br_blockcount) {
3797 distance = irec->br_startoff + irec->br_blockcount - end;
3798 irec->br_blockcount -= distance;
3799 }
3800}
3801
Brian Foster40214d12017-10-13 09:47:46 -07003802/* trim extent to within eof */
3803void
3804xfs_trim_extent_eof(
3805 struct xfs_bmbt_irec *irec,
3806 struct xfs_inode *ip)
3807
3808{
3809 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3810 i_size_read(VFS_I(ip))));
3811}
3812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003814 * Trim the returned map to the required bounds
3815 */
3816STATIC void
3817xfs_bmapi_trim_map(
3818 struct xfs_bmbt_irec *mval,
3819 struct xfs_bmbt_irec *got,
3820 xfs_fileoff_t *bno,
3821 xfs_filblks_t len,
3822 xfs_fileoff_t obno,
3823 xfs_fileoff_t end,
3824 int n,
3825 int flags)
3826{
3827 if ((flags & XFS_BMAPI_ENTIRE) ||
3828 got->br_startoff + got->br_blockcount <= obno) {
3829 *mval = *got;
3830 if (isnullstartblock(got->br_startblock))
3831 mval->br_startblock = DELAYSTARTBLOCK;
3832 return;
3833 }
3834
3835 if (obno > *bno)
3836 *bno = obno;
3837 ASSERT((*bno >= obno) || (n == 0));
3838 ASSERT(*bno < end);
3839 mval->br_startoff = *bno;
3840 if (isnullstartblock(got->br_startblock))
3841 mval->br_startblock = DELAYSTARTBLOCK;
3842 else
3843 mval->br_startblock = got->br_startblock +
3844 (*bno - got->br_startoff);
3845 /*
3846 * Return the minimum of what we got and what we asked for for
3847 * the length. We can use the len variable here because it is
3848 * modified below and we could have been there before coming
3849 * here if the first part of the allocation didn't overlap what
3850 * was asked for.
3851 */
3852 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3853 got->br_blockcount - (*bno - got->br_startoff));
3854 mval->br_state = got->br_state;
3855 ASSERT(mval->br_blockcount <= len);
3856 return;
3857}
3858
3859/*
3860 * Update and validate the extent map to return
3861 */
3862STATIC void
3863xfs_bmapi_update_map(
3864 struct xfs_bmbt_irec **map,
3865 xfs_fileoff_t *bno,
3866 xfs_filblks_t *len,
3867 xfs_fileoff_t obno,
3868 xfs_fileoff_t end,
3869 int *n,
3870 int flags)
3871{
3872 xfs_bmbt_irec_t *mval = *map;
3873
3874 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3875 ((mval->br_startoff + mval->br_blockcount) <= end));
3876 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3877 (mval->br_startoff < obno));
3878
3879 *bno = mval->br_startoff + mval->br_blockcount;
3880 *len = end - *bno;
3881 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3882 /* update previous map with new information */
3883 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3884 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3885 ASSERT(mval->br_state == mval[-1].br_state);
3886 mval[-1].br_blockcount = mval->br_blockcount;
3887 mval[-1].br_state = mval->br_state;
3888 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3889 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3890 mval[-1].br_startblock != HOLESTARTBLOCK &&
3891 mval->br_startblock == mval[-1].br_startblock +
3892 mval[-1].br_blockcount &&
3893 ((flags & XFS_BMAPI_IGSTATE) ||
3894 mval[-1].br_state == mval->br_state)) {
3895 ASSERT(mval->br_startoff ==
3896 mval[-1].br_startoff + mval[-1].br_blockcount);
3897 mval[-1].br_blockcount += mval->br_blockcount;
3898 } else if (*n > 0 &&
3899 mval->br_startblock == DELAYSTARTBLOCK &&
3900 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3901 mval->br_startoff ==
3902 mval[-1].br_startoff + mval[-1].br_blockcount) {
3903 mval[-1].br_blockcount += mval->br_blockcount;
3904 mval[-1].br_state = mval->br_state;
3905 } else if (!((*n == 0) &&
3906 ((mval->br_startoff + mval->br_blockcount) <=
3907 obno))) {
3908 mval++;
3909 (*n)++;
3910 }
3911 *map = mval;
3912}
3913
3914/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003915 * Map file blocks to filesystem blocks without allocation.
3916 */
3917int
3918xfs_bmapi_read(
3919 struct xfs_inode *ip,
3920 xfs_fileoff_t bno,
3921 xfs_filblks_t len,
3922 struct xfs_bmbt_irec *mval,
3923 int *nmap,
3924 int flags)
3925{
3926 struct xfs_mount *mp = ip->i_mount;
3927 struct xfs_ifork *ifp;
3928 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003929 xfs_fileoff_t obno;
3930 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003931 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003932 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003933 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003934 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003935 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003936
3937 ASSERT(*nmap >= 1);
3938 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003939 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003940 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003941
3942 if (unlikely(XFS_TEST_ERROR(
3943 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3944 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003945 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003946 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003947 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003948 }
3949
3950 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003951 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003952
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003953 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003954
3955 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003956
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003957 /* No CoW fork? Return a hole. */
3958 if (whichfork == XFS_COW_FORK && !ifp) {
3959 mval->br_startoff = bno;
3960 mval->br_startblock = HOLESTARTBLOCK;
3961 mval->br_blockcount = len;
3962 mval->br_state = XFS_EXT_NORM;
3963 *nmap = 1;
3964 return 0;
3965 }
3966
Dave Chinner5c8ed202011-09-18 20:40:45 +00003967 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3968 error = xfs_iread_extents(NULL, ip, whichfork);
3969 if (error)
3970 return error;
3971 }
3972
Christoph Hellwig334f3422016-11-24 11:39:43 +11003973 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
3974 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003975 end = bno + len;
3976 obno = bno;
3977
3978 while (bno < end && n < *nmap) {
3979 /* Reading past eof, act as though there's a hole up to end. */
3980 if (eof)
3981 got.br_startoff = end;
3982 if (got.br_startoff > bno) {
3983 /* Reading in a hole. */
3984 mval->br_startoff = bno;
3985 mval->br_startblock = HOLESTARTBLOCK;
3986 mval->br_blockcount =
3987 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3988 mval->br_state = XFS_EXT_NORM;
3989 bno += mval->br_blockcount;
3990 len -= mval->br_blockcount;
3991 mval++;
3992 n++;
3993 continue;
3994 }
3995
3996 /* set up the extent map to return. */
3997 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3998 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3999
4000 /* If we're done, stop now. */
4001 if (bno >= end || n >= *nmap)
4002 break;
4003
4004 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11004005 if (!xfs_iext_get_extent(ifp, ++idx, &got))
4006 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00004007 }
4008 *nmap = n;
4009 return 0;
4010}
4011
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004012/*
4013 * Add a delayed allocation extent to an inode. Blocks are reserved from the
4014 * global pool and the extent inserted into the inode in-core extent tree.
4015 *
4016 * On entry, got refers to the first extent beyond the offset of the extent to
4017 * allocate or eof is specified if no such extent exists. On return, got refers
4018 * to the extent record that was inserted to the inode fork.
4019 *
4020 * Note that the allocated extent may have been merged with contiguous extents
4021 * during insertion into the inode fork. Thus, got does not reflect the current
4022 * state of the inode fork on return. If necessary, the caller can use lastx to
4023 * look up the updated record in the inode fork.
4024 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10004025int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004026xfs_bmapi_reserve_delalloc(
4027 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004028 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11004029 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004030 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11004031 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004032 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004033 xfs_extnum_t *lastx,
4034 int eof)
4035{
4036 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07004037 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004038 xfs_extlen_t alen;
4039 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004040 char rt = XFS_IS_REALTIME_INODE(ip);
4041 xfs_extlen_t extsz;
4042 int error;
Brian Foster974ae922016-11-28 14:57:42 +11004043 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004044
Brian Foster974ae922016-11-28 14:57:42 +11004045 /*
4046 * Cap the alloc length. Keep track of prealloc so we know whether to
4047 * tag the inode before we return.
4048 */
4049 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004050 if (!eof)
4051 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11004052 if (prealloc && alen >= len)
4053 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004054
4055 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07004056 if (whichfork == XFS_COW_FORK)
4057 extsz = xfs_get_cowextsz_hint(ip);
4058 else
4059 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004060 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11004061 struct xfs_bmbt_irec prev;
4062
4063 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
4064 prev.br_startoff = NULLFILEOFF;
4065
4066 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004067 1, 0, &aoff, &alen);
4068 ASSERT(!error);
4069 }
4070
4071 if (rt)
4072 extsz = alen / mp->m_sb.sb_rextsize;
4073
4074 /*
4075 * Make a transaction-less quota reservation for delayed allocation
4076 * blocks. This number gets adjusted later. We return if we haven't
4077 * allocated blocks already inside this loop.
4078 */
4079 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4080 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4081 if (error)
4082 return error;
4083
4084 /*
4085 * Split changing sb for alen and indlen since they could be coming
4086 * from different places.
4087 */
4088 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4089 ASSERT(indlen > 0);
4090
4091 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004092 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004093 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11004094 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004095 }
4096
4097 if (error)
4098 goto out_unreserve_quota;
4099
Dave Chinner0d485ad2015-02-23 21:22:03 +11004100 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004101 if (error)
4102 goto out_unreserve_blocks;
4103
4104
4105 ip->i_delayed_blks += alen;
4106
4107 got->br_startoff = aoff;
4108 got->br_startblock = nullstartblock(indlen);
4109 got->br_blockcount = alen;
4110 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004111
Brian Fosterf65e6fa2017-03-08 09:58:08 -08004112 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004113
Brian Foster974ae922016-11-28 14:57:42 +11004114 /*
4115 * Tag the inode if blocks were preallocated. Note that COW fork
4116 * preallocation can occur at the start or end of the extent, even when
4117 * prealloc == 0, so we must also check the aligned offset and length.
4118 */
4119 if (whichfork == XFS_DATA_FORK && prealloc)
4120 xfs_inode_set_eofblocks_tag(ip);
4121 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
4122 xfs_inode_set_cowblocks_tag(ip);
4123
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004124 return 0;
4125
4126out_unreserve_blocks:
4127 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11004128 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004129 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11004130 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004131out_unreserve_quota:
4132 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10004133 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00004134 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4135 return error;
4136}
4137
Dave Chinnercf11da92014-07-15 07:08:24 +10004138static int
4139xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10004140 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004141{
4142 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07004143 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004144 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004145 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004146 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004147
Dave Chinnera99ebf42011-12-01 11:24:20 +00004148 ASSERT(bma->length > 0);
4149
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004150 /*
4151 * For the wasdelay case, we could also just allocate the stuff asked
4152 * for in this bmap call but that wouldn't be as good.
4153 */
4154 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00004155 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4156 bma->offset = bma->got.br_startoff;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004157 if (bma->idx)
4158 xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004159 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004160 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004161 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004162 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004163 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004164 }
4165
4166 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004167 * Set the data type being allocated. For the data fork, the first data
4168 * in the file is treated differently to all other allocations. For the
4169 * attribute fork, we only need to ensure the allocated range is not on
4170 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004171 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004172 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004173 bma->datatype = XFS_ALLOC_NOBUSY;
4174 if (whichfork == XFS_DATA_FORK) {
4175 if (bma->offset == 0)
4176 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4177 else
4178 bma->datatype |= XFS_ALLOC_USERDATA;
4179 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004180 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004181 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004182 }
4183
Dave Chinnere04426b2012-10-05 11:06:59 +10004184 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004185
4186 /*
4187 * Only want to do the alignment at the eof if it is userdata and
4188 * allocation length is larger than a stripe unit.
4189 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004190 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004191 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004192 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004193 if (error)
4194 return error;
4195 }
4196
4197 error = xfs_bmap_alloc(bma);
4198 if (error)
4199 return error;
4200
Dave Chinner29c8d172011-09-18 20:41:00 +00004201 if (bma->cur)
4202 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004203 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004204 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004205 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4206 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4207 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004208 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004209 }
4210 /*
4211 * Bump the number of extents we've allocated
4212 * in this call.
4213 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004214 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004215
Dave Chinner29c8d172011-09-18 20:41:00 +00004216 if (bma->cur)
4217 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004218 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4219
Dave Chinner963c30c2011-09-18 20:40:59 +00004220 bma->got.br_startoff = bma->offset;
4221 bma->got.br_startblock = bma->blkno;
4222 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004223 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004224
4225 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004226 * In the data fork, a wasdelay extent has been initialized, so
4227 * shouldn't be flagged as unwritten.
4228 *
4229 * For the cow fork, however, we convert delalloc reservations
4230 * (extents allocated for speculative preallocation) to
4231 * allocated unwritten extents, and only convert the unwritten
4232 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004233 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004234 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4235 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004236 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004237 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004238
Christoph Hellwigc6534242011-09-18 20:41:05 +00004239 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004240 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004241 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004242 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4243 whichfork, &bma->idx, &bma->cur, &bma->got,
4244 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004245
Christoph Hellwigc315c902011-09-18 20:41:02 +00004246 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004247 if (error)
4248 return error;
4249
4250 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004251 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4252 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4253 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004254 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004255 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004256
Dave Chinner963c30c2011-09-18 20:40:59 +00004257 ASSERT(bma->got.br_startoff <= bma->offset);
4258 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4259 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004260 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4261 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004262 return 0;
4263}
4264
Dave Chinnerb447fe52011-09-18 20:40:51 +00004265STATIC int
4266xfs_bmapi_convert_unwritten(
4267 struct xfs_bmalloca *bma,
4268 struct xfs_bmbt_irec *mval,
4269 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004270 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004271{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004272 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004273 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004274 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004275 int error;
4276
Dave Chinnerb447fe52011-09-18 20:40:51 +00004277 /* check if we need to do unwritten->real conversion */
4278 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4279 (flags & XFS_BMAPI_PREALLOC))
4280 return 0;
4281
4282 /* check if we need to do real->unwritten conversion */
4283 if (mval->br_state == XFS_EXT_NORM &&
4284 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4285 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4286 return 0;
4287
4288 /*
4289 * Modify (by adding) the state flag, if writing.
4290 */
4291 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004292 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4293 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004294 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004295 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004296 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004297 }
4298 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4299 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4300
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004301 /*
4302 * Before insertion into the bmbt, zero the range being converted
4303 * if required.
4304 */
4305 if (flags & XFS_BMAPI_ZERO) {
4306 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4307 mval->br_blockcount);
4308 if (error)
4309 return error;
4310 }
4311
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004312 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4313 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004314 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004315 /*
4316 * Log the inode core unconditionally in the unwritten extent conversion
4317 * path because the conversion might not have done so (e.g., if the
4318 * extent count hasn't changed). We need to make sure the inode is dirty
4319 * in the transaction for the sake of fsync(), even if nothing has
4320 * changed, because fsync() will not force the log for this transaction
4321 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004322 *
4323 * Note: If we're only converting cow fork extents, there aren't
4324 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004325 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004326 if (whichfork != XFS_COW_FORK)
4327 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004328 if (error)
4329 return error;
4330
4331 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004332 * Update our extent pointer, given that
4333 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4334 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004335 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004336 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004337
4338 /*
4339 * We may have combined previously unwritten space with written space,
4340 * so generate another request.
4341 */
4342 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004343 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004344 return 0;
4345}
4346
Christoph Hellwig44032802011-09-18 20:40:48 +00004347/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004348 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4349 * extent state if necessary. Details behaviour is controlled by the flags
4350 * parameter. Only allocates blocks from a single allocation group, to avoid
4351 * locking problems.
4352 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 * The returned value in "firstblock" from the first call in a transaction
4354 * must be remembered and presented to subsequent calls in "firstblock".
4355 * An upper bound for the number of blocks to be allocated is supplied to
4356 * the first call in "total"; if no allocation group has that many free
4357 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4358 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004359int
4360xfs_bmapi_write(
4361 struct xfs_trans *tp, /* transaction pointer */
4362 struct xfs_inode *ip, /* incore inode */
4363 xfs_fileoff_t bno, /* starting file offs. mapped */
4364 xfs_filblks_t len, /* length to map in file */
4365 int flags, /* XFS_BMAPI_... */
4366 xfs_fsblock_t *firstblock, /* first allocated block
4367 controls a.g. for allocs */
4368 xfs_extlen_t total, /* total blocks needed */
4369 struct xfs_bmbt_irec *mval, /* output: map values */
4370 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004371 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004373 struct xfs_mount *mp = ip->i_mount;
4374 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004375 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004376 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004377 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004378 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004379 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004380 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004381 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004384 xfs_fileoff_t orig_bno; /* original block number value */
4385 int orig_flags; /* original flags arg value */
4386 xfs_filblks_t orig_len; /* original value of len arg */
4387 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4388 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389
4390 orig_bno = bno;
4391 orig_len = len;
4392 orig_flags = flags;
4393 orig_mval = mval;
4394 orig_nmap = *nmap;
4395#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004396 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004397
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004399 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4400 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004401 ASSERT(tp != NULL ||
4402 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4403 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004404 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004405 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004406 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004407 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004408
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004409 /* zeroing is for currently only for data extents, not metadata */
4410 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4411 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4412 /*
4413 * we can allocate unwritten extents or pre-zero allocated blocks,
4414 * but it makes no sense to do both at once. This would result in
4415 * zeroing the unwritten extent twice, but it still being an
4416 * unwritten extent....
4417 */
4418 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4419 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4420
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 if (unlikely(XFS_TEST_ERROR(
4422 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004423 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004424 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004425 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004426 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004428
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004430 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004431
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004433
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004434 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004435
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004436 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004438 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004440 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004441 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004442 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004443 }
4444
4445 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4446 error = xfs_iread_extents(tp, ip, whichfork);
4447 if (error)
4448 goto error0;
4449 }
4450
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 n = 0;
4452 end = bno + len;
4453 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004454
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004455 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4456 eof = true;
4457 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4458 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004459 bma.tp = tp;
4460 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004461 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004462 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004463 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004464 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004465
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004467 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004468
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004469 /* in hole or beyoned EOF? */
4470 if (eof || bma.got.br_startoff > bno) {
4471 if (flags & XFS_BMAPI_DELALLOC) {
4472 /*
4473 * For the COW fork we can reasonably get a
4474 * request for converting an extent that races
4475 * with other threads already having converted
4476 * part of it, as there converting COW to
4477 * regular blocks is not protected using the
4478 * IOLOCK.
4479 */
4480 ASSERT(flags & XFS_BMAPI_COWFORK);
4481 if (!(flags & XFS_BMAPI_COWFORK)) {
4482 error = -EIO;
4483 goto error0;
4484 }
4485
4486 if (eof || bno >= end)
4487 break;
4488 } else {
4489 need_alloc = true;
4490 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004491 } else if (isnullstartblock(bma.got.br_startblock)) {
4492 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004493 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004494
4495 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 * First, deal with the hole before the allocated space
4497 * that we found, if any.
4498 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004499 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004500 bma.eof = eof;
4501 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4502 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004503 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004504 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004505
Dave Chinnera99ebf42011-12-01 11:24:20 +00004506 /*
4507 * There's a 32/64 bit type mismatch between the
4508 * allocation length request (which can be 64 bits in
4509 * length) and the bma length request, which is
4510 * xfs_extlen_t and therefore 32 bits. Hence we have to
4511 * check for 32-bit overflows and handle them here.
4512 */
4513 if (len > (xfs_filblks_t)MAXEXTLEN)
4514 bma.length = MAXEXTLEN;
4515 else
4516 bma.length = len;
4517
4518 ASSERT(len > 0);
4519 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004520 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 if (error)
4522 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004523 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004524 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004525
4526 /*
4527 * If this is a CoW allocation, record the data in
4528 * the refcount btree for orphan recovery.
4529 */
4530 if (whichfork == XFS_COW_FORK) {
4531 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4532 bma.blkno, bma.length);
4533 if (error)
4534 goto error0;
4535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004537
Dave Chinneraef9a892011-09-18 20:40:44 +00004538 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004539 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4540 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541
Dave Chinnerb447fe52011-09-18 20:40:51 +00004542 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004543 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004544 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004545 continue;
4546 if (error)
4547 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548
Dave Chinneraef9a892011-09-18 20:40:44 +00004549 /* update the extent map to return */
4550 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 /*
4553 * If we're done, stop now. Stop when we've allocated
4554 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4555 * the transaction may get too big.
4556 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004557 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004559
4560 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004561 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004562 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4563 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004566
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 /*
4568 * Transform from btree to extents, give it cur.
4569 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004570 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004571 int tmp_logflags = 0;
4572
Dave Chinner29c8d172011-09-18 20:41:00 +00004573 ASSERT(bma.cur);
4574 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004576 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 if (error)
4578 goto error0;
4579 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004580
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004582 XFS_IFORK_NEXTENTS(ip, whichfork) >
4583 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585error0:
4586 /*
4587 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004588 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004590 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004592 bma.logflags &= ~xfs_ilog_fext(whichfork);
4593 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004595 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 /*
4597 * Log whatever the flags say, even if error. Otherwise we might miss
4598 * detecting a case where the data is changed, there's an error,
4599 * and it's not logged so we don't shutdown when we should.
4600 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004601 if (bma.logflags)
4602 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004603
Dave Chinner29c8d172011-09-18 20:41:00 +00004604 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 if (!error) {
4606 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004607 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004609 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004610 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004612 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4614 }
4615 if (!error)
4616 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4617 orig_nmap, *nmap);
4618 return error;
4619}
4620
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004621static int
4622xfs_bmapi_remap(
4623 struct xfs_trans *tp,
4624 struct xfs_inode *ip,
4625 xfs_fileoff_t bno,
4626 xfs_filblks_t len,
4627 xfs_fsblock_t startblock,
4628 struct xfs_defer_ops *dfops)
4629{
4630 struct xfs_mount *mp = ip->i_mount;
4631 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4632 struct xfs_btree_cur *cur = NULL;
4633 xfs_fsblock_t firstblock = NULLFSBLOCK;
4634 struct xfs_bmbt_irec got;
4635 xfs_extnum_t idx;
4636 int logflags = 0, error;
4637
4638 ASSERT(len > 0);
4639 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4640 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4641
4642 if (unlikely(XFS_TEST_ERROR(
4643 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4644 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004645 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004646 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4647 return -EFSCORRUPTED;
4648 }
4649
4650 if (XFS_FORCED_SHUTDOWN(mp))
4651 return -EIO;
4652
4653 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4654 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4655 if (error)
4656 return error;
4657 }
4658
4659 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4660 /* make sure we only reflink into a hole. */
4661 ASSERT(got.br_startoff > bno);
4662 ASSERT(got.br_startoff - bno >= len);
4663 }
4664
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004665 ip->i_d.di_nblocks += len;
4666 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004667
4668 if (ifp->if_flags & XFS_IFBROOT) {
4669 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4670 cur->bc_private.b.firstblock = firstblock;
4671 cur->bc_private.b.dfops = dfops;
4672 cur->bc_private.b.flags = 0;
4673 }
4674
4675 got.br_startoff = bno;
4676 got.br_startblock = startblock;
4677 got.br_blockcount = len;
4678 got.br_state = XFS_EXT_NORM;
4679
4680 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4681 &got, &firstblock, dfops, &logflags);
4682 if (error)
4683 goto error0;
4684
4685 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4686 int tmp_logflags = 0;
4687
4688 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4689 &tmp_logflags, XFS_DATA_FORK);
4690 logflags |= tmp_logflags;
4691 }
4692
4693error0:
4694 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4695 logflags &= ~XFS_ILOG_DEXT;
4696 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4697 logflags &= ~XFS_ILOG_DBROOT;
4698
4699 if (logflags)
4700 xfs_trans_log_inode(tp, ip, logflags);
4701 if (cur) {
4702 xfs_btree_del_cursor(cur,
4703 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4704 }
4705 return error;
4706}
4707
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004709 * When a delalloc extent is split (e.g., due to a hole punch), the original
4710 * indlen reservation must be shared across the two new extents that are left
4711 * behind.
4712 *
4713 * Given the original reservation and the worst case indlen for the two new
4714 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004715 * reservation fairly across the two new extents. If necessary, steal available
4716 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4717 * ores == 1). The number of stolen blocks is returned. The availability and
4718 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004719 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004720static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004721xfs_bmap_split_indlen(
4722 xfs_filblks_t ores, /* original res. */
4723 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004724 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4725 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004726{
4727 xfs_filblks_t len1 = *indlen1;
4728 xfs_filblks_t len2 = *indlen2;
4729 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004730 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004731 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004732
4733 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004734 * Steal as many blocks as we can to try and satisfy the worst case
4735 * indlen for both new extents.
4736 */
Brian Foster75d65362017-02-13 22:48:30 -08004737 if (ores < nres && avail)
4738 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4739 ores += stolen;
4740
4741 /* nothing else to do if we've satisfied the new reservation */
4742 if (ores >= nres)
4743 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004744
4745 /*
Brian Foster75d65362017-02-13 22:48:30 -08004746 * We can't meet the total required reservation for the two extents.
4747 * Calculate the percent of the overall shortage between both extents
4748 * and apply this percentage to each of the requested indlen values.
4749 * This distributes the shortage fairly and reduces the chances that one
4750 * of the two extents is left with nothing when extents are repeatedly
4751 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004752 */
Brian Foster75d65362017-02-13 22:48:30 -08004753 resfactor = (ores * 100);
4754 do_div(resfactor, nres);
4755 len1 *= resfactor;
4756 do_div(len1, 100);
4757 len2 *= resfactor;
4758 do_div(len2, 100);
4759 ASSERT(len1 + len2 <= ores);
4760 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4761
4762 /*
4763 * Hand out the remainder to each extent. If one of the two reservations
4764 * is zero, we want to make sure that one gets a block first. The loop
4765 * below starts with len1, so hand len2 a block right off the bat if it
4766 * is zero.
4767 */
4768 ores -= (len1 + len2);
4769 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4770 if (ores && !len2 && *indlen2) {
4771 len2++;
4772 ores--;
4773 }
4774 while (ores) {
4775 if (len1 < *indlen1) {
4776 len1++;
4777 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004778 }
Brian Foster75d65362017-02-13 22:48:30 -08004779 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004780 break;
Brian Foster75d65362017-02-13 22:48:30 -08004781 if (len2 < *indlen2) {
4782 len2++;
4783 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004784 }
4785 }
4786
4787 *indlen1 = len1;
4788 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004789
4790 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004791}
4792
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004793int
4794xfs_bmap_del_extent_delay(
4795 struct xfs_inode *ip,
4796 int whichfork,
4797 xfs_extnum_t *idx,
4798 struct xfs_bmbt_irec *got,
4799 struct xfs_bmbt_irec *del)
4800{
4801 struct xfs_mount *mp = ip->i_mount;
4802 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4803 struct xfs_bmbt_irec new;
4804 int64_t da_old, da_new, da_diff = 0;
4805 xfs_fileoff_t del_endoff, got_endoff;
4806 xfs_filblks_t got_indlen, new_indlen, stolen;
4807 int error = 0, state = 0;
4808 bool isrt;
4809
4810 XFS_STATS_INC(mp, xs_del_exlist);
4811
4812 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4813 del_endoff = del->br_startoff + del->br_blockcount;
4814 got_endoff = got->br_startoff + got->br_blockcount;
4815 da_old = startblockval(got->br_startblock);
4816 da_new = 0;
4817
4818 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004819 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004820 ASSERT(del->br_blockcount > 0);
4821 ASSERT(got->br_startoff <= del->br_startoff);
4822 ASSERT(got_endoff >= del_endoff);
4823
4824 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004825 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004826
4827 do_div(rtexts, mp->m_sb.sb_rextsize);
4828 xfs_mod_frextents(mp, rtexts);
4829 }
4830
4831 /*
4832 * Update the inode delalloc counter now and wait to update the
4833 * sb counters as we might have to borrow some blocks for the
4834 * indirect block accounting.
4835 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004836 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4837 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004838 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004839 if (error)
4840 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004841 ip->i_delayed_blks -= del->br_blockcount;
4842
4843 if (whichfork == XFS_COW_FORK)
4844 state |= BMAP_COWFORK;
4845
4846 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004847 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004848 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004849 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004850
Christoph Hellwig0173c682017-10-17 14:16:22 -07004851 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4852 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004853 /*
4854 * Matches the whole extent. Delete the entry.
4855 */
4856 xfs_iext_remove(ip, *idx, 1, state);
4857 --*idx;
4858 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004859 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004860 /*
4861 * Deleting the first part of the extent.
4862 */
4863 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4864 got->br_startoff = del_endoff;
4865 got->br_blockcount -= del->br_blockcount;
4866 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4867 got->br_blockcount), da_old);
4868 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004869 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004870 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4871 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004872 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004873 /*
4874 * Deleting the last part of the extent.
4875 */
4876 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4877 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4878 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4879 got->br_blockcount), da_old);
4880 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004881 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004882 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4883 break;
4884 case 0:
4885 /*
4886 * Deleting the middle of the extent.
4887 *
4888 * Distribute the original indlen reservation across the two new
4889 * extents. Steal blocks from the deleted extent if necessary.
4890 * Stealing blocks simply fudges the fdblocks accounting below.
4891 * Warn if either of the new indlen reservations is zero as this
4892 * can lead to delalloc problems.
4893 */
4894 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4895
4896 got->br_blockcount = del->br_startoff - got->br_startoff;
4897 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4898
4899 new.br_blockcount = got_endoff - del_endoff;
4900 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4901
4902 WARN_ON_ONCE(!got_indlen || !new_indlen);
4903 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4904 del->br_blockcount);
4905
4906 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004907 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004908 trace_xfs_bmap_post_update(ip, *idx, 0, _THIS_IP_);
4909
4910 new.br_startoff = del_endoff;
4911 new.br_state = got->br_state;
4912 new.br_startblock = nullstartblock((int)new_indlen);
4913
4914 ++*idx;
4915 xfs_iext_insert(ip, *idx, 1, &new, state);
4916
4917 da_new = got_indlen + new_indlen - stolen;
4918 del->br_blockcount -= stolen;
4919 break;
4920 }
4921
4922 ASSERT(da_old >= da_new);
4923 da_diff = da_old - da_new;
4924 if (!isrt)
4925 da_diff += del->br_blockcount;
4926 if (da_diff)
4927 xfs_mod_fdblocks(mp, da_diff, false);
4928 return error;
4929}
4930
4931void
4932xfs_bmap_del_extent_cow(
4933 struct xfs_inode *ip,
4934 xfs_extnum_t *idx,
4935 struct xfs_bmbt_irec *got,
4936 struct xfs_bmbt_irec *del)
4937{
4938 struct xfs_mount *mp = ip->i_mount;
4939 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4940 struct xfs_bmbt_irec new;
4941 xfs_fileoff_t del_endoff, got_endoff;
4942 int state = BMAP_COWFORK;
4943
4944 XFS_STATS_INC(mp, xs_del_exlist);
4945
4946 del_endoff = del->br_startoff + del->br_blockcount;
4947 got_endoff = got->br_startoff + got->br_blockcount;
4948
4949 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004950 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004951 ASSERT(del->br_blockcount > 0);
4952 ASSERT(got->br_startoff <= del->br_startoff);
4953 ASSERT(got_endoff >= del_endoff);
4954 ASSERT(!isnullstartblock(got->br_startblock));
4955
4956 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004957 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004958 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004959 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004960
Christoph Hellwig0173c682017-10-17 14:16:22 -07004961 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4962 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004963 /*
4964 * Matches the whole extent. Delete the entry.
4965 */
4966 xfs_iext_remove(ip, *idx, 1, state);
4967 --*idx;
4968 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004969 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004970 /*
4971 * Deleting the first part of the extent.
4972 */
4973 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4974 got->br_startoff = del_endoff;
4975 got->br_blockcount -= del->br_blockcount;
4976 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004977 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004978 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4979 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004980 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004981 /*
4982 * Deleting the last part of the extent.
4983 */
4984 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4985 got->br_blockcount -= del->br_blockcount;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004986 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004987 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4988 break;
4989 case 0:
4990 /*
4991 * Deleting the middle of the extent.
4992 */
4993 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
4994 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwig67e4e692017-08-29 15:44:11 -07004995 xfs_iext_update_extent(ifp, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004996 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
4997
4998 new.br_startoff = del_endoff;
4999 new.br_blockcount = got_endoff - del_endoff;
5000 new.br_state = got->br_state;
5001 new.br_startblock = del->br_startblock + del->br_blockcount;
5002
5003 ++*idx;
5004 xfs_iext_insert(ip, *idx, 1, &new, state);
5005 break;
5006 }
5007}
5008
Brian Fostera9bd24a2016-03-15 11:42:46 +11005009/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11005010 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07005011 * after removing space.
Dave Chinner9e5987a2013-02-25 12:31:26 +11005012 */
5013STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005014xfs_bmap_del_extent_real(
Dave Chinner9e5987a2013-02-25 12:31:26 +11005015 xfs_inode_t *ip, /* incore inode pointer */
5016 xfs_trans_t *tp, /* current transaction pointer */
5017 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005018 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005019 xfs_btree_cur_t *cur, /* if null, not a btree */
5020 xfs_bmbt_irec_t *del, /* data to remove from extents */
5021 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005022 int whichfork, /* data or attr fork */
5023 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005024{
Dave Chinner9e5987a2013-02-25 12:31:26 +11005025 xfs_fsblock_t del_endblock=0; /* first block past del */
5026 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005027 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005028 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005029 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005030 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005031 xfs_fileoff_t got_endoff; /* first offset past got */
5032 int i; /* temp state */
5033 xfs_ifork_t *ifp; /* inode fork pointer */
5034 xfs_mount_t *mp; /* mount structure */
5035 xfs_filblks_t nblks; /* quota/sb block count */
5036 xfs_bmbt_irec_t new; /* new record to be inserted */
5037 /* REFERENCED */
5038 uint qfield; /* quota field to update */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005039 int state = 0;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005040 struct xfs_bmbt_irec old;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005041
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005042 mp = ip->i_mount;
5043 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005044
5045 if (whichfork == XFS_ATTR_FORK)
5046 state |= BMAP_ATTRFORK;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005047 else if (whichfork == XFS_COW_FORK)
5048 state |= BMAP_COWFORK;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005049
Dave Chinner9e5987a2013-02-25 12:31:26 +11005050 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005051 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005052 ASSERT(del->br_blockcount > 0);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005053 xfs_iext_get_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005054 ASSERT(got.br_startoff <= del->br_startoff);
5055 del_endoff = del->br_startoff + del->br_blockcount;
5056 got_endoff = got.br_startoff + got.br_blockcount;
5057 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005058 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +11005059 qfield = 0;
5060 error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005061
Christoph Hellwig1b24b632017-10-17 14:16:22 -07005062 /*
5063 * If it's the case where the directory code is running with no block
5064 * reservation, and the deleted block is in the middle of its extent,
5065 * and the resulting insert of an extent would cause transformation to
5066 * btree format, then reject it. The calling code will then swap blocks
5067 * around instead. We have to do this now, rather than waiting for the
5068 * conversion to btree format, since the transaction will be dirty then.
5069 */
5070 if (tp->t_blk_res == 0 &&
5071 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5072 XFS_IFORK_NEXTENTS(ip, whichfork) >=
5073 XFS_IFORK_MAXEXT(ip, whichfork) &&
5074 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
5075 return -ENOSPC;
5076
5077 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005078 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
5079 xfs_fsblock_t bno;
5080 xfs_filblks_t len;
5081
5082 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
5083 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
5084 bno = del->br_startblock;
5085 len = del->br_blockcount;
5086 do_div(bno, mp->m_sb.sb_rextsize);
5087 do_div(len, mp->m_sb.sb_rextsize);
5088 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
5089 if (error)
5090 goto done;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005091 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005092 nblks = len * mp->m_sb.sb_rextsize;
5093 qfield = XFS_TRANS_DQ_RTBCOUNT;
5094 } else {
5095 do_fx = 1;
5096 nblks = del->br_blockcount;
5097 qfield = XFS_TRANS_DQ_BCOUNT;
5098 }
5099
5100 del_endblock = del->br_startblock + del->br_blockcount;
5101 if (cur) {
5102 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
5103 got.br_startblock, got.br_blockcount, &i);
5104 if (error)
5105 goto done;
5106 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005107 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10005108
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005109 if (got.br_startoff == del->br_startoff)
5110 state |= BMAP_LEFT_FILLING;
5111 if (got_endoff == del_endoff)
5112 state |= BMAP_RIGHT_FILLING;
5113
5114 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
5115 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11005116 /*
5117 * Matches the whole extent. Delete the entry.
5118 */
Darrick J. Wong4862cfe2016-10-03 09:11:35 -07005119 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005120 xfs_iext_remove(ip, *idx, 1,
5121 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
5122 --*idx;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005123
5124 XFS_IFORK_NEXT_SET(ip, whichfork,
5125 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5126 flags |= XFS_ILOG_CORE;
5127 if (!cur) {
5128 flags |= xfs_ilog_fext(whichfork);
5129 break;
5130 }
5131 if ((error = xfs_btree_delete(cur, &i)))
5132 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11005133 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005134 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005135 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11005136 /*
5137 * Deleting the first part of the extent.
5138 */
5139 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005140 got.br_startoff = del_endoff;
5141 got.br_startblock = del_endblock;
5142 got.br_blockcount -= del->br_blockcount;
5143 xfs_iext_update_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005144 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5145 if (!cur) {
5146 flags |= xfs_ilog_fext(whichfork);
5147 break;
5148 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005149 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005150 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005151 goto done;
5152 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07005153 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11005154 /*
5155 * Deleting the last part of the extent.
5156 */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005157 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005158 got.br_blockcount -= del->br_blockcount;
5159 xfs_iext_update_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005160 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5161 if (!cur) {
5162 flags |= xfs_ilog_fext(whichfork);
5163 break;
5164 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005165 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005166 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005167 goto done;
5168 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005169 case 0:
5170 /*
5171 * Deleting the middle of the extent.
5172 */
Dave Chinner9e5987a2013-02-25 12:31:26 +11005173 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005174
5175 old = got;
5176 got.br_blockcount = del->br_startoff - got.br_startoff;
5177 xfs_iext_update_extent(ifp, *idx, &got);
5178
Dave Chinner9e5987a2013-02-25 12:31:26 +11005179 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005180 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a2013-02-25 12:31:26 +11005181 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005182 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005183
Christoph Hellwige1d75532017-10-17 14:16:21 -07005184 flags |= XFS_ILOG_CORE;
5185 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005186 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005187 if (error)
5188 goto done;
5189 error = xfs_btree_increment(cur, 0, &i);
5190 if (error)
5191 goto done;
5192 cur->bc_rec.b = new;
5193 error = xfs_btree_insert(cur, &i);
5194 if (error && error != -ENOSPC)
5195 goto done;
5196 /*
5197 * If get no-space back from btree insert, it tried a
5198 * split, and we have a zero block reservation. Fix up
5199 * our state and return the error.
5200 */
5201 if (error == -ENOSPC) {
5202 /*
5203 * Reset the cursor, don't trust it after any
5204 * insert operation.
5205 */
5206 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005207 got.br_startblock,
5208 got.br_blockcount, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005209 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005210 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005211 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5212 /*
5213 * Update the btree record back
5214 * to the original value.
5215 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005216 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005217 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005218 goto done;
5219 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005220 * Reset the extent record back
5221 * to the original value.
Dave Chinner9e5987a2013-02-25 12:31:26 +11005222 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07005223 xfs_iext_update_extent(ifp, *idx, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005224 flags = 0;
5225 error = -ENOSPC;
5226 goto done;
5227 }
5228 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5229 } else
5230 flags |= xfs_ilog_fext(whichfork);
5231 XFS_IFORK_NEXT_SET(ip, whichfork,
5232 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005233 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
5234 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5235 ++*idx;
5236 break;
5237 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005238
5239 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005240 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5241 if (error)
5242 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005243
Dave Chinner9e5987a2013-02-25 12:31:26 +11005244 /*
5245 * If we need to, add to list of extents to delete.
5246 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005247 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005248 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5249 error = xfs_refcount_decrease_extent(mp, dfops, del);
5250 if (error)
5251 goto done;
5252 } else
5253 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5254 del->br_blockcount, NULL);
5255 }
5256
Dave Chinner9e5987a2013-02-25 12:31:26 +11005257 /*
5258 * Adjust inode # blocks in the file.
5259 */
5260 if (nblks)
5261 ip->i_d.di_nblocks -= nblks;
5262 /*
5263 * Adjust quota data.
5264 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005265 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005266 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5267
Dave Chinner9e5987a2013-02-25 12:31:26 +11005268done:
5269 *logflagsp = flags;
5270 return error;
5271}
5272
5273/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274 * Unmap (remove) blocks from a file.
5275 * If nexts is nonzero then the number of extents to remove is limited to
5276 * that value. If not all extents in the block range can be removed then
5277 * *done is set.
5278 */
5279int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005280__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 xfs_trans_t *tp, /* transaction pointer */
5282 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005283 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005284 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005285 int flags, /* misc flags */
5286 xfs_extnum_t nexts, /* number of extents max */
5287 xfs_fsblock_t *firstblock, /* first allocated block
5288 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005289 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290{
5291 xfs_btree_cur_t *cur; /* bmap btree cursor */
5292 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005293 int error; /* error return value */
5294 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005295 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 xfs_ifork_t *ifp; /* inode fork pointer */
5297 int isrt; /* freeing in rt area */
5298 xfs_extnum_t lastx; /* last extent index used */
5299 int logflags; /* transaction logging flags */
5300 xfs_extlen_t mod; /* rt extent offset */
5301 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005302 int tmp_logflags; /* partial logging flags */
5303 int wasdel; /* was a delayed alloc extent */
5304 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005306 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005307 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005308 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005309 xfs_fileoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005310
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005311 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005312
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005313 whichfork = xfs_bmapi_whichfork(flags);
5314 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005315 ifp = XFS_IFORK_PTR(ip, whichfork);
5316 if (unlikely(
5317 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5318 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5319 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5320 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005321 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 }
5323 mp = ip->i_mount;
5324 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005325 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005326
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005327 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005328 ASSERT(len > 0);
5329 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005330
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005331 /*
5332 * Guesstimate how many blocks we can unmap without running the risk of
5333 * blowing out the transaction with a mix of EFIs and reflink
5334 * adjustments.
5335 */
5336 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5337 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5338 else
5339 max_len = len;
5340
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5342 (error = xfs_iread_extents(tp, ip, whichfork)))
5343 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005344 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005345 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 return 0;
5347 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005348 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005349 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005350 end = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005351
Linus Torvalds1da177e2005-04-16 15:20:36 -07005352 /*
5353 * Check to see if the given block number is past the end of the
5354 * file, back up to the last block if so...
5355 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005356 if (!xfs_iext_lookup_extent(ip, ifp, end, &lastx, &got)) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005357 ASSERT(lastx > 0);
5358 xfs_iext_get_extent(ifp, --lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005359 end = got.br_startoff + got.br_blockcount - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005361
Linus Torvalds1da177e2005-04-16 15:20:36 -07005362 logflags = 0;
5363 if (ifp->if_flags & XFS_IFBROOT) {
5364 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005365 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005367 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 cur->bc_private.b.flags = 0;
5369 } else
5370 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005371
5372 if (isrt) {
5373 /*
5374 * Synchronize by locking the bitmap inode.
5375 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005376 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005377 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005378 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5379 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005380 }
5381
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382 extno = 0;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005383 while (end != (xfs_fileoff_t)-1 && end >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005384 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005385 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005386 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387 * Just back up to the previous extent, if so.
5388 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005389 if (got.br_startoff > end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 if (--lastx < 0)
5391 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005392 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 }
5394 /*
5395 * Is the last block of this extent before the range
5396 * we're supposed to delete? If so, we're done.
5397 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005398 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005399 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005400 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005401 break;
5402 /*
5403 * Then deal with the (possibly delayed) allocated space
5404 * we found.
5405 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005407 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005408
5409 /*
5410 * Make sure we don't touch multiple AGF headers out of order
5411 * in a single transaction, as that could cause AB-BA deadlocks.
5412 */
5413 if (!wasdel) {
5414 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5415 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5416 break;
5417 prev_agno = agno;
5418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 if (got.br_startoff < start) {
5420 del.br_startoff = start;
5421 del.br_blockcount -= start - got.br_startoff;
5422 if (!wasdel)
5423 del.br_startblock += start - got.br_startoff;
5424 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005425 if (del.br_startoff + del.br_blockcount > end + 1)
5426 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005427
5428 /* How much can we safely unmap? */
5429 if (max_len < del.br_blockcount) {
5430 del.br_startoff += del.br_blockcount - max_len;
5431 if (!wasdel)
5432 del.br_startblock += del.br_blockcount - max_len;
5433 del.br_blockcount = max_len;
5434 }
5435
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 sum = del.br_startblock + del.br_blockcount;
5437 if (isrt &&
5438 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5439 /*
5440 * Realtime extent not lined up at the end.
5441 * The extent could have been split into written
5442 * and unwritten pieces, or we could just be
5443 * unmapping part of it. But we can't really
5444 * get rid of part of a realtime extent.
5445 */
5446 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005447 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005448 /*
5449 * This piece is unwritten, or we're not
5450 * using unwritten extents. Skip over it.
5451 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005452 ASSERT(end >= mod);
5453 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005454 del.br_blockcount : mod;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005455 if (end < got.br_startoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 if (--lastx >= 0)
Christoph Hellwige3f0f752017-10-17 14:16:20 -07005457 xfs_iext_get_extent(ifp, lastx,
5458 &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005459 }
5460 continue;
5461 }
5462 /*
5463 * It's written, turn it unwritten.
5464 * This is better than zeroing it.
5465 */
5466 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005467 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 /*
5469 * If this spans a realtime extent boundary,
5470 * chop it back to the start of the one we end at.
5471 */
5472 if (del.br_blockcount > mod) {
5473 del.br_startoff += del.br_blockcount - mod;
5474 del.br_startblock += del.br_blockcount - mod;
5475 del.br_blockcount = mod;
5476 }
5477 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005478 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005479 whichfork, &lastx, &cur, &del,
5480 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005481 if (error)
5482 goto error0;
5483 goto nodelete;
5484 }
5485 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5486 /*
5487 * Realtime extent is lined up at the end but not
5488 * at the front. We'll get rid of full extents if
5489 * we can.
5490 */
5491 mod = mp->m_sb.sb_rextsize - mod;
5492 if (del.br_blockcount > mod) {
5493 del.br_blockcount -= mod;
5494 del.br_startoff += mod;
5495 del.br_startblock += mod;
5496 } else if ((del.br_startoff == start &&
5497 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005498 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005499 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 /*
5501 * Can't make it unwritten. There isn't
5502 * a full extent here so just skip it.
5503 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005504 ASSERT(end >= del.br_blockcount);
5505 end -= del.br_blockcount;
5506 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005507 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 continue;
5509 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005510 struct xfs_bmbt_irec prev;
5511
Linus Torvalds1da177e2005-04-16 15:20:36 -07005512 /*
5513 * This one is already unwritten.
5514 * It must have a written left neighbor.
5515 * Unwrite the killed part of that one and
5516 * try again.
5517 */
5518 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005519 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005520 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005521 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 ASSERT(del.br_startblock ==
5523 prev.br_startblock + prev.br_blockcount);
5524 if (prev.br_startoff < start) {
5525 mod = start - prev.br_startoff;
5526 prev.br_blockcount -= mod;
5527 prev.br_startblock += mod;
5528 prev.br_startoff = start;
5529 }
5530 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005531 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005532 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005533 ip, whichfork, &lastx, &cur,
5534 &prev, firstblock, dfops,
5535 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 if (error)
5537 goto error0;
5538 goto nodelete;
5539 } else {
5540 ASSERT(del.br_state == XFS_EXT_NORM);
5541 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005542 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005543 ip, whichfork, &lastx, &cur,
5544 &del, firstblock, dfops,
5545 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546 if (error)
5547 goto error0;
5548 goto nodelete;
5549 }
5550 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005551
Brian Fosterb2706a02016-03-15 11:42:46 +11005552 if (wasdel) {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005553 error = xfs_bmap_del_extent_delay(ip, whichfork, &lastx,
5554 &got, &del);
5555 } else {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005556 error = xfs_bmap_del_extent_real(ip, tp, &lastx, dfops,
5557 cur, &del, &tmp_logflags, whichfork,
5558 flags);
5559 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005560 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005561
Linus Torvalds1da177e2005-04-16 15:20:36 -07005562 if (error)
5563 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005564
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005565 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005566 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 /*
5569 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005570 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005571 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005572 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005573 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005574 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005575 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005577 extno++;
5578 }
5579 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005580 if (end == (xfs_fileoff_t)-1 || end < start || lastx < 0)
Darrick J. Wong44535932016-10-03 09:11:29 -07005581 *rlen = 0;
5582 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005583 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005584
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 /*
5586 * Convert to a btree if necessary.
5587 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005588 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005589 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005590 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005591 &cur, 0, &tmp_logflags, whichfork);
5592 logflags |= tmp_logflags;
5593 if (error)
5594 goto error0;
5595 }
5596 /*
5597 * transform from btree to extents, give it cur
5598 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005599 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005600 ASSERT(cur != NULL);
5601 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5602 whichfork);
5603 logflags |= tmp_logflags;
5604 if (error)
5605 goto error0;
5606 }
5607 /*
5608 * transform from extents to local?
5609 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005610 error = 0;
5611error0:
5612 /*
5613 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005614 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005615 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005616 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005618 logflags &= ~xfs_ilog_fext(whichfork);
5619 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005621 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 /*
5623 * Log inode even in the error case, if the transaction
5624 * is dirty we'll need to shut down the filesystem.
5625 */
5626 if (logflags)
5627 xfs_trans_log_inode(tp, ip, logflags);
5628 if (cur) {
5629 if (!error) {
5630 *firstblock = cur->bc_private.b.firstblock;
5631 cur->bc_private.b.allocated = 0;
5632 }
5633 xfs_btree_del_cursor(cur,
5634 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5635 }
5636 return error;
5637}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005638
Darrick J. Wong44535932016-10-03 09:11:29 -07005639/* Unmap a range of a file. */
5640int
5641xfs_bunmapi(
5642 xfs_trans_t *tp,
5643 struct xfs_inode *ip,
5644 xfs_fileoff_t bno,
5645 xfs_filblks_t len,
5646 int flags,
5647 xfs_extnum_t nexts,
5648 xfs_fsblock_t *firstblock,
5649 struct xfs_defer_ops *dfops,
5650 int *done)
5651{
5652 int error;
5653
5654 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5655 dfops);
5656 *done = (len == 0);
5657 return error;
5658}
5659
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005660/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005661 * Determine whether an extent shift can be accomplished by a merge with the
5662 * extent that precedes the target hole of the shift.
5663 */
5664STATIC bool
5665xfs_bmse_can_merge(
5666 struct xfs_bmbt_irec *left, /* preceding extent */
5667 struct xfs_bmbt_irec *got, /* current extent to shift */
5668 xfs_fileoff_t shift) /* shift fsb */
5669{
5670 xfs_fileoff_t startoff;
5671
5672 startoff = got->br_startoff - shift;
5673
5674 /*
5675 * The extent, once shifted, must be adjacent in-file and on-disk with
5676 * the preceding extent.
5677 */
5678 if ((left->br_startoff + left->br_blockcount != startoff) ||
5679 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5680 (left->br_state != got->br_state) ||
5681 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5682 return false;
5683
5684 return true;
5685}
5686
5687/*
5688 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5689 * hole in the file. If an extent shift would result in the extent being fully
5690 * adjacent to the extent that currently precedes the hole, we can merge with
5691 * the preceding extent rather than do the shift.
5692 *
5693 * This function assumes the caller has verified a shift-by-merge is possible
5694 * with the provided extents via xfs_bmse_can_merge().
5695 */
5696STATIC int
5697xfs_bmse_merge(
5698 struct xfs_inode *ip,
5699 int whichfork,
5700 xfs_fileoff_t shift, /* shift fsb */
5701 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005702 struct xfs_bmbt_irec *got, /* extent to shift */
5703 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005704 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005705 int *logflags, /* output */
5706 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005707{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005708 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5709 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005710 xfs_filblks_t blockcount;
5711 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005712 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005713
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005714 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005715
5716 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5717 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005718 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005719
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005720 new = *left;
5721 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005722
5723 /*
5724 * Update the on-disk extent count, the btree if necessary and log the
5725 * inode.
5726 */
5727 XFS_IFORK_NEXT_SET(ip, whichfork,
5728 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5729 *logflags |= XFS_ILOG_CORE;
5730 if (!cur) {
5731 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005732 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005733 }
5734
5735 /* lookup and remove the extent to merge */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005736 error = xfs_bmbt_lookup_eq(cur, got->br_startoff, got->br_startblock,
5737 got->br_blockcount, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005738 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005739 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005740 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005741
5742 error = xfs_btree_delete(cur, &i);
5743 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005744 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005745 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005746
5747 /* lookup and update size of the previous extent */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005748 error = xfs_bmbt_lookup_eq(cur, left->br_startoff, left->br_startblock,
5749 left->br_blockcount, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005750 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005751 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005752 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005753
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005754 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005755 if (error)
5756 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005757
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005758done:
5759 xfs_iext_update_extent(ifp, current_ext - 1, &new);
5760 xfs_iext_remove(ip, current_ext, 1, 0);
5761
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005762 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005763 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5764 if (error)
5765 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005766 memcpy(&new, got, sizeof(new));
5767 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005768 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005769}
5770
5771/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005772 * Shift a single extent.
5773 */
5774STATIC int
5775xfs_bmse_shift_one(
5776 struct xfs_inode *ip,
5777 int whichfork,
5778 xfs_fileoff_t offset_shift_fsb,
5779 int *current_ext,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005780 struct xfs_bmbt_irec *got,
Brian Fostera979bdf2014-09-23 15:39:04 +10005781 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005782 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005783 enum shift_direction direction,
5784 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005785{
5786 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005787 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005788 xfs_fileoff_t startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005789 struct xfs_bmbt_irec adj_irec, new;
Brian Fostera979bdf2014-09-23 15:39:04 +10005790 int error;
5791 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005792 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005793
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005794 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005795 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005796 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005797
Brian Fosterf71721d2014-09-23 15:39:05 +10005798 /* delalloc extents should be prevented by caller */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005799 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got->br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005800
Namjae Jeona904b1c2015-03-25 15:08:56 +11005801 if (direction == SHIFT_LEFT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005802 startoff = got->br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005803
Namjae Jeona904b1c2015-03-25 15:08:56 +11005804 /*
5805 * Check for merge if we've got an extent to the left,
5806 * otherwise make sure there's enough room at the start
5807 * of the file for the shift.
5808 */
5809 if (!*current_ext) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005810 if (got->br_startoff < offset_shift_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005811 return -EINVAL;
5812 goto update_current_ext;
5813 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005814
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005815 /*
5816 * grab the left extent and check for a large enough hole.
5817 */
5818 xfs_iext_get_extent(ifp, *current_ext - 1, &adj_irec);
5819 if (startoff < adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005820 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005821
Dave Chinnerb11bd672014-12-04 09:42:24 +11005822 /* check whether to merge the extent or shift it down */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005823 if (xfs_bmse_can_merge(&adj_irec, got, offset_shift_fsb)) {
5824 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5825 *current_ext, got, &adj_irec,
5826 cur, logflags, dfops);
Dave Chinnerb11bd672014-12-04 09:42:24 +11005827 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005828 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005829 startoff = got->br_startoff + offset_shift_fsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005830 /* nothing to move if this is the last extent */
5831 if (*current_ext >= (total_extents - 1))
5832 goto update_current_ext;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005833
Namjae Jeona904b1c2015-03-25 15:08:56 +11005834 /*
5835 * If this is not the last extent in the file, make sure there
5836 * is enough room between current extent and next extent for
5837 * accommodating the shift.
5838 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005839 xfs_iext_get_extent(ifp, *current_ext + 1, &adj_irec);
5840 if (startoff + got->br_blockcount > adj_irec.br_startoff)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005841 return -EINVAL;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005842
Namjae Jeona904b1c2015-03-25 15:08:56 +11005843 /*
5844 * Unlike a left shift (which involves a hole punch),
5845 * a right shift does not modify extent neighbors
5846 * in any way. We should never find mergeable extents
5847 * in this scenario. Check anyways and warn if we
5848 * encounter two extents that could be one.
5849 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005850 if (xfs_bmse_can_merge(got, &adj_irec, offset_shift_fsb))
Namjae Jeona904b1c2015-03-25 15:08:56 +11005851 WARN_ON_ONCE(1);
5852 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005853
Brian Fostera979bdf2014-09-23 15:39:04 +10005854 /*
5855 * Increment the extent index for the next iteration, update the start
5856 * offset of the in-core extent and update the btree if applicable.
5857 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005858update_current_ext:
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005859 *logflags |= XFS_ILOG_CORE;
5860
5861 new = *got;
5862 new.br_startoff = startoff;
5863
5864 if (cur) {
5865 error = xfs_bmbt_lookup_eq(cur, got->br_startoff,
5866 got->br_startblock, got->br_blockcount, &i);
5867 if (error)
5868 return error;
5869 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5870
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005871 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005872 if (error)
5873 return error;
5874 } else {
5875 *logflags |= XFS_ILOG_DEXT;
5876 }
5877
5878 xfs_iext_update_extent(ifp, *current_ext, &new);
5879
Namjae Jeona904b1c2015-03-25 15:08:56 +11005880 if (direction == SHIFT_LEFT)
5881 (*current_ext)++;
5882 else
5883 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005884
Darrick J. Wong9c194642016-08-03 12:16:05 +10005885 /* update reverse mapping */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005886 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005887 if (error)
5888 return error;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005889 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fostera979bdf2014-09-23 15:39:04 +10005890}
5891
5892/*
Namjae Jeona904b1c2015-03-25 15:08:56 +11005893 * Shift extent records to the left/right to cover/create a hole.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005894 *
Brian Foster2c845f52014-09-23 15:37:09 +10005895 * The maximum number of extents to be shifted in a single operation is
Namjae Jeona904b1c2015-03-25 15:08:56 +11005896 * @num_exts. @stop_fsb specifies the file offset at which to stop shift and the
Brian Foster2c845f52014-09-23 15:37:09 +10005897 * file offset where we've left off is returned in @next_fsb. @offset_shift_fsb
5898 * is the length by which each extent is shifted. If there is no hole to shift
5899 * the extents into, this will be considered invalid operation and we abort
5900 * immediately.
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005901 */
5902int
5903xfs_bmap_shift_extents(
5904 struct xfs_trans *tp,
5905 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005906 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005907 xfs_fileoff_t offset_shift_fsb,
Brian Foster2c845f52014-09-23 15:37:09 +10005908 int *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005909 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005910 xfs_fsblock_t *firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005911 struct xfs_defer_ops *dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005912 enum shift_direction direction,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005913 int num_exts)
5914{
Brian Fosterca446d82014-09-02 12:12:53 +10005915 struct xfs_btree_cur *cur = NULL;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005916 struct xfs_bmbt_irec got;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005917 struct xfs_mount *mp = ip->i_mount;
5918 struct xfs_ifork *ifp;
5919 xfs_extnum_t nexts = 0;
Brian Foster2c845f52014-09-23 15:37:09 +10005920 xfs_extnum_t current_ext;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005921 xfs_extnum_t total_extents;
5922 xfs_extnum_t stop_extent;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005923 int error = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005924 int whichfork = XFS_DATA_FORK;
Brian Fosterca446d82014-09-02 12:12:53 +10005925 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005926
5927 if (unlikely(XFS_TEST_ERROR(
5928 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5929 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005930 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005931 XFS_ERROR_REPORT("xfs_bmap_shift_extents",
5932 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005933 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005934 }
5935
5936 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005937 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005938
Brian Foster2c845f52014-09-23 15:37:09 +10005939 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5940 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005941 ASSERT(direction == SHIFT_LEFT || direction == SHIFT_RIGHT);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005942
5943 ifp = XFS_IFORK_PTR(ip, whichfork);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005944 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5945 /* Read in all the extents */
5946 error = xfs_iread_extents(tp, ip, whichfork);
5947 if (error)
5948 return error;
5949 }
5950
Brian Fosterddb19e32014-09-23 15:38:09 +10005951 if (ifp->if_flags & XFS_IFBROOT) {
5952 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5953 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005954 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005955 cur->bc_private.b.flags = 0;
5956 }
5957
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005958 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005959 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10005960 * are collapsing out, so we cannot use the count of real extents here.
5961 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005962 */
Eric Sandeen5d829302016-11-08 12:59:42 +11005963 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005964 if (total_extents == 0) {
5965 *done = 1;
5966 goto del_cursor;
5967 }
5968
5969 /*
5970 * In case of first right shift, we need to initialize next_fsb
5971 */
5972 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005973 ASSERT(direction == SHIFT_RIGHT);
5974
5975 current_ext = total_extents - 1;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005976 xfs_iext_get_extent(ifp, current_ext, &got);
5977 if (stop_fsb > got.br_startoff) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005978 *done = 1;
5979 goto del_cursor;
5980 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005981 *next_fsb = got.br_startoff;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005982 } else {
5983 /*
5984 * Look up the extent index for the fsb where we start shifting. We can
5985 * henceforth iterate with current_ext as extent list changes are locked
5986 * out via ilock.
5987 *
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005988 * If next_fsb lies in a hole beyond which there are no extents we are
5989 * done.
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005990 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005991 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
5992 &got)) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005993 *done = 1;
5994 goto del_cursor;
5995 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005996 }
5997
5998 /* Lookup the extent index at which we have to stop */
5999 if (direction == SHIFT_RIGHT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006000 struct xfs_bmbt_irec s;
6001
6002 xfs_iext_lookup_extent(ip, ifp, stop_fsb, &stop_extent, &s);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006003 /* Make stop_extent exclusive of shift range */
6004 stop_extent--;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006005 if (current_ext <= stop_extent) {
6006 error = -EIO;
6007 goto del_cursor;
6008 }
6009 } else {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006010 stop_extent = total_extents;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07006011 if (current_ext >= stop_extent) {
6012 error = -EIO;
6013 goto del_cursor;
6014 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11006015 }
6016
6017 while (nexts++ < num_exts) {
Brian Fostera979bdf2014-09-23 15:39:04 +10006018 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006019 &current_ext, &got, cur, &logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10006020 direction, dfops);
Brian Fostera979bdf2014-09-23 15:39:04 +10006021 if (error)
6022 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006023 /*
6024 * If there was an extent merge during the shift, the extent
6025 * count can change. Update the total and grade the next record.
6026 */
6027 if (direction == SHIFT_LEFT) {
Eric Sandeen5d829302016-11-08 12:59:42 +11006028 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006029 stop_extent = total_extents;
6030 }
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006031
Namjae Jeona904b1c2015-03-25 15:08:56 +11006032 if (current_ext == stop_extent) {
6033 *done = 1;
6034 *next_fsb = NULLFSBLOCK;
Brian Fosterddb19e32014-09-23 15:38:09 +10006035 break;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006036 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006037 xfs_iext_get_extent(ifp, current_ext, &got);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006038 }
6039
Christoph Hellwig4da6b512017-08-29 15:44:13 -07006040 if (!*done)
Brian Foster2c845f52014-09-23 15:37:09 +10006041 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006042
6043del_cursor:
6044 if (cur)
6045 xfs_btree_del_cursor(cur,
6046 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6047
Brian Fosterca446d82014-09-02 12:12:53 +10006048 if (logflags)
6049 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10006050
Namjae Jeone1d8fb82014-02-24 10:58:19 +11006051 return error;
6052}
Namjae Jeona904b1c2015-03-25 15:08:56 +11006053
6054/*
6055 * Splits an extent into two extents at split_fsb block such that it is
6056 * the first block of the current_ext. @current_ext is a target extent
6057 * to be split. @split_fsb is a block where the extents is split.
6058 * If split_fsb lies in a hole or the first block of extents, just return 0.
6059 */
6060STATIC int
6061xfs_bmap_split_extent_at(
6062 struct xfs_trans *tp,
6063 struct xfs_inode *ip,
6064 xfs_fileoff_t split_fsb,
6065 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006066 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006067{
6068 int whichfork = XFS_DATA_FORK;
6069 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006070 struct xfs_bmbt_irec got;
6071 struct xfs_bmbt_irec new; /* split extent */
6072 struct xfs_mount *mp = ip->i_mount;
6073 struct xfs_ifork *ifp;
6074 xfs_fsblock_t gotblkcnt; /* new block count for got */
6075 xfs_extnum_t current_ext;
6076 int error = 0;
6077 int logflags = 0;
6078 int i = 0;
6079
6080 if (unlikely(XFS_TEST_ERROR(
6081 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
6082 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006083 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11006084 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
6085 XFS_ERRLEVEL_LOW, mp);
6086 return -EFSCORRUPTED;
6087 }
6088
6089 if (XFS_FORCED_SHUTDOWN(mp))
6090 return -EIO;
6091
6092 ifp = XFS_IFORK_PTR(ip, whichfork);
6093 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
6094 /* Read in all the extents */
6095 error = xfs_iread_extents(tp, ip, whichfork);
6096 if (error)
6097 return error;
6098 }
6099
6100 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006101 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11006102 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006103 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
6104 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006105 return 0;
6106
6107 gotblkcnt = split_fsb - got.br_startoff;
6108 new.br_startoff = split_fsb;
6109 new.br_startblock = got.br_startblock + gotblkcnt;
6110 new.br_blockcount = got.br_blockcount - gotblkcnt;
6111 new.br_state = got.br_state;
6112
6113 if (ifp->if_flags & XFS_IFBROOT) {
6114 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
6115 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006116 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006117 cur->bc_private.b.flags = 0;
6118 error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
6119 got.br_startblock,
6120 got.br_blockcount,
6121 &i);
6122 if (error)
6123 goto del_cursor;
6124 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6125 }
6126
Namjae Jeona904b1c2015-03-25 15:08:56 +11006127 got.br_blockcount = gotblkcnt;
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07006128 xfs_iext_update_extent(ifp, current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006129
6130 logflags = XFS_ILOG_CORE;
6131 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07006132 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006133 if (error)
6134 goto del_cursor;
6135 } else
6136 logflags |= XFS_ILOG_DEXT;
6137
6138 /* Add new extent */
6139 current_ext++;
6140 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6141 XFS_IFORK_NEXT_SET(ip, whichfork,
6142 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6143
6144 if (cur) {
6145 error = xfs_bmbt_lookup_eq(cur, new.br_startoff,
6146 new.br_startblock, new.br_blockcount,
6147 &i);
6148 if (error)
6149 goto del_cursor;
6150 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
6151 cur->bc_rec.b.br_state = new.br_state;
6152
6153 error = xfs_btree_insert(cur, &i);
6154 if (error)
6155 goto del_cursor;
6156 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6157 }
6158
6159 /*
6160 * Convert to a btree if necessary.
6161 */
6162 if (xfs_bmap_needs_btree(ip, whichfork)) {
6163 int tmp_logflags; /* partial log flag return val */
6164
6165 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006166 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006167 &cur, 0, &tmp_logflags, whichfork);
6168 logflags |= tmp_logflags;
6169 }
6170
6171del_cursor:
6172 if (cur) {
6173 cur->bc_private.b.allocated = 0;
6174 xfs_btree_del_cursor(cur,
6175 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6176 }
6177
6178 if (logflags)
6179 xfs_trans_log_inode(tp, ip, logflags);
6180 return error;
6181}
6182
6183int
6184xfs_bmap_split_extent(
6185 struct xfs_inode *ip,
6186 xfs_fileoff_t split_fsb)
6187{
6188 struct xfs_mount *mp = ip->i_mount;
6189 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006190 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006191 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006192 int error;
6193
Christoph Hellwig253f4912016-04-06 09:19:55 +10006194 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6195 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6196 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006197 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006198
6199 xfs_ilock(ip, XFS_ILOCK_EXCL);
6200 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6201
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006202 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006203
6204 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006205 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006206 if (error)
6207 goto out;
6208
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006209 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006210 if (error)
6211 goto out;
6212
Christoph Hellwig70393312015-06-04 13:48:08 +10006213 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006214
6215out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006216 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006217 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006218 return error;
6219}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006220
6221/* Deferred mapping is only for real extents in the data fork. */
6222static bool
6223xfs_bmap_is_update_needed(
6224 struct xfs_bmbt_irec *bmap)
6225{
6226 return bmap->br_startblock != HOLESTARTBLOCK &&
6227 bmap->br_startblock != DELAYSTARTBLOCK;
6228}
6229
6230/* Record a bmap intent. */
6231static int
6232__xfs_bmap_add(
6233 struct xfs_mount *mp,
6234 struct xfs_defer_ops *dfops,
6235 enum xfs_bmap_intent_type type,
6236 struct xfs_inode *ip,
6237 int whichfork,
6238 struct xfs_bmbt_irec *bmap)
6239{
6240 int error;
6241 struct xfs_bmap_intent *bi;
6242
6243 trace_xfs_bmap_defer(mp,
6244 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6245 type,
6246 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6247 ip->i_ino, whichfork,
6248 bmap->br_startoff,
6249 bmap->br_blockcount,
6250 bmap->br_state);
6251
6252 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6253 INIT_LIST_HEAD(&bi->bi_list);
6254 bi->bi_type = type;
6255 bi->bi_owner = ip;
6256 bi->bi_whichfork = whichfork;
6257 bi->bi_bmap = *bmap;
6258
Christoph Hellwig882d8782017-08-28 10:21:03 -07006259 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006260 if (error) {
6261 kmem_free(bi);
6262 return error;
6263 }
6264
6265 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6266 return 0;
6267}
6268
6269/* Map an extent into a file. */
6270int
6271xfs_bmap_map_extent(
6272 struct xfs_mount *mp,
6273 struct xfs_defer_ops *dfops,
6274 struct xfs_inode *ip,
6275 struct xfs_bmbt_irec *PREV)
6276{
6277 if (!xfs_bmap_is_update_needed(PREV))
6278 return 0;
6279
6280 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6281 XFS_DATA_FORK, PREV);
6282}
6283
6284/* Unmap an extent out of a file. */
6285int
6286xfs_bmap_unmap_extent(
6287 struct xfs_mount *mp,
6288 struct xfs_defer_ops *dfops,
6289 struct xfs_inode *ip,
6290 struct xfs_bmbt_irec *PREV)
6291{
6292 if (!xfs_bmap_is_update_needed(PREV))
6293 return 0;
6294
6295 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6296 XFS_DATA_FORK, PREV);
6297}
6298
6299/*
6300 * Process one of the deferred bmap operations. We pass back the
6301 * btree cursor to maintain our lock on the bmapbt between calls.
6302 */
6303int
6304xfs_bmap_finish_one(
6305 struct xfs_trans *tp,
6306 struct xfs_defer_ops *dfops,
6307 struct xfs_inode *ip,
6308 enum xfs_bmap_intent_type type,
6309 int whichfork,
6310 xfs_fileoff_t startoff,
6311 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006312 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006313 xfs_exntst_t state)
6314{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006315 xfs_fsblock_t firstfsb;
6316 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006317
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006318 /*
6319 * firstfsb is tied to the transaction lifetime and is used to
6320 * ensure correct AG locking order and schedule work item
6321 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6322 * to only making one bmap call per transaction, so it should
6323 * be safe to have it as a local variable here.
6324 */
6325 firstfsb = NULLFSBLOCK;
6326
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006327 trace_xfs_bmap_deferred(tp->t_mountp,
6328 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6329 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006330 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006331
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006332 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006333 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006334
6335 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006336 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006337 return -EIO;
6338
6339 switch (type) {
6340 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006341 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006342 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006343 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006344 break;
6345 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006346 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6347 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006348 break;
6349 default:
6350 ASSERT(0);
6351 error = -EFSCORRUPTED;
6352 }
6353
6354 return error;
6355}