blob: bf23de8df4c4c71960e6ae944e7e308a28bfbc25 [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/*
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001288 * Returns the relative block number of the first unused block(s) in the given
1289 * fork with at least "len" logically contiguous blocks free. This is the
1290 * lowest-address hole if the fork has holes, else the first block past the end
1291 * of fork. Return 0 if the fork is currently local (in-inode).
Dave Chinner9e5987a2013-02-25 12:31:26 +11001292 */
1293int /* error */
1294xfs_bmap_first_unused(
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001295 struct xfs_trans *tp, /* transaction pointer */
1296 struct xfs_inode *ip, /* incore inode */
1297 xfs_extlen_t len, /* size of hole to find */
1298 xfs_fileoff_t *first_unused, /* unused block */
1299 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001300{
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001301 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1302 struct xfs_bmbt_irec got;
1303 xfs_extnum_t idx = 0;
1304 xfs_fileoff_t lastaddr = 0;
1305 xfs_fileoff_t lowest, max;
1306 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001307
1308 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
1309 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
1310 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001311
Dave Chinner9e5987a2013-02-25 12:31:26 +11001312 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
1313 *first_unused = 0;
1314 return 0;
1315 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001316
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001317 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1318 error = xfs_iread_extents(tp, ip, whichfork);
1319 if (error)
1320 return error;
1321 }
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001322
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001323 lowest = max = *first_unused;
1324 while (xfs_iext_get_extent(ifp, idx++, &got)) {
Dave Chinner9e5987a2013-02-25 12:31:26 +11001325 /*
1326 * See if the hole before this extent will work.
1327 */
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001328 if (got.br_startoff >= lowest + len &&
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001329 got.br_startoff - max >= len)
1330 break;
Christoph Hellwigf2285c12017-08-29 15:44:12 -07001331 lastaddr = got.br_startoff + got.br_blockcount;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001332 max = XFS_FILEOFF_MAX(lastaddr, lowest);
1333 }
Christoph Hellwig29b3e942017-10-19 11:08:52 -07001334
Dave Chinner9e5987a2013-02-25 12:31:26 +11001335 *first_unused = max;
1336 return 0;
1337}
1338
1339/*
Zhi Yong Wu02bb4872013-08-12 03:14:54 +00001340 * Returns the file-relative block number of the last block - 1 before
Dave Chinner9e5987a2013-02-25 12:31:26 +11001341 * last_block (input value) in the file.
1342 * This is not based on i_size, it is based on the extent records.
1343 * Returns 0 for local files, as they do not have extent records.
1344 */
1345int /* error */
1346xfs_bmap_last_before(
Christoph Hellwig86685f72016-11-24 11:39:38 +11001347 struct xfs_trans *tp, /* transaction pointer */
1348 struct xfs_inode *ip, /* incore inode */
1349 xfs_fileoff_t *last_block, /* last block */
1350 int whichfork) /* data or attr fork */
Dave Chinner9e5987a2013-02-25 12:31:26 +11001351{
Christoph Hellwig86685f72016-11-24 11:39:38 +11001352 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1353 struct xfs_bmbt_irec got;
1354 xfs_extnum_t idx;
1355 int error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001356
Christoph Hellwig86685f72016-11-24 11:39:38 +11001357 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
1358 case XFS_DINODE_FMT_LOCAL:
Dave Chinner9e5987a2013-02-25 12:31:26 +11001359 *last_block = 0;
1360 return 0;
Christoph Hellwig86685f72016-11-24 11:39:38 +11001361 case XFS_DINODE_FMT_BTREE:
1362 case XFS_DINODE_FMT_EXTENTS:
1363 break;
1364 default:
1365 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001366 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001367
1368 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1369 error = xfs_iread_extents(tp, ip, whichfork);
1370 if (error)
1371 return error;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001372 }
Christoph Hellwig86685f72016-11-24 11:39:38 +11001373
1374 if (xfs_iext_lookup_extent(ip, ifp, *last_block - 1, &idx, &got)) {
1375 if (got.br_startoff <= *last_block - 1)
1376 return 0;
1377 }
1378
1379 if (xfs_iext_get_extent(ifp, idx - 1, &got)) {
1380 *last_block = got.br_startoff + got.br_blockcount;
1381 return 0;
1382 }
1383
1384 *last_block = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001385 return 0;
1386}
1387
Dave Chinner68988112013-08-12 20:49:42 +10001388int
Dave Chinner9e5987a2013-02-25 12:31:26 +11001389xfs_bmap_last_extent(
1390 struct xfs_trans *tp,
1391 struct xfs_inode *ip,
1392 int whichfork,
1393 struct xfs_bmbt_irec *rec,
1394 int *is_empty)
1395{
1396 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
1397 int error;
1398 int nextents;
1399
1400 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
1401 error = xfs_iread_extents(tp, ip, whichfork);
1402 if (error)
1403 return error;
1404 }
1405
Eric Sandeen5d829302016-11-08 12:59:42 +11001406 nextents = xfs_iext_count(ifp);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001407 if (nextents == 0) {
1408 *is_empty = 1;
1409 return 0;
1410 }
1411
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001412 xfs_iext_get_extent(ifp, nextents - 1, rec);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001413 *is_empty = 0;
1414 return 0;
1415}
1416
1417/*
1418 * Check the last inode extent to determine whether this allocation will result
1419 * in blocks being allocated at the end of the file. When we allocate new data
1420 * blocks at the end of the file which do not start at the previous data block,
1421 * we will try to align the new blocks at stripe unit boundaries.
1422 *
Dave Chinner6e708bc2013-11-22 10:41:16 +11001423 * Returns 1 in bma->aeof if the file (fork) is empty as any new write will be
Dave Chinner9e5987a2013-02-25 12:31:26 +11001424 * at, or past the EOF.
1425 */
1426STATIC int
1427xfs_bmap_isaeof(
1428 struct xfs_bmalloca *bma,
1429 int whichfork)
1430{
1431 struct xfs_bmbt_irec rec;
1432 int is_empty;
1433 int error;
1434
Thomas Meyer749f24f2017-10-09 11:38:54 -07001435 bma->aeof = false;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001436 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
1437 &is_empty);
Dave Chinner6e708bc2013-11-22 10:41:16 +11001438 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11001439 return error;
1440
Dave Chinner6e708bc2013-11-22 10:41:16 +11001441 if (is_empty) {
Thomas Meyer749f24f2017-10-09 11:38:54 -07001442 bma->aeof = true;
Dave Chinner6e708bc2013-11-22 10:41:16 +11001443 return 0;
1444 }
1445
Dave Chinner9e5987a2013-02-25 12:31:26 +11001446 /*
1447 * Check if we are allocation or past the last extent, or at least into
1448 * the last delayed allocated extent.
1449 */
1450 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
1451 (bma->offset >= rec.br_startoff &&
1452 isnullstartblock(rec.br_startblock));
1453 return 0;
1454}
1455
1456/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11001457 * Returns the file-relative block number of the first block past eof in
1458 * the file. This is not based on i_size, it is based on the extent records.
1459 * Returns 0 for local files, as they do not have extent records.
1460 */
1461int
1462xfs_bmap_last_offset(
Dave Chinner9e5987a2013-02-25 12:31:26 +11001463 struct xfs_inode *ip,
1464 xfs_fileoff_t *last_block,
1465 int whichfork)
1466{
1467 struct xfs_bmbt_irec rec;
1468 int is_empty;
1469 int error;
1470
1471 *last_block = 0;
1472
1473 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
1474 return 0;
1475
1476 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
1477 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Dave Chinner24513372014-06-25 14:58:08 +10001478 return -EIO;
Dave Chinner9e5987a2013-02-25 12:31:26 +11001479
1480 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
1481 if (error || is_empty)
1482 return error;
1483
1484 *last_block = rec.br_startoff + rec.br_blockcount;
1485 return 0;
1486}
1487
1488/*
1489 * Returns whether the selected fork of the inode has exactly one
1490 * block or not. For the data fork we check this matches di_size,
1491 * implying the file's range is 0..bsize-1.
1492 */
1493int /* 1=>1 block, 0=>otherwise */
1494xfs_bmap_one_block(
1495 xfs_inode_t *ip, /* incore inode */
1496 int whichfork) /* data or attr fork */
1497{
Dave Chinner9e5987a2013-02-25 12:31:26 +11001498 xfs_ifork_t *ifp; /* inode fork pointer */
1499 int rval; /* return value */
1500 xfs_bmbt_irec_t s; /* internal version of extent */
1501
1502#ifndef DEBUG
1503 if (whichfork == XFS_DATA_FORK)
1504 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
1505#endif /* !DEBUG */
1506 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
1507 return 0;
1508 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
1509 return 0;
1510 ifp = XFS_IFORK_PTR(ip, whichfork);
1511 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001512 xfs_iext_get_extent(ifp, 0, &s);
Dave Chinner9e5987a2013-02-25 12:31:26 +11001513 rval = s.br_startoff == 0 && s.br_blockcount == 1;
1514 if (rval && whichfork == XFS_DATA_FORK)
1515 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
1516 return rval;
1517}
1518
1519/*
1520 * Extent tree manipulation functions used during allocation.
1521 */
1522
1523/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001524 * Convert a delayed allocation to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 */
1526STATIC int /* error */
1527xfs_bmap_add_extent_delay_real(
Darrick J. Wong60b49842016-10-03 09:11:34 -07001528 struct xfs_bmalloca *bma,
1529 int whichfork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530{
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001531 struct xfs_bmbt_irec *new = &bma->got;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11001534 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 xfs_fileoff_t new_endoff; /* end offset of new entry */
1536 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
1537 /* left is 0, right is 1, prev is 2 */
1538 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07001539 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001540 xfs_filblks_t da_new; /* new count del alloc blocks used */
1541 xfs_filblks_t da_old; /* old count del alloc blocks used */
1542 xfs_filblks_t temp=0; /* value for da_new calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 int tmp_rval; /* partial logging flags */
Eric Sandeenc29aad42015-02-23 22:39:08 +11001544 struct xfs_mount *mp;
Darrick J. Wong60b49842016-10-03 09:11:34 -07001545 xfs_extnum_t *nextents;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001546 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
Eric Sandeenf1f96c42016-01-04 16:10:42 +11001548 mp = bma->ip->i_mount;
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001549 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001550 ASSERT(whichfork != XFS_ATTR_FORK);
1551 nextents = (whichfork == XFS_COW_FORK ? &bma->ip->i_cnextents :
1552 &bma->ip->i_d.di_nextents);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001553
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001554 ASSERT(bma->idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11001555 ASSERT(bma->idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001556 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001557 ASSERT(!bma->cur ||
1558 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001559
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11001560 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562#define LEFT r[0]
1563#define RIGHT r[1]
1564#define PREV r[2]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 /*
1567 * Set up a bunch of variables to make the tests simpler.
1568 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001569 xfs_iext_get_extent(ifp, bma->idx, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 new_endoff = new->br_startoff + new->br_blockcount;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001571 ASSERT(isnullstartblock(PREV.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 ASSERT(PREV.br_startoff <= new->br_startoff);
1573 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001574
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001575 da_old = startblockval(PREV.br_startblock);
1576 da_new = 0;
1577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 /*
1579 * Set flags determining what part of the previous delayed allocation
1580 * extent is being replaced by a real allocation.
1581 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001582 if (PREV.br_startoff == new->br_startoff)
1583 state |= BMAP_LEFT_FILLING;
1584 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1585 state |= BMAP_RIGHT_FILLING;
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /*
1588 * Check and set flags if this segment has a left neighbor.
1589 * Don't set contiguous if the combined extent would be too large.
1590 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001591 if (bma->idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001592 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001593 xfs_iext_get_extent(ifp, bma->idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001594
1595 if (isnullstartblock(LEFT.br_startblock))
1596 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001598
1599 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1600 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1601 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1602 LEFT.br_state == new->br_state &&
1603 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1604 state |= BMAP_LEFT_CONTIG;
1605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 /*
1607 * Check and set flags if this segment has a right neighbor.
1608 * Don't set contiguous if the combined extent would be too large.
1609 * Also check for all-three-contiguous being too large.
1610 */
Eric Sandeen5d829302016-11-08 12:59:42 +11001611 if (bma->idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001612 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07001613 xfs_iext_get_extent(ifp, bma->idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001614
1615 if (isnullstartblock(RIGHT.br_startblock))
1616 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001618
1619 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1620 new_endoff == RIGHT.br_startoff &&
1621 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1622 new->br_state == RIGHT.br_state &&
1623 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1624 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1625 BMAP_RIGHT_FILLING)) !=
1626 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1627 BMAP_RIGHT_FILLING) ||
1628 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1629 <= MAXEXTLEN))
1630 state |= BMAP_RIGHT_CONTIG;
1631
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 error = 0;
1633 /*
1634 * Switch out based on the FILLING and CONTIG state bits.
1635 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001636 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1637 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1638 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1639 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 /*
1641 * Filling in all of a previously delayed allocation extent.
1642 * The left and right neighbors are both contiguous with new.
1643 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001644 bma->idx--;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001645 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001646 xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001647
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001648 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001649 (*nextents)--;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001650 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1652 else {
1653 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001654 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001655 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001657 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001658 error = xfs_btree_delete(bma->cur, &i);
1659 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_decrement(bma->cur, 0, &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 Hellwiga67d00a2017-10-17 14:16:26 -07001666 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001667 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 goto done;
1669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 break;
1671
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001672 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 /*
1674 * Filling in all of a previously delayed allocation extent.
1675 * The left neighbor is contiguous, the right is not.
1676 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001677 bma->idx--;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001678
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001679 old = LEFT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001680 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001681 xfs_iext_update_extent(bma->ip, state, bma->idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001682
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001683 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1684 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 rval = XFS_ILOG_DEXT;
1686 else {
1687 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001688 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001689 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001691 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001692 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001693 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 goto done;
1695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 break;
1697
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001698 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /*
1700 * Filling in all of a previously delayed allocation extent.
1701 * The right neighbor is contiguous, the left is not.
1702 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001703 PREV.br_startblock = new->br_startblock;
1704 PREV.br_blockcount += RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001705 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001706
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001707 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1708 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 rval = XFS_ILOG_DEXT;
1710 else {
1711 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001712 error = xfs_bmbt_lookup_eq(bma->cur, &RIGHT, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001713 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001715 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001716 error = xfs_bmbt_update(bma->cur, &PREV);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001717 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 goto done;
1719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 break;
1721
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001722 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 /*
1724 * Filling in all of a previously delayed allocation extent.
1725 * Neither the left nor right neighbors are contiguous with
1726 * the new one.
1727 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001728 PREV.br_startblock = new->br_startblock;
1729 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001730 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001731
Darrick J. Wong60b49842016-10-03 09:11:34 -07001732 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001733 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1735 else {
1736 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001737 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001738 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001740 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001741 error = xfs_btree_insert(bma->cur, &i);
1742 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 == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 break;
1747
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001748 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 /*
1750 * Filling in the first part of a previous delayed allocation.
1751 * The left neighbor is contiguous.
1752 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001753 old = LEFT;
1754 temp = PREV.br_blockcount - new->br_blockcount;
1755 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
1756 startblockval(PREV.br_startblock));
1757
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001758 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001759 xfs_iext_update_extent(bma->ip, state, bma->idx - 1, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001760
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001761 PREV.br_blockcount = temp = PREV.br_blockcount - new->br_blockcount;
1762 PREV.br_startoff += new->br_blockcount;
1763 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001764 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001765
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001766 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 rval = XFS_ILOG_DEXT;
1768 else {
1769 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001770 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001771 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001773 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001774 error = xfs_bmbt_update(bma->cur, &LEFT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001775 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 goto done;
1777 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001778
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001779 bma->idx--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 break;
1781
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001782 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 /*
1784 * Filling in the first part of a previous delayed allocation.
1785 * The left neighbor is not contiguous.
1786 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001787 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001788 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001789 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1791 else {
1792 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001793 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001794 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001796 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001797 error = xfs_btree_insert(bma->cur, &i);
1798 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001800 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001802
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001803 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001804 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001805 bma->firstblock, bma->dfops,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001806 &bma->cur, 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 rval |= tmp_rval;
1808 if (error)
1809 goto done;
1810 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001811
1812 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001813 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001814 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001815 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001816
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001817 PREV.br_startoff = new_endoff;
1818 PREV.br_blockcount = temp;
1819 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001820 xfs_iext_update_extent(bma->ip, state, bma->idx + 1, &PREV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 break;
1822
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001823 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /*
1825 * Filling in the last part of a previous delayed allocation.
1826 * The right neighbor is contiguous with the new allocation.
1827 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001828 old = RIGHT;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001829 RIGHT.br_startoff = new->br_startoff;
1830 RIGHT.br_startblock = new->br_startblock;
1831 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001832 xfs_iext_update_extent(bma->ip, state, bma->idx + 1, &RIGHT);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001833
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001834 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 rval = XFS_ILOG_DEXT;
1836 else {
1837 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001838 error = xfs_bmbt_lookup_eq(bma->cur, &old, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001839 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001841 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07001842 error = xfs_bmbt_update(bma->cur, &RIGHT);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001843 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 goto done;
1845 }
Christoph Hellwigec90c552011-05-23 08:52:53 +00001846
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001847 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001848 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001849 startblockval(PREV.br_startblock));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001850
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001851 PREV.br_blockcount = temp;
1852 PREV.br_startblock = nullstartblock(da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001853 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001854
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001855 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 break;
1857
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001858 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 /*
1860 * Filling in the last part of a previous delayed allocation.
1861 * The right neighbor is not contiguous.
1862 */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001863 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001864 (*nextents)++;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001865 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1867 else {
1868 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001869 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001870 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001872 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001873 error = xfs_btree_insert(bma->cur, &i);
1874 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 == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001878
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001879 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001880 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001881 bma->firstblock, bma->dfops, &bma->cur, 1,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001882 &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 rval |= tmp_rval;
1884 if (error)
1885 goto done;
1886 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001887
1888 temp = PREV.br_blockcount - new->br_blockcount;
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001889 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
Eric Sandeen9d87c312009-01-14 23:22:07 -06001890 startblockval(PREV.br_startblock) -
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001891 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001892
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001893 PREV.br_startblock = nullstartblock(da_new);
1894 PREV.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001895 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00001896
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001897 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 break;
1899
1900 case 0:
1901 /*
1902 * Filling in the middle part of a previous delayed allocation.
1903 * Contiguity is impossible here.
1904 * This case is avoided almost all the time.
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001905 *
1906 * We start with a delayed allocation:
1907 *
1908 * +ddddddddddddddddddddddddddddddddddddddddddddddddddddddd+
1909 * PREV @ idx
1910 *
1911 * and we are allocating:
1912 * +rrrrrrrrrrrrrrrrr+
1913 * new
1914 *
1915 * and we set it up for insertion as:
1916 * +ddddddddddddddddddd+rrrrrrrrrrrrrrrrr+ddddddddddddddddd+
1917 * new
1918 * PREV @ idx LEFT RIGHT
1919 * inserted at idx + 1
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001921 old = PREV;
1922
1923 /* LEFT is the new middle */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001924 LEFT = *new;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001925
1926 /* RIGHT is the new right */
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001927 RIGHT.br_state = PREV.br_state;
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001928 RIGHT.br_startoff = new_endoff;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001929 RIGHT.br_blockcount =
1930 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1931 RIGHT.br_startblock =
1932 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1933 RIGHT.br_blockcount));
1934
1935 /* truncate PREV */
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001936 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
1937 PREV.br_startblock =
1938 nullstartblock(xfs_bmap_worst_indlen(bma->ip,
1939 PREV.br_blockcount));
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07001940 xfs_iext_update_extent(bma->ip, state, bma->idx, &PREV);
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001941
bpm@sgi.com24446fc2011-01-19 17:41:58 +00001942 /* insert LEFT (r[0]) and RIGHT (r[1]) at the same time */
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001943 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
Darrick J. Wong60b49842016-10-03 09:11:34 -07001944 (*nextents)++;
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001945
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001946 if (bma->cur == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1948 else {
1949 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07001950 error = xfs_bmbt_lookup_eq(bma->cur, new, &i);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001951 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11001953 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001954 error = xfs_btree_insert(bma->cur, &i);
1955 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 == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00001959
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001960 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001961 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001962 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001963 1, &tmp_rval, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 rval |= tmp_rval;
1965 if (error)
1966 goto done;
1967 }
Christoph Hellwig4dcb88692017-10-17 14:16:24 -07001968
1969 da_new = startblockval(PREV.br_startblock) +
1970 startblockval(RIGHT.br_startblock);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001971 bma->idx++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 break;
1973
Christoph Hellwig7574aa92009-11-25 00:00:19 +00001974 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1975 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1976 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1977 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1978 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1979 case BMAP_LEFT_CONTIG:
1980 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 /*
1982 * These cases are all impossible.
1983 */
1984 ASSERT(0);
1985 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001986
Darrick J. Wong9c194642016-08-03 12:16:05 +10001987 /* add reverse mapping */
1988 error = xfs_rmap_map_extent(mp, bma->dfops, bma->ip, whichfork, new);
1989 if (error)
1990 goto done;
1991
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001992 /* convert to a btree if necessary */
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001993 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00001994 int tmp_logflags; /* partial log flag return val */
1995
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00001996 ASSERT(bma->cur == NULL);
1997 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10001998 bma->firstblock, bma->dfops, &bma->cur,
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11001999 da_old > 0, &tmp_logflags, whichfork);
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002000 bma->logflags |= tmp_logflags;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002001 if (error)
2002 goto done;
2003 }
2004
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002005 if (bma->cur) {
2006 da_new += bma->cur->bc_private.b.allocated;
2007 bma->cur->bc_private.b.allocated = 0;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002008 }
2009
Christoph Hellwigca1862b2017-10-17 14:16:25 -07002010 /* adjust for changes in reserved delayed indirect blocks */
2011 if (da_new != da_old) {
2012 ASSERT(state == 0 || da_new < da_old);
2013 error = xfs_mod_fdblocks(mp, (int64_t)(da_old - da_new),
2014 false);
2015 }
Christoph Hellwig572a4cf2011-09-18 20:41:04 +00002016
Darrick J. Wong6d3eb1e2016-01-04 16:12:42 +11002017 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018done:
Darrick J. Wong60b49842016-10-03 09:11:34 -07002019 if (whichfork != XFS_COW_FORK)
2020 bma->logflags |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return error;
2022#undef LEFT
2023#undef RIGHT
2024#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025}
2026
2027/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002028 * Convert an unwritten allocation to a real allocation or vice versa.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 */
2030STATIC int /* error */
2031xfs_bmap_add_extent_unwritten_real(
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002032 struct xfs_trans *tp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002034 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002035 xfs_extnum_t *idx, /* extent number to update/insert */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002037 xfs_bmbt_irec_t *new, /* new data to add to file extents */
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002038 xfs_fsblock_t *first, /* pointer to firstblock variable */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002039 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Christoph Hellwigb4e91812010-06-23 18:11:15 +10002040 int *logflagsp) /* inode logging flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 xfs_btree_cur_t *cur; /* btree cursor */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 int i; /* temp state */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002045 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 xfs_fileoff_t new_endoff; /* end offset of new entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
2048 /* left is 0, right is 1, prev is 2 */
2049 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002050 int state = xfs_bmap_fork_to_state(whichfork);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002051 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002052 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002054 *logflagsp = 0;
2055
2056 cur = *curp;
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002057 ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002058
2059 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11002060 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002061 ASSERT(!isnullstartblock(new->br_startblock));
2062
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002063 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065#define LEFT r[0]
2066#define RIGHT r[1]
2067#define PREV r[2]
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 /*
2070 * Set up a bunch of variables to make the tests simpler.
2071 */
2072 error = 0;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002073 xfs_iext_get_extent(ifp, *idx, &PREV);
2074 ASSERT(new->br_state != PREV.br_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 new_endoff = new->br_startoff + new->br_blockcount;
2076 ASSERT(PREV.br_startoff <= new->br_startoff);
2077 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 /*
2080 * Set flags determining what part of the previous oldext allocation
2081 * extent is being replaced by a newext allocation.
2082 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002083 if (PREV.br_startoff == new->br_startoff)
2084 state |= BMAP_LEFT_FILLING;
2085 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
2086 state |= BMAP_RIGHT_FILLING;
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /*
2089 * Check and set flags if this segment has a left neighbor.
2090 * Don't set contiguous if the combined extent would be too large.
2091 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002092 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002093 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002094 xfs_iext_get_extent(ifp, *idx - 1, &LEFT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002095
2096 if (isnullstartblock(LEFT.br_startblock))
2097 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002099
2100 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2101 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
2102 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002103 LEFT.br_state == new->br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002104 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2105 state |= BMAP_LEFT_CONTIG;
2106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /*
2108 * Check and set flags if this segment has a right neighbor.
2109 * Don't set contiguous if the combined extent would be too large.
2110 * Also check for all-three-contiguous being too large.
2111 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002112 if (*idx < xfs_iext_count(ifp) - 1) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002113 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002114 xfs_iext_get_extent(ifp, *idx + 1, &RIGHT);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002115 if (isnullstartblock(RIGHT.br_startblock))
2116 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002118
2119 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2120 new_endoff == RIGHT.br_startoff &&
2121 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002122 new->br_state == RIGHT.br_state &&
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002123 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
2124 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2125 BMAP_RIGHT_FILLING)) !=
2126 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
2127 BMAP_RIGHT_FILLING) ||
2128 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
2129 <= MAXEXTLEN))
2130 state |= BMAP_RIGHT_CONTIG;
2131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 /*
2133 * Switch out based on the FILLING and CONTIG state bits.
2134 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002135 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2136 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
2137 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
2138 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 /*
2140 * Setting all of a previous oldext extent to newext.
2141 * The left and right neighbors are both contiguous with new.
2142 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002143 --*idx;
2144
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002145 LEFT.br_blockcount += PREV.br_blockcount + RIGHT.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002146 xfs_iext_update_extent(ip, state, *idx, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002147
Christoph Hellwigec90c552011-05-23 08:52:53 +00002148 xfs_iext_remove(ip, *idx + 1, 2, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002149 XFS_IFORK_NEXT_SET(ip, whichfork,
2150 XFS_IFORK_NEXTENTS(ip, whichfork) - 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 if (cur == NULL)
2152 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2153 else {
2154 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002155 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2156 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002158 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002159 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002161 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002162 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002164 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002165 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002167 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002168 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002170 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002171 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002172 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 goto done;
2174 }
2175 break;
2176
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002177 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 /*
2179 * Setting all of a previous oldext extent to newext.
2180 * The left neighbor is contiguous, the right is not.
2181 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002182 --*idx;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002183
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002184 LEFT.br_blockcount += PREV.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002185 xfs_iext_update_extent(ip, state, *idx, &LEFT);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002186
2187 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002188 XFS_IFORK_NEXT_SET(ip, whichfork,
2189 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (cur == NULL)
2191 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2192 else {
2193 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002194 error = xfs_bmbt_lookup_eq(cur, &PREV, &i);
2195 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002197 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002198 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002200 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002201 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002203 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002204 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002205 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 goto done;
2207 }
2208 break;
2209
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002210 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 /*
2212 * Setting all of a previous oldext extent to newext.
2213 * The right neighbor is contiguous, the left is not.
2214 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002215 PREV.br_blockcount += RIGHT.br_blockcount;
2216 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002217 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002218
Christoph Hellwigec90c552011-05-23 08:52:53 +00002219 xfs_iext_remove(ip, *idx + 1, 1, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002220 XFS_IFORK_NEXT_SET(ip, whichfork,
2221 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (cur == NULL)
2223 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2224 else {
2225 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002226 error = xfs_bmbt_lookup_eq(cur, &RIGHT, &i);
2227 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002229 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig91cca5df2008-10-30 16:58:01 +11002230 if ((error = xfs_btree_delete(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002232 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig8df4da42008-10-30 16:55:58 +11002233 if ((error = xfs_btree_decrement(cur, 0, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002235 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002236 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002237 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 goto done;
2239 }
2240 break;
2241
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002242 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 /*
2244 * Setting all of a previous oldext extent to newext.
2245 * Neither the left nor right neighbors are contiguous with
2246 * the new one.
2247 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002248 PREV.br_state = new->br_state;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002249 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002250
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (cur == NULL)
2252 rval = XFS_ILOG_DEXT;
2253 else {
2254 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002255 error = xfs_bmbt_lookup_eq(cur, new, &i);
2256 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002258 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002259 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002260 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 goto done;
2262 }
2263 break;
2264
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002265 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 /*
2267 * Setting the first part of a previous oldext extent to newext.
2268 * The left neighbor is contiguous.
2269 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002270 LEFT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002271 xfs_iext_update_extent(ip, state, *idx - 1, &LEFT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002272
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002273 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002274 PREV.br_startoff += new->br_blockcount;
2275 PREV.br_startblock += new->br_blockcount;
2276 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002277 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002278
Christoph Hellwigec90c552011-05-23 08:52:53 +00002279 --*idx;
2280
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (cur == NULL)
2282 rval = XFS_ILOG_DEXT;
2283 else {
2284 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002285 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002286 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002288 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002289 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002290 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002292 error = xfs_btree_decrement(cur, 0, &i);
2293 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002295 error = xfs_bmbt_update(cur, &LEFT);
Christoph Hellwigb0eab142011-09-18 20:41:06 +00002296 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 goto done;
2298 }
2299 break;
2300
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002301 case BMAP_LEFT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 /*
2303 * Setting the first part of a previous oldext extent to newext.
2304 * The left neighbor is not contiguous.
2305 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002306 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002307 PREV.br_startoff += new->br_blockcount;
2308 PREV.br_startblock += new->br_blockcount;
2309 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002310 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002311
Christoph Hellwigec90c552011-05-23 08:52:53 +00002312 xfs_iext_insert(ip, *idx, 1, new, state);
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002313 XFS_IFORK_NEXT_SET(ip, whichfork,
2314 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (cur == NULL)
2316 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2317 else {
2318 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002319 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002320 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002322 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002323 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002324 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 goto done;
2326 cur->bc_rec.b = *new;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002327 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002329 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331 break;
2332
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002333 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 /*
2335 * Setting the last part of a previous oldext extent to newext.
2336 * The right neighbor is contiguous with the new allocation.
2337 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002338 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002339 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002340 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002341
2342 ++*idx;
2343
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002344 RIGHT.br_startoff = new->br_startoff;
2345 RIGHT.br_startblock = new->br_startblock;
2346 RIGHT.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002347 xfs_iext_update_extent(ip, state, *idx, &RIGHT);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002348
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (cur == NULL)
2350 rval = XFS_ILOG_DEXT;
2351 else {
2352 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002353 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002354 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002356 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002357 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002358 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 goto done;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002360 error = xfs_btree_increment(cur, 0, &i);
2361 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 goto done;
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002363 error = xfs_bmbt_update(cur, &RIGHT);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002364 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 goto done;
2366 }
2367 break;
2368
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002369 case BMAP_RIGHT_FILLING:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 /*
2371 * Setting the last part of a previous oldext extent to newext.
2372 * The right neighbor is not contiguous.
2373 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002374 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002375 PREV.br_blockcount -= new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002376 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002377
Christoph Hellwigec90c552011-05-23 08:52:53 +00002378 ++*idx;
2379 xfs_iext_insert(ip, *idx, 1, new, state);
2380
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002381 XFS_IFORK_NEXT_SET(ip, whichfork,
2382 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 if (cur == NULL)
2384 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2385 else {
2386 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002387 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002388 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002390 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002391 error = xfs_bmbt_update(cur, &PREV);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002392 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 goto done;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002394 error = xfs_bmbt_lookup_eq(cur, new, &i);
2395 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002397 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002398 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002400 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 }
2402 break;
2403
2404 case 0:
2405 /*
2406 * Setting the middle part of a previous oldext extent to
2407 * newext. Contiguity is impossible here.
2408 * One extent becomes three extents.
2409 */
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002410 old = PREV;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002411 PREV.br_blockcount = new->br_startoff - PREV.br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002412 xfs_iext_update_extent(ip, state, *idx, &PREV);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 r[0] = *new;
2415 r[1].br_startoff = new_endoff;
2416 r[1].br_blockcount =
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002417 old.br_startoff + old.br_blockcount - new_endoff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 r[1].br_startblock = new->br_startblock + new->br_blockcount;
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002419 r[1].br_state = PREV.br_state;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002420
2421 ++*idx;
2422 xfs_iext_insert(ip, *idx, 2, &r[0], state);
2423
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002424 XFS_IFORK_NEXT_SET(ip, whichfork,
2425 XFS_IFORK_NEXTENTS(ip, whichfork) + 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (cur == NULL)
2427 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
2428 else {
2429 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002430 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwig79fa6142017-10-17 14:16:25 -07002431 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002433 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 /* new right extent - oldext */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002435 error = xfs_bmbt_update(cur, &r[1]);
2436 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 goto done;
2438 /* new left extent - oldext */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 cur->bc_rec.b = PREV;
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002440 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002442 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Lachlan McIlroyddea2d52008-06-23 13:25:53 +10002443 /*
2444 * Reset the cursor to the position of the new extent
2445 * we are about to insert as we can't trust it after
2446 * the previous insert.
2447 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002448 error = xfs_bmbt_lookup_eq(cur, new, &i);
2449 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002451 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 /* new middle extent - newext */
Christoph Hellwig4b22a572008-10-30 16:57:40 +11002453 if ((error = xfs_btree_insert(cur, &i)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002455 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 }
2457 break;
2458
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002459 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2460 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2461 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
2462 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
2463 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
2464 case BMAP_LEFT_CONTIG:
2465 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 /*
2467 * These cases are all impossible.
2468 */
2469 ASSERT(0);
2470 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002471
Darrick J. Wong9c194642016-08-03 12:16:05 +10002472 /* update reverse mappings */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002473 error = xfs_rmap_convert_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002474 if (error)
2475 goto done;
2476
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002477 /* convert to a btree if necessary */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002478 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002479 int tmp_logflags; /* partial log flag return val */
2480
2481 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10002482 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, &cur,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002483 0, &tmp_logflags, whichfork);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002484 *logflagsp |= tmp_logflags;
2485 if (error)
2486 goto done;
2487 }
2488
2489 /* clear out the allocated field, done with it now in any case. */
2490 if (cur) {
2491 cur->bc_private.b.allocated = 0;
2492 *curp = cur;
2493 }
2494
Darrick J. Wong05a630d2017-02-02 15:14:01 -08002495 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496done:
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002497 *logflagsp |= rval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 return error;
2499#undef LEFT
2500#undef RIGHT
2501#undef PREV
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
2504/*
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002505 * Convert a hole to a delayed allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002507STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508xfs_bmap_add_extent_hole_delay(
2509 xfs_inode_t *ip, /* incore inode pointer */
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002510 int whichfork,
Christoph Hellwigec90c552011-05-23 08:52:53 +00002511 xfs_extnum_t *idx, /* extent number to update/insert */
Christoph Hellwig1fd044d2011-09-18 20:40:49 +00002512 xfs_bmbt_irec_t *new) /* new data to add to file extents */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002514 xfs_ifork_t *ifp; /* inode fork pointer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2516 xfs_filblks_t newlen=0; /* new indirect size */
2517 xfs_filblks_t oldlen=0; /* old indirect size */
2518 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002519 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002520 xfs_filblks_t temp; /* temp for indirect calculations */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Darrick J. Wongbe51f812016-10-03 09:11:32 -07002522 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen9d87c312009-01-14 23:22:07 -06002523 ASSERT(isnullstartblock(new->br_startblock));
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002524
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 /*
2526 * Check and set flags if this segment has a left neighbor
2527 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002528 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002529 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002530 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002531
2532 if (isnullstartblock(left.br_startblock))
2533 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /*
2537 * Check and set flags if the current (right) segment exists.
2538 * If it doesn't exist, we're converting the hole at end-of-file.
2539 */
Eric Sandeen5d829302016-11-08 12:59:42 +11002540 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002541 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002542 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002543
2544 if (isnullstartblock(right.br_startblock))
2545 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 /*
2549 * Set contiguity flags on the left and right neighbors.
2550 * Don't let extents get too large, even if the pieces are contiguous.
2551 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002552 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
2553 left.br_startoff + left.br_blockcount == new->br_startoff &&
2554 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2555 state |= BMAP_LEFT_CONTIG;
2556
2557 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
2558 new->br_startoff + new->br_blockcount == right.br_startoff &&
2559 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2560 (!(state & BMAP_LEFT_CONTIG) ||
2561 (left.br_blockcount + new->br_blockcount +
2562 right.br_blockcount <= MAXEXTLEN)))
2563 state |= BMAP_RIGHT_CONTIG;
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 /*
2566 * Switch out based on the contiguity flags.
2567 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002568 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2569 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 /*
2571 * New allocation is contiguous with delayed allocations
2572 * on the left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002573 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002575 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 temp = left.br_blockcount + new->br_blockcount +
2577 right.br_blockcount;
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002578
Eric Sandeen9d87c312009-01-14 23:22:07 -06002579 oldlen = startblockval(left.br_startblock) +
2580 startblockval(new->br_startblock) +
2581 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002582 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2583 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002584 left.br_startblock = nullstartblock(newlen);
2585 left.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002586 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002587
Christoph Hellwigec90c552011-05-23 08:52:53 +00002588 xfs_iext_remove(ip, *idx + 1, 1, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 break;
2590
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002591 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 /*
2593 * New allocation is contiguous with a delayed allocation
2594 * on the left.
2595 * Merge the new allocation with the left neighbor.
2596 */
Christoph Hellwigec90c552011-05-23 08:52:53 +00002597 --*idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 temp = left.br_blockcount + new->br_blockcount;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002599
Eric Sandeen9d87c312009-01-14 23:22:07 -06002600 oldlen = startblockval(left.br_startblock) +
2601 startblockval(new->br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002602 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2603 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002604 left.br_blockcount = temp;
2605 left.br_startblock = nullstartblock(newlen);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002606 xfs_iext_update_extent(ip, state, *idx, &left);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 break;
2608
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002609 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 /*
2611 * New allocation is contiguous with a delayed allocation
2612 * on the right.
2613 * Merge the new allocation with the right neighbor.
2614 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 temp = new->br_blockcount + right.br_blockcount;
Eric Sandeen9d87c312009-01-14 23:22:07 -06002616 oldlen = startblockval(new->br_startblock) +
2617 startblockval(right.br_startblock);
Brian Foster0e339ef2017-02-13 22:48:18 -08002618 newlen = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2619 oldlen);
Christoph Hellwig3ffc18e2017-10-17 14:16:23 -07002620 right.br_startoff = new->br_startoff;
2621 right.br_startblock = nullstartblock(newlen);
2622 right.br_blockcount = temp;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002623 xfs_iext_update_extent(ip, state, *idx, &right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 break;
2625
2626 case 0:
2627 /*
2628 * New allocation is not contiguous with another
2629 * delayed allocation.
2630 * Insert a new entry.
2631 */
2632 oldlen = newlen = 0;
Christoph Hellwigec90c552011-05-23 08:52:53 +00002633 xfs_iext_insert(ip, *idx, 1, new, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 break;
2635 }
2636 if (oldlen != newlen) {
2637 ASSERT(oldlen > newlen);
Dave Chinner0d485ad2015-02-23 21:22:03 +11002638 xfs_mod_fdblocks(ip->i_mount, (int64_t)(oldlen - newlen),
2639 false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 /*
2641 * Nothing to do for disk quota accounting here.
2642 */
2643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
2646/*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002647 * Convert a hole to a real allocation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 */
2649STATIC int /* error */
2650xfs_bmap_add_extent_hole_real(
Christoph Hellwig6d045582017-04-11 16:45:54 -07002651 struct xfs_trans *tp,
2652 struct xfs_inode *ip,
2653 int whichfork,
2654 xfs_extnum_t *idx,
2655 struct xfs_btree_cur **curp,
2656 struct xfs_bmbt_irec *new,
2657 xfs_fsblock_t *first,
2658 struct xfs_defer_ops *dfops,
2659 int *logflagsp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Christoph Hellwig6d045582017-04-11 16:45:54 -07002661 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
2662 struct xfs_mount *mp = ip->i_mount;
2663 struct xfs_btree_cur *cur = *curp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 int error; /* error return value */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 int i; /* temp state */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 xfs_bmbt_irec_t left; /* left neighbor extent entry */
2667 xfs_bmbt_irec_t right; /* right neighbor extent entry */
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002668 int rval=0; /* return value (logging flags) */
Christoph Hellwig060ea652017-10-19 11:02:29 -07002669 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002670 struct xfs_bmbt_irec old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671
Christoph Hellwig6d045582017-04-11 16:45:54 -07002672 ASSERT(*idx >= 0);
2673 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002674 ASSERT(!isnullstartblock(new->br_startblock));
Christoph Hellwig6d045582017-04-11 16:45:54 -07002675 ASSERT(!cur || !(cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002676
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11002677 XFS_STATS_INC(mp, xs_add_exlist);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002678
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 /*
2680 * Check and set flags if this segment has a left neighbor.
2681 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002682 if (*idx > 0) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002683 state |= BMAP_LEFT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002684 xfs_iext_get_extent(ifp, *idx - 1, &left);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002685 if (isnullstartblock(left.br_startblock))
2686 state |= BMAP_LEFT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 /*
2690 * Check and set flags if this segment has a current value.
2691 * Not true if we're inserting into the "hole" at eof.
2692 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002693 if (*idx < xfs_iext_count(ifp)) {
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002694 state |= BMAP_RIGHT_VALID;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07002695 xfs_iext_get_extent(ifp, *idx, &right);
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002696 if (isnullstartblock(right.br_startblock))
2697 state |= BMAP_RIGHT_DELAY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 }
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002699
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 /*
2701 * We're inserting a real allocation between "left" and "right".
2702 * Set the contiguity flags. Don't let extents get too large.
2703 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002704 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
2705 left.br_startoff + left.br_blockcount == new->br_startoff &&
2706 left.br_startblock + left.br_blockcount == new->br_startblock &&
2707 left.br_state == new->br_state &&
2708 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
2709 state |= BMAP_LEFT_CONTIG;
2710
2711 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
2712 new->br_startoff + new->br_blockcount == right.br_startoff &&
2713 new->br_startblock + new->br_blockcount == right.br_startblock &&
2714 new->br_state == right.br_state &&
2715 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
2716 (!(state & BMAP_LEFT_CONTIG) ||
2717 left.br_blockcount + new->br_blockcount +
2718 right.br_blockcount <= MAXEXTLEN))
2719 state |= BMAP_RIGHT_CONTIG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002721 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722 /*
2723 * Select which case we're in here, and implement it.
2724 */
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002725 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
2726 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 /*
2728 * New allocation is contiguous with real allocations on the
2729 * left and on the right.
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11002730 * Merge all three into a single extent record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002732 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002733 left.br_blockcount += new->br_blockcount + right.br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002734 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002735
Christoph Hellwig6d045582017-04-11 16:45:54 -07002736 xfs_iext_remove(ip, *idx + 1, 1, state);
Christoph Hellwigec90c552011-05-23 08:52:53 +00002737
Christoph Hellwig6d045582017-04-11 16:45:54 -07002738 XFS_IFORK_NEXT_SET(ip, whichfork,
2739 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2740 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002741 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002742 } else {
2743 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002744 error = xfs_bmbt_lookup_eq(cur, &right, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002745 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002746 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002747 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002748 error = xfs_btree_delete(cur, &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_decrement(cur, 0, &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 Hellwiga67d00a2017-10-17 14:16:26 -07002756 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002757 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002758 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002760 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002762 case BMAP_LEFT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 /*
2764 * New allocation is contiguous with a real allocation
2765 * on the left.
2766 * Merge the new allocation with the left neighbor.
2767 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002768 --*idx;
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002769 old = left;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002770
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002771 left.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002772 xfs_iext_update_extent(ip, state, *idx, &left);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002773
Christoph Hellwig6d045582017-04-11 16:45:54 -07002774 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002775 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002776 } else {
2777 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002778 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002779 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002780 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002781 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002782 error = xfs_bmbt_update(cur, &left);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002783 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002784 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002786 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Christoph Hellwig7574aa92009-11-25 00:00:19 +00002788 case BMAP_RIGHT_CONTIG:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 /*
2790 * New allocation is contiguous with a real allocation
2791 * on the right.
2792 * Merge the new allocation with the right neighbor.
2793 */
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002794 old = right;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002795
Christoph Hellwig1abb9e52017-10-17 14:16:24 -07002796 right.br_startoff = new->br_startoff;
2797 right.br_startblock = new->br_startblock;
2798 right.br_blockcount += new->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07002799 xfs_iext_update_extent(ip, state, *idx, &right);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00002800
Christoph Hellwig6d045582017-04-11 16:45:54 -07002801 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002802 rval = xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002803 } else {
2804 rval = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002805 error = xfs_bmbt_lookup_eq(cur, &old, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002806 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002807 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002808 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07002809 error = xfs_bmbt_update(cur, &right);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002810 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002811 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002813 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
2815 case 0:
2816 /*
2817 * New allocation is not contiguous with another
2818 * real allocation.
2819 * Insert a new entry.
2820 */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002821 xfs_iext_insert(ip, *idx, 1, new, state);
2822 XFS_IFORK_NEXT_SET(ip, whichfork,
2823 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2824 if (cur == NULL) {
Eric Sandeen9d87c312009-01-14 23:22:07 -06002825 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002826 } else {
2827 rval = XFS_ILOG_CORE;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07002828 error = xfs_bmbt_lookup_eq(cur, new, &i);
Christoph Hellwigc6534242011-09-18 20:41:05 +00002829 if (error)
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002830 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11002831 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, done);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002832 error = xfs_btree_insert(cur, &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 == 1, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 }
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002837 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 }
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002839
Darrick J. Wong9c194642016-08-03 12:16:05 +10002840 /* add reverse mapping */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002841 error = xfs_rmap_map_extent(mp, dfops, ip, whichfork, new);
Darrick J. Wong9c194642016-08-03 12:16:05 +10002842 if (error)
2843 goto done;
2844
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002845 /* convert to a btree if necessary */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002846 if (xfs_bmap_needs_btree(ip, whichfork)) {
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002847 int tmp_logflags; /* partial log flag return val */
2848
Christoph Hellwig6d045582017-04-11 16:45:54 -07002849 ASSERT(cur == NULL);
2850 error = xfs_bmap_extents_to_btree(tp, ip, first, dfops, curp,
Christoph Hellwigc6534242011-09-18 20:41:05 +00002851 0, &tmp_logflags, whichfork);
Christoph Hellwig6d045582017-04-11 16:45:54 -07002852 *logflagsp |= tmp_logflags;
2853 cur = *curp;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00002854 if (error)
2855 goto done;
2856 }
2857
2858 /* clear out the allocated field, done with it now in any case. */
Christoph Hellwig6d045582017-04-11 16:45:54 -07002859 if (cur)
2860 cur->bc_private.b.allocated = 0;
Christoph Hellwigc6534242011-09-18 20:41:05 +00002861
Christoph Hellwig6d045582017-04-11 16:45:54 -07002862 xfs_bmap_check_leaf_extents(cur, ip, whichfork);
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002863done:
Christoph Hellwig6d045582017-04-11 16:45:54 -07002864 *logflagsp |= rval;
Olaf Weber3e57ecf2006-06-09 14:48:12 +10002865 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866}
2867
Nathan Scottdd9f4382006-01-11 15:28:28 +11002868/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11002869 * Functions used in the extent read, allocate and remove paths
2870 */
2871
2872/*
Nathan Scottdd9f4382006-01-11 15:28:28 +11002873 * Adjust the size of the new extent based on di_extsize and rt extsize.
2874 */
Dave Chinner68988112013-08-12 20:49:42 +10002875int
Nathan Scottdd9f4382006-01-11 15:28:28 +11002876xfs_bmap_extsize_align(
2877 xfs_mount_t *mp,
2878 xfs_bmbt_irec_t *gotp, /* next extent pointer */
2879 xfs_bmbt_irec_t *prevp, /* previous extent pointer */
2880 xfs_extlen_t extsz, /* align to this extent size */
2881 int rt, /* is this a realtime inode? */
2882 int eof, /* is extent at end-of-file? */
2883 int delay, /* creating delalloc extent? */
2884 int convert, /* overwriting unwritten extent? */
2885 xfs_fileoff_t *offp, /* in/out: aligned offset */
2886 xfs_extlen_t *lenp) /* in/out: aligned length */
2887{
2888 xfs_fileoff_t orig_off; /* original offset */
2889 xfs_extlen_t orig_alen; /* original length */
2890 xfs_fileoff_t orig_end; /* original off+len */
2891 xfs_fileoff_t nexto; /* next file offset */
2892 xfs_fileoff_t prevo; /* previous file offset */
2893 xfs_fileoff_t align_off; /* temp for offset */
2894 xfs_extlen_t align_alen; /* temp for length */
2895 xfs_extlen_t temp; /* temp for calculations */
2896
2897 if (convert)
2898 return 0;
2899
2900 orig_off = align_off = *offp;
2901 orig_alen = align_alen = *lenp;
2902 orig_end = orig_off + orig_alen;
2903
2904 /*
2905 * If this request overlaps an existing extent, then don't
2906 * attempt to perform any additional alignment.
2907 */
2908 if (!delay && !eof &&
2909 (orig_off >= gotp->br_startoff) &&
2910 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
2911 return 0;
2912 }
2913
2914 /*
2915 * If the file offset is unaligned vs. the extent size
2916 * we need to align it. This will be possible unless
2917 * the file was previously written with a kernel that didn't
2918 * perform this alignment, or if a truncate shot us in the
2919 * foot.
2920 */
2921 temp = do_mod(orig_off, extsz);
2922 if (temp) {
2923 align_alen += temp;
2924 align_off -= temp;
2925 }
Dave Chinner6dea405e2015-05-29 07:40:06 +10002926
2927 /* Same adjustment for the end of the requested area. */
2928 temp = (align_alen % extsz);
2929 if (temp)
Nathan Scottdd9f4382006-01-11 15:28:28 +11002930 align_alen += extsz - temp;
Dave Chinner6dea405e2015-05-29 07:40:06 +10002931
2932 /*
2933 * For large extent hint sizes, the aligned extent might be larger than
2934 * MAXEXTLEN. In that case, reduce the size by an extsz so that it pulls
2935 * the length back under MAXEXTLEN. The outer allocation loops handle
2936 * short allocation just fine, so it is safe to do this. We only want to
2937 * do it when we are forced to, though, because it means more allocation
2938 * operations are required.
2939 */
2940 while (align_alen > MAXEXTLEN)
2941 align_alen -= extsz;
2942 ASSERT(align_alen <= MAXEXTLEN);
2943
Nathan Scottdd9f4382006-01-11 15:28:28 +11002944 /*
2945 * If the previous block overlaps with this proposed allocation
2946 * then move the start forward without adjusting the length.
2947 */
2948 if (prevp->br_startoff != NULLFILEOFF) {
2949 if (prevp->br_startblock == HOLESTARTBLOCK)
2950 prevo = prevp->br_startoff;
2951 else
2952 prevo = prevp->br_startoff + prevp->br_blockcount;
2953 } else
2954 prevo = 0;
2955 if (align_off != orig_off && align_off < prevo)
2956 align_off = prevo;
2957 /*
2958 * If the next block overlaps with this proposed allocation
2959 * then move the start back without adjusting the length,
2960 * but not before offset 0.
2961 * This may of course make the start overlap previous block,
2962 * and if we hit the offset 0 limit then the next block
2963 * can still overlap too.
2964 */
2965 if (!eof && gotp->br_startoff != NULLFILEOFF) {
2966 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
2967 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
2968 nexto = gotp->br_startoff + gotp->br_blockcount;
2969 else
2970 nexto = gotp->br_startoff;
2971 } else
2972 nexto = NULLFILEOFF;
2973 if (!eof &&
2974 align_off + align_alen != orig_end &&
2975 align_off + align_alen > nexto)
2976 align_off = nexto > align_alen ? nexto - align_alen : 0;
2977 /*
2978 * If we're now overlapping the next or previous extent that
2979 * means we can't fit an extsz piece in this hole. Just move
2980 * the start forward to the first valid spot and set
2981 * the length so we hit the end.
2982 */
2983 if (align_off != orig_off && align_off < prevo)
2984 align_off = prevo;
2985 if (align_off + align_alen != orig_end &&
2986 align_off + align_alen > nexto &&
2987 nexto != NULLFILEOFF) {
2988 ASSERT(nexto > prevo);
2989 align_alen = nexto - align_off;
2990 }
2991
2992 /*
2993 * If realtime, and the result isn't a multiple of the realtime
2994 * extent size we need to remove blocks until it is.
2995 */
2996 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2997 /*
2998 * We're not covering the original request, or
2999 * we won't be able to once we fix the length.
3000 */
3001 if (orig_off < align_off ||
3002 orig_end > align_off + align_alen ||
3003 align_alen - temp < orig_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003004 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003005 /*
3006 * Try to fix it by moving the start up.
3007 */
3008 if (align_off + temp <= orig_off) {
3009 align_alen -= temp;
3010 align_off += temp;
3011 }
3012 /*
3013 * Try to fix it by moving the end in.
3014 */
3015 else if (align_off + align_alen - temp >= orig_end)
3016 align_alen -= temp;
3017 /*
3018 * Set the start to the minimum then trim the length.
3019 */
3020 else {
3021 align_alen -= orig_off - align_off;
3022 align_off = orig_off;
3023 align_alen -= align_alen % mp->m_sb.sb_rextsize;
3024 }
3025 /*
3026 * Result doesn't cover the request, fail it.
3027 */
3028 if (orig_off < align_off || orig_end > align_off + align_alen)
Dave Chinner24513372014-06-25 14:58:08 +10003029 return -EINVAL;
Nathan Scottdd9f4382006-01-11 15:28:28 +11003030 } else {
3031 ASSERT(orig_off >= align_off);
Dave Chinner6dea405e2015-05-29 07:40:06 +10003032 /* see MAXEXTLEN handling above */
3033 ASSERT(orig_end <= align_off + align_alen ||
3034 align_alen + extsz > MAXEXTLEN);
Nathan Scottdd9f4382006-01-11 15:28:28 +11003035 }
3036
3037#ifdef DEBUG
3038 if (!eof && gotp->br_startoff != NULLFILEOFF)
3039 ASSERT(align_off + align_alen <= gotp->br_startoff);
3040 if (prevp->br_startoff != NULLFILEOFF)
3041 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
3042#endif
3043
3044 *lenp = align_alen;
3045 *offp = align_off;
3046 return 0;
3047}
3048
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049#define XFS_ALLOC_GAP_UNITS 4
3050
Dave Chinner68988112013-08-12 20:49:42 +10003051void
Nathan Scotta365bdd2006-03-14 13:34:16 +11003052xfs_bmap_adjacent(
Dave Chinner68988112013-08-12 20:49:42 +10003053 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054{
3055 xfs_fsblock_t adjust; /* adjustment to block numbers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
3057 xfs_mount_t *mp; /* mount point structure */
3058 int nullfb; /* true if ap->firstblock isn't set */
3059 int rt; /* true if inode is realtime */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
3061#define ISVALID(x,y) \
3062 (rt ? \
3063 (x) < mp->m_sb.sb_rblocks : \
3064 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
3065 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
3066 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 mp = ap->ip->i_mount;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003069 nullfb = *ap->firstblock == NULLFSBLOCK;
Dave Chinner292378e2016-09-26 08:21:28 +10003070 rt = XFS_IS_REALTIME_INODE(ap->ip) &&
3071 xfs_alloc_is_userdata(ap->datatype);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003072 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 /*
3074 * If allocating at eof, and there's a previous real block,
Malcolm Parsons9da096f2009-03-29 09:55:42 +02003075 * try to use its last block as our starting point.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003077 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
3078 !isnullstartblock(ap->prev.br_startblock) &&
3079 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
3080 ap->prev.br_startblock)) {
Dave Chinner3a756672011-09-18 20:40:58 +00003081 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 /*
3083 * Adjust for the gap between prevp and us.
3084 */
Dave Chinner3a756672011-09-18 20:40:58 +00003085 adjust = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003086 (ap->prev.br_startoff + ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (adjust &&
Dave Chinner3a756672011-09-18 20:40:58 +00003088 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
3089 ap->blkno += adjust;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 }
3091 /*
3092 * If not at eof, then compare the two neighbor blocks.
3093 * Figure out whether either one gives us a good starting point,
3094 * and pick the better one.
3095 */
3096 else if (!ap->eof) {
3097 xfs_fsblock_t gotbno; /* right side block number */
3098 xfs_fsblock_t gotdiff=0; /* right side difference */
3099 xfs_fsblock_t prevbno; /* left side block number */
3100 xfs_fsblock_t prevdiff=0; /* left side difference */
3101
3102 /*
3103 * If there's a previous (left) block, select a requested
3104 * start block based on it.
3105 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003106 if (ap->prev.br_startoff != NULLFILEOFF &&
3107 !isnullstartblock(ap->prev.br_startblock) &&
3108 (prevbno = ap->prev.br_startblock +
3109 ap->prev.br_blockcount) &&
3110 ISVALID(prevbno, ap->prev.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 /*
3112 * Calculate gap to end of previous block.
3113 */
Dave Chinner3a756672011-09-18 20:40:58 +00003114 adjust = prevdiff = ap->offset -
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003115 (ap->prev.br_startoff +
3116 ap->prev.br_blockcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 /*
3118 * Figure the startblock based on the previous block's
3119 * end and the gap size.
3120 * Heuristic!
3121 * If the gap is large relative to the piece we're
3122 * allocating, or using it gives us an invalid block
3123 * number, then just use the end of the previous block.
3124 */
Dave Chinner3a756672011-09-18 20:40:58 +00003125 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 ISVALID(prevbno + prevdiff,
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003127 ap->prev.br_startblock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 prevbno += adjust;
3129 else
3130 prevdiff += adjust;
3131 /*
3132 * If the firstblock forbids it, can't use it,
3133 * must use default.
3134 */
3135 if (!rt && !nullfb &&
3136 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
3137 prevbno = NULLFSBLOCK;
3138 }
3139 /*
3140 * No previous block or can't follow it, just default.
3141 */
3142 else
3143 prevbno = NULLFSBLOCK;
3144 /*
3145 * If there's a following (right) block, select a requested
3146 * start block based on it.
3147 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003148 if (!isnullstartblock(ap->got.br_startblock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 /*
3150 * Calculate gap to start of next block.
3151 */
Dave Chinner3a756672011-09-18 20:40:58 +00003152 adjust = gotdiff = ap->got.br_startoff - ap->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 /*
3154 * Figure the startblock based on the next block's
3155 * start and the gap size.
3156 */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003157 gotbno = ap->got.br_startblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 /*
3159 * Heuristic!
3160 * If the gap is large relative to the piece we're
3161 * allocating, or using it gives us an invalid block
3162 * number, then just use the start of the next block
3163 * offset by our length.
3164 */
Dave Chinner3a756672011-09-18 20:40:58 +00003165 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 ISVALID(gotbno - gotdiff, gotbno))
3167 gotbno -= adjust;
Dave Chinner3a756672011-09-18 20:40:58 +00003168 else if (ISVALID(gotbno - ap->length, gotbno)) {
3169 gotbno -= ap->length;
3170 gotdiff += adjust - ap->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 } else
3172 gotdiff += adjust;
3173 /*
3174 * If the firstblock forbids it, can't use it,
3175 * must use default.
3176 */
3177 if (!rt && !nullfb &&
3178 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
3179 gotbno = NULLFSBLOCK;
3180 }
3181 /*
3182 * No next block, just default.
3183 */
3184 else
3185 gotbno = NULLFSBLOCK;
3186 /*
3187 * If both valid, pick the better one, else the only good
Dave Chinner3a756672011-09-18 20:40:58 +00003188 * one, else ap->blkno is already set (to 0 or the inode block).
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 */
3190 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003191 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 else if (prevbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003193 ap->blkno = prevbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 else if (gotbno != NULLFSBLOCK)
Dave Chinner3a756672011-09-18 20:40:58 +00003195 ap->blkno = gotbno;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 }
Nathan Scotta365bdd2006-03-14 13:34:16 +11003197#undef ISVALID
Nathan Scotta365bdd2006-03-14 13:34:16 +11003198}
3199
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003200static int
3201xfs_bmap_longest_free_extent(
3202 struct xfs_trans *tp,
3203 xfs_agnumber_t ag,
3204 xfs_extlen_t *blen,
3205 int *notinit)
3206{
3207 struct xfs_mount *mp = tp->t_mountp;
3208 struct xfs_perag *pag;
3209 xfs_extlen_t longest;
3210 int error = 0;
3211
3212 pag = xfs_perag_get(mp, ag);
3213 if (!pag->pagf_init) {
3214 error = xfs_alloc_pagf_init(mp, tp, ag, XFS_ALLOC_FLAG_TRYLOCK);
3215 if (error)
3216 goto out;
3217
3218 if (!pag->pagf_init) {
3219 *notinit = 1;
3220 goto out;
3221 }
3222 }
3223
Dave Chinner50adbcb2015-06-22 10:04:31 +10003224 longest = xfs_alloc_longest_free_extent(mp, pag,
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003225 xfs_alloc_min_freelist(mp, pag),
3226 xfs_ag_resv_needed(pag, XFS_AG_RESV_NONE));
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003227 if (*blen < longest)
3228 *blen = longest;
3229
3230out:
3231 xfs_perag_put(pag);
3232 return error;
3233}
3234
3235static void
3236xfs_bmap_select_minlen(
3237 struct xfs_bmalloca *ap,
3238 struct xfs_alloc_arg *args,
3239 xfs_extlen_t *blen,
3240 int notinit)
3241{
3242 if (notinit || *blen < ap->minlen) {
3243 /*
3244 * Since we did a BUF_TRYLOCK above, it is possible that
3245 * there is space for this request.
3246 */
3247 args->minlen = ap->minlen;
3248 } else if (*blen < args->maxlen) {
3249 /*
3250 * If the best seen length is less than the request length,
3251 * use the best as the minimum.
3252 */
3253 args->minlen = *blen;
3254 } else {
3255 /*
3256 * Otherwise we've seen an extent as big as maxlen, use that
3257 * as the minimum.
3258 */
3259 args->minlen = args->maxlen;
3260 }
3261}
3262
Nathan Scotta365bdd2006-03-14 13:34:16 +11003263STATIC int
Christoph Hellwigc467c042010-02-15 23:34:42 +00003264xfs_bmap_btalloc_nullfb(
3265 struct xfs_bmalloca *ap,
3266 struct xfs_alloc_arg *args,
3267 xfs_extlen_t *blen)
3268{
3269 struct xfs_mount *mp = ap->ip->i_mount;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003270 xfs_agnumber_t ag, startag;
3271 int notinit = 0;
3272 int error;
3273
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003274 args->type = XFS_ALLOCTYPE_START_BNO;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003275 args->total = ap->total;
3276
Christoph Hellwigc467c042010-02-15 23:34:42 +00003277 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3278 if (startag == NULLAGNUMBER)
3279 startag = ag = 0;
3280
Dave Chinner14b064c2011-01-27 12:16:28 +11003281 while (*blen < args->maxlen) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003282 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3283 &notinit);
3284 if (error)
3285 return error;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003286
Christoph Hellwigc467c042010-02-15 23:34:42 +00003287 if (++ag == mp->m_sb.sb_agcount)
3288 ag = 0;
3289 if (ag == startag)
3290 break;
Christoph Hellwigc467c042010-02-15 23:34:42 +00003291 }
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003292
3293 xfs_bmap_select_minlen(ap, args, blen, notinit);
3294 return 0;
3295}
3296
3297STATIC int
3298xfs_bmap_btalloc_filestreams(
3299 struct xfs_bmalloca *ap,
3300 struct xfs_alloc_arg *args,
3301 xfs_extlen_t *blen)
3302{
3303 struct xfs_mount *mp = ap->ip->i_mount;
3304 xfs_agnumber_t ag;
3305 int notinit = 0;
3306 int error;
3307
3308 args->type = XFS_ALLOCTYPE_NEAR_BNO;
3309 args->total = ap->total;
3310
3311 ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
3312 if (ag == NULLAGNUMBER)
3313 ag = 0;
3314
3315 error = xfs_bmap_longest_free_extent(args->tp, ag, blen, &notinit);
3316 if (error)
3317 return error;
3318
3319 if (*blen < args->maxlen) {
3320 error = xfs_filestream_new_ag(ap, &ag);
3321 if (error)
3322 return error;
3323
3324 error = xfs_bmap_longest_free_extent(args->tp, ag, blen,
3325 &notinit);
3326 if (error)
3327 return error;
3328
3329 }
3330
3331 xfs_bmap_select_minlen(ap, args, blen, notinit);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003332
3333 /*
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003334 * Set the failure fallback case to look in the selected AG as stream
3335 * may have moved.
Christoph Hellwigc467c042010-02-15 23:34:42 +00003336 */
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003337 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003338 return 0;
3339}
3340
3341STATIC int
Nathan Scotta365bdd2006-03-14 13:34:16 +11003342xfs_bmap_btalloc(
Dave Chinner68988112013-08-12 20:49:42 +10003343 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003344{
3345 xfs_mount_t *mp; /* mount point structure */
3346 xfs_alloctype_t atype = 0; /* type for allocation routines */
Dave Chinner292378e2016-09-26 08:21:28 +10003347 xfs_extlen_t align = 0; /* minimum allocation alignment */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003348 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
Christoph Hellwigc467c042010-02-15 23:34:42 +00003349 xfs_agnumber_t ag;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003350 xfs_alloc_arg_t args;
3351 xfs_extlen_t blen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003352 xfs_extlen_t nextminlen = 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003353 int nullfb; /* true if ap->firstblock isn't set */
3354 int isaligned;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003355 int tryagain;
3356 int error;
Dave Chinner33177f052013-12-12 16:34:36 +11003357 int stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003358
Dave Chinnera99ebf42011-12-01 11:24:20 +00003359 ASSERT(ap->length);
3360
Nathan Scotta365bdd2006-03-14 13:34:16 +11003361 mp = ap->ip->i_mount;
Dave Chinner33177f052013-12-12 16:34:36 +11003362
3363 /* stripe alignment for allocation is determined by mount parameters */
3364 stripe_align = 0;
3365 if (mp->m_swidth && (mp->m_flags & XFS_MOUNT_SWALLOC))
3366 stripe_align = mp->m_swidth;
3367 else if (mp->m_dalign)
3368 stripe_align = mp->m_dalign;
3369
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003370 if (ap->flags & XFS_BMAPI_COWFORK)
3371 align = xfs_get_cowextsz_hint(ap->ip);
3372 else if (xfs_alloc_is_userdata(ap->datatype))
Dave Chinner292378e2016-09-26 08:21:28 +10003373 align = xfs_get_extsz_hint(ap->ip);
Christoph Hellwig493611e2017-01-25 08:59:43 -08003374 if (align) {
Dave Chinnerbaf41a52011-09-18 20:40:56 +00003375 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003376 align, 0, ap->eof, 0, ap->conv,
Dave Chinner3a756672011-09-18 20:40:58 +00003377 &ap->offset, &ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003378 ASSERT(!error);
Dave Chinner3a756672011-09-18 20:40:58 +00003379 ASSERT(ap->length);
Nathan Scotta365bdd2006-03-14 13:34:16 +11003380 }
Dave Chinner33177f052013-12-12 16:34:36 +11003381
3382
Dave Chinner0937e0f2011-09-18 20:40:57 +00003383 nullfb = *ap->firstblock == NULLFSBLOCK;
3384 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
David Chinner2a82b8b2007-07-11 11:09:12 +10003385 if (nullfb) {
Dave Chinner292378e2016-09-26 08:21:28 +10003386 if (xfs_alloc_is_userdata(ap->datatype) &&
3387 xfs_inode_is_filestream(ap->ip)) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003388 ag = xfs_filestream_lookup_ag(ap->ip);
3389 ag = (ag != NULLAGNUMBER) ? ag : 0;
Dave Chinner3a756672011-09-18 20:40:58 +00003390 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
David Chinner2a82b8b2007-07-11 11:09:12 +10003391 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003392 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
David Chinner2a82b8b2007-07-11 11:09:12 +10003393 }
3394 } else
Dave Chinner3a756672011-09-18 20:40:58 +00003395 ap->blkno = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003396
3397 xfs_bmap_adjacent(ap);
3398
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 /*
Dave Chinner3a756672011-09-18 20:40:58 +00003400 * If allowed, use ap->blkno; otherwise must use firstblock since
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 * it's in the right allocation group.
3402 */
Dave Chinner3a756672011-09-18 20:40:58 +00003403 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 ;
3405 else
Dave Chinner3a756672011-09-18 20:40:58 +00003406 ap->blkno = *ap->firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 * Normal allocation, done through xfs_alloc_vextent.
3409 */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003410 tryagain = isaligned = 0;
Mark Tinguelya0041682012-09-20 13:16:45 -05003411 memset(&args, 0, sizeof(args));
Nathan Scotta365bdd2006-03-14 13:34:16 +11003412 args.tp = ap->tp;
3413 args.mp = mp;
Dave Chinner3a756672011-09-18 20:40:58 +00003414 args.fsbno = ap->blkno;
Darrick J. Wong340785c2016-08-03 11:33:42 +10003415 xfs_rmap_skip_owner_update(&args.oinfo);
Dave Chinner14b064c2011-01-27 12:16:28 +11003416
3417 /* Trim the allocation back to the maximum an AG can fit. */
Darrick J. Wong52548852016-08-03 11:38:24 +10003418 args.maxlen = MIN(ap->length, mp->m_ag_max_usable);
Dave Chinner0937e0f2011-09-18 20:40:57 +00003419 args.firstblock = *ap->firstblock;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003420 blen = 0;
3421 if (nullfb) {
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003422 /*
3423 * Search for an allocation group with a single extent large
3424 * enough for the request. If one isn't found, then adjust
3425 * the minimum allocation size to the largest space found.
3426 */
Dave Chinner292378e2016-09-26 08:21:28 +10003427 if (xfs_alloc_is_userdata(ap->datatype) &&
3428 xfs_inode_is_filestream(ap->ip))
Christoph Hellwigc977eb12014-04-23 07:11:41 +10003429 error = xfs_bmap_btalloc_filestreams(ap, &args, &blen);
3430 else
3431 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
Christoph Hellwigc467c042010-02-15 23:34:42 +00003432 if (error)
3433 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003434 } else if (ap->dfops->dop_low) {
David Chinner2a82b8b2007-07-11 11:09:12 +10003435 if (xfs_inode_is_filestream(ap->ip))
3436 args.type = XFS_ALLOCTYPE_FIRST_AG;
3437 else
3438 args.type = XFS_ALLOCTYPE_START_BNO;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003439 args.total = args.minlen = ap->minlen;
3440 } else {
3441 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3442 args.total = ap->total;
3443 args.minlen = ap->minlen;
3444 }
David Chinner957d0eb2007-06-18 16:50:37 +10003445 /* apply extent size hints if obtained earlier */
Christoph Hellwig493611e2017-01-25 08:59:43 -08003446 if (align) {
David Chinner957d0eb2007-06-18 16:50:37 +10003447 args.prod = align;
Dave Chinner3a756672011-09-18 20:40:58 +00003448 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003449 args.mod = (xfs_extlen_t)(args.prod - args.mod);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003450 } else if (mp->m_sb.sb_blocksize >= PAGE_SIZE) {
Nathan Scotta365bdd2006-03-14 13:34:16 +11003451 args.prod = 1;
3452 args.mod = 0;
3453 } else {
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03003454 args.prod = PAGE_SIZE >> mp->m_sb.sb_blocklog;
Dave Chinner3a756672011-09-18 20:40:58 +00003455 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003456 args.mod = (xfs_extlen_t)(args.prod - args.mod);
3457 }
3458 /*
3459 * If we are not low on available data blocks, and the
3460 * underlying logical volume manager is a stripe, and
3461 * the file offset is zero then try to allocate data
3462 * blocks on stripe unit boundary.
3463 * NOTE: ap->aeof is only set if the allocation length
3464 * is >= the stripe unit and the allocation offset is
3465 * at the end of file.
3466 */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003467 if (!ap->dfops->dop_low && ap->aeof) {
Dave Chinner3a756672011-09-18 20:40:58 +00003468 if (!ap->offset) {
Dave Chinner33177f052013-12-12 16:34:36 +11003469 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003470 atype = args.type;
3471 isaligned = 1;
3472 /*
3473 * Adjust for alignment
3474 */
Dave Chinner14b064c2011-01-27 12:16:28 +11003475 if (blen > args.alignment && blen <= args.maxlen)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003476 args.minlen = blen - args.alignment;
3477 args.minalignslop = 0;
3478 } else {
3479 /*
3480 * First try an exact bno allocation.
3481 * If it fails then do a near or start bno
3482 * allocation with alignment turned on.
3483 */
3484 atype = args.type;
3485 tryagain = 1;
3486 args.type = XFS_ALLOCTYPE_THIS_BNO;
3487 args.alignment = 1;
3488 /*
3489 * Compute the minlen+alignment for the
3490 * next case. Set slop so that the value
3491 * of minlen+alignment+slop doesn't go up
3492 * between the calls.
3493 */
Dave Chinner33177f052013-12-12 16:34:36 +11003494 if (blen > stripe_align && blen <= args.maxlen)
3495 nextminlen = blen - stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003496 else
3497 nextminlen = args.minlen;
Dave Chinner33177f052013-12-12 16:34:36 +11003498 if (nextminlen + stripe_align > args.minlen + 1)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003499 args.minalignslop =
Dave Chinner33177f052013-12-12 16:34:36 +11003500 nextminlen + stripe_align -
Nathan Scotta365bdd2006-03-14 13:34:16 +11003501 args.minlen - 1;
3502 else
3503 args.minalignslop = 0;
3504 }
3505 } else {
3506 args.alignment = 1;
3507 args.minalignslop = 0;
3508 }
3509 args.minleft = ap->minleft;
3510 args.wasdel = ap->wasdel;
Darrick J. Wong3fd129b2016-09-19 10:30:52 +10003511 args.resv = XFS_AG_RESV_NONE;
Dave Chinner292378e2016-09-26 08:21:28 +10003512 args.datatype = ap->datatype;
3513 if (ap->datatype & XFS_ALLOC_USERDATA_ZERO)
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003514 args.ip = ap->ip;
3515
3516 error = xfs_alloc_vextent(&args);
3517 if (error)
Nathan Scotta365bdd2006-03-14 13:34:16 +11003518 return error;
Dave Chinner3fbbbea2015-11-03 12:27:22 +11003519
Nathan Scotta365bdd2006-03-14 13:34:16 +11003520 if (tryagain && args.fsbno == NULLFSBLOCK) {
3521 /*
3522 * Exact allocation failed. Now try with alignment
3523 * turned on.
3524 */
3525 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003526 args.fsbno = ap->blkno;
Dave Chinner33177f052013-12-12 16:34:36 +11003527 args.alignment = stripe_align;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003528 args.minlen = nextminlen;
3529 args.minalignslop = 0;
3530 isaligned = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 if ((error = xfs_alloc_vextent(&args)))
3532 return error;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003533 }
3534 if (isaligned && args.fsbno == NULLFSBLOCK) {
3535 /*
3536 * allocation failed, so turn off alignment and
3537 * try again.
3538 */
3539 args.type = atype;
Dave Chinner3a756672011-09-18 20:40:58 +00003540 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003541 args.alignment = 0;
3542 if ((error = xfs_alloc_vextent(&args)))
3543 return error;
3544 }
3545 if (args.fsbno == NULLFSBLOCK && nullfb &&
3546 args.minlen > ap->minlen) {
3547 args.minlen = ap->minlen;
3548 args.type = XFS_ALLOCTYPE_START_BNO;
Dave Chinner3a756672011-09-18 20:40:58 +00003549 args.fsbno = ap->blkno;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003550 if ((error = xfs_alloc_vextent(&args)))
3551 return error;
3552 }
3553 if (args.fsbno == NULLFSBLOCK && nullfb) {
3554 args.fsbno = 0;
3555 args.type = XFS_ALLOCTYPE_FIRST_AG;
3556 args.total = ap->minlen;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003557 if ((error = xfs_alloc_vextent(&args)))
3558 return error;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10003559 ap->dfops->dop_low = true;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003560 }
3561 if (args.fsbno != NULLFSBLOCK) {
Dave Chinner0937e0f2011-09-18 20:40:57 +00003562 /*
3563 * check the allocation happened at the same or higher AG than
3564 * the first block that was allocated.
3565 */
3566 ASSERT(*ap->firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003567 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <=
3568 XFS_FSB_TO_AGNO(mp, args.fsbno));
Dave Chinner0937e0f2011-09-18 20:40:57 +00003569
Dave Chinner3a756672011-09-18 20:40:58 +00003570 ap->blkno = args.fsbno;
Dave Chinner0937e0f2011-09-18 20:40:57 +00003571 if (*ap->firstblock == NULLFSBLOCK)
3572 *ap->firstblock = args.fsbno;
Christoph Hellwig410d17f2017-02-16 17:12:51 -08003573 ASSERT(nullfb || fb_agno <= args.agno);
Dave Chinner3a756672011-09-18 20:40:58 +00003574 ap->length = args.len;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003575 if (!(ap->flags & XFS_BMAPI_COWFORK))
3576 ap->ip->i_d.di_nblocks += args.len;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003577 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
3578 if (ap->wasdel)
3579 ap->ip->i_delayed_blks -= args.len;
3580 /*
3581 * Adjust the disk quota also. This was reserved
3582 * earlier.
3583 */
Christoph Hellwig7d095252009-06-08 15:33:32 +02003584 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
Nathan Scotta365bdd2006-03-14 13:34:16 +11003585 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
3586 XFS_TRANS_DQ_BCOUNT,
3587 (long) args.len);
3588 } else {
Dave Chinner3a756672011-09-18 20:40:58 +00003589 ap->blkno = NULLFSBLOCK;
3590 ap->length = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 }
3592 return 0;
Nathan Scotta365bdd2006-03-14 13:34:16 +11003593}
3594
3595/*
3596 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
3597 * It figures out where to ask the underlying allocator to put the new extent.
3598 */
3599STATIC int
3600xfs_bmap_alloc(
Dave Chinner68988112013-08-12 20:49:42 +10003601 struct xfs_bmalloca *ap) /* bmap alloc argument struct */
Nathan Scotta365bdd2006-03-14 13:34:16 +11003602{
Dave Chinner292378e2016-09-26 08:21:28 +10003603 if (XFS_IS_REALTIME_INODE(ap->ip) &&
3604 xfs_alloc_is_userdata(ap->datatype))
Nathan Scotta365bdd2006-03-14 13:34:16 +11003605 return xfs_bmap_rtalloc(ap);
3606 return xfs_bmap_btalloc(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607}
3608
Darrick J. Wong0a0af282016-10-20 15:51:50 +11003609/* Trim extent to fit a logical block range. */
3610void
3611xfs_trim_extent(
3612 struct xfs_bmbt_irec *irec,
3613 xfs_fileoff_t bno,
3614 xfs_filblks_t len)
3615{
3616 xfs_fileoff_t distance;
3617 xfs_fileoff_t end = bno + len;
3618
3619 if (irec->br_startoff + irec->br_blockcount <= bno ||
3620 irec->br_startoff >= end) {
3621 irec->br_blockcount = 0;
3622 return;
3623 }
3624
3625 if (irec->br_startoff < bno) {
3626 distance = bno - irec->br_startoff;
3627 if (isnullstartblock(irec->br_startblock))
3628 irec->br_startblock = DELAYSTARTBLOCK;
3629 if (irec->br_startblock != DELAYSTARTBLOCK &&
3630 irec->br_startblock != HOLESTARTBLOCK)
3631 irec->br_startblock += distance;
3632 irec->br_startoff += distance;
3633 irec->br_blockcount -= distance;
3634 }
3635
3636 if (end < irec->br_startoff + irec->br_blockcount) {
3637 distance = irec->br_startoff + irec->br_blockcount - end;
3638 irec->br_blockcount -= distance;
3639 }
3640}
3641
Brian Foster40214d12017-10-13 09:47:46 -07003642/* trim extent to within eof */
3643void
3644xfs_trim_extent_eof(
3645 struct xfs_bmbt_irec *irec,
3646 struct xfs_inode *ip)
3647
3648{
3649 xfs_trim_extent(irec, 0, XFS_B_TO_FSB(ip->i_mount,
3650 i_size_read(VFS_I(ip))));
3651}
3652
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653/*
Dave Chinneraef9a892011-09-18 20:40:44 +00003654 * Trim the returned map to the required bounds
3655 */
3656STATIC void
3657xfs_bmapi_trim_map(
3658 struct xfs_bmbt_irec *mval,
3659 struct xfs_bmbt_irec *got,
3660 xfs_fileoff_t *bno,
3661 xfs_filblks_t len,
3662 xfs_fileoff_t obno,
3663 xfs_fileoff_t end,
3664 int n,
3665 int flags)
3666{
3667 if ((flags & XFS_BMAPI_ENTIRE) ||
3668 got->br_startoff + got->br_blockcount <= obno) {
3669 *mval = *got;
3670 if (isnullstartblock(got->br_startblock))
3671 mval->br_startblock = DELAYSTARTBLOCK;
3672 return;
3673 }
3674
3675 if (obno > *bno)
3676 *bno = obno;
3677 ASSERT((*bno >= obno) || (n == 0));
3678 ASSERT(*bno < end);
3679 mval->br_startoff = *bno;
3680 if (isnullstartblock(got->br_startblock))
3681 mval->br_startblock = DELAYSTARTBLOCK;
3682 else
3683 mval->br_startblock = got->br_startblock +
3684 (*bno - got->br_startoff);
3685 /*
3686 * Return the minimum of what we got and what we asked for for
3687 * the length. We can use the len variable here because it is
3688 * modified below and we could have been there before coming
3689 * here if the first part of the allocation didn't overlap what
3690 * was asked for.
3691 */
3692 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
3693 got->br_blockcount - (*bno - got->br_startoff));
3694 mval->br_state = got->br_state;
3695 ASSERT(mval->br_blockcount <= len);
3696 return;
3697}
3698
3699/*
3700 * Update and validate the extent map to return
3701 */
3702STATIC void
3703xfs_bmapi_update_map(
3704 struct xfs_bmbt_irec **map,
3705 xfs_fileoff_t *bno,
3706 xfs_filblks_t *len,
3707 xfs_fileoff_t obno,
3708 xfs_fileoff_t end,
3709 int *n,
3710 int flags)
3711{
3712 xfs_bmbt_irec_t *mval = *map;
3713
3714 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
3715 ((mval->br_startoff + mval->br_blockcount) <= end));
3716 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
3717 (mval->br_startoff < obno));
3718
3719 *bno = mval->br_startoff + mval->br_blockcount;
3720 *len = end - *bno;
3721 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3722 /* update previous map with new information */
3723 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3724 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3725 ASSERT(mval->br_state == mval[-1].br_state);
3726 mval[-1].br_blockcount = mval->br_blockcount;
3727 mval[-1].br_state = mval->br_state;
3728 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3729 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3730 mval[-1].br_startblock != HOLESTARTBLOCK &&
3731 mval->br_startblock == mval[-1].br_startblock +
3732 mval[-1].br_blockcount &&
3733 ((flags & XFS_BMAPI_IGSTATE) ||
3734 mval[-1].br_state == mval->br_state)) {
3735 ASSERT(mval->br_startoff ==
3736 mval[-1].br_startoff + mval[-1].br_blockcount);
3737 mval[-1].br_blockcount += mval->br_blockcount;
3738 } else if (*n > 0 &&
3739 mval->br_startblock == DELAYSTARTBLOCK &&
3740 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3741 mval->br_startoff ==
3742 mval[-1].br_startoff + mval[-1].br_blockcount) {
3743 mval[-1].br_blockcount += mval->br_blockcount;
3744 mval[-1].br_state = mval->br_state;
3745 } else if (!((*n == 0) &&
3746 ((mval->br_startoff + mval->br_blockcount) <=
3747 obno))) {
3748 mval++;
3749 (*n)++;
3750 }
3751 *map = mval;
3752}
3753
3754/*
Dave Chinner5c8ed202011-09-18 20:40:45 +00003755 * Map file blocks to filesystem blocks without allocation.
3756 */
3757int
3758xfs_bmapi_read(
3759 struct xfs_inode *ip,
3760 xfs_fileoff_t bno,
3761 xfs_filblks_t len,
3762 struct xfs_bmbt_irec *mval,
3763 int *nmap,
3764 int flags)
3765{
3766 struct xfs_mount *mp = ip->i_mount;
3767 struct xfs_ifork *ifp;
3768 struct xfs_bmbt_irec got;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003769 xfs_fileoff_t obno;
3770 xfs_fileoff_t end;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003771 xfs_extnum_t idx;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003772 int error;
Christoph Hellwig334f3422016-11-24 11:39:43 +11003773 bool eof = false;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003774 int n = 0;
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003775 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003776
3777 ASSERT(*nmap >= 1);
3778 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003779 XFS_BMAPI_IGSTATE|XFS_BMAPI_COWFORK)));
Christoph Hellwigeef334e2013-12-06 12:30:17 -08003780 ASSERT(xfs_isilocked(ip, XFS_ILOCK_SHARED|XFS_ILOCK_EXCL));
Dave Chinner5c8ed202011-09-18 20:40:45 +00003781
3782 if (unlikely(XFS_TEST_ERROR(
3783 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3784 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07003785 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinner5c8ed202011-09-18 20:40:45 +00003786 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10003787 return -EFSCORRUPTED;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003788 }
3789
3790 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10003791 return -EIO;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003792
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11003793 XFS_STATS_INC(mp, xs_blk_mapr);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003794
3795 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinner5c8ed202011-09-18 20:40:45 +00003796
Darrick J. Wong3993bae2016-10-03 09:11:32 -07003797 /* No CoW fork? Return a hole. */
3798 if (whichfork == XFS_COW_FORK && !ifp) {
3799 mval->br_startoff = bno;
3800 mval->br_startblock = HOLESTARTBLOCK;
3801 mval->br_blockcount = len;
3802 mval->br_state = XFS_EXT_NORM;
3803 *nmap = 1;
3804 return 0;
3805 }
3806
Dave Chinner5c8ed202011-09-18 20:40:45 +00003807 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3808 error = xfs_iread_extents(NULL, ip, whichfork);
3809 if (error)
3810 return error;
3811 }
3812
Christoph Hellwig334f3422016-11-24 11:39:43 +11003813 if (!xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got))
3814 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003815 end = bno + len;
3816 obno = bno;
3817
3818 while (bno < end && n < *nmap) {
3819 /* Reading past eof, act as though there's a hole up to end. */
3820 if (eof)
3821 got.br_startoff = end;
3822 if (got.br_startoff > bno) {
3823 /* Reading in a hole. */
3824 mval->br_startoff = bno;
3825 mval->br_startblock = HOLESTARTBLOCK;
3826 mval->br_blockcount =
3827 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3828 mval->br_state = XFS_EXT_NORM;
3829 bno += mval->br_blockcount;
3830 len -= mval->br_blockcount;
3831 mval++;
3832 n++;
3833 continue;
3834 }
3835
3836 /* set up the extent map to return. */
3837 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
3838 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
3839
3840 /* If we're done, stop now. */
3841 if (bno >= end || n >= *nmap)
3842 break;
3843
3844 /* Else go on to the next record. */
Christoph Hellwig334f3422016-11-24 11:39:43 +11003845 if (!xfs_iext_get_extent(ifp, ++idx, &got))
3846 eof = true;
Dave Chinner5c8ed202011-09-18 20:40:45 +00003847 }
3848 *nmap = n;
3849 return 0;
3850}
3851
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003852/*
3853 * Add a delayed allocation extent to an inode. Blocks are reserved from the
3854 * global pool and the extent inserted into the inode in-core extent tree.
3855 *
3856 * On entry, got refers to the first extent beyond the offset of the extent to
3857 * allocate or eof is specified if no such extent exists. On return, got refers
3858 * to the extent record that was inserted to the inode fork.
3859 *
3860 * Note that the allocated extent may have been merged with contiguous extents
3861 * during insertion into the inode fork. Thus, got does not reflect the current
3862 * state of the inode fork on return. If necessary, the caller can use lastx to
3863 * look up the updated record in the inode fork.
3864 */
Christoph Hellwig51446f52016-09-19 11:10:21 +10003865int
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003866xfs_bmapi_reserve_delalloc(
3867 struct xfs_inode *ip,
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003868 int whichfork,
Brian Foster974ae922016-11-28 14:57:42 +11003869 xfs_fileoff_t off,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003870 xfs_filblks_t len,
Brian Foster974ae922016-11-28 14:57:42 +11003871 xfs_filblks_t prealloc,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003872 struct xfs_bmbt_irec *got,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003873 xfs_extnum_t *lastx,
3874 int eof)
3875{
3876 struct xfs_mount *mp = ip->i_mount;
Darrick J. Wongbe51f812016-10-03 09:11:32 -07003877 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003878 xfs_extlen_t alen;
3879 xfs_extlen_t indlen;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003880 char rt = XFS_IS_REALTIME_INODE(ip);
3881 xfs_extlen_t extsz;
3882 int error;
Brian Foster974ae922016-11-28 14:57:42 +11003883 xfs_fileoff_t aoff = off;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003884
Brian Foster974ae922016-11-28 14:57:42 +11003885 /*
3886 * Cap the alloc length. Keep track of prealloc so we know whether to
3887 * tag the inode before we return.
3888 */
3889 alen = XFS_FILBLKS_MIN(len + prealloc, MAXEXTLEN);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003890 if (!eof)
3891 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
Brian Foster974ae922016-11-28 14:57:42 +11003892 if (prealloc && alen >= len)
3893 prealloc = alen - len;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003894
3895 /* Figure out the extent size, adjust alen */
Darrick J. Wongf7ca3522016-10-03 09:11:43 -07003896 if (whichfork == XFS_COW_FORK)
3897 extsz = xfs_get_cowextsz_hint(ip);
3898 else
3899 extsz = xfs_get_extsz_hint(ip);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003900 if (extsz) {
Christoph Hellwig65c5f412016-11-24 11:39:44 +11003901 struct xfs_bmbt_irec prev;
3902
3903 if (!xfs_iext_get_extent(ifp, *lastx - 1, &prev))
3904 prev.br_startoff = NULLFILEOFF;
3905
3906 error = xfs_bmap_extsize_align(mp, got, &prev, extsz, rt, eof,
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003907 1, 0, &aoff, &alen);
3908 ASSERT(!error);
3909 }
3910
3911 if (rt)
3912 extsz = alen / mp->m_sb.sb_rextsize;
3913
3914 /*
3915 * Make a transaction-less quota reservation for delayed allocation
3916 * blocks. This number gets adjusted later. We return if we haven't
3917 * allocated blocks already inside this loop.
3918 */
3919 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
3920 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
3921 if (error)
3922 return error;
3923
3924 /*
3925 * Split changing sb for alen and indlen since they could be coming
3926 * from different places.
3927 */
3928 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
3929 ASSERT(indlen > 0);
3930
3931 if (rt) {
Dave Chinnerbab98bb2015-02-23 21:22:54 +11003932 error = xfs_mod_frextents(mp, -((int64_t)extsz));
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003933 } else {
Dave Chinner0d485ad2015-02-23 21:22:03 +11003934 error = xfs_mod_fdblocks(mp, -((int64_t)alen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003935 }
3936
3937 if (error)
3938 goto out_unreserve_quota;
3939
Dave Chinner0d485ad2015-02-23 21:22:03 +11003940 error = xfs_mod_fdblocks(mp, -((int64_t)indlen), false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003941 if (error)
3942 goto out_unreserve_blocks;
3943
3944
3945 ip->i_delayed_blks += alen;
3946
3947 got->br_startoff = aoff;
3948 got->br_startblock = nullstartblock(indlen);
3949 got->br_blockcount = alen;
3950 got->br_state = XFS_EXT_NORM;
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003951
Brian Fosterf65e6fa2017-03-08 09:58:08 -08003952 xfs_bmap_add_extent_hole_delay(ip, whichfork, lastx, got);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003953
Brian Foster974ae922016-11-28 14:57:42 +11003954 /*
3955 * Tag the inode if blocks were preallocated. Note that COW fork
3956 * preallocation can occur at the start or end of the extent, even when
3957 * prealloc == 0, so we must also check the aligned offset and length.
3958 */
3959 if (whichfork == XFS_DATA_FORK && prealloc)
3960 xfs_inode_set_eofblocks_tag(ip);
3961 if (whichfork == XFS_COW_FORK && (prealloc || aoff < off || alen > len))
3962 xfs_inode_set_cowblocks_tag(ip);
3963
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003964 return 0;
3965
3966out_unreserve_blocks:
3967 if (rt)
Dave Chinnerbab98bb2015-02-23 21:22:54 +11003968 xfs_mod_frextents(mp, extsz);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003969 else
Dave Chinner0d485ad2015-02-23 21:22:03 +11003970 xfs_mod_fdblocks(mp, alen, false);
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003971out_unreserve_quota:
3972 if (XFS_IS_QUOTA_ON(mp))
Dave Chinnerea562ed2012-05-08 20:48:53 +10003973 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
Christoph Hellwigb64dfe42011-09-18 20:40:47 +00003974 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
3975 return error;
3976}
3977
Dave Chinnercf11da92014-07-15 07:08:24 +10003978static int
3979xfs_bmapi_allocate(
Dave Chinnere04426b2012-10-05 11:06:59 +10003980 struct xfs_bmalloca *bma)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003981{
3982 struct xfs_mount *mp = bma->ip->i_mount;
Darrick J. Wong60b49842016-10-03 09:11:34 -07003983 int whichfork = xfs_bmapi_whichfork(bma->flags);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003984 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00003985 int tmp_logflags = 0;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003986 int error;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003987
Dave Chinnera99ebf42011-12-01 11:24:20 +00003988 ASSERT(bma->length > 0);
3989
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003990 /*
3991 * For the wasdelay case, we could also just allocate the stuff asked
3992 * for in this bmap call but that wouldn't be as good.
3993 */
3994 if (bma->wasdel) {
Dave Chinner963c30c2011-09-18 20:40:59 +00003995 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
3996 bma->offset = bma->got.br_startoff;
Christoph Hellwige3f0f752017-10-17 14:16:20 -07003997 if (bma->idx)
3998 xfs_iext_get_extent(ifp, bma->idx - 1, &bma->prev);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00003999 } else {
Dave Chinner963c30c2011-09-18 20:40:59 +00004000 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004001 if (!bma->eof)
Dave Chinner963c30c2011-09-18 20:40:59 +00004002 bma->length = XFS_FILBLKS_MIN(bma->length,
Dave Chinner3a756672011-09-18 20:40:58 +00004003 bma->got.br_startoff - bma->offset);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004004 }
4005
4006 /*
Dave Chinner292378e2016-09-26 08:21:28 +10004007 * Set the data type being allocated. For the data fork, the first data
4008 * in the file is treated differently to all other allocations. For the
4009 * attribute fork, we only need to ensure the allocated range is not on
4010 * the busy list.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004011 */
Dave Chinnere04426b2012-10-05 11:06:59 +10004012 if (!(bma->flags & XFS_BMAPI_METADATA)) {
Dave Chinner292378e2016-09-26 08:21:28 +10004013 bma->datatype = XFS_ALLOC_NOBUSY;
4014 if (whichfork == XFS_DATA_FORK) {
4015 if (bma->offset == 0)
4016 bma->datatype |= XFS_ALLOC_INITIAL_USER_DATA;
4017 else
4018 bma->datatype |= XFS_ALLOC_USERDATA;
4019 }
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004020 if (bma->flags & XFS_BMAPI_ZERO)
Dave Chinner292378e2016-09-26 08:21:28 +10004021 bma->datatype |= XFS_ALLOC_USERDATA_ZERO;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004022 }
4023
Dave Chinnere04426b2012-10-05 11:06:59 +10004024 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004025
4026 /*
4027 * Only want to do the alignment at the eof if it is userdata and
4028 * allocation length is larger than a stripe unit.
4029 */
Dave Chinner963c30c2011-09-18 20:40:59 +00004030 if (mp->m_dalign && bma->length >= mp->m_dalign &&
Dave Chinnere04426b2012-10-05 11:06:59 +10004031 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
Dave Chinner1b164472011-09-18 20:40:55 +00004032 error = xfs_bmap_isaeof(bma, whichfork);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004033 if (error)
4034 return error;
4035 }
4036
4037 error = xfs_bmap_alloc(bma);
4038 if (error)
4039 return error;
4040
Dave Chinner29c8d172011-09-18 20:41:00 +00004041 if (bma->cur)
4042 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Dave Chinner963c30c2011-09-18 20:40:59 +00004043 if (bma->blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004044 return 0;
Dave Chinner29c8d172011-09-18 20:41:00 +00004045 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4046 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4047 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004048 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004049 }
4050 /*
4051 * Bump the number of extents we've allocated
4052 * in this call.
4053 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004054 bma->nallocs++;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004055
Dave Chinner29c8d172011-09-18 20:41:00 +00004056 if (bma->cur)
4057 bma->cur->bc_private.b.flags =
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004058 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4059
Dave Chinner963c30c2011-09-18 20:40:59 +00004060 bma->got.br_startoff = bma->offset;
4061 bma->got.br_startblock = bma->blkno;
4062 bma->got.br_blockcount = bma->length;
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004063 bma->got.br_state = XFS_EXT_NORM;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004064
4065 /*
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004066 * In the data fork, a wasdelay extent has been initialized, so
4067 * shouldn't be flagged as unwritten.
4068 *
4069 * For the cow fork, however, we convert delalloc reservations
4070 * (extents allocated for speculative preallocation) to
4071 * allocated unwritten extents, and only convert the unwritten
4072 * extents to real extents when we're about to write the data.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004073 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004074 if ((!bma->wasdel || (bma->flags & XFS_BMAPI_COWFORK)) &&
4075 (bma->flags & XFS_BMAPI_PREALLOC) &&
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004076 xfs_sb_version_hasextflgbit(&mp->m_sb))
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004077 bma->got.br_state = XFS_EXT_UNWRITTEN;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004078
Christoph Hellwigc6534242011-09-18 20:41:05 +00004079 if (bma->wasdel)
Darrick J. Wong60b49842016-10-03 09:11:34 -07004080 error = xfs_bmap_add_extent_delay_real(bma, whichfork);
Christoph Hellwigc6534242011-09-18 20:41:05 +00004081 else
Christoph Hellwig6d045582017-04-11 16:45:54 -07004082 error = xfs_bmap_add_extent_hole_real(bma->tp, bma->ip,
4083 whichfork, &bma->idx, &bma->cur, &bma->got,
4084 bma->firstblock, bma->dfops, &bma->logflags);
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004085
Christoph Hellwigc315c902011-09-18 20:41:02 +00004086 bma->logflags |= tmp_logflags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004087 if (error)
4088 return error;
4089
4090 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004091 * Update our extent pointer, given that xfs_bmap_add_extent_delay_real
4092 * or xfs_bmap_add_extent_hole_real might have merged it into one of
4093 * the neighbouring ones.
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004094 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004095 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004096
Dave Chinner963c30c2011-09-18 20:40:59 +00004097 ASSERT(bma->got.br_startoff <= bma->offset);
4098 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4099 bma->offset + bma->length);
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004100 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4101 bma->got.br_state == XFS_EXT_UNWRITTEN);
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004102 return 0;
4103}
4104
Dave Chinnerb447fe52011-09-18 20:40:51 +00004105STATIC int
4106xfs_bmapi_convert_unwritten(
4107 struct xfs_bmalloca *bma,
4108 struct xfs_bmbt_irec *mval,
4109 xfs_filblks_t len,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004110 int flags)
Dave Chinnerb447fe52011-09-18 20:40:51 +00004111{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07004112 int whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004113 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004114 int tmp_logflags = 0;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004115 int error;
4116
Dave Chinnerb447fe52011-09-18 20:40:51 +00004117 /* check if we need to do unwritten->real conversion */
4118 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4119 (flags & XFS_BMAPI_PREALLOC))
4120 return 0;
4121
4122 /* check if we need to do real->unwritten conversion */
4123 if (mval->br_state == XFS_EXT_NORM &&
4124 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4125 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4126 return 0;
4127
4128 /*
4129 * Modify (by adding) the state flag, if writing.
4130 */
4131 ASSERT(mval->br_blockcount <= len);
Dave Chinner29c8d172011-09-18 20:41:00 +00004132 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4133 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
Dave Chinnerb447fe52011-09-18 20:40:51 +00004134 bma->ip, whichfork);
Dave Chinner29c8d172011-09-18 20:41:00 +00004135 bma->cur->bc_private.b.firstblock = *bma->firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004136 bma->cur->bc_private.b.dfops = bma->dfops;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004137 }
4138 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4139 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4140
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004141 /*
4142 * Before insertion into the bmbt, zero the range being converted
4143 * if required.
4144 */
4145 if (flags & XFS_BMAPI_ZERO) {
4146 error = xfs_zero_extent(bma->ip, mval->br_startblock,
4147 mval->br_blockcount);
4148 if (error)
4149 return error;
4150 }
4151
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004152 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, whichfork,
4153 &bma->idx, &bma->cur, mval, bma->firstblock, bma->dfops,
Christoph Hellwigc315c902011-09-18 20:41:02 +00004154 &tmp_logflags);
Brian Foster2e588a42015-06-01 07:15:23 +10004155 /*
4156 * Log the inode core unconditionally in the unwritten extent conversion
4157 * path because the conversion might not have done so (e.g., if the
4158 * extent count hasn't changed). We need to make sure the inode is dirty
4159 * in the transaction for the sake of fsync(), even if nothing has
4160 * changed, because fsync() will not force the log for this transaction
4161 * unless it sees the inode pinned.
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004162 *
4163 * Note: If we're only converting cow fork extents, there aren't
4164 * any on-disk updates to make, so we don't need to log anything.
Brian Foster2e588a42015-06-01 07:15:23 +10004165 */
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004166 if (whichfork != XFS_COW_FORK)
4167 bma->logflags |= tmp_logflags | XFS_ILOG_CORE;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004168 if (error)
4169 return error;
4170
4171 /*
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00004172 * Update our extent pointer, given that
4173 * xfs_bmap_add_extent_unwritten_real might have merged it into one
4174 * of the neighbouring ones.
Dave Chinnerb447fe52011-09-18 20:40:51 +00004175 */
Christoph Hellwige3f0f752017-10-17 14:16:20 -07004176 xfs_iext_get_extent(ifp, bma->idx, &bma->got);
Dave Chinnerb447fe52011-09-18 20:40:51 +00004177
4178 /*
4179 * We may have combined previously unwritten space with written space,
4180 * so generate another request.
4181 */
4182 if (mval->br_blockcount < len)
Dave Chinner24513372014-06-25 14:58:08 +10004183 return -EAGAIN;
Dave Chinnerb447fe52011-09-18 20:40:51 +00004184 return 0;
4185}
4186
Christoph Hellwig44032802011-09-18 20:40:48 +00004187/*
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004188 * Map file blocks to filesystem blocks, and allocate blocks or convert the
4189 * extent state if necessary. Details behaviour is controlled by the flags
4190 * parameter. Only allocates blocks from a single allocation group, to avoid
4191 * locking problems.
4192 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 * The returned value in "firstblock" from the first call in a transaction
4194 * must be remembered and presented to subsequent calls in "firstblock".
4195 * An upper bound for the number of blocks to be allocated is supplied to
4196 * the first call in "total"; if no allocation group has that many free
4197 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
4198 */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004199int
4200xfs_bmapi_write(
4201 struct xfs_trans *tp, /* transaction pointer */
4202 struct xfs_inode *ip, /* incore inode */
4203 xfs_fileoff_t bno, /* starting file offs. mapped */
4204 xfs_filblks_t len, /* length to map in file */
4205 int flags, /* XFS_BMAPI_... */
4206 xfs_fsblock_t *firstblock, /* first allocated block
4207 controls a.g. for allocs */
4208 xfs_extlen_t total, /* total blocks needed */
4209 struct xfs_bmbt_irec *mval, /* output: map values */
4210 int *nmap, /* i/o: mval size/count */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004211 struct xfs_defer_ops *dfops) /* i/o: list extents to free */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212{
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004213 struct xfs_mount *mp = ip->i_mount;
4214 struct xfs_ifork *ifp;
Dave Chinnera30b0362013-09-02 20:49:36 +10004215 struct xfs_bmalloca bma = { NULL }; /* args for xfs_bmap_alloc */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004216 xfs_fileoff_t end; /* end of mapped file region */
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004217 bool eof = false; /* after the end of extents */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004218 int error; /* error return */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004219 int n; /* current extent index */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004220 xfs_fileoff_t obno; /* old block number (offset) */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004221 int whichfork; /* data or attr fork */
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223#ifdef DEBUG
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004224 xfs_fileoff_t orig_bno; /* original block number value */
4225 int orig_flags; /* original flags arg value */
4226 xfs_filblks_t orig_len; /* original value of len arg */
4227 struct xfs_bmbt_irec *orig_mval; /* original value of mval */
4228 int orig_nmap; /* original value of *nmap */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
4230 orig_bno = bno;
4231 orig_len = len;
4232 orig_flags = flags;
4233 orig_mval = mval;
4234 orig_nmap = *nmap;
4235#endif
Darrick J. Wong60b49842016-10-03 09:11:34 -07004236 whichfork = xfs_bmapi_whichfork(flags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 ASSERT(*nmap >= 1);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004239 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4240 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
Darrick J. Wong05a630d2017-02-02 15:14:01 -08004241 ASSERT(tp != NULL ||
4242 (flags & (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK)) ==
4243 (XFS_BMAPI_CONVERT | XFS_BMAPI_COWFORK));
Dave Chinnera99ebf42011-12-01 11:24:20 +00004244 ASSERT(len > 0);
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004245 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL);
Christoph Hellwigeef334e2013-12-06 12:30:17 -08004246 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004247 ASSERT(!(flags & XFS_BMAPI_REMAP));
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004248
Dave Chinner3fbbbea2015-11-03 12:27:22 +11004249 /* zeroing is for currently only for data extents, not metadata */
4250 ASSERT((flags & (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO)) !=
4251 (XFS_BMAPI_METADATA | XFS_BMAPI_ZERO));
4252 /*
4253 * we can allocate unwritten extents or pre-zero allocated blocks,
4254 * but it makes no sense to do both at once. This would result in
4255 * zeroing the unwritten extent twice, but it still being an
4256 * unwritten extent....
4257 */
4258 ASSERT((flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO)) !=
4259 (XFS_BMAPI_PREALLOC | XFS_BMAPI_ZERO));
4260
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 if (unlikely(XFS_TEST_ERROR(
4262 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
Dave Chinnerf3508bc2013-07-10 07:04:00 +10004263 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004264 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004265 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10004266 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 }
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004268
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10004270 return -EIO;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004271
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 ifp = XFS_IFORK_PTR(ip, whichfork);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004273
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004274 XFS_STATS_INC(mp, xs_blk_mapw);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004275
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004276 if (*firstblock == NULLFSBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
Dave Chinner0937e0f2011-09-18 20:40:57 +00004278 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 else
Dave Chinner0937e0f2011-09-18 20:40:57 +00004280 bma.minleft = 1;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004281 } else {
Dave Chinner0937e0f2011-09-18 20:40:57 +00004282 bma.minleft = 0;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004283 }
4284
4285 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4286 error = xfs_iread_extents(tp, ip, whichfork);
4287 if (error)
4288 goto error0;
4289 }
4290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 n = 0;
4292 end = bno + len;
4293 obno = bno;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004294
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004295 if (!xfs_iext_lookup_extent(ip, ifp, bno, &bma.idx, &bma.got))
4296 eof = true;
4297 if (!xfs_iext_get_extent(ifp, bma.idx - 1, &bma.prev))
4298 bma.prev.br_startoff = NULLFILEOFF;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004299 bma.tp = tp;
4300 bma.ip = ip;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004301 bma.total = total;
Dave Chinner292378e2016-09-26 08:21:28 +10004302 bma.datatype = 0;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004303 bma.dfops = dfops;
Dave Chinner0937e0f2011-09-18 20:40:57 +00004304 bma.firstblock = firstblock;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10004305
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 while (bno < end && n < *nmap) {
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004307 bool need_alloc = false, wasdelay = false;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004308
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004309 /* in hole or beyoned EOF? */
4310 if (eof || bma.got.br_startoff > bno) {
4311 if (flags & XFS_BMAPI_DELALLOC) {
4312 /*
4313 * For the COW fork we can reasonably get a
4314 * request for converting an extent that races
4315 * with other threads already having converted
4316 * part of it, as there converting COW to
4317 * regular blocks is not protected using the
4318 * IOLOCK.
4319 */
4320 ASSERT(flags & XFS_BMAPI_COWFORK);
4321 if (!(flags & XFS_BMAPI_COWFORK)) {
4322 error = -EIO;
4323 goto error0;
4324 }
4325
4326 if (eof || bno >= end)
4327 break;
4328 } else {
4329 need_alloc = true;
4330 }
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004331 } else if (isnullstartblock(bma.got.br_startblock)) {
4332 wasdelay = true;
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004333 }
Darrick J. Wongf65306e2016-10-03 09:11:27 -07004334
4335 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 * First, deal with the hole before the allocated space
4337 * that we found, if any.
4338 */
Christoph Hellwigd2b39642017-01-20 09:31:54 -08004339 if (need_alloc || wasdelay) {
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004340 bma.eof = eof;
4341 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
4342 bma.wasdel = wasdelay;
Dave Chinner3a756672011-09-18 20:40:58 +00004343 bma.offset = bno;
Dave Chinnere04426b2012-10-05 11:06:59 +10004344 bma.flags = flags;
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004345
Dave Chinnera99ebf42011-12-01 11:24:20 +00004346 /*
4347 * There's a 32/64 bit type mismatch between the
4348 * allocation length request (which can be 64 bits in
4349 * length) and the bma length request, which is
4350 * xfs_extlen_t and therefore 32 bits. Hence we have to
4351 * check for 32-bit overflows and handle them here.
4352 */
4353 if (len > (xfs_filblks_t)MAXEXTLEN)
4354 bma.length = MAXEXTLEN;
4355 else
4356 bma.length = len;
4357
4358 ASSERT(len > 0);
4359 ASSERT(bma.length > 0);
Dave Chinnere04426b2012-10-05 11:06:59 +10004360 error = xfs_bmapi_allocate(&bma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 if (error)
4362 goto error0;
Dave Chinner3a756672011-09-18 20:40:58 +00004363 if (bma.blkno == NULLFSBLOCK)
Dave Chinner7e47a4e2011-09-18 20:40:50 +00004364 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -07004365
4366 /*
4367 * If this is a CoW allocation, record the data in
4368 * the refcount btree for orphan recovery.
4369 */
4370 if (whichfork == XFS_COW_FORK) {
4371 error = xfs_refcount_alloc_cow_extent(mp, dfops,
4372 bma.blkno, bma.length);
4373 if (error)
4374 goto error0;
4375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 }
Christoph Hellwig44032802011-09-18 20:40:48 +00004377
Dave Chinneraef9a892011-09-18 20:40:44 +00004378 /* Deal with the allocated space we found. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004379 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
4380 end, n, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381
Dave Chinnerb447fe52011-09-18 20:40:51 +00004382 /* Execute unwritten extent conversion if necessary */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004383 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
Dave Chinner24513372014-06-25 14:58:08 +10004384 if (error == -EAGAIN)
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004385 continue;
4386 if (error)
4387 goto error0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Dave Chinneraef9a892011-09-18 20:40:44 +00004389 /* update the extent map to return */
4390 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4391
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 /*
4393 * If we're done, stop now. Stop when we've allocated
4394 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4395 * the transaction may get too big.
4396 */
Dave Chinnere0c3da52011-09-18 20:41:01 +00004397 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 break;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004399
4400 /* Else go on to the next record. */
Dave Chinnerbaf41a52011-09-18 20:40:56 +00004401 bma.prev = bma.got;
Christoph Hellwig2d58f6e2016-11-24 11:39:43 +11004402 if (!xfs_iext_get_extent(ifp, ++bma.idx, &bma.got))
4403 eof = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 *nmap = n;
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004406
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 /*
4408 * Transform from btree to extents, give it cur.
4409 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004410 if (xfs_bmap_wants_extents(ip, whichfork)) {
Christoph Hellwigc315c902011-09-18 20:41:02 +00004411 int tmp_logflags = 0;
4412
Dave Chinner29c8d172011-09-18 20:41:00 +00004413 ASSERT(bma.cur);
4414 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 &tmp_logflags, whichfork);
Christoph Hellwigc315c902011-09-18 20:41:02 +00004416 bma.logflags |= tmp_logflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 if (error)
4418 goto error0;
4419 }
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004420
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00004422 XFS_IFORK_NEXTENTS(ip, whichfork) >
4423 XFS_IFORK_MAXEXT(ip, whichfork));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425error0:
4426 /*
4427 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11004428 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004430 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004432 bma.logflags &= ~xfs_ilog_fext(whichfork);
4433 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Christoph Hellwigc315c902011-09-18 20:41:02 +00004435 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 /*
4437 * Log whatever the flags say, even if error. Otherwise we might miss
4438 * detecting a case where the data is changed, there's an error,
4439 * and it's not logged so we don't shutdown when we should.
4440 */
Christoph Hellwigc315c902011-09-18 20:41:02 +00004441 if (bma.logflags)
4442 xfs_trans_log_inode(tp, ip, bma.logflags);
Dave Chinnerc0dc7822011-09-18 20:40:52 +00004443
Dave Chinner29c8d172011-09-18 20:41:00 +00004444 if (bma.cur) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 if (!error) {
4446 ASSERT(*firstblock == NULLFSBLOCK ||
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004447 XFS_FSB_TO_AGNO(mp, *firstblock) <=
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 XFS_FSB_TO_AGNO(mp,
Christoph Hellwig410d17f2017-02-16 17:12:51 -08004449 bma.cur->bc_private.b.firstblock));
Dave Chinner29c8d172011-09-18 20:41:00 +00004450 *firstblock = bma.cur->bc_private.b.firstblock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 }
Dave Chinner29c8d172011-09-18 20:41:00 +00004452 xfs_btree_del_cursor(bma.cur,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4454 }
4455 if (!error)
4456 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4457 orig_nmap, *nmap);
4458 return error;
4459}
4460
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004461static int
4462xfs_bmapi_remap(
4463 struct xfs_trans *tp,
4464 struct xfs_inode *ip,
4465 xfs_fileoff_t bno,
4466 xfs_filblks_t len,
4467 xfs_fsblock_t startblock,
4468 struct xfs_defer_ops *dfops)
4469{
4470 struct xfs_mount *mp = ip->i_mount;
4471 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4472 struct xfs_btree_cur *cur = NULL;
4473 xfs_fsblock_t firstblock = NULLFSBLOCK;
4474 struct xfs_bmbt_irec got;
4475 xfs_extnum_t idx;
4476 int logflags = 0, error;
4477
4478 ASSERT(len > 0);
4479 ASSERT(len <= (xfs_filblks_t)MAXEXTLEN);
4480 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
4481
4482 if (unlikely(XFS_TEST_ERROR(
4483 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4484 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07004485 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004486 XFS_ERROR_REPORT("xfs_bmapi_remap", XFS_ERRLEVEL_LOW, mp);
4487 return -EFSCORRUPTED;
4488 }
4489
4490 if (XFS_FORCED_SHUTDOWN(mp))
4491 return -EIO;
4492
4493 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4494 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4495 if (error)
4496 return error;
4497 }
4498
4499 if (xfs_iext_lookup_extent(ip, ifp, bno, &idx, &got)) {
4500 /* make sure we only reflink into a hole. */
4501 ASSERT(got.br_startoff > bno);
4502 ASSERT(got.br_startoff - bno >= len);
4503 }
4504
Christoph Hellwigbf8eadb2017-04-11 16:45:56 -07004505 ip->i_d.di_nblocks += len;
4506 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07004507
4508 if (ifp->if_flags & XFS_IFBROOT) {
4509 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
4510 cur->bc_private.b.firstblock = firstblock;
4511 cur->bc_private.b.dfops = dfops;
4512 cur->bc_private.b.flags = 0;
4513 }
4514
4515 got.br_startoff = bno;
4516 got.br_startblock = startblock;
4517 got.br_blockcount = len;
4518 got.br_state = XFS_EXT_NORM;
4519
4520 error = xfs_bmap_add_extent_hole_real(tp, ip, XFS_DATA_FORK, &idx, &cur,
4521 &got, &firstblock, dfops, &logflags);
4522 if (error)
4523 goto error0;
4524
4525 if (xfs_bmap_wants_extents(ip, XFS_DATA_FORK)) {
4526 int tmp_logflags = 0;
4527
4528 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4529 &tmp_logflags, XFS_DATA_FORK);
4530 logflags |= tmp_logflags;
4531 }
4532
4533error0:
4534 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS)
4535 logflags &= ~XFS_ILOG_DEXT;
4536 else if (ip->i_d.di_format != XFS_DINODE_FMT_BTREE)
4537 logflags &= ~XFS_ILOG_DBROOT;
4538
4539 if (logflags)
4540 xfs_trans_log_inode(tp, ip, logflags);
4541 if (cur) {
4542 xfs_btree_del_cursor(cur,
4543 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4544 }
4545 return error;
4546}
4547
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548/*
Brian Fostera9bd24a2016-03-15 11:42:46 +11004549 * When a delalloc extent is split (e.g., due to a hole punch), the original
4550 * indlen reservation must be shared across the two new extents that are left
4551 * behind.
4552 *
4553 * Given the original reservation and the worst case indlen for the two new
4554 * extents (as calculated by xfs_bmap_worst_indlen()), split the original
Brian Fosterd34999c92016-03-15 11:42:47 +11004555 * reservation fairly across the two new extents. If necessary, steal available
4556 * blocks from a deleted extent to make up a reservation deficiency (e.g., if
4557 * ores == 1). The number of stolen blocks is returned. The availability and
4558 * subsequent accounting of stolen blocks is the responsibility of the caller.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004559 */
Brian Fosterd34999c92016-03-15 11:42:47 +11004560static xfs_filblks_t
Brian Fostera9bd24a2016-03-15 11:42:46 +11004561xfs_bmap_split_indlen(
4562 xfs_filblks_t ores, /* original res. */
4563 xfs_filblks_t *indlen1, /* ext1 worst indlen */
Brian Fosterd34999c92016-03-15 11:42:47 +11004564 xfs_filblks_t *indlen2, /* ext2 worst indlen */
4565 xfs_filblks_t avail) /* stealable blocks */
Brian Fostera9bd24a2016-03-15 11:42:46 +11004566{
4567 xfs_filblks_t len1 = *indlen1;
4568 xfs_filblks_t len2 = *indlen2;
4569 xfs_filblks_t nres = len1 + len2; /* new total res. */
Brian Fosterd34999c92016-03-15 11:42:47 +11004570 xfs_filblks_t stolen = 0;
Brian Foster75d65362017-02-13 22:48:30 -08004571 xfs_filblks_t resfactor;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004572
4573 /*
Brian Fosterd34999c92016-03-15 11:42:47 +11004574 * Steal as many blocks as we can to try and satisfy the worst case
4575 * indlen for both new extents.
4576 */
Brian Foster75d65362017-02-13 22:48:30 -08004577 if (ores < nres && avail)
4578 stolen = XFS_FILBLKS_MIN(nres - ores, avail);
4579 ores += stolen;
4580
4581 /* nothing else to do if we've satisfied the new reservation */
4582 if (ores >= nres)
4583 return stolen;
Brian Fosterd34999c92016-03-15 11:42:47 +11004584
4585 /*
Brian Foster75d65362017-02-13 22:48:30 -08004586 * We can't meet the total required reservation for the two extents.
4587 * Calculate the percent of the overall shortage between both extents
4588 * and apply this percentage to each of the requested indlen values.
4589 * This distributes the shortage fairly and reduces the chances that one
4590 * of the two extents is left with nothing when extents are repeatedly
4591 * split.
Brian Fostera9bd24a2016-03-15 11:42:46 +11004592 */
Brian Foster75d65362017-02-13 22:48:30 -08004593 resfactor = (ores * 100);
4594 do_div(resfactor, nres);
4595 len1 *= resfactor;
4596 do_div(len1, 100);
4597 len2 *= resfactor;
4598 do_div(len2, 100);
4599 ASSERT(len1 + len2 <= ores);
4600 ASSERT(len1 < *indlen1 && len2 < *indlen2);
4601
4602 /*
4603 * Hand out the remainder to each extent. If one of the two reservations
4604 * is zero, we want to make sure that one gets a block first. The loop
4605 * below starts with len1, so hand len2 a block right off the bat if it
4606 * is zero.
4607 */
4608 ores -= (len1 + len2);
4609 ASSERT((*indlen1 - len1) + (*indlen2 - len2) >= ores);
4610 if (ores && !len2 && *indlen2) {
4611 len2++;
4612 ores--;
4613 }
4614 while (ores) {
4615 if (len1 < *indlen1) {
4616 len1++;
4617 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004618 }
Brian Foster75d65362017-02-13 22:48:30 -08004619 if (!ores)
Brian Fostera9bd24a2016-03-15 11:42:46 +11004620 break;
Brian Foster75d65362017-02-13 22:48:30 -08004621 if (len2 < *indlen2) {
4622 len2++;
4623 ores--;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004624 }
4625 }
4626
4627 *indlen1 = len1;
4628 *indlen2 = len2;
Brian Fosterd34999c92016-03-15 11:42:47 +11004629
4630 return stolen;
Brian Fostera9bd24a2016-03-15 11:42:46 +11004631}
4632
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004633int
4634xfs_bmap_del_extent_delay(
4635 struct xfs_inode *ip,
4636 int whichfork,
4637 xfs_extnum_t *idx,
4638 struct xfs_bmbt_irec *got,
4639 struct xfs_bmbt_irec *del)
4640{
4641 struct xfs_mount *mp = ip->i_mount;
4642 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4643 struct xfs_bmbt_irec new;
4644 int64_t da_old, da_new, da_diff = 0;
4645 xfs_fileoff_t del_endoff, got_endoff;
4646 xfs_filblks_t got_indlen, new_indlen, stolen;
Christoph Hellwig060ea652017-10-19 11:02:29 -07004647 int state = xfs_bmap_fork_to_state(whichfork);
4648 int error = 0;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004649 bool isrt;
4650
4651 XFS_STATS_INC(mp, xs_del_exlist);
4652
4653 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
4654 del_endoff = del->br_startoff + del->br_blockcount;
4655 got_endoff = got->br_startoff + got->br_blockcount;
4656 da_old = startblockval(got->br_startblock);
4657 da_new = 0;
4658
4659 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004660 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004661 ASSERT(del->br_blockcount > 0);
4662 ASSERT(got->br_startoff <= del->br_startoff);
4663 ASSERT(got_endoff >= del_endoff);
4664
4665 if (isrt) {
Eric Sandeen4f1adf32017-04-19 15:19:32 -07004666 uint64_t rtexts = XFS_FSB_TO_B(mp, del->br_blockcount);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004667
4668 do_div(rtexts, mp->m_sb.sb_rextsize);
4669 xfs_mod_frextents(mp, rtexts);
4670 }
4671
4672 /*
4673 * Update the inode delalloc counter now and wait to update the
4674 * sb counters as we might have to borrow some blocks for the
4675 * indirect block accounting.
4676 */
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004677 error = xfs_trans_reserve_quota_nblks(NULL, ip,
4678 -((long)del->br_blockcount), 0,
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004679 isrt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
Darrick J. Wong4fd29ec42016-11-08 11:59:26 +11004680 if (error)
4681 return error;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004682 ip->i_delayed_blks -= del->br_blockcount;
4683
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004684 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004685 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004686 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004687 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004688
Christoph Hellwig0173c682017-10-17 14:16:22 -07004689 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4690 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004691 /*
4692 * Matches the whole extent. Delete the entry.
4693 */
4694 xfs_iext_remove(ip, *idx, 1, state);
4695 --*idx;
4696 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004697 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004698 /*
4699 * Deleting the first part of the extent.
4700 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004701 got->br_startoff = del_endoff;
4702 got->br_blockcount -= del->br_blockcount;
4703 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4704 got->br_blockcount), da_old);
4705 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004706 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004707 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004708 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004709 /*
4710 * Deleting the last part of the extent.
4711 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004712 got->br_blockcount = got->br_blockcount - del->br_blockcount;
4713 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip,
4714 got->br_blockcount), da_old);
4715 got->br_startblock = nullstartblock((int)da_new);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004716 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004717 break;
4718 case 0:
4719 /*
4720 * Deleting the middle of the extent.
4721 *
4722 * Distribute the original indlen reservation across the two new
4723 * extents. Steal blocks from the deleted extent if necessary.
4724 * Stealing blocks simply fudges the fdblocks accounting below.
4725 * Warn if either of the new indlen reservations is zero as this
4726 * can lead to delalloc problems.
4727 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004728 got->br_blockcount = del->br_startoff - got->br_startoff;
4729 got_indlen = xfs_bmap_worst_indlen(ip, got->br_blockcount);
4730
4731 new.br_blockcount = got_endoff - del_endoff;
4732 new_indlen = xfs_bmap_worst_indlen(ip, new.br_blockcount);
4733
4734 WARN_ON_ONCE(!got_indlen || !new_indlen);
4735 stolen = xfs_bmap_split_indlen(da_old, &got_indlen, &new_indlen,
4736 del->br_blockcount);
4737
4738 got->br_startblock = nullstartblock((int)got_indlen);
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004739 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004740
4741 new.br_startoff = del_endoff;
4742 new.br_state = got->br_state;
4743 new.br_startblock = nullstartblock((int)new_indlen);
4744
4745 ++*idx;
4746 xfs_iext_insert(ip, *idx, 1, &new, state);
4747
4748 da_new = got_indlen + new_indlen - stolen;
4749 del->br_blockcount -= stolen;
4750 break;
4751 }
4752
4753 ASSERT(da_old >= da_new);
4754 da_diff = da_old - da_new;
4755 if (!isrt)
4756 da_diff += del->br_blockcount;
4757 if (da_diff)
4758 xfs_mod_fdblocks(mp, da_diff, false);
4759 return error;
4760}
4761
4762void
4763xfs_bmap_del_extent_cow(
4764 struct xfs_inode *ip,
4765 xfs_extnum_t *idx,
4766 struct xfs_bmbt_irec *got,
4767 struct xfs_bmbt_irec *del)
4768{
4769 struct xfs_mount *mp = ip->i_mount;
4770 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_COW_FORK);
4771 struct xfs_bmbt_irec new;
4772 xfs_fileoff_t del_endoff, got_endoff;
4773 int state = BMAP_COWFORK;
4774
4775 XFS_STATS_INC(mp, xs_del_exlist);
4776
4777 del_endoff = del->br_startoff + del->br_blockcount;
4778 got_endoff = got->br_startoff + got->br_blockcount;
4779
4780 ASSERT(*idx >= 0);
Eric Sandeen5d829302016-11-08 12:59:42 +11004781 ASSERT(*idx <= xfs_iext_count(ifp));
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004782 ASSERT(del->br_blockcount > 0);
4783 ASSERT(got->br_startoff <= del->br_startoff);
4784 ASSERT(got_endoff >= del_endoff);
4785 ASSERT(!isnullstartblock(got->br_startblock));
4786
4787 if (got->br_startoff == del->br_startoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004788 state |= BMAP_LEFT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004789 if (got_endoff == del_endoff)
Christoph Hellwig0173c682017-10-17 14:16:22 -07004790 state |= BMAP_RIGHT_FILLING;
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004791
Christoph Hellwig0173c682017-10-17 14:16:22 -07004792 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4793 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004794 /*
4795 * Matches the whole extent. Delete the entry.
4796 */
4797 xfs_iext_remove(ip, *idx, 1, state);
4798 --*idx;
4799 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004800 case BMAP_LEFT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004801 /*
4802 * Deleting the first part of the extent.
4803 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004804 got->br_startoff = del_endoff;
4805 got->br_blockcount -= del->br_blockcount;
4806 got->br_startblock = del->br_startblock + del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004807 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004808 break;
Christoph Hellwig0173c682017-10-17 14:16:22 -07004809 case BMAP_RIGHT_FILLING:
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004810 /*
4811 * Deleting the last part of the extent.
4812 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004813 got->br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004814 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004815 break;
4816 case 0:
4817 /*
4818 * Deleting the middle of the extent.
4819 */
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004820 got->br_blockcount = del->br_startoff - got->br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004821 xfs_iext_update_extent(ip, state, *idx, got);
Christoph Hellwigfa5c8362016-10-20 15:54:14 +11004822
4823 new.br_startoff = del_endoff;
4824 new.br_blockcount = got_endoff - del_endoff;
4825 new.br_state = got->br_state;
4826 new.br_startblock = del->br_startblock + del->br_blockcount;
4827
4828 ++*idx;
4829 xfs_iext_insert(ip, *idx, 1, &new, state);
4830 break;
4831 }
4832}
4833
Brian Fostera9bd24a2016-03-15 11:42:46 +11004834/*
Dave Chinner9e5987a2013-02-25 12:31:26 +11004835 * Called by xfs_bmapi to update file extent records and the btree
Christoph Hellwige1d75532017-10-17 14:16:21 -07004836 * after removing space.
Dave Chinner9e5987a2013-02-25 12:31:26 +11004837 */
4838STATIC int /* error */
Christoph Hellwige1d75532017-10-17 14:16:21 -07004839xfs_bmap_del_extent_real(
Dave Chinner9e5987a2013-02-25 12:31:26 +11004840 xfs_inode_t *ip, /* incore inode pointer */
4841 xfs_trans_t *tp, /* current transaction pointer */
4842 xfs_extnum_t *idx, /* extent number to update/delete */
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10004843 struct xfs_defer_ops *dfops, /* list of extents to be freed */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004844 xfs_btree_cur_t *cur, /* if null, not a btree */
4845 xfs_bmbt_irec_t *del, /* data to remove from extents */
4846 int *logflagsp, /* inode logging flags */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07004847 int whichfork, /* data or attr fork */
4848 int bflags) /* bmapi flags */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004849{
Dave Chinner9e5987a2013-02-25 12:31:26 +11004850 xfs_fsblock_t del_endblock=0; /* first block past del */
4851 xfs_fileoff_t del_endoff; /* first offset past del */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004852 int do_fx; /* free extent at end of routine */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004853 int error; /* error return value */
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004854 int flags = 0;/* inode logging flags */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004855 struct xfs_bmbt_irec got; /* current extent entry */
Dave Chinner9e5987a2013-02-25 12:31:26 +11004856 xfs_fileoff_t got_endoff; /* first offset past got */
4857 int i; /* temp state */
4858 xfs_ifork_t *ifp; /* inode fork pointer */
4859 xfs_mount_t *mp; /* mount structure */
4860 xfs_filblks_t nblks; /* quota/sb block count */
4861 xfs_bmbt_irec_t new; /* new record to be inserted */
4862 /* REFERENCED */
4863 uint qfield; /* quota field to update */
Christoph Hellwig060ea652017-10-19 11:02:29 -07004864 int state = xfs_bmap_fork_to_state(whichfork);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004865 struct xfs_bmbt_irec old;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004866
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11004867 mp = ip->i_mount;
4868 XFS_STATS_INC(mp, xs_del_exlist);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004869
Dave Chinner9e5987a2013-02-25 12:31:26 +11004870 ifp = XFS_IFORK_PTR(ip, whichfork);
Eric Sandeen5d829302016-11-08 12:59:42 +11004871 ASSERT((*idx >= 0) && (*idx < xfs_iext_count(ifp)));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004872 ASSERT(del->br_blockcount > 0);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004873 xfs_iext_get_extent(ifp, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004874 ASSERT(got.br_startoff <= del->br_startoff);
4875 del_endoff = del->br_startoff + del->br_blockcount;
4876 got_endoff = got.br_startoff + got.br_blockcount;
4877 ASSERT(got_endoff >= del_endoff);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004878 ASSERT(!isnullstartblock(got.br_startblock));
Dave Chinner9e5987a2013-02-25 12:31:26 +11004879 qfield = 0;
4880 error = 0;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004881
Christoph Hellwig1b24b632017-10-17 14:16:22 -07004882 /*
4883 * If it's the case where the directory code is running with no block
4884 * reservation, and the deleted block is in the middle of its extent,
4885 * and the resulting insert of an extent would cause transformation to
4886 * btree format, then reject it. The calling code will then swap blocks
4887 * around instead. We have to do this now, rather than waiting for the
4888 * conversion to btree format, since the transaction will be dirty then.
4889 */
4890 if (tp->t_blk_res == 0 &&
4891 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4892 XFS_IFORK_NEXTENTS(ip, whichfork) >=
4893 XFS_IFORK_MAXEXT(ip, whichfork) &&
4894 del->br_startoff > got.br_startoff && del_endoff < got_endoff)
4895 return -ENOSPC;
4896
4897 flags = XFS_ILOG_CORE;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004898 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
4899 xfs_fsblock_t bno;
4900 xfs_filblks_t len;
4901
4902 ASSERT(do_mod(del->br_blockcount, mp->m_sb.sb_rextsize) == 0);
4903 ASSERT(do_mod(del->br_startblock, mp->m_sb.sb_rextsize) == 0);
4904 bno = del->br_startblock;
4905 len = del->br_blockcount;
4906 do_div(bno, mp->m_sb.sb_rextsize);
4907 do_div(len, mp->m_sb.sb_rextsize);
4908 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
4909 if (error)
4910 goto done;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004911 do_fx = 0;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004912 nblks = len * mp->m_sb.sb_rextsize;
4913 qfield = XFS_TRANS_DQ_RTBCOUNT;
4914 } else {
4915 do_fx = 1;
4916 nblks = del->br_blockcount;
4917 qfield = XFS_TRANS_DQ_BCOUNT;
4918 }
4919
4920 del_endblock = del->br_startblock + del->br_blockcount;
4921 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07004922 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004923 if (error)
4924 goto done;
4925 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004926 }
Darrick J. Wong340785c2016-08-03 11:33:42 +10004927
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004928 if (got.br_startoff == del->br_startoff)
4929 state |= BMAP_LEFT_FILLING;
4930 if (got_endoff == del_endoff)
4931 state |= BMAP_RIGHT_FILLING;
4932
4933 switch (state & (BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING)) {
4934 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004935 /*
4936 * Matches the whole extent. Delete the entry.
4937 */
Christoph Hellwig35e62da2017-10-19 11:04:43 -07004938 xfs_iext_remove(ip, *idx, 1, state);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004939 --*idx;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004940
4941 XFS_IFORK_NEXT_SET(ip, whichfork,
4942 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
4943 flags |= XFS_ILOG_CORE;
4944 if (!cur) {
4945 flags |= xfs_ilog_fext(whichfork);
4946 break;
4947 }
4948 if ((error = xfs_btree_delete(cur, &i)))
4949 goto done;
Eric Sandeenc29aad42015-02-23 22:39:08 +11004950 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004951 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004952 case BMAP_LEFT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004953 /*
4954 * Deleting the first part of the extent.
4955 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004956 got.br_startoff = del_endoff;
4957 got.br_startblock = del_endblock;
4958 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004959 xfs_iext_update_extent(ip, state, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004960 if (!cur) {
4961 flags |= xfs_ilog_fext(whichfork);
4962 break;
4963 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004964 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004965 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004966 goto done;
4967 break;
Christoph Hellwig491f6f8a2017-10-17 14:16:23 -07004968 case BMAP_RIGHT_FILLING:
Dave Chinner9e5987a2013-02-25 12:31:26 +11004969 /*
4970 * Deleting the last part of the extent.
4971 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004972 got.br_blockcount -= del->br_blockcount;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004973 xfs_iext_update_extent(ip, state, *idx, &got);
Dave Chinner9e5987a2013-02-25 12:31:26 +11004974 if (!cur) {
4975 flags |= xfs_ilog_fext(whichfork);
4976 break;
4977 }
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004978 error = xfs_bmbt_update(cur, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004979 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11004980 goto done;
4981 break;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004982 case 0:
4983 /*
4984 * Deleting the middle of the extent.
4985 */
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004986 old = got;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004987
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004988 got.br_blockcount = del->br_startoff - got.br_startoff;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07004989 xfs_iext_update_extent(ip, state, *idx, &got);
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004990
Dave Chinner9e5987a2013-02-25 12:31:26 +11004991 new.br_startoff = del_endoff;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004992 new.br_blockcount = got_endoff - del_endoff;
Dave Chinner9e5987a2013-02-25 12:31:26 +11004993 new.br_state = got.br_state;
Christoph Hellwige1d75532017-10-17 14:16:21 -07004994 new.br_startblock = del_endblock;
Christoph Hellwig48fd52b2017-10-17 14:16:23 -07004995
Christoph Hellwige1d75532017-10-17 14:16:21 -07004996 flags |= XFS_ILOG_CORE;
4997 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07004998 error = xfs_bmbt_update(cur, &got);
Christoph Hellwige1d75532017-10-17 14:16:21 -07004999 if (error)
5000 goto done;
5001 error = xfs_btree_increment(cur, 0, &i);
5002 if (error)
5003 goto done;
5004 cur->bc_rec.b = new;
5005 error = xfs_btree_insert(cur, &i);
5006 if (error && error != -ENOSPC)
5007 goto done;
5008 /*
5009 * If get no-space back from btree insert, it tried a
5010 * split, and we have a zero block reservation. Fix up
5011 * our state and return the error.
5012 */
5013 if (error == -ENOSPC) {
5014 /*
5015 * Reset the cursor, don't trust it after any
5016 * insert operation.
5017 */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005018 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005019 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005020 goto done;
Christoph Hellwige1d75532017-10-17 14:16:21 -07005021 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5022 /*
5023 * Update the btree record back
5024 * to the original value.
5025 */
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005026 error = xfs_bmbt_update(cur, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005027 if (error)
Dave Chinner9e5987a2013-02-25 12:31:26 +11005028 goto done;
5029 /*
Christoph Hellwige1d75532017-10-17 14:16:21 -07005030 * Reset the extent record back
5031 * to the original value.
Dave Chinner9e5987a2013-02-25 12:31:26 +11005032 */
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005033 xfs_iext_update_extent(ip, state, *idx, &old);
Christoph Hellwige1d75532017-10-17 14:16:21 -07005034 flags = 0;
5035 error = -ENOSPC;
5036 goto done;
5037 }
5038 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, done);
5039 } else
5040 flags |= xfs_ilog_fext(whichfork);
5041 XFS_IFORK_NEXT_SET(ip, whichfork,
5042 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
Dave Chinner9e5987a2013-02-25 12:31:26 +11005043 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
5044 ++*idx;
5045 break;
5046 }
Darrick J. Wong9c194642016-08-03 12:16:05 +10005047
5048 /* remove reverse mapping */
Christoph Hellwige1d75532017-10-17 14:16:21 -07005049 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, del);
5050 if (error)
5051 goto done;
Darrick J. Wong9c194642016-08-03 12:16:05 +10005052
Dave Chinner9e5987a2013-02-25 12:31:26 +11005053 /*
5054 * If we need to, add to list of extents to delete.
5055 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005056 if (do_fx && !(bflags & XFS_BMAPI_REMAP)) {
Darrick J. Wong62aab202016-10-03 09:11:23 -07005057 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK) {
5058 error = xfs_refcount_decrease_extent(mp, dfops, del);
5059 if (error)
5060 goto done;
5061 } else
5062 xfs_bmap_add_free(mp, dfops, del->br_startblock,
5063 del->br_blockcount, NULL);
5064 }
5065
Dave Chinner9e5987a2013-02-25 12:31:26 +11005066 /*
5067 * Adjust inode # blocks in the file.
5068 */
5069 if (nblks)
5070 ip->i_d.di_nblocks -= nblks;
5071 /*
5072 * Adjust quota data.
5073 */
Darrick J. Wong4847acf2016-10-03 09:11:27 -07005074 if (qfield && !(bflags & XFS_BMAPI_REMAP))
Dave Chinner9e5987a2013-02-25 12:31:26 +11005075 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
5076
Dave Chinner9e5987a2013-02-25 12:31:26 +11005077done:
5078 *logflagsp = flags;
5079 return error;
5080}
5081
5082/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083 * Unmap (remove) blocks from a file.
5084 * If nexts is nonzero then the number of extents to remove is limited to
5085 * that value. If not all extents in the block range can be removed then
5086 * *done is set.
5087 */
5088int /* error */
Darrick J. Wong44535932016-10-03 09:11:29 -07005089__xfs_bunmapi(
Linus Torvalds1da177e2005-04-16 15:20:36 -07005090 xfs_trans_t *tp, /* transaction pointer */
5091 struct xfs_inode *ip, /* incore inode */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005092 xfs_fileoff_t start, /* first file offset deleted */
Darrick J. Wong44535932016-10-03 09:11:29 -07005093 xfs_filblks_t *rlen, /* i/o: amount remaining */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 int flags, /* misc flags */
5095 xfs_extnum_t nexts, /* number of extents max */
5096 xfs_fsblock_t *firstblock, /* first allocated block
5097 controls a.g. for allocs */
Darrick J. Wong44535932016-10-03 09:11:29 -07005098 struct xfs_defer_ops *dfops) /* i/o: deferred updates */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099{
5100 xfs_btree_cur_t *cur; /* bmap btree cursor */
5101 xfs_bmbt_irec_t del; /* extent being deleted */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 int error; /* error return value */
5103 xfs_extnum_t extno; /* extent number in list */
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005104 xfs_bmbt_irec_t got; /* current extent record */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 xfs_ifork_t *ifp; /* inode fork pointer */
5106 int isrt; /* freeing in rt area */
5107 xfs_extnum_t lastx; /* last extent index used */
5108 int logflags; /* transaction logging flags */
5109 xfs_extlen_t mod; /* rt extent offset */
5110 xfs_mount_t *mp; /* mount structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 int tmp_logflags; /* partial logging flags */
5112 int wasdel; /* was a delayed alloc extent */
5113 int whichfork; /* data or attribute fork */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 xfs_fsblock_t sum;
Darrick J. Wong44535932016-10-03 09:11:29 -07005115 xfs_filblks_t len = *rlen; /* length to unmap in file */
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005116 xfs_fileoff_t max_len;
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005117 xfs_agnumber_t prev_agno = NULLAGNUMBER, agno;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005118 xfs_fileoff_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005120 trace_xfs_bunmap(ip, start, len, flags, _RET_IP_);
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00005121
Darrick J. Wong3993bae2016-10-03 09:11:32 -07005122 whichfork = xfs_bmapi_whichfork(flags);
5123 ASSERT(whichfork != XFS_COW_FORK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124 ifp = XFS_IFORK_PTR(ip, whichfork);
5125 if (unlikely(
5126 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5127 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5128 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5129 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +10005130 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 }
5132 mp = ip->i_mount;
5133 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005134 return -EIO;
Christoph Hellwig54893272011-05-11 15:04:03 +00005135
Christoph Hellwigeef334e2013-12-06 12:30:17 -08005136 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137 ASSERT(len > 0);
5138 ASSERT(nexts >= 0);
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005139
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005140 /*
5141 * Guesstimate how many blocks we can unmap without running the risk of
5142 * blowing out the transaction with a mix of EFIs and reflink
5143 * adjustments.
5144 */
5145 if (xfs_is_reflink_inode(ip) && whichfork == XFS_DATA_FORK)
5146 max_len = min(len, xfs_refcount_max_unmap(tp->t_log_res));
5147 else
5148 max_len = len;
5149
Linus Torvalds1da177e2005-04-16 15:20:36 -07005150 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5151 (error = xfs_iread_extents(tp, ip, whichfork)))
5152 return error;
Eric Sandeen5d829302016-11-08 12:59:42 +11005153 if (xfs_iext_count(ifp) == 0) {
Darrick J. Wong44535932016-10-03 09:11:29 -07005154 *rlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155 return 0;
5156 }
Bill O'Donnellff6d6af2015-10-12 18:21:22 +11005157 XFS_STATS_INC(mp, xs_blk_unmap);
Nathan Scottdd9f4382006-01-11 15:28:28 +11005158 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005159 end = start + len - 1;
Christoph Hellwigb4e91812010-06-23 18:11:15 +10005160
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161 /*
5162 * Check to see if the given block number is past the end of the
5163 * file, back up to the last block if so...
5164 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005165 if (!xfs_iext_lookup_extent(ip, ifp, end, &lastx, &got)) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005166 ASSERT(lastx > 0);
5167 xfs_iext_get_extent(ifp, --lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005168 end = got.br_startoff + got.br_blockcount - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005169 }
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005170
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 logflags = 0;
5172 if (ifp->if_flags & XFS_IFBROOT) {
5173 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
Christoph Hellwig561f7d12008-10-30 16:53:59 +11005174 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005176 cur->bc_private.b.dfops = dfops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177 cur->bc_private.b.flags = 0;
5178 } else
5179 cur = NULL;
Kamal Dasu5575acc2012-02-23 00:41:39 +00005180
5181 if (isrt) {
5182 /*
5183 * Synchronize by locking the bitmap inode.
5184 */
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005185 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL|XFS_ILOCK_RTBITMAP);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005186 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
Darrick J. Wongf4a06602016-08-03 11:00:42 +10005187 xfs_ilock(mp->m_rsumip, XFS_ILOCK_EXCL|XFS_ILOCK_RTSUM);
5188 xfs_trans_ijoin(tp, mp->m_rsumip, XFS_ILOCK_EXCL);
Kamal Dasu5575acc2012-02-23 00:41:39 +00005189 }
5190
Linus Torvalds1da177e2005-04-16 15:20:36 -07005191 extno = 0;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005192 while (end != (xfs_fileoff_t)-1 && end >= start && lastx >= 0 &&
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005193 (nexts == 0 || extno < nexts) && max_len > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 /*
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005195 * Is the found extent after a hole in which end lives?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005196 * Just back up to the previous extent, if so.
5197 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005198 if (got.br_startoff > end) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199 if (--lastx < 0)
5200 break;
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005201 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005202 }
5203 /*
5204 * Is the last block of this extent before the range
5205 * we're supposed to delete? If so, we're done.
5206 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005207 end = XFS_FILEOFF_MIN(end,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 got.br_startoff + got.br_blockcount - 1);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005209 if (end < start)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005210 break;
5211 /*
5212 * Then deal with the (possibly delayed) allocated space
5213 * we found.
5214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215 del = got;
Eric Sandeen9d87c312009-01-14 23:22:07 -06005216 wasdel = isnullstartblock(del.br_startblock);
Christoph Hellwig5b094d62017-07-18 11:16:51 -07005217
5218 /*
5219 * Make sure we don't touch multiple AGF headers out of order
5220 * in a single transaction, as that could cause AB-BA deadlocks.
5221 */
5222 if (!wasdel) {
5223 agno = XFS_FSB_TO_AGNO(mp, del.br_startblock);
5224 if (prev_agno != NULLAGNUMBER && prev_agno > agno)
5225 break;
5226 prev_agno = agno;
5227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005228 if (got.br_startoff < start) {
5229 del.br_startoff = start;
5230 del.br_blockcount -= start - got.br_startoff;
5231 if (!wasdel)
5232 del.br_startblock += start - got.br_startoff;
5233 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005234 if (del.br_startoff + del.br_blockcount > end + 1)
5235 del.br_blockcount = end + 1 - del.br_startoff;
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005236
5237 /* How much can we safely unmap? */
5238 if (max_len < del.br_blockcount) {
5239 del.br_startoff += del.br_blockcount - max_len;
5240 if (!wasdel)
5241 del.br_startblock += del.br_blockcount - max_len;
5242 del.br_blockcount = max_len;
5243 }
5244
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 sum = del.br_startblock + del.br_blockcount;
5246 if (isrt &&
5247 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5248 /*
5249 * Realtime extent not lined up at the end.
5250 * The extent could have been split into written
5251 * and unwritten pieces, or we could just be
5252 * unmapping part of it. But we can't really
5253 * get rid of part of a realtime extent.
5254 */
5255 if (del.br_state == XFS_EXT_UNWRITTEN ||
Eric Sandeen62118702008-03-06 13:44:28 +11005256 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 /*
5258 * This piece is unwritten, or we're not
5259 * using unwritten extents. Skip over it.
5260 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005261 ASSERT(end >= mod);
5262 end -= mod > del.br_blockcount ?
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 del.br_blockcount : mod;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005264 if (end < got.br_startoff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005265 if (--lastx >= 0)
Christoph Hellwige3f0f752017-10-17 14:16:20 -07005266 xfs_iext_get_extent(ifp, lastx,
5267 &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 }
5269 continue;
5270 }
5271 /*
5272 * It's written, turn it unwritten.
5273 * This is better than zeroing it.
5274 */
5275 ASSERT(del.br_state == XFS_EXT_NORM);
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005276 ASSERT(tp->t_blk_res > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005277 /*
5278 * If this spans a realtime extent boundary,
5279 * chop it back to the start of the one we end at.
5280 */
5281 if (del.br_blockcount > mod) {
5282 del.br_startoff += del.br_blockcount - mod;
5283 del.br_startblock += del.br_blockcount - mod;
5284 del.br_blockcount = mod;
5285 }
5286 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005287 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005288 whichfork, &lastx, &cur, &del,
5289 firstblock, dfops, &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 if (error)
5291 goto error0;
5292 goto nodelete;
5293 }
5294 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5295 /*
5296 * Realtime extent is lined up at the end but not
5297 * at the front. We'll get rid of full extents if
5298 * we can.
5299 */
5300 mod = mp->m_sb.sb_rextsize - mod;
5301 if (del.br_blockcount > mod) {
5302 del.br_blockcount -= mod;
5303 del.br_startoff += mod;
5304 del.br_startblock += mod;
5305 } else if ((del.br_startoff == start &&
5306 (del.br_state == XFS_EXT_UNWRITTEN ||
Christoph Hellwiga7e5d032016-03-02 09:58:21 +11005307 tp->t_blk_res == 0)) ||
Eric Sandeen62118702008-03-06 13:44:28 +11005308 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 /*
5310 * Can't make it unwritten. There isn't
5311 * a full extent here so just skip it.
5312 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005313 ASSERT(end >= del.br_blockcount);
5314 end -= del.br_blockcount;
5315 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005316 xfs_iext_get_extent(ifp, lastx, &got);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 continue;
5318 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005319 struct xfs_bmbt_irec prev;
5320
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 /*
5322 * This one is already unwritten.
5323 * It must have a written left neighbor.
5324 * Unwrite the killed part of that one and
5325 * try again.
5326 */
5327 ASSERT(lastx > 0);
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005328 xfs_iext_get_extent(ifp, lastx - 1, &prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005329 ASSERT(prev.br_state == XFS_EXT_NORM);
Eric Sandeen9d87c312009-01-14 23:22:07 -06005330 ASSERT(!isnullstartblock(prev.br_startblock));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005331 ASSERT(del.br_startblock ==
5332 prev.br_startblock + prev.br_blockcount);
5333 if (prev.br_startoff < start) {
5334 mod = start - prev.br_startoff;
5335 prev.br_blockcount -= mod;
5336 prev.br_startblock += mod;
5337 prev.br_startoff = start;
5338 }
5339 prev.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwigec90c552011-05-23 08:52:53 +00005340 lastx--;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005341 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005342 ip, whichfork, &lastx, &cur,
5343 &prev, firstblock, dfops,
5344 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 if (error)
5346 goto error0;
5347 goto nodelete;
5348 } else {
5349 ASSERT(del.br_state == XFS_EXT_NORM);
5350 del.br_state = XFS_EXT_UNWRITTEN;
Christoph Hellwiga5bd606b2011-09-18 20:40:54 +00005351 error = xfs_bmap_add_extent_unwritten_real(tp,
Darrick J. Wong05a630d2017-02-02 15:14:01 -08005352 ip, whichfork, &lastx, &cur,
5353 &del, firstblock, dfops,
5354 &logflags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 if (error)
5356 goto error0;
5357 goto nodelete;
5358 }
5359 }
Nathan Scott06d10dd2005-06-21 15:48:47 +10005360
Brian Fosterb2706a02016-03-15 11:42:46 +11005361 if (wasdel) {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005362 error = xfs_bmap_del_extent_delay(ip, whichfork, &lastx,
5363 &got, &del);
5364 } else {
Christoph Hellwige1d75532017-10-17 14:16:21 -07005365 error = xfs_bmap_del_extent_real(ip, tp, &lastx, dfops,
5366 cur, &del, &tmp_logflags, whichfork,
5367 flags);
5368 logflags |= tmp_logflags;
Christoph Hellwigb213d692017-10-17 14:16:20 -07005369 }
Brian Fosterb2706a02016-03-15 11:42:46 +11005370
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 if (error)
5372 goto error0;
Brian Fosterb2706a02016-03-15 11:42:46 +11005373
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07005374 max_len -= del.br_blockcount;
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005375 end = del.br_startoff - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376nodelete:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 /*
5378 * If not done go on to the next (previous) record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 */
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005380 if (end != (xfs_fileoff_t)-1 && end >= start) {
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005381 if (lastx >= 0) {
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005382 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005383 if (got.br_startoff > end && --lastx >= 0)
Christoph Hellwig7efc7942016-11-24 11:39:44 +11005384 xfs_iext_get_extent(ifp, lastx, &got);
Christoph Hellwig00239ac2011-05-11 15:04:08 +00005385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386 extno++;
5387 }
5388 }
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005389 if (end == (xfs_fileoff_t)-1 || end < start || lastx < 0)
Darrick J. Wong44535932016-10-03 09:11:29 -07005390 *rlen = 0;
5391 else
Christoph Hellwig8280f6e2017-10-17 14:16:21 -07005392 *rlen = end - start + 1;
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005393
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 /*
5395 * Convert to a btree if necessary.
5396 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005397 if (xfs_bmap_needs_btree(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005399 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, dfops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 &cur, 0, &tmp_logflags, whichfork);
5401 logflags |= tmp_logflags;
5402 if (error)
5403 goto error0;
5404 }
5405 /*
5406 * transform from btree to extents, give it cur
5407 */
Christoph Hellwig8096b1e2011-12-18 20:00:07 +00005408 else if (xfs_bmap_wants_extents(ip, whichfork)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409 ASSERT(cur != NULL);
5410 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5411 whichfork);
5412 logflags |= tmp_logflags;
5413 if (error)
5414 goto error0;
5415 }
5416 /*
5417 * transform from extents to local?
5418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419 error = 0;
5420error0:
5421 /*
5422 * Log everything. Do this after conversion, there's no point in
Mandy Kirkconnell4eea22f2006-03-14 13:29:52 +11005423 * logging the extent records if we've converted to btree format.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 */
Eric Sandeen9d87c312009-01-14 23:22:07 -06005425 if ((logflags & xfs_ilog_fext(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005426 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005427 logflags &= ~xfs_ilog_fext(whichfork);
5428 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
Eric Sandeen9d87c312009-01-14 23:22:07 -06005430 logflags &= ~xfs_ilog_fbroot(whichfork);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 /*
5432 * Log inode even in the error case, if the transaction
5433 * is dirty we'll need to shut down the filesystem.
5434 */
5435 if (logflags)
5436 xfs_trans_log_inode(tp, ip, logflags);
5437 if (cur) {
5438 if (!error) {
5439 *firstblock = cur->bc_private.b.firstblock;
5440 cur->bc_private.b.allocated = 0;
5441 }
5442 xfs_btree_del_cursor(cur,
5443 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5444 }
5445 return error;
5446}
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005447
Darrick J. Wong44535932016-10-03 09:11:29 -07005448/* Unmap a range of a file. */
5449int
5450xfs_bunmapi(
5451 xfs_trans_t *tp,
5452 struct xfs_inode *ip,
5453 xfs_fileoff_t bno,
5454 xfs_filblks_t len,
5455 int flags,
5456 xfs_extnum_t nexts,
5457 xfs_fsblock_t *firstblock,
5458 struct xfs_defer_ops *dfops,
5459 int *done)
5460{
5461 int error;
5462
5463 error = __xfs_bunmapi(tp, ip, bno, &len, flags, nexts, firstblock,
5464 dfops);
5465 *done = (len == 0);
5466 return error;
5467}
5468
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005469/*
Brian Fosterddb19e32014-09-23 15:38:09 +10005470 * Determine whether an extent shift can be accomplished by a merge with the
5471 * extent that precedes the target hole of the shift.
5472 */
5473STATIC bool
5474xfs_bmse_can_merge(
5475 struct xfs_bmbt_irec *left, /* preceding extent */
5476 struct xfs_bmbt_irec *got, /* current extent to shift */
5477 xfs_fileoff_t shift) /* shift fsb */
5478{
5479 xfs_fileoff_t startoff;
5480
5481 startoff = got->br_startoff - shift;
5482
5483 /*
5484 * The extent, once shifted, must be adjacent in-file and on-disk with
5485 * the preceding extent.
5486 */
5487 if ((left->br_startoff + left->br_blockcount != startoff) ||
5488 (left->br_startblock + left->br_blockcount != got->br_startblock) ||
5489 (left->br_state != got->br_state) ||
5490 (left->br_blockcount + got->br_blockcount > MAXEXTLEN))
5491 return false;
5492
5493 return true;
5494}
5495
5496/*
5497 * A bmap extent shift adjusts the file offset of an extent to fill a preceding
5498 * hole in the file. If an extent shift would result in the extent being fully
5499 * adjacent to the extent that currently precedes the hole, we can merge with
5500 * the preceding extent rather than do the shift.
5501 *
5502 * This function assumes the caller has verified a shift-by-merge is possible
5503 * with the provided extents via xfs_bmse_can_merge().
5504 */
5505STATIC int
5506xfs_bmse_merge(
5507 struct xfs_inode *ip,
5508 int whichfork,
5509 xfs_fileoff_t shift, /* shift fsb */
5510 int current_ext, /* idx of gotp */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005511 struct xfs_bmbt_irec *got, /* extent to shift */
5512 struct xfs_bmbt_irec *left, /* preceding extent */
Brian Fosterddb19e32014-09-23 15:38:09 +10005513 struct xfs_btree_cur *cur,
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005514 int *logflags, /* output */
5515 struct xfs_defer_ops *dfops)
Brian Fosterddb19e32014-09-23 15:38:09 +10005516{
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005517 struct xfs_bmbt_irec new;
Brian Fosterddb19e32014-09-23 15:38:09 +10005518 xfs_filblks_t blockcount;
5519 int error, i;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005520 struct xfs_mount *mp = ip->i_mount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005521
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005522 blockcount = left->br_blockcount + got->br_blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005523
5524 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
5525 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005526 ASSERT(xfs_bmse_can_merge(left, got, shift));
Brian Fosterddb19e32014-09-23 15:38:09 +10005527
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005528 new = *left;
5529 new.br_blockcount = blockcount;
Brian Fosterddb19e32014-09-23 15:38:09 +10005530
5531 /*
5532 * Update the on-disk extent count, the btree if necessary and log the
5533 * inode.
5534 */
5535 XFS_IFORK_NEXT_SET(ip, whichfork,
5536 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
5537 *logflags |= XFS_ILOG_CORE;
5538 if (!cur) {
5539 *logflags |= XFS_ILOG_DEXT;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005540 goto done;
Brian Fosterddb19e32014-09-23 15:38:09 +10005541 }
5542
5543 /* lookup and remove the extent to merge */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005544 error = xfs_bmbt_lookup_eq(cur, got, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005545 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005546 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005547 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005548
5549 error = xfs_btree_delete(cur, &i);
5550 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005551 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005552 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005553
5554 /* lookup and update size of the previous extent */
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005555 error = xfs_bmbt_lookup_eq(cur, left, &i);
Brian Fosterddb19e32014-09-23 15:38:09 +10005556 if (error)
Dave Chinner4db431f2014-12-04 09:42:40 +11005557 return error;
Eric Sandeen5fb5aee2015-02-23 22:39:13 +11005558 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
Brian Fosterddb19e32014-09-23 15:38:09 +10005559
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005560 error = xfs_bmbt_update(cur, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005561 if (error)
5562 return error;
Brian Fosterddb19e32014-09-23 15:38:09 +10005563
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005564done:
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005565 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
5566 current_ext - 1, &new);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005567 xfs_iext_remove(ip, current_ext, 1, 0);
5568
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005569 /* update reverse mapping. rmap functions merge the rmaps for us */
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005570 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, got);
5571 if (error)
5572 return error;
Darrick J. Wong4cc1ee52017-08-30 16:06:36 -07005573 memcpy(&new, got, sizeof(new));
5574 new.br_startoff = left->br_startoff + left->br_blockcount;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005575 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, &new);
Brian Fosterddb19e32014-09-23 15:38:09 +10005576}
5577
Christoph Hellwigbf806282017-10-19 11:07:34 -07005578static int
5579xfs_bmap_shift_update_extent(
5580 struct xfs_inode *ip,
5581 int whichfork,
5582 xfs_extnum_t idx,
5583 struct xfs_bmbt_irec *got,
5584 struct xfs_btree_cur *cur,
5585 int *logflags,
5586 struct xfs_defer_ops *dfops,
5587 xfs_fileoff_t startoff)
Brian Fostera979bdf2014-09-23 15:39:04 +10005588{
Christoph Hellwigbf806282017-10-19 11:07:34 -07005589 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005590 struct xfs_bmbt_irec prev = *got;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005591 int error, i;
Brian Fostera979bdf2014-09-23 15:39:04 +10005592
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005593 *logflags |= XFS_ILOG_CORE;
5594
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005595 got->br_startoff = startoff;
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005596
5597 if (cur) {
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005598 error = xfs_bmbt_lookup_eq(cur, &prev, &i);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005599 if (error)
5600 return error;
5601 XFS_WANT_CORRUPTED_RETURN(mp, i == 1);
5602
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005603 error = xfs_bmbt_update(cur, got);
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005604 if (error)
5605 return error;
5606 } else {
5607 *logflags |= XFS_ILOG_DEXT;
5608 }
5609
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005610 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork), idx, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005611
Darrick J. Wong9c194642016-08-03 12:16:05 +10005612 /* update reverse mapping */
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005613 error = xfs_rmap_unmap_extent(mp, dfops, ip, whichfork, &prev);
Darrick J. Wong9c194642016-08-03 12:16:05 +10005614 if (error)
5615 return error;
Christoph Hellwig11f75b32017-10-19 11:08:51 -07005616 return xfs_rmap_map_extent(mp, dfops, ip, whichfork, got);
Brian Fostera979bdf2014-09-23 15:39:04 +10005617}
5618
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005619int
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005620xfs_bmap_collapse_extents(
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005621 struct xfs_trans *tp,
5622 struct xfs_inode *ip,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005623 xfs_fileoff_t *next_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005624 xfs_fileoff_t offset_shift_fsb,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005625 bool *done,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005626 xfs_fileoff_t stop_fsb,
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005627 xfs_fsblock_t *firstblock,
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005628 struct xfs_defer_ops *dfops)
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005629{
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005630 int whichfork = XFS_DATA_FORK;
5631 struct xfs_mount *mp = ip->i_mount;
5632 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5633 struct xfs_btree_cur *cur = NULL;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005634 struct xfs_bmbt_irec got, prev;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005635 xfs_extnum_t current_ext;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005636 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005637 int error = 0;
5638 int logflags = 0;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005639
5640 if (unlikely(XFS_TEST_ERROR(
5641 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5642 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005643 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005644 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10005645 return -EFSCORRUPTED;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005646 }
5647
5648 if (XFS_FORCED_SHUTDOWN(mp))
Dave Chinner24513372014-06-25 14:58:08 +10005649 return -EIO;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005650
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005651 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005652
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005653 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005654 error = xfs_iread_extents(tp, ip, whichfork);
5655 if (error)
5656 return error;
5657 }
5658
Brian Fosterddb19e32014-09-23 15:38:09 +10005659 if (ifp->if_flags & XFS_IFBROOT) {
5660 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5661 cur->bc_private.b.firstblock = *firstblock;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005662 cur->bc_private.b.dfops = dfops;
Brian Fosterddb19e32014-09-23 15:38:09 +10005663 cur->bc_private.b.flags = 0;
5664 }
5665
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005666 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext, &got)) {
5667 *done = true;
5668 goto del_cursor;
5669 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005670 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005671
Christoph Hellwigbf806282017-10-19 11:07:34 -07005672 new_startoff = got.br_startoff - offset_shift_fsb;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005673 if (xfs_iext_get_extent(ifp, current_ext - 1, &prev)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005674 if (new_startoff < prev.br_startoff + prev.br_blockcount) {
5675 error = -EINVAL;
5676 goto del_cursor;
5677 }
5678
Christoph Hellwigbf806282017-10-19 11:07:34 -07005679 if (xfs_bmse_can_merge(&prev, &got, offset_shift_fsb)) {
5680 error = xfs_bmse_merge(ip, whichfork, offset_shift_fsb,
5681 current_ext, &got, &prev, cur,
5682 &logflags, dfops);
5683 if (error)
5684 goto del_cursor;
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005685
5686 /* update got after merge */
5687 if (!xfs_iext_get_extent(ifp, current_ext, &got)) {
5688 *done = true;
5689 goto del_cursor;
5690 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005691 goto done;
5692 }
5693 } else {
5694 if (got.br_startoff < offset_shift_fsb) {
5695 error = -EINVAL;
5696 goto del_cursor;
5697 }
5698 }
5699
5700 error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got,
5701 cur, &logflags, dfops, new_startoff);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005702 if (error)
5703 goto del_cursor;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005704
Christoph Hellwig40591bd2017-10-19 11:08:51 -07005705 if (!xfs_iext_get_extent(ifp, ++current_ext, &got)) {
5706 *done = true;
5707 goto del_cursor;
5708 }
5709
5710done:
Christoph Hellwigbf806282017-10-19 11:07:34 -07005711 *next_fsb = got.br_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005712del_cursor:
5713 if (cur)
5714 xfs_btree_del_cursor(cur,
5715 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005716 if (logflags)
5717 xfs_trans_log_inode(tp, ip, logflags);
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005718 return error;
5719}
5720
5721int
5722xfs_bmap_insert_extents(
5723 struct xfs_trans *tp,
5724 struct xfs_inode *ip,
5725 xfs_fileoff_t *next_fsb,
5726 xfs_fileoff_t offset_shift_fsb,
5727 bool *done,
5728 xfs_fileoff_t stop_fsb,
5729 xfs_fsblock_t *firstblock,
5730 struct xfs_defer_ops *dfops)
5731{
5732 int whichfork = XFS_DATA_FORK;
5733 struct xfs_mount *mp = ip->i_mount;
5734 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
5735 struct xfs_btree_cur *cur = NULL;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005736 struct xfs_bmbt_irec got, next;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005737 xfs_extnum_t current_ext;
Christoph Hellwigbf806282017-10-19 11:07:34 -07005738 xfs_fileoff_t new_startoff;
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005739 int error = 0;
5740 int logflags = 0;
5741
5742 if (unlikely(XFS_TEST_ERROR(
5743 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5744 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
5745 mp, XFS_ERRTAG_BMAPIFORMAT))) {
5746 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, mp);
5747 return -EFSCORRUPTED;
5748 }
5749
5750 if (XFS_FORCED_SHUTDOWN(mp))
5751 return -EIO;
5752
5753 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL));
5754
5755 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5756 error = xfs_iread_extents(tp, ip, whichfork);
5757 if (error)
5758 return error;
5759 }
5760
5761 if (ifp->if_flags & XFS_IFBROOT) {
5762 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5763 cur->bc_private.b.firstblock = *firstblock;
5764 cur->bc_private.b.dfops = dfops;
5765 cur->bc_private.b.flags = 0;
5766 }
5767
Namjae Jeona904b1c2015-03-25 15:08:56 +11005768 if (*next_fsb == NULLFSBLOCK) {
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005769 current_ext = xfs_iext_count(ifp) - 1;
5770 if (!xfs_iext_get_extent(ifp, current_ext, &got) ||
5771 stop_fsb > got.br_startoff) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005772 *done = true;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005773 goto del_cursor;
5774 }
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005775 } else {
Christoph Hellwig4da6b512017-08-29 15:44:13 -07005776 if (!xfs_iext_lookup_extent(ip, ifp, *next_fsb, &current_ext,
5777 &got)) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005778 *done = true;
Christoph Hellwig05b7c8a2017-08-29 15:44:12 -07005779 goto del_cursor;
5780 }
Namjae Jeona904b1c2015-03-25 15:08:56 +11005781 }
Christoph Hellwigbf806282017-10-19 11:07:34 -07005782 XFS_WANT_CORRUPTED_RETURN(mp, !isnullstartblock(got.br_startblock));
Namjae Jeona904b1c2015-03-25 15:08:56 +11005783
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005784 if (stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005785 error = -EIO;
5786 goto del_cursor;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005787 }
5788
Christoph Hellwigbf806282017-10-19 11:07:34 -07005789 new_startoff = got.br_startoff + offset_shift_fsb;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005790 if (xfs_iext_get_extent(ifp, current_ext + 1, &next)) {
Christoph Hellwigbf806282017-10-19 11:07:34 -07005791 if (new_startoff + got.br_blockcount > next.br_startoff) {
5792 error = -EINVAL;
5793 goto del_cursor;
5794 }
5795
5796 /*
5797 * Unlike a left shift (which involves a hole punch), a right
5798 * shift does not modify extent neighbors in any way. We should
5799 * never find mergeable extents in this scenario. Check anyways
5800 * and warn if we encounter two extents that could be one.
5801 */
5802 if (xfs_bmse_can_merge(&got, &next, offset_shift_fsb))
5803 WARN_ON_ONCE(1);
5804 }
5805
5806 error = xfs_bmap_shift_update_extent(ip, whichfork, current_ext, &got,
5807 cur, &logflags, dfops, new_startoff);
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005808 if (error)
5809 goto del_cursor;
Christoph Hellwig5936dc52017-10-19 11:08:52 -07005810
5811 if (!xfs_iext_get_extent(ifp, --current_ext, &got) ||
5812 stop_fsb >= got.br_startoff + got.br_blockcount) {
Christoph Hellwigecfea3f2017-10-19 11:07:11 -07005813 *done = true;
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005814 goto del_cursor;
5815 }
Christoph Hellwig6b18af02017-10-19 11:07:10 -07005816
5817 *next_fsb = got.br_startoff;
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005818del_cursor:
5819 if (cur)
5820 xfs_btree_del_cursor(cur,
5821 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
Brian Fosterca446d82014-09-02 12:12:53 +10005822 if (logflags)
5823 xfs_trans_log_inode(tp, ip, logflags);
Namjae Jeone1d8fb82014-02-24 10:58:19 +11005824 return error;
5825}
Namjae Jeona904b1c2015-03-25 15:08:56 +11005826
5827/*
5828 * Splits an extent into two extents at split_fsb block such that it is
5829 * the first block of the current_ext. @current_ext is a target extent
5830 * to be split. @split_fsb is a block where the extents is split.
5831 * If split_fsb lies in a hole or the first block of extents, just return 0.
5832 */
5833STATIC int
5834xfs_bmap_split_extent_at(
5835 struct xfs_trans *tp,
5836 struct xfs_inode *ip,
5837 xfs_fileoff_t split_fsb,
5838 xfs_fsblock_t *firstfsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005839 struct xfs_defer_ops *dfops)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005840{
5841 int whichfork = XFS_DATA_FORK;
5842 struct xfs_btree_cur *cur = NULL;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005843 struct xfs_bmbt_irec got;
5844 struct xfs_bmbt_irec new; /* split extent */
5845 struct xfs_mount *mp = ip->i_mount;
5846 struct xfs_ifork *ifp;
5847 xfs_fsblock_t gotblkcnt; /* new block count for got */
5848 xfs_extnum_t current_ext;
5849 int error = 0;
5850 int logflags = 0;
5851 int i = 0;
5852
5853 if (unlikely(XFS_TEST_ERROR(
5854 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5855 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07005856 mp, XFS_ERRTAG_BMAPIFORMAT))) {
Namjae Jeona904b1c2015-03-25 15:08:56 +11005857 XFS_ERROR_REPORT("xfs_bmap_split_extent_at",
5858 XFS_ERRLEVEL_LOW, mp);
5859 return -EFSCORRUPTED;
5860 }
5861
5862 if (XFS_FORCED_SHUTDOWN(mp))
5863 return -EIO;
5864
5865 ifp = XFS_IFORK_PTR(ip, whichfork);
5866 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5867 /* Read in all the extents */
5868 error = xfs_iread_extents(tp, ip, whichfork);
5869 if (error)
5870 return error;
5871 }
5872
5873 /*
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005874 * If there are not extents, or split_fsb lies in a hole we are done.
Namjae Jeona904b1c2015-03-25 15:08:56 +11005875 */
Christoph Hellwig4c35445b2017-08-29 15:44:13 -07005876 if (!xfs_iext_lookup_extent(ip, ifp, split_fsb, &current_ext, &got) ||
5877 got.br_startoff >= split_fsb)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005878 return 0;
5879
5880 gotblkcnt = split_fsb - got.br_startoff;
5881 new.br_startoff = split_fsb;
5882 new.br_startblock = got.br_startblock + gotblkcnt;
5883 new.br_blockcount = got.br_blockcount - gotblkcnt;
5884 new.br_state = got.br_state;
5885
5886 if (ifp->if_flags & XFS_IFBROOT) {
5887 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5888 cur->bc_private.b.firstblock = *firstfsb;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005889 cur->bc_private.b.dfops = dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005890 cur->bc_private.b.flags = 0;
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005891 error = xfs_bmbt_lookup_eq(cur, &got, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005892 if (error)
5893 goto del_cursor;
5894 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5895 }
5896
Namjae Jeona904b1c2015-03-25 15:08:56 +11005897 got.br_blockcount = gotblkcnt;
Christoph Hellwigca5d8e52017-10-19 11:04:44 -07005898 xfs_iext_update_extent(ip, xfs_bmap_fork_to_state(whichfork),
5899 current_ext, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005900
5901 logflags = XFS_ILOG_CORE;
5902 if (cur) {
Christoph Hellwiga67d00a2017-10-17 14:16:26 -07005903 error = xfs_bmbt_update(cur, &got);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005904 if (error)
5905 goto del_cursor;
5906 } else
5907 logflags |= XFS_ILOG_DEXT;
5908
5909 /* Add new extent */
5910 current_ext++;
5911 xfs_iext_insert(ip, current_ext, 1, &new, 0);
5912 XFS_IFORK_NEXT_SET(ip, whichfork,
5913 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
5914
5915 if (cur) {
Christoph Hellwige16cf9b2017-10-17 14:16:26 -07005916 error = xfs_bmbt_lookup_eq(cur, &new, &i);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005917 if (error)
5918 goto del_cursor;
5919 XFS_WANT_CORRUPTED_GOTO(mp, i == 0, del_cursor);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005920 error = xfs_btree_insert(cur, &i);
5921 if (error)
5922 goto del_cursor;
5923 XFS_WANT_CORRUPTED_GOTO(mp, i == 1, del_cursor);
5924 }
5925
5926 /*
5927 * Convert to a btree if necessary.
5928 */
5929 if (xfs_bmap_needs_btree(ip, whichfork)) {
5930 int tmp_logflags; /* partial log flag return val */
5931
5932 ASSERT(cur == NULL);
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005933 error = xfs_bmap_extents_to_btree(tp, ip, firstfsb, dfops,
Namjae Jeona904b1c2015-03-25 15:08:56 +11005934 &cur, 0, &tmp_logflags, whichfork);
5935 logflags |= tmp_logflags;
5936 }
5937
5938del_cursor:
5939 if (cur) {
5940 cur->bc_private.b.allocated = 0;
5941 xfs_btree_del_cursor(cur,
5942 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5943 }
5944
5945 if (logflags)
5946 xfs_trans_log_inode(tp, ip, logflags);
5947 return error;
5948}
5949
5950int
5951xfs_bmap_split_extent(
5952 struct xfs_inode *ip,
5953 xfs_fileoff_t split_fsb)
5954{
5955 struct xfs_mount *mp = ip->i_mount;
5956 struct xfs_trans *tp;
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005957 struct xfs_defer_ops dfops;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005958 xfs_fsblock_t firstfsb;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005959 int error;
5960
Christoph Hellwig253f4912016-04-06 09:19:55 +10005961 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
5962 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
5963 if (error)
Namjae Jeona904b1c2015-03-25 15:08:56 +11005964 return error;
Namjae Jeona904b1c2015-03-25 15:08:56 +11005965
5966 xfs_ilock(ip, XFS_ILOCK_EXCL);
5967 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
5968
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005969 xfs_defer_init(&dfops, &firstfsb);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005970
5971 error = xfs_bmap_split_extent_at(tp, ip, split_fsb,
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005972 &firstfsb, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005973 if (error)
5974 goto out;
5975
Christoph Hellwig8ad7c6292017-08-28 10:21:04 -07005976 error = xfs_defer_finish(&tp, &dfops);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005977 if (error)
5978 goto out;
5979
Christoph Hellwig70393312015-06-04 13:48:08 +10005980 return xfs_trans_commit(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005981
5982out:
Darrick J. Wong2c3234d2016-08-03 11:19:29 +10005983 xfs_defer_cancel(&dfops);
Christoph Hellwig4906e212015-06-04 13:47:56 +10005984 xfs_trans_cancel(tp);
Namjae Jeona904b1c2015-03-25 15:08:56 +11005985 return error;
5986}
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07005987
5988/* Deferred mapping is only for real extents in the data fork. */
5989static bool
5990xfs_bmap_is_update_needed(
5991 struct xfs_bmbt_irec *bmap)
5992{
5993 return bmap->br_startblock != HOLESTARTBLOCK &&
5994 bmap->br_startblock != DELAYSTARTBLOCK;
5995}
5996
5997/* Record a bmap intent. */
5998static int
5999__xfs_bmap_add(
6000 struct xfs_mount *mp,
6001 struct xfs_defer_ops *dfops,
6002 enum xfs_bmap_intent_type type,
6003 struct xfs_inode *ip,
6004 int whichfork,
6005 struct xfs_bmbt_irec *bmap)
6006{
6007 int error;
6008 struct xfs_bmap_intent *bi;
6009
6010 trace_xfs_bmap_defer(mp,
6011 XFS_FSB_TO_AGNO(mp, bmap->br_startblock),
6012 type,
6013 XFS_FSB_TO_AGBNO(mp, bmap->br_startblock),
6014 ip->i_ino, whichfork,
6015 bmap->br_startoff,
6016 bmap->br_blockcount,
6017 bmap->br_state);
6018
6019 bi = kmem_alloc(sizeof(struct xfs_bmap_intent), KM_SLEEP | KM_NOFS);
6020 INIT_LIST_HEAD(&bi->bi_list);
6021 bi->bi_type = type;
6022 bi->bi_owner = ip;
6023 bi->bi_whichfork = whichfork;
6024 bi->bi_bmap = *bmap;
6025
Christoph Hellwig882d8782017-08-28 10:21:03 -07006026 error = xfs_defer_ijoin(dfops, bi->bi_owner);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006027 if (error) {
6028 kmem_free(bi);
6029 return error;
6030 }
6031
6032 xfs_defer_add(dfops, XFS_DEFER_OPS_TYPE_BMAP, &bi->bi_list);
6033 return 0;
6034}
6035
6036/* Map an extent into a file. */
6037int
6038xfs_bmap_map_extent(
6039 struct xfs_mount *mp,
6040 struct xfs_defer_ops *dfops,
6041 struct xfs_inode *ip,
6042 struct xfs_bmbt_irec *PREV)
6043{
6044 if (!xfs_bmap_is_update_needed(PREV))
6045 return 0;
6046
6047 return __xfs_bmap_add(mp, dfops, XFS_BMAP_MAP, ip,
6048 XFS_DATA_FORK, PREV);
6049}
6050
6051/* Unmap an extent out of a file. */
6052int
6053xfs_bmap_unmap_extent(
6054 struct xfs_mount *mp,
6055 struct xfs_defer_ops *dfops,
6056 struct xfs_inode *ip,
6057 struct xfs_bmbt_irec *PREV)
6058{
6059 if (!xfs_bmap_is_update_needed(PREV))
6060 return 0;
6061
6062 return __xfs_bmap_add(mp, dfops, XFS_BMAP_UNMAP, ip,
6063 XFS_DATA_FORK, PREV);
6064}
6065
6066/*
6067 * Process one of the deferred bmap operations. We pass back the
6068 * btree cursor to maintain our lock on the bmapbt between calls.
6069 */
6070int
6071xfs_bmap_finish_one(
6072 struct xfs_trans *tp,
6073 struct xfs_defer_ops *dfops,
6074 struct xfs_inode *ip,
6075 enum xfs_bmap_intent_type type,
6076 int whichfork,
6077 xfs_fileoff_t startoff,
6078 xfs_fsblock_t startblock,
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006079 xfs_filblks_t *blockcount,
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006080 xfs_exntst_t state)
6081{
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006082 xfs_fsblock_t firstfsb;
6083 int error = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006084
Darrick J. Wong4c1a67b2017-07-17 14:30:51 -07006085 /*
6086 * firstfsb is tied to the transaction lifetime and is used to
6087 * ensure correct AG locking order and schedule work item
6088 * continuations. XFS_BUI_MAX_FAST_EXTENTS (== 1) restricts us
6089 * to only making one bmap call per transaction, so it should
6090 * be safe to have it as a local variable here.
6091 */
6092 firstfsb = NULLFSBLOCK;
6093
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006094 trace_xfs_bmap_deferred(tp->t_mountp,
6095 XFS_FSB_TO_AGNO(tp->t_mountp, startblock), type,
6096 XFS_FSB_TO_AGBNO(tp->t_mountp, startblock),
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006097 ip->i_ino, whichfork, startoff, *blockcount, state);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006098
Christoph Hellwig39e07da2017-04-11 16:45:53 -07006099 if (WARN_ON_ONCE(whichfork != XFS_DATA_FORK))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006100 return -EFSCORRUPTED;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006101
6102 if (XFS_TEST_ERROR(false, tp->t_mountp,
Darrick J. Wong9e24cfd2017-06-20 17:54:47 -07006103 XFS_ERRTAG_BMAP_FINISH_ONE))
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006104 return -EIO;
6105
6106 switch (type) {
6107 case XFS_BMAP_MAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006108 error = xfs_bmapi_remap(tp, ip, startoff, *blockcount,
Christoph Hellwig6ebd5a42017-04-11 16:45:55 -07006109 startblock, dfops);
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006110 *blockcount = 0;
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006111 break;
6112 case XFS_BMAP_UNMAP:
Darrick J. Wonge1a4e372017-06-14 21:25:57 -07006113 error = __xfs_bunmapi(tp, ip, startoff, blockcount,
6114 XFS_BMAPI_REMAP, 1, &firstfsb, dfops);
Darrick J. Wong9f3afb52016-10-03 09:11:28 -07006115 break;
6116 default:
6117 ASSERT(0);
6118 error = -EFSCORRUPTED;
6119 }
6120
6121 return error;
6122}