blob: 47fb51774fcc6c2e8132db1ae6a11c13fb64d5a8 [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,
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700115 struct xfs_bmbt_irec *irec,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100116 int *stat) /* success/failure */
117{
Christoph Hellwige16cf9b2017-10-17 14:16:26 -0700118 cur->bc_rec.b = *irec;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100119 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
120}
121
122STATIC int /* error */
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700123xfs_bmbt_lookup_first(
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100124 struct xfs_btree_cur *cur,
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100125 int *stat) /* success/failure */
126{
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700127 cur->bc_rec.b.br_startoff = 0;
128 cur->bc_rec.b.br_startblock = 0;
129 cur->bc_rec.b.br_blockcount = 0;
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100130 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
131}
132
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100133/*
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000134 * Check if the inode needs to be converted to btree format.
135 */
136static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
137{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700138 return whichfork != XFS_COW_FORK &&
139 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000140 XFS_IFORK_NEXTENTS(ip, whichfork) >
141 XFS_IFORK_MAXEXT(ip, whichfork);
142}
143
144/*
145 * Check if the inode should be converted to extent format.
146 */
147static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
148{
Darrick J. Wong60b49842016-10-03 09:11:34 -0700149 return whichfork != XFS_COW_FORK &&
150 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
Christoph Hellwig8096b1e2011-12-18 20:00:07 +0000151 XFS_IFORK_NEXTENTS(ip, whichfork) <=
152 XFS_IFORK_MAXEXT(ip, whichfork);
153}
154
155/*
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700156 * Update the record referred to by cur to the value given by irec
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100157 * This either works (return 0) or gets an EFSCORRUPTED error.
158 */
159STATIC int
160xfs_bmbt_update(
161 struct xfs_btree_cur *cur,
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700162 struct xfs_bmbt_irec *irec)
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100163{
164 union xfs_btree_rec rec;
165
Christoph Hellwiga67d00a2017-10-17 14:16:26 -0700166 xfs_bmbt_disk_set_all(&rec.bmbt, irec);
Christoph Hellwig278d0ca2008-10-30 16:56:32 +1100167 return xfs_btree_update(cur, &rec);
168}
Christoph Hellwigfe033cc2008-10-30 16:56:09 +1100169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100171 * Compute the worst-case number of indirect blocks that will be used
172 * for ip's delayed extent of length "len".
173 */
174STATIC xfs_filblks_t
175xfs_bmap_worst_indlen(
176 xfs_inode_t *ip, /* incore inode pointer */
177 xfs_filblks_t len) /* delayed extent length */
178{
179 int level; /* btree level number */
180 int maxrecs; /* maximum record count at this level */
181 xfs_mount_t *mp; /* mount structure */
182 xfs_filblks_t rval; /* return value */
183
184 mp = ip->i_mount;
185 maxrecs = mp->m_bmap_dmxr[0];
186 for (level = 0, rval = 0;
187 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
188 level++) {
189 len += maxrecs - 1;
190 do_div(len, maxrecs);
191 rval += len;
Darrick J. Wong5e5c9432017-09-18 09:41:17 -0700192 if (len == 1)
193 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100194 level - 1;
195 if (level == 0)
196 maxrecs = mp->m_bmap_dmxr[1];
197 }
198 return rval;
199}
200
201/*
202 * Calculate the default attribute fork offset for newly created inodes.
203 */
204uint
205xfs_default_attroffset(
206 struct xfs_inode *ip)
207{
208 struct xfs_mount *mp = ip->i_mount;
209 uint offset;
210
211 if (mp->m_sb.sb_inodesize == 256) {
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100212 offset = XFS_LITINO(mp, ip->i_d.di_version) -
Dave Chinner9e5987a2013-02-25 12:31:26 +1100213 XFS_BMDR_SPACE_CALC(MINABTPTRS);
214 } else {
215 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
216 }
217
Christoph Hellwig56cea2d2013-03-12 23:30:36 +1100218 ASSERT(offset < XFS_LITINO(mp, ip->i_d.di_version));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100219 return offset;
220}
221
222/*
223 * Helper routine to reset inode di_forkoff field when switching
224 * attribute fork from local to extent format - we reset it where
225 * possible to make space available for inline data fork extents.
226 */
227STATIC void
228xfs_bmap_forkoff_reset(
Dave Chinner9e5987a2013-02-25 12:31:26 +1100229 xfs_inode_t *ip,
230 int whichfork)
231{
232 if (whichfork == XFS_ATTR_FORK &&
233 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
Dave Chinner9e5987a2013-02-25 12:31:26 +1100234 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
235 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
236
237 if (dfl_forkoff > ip->i_d.di_forkoff)
238 ip->i_d.di_forkoff = dfl_forkoff;
239 }
240}
241
Dave Chinner9e5987a2013-02-25 12:31:26 +1100242#ifdef DEBUG
243STATIC struct xfs_buf *
244xfs_bmap_get_bp(
245 struct xfs_btree_cur *cur,
246 xfs_fsblock_t bno)
247{
248 struct xfs_log_item_desc *lidp;
249 int i;
250
251 if (!cur)
252 return NULL;
253
254 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
255 if (!cur->bc_bufs[i])
256 break;
257 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
258 return cur->bc_bufs[i];
259 }
260
261 /* Chase down all the log items to see if the bp is there */
262 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
263 struct xfs_buf_log_item *bip;
264 bip = (struct xfs_buf_log_item *)lidp->lid_item;
265 if (bip->bli_item.li_type == XFS_LI_BUF &&
266 XFS_BUF_ADDR(bip->bli_buf) == bno)
267 return bip->bli_buf;
268 }
269
270 return NULL;
271}
272
273STATIC void
274xfs_check_block(
275 struct xfs_btree_block *block,
276 xfs_mount_t *mp,
277 int root,
278 short sz)
279{
280 int i, j, dmxr;
281 __be64 *pp, *thispa; /* pointer to block address */
282 xfs_bmbt_key_t *prevp, *keyp;
283
284 ASSERT(be16_to_cpu(block->bb_level) > 0);
285
286 prevp = NULL;
287 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
288 dmxr = mp->m_bmap_dmxr[0];
289 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
290
291 if (prevp) {
292 ASSERT(be64_to_cpu(prevp->br_startoff) <
293 be64_to_cpu(keyp->br_startoff));
294 }
295 prevp = keyp;
296
297 /*
298 * Compare the block numbers to see if there are dups.
299 */
300 if (root)
301 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
302 else
303 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
304
305 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
306 if (root)
307 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
308 else
309 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
310 if (*thispa == *pp) {
311 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
312 __func__, j, i,
313 (unsigned long long)be64_to_cpu(*thispa));
314 panic("%s: ptrs are equal in node\n",
315 __func__);
316 }
317 }
318 }
319}
320
321/*
322 * Check that the extents for the inode ip are in the right order in all
Dave Chinnere3543812016-01-08 11:28:49 +1100323 * btree leaves. THis becomes prohibitively expensive for large extent count
324 * files, so don't bother with inodes that have more than 10,000 extents in
325 * them. The btree record ordering checks will still be done, so for such large
326 * bmapbt constructs that is going to catch most corruptions.
Dave Chinner9e5987a2013-02-25 12:31:26 +1100327 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100328STATIC void
329xfs_bmap_check_leaf_extents(
330 xfs_btree_cur_t *cur, /* btree cursor or null */
331 xfs_inode_t *ip, /* incore inode pointer */
332 int whichfork) /* data or attr fork */
333{
334 struct xfs_btree_block *block; /* current btree block */
335 xfs_fsblock_t bno; /* block # of "block" */
336 xfs_buf_t *bp; /* buffer for "block" */
337 int error; /* error return value */
338 xfs_extnum_t i=0, j; /* index into the extents list */
339 xfs_ifork_t *ifp; /* fork structure */
340 int level; /* btree level, for checking */
341 xfs_mount_t *mp; /* file system mount structure */
342 __be64 *pp; /* pointer to block address */
343 xfs_bmbt_rec_t *ep; /* pointer to current extent */
344 xfs_bmbt_rec_t last = {0, 0}; /* last extent in prev block */
345 xfs_bmbt_rec_t *nextp; /* pointer to next extent */
346 int bp_release = 0;
347
348 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
349 return;
350 }
351
Dave Chinnere3543812016-01-08 11:28:49 +1100352 /* skip large extent count inodes */
353 if (ip->i_d.di_nextents > 10000)
354 return;
355
Dave Chinner9e5987a2013-02-25 12:31:26 +1100356 bno = NULLFSBLOCK;
357 mp = ip->i_mount;
358 ifp = XFS_IFORK_PTR(ip, whichfork);
359 block = ifp->if_broot;
360 /*
361 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
362 */
363 level = be16_to_cpu(block->bb_level);
364 ASSERT(level > 0);
365 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
366 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
367 bno = be64_to_cpu(*pp);
368
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000369 ASSERT(bno != NULLFSBLOCK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100370 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
371 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
372
373 /*
374 * Go down the tree until leaf level is reached, following the first
375 * pointer (leftmost) at each level.
376 */
377 while (level-- > 0) {
378 /* See if buf is in cur first */
379 bp_release = 0;
380 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
381 if (!bp) {
382 bp_release = 1;
383 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
384 XFS_BMAP_BTREE_REF,
385 &xfs_bmbt_buf_ops);
386 if (error)
387 goto error_norelse;
388 }
389 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100390 if (level == 0)
391 break;
392
393 /*
394 * Check this block for basic sanity (increasing keys and
395 * no duplicate blocks).
396 */
397
398 xfs_check_block(block, mp, 0, 0);
399 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
400 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +1100401 XFS_WANT_CORRUPTED_GOTO(mp,
402 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100403 if (bp_release) {
404 bp_release = 0;
405 xfs_trans_brelse(NULL, bp);
406 }
407 }
408
409 /*
410 * Here with bp and block set to the leftmost leaf node in the tree.
411 */
412 i = 0;
413
414 /*
415 * Loop over all leaf nodes checking that all extents are in the right order.
416 */
417 for (;;) {
418 xfs_fsblock_t nextbno;
419 xfs_extnum_t num_recs;
420
421
422 num_recs = xfs_btree_get_numrecs(block);
423
424 /*
425 * Read-ahead the next leaf block, if any.
426 */
427
428 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
429
430 /*
431 * Check all the extents to make sure they are OK.
432 * If we had a previous block, the last entry should
433 * conform with the first entry in this one.
434 */
435
436 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
437 if (i) {
438 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
439 xfs_bmbt_disk_get_blockcount(&last) <=
440 xfs_bmbt_disk_get_startoff(ep));
441 }
442 for (j = 1; j < num_recs; j++) {
443 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
444 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
445 xfs_bmbt_disk_get_blockcount(ep) <=
446 xfs_bmbt_disk_get_startoff(nextp));
447 ep = nextp;
448 }
449
450 last = *ep;
451 i += num_recs;
452 if (bp_release) {
453 bp_release = 0;
454 xfs_trans_brelse(NULL, bp);
455 }
456 bno = nextbno;
457 /*
458 * If we've reached the end, stop.
459 */
460 if (bno == NULLFSBLOCK)
461 break;
462
463 bp_release = 0;
464 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
465 if (!bp) {
466 bp_release = 1;
467 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
468 XFS_BMAP_BTREE_REF,
469 &xfs_bmbt_buf_ops);
470 if (error)
471 goto error_norelse;
472 }
473 block = XFS_BUF_TO_BLOCK(bp);
474 }
Luis de Bethencourta5fd2762016-03-09 08:17:56 +1100475
Dave Chinner9e5987a2013-02-25 12:31:26 +1100476 return;
477
478error0:
479 xfs_warn(mp, "%s: at error0", __func__);
480 if (bp_release)
481 xfs_trans_brelse(NULL, bp);
482error_norelse:
483 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
484 __func__, i);
485 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
486 return;
487}
488
489/*
Dave Chinner9e5987a2013-02-25 12:31:26 +1100490 * Validate that the bmbt_irecs being returned from bmapi are valid
Zhi Yong Wua97f4df2013-08-12 03:14:53 +0000491 * given the caller's original parameters. Specifically check the
492 * ranges of the returned irecs to ensure that they only extend beyond
Dave Chinner9e5987a2013-02-25 12:31:26 +1100493 * the given parameters if the XFS_BMAPI_ENTIRE flag was set.
494 */
495STATIC void
496xfs_bmap_validate_ret(
497 xfs_fileoff_t bno,
498 xfs_filblks_t len,
499 int flags,
500 xfs_bmbt_irec_t *mval,
501 int nmap,
502 int ret_nmap)
503{
504 int i; /* index to map values */
505
506 ASSERT(ret_nmap <= nmap);
507
508 for (i = 0; i < ret_nmap; i++) {
509 ASSERT(mval[i].br_blockcount > 0);
510 if (!(flags & XFS_BMAPI_ENTIRE)) {
511 ASSERT(mval[i].br_startoff >= bno);
512 ASSERT(mval[i].br_blockcount <= len);
513 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
514 bno + len);
515 } else {
516 ASSERT(mval[i].br_startoff < bno + len);
517 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
518 bno);
519 }
520 ASSERT(i == 0 ||
521 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
522 mval[i].br_startoff);
523 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
524 mval[i].br_startblock != HOLESTARTBLOCK);
525 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
526 mval[i].br_state == XFS_EXT_UNWRITTEN);
527 }
528}
529
530#else
531#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
Darrick J. Wong7bf7a192017-08-31 15:11:06 -0700532#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap) do { } while (0)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100533#endif /* DEBUG */
534
535/*
536 * bmap free list manipulation functions
537 */
538
539/*
540 * Add the extent to the list of extents to be free at transaction end.
541 * The list is maintained sorted (by block number).
542 */
543void
544xfs_bmap_add_free(
Darrick J. Wong340785c2016-08-03 11:33:42 +1000545 struct xfs_mount *mp,
546 struct xfs_defer_ops *dfops,
547 xfs_fsblock_t bno,
548 xfs_filblks_t len,
549 struct xfs_owner_info *oinfo)
Dave Chinner9e5987a2013-02-25 12:31:26 +1100550{
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000551 struct xfs_extent_free_item *new; /* new element */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100552#ifdef DEBUG
553 xfs_agnumber_t agno;
554 xfs_agblock_t agbno;
555
556 ASSERT(bno != NULLFSBLOCK);
557 ASSERT(len > 0);
558 ASSERT(len <= MAXEXTLEN);
559 ASSERT(!isnullstartblock(bno));
560 agno = XFS_FSB_TO_AGNO(mp, bno);
561 agbno = XFS_FSB_TO_AGBNO(mp, bno);
562 ASSERT(agno < mp->m_sb.sb_agcount);
563 ASSERT(agbno < mp->m_sb.sb_agblocks);
564 ASSERT(len < mp->m_sb.sb_agblocks);
565 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
566#endif
567 ASSERT(xfs_bmap_free_item_zone != NULL);
Darrick J. Wong340785c2016-08-03 11:33:42 +1000568
Dave Chinner9e5987a2013-02-25 12:31:26 +1100569 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
Darrick J. Wong310a75a2016-08-03 11:18:10 +1000570 new->xefi_startblock = bno;
571 new->xefi_blockcount = (xfs_extlen_t)len;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000572 if (oinfo)
573 new->xefi_oinfo = *oinfo;
574 else
575 xfs_rmap_skip_owner_update(&new->xefi_oinfo);
Darrick J. Wongba9e7802016-08-03 11:26:33 +1000576 trace_xfs_bmap_free_defer(mp, XFS_FSB_TO_AGNO(mp, bno), 0,
577 XFS_FSB_TO_AGBNO(mp, bno), len);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000578 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_FREE, &new->xefi_list);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100579}
580
581/*
582 * Inode fork format manipulation functions
583 */
584
585/*
586 * Transform a btree format file with only one leaf node, where the
587 * extents list will fit in the inode, into an extents format file.
588 * Since the file extents are already in-core, all we have to do is
589 * give up the space for the btree root and pitch the leaf block.
590 */
591STATIC int /* error */
592xfs_bmap_btree_to_extents(
593 xfs_trans_t *tp, /* transaction pointer */
594 xfs_inode_t *ip, /* incore inode pointer */
595 xfs_btree_cur_t *cur, /* btree cursor */
596 int *logflagsp, /* inode logging flags */
597 int whichfork) /* data or attr fork */
598{
599 /* REFERENCED */
600 struct xfs_btree_block *cblock;/* child btree block */
601 xfs_fsblock_t cbno; /* child block number */
602 xfs_buf_t *cbp; /* child block's buffer */
603 int error; /* error return value */
604 xfs_ifork_t *ifp; /* inode fork data */
605 xfs_mount_t *mp; /* mount point structure */
606 __be64 *pp; /* ptr to block address */
607 struct xfs_btree_block *rblock;/* root btree block */
Darrick J. Wong340785c2016-08-03 11:33:42 +1000608 struct xfs_owner_info oinfo;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100609
610 mp = ip->i_mount;
611 ifp = XFS_IFORK_PTR(ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -0700612 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100613 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
614 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
615 rblock = ifp->if_broot;
616 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
617 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
618 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
619 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
620 cbno = be64_to_cpu(*pp);
621 *logflagsp = 0;
622#ifdef DEBUG
Darrick J. Wongf1357612017-10-17 21:37:33 -0700623 XFS_WANT_CORRUPTED_RETURN(cur->bc_mp,
624 xfs_btree_check_lptr(cur, cbno, 1));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100625#endif
626 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
627 &xfs_bmbt_buf_ops);
628 if (error)
629 return error;
630 cblock = XFS_BUF_TO_BLOCK(cbp);
631 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
632 return error;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000633 xfs_rmap_ino_bmbt_owner(&oinfo, ip->i_ino, whichfork);
634 xfs_bmap_add_free(mp, cur->bc_private.b.dfops, cbno, 1, &oinfo);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100635 ip->i_d.di_nblocks--;
636 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
637 xfs_trans_binval(tp, cbp);
638 if (cur->bc_bufs[0] == cbp)
639 cur->bc_bufs[0] = NULL;
640 xfs_iroot_realloc(ip, -1, whichfork);
641 ASSERT(ifp->if_broot == NULL);
642 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
643 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
644 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
645 return 0;
646}
647
648/*
649 * Convert an extents-format file into a btree-format file.
650 * The new file will have a root block (in the inode) and a single child block.
651 */
652STATIC int /* error */
653xfs_bmap_extents_to_btree(
654 xfs_trans_t *tp, /* transaction pointer */
655 xfs_inode_t *ip, /* incore inode pointer */
656 xfs_fsblock_t *firstblock, /* first-block-allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000657 struct xfs_defer_ops *dfops, /* blocks freed in xaction */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100658 xfs_btree_cur_t **curp, /* cursor returned to caller */
659 int wasdel, /* converting a delayed alloc */
660 int *logflagsp, /* inode logging flags */
661 int whichfork) /* data or attr fork */
662{
663 struct xfs_btree_block *ablock; /* allocated (child) bt block */
664 xfs_buf_t *abp; /* buffer for ablock */
665 xfs_alloc_arg_t args; /* allocation arguments */
666 xfs_bmbt_rec_t *arp; /* child record pointer */
667 struct xfs_btree_block *block; /* btree root block */
668 xfs_btree_cur_t *cur; /* bmap btree cursor */
669 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
670 int error; /* error return value */
671 xfs_extnum_t i, cnt; /* extent record index */
672 xfs_ifork_t *ifp; /* inode fork pointer */
673 xfs_bmbt_key_t *kp; /* root block key pointer */
674 xfs_mount_t *mp; /* mount structure */
675 xfs_extnum_t nextents; /* number of file extents */
676 xfs_bmbt_ptr_t *pp; /* root block address pointer */
677
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500678 mp = ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -0700679 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100680 ifp = XFS_IFORK_PTR(ip, whichfork);
681 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
682
683 /*
684 * Make space in the inode incore.
685 */
686 xfs_iroot_realloc(ip, 1, whichfork);
687 ifp->if_flags |= XFS_IFBROOT;
688
689 /*
690 * Fill in the root.
691 */
692 block = ifp->if_broot;
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800693 xfs_btree_init_block_int(mp, block, XFS_BUF_DADDR_NULL,
694 XFS_BTNUM_BMAP, 1, 1, ip->i_ino,
Eric Sandeenf88ae462017-01-27 23:16:37 -0800695 XFS_BTREE_LONG_PTRS);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100696 /*
697 * Need a cursor. Can't allocate until bb_level is filled in.
698 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100699 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
700 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000701 cur->bc_private.b.dfops = dfops;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100702 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
703 /*
704 * Convert to a btree with two levels, one record in root.
705 */
706 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
707 memset(&args, 0, sizeof(args));
708 args.tp = tp;
709 args.mp = mp;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000710 xfs_rmap_ino_bmbt_owner(&args.oinfo, ip->i_ino, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100711 args.firstblock = *firstblock;
712 if (*firstblock == NULLFSBLOCK) {
713 args.type = XFS_ALLOCTYPE_START_BNO;
714 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000715 } else if (dfops->dop_low) {
Dave Chinner9e5987a2013-02-25 12:31:26 +1100716 args.type = XFS_ALLOCTYPE_START_BNO;
717 args.fsbno = *firstblock;
718 } else {
719 args.type = XFS_ALLOCTYPE_NEAR_BNO;
720 args.fsbno = *firstblock;
721 }
722 args.minlen = args.maxlen = args.prod = 1;
723 args.wasdel = wasdel;
724 *logflagsp = 0;
725 if ((error = xfs_alloc_vextent(&args))) {
726 xfs_iroot_realloc(ip, -1, whichfork);
727 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
728 return error;
729 }
Darrick J. Wong90e20562016-10-03 09:11:45 -0700730
Christoph Hellwig2fcc3192017-03-08 10:38:53 -0800731 if (WARN_ON_ONCE(args.fsbno == NULLFSBLOCK)) {
732 xfs_iroot_realloc(ip, -1, whichfork);
733 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
734 return -ENOSPC;
735 }
Dave Chinner9e5987a2013-02-25 12:31:26 +1100736 /*
737 * Allocation can't fail, the space was reserved.
738 */
Dave Chinner9e5987a2013-02-25 12:31:26 +1100739 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -0800740 args.agno >= XFS_FSB_TO_AGNO(mp, *firstblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100741 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
742 cur->bc_private.b.allocated++;
743 ip->i_d.di_nblocks++;
744 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
745 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
746 /*
747 * Fill in the child block.
748 */
749 abp->b_ops = &xfs_bmbt_buf_ops;
750 ablock = XFS_BUF_TO_BLOCK(abp);
Eric Sandeenb6f41e42017-01-27 23:16:39 -0800751 xfs_btree_init_block_int(mp, ablock, abp->b_bn,
752 XFS_BTNUM_BMAP, 0, 0, ip->i_ino,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500753 XFS_BTREE_LONG_PTRS);
754
Dave Chinner9e5987a2013-02-25 12:31:26 +1100755 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
Eric Sandeen5d829302016-11-08 12:59:42 +1100756 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100757 for (cnt = i = 0; i < nextents; i++) {
758 ep = xfs_iext_get_ext(ifp, i);
759 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
760 arp->l0 = cpu_to_be64(ep->l0);
761 arp->l1 = cpu_to_be64(ep->l1);
762 arp++; cnt++;
763 }
764 }
765 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
766 xfs_btree_set_numrecs(ablock, cnt);
767
768 /*
769 * Fill in the root key and pointer.
770 */
771 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
772 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
773 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
774 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
775 be16_to_cpu(block->bb_level)));
776 *pp = cpu_to_be64(args.fsbno);
777
778 /*
779 * Do all this logging at the end so that
780 * the root is at the right level.
781 */
782 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
783 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
784 ASSERT(*curp == NULL);
785 *curp = cur;
786 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
787 return 0;
788}
789
790/*
791 * Convert a local file to an extents file.
792 * This code is out of bounds for data forks of regular files,
793 * since the file data needs to get logged so things will stay consistent.
794 * (The bmap-level manipulations are ok, though).
795 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000796void
797xfs_bmap_local_to_extents_empty(
798 struct xfs_inode *ip,
799 int whichfork)
800{
801 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
802
Darrick J. Wong60b49842016-10-03 09:11:34 -0700803 ASSERT(whichfork != XFS_COW_FORK);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000804 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
805 ASSERT(ifp->if_bytes == 0);
806 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
807
Eric Sandeen6a9edd32014-04-14 18:59:26 +1000808 xfs_bmap_forkoff_reset(ip, whichfork);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000809 ifp->if_flags &= ~XFS_IFINLINE;
810 ifp->if_flags |= XFS_IFEXTENTS;
811 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
812}
813
814
Dave Chinner9e5987a2013-02-25 12:31:26 +1100815STATIC int /* error */
816xfs_bmap_local_to_extents(
817 xfs_trans_t *tp, /* transaction pointer */
818 xfs_inode_t *ip, /* incore inode pointer */
819 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
820 xfs_extlen_t total, /* total blocks needed by transaction */
821 int *logflagsp, /* inode logging flags */
822 int whichfork,
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500823 void (*init_fn)(struct xfs_trans *tp,
824 struct xfs_buf *bp,
Dave Chinner9e5987a2013-02-25 12:31:26 +1100825 struct xfs_inode *ip,
826 struct xfs_ifork *ifp))
827{
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000828 int error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100829 int flags; /* logging flags returned */
830 xfs_ifork_t *ifp; /* inode fork pointer */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000831 xfs_alloc_arg_t args; /* allocation arguments */
832 xfs_buf_t *bp; /* buffer for extent block */
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700833 struct xfs_bmbt_irec rec;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100834
835 /*
836 * We don't want to deal with the case of keeping inode data inline yet.
837 * So sending the data fork of a regular inode is invalid.
838 */
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100839 ASSERT(!(S_ISREG(VFS_I(ip)->i_mode) && whichfork == XFS_DATA_FORK));
Dave Chinner9e5987a2013-02-25 12:31:26 +1100840 ifp = XFS_IFORK_PTR(ip, whichfork);
841 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000842
843 if (!ifp->if_bytes) {
844 xfs_bmap_local_to_extents_empty(ip, whichfork);
845 flags = XFS_ILOG_CORE;
846 goto done;
847 }
848
Dave Chinner9e5987a2013-02-25 12:31:26 +1100849 flags = 0;
850 error = 0;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000851 ASSERT((ifp->if_flags & (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) ==
852 XFS_IFINLINE);
853 memset(&args, 0, sizeof(args));
854 args.tp = tp;
855 args.mp = ip->i_mount;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000856 xfs_rmap_ino_owner(&args.oinfo, ip->i_ino, whichfork, 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000857 args.firstblock = *firstblock;
858 /*
859 * Allocate a block. We know we need only one, since the
860 * file currently fits in an inode.
861 */
862 if (*firstblock == NULLFSBLOCK) {
863 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
864 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100865 } else {
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000866 args.fsbno = *firstblock;
867 args.type = XFS_ALLOCTYPE_NEAR_BNO;
Dave Chinner9e5987a2013-02-25 12:31:26 +1100868 }
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000869 args.total = total;
870 args.minlen = args.maxlen = args.prod = 1;
871 error = xfs_alloc_vextent(&args);
872 if (error)
873 goto done;
874
875 /* Can't fail, the space was reserved. */
876 ASSERT(args.fsbno != NULLFSBLOCK);
877 ASSERT(args.len == 1);
878 *firstblock = args.fsbno;
879 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
880
Dave Chinnerfe22d552015-01-22 09:30:06 +1100881 /*
Brian Fosterb7cdc662015-10-12 15:40:24 +1100882 * Initialize the block, copy the data and log the remote buffer.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100883 *
Brian Fosterb7cdc662015-10-12 15:40:24 +1100884 * The callout is responsible for logging because the remote format
885 * might differ from the local format and thus we don't know how much to
886 * log here. Note that init_fn must also set the buffer log item type
887 * correctly.
Dave Chinnerfe22d552015-01-22 09:30:06 +1100888 */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000889 init_fn(tp, bp, ip, ifp);
890
Brian Fosterb7cdc662015-10-12 15:40:24 +1100891 /* account for the change in fork size */
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000892 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
893 xfs_bmap_local_to_extents_empty(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +1100894 flags |= XFS_ILOG_CORE;
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000895
Christoph Hellwig50bb44c2017-08-29 15:44:11 -0700896 rec.br_startoff = 0;
897 rec.br_startblock = args.fsbno;
898 rec.br_blockcount = 1;
899 rec.br_state = XFS_EXT_NORM;
900 xfs_iext_insert(ip, 0, 1, &rec, 0);
901
Dave Chinnerf3508bc2013-07-10 07:04:00 +1000902 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
903 ip->i_d.di_nblocks = 1;
904 xfs_trans_mod_dquot_byino(tp, ip,
905 XFS_TRANS_DQ_BCOUNT, 1L);
906 flags |= xfs_ilog_fext(whichfork);
907
Dave Chinner9e5987a2013-02-25 12:31:26 +1100908done:
909 *logflagsp = flags;
910 return error;
911}
912
913/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 * Called from xfs_bmap_add_attrfork to handle btree format files.
915 */
916STATIC int /* error */
917xfs_bmap_add_attrfork_btree(
918 xfs_trans_t *tp, /* transaction pointer */
919 xfs_inode_t *ip, /* incore inode pointer */
920 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000921 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 int *flags) /* inode logging flags */
923{
924 xfs_btree_cur_t *cur; /* btree cursor */
925 int error; /* error return value */
926 xfs_mount_t *mp; /* file system mount struct */
927 int stat; /* newroot status */
928
929 mp = ip->i_mount;
930 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
931 *flags |= XFS_ILOG_DBROOT;
932 else {
Christoph Hellwig561f7d12008-10-30 16:53:59 +1100933 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000934 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 cur->bc_private.b.firstblock = *firstblock;
Christoph Hellwigb5cfbc22017-10-17 14:16:27 -0700936 error = xfs_bmbt_lookup_first(cur, &stat);
937 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 goto error0;
Lachlan McIlroy6bd8fc82008-06-23 13:25:46 +1000939 /* must be at least one entry */
Eric Sandeenc29aad42015-02-23 22:39:08 +1100940 XFS_WANT_CORRUPTED_GOTO(mp, stat == 1, error0);
Christoph Hellwigea77b0a2008-10-30 16:57:28 +1100941 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 goto error0;
943 if (stat == 0) {
944 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
Dave Chinner24513372014-06-25 14:58:08 +1000945 return -ENOSPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947 *firstblock = cur->bc_private.b.firstblock;
948 cur->bc_private.b.allocated = 0;
949 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
950 }
951 return 0;
952error0:
953 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
954 return error;
955}
956
957/*
958 * Called from xfs_bmap_add_attrfork to handle extents format files.
959 */
960STATIC int /* error */
961xfs_bmap_add_attrfork_extents(
962 xfs_trans_t *tp, /* transaction pointer */
963 xfs_inode_t *ip, /* incore inode pointer */
964 xfs_fsblock_t *firstblock, /* first block allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000965 struct xfs_defer_ops *dfops, /* blocks to free at commit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 int *flags) /* inode logging flags */
967{
968 xfs_btree_cur_t *cur; /* bmap btree cursor */
969 int error; /* error return value */
970
971 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
972 return 0;
973 cur = NULL;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +1000974 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops, &cur, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 flags, XFS_DATA_FORK);
976 if (cur) {
977 cur->bc_private.b.allocated = 0;
978 xfs_btree_del_cursor(cur,
979 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
980 }
981 return error;
982}
983
984/*
Dave Chinner1e823792013-02-11 15:58:13 +1100985 * Called from xfs_bmap_add_attrfork to handle local format files. Each
986 * different data fork content type needs a different callout to do the
987 * conversion. Some are basic and only require special block initialisation
988 * callouts for the data formating, others (directories) are so specialised they
989 * handle everything themselves.
990 *
991 * XXX (dgc): investigate whether directory conversion can use the generic
992 * formatting callout. It should be possible - it's just a very complex
Christoph Hellwigee1a47a2013-04-21 14:53:46 -0500993 * formatter.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 */
995STATIC int /* error */
996xfs_bmap_add_attrfork_local(
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_da_args_t dargs; /* args for dir/attr code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
1006 return 0;
Dave Chinner1e823792013-02-11 15:58:13 +11001007
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001008 if (S_ISDIR(VFS_I(ip)->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 memset(&dargs, 0, sizeof(dargs));
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001010 dargs.geo = ip->i_mount->m_dir_geo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 dargs.dp = ip;
1012 dargs.firstblock = firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001013 dargs.dfops = dfops;
Dave Chinnerd6cf1302014-06-06 15:14:11 +10001014 dargs.total = dargs.geo->fsbcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 dargs.whichfork = XFS_DATA_FORK;
1016 dargs.trans = tp;
Dave Chinner1e823792013-02-11 15:58:13 +11001017 return xfs_dir2_sf_to_block(&dargs);
1018 }
1019
Dave Chinnerc19b3b052016-02-09 16:54:58 +11001020 if (S_ISLNK(VFS_I(ip)->i_mode))
Dave Chinner1e823792013-02-11 15:58:13 +11001021 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
1022 flags, XFS_DATA_FORK,
1023 xfs_symlink_local_to_remote);
1024
Dave Chinnerf3508bc2013-07-10 07:04:00 +10001025 /* should only be called for types that support local format data */
1026 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001027 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
1030/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001031 * Convert inode from non-attributed to attributed.
1032 * Must not be in a transaction, ip must not be locked.
1033 */
1034int /* error code */
1035xfs_bmap_add_attrfork(
1036 xfs_inode_t *ip, /* incore inode pointer */
1037 int size, /* space new attribute needs */
1038 int rsvd) /* xact may use reserved blks */
1039{
1040 xfs_fsblock_t firstblock; /* 1st block/ag allocated */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001041 struct xfs_defer_ops dfops; /* freed extent records */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001042 xfs_mount_t *mp; /* mount structure */
1043 xfs_trans_t *tp; /* transaction pointer */
1044 int blks; /* space reservation */
1045 int version = 1; /* superblock attr version */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001046 int logflags; /* logging flags */
1047 int error; /* error return value */
1048
1049 ASSERT(XFS_IFORK_Q(ip) == 0);
1050
1051 mp = ip->i_mount;
1052 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
Christoph Hellwig253f4912016-04-06 09:19:55 +10001053
Dave Chinner9e5987a2013-02-25 12:31:26 +11001054 blks = XFS_ADDAFORK_SPACE_RES(mp);
Christoph Hellwig253f4912016-04-06 09:19:55 +10001055
1056 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_addafork, blks, 0,
1057 rsvd ? XFS_TRANS_RESERVE : 0, &tp);
1058 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001059 return error;
Christoph Hellwig253f4912016-04-06 09:19:55 +10001060
Dave Chinner9e5987a2013-02-25 12:31:26 +11001061 xfs_ilock(ip, XFS_ILOCK_EXCL);
1062 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
1063 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
1064 XFS_QMOPT_RES_REGBLKS);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001065 if (error)
1066 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001067 if (XFS_IFORK_Q(ip))
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001068 goto trans_cancel;
Darrick J. Wong0f352f82016-12-05 12:38:11 +11001069 if (ip->i_d.di_anextents != 0) {
1070 error = -EFSCORRUPTED;
1071 goto trans_cancel;
1072 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001073 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
1074 /*
1075 * For inodes coming from pre-6.2 filesystems.
1076 */
1077 ASSERT(ip->i_d.di_aformat == 0);
1078 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
1079 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001080
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001081 xfs_trans_ijoin(tp, ip, 0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001082 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1083
1084 switch (ip->i_d.di_format) {
1085 case XFS_DINODE_FMT_DEV:
1086 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
1087 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001088 case XFS_DINODE_FMT_LOCAL:
1089 case XFS_DINODE_FMT_EXTENTS:
1090 case XFS_DINODE_FMT_BTREE:
1091 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
1092 if (!ip->i_d.di_forkoff)
1093 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
1094 else if (mp->m_flags & XFS_MOUNT_ATTR2)
1095 version = 2;
1096 break;
1097 default:
1098 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10001099 error = -EINVAL;
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001100 goto trans_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001101 }
1102
1103 ASSERT(ip->i_afp == NULL);
1104 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
1105 ip->i_afp->if_flags = XFS_IFEXTENTS;
1106 logflags = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001107 xfs_defer_init(&dfops, &firstblock);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001108 switch (ip->i_d.di_format) {
1109 case XFS_DINODE_FMT_LOCAL:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001110 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001111 &logflags);
1112 break;
1113 case XFS_DINODE_FMT_EXTENTS:
1114 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001115 &dfops, &logflags);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001116 break;
1117 case XFS_DINODE_FMT_BTREE:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001118 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &dfops,
Dave Chinner9e5987a2013-02-25 12:31:26 +11001119 &logflags);
1120 break;
1121 default:
1122 error = 0;
1123 break;
1124 }
1125 if (logflags)
1126 xfs_trans_log_inode(tp, ip, logflags);
1127 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001128 goto bmap_cancel;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001129 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
1130 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
Dave Chinner61e63ec2015-01-22 09:10:31 +11001131 bool log_sb = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001132
1133 spin_lock(&mp->m_sb_lock);
1134 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
1135 xfs_sb_version_addattr(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001136 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001137 }
1138 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
1139 xfs_sb_version_addattr2(&mp->m_sb);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001140 log_sb = true;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001141 }
Dave Chinner4d11a402015-01-22 09:10:26 +11001142 spin_unlock(&mp->m_sb_lock);
Dave Chinner61e63ec2015-01-22 09:10:31 +11001143 if (log_sb)
1144 xfs_log_sb(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001145 }
1146
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07001147 error = xfs_defer_finish(&tp, &dfops);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001148 if (error)
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001149 goto bmap_cancel;
Christoph Hellwig70393312015-06-04 13:48:08 +10001150 error = xfs_trans_commit(tp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001151 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001152 return error;
1153
1154bmap_cancel:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001155 xfs_defer_cancel(&dfops);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001156trans_cancel:
Christoph Hellwig4906e212015-06-04 13:47:56 +10001157 xfs_trans_cancel(tp);
Mark Tinguely9e3908e2013-11-07 15:43:28 -06001158 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001159 return error;
1160}
1161
1162/*
1163 * Internal and external extent tree search functions.
1164 */
1165
1166/*
1167 * Read in the extents to if_extents.
1168 * All inode fields are set up by caller, we just traverse the btree
1169 * and copy the records in. If the file system cannot contain unwritten
1170 * extents, the records are checked for no "state" flags.
1171 */
1172int /* error */
1173xfs_bmap_read_extents(
1174 xfs_trans_t *tp, /* transaction pointer */
1175 xfs_inode_t *ip, /* incore inode */
1176 int whichfork) /* data or attr fork */
1177{
1178 struct xfs_btree_block *block; /* current btree block */
1179 xfs_fsblock_t bno; /* block # of "block" */
1180 xfs_buf_t *bp; /* buffer for "block" */
1181 int error; /* error return value */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001182 xfs_extnum_t i, j; /* index into the extents list */
1183 xfs_ifork_t *ifp; /* fork structure */
1184 int level; /* btree level, for checking */
1185 xfs_mount_t *mp; /* file system mount structure */
1186 __be64 *pp; /* pointer to block address */
1187 /* REFERENCED */
1188 xfs_extnum_t room; /* number of entries there's room for */
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001189 int state = xfs_bmap_fork_to_state(whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001190
Dave Chinner9e5987a2013-02-25 12:31:26 +11001191 mp = ip->i_mount;
1192 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001193 block = ifp->if_broot;
1194 /*
1195 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
1196 */
1197 level = be16_to_cpu(block->bb_level);
1198 ASSERT(level > 0);
1199 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
1200 bno = be64_to_cpu(*pp);
Darrick J. Wongd5a91ba2017-02-02 15:13:58 -08001201
Dave Chinner9e5987a2013-02-25 12:31:26 +11001202 /*
1203 * Go down the tree until leaf level is reached, following the first
1204 * pointer (leftmost) at each level.
1205 */
1206 while (level-- > 0) {
1207 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1208 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1209 if (error)
1210 return error;
1211 block = XFS_BUF_TO_BLOCK(bp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001212 if (level == 0)
1213 break;
1214 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
1215 bno = be64_to_cpu(*pp);
Eric Sandeenc29aad42015-02-23 22:39:08 +11001216 XFS_WANT_CORRUPTED_GOTO(mp,
1217 XFS_FSB_SANITY_CHECK(mp, bno), error0);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001218 xfs_trans_brelse(tp, bp);
1219 }
1220 /*
1221 * Here with bp and block set to the leftmost leaf node in the tree.
1222 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001223 room = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001224 i = 0;
1225 /*
1226 * Loop over all leaf nodes. Copy information to the extent records.
1227 */
1228 for (;;) {
1229 xfs_bmbt_rec_t *frp;
1230 xfs_fsblock_t nextbno;
1231 xfs_extnum_t num_recs;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001232
1233 num_recs = xfs_btree_get_numrecs(block);
1234 if (unlikely(i + num_recs > room)) {
1235 ASSERT(i + num_recs <= room);
1236 xfs_warn(ip->i_mount,
1237 "corrupt dinode %Lu, (btree extents).",
1238 (unsigned long long) ip->i_ino);
1239 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
1240 XFS_ERRLEVEL_LOW, ip->i_mount, block);
1241 goto error0;
1242 }
Dave Chinner9e5987a2013-02-25 12:31:26 +11001243 /*
1244 * Read-ahead the next leaf block, if any.
1245 */
1246 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
1247 if (nextbno != NULLFSBLOCK)
1248 xfs_btree_reada_bufl(mp, nextbno, 1,
1249 &xfs_bmbt_buf_ops);
1250 /*
1251 * Copy records into the extent records.
1252 */
1253 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001254 for (j = 0; j < num_recs; j++, i++, frp++) {
1255 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
1256 trp->l0 = be64_to_cpu(frp->l0);
1257 trp->l1 = be64_to_cpu(frp->l1);
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001258 if (!xfs_bmbt_validate_extent(mp, whichfork, trp)) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001259 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
Christoph Hellwig0c1d9e42017-04-20 09:42:48 -07001260 XFS_ERRLEVEL_LOW, mp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001261 goto error0;
1262 }
Christoph Hellwige8e0e172017-10-19 11:06:29 -07001263 trace_xfs_read_extent(ip, i, state, _THIS_IP_);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001264 }
1265 xfs_trans_brelse(tp, bp);
1266 bno = nextbno;
1267 /*
1268 * If we've reached the end, stop.
1269 */
1270 if (bno == NULLFSBLOCK)
1271 break;
1272 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
1273 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
1274 if (error)
1275 return error;
1276 block = XFS_BUF_TO_BLOCK(bp);
1277 }
Darrick J. Wong356a3222016-12-05 12:36:56 +11001278 if (i != XFS_IFORK_NEXTENTS(ip, whichfork))
1279 return -EFSCORRUPTED;
Eric Sandeen5d829302016-11-08 12:59:42 +11001280 ASSERT(i == xfs_iext_count(ifp));
Dave Chinner9e5987a2013-02-25 12:31:26 +11001281 return 0;
1282error0:
1283 xfs_trans_brelse(tp, bp);
Dave Chinner24513372014-06-25 14:58:08 +10001284 return -EFSCORRUPTED;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001285}
1286
Dave Chinner9e5987a2013-02-25 12:31:26 +11001287/*
1288 * Returns the file-relative block number of the first unused block(s)
1289 * in the file with at least "len" logically contiguous blocks free.
1290 * This is the lowest-address hole if the file has holes, else the first block
1291 * past the end of file.
1292 * Return 0 if the file is currently local (in-inode).
1293 */
1294int /* error */
1295xfs_bmap_first_unused(
1296 xfs_trans_t *tp, /* transaction pointer */
1297 xfs_inode_t *ip, /* incore inode */
1298 xfs_extlen_t len, /* size of hole to find */
1299 xfs_fileoff_t *first_unused, /* unused block */
1300 int whichfork) /* data or attr fork */
1301{
1302 int error; /* error return value */
1303 int idx; /* extent record index */
1304 xfs_ifork_t *ifp; /* inode fork pointer */
1305 xfs_fileoff_t lastaddr; /* last block number seen */
1306 xfs_fileoff_t lowest; /* lowest useful block */
1307 xfs_fileoff_t max; /* starting useful block */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001308 xfs_extnum_t nextents; /* number of extent entries */
1309
1310 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1311 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1312 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
1313 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1314 *first_unused = 0;
1315 return 0;
1316 }
1317 ifp = XFS_IFORK_PTR(ip, whichfork);
1318 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
1319 (error = xfs_iread_extents(tp, ip, whichfork)))
1320 return error;
1321 lowest = *first_unused;
Eric Sandeen5d829302016-11-08 12:59:42 +11001322 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001323 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001324 struct xfs_bmbt_irec got;
1325
1326 xfs_iext_get_extent(ifp, idx, &got);
1327
Dave Chinner9e5987a2013-02-25 12:31:26 +11001328 /*
1329 * See if the hole before this extent will work.
1330 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001331 if (got.br_startoff >= lowest + len &&
1332 got.br_startoff - max >= len) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001333 *first_unused = max;
1334 return 0;
1335 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001336 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001337 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1338 }
1339 *first_unused = max;
1340 return 0;
1341}
1342
1343/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001344 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001345 * last_block (input value) in the file.
1346 * This is not based on i_size, it is based on the extent records.
1347 * Returns 0 for local files, as they do not have extent records.
1348 */
1349int /* error */
1350xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001351 struct xfs_trans *tp, /* transaction pointer */
1352 struct xfs_inode *ip, /* incore inode */
1353 xfs_fileoff_t *last_block, /* last block */
1354 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001355{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001356 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1357 struct xfs_bmbt_irec got;
1358 xfs_extnum_t idx;
1359 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001360
Christoph Hellwig86685f72016-11-24 11:39:38 +11001361 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1362 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001363 *last_block = 0;
1364 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001365 case XFS_DINODE_FMT_BTREE:
1366 case XFS_DINODE_FMT_EXTENTS:
1367 break;
1368 default:
1369 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001370 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001371
1372 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1373 error = xfs_iread_extents(tp, ip, whichfork);
1374 if (error)
1375 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001376 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001377
1378 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1379 if (got.br_startoff <= *last_block - 1)
1380 return 0;
1381 }
1382
1383 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1384 *last_block = got.br_startoff + got.br_blockcount;
1385 return 0;
1386 }
1387
1388 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001389 return 0;
1390}
1391
Dave Chinner68988112013-08-12 20:49:42 +10001392int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001393xfs_bmap_last_extent(
1394 struct xfs_trans *tp,
1395 struct xfs_inode *ip,
1396 int whichfork,
1397 struct xfs_bmbt_irec *rec,
1398 int *is_empty)
1399{
1400 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1401 int error;
1402 int nextents;
1403
1404 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1405 error = xfs_iread_extents(tp, ip, whichfork);
1406 if (error)
1407 return error;
1408 }
1409
Eric Sandeen5d829302016-11-08 12:59:42 +11001410 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001411 if (nextents == 0) {
1412 *is_empty = 1;
1413 return 0;
1414 }
1415
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001416 xfs_iext_get_extent(ifp, nextents - 1, rec);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001417 *is_empty = 0;
1418 return 0;
1419}
1420
1421/*
1422 * Check the last inode extent to determine whether this allocation will result
1423 * in blocks being allocated at the end of the file. When we allocate new data
1424 * blocks at the end of the file which do not start at the previous data block,
1425 * we will try to align the new blocks at stripe unit boundaries.
1426 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001427 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001428 * at, or past the EOF.
1429 */
1430STATIC int
1431xfs_bmap_isaeof(
1432 struct xfs_bmalloca *bma,
1433 int whichfork)
1434{
1435 struct xfs_bmbt_irec rec;
1436 int is_empty;
1437 int error;
1438
Thomas Meyer749f24f2017-10-09 11:38:54 -07001439 bma->aeof = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001440 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1441 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001442 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001443 return error;
1444
Dave Chinner6e708bc2013-11-22 10:41:16 +11001445 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001446 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001447 return 0;
1448 }
1449
Dave Chinner9e5987a2013-02-25 12:31:26 +11001450 /*
1451 * Check if we are allocation or past the last extent, or at least into
1452 * the last delayed allocated extent.
1453 */
1454 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1455 (bma->offset >= rec.br_startoff &&
1456 isnullstartblock(rec.br_startblock));
1457 return 0;
1458}
1459
1460/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001461 * Returns the file-relative block number of the first block past eof in
1462 * the file. This is not based on i_size, it is based on the extent records.
1463 * Returns 0 for local files, as they do not have extent records.
1464 */
1465int
1466xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001467 struct xfs_inode *ip,
1468 xfs_fileoff_t *last_block,
1469 int whichfork)
1470{
1471 struct xfs_bmbt_irec rec;
1472 int is_empty;
1473 int error;
1474
1475 *last_block = 0;
1476
1477 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1478 return 0;
1479
1480 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1481 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001482 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001483
1484 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1485 if (error || is_empty)
1486 return error;
1487
1488 *last_block = rec.br_startoff + rec.br_blockcount;
1489 return 0;
1490}
1491
1492/*
1493 * Returns whether the selected fork of the inode has exactly one
1494 * block or not. For the data fork we check this matches di_size,
1495 * implying the file's range is 0..bsize-1.
1496 */
1497int /* 1=>1 block, 0=>otherwise */
1498xfs_bmap_one_block(
1499 xfs_inode_t *ip, /* incore inode */
1500 int whichfork) /* data or attr fork */
1501{
Dave Chinner9e5987a2013-02-25 12:31:26 +11001502 xfs_ifork_t *ifp; /* inode fork pointer */
1503 int rval; /* return value */
1504 xfs_bmbt_irec_t s; /* internal version of extent */
1505
1506#ifndef DEBUG
1507 if (whichfork == XFS_DATA_FORK)
1508 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1509#endif /* !DEBUG */
1510 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1511 return 0;
1512 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1513 return 0;
1514 ifp = XFS_IFORK_PTR(ip, whichfork);
1515 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001516 xfs_iext_get_extent(ifp, 0, &s);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001517 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1518 if (rval && whichfork == XFS_DATA_FORK)
1519 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1520 return rval;
1521}
1522
1523/*
1524 * Extent tree manipulation functions used during allocation.
1525 */
1526
1527/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001528 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 */
1530STATIC int /* error */
1531xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001532 struct xfs_bmalloca *bma,
1533 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001535 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001538 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 xfs_fileoff_t new_endoff; /* end offset of new entry */
1540 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1541 /* left is 0, right is 1, prev is 2 */
1542 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001543 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001544 xfs_filblks_t da_new; /* new count del alloc blocks used */
1545 xfs_filblks_t da_old; /* old count del alloc blocks used */
1546 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001548 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001549 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001550 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001552 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001553 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001554 ASSERT(whichfork != XFS_ATTR_FORK);
1555 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1556 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001557
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001558 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001559 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001560 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001561 ASSERT(!bma->cur ||
1562 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001563
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001564 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001565
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566#define LEFT r[0]
1567#define RIGHT r[1]
1568#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 /*
1571 * Set up a bunch of variables to make the tests simpler.
1572 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001573 xfs_iext_get_extent(ifp, bma->idx, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001575 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 ASSERT(PREV.br_startoff <= new->br_startoff);
1577 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001578
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001579 da_old = startblockval(PREV.br_startblock);
1580 da_new = 0;
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 /*
1583 * Set flags determining what part of the previous delayed allocation
1584 * extent is being replaced by a real allocation.
1585 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001586 if (PREV.br_startoff == new->br_startoff)
1587 state |= BMAP_LEFT_FILLING;
1588 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1589 state |= BMAP_RIGHT_FILLING;
1590
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 /*
1592 * Check and set flags if this segment has a left neighbor.
1593 * Don't set contiguous if the combined extent would be too large.
1594 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001595 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001596 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001597 xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001598
1599 if (isnullstartblock(LEFT.br_startblock))
1600 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001602
1603 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1604 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1605 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1606 LEFT.br_state == new->br_state &&
1607 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1608 state |= BMAP_LEFT_CONTIG;
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /*
1611 * Check and set flags if this segment has a right neighbor.
1612 * Don't set contiguous if the combined extent would be too large.
1613 * Also check for all-three-contiguous being too large.
1614 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001615 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001616 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001617 xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001618
1619 if (isnullstartblock(RIGHT.br_startblock))
1620 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001622
1623 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1624 new_endoff == RIGHT.br_startoff &&
1625 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1626 new->br_state == RIGHT.br_state &&
1627 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1628 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1629 BMAP_RIGHT_FILLING)) !=
1630 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1631 BMAP_RIGHT_FILLING) ||
1632 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1633 <= MAXEXTLEN))
1634 state |= BMAP_RIGHT_CONTIG;
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 error = 0;
1637 /*
1638 * Switch out based on the FILLING and CONTIG state bits.
1639 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001640 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1641 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1642 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1643 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /*
1645 * Filling in all of a previously delayed allocation extent.
1646 * The left and right neighbors are both contiguous with new.
1647 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001648 bma->idx--;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001649 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001650 xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001651
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001652 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001653 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001654 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1656 else {
1657 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001658 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001659 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001661 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001662 error = xfs_btree_delete(bma->cur, &i);
1663 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001665 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001666 error = xfs_btree_decrement(bma->cur, 0, &i);
1667 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001669 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001670 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001671 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 goto done;
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 break;
1675
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001676 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 /*
1678 * Filling in all of a previously delayed allocation extent.
1679 * The left neighbor is contiguous, the right is not.
1680 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001681 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001682
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001683 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001684 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001685 xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001686
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001687 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1688 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 rval = XFS_ILOG_DEXT;
1690 else {
1691 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001692 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001693 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001695 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001696 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001697 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 goto done;
1699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 break;
1701
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001702 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 /*
1704 * Filling in all of a previously delayed allocation extent.
1705 * The right neighbor is contiguous, the left is not.
1706 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001707 PREV.br_startblock = new->br_startblock;
1708 PREV.br_blockcount += RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001709 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001710
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001711 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1712 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 rval = XFS_ILOG_DEXT;
1714 else {
1715 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001716 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001717 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001719 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001720 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001721 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 goto done;
1723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 break;
1725
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001726 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 /*
1728 * Filling in all of a previously delayed allocation extent.
1729 * Neither the left nor right neighbors are contiguous with
1730 * the new one.
1731 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001732 PREV.br_startblock = new->br_startblock;
1733 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001734 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001735
Darrick J. Wong60b49842016-10-03 09:11:34 -07001736 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001737 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1739 else {
1740 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001741 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001742 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001744 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001745 error = xfs_btree_insert(bma->cur, &i);
1746 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001748 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 break;
1751
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001752 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 /*
1754 * Filling in the first part of a previous delayed allocation.
1755 * The left neighbor is contiguous.
1756 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001757 old = LEFT;
1758 temp = PREV.br_blockcount - new->br_blockcount;
1759 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1760 startblockval(PREV.br_startblock));
1761
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001762 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001763 xfs_iext_update_extent(bma->ip, state, bma->idx - 1, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001764
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001765 PREV.br_blockcount = temp = PREV.br_blockcount - new->br_blockcount;
1766 PREV.br_startoff += new->br_blockcount;
1767 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001768 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001769
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001770 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 rval = XFS_ILOG_DEXT;
1772 else {
1773 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001774 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001775 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001777 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001778 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001779 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 goto done;
1781 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001782
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001783 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 break;
1785
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001786 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 /*
1788 * Filling in the first part of a previous delayed allocation.
1789 * The left neighbor is not contiguous.
1790 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001791 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001792 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001793 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1795 else {
1796 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001797 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001798 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 error = xfs_btree_insert(bma->cur, &i);
1802 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001804 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001806
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001807 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001808 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001809 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001810 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 rval |= tmp_rval;
1812 if (error)
1813 goto done;
1814 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001815
1816 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001817 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001818 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001819 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001820
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001821 PREV.br_startoff = new_endoff;
1822 PREV.br_blockcount = temp;
1823 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001824 xfs_iext_update_extent(bma->ip, state, bma->idx + 1, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 break;
1826
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001827 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /*
1829 * Filling in the last part of a previous delayed allocation.
1830 * The right neighbor is contiguous with the new allocation.
1831 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001832 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001833 RIGHT.br_startoff = new->br_startoff;
1834 RIGHT.br_startblock = new->br_startblock;
1835 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001836 xfs_iext_update_extent(bma->ip, state, bma->idx + 1, &RIGHT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001837
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001838 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 rval = XFS_ILOG_DEXT;
1840 else {
1841 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001842 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001843 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001845 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001846 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001847 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 goto done;
1849 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001850
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001851 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001852 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001853 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001854
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001855 PREV.br_blockcount = temp;
1856 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001857 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001858
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001859 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 break;
1861
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001862 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 /*
1864 * Filling in the last part of a previous delayed allocation.
1865 * The right neighbor is not contiguous.
1866 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001867 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001868 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001869 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1871 else {
1872 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001873 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001874 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001876 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001877 error = xfs_btree_insert(bma->cur, &i);
1878 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001880 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001882
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001883 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001884 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001885 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001886 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 rval |= tmp_rval;
1888 if (error)
1889 goto done;
1890 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001891
1892 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001893 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001894 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001895 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001896
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001897 PREV.br_startblock = nullstartblock(da_new);
1898 PREV.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001899 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001900
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001901 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 break;
1903
1904 case 0:
1905 /*
1906 * Filling in the middle part of a previous delayed allocation.
1907 * Contiguity is impossible here.
1908 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001909 *
1910 * We start with a delayed allocation:
1911 *
1912 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1913 * PREV @ idx
1914 *
1915 * and we are allocating:
1916 * +rrrrrrrrrrrrrrrrr+
1917 * new
1918 *
1919 * and we set it up for insertion as:
1920 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1921 * new
1922 * PREV @ idx LEFT RIGHT
1923 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001925 old = PREV;
1926
1927 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001928 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001929
1930 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001931 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001932 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001933 RIGHT.br_blockcount =
1934 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1935 RIGHT.br_startblock =
1936 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1937 RIGHT.br_blockcount));
1938
1939 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001940 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1941 PREV.br_startblock =
1942 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1943 PREV.br_blockcount));
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001944 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001945
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001946 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001947 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001948 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001949
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001950 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1952 else {
1953 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001954 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001955 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001957 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001958 error = xfs_btree_insert(bma->cur, &i);
1959 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001961 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001963
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001964 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001965 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001966 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001967 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 rval |= tmp_rval;
1969 if (error)
1970 goto done;
1971 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001972
1973 da_new = startblockval(PREV.br_startblock) +
1974 startblockval(RIGHT.br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001975 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 break;
1977
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001978 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1979 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1980 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1981 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1982 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1983 case BMAP_LEFT_CONTIG:
1984 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 /*
1986 * These cases are all impossible.
1987 */
1988 ASSERT(0);
1989 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001990
Darrick J. Wong9c194642016-08-03 12:16:05 +10001991 /* add reverse mapping */
1992 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
1993 if (error)
1994 goto done;
1995
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001996 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001997 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001998 int tmp_logflags; /* partial log flag return val */
1999
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002000 ASSERT(bma->cur == NULL);
2001 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002002 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002003 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002004 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002005 if (error)
2006 goto done;
2007 }
2008
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002009 if (bma->cur) {
2010 da_new += bma->cur->bc_private.b.allocated;
2011 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002012 }
2013
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002014 /* adjust for changes in reserved delayed indirect blocks */
2015 if (da_new != da_old) {
2016 ASSERT(state == 0 || da_new < da_old);
2017 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2018 false);
2019 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002020
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002021 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002023 if (whichfork != XFS_COW_FORK)
2024 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 return error;
2026#undef LEFT
2027#undef RIGHT
2028#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
2031/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002032 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 */
2034STATIC int /* error */
2035xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002036 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002038 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002039 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002041 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002042 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002043 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002044 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002049 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2052 /* left is 0, right is 1, prev is 2 */
2053 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002054 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002055 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002056 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002058 *logflagsp = 0;
2059
2060 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002061 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002062
2063 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002064 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002065 ASSERT(!isnullstartblock(new->br_startblock));
2066
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002067 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069#define LEFT r[0]
2070#define RIGHT r[1]
2071#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 /*
2074 * Set up a bunch of variables to make the tests simpler.
2075 */
2076 error = 0;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002077 xfs_iext_get_extent(ifp, *idx, &PREV);
2078 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 new_endoff = new->br_startoff + new->br_blockcount;
2080 ASSERT(PREV.br_startoff <= new->br_startoff);
2081 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /*
2084 * Set flags determining what part of the previous oldext allocation
2085 * extent is being replaced by a newext allocation.
2086 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002087 if (PREV.br_startoff == new->br_startoff)
2088 state |= BMAP_LEFT_FILLING;
2089 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2090 state |= BMAP_RIGHT_FILLING;
2091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 /*
2093 * Check and set flags if this segment has a left neighbor.
2094 * Don't set contiguous if the combined extent would be too large.
2095 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002096 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002097 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002098 xfs_iext_get_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002099
2100 if (isnullstartblock(LEFT.br_startblock))
2101 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002103
2104 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2105 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2106 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002107 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002108 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2109 state |= BMAP_LEFT_CONTIG;
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 /*
2112 * Check and set flags if this segment has a right neighbor.
2113 * Don't set contiguous if the combined extent would be too large.
2114 * Also check for all-three-contiguous being too large.
2115 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002116 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002117 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002118 xfs_iext_get_extent(ifp, *idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002119 if (isnullstartblock(RIGHT.br_startblock))
2120 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002122
2123 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2124 new_endoff == RIGHT.br_startoff &&
2125 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002126 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002127 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2128 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2129 BMAP_RIGHT_FILLING)) !=
2130 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2131 BMAP_RIGHT_FILLING) ||
2132 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2133 <= MAXEXTLEN))
2134 state |= BMAP_RIGHT_CONTIG;
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 /*
2137 * Switch out based on the FILLING and CONTIG state bits.
2138 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002139 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2140 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2141 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2142 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 /*
2144 * Setting all of a previous oldext extent to newext.
2145 * The left and right neighbors are both contiguous with new.
2146 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002147 --*idx;
2148
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002149 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002150 xfs_iext_update_extent(ip, state, *idx, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002151
Christoph Hellwigec90c552011-05-23 08:52:53 +00002152 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002153 XFS_IFORK_NEXT_SET(ip, whichfork,
2154 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 if (cur == NULL)
2156 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2157 else {
2158 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002159 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2160 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002162 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002163 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002165 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002166 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002168 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002169 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002171 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002172 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002174 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002175 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002176 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 goto done;
2178 }
2179 break;
2180
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002181 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 /*
2183 * Setting all of a previous oldext extent to newext.
2184 * The left neighbor is contiguous, the right is not.
2185 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002186 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002187
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002188 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002189 xfs_iext_update_extent(ip, state, *idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002190
2191 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002192 XFS_IFORK_NEXT_SET(ip, whichfork,
2193 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 if (cur == NULL)
2195 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2196 else {
2197 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002198 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2199 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002201 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002202 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002204 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002205 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002207 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002208 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002209 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 goto done;
2211 }
2212 break;
2213
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002214 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 /*
2216 * Setting all of a previous oldext extent to newext.
2217 * The right neighbor is contiguous, the left is not.
2218 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002219 PREV.br_blockcount += RIGHT.br_blockcount;
2220 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002221 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002222
Christoph Hellwigec90c552011-05-23 08:52:53 +00002223 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002224 XFS_IFORK_NEXT_SET(ip, whichfork,
2225 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 if (cur == NULL)
2227 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2228 else {
2229 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002230 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2231 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002233 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002234 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002236 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002237 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002239 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002240 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002241 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 goto done;
2243 }
2244 break;
2245
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002246 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 /*
2248 * Setting all of a previous oldext extent to newext.
2249 * Neither the left nor right neighbors are contiguous with
2250 * the new one.
2251 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002252 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002253 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002254
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (cur == NULL)
2256 rval = XFS_ILOG_DEXT;
2257 else {
2258 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002259 error = xfs_bmbt_lookup_eq(cur, new, &i);
2260 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002262 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002263 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002264 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 goto done;
2266 }
2267 break;
2268
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002269 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 /*
2271 * Setting the first part of a previous oldext extent to newext.
2272 * The left neighbor is contiguous.
2273 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002274 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002275 xfs_iext_update_extent(ip, state, *idx - 1, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002276
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002277 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002278 PREV.br_startoff += new->br_blockcount;
2279 PREV.br_startblock += new->br_blockcount;
2280 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002281 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002282
Christoph Hellwigec90c552011-05-23 08:52:53 +00002283 --*idx;
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 if (cur == NULL)
2286 rval = XFS_ILOG_DEXT;
2287 else {
2288 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002289 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002290 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002292 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002293 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002294 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002296 error = xfs_btree_decrement(cur, 0, &i);
2297 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002299 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002300 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 goto done;
2302 }
2303 break;
2304
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002305 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 /*
2307 * Setting the first part of a previous oldext extent to newext.
2308 * The left neighbor is not contiguous.
2309 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002310 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002311 PREV.br_startoff += new->br_blockcount;
2312 PREV.br_startblock += new->br_blockcount;
2313 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002314 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002315
Christoph Hellwigec90c552011-05-23 08:52:53 +00002316 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002317 XFS_IFORK_NEXT_SET(ip, whichfork,
2318 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 if (cur == NULL)
2320 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2321 else {
2322 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002323 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002324 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002326 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002327 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002328 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 goto done;
2330 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002331 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002333 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335 break;
2336
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002337 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /*
2339 * Setting the last part of a previous oldext extent to newext.
2340 * The right neighbor is contiguous with the new allocation.
2341 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002342 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002343 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002344 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002345
2346 ++*idx;
2347
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002348 RIGHT.br_startoff = new->br_startoff;
2349 RIGHT.br_startblock = new->br_startblock;
2350 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002351 xfs_iext_update_extent(ip, state, *idx, &RIGHT);
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;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002357 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002358 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002360 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002361 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002362 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002364 error = xfs_btree_increment(cur, 0, &i);
2365 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002367 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002368 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 goto done;
2370 }
2371 break;
2372
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002373 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 /*
2375 * Setting the last part of a previous oldext extent to newext.
2376 * The right neighbor is not contiguous.
2377 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002378 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002379 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002380 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002381
Christoph Hellwigec90c552011-05-23 08:52:53 +00002382 ++*idx;
2383 xfs_iext_insert(ip, *idx, 1, new, state);
2384
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002385 XFS_IFORK_NEXT_SET(ip, whichfork,
2386 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 if (cur == NULL)
2388 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2389 else {
2390 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002391 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002392 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002394 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002395 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002396 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002398 error = xfs_bmbt_lookup_eq(cur, new, &i);
2399 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002401 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002402 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002404 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 }
2406 break;
2407
2408 case 0:
2409 /*
2410 * Setting the middle part of a previous oldext extent to
2411 * newext. Contiguity is impossible here.
2412 * One extent becomes three extents.
2413 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002414 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002415 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002416 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 r[0] = *new;
2419 r[1].br_startoff = new_endoff;
2420 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002421 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002423 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002424
2425 ++*idx;
2426 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2427
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002428 XFS_IFORK_NEXT_SET(ip, whichfork,
2429 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (cur == NULL)
2431 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2432 else {
2433 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002434 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002435 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002437 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002439 error = xfs_bmbt_update(cur, &r[1]);
2440 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 goto done;
2442 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002444 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002446 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002447 /*
2448 * Reset the cursor to the position of the new extent
2449 * we are about to insert as we can't trust it after
2450 * the previous insert.
2451 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002452 error = xfs_bmbt_lookup_eq(cur, new, &i);
2453 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002455 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002457 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002459 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 }
2461 break;
2462
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002463 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2464 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2465 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2466 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2467 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2468 case BMAP_LEFT_CONTIG:
2469 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 /*
2471 * These cases are all impossible.
2472 */
2473 ASSERT(0);
2474 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002475
Darrick J. Wong9c194642016-08-03 12:16:05 +10002476 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002477 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002478 if (error)
2479 goto done;
2480
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002481 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002482 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002483 int tmp_logflags; /* partial log flag return val */
2484
2485 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002486 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002487 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002488 *logflagsp |= tmp_logflags;
2489 if (error)
2490 goto done;
2491 }
2492
2493 /* clear out the allocated field, done with it now in any case. */
2494 if (cur) {
2495 cur->bc_private.b.allocated = 0;
2496 *curp = cur;
2497 }
2498
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002499 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002501 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 return error;
2503#undef LEFT
2504#undef RIGHT
2505#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506}
2507
2508/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002509 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002511STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512xfs_bmap_add_extent_hole_delay(
2513 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002514 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002515 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002516 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002518 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2520 xfs_filblks_t newlen=0; /* new indirect size */
2521 xfs_filblks_t oldlen=0; /* old indirect size */
2522 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002523 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002524 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002526 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002527 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 /*
2530 * Check and set flags if this segment has a left neighbor
2531 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002532 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002533 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002534 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002535
2536 if (isnullstartblock(left.br_startblock))
2537 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /*
2541 * Check and set flags if the current (right) segment exists.
2542 * If it doesn't exist, we're converting the hole at end-of-file.
2543 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002544 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002545 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002546 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002547
2548 if (isnullstartblock(right.br_startblock))
2549 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 /*
2553 * Set contiguity flags on the left and right neighbors.
2554 * Don't let extents get too large, even if the pieces are contiguous.
2555 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002556 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2557 left.br_startoff + left.br_blockcount == new->br_startoff &&
2558 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2559 state |= BMAP_LEFT_CONTIG;
2560
2561 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2562 new->br_startoff + new->br_blockcount == right.br_startoff &&
2563 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2564 (!(state & BMAP_LEFT_CONTIG) ||
2565 (left.br_blockcount + new->br_blockcount +
2566 right.br_blockcount <= MAXEXTLEN)))
2567 state |= BMAP_RIGHT_CONTIG;
2568
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 /*
2570 * Switch out based on the contiguity flags.
2571 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002572 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2573 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 /*
2575 * New allocation is contiguous with delayed allocations
2576 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002577 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002579 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 temp = left.br_blockcount + new->br_blockcount +
2581 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002582
Eric Sandeen9d87c312009-01-14 23:22:07 -06002583 oldlen = startblockval(left.br_startblock) +
2584 startblockval(new->br_startblock) +
2585 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002586 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2587 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002588 left.br_startblock = nullstartblock(newlen);
2589 left.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002590 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002591
Christoph Hellwigec90c552011-05-23 08:52:53 +00002592 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593 break;
2594
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002595 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 /*
2597 * New allocation is contiguous with a delayed allocation
2598 * on the left.
2599 * Merge the new allocation with the left neighbor.
2600 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002601 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002603
Eric Sandeen9d87c312009-01-14 23:22:07 -06002604 oldlen = startblockval(left.br_startblock) +
2605 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002606 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2607 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002608 left.br_blockcount = temp;
2609 left.br_startblock = nullstartblock(newlen);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002610 xfs_iext_update_extent(ip, state, *idx, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 break;
2612
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002613 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 /*
2615 * New allocation is contiguous with a delayed allocation
2616 * on the right.
2617 * Merge the new allocation with the right neighbor.
2618 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002620 oldlen = startblockval(new->br_startblock) +
2621 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002622 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2623 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002624 right.br_startoff = new->br_startoff;
2625 right.br_startblock = nullstartblock(newlen);
2626 right.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002627 xfs_iext_update_extent(ip, state, *idx, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 break;
2629
2630 case 0:
2631 /*
2632 * New allocation is not contiguous with another
2633 * delayed allocation.
2634 * Insert a new entry.
2635 */
2636 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002637 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 break;
2639 }
2640 if (oldlen != newlen) {
2641 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002642 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2643 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 /*
2645 * Nothing to do for disk quota accounting here.
2646 */
2647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
2650/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002651 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 */
2653STATIC int /* error */
2654xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002655 struct xfs_trans *tp,
2656 struct xfs_inode *ip,
2657 int whichfork,
2658 xfs_extnum_t *idx,
2659 struct xfs_btree_cur **curp,
2660 struct xfs_bmbt_irec *new,
2661 xfs_fsblock_t *first,
2662 struct xfs_defer_ops *dfops,
2663 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002665 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2666 struct xfs_mount *mp = ip->i_mount;
2667 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2671 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002672 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002673 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002674 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Christoph Hellwig6d045582017-04-11 16:45:54 -07002676 ASSERT(*idx >= 0);
2677 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002678 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002679 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002680
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002681 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002682
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 /*
2684 * Check and set flags if this segment has a left neighbor.
2685 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002686 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002687 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002688 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002689 if (isnullstartblock(left.br_startblock))
2690 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 /*
2694 * Check and set flags if this segment has a current value.
2695 * Not true if we're inserting into the "hole" at eof.
2696 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002697 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002698 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002699 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002700 if (isnullstartblock(right.br_startblock))
2701 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /*
2705 * We're inserting a real allocation between "left" and "right".
2706 * Set the contiguity flags. Don't let extents get too large.
2707 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002708 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2709 left.br_startoff + left.br_blockcount == new->br_startoff &&
2710 left.br_startblock + left.br_blockcount == new->br_startblock &&
2711 left.br_state == new->br_state &&
2712 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2713 state |= BMAP_LEFT_CONTIG;
2714
2715 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2716 new->br_startoff + new->br_blockcount == right.br_startoff &&
2717 new->br_startblock + new->br_blockcount == right.br_startblock &&
2718 new->br_state == right.br_state &&
2719 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2720 (!(state & BMAP_LEFT_CONTIG) ||
2721 left.br_blockcount + new->br_blockcount +
2722 right.br_blockcount <= MAXEXTLEN))
2723 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002725 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 /*
2727 * Select which case we're in here, and implement it.
2728 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002729 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2730 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 /*
2732 * New allocation is contiguous with real allocations on the
2733 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002734 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002736 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002737 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002738 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002739
Christoph Hellwig6d045582017-04-11 16:45:54 -07002740 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002741
Christoph Hellwig6d045582017-04-11 16:45:54 -07002742 XFS_IFORK_NEXT_SET(ip, whichfork,
2743 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2744 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002745 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002746 } else {
2747 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002748 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002749 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002750 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002751 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002752 error = xfs_btree_delete(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002753 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002754 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002755 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002756 error = xfs_btree_decrement(cur, 0, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002757 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002758 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002759 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002760 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002761 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002762 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002764 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002766 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 /*
2768 * New allocation is contiguous with a real allocation
2769 * on the left.
2770 * Merge the new allocation with the left neighbor.
2771 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002772 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002773 old = left;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002774
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002775 left.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002776 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002777
Christoph Hellwig6d045582017-04-11 16:45:54 -07002778 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002779 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002780 } else {
2781 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002782 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002783 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002784 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002785 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002786 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002787 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002788 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002790 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002792 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 /*
2794 * New allocation is contiguous with a real allocation
2795 * on the right.
2796 * Merge the new allocation with the right neighbor.
2797 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002798 old = right;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002799
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002800 right.br_startoff = new->br_startoff;
2801 right.br_startblock = new->br_startblock;
2802 right.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002803 xfs_iext_update_extent(ip, state, *idx, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002804
Christoph Hellwig6d045582017-04-11 16:45:54 -07002805 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002806 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002807 } else {
2808 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002809 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002810 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002811 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002812 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002813 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002814 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002815 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002817 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 case 0:
2820 /*
2821 * New allocation is not contiguous with another
2822 * real allocation.
2823 * Insert a new entry.
2824 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002825 xfs_iext_insert(ip, *idx, 1, new, state);
2826 XFS_IFORK_NEXT_SET(ip, whichfork,
2827 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2828 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002829 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002830 } else {
2831 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002832 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002833 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002834 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002835 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002836 error = xfs_btree_insert(cur, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002837 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002838 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002839 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002841 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002843
Darrick J. Wong9c194642016-08-03 12:16:05 +10002844 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002845 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002846 if (error)
2847 goto done;
2848
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002849 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002850 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002851 int tmp_logflags; /* partial log flag return val */
2852
Christoph Hellwig6d045582017-04-11 16:45:54 -07002853 ASSERT(cur == NULL);
2854 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002855 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002856 *logflagsp |= tmp_logflags;
2857 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002858 if (error)
2859 goto done;
2860 }
2861
2862 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002863 if (cur)
2864 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002865
Christoph Hellwig6d045582017-04-11 16:45:54 -07002866 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002867done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002868 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002869 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870}
2871
Nathan Scottdd9f4382006-01-11 15:28:28 +11002872/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11002873 * Functions used in the extent read, allocate and remove paths
2874 */
2875
2876/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002877 * Adjust the size of the new extent based on di_extsize and rt extsize.
2878 */
Dave Chinner68988112013-08-12 20:49:42 +10002879int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002880xfs_bmap_extsize_align(
2881 xfs_mount_t *mp,
2882 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2883 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2884 xfs_extlen_t extsz, /* align to this extent size */
2885 int rt, /* is this a realtime inode? */
2886 int eof, /* is extent at end-of-file? */
2887 int delay, /* creating delalloc extent? */
2888 int convert, /* overwriting unwritten extent? */
2889 xfs_fileoff_t *offp, /* in/out: aligned offset */
2890 xfs_extlen_t *lenp) /* in/out: aligned length */
2891{
2892 xfs_fileoff_t orig_off; /* original offset */
2893 xfs_extlen_t orig_alen; /* original length */
2894 xfs_fileoff_t orig_end; /* original off+len */
2895 xfs_fileoff_t nexto; /* next file offset */
2896 xfs_fileoff_t prevo; /* previous file offset */
2897 xfs_fileoff_t align_off; /* temp for offset */
2898 xfs_extlen_t align_alen; /* temp for length */
2899 xfs_extlen_t temp; /* temp for calculations */
2900
2901 if (convert)
2902 return 0;
2903
2904 orig_off = align_off = *offp;
2905 orig_alen = align_alen = *lenp;
2906 orig_end = orig_off + orig_alen;
2907
2908 /*
2909 * If this request overlaps an existing extent, then don't
2910 * attempt to perform any additional alignment.
2911 */
2912 if (!delay && !eof &&
2913 (orig_off >= gotp->br_startoff) &&
2914 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2915 return 0;
2916 }
2917
2918 /*
2919 * If the file offset is unaligned vs. the extent size
2920 * we need to align it. This will be possible unless
2921 * the file was previously written with a kernel that didn't
2922 * perform this alignment, or if a truncate shot us in the
2923 * foot.
2924 */
2925 temp = do_mod(orig_off, extsz);
2926 if (temp) {
2927 align_alen += temp;
2928 align_off -= temp;
2929 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002930
2931 /* Same adjustment for the end of the requested area. */
2932 temp = (align_alen % extsz);
2933 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002934 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002935
2936 /*
2937 * For large extent hint sizes, the aligned extent might be larger than
2938 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2939 * the length back under MAXEXTLEN. The outer allocation loops handle
2940 * short allocation just fine, so it is safe to do this. We only want to
2941 * do it when we are forced to, though, because it means more allocation
2942 * operations are required.
2943 */
2944 while (align_alen > MAXEXTLEN)
2945 align_alen -= extsz;
2946 ASSERT(align_alen <= MAXEXTLEN);
2947
Nathan Scottdd9f4382006-01-11 15:28:28 +11002948 /*
2949 * If the previous block overlaps with this proposed allocation
2950 * then move the start forward without adjusting the length.
2951 */
2952 if (prevp->br_startoff != NULLFILEOFF) {
2953 if (prevp->br_startblock == HOLESTARTBLOCK)
2954 prevo = prevp->br_startoff;
2955 else
2956 prevo = prevp->br_startoff + prevp->br_blockcount;
2957 } else
2958 prevo = 0;
2959 if (align_off != orig_off && align_off < prevo)
2960 align_off = prevo;
2961 /*
2962 * If the next block overlaps with this proposed allocation
2963 * then move the start back without adjusting the length,
2964 * but not before offset 0.
2965 * This may of course make the start overlap previous block,
2966 * and if we hit the offset 0 limit then the next block
2967 * can still overlap too.
2968 */
2969 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2970 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2971 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2972 nexto = gotp->br_startoff + gotp->br_blockcount;
2973 else
2974 nexto = gotp->br_startoff;
2975 } else
2976 nexto = NULLFILEOFF;
2977 if (!eof &&
2978 align_off + align_alen != orig_end &&
2979 align_off + align_alen > nexto)
2980 align_off = nexto > align_alen ? nexto - align_alen : 0;
2981 /*
2982 * If we're now overlapping the next or previous extent that
2983 * means we can't fit an extsz piece in this hole. Just move
2984 * the start forward to the first valid spot and set
2985 * the length so we hit the end.
2986 */
2987 if (align_off != orig_off && align_off < prevo)
2988 align_off = prevo;
2989 if (align_off + align_alen != orig_end &&
2990 align_off + align_alen > nexto &&
2991 nexto != NULLFILEOFF) {
2992 ASSERT(nexto > prevo);
2993 align_alen = nexto - align_off;
2994 }
2995
2996 /*
2997 * If realtime, and the result isn't a multiple of the realtime
2998 * extent size we need to remove blocks until it is.
2999 */
3000 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
3001 /*
3002 * We're not covering the original request, or
3003 * we won't be able to once we fix the length.
3004 */
3005 if (orig_off < align_off ||
3006 orig_end > align_off + align_alen ||
3007 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003008 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003009 /*
3010 * Try to fix it by moving the start up.
3011 */
3012 if (align_off + temp <= orig_off) {
3013 align_alen -= temp;
3014 align_off += temp;
3015 }
3016 /*
3017 * Try to fix it by moving the end in.
3018 */
3019 else if (align_off + align_alen - temp >= orig_end)
3020 align_alen -= temp;
3021 /*
3022 * Set the start to the minimum then trim the length.
3023 */
3024 else {
3025 align_alen -= orig_off - align_off;
3026 align_off = orig_off;
3027 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3028 }
3029 /*
3030 * Result doesn't cover the request, fail it.
3031 */
3032 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003033 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003034 } else {
3035 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003036 /* see MAXEXTLEN handling above */
3037 ASSERT(orig_end <= align_off + align_alen ||
3038 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003039 }
3040
3041#ifdef DEBUG
3042 if (!eof && gotp->br_startoff != NULLFILEOFF)
3043 ASSERT(align_off + align_alen <= gotp->br_startoff);
3044 if (prevp->br_startoff != NULLFILEOFF)
3045 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3046#endif
3047
3048 *lenp = align_alen;
3049 *offp = align_off;
3050 return 0;
3051}
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053#define XFS_ALLOC_GAP_UNITS 4
3054
Dave Chinner68988112013-08-12 20:49:42 +10003055void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003056xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003057 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058{
3059 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3061 xfs_mount_t *mp; /* mount point structure */
3062 int nullfb; /* true if ap->firstblock isn't set */
3063 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
3065#define ISVALID(x,y) \
3066 (rt ? \
3067 (x) < mp->m_sb.sb_rblocks : \
3068 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3069 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3070 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003073 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003074 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3075 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003076 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 /*
3078 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003079 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003081 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3082 !isnullstartblock(ap->prev.br_startblock) &&
3083 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3084 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003085 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 /*
3087 * Adjust for the gap between prevp and us.
3088 */
Dave Chinner3a756672011-09-18 20:40:58 +00003089 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003090 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003092 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3093 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 }
3095 /*
3096 * If not at eof, then compare the two neighbor blocks.
3097 * Figure out whether either one gives us a good starting point,
3098 * and pick the better one.
3099 */
3100 else if (!ap->eof) {
3101 xfs_fsblock_t gotbno; /* right side block number */
3102 xfs_fsblock_t gotdiff=0; /* right side difference */
3103 xfs_fsblock_t prevbno; /* left side block number */
3104 xfs_fsblock_t prevdiff=0; /* left side difference */
3105
3106 /*
3107 * If there's a previous (left) block, select a requested
3108 * start block based on it.
3109 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003110 if (ap->prev.br_startoff != NULLFILEOFF &&
3111 !isnullstartblock(ap->prev.br_startblock) &&
3112 (prevbno = ap->prev.br_startblock +
3113 ap->prev.br_blockcount) &&
3114 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 /*
3116 * Calculate gap to end of previous block.
3117 */
Dave Chinner3a756672011-09-18 20:40:58 +00003118 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003119 (ap->prev.br_startoff +
3120 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 /*
3122 * Figure the startblock based on the previous block's
3123 * end and the gap size.
3124 * Heuristic!
3125 * If the gap is large relative to the piece we're
3126 * allocating, or using it gives us an invalid block
3127 * number, then just use the end of the previous block.
3128 */
Dave Chinner3a756672011-09-18 20:40:58 +00003129 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003131 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 prevbno += adjust;
3133 else
3134 prevdiff += adjust;
3135 /*
3136 * If the firstblock forbids it, can't use it,
3137 * must use default.
3138 */
3139 if (!rt && !nullfb &&
3140 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3141 prevbno = NULLFSBLOCK;
3142 }
3143 /*
3144 * No previous block or can't follow it, just default.
3145 */
3146 else
3147 prevbno = NULLFSBLOCK;
3148 /*
3149 * If there's a following (right) block, select a requested
3150 * start block based on it.
3151 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003152 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 /*
3154 * Calculate gap to start of next block.
3155 */
Dave Chinner3a756672011-09-18 20:40:58 +00003156 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 /*
3158 * Figure the startblock based on the next block's
3159 * start and the gap size.
3160 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003161 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 /*
3163 * Heuristic!
3164 * If the gap is large relative to the piece we're
3165 * allocating, or using it gives us an invalid block
3166 * number, then just use the start of the next block
3167 * offset by our length.
3168 */
Dave Chinner3a756672011-09-18 20:40:58 +00003169 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 ISVALID(gotbno - gotdiff, gotbno))
3171 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003172 else if (ISVALID(gotbno - ap->length, gotbno)) {
3173 gotbno -= ap->length;
3174 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 } else
3176 gotdiff += adjust;
3177 /*
3178 * If the firstblock forbids it, can't use it,
3179 * must use default.
3180 */
3181 if (!rt && !nullfb &&
3182 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3183 gotbno = NULLFSBLOCK;
3184 }
3185 /*
3186 * No next block, just default.
3187 */
3188 else
3189 gotbno = NULLFSBLOCK;
3190 /*
3191 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003192 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 */
3194 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003195 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003197 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003199 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003201#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003202}
3203
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003204static int
3205xfs_bmap_longest_free_extent(
3206 struct xfs_trans *tp,
3207 xfs_agnumber_t ag,
3208 xfs_extlen_t *blen,
3209 int *notinit)
3210{
3211 struct xfs_mount *mp = tp->t_mountp;
3212 struct xfs_perag *pag;
3213 xfs_extlen_t longest;
3214 int error = 0;
3215
3216 pag = xfs_perag_get(mp, ag);
3217 if (!pag->pagf_init) {
3218 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3219 if (error)
3220 goto out;
3221
3222 if (!pag->pagf_init) {
3223 *notinit = 1;
3224 goto out;
3225 }
3226 }
3227
Dave Chinner50adbcb2015-06-22 10:04:31 +10003228 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003229 xfs_alloc_min_freelist(mp, pag),
3230 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003231 if (*blen < longest)
3232 *blen = longest;
3233
3234out:
3235 xfs_perag_put(pag);
3236 return error;
3237}
3238
3239static void
3240xfs_bmap_select_minlen(
3241 struct xfs_bmalloca *ap,
3242 struct xfs_alloc_arg *args,
3243 xfs_extlen_t *blen,
3244 int notinit)
3245{
3246 if (notinit || *blen < ap->minlen) {
3247 /*
3248 * Since we did a BUF_TRYLOCK above, it is possible that
3249 * there is space for this request.
3250 */
3251 args->minlen = ap->minlen;
3252 } else if (*blen < args->maxlen) {
3253 /*
3254 * If the best seen length is less than the request length,
3255 * use the best as the minimum.
3256 */
3257 args->minlen = *blen;
3258 } else {
3259 /*
3260 * Otherwise we've seen an extent as big as maxlen, use that
3261 * as the minimum.
3262 */
3263 args->minlen = args->maxlen;
3264 }
3265}
3266
Nathan Scotta365bdd2006-03-14 13:34:16 +11003267STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003268xfs_bmap_btalloc_nullfb(
3269 struct xfs_bmalloca *ap,
3270 struct xfs_alloc_arg *args,
3271 xfs_extlen_t *blen)
3272{
3273 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003274 xfs_agnumber_t ag, startag;
3275 int notinit = 0;
3276 int error;
3277
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003278 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003279 args->total = ap->total;
3280
Christoph Hellwigc467c042010-02-15 23:34:42 +00003281 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3282 if (startag == NULLAGNUMBER)
3283 startag = ag = 0;
3284
Dave Chinner14b064c2011-01-27 12:16:28 +11003285 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003286 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3287 &notinit);
3288 if (error)
3289 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003290
Christoph Hellwigc467c042010-02-15 23:34:42 +00003291 if (++ag == mp->m_sb.sb_agcount)
3292 ag = 0;
3293 if (ag == startag)
3294 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003295 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003296
3297 xfs_bmap_select_minlen(ap, args, blen, notinit);
3298 return 0;
3299}
3300
3301STATIC int
3302xfs_bmap_btalloc_filestreams(
3303 struct xfs_bmalloca *ap,
3304 struct xfs_alloc_arg *args,
3305 xfs_extlen_t *blen)
3306{
3307 struct xfs_mount *mp = ap->ip->i_mount;
3308 xfs_agnumber_t ag;
3309 int notinit = 0;
3310 int error;
3311
3312 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3313 args->total = ap->total;
3314
3315 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3316 if (ag == NULLAGNUMBER)
3317 ag = 0;
3318
3319 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3320 if (error)
3321 return error;
3322
3323 if (*blen < args->maxlen) {
3324 error = xfs_filestream_new_ag(ap, &ag);
3325 if (error)
3326 return error;
3327
3328 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3329 &notinit);
3330 if (error)
3331 return error;
3332
3333 }
3334
3335 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003336
3337 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003338 * Set the failure fallback case to look in the selected AG as stream
3339 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003340 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003341 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003342 return 0;
3343}
3344
3345STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003346xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003347 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003348{
3349 xfs_mount_t *mp; /* mount point structure */
3350 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003351 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003352 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003353 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003354 xfs_alloc_arg_t args;
3355 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003356 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003357 int nullfb; /* true if ap->firstblock isn't set */
3358 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003359 int tryagain;
3360 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003361 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003362
Dave Chinnera99ebf42011-12-01 11:24:20 +00003363 ASSERT(ap->length);
3364
Nathan Scotta365bdd2006-03-14 13:34:16 +11003365 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003366
3367 /* stripe alignment for allocation is determined by mount parameters */
3368 stripe_align = 0;
3369 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3370 stripe_align = mp->m_swidth;
3371 else if (mp->m_dalign)
3372 stripe_align = mp->m_dalign;
3373
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003374 if (ap->flags & XFS_BMAPI_COWFORK)
3375 align = xfs_get_cowextsz_hint(ap->ip);
3376 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003377 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003378 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003379 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003380 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003381 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003382 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003383 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003384 }
Dave Chinner33177f052013-12-12 16:34:36 +11003385
3386
Dave Chinner0937e0f2011-09-18 20:40:57 +00003387 nullfb = *ap->firstblock == NULLFSBLOCK;
3388 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003389 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003390 if (xfs_alloc_is_userdata(ap->datatype) &&
3391 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003392 ag = xfs_filestream_lookup_ag(ap->ip);
3393 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003394 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003395 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003396 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003397 }
3398 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003399 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003400
3401 xfs_bmap_adjacent(ap);
3402
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003404 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 * it's in the right allocation group.
3406 */
Dave Chinner3a756672011-09-18 20:40:58 +00003407 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 ;
3409 else
Dave Chinner3a756672011-09-18 20:40:58 +00003410 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 * Normal allocation, done through xfs_alloc_vextent.
3413 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003414 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003415 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003416 args.tp = ap->tp;
3417 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003418 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003419 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003420
3421 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003422 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003423 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003424 blen = 0;
3425 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003426 /*
3427 * Search for an allocation group with a single extent large
3428 * enough for the request. If one isn't found, then adjust
3429 * the minimum allocation size to the largest space found.
3430 */
Dave Chinner292378e2016-09-26 08:21:28 +10003431 if (xfs_alloc_is_userdata(ap->datatype) &&
3432 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003433 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3434 else
3435 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003436 if (error)
3437 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003438 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003439 if (xfs_inode_is_filestream(ap->ip))
3440 args.type = XFS_ALLOCTYPE_FIRST_AG;
3441 else
3442 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003443 args.total = args.minlen = ap->minlen;
3444 } else {
3445 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3446 args.total = ap->total;
3447 args.minlen = ap->minlen;
3448 }
David Chinner957d0eb2007-06-18 16:50:37 +10003449 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003450 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003451 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003452 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003453 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003454 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003455 args.prod = 1;
3456 args.mod = 0;
3457 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003458 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003459 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003460 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3461 }
3462 /*
3463 * If we are not low on available data blocks, and the
3464 * underlying logical volume manager is a stripe, and
3465 * the file offset is zero then try to allocate data
3466 * blocks on stripe unit boundary.
3467 * NOTE: ap->aeof is only set if the allocation length
3468 * is >= the stripe unit and the allocation offset is
3469 * at the end of file.
3470 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003471 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003472 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003473 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003474 atype = args.type;
3475 isaligned = 1;
3476 /*
3477 * Adjust for alignment
3478 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003479 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003480 args.minlen = blen - args.alignment;
3481 args.minalignslop = 0;
3482 } else {
3483 /*
3484 * First try an exact bno allocation.
3485 * If it fails then do a near or start bno
3486 * allocation with alignment turned on.
3487 */
3488 atype = args.type;
3489 tryagain = 1;
3490 args.type = XFS_ALLOCTYPE_THIS_BNO;
3491 args.alignment = 1;
3492 /*
3493 * Compute the minlen+alignment for the
3494 * next case. Set slop so that the value
3495 * of minlen+alignment+slop doesn't go up
3496 * between the calls.
3497 */
Dave Chinner33177f052013-12-12 16:34:36 +11003498 if (blen > stripe_align && blen <= args.maxlen)
3499 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003500 else
3501 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003502 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003503 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003504 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003505 args.minlen - 1;
3506 else
3507 args.minalignslop = 0;
3508 }
3509 } else {
3510 args.alignment = 1;
3511 args.minalignslop = 0;
3512 }
3513 args.minleft = ap->minleft;
3514 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003515 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003516 args.datatype = ap->datatype;
3517 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003518 args.ip = ap->ip;
3519
3520 error = xfs_alloc_vextent(&args);
3521 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003522 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003523
Nathan Scotta365bdd2006-03-14 13:34:16 +11003524 if (tryagain && args.fsbno == NULLFSBLOCK) {
3525 /*
3526 * Exact allocation failed. Now try with alignment
3527 * turned on.
3528 */
3529 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003530 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003531 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003532 args.minlen = nextminlen;
3533 args.minalignslop = 0;
3534 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 if ((error = xfs_alloc_vextent(&args)))
3536 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003537 }
3538 if (isaligned && args.fsbno == NULLFSBLOCK) {
3539 /*
3540 * allocation failed, so turn off alignment and
3541 * try again.
3542 */
3543 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003544 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003545 args.alignment = 0;
3546 if ((error = xfs_alloc_vextent(&args)))
3547 return error;
3548 }
3549 if (args.fsbno == NULLFSBLOCK && nullfb &&
3550 args.minlen > ap->minlen) {
3551 args.minlen = ap->minlen;
3552 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003553 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003554 if ((error = xfs_alloc_vextent(&args)))
3555 return error;
3556 }
3557 if (args.fsbno == NULLFSBLOCK && nullfb) {
3558 args.fsbno = 0;
3559 args.type = XFS_ALLOCTYPE_FIRST_AG;
3560 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003561 if ((error = xfs_alloc_vextent(&args)))
3562 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003563 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003564 }
3565 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003566 /*
3567 * check the allocation happened at the same or higher AG than
3568 * the first block that was allocated.
3569 */
3570 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003571 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3572 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003573
Dave Chinner3a756672011-09-18 20:40:58 +00003574 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003575 if (*ap->firstblock == NULLFSBLOCK)
3576 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003577 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003578 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003579 if (!(ap->flags & XFS_BMAPI_COWFORK))
3580 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003581 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3582 if (ap->wasdel)
3583 ap->ip->i_delayed_blks -= args.len;
3584 /*
3585 * Adjust the disk quota also. This was reserved
3586 * earlier.
3587 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003588 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003589 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3590 XFS_TRANS_DQ_BCOUNT,
3591 (long) args.len);
3592 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003593 ap->blkno = NULLFSBLOCK;
3594 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 }
3596 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003597}
3598
3599/*
3600 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3601 * It figures out where to ask the underlying allocator to put the new extent.
3602 */
3603STATIC int
3604xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003605 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003606{
Dave Chinner292378e2016-09-26 08:21:28 +10003607 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3608 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003609 return xfs_bmap_rtalloc(ap);
3610 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611}
3612
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003613/* Trim extent to fit a logical block range. */
3614void
3615xfs_trim_extent(
3616 struct xfs_bmbt_irec *irec,
3617 xfs_fileoff_t bno,
3618 xfs_filblks_t len)
3619{
3620 xfs_fileoff_t distance;
3621 xfs_fileoff_t end = bno + len;
3622
3623 if (irec->br_startoff + irec->br_blockcount <= bno ||
3624 irec->br_startoff >= end) {
3625 irec->br_blockcount = 0;
3626 return;
3627 }
3628
3629 if (irec->br_startoff < bno) {
3630 distance = bno - irec->br_startoff;
3631 if (isnullstartblock(irec->br_startblock))
3632 irec->br_startblock = DELAYSTARTBLOCK;
3633 if (irec->br_startblock != DELAYSTARTBLOCK &&
3634 irec->br_startblock != HOLESTARTBLOCK)
3635 irec->br_startblock += distance;
3636 irec->br_startoff += distance;
3637 irec->br_blockcount -= distance;
3638 }
3639
3640 if (end < irec->br_startoff + irec->br_blockcount) {
3641 distance = irec->br_startoff + irec->br_blockcount - end;
3642 irec->br_blockcount -= distance;
3643 }
3644}
3645
Brian Foster40214d12017-10-13 09:47:46 -07003646/* trim extent to within eof */
3647void
3648xfs_trim_extent_eof(
3649 struct xfs_bmbt_irec *irec,
3650 struct xfs_inode *ip)
3651
3652{
3653 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3654 i_size_read(VFS_I(ip))));
3655}
3656
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003658 * Trim the returned map to the required bounds
3659 */
3660STATIC void
3661xfs_bmapi_trim_map(
3662 struct xfs_bmbt_irec *mval,
3663 struct xfs_bmbt_irec *got,
3664 xfs_fileoff_t *bno,
3665 xfs_filblks_t len,
3666 xfs_fileoff_t obno,
3667 xfs_fileoff_t end,
3668 int n,
3669 int flags)
3670{
3671 if ((flags & XFS_BMAPI_ENTIRE) ||
3672 got->br_startoff + got->br_blockcount <= obno) {
3673 *mval = *got;
3674 if (isnullstartblock(got->br_startblock))
3675 mval->br_startblock = DELAYSTARTBLOCK;
3676 return;
3677 }
3678
3679 if (obno > *bno)
3680 *bno = obno;
3681 ASSERT((*bno >= obno) || (n == 0));
3682 ASSERT(*bno < end);
3683 mval->br_startoff = *bno;
3684 if (isnullstartblock(got->br_startblock))
3685 mval->br_startblock = DELAYSTARTBLOCK;
3686 else
3687 mval->br_startblock = got->br_startblock +
3688 (*bno - got->br_startoff);
3689 /*
3690 * Return the minimum of what we got and what we asked for for
3691 * the length. We can use the len variable here because it is
3692 * modified below and we could have been there before coming
3693 * here if the first part of the allocation didn't overlap what
3694 * was asked for.
3695 */
3696 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3697 got->br_blockcount - (*bno - got->br_startoff));
3698 mval->br_state = got->br_state;
3699 ASSERT(mval->br_blockcount <= len);
3700 return;
3701}
3702
3703/*
3704 * Update and validate the extent map to return
3705 */
3706STATIC void
3707xfs_bmapi_update_map(
3708 struct xfs_bmbt_irec **map,
3709 xfs_fileoff_t *bno,
3710 xfs_filblks_t *len,
3711 xfs_fileoff_t obno,
3712 xfs_fileoff_t end,
3713 int *n,
3714 int flags)
3715{
3716 xfs_bmbt_irec_t *mval = *map;
3717
3718 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3719 ((mval->br_startoff + mval->br_blockcount) <= end));
3720 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3721 (mval->br_startoff < obno));
3722
3723 *bno = mval->br_startoff + mval->br_blockcount;
3724 *len = end - *bno;
3725 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3726 /* update previous map with new information */
3727 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3728 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3729 ASSERT(mval->br_state == mval[-1].br_state);
3730 mval[-1].br_blockcount = mval->br_blockcount;
3731 mval[-1].br_state = mval->br_state;
3732 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3733 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3734 mval[-1].br_startblock != HOLESTARTBLOCK &&
3735 mval->br_startblock == mval[-1].br_startblock +
3736 mval[-1].br_blockcount &&
3737 ((flags & XFS_BMAPI_IGSTATE) ||
3738 mval[-1].br_state == mval->br_state)) {
3739 ASSERT(mval->br_startoff ==
3740 mval[-1].br_startoff + mval[-1].br_blockcount);
3741 mval[-1].br_blockcount += mval->br_blockcount;
3742 } else if (*n > 0 &&
3743 mval->br_startblock == DELAYSTARTBLOCK &&
3744 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3745 mval->br_startoff ==
3746 mval[-1].br_startoff + mval[-1].br_blockcount) {
3747 mval[-1].br_blockcount += mval->br_blockcount;
3748 mval[-1].br_state = mval->br_state;
3749 } else if (!((*n == 0) &&
3750 ((mval->br_startoff + mval->br_blockcount) <=
3751 obno))) {
3752 mval++;
3753 (*n)++;
3754 }
3755 *map = mval;
3756}
3757
3758/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003759 * Map file blocks to filesystem blocks without allocation.
3760 */
3761int
3762xfs_bmapi_read(
3763 struct xfs_inode *ip,
3764 xfs_fileoff_t bno,
3765 xfs_filblks_t len,
3766 struct xfs_bmbt_irec *mval,
3767 int *nmap,
3768 int flags)
3769{
3770 struct xfs_mount *mp = ip->i_mount;
3771 struct xfs_ifork *ifp;
3772 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003773 xfs_fileoff_t obno;
3774 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003775 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003776 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003777 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003778 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003779 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003780
3781 ASSERT(*nmap >= 1);
3782 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003783 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003784 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003785
3786 if (unlikely(XFS_TEST_ERROR(
3787 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3788 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003789 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003790 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003791 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003792 }
3793
3794 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003795 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003796
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003797 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003798
3799 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003800
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003801 /* No CoW fork? Return a hole. */
3802 if (whichfork == XFS_COW_FORK && !ifp) {
3803 mval->br_startoff = bno;
3804 mval->br_startblock = HOLESTARTBLOCK;
3805 mval->br_blockcount = len;
3806 mval->br_state = XFS_EXT_NORM;
3807 *nmap = 1;
3808 return 0;
3809 }
3810
Dave Chinner5c8ed202011-09-18 20:40:45 +00003811 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3812 error = xfs_iread_extents(NULL, ip, whichfork);
3813 if (error)
3814 return error;
3815 }
3816
Christoph Hellwig334f3422016-11-24 11:39:43 +11003817 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
3818 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003819 end = bno + len;
3820 obno = bno;
3821
3822 while (bno < end && n < *nmap) {
3823 /* Reading past eof, act as though there's a hole up to end. */
3824 if (eof)
3825 got.br_startoff = end;
3826 if (got.br_startoff > bno) {
3827 /* Reading in a hole. */
3828 mval->br_startoff = bno;
3829 mval->br_startblock = HOLESTARTBLOCK;
3830 mval->br_blockcount =
3831 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3832 mval->br_state = XFS_EXT_NORM;
3833 bno += mval->br_blockcount;
3834 len -= mval->br_blockcount;
3835 mval++;
3836 n++;
3837 continue;
3838 }
3839
3840 /* set up the extent map to return. */
3841 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3842 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3843
3844 /* If we're done, stop now. */
3845 if (bno >= end || n >= *nmap)
3846 break;
3847
3848 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11003849 if (!xfs_iext_get_extent(ifp, ++idx, &got))
3850 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003851 }
3852 *nmap = n;
3853 return 0;
3854}
3855
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003856/*
3857 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3858 * global pool and the extent inserted into the inode in-core extent tree.
3859 *
3860 * On entry, got refers to the first extent beyond the offset of the extent to
3861 * allocate or eof is specified if no such extent exists. On return, got refers
3862 * to the extent record that was inserted to the inode fork.
3863 *
3864 * Note that the allocated extent may have been merged with contiguous extents
3865 * during insertion into the inode fork. Thus, got does not reflect the current
3866 * state of the inode fork on return. If necessary, the caller can use lastx to
3867 * look up the updated record in the inode fork.
3868 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003869int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003870xfs_bmapi_reserve_delalloc(
3871 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003872 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003873 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003874 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003875 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003876 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003877 xfs_extnum_t *lastx,
3878 int eof)
3879{
3880 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003881 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003882 xfs_extlen_t alen;
3883 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003884 char rt = XFS_IS_REALTIME_INODE(ip);
3885 xfs_extlen_t extsz;
3886 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003887 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003888
Brian Foster974ae922016-11-28 14:57:42 +11003889 /*
3890 * Cap the alloc length. Keep track of prealloc so we know whether to
3891 * tag the inode before we return.
3892 */
3893 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003894 if (!eof)
3895 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003896 if (prealloc && alen >= len)
3897 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003898
3899 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003900 if (whichfork == XFS_COW_FORK)
3901 extsz = xfs_get_cowextsz_hint(ip);
3902 else
3903 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003904 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003905 struct xfs_bmbt_irec prev;
3906
3907 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
3908 prev.br_startoff = NULLFILEOFF;
3909
3910 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003911 1, 0, &aoff, &alen);
3912 ASSERT(!error);
3913 }
3914
3915 if (rt)
3916 extsz = alen / mp->m_sb.sb_rextsize;
3917
3918 /*
3919 * Make a transaction-less quota reservation for delayed allocation
3920 * blocks. This number gets adjusted later. We return if we haven't
3921 * allocated blocks already inside this loop.
3922 */
3923 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
3924 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
3925 if (error)
3926 return error;
3927
3928 /*
3929 * Split changing sb for alen and indlen since they could be coming
3930 * from different places.
3931 */
3932 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3933 ASSERT(indlen > 0);
3934
3935 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11003936 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003937 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11003938 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003939 }
3940
3941 if (error)
3942 goto out_unreserve_quota;
3943
Dave Chinner0d485ad2015-02-23 21:22:03 +11003944 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003945 if (error)
3946 goto out_unreserve_blocks;
3947
3948
3949 ip->i_delayed_blks += alen;
3950
3951 got->br_startoff = aoff;
3952 got->br_startblock = nullstartblock(indlen);
3953 got->br_blockcount = alen;
3954 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003955
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003956 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003957
Brian Foster974ae922016-11-28 14:57:42 +11003958 /*
3959 * Tag the inode if blocks were preallocated. Note that COW fork
3960 * preallocation can occur at the start or end of the extent, even when
3961 * prealloc == 0, so we must also check the aligned offset and length.
3962 */
3963 if (whichfork == XFS_DATA_FORK && prealloc)
3964 xfs_inode_set_eofblocks_tag(ip);
3965 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
3966 xfs_inode_set_cowblocks_tag(ip);
3967
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003968 return 0;
3969
3970out_unreserve_blocks:
3971 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11003972 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003973 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11003974 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003975out_unreserve_quota:
3976 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10003977 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003978 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
3979 return error;
3980}
3981
Dave Chinnercf11da92014-07-15 07:08:24 +10003982static int
3983xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10003984 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003985{
3986 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003987 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003988 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00003989 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003990 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003991
Dave Chinnera99ebf42011-12-01 11:24:20 +00003992 ASSERT(bma->length > 0);
3993
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003994 /*
3995 * For the wasdelay case, we could also just allocate the stuff asked
3996 * for in this bmap call but that wouldn't be as good.
3997 */
3998 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00003999 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4000 bma->offset = bma->got.br_startoff;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004001 if (bma->idx)
4002 xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004003 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004004 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004005 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004006 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004007 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004008 }
4009
4010 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004011 * Set the data type being allocated. For the data fork, the first data
4012 * in the file is treated differently to all other allocations. For the
4013 * attribute fork, we only need to ensure the allocated range is not on
4014 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004015 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004016 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004017 bma->datatype = XFS_ALLOC_NOBUSY;
4018 if (whichfork == XFS_DATA_FORK) {
4019 if (bma->offset == 0)
4020 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4021 else
4022 bma->datatype |= XFS_ALLOC_USERDATA;
4023 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004024 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004025 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004026 }
4027
Dave Chinnere04426b2012-10-05 11:06:59 +10004028 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004029
4030 /*
4031 * Only want to do the alignment at the eof if it is userdata and
4032 * allocation length is larger than a stripe unit.
4033 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004034 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004035 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004036 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004037 if (error)
4038 return error;
4039 }
4040
4041 error = xfs_bmap_alloc(bma);
4042 if (error)
4043 return error;
4044
Dave Chinner29c8d172011-09-18 20:41:00 +00004045 if (bma->cur)
4046 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004047 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004048 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004049 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4050 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4051 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004052 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004053 }
4054 /*
4055 * Bump the number of extents we've allocated
4056 * in this call.
4057 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004058 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004059
Dave Chinner29c8d172011-09-18 20:41:00 +00004060 if (bma->cur)
4061 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004062 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4063
Dave Chinner963c30c2011-09-18 20:40:59 +00004064 bma->got.br_startoff = bma->offset;
4065 bma->got.br_startblock = bma->blkno;
4066 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004067 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004068
4069 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004070 * In the data fork, a wasdelay extent has been initialized, so
4071 * shouldn't be flagged as unwritten.
4072 *
4073 * For the cow fork, however, we convert delalloc reservations
4074 * (extents allocated for speculative preallocation) to
4075 * allocated unwritten extents, and only convert the unwritten
4076 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004077 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004078 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4079 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004080 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004081 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004082
Christoph Hellwigc6534242011-09-18 20:41:05 +00004083 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004084 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004085 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004086 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4087 whichfork, &bma->idx, &bma->cur, &bma->got,
4088 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004089
Christoph Hellwigc315c902011-09-18 20:41:02 +00004090 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004091 if (error)
4092 return error;
4093
4094 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004095 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4096 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4097 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004098 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004099 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004100
Dave Chinner963c30c2011-09-18 20:40:59 +00004101 ASSERT(bma->got.br_startoff <= bma->offset);
4102 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4103 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004104 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4105 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004106 return 0;
4107}
4108
Dave Chinnerb447fe52011-09-18 20:40:51 +00004109STATIC int
4110xfs_bmapi_convert_unwritten(
4111 struct xfs_bmalloca *bma,
4112 struct xfs_bmbt_irec *mval,
4113 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004114 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004115{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004116 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004117 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004118 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004119 int error;
4120
Dave Chinnerb447fe52011-09-18 20:40:51 +00004121 /* check if we need to do unwritten->real conversion */
4122 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4123 (flags & XFS_BMAPI_PREALLOC))
4124 return 0;
4125
4126 /* check if we need to do real->unwritten conversion */
4127 if (mval->br_state == XFS_EXT_NORM &&
4128 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4129 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4130 return 0;
4131
4132 /*
4133 * Modify (by adding) the state flag, if writing.
4134 */
4135 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004136 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4137 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004138 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004139 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004140 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004141 }
4142 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4143 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4144
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004145 /*
4146 * Before insertion into the bmbt, zero the range being converted
4147 * if required.
4148 */
4149 if (flags & XFS_BMAPI_ZERO) {
4150 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4151 mval->br_blockcount);
4152 if (error)
4153 return error;
4154 }
4155
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004156 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4157 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004158 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004159 /*
4160 * Log the inode core unconditionally in the unwritten extent conversion
4161 * path because the conversion might not have done so (e.g., if the
4162 * extent count hasn't changed). We need to make sure the inode is dirty
4163 * in the transaction for the sake of fsync(), even if nothing has
4164 * changed, because fsync() will not force the log for this transaction
4165 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004166 *
4167 * Note: If we're only converting cow fork extents, there aren't
4168 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004169 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004170 if (whichfork != XFS_COW_FORK)
4171 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004172 if (error)
4173 return error;
4174
4175 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004176 * Update our extent pointer, given that
4177 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4178 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004179 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004180 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004181
4182 /*
4183 * We may have combined previously unwritten space with written space,
4184 * so generate another request.
4185 */
4186 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004187 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004188 return 0;
4189}
4190
Christoph Hellwig44032802011-09-18 20:40:48 +00004191/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004192 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4193 * extent state if necessary. Details behaviour is controlled by the flags
4194 * parameter. Only allocates blocks from a single allocation group, to avoid
4195 * locking problems.
4196 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 * The returned value in "firstblock" from the first call in a transaction
4198 * must be remembered and presented to subsequent calls in "firstblock".
4199 * An upper bound for the number of blocks to be allocated is supplied to
4200 * the first call in "total"; if no allocation group has that many free
4201 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4202 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004203int
4204xfs_bmapi_write(
4205 struct xfs_trans *tp, /* transaction pointer */
4206 struct xfs_inode *ip, /* incore inode */
4207 xfs_fileoff_t bno, /* starting file offs. mapped */
4208 xfs_filblks_t len, /* length to map in file */
4209 int flags, /* XFS_BMAPI_... */
4210 xfs_fsblock_t *firstblock, /* first allocated block
4211 controls a.g. for allocs */
4212 xfs_extlen_t total, /* total blocks needed */
4213 struct xfs_bmbt_irec *mval, /* output: map values */
4214 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004215 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004217 struct xfs_mount *mp = ip->i_mount;
4218 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004219 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004220 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004221 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004222 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004223 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004224 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004225 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004226
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004228 xfs_fileoff_t orig_bno; /* original block number value */
4229 int orig_flags; /* original flags arg value */
4230 xfs_filblks_t orig_len; /* original value of len arg */
4231 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4232 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233
4234 orig_bno = bno;
4235 orig_len = len;
4236 orig_flags = flags;
4237 orig_mval = mval;
4238 orig_nmap = *nmap;
4239#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004240 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004243 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4244 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004245 ASSERT(tp != NULL ||
4246 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4247 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004248 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004249 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004250 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004251 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004252
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004253 /* zeroing is for currently only for data extents, not metadata */
4254 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4255 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4256 /*
4257 * we can allocate unwritten extents or pre-zero allocated blocks,
4258 * but it makes no sense to do both at once. This would result in
4259 * zeroing the unwritten extent twice, but it still being an
4260 * unwritten extent....
4261 */
4262 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4263 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4264
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 if (unlikely(XFS_TEST_ERROR(
4266 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004267 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004268 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004269 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004270 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004272
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004274 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004275
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004277
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004278 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004279
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004280 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004282 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004284 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004285 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004286 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004287 }
4288
4289 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4290 error = xfs_iread_extents(tp, ip, whichfork);
4291 if (error)
4292 goto error0;
4293 }
4294
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 n = 0;
4296 end = bno + len;
4297 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004298
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004299 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4300 eof = true;
4301 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4302 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004303 bma.tp = tp;
4304 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004305 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004306 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004307 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004308 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004309
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004311 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004312
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004313 /* in hole or beyoned EOF? */
4314 if (eof || bma.got.br_startoff > bno) {
4315 if (flags & XFS_BMAPI_DELALLOC) {
4316 /*
4317 * For the COW fork we can reasonably get a
4318 * request for converting an extent that races
4319 * with other threads already having converted
4320 * part of it, as there converting COW to
4321 * regular blocks is not protected using the
4322 * IOLOCK.
4323 */
4324 ASSERT(flags & XFS_BMAPI_COWFORK);
4325 if (!(flags & XFS_BMAPI_COWFORK)) {
4326 error = -EIO;
4327 goto error0;
4328 }
4329
4330 if (eof || bno >= end)
4331 break;
4332 } else {
4333 need_alloc = true;
4334 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004335 } else if (isnullstartblock(bma.got.br_startblock)) {
4336 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004337 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004338
4339 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 * First, deal with the hole before the allocated space
4341 * that we found, if any.
4342 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004343 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004344 bma.eof = eof;
4345 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4346 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004347 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004348 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004349
Dave Chinnera99ebf42011-12-01 11:24:20 +00004350 /*
4351 * There's a 32/64 bit type mismatch between the
4352 * allocation length request (which can be 64 bits in
4353 * length) and the bma length request, which is
4354 * xfs_extlen_t and therefore 32 bits. Hence we have to
4355 * check for 32-bit overflows and handle them here.
4356 */
4357 if (len > (xfs_filblks_t)MAXEXTLEN)
4358 bma.length = MAXEXTLEN;
4359 else
4360 bma.length = len;
4361
4362 ASSERT(len > 0);
4363 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004364 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 if (error)
4366 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004367 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004368 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004369
4370 /*
4371 * If this is a CoW allocation, record the data in
4372 * the refcount btree for orphan recovery.
4373 */
4374 if (whichfork == XFS_COW_FORK) {
4375 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4376 bma.blkno, bma.length);
4377 if (error)
4378 goto error0;
4379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004381
Dave Chinneraef9a892011-09-18 20:40:44 +00004382 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004383 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4384 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385
Dave Chinnerb447fe52011-09-18 20:40:51 +00004386 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004387 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004388 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004389 continue;
4390 if (error)
4391 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
Dave Chinneraef9a892011-09-18 20:40:44 +00004393 /* update the extent map to return */
4394 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4395
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 /*
4397 * If we're done, stop now. Stop when we've allocated
4398 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4399 * the transaction may get too big.
4400 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004401 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004403
4404 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004405 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004406 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4407 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004410
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 /*
4412 * Transform from btree to extents, give it cur.
4413 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004414 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004415 int tmp_logflags = 0;
4416
Dave Chinner29c8d172011-09-18 20:41:00 +00004417 ASSERT(bma.cur);
4418 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004420 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 if (error)
4422 goto error0;
4423 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004424
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004426 XFS_IFORK_NEXTENTS(ip, whichfork) >
4427 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429error0:
4430 /*
4431 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004432 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004434 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004436 bma.logflags &= ~xfs_ilog_fext(whichfork);
4437 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004439 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 /*
4441 * Log whatever the flags say, even if error. Otherwise we might miss
4442 * detecting a case where the data is changed, there's an error,
4443 * and it's not logged so we don't shutdown when we should.
4444 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004445 if (bma.logflags)
4446 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004447
Dave Chinner29c8d172011-09-18 20:41:00 +00004448 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 if (!error) {
4450 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004451 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004453 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004454 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004456 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4458 }
4459 if (!error)
4460 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4461 orig_nmap, *nmap);
4462 return error;
4463}
4464
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004465static int
4466xfs_bmapi_remap(
4467 struct xfs_trans *tp,
4468 struct xfs_inode *ip,
4469 xfs_fileoff_t bno,
4470 xfs_filblks_t len,
4471 xfs_fsblock_t startblock,
4472 struct xfs_defer_ops *dfops)
4473{
4474 struct xfs_mount *mp = ip->i_mount;
4475 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4476 struct xfs_btree_cur *cur = NULL;
4477 xfs_fsblock_t firstblock = NULLFSBLOCK;
4478 struct xfs_bmbt_irec got;
4479 xfs_extnum_t idx;
4480 int logflags = 0, error;
4481
4482 ASSERT(len > 0);
4483 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4484 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4485
4486 if (unlikely(XFS_TEST_ERROR(
4487 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4488 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004489 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004490 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4491 return -EFSCORRUPTED;
4492 }
4493
4494 if (XFS_FORCED_SHUTDOWN(mp))
4495 return -EIO;
4496
4497 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4498 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4499 if (error)
4500 return error;
4501 }
4502
4503 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4504 /* make sure we only reflink into a hole. */
4505 ASSERT(got.br_startoff > bno);
4506 ASSERT(got.br_startoff - bno >= len);
4507 }
4508
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004509 ip->i_d.di_nblocks += len;
4510 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004511
4512 if (ifp->if_flags & XFS_IFBROOT) {
4513 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4514 cur->bc_private.b.firstblock = firstblock;
4515 cur->bc_private.b.dfops = dfops;
4516 cur->bc_private.b.flags = 0;
4517 }
4518
4519 got.br_startoff = bno;
4520 got.br_startblock = startblock;
4521 got.br_blockcount = len;
4522 got.br_state = XFS_EXT_NORM;
4523
4524 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4525 &got, &firstblock, dfops, &logflags);
4526 if (error)
4527 goto error0;
4528
4529 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4530 int tmp_logflags = 0;
4531
4532 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4533 &tmp_logflags, XFS_DATA_FORK);
4534 logflags |= tmp_logflags;
4535 }
4536
4537error0:
4538 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4539 logflags &= ~XFS_ILOG_DEXT;
4540 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4541 logflags &= ~XFS_ILOG_DBROOT;
4542
4543 if (logflags)
4544 xfs_trans_log_inode(tp, ip, logflags);
4545 if (cur) {
4546 xfs_btree_del_cursor(cur,
4547 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4548 }
4549 return error;
4550}
4551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004553 * When a delalloc extent is split (e.g., due to a hole punch), the original
4554 * indlen reservation must be shared across the two new extents that are left
4555 * behind.
4556 *
4557 * Given the original reservation and the worst case indlen for the two new
4558 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004559 * reservation fairly across the two new extents. If necessary, steal available
4560 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4561 * ores == 1). The number of stolen blocks is returned. The availability and
4562 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004563 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004564static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004565xfs_bmap_split_indlen(
4566 xfs_filblks_t ores, /* original res. */
4567 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004568 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4569 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004570{
4571 xfs_filblks_t len1 = *indlen1;
4572 xfs_filblks_t len2 = *indlen2;
4573 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004574 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004575 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004576
4577 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004578 * Steal as many blocks as we can to try and satisfy the worst case
4579 * indlen for both new extents.
4580 */
Brian Foster75d65362017-02-13 22:48:30 -08004581 if (ores < nres && avail)
4582 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4583 ores += stolen;
4584
4585 /* nothing else to do if we've satisfied the new reservation */
4586 if (ores >= nres)
4587 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004588
4589 /*
Brian Foster75d65362017-02-13 22:48:30 -08004590 * We can't meet the total required reservation for the two extents.
4591 * Calculate the percent of the overall shortage between both extents
4592 * and apply this percentage to each of the requested indlen values.
4593 * This distributes the shortage fairly and reduces the chances that one
4594 * of the two extents is left with nothing when extents are repeatedly
4595 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004596 */
Brian Foster75d65362017-02-13 22:48:30 -08004597 resfactor = (ores * 100);
4598 do_div(resfactor, nres);
4599 len1 *= resfactor;
4600 do_div(len1, 100);
4601 len2 *= resfactor;
4602 do_div(len2, 100);
4603 ASSERT(len1 + len2 <= ores);
4604 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4605
4606 /*
4607 * Hand out the remainder to each extent. If one of the two reservations
4608 * is zero, we want to make sure that one gets a block first. The loop
4609 * below starts with len1, so hand len2 a block right off the bat if it
4610 * is zero.
4611 */
4612 ores -= (len1 + len2);
4613 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4614 if (ores && !len2 && *indlen2) {
4615 len2++;
4616 ores--;
4617 }
4618 while (ores) {
4619 if (len1 < *indlen1) {
4620 len1++;
4621 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004622 }
Brian Foster75d65362017-02-13 22:48:30 -08004623 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004624 break;
Brian Foster75d65362017-02-13 22:48:30 -08004625 if (len2 < *indlen2) {
4626 len2++;
4627 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004628 }
4629 }
4630
4631 *indlen1 = len1;
4632 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004633
4634 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004635}
4636
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004637int
4638xfs_bmap_del_extent_delay(
4639 struct xfs_inode *ip,
4640 int whichfork,
4641 xfs_extnum_t *idx,
4642 struct xfs_bmbt_irec *got,
4643 struct xfs_bmbt_irec *del)
4644{
4645 struct xfs_mount *mp = ip->i_mount;
4646 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4647 struct xfs_bmbt_irec new;
4648 int64_t da_old, da_new, da_diff = 0;
4649 xfs_fileoff_t del_endoff, got_endoff;
4650 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004651 int state = xfs_bmap_fork_to_state(whichfork);
4652 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004653 bool isrt;
4654
4655 XFS_STATS_INC(mp, xs_del_exlist);
4656
4657 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4658 del_endoff = del->br_startoff + del->br_blockcount;
4659 got_endoff = got->br_startoff + got->br_blockcount;
4660 da_old = startblockval(got->br_startblock);
4661 da_new = 0;
4662
4663 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004664 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004665 ASSERT(del->br_blockcount > 0);
4666 ASSERT(got->br_startoff <= del->br_startoff);
4667 ASSERT(got_endoff >= del_endoff);
4668
4669 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004670 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004671
4672 do_div(rtexts, mp->m_sb.sb_rextsize);
4673 xfs_mod_frextents(mp, rtexts);
4674 }
4675
4676 /*
4677 * Update the inode delalloc counter now and wait to update the
4678 * sb counters as we might have to borrow some blocks for the
4679 * indirect block accounting.
4680 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004681 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4682 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004683 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004684 if (error)
4685 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004686 ip->i_delayed_blks -= del->br_blockcount;
4687
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004688 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004689 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004690 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004691 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004692
Christoph Hellwig0173c682017-10-17 14:16:22 -07004693 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4694 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004695 /*
4696 * Matches the whole extent. Delete the entry.
4697 */
4698 xfs_iext_remove(ip, *idx, 1, state);
4699 --*idx;
4700 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004701 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004702 /*
4703 * Deleting the first part of the extent.
4704 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004705 got->br_startoff = del_endoff;
4706 got->br_blockcount -= del->br_blockcount;
4707 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4708 got->br_blockcount), da_old);
4709 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004710 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004711 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004712 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004713 /*
4714 * Deleting the last part of the extent.
4715 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004716 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4717 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4718 got->br_blockcount), da_old);
4719 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004720 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004721 break;
4722 case 0:
4723 /*
4724 * Deleting the middle of the extent.
4725 *
4726 * Distribute the original indlen reservation across the two new
4727 * extents. Steal blocks from the deleted extent if necessary.
4728 * Stealing blocks simply fudges the fdblocks accounting below.
4729 * Warn if either of the new indlen reservations is zero as this
4730 * can lead to delalloc problems.
4731 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004732 got->br_blockcount = del->br_startoff - got->br_startoff;
4733 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4734
4735 new.br_blockcount = got_endoff - del_endoff;
4736 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4737
4738 WARN_ON_ONCE(!got_indlen || !new_indlen);
4739 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4740 del->br_blockcount);
4741
4742 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004743 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004744
4745 new.br_startoff = del_endoff;
4746 new.br_state = got->br_state;
4747 new.br_startblock = nullstartblock((int)new_indlen);
4748
4749 ++*idx;
4750 xfs_iext_insert(ip, *idx, 1, &new, state);
4751
4752 da_new = got_indlen + new_indlen - stolen;
4753 del->br_blockcount -= stolen;
4754 break;
4755 }
4756
4757 ASSERT(da_old >= da_new);
4758 da_diff = da_old - da_new;
4759 if (!isrt)
4760 da_diff += del->br_blockcount;
4761 if (da_diff)
4762 xfs_mod_fdblocks(mp, da_diff, false);
4763 return error;
4764}
4765
4766void
4767xfs_bmap_del_extent_cow(
4768 struct xfs_inode *ip,
4769 xfs_extnum_t *idx,
4770 struct xfs_bmbt_irec *got,
4771 struct xfs_bmbt_irec *del)
4772{
4773 struct xfs_mount *mp = ip->i_mount;
4774 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4775 struct xfs_bmbt_irec new;
4776 xfs_fileoff_t del_endoff, got_endoff;
4777 int state = BMAP_COWFORK;
4778
4779 XFS_STATS_INC(mp, xs_del_exlist);
4780
4781 del_endoff = del->br_startoff + del->br_blockcount;
4782 got_endoff = got->br_startoff + got->br_blockcount;
4783
4784 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004785 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004786 ASSERT(del->br_blockcount > 0);
4787 ASSERT(got->br_startoff <= del->br_startoff);
4788 ASSERT(got_endoff >= del_endoff);
4789 ASSERT(!isnullstartblock(got->br_startblock));
4790
4791 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004792 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004793 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004794 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004795
Christoph Hellwig0173c682017-10-17 14:16:22 -07004796 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4797 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004798 /*
4799 * Matches the whole extent. Delete the entry.
4800 */
4801 xfs_iext_remove(ip, *idx, 1, state);
4802 --*idx;
4803 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004804 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004805 /*
4806 * Deleting the first part of the extent.
4807 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004808 got->br_startoff = del_endoff;
4809 got->br_blockcount -= del->br_blockcount;
4810 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004811 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004812 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004813 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004814 /*
4815 * Deleting the last part of the extent.
4816 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004817 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004818 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004819 break;
4820 case 0:
4821 /*
4822 * Deleting the middle of the extent.
4823 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004824 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004825 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004826
4827 new.br_startoff = del_endoff;
4828 new.br_blockcount = got_endoff - del_endoff;
4829 new.br_state = got->br_state;
4830 new.br_startblock = del->br_startblock + del->br_blockcount;
4831
4832 ++*idx;
4833 xfs_iext_insert(ip, *idx, 1, &new, state);
4834 break;
4835 }
4836}
4837
Brian Fostera9bd24a2016-03-15 11:42:46 +11004838/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004839 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004840 * after removing space.
Dave Chinner9e5987a2013-02-25 12:31:26 +11004841 */
4842STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004843xfs_bmap_del_extent_real(
Dave Chinner9e5987a2013-02-25 12:31:26 +11004844 xfs_inode_t *ip, /* incore inode pointer */
4845 xfs_trans_t *tp, /* current transaction pointer */
4846 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004847 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004848 xfs_btree_cur_t *cur, /* if null, not a btree */
4849 xfs_bmbt_irec_t *del, /* data to remove from extents */
4850 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004851 int whichfork, /* data or attr fork */
4852 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004853{
Dave Chinner9e5987a2013-02-25 12:31:26 +11004854 xfs_fsblock_t del_endblock=0; /* first block past del */
4855 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004856 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004857 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004858 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004859 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004860 xfs_fileoff_t got_endoff; /* first offset past got */
4861 int i; /* temp state */
4862 xfs_ifork_t *ifp; /* inode fork pointer */
4863 xfs_mount_t *mp; /* mount structure */
4864 xfs_filblks_t nblks; /* quota/sb block count */
4865 xfs_bmbt_irec_t new; /* new record to be inserted */
4866 /* REFERENCED */
4867 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004868 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004869 struct xfs_bmbt_irec old;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004870
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004871 mp = ip->i_mount;
4872 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004873
Dave Chinner9e5987a2013-02-25 12:31:26 +11004874 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11004875 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004876 ASSERT(del->br_blockcount > 0);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004877 xfs_iext_get_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004878 ASSERT(got.br_startoff <= del->br_startoff);
4879 del_endoff = del->br_startoff + del->br_blockcount;
4880 got_endoff = got.br_startoff + got.br_blockcount;
4881 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004882 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004883 qfield = 0;
4884 error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004885
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004886 /*
4887 * If it's the case where the directory code is running with no block
4888 * reservation, and the deleted block is in the middle of its extent,
4889 * and the resulting insert of an extent would cause transformation to
4890 * btree format, then reject it. The calling code will then swap blocks
4891 * around instead. We have to do this now, rather than waiting for the
4892 * conversion to btree format, since the transaction will be dirty then.
4893 */
4894 if (tp->t_blk_res == 0 &&
4895 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4896 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4897 XFS_IFORK_MAXEXT(ip, whichfork) &&
4898 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4899 return -ENOSPC;
4900
4901 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004902 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4903 xfs_fsblock_t bno;
4904 xfs_filblks_t len;
4905
4906 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
4907 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
4908 bno = del->br_startblock;
4909 len = del->br_blockcount;
4910 do_div(bno, mp->m_sb.sb_rextsize);
4911 do_div(len, mp->m_sb.sb_rextsize);
4912 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4913 if (error)
4914 goto done;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004915 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004916 nblks = len * mp->m_sb.sb_rextsize;
4917 qfield = XFS_TRANS_DQ_RTBCOUNT;
4918 } else {
4919 do_fx = 1;
4920 nblks = del->br_blockcount;
4921 qfield = XFS_TRANS_DQ_BCOUNT;
4922 }
4923
4924 del_endblock = del->br_startblock + del->br_blockcount;
4925 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07004926 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004927 if (error)
4928 goto done;
4929 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004930 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004931
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004932 if (got.br_startoff == del->br_startoff)
4933 state |= BMAP_LEFT_FILLING;
4934 if (got_endoff == del_endoff)
4935 state |= BMAP_RIGHT_FILLING;
4936
4937 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4938 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004939 /*
4940 * Matches the whole extent. Delete the entry.
4941 */
Christoph Hellwig35e62da2017-10-19 11:04:43 -07004942 xfs_iext_remove(ip, *idx, 1, state);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004943 --*idx;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004944
4945 XFS_IFORK_NEXT_SET(ip, whichfork,
4946 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4947 flags |= XFS_ILOG_CORE;
4948 if (!cur) {
4949 flags |= xfs_ilog_fext(whichfork);
4950 break;
4951 }
4952 if ((error = xfs_btree_delete(cur, &i)))
4953 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004954 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004955 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004956 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004957 /*
4958 * Deleting the first part of the extent.
4959 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004960 got.br_startoff = del_endoff;
4961 got.br_startblock = del_endblock;
4962 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004963 xfs_iext_update_extent(ip, state, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004964 if (!cur) {
4965 flags |= xfs_ilog_fext(whichfork);
4966 break;
4967 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004968 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004969 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004970 goto done;
4971 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004972 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004973 /*
4974 * Deleting the last part of the extent.
4975 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004976 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004977 xfs_iext_update_extent(ip, state, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004978 if (!cur) {
4979 flags |= xfs_ilog_fext(whichfork);
4980 break;
4981 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004982 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004983 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004984 goto done;
4985 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004986 case 0:
4987 /*
4988 * Deleting the middle of the extent.
4989 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004990 old = got;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004991
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004992 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004993 xfs_iext_update_extent(ip, state, *idx, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004994
Dave Chinner9e5987a2013-02-25 12:31:26 +11004995 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004996 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004997 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004998 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004999
Christoph Hellwige1d75532017-10-17 14:16:21 -07005000 flags |= XFS_ILOG_CORE;
5001 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005002 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005003 if (error)
5004 goto done;
5005 error = xfs_btree_increment(cur, 0, &i);
5006 if (error)
5007 goto done;
5008 cur->bc_rec.b = new;
5009 error = xfs_btree_insert(cur, &i);
5010 if (error && error != -ENOSPC)
5011 goto done;
5012 /*
5013 * If get no-space back from btree insert, it tried a
5014 * split, and we have a zero block reservation. Fix up
5015 * our state and return the error.
5016 */
5017 if (error == -ENOSPC) {
5018 /*
5019 * Reset the cursor, don't trust it after any
5020 * insert operation.
5021 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005022 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005023 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005024 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005025 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5026 /*
5027 * Update the btree record back
5028 * to the original value.
5029 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005030 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005031 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005032 goto done;
5033 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005034 * Reset the extent record back
5035 * to the original value.
Dave Chinner9e5987a2013-02-25 12:31:26 +11005036 */
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005037 xfs_iext_update_extent(ip, state, *idx, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005038 flags = 0;
5039 error = -ENOSPC;
5040 goto done;
5041 }
5042 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5043 } else
5044 flags |= xfs_ilog_fext(whichfork);
5045 XFS_IFORK_NEXT_SET(ip, whichfork,
5046 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005047 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5048 ++*idx;
5049 break;
5050 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005051
5052 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005053 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5054 if (error)
5055 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005056
Dave Chinner9e5987a2013-02-25 12:31:26 +11005057 /*
5058 * If we need to, add to list of extents to delete.
5059 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005060 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005061 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5062 error = xfs_refcount_decrease_extent(mp, dfops, del);
5063 if (error)
5064 goto done;
5065 } else
5066 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5067 del->br_blockcount, NULL);
5068 }
5069
Dave Chinner9e5987a2013-02-25 12:31:26 +11005070 /*
5071 * Adjust inode # blocks in the file.
5072 */
5073 if (nblks)
5074 ip->i_d.di_nblocks -= nblks;
5075 /*
5076 * Adjust quota data.
5077 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005078 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005079 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5080
Dave Chinner9e5987a2013-02-25 12:31:26 +11005081done:
5082 *logflagsp = flags;
5083 return error;
5084}
5085
5086/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 * Unmap (remove) blocks from a file.
5088 * If nexts is nonzero then the number of extents to remove is limited to
5089 * that value. If not all extents in the block range can be removed then
5090 * *done is set.
5091 */
5092int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005093__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 xfs_trans_t *tp, /* transaction pointer */
5095 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005096 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005097 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 int flags, /* misc flags */
5099 xfs_extnum_t nexts, /* number of extents max */
5100 xfs_fsblock_t *firstblock, /* first allocated block
5101 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005102 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103{
5104 xfs_btree_cur_t *cur; /* bmap btree cursor */
5105 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106 int error; /* error return value */
5107 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005108 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109 xfs_ifork_t *ifp; /* inode fork pointer */
5110 int isrt; /* freeing in rt area */
5111 xfs_extnum_t lastx; /* last extent index used */
5112 int logflags; /* transaction logging flags */
5113 xfs_extlen_t mod; /* rt extent offset */
5114 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115 int tmp_logflags; /* partial logging flags */
5116 int wasdel; /* was a delayed alloc extent */
5117 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005119 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005120 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005121 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005122 xfs_fileoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005124 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005125
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005126 whichfork = xfs_bmapi_whichfork(flags);
5127 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 ifp = XFS_IFORK_PTR(ip, whichfork);
5129 if (unlikely(
5130 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5131 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5132 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5133 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005134 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 }
5136 mp = ip->i_mount;
5137 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005138 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005139
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005140 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 ASSERT(len > 0);
5142 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005143
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005144 /*
5145 * Guesstimate how many blocks we can unmap without running the risk of
5146 * blowing out the transaction with a mix of EFIs and reflink
5147 * adjustments.
5148 */
5149 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5150 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5151 else
5152 max_len = len;
5153
Linus Torvalds1da177e2005-04-16 15:20:36 -07005154 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5155 (error = xfs_iread_extents(tp, ip, whichfork)))
5156 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005157 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005158 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005159 return 0;
5160 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005161 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005162 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005163 end = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005164
Linus Torvalds1da177e2005-04-16 15:20:36 -07005165 /*
5166 * Check to see if the given block number is past the end of the
5167 * file, back up to the last block if so...
5168 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005169 if (!xfs_iext_lookup_extent(ip, ifp, end, &lastx, &got)) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005170 ASSERT(lastx > 0);
5171 xfs_iext_get_extent(ifp, --lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005172 end = got.br_startoff + got.br_blockcount - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005174
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 logflags = 0;
5176 if (ifp->if_flags & XFS_IFBROOT) {
5177 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005178 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005179 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005180 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005181 cur->bc_private.b.flags = 0;
5182 } else
5183 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005184
5185 if (isrt) {
5186 /*
5187 * Synchronize by locking the bitmap inode.
5188 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005189 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005190 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005191 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5192 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005193 }
5194
Linus Torvalds1da177e2005-04-16 15:20:36 -07005195 extno = 0;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005196 while (end != (xfs_fileoff_t)-1 && end >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005197 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005199 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200 * Just back up to the previous extent, if so.
5201 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005202 if (got.br_startoff > end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 if (--lastx < 0)
5204 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005205 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005206 }
5207 /*
5208 * Is the last block of this extent before the range
5209 * we're supposed to delete? If so, we're done.
5210 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005211 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005212 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005213 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214 break;
5215 /*
5216 * Then deal with the (possibly delayed) allocated space
5217 * we found.
5218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005220 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005221
5222 /*
5223 * Make sure we don't touch multiple AGF headers out of order
5224 * in a single transaction, as that could cause AB-BA deadlocks.
5225 */
5226 if (!wasdel) {
5227 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5228 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5229 break;
5230 prev_agno = agno;
5231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232 if (got.br_startoff < start) {
5233 del.br_startoff = start;
5234 del.br_blockcount -= start - got.br_startoff;
5235 if (!wasdel)
5236 del.br_startblock += start - got.br_startoff;
5237 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005238 if (del.br_startoff + del.br_blockcount > end + 1)
5239 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005240
5241 /* How much can we safely unmap? */
5242 if (max_len < del.br_blockcount) {
5243 del.br_startoff += del.br_blockcount - max_len;
5244 if (!wasdel)
5245 del.br_startblock += del.br_blockcount - max_len;
5246 del.br_blockcount = max_len;
5247 }
5248
Linus Torvalds1da177e2005-04-16 15:20:36 -07005249 sum = del.br_startblock + del.br_blockcount;
5250 if (isrt &&
5251 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5252 /*
5253 * Realtime extent not lined up at the end.
5254 * The extent could have been split into written
5255 * and unwritten pieces, or we could just be
5256 * unmapping part of it. But we can't really
5257 * get rid of part of a realtime extent.
5258 */
5259 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005260 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 /*
5262 * This piece is unwritten, or we're not
5263 * using unwritten extents. Skip over it.
5264 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005265 ASSERT(end >= mod);
5266 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005267 del.br_blockcount : mod;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005268 if (end < got.br_startoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005269 if (--lastx >= 0)
Christoph Hellwige3f0f752017-10-17 14:16:20 -07005270 xfs_iext_get_extent(ifp, lastx,
5271 &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005272 }
5273 continue;
5274 }
5275 /*
5276 * It's written, turn it unwritten.
5277 * This is better than zeroing it.
5278 */
5279 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005280 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 /*
5282 * If this spans a realtime extent boundary,
5283 * chop it back to the start of the one we end at.
5284 */
5285 if (del.br_blockcount > mod) {
5286 del.br_startoff += del.br_blockcount - mod;
5287 del.br_startblock += del.br_blockcount - mod;
5288 del.br_blockcount = mod;
5289 }
5290 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005291 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005292 whichfork, &lastx, &cur, &del,
5293 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005294 if (error)
5295 goto error0;
5296 goto nodelete;
5297 }
5298 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5299 /*
5300 * Realtime extent is lined up at the end but not
5301 * at the front. We'll get rid of full extents if
5302 * we can.
5303 */
5304 mod = mp->m_sb.sb_rextsize - mod;
5305 if (del.br_blockcount > mod) {
5306 del.br_blockcount -= mod;
5307 del.br_startoff += mod;
5308 del.br_startblock += mod;
5309 } else if ((del.br_startoff == start &&
5310 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005311 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005312 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005313 /*
5314 * Can't make it unwritten. There isn't
5315 * a full extent here so just skip it.
5316 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005317 ASSERT(end >= del.br_blockcount);
5318 end -= del.br_blockcount;
5319 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005320 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 continue;
5322 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005323 struct xfs_bmbt_irec prev;
5324
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 /*
5326 * This one is already unwritten.
5327 * It must have a written left neighbor.
5328 * Unwrite the killed part of that one and
5329 * try again.
5330 */
5331 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005332 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005334 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335 ASSERT(del.br_startblock ==
5336 prev.br_startblock + prev.br_blockcount);
5337 if (prev.br_startoff < start) {
5338 mod = start - prev.br_startoff;
5339 prev.br_blockcount -= mod;
5340 prev.br_startblock += mod;
5341 prev.br_startoff = start;
5342 }
5343 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005344 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005345 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005346 ip, whichfork, &lastx, &cur,
5347 &prev, firstblock, dfops,
5348 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005349 if (error)
5350 goto error0;
5351 goto nodelete;
5352 } else {
5353 ASSERT(del.br_state == XFS_EXT_NORM);
5354 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005355 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005356 ip, whichfork, &lastx, &cur,
5357 &del, firstblock, dfops,
5358 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 if (error)
5360 goto error0;
5361 goto nodelete;
5362 }
5363 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005364
Brian Fosterb2706a02016-03-15 11:42:46 +11005365 if (wasdel) {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005366 error = xfs_bmap_del_extent_delay(ip, whichfork, &lastx,
5367 &got, &del);
5368 } else {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005369 error = xfs_bmap_del_extent_real(ip, tp, &lastx, dfops,
5370 cur, &del, &tmp_logflags, whichfork,
5371 flags);
5372 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005373 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005374
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375 if (error)
5376 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005377
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005378 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005379 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381 /*
5382 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005384 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005385 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005386 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005387 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005388 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 extno++;
5391 }
5392 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005393 if (end == (xfs_fileoff_t)-1 || end < start || lastx < 0)
Darrick J. Wong44535932016-10-03 09:11:29 -07005394 *rlen = 0;
5395 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005396 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005397
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 /*
5399 * Convert to a btree if necessary.
5400 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005401 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005403 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005404 &cur, 0, &tmp_logflags, whichfork);
5405 logflags |= tmp_logflags;
5406 if (error)
5407 goto error0;
5408 }
5409 /*
5410 * transform from btree to extents, give it cur
5411 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005412 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413 ASSERT(cur != NULL);
5414 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5415 whichfork);
5416 logflags |= tmp_logflags;
5417 if (error)
5418 goto error0;
5419 }
5420 /*
5421 * transform from extents to local?
5422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 error = 0;
5424error0:
5425 /*
5426 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005427 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005428 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005429 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005430 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005431 logflags &= ~xfs_ilog_fext(whichfork);
5432 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005434 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005435 /*
5436 * Log inode even in the error case, if the transaction
5437 * is dirty we'll need to shut down the filesystem.
5438 */
5439 if (logflags)
5440 xfs_trans_log_inode(tp, ip, logflags);
5441 if (cur) {
5442 if (!error) {
5443 *firstblock = cur->bc_private.b.firstblock;
5444 cur->bc_private.b.allocated = 0;
5445 }
5446 xfs_btree_del_cursor(cur,
5447 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5448 }
5449 return error;
5450}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005451
Darrick J. Wong44535932016-10-03 09:11:29 -07005452/* Unmap a range of a file. */
5453int
5454xfs_bunmapi(
5455 xfs_trans_t *tp,
5456 struct xfs_inode *ip,
5457 xfs_fileoff_t bno,
5458 xfs_filblks_t len,
5459 int flags,
5460 xfs_extnum_t nexts,
5461 xfs_fsblock_t *firstblock,
5462 struct xfs_defer_ops *dfops,
5463 int *done)
5464{
5465 int error;
5466
5467 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5468 dfops);
5469 *done = (len == 0);
5470 return error;
5471}
5472
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005473/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005474 * Determine whether an extent shift can be accomplished by a merge with the
5475 * extent that precedes the target hole of the shift.
5476 */
5477STATIC bool
5478xfs_bmse_can_merge(
5479 struct xfs_bmbt_irec *left, /* preceding extent */
5480 struct xfs_bmbt_irec *got, /* current extent to shift */
5481 xfs_fileoff_t shift) /* shift fsb */
5482{
5483 xfs_fileoff_t startoff;
5484
5485 startoff = got->br_startoff - shift;
5486
5487 /*
5488 * The extent, once shifted, must be adjacent in-file and on-disk with
5489 * the preceding extent.
5490 */
5491 if ((left->br_startoff + left->br_blockcount != startoff) ||
5492 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5493 (left->br_state != got->br_state) ||
5494 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5495 return false;
5496
5497 return true;
5498}
5499
5500/*
5501 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5502 * hole in the file. If an extent shift would result in the extent being fully
5503 * adjacent to the extent that currently precedes the hole, we can merge with
5504 * the preceding extent rather than do the shift.
5505 *
5506 * This function assumes the caller has verified a shift-by-merge is possible
5507 * with the provided extents via xfs_bmse_can_merge().
5508 */
5509STATIC int
5510xfs_bmse_merge(
5511 struct xfs_inode *ip,
5512 int whichfork,
5513 xfs_fileoff_t shift, /* shift fsb */
5514 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005515 struct xfs_bmbt_irec *got, /* extent to shift */
5516 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005517 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005518 int *logflags, /* output */
5519 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005520{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005521 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005522 xfs_filblks_t blockcount;
5523 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005524 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005525
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005526 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005527
5528 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5529 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005530 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005531
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005532 new = *left;
5533 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005534
5535 /*
5536 * Update the on-disk extent count, the btree if necessary and log the
5537 * inode.
5538 */
5539 XFS_IFORK_NEXT_SET(ip, whichfork,
5540 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5541 *logflags |= XFS_ILOG_CORE;
5542 if (!cur) {
5543 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005544 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005545 }
5546
5547 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005548 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005549 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005550 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005551 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005552
5553 error = xfs_btree_delete(cur, &i);
5554 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005555 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005556 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005557
5558 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005559 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005560 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005561 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005562 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005563
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005564 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005565 if (error)
5566 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005567
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005568done:
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005569 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
5570 current_ext - 1, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005571 xfs_iext_remove(ip, current_ext, 1, 0);
5572
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005573 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005574 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5575 if (error)
5576 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005577 memcpy(&new, got, sizeof(new));
5578 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005579 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005580}
5581
5582/*
Brian Fostera979bdf2014-09-23 15:39:04 +10005583 * Shift a single extent.
5584 */
5585STATIC int
5586xfs_bmse_shift_one(
5587 struct xfs_inode *ip,
5588 int whichfork,
5589 xfs_fileoff_t offset_shift_fsb,
5590 int *current_ext,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005591 struct xfs_bmbt_irec *got,
Brian Fostera979bdf2014-09-23 15:39:04 +10005592 struct xfs_btree_cur *cur,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005593 int *logflags,
Darrick J. Wong9c194642016-08-03 12:16:05 +10005594 enum shift_direction direction,
5595 struct xfs_defer_ops *dfops)
Brian Fostera979bdf2014-09-23 15:39:04 +10005596{
5597 struct xfs_ifork *ifp;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005598 struct xfs_mount *mp;
Brian Fostera979bdf2014-09-23 15:39:04 +10005599 xfs_fileoff_t startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005600 struct xfs_bmbt_irec adj_irec, new;
Brian Fostera979bdf2014-09-23 15:39:04 +10005601 int error;
5602 int i;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005603 int total_extents;
Brian Fostera979bdf2014-09-23 15:39:04 +10005604
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005605 mp = ip->i_mount;
Brian Fostera979bdf2014-09-23 15:39:04 +10005606 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11005607 total_extents = xfs_iext_count(ifp);
Brian Fostera979bdf2014-09-23 15:39:04 +10005608
Brian Fosterf71721d2014-09-23 15:39:05 +10005609 /* delalloc extents should be prevented by caller */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005610 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got->br_startblock));
Brian Fosterf71721d2014-09-23 15:39:05 +10005611
Namjae Jeona904b1c2015-03-25 15:08:56 +11005612 if (direction == SHIFT_LEFT) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005613 startoff = got->br_startoff - offset_shift_fsb;
Dave Chinnerb11bd672014-12-04 09:42:24 +11005614
Namjae Jeona904b1c2015-03-25 15:08:56 +11005615 /*
5616 * Check for merge if we've got an extent to the left,
5617 * otherwise make sure there's enough room at the start
5618 * of the file for the shift.
5619 */
5620 if (!*current_ext) {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005621 if (got->br_startoff < offset_shift_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005622 return -EINVAL;
5623 goto update_current_ext;
5624 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005625
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005626 /*
5627 * grab the left extent and check for a large enough hole.
5628 */
5629 xfs_iext_get_extent(ifp, *current_ext - 1, &adj_irec);
5630 if (startoff < adj_irec.br_startoff + adj_irec.br_blockcount)
Brian Fostera979bdf2014-09-23 15:39:04 +10005631 return -EINVAL;
Brian Fostera979bdf2014-09-23 15:39:04 +10005632
Dave Chinnerb11bd672014-12-04 09:42:24 +11005633 /* check whether to merge the extent or shift it down */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005634 if (xfs_bmse_can_merge(&adj_irec, got, offset_shift_fsb)) {
5635 return xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5636 *current_ext, got, &adj_irec,
5637 cur, logflags, dfops);
Dave Chinnerb11bd672014-12-04 09:42:24 +11005638 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005639 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005640 startoff = got->br_startoff + offset_shift_fsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005641 /* nothing to move if this is the last extent */
5642 if (*current_ext >= (total_extents - 1))
5643 goto update_current_ext;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005644
Namjae Jeona904b1c2015-03-25 15:08:56 +11005645 /*
5646 * If this is not the last extent in the file, make sure there
5647 * is enough room between current extent and next extent for
5648 * accommodating the shift.
5649 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005650 xfs_iext_get_extent(ifp, *current_ext + 1, &adj_irec);
5651 if (startoff + got->br_blockcount > adj_irec.br_startoff)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005652 return -EINVAL;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005653
Namjae Jeona904b1c2015-03-25 15:08:56 +11005654 /*
5655 * Unlike a left shift (which involves a hole punch),
5656 * a right shift does not modify extent neighbors
5657 * in any way. We should never find mergeable extents
5658 * in this scenario. Check anyways and warn if we
5659 * encounter two extents that could be one.
5660 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005661 if (xfs_bmse_can_merge(got, &adj_irec, offset_shift_fsb))
Namjae Jeona904b1c2015-03-25 15:08:56 +11005662 WARN_ON_ONCE(1);
5663 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005664
Brian Fostera979bdf2014-09-23 15:39:04 +10005665 /*
5666 * Increment the extent index for the next iteration, update the start
5667 * offset of the in-core extent and update the btree if applicable.
5668 */
Namjae Jeona904b1c2015-03-25 15:08:56 +11005669update_current_ext:
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005670 *logflags |= XFS_ILOG_CORE;
5671
5672 new = *got;
5673 new.br_startoff = startoff;
5674
5675 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005676 error = xfs_bmbt_lookup_eq(cur, got, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005677 if (error)
5678 return error;
5679 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5680
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005681 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005682 if (error)
5683 return error;
5684 } else {
5685 *logflags |= XFS_ILOG_DEXT;
5686 }
5687
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005688 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
5689 *current_ext, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005690
Namjae Jeona904b1c2015-03-25 15:08:56 +11005691 if (direction == SHIFT_LEFT)
5692 (*current_ext)++;
5693 else
5694 (*current_ext)--;
Brian Fostera979bdf2014-09-23 15:39:04 +10005695
Darrick J. Wong9c194642016-08-03 12:16:05 +10005696 /* update reverse mapping */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005697 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005698 if (error)
5699 return error;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005700 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fostera979bdf2014-09-23 15:39:04 +10005701}
5702
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005703int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005704xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005705 struct xfs_trans *tp,
5706 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005707 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005708 xfs_fileoff_t offset_shift_fsb,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005709 bool *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005710 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005711 xfs_fsblock_t *firstblock,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005712 struct xfs_defer_ops *dfops)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005713{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005714 int whichfork = XFS_DATA_FORK;
5715 struct xfs_mount *mp = ip->i_mount;
5716 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5717 struct xfs_btree_cur *cur = NULL;
5718 struct xfs_bmbt_irec got;
5719 xfs_extnum_t current_ext;
5720 xfs_extnum_t total_extents;
5721 xfs_extnum_t stop_extent;
5722 int error = 0;
5723 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005724
5725 if (unlikely(XFS_TEST_ERROR(
5726 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5727 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005728 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005729 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005730 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005731 }
5732
5733 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005734 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005735
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005736 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005737
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005738 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005739 error = xfs_iread_extents(tp, ip, whichfork);
5740 if (error)
5741 return error;
5742 }
5743
Brian Fosterddb19e32014-09-23 15:38:09 +10005744 if (ifp->if_flags & XFS_IFBROOT) {
5745 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5746 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005747 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005748 cur->bc_private.b.flags = 0;
5749 }
5750
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005751 /*
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005752 * There may be delalloc extents in the data fork before the range we
Brian Foster2c845f52014-09-23 15:37:09 +10005753 * are collapsing out, so we cannot use the count of real extents here.
5754 * Instead we have to calculate it from the incore fork.
Dave Chinnerd39a2ce2014-04-17 08:15:25 +10005755 */
Eric Sandeen5d829302016-11-08 12:59:42 +11005756 total_extents = xfs_iext_count(ifp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005757 if (total_extents == 0) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005758 *done = true;
5759 goto del_cursor;
5760 }
5761
5762 /*
5763 * Look up the extent index for the fsb where we start shifting. We can
5764 * henceforth iterate with current_ext as extent list changes are locked
5765 * out via ilock.
5766 *
5767 * If next_fsb lies in a hole beyond which there are no extents we are
5768 * done.
5769 */
5770 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext, &got)) {
5771 *done = true;
5772 goto del_cursor;
5773 }
5774
5775 stop_extent = total_extents;
5776 if (current_ext >= stop_extent) {
5777 error = -EIO;
5778 goto del_cursor;
5779 }
5780
5781 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
5782 &current_ext, &got, cur, &logflags,
5783 SHIFT_LEFT, dfops);
5784 if (error)
5785 goto del_cursor;
5786 /*
5787 * If there was an extent merge during the shift, the extent
5788 * count can change. Update the total and grade the next record.
5789 */
5790 total_extents = xfs_iext_count(ifp);
5791 stop_extent = total_extents;
5792 if (current_ext == stop_extent) {
5793 *done = true;
5794 goto del_cursor;
5795 }
5796 xfs_iext_get_extent(ifp, current_ext, &got);
5797
5798 if (!*done)
5799 *next_fsb = got.br_startoff;
5800
5801del_cursor:
5802 if (cur)
5803 xfs_btree_del_cursor(cur,
5804 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5805
5806 if (logflags)
5807 xfs_trans_log_inode(tp, ip, logflags);
5808
5809 return error;
5810}
5811
5812int
5813xfs_bmap_insert_extents(
5814 struct xfs_trans *tp,
5815 struct xfs_inode *ip,
5816 xfs_fileoff_t *next_fsb,
5817 xfs_fileoff_t offset_shift_fsb,
5818 bool *done,
5819 xfs_fileoff_t stop_fsb,
5820 xfs_fsblock_t *firstblock,
5821 struct xfs_defer_ops *dfops)
5822{
5823 int whichfork = XFS_DATA_FORK;
5824 struct xfs_mount *mp = ip->i_mount;
5825 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5826 struct xfs_btree_cur *cur = NULL;
5827 struct xfs_bmbt_irec got, s;
5828 xfs_extnum_t current_ext;
5829 xfs_extnum_t total_extents;
5830 xfs_extnum_t stop_extent;
5831 int error = 0;
5832 int logflags = 0;
5833
5834 if (unlikely(XFS_TEST_ERROR(
5835 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5836 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5837 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5838 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5839 return -EFSCORRUPTED;
5840 }
5841
5842 if (XFS_FORCED_SHUTDOWN(mp))
5843 return -EIO;
5844
5845 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5846
5847 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5848 error = xfs_iread_extents(tp, ip, whichfork);
5849 if (error)
5850 return error;
5851 }
5852
5853 if (ifp->if_flags & XFS_IFBROOT) {
5854 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5855 cur->bc_private.b.firstblock = *firstblock;
5856 cur->bc_private.b.dfops = dfops;
5857 cur->bc_private.b.flags = 0;
5858 }
5859
5860 /*
5861 * There may be delalloc extents in the data fork before the range we
5862 * are collapsing out, so we cannot use the count of real extents here.
5863 * Instead we have to calculate it from the incore fork.
5864 */
5865 total_extents = xfs_iext_count(ifp);
5866 if (total_extents == 0) {
5867 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005868 goto del_cursor;
5869 }
5870
5871 /*
5872 * In case of first right shift, we need to initialize next_fsb
5873 */
5874 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005875 current_ext = total_extents - 1;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005876 xfs_iext_get_extent(ifp, current_ext, &got);
5877 if (stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005878 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005879 goto del_cursor;
5880 }
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005881 *next_fsb = got.br_startoff;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005882 } else {
5883 /*
5884 * Look up the extent index for the fsb where we start shifting. We can
5885 * henceforth iterate with current_ext as extent list changes are locked
5886 * out via ilock.
5887 *
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005888 * If next_fsb lies in a hole beyond which there are no extents we are
5889 * done.
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005890 */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005891 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
5892 &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005893 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005894 goto del_cursor;
5895 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005896 }
5897
5898 /* Lookup the extent index at which we have to stop */
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005899 xfs_iext_lookup_extent(ip, ifp, stop_fsb, &stop_extent, &s);
5900 /* Make stop_extent exclusive of shift range */
5901 stop_extent--;
5902 if (current_ext <= stop_extent) {
5903 error = -EIO;
5904 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005905 }
5906
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005907 error = xfs_bmse_shift_one(ip, whichfork, offset_shift_fsb,
5908 &current_ext, &got, cur, &logflags,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005909 SHIFT_RIGHT, dfops);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005910 if (error)
5911 goto del_cursor;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005912 if (current_ext == stop_extent) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005913 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005914 goto del_cursor;
5915 }
5916 xfs_iext_get_extent(ifp, current_ext, &got);
5917
5918 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005919
5920del_cursor:
5921 if (cur)
5922 xfs_btree_del_cursor(cur,
5923 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5924
Brian Fosterca446d82014-09-02 12:12:53 +10005925 if (logflags)
5926 xfs_trans_log_inode(tp, ip, logflags);
Brian Foster2c845f52014-09-23 15:37:09 +10005927
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005928 return error;
5929}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005930
5931/*
5932 * Splits an extent into two extents at split_fsb block such that it is
5933 * the first block of the current_ext. @current_ext is a target extent
5934 * to be split. @split_fsb is a block where the extents is split.
5935 * If split_fsb lies in a hole or the first block of extents, just return 0.
5936 */
5937STATIC int
5938xfs_bmap_split_extent_at(
5939 struct xfs_trans *tp,
5940 struct xfs_inode *ip,
5941 xfs_fileoff_t split_fsb,
5942 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005943 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005944{
5945 int whichfork = XFS_DATA_FORK;
5946 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005947 struct xfs_bmbt_irec got;
5948 struct xfs_bmbt_irec new; /* split extent */
5949 struct xfs_mount *mp = ip->i_mount;
5950 struct xfs_ifork *ifp;
5951 xfs_fsblock_t gotblkcnt; /* new block count for got */
5952 xfs_extnum_t current_ext;
5953 int error = 0;
5954 int logflags = 0;
5955 int i = 0;
5956
5957 if (unlikely(XFS_TEST_ERROR(
5958 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5959 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005960 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005961 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5962 XFS_ERRLEVEL_LOW, mp);
5963 return -EFSCORRUPTED;
5964 }
5965
5966 if (XFS_FORCED_SHUTDOWN(mp))
5967 return -EIO;
5968
5969 ifp = XFS_IFORK_PTR(ip, whichfork);
5970 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5971 /* Read in all the extents */
5972 error = xfs_iread_extents(tp, ip, whichfork);
5973 if (error)
5974 return error;
5975 }
5976
5977 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005978 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005979 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005980 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
5981 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005982 return 0;
5983
5984 gotblkcnt = split_fsb - got.br_startoff;
5985 new.br_startoff = split_fsb;
5986 new.br_startblock = got.br_startblock + gotblkcnt;
5987 new.br_blockcount = got.br_blockcount - gotblkcnt;
5988 new.br_state = got.br_state;
5989
5990 if (ifp->if_flags & XFS_IFBROOT) {
5991 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5992 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005993 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005994 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005995 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005996 if (error)
5997 goto del_cursor;
5998 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5999 }
6000
Namjae Jeona904b1c2015-03-25 15:08:56 +11006001 got.br_blockcount = gotblkcnt;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07006002 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
6003 current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006004
6005 logflags = XFS_ILOG_CORE;
6006 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07006007 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006008 if (error)
6009 goto del_cursor;
6010 } else
6011 logflags |= XFS_ILOG_DEXT;
6012
6013 /* Add new extent */
6014 current_ext++;
6015 xfs_iext_insert(ip, current_ext, 1, &new, 0);
6016 XFS_IFORK_NEXT_SET(ip, whichfork,
6017 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
6018
6019 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07006020 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006021 if (error)
6022 goto del_cursor;
6023 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006024 error = xfs_btree_insert(cur, &i);
6025 if (error)
6026 goto del_cursor;
6027 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
6028 }
6029
6030 /*
6031 * Convert to a btree if necessary.
6032 */
6033 if (xfs_bmap_needs_btree(ip, whichfork)) {
6034 int tmp_logflags; /* partial log flag return val */
6035
6036 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006037 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11006038 &cur, 0, &tmp_logflags, whichfork);
6039 logflags |= tmp_logflags;
6040 }
6041
6042del_cursor:
6043 if (cur) {
6044 cur->bc_private.b.allocated = 0;
6045 xfs_btree_del_cursor(cur,
6046 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
6047 }
6048
6049 if (logflags)
6050 xfs_trans_log_inode(tp, ip, logflags);
6051 return error;
6052}
6053
6054int
6055xfs_bmap_split_extent(
6056 struct xfs_inode *ip,
6057 xfs_fileoff_t split_fsb)
6058{
6059 struct xfs_mount *mp = ip->i_mount;
6060 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006061 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006062 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006063 int error;
6064
Christoph Hellwig253f4912016-04-06 09:19:55 +10006065 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
6066 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
6067 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11006068 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11006069
6070 xfs_ilock(ip, XFS_ILOCK_EXCL);
6071 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
6072
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006073 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006074
6075 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006076 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006077 if (error)
6078 goto out;
6079
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07006080 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006081 if (error)
6082 goto out;
6083
Christoph Hellwig70393312015-06-04 13:48:08 +10006084 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006085
6086out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10006087 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10006088 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11006089 return error;
6090}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006091
6092/* Deferred mapping is only for real extents in the data fork. */
6093static bool
6094xfs_bmap_is_update_needed(
6095 struct xfs_bmbt_irec *bmap)
6096{
6097 return bmap->br_startblock != HOLESTARTBLOCK &&
6098 bmap->br_startblock != DELAYSTARTBLOCK;
6099}
6100
6101/* Record a bmap intent. */
6102static int
6103__xfs_bmap_add(
6104 struct xfs_mount *mp,
6105 struct xfs_defer_ops *dfops,
6106 enum xfs_bmap_intent_type type,
6107 struct xfs_inode *ip,
6108 int whichfork,
6109 struct xfs_bmbt_irec *bmap)
6110{
6111 int error;
6112 struct xfs_bmap_intent *bi;
6113
6114 trace_xfs_bmap_defer(mp,
6115 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6116 type,
6117 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6118 ip->i_ino, whichfork,
6119 bmap->br_startoff,
6120 bmap->br_blockcount,
6121 bmap->br_state);
6122
6123 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6124 INIT_LIST_HEAD(&bi->bi_list);
6125 bi->bi_type = type;
6126 bi->bi_owner = ip;
6127 bi->bi_whichfork = whichfork;
6128 bi->bi_bmap = *bmap;
6129
Christoph Hellwig882d8782017-08-28 10:21:03 -07006130 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006131 if (error) {
6132 kmem_free(bi);
6133 return error;
6134 }
6135
6136 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6137 return 0;
6138}
6139
6140/* Map an extent into a file. */
6141int
6142xfs_bmap_map_extent(
6143 struct xfs_mount *mp,
6144 struct xfs_defer_ops *dfops,
6145 struct xfs_inode *ip,
6146 struct xfs_bmbt_irec *PREV)
6147{
6148 if (!xfs_bmap_is_update_needed(PREV))
6149 return 0;
6150
6151 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6152 XFS_DATA_FORK, PREV);
6153}
6154
6155/* Unmap an extent out of a file. */
6156int
6157xfs_bmap_unmap_extent(
6158 struct xfs_mount *mp,
6159 struct xfs_defer_ops *dfops,
6160 struct xfs_inode *ip,
6161 struct xfs_bmbt_irec *PREV)
6162{
6163 if (!xfs_bmap_is_update_needed(PREV))
6164 return 0;
6165
6166 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6167 XFS_DATA_FORK, PREV);
6168}
6169
6170/*
6171 * Process one of the deferred bmap operations. We pass back the
6172 * btree cursor to maintain our lock on the bmapbt between calls.
6173 */
6174int
6175xfs_bmap_finish_one(
6176 struct xfs_trans *tp,
6177 struct xfs_defer_ops *dfops,
6178 struct xfs_inode *ip,
6179 enum xfs_bmap_intent_type type,
6180 int whichfork,
6181 xfs_fileoff_t startoff,
6182 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006183 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006184 xfs_exntst_t state)
6185{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006186 xfs_fsblock_t firstfsb;
6187 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006188
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006189 /*
6190 * firstfsb is tied to the transaction lifetime and is used to
6191 * ensure correct AG locking order and schedule work item
6192 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6193 * to only making one bmap call per transaction, so it should
6194 * be safe to have it as a local variable here.
6195 */
6196 firstfsb = NULLFSBLOCK;
6197
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006198 trace_xfs_bmap_deferred(tp->t_mountp,
6199 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6200 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006201 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006202
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006203 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006204 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006205
6206 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006207 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006208 return -EIO;
6209
6210 switch (type) {
6211 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006212 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006213 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006214 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006215 break;
6216 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006217 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6218 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006219 break;
6220 default:
6221 ASSERT(0);
6222 error = -EFSCORRUPTED;
6223 }
6224
6225 return error;
6226}